[PR #1943] rdna2spirv: extract per-lane bits when reading plain scalar data in VCC as a lane mask (SHADER-VCC-DATA-MASK-SRC) #1949

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

📋 Pull Request Information

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

Base: main ← Head: fix/vcc-data-mask-src


📝 Commits (1)

  • c82d668 rdna2spirv: extract lane bits when reading plain scalar data in VCC as mask (SHADER-VCC-DATA-MASK-SRC)

📊 Changes

3 files changed (+280 additions, -5 deletions)

View changed files

📝 docs/design.md (+1 -1)
➕ tests/semantic/test_vcc_data_mask_src.py (+250 -0)
📝 tools/rdna2spirv.py (+29 -4)

📄 Description

Claimed as SHADER-VCC-DATA-MASK-SRC in #1608.

What changed and why

When VCC (vcc / vcc_lo / vcc_hi) is written as plain scalar data (e.g. s_mov_b32 vcc_lo, K, s_mov_b32 vcc_hi, K, SGPR copies, or scalar ALU/loads) and left uncertified by tools/lane_masks.py, reading it as a lane mask through Gen.mask_src() (v_cndmask_b32, v_add_co_ci_u32, s_*_saveexec_*, s_mov_b32 exec_lo, vcc_lo, s_and_b32 exec_hi, exec_hi, vcc_hi, etc.) was broken in both wave32 and wave64:

  1. Wave32 (tools/rdna2spirv.py, Gen.mask_src):
    • _scalar_word_lane_bit() extraction for vcc / vcc_lo was gated on self.wave32_vcc_pointer (only set when the shader had s_load_* ..., vcc).
    • In any other wave32 shader, s_mov_b32 vcc_lo, 0x00000002 followed by v_cndmask_b32_e64 v6, 3, 5, vcc_lo returned raw 0x2 instead of bit lane & 31, enabling every lane instead of lane 1 alone.
  2. Wave64 (tools/rdna2spirv.py, Gen.mask_src, Gen.mask_dst, Gen.mov64, Gen.put_mask):
    • mask_src() loaded only %vcc and returned it raw without loading %vcc_hi for lanes 32..63 or extracting bit lane & 31. So s_mov_b32 vcc_lo, 0 + s_mov_b32 vcc_hi, 0x80000001 saw 0 on all 64 lanes, and s_mov_b32 vcc_lo, 0x2 saw non-zero on all 64 lanes.
    • Wave64 32-bit vcc_hi plain-data mask reads (e.g. s_and_b32 exec_hi, exec_hi, vcc_hi) also loaded %vcc instead of %vcc_hi.
    • Certified wave64 64-bit mask writes to vcc (mask_dst, mov64, and high-half put_mask) now mirror this lane's 0 / ~0 bit into %vcc_hi (matching how mask_dst already mirrors SGPR pairs into both s[r] and s[r+1]), so a control-flow join between a certified v_cmp mask and a plain-data vcc_lo/vcc_hi write (or s_mov_b64 vcc, s[4:5] from uncertified data SGPRs) stays exact on all 64 lanes on every path.

Fix:

  • tools/rdna2spirv.py:
    • Gen.mask_src: when not self.holds_lane_mask(a) for vcc/vcc_lo/vcc_hi, extract bit lane & 31 via _scalar_word_lane_bit() unconditionally (wave32 vcc/vcc_lo from %vcc; wave64 32-bit vcc_hi from %vcc_hi; wave64 64-bit vcc from %vcc for lanes 0..31 and %vcc_hi for lanes 32..63, matching the SGPR-pair fallback right above it). Certified masks still load %vcc directly with zero extra instructions.
    • Gen.mask_dst / Gen.mov64 / Gen.put_mask: mirror wave64 vcc per-lane mask writes into %vcc_hi (wave32 %vcc_hi remains untouched as an independent SGPR).
  • docs/design.md: document plain-data VCC lane-bit extraction in the Constant lane masks (Gen.mask_src immediates) contract row.
  • tests/semantic/test_vcc_data_mask_src.py: add wave32, wave64, and --wave64-emulate execution tests (v_cndmask_b32, v_add_co_ci_u32, s_and_saveexec_b32, s_and_b32 exec_hi, ..., vcc_hi, s_mov_b64 vcc, s[4:5], and wave64 certified-mask/plain-data CFG join), each assembled with tools/spirv_asm.py.

Negative control (RED on origin/main b0f3d39 -> GREEN on this branch)

  • LEONIDA_TOOLS_DIR=<origin/main tools> python -I tests/semantic/test_vcc_data_mask_src.py: FAILED (9 failures across 5 of 6 tests) — wave32 vcc_lo cndmask/carry-in selected all 32 lanes, wave64 vcc_lo/vcc_hi cndmask (native + --wave64-emulate) and carry-in ignored %vcc_hi and selected all/no lanes, wave64 vcc_hi half-mask read read %vcc, and the wave64 CFG join failed on the plain-data branch.
  • On this branch: 6/6 OK.

Tests run (Windows 11, Python 3.12.10, CPU / standard library only; no GPU or game files)

  • python -I tests/semantic/test_vcc_data_mask_src.py -v -> 6/6 OK (also python -m unittest discover -s tests/semantic -p test_vcc_data_mask_src.py -> OK)
  • python -I tests/semantic/test_wave64_vcc_half_write.py -> 7/7 OK
  • python -I tests/semantic/test_wave64_vcc_data_branch.py -> 5/5 OK
  • python -I tests/semantic/test_vcmp_dst_wave32_vcchi.py -> 7/7 OK
  • python -I tests/semantic/test_sload_vcc_base.py -> 5/5 OK
  • python -I tests/semantic/test_control_flow_vcc.py -> 24/24 OK
  • python -I tests/semantic/test_vcc_tuple_fallback.py -> 5/5 OK
  • python -I tests/semantic/test_mask_src_sgpr_const.py -> 7/7 OK
  • python -I tests/semantic/test_salu_b32_lane_masks.py -> 9/9 OK (1 skip)
  • python -I tests/semantic/test_salu64_bitwise_data.py -> 11/11 OK
  • python -m unittest discover -s tests/semantic -p test_exec_computed.py -> 2/2 OK
  • python -m unittest discover -s tests/semantic -p test_value_semantics.py -> 41/41 OK
  • python -I tests/test_lane_masks.py -> OK
  • python -I tests/test_wint_diff.py -> 29/29 OK
  • python -I tests/test_text_write_line_endings.py -> 3/3 OK
  • All 12 tests/test_docs_*.py suites + all GroupNonUniform semantic tests -> OK
  • python -m compileall -q tools/rdna2spirv.py tests/semantic/test_vcc_data_mask_src.py -> OK
  • python -I scripts/check_no_game_data.py tools/rdna2spirv.py docs/design.md tests/semantic/test_vcc_data_mask_src.py -> OK
  • git diff --check -> clean

AI-assisted: yes.


🔄 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/1943 **Author:** [@ShotTechOp](https://github.com/ShotTechOp) **Created:** 11/25/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `fix/vcc-data-mask-src` --- ### 📝 Commits (1) - [`c82d668`](https://github.com/ubiquitarianism/leonida/commit/c82d668d0ab46bdc0ab5775a764e73d919ca076f) rdna2spirv: extract lane bits when reading plain scalar data in VCC as mask (SHADER-VCC-DATA-MASK-SRC) ### 📊 Changes **3 files changed** (+280 additions, -5 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+1 -1) ➕ `tests/semantic/test_vcc_data_mask_src.py` (+250 -0) 📝 `tools/rdna2spirv.py` (+29 -4) </details> ### 📄 Description Claimed as `SHADER-VCC-DATA-MASK-SRC` in #1608. ### What changed and why When VCC (`vcc` / `vcc_lo` / `vcc_hi`) is written as plain scalar data (e.g. `s_mov_b32 vcc_lo, K`, `s_mov_b32 vcc_hi, K`, SGPR copies, or scalar ALU/loads) and left uncertified by `tools/lane_masks.py`, reading it as a lane mask through `Gen.mask_src()` (`v_cndmask_b32`, `v_add_co_ci_u32`, `s_*_saveexec_*`, `s_mov_b32 exec_lo, vcc_lo`, `s_and_b32 exec_hi, exec_hi, vcc_hi`, etc.) was broken in both wave32 and wave64: 1. **Wave32 (`tools/rdna2spirv.py`, `Gen.mask_src`)**: - `_scalar_word_lane_bit()` extraction for `vcc` / `vcc_lo` was gated on `self.wave32_vcc_pointer` (only set when the shader had `s_load_* ..., vcc`). - In any other wave32 shader, `s_mov_b32 vcc_lo, 0x00000002` followed by `v_cndmask_b32_e64 v6, 3, 5, vcc_lo` returned raw `0x2` instead of bit `lane & 31`, enabling **every** lane instead of lane 1 alone. 2. **Wave64 (`tools/rdna2spirv.py`, `Gen.mask_src`, `Gen.mask_dst`, `Gen.mov64`, `Gen.put_mask`)**: - `mask_src()` loaded only `%vcc` and returned it raw without loading `%vcc_hi` for lanes 32..63 or extracting bit `lane & 31`. So `s_mov_b32 vcc_lo, 0` + `s_mov_b32 vcc_hi, 0x80000001` saw `0` on all 64 lanes, and `s_mov_b32 vcc_lo, 0x2` saw non-zero on all 64 lanes. - Wave64 32-bit `vcc_hi` plain-data mask reads (e.g. `s_and_b32 exec_hi, exec_hi, vcc_hi`) also loaded `%vcc` instead of `%vcc_hi`. - Certified wave64 64-bit mask writes to `vcc` (`mask_dst`, `mov64`, and high-half `put_mask`) now mirror this lane's `0` / `~0` bit into `%vcc_hi` (matching how `mask_dst` already mirrors SGPR pairs into both `s[r]` and `s[r+1]`), so a control-flow join between a certified `v_cmp` mask and a plain-data `vcc_lo`/`vcc_hi` write (or `s_mov_b64 vcc, s[4:5]` from uncertified data SGPRs) stays exact on all 64 lanes on every path. **Fix:** - `tools/rdna2spirv.py`: - `Gen.mask_src`: when `not self.holds_lane_mask(a)` for `vcc`/`vcc_lo`/`vcc_hi`, extract bit `lane & 31` via `_scalar_word_lane_bit()` unconditionally (wave32 `vcc`/`vcc_lo` from `%vcc`; wave64 32-bit `vcc_hi` from `%vcc_hi`; wave64 64-bit `vcc` from `%vcc` for lanes 0..31 and `%vcc_hi` for lanes 32..63, matching the SGPR-pair fallback right above it). Certified masks still load `%vcc` directly with zero extra instructions. - `Gen.mask_dst` / `Gen.mov64` / `Gen.put_mask`: mirror wave64 `vcc` per-lane mask writes into `%vcc_hi` (wave32 `%vcc_hi` remains untouched as an independent SGPR). - `docs/design.md`: document plain-data VCC lane-bit extraction in the `Constant lane masks (Gen.mask_src immediates)` contract row. - `tests/semantic/test_vcc_data_mask_src.py`: add wave32, wave64, and `--wave64-emulate` execution tests (`v_cndmask_b32`, `v_add_co_ci_u32`, `s_and_saveexec_b32`, `s_and_b32 exec_hi, ..., vcc_hi`, `s_mov_b64 vcc, s[4:5]`, and wave64 certified-mask/plain-data CFG join), each assembled with `tools/spirv_asm.py`. ### Negative control (RED on `origin/main` `b0f3d39` -> GREEN on this branch) - `LEONIDA_TOOLS_DIR=<origin/main tools> python -I tests/semantic/test_vcc_data_mask_src.py`: **FAILED (9 failures across 5 of 6 tests)** — wave32 `vcc_lo` cndmask/carry-in selected all 32 lanes, wave64 `vcc_lo`/`vcc_hi` cndmask (native + `--wave64-emulate`) and carry-in ignored `%vcc_hi` and selected all/no lanes, wave64 `vcc_hi` half-mask read read `%vcc`, and the wave64 CFG join failed on the plain-data branch. - On this branch: **6/6 OK**. ### Tests run (Windows 11, Python 3.12.10, CPU / standard library only; no GPU or game files) - `python -I tests/semantic/test_vcc_data_mask_src.py -v` -> 6/6 OK (also `python -m unittest discover -s tests/semantic -p test_vcc_data_mask_src.py` -> OK) - `python -I tests/semantic/test_wave64_vcc_half_write.py` -> 7/7 OK - `python -I tests/semantic/test_wave64_vcc_data_branch.py` -> 5/5 OK - `python -I tests/semantic/test_vcmp_dst_wave32_vcchi.py` -> 7/7 OK - `python -I tests/semantic/test_sload_vcc_base.py` -> 5/5 OK - `python -I tests/semantic/test_control_flow_vcc.py` -> 24/24 OK - `python -I tests/semantic/test_vcc_tuple_fallback.py` -> 5/5 OK - `python -I tests/semantic/test_mask_src_sgpr_const.py` -> 7/7 OK - `python -I tests/semantic/test_salu_b32_lane_masks.py` -> 9/9 OK (1 skip) - `python -I tests/semantic/test_salu64_bitwise_data.py` -> 11/11 OK - `python -m unittest discover -s tests/semantic -p test_exec_computed.py` -> 2/2 OK - `python -m unittest discover -s tests/semantic -p test_value_semantics.py` -> 41/41 OK - `python -I tests/test_lane_masks.py` -> OK - `python -I tests/test_wint_diff.py` -> 29/29 OK - `python -I tests/test_text_write_line_endings.py` -> 3/3 OK - All 12 `tests/test_docs_*.py` suites + all `GroupNonUniform` semantic tests -> OK - `python -m compileall -q tools/rdna2spirv.py tests/semantic/test_vcc_data_mask_src.py` -> OK - `python -I scripts/check_no_game_data.py tools/rdna2spirv.py docs/design.md tests/semantic/test_vcc_data_mask_src.py` -> OK - `git diff --check` -> clean AI-assisted: yes. --- <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#1949
No description provided.