Skip to content

rdna2spirv: add v_dot2/4/8 integer dot products and v_pk_fmac_f16 (SHADER-300) - #1808

Merged
ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
Taffyyy-ops:feat/rdna2-dot-int-pk-fmac-f16
Nov 22, 2026
Merged

ItsNotRexed merged 2 commits into
ItsNotRexed:mainfrom
Taffyyy-ops:feat/rdna2-dot-int-pk-fmac-f16

Conversation

@Taffyyy-ops

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: SHADER-300 (Refs #1745, claim #1548 (comment); split agreed with @aslan2142 in #1745 (comment), where #1754 covers v_dot4_i32_i8 and v_dot4c_i32_i8).

What and why

AI-assisted implementation of the remaining six SHADER-300 instructions (v_dot2_i32_i16, v_dot2_u32_u16, v_dot4_u32_u8, v_dot8_i32_i4, v_dot8_u32_u4, v_pk_fmac_f16), needed by the FidelityFX FSPX compute programs (docs/fspx.md / #1661) and listed as unknown in RDNA2 ISA coverage (#1562):

  • tools/rdna2spirv.py:
    • Implemented VOP3P packed integer dot-product instructions v_dot2_i32_i16, v_dot2_u32_u16, v_dot4_u32_u8, v_dot8_i32_i4, and v_dot8_u32_u4: extracts each 16/8/4-bit element with OpBitFieldSExtract / OpBitFieldUExtract, sums elementwise products with S2 wrapping at 32 bits, and saturates to [INT32_MIN, INT32_MAX] / [0, UINT32_MAX] under clamp (signed carry tracking handles the v_dot2_i32_i16 (-32768)^2 + (-32768)^2 + S2 overflow/cancellation edge). Non-zero op_sel/op_sel_hi, neg_lo/neg_hi, DPP/SDWA forms, and source neg/abs raise Unsupported.
    • Implemented VOP2 v_pk_fmac_f16 (supporting _dpp via Gen.non_dpp_mods): unpacks src0, vsrc1, and vdst as f16x2 (Gen.src_h), fuses fma(src0[k], vsrc1[k], vdst[k]) per half with single f16 rounding (Gen.fma_f16_odd, SHADER-55), and packs with Gen.f16_pack2; rejects non-DPP modifiers, _sdwa, neg/abs, and non-VGPR vdst.
    • Updated read_before_write_vgprs and reg_reads_writes to track vdst reads on v_pk_fmac_f16.
    • Deliberately leaves v_dot4_i32_i8 and v_dot4c_i32_i8 unhandled to avoid overlapping with Translator: v_dot4_i32_i8 and v_dot4c_i32_i8, confirmed on RDNA2 hardware (17 of 29 FSPX programs translate, 13 before) #1754.
  • docs/design.md:
    • Added the contract-table entry below Packed 16-bit integer ops documenting the six instructions, their semantics, rejected forms, and test confirmation.
  • tests/semantic/test_alu_dot_pk_fmac.py:
    • Added hand-written .asm fixture PS_DOT_PK_FMAC_ASM (PS_DOT_PK_FMAC_EXPECTED = (0x004A8008, 0x0214FFFF, 0x00002126, 0x3C00BE00)) and unit tests covering signed/unsigned 4/8/16-bit lanes, 32-bit wrap and clamp saturation (including (-32768)^2 + (-32768)^2 + acc), single-rounded v_pk_fmac_f16 tie triples, inline fimm/literal/SGPR sources, vdst aliasing, DPP translation, read_before_write_vgprs/reg_reads_writes, random sweeps, rejected forms, and verifying v_dot4_i32_i8 / v_dot4c_i32_i8 remain unhandled here.

Tested

Tested on Windows 11 with NVIDIA GeForce RTX 5060 Laptop GPU (driver 621.24, Vulkan 1.4.357), with LEONIDA_GAME_DIR and LEONIDA_MOVIE unset:

  • python -m compileall -q make_game.py tools tests: exit 0
  • python -I make_game.py --help: exit 0
  • python -I tests/semantic/test_alu_dot_pk_fmac.py -v: 13/13 passed (2.086s)
  • python -I tests/test_isa_coverage.py -v: 13/13 passed without XML (3 skipped in RealIsaTests), and 13/13 passed with LEONIDA_RDNA2_ISA_XML set to amdgpu_isa_rdna2.xml (15.388s)
  • python -m unittest discover -s tests/semantic -v: 1463 tests run, OK (2 skipped: test_bary_fallback.BaryFallbackGeometryShader.test_assembles_and_validates and test_fma_khr.FmaKhrTests.test_spirv_tools_accept_it because spirv-as/spirv-val are not installed locally)
  • node --test tests/test_viewer_streaming.cjs: 35/35 passed (fail 0, skipped 0)
  • CI stdlib unit tests (python -I, 173 tests/test_*.py files): 171/173 passed; 2 pre-existing upstream/main (89b047cd) Windows failures unrelated to this PR:
    • tests/test_make_game_assets_pools.py (4 ran, 2 failed: / vs \\ separator mismatch in path assertion)
    • tests/test_world_index.py (33 ran, 1 failed, 1 skipped: test_new_report_paths_that_differ_only_in_case on Unicode CAFE\u0301.json on Windows NTFS)
  • .\runtime\build\d3d12_vulkan_device_test.exe on NVIDIA GeForce RTX 5060 Laptop GPU: exit 0 (FORMAT_INFO 195/195, CheckFeatureSupport 15/15, FORMAT_SUPPORT 80/80, copy comparison 69/69, Depth/stencil MSAA 12 compared / 8 skipped / 0 mismatches, Resolve comparison 60/60)

Not run locally (tools absent / no retail package):

  • Retail shader corpus translation and spirv-val counts (LEONIDA_GAME_DIR unset; spirv-as/spirv-val/llvm-mc not installed)
  • dotnet build tools/rpf9dump/rpf9dump.csproj -c Release (.NET 8 SDK not installed)
  • runtime/test/shaders/build.py / check.py (no llvm-mc) and native pytest suite (pytest not installed)
  • Full ctest rebuild on 89b047cd (no C++ files modified)

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

@aslan2142

Copy link
Copy Markdown
Contributor

@Taffyyy-ops, here are the RDNA2 hardware words I offered for SHADER-300: five of the six instructions in this PR, with a rule each. I have not yet compared them with the rules in this PR; that comes next. v_pk_fmac_f16 is still to come (see the end).

Setup

  • GPU: the gfx1036 iGPU of a Ryzen 9 7950X3D (PCI 1002:164e), which drives no display.
  • Software: Linux 7.3.5 amdgpu, libdrm 2.4.134, tools/hw_oracle raw dispatch from current main, AMD clang 24.0.0git -mcpu=gfx1036.
  • Every form below assembles for gfx1036 with both LLVM 23.1.5 llvm-mc and ROCm's.
  • Each operand set ran as plain, sources swapped, S0/S1 from SGPRs, and clamp in both orders. The destination was pre-filled with 0x5A5A5A5A.
  • All five are VOP3P with the addend in S2. None accumulates into the destination, and swapped or SGPR sources gave the same word.

Rules. 12 operand sets each, 300 words. My first-guess model matched every word.

  • v_dot4_u32_u8: D = Σ zext(S0.b[i]) * zext(S1.b[i]) + S2, 4 bytes, mod 2^32. clamp saturates the exact sum to 0..0xFFFFFFFF.
  • v_dot2_i32_i16: D = Σ sext(S0.h[i]) * sext(S1.h[i]) + sext(S2), 2 halves, mod 2^32. clamp saturates the exact sum to int32.
  • v_dot2_u32_u16: the same with zero extension. clamp saturates to uint32, and the products alone can exceed 32 bits.
  • v_dot8_i32_i4: 8 nibbles (bits 4i..4i+3), sign-extended, plus sext(S2), mod 2^32. clamp saturates to int32.
  • v_dot8_u32_u4: 8 nibbles, zero-extended, plus S2, mod 2^32. clamp saturates to uint32.

Selected words (S0 S1 S2 -> D / D with clamp)

v_dot4_u32_u8   04030201 08070605 00000000 -> 00000046 / 00000046   lane pairing (70)
v_dot4_u32_u8   000000FF 00000002 00000000 -> 000001FE / 000001FE   zero extension
v_dot4_u32_u8   FFFFFFFF FFFFFFFF FFFFFFFF -> 0003F803 / FFFFFFFF   wrap / unsigned clamp
v_dot4_u32_u8   FFFFFFFF FFFFFFFF 7FFFFFFF -> 8003F803 / 8003F803   clamp is unsigned, S2 unsigned
v_dot2_i32_i16  0000FFFF 00000002 00000000 -> FFFFFFFE / FFFFFFFE   sign extension
v_dot2_i32_i16  80008000 80008000 00000000 -> 80000000 / 7FFFFFFF   products alone overflow int32
v_dot2_i32_i16  7FFF7FFF 7FFF7FFF 7FFFFFFF -> FFFE0001 / 7FFFFFFF   wraps up
v_dot2_i32_i16  80008000 7FFF7FFF 80000000 -> 00010000 / 80000000   wraps down
v_dot2_u32_u16  FFFFFFFF FFFFFFFF 00000000 -> FFFC0002 / FFFFFFFF   2*65535^2 > 2^32
v_dot2_u32_u16  00010001 00010001 FFFFFFFF -> 00000001 / FFFFFFFF   wrap / clamp
v_dot8_i32_i4   87654321 12345678 00000000 -> 00000058 / 00000058   lane pairing, signed nibbles
v_dot8_i32_i4   77777777 77777777 7FFFFFFF -> 80000187 / 7FFFFFFF   wraps up
v_dot8_i32_i4   88888888 77777777 80000000 -> 7FFFFE40 / 80000000   wraps down
v_dot8_u32_u4   87654321 12345678 00000000 -> 00000078 / 00000078   lane pairing, unsigned
v_dot8_u32_u4   11111111 11111111 FFFFFFF8 -> 00000000 / FFFFFFFF   wrap / clamp

I have all 60 sets as Python tuples (S0, S1, S2, D, D_clamp), with a ref that reproduces them, ready to paste into a semantic test. Say if you want them here.

Modifiers the assembler accepts on these (184 more words)

  • op_sel/op_sel_hi are accepted only on the two 16-bit forms. They work as on packed 16-bit ops: lane 0 of S_k reads the high half if op_sel[k], lane 1 if op_sel_hi[k]. Neither field changes S2.
  • neg_lo/neg_hi are accepted on all five, and they are not ignored. My first guess was that they do nothing; the hardware treats them as f16/f32 sign bits:
    • S0/S1: neg_lo XORs bit 15 and neg_hi XORs bit 31 of the dword, whatever the lane layout. For example, v_dot4_u32_u8 ... neg_lo:[1,0,0] with S0 = 04030201 reads byte 1 as 0x82.
    • S2: neg_hi[2] clears bit 31 (abs), then neg_lo[2] flips it (neg).
    • I formed this from the words. The S2 abs part rests on 5 discriminating words, plus 26 with both bits set.
  • Suggestion: refuse neg_* on these ops unless you model it. A rule that drops the bits gives wrong sums.

Not measured yet

  • v_pk_fmac_f16: it assembles for gfx1036 as VOP2 only (VOP3 is rejected, so no clamp/neg/op_sel on it), but I have not run it yet. A kernel bug of mine (a result register that was also the store address) made the iGPU fault on that same run, so I stopped there.
  • neg combined with op_sel, which flips first is open.
  • clamp combined with modifiers.
  • v_dot4c_u32_u8, v_dot2c_i32_i16 and v_dot8c_i32_i4 do not assemble for gfx1036.

AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2).

@Taffyyy-ops

Copy link
Copy Markdown
Contributor Author

Thanks a lot for the RDNA2 hardware measurements @aslan2142! Great to see all 300 words and the clamp edge cases match our rules, and neg_lo/neg_hi are already rejected as Unsupported in this PR. Yes, please paste the 60 test tuples here (and v_pk_fmac_f16 whenever you get a chance to run it) — I'll add them straight into tests/semantic/test_alu_dot_pk_fmac.py and cite the gfx1036 hardware confirmation in docs/design.md.

@Impre-visible

Copy link
Copy Markdown

Tested 2393188 on NVIDIA GeForce GTX 1660 (Turing, TU116, subgroup 32..32), driver 620.73.04 (proprietary, Vulkan 1.4.357), Arch Linux (kernel 6.18): pass, no regression.

Native build of runtime/ from this head (Ninja, RelWithDebInfo, clang 23.1.5), llvm-mc 23.1.5, SPIRV-Tools v2026.4.

  • spirv-val on the modules the PR's semantic tests translate (test_alu_dot_pk_fmac.py): distinct modules: 86, pass spirv-val: 86, rejected: 0
  • tests/semantic: 1580 OK
  • build.py: 182 shaders, 182 translated, 182 pass spirv-val; check.py: 119 of 154 pass, 35 skipped (wave64 without emulation, mesh fixtures)
  • run_gpu_tests.py: 175 shaders: 175 translated, 175 pass spirv-val; 130/170 passed, 40 skipped, 0 failed
  • LEONIDA_W64_EMULATE=1 run_gpu_tests.py: 161/170 passed, 9 skipped (8 fixtures run by the d3d12 tests, 1 needs a 64-wide fragment subgroup), 0 failed

The PR adds no GPU fixture, so the new ops' values are covered only by the semantic tests here.

AI-assisted run (Claude). Synthetic programs only, no game files involved.

@Youssefrah16

Copy link
Copy Markdown

Tested 23931889 on AMD Radeon 680M (gfx1035, RDNA2, Windows 11 10.0.26300, driver 26.5.1 / amdhip64_6.dll): pass (AI-assisted).

  • python -m compileall -q tools/rdna2spirv.py tests/semantic/test_alu_dot_pk_fmac.py: clean
  • python -I tests/semantic/test_alu_dot_pk_fmac.py -v: 13/13 OK
  • spirv-as + spirv-val --target-env vulkan1.2 on synthetic shader covering all six opcodes (v_dot2_i32_i16, v_dot2_u32_u16, v_dot4_u32_u8, v_dot8_i32_i4, v_dot8_u32_u4, v_pk_fmac_f16): OK
  • Native RDNA2 gfx1035 silicon check via tools/hw_oracle (18 cases covering all six opcodes, signed/unsigned overflow wrapping, VOP3 clamp saturation on both positive and negative overflow, and v_pk_fmac_f16 FMA rounding × wave32 & wave64): 36/36 bit-exact match, 0 mismatches, 0 skipped.

@aslan2142

Copy link
Copy Markdown
Contributor

@Taffyyy-ops, I ran the gfx1036 words from my earlier comment through this branch's translator (head 23931889; the lines in question are unchanged at 307809a), the way test_alu_dot_pk_fmac.py does it (translate_lines + mrt0_words).

Verdict: the five integer dot products match the hardware on every measured word, clamp included. Every op_sel and neg_* form I measured is refused, which is right while they are not modelled. There is one inverted check on op_sel_hi (below).

  • v_dot4_u32_u8: matches 12/12 plain sets (as measured, sources swapped, S0/S1 from SGPRs) and 12/12 clamp sets (both orders). neg_lo/neg_hi (8 forms): refused.
  • v_dot2_i32_i16: matches 12/12 plain and 12/12 clamp, including 80008000 80008000 0 -> 80000000 / 7FFFFFFF. op_sel (7 forms) and neg_* (7 forms): refused.
  • v_dot2_u32_u16: matches 12/12 plain and 12/12 clamp, including FFFFFFFF FFFFFFFF 0 -> FFFC0002 / FFFFFFFF. op_sel and neg_*: refused.
  • v_dot8_i32_i4: matches 12/12 plain and 12/12 clamp. neg_* (8 forms): refused.
  • v_dot8_u32_u4: matches 12/12 plain and 12/12 clamp. neg_* (8 forms): refused.
  • v_pk_fmac_f16: not checked yet. I measure it next on the same GPU.

The op_sel_hi check is inverted (tools/rdna2spirv.py, the v_dot2/4/8 branch, bits() and the Unsupported test below it):

  • bits() returns [0,0,0] for a missing field, so any(bits("op_sel_hi")) refuses the hardware default op_sel_hi:[1,1,1] when it is written out, and accepts op_sel_hi:[0,0,0], computing it as the default.
  • On gfx1036 op_sel_hi[k] = 0 makes lane 1 of S_k read the low half (measured with op_sel_hi:[0,1,1] and [1,0,1]). So for the two v_dot2_*, [0,0,0] should give S0.lo*S1.lo*2 + S2. That is my measured rule applied to this form; I did not run [0,0,0] itself.
  • For v_dot4_u32_u8 and v_dot8_*, llvm-mc rejects any op_sel/op_sel_hi for gfx1036. On v_dot4_i32_i8 (Translator: v_dot4_i32_i8 and v_dot4c_i32_i8, confirmed on RDNA2 hardware (17 of 29 FSPX programs translate, 13 before) #1754) the hardware did not ignore them, so test_sources_and_zero_opsel pins a guess.
  • A suggested fix: default op_sel_hi to [1,1,1], accept only op_sel:[0,0,0] and op_sel_hi:[1,1,1], and refuse anything else.
form                                S0       S1       S2       translator  measured rule
v_dot2_i32_i16 op_sel_hi:[0,0,0]    00020001 00040003 00000000 0000000B    00000006
v_dot2_i32_i16 op_sel_hi:[0,0,0]    FFFF0000 00020000 00000000 FFFFFFFE    00000000
v_dot2_u32_u16 op_sel_hi:[0,0,0]    0000FFFF 00000002 00000000 0001FFFE    0003FFFC
v_dot2_i32_i16 op_sel_hi:[1,1,1]    (any)                      refused     same as plain

The 60 tuples you asked for, (S0, S1, S2, D, D with clamp), measured on gfx1036:

MEASURED, as Python literals
MEASURED = {
    "v_dot4_u32_u8": [
        (0x04030201, 0x08070605, 0x00000000, 0x00000046, 0x00000046),   # lane pairing: 1*5+2*6+3*7+4*8 = 70; a reversed pairing gives 60
        (0x04030201, 0x08070605, 0x000003E8, 0x0000042E, 0x0000042E),   # third source is added (70 + 1000)
        (0x000000FF, 0x00000002, 0x00000000, 0x000001FE, 0x000001FE),   # lane 0 alone, top bit set: zero-extended 510, sign-extended -2
        (0xFF000000, 0x02000000, 0x00000000, 0x000001FE, 0x000001FE),   # lane 3 alone: 510 again, and no lane 3 * lane 0 cross term
        (0x00FF0000, 0x0000FF00, 0x00000000, 0x00000000, 0x00000000),   # different lanes only: 0 (no cross products)
        (0x80808080, 0x7F7F7F7F, 0x00000005, 0x0000FE05, 0x0000FE05),   # unsigned 4*128*127+5 = 65029; signed would be -65019
        (0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x0003F804, 0x0003F804),   # largest product sum 4*255*255 = 260100
        (0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x0003F803, 0xFFFFFFFF),   # wraps upward: 260100 - 1 mod 2^32; clamp: 0xFFFFFFFF if unsigned
        (0xFFFFFFFF, 0xFFFFFFFF, 0x7FFFFFFF, 0x8003F803, 0x8003F803),   # passes 2^31: no wrap unsigned; a signed clamp would stop at 0x7FFFFFFF
        (0x01010101, 0x01010101, 0xFFFFFFFC, 0x00000000, 0xFFFFFFFF),   # 4 + 0xFFFFFFFC wraps to exactly 0; clamp: 0xFFFFFFFF
        (0x12345678, 0x9ABCDEF0, 0x80000000, 0x8000EC18, 0x8000EC18),   # mixed bytes plus an addend with the top bit set
        (0x00000000, 0x12345678, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF),   # zero dot product passes the addend through
    ],
    "v_dot2_i32_i16": [
        (0x00020001, 0x00040003, 0x00000000, 0x0000000B, 0x0000000B),   # lane pairing: 1*3+2*4 = 11; crossed pairing gives 10
        (0x00020001, 0x00040003, 0x000003E8, 0x000003F3, 0x000003F3),   # third source added (1011)
        (0x0000FFFF, 0x00000002, 0x00000000, 0xFFFFFFFE, 0xFFFFFFFE),   # lane 0 alone, top bit set: sign-extended -2, zero-extended 131070
        (0xFFFF0000, 0x00020000, 0x00000000, 0xFFFFFFFE, 0xFFFFFFFE),   # lane 1 alone: -2
        (0x80000000, 0x00008000, 0x00000000, 0x00000000, 0x00000000),   # different lanes only: 0
        (0x80008000, 0x7FFF7FFF, 0x00000000, 0x80010000, 0x80010000),   # 2*(-32768*32767) = -2147418112
        (0x80008000, 0x80008000, 0x00000000, 0x80000000, 0x7FFFFFFF),   # 2*(-32768)^2 = 2^31: exceeds int32 with no addend; clamp 0x7FFFFFFF
        (0x7FFF7FFF, 0x7FFF7FFF, 0x7FFFFFFF, 0xFFFE0001, 0x7FFFFFFF),   # wraps upward past 0x7FFFFFFF; clamp 0x7FFFFFFF
        (0x80008000, 0x7FFF7FFF, 0x80000000, 0x00010000, 0x80000000),   # wraps downward past 0x80000000; clamp 0x80000000
        (0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000001, 0x00000001),   # 2*(-1*-1) - 1 = 1
        (0x8000FFFF, 0x00017FFF, 0x00000064, 0xFFFF0065, 0xFFFF0065),   # mixed: -1*32767 + -32768*1 + 100
        (0x00000000, 0x12345678, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF),   # zero dot product passes the addend through
    ],
    "v_dot2_u32_u16": [
        (0x00020001, 0x00040003, 0x00000000, 0x0000000B, 0x0000000B),   # lane pairing: 11; crossed pairing gives 10
        (0x00020001, 0x00040003, 0x000003E8, 0x000003F3, 0x000003F3),   # third source added (1011)
        (0x0000FFFF, 0x00000002, 0x00000000, 0x0001FFFE, 0x0001FFFE),   # lane 0 alone, top bit set: zero-extended 131070, signed -2
        (0xFFFF0000, 0x00020000, 0x00000000, 0x0001FFFE, 0x0001FFFE),   # lane 1 alone: 131070
        (0x80000000, 0x00008000, 0x00000000, 0x00000000, 0x00000000),   # different lanes only: 0
        (0x80008000, 0x80008000, 0x00000000, 0x80000000, 0x80000000),   # 2*32768^2 = 2^31 exactly (no wrap unsigned)
        (0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0xFFFC0002, 0xFFFFFFFF),   # 2*65535^2 = 0x1FFFC0002: the products alone exceed 32 bits; clamp 0xFFFFFFFF
        (0xFFFF0001, 0xFFFF0001, 0x00000000, 0xFFFE0002, 0xFFFE0002),   # 65535^2 + 1 = 0xFFFE0002, fits
        (0x00010001, 0x00010001, 0xFFFFFFFF, 0x00000001, 0xFFFFFFFF),   # 2 + 0xFFFFFFFF wraps to 1; clamp 0xFFFFFFFF
        (0x80008000, 0x80008000, 0x7FFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF),   # 2^31 + 0x7FFFFFFF = 0xFFFFFFFF, no overflow unsigned; signed clamp would stop
        (0x12345678, 0x9ABCDEF0, 0x80000000, 0xD64DC6B0, 0xD64DC6B0),   # mixed halves plus an addend with the top bit set
        (0x00000000, 0x12345678, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF),   # zero dot product passes the addend through
    ],
    "v_dot8_i32_i4": [
        (0x87654321, 0x12345678, 0x00000000, 0x00000058, 0x00000058),   # lane pairing over 8 nibbles, with 8..F negative in the top three
        (0x76543210, 0x01234567, 0x000003E8, 0x00000420, 0x00000420),   # lane pairing, all nibbles positive, plus 1000
        (0x0000000F, 0x00000002, 0x00000000, 0xFFFFFFFE, 0xFFFFFFFE),   # nibble 0 alone, top bit set: signed -2, unsigned 30
        (0xF0000000, 0x20000000, 0x00000000, 0xFFFFFFFE, 0xFFFFFFFE),   # nibble 7 alone: -2
        (0x000000F0, 0x0000000F, 0x00000000, 0x00000000, 0x00000000),   # different nibbles only: 0 (no cross products)
        (0x88888888, 0x88888888, 0x00000000, 0x00000200, 0x00000200),   # 8*(-8*-8) = 512
        (0x88888888, 0x77777777, 0x00000000, 0xFFFFFE40, 0xFFFFFE40),   # 8*(-8*7) = -448
        (0x77777777, 0x77777777, 0x7FFFFFFF, 0x80000187, 0x7FFFFFFF),   # wraps upward: 392 + 0x7FFFFFFF; clamp 0x7FFFFFFF
        (0x88888888, 0x77777777, 0x80000000, 0x7FFFFE40, 0x80000000),   # wraps downward: -448 + 0x80000000; clamp 0x80000000
        (0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000007, 0x00000007),   # 8*(-1*-1) - 1 = 7
        (0xF0F0F0F0, 0x0F0F0F0F, 0x00000000, 0x00000000, 0x00000000),   # only odd nibbles of a, even of b: 0
        (0x00000000, 0x12345678, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF),   # zero dot product passes the addend through
    ],
    "v_dot8_u32_u4": [
        (0x87654321, 0x12345678, 0x00000000, 0x00000078, 0x00000078),   # lane pairing over 8 nibbles, unsigned
        (0x76543210, 0x01234567, 0x000003E8, 0x00000420, 0x00000420),   # lane pairing plus 1000
        (0x0000000F, 0x00000002, 0x00000000, 0x0000001E, 0x0000001E),   # nibble 0 alone, top bit set: unsigned 30, signed -2
        (0xF0000000, 0x20000000, 0x00000000, 0x0000001E, 0x0000001E),   # nibble 7 alone: 30
        (0x000000F0, 0x0000000F, 0x00000000, 0x00000000, 0x00000000),   # different nibbles only: 0
        (0x88888888, 0x77777777, 0x00000000, 0x000001C0, 0x000001C0),   # 8*(8*7) = 448; signed would be -448
        (0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000708, 0x00000708),   # largest: 8*225 = 1800
        (0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0x00000707, 0xFFFFFFFF),   # wraps upward: 1799; clamp 0xFFFFFFFF
        (0xFFFFFFFF, 0xFFFFFFFF, 0x7FFFFFFF, 0x80000707, 0x80000707),   # passes 2^31: no wrap unsigned; a signed clamp would stop at 0x7FFFFFFF
        (0x11111111, 0x11111111, 0xFFFFFFF8, 0x00000000, 0xFFFFFFFF),   # 8 + 0xFFFFFFF8 wraps to exactly 0; clamp 0xFFFFFFFF
        (0xF0F0F0F0, 0x0F0F0F0F, 0x00000000, 0x00000000, 0x00000000),   # only odd nibbles of a, even of b: 0
        (0x00000000, 0x12345678, 0xDEADBEEF, 0xDEADBEEF, 0xDEADBEEF),   # zero dot product passes the addend through
    ],
}

AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2).

@aslan2142

Copy link
Copy Markdown
Contributor

@Taffyyy-ops, here is v_pk_fmac_f16 measured on the same gfx1036 iGPU. I ran the words through this branch's translator (head 23931889) the same way as before.

Verdict: the translator matches the hardware on all 22 default-MODE words, in both source orders. That includes the fused last bit, signed zeros, inf, NaN propagation and denormal results. Nothing to change for the default MODE.

The measured rule

  • Each 16-bit half on its own: D.h = fma(S0.h, S1.h, D_old.h) in binary16, rounded once (fused). The accumulator is the destination's old value.
  • v_pk_fmac_f16 vD, v1, v2, the same with the sources swapped, and v_pk_fma_f16 vX, v1, v2, v3 (with v3 = D_old) gave the same word every time.
  • A NaN operand comes out quieted (bit 9 set, payload kept), for example 7C01 -> 7E01 and 7D23 -> 7F23.
  • An invalid operation (inf * 0, inf - inf) gives FE00, with the sign bit set. My first guess was 7E00; your rule already gives FE00.
  • Overflow gives 7C00 when rounding to nearest. The halves are independent: a NaN or an overflow in one half never changed the other.
  • On gfx1036 it assembles as VOP2 (and DPP) only. VOP3 is rejected, so there is no clamp/neg/op_sel to handle, which matches what you refuse.

How it was measured

  • 22 cases, each run once in the low and once in the high half of a word, under 3 MODEs: 66 words, 3 forms each.
  • The default MODE, read back as HW_REG_MODE = 0x3C0: round to nearest even, f16 denormals kept, IEEE and DX10_CLAMP on.
  • Also run: f16 denormals flushed (.amdhsa_float_denorm_mode_16_64 0, read back 0x300) and round toward zero (.amdhsa_float_round_mode_16_64 3, read back 0x3CC).

Selected words, default MODE (S0 S1 D_old -> D, two cases per word)

3C014000 3C034200 BC003C00 -> 1C014700   lo 2*3+1; hi fused 1C01 (multiply-then-add: 1C00)
3C013C01 3C013C03 BC02BC00 -> 00101C01   lo cancellation to the denormal 0010 (unfused: 0000)
3C013C01 3E003C01 8001BC02 -> 3E010010   hi: a tie decided by a -2^-24 addend, 3E01 (unfused: 3E02)
80000000 3C003C00 80008000 -> 80000000   -0*1 + -0 = -0 in both halves
3C008000 BC003C00 3C008000 -> 00008000   1*-1 + 1 = +0; -0*1 + -0 = -0
7C007C00 0000C000 3C007C00 -> FE00FE00   inf*0 and inf - inf
7BFF7C00 40000000 00003C00 -> 7C00FE00   overflow to inf; the other half's NaN stays in its half
7C017E55 3C003C00 3C003C00 -> 7E017E55   sNaN quieted, qNaN payload kept
00030001 38004400 00000000 -> 00020004   denormal tie to even; denormal input kept

For information, not a bug in this PR

  • The translator has no MODE input. Under a flushing or toward-zero MODE, 10 halves differ: for example 0003 * 3800 flushed gives 0000 (translator 0002), and 7BFF * 2 toward zero gives 7BFF (translator 7C00).
  • That only matters if a shader runs with a non-default f16 MODE.

Two follow-ups on my earlier comment

  • The 8 words I had lost (neg_lo:[0,0,1] neg_hi:[0,0,1] on the two v_dot2_*) are now measured. They match the rule I described (S2 bit 31 set), and this branch refuses the form.
  • The inverted op_sel_hi check I reported still stands.

Not measured: rounding toward +inf or -inf, the two one-sided denormal modes, IEEE off, and two NaN operands at once.

All words, (S0, S1, D_old, D) per MODE, with the MODE read back in the kernel:

MEASURED_PK_FMAC, as Python literals
MODE_ARGS = {"default": (0, False), "flush": (0, True), "rtz": (3, False)}   # (rmode, flush)

MEASURED_PK_FMAC = {
    "default": (0x000003C0, [
        (0x3C014000, 0x3C034200, 0xBC003C00, 0x1C014700),
        (0x3C013C01, 0x3C013C03, 0xBC02BC00, 0x00101C01),
        (0x3C013C01, 0x3E003C01, 0x8001BC02, 0x3E010010),
        (0x3E013C01, 0x3E013E00, 0x00008001, 0x40823E01),
        (0xBE013E01, 0x3E013E01, 0x80000000, 0xC0824082),
        (0x0000BE01, 0x3C003E01, 0x80008000, 0x0000C082),
        (0x80000000, 0x3C003C00, 0x80008000, 0x80000000),
        (0x3C008000, 0xBC003C00, 0x3C008000, 0x00008000),
        (0x80003C00, 0x3C00BC00, 0x00003C00, 0x00000000),
        (0x7C008000, 0x40003C00, 0x3C000000, 0x7C000000),
        (0x7C007C00, 0xC0004000, 0x7C003C00, 0xFE007C00),
        (0x7C007C00, 0x0000C000, 0x3C007C00, 0xFE00FE00),
        (0x7BFF7C00, 0x40000000, 0x00003C00, 0x7C00FE00),
        (0x7E557BFF, 0x3C004000, 0x3C000000, 0x7E557C00),
        (0x7C017E55, 0x3C003C00, 0x3C003C00, 0x7E017E55),
        (0x3C007C01, 0x3C003C00, 0x7D233C00, 0x7F237E01),
        (0x04003C00, 0x38003C00, 0x00007D23, 0x02007F23),
        (0x00010400, 0x44003800, 0x00000000, 0x00040200),
        (0x00030001, 0x38004400, 0x00000000, 0x00020004),
        (0x3C000003, 0x3C003800, 0x64000000, 0x64010002),
        (0x44003C00, 0xC2003C00, 0x49006400, 0xC0006401),
        (0x40004400, 0x4200C200, 0x3C004900, 0x4700C000),
    ]),
    "flush": (0x00000300, [
        (0x3C014000, 0x3C034200, 0xBC003C00, 0x1C014700),
        (0x3C013C01, 0x3C013C03, 0xBC02BC00, 0x00001C01),
        (0x3C013C01, 0x3E003C01, 0x8001BC02, 0x3E020000),
        (0x3E013C01, 0x3E013E00, 0x00008001, 0x40823E02),
        (0xBE013E01, 0x3E013E01, 0x80000000, 0xC0824082),
        (0x0000BE01, 0x3C003E01, 0x80008000, 0x0000C082),
        (0x80000000, 0x3C003C00, 0x80008000, 0x80000000),
        (0x3C008000, 0xBC003C00, 0x3C008000, 0x00008000),
        (0x80003C00, 0x3C00BC00, 0x00003C00, 0x00000000),
        (0x7C008000, 0x40003C00, 0x3C000000, 0x7C000000),
        (0x7C007C00, 0xC0004000, 0x7C003C00, 0xFE007C00),
        (0x7C007C00, 0x0000C000, 0x3C007C00, 0xFE00FE00),
        (0x7BFF7C00, 0x40000000, 0x00003C00, 0x7C00FE00),
        (0x7E557BFF, 0x3C004000, 0x3C000000, 0x7E557C00),
        (0x7C017E55, 0x3C003C00, 0x3C003C00, 0x7E017E55),
        (0x3C007C01, 0x3C003C00, 0x7D233C00, 0x7F237E01),
        (0x04003C00, 0x38003C00, 0x00007D23, 0x00007F23),
        (0x00010400, 0x44003800, 0x00000000, 0x00000000),
        (0x00030001, 0x38004400, 0x00000000, 0x00000000),
        (0x3C000003, 0x3C003800, 0x64000000, 0x64010000),
        (0x44003C00, 0xC2003C00, 0x49006400, 0xC0006401),
        (0x40004400, 0x4200C200, 0x3C004900, 0x4700C000),
    ]),
    "rtz": (0x000003CC, [
        (0x3C014000, 0x3C034200, 0xBC003C00, 0x1C004700),
        (0x3C013C01, 0x3C013C03, 0xBC02BC00, 0x00101C00),
        (0x3C013C01, 0x3E003C01, 0x8001BC02, 0x3E010010),
        (0x3E013C01, 0x3E013E00, 0x00008001, 0x40813E01),
        (0xBE013E01, 0x3E013E01, 0x80000000, 0xC0814081),
        (0x0000BE01, 0x3C003E01, 0x80008000, 0x0000C081),
        (0x80000000, 0x3C003C00, 0x80008000, 0x80000000),
        (0x3C008000, 0xBC003C00, 0x3C008000, 0x00008000),
        (0x80003C00, 0x3C00BC00, 0x00003C00, 0x00000000),
        (0x7C008000, 0x40003C00, 0x3C000000, 0x7C000000),
        (0x7C007C00, 0xC0004000, 0x7C003C00, 0xFE007C00),
        (0x7C007C00, 0x0000C000, 0x3C007C00, 0xFE00FE00),
        (0x7BFF7C00, 0x40000000, 0x00003C00, 0x7BFFFE00),
        (0x7E557BFF, 0x3C004000, 0x3C000000, 0x7E557BFF),
        (0x7C017E55, 0x3C003C00, 0x3C003C00, 0x7E017E55),
        (0x3C007C01, 0x3C003C00, 0x7D233C00, 0x7F237E01),
        (0x04003C00, 0x38003C00, 0x00007D23, 0x02007F23),
        (0x00010400, 0x44003800, 0x00000000, 0x00040200),
        (0x00030001, 0x38004400, 0x00000000, 0x00010004),
        (0x3C000003, 0x3C003800, 0x64000000, 0x64010001),
        (0x44003C00, 0xC2003C00, 0x49006400, 0xC0006401),
        (0x40004400, 0x4200C200, 0x3C004900, 0x4700C000),
    ]),
}

AI-assisted (Claude Code, Claude Opus 5.5 and Claude Fable 5.2).

@Taffyyy-ops

Copy link
Copy Markdown
Contributor Author

Huge thanks for catching that inverted op_sel_hi check and for running all 82 hardware vectors on gfx1036 @aslan2142!

Updated in 8e7f591c:

  • Fixed tools/rdna2spirv.py so op_sel defaults to [0,0,0] and op_sel_hi defaults to [1,1,1], accepting explicit default op_sel:[0,0,0] / op_sel_hi:[1,1,1] while refusing any non-default op_sel/op_sel_hi (such as op_sel_hi:[0,0,0]) as Unsupported.
  • Added all 60 gfx1036 integer dot-product tuples (plain & clamp, 60/60 bit-exact) and all 22 default-MODE v_pk_fmac_f16 tuples (both source orders) to tests/semantic/test_alu_dot_pk_fmac.py (14/14 tests passing).
  • Updated docs/design.md to document the op_sel_hi:[1,1,1] default and cite the RDNA2 gfx1036/gfx1035 hardware verification.

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.

5 participants