Skip to content

feat(conversion): lower a known Forth stack to SSA values - #152

Open
tetsuo-cpp wants to merge 6 commits into
canonfrom
cursor/known-stack-vector-a11c
Open

tetsuo-cpp wants to merge 6 commits into
canonfrom
cursor/known-stack-vector-a11c

Conversation

@tetsuo-cpp

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

Copy link
Copy Markdown
Owner

Why

Known Forth stack cells should live in registers, and the existing memory lowering should stay for a stack whose depth the compiler cannot see. A compile-time vector of values covers the known case. The memory stack covers the rest.

What changed

  • tag-forth-stack-depth runs first and only writes known_depth. It does not change operands.
  • convert-forth-to-memref reads that attribute. Each word builds its own arith or memory op. A stack cursor stores the cell in the memref stack when the attribute is absent, and in a compile-time vector of SSA values when it is present.
  • The shared word-lookup table is gone.
  • A join where every path agrees on the depth takes one integer argument per cell. A call, a dynamic pick or roll, or an unequal branch leaves the whole function on the memref patterns.

Scope

  • Covers straight-line kernels and branches that meet again at the same depth.
  • Leaves out calls, dynamic stack indexing, do-loops, and any switch between the vector and the memory stack in the middle of one function.

Tradeoffs

One function uses one path. A single dynamic word sends that function to the memory stack, so the pass does not spill and reload mid-kernel. The inliner still runs after this lowering, so a kernel that calls another word keeps the memory stack.

Blast Radius

Kernels whose depth is uniform lose the 256-cell stack allocation in the IR. At the default opt level that allocation already disappears from PTX, so the PTX of 2 3 + matches the memory path. The split is visible in the IR and at -O0.

Verification

  • cmake --build build --target check-warpforth passed 145 of 145 tests. known-straight-line.mlir checks arith.addi of 2 and 3 and the absence of the 256-cell stack buffer. stack-manipulation.mlir still checks the memref sequences for dup, swap, rot, nip, over, tuck, pick, and roll.
  • warpforthc --arch=sm_80 -O0 on 2 3 + emits mov.b64 of 5 and st.global.u64, with no local memory. The same kernel with 0 PICK DROP appended, which forces the memory path, emits .local .align 8 .b8 __local_depot0[2048]. At the default opt level both emit mov.b64 of 5 and no local memory.
Open in Web Open in Cursor 

cursoragent and others added 6 commits November 9, 2026 15:00
A single-block function whose words all have fixed-window rows keeps
its cells in a SmallVector of SSA values. forth.constant, the integer
binary ops, and the global i64 store build their arith op or llvm.store
in one compute function, and the memref patterns call that same
function. A function with a join, a call, or a word that has no row
still uses the existing patterns.

The straight-line test stores 2 3 +, the sum 5, and has no data-stack
buffer. literal.mlir, arithmetic.mlir, and stack-init.mlir take the
same path, so their checks follow the new IR.

Co-authored-by: Alex Cameron <asc@tetsuo.sh>
Move the remaining fixed-window words onto lookupWord so the pattern
and the known-stack walk emit each arith, memory, or shuffle op once.
pick, roll, pop, pop_flag, and push_value stay on the patterns, and a
function that uses them keeps the memref stack. Straight-line functions
whose words are all in the table lower to SSA values.

Co-authored-by: Alex Cameron <asc@tetsuo.sh>
When every predecessor agrees on the stack depth, the join takes one
i64 argument per cell, deepest first. pop_flag's i1 is the compare of
that cell against zero, and the memref pattern calls the same compute.
A disagreement, a stack call, pick, roll, or a short pop leaves the
whole function on the existing patterns.

Co-authored-by: Alex Cameron <asc@tetsuo.sh>
The vector's top is its back, so push, drop, and top are operations.
A branch into the function entry is ineligible. Branch operands follow
the destination block's arguments. A word's compute must return one
value per push plus each non-stack result.

Co-authored-by: Alex Cameron <asc@tetsuo.sh>
Analysis writes known_depth on each op and stops there. Lowering reads
that attribute and emits the SSA cells. An op without the attribute
stays on the memref patterns.

Co-authored-by: Alex Cameron <asc@tetsuo.sh>
tag-forth-stack-depth writes known_depth and stops. Each Forth rewrite
emits its own op through a stack cursor, so the memref path and the SSA
path share one body. The word lookup table is gone.

Co-authored-by: Alex Cameron <asc@tetsuo.sh>

This branch has not been deployed

No deployments
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.

2 participants