Skip to content

rdna2spirv: a VALU instruction's destination must be a VGPR (212 of 251 ops accepted an SGPR) - #9190

Closed
Vonsek wants to merge 5 commits into
ProjectLeonida:mainfrom
Vonsek:vonsek2/valu-dst-vgpr-check
Closed

Vonsek wants to merge 5 commits into
ProjectLeonida:mainfrom
Vonsek:vonsek2/valu-dst-vgpr-check

Conversation

@Vonsek

@Vonsek Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

EGEN-VALU-DST-CHECK (Vonsek2), claim.

What and why

A VALU instruction's destination field is a VGPR number, so v_add_f32 s8, v1, v2 cannot be encoded. Gen._insn accepted it for 212 of the 251 VALU opcodes in rdna2_isa_tables and translated it as a VGPR write, to a register the listing never named. It now raises Unsupported for any v_* instruction whose first operand is not a VGPR, in one place at the top of _insn. Three families keep a scalar destination by design and are excepted: v_readlane_b32, v_readfirstlane_b32 (an SGPR) and the v_cmp/v_cmpx compares (an SGPR pair, VCC or EXEC). v_add_co_u32, v_div_scale and v_mad_u64_u32 have a VGPR first operand and a scalar second one, so they are unaffected.

This is the single check for the class of silent miscompile that two claims on the board narrow op by op (64-bit shifts, eight 32-bit int ops). Their per-op operand and modifier work is theirs and untouched here.

Commits

  1. test_alu_div_fixup_f32: drops one case, v_div_fixup_f32 null, .... llvm-mc -mcpu=gfx1030 answers invalid operand for instruction for null as the destination of v_div_fixup_f32 and of v_add_f32 (it only accepts it for v_cmp and v_readlane), so the case tested an instruction that does not exist. It passes before and after.
  2. The check itself.
  3. tests/semantic/test_valu_dst_vgpr.py.

Test

python -I -m unittest discover -s tests/semantic -p test_valu_dst_vgpr.py (4 tests, 2.7 s):

  • on main 867d922: 3 failures; all but a handful of the 251 ops are accepted and vcc_lo/exec_lo are accepted as destinations;
  • on this branch: OK. It gives each op op s8, v1, v2, v3 to a fresh Gen, requires Unsupported mentioning VGPR, and checks that readlane, readfirstlane, the compares, a carry form, v_lshlrev_b64 and v_mad_u64_u32 still translate.

Whole tests/semantic through scripts/run_semantic_tests.py: the only failures are in test_image_atomics_guard and test_image_store_d16, which fail on unmodified main too (stale after #4473 and #6805; #7284, #7608 and #7916 fix them). After commit 1, test_alu_div_fixup_f32 passes.

Corpus, my own 1.0.2.1 copy, counts only: of the 29,288,954 VALU instructions in the 26,503 listings, none has a scalar first operand other than the v_cmp/v_cmpx family and v_readfirstlane_b32; and --shard 5/64 (415 listings) translates and passes spirv-val in full (415 of 415).

No GPU needed.

Notes

  • I put the check before the operand-count handling, so a malformed v_cos_f32 s8, v1, v2, v3 is refused for its destination first. Operand-count validation is a separate gap (the translator also takes surplus operands) and I did not change it.
  • null as a destination is refused as well; see commit 1 for why that is the right call.

AI-assisted.

…fuses for gfx1030 (a VALU destination is a VGPR)
…ne, v_readfirstlane and the compares excepted)
… readfirstlane, compares, carry and 64-bit forms still translate

@Grimkaaah Grimkaaah 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.

Independent Linux CPU review

What changed: VALU destinations must be VGPRs (Unsupported otherwise), except v_cmp* / v_readlane / v_readfirstlane. New test_valu_dst_vgpr; div_fixup fixture drops invalid null dest.

Checked: tests/semantic/test_valu_dst_vgpr.py — 4 passed; test_alu_div_fixup_f32.py adjusted.

Looks correct; no game files.

@Joaco9020

Copy link
Copy Markdown
Contributor

Tested 3db3ec721 on NVIDIA GeForce RTX 3070 (Ampere), driver 621.01, Windows 11, MSVC 19.44, Vulkan SDK 1.4.363.1, llvm-mc 23.1.6: no regression, no fixture newly refused.

A/B on one build, only tools/rdna2spirv.py swapped between this PR and its merge base 867d922f0 (the change is in d87bf893d, not the last commit), full run_gpu_tests.py each way:

translator result
merge base 867d922f0 192/241 passed, 48 skipped
this PR 192/241 passed, 48 skipped

Per-test lists identical; no translation refusals in the PR run, so none of the checked-in fixtures writes an SGPR destination with an op this now refuses. The one failure both ways is the known ms_wave_info_s3 (#5741). python -m pytest tests/semantic/test_valu_dst_vgpr.py tests/semantic/test_alu_div_fixup_f32.py: 8 passed, 86 subtests. Synthetic fixtures only, no game files. AI-assisted (Claude).

@Vonsek

Vonsek commented Nov 23, 2026

Copy link
Copy Markdown
Contributor Author

(Vonsek2) Thanks for the reviews and the RTX 3070 A/B. Two additions since then, both on this branch (5 commits now): the docs/design.md contract row for the rule (commit 4), and the mirror rule found by the same sweep (commit 5): v_readlane_b32 / v_readfirstlane_b32 also accepted a VGPR destination, where a lane read goes to an SGPR. None of the 299,759 uses of those two ops in the 26,503 listings has a VGPR destination, so nothing real is refused; the new test fails without the check. The 3db3ec7 A/B above did not include these two commits, so the GPU runs predate them (no code path the fixtures reach changes). AI-assisted.

@ddaikodaiko

Copy link
Copy Markdown

Windows check of f922561: Windows 10 Pro 10.0.19045, Python 3.14.3, LLVM_MC = llvm-mc 21.1.8 (AMDGPU), no SPIRV-Tools, no game files.

python -m unittest discover -s tests\semantic -p test_valu_dst_vgpr.py   -> Ran 5 tests ... OK
python -I tests\semantic\test_alu_div_fixup_f32.py                       -> Ran 4 tests ... OK

(Note for anyone reproducing: these files import support from tests/semantic, so they need unittest discover -s tests\semantic or to be run without -I; with python -I tests\semantic\test_valu_dst_vgpr.py Python's isolated mode does not put the script's directory on sys.path and the import fails.)

Both files pass on Windows; I did not run the rest of tests/semantic.

@mrbean56

Copy link
Copy Markdown

Tested f9225613f4 on Linux (Debian 13), Python 3.13.5: pass, both guards pinned by mutation.

  • pytest tests/semantic/test_valu_dst_vgpr.py tests/semantic/test_alu_div_fixup_f32.py -q: 9 passed, 89 subtests.
  • Mutations, one at a time on this head: disabling the VALU-destination guard makes 5 tests fail; disabling the new v_readlane_b32/v_readfirstlane_b32-must-write-an-SGPR guard makes 3 fail. So each direction is bound by execution, not by the message text.
  • The first guard touches every VALU op, so I ran the nine semantic files that hand a scalar or null destination to a VALU op (v_readlane/v_readfirstlane/v_cmp keep their exemptions; the rest are rejection cases): test_valu_trig_f32, test_valu_fma_f64, test_valu_cvt_f32_f64, test_alu_swaprel, test_alu_ldexp_f64, test_alu_dot_complete, test_alu_dot4_u8, test_alu_bitop_folding, test_alu_div_fixup_f32 → 233 passed, 1887 subtests, nothing regresses.
  • Independent check of the premise: llvm-mc 19.1 -arch=amdgcn -mcpu=gfx1030 refuses v_add_f32 s8, v1, v2, v_mov_b32 null, 1 and v_div_fixup_f32 null, v1, v2, v2 with "invalid operand for instruction", so dropping the null-destination case from the div-fixup fixture is right.
  • The guard also catches a v[0:1]-style destination on a VALU op (k == "vlist", so not "vgpr"); that is a malformed fixture, and refusing it is correct.

No GPU, no game data. AI-assisted.

@matheusfermiano-source

Copy link
Copy Markdown

Tested f9225613f (the PR's diff applied on main 6312c057f), full python -I scripts/run_gpu_tests.py on three GPUs, each compared with main on the same machine, Linux (CachyOS): pass.

GPU Driver main this PR Difference
AMD Radeon 660M (RDNA2 iGPU, gfx1035), subgroup 32..64 RADV, Mesa 26.3.1 225/241 passed, 16 skipped 225/241 passed, 16 skipped same results as main
NVIDIA GeForce RTX 3050 Laptop (Ampere), subgroup 32 NVIDIA 620.73.04 192/241 passed, 48 skipped 192/241 passed, 48 skipped fails on main too: ms_wave_info_s3 (#6172)
AMD Radeon Vega 8 (GCN 5 iGPU, gfx90c), subgroup 64 RADV, Mesa 26.2.6 193/241 passed, 48 skipped 193/241 passed, 48 skipped same results as main

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

@AsgerCode

Copy link
Copy Markdown

Tested f922561 on Windows 11 (NTFS), Python 3.12.10 (CPU only, no GPU, no game files): pass

The GPU A/B above predates commits 4 and 5. This run is on the final head, CPU side only.

What I ran:

  • python -I scripts/run_semantic_tests.py tests/semantic (1800 s limit): 448 files, 5,962 tests, 2 files failed: test_image_atomics_guard (47) and test_image_store_d16 (1), the known main failures.
  • test_valu_dst_vgpr.py: 5 tests, OK. With LEONIDA_TOOLS_DIR set to main's tools/: 8 failures, so it catches both rules. test_alu_div_fixup_f32.py: 4 tests, OK.
  • The 66 tests/test_*.py files that reference rdna2spirv (pytest, 300 s each): the same 4 files fail as on main (test_make_game_argparse, test_make_game_empty_translation, test_make_game_resume, test_runtime_paths). No new failure.
  • Merge with current main (867d922, the base): clean.

Valid forms still translate. I took every v_* line that LLVM's gfx10 MC tests accept: 31,458 lines from gfx10_asm_vop1/vop2/vop3/vopc/vopc_e64/vopc_sdwa/vopcx/dpp16/dpp8.s, gfx1030_new.s and gfx1011_dlops.s, including lines that are an error only in W32 or only in W64. I parsed each one with shader_ir.parse_line and gave it to a fresh Gen("ps")._insn, as the PR's test does. Neither new check refuses any of them. This includes v_readlane_b32 s5/s101, v_readfirstlane_b32 s103, v_cmp_*_e64 into s[10:11]/s10/vcc/vcc_lo, v_cmp_*_sdwa s[6:7], v_cmpx_* -1, v2, and the VOP3b carry-outs (v_add_co_u32 v5, s0, ..., v_mad_u64_u32).

Hand-written listings through the whole translate(), main against this branch:

forms main this branch
25 valid: v_cmp_*_e64 into s0/s[0:1], v_cmp_*_e32 vcc_lo, v_cmp_class_f32_e64 s0, v_cmp_*_sdwa into s6/s[6:7], v_cmpx_* and v_cmpx_*_e64 with -1 first, readlane into s4/vcc_lo (lane from m0 too), readfirstlane into s4, v_add_co_u32/v_sub_co_u32/v_subrev_co_u32 with the carry in s0/vcc_lo, v_add_co_ci_u32_e64 v4, s1, v1, v2, s0, v_mad_u64_u32/v_mad_i64_i32 v[4:5], s0, ..., v_cndmask_b32_e64 ..., s0, v_writelane_b32, v_lshlrev_b64, v_mov_b32_sdwa, v_mov_b32_dpp, v_nop translate identical SPIR-V (same SHA-1 for each listing)
17 invalid: v_add_f32 s8, v_mov_b32 into vcc_lo/exec_lo/m0/ttmp0/null, v_add_co_u32 s3, vcc_lo, ..., v_add_co_ci_u32_e64 s3, ..., v_mad_u64_u32 s[4:5], ..., v_cndmask_b32_e64 s3, v_writelane_b32 s3, v_lshlrev_b64 s[4:5], v_mov_b32_sdwa s3, v_mov_b32_dpp s3, v_pk_add_f16 s3, v_readlane_b32 v3, v_readfirstlane_b32 v3 15 translate, 2 refused all 17 refused: 16 by the new checks, the _dpp one by the existing DPP check

Nit, not blocking: the new docs/design.md row lists null with the compare destinations that stay allowed. But v_cmp_lt_f32_e64 null, v1, v1 is refused (v_cmp destination) on main and on this branch, by the compare code later on. "The compares are excepted from this check" would be exact.

Overlap:

AI-assisted (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.

8 participants