rdna2spirv: the wave mask gather is stored before it is read, so every lane runs it (SHADER-WAVEMASK-EAGER) #953

Open
github-bot wants to merge 1 commit from github-bot/gh-953 into main AGit
Owner

@⁠hugo0024 on GitHub · 2026-11-24 15:39 UTC · original
hugo0024:shader-wavemask-eager → main · +150 −3 · 4 files · 1 commits · open

Claimed in the coordination issue as: SHADER-WAVEMASK-EAGER (claim)

What and why

On MoltenVK, v_mbcnt of a lane mask counts too few lanes when it runs with EXEC narrowed. tools/crosslane_fuzz.py finds it on an Apple M5: 4 of 100 wave32 programs of the default seed mismatch, the same four with the same words every run. The smallest one:

v_cmp_le_u32_e64 s16, v1, v6       ; s16 = a mask over all lanes
v_cmpx_lt_u32_e32 8, v2            ; EXEC narrowed
v_mbcnt_lo_u32_b32 v6, s16, v7     ; lanes still on count the bits of s16 below them

A register holds only this lane's bit of a mask, and Gen.wave_mask gathers the other lanes' bits with a subgroup operation. v_mbcnt writes a VGPR, so its result is an OpSelect on the lane's EXEC. SPIRV-Cross inlines a result that is used once, and the MSL MoltenVK 1.4.2 generates for that line is:

_113[6u] = _366 ? (popcount(simd_or(_378 ? _381 : 0u) & ((_397 < 32u) ? ((1u << _397) - 1u) : 4294967295u)) + _113[7u]) : _113[6u];

simd_or is in the taken arm of the ternary on EXEC. A lane that is off never runs it, so its bit is missing from what every other lane counts. This is the mechanism SHADER-529 fixed for DPP's ballot and shuffle, in a place that fix did not cover.

  • tools/rdna2spirv.py: Gen.wave_mask stores the gathered mask to a variable of its own with Gen.eager (both words of the OpGroupNonUniformBitwiseOr path, and the ballot word of the wave32 fragment path). A store is a statement every lane runs. The emulated path has no subgroup operation and is unchanged.
  • tests/semantic/test_wave_mask_eager.py: v_mbcnt, s_bcnt1 and s_ff1 of a compare mask, of VCC and of EXEC, read under a narrowed EXEC in a vertex, a compute and a pixel shader, checked with the model of SPIRV-Cross's forwarding from test_dpp_eager_subgroup.py, and the values against a plain-Python reference in spirv_wave_interp.py.
  • docs/design.md ("Whole-wave masks" row) and tests/semantic/README.md: the rule and how it was confirmed.

No new instruction is translated; the contract row that changes is the existing one.

Tested

Apple M5, MoltenVK 1.4.2, main a28927849. crosslane_fuzz.py with the same vk_compute_test and the translator of main (before) or of this branch (after); match / mismatch / hypothesis-only:

Run Before After
--wave 32 --modes native --no-maskdata, 100 programs 92 / 4 / 4 100 / 0 / 0
the same with --dpp --seed 7, 200 programs 193 / 5 / 2 200 / 0 / 0
--wave 32 --modes native, 100 programs 86 / 9 / 5 91 / 7 / 2
--wave 64 --modes emulate --no-maskdata, 100 programs 100 / 0 / 0 100 / 0 / 0
  • The seven programs that still mismatch in the third row all use masks read as data, the open case --no-maskdata leaves out (docs/shader_validation.md). Not touched here.
  • tests/semantic/test_wave_mask_eager.py: 5 tests pass. With the translator of main it fails 7 ways: v_mbcnt in the vertex and the compute shader for each of the three masks, and the shape test.
  • Translator suite: 466 files, 6113 tests, 0 files failed. pytest files: 1868 passed, 18 skipped, as on main.
  • build.py: 281 of 281 fixtures translate and pass spirv-val before and after; 33 of the 281 modules change. run_gpu_tests.py: 257 of 258 translated and valid before and after.
  • run_gpu_tests.py, native and with LEONIDA_W64_EMULATE=1: every test has the same verdict before and after (184/249 and 215/249 passed; failed cs_fma_fused, cs_imgload_tfe, cs_imgload_d16_tfe and the intermittent ps_imgatomic, #543). ctest: 72/72. lds_fuzz.py and valu_fuzz.py --family mem: 40/40 each.
  • Standard-library loop: 484 files, 1 failed, tests/test_tool_stdout_encoding.py, which fails the same way on main. tests/test_docs_*.py pass.

Not measured: the translated and spirv-val-valid counts over the game's shaders (no game files here), and any GPU other than this one. On a driver that already ran the gather in every lane the change is a store and a load of the same value.

GPUs tested: Apple M5 (8-core GPU), MoltenVK 1.4.2, macOS 27.2.1 (26B131)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

🤖 Generated with Claude Code

**@⁠hugo0024** on GitHub · 2026-11-24 15:39 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/953) `hugo0024:shader-wavemask-eager` → `main` · +150 −3 · 4 files · 1 commits · open Claimed in the coordination issue as: SHADER-WAVEMASK-EAGER ([claim](https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14)) ### What and why On MoltenVK, `v_mbcnt` of a lane mask counts too few lanes when it runs with EXEC narrowed. `tools/crosslane_fuzz.py` finds it on an Apple M5: 4 of 100 wave32 programs of the default seed mismatch, the same four with the same words every run. The smallest one: ```text v_cmp_le_u32_e64 s16, v1, v6 ; s16 = a mask over all lanes v_cmpx_lt_u32_e32 8, v2 ; EXEC narrowed v_mbcnt_lo_u32_b32 v6, s16, v7 ; lanes still on count the bits of s16 below them ``` A register holds only this lane's bit of a mask, and `Gen.wave_mask` gathers the other lanes' bits with a subgroup operation. `v_mbcnt` writes a VGPR, so its result is an `OpSelect` on the lane's EXEC. SPIRV-Cross inlines a result that is used once, and the MSL MoltenVK 1.4.2 generates for that line is: ```text _113[6u] = _366 ? (popcount(simd_or(_378 ? _381 : 0u) & ((_397 < 32u) ? ((1u << _397) - 1u) : 4294967295u)) + _113[7u]) : _113[6u]; ``` `simd_or` is in the taken arm of the ternary on EXEC. A lane that is off never runs it, so its bit is missing from what every other lane counts. This is the mechanism SHADER-529 fixed for DPP's ballot and shuffle, in a place that fix did not cover. - `tools/rdna2spirv.py`: `Gen.wave_mask` stores the gathered mask to a variable of its own with `Gen.eager` (both words of the `OpGroupNonUniformBitwiseOr` path, and the ballot word of the wave32 fragment path). A store is a statement every lane runs. The emulated path has no subgroup operation and is unchanged. - `tests/semantic/test_wave_mask_eager.py`: `v_mbcnt`, `s_bcnt1` and `s_ff1` of a compare mask, of VCC and of EXEC, read under a narrowed EXEC in a vertex, a compute and a pixel shader, checked with the model of SPIRV-Cross's forwarding from `test_dpp_eager_subgroup.py`, and the values against a plain-Python reference in `spirv_wave_interp.py`. - `docs/design.md` ("Whole-wave masks" row) and `tests/semantic/README.md`: the rule and how it was confirmed. No new instruction is translated; the contract row that changes is the existing one. ### Tested Apple M5, MoltenVK 1.4.2, `main` `a28927849`. `crosslane_fuzz.py` with the same `vk_compute_test` and the translator of `main` (before) or of this branch (after); match / mismatch / hypothesis-only: | Run | Before | After | |---|---|---| | `--wave 32 --modes native --no-maskdata`, 100 programs | 92 / 4 / 4 | 100 / 0 / 0 | | the same with `--dpp --seed 7`, 200 programs | 193 / 5 / 2 | 200 / 0 / 0 | | `--wave 32 --modes native`, 100 programs | 86 / 9 / 5 | 91 / 7 / 2 | | `--wave 64 --modes emulate --no-maskdata`, 100 programs | 100 / 0 / 0 | 100 / 0 / 0 | - The seven programs that still mismatch in the third row all use masks read as data, the open case `--no-maskdata` leaves out (`docs/shader_validation.md`). Not touched here. - `tests/semantic/test_wave_mask_eager.py`: 5 tests pass. With the translator of `main` it fails 7 ways: `v_mbcnt` in the vertex and the compute shader for each of the three masks, and the shape test. - Translator suite: 466 files, 6113 tests, 0 files failed. pytest files: 1868 passed, 18 skipped, as on `main`. - `build.py`: 281 of 281 fixtures translate and pass spirv-val before and after; 33 of the 281 modules change. `run_gpu_tests.py`: 257 of 258 translated and valid before and after. - `run_gpu_tests.py`, native and with `LEONIDA_W64_EMULATE=1`: every test has the same verdict before and after (184/249 and 215/249 passed; failed `cs_fma_fused`, `cs_imgload_tfe`, `cs_imgload_d16_tfe` and the intermittent `ps_imgatomic`, #543). `ctest`: 72/72. `lds_fuzz.py` and `valu_fuzz.py --family mem`: 40/40 each. - Standard-library loop: 484 files, 1 failed, `tests/test_tool_stdout_encoding.py`, which fails the same way on `main`. `tests/test_docs_*.py` pass. Not measured: the translated and `spirv-val`-valid counts over the game's shaders (no game files here), and any GPU other than this one. On a driver that already ran the gather in every lane the change is a store and a load of the same value. GPUs tested: Apple M5 (8-core GPU), MoltenVK 1.4.2, macOS 27.2.1 (26B131) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
A register holds this lane's bit of a lane mask; v_mbcnt, s_bcnt1 and
s_ff1 gather the other lanes' bits with a subgroup operation
(Gen.wave_mask). v_mbcnt writes a VGPR, so its result is selected on the
lane's EXEC, and MoltenVK's SPIRV-Cross inlined the single-use gather
into the taken arm of that ternary:

    v6 = active ? popcount(simd_or(mine ? bit : 0) & below) + acc : v6;

A lane that is off never ran the gather, so its bit was missing from
every other lane's count. Gen.wave_mask now stores the gather to a
variable of its own with Gen.eager, as SHADER-529 did for DPP's ballot
and shuffle.

On an Apple M5 (MoltenVK 1.4.2), tools/crosslane_fuzz.py with wave 32,
native and no maskdata goes from 92 of 100 programs matching to 100 of
100, and from 193 to 200 of 200 with DPP at seed 7.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:30:07 +00:00
Author
Owner

@⁠shwepz on GitHub · 2026-11-24 15:49 UTC · original · reviewed

Reviewed and ran locally (Windows 11, Python 3.11, 34f51ab on top of a289278). Looks correct to me.

Verified

  • tests/semantic/test_wave_mask_eager.py passes on this branch (5/5). The same file run against main's translator fails 7 checks: the vertex and compute subtests report the OpGroupNonUniformBitwiseOr in an arm of OpSelect, and test_the_gather_is_stored_before_v_mbcnt_selects_on_exec sees the gather used by the select instead of an OpStore. So the test catches exactly this bug.
  • No regressions in the neighbouring suites, same result on main and on this branch: test_dpp_eager_subgroup.py (7), test_salu_flbit_lane_mask.py (4), test_lane_masks_vector_bitwise.py (4), tests/test_lane_masks.py.

Code

  • The emulated path (x_mcache / x_round) returns before the new eager calls. That's fine: it exchanges through the buffer and has no subgroup op to inline.
  • Wave32 fragment path: eager-storing the extracted word is enough. The OpStore forces the ballot to run in every lane, even though the ballot itself isn't stored.
  • I also checked the other subgroup sites that end in an EXEC-gated dst() without eager: the ds_append/ds_consume result shuffle (~L4996) and the v_readfirstlane-style shuffle (~L7719). Neither has this problem, because the source lane is the first active lane, which takes the arm. Unlike an OR-reduce or a ballot, the lanes that are off don't contribute anything there. Nothing more is needed in this PR.

Questions

  1. Corpus impact: every native wave_mask caller now gets an extra Function variable plus a store and a load. Could you add to the design.md row how many corpus .spv change and whether they still pass spirv-val (as the other rows do)?
  2. Has anyone run cs_wavemask / cs_wave64_ballot / cs_wm_* on a native wave64 driver (AMD RADV or AMD Windows) with this branch? It should be a no-op there, but it's the one place a regression would show.

Nit

  • Since this PR edits the "Whole-wave masks" row: the row already repeats "Whole-mask counts, first-set-lane queries and equality use subgroup operations." twice (that's on main already). It could be dropped while you're there.
**@⁠shwepz** on GitHub · 2026-11-24 15:49 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/953#pullrequestreview-6042607885) · **reviewed** Reviewed and ran locally (Windows 11, Python 3.11, `34f51ab` on top of `a289278`). Looks correct to me. **Verified** - `tests/semantic/test_wave_mask_eager.py` passes on this branch (5/5). The same file run against `main`'s translator fails 7 checks: the vertex and compute subtests report the `OpGroupNonUniformBitwiseOr` in an arm of `OpSelect`, and `test_the_gather_is_stored_before_v_mbcnt_selects_on_exec` sees the gather used by the select instead of an `OpStore`. So the test catches exactly this bug. - No regressions in the neighbouring suites, same result on `main` and on this branch: `test_dpp_eager_subgroup.py` (7), `test_salu_flbit_lane_mask.py` (4), `test_lane_masks_vector_bitwise.py` (4), `tests/test_lane_masks.py`. **Code** - The emulated path (`x_mcache` / `x_round`) returns before the new `eager` calls. That's fine: it exchanges through the buffer and has no subgroup op to inline. - Wave32 fragment path: eager-storing the extracted `word` is enough. The `OpStore` forces the ballot to run in every lane, even though the ballot itself isn't stored. - I also checked the other subgroup sites that end in an EXEC-gated `dst()` without `eager`: the `ds_append`/`ds_consume` result shuffle (~L4996) and the `v_readfirstlane`-style shuffle (~L7719). Neither has this problem, because the source lane is the first active lane, which takes the arm. Unlike an OR-reduce or a ballot, the lanes that are off don't contribute anything there. Nothing more is needed in this PR. **Questions** 1. Corpus impact: every native `wave_mask` caller now gets an extra Function variable plus a store and a load. Could you add to the `design.md` row how many corpus `.spv` change and whether they still pass `spirv-val` (as the other rows do)? 2. Has anyone run `cs_wavemask` / `cs_wave64_ballot` / `cs_wm_*` on a native wave64 driver (AMD RADV or AMD Windows) with this branch? It should be a no-op there, but it's the one place a regression would show. **Nit** - Since this PR edits the "Whole-wave masks" row: the row already repeats "Whole-mask counts, first-set-lane queries and equality use subgroup operations." twice (that's on `main` already). It could be dropped while you're there.
Author
Owner

@⁠hugo0024 on GitHub · 2026-11-24 16:03 UTC · original

Thanks for running it both ways.

  1. I have no game files, so I cannot give corpus counts. What I measured is in the description: all 281 synthetic fixtures still translate and pass spirv-val, and 33 of those modules change. If someone with the corpus runs the before/after counts I will add them to the design.md row.
  2. Not by me: the only GPU here is an Apple M5 (MoltenVK, 32-wide subgroups), where those tests skip natively. A run of cs_wavemask, cs_wave64_ballot and cs_wm_* on RADV or AMD's Windows driver with this branch would be welcome; the change there is a store and a load of the same value.

The repeated sentence in that row is on main already; I left it so this pull request stays one topic.

**@⁠hugo0024** on GitHub · 2026-11-24 16:03 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/953#issuecomment-7137060280) Thanks for running it both ways. 1. I have no game files, so I cannot give corpus counts. What I measured is in the description: all 281 synthetic fixtures still translate and pass spirv-val, and 33 of those modules change. If someone with the corpus runs the before/after counts I will add them to the design.md row. 2. Not by me: the only GPU here is an Apple M5 (MoltenVK, 32-wide subgroups), where those tests skip natively. A run of cs_wavemask, cs_wave64_ballot and cs_wm_* on RADV or AMD's Windows driver with this branch would be welcome; the change there is a store and a load of the same value. The repeated sentence in that row is on main already; I left it so this pull request stays one topic.
Author
Owner

@⁠thesubtleties on GitHub · 2026-11-24 19:21 UTC · original

Tested 15185922c merged onto main 3aa97542, including the RADV run you asked for. NVIDIA RTX 3080 (620.73.04) and AMD Raphael RDNA2 iGPU gfx1036 (RADV, Mesa 26.3.1), CachyOS.

  • cs_wavemask, cs_wave64_ballot, cs_wm_bfm64, cs_wm_half_cmp, cs_wm_half_ops and cs_wm_scc_rfl pass on gfx1036, native and with LEONIDA_W64_EMULATE=1.
  • The full GPU suite is per-test identical to main on both GPUs, native and emulated.
  • crosslane_fuzz.py gives the same output as main (4 runs of 250 programs).
  • run_semantic_tests.py tests/semantic: 471 files / 6,173 tests, 0 failed.

Not tested: MoltenVK, game corpus, performance. AI-assisted (Claude Code)

**@⁠thesubtleties** on GitHub · 2026-11-24 19:21 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/953#issuecomment-7141978899) Tested `15185922c` merged onto main `3aa97542`, including the RADV run you asked for. NVIDIA RTX 3080 (620.73.04) and AMD Raphael RDNA2 iGPU gfx1036 (RADV, Mesa 26.3.1), CachyOS. - `cs_wavemask`, `cs_wave64_ballot`, `cs_wm_bfm64`, `cs_wm_half_cmp`, `cs_wm_half_ops` and `cs_wm_scc_rfl` pass on gfx1036, native and with `LEONIDA_W64_EMULATE=1`. - The full GPU suite is per-test identical to main on both GPUs, native and emulated. - `crosslane_fuzz.py` gives the same output as main (4 runs of 250 programs). - `run_semantic_tests.py tests/semantic`: 471 files / 6,173 tests, 0 failed. Not tested: MoltenVK, game corpus, performance. AI-assisted (Claude Code)
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-25 19:12 UTC · original · approved these changes

Approving. Merged into current main (9f9a7c5) with no conflicts. test_wave_mask_eager.py passes, and the full translator suite gives 491 files / 6,297 tests with only test_int32_to_f64 failing, the same baseline failure as main here (Linux, Python 3.13).

It's a small, safe change: the wave-mask gather words go through eager() on both the wave32 and wave64 returns. The ballot is then stored at the point it is computed, so a downstream compiler (SPIRV-Cross for MoltenVK here) can't sink it into an EXEC-guarded select arm where inactive lanes would skip the subgroup op. On drivers that don't sink it, the values are unchanged.

AI-assisted (Claude).

**@⁠tiguan26** on GitHub · 2026-11-25 19:12 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/953#pullrequestreview-6063488575) · **approved these changes** Approving. Merged into current main (9f9a7c5) with no conflicts. `test_wave_mask_eager.py` passes, and the full translator suite gives 491 files / 6,297 tests with only `test_int32_to_f64` failing, the same baseline failure as main here (Linux, Python 3.13). It's a small, safe change: the wave-mask gather words go through `eager()` on both the wave32 and wave64 returns. The ballot is then stored at the point it is computed, so a downstream compiler (SPIRV-Cross for MoltenVK here) can't sink it into an EXEC-guarded select arm where inactive lanes would skip the subgroup op. On drivers that don't sink it, the values are unchanged. AI-assisted (Claude).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

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