fix(shader): round the f16 fused multiply-adds once #2000
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2000"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by hansjm10 on 2026-10-09T04:43:59Z.
What
v_fma_f16,v_fmac_f16,v_fmamk_f16,v_fmaak_f16,v_pk_fma_f16andv_pk_fmac_f16were translated as an f32 FMA rounded to f16, which rounds twice:0x62e9 * 0xd159 + 0x23ecgave0xf89e, the hardware0xf89f(TechnicalDebt).The product of two f16 values is exact in f32. The translation now adds it to the addend in f32, recovers the exact rounding error with TwoSum, rounds the sum to odd and converts it with the existing round-to-nearest-even f16 conversion. Rounding to odd with at least p + 2 bits and then to nearest with p bits is one rounding (Boldo and Melquiond, "Emulation of FMA and Correctly Rounded Sums: Proved Algorithms Using Rounding to Odd", IEEE Transactions on Computers 57(4), 2008); f32 has 24 bits, f16 11. The f32 add and sub are already emitted with
NoContraction, so drivers keep them exact. No f64 is needed.v_fma_mix*(f32 sources) is unchanged.Source of the semantics: measured on RDNA2, gfx1036 (Ryzen iGPU, RADV, Mesa 22.3.6), through a Vulkan compute shader whose f16
fma()ACO compiled tov_fmac_f16_e32,v_pk_fmac_f16,v_fma_f16andv_pk_fma_f16(checked in theRADV_DEBUG=shadersdisassembly). For each of the four, 2,200,256 inputs (the test rows, 1M uniform random, 1M near-cancellation, 200k with subnormal results) all match exact single rounding to nearest even, with f16 denormals kept and overflow to infinity. Not run with the hardware oracle: the float mode was the driver's default, and no NaN inputs were used. The PS5's gfx1013 is not measured.Removes the TechnicalDebt entry.
Tested
Linux (Debian 12, GCC 12.2), Release.
agc_driver_float16_fma_rounding: 64 lanes, 7 instruction forms. 116 of its 128v_fma_f16/v_pk_fma_f16triples and 37 of itsv_fmaak_f16/v_fmamk_f16rows round differently when rounded twice; they include results at the f16 overflow boundary (252 * 260 - 2^-24:0x7bff, twice rounded0x7c00), subnormal results, signed zeros and infinities. Fails on main (lane 0v_fma_f16is0xf89e, expected0xf89f); passes on RADV gfx1036, Intel Arc A310 (ANV) and lavapipe, under 0.1 s.agc_driver_float16_ternary_clamp,agc_driver_packed_alu,agc_driver_fma_rounding,agc_driver_float16_modifierspass on all three devices.agc|shader|recompiler|spirv|fused|vopon lavapipe (Mesa 22.3.6): 6 failures that fail identically on main (missing device features), andagc_driver_flat_storetimed out under-j12. On RADV gfx1036: 8 failures that also fail on main, andagc_driver_scalar_wide_loadsfailed once under-j8and passes 5 of 5 alone.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T04:51:55Z.
Review: COMMENTED
The finite-value path has a sound basis: the f16 product is exact in f32, and the SPIR-V f32 add/sub/mul emitters apply
NoContraction, which the TwoSum error recovery needs. The new execution test also exercises the conversion boundary and all six instruction families.One scope note: the PR says NaNs were not measured or included in the hardware matrix, while the new non-mix path replaces one FMA with multiply/add operations. The existing TechnicalDebt entry still says arithmetic NaN bits are driver-dependent; please confirm that remains the intended documented limit for this path, especially invalid
inf * 0inputs to the non-packed instructions. I am not approving without upstream validation: Build and Conventions are stillaction_requiredon this head.Imported from boykopovar/AnyPS5. Originally posted by hansjm10 on 2026-10-09T04:59:38Z.
Yes, that stays the documented limit: the scalar f16 FMA NaN payload is the driver's, as TechnicalDebt says for
v_add/mul/fma_f32/f16and for scalar f16 arithmetic under the clamp modifier.mul+addis NaN exactly when the fused op was: a NaN source,inf * 0orinf - inf. A NaN sum makes the TwoSum error NaN, the ordered!= 0test is false, and the sum passes through unchanged.packedFloat16still builds NaN lanes from the source bits (the gfx1035 rule, includinginf * 0giving the default NaN over a NaN src2).fmato itsmul/add.Checked with a throwaway probe (12 lanes of NaN, sNaN,
inf * 0,inf * 0 + NaN,inf - infand all-NaN sources through the 7 forms of the new test), on main and on this branch:v_fma_f16/v_fmac_f16changed, in two cases.inf * 0 + 0x7e01was0x7e01, now0xfe00; sources0x7e11, 0x7c22, 0x7e33gave0x7e33, now0x7e11. Packed,v_fmaak_f16andv_fmamk_f16are unchanged. On this branch the three drivers return the same bits for every probe row; on main ANV differed in those two.0x7c01 * 1 + 1gives0x7e01), where the packed path keeps0x7c01at IEEE=0, as already documented.Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T09:36:11Z.
Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.
main)docs/dev/TechnicalDebt.mdL330-332docs/dev/TechnicalDebt.mdL330-332core/shader/recompiler/Translation/src/FloatInstructions.cppL21-22, L25-26, L216-217docs/dev/TechnicalDebt.mdL331-332docs/dev/TechnicalDebt.mdL331-332docs/dev/TechnicalDebt.mdL331-332core/shader/recompiler/Translation/src/FloatInstructions.cppL216docs/dev/TechnicalDebt.mdL331-332Imported from boykopovar/AnyPS5. Originally posted by popcoder565 on 2026-10-09T15:23:44Z.
Native Windows run, since no Windows results were posted. Setup: Windows 10 (19045), RTX 3060 Laptop (NVIDIA 616.92), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release. Head
6de064f6, merged ontomaind70b8998in one build with 33 other AGC/shader PRs that had no Windows run.agc_driver_float16_fma_rounding. None of them reported skipping a case.PATHsetup.No title was run.
🤖 Addressed by Claude Code
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-10T06:07:57Z.
Checked on the HW oracle (RDNA2 gfx1035) with the test's seven instruction forms: with f16 denormals kept, at IEEE=0 and IEEE=1, the hardware matches the
Expectedtable bit for bit and matches this lowering on 4,820 more rows (random plus ±0, subnormals, the 0x7bff overflow boundary, infinities, NaNs), and every finite case equals exact single rounding. The test fails without the change and passes on RADV and on CI's lavapipe (Mesa 25.2.8); popcoder565's Windows/NVIDIA run covers the rest. Note for whichever of this and #2025 lands second: they touch the same lines ofFloatInstructions.cpp, andclampF16OverflowwrapsfmaF16RoundedToOddwithout issue. Merging.