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

Open
github-bot wants to merge 2 commits from github-bot/gh-1735 into main AGit
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 10:20 UTC · original
IlyassRS3:fix/w64-fold-b32-mask-barriers → main · +312 −68 · 3 files · 2 commits · open

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

Summary

Follow-up to @⁠whoalin1's finding (#1608): 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).
**@⁠IlyassRS3** on GitHub · 2026-11-25 10:20 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1735) `IlyassRS3:fix/w64-fold-b32-mask-barriers` → `main` · +312 −68 · 3 files · 2 commits · open Item ID: SHADER-W64-FOLD-B32-MASK-BARRIERS Claim: https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608 Depends on: none ### Summary Follow-up to @⁠whoalin1's finding (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/1608): 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).
Address @adrianmjim's review note on #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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:03:06 +00:00
Author
Owner

@⁠copilot-pull-request-reviewer[bot] on GitHub · 2026-11-25 10:20 UTC · original · reviewed

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.

**@⁠copilot-pull-request-reviewer[bot]** on GitHub · 2026-11-25 10:20 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1735#pullrequestreview-6055734915) · **reviewed** 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.
Author
Owner

@⁠lixonik on GitHub · 2026-11-25 10:29 UTC · original · reviewed

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.

**@⁠lixonik** on GitHub · 2026-11-25 10:29 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1735#pullrequestreview-6055864010) · **reviewed** 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.
Author
Owner

@⁠ecox982745 on GitHub · 2026-11-25 10:46 UTC · original

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.

**@⁠ecox982745** on GitHub · 2026-11-25 10:46 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1735#issuecomment-7160155872) 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.
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-25 12:00 UTC · original

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

**@⁠adrianmjim** on GitHub · 2026-11-25 12:00 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1735#issuecomment-7161801236) 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 #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.
Author
Owner

@⁠IlyassRS3 on GitHub · 2026-11-25 12:20 UTC · original

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.

**@⁠IlyassRS3** on GitHub · 2026-11-25 12:20 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1735#issuecomment-7162236895) 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.
This pull request has changes conflicting with the target branch.
  • docs/design.md
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1735/head:github-bot/gh-1735'
git switch 'github-bot/gh-1735'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1735'
git switch 'github-bot/gh-1735'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1735'
git switch 'github-bot/gh-1735'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1735'
git switch 'main'
git merge --squash 'github-bot/gh-1735'
git switch 'main'
git merge --ff-only 'github-bot/gh-1735'
git switch 'main'
git merge 'github-bot/gh-1735'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.