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

…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.
@rgvxsthi

Copy link
Copy Markdown

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

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.

4 participants