Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .claude/skills/verify-warpforth/features/staged-pipeline.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ A user debugging a kernel runs the compiler in stages: Forth to `forth`-dialect
## Sub-features

- `stage-translate` `warpforth-translate --forth-to-mlir` emits `forth` dialect ops.
- `stage-passes` `warpforth-opt --convert-forth-to-memref` and `--convert-forth-to-gpu` run single lowerings.
- `stage-passes` `warpforth-opt --inline --lower-forth-stack` and `--convert-forth-to-gpu` run single lowerings.
- `stage-pipeline` `warpforth-opt --warpforth-pipeline` lowers to a `gpu.binary`.
- `stage-options` pipeline options `chip`, `features`, `opt-level` (0-3), `compilation-target` (`llvm|isa|bin|fatbin`).
- `stage-ptx` `warpforth-translate --mlir-to-ptx` turns pipeline output into PTX.
Expand All @@ -22,7 +22,7 @@ Preconditions:
- Baseline preconditions hold. `K=.claude/skills/verify-warpforth/fixtures/smoke/matmul.forth`.

- **Translate.** Run `build/bin/warpforth-translate --forth-to-mlir $K`. Output contains `forth.` ops and a `func.func` for `main`; exit 0.
- **Single pass.** Pipe that into `build/bin/warpforth-opt --convert-forth-to-memref`. Output has `memref<256xi64>` and no `!forth.stack`.
- **Single pass.** Pipe that into `build/bin/warpforth-opt --inline --lower-forth-stack`. Output has no `!forth.stack` and no `memref<256xi64>` stack buffer; the loop header block takes its stack cells as `i64` arguments.
- **Full pipeline to PTX.** Run the three-tool pipe above. PTX on stdout with `.visible .entry main(`; compare with `build/bin/warpforthc $K` output (should match for the same arch).
- **Target options.** Run the `--pass-pipeline` form with `chip=sm_80`. The `gpu.binary` attribute shows `chip = "sm_80"` and `O = 3`; its embedded assembly has `.target sm_80`.
- **Evidence.** `$S/compile.sh $K sm_80` stores `kernel.mlir` and `lowered.mlir` for the same journey.
Expand Down
13 changes: 7 additions & 6 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ CMake.
- `include/warpforth/Dialect/Forth/ForthDialect.td` - Type definitions

**Implementation:**
- `lib/Conversion/ForthToMemRef/ForthToMemRef.cpp` - Stack to MemRef conversion patterns
- `lib/Conversion/ForthStack/StackWords.cpp` - Stack-word table: the stack effect and lowering of every Forth op that takes a stack
- `lib/Conversion/ForthStack/LowerForthStack.cpp` - Stack lowering to i64 SSA cells and a residual buffer
- `lib/Conversion/ForthToGPU/ForthToGPU.cpp` - GPU conversion logic
- `lib/Translation/ForthToMLIR/ForthToMLIR.cpp` - Forth parser and translator

Expand All @@ -50,7 +51,7 @@ request per line on stdin and writes one JSON response per line on stdout.
./build/bin/warpforth-translate --forth-to-mlir test/example.forth

# Run conversion passes
./build/bin/warpforth-opt --convert-forth-to-memref input.mlir
./build/bin/warpforth-opt --inline --lower-forth-stack input.mlir
./build/bin/warpforth-opt --convert-forth-to-gpu input.mlir

# Full pipeline to PTX
Expand Down Expand Up @@ -78,7 +79,7 @@ def Forth_NewOp : Forth_Op<"opname", [Pure]> {
}
```

Add corresponding conversion pattern in `lib/Conversion/ForthToMemRef/ForthToMemRef.cpp`.
Add a row for it in `lib/Conversion/ForthStack/StackWords.cpp` that declares its stack effect and lowers it on i64 cells. Until the row exists, `--lower-forth-stack` fails on every input.

## GPU Tests

Expand All @@ -102,10 +103,10 @@ uv run ruff format gpu_test/
- **Kernel Parameters**: Declared in the `\!` header. `\! kernel <name>` is required and must appear first. `\! param <name> i64[<N>]` becomes a `memref<Nxi64>` argument; `\! param <name> i64` becomes an `i64` argument. `\! param <name> f64[<N>]` becomes a `memref<Nxf64>` argument; `\! param <name> f64` becomes an `f64` argument (bitcast to i64 when pushed to stack). Using a param name in code emits `forth.param_ref` (arrays push address; scalars push value).
- **Shared Memory**: `\! shared <name> i64[<N>]` or `\! shared <name> f64[<N>]` declares GPU shared (workgroup) memory. Emits a tagged `memref.alloca` at kernel entry; ForthToGPU converts it to a `gpu.func` workgroup attribution. Using the shared name in code pushes its base address onto the stack. Use `S@`/`S!` for i64 or `SF@`/`SF!` for f64 shared accesses. Cannot be referenced inside word definitions.
- **Reduced-Width Memory**: `I8@ I16@ I32@` load a narrow integer, sign-extend to i64. `I8! I16! I32!` truncate i64 to narrow integer, store. `HF@ BF@ F32@` load a narrow float, extend to f64, bitcast to i64. `HF! BF! F32!` bitcast i64 to f64, truncate to narrow float, store. `S`-prefixed variants (`SI8@`, `SHF!`, etc.) use shared memory (address space 3).
- **Conversion**: `!forth.stack` → `memref<256xi64>` with explicit stack pointer
- **GPU**: Functions wrapped in `gpu.module`, `main` gets `gpu.kernel` attribute, configured with bare pointers for NVVM conversion
- **Stack Lowering**: After inlining, `--lower-forth-stack` turns each cell whose depth and slot are known at compile time into an i64 SSA value, and block arguments carry cells across branches and loops. A cell below the known depth, or one that a runtime `PICK`/`ROLL` index addresses, lives in a per-thread `memref<256xi64>` residual buffer.
- **GPU**: The kernel is wrapped in `gpu.module` and gets the `gpu.kernel` attribute, configured with bare pointers for NVVM conversion
- **Local Variables**: `{ a b c -- }` at the start of a word definition binds read-only locals. Pops values from the stack in reverse name order (c, b, a) using `forth.pop`, stores SSA values. Referencing a local emits `forth.push_value`. SSA values from the entry block dominate all control flow, so locals work across IF/ELSE/THEN, loops, etc. On GPU, locals map directly to registers.
- **User-defined Words**: Modeled as `func.func` with signature `(!forth.stack) -> !forth.stack`, called via `func.call`
- **User-defined Words**: Modeled as `func.func` with signature `(!forth.stack) -> !forth.stack`, called via `func.call`, and inlined into the kernel before the stack is lowered

## Conventions

Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ WarpForth compiles Forth through a series of MLIR dialect lowerings, each replac
| Stage | Pass | Description |
|-------|-------------|-------------|
| **Parsing** | `warpforth-translate --forth-to-mlir` | Parses Forth source into the `forth` dialect. The kernel is represented as a series of stack ops on an abstract `!forth.stack` type. |
| **Stack lowering** | `warpforth-opt --convert-forth-to-memref` | The abstract `!forth.stack` type is materialized as a `memref<256xi64>` buffer and `index` pair. Stack ops become explicit loads, stores, and pointer arithmetic. |
| **GPU wrapping** | `warpforth-opt --convert-forth-to-gpu` | Functions are wrapped in a `gpu.module`, the kernel entry point is marked as a `gpu.kernel` and GPU intrinsic words are lowered to `gpu` ops. |
| **Inlining** | `warpforth-opt --inline` | User-defined words are inlined into the kernel, so the kernel is the whole program when its stack is lowered. |
| **Stack lowering** | `warpforth-opt --lower-forth-stack` | Each stack cell whose depth and slot are known at compile time becomes an `i64` SSA value, and block arguments carry cells across branches and loops. A cell below the known depth, or one that a runtime `PICK` or `ROLL` index addresses, lives in a per-thread `memref<256xi64>` buffer. |
| **GPU wrapping** | `warpforth-opt --convert-forth-to-gpu` | The kernel is moved into a `gpu.module` and marked as a `gpu.kernel`, and GPU intrinsic words are lowered to `gpu` ops. |
| **NVVM/LLVM lowering** | Standard MLIR passes | GPU→NVVM, math→LLVM intrinsics and NVVM→LLVM. |
| **Code generation** | `warpforth-translate --mlir-to-ptx` | The GPU module is serialized to PTX assembly via LLVM's NVPTX backend. |

Expand Down
24 changes: 24 additions & 0 deletions include/warpforth/Conversion/ForthStack/LowerForthStack.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//===- LowerForthStack.h - Lower the Forth stack ----------------*- C++ -*-===//
//
// This file declares the pass that lowers the Forth data stack.
//
//===----------------------------------------------------------------------===//

#ifndef WARPFORTH_CONVERSION_FORTHSTACK_LOWERFORTHSTACK_H
#define WARPFORTH_CONVERSION_FORTHSTACK_LOWERFORTHSTACK_H

#include "mlir/Pass/Pass.h"
#include <memory>

namespace mlir {
namespace warpforth {

/// Creates the pass that lowers every !forth.stack value. Every user word must
/// already be inlined. A cell whose depth and slot are known at compile time
/// becomes an i64 SSA value.
std::unique_ptr<Pass> createLowerForthStackPass();

} // namespace warpforth
} // namespace mlir

#endif
26 changes: 0 additions & 26 deletions include/warpforth/Conversion/ForthToMemRef/ForthToMemRef.h

This file was deleted.

39 changes: 20 additions & 19 deletions include/warpforth/Conversion/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@

include "mlir/Pass/PassBase.td"

def ConvertForthToMemRef
: Pass<"convert-forth-to-memref", "mlir::ModuleOp"> {
let summary = "Convert Forth dialect to MemRef dialect";
def LowerForthStack : Pass<"lower-forth-stack", "mlir::ModuleOp"> {
let summary = "Lower the Forth stack to i64 SSA values";
let description = [{
This pass converts Forth dialect operations to MemRef dialect operations.

The abstract `!forth.stack` type is lowered to a concrete representation:
- Stack buffer: `memref<256xi64>` (fixed-size 256-element array)
- Stack pointer: `index` type (current top of stack position)

Forth operations are converted to corresponding memref load/store operations
with explicit stack pointer arithmetic.
Lowers every `!forth.stack` value. Every user word must already be
inlined, so run `--inline` first. A cell whose depth and slot are known at
compile time becomes an `i64` SSA value, and each word becomes the `arith`,
`memref`, or `llvm` ops that compute its result.

A cell below the known depth, or one that a runtime `PICK` or `ROLL` index
addresses, lives in a per-thread `memref<256xi64>`. Index 0 of that buffer
is empty, and a push stores at depth + 1.
}];

let constructor = "mlir::warpforth::createConvertForthToMemRefPass()";
let constructor = "mlir::warpforth::createLowerForthStackPass()";

let dependentDialects = ["mlir::memref::MemRefDialect",
"mlir::arith::ArithDialect",
"mlir::LLVM::LLVMDialect",
let dependentDialects = ["mlir::arith::ArithDialect",
"mlir::cf::ControlFlowDialect",
"mlir::math::MathDialect"];
"mlir::forth::ForthDialect",
"mlir::LLVM::LLVMDialect",
"mlir::math::MathDialect",
"mlir::memref::MemRefDialect"];
}

def ConvertForthToGPU : Pass<"convert-forth-to-gpu", "mlir::ModuleOp"> {
Expand All @@ -38,9 +38,10 @@ def ConvertForthToGPU : Pass<"convert-forth-to-gpu", "mlir::ModuleOp"> {
This pass converts Forth functions to the GPU dialect to prepare them for
GPU execution.

The pass wraps func.func operations in a gpu.module and converts them to
gpu.func operations. Functions named "main" receive the gpu.kernel
attribute, making them launchable from host code.
The pass moves the kernel, the func.func marked `forth.kernel`, into a
gpu.module as a gpu.func with the gpu.kernel attribute, making it
launchable from host code. Every Forth word must already be inlined, so
any other func.func is an error.

Additionally, the pass lowers forth.intrinsic operations to corresponding
GPU dialect operations (gpu.thread_id, gpu.block_id, gpu.block_dim,
Expand Down
4 changes: 2 additions & 2 deletions lib/Conversion/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ add_mlir_library(MLIRConversionPasses

LINK_LIBS PUBLIC
MLIRPass
MLIRForthToMemRefConversion
MLIRForthStackConversion
MLIRForthToGPUConversion
MLIRGPUDialect
MLIRGPUToNVVMTransforms
Expand All @@ -21,5 +21,5 @@ target_compile_definitions(obj.MLIRConversionPasses PRIVATE
WARPFORTH_LIBDEVICE_PATH="${WARPFORTH_LIBDEVICE_PATH}"
)

add_subdirectory(ForthToMemRef)
add_subdirectory(ForthStack)
add_subdirectory(ForthToGPU)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_mlir_conversion_library(MLIRForthToMemRefConversion
ForthToMemRef.cpp
add_mlir_conversion_library(MLIRForthStackConversion
LowerForthStack.cpp
StackWords.cpp

DEPENDS
MLIRConversionPassIncGen
Expand All @@ -8,12 +9,15 @@ add_mlir_conversion_library(MLIRForthToMemRefConversion
LINK_LIBS PUBLIC
MLIRIR
MLIRPass
MLIRTransformUtils
MLIRMemRefDialect
MLIRArithDialect
MLIRLLVMDialect
MLIRFuncDialect
MLIRControlFlowDialect
MLIRControlFlowInterfaces
MLIRDialectUtils
MLIRFuncDialect
MLIRLLVMDialect
MLIRMathDialect
MLIRMemRefDialect
MLIRNVVMDialect
MLIRTransformUtils
MLIRForth
)
Loading
Loading