Skip to content

wave64_emulate_opt: guard 32-bit opposite-half and live-SCC mask barriers (SHADER-W64-FOLD-B32-MASK-BARRIERS) - #1735

Open
IlyassRS3 wants to merge 2 commits into
ProjectLeonida:mainfrom
IlyassRS3:fix/w64-fold-b32-mask-barriers
Open

IlyassRS3 wants to merge 2 commits into
ProjectLeonida:mainfrom
IlyassRS3:fix/w64-fold-b32-mask-barriers

Conversation

@IlyassRS3

Copy link
Copy Markdown
Contributor

Item ID: SHADER-W64-FOLD-B32-MASK-BARRIERS
Claim: #1608 (comment)
Depends on: none

Summary

Follow-up to @whoalin1's finding (#1608 (comment)): aligns tools/wave64_emulate_opt.py (_classify_block_body, find_local_exec_folds, _insn_may_barrier_with_meta, and _is_uniform_scc_safe) with all 32-bit wave64 lane-mask barrier/vote paths lowered by tools/rdna2spirv.py under --wave64-emulate.

Root cause

Three 32-bit mask paths in tools/rdna2spirv.py emit wave_mask / g_vote (x_round / x_split / OpControlBarrier) under --wave64-emulate, but were missed by tools/wave64_emulate_opt.py:

  1. Opposite-half _mask_b32_src (rdna2spirv.py:6095-6110) on s_mov_b32, s_cselect_b32, s_not_b32, and MASK_SOP2_B32 (independent of SCC liveness).
  2. Live-SCC scc_logic votes (rdna2spirv.py:6134-6156) on _SOP2_B32_LOGIC (s_and/or/xor/andn2/orn2/nand/nor/xnor/not_b32) when writing a mask destination (exec_lo/hi, or vcc_lo/hi with mask) or reading same-half / single-half lane-mask sources.
  3. Opposite-half wave_mask on s_*_saveexec_b32 / s_*_wrexec_b32 (rdna2spirv.py:11163-11215) when reading a high-half mask source or saving into exec_hi/vcc_hi (independent of SCC liveness).

Before this change, folding an s_cbranch_execz region containing any of these instructions tripped AssertionError: wave64 emulation: x_split inside a folded lane-local region, and multi-wave plan_structured_emulation misclassified wave-divergent guards containing them as inline_sync_free.

Changes

  • tools/wave64_emulate_opt.py:
    • Added _mask_half, _mask_b32_dst_half, and a single shared _mask_chk_may_barrier helper used by _classify_block_body, find_local_exec_folds, and _insn_may_barrier_with_meta.
    • Added _MASK_CHK_B32_MOV, _MASK_CHK_B32_LOGIC, _MASK_CHK_B32_CSELECT, and opposite-half checks on _ALL_SAVEEXEC_WREXEC _b32 ops.
    • Extended _is_uniform_scc_safe to also detect vcc_lo/vcc_hi mask writes via _mask_b32_dst_half.
    • Same-half dead-SCC 32-bit mask ops (e.g. s_and_b32 vcc_lo, vcc_lo, -1 with dead SCC) continue to fold with 0 barriers.
  • tests/semantic/test_wave64_emulate_barriers.py: added B32MaskBarrierTests covering unit barrier prediction, all 9 folded s_cbranch_execz cases (64-thread workgroup interpreter + spirv_asm.assemble), a same-half dead-SCC positive fold control, and a 128-thread multi-wave divergent guard test (reverse_order=False/True).
  • docs/design.md: added contract-table row for --wave64-emulate 32-bit opposite-half and live-SCC barrier guards.

Verification

  • AI-assisted: yes
  • GPUs tested: none (CPU-only on macOS; no Vulkan GPU, spirv-val, or retail game files)
  • python3 -I tests/semantic/test_wave64_emulate_barriers.py: 17/17 OK
  • python3 -I tests/semantic/test_wave64_emulate_opt.py: 33/33 OK
  • python3 -I tests/test_wave64_emulate_opt_cfg.py: 26/26 OK
  • python3 -I scripts/run_semantic_tests.py tests/semantic: 487 files, 6,283 tests, 0 files failed (76 s)
  • python3 -m compileall -q make_game.py tools tests: OK
  • python3 -I tests/test_text_write_line_endings.py: 3/3 OK
  • python3 -I scripts/check_no_game_data.py tools/wave64_emulate_opt.py tests/semantic/test_wave64_emulate_barriers.py docs/design.md: OK
  • Mutation check (running B32MaskBarrierTests against unpatched main:tools/wave64_emulate_opt.py): 11 failures (all 9 folded s_cbranch_execz cases crash with AssertionError: wave64 emulation: x_split inside a folded lane-local region, plus the unit check and 128-thread multi-wave guard check).

Copilot AI balanced review requested due to automatic review settings November 25, 2026 10:20

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Independent A/B on native Windows / CPython 3.12.1 at 499b6bce and its merge base 3de22a19. Running the four new B32MaskBarrierTests against the merge-base implementation reproduced 11 failures, including the multi-wave guard and opposite-half cases. At the PR head all 17 barrier tests and all 33 wave64 optimizer tests passed.

AI-assisted. Hand-written fixtures and the CPU workgroup interpreter only.

@ecox982745

Copy link
Copy Markdown

I checked the wave64 mask changes on an RTX 4090 (native Windows 11, NVIDIA 616.64). I used the same eight hand-written compute shaders at parent 3de22a190 and head 499b6bce3, assembled with LLVM 23.1.6 and translated with --wave64-emulate. The physical subgroups were 32 lanes wide.

Cases Parent PR head
Four 64-thread shaders that copy, AND, invert, or conditionally select the high half of VCC into the low half under partial EXEC Translator assertion on all four All lane results matched
Two 64-thread shaders where the next scalar select consumes SCC after a mask AND, with either a VCC or SGPR destination Translator assertion on both All lane results matched, including the zero-mask result
One 128-thread shader where only the first logical wave takes the branch that copies the high half of VCC Translator assertion Both waves matched independently, including all 64 lanes of the wave that skipped the branch
Same-half AND with unused SCC Passed Byte-identical SPIR-V and identical readbacks

On the head, all eight modules passed spirv-val --target-env vulkan1.2. I dispatched each with five runtime thresholds (0, 5, 40, 63, 96) and compared every lane to its expected integer output: 2,880/2,880 words matched across 40 dispatches. The 16 trailing guard words stayed untouched in each run. The seven parent assertions happened before GPU dispatch. The two focused CPU suites also passed, 17/17 and 33/33. Synthetic inputs only. AI-assisted verification.

@adrianmjim

Copy link
Copy Markdown

Tested 499b6bce3 applied onto main 42537e7b6 on AMD: Radeon RX 7900 XT (RDNA3, subgroup 32..64), driver 32.0.32029.2010 (26.11.2), Windows 11 build 26300, Vulkan SDK 1.4.363.0, llvm-mc 23.1.5, test programs from a main 42537e7b6 build. No game files.

  • CPU: tests/semantic/test_wave64_emulate_barriers.py 17/17, tests/semantic/test_wave64_emulate_opt.py 33/33 and tests/test_wave64_emulate_opt_cfg.py 26/26 pass.
  • GPU, the whole suite with LEONIDA_W64_EMULATE=1: 244/263 passed, 18 skipped, 1 failed on both main and this PR, with the same status for every fixture. The one failure is cs_imgatomic_ext, which cs_imgatomic_ext: use the volume T# key vk_store_test now expects (GPU-IMG-ATOMIC-EXT-VOLUME-TAG) #1518 fixes. So the stricter fold and plan checks change no fixture's result (status and printed values) under emulation on this card.

For anyone tracking it: this PR doesn't touch the fold gap I described on #808, where a region that contains the last block, ending in a conditional branch with no s_endpgm after it, loses its "end" edge. My repro gives identical wrong stores on main and with this PR. Inside find_local_exec_folds the PR only changes the per-instruction "clean" test (its other changes are in the plan's barrier and uniform-SCC checks), and those programs have no 32-bit mask ops, so it neither fixes nor worsens that.

A small test note: in B32MaskBarrierTests the comment says lanes 36..47 are active, but the setup activates 36..63. The nine EXECZ-region cases in test_opposite_half_and_live_scc_b32_inside_execz_regions only check lanes 0..15, so a bug that clobbered the other half of VCC/EXEC in these paths would still pass. Checking all 64 lanes (16..35 staying 0, 36..63 the expected values, which needs the expect lambdas extended past lane 15) would cover it.

Address @adrianmjim's review note on ProjectLeonida#1735: fix the active-lane comment
(36..63, not 36..47) and assert expected outputs across all 64 lanes
(0..15, inactive 16..35 staying 0, and 36..63 preserving untouched high
half state) for all nine EXECZ-region cases.
@IlyassRS3

Copy link
Copy Markdown
Contributor Author

Thank you @adrianmjim for the RX 7900 XT full-suite run and the sharp test note!

Updated B32MaskBarrierTests.test_opposite_half_and_live_scc_b32_inside_execz_regions to fix the comment (36..63) and assert all 64 lanes (0..15, inactive 16..35 staying 0, and 36..63 verifying the untouched high half of vcc/exec is preserved across all nine cases); re-ran test_wave64_emulate_barriers.py (17/17), test_wave64_emulate_opt.py (33/33), and test_wave64_emulate_opt_cfg.py (26/26) — all pass.

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

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants