Skip to content

feat(dialect): add warp shuffle words - #150

Merged
tetsuo-cpp merged 2 commits into
canonfrom
cursor/warp-shuffle-words-7f40
Nov 9, 2026
Merged

tetsuo-cpp merged 2 commits into
canonfrom
cursor/warp-shuffle-words-7f40

Conversation

@tetsuo-cpp

Copy link
Copy Markdown
Owner

Why

Forth kernels need to move one stack cell between lanes of a warp without a shared-memory round trip. Issue #10 asks for SHFL-DOWN, SHFL-UP, SHFL-XOR, SHFL-IDX, plus LANE-ID and WARP-SIZE.

What

forth.shuffle carries the mode. The memref pass emits gpu.shuffle of the i64 cell at width 32, and the selector is masked to the low five bits. convert-gpu-to-nvvm splits that into two shfl.sync instructions with mask 0xFFFFFFFF. LANE-ID is gpu.lane_id. WARP-SIZE is the constant 32.

A colon word that only shuffles inlines. The pipeline test pins the four modes, %laneid, and no leftover .func.

Benchmarks

The shared trees in reduce_sum_f64 and attention_f32 were rewritten to warp folds, compiled with the same warpforthc, and timed on one RTX 2070 (50 paired samples, harness noise floor 2%). Head is the shuffle fold, base is the tree.

Case Base Head Median ratio Verdict
reduce_sum_f64 1140.4 µs 1137.4 µs 0.997 no change
attention_f32 1188.4 µs 1221.9 µs 1.028 slower

Both rewrites matched the case reference. They are not in the tree. The reduce kernel reads 8 MiB in 1.14 ms, about 7.4 GB/s, so the grid-stride loop dominates the fold. Comments no longer say the word set has no warp shuffle.

Checked

  • check-warpforth: 142 tests.
  • check-clang-tidy after the dialect change.
  • On Vast.ai instance 59828130 (RTX 2070, driver 580.178.04), the new GPU tests passed, including an 8-thread block: whole-cell shuffles, both-half SHFL-IDX broadcast, partial-warp max and sum, block reduce at 64 and 256, and LANE-ID on a 16×4 block. The rental was destroyed on exit.

Open

gpu.shuffle is Pure, and the optimize window runs on gpu.func. No current pass moves a Pure op across a branch. A later one could hoist a shuffle out of a divergent region, which is undefined. The op itself stays trait-free so a Forth pass will not speculate it. The full mask on a partial warp was measured on this one GPU, where an 8-thread block returned the right cells.

Open in Web Open in Cursor 

SHFL-DOWN, SHFL-UP, SHFL-XOR, and SHFL-IDX move one i64 stack cell
across a warp. The memref pass emits gpu.shuffle of that cell at width
32, and the NVVM pass splits it into two shfl.sync instructions with a
full mask. LANE-ID reads %laneid. WARP-SIZE is the constant 32.
A shuffle fold of the same partials was correct and not faster. On one RTX
2070, 50 paired samples, noise floor 2%: reduce_sum_f64 moved 1140 us to
1137 us (no change), and attention_f32 moved 1188 us to 1222 us (slower).
The trees stay. Comments no longer say the word set has no warp shuffle.
@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.4 us 272.4 us 1.01x
axpy_f64 no change 1.000 0.000 403.1 us 403.1 us 1.01x
reduce_sum_f64 no change 1.000 0.000 448.5 us 448.5 us 1.64x
matmul_f64_naive no change 1.000 0.000 43.1 us 43.1 us 1.49x
matmul_f64_tiled no change 1.000 0.000 343.0 us 343.0 us 1.61x
attention_f32 no change 1.000 0.000 1189.5 us 1189.5 us 9.31x
  • Base: 5a239604c9262f265372be8b60b5ef02995c2ead
  • Head: 86c7e5e25ad5c77a98b298916ac73a2d6ebf7913
  • Rental: 6e076ac49014a41d64330330f0e48900, 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
@cursor
cursor Bot force-pushed the cursor/warp-shuffle-words-7f40 branch from b56e26f to f8ef969 Compare November 9, 2026 12:40
@tetsuo-cpp
tetsuo-cpp merged commit 0fedc7b into canon Nov 9, 2026
1 check passed
@tetsuo-cpp
tetsuo-cpp deleted the cursor/warp-shuffle-words-7f40 branch November 9, 2026 12:48
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