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

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

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/2355
Author: @ShotTechOp
Created: 11/26/2026
Status: 🔄 Open

Base: main ← Head: fix/bitset-mask-and-dst


📝 Commits (1)

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

📊 Changes

5 files changed (+273 additions, -19 deletions)

View changed files

📝 docs/design.md (+1 -0)
➕ tests/semantic/test_bitset32_mask.py (+231 -0)
📝 tests/semantic/test_scalar_alu_imm_bitset.py (+2 -3)
📝 tools/lane_masks.py (+9 -0)
📝 tools/rdna2spirv.py (+30 -16)

📄 Description

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

🔄 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/2355 **Author:** [@ShotTechOp](https://github.com/ShotTechOp) **Created:** 11/26/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/bitset-mask-and-dst` --- ### 📝 Commits (1) - [`69726d1`](https://github.com/ubiquitarianism/leonida/commit/69726d1ceca0b8579023c6aaf7bab95ed29891b0) rdna2spirv, lane_masks: handle lane-mask destinations and sources in s_bitset0/1_b32 (SHADER-BITSET32-MASK-AND-DST) ### 📊 Changes **5 files changed** (+273 additions, -19 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+1 -0) ➕ `tests/semantic/test_bitset32_mask.py` (+231 -0) 📝 `tests/semantic/test_scalar_alu_imm_bitset.py` (+2 -3) 📝 `tools/lane_masks.py` (+9 -0) 📝 `tools/rdna2spirv.py` (+30 -16) </details> ### 📄 Description 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). --- <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#2356
No description provided.