Skip to content

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

Open
ShotTechOp wants to merge 1 commit into
ProjectLeonida:mainfrom
ShotTechOp:fix/short-co-ci-mask-operands
Open

ShotTechOp wants to merge 1 commit into
ProjectLeonida:mainfrom
ShotTechOp:fix/short-co-ci-mask-operands

Conversation

@ShotTechOp

Copy link
Copy Markdown
Contributor

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

…o_ci_u32 operand classification (SHADER-SHORT-CO-CI-MASK-OPERANDS)
@ecox982745

Copy link
Copy Markdown

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

@lixonik lixonik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@wavehs wavehs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@tiguan26 tiguan26 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approving e1ca6cc.

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

@Tauwni

Tauwni commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Tested e1ca6ccea merged onto main ec91d695c on a Mac (profile S), where wave64 is always emulated on 32-wide subgroups: Apple M3, macOS 26.5.1, MoltenVK 1.4.2, KosmicKrisp and llvmpipe (Mesa 26.3.1). No game files.

scripts/run_gpu_tests.py, auto mode, test programs from main:

Driver main main + this PR
MoltenVK 196/269 196/269
MoltenVK, LEONIDA_W64_EMULATE=1 230/269 230/269
KosmicKrisp 191/269 191/269
llvmpipe 177/269 177/269

The failures are the same on both sides (the known ones: #1724, #1518 and #1774 on MoltenVK, #2229's on KosmicKrisp). The translator suite passes with no failures.

Evidence: MEASURED, 1 run each. This only shows that nothing changes on Apple drivers. It says nothing about the game.

AI-assisted (Claude Code, Claude Opus 5.5).

@kawaii-shakal

Copy link
Copy Markdown
Contributor

CPU check on Linux, merged with main c0201a8: test_short_co_ci_masks.py 6/6 on the merge; copied onto main it fails (writes_exec subtests, wave32 data/VCC, and Unsupported: 64-bit operand for wave64), as you describe. Merged together with #2438, #2447, #2355 and #2360 the full translator suite passes (506 files, 6,415 tests).

Heads-up: #2447 fixes the same short form one level earlier. Its explicit_carry_vcc rewrites v_add_co_ci_u32 vD, src0, src1 (and sub/subrev) to the explicit form when the listing is read, so in _translate the len(a) == 3 branches here are no longer reached once both are in. They still cover direct callers of lane_masks / wave64_halves. Not a blocker, just so whoever lands second knows about the duplicate.

AI-assisted (Claude Code); I ran the tests and checked the results.


Generated by Claude Code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants