Skip to content

fix(shader): keep fused multiply-adds fused and v_mad/v_mac_f32 rounded twice - #263

Closed
adriadam10 wants to merge 2 commits into
boykopovar:mainfrom
adriadam10:fix/shader-fma-fusion
Closed

adriadam10 wants to merge 2 commits into
boykopovar:mainfrom
adriadam10:fix/shader-fma-fusion

Conversation

@adriadam10

@adriadam10 adriadam10 commented Oct 1, 2026 •

Copy link
Copy Markdown
Collaborator

What

Two rounding bugs in multiply-add, both visible as off-by-one-ulp results:

  1. The driver could fuse or split float ops. No f32 op carried NoContraction, so the Vulkan driver is free to contract a*b + c into an fma or to split an fma. RADV contracts a guest v_mul_f32 followed by v_add_f32 into 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 carry NoContraction.
  2. v_mad_f32, v_mac_f32 (VOP2 0x1f and its VOP3 form), v_madmk_f32 and v_madak_f32 were translated as fused. They round the product before the add (the ISA describes v_mad_f32 as an add after an IEEE multiply, and LLVM selects them for its separately rounded fmad), unlike v_fma_f32/v_fmac_f32/v_fmamk_f32/v_fmaak_f32. The decoder maps both families to the same RdnaOpcode (VMacF32, VMadmkF32, VMadakF32), so the translation tells them apart by the encoding and uses a new IR op FPMad32, emitted as a separate NoContraction multiply and add. LLVM picks v_mad/v_mac for fmuladd when 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 from llvm-mc -mcpu=gfx1013) running the eight fused/unfused forms plus v_mul_f32 + v_add_f32 on 64 lanes. Inputs are picked so that c = -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 GLSLstd450Fma into a multiply and an add even with NoContraction, so on such a device the fused forms cannot be exact. The test checks them only when the device does fuse, detected from the v_fma_f32 rows where fused and unfused differ, and prints that it skipped them otherwise. The mad family and v_mul + v_add are checked everywhere.

Tested

Linux, full ctest (99) passes.

  • With the fix: test passes on RADV (Radeon 660M) and lavapipe.
  • Without it, RADV: v_mul_f32 + v_add_f32 wrong in 51 lanes and the whole mad family fused (v_mad, v_mac e32/e64: 51; v_madak, v_madmk: 64). lavapipe: the same, except v_mul + v_add, which it does not contract.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: ci: run the shader execution tests on lavapipe #251 (test harness); review only the last commit
  • AI-assisted: yes

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>
…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
adriadam10 force-pushed the fix/shader-fma-fusion branch from 5b28d81 to a16293d Compare October 1, 2026 20:59
@github-actions github-actions Bot added build shader core/shader agc libSceAgc* ci .github labels Oct 2, 2026
@adriadam10

Copy link
Copy Markdown
Collaborator Author

Closing: this commit already landed on main with #251 (9e0060c), so there is nothing left to merge here.

@adriadam10 adriadam10 closed this Oct 3, 2026
@adriadam10
adriadam10 deleted the fix/shader-fma-fusion branch October 5, 2026 06:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agc libSceAgc* build ci .github shader core/shader

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant