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

Open
opened 2026-11-26 20:19:43 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/953
Author: @hugo0024
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: shader-wavemask-eager


📝 Commits (1)

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

📊 Changes

4 files changed (+150 additions, -3 deletions)

View changed files

📝 docs/design.md (+1 -1)
📝 tests/semantic/README.md (+3 -0)
➕ tests/semantic/test_wave_mask_eager.py (+140 -0)
📝 tools/rdna2spirv.py (+6 -2)

📄 Description

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/953 **Author:** [@hugo0024](https://github.com/hugo0024) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `shader-wavemask-eager` --- ### 📝 Commits (1) - [`eb2b1b4`](https://github.com/ubiquitarianism/leonida/commit/eb2b1b4c367a4c41271fdb28484b0d3c36f2e4a5) rdna2spirv: the wave mask gather is stored before it is read, so every lane runs it (SHADER-WAVEMASK-EAGER) ### 📊 Changes **4 files changed** (+150 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+1 -1) 📝 `tests/semantic/README.md` (+3 -0) ➕ `tests/semantic/test_wave_mask_eager.py` (+140 -0) 📝 `tools/rdna2spirv.py` (+6 -2) </details> ### 📄 Description Claimed in the coordination issue as: SHADER-WAVEMASK-EAGER ([claim](https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7136038137)) ### 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#992
No description provided.