Skip to content

feat(rdna2spirv): implement v_fma_legacy_f32 and v_fmac_legacy_f32 - #3188

Open
klarnet2009 wants to merge 2 commits into
ItsNotRexed:mainfrom
klarnet2009:feat/rdna2spirv-fma-legacy-f32
Open

klarnet2009 wants to merge 2 commits into
ItsNotRexed:mainfrom
klarnet2009:feat/rdna2spirv-fma-legacy-f32

Conversation

@klarnet2009

Copy link
Copy Markdown
Contributor

What and why

Implements v_fma_legacy_f32 and v_fmac_legacy_f32 (GFX10.3 single-rounded FMA with DX9 legacy zero rule 0 * anything = +0.0 and without flushing f32 denormals) in tools/rdna2spirv.py:

  • Extends the legacy FMA/MAC handler and both RMW pre-passes (read_before_write_vgprs, reg_reads_writes) for v_fmac_legacy_f32.
  • Documents both ops in docs/design.md (CPU-checked, not yet hardware-measured on RDNA2) and adds synthetic translator-to-interpreter + RMW tests in tests/semantic/test_alu_fma_legacy_f32.py (5 tests).

Tested

  • OS: Windows (Python 3.12.0), GPU: n/a (CPU translator + spirv_mini_interp.py)
  • py -3.12 -I -m compileall -q tools/rdna2spirv.py tests/semantic/test_alu_fma_legacy_f32.py: passed (exit 0)
  • py -3.12 -I tests/semantic/test_alu_fma_legacy_f32.py -v: Ran 5 tests in 0.056s, OK
  • py -3.12 -I tests/semantic/test_valu_mad_lit.py -v: Ran 15 tests in 1.574s, OK
  • py -3.12 -I tests/test_isa_coverage.py -v: Ran 13 tests in 3.376s, OK (skipped=3 for LEONIDA_RDNA2_ISA_XML)
  • py -3.12 -I tests/test_ctest_registration.py -v: Ran 4 tests in 0.042s, OK
  • Deliberate-breakage check: reverted tools/rdna2spirv.py to HEAD and confirmed test_alu_fma_legacy_f32.py failed (1 failure, 4 Unsupported errors), then restored and re-ran 5/5 passing.

Checklist

  • No game data / keys / decryption
  • Standard library only (python -I compatible)
  • AI-assisted: yes

@da1chan9ha0

Copy link
Copy Markdown

Independently tested d502952878295f3383b645487b9428eeb871182d on an RTX 4070 Laptop (Windows 11, NVIDIA 621.24, Vulkan 1.4.357). LLVM 23.1.5 assembled the hand-written compute fixtures for gfx1030. I used separately built harnesses for the PR and main 1628210, and validated all dispatched modules.

Both GLSL.std.450 Fma and OpFmaKHR gave the fused cancellation result 0x28800000 for both legacy instructions. The DX9 zero-multiply controls also matched, including 0 * Inf with a negative addend and with negative zero.

One device limitation worth documenting: the raw input word 0x00400000 (f32 subnormal) read back correctly when copied as bits, but using it in a legacy FMA or FMAC produced zero in the tested arithmetic cases under both emission modes. For example, 0x00400000 * 2 + 0 returned 0x00000000 rather than 0x00800000. The ordinary FMA control on unmodified main showed the same behavior. This adapter reports shaderDenormPreserveFloat32=false and shaderFmaFloat32=true. So the GPU results support the fused and legacy-zero behavior, while f32 denormal preservation cannot be assumed on this device. This is evidence from translated NVIDIA Vulkan execution; the native RDNA2 behavior was not measured.

Synthetic inputs only. AI-assisted testing and reporting.

@javdc

javdc commented Nov 22, 2026

Copy link
Copy Markdown

Tested d502952 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_alu_fma_legacy_f32.py — 5/5 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

Measured v_fma_legacy_f32 / v_fmac_legacy_f32 natively on RDNA2 and compared with this head (d5029528) on two GPUs. The zero rule is confirmed. The "without flushing f32 denormals" sentence in the new docs/design.md row is the opposite of what the hardware does. AI-assisted run, synthetic shaders only, no game files.

Native, AMD Radeon iGPU (Ryzen 9 9950X3D, gfx1036, driver 25.10.30.02), raw kernels through tools/hw_oracle, MODE.IEEE 0 and 1 × denorm32 0 and 3. Both legacy ops gave the same word in all four MODEs; v_fma_f32 is the control:

a·b + c v_fma_legacy_f32 = v_fmac_legacy_f32 v_fma_f32, denorm32 = 0 / 3
0·inf + 1, inf·0 + 1, −0·inf + 1, 0·NaN + 1 3f800000 NaN
−0·5 + (−0) 00000000 80000000
2·3 + 1 / (1+2⁻²³)² − 1 40e00000 / 34800000 (single rounding) same
0x00000003·1 + 0 00000000 00000000 / 00000003
0x80000003·1 + 0 00000000 00000000 / 80000003
2·0x00000003 + 0 00000000 00000000 / 00000006
0·1 + 0x00000003 00000000 00000000 / 00000003
min-normal·0.5 + 0 00000000 00000000 / 00400000
−min-normal·0.5 + 0 80000000 80000000 / 80400000
1·min-normal + 0x80000003 00800000 00800000 / 007ffffd
inf·1 + (−inf) ffc00000 ffc00000

So the zero rule is as the PR says (+0 product, NaN and inf included, and −0·x + −0 = +0). But the legacy ops flush f32 denormals in every operand and in the result even with denorm32 = 3, where v_fma_f32 keeps them. A denormal result flushes to a zero of its sign.

This head, run-time operands (s4..s6 from VKC_SGPRS, a hand-written compute shader, not part of this PR), on the iGPU through Vulkan and on an RTX 5080 (621.01). It had to be assembled for gfx1030: llvm-mc refuses both ops for gfx1013 ("instruction not supported on this GPU"), as docs/shader_validation.md already notes ("new in GFX10.3, not in gfx1013"). All 13 cases above matched the hardware words on the iGPU. On the RTX 5080, 12 matched, and inf·1 + (−inf) gave 7fffffff (the driver's NaN) instead of ffc00000. I didn't check whether plain v_fma_f32 does the same on that driver.

The translated denormal cases came out flushed, as on hardware, on both GPUs. But the PR doesn't flush explicitly, so that comes from the drivers' default f32 denormal handling, which Vulkan doesn't fix (the v_floor/ceil/fract_f32 text in the "Float min/max/med3 with NaN" row describes the same problem). A driver that keeps f32 denormals would give 0x00000003 etc. here. The row's "without flushing f32 denormals" should say the opposite, and the hardware measurement could replace the "not yet hardware-measured" note.

Also worth stating in the PR: since gfx1013 has neither op, they should never occur in the console's shaders.

@JRamos1993

Copy link
Copy Markdown
Contributor

Tested d502952 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_fma_legacy_f32.py: ran 5 tests in 0.122s; OK.

AI-assisted (Claude Code).

@TreXito

TreXito commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

Tested d5029528 (base 16282101) on NVIDIA GeForce RTX 3060 Ti (Ampere), driver 580.173.02 (Vulkan 1.4.312), Linux (Ubuntu 26.04 container on a TrueNAS 26.0 host): pass, as a regression check only.

  • scripts/run_gpu_tests.py, whole suite: 142/189 passed, 46 skipped, 1 failed, identical per test to the base. The failure is ps_sdwa_f16_const, the NVIDIA f16 denormal flush on main (rdna2spirv: widen f16 denormal operands without UnpackHalf2x16 (NVIDIA reads them as 0) #3220 / rdna2spirv: declare DenormPreserve 16 so a driver keeps f16 denormals (SHADER-526) #3040), not this PR.
  • ps_legacy_mul, ps_fma_literal and ps_fmac_f16_mods pass, so the shared legacy-multiply and FMA paths this PR extends are unchanged on the GPU.
  • ctest on the Linux build (CMake + Ninja, GCC): 37/37 passed.
  • Not covered: the suite has no fixture with v_fma_legacy_f32 or v_fmac_legacy_f32, so this run doesn't exercise the new ops on hardware. Your semantic cases would work as ps_* fixtures with exact mrt0 words. The denormal one is the interesting case on NVIDIA, which reports shaderDenormPreserveFloat32 = false.
  • The branch conflicts with main now; this is the head as pushed.

AI-assisted (Claude Code). Synthetic test shaders only, no game files.

@klarnet2009

Copy link
Copy Markdown
Contributor Author

Thank you @TheMrBeez for measuring v_fma_legacy_f32 on native RDNA2 (gfx1036)! Fixed in feat/rdna2spirv-fma-legacy-f32:

  • Updated v_fma_legacy_f32 / v_fmac_legacy_f32 in tools/rdna2spirv.py to flush f32 denormals on all three inputs (flush_f32(src_f(...))) and on the result (flush_f32(clamp_mods(...))).
  • Added test_denormals_flushed_gfx1036 in tests/semantic/test_alu_fma_legacy_f32.py pinning all 7 gfx1036 denormal cases you measured (for both v_fma_legacy_f32 and v_fmac_legacy_f32).
  • Updated docs/design.md to record your gfx1036 hardware measurement and the GFX10.3-only note.

Verified with py -3.12 -I:

  • tests/semantic/test_alu_fma_legacy_f32.py -v: 5/5 OK (0.154s)
  • tests/semantic/test_valu_mad_lit.py -v: 15/15 OK (1.961s)
  • tests/test_isa_coverage.py -v: 18 tests OK (3 skipped for LEONIDA_RDNA2_ISA_XML unset, 8.717s)
  • tests/test_ctest_registration.py -v: 4/4 OK (0.421s)
  • compileall -q tools/rdna2spirv.py tests/semantic/test_alu_fma_legacy_f32.py: exit 0

@iurysf

iurysf commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Re-tested 460776ed (head moved since the earlier results) on Windows 11, Python 3.14, llvm-mc 23.1.6, SPIRV-Tools 2026.4, CPU only: pass. test_alu_fma_legacy_f32.py: 5 tests OK; with only tools/rdna2spirv.py restored to main it gives 1 failure and 10 errors. Not run: full semantic suite, GPU. Tested with AI assistance (Claude); synthetic shaders only, no game data.

@Kev-Ball

Copy link
Copy Markdown

Tested 92c7df59 on an NVIDIA GeForce RTX 3080 Ti (32-lane subgroups), driver 620.73.04, CachyOS Linux 7.3.5-2-cachyos. I rebuilt the Vulkan harnesses from this revision and selected the NVIDIA device explicitly.

Reused the instruction lines and assertions in test_alu_fma_legacy_f32.py, routing its run_ps helper through Vulkan 1.2 assembly/validation and vk_harness with vs_ids. Across 11 draws, all 4,096 pixels of each draw were identical, and the GPU words satisfied the existing rounding, legacy-zero, denormal and modifier assertions. All five test methods passed (the register prepass method is CPU-only). This exercises the translated path on Ampere.

AI-assisted verification with synthetic inputs.

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