Split wave64: masks read only lane by lane stay in their half #94

Manually merged
github merged 1 commit from gh-pr/94/head into gh-pr/94/base 2026-11-26 04:09:43 +00:00
Owner

@⁠hexgeon1 on GitHub · 2026-11-23 10:50 UTC · original
hexgeon1:wave64-local-masks → main · +898 −50 · 7 files · 1 commits · merged 2026-11-23 11:02 UTC

Split wave64: masks read only lane by lane are not exchanged

In the split wave64 form (#67), every mask write (v_cmp*, carry-outs) exchanges the two wave32 halves' ballots so that both hold the whole 64-bit mask. Many masks are never read whole: a v_cndmask_b32 selector or the carry-in of v_add_co_ci / v_sub(rev)_co_ci only reads the reading lane's own bit, which its own half already has. This PR finds those masks and gives them the half's own ballot word (xcl_x_ballot_local) instead of an exchange. That covers every 64-bit address add and every select. #67 lists this as its next step.

How it works

gpu/recomp/x64_masks.rs is a backward liveness pass over the SGPRs. A register is whole-live after an instruction when some path may read its whole value before it is written again. A mask written to registers that are not whole-live stays in its half. Both halves make the same static choice, so their exchange counts stay in step.

After a local mask the halves hold different values in those registers, so a missed whole read lets them diverge: wrong results, or exchanges that no longer pair up. For that reason the rules follow what the emitter does, not only the ISA operand tables:

  • What counts as an overwrite: a write counts only when the emitter replaces the whole register.
    • s_cmov* and s_cmovk don't: they write only when a condition holds.
    • s_addk, s_mulk and s_bitset* don't: they read the old value first.
    • s_movreld and s_movrelsd_2 don't: they write s[dst + m0].
    • An SDWA compare with SD = 1 doesn't overwrite VCC: it writes its SDST instead.
  • s_movrels*: reads every register from its base up to s123, VCC included.
  • Other whole reads:
    • SDWA scalar sources (S0 / S1);
    • SMEM register offsets;
    • src_vccz / src_execz;
    • M0;
    • an SGPR used as a VALU scalar operand, even when the same register is also the instruction's selector.
  • Treated as reading everything:
    • EXEC is always whole-live.
    • An indirect jump makes every SGPR whole-live. A far jump (s_getpc / s_add / s_setpc) counts as a plain branch.
    • The end of a kernel-mode program reads every SGPR, because the test runner reads them back and checks that both halves agree.
  • Loop exits through the iteration budget (LOOP_GUARD): a loop can be left where its budget is checked. That is at its top, or, in the pc dispatcher form, before every block, which ends the program. Whatever is live after the loop, or at the end, therefore counts as live before each instruction in the loop. The loop ranges come from the translator's own merge, now one function: loop_ranges() in recomp/mod.rs.
  • Always local: a mask with no destination (a VOP3 compare into NULL), or one that is 0 in every lane (the false mask of v_div_scale). Either way both halves hold the same value.
  • Geometry mode (ES → GS through s_setpc) has no liveness pass and exchanges every other mask as before.

XCL_X64_ALL_MASKS=1 exchanges every mask, for comparison. On RDNA, XCL_SPLIT_WAVE64=1 (ba905cf) runs the split form there too.

Cost

bench_split_wave64 (#67's benchmark) now does two things differently:

  • It counts the exchanges each loop iteration makes, from the generated HLSL.
  • It times GPU work only. Each dispatch sits between two GPU timestamps, using the median of 9 dispatches recorded in one command list (new runner::dispatch_timed). Before, it took the wall-clock time of a whole runner::dispatch: pipeline creation, buffer creation, uploads, submission and the CPU's wait. That overhead is the same for wave32 and wave64, so it pulled every ratio towards 1; #67's 5070 Ti figures were measured that way.

Measured on an RTX 3090 (driver 617.14): three runs with XCL_X64_ALL_MASKS=1 and five with the default. Each figure is wave64 split GPU time ÷ wave32 GPU time:

mask only mask + v_readfirstlane
exchanges per iteration (all masks / default) 1 / 0 2 / 1
one console wave per group (barrier) 1.64–1.68× → 1.04–1.11× 1.99–2.02× → 1.74–1.81×
four console waves per group (counter-paired) 3.90–4.14× → 1.07–1.16× 6.45–6.64× → 4.09–4.34×

The wave32 baseline is 11.4–14.1 ms. The 4–16 % that remains with no exchange at all is the rest of the split form (64-bit masks, assigning console threads at the start); I haven't measured it further.

What the benchmark does and doesn't show: its loop mask is exactly the kind this pass keeps local, so the result is close to guaranteed by construction. It shows that the exchange is gone and what an exchange costs, which matters most in the counter-paired form. It doesn't show how much real shaders gain: that depends on how many of their masks are read only lane by lane (see Not checked). It is also one synthetic loop on one GPU.

Still exchanged: readlane / readfirstlane, ds_bpermute, coherent scalar loads, and masks read whole.

Tests

  • x64_masks::tests, 16 unit tests:

    • lane-by-lane reads: a select, a carry chain, a mask overwritten before any whole read;
    • whole reads: a whole read on one path only or around a loop, scalar reads and branches, a selector that is also a scalar source;
    • partial writes and hidden reads: one test per rule above (SDWA SD = 1, read-modify-write SALU, s_movrels / s_movreld / s_movrelsd_2, SMEM offset, SDWA S0 / S1);
    • control flow: far jumps, and loops left through the budget (inside the loop, before its head, and the dispatcher exit to the end);
    • EXEC.
  • gpu_split_wave64_lane_local_masks: a select and a 64-bit carry chain whose masks have bits in both halves, checked lane by lane, plus a check that all four masks are emitted local. Putting the local word in the wrong half makes it fail.

  • gpu_split_wave64_masks_read_whole_after_partial_writes:

    • six small kernels, one per partial-write or hidden-read rule;
    • checks the results and that each mask is still exchanged, while the runner checks both halves end with the same SGPRs;
    • also checks that a compare into NULL is not exchanged.

    Without the rules it fails with "the halves ended with different SGPRs".

  • gpu_split_wave64_local_and_exchanged_masks_in_diverging_waves: four console waves per group (256 and 8 × 8 × 4), so the counter-paired form.

    • The waves loop a different number of times.
    • Each iteration writes a mask that only odd waves read whole, on their own path, plus a 64-bit carry chain.
    • The test checks 1 exchanged and 2 local masks, and every thread's results.

The new unit tests for the partial-write rules and for the loop exits fail without their fixes. I checked this by running them against earlier versions of the pass.

Workspace, release, RTX 3090 on Windows 10 with the D3D12 Agility SDK 1.616.1, on 697dbb1:

  • All tests: 459 passed, 0 failed, 36 ignored. Skipped as no wave64: gpu_wave32_program_in_wave64_hardware and the native half of gpu_geometry_wave32_prefix_count_over_a_mask.
  • Spin mode: with XCL_WAVE64_SPIN=1, the gpu tests give 224 passed, 0 failed.

Other open PRs

  • #91 (s_round_mode / s_setreg*, now on main): its s_setreg_b32 only reads its SGPR, and the pass counts that read as whole, so a mask feeding it is still exchanged.
  • #83 gets one more conflict, in recomp/mod.rs next to loop_ranges(). It is textual only; #83 already conflicts with main in three other files.
  • #33 already conflicts with main in five files; this PR adds none.

Not checked

  • Real compute shaders: there are no compute-heavy shader packs on this machine. Minecraft's 273 wave64 programs, translated as compute or mesh, have 38 mask writes, all only read lane by lane (XCL_X64_ALL_MASKS=1: 38 exchanged).
  • AMD: not run there, including with XCL_SPLIT_WAVE64=1.
  • Native wave64 HLSL is unchanged by construction: the pass, cur_pc (emits nothing) and xcl_x_ballot_local only take effect in the split form. It was not dumped and compared.

🤖 Generated with Claude Code

**@⁠hexgeon1** on GitHub · 2026-11-23 10:50 UTC · [original](https://github.com/xcl-dev/xcl/pull/94) `hexgeon1:wave64-local-masks` → `main` · +898 −50 · 7 files · 1 commits · merged 2026-11-23 11:02 UTC ## Split wave64: masks read only lane by lane are not exchanged In the split wave64 form (#67), every mask write (`v_cmp*`, carry-outs) exchanges the two wave32 halves' ballots so that both hold the whole 64-bit mask. Many masks are never read whole: a `v_cndmask_b32` selector or the carry-in of `v_add_co_ci` / `v_sub(rev)_co_ci` only reads the reading lane's own bit, which its own half already has. This PR finds those masks and gives them the half's own ballot word (`xcl_x_ballot_local`) instead of an exchange. That covers every 64-bit address add and every select. #67 lists this as its next step. ### How it works `gpu/recomp/x64_masks.rs` is a backward liveness pass over the SGPRs. A register is *whole-live* after an instruction when some path may read its whole value before it is written again. A mask written to registers that are not whole-live stays in its half. Both halves make the same static choice, so their exchange counts stay in step. After a local mask the halves hold different values in those registers, so a missed whole read lets them diverge: wrong results, or exchanges that no longer pair up. For that reason the rules follow what the emitter does, not only the ISA operand tables: - **What counts as an overwrite:** a write counts only when the emitter replaces the whole register. - `s_cmov*` and `s_cmovk` don't: they write only when a condition holds. - `s_addk`, `s_mulk` and `s_bitset*` don't: they read the old value first. - `s_movreld` and `s_movrelsd_2` don't: they write `s[dst + m0]`. - An SDWA compare with SD = 1 doesn't overwrite VCC: it writes its SDST instead. - **`s_movrels*`:** reads every register from its base up to s123, VCC included. - **Other whole reads:** - SDWA scalar sources (S0 / S1); - SMEM register offsets; - `src_vccz` / `src_execz`; - M0; - an SGPR used as a VALU scalar operand, even when the same register is also the instruction's selector. - **Treated as reading everything:** - EXEC is always whole-live. - An indirect jump makes every SGPR whole-live. A far jump (`s_getpc` / `s_add` / `s_setpc`) counts as a plain branch. - The end of a kernel-mode program reads every SGPR, because the test runner reads them back and checks that both halves agree. - **Loop exits through the iteration budget (`LOOP_GUARD`):** a loop can be left where its budget is checked. That is at its top, or, in the `pc` dispatcher form, before every block, which ends the program. Whatever is live after the loop, or at the end, therefore counts as live before each instruction in the loop. The loop ranges come from the translator's own merge, now one function: `loop_ranges()` in `recomp/mod.rs`. - **Always local:** a mask with no destination (a VOP3 compare into NULL), or one that is 0 in every lane (the `false` mask of `v_div_scale`). Either way both halves hold the same value. - **Geometry mode** (ES → GS through `s_setpc`) has no liveness pass and exchanges every other mask as before. `XCL_X64_ALL_MASKS=1` exchanges every mask, for comparison. On RDNA, `XCL_SPLIT_WAVE64=1` (ba905cf) runs the split form there too. ### Cost `bench_split_wave64` (#67's benchmark) now does two things differently: - **It counts the exchanges** each loop iteration makes, from the generated HLSL. - **It times GPU work only.** Each dispatch sits between two GPU timestamps, using the median of 9 dispatches recorded in one command list (new `runner::dispatch_timed`). Before, it took the wall-clock time of a whole `runner::dispatch`: pipeline creation, buffer creation, uploads, submission and the CPU's wait. That overhead is the same for wave32 and wave64, so it pulled every ratio towards 1; #67's 5070 Ti figures were measured that way. Measured on an RTX 3090 (driver 617.14): three runs with `XCL_X64_ALL_MASKS=1` and five with the default. Each figure is wave64 split GPU time ÷ wave32 GPU time: | | mask only | mask + `v_readfirstlane` | |---|---|---| | exchanges per iteration (all masks / default) | 1 / 0 | 2 / 1 | | one console wave per group (barrier) | 1.64–1.68× → 1.04–1.11× | 1.99–2.02× → 1.74–1.81× | | four console waves per group (counter-paired) | 3.90–4.14× → 1.07–1.16× | 6.45–6.64× → 4.09–4.34× | The wave32 baseline is 11.4–14.1 ms. The 4–16 % that remains with no exchange at all is the rest of the split form (64-bit masks, assigning console threads at the start); I haven't measured it further. **What the benchmark does and doesn't show:** its loop mask is exactly the kind this pass keeps local, so the result is close to guaranteed by construction. It shows that the exchange is gone and what an exchange costs, which matters most in the counter-paired form. It doesn't show how much real shaders gain: that depends on how many of their masks are read only lane by lane (see *Not checked*). It is also one synthetic loop on one GPU. Still exchanged: readlane / readfirstlane, `ds_bpermute`, coherent scalar loads, and masks read whole. ### Tests - **`x64_masks::tests`, 16 unit tests:** - **lane-by-lane reads:** a select, a carry chain, a mask overwritten before any whole read; - **whole reads:** a whole read on one path only or around a loop, scalar reads and branches, a selector that is also a scalar source; - **partial writes and hidden reads:** one test per rule above (SDWA SD = 1, read-modify-write SALU, `s_movrels` / `s_movreld` / `s_movrelsd_2`, SMEM offset, SDWA S0 / S1); - **control flow:** far jumps, and loops left through the budget (inside the loop, before its head, and the dispatcher exit to the end); - **EXEC.** - **`gpu_split_wave64_lane_local_masks`:** a select and a 64-bit carry chain whose masks have bits in both halves, checked lane by lane, plus a check that all four masks are emitted local. Putting the local word in the wrong half makes it fail. - **`gpu_split_wave64_masks_read_whole_after_partial_writes`:** - six small kernels, one per partial-write or hidden-read rule; - checks the results and that each mask is still exchanged, while the runner checks both halves end with the same SGPRs; - also checks that a compare into NULL is not exchanged. Without the rules it fails with "the halves ended with different SGPRs". - **`gpu_split_wave64_local_and_exchanged_masks_in_diverging_waves`:** four console waves per group (256 and 8 × 8 × 4), so the counter-paired form. - The waves loop a different number of times. - Each iteration writes a mask that only odd waves read whole, on their own path, plus a 64-bit carry chain. - The test checks 1 exchanged and 2 local masks, and every thread's results. The new unit tests for the partial-write rules and for the loop exits fail without their fixes. I checked this by running them against earlier versions of the pass. Workspace, release, RTX 3090 on Windows 10 with the D3D12 Agility SDK 1.616.1, on `697dbb1`: - **All tests:** 459 passed, 0 failed, 36 ignored. Skipped as `no wave64`: `gpu_wave32_program_in_wave64_hardware` and the native half of `gpu_geometry_wave32_prefix_count_over_a_mask`. - **Spin mode:** with `XCL_WAVE64_SPIN=1`, the `gpu` tests give 224 passed, 0 failed. ### Other open PRs - **#91** (`s_round_mode` / `s_setreg*`, now on `main`): its `s_setreg_b32` only reads its SGPR, and the pass counts that read as whole, so a mask feeding it is still exchanged. - **#83** gets one more conflict, in `recomp/mod.rs` next to `loop_ranges()`. It is textual only; #83 already conflicts with `main` in three other files. - **#33** already conflicts with `main` in five files; this PR adds none. ### Not checked - **Real compute shaders:** there are no compute-heavy shader packs on this machine. Minecraft's 273 wave64 programs, translated as compute or mesh, have 38 mask writes, all only read lane by lane (`XCL_X64_ALL_MASKS=1`: 38 exchanged). - **AMD:** not run there, including with `XCL_SPLIT_WAVE64=1`. - **Native wave64 HLSL** is unchanged by construction: the pass, `cur_pc` (emits nothing) and `xcl_x_ballot_local` only take effect in the split form. It was not dumped and compared. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
In the split wave64 form (#67) every mask write exchanges the two wave32
halves' ballots. A mask whose readers take only the reading lane's own bit
(v_cndmask_b32's selector, the carry-in of v_add_co_ci / v_sub(rev)_co_ci)
needs no exchange: xcl_x_ballot_local gives the half's own word.

x64_masks.rs finds them with a backward liveness pass over the SGPRs. A
register is whole-live when some path may read its whole value before it is
written again. The rules follow the emitter, not only the operand tables:
s_cmov*, s_addk, s_mulk, s_bitset*, s_movreld and SDWA compares into their
SDST are not overwrites; s_movrels reads every register from its base; SDWA
scalar sources and SMEM register offsets are reads; EXEC, indirect jumps, the
kernel runner's end and loop exits through LOOP_GUARD count as whole reads
(loop ranges from the translator's loop_ranges, now shared). Masks into NULL
or constant false are never exchanged. Geometry mode keeps its exchanges.
XCL_X64_ALL_MASKS=1 exchanges every mask, for comparison.

bench_split_wave64 now counts each iteration's exchanges and times GPU work
only (runner::dispatch_timed: timestamps, median of 9; before, the wall time
of a whole dispatch with pipeline creation and copies). RTX 3090, all masks
(3 runs) vs default (5 runs), split / wave32:
- mask only: 1.64-1.68x -> 1.04-1.11x (one console wave per group),
  3.90-4.14x -> 1.07-1.16x (four, counter-paired)
- with a v_readfirstlane: 1.99-2.02x -> 1.74-1.81x, 6.45-6.64x -> 4.09-4.34x
The benchmark's mask is exactly the kind the pass keeps local: it shows the
exchange gone, not how much real shaders gain.

Tests: 16 for the analysis, gpu_split_wave64_lane_local_masks,
gpu_split_wave64_masks_read_whole_after_partial_writes (one kernel per rule;
the halves' SGPRs differ without it) and
gpu_split_wave64_local_and_exchanged_masks_in_diverging_waves (counter-paired,
waves on different paths). Workspace, release, RTX 3090: 459 passed, 0 failed;
XCL_WAVE64_SPIN=1: 224 passed. Not checked: real compute shaders (Minecraft's
273 wave64 programs as compute have 38 mask writes, all local), AMD.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:09:40 +00:00
github manually merged commit b4019c41fd into gh-pr/94/base 2026-11-26 04:09:43 +00:00
Author
Owner

@⁠ivall on GitHub · 2026-11-23 11:03 UTC · original

Merged – thanks, this is the step #67 named. Checked on AMD, which you couldn't: Checked on Windows 11 with an RX 9070 (RDNA4, MSVC build): cargo test --workspace green with the retail samples (shader pack, console DLLs, texture archive), natively and with XCL_SPLIT_WAVE64=1.

Real compute shaders in the split form: XDefiant run on the RX 9070 with XCL_SPLIT_WAVE64=1, so every wave64 compute and mesh program of the title goes through the split form with your local masks – its title screen renders right, no translation errors, no device removal. (On the first, cold-cache run the UI stayed incomplete until a restart; that is XCL's known cold-cache issue with pipelines recorded behind a stand-in, also seen without the split form.)

**@⁠ivall** on GitHub · 2026-11-23 11:03 UTC · [original](https://github.com/xcl-dev/xcl/pull/94#issuecomment-7098953670) Merged – thanks, this is the step #67 named. Checked on AMD, which you couldn't: Checked on Windows 11 with an RX 9070 (RDNA4, MSVC build): `cargo test --workspace` green with the retail samples (shader pack, console DLLs, texture archive), natively and with `XCL_SPLIT_WAVE64=1`. Real compute shaders in the split form: XDefiant run on the RX 9070 with `XCL_SPLIT_WAVE64=1`, so every wave64 compute and mesh program of the title goes through the split form with your local masks – its title screen renders right, no translation errors, no device removal. (On the first, cold-cache run the UI stayed incomplete until a restart; that is XCL's known cold-cache issue with pipelines recorded behind a stand-in, also seen without the split form.)
This discussion has been locked. Commenting is limited to contributors.
No description provided.