Skip to content

rdna2spirv: allow packed 16-bit integer ops to read constants under op_sel and default op_sel_hi - #3368

Closed
sarveshgamers wants to merge 1 commit into
ItsNotRexed:mainfrom
sarveshgamers:feat/shader-pk-int-constants
Closed

sarveshgamers wants to merge 1 commit into
ItsNotRexed:mainfrom
sarveshgamers:feat/shader-pk-int-constants

Conversation

@sarveshgamers

Copy link
Copy Markdown
Contributor

Summary

In VOP3P packed 16-bit integer instructions (v_pk_add/sub_u16/i16, v_pk_mul_lo_u16, v_pk_mad_u16/i16, v_pk_max/min_u16/i16), op_sel_hi defaults to [1, 1], which previously caused the translator to reject any instruction containing a constant operand (raise Unsupported(op + " high half of a constant")).

In RDNA2 VOP3P, an immediate constant provides a full 32-bit scalar word where bits [15:0] are half 0 and bits [31:16] are half 1. The existing OpBitFieldUExtract / OpBitFieldSExtract lowering already slices the 32-bit constant correctly for any combination of op_sel and op_sel_hi:

  • An inline constant 1 provides 0x00000001 (low half 1, high half 0)
  • An inline constant -1 provides 0xffffffff (both halves 0xffff)
  • A 32-bit literal constant provides its explicit low and high halves (e.g. 0xffff0008 provides low half 8, high half 0xffff)

Changes

  • tools/rdna2spirv.py: Removed the artificial restriction if a[1 + i]["k"] not in ("vgpr", "sgpr") and (sel[i] or selhi[i]): raise Unsupported(...).
  • tests/semantic/test_alu_pk_int16.py:
    • Expanded test_constants to test inline constants with default and modified op_sel/op_sel_hi, negative constants, 32-bit literals in both source positions (src0 and src1), and 3-source v_pk_mad_u16.
    • Removed "v_pk_add_u16 v0, v1, 5" from test_rejected_forms.
  • docs/design.md: Updated the contract row for Packed 16-bit integer ALU.

Testing

  • python -m unittest tests/semantic/test_alu_pk_int16.py -v: 7/7 tests passed.
  • python -m unittest discover -s tests/semantic -v: all 4,137 tests passed.
  • python -m compileall -q make_game.py tools tests: 0 errors.
  • Disassembly test on upscaler compute shader: translates and passes spirv-val.
  • AI-assisted.

Claimed in #3232.

…p_sel and default op_sel_hi

In VOP3P packed 16-bit integer instructions (v_pk_add/sub/mul_lo/mad/max/min), op_sel_hi defaults to [1, 1], which caused the translator to reject any instruction with a constant operand ('high half of a constant').

In RDNA2 VOP3P, an immediate constant provides a full 32-bit scalar word where bits [15:0] are half 0 and bits [31:16] are half 1. The existing OpBitFieldUExtract / OpBitFieldSExtract lowering already slices the 32-bit constant correctly for any combination of op_sel and op_sel_hi.

Remove the artificial constant restriction, expand tests/semantic/test_alu_pk_int16.py to cover constants in src0/src1 under default and swapped op_sel / op_sel_hi, and update docs/design.md.

All 4,137 semantic tests pass. Translates and validates upscaler shader amd_fidelityfx_upscaler_xbox_00.s. AI-assisted.
@kacper-serewis

Copy link
Copy Markdown
Contributor

Independent AI-assisted CPU review at 83110f4 found no translator defect: seven focused tests and nine hand-calculated cases passed; all nine modules passed Vulkan 1.2 spirv-val. Eight high-half controls were rejected on parent ec8ec4c; the low-only control matched both.

Coordination hold: open #2224 already removes this same guard and supplies the raw-kernel constant evidence. Please coordinate one implementation before merge. Also cite the gfx1036 constant measurements and wave64 recheck from #2224 next to this contract rule; the current gfx1035 citation establishes arithmetic/selectors rather than these newly accepted constant encodings. No GPU or game data used in this review.

@Vonsek

Vonsek commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Tested 83110f4 against the hardware. Measured on gfx1036 (RDNA2, driver 26.10.1, game MODE; integer op, so the float MODE does not enter): v_pk_add_u16 v8, K, v2 with v2 = 0, through raw hw_oracle kernels, K in 1 -1 5 -16 64 0xffff0008 0x00010002 0x12345678 and six modifier sets (none, op_sel:[1,0], op_sel_hi:[0,0], both, op_sel:[1,1] op_sel_hi:[1,1], op_sel:[0,0] op_sel_hi:[1,0]), 48 cases. I ran the same 48 programs through this branch's translation (the semantic interpreter, run_atomic-style listing) and compared the result words: 48 of 48 identical.

What the hardware does, which is what the PR says: an inline integer constant is a 32-bit word with its halves as given, positive ones with a zero high half (1 -> 0x00000001, so the default op_sel_hi:[1,1] result is 0x00000001 and op_sel:[1,0] gives 0), negative ones sign-extended (-1 -> 0xffffffff in every case, -16 -> 0xfffffff0 by default and 0xfff0fff0 with op_sel_hi:[0,0]); a literal supplies its own halves (0xffff0008: default 0xffff0008, op_sel_hi:[0,0] -> 0x00080008, op_sel:[1,0] op_sel_hi:[0,0] -> 0x0008ffff; 0x12345678: default 0x12345678, op_sel:[1,0] -> 0x12341234, op_sel_hi:[0,0] -> 0x56785678). Result low half = the half of src0 that op_sel[0] picks, result high half = the one op_sel_hi[0] picks.

Only v_pk_add_u16 was measured; the PR changes the shared operand read of v_pk_sub/mul_lo/mad/max/min too, which I did not run. The script is local, not pushed. AI-assisted.

@sarveshgamers

Copy link
Copy Markdown
Contributor Author

Thank you @Vonsek for running the 48-case hardware oracle verification on native RDNA2 gfx1036 and confirming the 48/48 bit-exact match across modifiers and constants! And thank you @kacper-serewis for the review.

Since #2224 was already opened earlier with the hardware measurement and GPU test fixture (\cs_w32_pk_int_const.asm), deferring to #2224 to avoid duplicate merges. Closing in favor of #2224.

@sarveshgamers

Copy link
Copy Markdown
Contributor Author

Thanks @Vonsek for the thorough 48-case RDNA2 \gfx1036\ hardware verification and @kacper-serewis for the review! Since #2224 was already opened earlier with the full GPU test fixture (\cs_w32_pk_int_const.asm), closing this PR in favor of #2224 to avoid duplicate merges.

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.

3 participants