Skip to content

feat(conversion): lower known Forth stack slots to SSA values - #151

Closed
tetsuo-cpp wants to merge 8 commits into
canonfrom
cursor/compile-time-stack-flatten-8692
Closed

tetsuo-cpp wants to merge 8 commits into
canonfrom
cursor/compile-time-stack-flatten-8692

Conversation

@tetsuo-cpp

@tetsuo-cpp tetsuo-cpp commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

Why

Forth slots whose depth and identity are known at compile time should live in registers. The compiler now lowers those cells to SSA values and keeps a memory stack only where the depth or the slot is not static.

What changed

  • --lower-forth-stack replaces the memref stack pass and runs after inlining.
  • Equal depths stay in registers across branches and loops. A dynamic PICK or ROLL, or an unequal join, uses a 256-cell residual buffer.
  • WordLowering holds a word's effect and its compute. StackWordRow is the table entry.
  • Private words that nothing calls are erased, including an unused word whose only caller was another unused word.

Scope

  • Covers the stack representation from Forth through GPU lowering.
  • A static underflow still loads the empty slot, so programs that compiled before still compile.
  • Does not measure GPU time, and does not claim a speedup.

Tradeoffs

  • A hard error on static underflow was rejected so existing programs keep compiling.
  • A second calling convention for words was rejected. Words are inlined first, and a word that remains is an error.

Blast Radius

Every kernel's stack IR changes. A straight-line kernel no longer allocates the 256-cell buffer. Residual programs keep the old layout, with the first push at index 1.

Verification

  • check-warpforth passed 136 tests. clang-tidy and the include checker reported no findings.
  • 2 3 + at opt level 0 stores 5 with st.global.u64 and no local stack depot.
  • 1 C IF 7 THEN stores 7 or 1, and 10 20 30 PICK loads the cell at index 1.
  • An unused word that only calls another unused word compiles. At opt level 0 the kernel stores 5 and emits no device function.
Open in Web Open in Cursor 

@tetsuo-cpp tetsuo-cpp added the gpu-test Run GPU tests for this pull request label Nov 9, 2026
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown

Bench compare against canon: no case slower across 6 cases

case verdict ratio mad base head gap
scale_i64 no change 1.000 0.000 272.0 us 272.0 us 1.01x
axpy_f64 no change 1.000 0.000 402.7 us 402.7 us 1.01x
reduce_sum_f64 no change 1.000 0.000 450.3 us 450.3 us 1.64x
matmul_f64_naive no change 1.000 0.000 43.2 us 43.2 us 1.49x
matmul_f64_tiled no change 1.002 0.000 343.8 us 344.4 us 1.59x
attention_f32 no change 1.000 0.000 1191.4 us 1191.6 us 9.68x
  • Base: 5a239604c9262f265372be8b60b5ef02995c2ead
  • Head: 3362c0289eb87c498b5d3aabc34be13cdfc4170e
  • Rental: 5175f5db792f7713b2e92338940c9169, driver 13.2, SM clock 2460 MHz, memory clock 8501 MHz, sm_89

@github-actions github-actions Bot removed the gpu-test Run GPU tests for this pull request label Nov 9, 2026
The Forth dialect registered no inliner interface, so the inliner refused
any word that still held a Forth op after memref lowering. A word that
reads TID-X or calls BARRIER reached PTX as a .visible .func.

Compile-time stack flattening needs every word inlined into its kernel
before the stack is lowered, so the inliner moves ahead of that stage.
Its canonicalizer then deletes Forth code whose result is never stored.
optimization.forth and source-locations.forth inspected such code, so
they store their values.
The memref stack turns every cell into a store and a load through a
256-cell local buffer, even where the compiler knows the depth of every
cell. Once words are inlined, a straight-line kernel's stack is fully
known, so its cells can be plain i64 values.

lower-forth-stack does that for constants, parameter references, integer
arithmetic, and the global store. The stack-word table in StackWords.cpp
declares each word's stack effect and its compute, and the pass alone
decides where cells live. A function that uses any other word, has more
than one block, or pops below its known depth is left for
convert-forth-to-memref, which still runs after it until every word has
a row.
IF/ELSE and DO loops pass the stack between blocks as a block argument,
so the straight-line lowering stopped at the first branch. When every
path into a block agrees on the stack depth, the block can take the
cells themselves.

Shape inference now runs to a fixpoint over the blocks in reverse
post-order. A stack block argument whose incoming shapes agree becomes
one i64 argument per cell, and each branch forwards the cells. pop,
pop_flag, and push_value get rows, so the DO bounds, IF flags, and loop
index read cells directly. The DO counter stays in its memref<1xi64>.
A join whose incoming depths differ still goes to
convert-forth-to-memref.
… is known

A literal PICK or ROLL index names a register cell, so the word is just
another use of that value or a reordering of the cells. Only a runtime
index, a pop below the known depth, or a join of unequal depths needs
cells that can be addressed in memory.

Those cases now use a per-thread memref<256xi64> created on first use,
with the memref stack's layout: index 0 is empty, a push stores at depth
+ 1, and there is no overflow check or address space. A dynamic PICK
stores the live cells only as the image for its load and keeps them in
registers. A dynamic ROLL stores them, runs the same shift loop as
before, and reloads them. A pop the static depth cannot cover widens
the stack to the buffer and loads index 0, as before, so every program
that compiled still compiles. A join of unequal depths keeps the top
cells that every edge holds and takes the memory depth as an index
block argument.
Stack shuffles, float and bitwise arithmetic, comparisons, conversions,
loads and stores of every width, and the GPU index words now lower
through the stack-word table. Every kernel the parser emits takes the
new path and keeps its cells in registers wherever the depth is known,
and a shuffle becomes a reordering of SSA values with no ops at all.

The pass checks at initialization that every Forth op that takes a
stack has a row, so an op added to the dialect without one fails every
run instead of falling back to the memref stack.

The new tests reuse the inputs of the ForthToMemRef tests and check the
stored values, operand order, element and pointer types, and the block
argument types that loops carry.
Every Forth word now has a stack-word row, so no function falls through
to convert-forth-to-memref and that pass has no caller left. Deleting
it leaves --lower-forth-stack as the one place that decides where a
cell lives, and its fallback becomes an error for any op that uses a
stack without a row.

Inlining leaves the kernel as the only function, so ForthToGPU drops
the branch that moved word functions into the gpu.module and reports
any other function instead. The pipeline tests run --lower-forth-stack,
with --inline only where the program defines words, and the docs
describe the new stages.
WordLowering holds the effect and the compute. StackWordRow is the
table entry. A dynamic ROLL shifts cells in shiftCellsDown.
The inliner deletes an unused caller and leaves its private callee.
That leftover word still has a stack signature, so a program that
never calls it failed to compile.
@cursor
cursor Bot force-pushed the cursor/compile-time-stack-flatten-8692 branch from 4cda6cd to 969a71a Compare November 9, 2026 12:39
@tetsuo-cpp tetsuo-cpp closed this Nov 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant