Skip to content

rdna2spirv: v_fmamk_f16 and v_fmaak_f16 - #1989

Merged
ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
LouisRiverstone:valu-fmamk-fmaak-f16
Nov 22, 2026
Merged

ItsNotRexed merged 1 commit into
ItsNotRexed:mainfrom
LouisRiverstone:valu-fmamk-fmaak-f16

Conversation

@LouisRiverstone

@LouisRiverstone LouisRiverstone commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

v_fmamk_f16 and v_fmaak_f16 raised Unsupported, while v_fmamk_f32/v_fmaak_f32 and v_fmac_f16 are translated. RDNA2 ISA, VOP2 with a literal K:

  • v_fmamk_f16: D = S0 * K + S1, printed vdst, src0, K, vsrc1
  • v_fmaak_f16: D = S0 * S1 + K, printed vdst, src0, vsrc1, K

Implementation: in print order both are a[1] * a[2] + a[3], as the f32 forms already handle them (their fmamk operand order was fixed on 2026-11-21, per the comment there).

  • K is the f16 in the literal's low 16 bits. A float literal such as 0.5 is converted to f16, through src_h.
  • One rounding to f16 through fma_f16_odd, the same as v_fmac_f16 (SHADER-55).
  • The result goes to the low half and the high half is kept: GFX10 f16 VOP2 writes are partial (f16_out).

Tests: tests/semantic/test_valu_fmamk_fmaak_f16.py. Each case must give exactly the bits of v_fma_f16 with K in a VGPR, whose rounding is tested elsewhere:

  • fmamk on a 6×12×4 grid and fmaak on a 6×4×12 grid of f16 values: ±1, 0.5, large values, subnormals, ±0 and inf
  • a float literal K, and the operand order (2·0.5+3 = 4 against 2·3+0.5 = 6.5)
  • 25 seeded cases
  • the high half kept

Halves are exported separately, so no NaN pattern can be quieted. Without the change the file fails (602 errors); with it, 4/4 pass. python -I -m unittest discover -s tests/semantic: 1648 tests OK (2 skipped).

docs/design.md: new row after "f16 inline constants".

Tested on CPU only; no GPU run. No game files. I searched for an open PR or claim first and found none.

AI-assisted (Claude Code): I reviewed and ran the change and the tests.

@duudiis

duudiis commented Nov 22, 2026

Copy link
Copy Markdown

Independently tested 483f1e93cf7e1c8700a4a67bef16535d61a7e325 on Windows with Python 3.11.0. All four new semantic tests passed. I also checked fused cancellation using (1 + 2^-10) * (1 - 2^-10) - 1: both forms returned 0xabcd8010 from an 0xabcd upper-half sentinel, as expected.

One integration caveat worth recording in the contract row: LLVM #237158 reports that affected llvm-mc builds can print a different K for these instructions, and can crash on FP16 K = 0xff. LLVM #238820 adds GFX10 coverage. The current PR reads K from the printed operand. I exercised both forms with synthetic listings that differed only in the printed K (0x80 vs 0x0), with the same encoding comment containing K = 0x80. Both returned 0xabcd0080 for correct text and 0xabcd0000 for the misprinted operand. This is a CPU translation check using synthetic text, not a local reproduction of the LLVM crash. The implementation works for correct disassembly. Could the docs say that explicitly and link the LLVM bug so this has a separate place to track it?

AI-assisted verification and review.

@LouisRiverstone

Copy link
Copy Markdown
Contributor Author

Overlap note: @IAmNotSiob's #1976 (opened 15:20, three minutes before this one) also adds v_fmamk_f16 / v_fmaak_f16, among eleven ops. If #1976 lands first, I will close this or reduce it to its test (test_valu_fmamk_fmaak_f16.py checks both against v_fma_f16 with K in a VGPR), whichever the maintainer prefers.

Both were rejected while the f32 forms are translated. In print order
both are a[1] * a[2] + a[3] (fmamk S0 * K + S1, fmaak S0 * S1 + K), with
K the literal's low 16 bits (a float literal converted to f16), one
rounding to f16 as v_fmac_f16, and the high half kept (GFX10 f16 VOP2).

tests/semantic/test_valu_fmamk_fmaak_f16.py compares both with v_fma_f16
with K in a VGPR. docs/design.md row added.
@CaravelaLabs

Copy link
Copy Markdown
Contributor

Tested d586d053 merged onto main 16282101 on AMD Radeon 8060S (RADV GFX1151, RDNA 3.5, subgroup 32..64), Mesa 25.2.8, Ubuntu 24.04, LLVM 23.1.6, SPIRV-Tools 2026.4: pass.

  • python -I scripts/run_gpu_tests.py --build runtime/build/linux: 172/189 passed, 15 skipped, identical per test to plain main 16282101 on this machine. The two failures in both runs, cs_signed_zero_nan and cs_w32_salu64, are the known Mesa 25.2 RADV ones (GPU test results (one comment per GPU and driver) #166, docs/testing.md).
  • tests/semantic/test_valu_fmamk_fmaak_f16.py: 4 passed, 601 subtests.
  • GPU probe: a throwaway hand-written compute fixture (not committed, no game data) with the cases below, translated by this branch and run with vk_compute_test (wave32). v_fmamk_f16 v, 2.0, 0.5(K), 3.0 → 0xabcd4400 and v_fmaak_f16 v, 2.0, 3.0, 0.5(K) → 0xabcd4680 (high half kept); both forms bit-identical to v_fma_f16 with K in a VGPR on a rounding case (0x3555·0x3555 + 0x8400 → 0x2f1b) and near the top of the range (0xc500·0x3555 + 0x7bff → 0x7bff). main rejects both opcodes.

gfx1151 is RDNA 3.5, not RDNA2: this shows the translated SPIR-V gives the modelled words on a real driver, not that RDNA2 hardware does. AI-assisted (Claude Code).

@javdc

javdc commented Nov 22, 2026

Copy link
Copy Markdown

Tested d586d05 on Steam Deck LCD (Jupiter), AMD Custom GPU 0405 "Van Gogh", RADV Mesa 26.2.0-devel (git-5c65d4fc31), Vulkan 1.4.350, SteamOS 3.9.5 (holo), kernel 7.2.7-valve2-2-neptune-72-g31504582a073: pass.

Semantic tests:

  • python3 tests/semantic/test_valu_fmamk_fmaak_f16.py — 4/4 OK

Full GPU suite:

  • python3 -I scripts/run_gpu_tests.py --build /home/deck/opencode/Leonida/runtime/build — 175/189 passed, 14 skipped, 0 failed. Skips are the expected D3D12-owned skips on this Deck.

AI-assisted (OpenCode).

@TheMrBeez

Copy link
Copy Markdown

Native hardware check of the two encodings on RDNA2: pass. AI-assisted, synthetic only, no game files.

The PR's tests compare the translated v_fmamk_f16/v_fmaak_f16 with the translated v_fma_f16, so I checked the same equivalence on the hardware. AMD Radeon iGPU (Ryzen 9 9950X3D, gfx1036, driver 25.10.30.02), raw kernels through tools/hw_oracle (AMD clang from rocm-sdk-core 10.0.0), wave32 and wave64. Each case runs v_fmamk_f16 v8, v1, K, v2 against v_fma_f16 v9, v1, vK, v2, and v_fmaak_f16 v8, v1, v2, K against v_fma_f16 v9, v1, v2, vK. Both destinations start as 0xabcd1111.

  • 876/876 equal in each wave size: a grid of f16 values (±1, 0.5, 3, the largest finite value, the smallest subnormal, ±0, +inf, 1+ulp, −5, 2^-10) for S0 × K × S1, plus 150 seeded random finite triples, for both ops.
  • The high half is kept (0xabcd) in every case.
  • Operand order: v_fmamk_f16(2, K=0.5, 3) = 0xabcd4400 (4.0) and v_fmaak_f16(2, 3, K=0.5) = 0xabcd4680 (6.5), the a[1]*a[2]+a[3] print order the PR uses.

So on hardware both ops are exactly v_fma_f16 with K in a register, which is what the semantic test pins the translation to. I didn't rerun the GPU suite, since three people already have.

@ItsNotRexed
ItsNotRexed merged commit dead2b3 into ItsNotRexed:main Nov 22, 2026
2 of 4 checks passed
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.

6 participants