Skip to content

rdna2spirv: verify 16-bit add/sub clamp and op_sel, support VOP3 literals - #3228

Open
abnalstlll-byte wants to merge 1 commit into
ItsNotRexed:mainfrom
abnalstlll-byte:feat/sub-16bit-clamp-rdna2
Open

abnalstlll-byte wants to merge 1 commit into
ItsNotRexed:mainfrom
abnalstlll-byte:feat/sub-16bit-clamp-rdna2

Conversation

@abnalstlll-byte

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue: #3047 (comment)

What and why

  1. RDNA2 ISA Pre-flight & Clarification:
    • AMD dropped reversed 16-bit integer subtraction (�_subrev_nc_u16 / �_subrev_nc_i16 do not exist in the GFX10.3 instruction set; �_subrev_f16 is the only 16-bit reverse subtract opcode). Documented this hardware fact in docs/design.md row 261.
  2. 16-bit Integer VOP3 Literal Support & Half Preservation:
    • In ools/rdna2spirv.py, VOP3 instructions with 32-bit literal operands are 12 bytes long (len(enc) == 24), whereas previously only 8-byte encodings (len(enc) == 16) were recognized as VOP3, causing literal forms to be misclassified as VOP2 and erroneously zeroing the destination's high half.
    • Identified VOP3 via major opcode bits 31:26 ((int(enc[6:8], 16) & 0xFC) in (0xD4, 0xD0)), ensuring 16-bit integer add/sub with literals properly preserves the untouched destination half.
    • Rejection guard added if op_sel selects the high half of a non-VGPR destination.
  3. Hardware Oracle / Probe Coverage:
    • Added �_add_sub_nc_i16_clamp case to ools/int_hw_probe.py (�alu_add_nc_clamp), covering wrap, signed saturation to [-32768, 32767], op_sel source half selection, and untouched destination half preservation.
  4. Float16 Reverse Subtraction (�_subrev_f16):
    • Added semantic tests in ests/semantic/test_alu_subrev_f16.py for VOP3 omod (mul:2, mul:4, div:2) combined with clamp.
  5. Semantic Regression Tests:
    • Added tests in ests/semantic/test_valu_add_sub_nc_i16.py covering SGPR sources, literal constants with high-half preservation, and non-VGPR destination rejection.

Tested

  • python -m compileall -q make_game.py tools tests: OK (0)
  • git diff --check upstream/main: clean (exit 0)
  • python -I tests/test_isa_coverage.py: 13 passed, 3 skipped (exit 0)
  • python -I tools/int_hw_probe.py --self-test: 112 cases OK
  • python -I tools/int_hw_probe.py --group valu_add_nc_clamp --oracle translator: 4/4 cases match bit-for-bit
  • python -m unittest tests/semantic/test_valu_add_sub_nc_i16.py tests/semantic/test_alu_u16_clamp.py tests/semantic/test_alu_subrev_f16.py tests/semantic/test_valu_int16.py: 64 passed (exit 0)
  • pytest tests/test_f16_op_sel.py tests/test_f16_op_sel_values.py: 164 passed (exit 0)
  • Full semantic test suite (python -m unittest discover -s tests/semantic): 4,142 tests passed (exit 0)

AI-assisted (Gemini). No game files or proprietary assets used.

@whattheflop

Copy link
Copy Markdown
Contributor

Independent CPU review at de37be13f2c6fb0d1c8dfc31041add194e4049e0 against its exact parent 162821017ea3c286c8bec860f3ff94f037912061 (AI-assisted).

I assembled synthetic gfx1030 programs with WSL LLVM 21.1.8 and cross-checked the instruction text and bytes against object disassembly. I fed the assembler listings with their encoding bytes to the stock translator and ran its output in the stock semantic interpreter under uv Python 3.12.13. An independent integer reference checked both halves of the destination.

  • All 3,072 literal cases matched at head. The matrix covered signed and unsigned 16-bit add and sub, a 12-byte VOP3 literal in either source slot, all eight op_sel combinations, and wrap and saturation.
  • On the parent, 2,304 cases with a high source half selected raised Unsupported. The other 768 literal cases returned the wrong destination word. For example, v_add_nc_i16 v0, 0x7fff, v2 op_sel:[0,0,1] with v2 = 0x00010001 and old v0 = 0xa53c496b returned 0x00008000 on the parent and the expected 0x8000496b at head.
  • All 512 ordinary 8-byte VOP3 register-source controls matched on both revisions.

I found no issue in this CPU scope.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested de37be1 on an RTX 5090 (driver 616.92), Windows 11, Vulkan SDK 1.4.363. The GPU test programs were built from the same tree:

  • scripts/run_gpu_tests.py: 143/189 passed, 46 skipped, 0 failed. main 1628210 gives 143/189, 46 skipped, 0 failed. All 194 test shaders translate and pass spirv-val.
  • tests/semantic/test_alu_subrev_f16.py: ran 40 tests in 1.378s; OK.
  • tests/semantic/test_valu_add_sub_nc_i16.py: ran 7 tests in 3.551s; OK.

AI-assisted (Claude Code).

@rgvxsthi

Copy link
Copy Markdown
Contributor

Tested d700b71c merged onto main d0835dd9 (6 commits behind; merges cleanly) on Windows 11 Pro 10.0.26300, Python 3.14.10, llvm-mc 23.1.6, Vulkan SDK 1.4.363.0 (spirv-as / spirv-val): pass, no regressions.

  • The PR's tests: python -m unittest tests.semantic.test_valu_add_sub_nc_i16 tests.semantic.test_alu_subrev_f16 tests.semantic.test_alu_u16_clamp: 50 tests OK.
  • Full translator suite, python -m unittest discover -s tests/semantic, run on this PR and on plain main side by side:

Not run: the int_hw_probe.py hardware oracle, since this GPU is RDNA4 (RX 9070), not the RDNA2 the probe targets. No game files. AI-assisted (Claude Code).

…rals

- Clarify RDNA2 ISA status: GFX10.3 dropped reversed 16-bit integer subtraction (v_subrev_nc_u16 / v_subrev_nc_i16 do not exist on hardware; v_subrev_f16 is the only 16-bit reverse subtract opcode).
- In tools/rdna2spirv.py, correctly identify VOP3 encoding for 16-bit integer ops when 32-bit literal operands extend the instruction to 12 bytes, preserving the untouched destination half.
- Guard against non-VGPR destination when op_sel writes to destination high half.
- In tools/int_hw_probe.py, add v_add_sub_nc_i16_clamp covering signed saturation to [-32768, 32767], op_sel source selection, and untouched half preservation.
- In tests/semantic/test_alu_subrev_f16.py, add test_vop3_omod_and_clamp.
- In tests/semantic/test_valu_add_sub_nc_i16.py, add coverage for SGPR sources, literal constants, high-half preservation, and non-VGPR op_sel destination rejection.
- Update docs/design.md row 261 with RDNA2 ISA details and probe status.
@tylerstraub

Copy link
Copy Markdown
Contributor

Tested 923512c181803056ce7b12f0a17ffbe18982e5c0 on NVIDIA GeForce RTX 5090, 595.99.02 (Linux).

I ran a synthetic pixel shader with four literal-operand VOP3 instructions through llvm-mc, the translator, spirv-val and the Vulkan harness built from this head. Each destination started at 0x43215678, and all four instructions used the literal 0x1234.

Operation Expected and observed word
Signed add to 0x7ffe, wrapping 0x43219232
Signed subtract from 0x7ffe, high-half destination 0x6dca5678
Signed add to 0x7ffe, clamped 0x43217fff
Signed subtract from 0x8001, clamped 0x43218000

All four output words matched at all 4,096 pixels. Both destination selections preserved the untouched half. This was a filtered synthetic draw.

AI-assisted verification.

@undeadhuman37

Copy link
Copy Markdown
Contributor

Tested 923512c1 merged into main 98fa503f on NVIDIA GeForce RTX 4070 SUPER, driver 621.01, Windows 11 10.0.26200, Vulkan SDK 1.4.363.1: pass.

  • tools/int_hw_probe.py --oracle translator: valu_add_nc_clamp 4/4 match (incl. v_add_sub_nc_i16_clamp, op_sel untouched half kept: 0x5a5a7fff 0x5a5a8000 0x7fff5a5a 0x5a5abbbc), valu_add_co_clamp 3/3 match; --self-test: 119 cases across 25 groups OK.
  • tests/semantic/test_alu_subrev_f16.py, test_valu_add_sub_nc_i16.py: OK.

AI-assisted (Claude Code).

@lunarminus

Copy link
Copy Markdown

Tested 923512c1 (the PR head, on main 0db7fed5) on an AMD Radeon 760M (RDNA3 iGPU, gfx1103, subgroup 32..64), RADV Mesa 26.3.1, CachyOS Linux: pass

  • What was tested: 923512c1 is based on main 0db7fed5, which already contains d0835dd9. Merging onto d0835dd9 was therefore a fast-forward, and the tree tested is the PR head itself. I built the runtime from that tree (cmake --build, exit 0).
  • The PR's semantic files: python -m unittest -v tests.semantic.test_valu_add_sub_nc_i16 tests.semantic.test_alu_u16_clamp tests.semantic.test_alu_subrev_f16 tests.semantic.test_valu_int16 ran 66 tests, OK. The 4 new tests all ran and passed: test_sgpr_and_inline_constants, test_literal_and_high_half_preservation, test_non_vgpr_opsel_dst_rejected and test_vop3_omod_and_clamp.
  • New tests against the parent translator: I ran the PR's two test files against the parent 0db7fed5 translator. Only test_non_vgpr_opsel_dst_rejected fails there (2 subtests: v_add_nc_i16 and v_sub_nc_i16). The literal and SGPR tests already pass without the PR.
  • Other CPU checks:
    • python -I tests/test_isa_coverage.py: 20 tests OK, 4 skipped.
    • python -I tools/int_hw_probe.py --self-test: 119 cases across 25 groups OK.
    • --group valu_add_nc_clamp --oracle translator: 4/4 match, including v_add_sub_nc_i16_clamp.
    • pytest tests/test_f16_op_sel.py tests/test_f16_op_sel_values.py: 164 passed.
  • Full semantic suite (python -m unittest discover -s tests/semantic):
    • PR: 6249 tests, failures=3, errors=2, skipped=1, expected failures=1.
    • 0db7fed5 without the PR: 6245 tests with the same counts.
    • The failing test IDs are identical on both.
  • scripts/run_gpu_tests.py:
    • Native: 202/217 passed, 15 skipped, 0 failed.
    • LEONIDA_W64_EMULATE=1: 202/217 passed, 15 skipped, 0 failed.
    • Both modes: 221 shaders, 221 translated, 221 pass spirv-val.
  • Per-test comparison with d0835dd9 without the PR (196/210 passed, 14 skipped in each mode): all 210 baseline result lines have the same PASS/SKIP result. The 7 extra tests are fixtures added on main since d0835dd9; this PR adds no GPU fixture. Six of them pass: cs_smem_sconst_off, ps_far_branch, ps_sample_c, ps_sample_c_b, ps_texture_b_cl and ps_texture_cl. ps_m2_literal is skipped (run by d3d12_m2_textured_test). The results are the same in both modes.
  • Not run: the int_hw_probe.py hardware oracle. Its Linux DRM launcher supports GFX10 (RDNA1/RDNA2) only, so it can't run on gfx1103. The new i16 case is not hardware-checked here.

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

@kapitche

Copy link
Copy Markdown
Contributor

Tested 923512c merged with main 3faac55 on native Windows (Python 3.13.1, llvm-mc 23.1.6) and with main 98fa503 on Linux (Python 3.12.16): pass. tests/semantic/test_valu_add_sub_nc_i16.py, test_value_semantics.py and the rdna2spirv and shader_checks tests: Windows 10/10, Linux 14/14. Negative control with main's tools/rdna2spirv.py and tools/int_hw_probe.py: test_valu_add_sub_nc_i16 fails (2 failures).

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