[llvm] [AArch64] Don't call fma/fmaf for @llvm.fmuladd.f64/f32 in softfp (PR #205356)
Simon Tatham via llvm-commits
llvm-commits at lists.llvm.org
Tue Jun 23 07:30:18 PDT 2026
https://github.com/statham-arm created https://github.com/llvm/llvm-project/pull/205356
@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.
>From f94ec52b1f3260596019b4dddc58b950b2e51fee Mon Sep 17 00:00:00 2001
From: Simon Tatham <simon.tatham at arm.com>
Date: Tue, 23 Jun 2026 15:24:01 +0100
Subject: [PATCH] [AArch64] Don't call fma/fmaf for @llvm.fmuladd.f64/f32
@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.
---
.../Target/AArch64/AArch64ISelLowering.cpp | 4 +-
llvm/test/CodeGen/AArch64/aarch64-fmuladd.ll | 47 +++++++++++++++++++
2 files changed, 49 insertions(+), 2 deletions(-)
create mode 100644 llvm/test/CodeGen/AArch64/aarch64-fmuladd.ll
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
+}
More information about the llvm-commits
mailing list