Repository navigation
feat(conversion): lower known Forth stack slots to SSA values - #151
Closed
tetsuo-cpp wants to merge 8 commits into
Closed
tetsuo-cpp wants to merge 8 commits into
tetsuo-cpp wants to merge 8 commits into
Conversation
Bench compare against
|
| 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
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
Bot
force-pushed
the
cursor/compile-time-stack-flatten-8692
branch
from
November 9, 2026 12:39
4cda6cd to
969a71a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-stackreplaces the memref stack pass and runs after inlining.PICKorROLL, or an unequal join, uses a 256-cell residual buffer.WordLoweringholds a word's effect and its compute.StackWordRowis the table entry.Scope
Tradeoffs
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-warpforthpassed 136 tests. clang-tidy and the include checker reported no findings.2 3 +at opt level 0 stores 5 withst.global.u64and no local stack depot.1 C IF 7 THENstores 7 or 1, and10 20 30 PICKloads the cell at index 1.