Skip to content

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

Merged
ivall merged 1 commit into
xcl-dev:mainfrom
hexgeon1:wave64-local-masks
Nov 23, 2026
Merged

ivall merged 1 commit into
xcl-dev:mainfrom
hexgeon1:wave64-local-masks

Conversation

@hexgeon1

Copy link
Copy Markdown
Contributor

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; Wave64 console programs on GPUs with 32-lane waves only (NVIDIA) #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

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

In the split wave64 form (xcl-dev#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]>
@ivall
ivall merged commit f5999e4 into xcl-dev:main Nov 23, 2026
@ivall

ivall commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

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.)

BBareth pushed a commit to BBareth/xcl that referenced this pull request Nov 23, 2026
KickoffX (xcl-dev#93), Wait32/64BitValueX (xcl-dev#71/xcl-dev#72), RSSetDepthBiasX (xcl-dev#94). Conflicts with main resolved (console slots and PC overrides united; ExecuteCommandLists keeps the per-queue title-memory refresh). KickoffX resets the PC list mid-recording: it now also restores what the Reset clears and console code had set (descriptor heaps, topology, viewports, scissors, blend factor, stencil ref, vertex buffers, render targets) - on the console a kickoff leaves the list's state as it was; without the heaps a translated draw after it would read garbage descriptors.
Michael-Glaser added a commit to Michael-Glaser/xcl that referenced this pull request Nov 24, 2026
…cl-dev#104)

On newer GDK runtimes (QUEUE_SHIFTED), 20 command list console methods shift up by +2 via NEWER_LIST_SLOTS, leaving 11 older slots (xcl-dev#59, xcl-dev#60, xcl-dev#68, xcl-dev#69, xcl-dev#78, xcl-dev#81, xcl-dev#84, xcl-dev#88, xcl-dev#89, xcl-dev#93, xcl-dev#94) without another shifted method taking their old index. Those slots remained wired directly to the older layout's handlers, so a newer-layout call hit the wrong method.

Wrap those orphaned slots in make_layout_split so newer-layout title calls go to a logged stub while older-layout titles keep the handler and non-title callers keep their PC target (including cl_execute_indirect at xcl-dev#59 and the original base-table stub for slots >= scanned). Slot xcl-dev#10 (Reset) stays Reset on both layouts and is not orphaned.

Assisted-by: Antigravity (Gemini 4 Neon)
Michael-Glaser added a commit to Michael-Glaser/xcl that referenced this pull request Nov 24, 2026
…vall#104)

On newer GDK runtimes (QUEUE_SHIFTED), 20 command list console methods shift up by +2 via NEWER_LIST_SLOTS, leaving 11 older slots (ivall#59, ivall#60, ivall#68, ivall#69, ivall#78, ivall#81, ivall#84, ivall#88, ivall#89, ivall#93, ivall#94) without another shifted method taking their old index. Those slots remained wired directly to the older layout's handlers, so a newer-layout call hit the wrong method.

Wrap those orphaned slots in make_layout_split so newer-layout title calls go to a logged stub while older-layout titles keep the handler and non-title callers keep their PC target (including cl_execute_indirect at ivall#59 and the original base-table stub for slots >= scanned). Slot ivall#10 (Reset) stays Reset on both layouts and is not orphaned.

Assisted-by: Antigravity (Gemini 4 Neon)
This was referenced Nov 25, 2026
rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 27, 2026
… copies at once

VA-vs-Map, headroom and ring-uploads title legs go green (Vulkan 9 -> 6
FAILs, null e2e 10 -> 7): buffer heaps come from the high-VA graphics
pool (window 0, real committable host addresses), so placed buffers
report VA == Map address with title-side aliasing offsets, and PC slot
15 runs the same immediate host memcpy as the console DMA copy (the
single queue is already coherent).

Evidence: new lib test heap_backed_placed_buffers_share_va_and_map_and_copy
fails pre-slice (fake VAs, detached Map, slot-15 stub) and passes post;
title fresh-root Vulkan run: done, failures: 6 (triangle, desc-heaps,
committed-tex, bias-xcl-dev#94, title-mem tex, streaming tex remain); retail
HOLDS (exit 139 @ exe+0x2237320, SIGSEGV rbx NULL, 20 stubs, 0 d3d12).
Full lib shows the usual environment flakes (a different unrelated test
fails each run, identical on the pristine tree); d3d12x_linux 14/14 green.
rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 27, 2026
…nd struct

Device xcl-dev#60/xcl-dev#55 + PC10 stash the PSO's draw bytes (VS/PS ptr+len,
RTV formats/DSV format, rasterizer + blend, PC graphics-desc layout)
into PsoRecord at creation; derived blend PSOs (xcl-dev#55) inherit the base
record (patch bytes for S3). List slot 25 SetPipelineState binds the
record into ListDrawRecord. Store-only: no draw, no validation, no
execute; triangle/bias FAILs stay failing. Linux reads title pointers
directly like the other null shims (shader::readable is no-op false
here); Windows capture path unchanged.

Evidence: new draw_s1_pso_record_populated_pre_draw fails pre-change
(9 unresolved record symbols) and passes post; full lib 805 passed,
0 failed, 1 ignored (one known parallel VFS flake, green serially +
on rerun); both cargo checks clean; title null 3 FAILs (triangle,
present, bias-xcl-dev#94) / Vulkan 2 (triangle, bias-xcl-dev#94), all D3D12.X
GPU-only, 0 non-GPU, delta 0; retail HOLDS (exit 139 @
exe+0x2237320, SIGSEGV fault 0, 20 stubs, xgr XAsync/XNetworking/XUser,
0 d3d12).
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.

2 participants