[llvm] [AArch64] Don't call fma/fmaf for @llvm.fmuladd.f64/f32 in softfp (PR #205356)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 23 07:32:11 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-aarch64

Author: Simon Tatham (statham-arm)

<details>
<summary>Changes</summary>

@<!-- -->llvm.fmuladd is the IR intrinsic that leaves it up to code generation whether to fuse an FP multiply+add pair or leave them separate. Generally you only fuse them if fused mul+add has good performance.

On AArch64, for the float and double instances of this intrinsic, isel was unconditionally fusing the operations. This is sensible with hardware FP, but a bad idea for the rare case of AArch64 without hardware FP, because that leads to a call to the libm `fma()` or `fmaf()` function. That function generally (in multiple libcs) seems to be much slower than separate mul+add operations. So this patch checks for the presence of FP before reporting that fusing the operations is a performance win.

---
Full diff: https://github.com/llvm/llvm-project/pull/205356.diff


2 Files Affected:

- (modified) llvm/lib/Target/AArch64/AArch64ISelLowering.cpp (+2-2) 
- (added) llvm/test/CodeGen/AArch64/aarch64-fmuladd.ll (+47) 


``````````diff
diff --git a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
index e56a9be69dd7c..c36217c176a65 100644
--- a/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
+++ b/llvm/lib/Target/AArch64/AArch64ISelLowering.cpp
@@ -19719,7 +19719,7 @@ bool AArch64TargetLowering::isFMAFasterThanFMulAndFAdd(
     return Subtarget->hasFullFP16();
   case MVT::f32:
   case MVT::f64:
-    return true;
+    return Subtarget->hasFPARMv8();
   case MVT::bf16:
     return VT.isScalableVector() && Subtarget->hasBF16() &&
            Subtarget->isNonStreamingSVEorSME2Available();
@@ -19735,7 +19735,7 @@ bool AArch64TargetLowering::isFMAFasterThanFMulAndFAdd(const Function &F,
   switch (Ty->getScalarType()->getTypeID()) {
   case Type::FloatTyID:
   case Type::DoubleTyID:
-    return true;
+    return Subtarget->hasFPARMv8();
   default:
     return false;
   }
diff --git a/llvm/test/CodeGen/AArch64/aarch64-fmuladd.ll b/llvm/test/CodeGen/AArch64/aarch64-fmuladd.ll
new file mode 100644
index 0000000000000..ff243cb05feac
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/aarch64-fmuladd.ll
@@ -0,0 +1,47 @@
+; RUN: llc -mtriple=aarch64                      -global-isel=0 %s -o - | FileCheck %s --check-prefix=CHECK,FP
+; RUN: llc -mtriple=aarch64                  -O0 -global-isel=1 %s -o - | FileCheck %s --check-prefix=CHECK,FP
+; RUN: llc -mtriple=aarch64 -mattr=-fp-armv8     -global-isel=0 %s -o - | FileCheck %s --check-prefix=CHECK,NOFP
+; RUN: llc -mtriple=aarch64 -mattr=-fp-armv8 -O0 -global-isel=1 %s -o - | FileCheck %s --check-prefix=CHECK,NOFP
+
+; Test that @llvm.fmuladd, with the semantics "multiply and add can be
+; fused or separate at the compiler's option", fuses them in the
+; normal case of hardware FP, but doesn't fuse them in AArch64 softfp,
+; on the theory that the libc fma function is likely slower than the
+; combination.
+;
+; However, @llvm.fma must fuse them regardless, so that _should_ emit
+; a call to fma in softfp modes.
+
+define float @whichever_float(float %0, float %1, float %2) {
+  %result = call float @llvm.fmuladd.f32(float %0, float %1, float %2)
+  ret float %result
+; CHECK: {{^}}whichever_float:
+; FP: fmadd s0, s0, s1, s2
+; NOFP: bl __mulsf3
+; NOFP: bl __addsf3
+}
+
+define double @whichever_double(double %0, double %1, double %2) {
+  %result = call double @llvm.fmuladd.f64(double %0, double %1, double %2)
+  ret double %result
+; CHECK: {{^}}whichever_double:
+; FP: fmadd d0, d0, d1, d2
+; NOFP: bl __muldf3
+; NOFP: bl __adddf3
+}
+
+define float @force_fma_float(float %0, float %1, float %2) {
+  %result = call float @llvm.fma.f32(float %0, float %1, float %2)
+  ret float %result
+; CHECK: {{^}}force_fma_float:
+; FP: fmadd s0, s0, s1, s2
+; NOFP: bl fmaf
+}
+
+define double @force_fma_double(double %0, double %1, double %2) {
+  %result = call double @llvm.fma.f64(double %0, double %1, double %2)
+  ret double %result
+; CHECK: {{^}}force_fma_double:
+; FP: fmadd d0, d0, d1, d2
+; NOFP: bl fma
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/205356


More information about the llvm-commits mailing list