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

Closed
adriadam10 wants to merge 1 commit from upstream-pr/263 into main
adriadam10 commented 2026-10-01 20:52:03 +00:00 (Migrated from github.com)

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:

  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: #251 (test harness); review only the last commit
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4709458516 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/263). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-01T20:52:03Z. --- ### 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 - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #251 (test harness); review only the last commit - [x] AI-assisted: yes
adriadam10 commented 2026-10-03 09:40:14 +00:00 (Migrated from github.com)

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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5967867712 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/263#issuecomment-5967867712). Originally posted by [adriadam10](https://github.com/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

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!263
No description provided.