[llvm] [SLP] Partially revert #224931 for alternate nodes (PR #228659)

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 2 22:26:02 PDT 2026


https://github.com/hidekisaito updated https://github.com/llvm/llvm-project/pull/228659

>From 076266fc9704c7efc786d6e020a69cfc6967b809 Mon Sep 17 00:00:00 2001
From: Hideki Saito <hidekido at amd.com>
Date: Sat, 3 Oct 2026 04:37:10 +0000
Subject: [PATCH] [SLP] Partially revert #224931 for alternate nodes

Do not pass scalar context to alternate node vector cost queries.

PR #224931 passes the scalar main/alternate op as the context instruction when
costing the vector ops of an alternate node. Those vector ops only feed the
lane-select shuffle, so use-based discounts derived from the scalar's users
do not apply. On AMDGPU this priced an [fmul | fadd] node's vector fmul as
fused (free) where the sole user of the scalar fmul is fadd/fsub, keeping
unprofitable subtrees vectorized. In a rocFFT kernel on gfx950 this raised
VGPR usage from 160 to 178 and caused a performance regression. Restore the
null context for these queries.
---
 .../Transforms/Vectorize/SLPVectorizer.cpp    |  7 +-
 .../AMDGPU/alt-fmul-fadd-cost.ll              | 76 +++++++++++++++++++
 2 files changed, 79 insertions(+), 4 deletions(-)
 create mode 100644 llvm/test/Transforms/SLPVectorizer/AMDGPU/alt-fmul-fadd-cost.ll

diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 516b4af4c4a528..fcb8a7057c9ad9 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -17908,10 +17908,9 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
           for (unsigned Idx : seq<unsigned>(1, E->getNumOperands())) {
             TTI::OperandValueInfo ColInfo = getOperandInfo(E->getOperand(Idx));
             if (!RunningInfo.isConstant() || !ColInfo.isConstant())
-              Cost += TTIRef.getArithmeticInstrCost(
-                  Opcode, VecTy, CostKind, RunningInfo, ColInfo, {},
-                  Opcode == E->getOpcode() ? E->getMainOp() : E->getAltOp(),
-                  TLI);
+              Cost += TTIRef.getArithmeticInstrCost(Opcode, VecTy, CostKind,
+                                                    RunningInfo, ColInfo, {},
+                                                    /*CxtI=*/nullptr, TLI);
             TTI::OperandValueKind Kind = TTI::OK_AnyValue;
             if (RunningInfo.isConstant() && ColInfo.isConstant())
               Kind = RunningInfo.Kind == TTI::OK_UniformConstantValue &&
diff --git a/llvm/test/Transforms/SLPVectorizer/AMDGPU/alt-fmul-fadd-cost.ll b/llvm/test/Transforms/SLPVectorizer/AMDGPU/alt-fmul-fadd-cost.ll
new file mode 100644
index 00000000000000..fb4d98adf0eafe
--- /dev/null
+++ b/llvm/test/Transforms/SLPVectorizer/AMDGPU/alt-fmul-fadd-cost.ll
@@ -0,0 +1,76 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -mtriple=amdgpu9.50-amd-amdhsa -passes=slp-vectorizer -pass-remarks-output=%t < %s | FileCheck %s
+; RUN: FileCheck --input-file=%t --check-prefix=YAML %s
+
+; An alternate [fmul | fadd] node is emitted as a full vector fmul, a full
+; vector fadd and a lane-select shuffle. The vector fmul only feeds the
+; shuffle, so it cannot fuse into an fma even though the scalar fmul in lane 0
+; feeds an fsub. Its cost must not get the fmul+fadd fusion discount.
+
+; YAML-LABEL: Name:            StoresVectorized
+; YAML-NEXT:  Function:        alt_fmul_fadd{{$}}
+; YAML-NEXT:  Args:
+; YAML-NEXT:    - String:          'Stores SLP vectorized with cost '
+; YAML-NEXT:    - Cost:            '-1'
+
+define void @alt_fmul_fadd(float %a0, float %a1, float %b0, float %b1, float %c0, float %c1, ptr addrspace(1) %out) {
+; CHECK-LABEL: @alt_fmul_fadd(
+; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <2 x float> poison, float [[A0:%.*]], i64 0
+; CHECK-NEXT:    [[TMP2:%.*]] = insertelement <2 x float> [[TMP1]], float [[A1:%.*]], i64 1
+; CHECK-NEXT:    [[TMP3:%.*]] = insertelement <2 x float> poison, float [[B0:%.*]], i64 0
+; CHECK-NEXT:    [[TMP4:%.*]] = insertelement <2 x float> [[TMP3]], float [[B1:%.*]], i64 1
+; CHECK-NEXT:    [[TMP5:%.*]] = fmul contract <2 x float> [[TMP2]], [[TMP4]]
+; CHECK-NEXT:    [[TMP6:%.*]] = fadd contract <2 x float> [[TMP2]], [[TMP4]]
+; CHECK-NEXT:    [[TMP7:%.*]] = shufflevector <2 x float> [[TMP5]], <2 x float> [[TMP6]], <2 x i32> <i32 0, i32 3>
+; CHECK-NEXT:    [[TMP8:%.*]] = insertelement <2 x float> poison, float [[C0:%.*]], i64 0
+; CHECK-NEXT:    [[TMP9:%.*]] = insertelement <2 x float> [[TMP8]], float [[C1:%.*]], i64 1
+; CHECK-NEXT:    [[TMP10:%.*]] = fsub contract <2 x float> [[TMP7]], [[TMP9]]
+; CHECK-NEXT:    store <2 x float> [[TMP10]], ptr addrspace(1) [[OUT:%.*]], align 4
+; CHECK-NEXT:    ret void
+;
+  %m0 = fmul contract float %a0, %b0
+  %s1 = fadd contract float %a1, %b1
+  %r0 = fsub contract float %m0, %c0
+  %r1 = fsub contract float %s1, %c1
+  store float %r0, ptr addrspace(1) %out, align 4
+  %out1 = getelementptr inbounds float, ptr addrspace(1) %out, i64 1
+  store float %r1, ptr addrspace(1) %out1, align 4
+  ret void
+}
+
+; YAML-LABEL: Name:            StoresVectorized
+; YAML-NEXT:  Function:        alt_fmul_fadd_loads{{$}}
+; YAML-NEXT:  Args:
+; YAML-NEXT:    - String:          'Stores SLP vectorized with cost '
+; YAML-NEXT:    - Cost:            '-4'
+
+define void @alt_fmul_fadd_loads(ptr addrspace(1) %pa, ptr addrspace(1) %pb, ptr addrspace(1) %pc, ptr addrspace(1) %out) {
+; CHECK-LABEL: @alt_fmul_fadd_loads(
+; CHECK-NEXT:    [[TMP1:%.*]] = load <2 x float>, ptr addrspace(1) [[PA:%.*]], align 4
+; CHECK-NEXT:    [[TMP2:%.*]] = load <2 x float>, ptr addrspace(1) [[PB:%.*]], align 4
+; CHECK-NEXT:    [[TMP3:%.*]] = load <2 x float>, ptr addrspace(1) [[PC:%.*]], align 4
+; CHECK-NEXT:    [[TMP4:%.*]] = fmul contract <2 x float> [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    [[TMP5:%.*]] = fadd contract <2 x float> [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    [[TMP6:%.*]] = shufflevector <2 x float> [[TMP4]], <2 x float> [[TMP5]], <2 x i32> <i32 0, i32 3>
+; CHECK-NEXT:    [[TMP7:%.*]] = fsub contract <2 x float> [[TMP6]], [[TMP3]]
+; CHECK-NEXT:    store <2 x float> [[TMP7]], ptr addrspace(1) [[OUT:%.*]], align 4
+; CHECK-NEXT:    ret void
+;
+  %a0 = load float, ptr addrspace(1) %pa, align 4
+  %pa1 = getelementptr inbounds float, ptr addrspace(1) %pa, i64 1
+  %a1 = load float, ptr addrspace(1) %pa1, align 4
+  %b0 = load float, ptr addrspace(1) %pb, align 4
+  %pb1 = getelementptr inbounds float, ptr addrspace(1) %pb, i64 1
+  %b1 = load float, ptr addrspace(1) %pb1, align 4
+  %c0 = load float, ptr addrspace(1) %pc, align 4
+  %pc1 = getelementptr inbounds float, ptr addrspace(1) %pc, i64 1
+  %c1 = load float, ptr addrspace(1) %pc1, align 4
+  %m0 = fmul contract float %a0, %b0
+  %s1 = fadd contract float %a1, %b1
+  %r0 = fsub contract float %m0, %c0
+  %r1 = fsub contract float %s1, %c1
+  store float %r0, ptr addrspace(1) %out, align 4
+  %out1 = getelementptr inbounds float, ptr addrspace(1) %out, i64 1
+  store float %r1, ptr addrspace(1) %out1, align 4
+  ret void
+}



More information about the llvm-commits mailing list