Skip to content

shader-compiler: portable Wave64 ballot, vote & exec lowering for non-64-wide GPUs (#151) - #201

Open
Vladimir-Human wants to merge 2 commits into
brcly:mainfrom
Vladimir-Human:xshader/portable-wave64-ballot
Open

Vladimir-Human wants to merge 2 commits into
brcly:mainfrom
Vladimir-Human:xshader/portable-wave64-ballot

Conversation

@Vladimir-Human

Copy link
Copy Markdown
Contributor

Summary

AI-assisted implementation towards #151 adding portable Wave64 ballot, vote, and first-lane lowering in tools/shader-compiler (hlsl::portable_wave64) so console Wave64 compute shaders using subgroup ballots/votes (WaveActiveBallot, WaveActiveAnyTrue, WaveActiveAllTrue, WaveReadLaneFirst, scalar-mask logic/s_mov_b64/s_cmov_b64/s_cselect_b64/s_*_wrexec_b64/s_*_saveexec_b64, 64-bit Ballot64 propagation into exec/vcc/scc) run reliably across non-64-wide host GPUs (e.g., Wave32-only GPUs where native WaveActiveBallot() only covers 32 lanes), without requiring [WaveSize(64)].

Whenever a shader uses constructs that cannot be statically proven safe to rewrite (divergent control flow across votes, multi-wave wave-varying barriers without uniform provenance, non-multiple-of-64 groups, unhandled 32-bit ExecLo/VccLo or s_wqm/SDWA/exec_snapshots consumers), the pass cleanly declines lowering and preserves existing behavior.

Highlights

  • Provenance & Convergence Analysis (uniformity.rs): Tracks group-uniform values (including VOP3 v_readfirstlane_b32 scalar destinations), hoists provably group-uniform subgroup votes out of convergent regions when needed, and verifies barrier/control-flow convergence across [64*N, 1, 1] and multi-dimensional workgroups (e.g., [8,8,1], [4,4,4]).
  • 64-bit Ballot Mask Tracking (masks.rs): Distinguishes 64-bit Ballot64 lane-mask registers from raw 64-bit masks and Opnd::Null, supports scalar/mask logic promotion (s_and/s_or/s_xor/s_andn2/s_nand/s_nor/s_xnor/s_orn1/s_orn2), s_cmov_b64, s_cselect_b64, s_*_wrexec_b64, s_*_saveexec_b64, and separates -1 (all-64-ones) from 0xffff_ffff literals.
  • Portable HLSL Emission (emit.rs): Emits groupshared uint2 per-wave ballot scratch (wave_id = gindex >> 6, (lane & 63u)) with EXEC-masked accumulation and barriers strictly isolated to proven convergent sites. Zero changes to runtime/d3d12.
  • Clean mergeability: Clean git merge-tree against both origin/main and PR xshader: lower constant-lane v_readlane_b32 for Wave32 and portable Wave64 #185.

Verification (079143b16)

  • python -m unittest discover -s tests -p "test_repository_hygiene.py" -v: 4/4 passed (all new/modified .rs modules well under limit).
  • cargo test --manifest-path tools/shader-compiler/Cargo.toml: 334 unit + 2 integration passed (0 warnings).
  • cargo test --manifest-path runtime/d3d12/Cargo.toml -- --test-threads=1: 134/134 passed.
  • python build.py lanes + lanes.exe --adapter 10de:2783 --selftest all: 15/15 passed.
  • Live Wave32-only RTX 4070 SUPER (10de:2783, wave_min=32 wave_max=32, DXC cs_6_0 & cs_6_6 -WX, D3D12 Debug Layer):
    • 165/165 dispatches passed (139,520/139,520 words bit-exact) + independent 64/64 adversarial multi-group ([64,1,1], [128,1,1], [8,8,1], [4,4,4]) & 15/15 convergence repro suite, debug_errors=0 debug_warnings=0.

Refs #151.

Co-Authored-By: AI Assistant

…, prefix counts, and cross-half reads

Support compute threadgroups (nx, ny, nz) where T = nx * ny * nz is a
positive multiple of 64 (<= 1024), partitioning groupshared scratch per
64-lane logical wave via flattened thread index (wave_id = gindex >> 6,
lane64 = gindex & 63u, half_id = (lane64 >> 5) & 1u, lane32 = lane64 & 31u).

Lower 64-lane WaveActiveBallot, WaveActiveAnyTrue, WaveActiveAllTrue,
WaveActiveCountBits, WavePrefixCountBits, FirstActive, v_readfirstlane_b32,
cross-half WaveReadLaneAt, v_permlanex16_b32, and 64-lane reductions onto
portable groupshared helpers with strict 3-level (Group > Wave > Divergent)
uniformity and loop-convergence verification, hoisting wave-uniform forward
skip barriers and refusing lane-divergent or wave-varying loop sites so
Wave32-only GPUs run cleanly without [WaveSize(64)] and without deadlocks.
- F1: avoid
ul clobber by hi(Null) on PostAnyTrue(Null) 64-bit logic votes via dedicated w64_null{idx} register.
- F2: extract per-lane bit when writing Ballot64 results into �xec/�cc via s_mov_b64, s_cmov_b64, s_cselect_b64, or 64-bit logic ops.
- F3: support s_*_wrexec_b64 and s_{nor,xnor,orn1}_saveexec_b64 alongside saveexec_b64.
- F4: treat Literal(u32::MAX) as zero-extended low-32 mask (not 64-bit -1), promoting to Ballot64 for exact (0xffffffffu, 0u) compares/logic.
- F5: promote s_<logic>_b64 combining �xec/�cc with scalar SGPR pairs or non-mask constants so �xec/�cc is balloted before bitwise logic.
- F6: preserve s_cmov_b64 mask/ballot provenance, mark s_wqm_b64/SDWA �_cmp_*/�xec_snapshots destinations as unpromotable Mask, and guard is_ballot_word_mov against SDWA/modifiers.
- F7: refuse unhandled 32-bit/non-lowered �xec/�cc reads (s_wqm_b32, s_bcnt1_i32_b32, etc.) instead of falling through to native Wave* intrinsics or 1-bit flags.
- F8: decode VOP3 �_readfirstlane_b32 scalar dst (
aw[0] & 127, 125 => Null) in both uniformity analysis and emission so multi-wave dependent barriers hoist properly.
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