Repository navigation
fix(shader): keep fused multiply-adds fused and v_mad/v_mac_f32 rounded twice - #263
Closed
adriadam10 wants to merge 2 commits into
Closed
adriadam10 wants to merge 2 commits into
adriadam10 wants to merge 2 commits into
Conversation
MixPrecision and SdwaDestination dispatch hand-assembled RDNA through the recompiler and compare every lane with a CPU model, but were never in ctest. Move them to tests/execution/, which is globbed into one test per file with the same target names, so new execution tests add a file and no CMake lines. They exit 77 (skipped) when the host has no Vulkan device; the Linux runner installs lavapipe and sets ANYPS5_REQUIRE_VULKAN=1 so they cannot skip there. Co-Authored-By: Claude <noreply@anthropic.com>
Merged
9 tasks done
…ed twice SPIR-V float ops without NoContraction let the driver fuse or split them: RADV contracted a guest v_mul_f32 + v_add_f32 into one fma, which made the f32 division macro one ulp off. f32 add, sub, mul and fma now carry NoContraction. v_mad_f32, v_mac_f32, v_madmk_f32 and v_madak_f32 round the product before the add, unlike their v_fma*/v_fmac* counterparts, but all of them were translated to FPFma32. They now use a new FPMad32, emitted as a separate NoContraction multiply and add. Co-Authored-By: Claude <noreply@anthropic.com>
adriadam10
force-pushed
the
fix/shader-fma-fusion
branch
from
October 1, 2026 20:59
5b28d81 to
a16293d
Compare
This was referenced Oct 2, 2026
Merged
This was referenced Oct 2, 2026
This was referenced Oct 2, 2026
This was referenced Oct 2, 2026
Collaborator
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)