Skip to content

rdna2spirv: validate v_pk_*_b16/i16 shift modifiers and add semantic tests - #3853

Open
kotelkonrad wants to merge 1 commit into
ItsNotRexed:mainfrom
kotelkonrad:fix/pk-shift-b16-modifiers
Open

kotelkonrad wants to merge 1 commit into
ItsNotRexed:mainfrom
kotelkonrad:fix/pk-shift-b16-modifiers

Conversation

@kotelkonrad

@kotelkonrad kotelkonrad commented Nov 22, 2026 •

Copy link
Copy Markdown

Claimed in #3796: #3796 (comment)

What and why

tools/rdna2spirv.py had no dedicated semantic translator test for v_pk_lshlrev_b16, v_pk_lshrrev_b16, and v_pk_ashrrev_i16, and silently accepted unsupported modifiers (clamp, non-zero neg_lo/neg_hi, mul/div, source neg/abs, out-of-range/extra-element op_sel) or crashed with IndexError on short op_sel_hi:[1].

  • tools/rdna2spirv.py: reject clamp, unsupported modifier prefixes, source neg/abs, non-zero neg_lo/neg_hi, and malformed/out-of-range op_sel/op_sel_hi with Unsupported.
  • tests/semantic/test_valu_pk_shift_b16.py: translator-to-interpreter semantic tests (0..15 and & 15 shift masking, signed vs unsigned right shift, half isolation, op_sel/op_sel_hi permutations, SGPR/inline/literal sources, destination aliasing, seeded cases, rejected modifiers, and negative controls).

Testing

  • Before fix: python -I tests/semantic/test_valu_pk_shift_b16.py -v failed 13 invalid-modifier subtests (silently accepted) and errored on 1 (IndexError).
  • After fix (CPU/interpreter only, no GPU test needed, Windows 11, Python 3.13, no game files):
    • python -I tests/semantic/test_valu_pk_shift_b16.py -v: 8/8 passed
    • python -I tests/semantic/test_alu_pk_int16.py: 7/7 passed
    • python -I tests/semantic/test_mutations.py: 2/2 passed
    • python -I tests/test_text_write_line_endings.py: 2/2 passed

Depends on: none
AI-assisted: yes (CPU/interpreter only, no game data). Antigravity Gemini 4 Neon

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Measured on gfx1036 (RDNA2, driver 26.10.1, game MODE; integer ops) through raw hw_oracle kernels, one lane: v_pk_lshlrev_b16, v_pk_lshrrev_b16 and v_pk_ashrrev_i16 v8, v1, v2 (v1 the shift count, v2 the value) with edge and random words (0, 0xffff, 0x8000, 0x7fff, random halves) under four modifier sets: none, op_sel:[1,0] op_sel_hi:[0,1], op_sel:[1,1] op_sel_hi:[1,1] and op_sel:[0,1] op_sel_hi:[0,0], 100 cases each, 1200 in all. Every result equals, per lane, the shift of the selected half (shift amount & 15, logical or arithmetic) with the halves chosen by op_sel for lane 0 and op_sel_hi for lane 1: all 1200 match, so the semantics this PR's tests describe (0..15 and & 15 masking, signed vs unsigned right shift, half selection) are what the hardware does. The modifier refusals are not a hardware question; this PR's translation was not part of this run. The script is local, not pushed. AI-assisted.

@kotorvr

kotorvr commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Checked be744fc against my own 1.0.2.1 work folder (Windows 11, Python 3.13.7, counts only): the new refusals do not refuse any retail shader.

The question for this PR is whether rejecting clamp, neg_lo/neg_hi, source neg/abs, omod and malformed op_sel turns a shader that translates on main into an Unsupported one. So I looked at every shader that has one of the three ops:

  • grep over all 25,734 disassembly listings: v_pk_lshlrev_b16, v_pk_lshrrev_b16 and v_pk_ashrrev_i16 appear in 2 shaders, once each. The only modifiers on them are op_sel / op_sel_hi with in-range values; no clamp, neg_*, abs or omod.
  • Both translated with rdna2spirv.translate() from main f847748 (the branch's base) and from be744fc: both translate on both sides, and the SPIR-V text is byte-identical (same SHA-256).

On the branch: python -m unittest discover -s tests/semantic -p test_valu_pk_shift_b16.py 8 tests OK, -p "test_*pk*.py" 292 OK; python -I -m compileall -q make_game.py tools tests exit 0.

Not run: the GPU suite (none of its fixtures uses these ops), and the full semantic suite. No game data quoted. AI-assisted check (Claude Code).

@requiemsnkrp

Copy link
Copy Markdown

Tested be744fc3 (merged onto main aa2988cc) on NVIDIA GeForce RTX 3060, driver 32.0.16.1714, Windows 11, MSVC 19.51, Vulkan SDK 1.4.363.0: pass for this PR's tests. The test_mutations failure below comes from main.

  • With LLVM_MC = llvm-mc 21: python -I tests/semantic/test_valu_pk_shift_b16.py gives Ran 8 tests ... OK; test_alu_pk_int16.py Ran 7 tests ... OK; tests/test_text_write_line_endings.py Ran 3 tests ... OK.
  • tests/semantic/test_mutations.py: Ran 3 tests ... FAILED (failures=2), both on mutation='v_mul_legacy_f32 drops zero-times-anything guard': mutation anchor must occur exactly once. This PR doesn't touch that code. On origin/main itself, the v_mul_legacy_f32 branch of tools/rdna2spirv.py now begins with the comment # The inferred game's f32 denorm mode is flush-in/flush-out..., while main's test_mutations.py anchor still expects # Legacy multiply treats 0 * anything .... So main has the same failure: I checked this by comparing the two files and did not rerun the test on main. main's mutation anchor needs updating.
  • Full ctest (-E "present|swapchain|frame_pacer", LEONIDA_VK_VALIDATION=1, llvm-mc build, ctest fixtures translated by this rdna2spirv.py): 99% tests passed, 1 tests failed out of 148. The failure is run_tiny_game_dir (GAME: did not resolve to the game folder). It passes when rerun alone and failed the same way for an unrelated Python-only PR, so it looks like a -j 4 flake in a C++ loader test, not this change.

AI-assisted (Claude). No game files.

@dev-ewann

Copy link
Copy Markdown
Contributor

Reviewed and tested be744fc (merged on main aa2988c): the new tests pass, but the PR breaks an existing test, so I think it needs a fix.

Commands (Linux, python3 -I -B):

  • python -m unittest discover -s tests/semantic -p test_valu_pk_shift_b16.py: 8 tests OK.
  • python tests/test_pk16_diff.py: 15 tests OK.
  • python -m unittest discover -s tests/semantic -p test_value_semantics.py -k test_packed_b16_shifts_with_op_sel: error with this PR (Unsupported: v_pk_lshlrev_b16 modifiers), OK with main's tools/rdna2spirv.py on the same merge. In the full tests/semantic run it is the only new failure compared with main.

Cause: tests/semantic/fixtures/disasm/ps_ops6.s lines 7 and 10 have v_pk_lshlrev_b16 v5, v3, v4 op_sel:[1,0,0] op_sel_hi:[0,0,0] (and the same for v_pk_lshrrev_b16). The new len(...) != 2 checks reject them. llvm-mc 18 at gfx1013 accepts op_sel:[1,0,0] op_sel_hi:[0,0,0] (encodes it as op_sel:[1,0] op_sel_hi:[0,0]), and also short lists: op_sel_hi:[1] assembles as op_sel_hi:[1,0], so a missing element is 0, not the default 1. Either accept lengths 1..3 (pad with 0, ignore the third element, as llvm does), or update ps_ops6 and drop op_sel:[0,0,0] / op_sel_hi:[1] from bad_lines with a reason.

Checked with llvm-mc 18 at gfx1013, for the three ops:

  • clamp assembles (byte 1 = 0xc0); neg_lo:[1,0] assembles (byte 7 0x18 -> 0x38); neg_hi:[0,1] assembles (byte 1 = 0x42).
  • op_sel:[2,0], mul:2, -v1 and |v1| are errors.

So rejecting clamp, neg_lo/neg_hi and source neg/abs (encodable but not implemented) is right; only the length check is too strict. The changed block is only used by these three shifts; the other v_pk_* go through other branches.

Test expectations come from an independent ISA model (ref_pk_shift: 4-bit count mask, signed 16-bit extraction, op_sel half selection) with hand-computed sanity values. Good.

Mutations on the added lines of rdna2spirv.py (23, test = this PR's file): 18 killed, 5 survived.

  • clamp check -> False: equivalent (the prefix check still raises).
  • endswith("]") -> False, and except (IndexError, ValueError) -> except ValueError: unreachable, the operand parser already rejects op_sel:bad / op_sel:[0,10.
  • except -> except IndexError: the ValueError path (op_sel:[a,b]) has no test.
  • for b in sel + selhi -> for b in sel: no test passes an out-of-range op_sel_hi (e.g. op_sel_hi:[1,2]).

Suggest adding those two tests and dropping or documenting the unreachable branches.

No GPU used (CPU interpreter). AI-assisted (Claude), on behalf of dev-ewann.

@dcastell1

Copy link
Copy Markdown
Contributor

Ran the semantic suite on this branch at be744fc34 (full CPU): 1 error, and it is the PR's own new fixture:

ERROR: test_packed_b16_shifts_with_op_sel (test_value_semantics.TranslatorValueSemantics)
  self.check("ps_ops6", EXPECTED_OPS6)
rdna2spirv.Unsupported: v_pk_lshlrev_b16 modifiers

tests/semantic/fixtures/disasm/ps_ops6.s (added here) contains v_pk_lshlrev_b16 v5, v3, v4 op_sel:[1,0,0] op_sel_hi:[0,0,0], and the translator on the same commit still rejects v_pk_lshlrev_b16 with modifiers, so the case cannot pass. Either the validation needs to accept that exact combination (and the test then pins it) or the fixture uses a combination you intended to reject and EXPECTED_OPS6 has to come from a shader that translates.

For reference: test_value_semantics on main (aa2988cc5) is OK, so this is the branch, not the base.

@TreXito

TreXito commented Nov 22, 2026

Copy link
Copy Markdown

Tested be744fc3 (base f847748c) on Linux (Ubuntu 26.04 container, Python 3.14), NVIDIA RTX 3060 Ti, driver 580.173.02: pass, and the new tests are red on the base.

  • tests/semantic/test_valu_pk_shift_b16.py: 8 passed, 242 subtests passed. With tools/ from f847748c: 14 failed (Unsupported not raised for the rejected modifiers), which matches your before-fix run.
  • scripts/run_gpu_tests.py: 152/199 passed, 46 skipped. That matches main f847748c test for test on this card. The one failure, ps_sdwa_f16_const, fails on that base too and has since been fixed on main. ctest: 41/41 passed.
  • Not from this PR: on main aa2988cc, tests/semantic/test_mutations.py fails 2 subtests (the v_mul_legacy_f32 anchor). @requiemsnkrp saw that while testing this PR. It is tests/semantic: re-anchor the v_mul_legacy_f32 mutation after the flush change #3883.

AI-assisted (Claude Code). Synthetic test shaders only, no game files.

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