fix(shader): keep fused multiply-adds fused and v_mad/v_mac_f32 rounded twice #263
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/263"
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 adriadam10 on 2026-10-01T20:52:03Z.
What
Two rounding bugs in multiply-add, both visible as off-by-one-ulp results:
NoContraction, so the Vulkan driver is free to contracta*b + cinto an fma or to split an fma. RADV contracts a guestv_mul_f32followed byv_add_f32into one fma (51 of 64 lanes differ in the test), which the hardware rounds twice. This is also what made LLVM's precise f32 division macro come out one ulp off on RADV. f32 add, sub, mul and fma results now carryNoContraction.v_mad_f32,v_mac_f32(VOP2 0x1f and its VOP3 form),v_madmk_f32andv_madak_f32were translated as fused. They round the product before the add (the ISA describesv_mad_f32as an add after an IEEE multiply, and LLVM selects them for its separately roundedfmad), unlikev_fma_f32/v_fmac_f32/v_fmamk_f32/v_fmaak_f32. The decoder maps both families to the sameRdnaOpcode(VMacF32,VMadmkF32,VMadakF32), so the translation tells them apart by the encoding and uses a new IR opFPMad32, emitted as a separateNoContractionmultiply and add. LLVM picksv_mad/v_macforfmuladdwhen f32 denormals are flushed, which is the graphics default, so these are common.The mad family isn't present on gfx10.3, so I could not check it against hardware here (my GPU is gfx1035). The behaviour follows the ISA doc and LLVM's semantics for these instructions.
Test
agc_driver_fma_rounding: hand-assembled shader (words fromllvm-mc -mcpu=gfx1013) running the eight fused/unfused forms plusv_mul_f32+v_add_f32on 64 lanes. Inputs are picked so thatc = -round(a*b), where fused and unfused results differ (51 of 64 rows). Expected values are computed with exact rational arithmetic and embedded.Fusion cannot be required in Vulkan. Ubuntu's lavapipe (the CI one) splits
GLSLstd450Fmainto a multiply and an add even withNoContraction, so on such a device the fused forms cannot be exact. The test checks them only when the device does fuse, detected from thev_fma_f32rows where fused and unfused differ, and prints that it skipped them otherwise. The mad family andv_mul+v_addare checked everywhere.Tested
Linux, full ctest (99) passes.
v_mul_f32 + v_add_f32wrong in 51 lanes and the whole mad family fused (v_mad,v_mace32/e64: 51;v_madak,v_madmk: 64). lavapipe: the same, exceptv_mul + v_add, which it does not contract.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 adriadam10 on 2026-10-03T09:40:14Z.
Closing: this commit already landed on main with #251 (
9e0060cb), so there is nothing left to merge here.Pull request closed