[llvm] [Reassociate]Reject fma pairs with a multi-use fmul addend (PR #220640)
Alexey Bataev via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 2 09:20:30 PDT 2026
https://github.com/alexey-bataev created https://github.com/llvm/llvm-project/pull/220640
Keeping such pairs frozen hid shared partial sums from reassociation.
Fixes the vbor regression from #215873 left over after #218675.
Assisted-by: Cursor
>From da07688b9e5044fbd9825255bd9f933da03f966a Mon Sep 17 00:00:00 2001
From: Alexey Bataev <a.bataev at outlook.com>
Date: Wed, 2 Sep 2026 09:20:17 -0700
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
=?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Created using spr 1.3.7
---
llvm/lib/Transforms/Scalar/Reassociate.cpp | 9 ++++---
llvm/test/Transforms/Reassociate/fma-pairs.ll | 26 +++++++++++++++++++
2 files changed, 31 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/Reassociate.cpp b/llvm/lib/Transforms/Scalar/Reassociate.cpp
index 59c29ebeb21ee..5e5b0a8ae1d8f 100644
--- a/llvm/lib/Transforms/Scalar/Reassociate.cpp
+++ b/llvm/lib/Transforms/Scalar/Reassociate.cpp
@@ -194,13 +194,14 @@ static BinaryOperator *isFMulAddCandidate(Value *V) {
return m_CombineAnd(m_AllowContract(m_OneUse(m_FMul(m_Value(), m_Value()))),
m_BinOp(FMul));
};
- BinaryOperator *Mul = nullptr, *OtherMul = nullptr;
+ BinaryOperator *Mul = nullptr;
Value *OtherOp = nullptr;
- // Keep constants and nested additions visible to the enclosing expression so
- // they can participate in folding and reassociation.
+ // Keep constants, nested additions and other contractible multiplies visible
+ // to the enclosing expression so they can participate in folding,
+ // reassociation and factorization.
if (!match(FAdd, m_c_FAdd(ContractableFMul(Mul), m_Value(OtherOp))) ||
isa<Constant>(OtherOp) || isReassociableOp(OtherOp, Instruction::FAdd) ||
- match(OtherOp, ContractableFMul(OtherMul)))
+ match(OtherOp, m_AllowContract(m_FMul(m_Value(), m_Value()))))
return nullptr;
return Mul;
}
diff --git a/llvm/test/Transforms/Reassociate/fma-pairs.ll b/llvm/test/Transforms/Reassociate/fma-pairs.ll
index e3e330ebce039..f7c9c9b86e730 100644
--- a/llvm/test/Transforms/Reassociate/fma-pairs.ll
+++ b/llvm/test/Transforms/Reassociate/fma-pairs.ll
@@ -454,3 +454,29 @@ define double @nested_fma_subtraction(double %acc, double %a, double %b, double
ret double %result
}
+
+define float @multi_use_fmul_addend(float %a, float %c, float %d, float %e, float %f) {
+; CHECK-LABEL: define float @multi_use_fmul_addend(
+; CHECK-SAME: float [[A:%.*]], float [[C:%.*]], float [[D:%.*]], float [[E:%.*]], float [[F:%.*]]) {
+; CHECK-NEXT: [[AC:%.*]] = fmul fast float [[C]], [[A]]
+; CHECK-NEXT: [[AD:%.*]] = fmul fast float [[D]], [[A]]
+; CHECK-NEXT: [[AE:%.*]] = fmul fast float [[E]], [[A]]
+; CHECK-NEXT: [[E_SUM:%.*]] = fadd fast float [[AD]], [[AC]]
+; CHECK-NEXT: [[T1:%.*]] = fmul fast float [[E_SUM]], [[E]]
+; CHECK-NEXT: [[F_SUM0:%.*]] = fadd fast float [[AD]], [[AC]]
+; CHECK-NEXT: [[F_SUM:%.*]] = fadd fast float [[F_SUM0]], [[AE]]
+; CHECK-NEXT: [[T2:%.*]] = fmul fast float [[F_SUM]], [[F]]
+; CHECK-NEXT: [[R:%.*]] = fadd fast float [[T2]], [[T1]]
+; CHECK-NEXT: ret float [[R]]
+;
+ %ac = fmul fast float %a, %c
+ %ad = fmul fast float %a, %d
+ %ae = fmul fast float %a, %e
+ %e.sum = fadd fast float %ad, %ac
+ %t1 = fmul fast float %e.sum, %e
+ %f.sum0 = fadd fast float %ae, %ad
+ %f.sum = fadd fast float %f.sum0, %ac
+ %t2 = fmul fast float %f.sum, %f
+ %r = fadd fast float %t1, %t2
+ ret float %r
+}
More information about the llvm-commits
mailing list