Skip to content

Recompiler: a split-wave64 bitwise mask tree exchanges once, at its end - #1597

Open
MiguelMachado-dev wants to merge 3 commits into
xcl-dev:mainfrom
MiguelMachado-dev:recomp/x64-mask-coalesce
Open

MiguelMachado-dev wants to merge 3 commits into
xcl-dev:mainfrom
MiguelMachado-dev:recomp/x64-mask-coalesce

Conversation

@MiguelMachado-dev

@MiguelMachado-dev MiguelMachado-dev commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #1596, which is stacked on #1111; only the top commit (6acbfb1, +328 / −9 with its tests) belongs here. The diff against main also carries those two PRs, which is why it is over 800 lines. Part of #1269.

Compares that 64-bit bitwise ops combine into a mask that is read whole (s_bcnt1, a saveexec, a live scc, …) each exchanged the two halves' ballots. The combination is lane-wise, so only its result needs both halves. Within one basic block, x64_masks now follows such trees. When two or more compares or carries feed a tree of lane-wise s_*_b64 ops (not s_cmov / s_cselect) and nothing else reads them, the compares keep the local ballot. The tree's last op exchanges its result once, with xcl_x_sync_mask, before scc is derived from it.

A block ends after every branch, conditional or not, and wherever a jump lands. Ending one only at branch targets would be unsafe: a tree on the fall-through side could make local a compare that the taken side of a conditional branch reads whole. LOOP_GUARD runs only at block starts.

What is in it

  • crates/xcl/src/gpu/recomp/x64_masks.rs: the liveness solution is shared (solve), and analyze returns Plan { whole_live, sync }. by_pc is gone; its one caller now uses analyze.
  • crates/xcl/src/gpu/recomp/emit.rs: x64_sync; salu64 exchanges the result at those pcs; the xcl_x_sync_mask helper. In a partial wave with no other half, the helper keeps the word this half computed for the missing one.
  • crates/xcl/src/gpu/recomp/mod.rs: uses analyze.
  • Tests:
    • unit tests a_bitwise_tree_read_whole_exchanges_once_at_its_end and a_compare_read_whole_outside_the_tree_keeps_its_exchange. The latter covers a compare that is read whole after the tree, copied to a whole-live pair, or read on the taken side of a conditional branch. With the old block rule (blocks split only at branch targets), the branch case fails.
    • GPU test gpu_split_wave64_bitwise_mask_tree_exchanges_once: s_bcnt1 of an s_and_b64 of two compares, and scc of an s_and_b64 whose match is in the upper half only. 4 local compares, 2 syncs, and whole-wave results in both halves.
    • GPU test gpu_split_wave64_bitwise_mask_tree_in_a_partial_wave: a wave64 group of 32 threads, so there is no upper half. It runs s_nor_b64 of two compares, read by s_bcnt1 and by scc. The compares' bits in the missing half are 0, but s_nor_b64 sets them, and the helper used to replace that word with 0: s_bcnt1 gave 16 instead of 48. @MatyEjsr found this and reproduced it on an RTX 3090 (comment on this PR).
    • gpu_split_wave64_bitwise_lane_masks (Recompiler: propagate split-wave64 lane-local masks through 64-bit bitwise SALU ops #1111) expected the two compares under a live scc to exchange. They now stay local and the s_and_b64 exchanges; the scc result it checks is unchanged.

Measured with the same temporary local test as the PR below this one (all wave64 CS / MS programs of the retail 1.0.2.1 shaders.rpf, split form, 4 windows). An exchange here is a mask write that uses xcl_x_ballot, or an xcl_x_sync_mask:

commit exchanged mask writes tree syncs exchanges
main 9e3831e 210 324 0 210 324
PR below 524b25c 153 728 0 153 728
this PR b71f7ee 127 965 10 473 138 438 (−10.0 % on 524b25c, −34.2 % on main)

HLSL over the 10 490 programs: 2 648.5 MB → 2 652.9 MB (+0.17 %).

These numbers were measured at b71f7ee. 6acbfb1 changes only what xcl_x_sync_mask returns when there is no other half, not where the exchanges go; they were not measured again.

docs/wave64-on-wave32.md:41 says a mask skips the exchange only when it "is only read lane by lane". After this change that is no longer the whole rule. The doc is not edited here.

Tested

  • NVIDIA GeForce RTX 5080 (32-lane waves, so the split form), driver 621.24; Intel Core i7-14700KF, 64 GB; Windows 11 build 26300; rustc 1.97.1; DXC 1.8.2502.11; debug for tests, release for the smoke run.
  • cargo build --workspace && cargo test --workspace -- --test-threads=4 at 6acbfb1: 789 passed, 0 failed, 41 ignored.
  • gpu_split_wave64_bitwise_mask_tree_in_a_partial_wave without the xcl_x_sync_mask fix: failed (s_bcnt1 16, expected 48). With it, cargo test -p xcl --lib gpu_split_wave64 -- --test-threads=1: 9 passed, 0 failed.
  • SKIPPED: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no wave64), heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).

Regression run
Each block is the second of two back-to-back 60 s runs of the same build (the first one filled the shader and driver caches).

main:

xcl-smoke v1
commit: 9e3831e1f570d3646e53ab753b19ec07eb8f1485
gpu: NVIDIA GeForce RTX 5080
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1617 (PresentX 1617)
presents per 2 s (last 3): 60 60 59
device removals: 0
crashes: 0
peak commit: 15047 MB

PR:

xcl-smoke v1
commit: 6acbfb1f4d57e5d914161dde56faaf28da18f973
gpu: NVIDIA GeForce RTX 5080
title: 3c290c16
seconds: 60
exit: 0x0
frames shown: 1612 (PresentX 1612)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14621 MB

Not tested

  • AMD and native wave64 GPUs, where the split form is not used.
  • DXC and driver compile times before and after; only the exchange counts were measured.
  • Trees across basic blocks, which are left exchanged.
  • Whether the smoke run reaches the changed code: in 60 s it built 26 compute pipelines from translated console CS, and the log does not say how many of them are wave64 programs in the split form. The exchange counts above cover the whole pack.

AI use

  • Claude Code, Claude Opus 5.5: wrote the code, the tests, the measurement script and this description, and ran the tests, the smoke runs and the measurement on the hardware above.

Zondybtw and others added 2 commits November 24, 2026 14:46
x64_masks kept EXEC whole-live everywhere, so every v_cmpx and every
s_*_saveexec_b64 / s_*_wrexec_b64 exchanged the two halves' masks, and so
did the compares feeding them. Every VALU reads EXEC lane by lane (the
emitter's lanebit), and DPP, ds_swizzle and v_permlane(x)16 read it for a
source lane in the reader's own half, so EXEC needs the whole value only
where the program reads it whole: s_cbranch_exec(n)z, src_execz, a scalar
operand, v_readfirstlane (first active lane) and ds_(b)permute (lanes of
the other half). The last three are not in the operand tables and are now
added in uses().

EXEC is now a mask like the others in the liveness: no longer live at
every instruction or at the end of a compute program (the kernel runner
still reads every SGPR at the end). An even-aligned saveexec / wrexec
needs whole inputs only when the new EXEC or its scc is read whole, and
whole EXEC alone when only the saved old EXEC is; odd pairs keep reading
their operands whole. The emitter lets EXEC (s126) take the local ballot.

Three tests from the bitwise-mask change relied on EXEC being always
live; they now read EXEC whole after the write.

Assisted-by: Claude Code (Claude Opus 5.5)
@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Read b71f7ee against the ISA. The split-form side holds up:

  • Which ops can stay local: lane_wise_salu64 has only ops whose two dwords don't mix: not/and/or/xor/andn2/orn2/nand/nor/xnor, and s_wqm_b64 (its 4-lane quads never straddle lane 32). Shifts, s_brev_b64, s_bfe_*64, s_bitreplicate, s_ff*/s_bcnt* aren't in it, which is right, since they move or count bits across the boundary.
  • The halves stay in step: both halves run the same instruction stream, so the tree's last op exchanges in both at the same point, and the exchange count stays matched.

One wording point: the description says s_cmov / s_cselect are not tree ops, but lane_wise_salu64 classes them with s_mov_b64 (lane-wise, no scc write). That is only safe while the scc they read is the same in both halves, i.e. never derived from a local, unsynced tree value. The rule that a live scc makes the op whole-live seems to give that. If so, a comment there saying it is the guarantee would help the next reader.

I haven't run it (the RTX 5080 run covers the 32-lane case).

AI use: Claude Code (Claude Opus 5.5) read the change and wrote this.

@futurepro62

Copy link
Copy Markdown

Full-suite run, since this PR has no test report yet. Tested exact head b71f7ee (on its base 9e3831e).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, DXC + dxil.dll 1.9.2602.17 (Windows SDK 10.0.26100), debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4 --skip heap_window_out_of_memory_falls_back_to_the_switch_form: 787 passed, 0 failed, 41 ignored. SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.
  • The new tests ran and passed: gpu_split_wave64_bitwise_lane_masks, gpu_split_wave64_predicated_exec_masks, gpu_split_wave64_bitwise_mask_tree_exchanges_once, saveexec_needs_whole_inputs_only_for_a_whole_read, a_bitwise_tree_read_whole_exchanges_once_at_its_end, a_compare_read_whole_outside_the_tree_keeps_its_exchange, bitwise_mask_chain_for_selector_needs_no_exchange, bitwise_mask_with_live_scc_keeps_exchange, bitwise_mask_feeding_whole_read_keeps_exchange, cselect_and_cmov_b64_propagate_lane_masks_and_keep_scc_uniform, vccz_operand_and_odd_alignment_in_bitwise_ops_keep_exchange, loop_exit_reading_scc_keeps_exchange_inside_loop.
  • Same head on the AMD Radeon integrated GPU (iGPU) of the Ryzen 7 9800X3D (RDNA2, XCL_ADAPTER=radeon), cargo test --workspace -- --test-threads=4: 788 passed, 0 failed, 41 ignored. SKIPPED: heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • heap_window_out_of_memory_falls_back_to_the_switch_form was skipped with --skip. It fails on main 9e3831e too, not because of this PR: under XCL_REQUIRE_GPU=mesh, runner::skip_test turns every skip except "no wave64" into a failure, including this test's opt-in "no heap windows" skip (XCL_HEAP_WINDOWS unset). Reported as Tests: heap_window_out_of_memory_falls_back_to_the_switch_form fails under XCL_REQUIRE_GPU=mesh (opt-in skip counted as missing coverage) #1651.

Not tested

  • a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.

@ScaryFr92

Copy link
Copy Markdown

Tested #1597 at b71f7ee (with #1596 and #1111 below it) on Ampere: 788 passed, 0 failed, 41 ignored; the 8 split-wave64 GPU tests pass with XCL_REQUIRE_GPU=wave64.

Tested

  • NVIDIA GeForce RTX 3070 8 GB (Ampere, 32-lane waves, so the split form), driver 32.0.16.1088; Intel Core i7-10700K, 32 GB; Windows 11 Pro build 26200; Rust 1.100.0 x86_64-pc-windows-msvc; DXC 1.9.2602.17 (Windows SDK 10.0.26100); debug build, clean tree at b71f7ee.
  • cargo build --workspace && cargo test --workspace -- --test-threads=4: 788 passed, 0 failed, 41 ignored (same counts as the RTX 5080 run in the description, which used DXC 1.8).
  • SKIPPED: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no wave64), heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows).
  • With XCL_REQUIRE_GPU=wave64: cargo test -p xcl --lib gpu::recomp::tests::gpu_split_wave64 -- --test-threads=1: 8 passed, 0 failed, 0 skipped, including gpu_split_wave64_bitwise_mask_tree_exchanges_once and gpu_split_wave64_predicated_exec_masks.

Not tested

  • No retail title: no smoke run, no exchange counts on a shader pack, no performance numbers.
  • AMD, and NVIDIA generations other than Ampere.

AI use

  • Claude Code (Claude Opus 5.5) ran the build and the tests and drafted this report.

@MatyEjsr

Copy link
Copy Markdown

Tested PR #1597 at commit b71f7eed783cae297304712545262102899ae96c on NVIDIA GeForce RTX 3090 (split-wave64) and reviewed x64_masks.rs + emit.rs.

Environment

  • OS: Windows 11 Pro for Workstations Build 26200 (10.0.26200)
  • CPU: AMD Ryzen 7 7800X3D (8C/8T)
  • GPU: NVIDIA GeForce RTX 3090, driver 617.42 (32.0.16.1742)
  • RAM: 31.4 GB
  • Toolchain: rustc 1.100.0 (debug build), DXC 1.9.2602.17 (21d28f72)

Tested

  • cargo build --workspace -j 8: ok (10.36s)
  • cargo test --workspace -j 8 -- --test-threads=8 --nocapture: 788 passed (783 executed + 5 SKIPPED), 0 failed, 41 ignored
    • SKIPPED: gpu_geometry_wave32_prefix_count_over_a_mask (no wave64), gpu_wave32_program_in_wave64_hardware (no wave64), test_title_draws_with_a_root_signature_under_the_debug_layer (no d3d12SDKLayers.dll), heap_window_out_of_memory_falls_back_to_the_switch_form (no heap windows), uploads_start_from_each_subresources_state (no D3D12 debug layer)
  • cargo test -p xcl --lib x64_masks -- --test-threads=8: 25 passed, 0 failed (0.00s)
  • cargo test -p xcl --lib gpu_split_wave64 -- --test-threads=8 --nocapture: 8 passed, 0 failed (0.35s)

Review finding (bug confirmed on GPU): xcl_x_sync_mask zeroes the upper 32-bit word on unpaired partial waves (xcl_xn == 0u)
In crates/xcl/src/gpu/recomp/emit.rs (xcl_x_sync_mask):

uint2 xcl_x_sync_mask(uint lo, uint hi) {
    uint m = select(xcl_half != 0u, hi, lo);
    xcl_x_put(0u, m);
    uint o = xcl_x_meet() ? xcl_x_get(0u) : 0u;
    return xcl_half != 0u ? uint2(o, m) : uint2(m, o);
}

When a wave64 compute dispatch has an unpaired lower half (xcl_xn == 0u, e.g., [32, 1, 1] or the last wave of [96, 1, 1]), xcl_x_meet() returns false, so o = 0u clobbers hi. Because xcl_x_ballot_local already places 0u in the absent upper half's word, any inverting op in the tree (s_not_b64, s_nor_b64, s_nand_b64, s_xnor_b64, s_orn2_b64) or any tree combined with a uniform/scalar mask that has non-zero upper bits (s_or_b64, s_xor_b64) already computed the exact upper 32-bit word in hi, and xcl_x_sync_mask overwrites it with 0u (which also corrupts scc and s_bcnt1_i32_b64).

Verified on RTX 3090 with a [32, 1, 1] wave64 compute test covering (1) s_nor_b64 of two compares + s_bcnt1_i32_b64, (2) s_nor_b64 with all 32 active lanes matching + s_cselect_b32 on scc, and (3) s_and_b64 of two compares followed by s_or_b64 with uniform (0, 0x12345678):

  • Unmodified b71f7ee (sync_mask=3 ballot=0 ballot_local=6): s21=0x00000000 bcnt1=16 cselect=11 s25=0x00000000 (all 4 wrong)
  • XCL_X64_ALL_MASKS=1 (sync_mask=0 ballot=6 ballot_local=0): s21=0xffffffff bcnt1=48 cselect=77 s25=0x12345678 (all 4 pass)
  • Changing the fallback in xcl_x_sync_mask to uint o = xcl_x_meet() ? xcl_x_get(0u) : select(xcl_half != 0u, lo, hi); (sync_mask=3 ballot=0 ballot_local=6): s21=0xffffffff bcnt1=48 cselect=77 s25=0x12345678 (all 4 pass, and all 9 gpu_split_wave64 tests pass).

Not tested

  • AMD / native wave64 GPUs, release build, retail smoke run, D3D12 debug layer tests, heap-window OOM fallback.

AI use

  • agy with gemini-4-neon-high: audited the diff, ran the test suite and targeted partial-wave GPU reproduction on the hardware above, and drafted this report.

@DavidM1911

Copy link
Copy Markdown
Contributor

Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~22 ignored. (Intermittent single-test flake under parallel scheduling; clean on single-threaded rerun — same flake seen on unmodified main.)

Thanks!

Compares combined by 64-bit bitwise ops into a mask that is read whole
(s_bcnt1, saveexec, a live scc, ...) each exchanged the two halves'
ballots, although the combination is lane-wise and only its result needs
both halves. Within one basic block, x64_masks now follows such trees:
when two or more compares or carries feed a tree of lane-wise
s_*_b64 ops (not s_cmov / s_cselect) and nothing else reads them, the
compares keep the local ballot and the tree's last op exchanges its
result once with xcl_x_sync_mask (salu64, before scc is derived from it).

A block ends after every branch, conditional or not, and where any jump
lands: ending one only at branch targets would let a compare read whole
on the taken side of a conditional branch be made local by a tree on the
fall-through side. LOOP_GUARD runs only at block starts.

gpu_split_wave64_bitwise_lane_masks expected the two compares under a
live scc to exchange; they now stay local and the s_and_b64 exchanges.

With no other half (a partial wave, xcl_xn == 0), xcl_x_sync_mask keeps
the word the half computed for the absent one: its compare bits are 0,
but an inverting tree op (s_nor_b64, s_not_b64, ...) sets them, and a
fallback of 0 gave a wrong s_bcnt1 and scc.
gpu_split_wave64_bitwise_mask_tree_in_a_partial_wave covers it (16
instead of 48 without the fallback).

Assisted-by: Claude Code (Claude Opus 5.5)
@MiguelMachado-dev

Copy link
Copy Markdown
Contributor Author

@MatyEjsr thanks for finding this, and for the reproduction. The fix is in 6acbfb1 (squashed into this PR's commit). It is the fallback you suggested: with no other half, xcl_x_sync_mask keeps the word this half computed for the missing one instead of 0.

New GPU test, gpu_split_wave64_bitwise_mask_tree_in_a_partial_wave: a wave64 group of 32 threads with s_nor_b64 of two compares, read by s_bcnt1 and by scc. On an RTX 5080 it fails without the fix (s_bcnt1 16, expected 48) and passes with it. At 6acbfb1: cargo test -p xcl --lib gpu_split_wave64 -- --test-threads=1 gives 9 passed, and the whole suite gives 789 passed, 0 failed, 41 ignored.

AI use: Claude Code (Claude Opus 5.5) wrote the fix, the test and this comment.

@qcdaark

qcdaark commented Nov 25, 2026

Copy link
Copy Markdown

Retail data point on RTX 3070 Ti (split-wave64): with this PR the world-load GPU hang from #675 is still there, 2 of 2 runs, with the default spin meet and with XCL_WAVE64_SPIN=0.

Setup: RTX 3070 Ti 8 GB, driver 621.24, Ryzen 9 7900X, 32 GB, Windows 11 26300, release. Retail 1.0.2.1 from #675.

run meet end
A default (counter spin) 0x887a0006 at 154 s, then 0x80000003 at +0x2474606
B XCL_WAVE64_SPIN=0 0x887a0006 at 221 s, then the process ends with the same status

+0x2474606 after the device removal is the same signature as the HBO hang runs on this card. Aftermath was off for these two runs, so I can't say which program was in flight this time.

A build note for the rebase: this PR removes x64_masks::by_pc, but main still calls it in crates/xcl/src/gpu/dxil/kernel.rs (direct DXIL, lw.whole_live = …by_pc(insts, kernel)), and my older tree calls it there too. To build, I kept a by_pc that returns solve's liveness without the mask trees, since the direct path does not emit xcl_x_sync_mask. That patch is attached as build-fix.patch.

Attached (user name removed): xcl.log and report.json for both runs.

AI use: Claude Code (Claude Opus 5.5) built the tree, ran the title and wrote this; I checked it before posting.
build-fix.patch
runA-pr1597-default-report.json
runA-pr1597-default-xcl.log
runB-pr1597-spin0-report.json
runB-pr1597-spin0-xcl.log

@grahamster2

Copy link
Copy Markdown
Collaborator

Some numbers on why this one matters for the split-wave64 cost on NVIDIA.

I counted the static exchanges in the split-form HLSL of the 374 wave64 compute programs in a retail title's shader pack (main cbc296a, xcl shaders --export-hlsl, RTX 3080 as the 32-lane device). There are 25,352 exchanges, and 22,825 of them (90 %) are xcl_x_ballot: a v_cmp mask exchanged on its way through s_and/s_or/s_xor_b64 into s_and_saveexec_b64 / s_cbranch_execz. That is the mask-tree pattern this PR folds into one exchange at the end. 4.7k of the exchanges sit inside loops.

The other open exchange PRs, each cherry-picked onto the same main and counted the same way:

PR exchanges change in loops
main 25,352 4,746
#1193 (uniform readfirstlane) 24,945 -1.6 % 4,696
#944 (batching) 24,711 -2.5 % 4,441
#573 (wave size from the header) 25,276 -0.3 % 4,732

None of them touches the ballots. On the 3080 an exchange costs about 3x a native loop iteration in the one-wave-group spin form (numbers in #1244 and #675), so the ballots are where the time goes.

This branch is based 491 commits behind main and doesn't cherry-pick cleanly, so I couldn't count it. Once it's rebased on current main, I'm happy to run the same count (total, in loops, per program) and the bench_split_wave64 / bench_compute_lds_block loops on the 3080 and post the numbers here.

AI-assisted (Claude Code, Claude Opus 5.5); counts and timings from runs on my machine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

goal:world-load Bears on the retail title getting past world loading

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants