Skip to content

rdna2spirv: fix operand layout and carry mask for _co_ci_u32 SDWA/DPP - #3553

Open
Wasler wants to merge 2 commits into
ItsNotRexed:mainfrom
Wasler:fix/co-ci-sdwa-dpp
Open

Wasler wants to merge 2 commits into
ItsNotRexed:mainfrom
Wasler:fix/co-ci-sdwa-dpp

Conversation

@Wasler

@Wasler Wasler commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-637 (claim in #3047, carried forward to #3381)

What and why

v_add_co_ci_u32, v_sub_co_ci_u32, and v_subrev_co_ci_u32 use the VOP2 5-operand carry layout [vdst, cd, src0, src1, cin] in _sdwa and _dpp encodings, placing the carry-out scalar mask destination (cd) at args[1] before src0 (args[2]), src1 (args[3]), and cin (args[4]).

Two translator bugs broke these six forms:

  • Operand indexing (Gen.insn / Gen.dpp_insn): Both hardcoded first = 1 (args[1]) as src0. Under _sdwa, src0_sel was applied to cd, src1_sel to src0, and src1 (including sext(src1)) received no selection. Under _dpp, reading args[1] (vcc_lo/vcc/SGPR mask) failed with Unsupported: mask destination and never shuffled src0 (for v_subrev_co_ci_u32_dpp as well, RDNA2 shuffles src0 at args[2] rather than src1).
  • DPP disabled-lane scalar carry mask (_co_ci_u32): When a lane is disabled by row_mask, bank_mask, or an out-of-row/fi:0 bound_ctrl:0 invalid source (self.dpp_guard), hardware keeps vdst[lane] and clears the scalar carry-out bit cd[lane] = 0 rather than writing carry-out from the fallback value or leaking prior cd bits.

Changes:

Tested

  • GPU (NVIDIA GeForce RTX 5060 Ti, driver 621.01, Windows 11 x64, LLVM 23.1.6, SPIRV-Tools 2026.4):
    • python -I scripts/run_gpu_tests.py --build runtime/build --llvm-mc llvm-mc.exe: 205/205 GPU test shaders translate and pass spirv-val (154/200 passed, 46 skipped, 0 failed — skipped are native-wave64/mesh/D3D12-harness fixtures unavailable on 32-wide Vulkan compute). Includes PASS cs_co_ci_sdwa_dpp 268374580 4294868992 8192 30583 65546 131066 6 43981 (no game shader corpus locally; before this fix, v_{add,sub,subrev}_co_ci_u32_dpp raised Unsupported: mask destination and _sdwa misapplied selects).
    • With LEONIDA_W64_EMULATE=1: PASS cs_dpp_w64 1 0 3 2 5 4 7 6 (1/1 passed); synthetic wave64 --wave64-emulate modules containing v_{add,sub,subrev}_co_ci_u32_{dpp,sdwa} assemble with spirv-as and pass spirv-val.
  • Differential & cross-lane fuzzers:
    • python -I tools/sdwa_diff.py --self-test -> passed
    • python -I tools/sdwa_diff.py --programs 1000 --seed 1 -> 940 pass, 0 mismatch, 60 skipped
    • python -I tools/sdwa_diff.py --ops v_add_co_ci_u32,v_sub_co_ci_u32,v_subrev_co_ci_u32 --programs 1000 --seed 7 -> 1000 pass, 0 mismatch, 0 skipped
    • python -I tools/crosslane_fuzz.py --no-maskdata --programs 50 --seed 1 --dpp -> wave32 native 20 match, 0 mismatch, 5 hyp_only; wave64 emulate 22 match, 0 mismatch, 3 hyp_only
  • CPU / CI suites:
    • python -m pytest tests/test_docs_design_rows.py tests/test_docs_links.py tests/test_docs_list_items.py tests/test_docs_repo_paths.py tests/test_text_write_line_endings.py tests/test_sdwa_diff.py tests/test_shader_checks.py tests/test_shader_diff.py tests/test_lane_masks.py tests/test_wave64_halves_fixtures.py -q -> 141 passed, 252 subtests passed
    • python -m pytest tests/test_translate.py -q -> 564 passed, 4 skipped
    • python -I tests/semantic/test_co_ci_sdwa_dpp.py -v -> 10/10 OK; python -I tests/semantic/test_carry_exec_mask.py -v -> 9/9 OK
    • All 333 files in tests/semantic/test_*.py (python -m unittest <stem>) -> 7332 tests, 0 failures
    • python -I tests/semantic/regen_fixtures.py --check, python -m compileall -q make_game.py tools tests, node --test tests/test_*.cjs (42/42) -> passed
  • Note: The dpp_guard scalar carry-out zeroing rule (cd[lane] = 0 on DPP-disabled lanes) follows the AMD RDNA2/RDNA3.5 ISA and was verified on NVIDIA Vulkan and CPU, not yet measured on physical RDNA2 hardware.

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 6ae5aff against the hardware. Measured on gfx1036 (RDNA2, driver 26.10.1, game MODE; integer ops) through raw hw_oracle kernels, one lane: v_add_co_ci_u32_sdwa, v_sub_co_ci_u32_sdwa and v_subrev_co_ci_u32_sdwa v8, vcc_lo, S0, S1, vcc_lo dst_sel:DWORD dst_unused:UNUSED_PAD with random src0_sel / src1_sel over BYTE_0..3, WORD_0, WORD_1, DWORD, sext() on either source in half the cases, random carry-in and edge-value operands: 300 cases. The hardware equals the formula (selected field, zero or sign extended, a + b + cin / a - b - cin / b - a - cin; carry-out in VCC bit 0): 300 of 300. I then ran the same 300 programs through this branch's translation (semantic interpreter): 300 of 300 identical to the hardware (word and carry bit), so the src0 / src1 selection and the carry layout of the SDWA forms are right.

DPP, measured only: v_add_co_ci_u32_dpp v8, vcc_lo, v1, v2, vcc_lo row_shr:1 [bound_ctrl:0] over 32 lanes with VCC seeded all ones and no carries in range: lanes 0 and 16 have no source (row_shr:1 reads outside the row). With bound_ctrl:0 they compute with a zero source (0x41 = 0 + 0x40 + carry-in); without it they keep the old vdst (0xdeadbeef) and VCC afterwards is 0x00000000 in both runs, i.e. the carry-out bit of a disabled lane is cleared, not left at the seeded 1, as the PR says. I did not run this PR's DPP translation, and the quad_perm / row_mask / bank_mask variants I tried did not assemble in that operand order, so those masks are not measured. The scripts are local, not pushed. AI-assisted.

@kacper-serewis kacper-serewis 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.

At 6ae5aff, 19 focused tests passed. Independent CPU interpretation checked destination and carry/borrow on 384 lanes across 12 listings. SDWA, quad_perm and row_shr with omitted bound_ctrl matched; six boundary-lane differences with explicit bound_ctrl:0 agree with the existing hardware review. This supports the operand-layout change and leaves the explicit-boundary semantics unresolved. AI-assisted synthetic CPU validation.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Hardware data for the _dpp half, since the design row here says "not measured on physical RDNA2 hardware". Raw wave64 (and wave32 for the non-carry ops) kernels through tools/hw_oracle on a gfx1036 (Radeon iGPU, Ryzen 9 9950X3D):

  • Carry-out of unwritten lanes: confirmed. For v_add_co_ci_u32_dpp and v_sub_co_ci_u32_dpp the carry-out mask is rewritten whole; a lane that is off in EXEC, turned off by row_mask/bank_mask, or has an invalid source with bound_ctrl:0 gets 0 whatever the mask held, and bound_ctrl:1 computes with the selected operand = 0 (60 random programs and a 48-case table, all lanes).
  • v_subrev_co_ci_u32_dpp selects the other operand. With v_subrev_co_ci_u32_dpp v2, vcc, v3, v4, vcc row_shr:1 (v3 = 3·lane + 100, v4 = 5·lane + 1000, carry-in 0) lane 1 gives 0x381 = v4[lane 0] − v3[lane 1], not v4[lane 1] − v3[lane 0] = 0x389; with bound_ctrl:1 lane 0 gives 0xffffff9c = 0 − v3[0]. So the lane select and the bound zero land on the second source operand (args[3], the minuend), not on args[2]. v_add_co_ci_u32_dpp and v_sub_co_ci_u32_dpp select args[2] as this PR has it.

The same holds for the non-carry reversed integer ops (v_subrev_nc_u32, v_lshlrev_b32, v_lshrrev_b32, v_ashrrev_i32), which is #3672; the float v_subrev_f32/v_subrev_f16 select the first operand. Case 7 of cs_co_ci_sdwa_dpp (quad_perm, lane 2 reading src0 from lane 3) would come out different on an AMD GPU. I have not measured the _sdwa forms.

AI-assisted.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

The _sdwa half, measured now on the same gfx1036 (raw wave32 kernels, 32 lanes with different operands, result and carry-out of every lane): for v_add_co_ci_u32_sdwa, v_sub_co_ci_u32_sdwa and v_subrev_co_ci_u32_sdwa, src0_sel and sext() apply to the first source operand and src1_sel to the second, in assembler order, for all three ops (five select combinations each: BYTE_1/WORD_1, sext WORD_0/BYTE_3, BYTE_2/sext WORD_0, DWORD/BYTE_0, sext WORD_1/DWORD). So the SDWA operand layout in this PR is what the hardware does; only the _dpp select of v_subrev_co_ci_u32 is on the other operand, as in my comment above.

AI-assisted.

@crastinator

Copy link
Copy Markdown
Contributor

Tested 6ae5aff merged into main f847748c (clean merge) on AMD Radeon RX 6700 XT (Navi 22, gfx1031, subgroup 32..64), RADV Mesa 26.3.1, CachyOS, Linux 7.3.5, GCC 16.2.1, llvm-mc 23.1.5: pass.

  • New test passes natively: cs_co_ci_sdwa_dpp (268374580 4294868992 8192 30583 65546 131066 6 43981).
  • Whole run_gpu_tests.py: 186/200 native, 186/200 with LEONIDA_W64_EMULATE=1 (the skips are the D3D12-only fixtures and ps_w64x_readlane, as on main). ctest 41/41.
  • tests/semantic/test_co_ci_sdwa_dpp.py, tests/test_sdwa_diff.py: 31 passed, 33 subtests passed.

No game files. AI-assisted (Claude Code).

@Wasler

Wasler commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

@Vonsek Thank you for the RDNA2 (gfx1036) hardware verification!

Updated in 92e9c26:

  • Gen.dpp_insn ( ools/rdna2spirv.py) now applies the DPP lane select and �ound_ctrl:1 zero-substitution to �rgs[3] (the minuend / second source operand) for �_subrev_co_ci_u32_dpp, while keeping �rgs[2] (src0) for �_add_co_ci_u32_dpp / �_sub_co_ci_u32_dpp and keeping _sdwa selecting src0_sel on �rgs[2] and src1_sel on �rgs[3] in assembler order.
  • Updated
    untime/test/shaders/cs_co_ci_sdwa_dpp.asm case 7 (out[6]) so both sources vary per lane (�12 = 10 + lane, �19 = 20 + 4*lane, checking 32 - 12 - 1 = 19 =

… (SHADER-637)

# Conflicts:
#	docs/design.md
#	scripts/run_gpu_tests.py
#	tests/semantic/regen_fixtures.py
@Wasler
Wasler force-pushed the fix/co-ci-sdwa-dpp branch from 92e9c26 to 2fadac6 Compare November 22, 2026 22:07
@JRamos1993

JRamos1993 commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Tested 2fadac6, alone and merged onto main aa2988c (Windows 11, Python 3.14, llvm-mc 21.1.8; GPU suite on an RTX 5090, driver 616.92).

  • GPU suite: scripts/run_gpu_tests.py gives 165/211 passed, 46 skipped, 0 failed. That's main's 164/210 plus the new cs_co_ci_sdwa_dpp, which passes.
  • tests/semantic/test_co_ci_sdwa_dpp.py: 10 OK.

tests/test_sdwa_diff.py fails 1 of 21, on this head and merged; it passes on main aa2988c. test_short_seeded_run_matches reports program 36: word 3 (VCC): reference 0x00000000, translated 0x00000001. The ops that set VCC there are:

s_mov_b32 vcc_lo, 0
v_cmp_nlt_f16_sdwa vcc_lo, -v6, v2 src0_sel:WORD_1 src1_sel:DWORD
v_cmp_class_f16_sdwa vcc_lo, 2.0, 1.0 src0_sel:DWORD src1_sel:WORD_1

with v2 = 0x80000000 and v6 = 0x78c9e147.

The translator change isn't the cause. With the merge base's tools/rdna2spirv.py and this PR's tools/sdwa_diff.py and test, the same program fails the same way. The new generator ops shift the seeded programs, so this run now reaches a case where main's translator and the reference already disagree.

The last compare reads an inline constant through src1_sel:WORD_1, so it depends on how the class-mask operand 1.0 is laid out for an f16 SDWA op: f16 0x3C00 in the low half gives a WORD_1 of 0, while f32 0x3F800000 gives 0x3F80. Settling which one hardware uses would decide whether the translator or sdwa_diff's reference needs the fix. Until then, the seeded run is red with this PR.

AI-assisted (Claude Code).

@PingasMech

Copy link
Copy Markdown

I reproduced the program 36 failure at 2fadac65 on main 98fa503f. #3894 (ce724210) fixes it with one translator change. For an f16 class comparison, the inline mask 1.0 is binary16 0x00003c00, so selecting its upper 16 bits gives zero. #3894 cites the native gfx1036 measurement for this rule.

I kept #3553’s generator, seed, reference model and tests identical. Changing only the mask read in Gen.cmp_class to use src_h for f16 took tests/test_sdwa_diff.py from 21/22 to 22/22. In a separate synthetic compute test on an Intel UHD 770 (Windows driver 32.0.101.7088), the failing comparison changed from true to false as expected. A second inline-mask case changed from false to true, and both register-mask controls kept their expected results.

With both PRs on that main commit, the 22 differential tests, 10 carry tests and five class-comparison tests passed. The original cs_co_ci_sdwa_dpp GPU fixture passed too. Git reported conflicts only in docs/design.md; I kept the current-side text in those hunks for this code test.

I’d keep #3553’s seed and expected result, and land #3894’s mask fix first. AI-assisted review using synthetic inputs.

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.

6 participants