Repository navigation
feat(conversion): lower a known Forth stack to SSA values - #152
Open
tetsuo-cpp wants to merge 6 commits into
Open
tetsuo-cpp wants to merge 6 commits into
tetsuo-cpp wants to merge 6 commits into
Conversation
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
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
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-depthruns first and only writesknown_depth. It does not change operands.convert-forth-to-memrefreads 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.Scope
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-warpforthpassed 145 of 145 tests.known-straight-line.mlirchecksarith.addiof 2 and 3 and the absence of the 256-cell stack buffer.stack-manipulation.mlirstill checks the memref sequences for dup, swap, rot, nip, over, tuck, pick, and roll.warpforthc --arch=sm_80 -O0on2 3 +emitsmov.b64of 5 andst.global.u64, with no local memory. The same kernel with0 PICK DROPappended, which forces the memory path, emits.local .align 8 .b8 __local_depot0[2048]. At the default opt level both emitmov.b64of 5 and no local memory.