rdna2spirv, lane_masks: handle lane-mask destinations and sources in s_bitset0/1_b32 (SHADER-BITSET32-MASK-AND-DST) #2355

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

@⁠ShotTechOp on GitHub · 2026-11-26 15:06 UTC · original
ShotTechOp:fix/bitset-mask-and-dst → main · +273 −19 · 5 files · 1 commits · open

Item: SHADER-BITSET32-MASK-AND-DST

Problem

In tools/rdna2spirv.py, s_bitset0_b32 and s_bitset1_b32 always applied old & ~(1 << (b & 31)) / old | (1 << (b & 31)) directly to self.src_u(a[0]) and wrote back via self.dst(a[0], r), while tools/lane_masks.py dropped mask tracking for a[0] on both opcodes:

  1. When a[0] holds a lane mask (vcc_lo/vcc_hi holding a mask, or a mask SGPR tracked by lane_masks.py), each invocation holds only its own lane's bit (0 or 0xFFFFFFFF), so ORing 1 << k turned every inactive lane non-zero (activating all 32 lanes) and ANDing ~(1 << k) left the targeted lane non-zero (0xFFFFFFDF etc.).
  2. In tools/lane_masks.py, mutating a lane mask with s_bitset0/1_b32 dropped a[0] from state, causing downstream mask consumers (s_bcnt1_i32_b32, s_ff1_i32_b32, s_bcnt1_i32_b64, etc.) to read per-lane 0/0xFFFFFFFF as scalar data instead of collecting the wave mask.
  3. When the bit-index source a[1] holds a lane mask (e.g. s_bitset1_b32 s10, vcc_lo), self.src_u(a[1]) read each lane's 0/0xFFFFFFFF instead of the wave's 32-bit mask word, producing divergent per-lane shifts (bit 0 vs bit 31).

Fix

  • tools/lane_masks.py: Preserve MASK / MASK_LO / MASK_HI (and wave64 vcc mask status for vcc_lo/vcc_hi) when s_bitset0_b32 or s_bitset1_b32 mutates an existing lane mask; clear tracking to plain data when mutating non-mask or EITHER constants.
  • tools/rdna2spirv.py: Merge s_bitset0_b32 and s_bitset1_b32 into one handler. Materialize a lane-mask bit-index source via Gen.lane_mask_word. When self.holds_lane_mask(a[0]), compare wave_lane() against (b & 31) (or (b & 31) + 32 for a wave64 high half) and update only the addressed lane via Gen.put_mask (keeping the untouched half of EXEC/VCC in wave64 and updating %vcc_hi when a[0] is vcc_hi).
  • docs/design.md: Document the s_bitset0_b32/s_bitset1_b32 lane-mask contract row.
  • tests/semantic/test_bitset32_mask.py (and updated tests/semantic/test_scalar_alu_imm_bitset.py comment/loop): Add wave32, wave64, and --wave64-emulate tests for EXEC, VCC, mask SGPRs, and lane-mask bit-index sources.

Verification

AI-assisted: yes.
Environment: Windows 11, Python 3.12.10, CPU-only (no game files, no spirv-val, not GPU-verified).

  • GREEN (branch):
    • python -I tests/semantic/test_bitset32_mask.py -v -> 8/8 OK (wave32, wave64 native, --wave64-emulate)
    • python -I tests/semantic/test_scalar_alu_imm_bitset.py -> 17/17 OK
    • python -I tests/semantic/test_salu_bitset_b64.py -> 5/5 OK
    • python -I tests/semantic/test_salu_b32_lane_masks.py -> 8/8 OK (1 skipped: spirv-val absent)
    • python -I tests/test_lane_masks.py -> OK
    • python -I tests/test_text_write_line_endings.py -> 3/3 OK
    • python -m compileall -q tools/rdna2spirv.py tools/lane_masks.py tests/semantic/test_bitset32_mask.py tests/semantic/test_scalar_alu_imm_bitset.py -> 0
    • python -I scripts/check_no_game_data.py ... -> 0
    • git diff --check -> 0
  • RED control (origin/main fbf100b1):
    • Running tests/semantic/test_bitset32_mask.py against origin/main's tools/ fails 6/8 assertions (test_wave32_vcc_lo_and_mask_sgpr_bitset, test_wave64_vcc_and_mask_sgpr_halves_bitset native & emulate, test_lane_mask_bit_index_source_uses_wave_mask_word, and both LaneMasksBitset32Tests preservation checks).
**@⁠ShotTechOp** on GitHub · 2026-11-26 15:06 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2355) `ShotTechOp:fix/bitset-mask-and-dst` → `main` · +273 −19 · 5 files · 1 commits · open Item: `SHADER-BITSET32-MASK-AND-DST` ### Problem In `tools/rdna2spirv.py`, `s_bitset0_b32` and `s_bitset1_b32` always applied `old & ~(1 << (b & 31))` / `old | (1 << (b & 31))` directly to `self.src_u(a[0])` and wrote back via `self.dst(a[0], r)`, while `tools/lane_masks.py` dropped mask tracking for `a[0]` on both opcodes: 1. When `a[0]` holds a lane mask (`vcc_lo`/`vcc_hi` holding a mask, or a mask SGPR tracked by `lane_masks.py`), each invocation holds only its own lane's bit (`0` or `0xFFFFFFFF`), so ORing `1 << k` turned every inactive lane non-zero (activating all 32 lanes) and ANDing `~(1 << k)` left the targeted lane non-zero (`0xFFFFFFDF` etc.). 2. In `tools/lane_masks.py`, mutating a lane mask with `s_bitset0/1_b32` dropped `a[0]` from `state`, causing downstream mask consumers (`s_bcnt1_i32_b32`, `s_ff1_i32_b32`, `s_bcnt1_i32_b64`, etc.) to read per-lane `0`/`0xFFFFFFFF` as scalar data instead of collecting the wave mask. 3. When the bit-index source `a[1]` holds a lane mask (e.g. `s_bitset1_b32 s10, vcc_lo`), `self.src_u(a[1])` read each lane's `0`/`0xFFFFFFFF` instead of the wave's 32-bit mask word, producing divergent per-lane shifts (bit 0 vs bit 31). ### Fix - **`tools/lane_masks.py`**: Preserve `MASK` / `MASK_LO` / `MASK_HI` (and wave64 `vcc` mask status for `vcc_lo`/`vcc_hi`) when `s_bitset0_b32` or `s_bitset1_b32` mutates an existing lane mask; clear tracking to plain data when mutating non-mask or `EITHER` constants. - **`tools/rdna2spirv.py`**: Merge `s_bitset0_b32` and `s_bitset1_b32` into one handler. Materialize a lane-mask bit-index source via `Gen.lane_mask_word`. When `self.holds_lane_mask(a[0])`, compare `wave_lane()` against `(b & 31)` (or `(b & 31) + 32` for a wave64 high half) and update only the addressed lane via `Gen.put_mask` (keeping the untouched half of EXEC/VCC in wave64 and updating `%vcc_hi` when `a[0]` is `vcc_hi`). - **`docs/design.md`**: Document the `s_bitset0_b32`/`s_bitset1_b32` lane-mask contract row. - **`tests/semantic/test_bitset32_mask.py`** (and updated `tests/semantic/test_scalar_alu_imm_bitset.py` comment/loop): Add wave32, wave64, and `--wave64-emulate` tests for EXEC, VCC, mask SGPRs, and lane-mask bit-index sources. ### Verification AI-assisted: yes. Environment: Windows 11, Python 3.12.10, CPU-only (no game files, no `spirv-val`, not GPU-verified). - **GREEN (branch)**: - `python -I tests/semantic/test_bitset32_mask.py -v` -> 8/8 OK (wave32, wave64 native, `--wave64-emulate`) - `python -I tests/semantic/test_scalar_alu_imm_bitset.py` -> 17/17 OK - `python -I tests/semantic/test_salu_bitset_b64.py` -> 5/5 OK - `python -I tests/semantic/test_salu_b32_lane_masks.py` -> 8/8 OK (1 skipped: `spirv-val` absent) - `python -I tests/test_lane_masks.py` -> OK - `python -I tests/test_text_write_line_endings.py` -> 3/3 OK - `python -m compileall -q tools/rdna2spirv.py tools/lane_masks.py tests/semantic/test_bitset32_mask.py tests/semantic/test_scalar_alu_imm_bitset.py` -> 0 - `python -I scripts/check_no_game_data.py ...` -> 0 - `git diff --check` -> 0 - **RED control (`origin/main` `fbf100b1`)**: - Running `tests/semantic/test_bitset32_mask.py` against `origin/main`'s `tools/` fails 6/8 assertions (`test_wave32_vcc_lo_and_mask_sgpr_bitset`, `test_wave64_vcc_and_mask_sgpr_halves_bitset` native & emulate, `test_lane_mask_bit_index_source_uses_wave_mask_word`, and both `LaneMasksBitset32Tests` preservation checks).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 15:09:45 +00:00
Author
Owner

@⁠ecox982745 on GitHub · 2026-11-26 15:19 UTC · original

The bit-set fix passed all 66 synthetic GPU dispatches on an RTX 4090 at 69726d1ce. The same inputs matched the CPU reference in 23 of 66 dispatches on parent fbf100b1b.

Compute mode Parent PR
Native wave32 9/21 21/21
Wave64 emulated on 32-lane subgroups 14/45 45/45

One case exposes the bug with a single active lane. Lane 0 sets VCC bit 0, then s_mov_b32 s10, vcc_lo saves the word 1. After s_bitset1_b32 s10, s10, the word should be 3 and its population count should be 2 in every lane. The parent returned 32 in lane 0 and 1 in lane 1. The PR returned 2 in all 32 lanes. The corresponding saved-high-half cases also passed in wave64 emulation.

The remaining probes covered setting and clearing EXEC and VCC bits, using either VCC half as the bit-index source, wrapped indices including 32 and 63, and ordinary scalar controls. I checked every output word and the untouched tail of each buffer. All 13 PR modules passed spirv-val --target-env vulkan1.2. The eight committed CPU tests also passed.

Windows 11, NVIDIA 616.64, Vulkan validation enabled, synthetic shader listings. AI-assisted verification by OpenAI Codex (GPT-6).

**@⁠ecox982745** on GitHub · 2026-11-26 15:19 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2355#issuecomment-7196113305) The bit-set fix passed all 66 synthetic GPU dispatches on an RTX 4090 at `69726d1ce`. The same inputs matched the CPU reference in 23 of 66 dispatches on parent `fbf100b1b`. | Compute mode | Parent | PR | | --- | ---: | ---: | | Native wave32 | 9/21 | 21/21 | | Wave64 emulated on 32-lane subgroups | 14/45 | 45/45 | One case exposes the bug with a single active lane. Lane 0 sets VCC bit 0, then `s_mov_b32 s10, vcc_lo` saves the word `1`. After `s_bitset1_b32 s10, s10`, the word should be `3` and its population count should be `2` in every lane. The parent returned `32` in lane 0 and `1` in lane 1. The PR returned `2` in all 32 lanes. The corresponding saved-high-half cases also passed in wave64 emulation. The remaining probes covered setting and clearing EXEC and VCC bits, using either VCC half as the bit-index source, wrapped indices including 32 and 63, and ordinary scalar controls. I checked every output word and the untouched tail of each buffer. All 13 PR modules passed `spirv-val --target-env vulkan1.2`. The eight committed CPU tests also passed. Windows 11, NVIDIA 616.64, Vulkan validation enabled, synthetic shader listings. AI-assisted verification by OpenAI Codex (GPT-6).
Author
Owner

@⁠lixonik on GitHub · 2026-11-26 15:37 UTC · original · reviewed

Windows 11: 30/30 bitset semantic tests passed, including tracked lane-mask operands and the immediate-bitset cases. No findings.

AI-assisted.

**@⁠lixonik** on GitHub · 2026-11-26 15:37 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2355#pullrequestreview-6076878768) · **reviewed** Windows 11: 30/30 bitset semantic tests passed, including tracked lane-mask operands and the immediate-bitset cases. No findings. AI-assisted.
Author
Owner

@⁠wavehs on GitHub · 2026-11-26 18:26 UTC · original · reviewed

macOS arm64 check of #2355 (SHADER-BITSET32-MASK-AND-DST). Translator plus lane_masks.py and one semantic test; the GPU part is MoltenVK/Kosmickrisp.

Tested head 69726d1ce merged onto main bdbc7e923 as merge b56572d3e (clean; branch 21 commits behind, git merge-tree clean).

Environment: Apple M3 Pro, arm64 (subgroups 4..32), macOS 27.2.1, CPython 3.15.2, SPIRV-Tools v2026.4, llvm-mc 23.1.5, MoltenVK 1.4.0; KosmicKrisp and lavapipe = Mesa 26.3.0.

The new test passes, and the control bites

  • python -I tests/semantic/test_bitset32_mask.py: Ran 8 tests ... OK at the head and after merging onto bdbc7e923; the updated tests/semantic/test_scalar_alu_imm_bitset.py still passes.
  • Negative control (test kept, tools/rdna2spirv.py and tools/lane_masks.py put back to main's): FAILED (failures=6) - mask bit-index source, wave32 vcc_lo + mask SGPR, wave64 VCC/SGPR halves in native and --wave64-emulate, and the two lane_masks checks that a mask kind survives. So both the emission and the tracking are pinned.

What I checked in the code

  • The mask path updates only the addressed lane: hit = (SubgroupLocalInvocationId == (S0 & 31) [+32 for a wave64 high half]), then put_mask writes 0 / ~0 or keeps the old per-lane bit. The untouched half of a wave64 EXEC/VCC is preserved, and SCC is not written, which matches the row and the ISA. The non-mask path keeps the old bit math, and s_bitset0_b32 now shares it with s_bitset1_b32 instead of a separate block.
  • The lane_masks.step addition keeps a mask kind on a mask destination and drops it to data otherwise.
  • Nit, not a failure: for a wave64 vcc_lo/vcc_hi write when state["vcc"] is a full MASK, step returns without changing the kind. Native reads go through the live %vcc/%vcc_hi, and the new wave64 test reads the result through s_bcnt1_i32_b64 vcc in both native and --wave64-emulate and passes, so I could not make it stale; a one-line comment saying why the full-mask kind is kept would save the next reader the same detour.

No regressions

  • Semantic suite: 500 files / 6,367 tests / 0 failed at main bdbc7e923; 501 files / 6,375 tests / 0 failed at the merge (+1 file, +8 tests).
  • GPU fixtures (268): MoltenVK 195 PASS / 3 FAIL / 70 SKIP, KosmicKrisp 190 / 5 / 73, identical at main and the merge after normalising the per-run temp directory; same pre-existing failures (cs_fma_fused, cs_imgatomic_ext, ps_ssharp on MV; cs_bvh, cs_bvh32, cs_imgatomic_ext, ps_cndmask_sign_mods, ps_valu16_mad_sel on KK).

Finding
The mask-destination path has no GPU fixture. cs_w32_salu_ext.asm is the only fixture with s_bitset0/1_b32, and it runs them on plain SGPRs (s65, s66); cs_w32_salu64.asm has the _b64 form on scalar pairs. Nothing on a GPU writes a lane mask (exec_lo, vcc_lo/vcc_hi, a mask SGPR) with s_bitset and then counts the mask, so the put_mask/wave_lane half logic rests on the interpreter, which the doc row already says ("not GPU-verified"). Adding four stores to an existing cs_w32_* fixture (mask before, bitset, s_bcnt1, store) would cover it on any GPU, and would catch a wrong half split in wave64 the way cs_w64x_partial_scmp.asm does for compares.

python -I scripts/check_no_game_data.py on the changed files: clean. git diff --check: clean.

No game files. AI-assisted.

**@⁠wavehs** on GitHub · 2026-11-26 18:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2355#pullrequestreview-6078969877) · **reviewed** **macOS arm64 check of #2355** (SHADER-BITSET32-MASK-AND-DST). Translator plus `lane_masks.py` and one semantic test; the GPU part is MoltenVK/Kosmickrisp. Tested head `69726d1ce` merged onto `main` `bdbc7e923` as merge `b56572d3e` (clean; branch 21 commits behind, `git merge-tree` clean). Environment: Apple M3 Pro, arm64 (subgroups 4..32), macOS 27.2.1, CPython 3.15.2, SPIRV-Tools v2026.4, llvm-mc 23.1.5, MoltenVK 1.4.0; KosmicKrisp and lavapipe = Mesa 26.3.0. **The new test passes, and the control bites** - `python -I tests/semantic/test_bitset32_mask.py`: `Ran 8 tests ... OK` at the head and after merging onto `bdbc7e923`; the updated `tests/semantic/test_scalar_alu_imm_bitset.py` still passes. - Negative control (test kept, `tools/rdna2spirv.py` and `tools/lane_masks.py` put back to `main`'s): `FAILED (failures=6)` - mask bit-index source, wave32 `vcc_lo` + mask SGPR, wave64 VCC/SGPR halves in native and `--wave64-emulate`, and the two `lane_masks` checks that a mask kind survives. So both the emission and the tracking are pinned. **What I checked in the code** - The mask path updates only the addressed lane: `hit = (SubgroupLocalInvocationId == (S0 & 31) [+32 for a wave64 high half])`, then `put_mask` writes 0 / ~0 or keeps the old per-lane bit. The untouched half of a wave64 EXEC/VCC is preserved, and SCC is not written, which matches the row and the ISA. The non-mask path keeps the old bit math, and `s_bitset0_b32` now shares it with `s_bitset1_b32` instead of a separate block. - The `lane_masks.step` addition keeps a mask kind on a mask destination and drops it to data otherwise. - Nit, not a failure: for a wave64 `vcc_lo`/`vcc_hi` write when `state["vcc"]` is a full `MASK`, `step` returns without changing the kind. Native reads go through the live `%vcc`/`%vcc_hi`, and the new wave64 test reads the result through `s_bcnt1_i32_b64 vcc` in both native and `--wave64-emulate` and passes, so I could not make it stale; a one-line comment saying why the full-mask kind is kept would save the next reader the same detour. **No regressions** - Semantic suite: 500 files / 6,367 tests / 0 failed at `main` `bdbc7e923`; 501 files / 6,375 tests / 0 failed at the merge (+1 file, +8 tests). - GPU fixtures (268): MoltenVK 195 PASS / 3 FAIL / 70 SKIP, KosmicKrisp 190 / 5 / 73, identical at `main` and the merge after normalising the per-run temp directory; same pre-existing failures (`cs_fma_fused`, `cs_imgatomic_ext`, `ps_ssharp` on MV; `cs_bvh`, `cs_bvh32`, `cs_imgatomic_ext`, `ps_cndmask_sign_mods`, `ps_valu16_mad_sel` on KK). **Finding** The mask-destination path has no GPU fixture. `cs_w32_salu_ext.asm` is the only fixture with `s_bitset0/1_b32`, and it runs them on plain SGPRs (`s65`, `s66`); `cs_w32_salu64.asm` has the `_b64` form on scalar pairs. Nothing on a GPU writes a lane mask (`exec_lo`, `vcc_lo`/`vcc_hi`, a mask SGPR) with `s_bitset` and then counts the mask, so the `put_mask`/`wave_lane` half logic rests on the interpreter, which the doc row already says ("not GPU-verified"). Adding four stores to an existing `cs_w32_*` fixture (mask before, bitset, `s_bcnt1`, store) would cover it on any GPU, and would catch a wrong half split in wave64 the way `cs_w64x_partial_scmp.asm` does for compares. `python -I scripts/check_no_game_data.py` on the changed files: clean. `git diff --check`: clean. No game files. AI-assisted.
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-27 04:46 UTC · original · approved these changes

Approving 69726d1ce.

s_bitset0/1_b32 on a lane mask now follows the translator's mask model: a lane holds only its own bit, so setting or clearing bit n changes lane n's value alone (n + 32 for the high half in wave64), and the destination stays a tracked mask in lane_masks.step instead of being dropped to data. On plain data the two ops keep their shift-and-or/and-not form, now in one branch. The updated test_scalar_alu_imm_bitset.py case follows from that: these ops no longer clear mask tracking.

test_bitset32_mask.py 8 passed (4 subtests), test_scalar_alu_imm_bitset.py 17 passed (265 subtests). Merged onto main d1dff7949 together with #2397, #2416 and the other three of this series below (no conflicts), on Linux with Python 3.13. The full translator suite (run_semantic_tests.py tests/semantic) on that tree has only the 8 failures main has here (7 in test_fspx_dot4_op_sel_disasm, test_int32_to_f64), and tests/test_translate.py only main's own ps_sdwa_f16_const.asm (an llvm-mc version difference in this environment). AI-assisted (Claude Code, Claude Opus 5.5).

**@⁠tiguan26** on GitHub · 2026-11-27 04:46 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2355#pullrequestreview-6083599580) · **approved these changes** Approving 69726d1ce. `s_bitset0/1_b32` on a lane mask now follows the translator's mask model: a lane holds only its own bit, so setting or clearing bit n changes lane n's value alone (n + 32 for the high half in wave64), and the destination stays a tracked mask in `lane_masks.step` instead of being dropped to data. On plain data the two ops keep their shift-and-or/and-not form, now in one branch. The updated `test_scalar_alu_imm_bitset.py` case follows from that: these ops no longer clear mask tracking. `test_bitset32_mask.py` 8 passed (4 subtests), `test_scalar_alu_imm_bitset.py` 17 passed (265 subtests). Merged onto `main` `d1dff7949` together with #2397, #2416 and the other three of this series below (no conflicts), on Linux with Python 3.13. The full translator suite (`run_semantic_tests.py tests/semantic`) on that tree has only the 8 failures `main` has here (7 in `test_fspx_dot4_op_sel_disasm`, `test_int32_to_f64`), and `tests/test_translate.py` only `main`'s own `ps_sdwa_f16_const.asm` (an llvm-mc version difference in this environment). AI-assisted (Claude Code, Claude Opus 5.5).
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/2355/head:github-bot/gh-2355'
git switch 'github-bot/gh-2355'

Merge

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