rdna2spirv, lane_masks, wave64_halves: fix 3-operand short-form v_*_co_ci_u32 operand classification (SHADER-SHORT-CO-CI-MASK-OPERANDS) #2357

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

@⁠ShotTechOp on GitHub · 2026-11-26 15:12 UTC · original
ShotTechOp:fix/short-co-ci-mask-operands → main · +177 −4 · 4 files · 1 commits · open

Item: SHADER-SHORT-CO-CI-MASK-OPERANDS

Problem

Gen._insn (tools/rdna2spirv.py) explicitly accepts the 3-operand short VOP2 assembler spelling v_add_co_ci_u32 vD, src0, src1 (and v_sub_co_ci_u32 / v_subrev_co_ci_u32), where carry-out and carry-in are both implicit vcc and operand 1 is the 32-bit source src0. However, four analysis helpers treated operand 1 as the VOP3b carry-out destination:

  1. tools/lane_masks.py (step): Called put(state, a[1], MASK, wave64), falsely marking src0 (e.g. an SGPR holding plain scalar data) as a lane mask and failing to mark implicit vcc as MASK. Downstream scalar ops reading src0 (e.g. s_bcnt1_i32_b32) then read the wave ballot (32 instead of popcnt(src0)).
  2. tools/rdna2spirv.py (widen_wave64_masks): Always used at = (1, 4) for _co_ci_, widening a 32-bit src0 SGPR (a[1]) to n=2 in wave64 shaders, which crashed translation with Unsupported("64-bit operand").
  3. tools/rdna2spirv.py (writes_exec): Treated a[1] as a destination on all VOP3_TWO_DESTS ops, so v_add_co_ci_u32 v1, exec_lo, v0 was falsely flagged as writing EXEC.
  4. tools/wave64_halves.py (operands): Split dests, srcs = [a[0], a[1]], list(a[2:]) when a[1] was an SGPR/special, dropping src0 from srcs, falsely claiming src0 was overwritten, and omitting implicit "vcc" from both defs and uses.

Fix

  • tools/lane_masks.py: For 3-operand v_*_co_ci_u32, put MASK on implicit vcc instead of a[1].
  • tools/rdna2spirv.py: In widen_wave64_masks, only widen (1, 4) when len(a) > 3; in writes_exec, skip the a[1] destination check for 3-operand *_ci_u32.
  • tools/wave64_halves.py: For 3-operand v_*_co_ci_u32, keep dests = [a[0]] and srcs = [a[1], a[2]], and append "vcc" to defs (len(srcs) == 2 then also adds "vcc" to uses).
  • tests/semantic/test_short_co_ci_masks.py: Unit + wave32/wave64 execution regression tests covering all four sites.

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_short_co_ci_masks.py -v -> 6/6 OK
    • python -I tests/test_lane_masks.py -> OK
    • python -I tests/semantic/test_wave64_halves.py -> 127/127 OK
    • python -I tests/test_text_write_line_endings.py -> 3/3 OK
    • python -m compileall -q tools/rdna2spirv.py tools/lane_masks.py tools/wave64_halves.py tests/semantic/test_short_co_ci_masks.py -> 0
    • python -I scripts/check_no_game_data.py ... -> 0
    • git diff --check -> 0
  • RED control (origin/main fbf100b1):
    • Running tests/semantic/test_short_co_ci_masks.py against origin/main's tools/ fails all 6 tests (14 assertion failures + 1 Unsupported("64-bit operand") crash on wave64).
**@⁠ShotTechOp** on GitHub · 2026-11-26 15:12 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2357) `ShotTechOp:fix/short-co-ci-mask-operands` → `main` · +177 −4 · 4 files · 1 commits · open Item: `SHADER-SHORT-CO-CI-MASK-OPERANDS` ### Problem `Gen._insn` (`tools/rdna2spirv.py`) explicitly accepts the 3-operand short VOP2 assembler spelling `v_add_co_ci_u32 vD, src0, src1` (and `v_sub_co_ci_u32` / `v_subrev_co_ci_u32`), where carry-out and carry-in are both implicit `vcc` and operand 1 is the 32-bit source `src0`. However, four analysis helpers treated operand 1 as the VOP3b carry-out destination: 1. **`tools/lane_masks.py` (`step`)**: Called `put(state, a[1], MASK, wave64)`, falsely marking `src0` (e.g. an SGPR holding plain scalar data) as a lane mask and failing to mark implicit `vcc` as `MASK`. Downstream scalar ops reading `src0` (e.g. `s_bcnt1_i32_b32`) then read the wave ballot (`32` instead of `popcnt(src0)`). 2. **`tools/rdna2spirv.py` (`widen_wave64_masks`)**: Always used `at = (1, 4)` for `_co_ci_`, widening a 32-bit `src0` SGPR (`a[1]`) to `n=2` in wave64 shaders, which crashed translation with `Unsupported("64-bit operand")`. 3. **`tools/rdna2spirv.py` (`writes_exec`)**: Treated `a[1]` as a destination on all `VOP3_TWO_DESTS` ops, so `v_add_co_ci_u32 v1, exec_lo, v0` was falsely flagged as writing EXEC. 4. **`tools/wave64_halves.py` (`operands`)**: Split `dests, srcs = [a[0], a[1]], list(a[2:])` when `a[1]` was an SGPR/special, dropping `src0` from `srcs`, falsely claiming `src0` was overwritten, and omitting implicit `"vcc"` from both `defs` and `uses`. ### Fix - **`tools/lane_masks.py`**: For 3-operand `v_*_co_ci_u32`, put `MASK` on implicit `vcc` instead of `a[1]`. - **`tools/rdna2spirv.py`**: In `widen_wave64_masks`, only widen `(1, 4)` when `len(a) > 3`; in `writes_exec`, skip the `a[1]` destination check for 3-operand `*_ci_u32`. - **`tools/wave64_halves.py`**: For 3-operand `v_*_co_ci_u32`, keep `dests = [a[0]]` and `srcs = [a[1], a[2]]`, and append `"vcc"` to `defs` (`len(srcs) == 2` then also adds `"vcc"` to `uses`). - **`tests/semantic/test_short_co_ci_masks.py`**: Unit + wave32/wave64 execution regression tests covering all four sites. ### 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_short_co_ci_masks.py -v` -> 6/6 OK - `python -I tests/test_lane_masks.py` -> OK - `python -I tests/semantic/test_wave64_halves.py` -> 127/127 OK - `python -I tests/test_text_write_line_endings.py` -> 3/3 OK - `python -m compileall -q tools/rdna2spirv.py tools/lane_masks.py tools/wave64_halves.py tests/semantic/test_short_co_ci_masks.py` -> 0 - `python -I scripts/check_no_game_data.py ...` -> 0 - `git diff --check` -> 0 - **RED control (`origin/main` `fbf100b1`)**: - Running `tests/semantic/test_short_co_ci_masks.py` against `origin/main`'s `tools/` fails all 6 tests (14 assertion failures + 1 `Unsupported("64-bit operand")` crash on wave64).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 15:15:45 +00:00
Author
Owner

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

On an RTX 4090, all 30 short-form carry/borrow dispatches at e1ca6ccea matched both an independent integer reference and the byte-for-byte GPU readback from the 30 corresponding explicit-form controls.

Listing and execution mode Parent fbf100b1b PR e1ca6ccea
Short form, native wave32 6/15 matched 15/15 matched
Short form, wave64 emulated on 32-lane subgroups All three shaders stopped at translation with 64-bit operand 15/15 matched
Explicit carry operands, both modes 30/30 matched 30/30 matched

Each case read back the per-lane arithmetic result, the population count of the original scalar source, and the population count of the outgoing carry or borrow mask. For example, with scalar source 1 and carry-in set on lanes 0–2, the parent’s short-form addition reported a source population count of 32 in lane 0. The PR returned 1. The matrix covered add, subtract and reverse subtract. All 12 PR modules passed spirv-val --target-env vulkan1.2, and the six committed CPU tests passed.

I also tested how this composes with #2355’s bit-set fix. On the clean local merge of the two heads (tree 3b8e75816), each shader changed selected VCC bits immediately before the short-form arithmetic consumed them. All 30 additional dispatches matched the integer reference, including emulated high-half carry-in.

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:20 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2357#issuecomment-7196120263) On an RTX 4090, all 30 short-form carry/borrow dispatches at `e1ca6ccea` matched both an independent integer reference and the byte-for-byte GPU readback from the 30 corresponding explicit-form controls. | Listing and execution mode | Parent `fbf100b1b` | PR `e1ca6ccea` | | --- | --- | --- | | Short form, native wave32 | 6/15 matched | 15/15 matched | | Short form, wave64 emulated on 32-lane subgroups | All three shaders stopped at translation with `64-bit operand` | 15/15 matched | | Explicit carry operands, both modes | 30/30 matched | 30/30 matched | Each case read back the per-lane arithmetic result, the population count of the original scalar source, and the population count of the outgoing carry or borrow mask. For example, with scalar source `1` and carry-in set on lanes 0–2, the parent’s short-form addition reported a source population count of `32` in lane 0. The PR returned `1`. The matrix covered add, subtract and reverse subtract. All 12 PR modules passed `spirv-val --target-env vulkan1.2`, and the six committed CPU tests passed. I also tested how this composes with [#2355’s bit-set fix](https://git.gtahub.st/github/ubiquitarianism--leonida/pulls/2355). On the clean local merge of the two heads (tree `3b8e75816`), each shader changed selected VCC bits immediately before the short-form arithmetic consumed them. All 30 additional dispatches matched the integer reference, including emulated high-half carry-in. 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: 6/6 short-form carry-in/carry-out semantic tests passed, covering the three-operand form, wave32 and wave64. No findings from these checks.

AI-assisted.

**@⁠lixonik** on GitHub · 2026-11-26 15:37 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2357#pullrequestreview-6076878337) · **reviewed** Windows 11: 6/6 short-form carry-in/carry-out semantic tests passed, covering the three-operand form, wave32 and wave64. No findings from these checks. AI-assisted.
Author
Owner

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

macOS arm64 check of #2357 (SHADER-SHORT-CO-CI-MASK-OPERANDS). Analysis-only translator change plus one semantic test; the GPU part is MoltenVK/Kosmickrisp.

Tested head e1ca6ccea merged onto main bdbc7e923 as merge 540bafef8 (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 shows the bug

  • python -I tests/semantic/test_short_co_ci_masks.py: Ran 6 tests ... OK at the head and after merging onto bdbc7e923.
  • Negative control (test kept, tools/rdna2spirv.py, tools/lane_masks.py and tools/wave64_halves.py put back to main's): FAILED (failures=8, errors=1).
    • The error is the real failure mode, not just a model mismatch: test_wave64_short_co_ci_translates_and_runs aborts with rdna2spirv.Unsupported: 64-bit operand on main, because widen_wave64_masks widens src0 (an SGPR used as a value) to a pair for the 3-operand spelling.
    • The analysis tests fail with AssertionError: 10 unexpectedly found in {10: 'mask'} (lane masks marks s10, the source, as a mask destination) for v_add_co_ci_u32, v_sub_co_ci_u32 and v_subrev_co_ci_u32 in wave32 and wave64, and the wave64_halves test fails too.
  • The four sites the fix touches (lane_masks step, writes_exec, widen_wave64_masks, wave64_halves operands) each have an assertion, so the test is not only an end-to-end check.

No regressions

  • Semantic suite: 500 files / 6,367 tests / 0 failed at main bdbc7e923; 501 files / 6,373 tests / 0 failed at the merge (+1 file, +6 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). cs_w64_mask_imm.asm and ps_ops2.asm use v_*_co_ci_u32, so the 5-operand path is covered there, and its results are unchanged.

Findings

  1. The same operand assumption is still in two analysis tools: scripts/audit_shader4_regs.py:216-221 (is_dual_dst on any _co_ op, then dsts = args[:2]) and scripts/vs_input_survey.py:86-88 (same len(args) >= 2 test). Given the 3-operand spelling, both treat src0 as the carry-out destination, exactly what this PR fixes elsewhere. It does not affect corpus reports today because llvm-mc/the disassembler print the _e32 form with five operands (vD, vcc_lo, src0, src1, vcc_lo), as tests/semantic/test_carry_exec_mask.py shows; but the translator and its tests accept the short spelling, so an audit run on hand-written .s input would be wrong. Adding the same len(args) == 3 exclusion plus a line in tests/test_audit_shader4_regs.py / tests/test_vs_input_survey.py closes it.
  2. GPU evidence for the short spelling itself: none of the shaders in runtime/test/shaders uses the 3-operand form, so the GPU runs above only show the 5-operand path is unchanged. That is expected for an analysis fix (no SPIR-V text changes for correct inputs), but worth saying in the description so nobody reads the green suite as hardware confirmation of the short form.

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:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/2357#pullrequestreview-6078945566) · **reviewed** **macOS arm64 check of #2357** (SHADER-SHORT-CO-CI-MASK-OPERANDS). Analysis-only translator change plus one semantic test; the GPU part is MoltenVK/Kosmickrisp. Tested head `e1ca6ccea` merged onto `main` `bdbc7e923` as merge `540bafef8` (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 shows the bug** - `python -I tests/semantic/test_short_co_ci_masks.py`: `Ran 6 tests ... OK` at the head and after merging onto `bdbc7e923`. - Negative control (test kept, `tools/rdna2spirv.py`, `tools/lane_masks.py` and `tools/wave64_halves.py` put back to `main`'s): `FAILED (failures=8, errors=1)`. - The error is the real failure mode, not just a model mismatch: `test_wave64_short_co_ci_translates_and_runs` aborts with `rdna2spirv.Unsupported: 64-bit operand` on `main`, because `widen_wave64_masks` widens `src0` (an SGPR used as a value) to a pair for the 3-operand spelling. - The analysis tests fail with `AssertionError: 10 unexpectedly found in {10: 'mask'}` (lane masks marks `s10`, the source, as a mask destination) for `v_add_co_ci_u32`, `v_sub_co_ci_u32` and `v_subrev_co_ci_u32` in wave32 and wave64, and the `wave64_halves` test fails too. - The four sites the fix touches (lane_masks step, writes_exec, widen_wave64_masks, wave64_halves operands) each have an assertion, so the test is not only an end-to-end check. **No regressions** - Semantic suite: 500 files / 6,367 tests / 0 failed at `main` `bdbc7e923`; 501 files / 6,373 tests / 0 failed at the merge (+1 file, +6 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). `cs_w64_mask_imm.asm` and `ps_ops2.asm` use `v_*_co_ci_u32`, so the 5-operand path is covered there, and its results are unchanged. **Findings** 1. The same operand assumption is still in two analysis tools: `scripts/audit_shader4_regs.py:216-221` (`is_dual_dst` on any `_co_` op, then `dsts = args[:2]`) and `scripts/vs_input_survey.py:86-88` (same `len(args) >= 2` test). Given the 3-operand spelling, both treat `src0` as the carry-out destination, exactly what this PR fixes elsewhere. It does not affect corpus reports today because llvm-mc/the disassembler print the `_e32` form with five operands (`vD, vcc_lo, src0, src1, vcc_lo`), as `tests/semantic/test_carry_exec_mask.py` shows; but the translator and its tests accept the short spelling, so an audit run on hand-written `.s` input would be wrong. Adding the same `len(args) == 3` exclusion plus a line in `tests/test_audit_shader4_regs.py` / `tests/test_vs_input_survey.py` closes it. 2. GPU evidence for the short spelling itself: none of the shaders in `runtime/test/shaders` uses the 3-operand form, so the GPU runs above only show the 5-operand path is unchanged. That is expected for an analysis fix (no SPIR-V text changes for correct inputs), but worth saying in the description so nobody reads the green suite as hardware confirmation of the short form. `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 e1ca6ccea.

The three-operand short form of v_add/sub/subrev_co_ci_u32 carries in and out through an implicit VCC, and the change teaches that to all three analyses consistently: lane_masks.step and wave64_halves.operands record VCC as its mask definition, writes_exec no longer reads its second operand as a VOP3 carry-out destination, and widen_wave64_masks leaves its operands alone.

test_short_co_ci_masks.py 6 passed (12 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/2357#pullrequestreview-6083600746) · **approved these changes** Approving e1ca6ccea. The three-operand short form of `v_add/sub/subrev_co_ci_u32` carries in and out through an implicit VCC, and the change teaches that to all three analyses consistently: `lane_masks.step` and `wave64_halves.operands` record VCC as its mask definition, `writes_exec` no longer reads its second operand as a VOP3 carry-out destination, and `widen_wave64_masks` leaves its operands alone. `test_short_co_ci_masks.py` 6 passed (12 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/2357/head:github-bot/gh-2357'
git switch 'github-bot/gh-2357'

Merge

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