[llvm] [WebAssembly] Lower fmuladd to fma (PR #161355)
Derek Schuff via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 3 08:42:06 PDT 2025
================
@@ -103,9 +136,8 @@ define <4 x float> @fmuladd_4xf32(<4 x float> %a, <4 x float> %b, <4 x float> %c
; RELAXED-LABEL: fmuladd_4xf32:
; RELAXED: .functype fmuladd_4xf32 (v128, v128, v128) -> (v128)
; RELAXED-NEXT: # %bb.0:
-; RELAXED-NEXT: f32x4.mul $push0=, $0, $1
-; RELAXED-NEXT: f32x4.add $push1=, $pop0, $2
----------------
dschuff wrote:
My reading of the LLVM [FMA intrinsic](https://llvm.org/docs/LangRef.html#llvm-fma-intrinsic) is that it only allows double-rounding if the 'afn' flag is set, and it looks like the ISD FMA is the same. That means that the relaxed madd is not actually a valid implementation of that, because it might round once or twice. So in principle, this pattern should only match when the fast math flag is set on the ISD node. (And unfortunately I think that just because the user enabled relaxed-simd doesn't necessarily mean that they are opting in to all FMA operations being allowed to double round. All the feature-enablement flags just control what proposal is allowed in the output, rather than semantics).
So relaxed-simd or no, we have the general problem that if single-rounding is required, there really is no guaranteed valid implementation, short of some kind of slow software emulation.
If the FMAs are generated from e.g. C++ where double or single rounding is allowed, the mul + add or whatever fused operations are generated should already have the appropriate flags and things should just work. (or we could in principle fix them so they do). But something like std::fma requires single rounding, and I don't think we can claim to correctly implement that with relaxed simd without some way for the user to opt-in to the nondeterminism. Which, probably we should go ahead and add somehow, because as you found above, the performance benefit seems pretty substantial.
https://github.com/llvm/llvm-project/pull/161355
More information about the llvm-commits
mailing list