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

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

@⁠ShotTechOp on GitHub · 2026-11-25 16:23 UTC · original
ShotTechOp:fix/vcc-data-mask-src → main · +280 −5 · 3 files · 1 commits · open

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.

**@⁠ShotTechOp** on GitHub · 2026-11-25 16:23 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1943) `ShotTechOp:fix/vcc-data-mask-src` → `main` · +280 −5 · 3 files · 1 commits · open 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 10:12:14 +00:00
Author
Owner

@⁠efeekrb on GitHub · 2026-11-25 17:45 UTC · original

Tested on an NVIDIA GeForce RTX 4060 Laptop GPU (NVIDIA 610.78), Windows 11 build 26300, with an Intel UHD iGPU also present (LEONIDA_VK_DEVICE=4060). This PR's diff applies cleanly to main at 24a29f8 (where the test programs were built) and to main at b9320a118 (git apply --check).

  • Unit tests with the PR applied: tests/semantic/test_vcc_data_mask_src.py ran 6 tests, OK.
  • run_gpu_tests.py, full suite, with the PR applied: 274/274 shaders translated and pass spirv-val. Native 209/264 passed, 54 skipped, 1 failed; LEONIDA_W64_EMULATE=1 243/264 passed, 20 skipped, 1 failed. The one failure is cs_imgatomic_ext (the one #1518 addresses), and the counts are identical to main at 24a29f8 without this PR.

So no change on this GPU in either mode. The suite shows no regression; it does not tell whether a fixture exercises the rule this PR changes. No game files; counts and pass/fail lines only. AI-assisted (Claude Code).

**@⁠efeekrb** on GitHub · 2026-11-25 17:45 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1943#issuecomment-7169952252) Tested on an NVIDIA GeForce RTX 4060 Laptop GPU (NVIDIA 610.78), Windows 11 build 26300, with an Intel UHD iGPU also present (`LEONIDA_VK_DEVICE=4060`). This PR's diff applies cleanly to `main` at `24a29f8` (where the test programs were built) and to `main` at `b9320a118` (`git apply --check`). - Unit tests with the PR applied: `tests/semantic/test_vcc_data_mask_src.py` ran 6 tests, OK. - `run_gpu_tests.py`, full suite, with the PR applied: 274/274 shaders translated and pass spirv-val. Native 209/264 passed, 54 skipped, 1 failed; `LEONIDA_W64_EMULATE=1` 243/264 passed, 20 skipped, 1 failed. The one failure is `cs_imgatomic_ext` (the one #1518 addresses), and the counts are identical to `main` at `24a29f8` without this PR. So no change on this GPU in either mode. The suite shows no regression; it does not tell whether a fixture exercises the rule this PR changes. No game files; counts and pass/fail lines only. AI-assisted (Claude Code).
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/1943/head:github-bot/gh-1943'
git switch 'github-bot/gh-1943'

Merge

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