[SLP] Regression from SLP forcing chain of scalar fadds into a vectorized load + reduce add
Noticed this regression on the bpi-f3 (rva22u64_v): https://cc-perf.igalia.com/db_default/v4/nts/profile/291/529/12
I believe this behaviour is caused by this commit: #198819
It seems that SLP found a chain of fadds here and is vectorizing them in order to use the reduction add intrinsic. Looking at the tests added by the commit mentioned above, the intended use case seems to be when the inputs are already in a vector and the fadd chain is fed by extractelement instructions. The code here was not in a vector, resulting in the code having to build upa vector with multiple insertelement.
Before SLP:
for.body8.i: ; preds = %for.body8.i, %for.cond5.preheader.i
%18 = phi double [ %.pre117.i, %for.cond5.preheader.i ], [ %26, %for.body8.i ]
%19 = phi double [ %.pre116.i, %for.cond5.preheader.i ], [ %18, %for.body8.i ]
%20 = phi double [ %.pre115.i, %for.cond5.preheader.i ], [ %25, %for.body8.i ]
%21 = phi double [ %.pre114.i, %for.cond5.preheader.i ], [ %div.i40, %for.body8.i ]
%22 = phi double [ %.pre113.i, %for.cond5.preheader.i ], [ %24, %for.body8.i ]
%23 = phi double [ %.pre.i, %for.cond5.preheader.i ], [ %22, %for.body8.i ]
%indvars.iv.i38 = phi i64 [ 1, %for.cond5.preheader.i ], [ %indvars.iv.next.i39, %for.body8.i ]
%add.i = fadd double %22, %23
%indvars.iv.next.i39 = add nuw nsw i64 %indvars.iv.i38, 1
%arrayidx23.i = getelementptr inbounds nuw [8 x i8], ptr %arrayidx.i37, i64 %indvars.iv.next.i39
%24 = load double, ptr %arrayidx23.i, align 8, !tbaa !14
%add24.i = fadd double %add.i, %24
%add30.i = fadd double %21, %add24.i
%arrayidx34.i = getelementptr inbounds nuw [8 x i8], ptr %17, i64 %indvars.iv.i38
%add35.i = fadd double %20, %add30.i
%arrayidx40.i = getelementptr inbounds nuw [8 x i8], ptr %17, i64 %indvars.iv.next.i39
%25 = load double, ptr %arrayidx40.i, align 8, !tbaa !14
%add41.i = fadd double %25, %add35.i
%add48.i = fadd double %19, %add41.i
%add54.i = fadd double %18, %add48.i
%arrayidx60.i = getelementptr inbounds nuw [8 x i8], ptr %arrayidx44.i, i64 %indvars.iv.next.i39
%26 = load double, ptr %arrayidx60.i, align 8, !tbaa !14
%add61.i = fadd double %26, %add54.i
%div.i40 = fdiv double %add61.i, 9.000000e+00
store double %div.i40, ptr %arrayidx34.i, align 8, !tbaa !14
%exitcond.not.i41 = icmp eq i64 %indvars.iv.next.i39, 1999
br i1 %exitcond.not.i41, label %for.inc66.i, label %for.body8.i, !llvm.loop !25
After SLP:
for.body8.i: ; preds = %for.body8.i, %for.cond5.preheader.i
%18 = phi double [ %.pre117.i, %for.cond5.preheader.i ], [ %26, %for.body8.i ]
%19 = phi double [ %.pre116.i, %for.cond5.preheader.i ], [ %18, %for.body8.i ]
%20 = phi double [ %.pre115.i, %for.cond5.preheader.i ], [ %25, %for.body8.i ]
%21 = phi double [ %.pre114.i, %for.cond5.preheader.i ], [ %div.i40, %for.body8.i ]
%22 = phi double [ %.pre113.i, %for.cond5.preheader.i ], [ %24, %for.body8.i ]
%23 = phi double [ %.pre.i, %for.cond5.preheader.i ], [ %22, %for.body8.i ]
%indvars.iv.i38 = phi i64 [ 1, %for.cond5.preheader.i ], [ %indvars.iv.next.i39, %for.body8.i ]
%add.i = fadd double %22, %23
%indvars.iv.next.i39 = add nuw nsw i64 %indvars.iv.i38, 1
%arrayidx23.i = getelementptr inbounds nuw [8 x i8], ptr %arrayidx.i37, i64 %indvars.iv.next.i39
%24 = load double, ptr %arrayidx23.i, align 8, !tbaa !14
%arrayidx34.i = getelementptr inbounds nuw [8 x i8], ptr %17, i64 %indvars.iv.i38
%arrayidx40.i = getelementptr inbounds nuw [8 x i8], ptr %17, i64 %indvars.iv.next.i39
%25 = load double, ptr %arrayidx40.i, align 8, !tbaa !14
%arrayidx60.i = getelementptr inbounds nuw [8 x i8], ptr %arrayidx44.i, i64 %indvars.iv.next.i39
%26 = load double, ptr %arrayidx60.i, align 8, !tbaa !14
%27 = insertelement <8 x double> poison, double %24, i32 0
%28 = insertelement <8 x double> %27, double %add.i, i32 1
%29 = insertelement <8 x double> %28, double %21, i32 2
%30 = insertelement <8 x double> %29, double %20, i32 3
%31 = insertelement <8 x double> %30, double %25, i32 4
%32 = insertelement <8 x double> %31, double %19, i32 5
%33 = insertelement <8 x double> %32, double %18, i32 6
%34 = insertelement <8 x double> %33, double %26, i32 7
%35 = call double @llvm.vector.reduce.fadd.v8f64(double -0.000000e+00, <8 x double> %34)
%div.i40 = fdiv double %35, 9.000000e+00
store double %div.i40, ptr %arrayidx34.i, align 8, !tbaa !14
%exitcond.not.i41 = icmp eq i64 %indvars.iv.next.i39, 1999
br i1 %exitcond.not.i41, label %for.inc66.i, label %for.body8.i, !llvm.loop !25
Link for godbolt: https://godbolt.org/z/qM6snqPzx
cc @alexey-bataev
I noticed after printing the elements in Candidates from
SmallVector<Value *> Candidates(ReducedVals.back());
that the candidates are mainly PHI instructions. Perhaps a viable solution would be to bail out early if any of the candidate is a phi instruction? I have tried this locally with success and no failing tests, would appreciate thoughts.
Costmodel issue for ordered reduction for this particular target, need to tweak TTI
If you're not in a hurry to wrap this up, would it be alright if I try taking a stab at this?
I think this may not be an issue from TTI. Looking at this snippet of the log.
SLP: Adding cost 15 for bundle Idx: 0, n=8 [ %24 = load double, ptr %arrayidx23.i, align 8, !tbaa !16, ..].
SLP: Current total cost = 15
SLP: Adding cost -4 for reduction of n=8 [ %24 = load double, ptr %arrayidx23.i, align 8, !tbaa !16, ..] (It is a splitting reduction)
Scale 1996002000 For entry 0
SLP: Spill Cost = 0.
SLP: Extract Cost = 0.
SLP: Reduction Cost = -7984008000.
SLP: Total Cost = -7984007985.
SLP: Found cost = -7984007985 for ordered reduction
SLP: Vectorizing ordered reduction at cost:-7984007985. (HorRdx)
It seems that the cost 15 here isn't also scaled up, but the reduction cost is. I traced the cause of this log to this code:
if (!CostIsFree && !Scale) {
Scale = IsGatherLike ? getGatherNodeEffectiveScale(TE)
: getScaleToLoopIterations(TE);
C *= Scale;
EntryToScale.try_emplace(&TE, Scale);
if (!TE.isGather() && TE.hasState()) {
PrevVecParent = TE.getMainOp()->getParent();
PrevScale = Scale;
}
}
Cost += C;
NodesCosts.try_emplace(&TE, C);
LLVM_DEBUG(dbgs() << "SLP: Adding cost " << C << " for bundle "
<< shortBundleName(TE.Scalars, TE.Idx) << ".\n"
<< "SLP: Current total cost = " << Cost << "\n");
It sems getGatherNodeEffectiveScale, which calls getScaleToLoopIterations returns 1. From extra debugs I add, EI.UserTE in getScaleToLoopIterations is null, causing if (!Parent) to run and returning 1. Perhaps if EI.UserTE is null, we can try setting the Parent from TE.Scalars?
I think this may not be an issue from TTI. Looking at this snippet of the log.
SLP: Adding cost 15 for bundle Idx: 0, n=8 [ %24 = load double, ptr %arrayidx23.i, align 8, !tbaa !16, ..]. SLP: Current total cost = 15 SLP: Adding cost -4 for reduction of n=8 [ %24 = load double, ptr %arrayidx23.i, align 8, !tbaa !16, ..] (It is a splitting reduction) Scale 1996002000 For entry 0 SLP: Spill Cost = 0. SLP: Extract Cost = 0. SLP: Reduction Cost = -7984008000. SLP: Total Cost = -7984007985. SLP: Found cost = -7984007985 for ordered reduction SLP: Vectorizing ordered reduction at cost:-7984007985. (HorRdx)It seems that the cost 15 here isn't also scaled up, but the reduction cost is. I traced the cause of this log to this code:
if (!CostIsFree && !Scale) { Scale = IsGatherLike ? getGatherNodeEffectiveScale(TE) : getScaleToLoopIterations(TE); C *= Scale; EntryToScale.try_emplace(&TE, Scale); if (!TE.isGather() && TE.hasState()) { PrevVecParent = TE.getMainOp()->getParent(); PrevScale = Scale; } } Cost += C; NodesCosts.try_emplace(&TE, C); LLVM_DEBUG(dbgs() << "SLP: Adding cost " << C << " for bundle " << shortBundleName(TE.Scalars, TE.Idx) << ".\n" << "SLP: Current total cost = " << Cost << "\n");It sems
getGatherNodeEffectiveScale, which callsgetScaleToLoopIterationsreturns 1. From extra debugs I add,EI.UserTEingetScaleToLoopIterationsis null, causingif (!Parent)to run and returning 1. Perhaps ifEI.UserTEis null, we can try setting theParentfromTE.Scalars?
I think in this case, Parent should be deduced from the reduction root parent
Is something like below inline with what you were thinking? We can scale the cost of TreeCost similar to how the ReductionCost does it in getTreeCost.
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 6bd595c86d55..506fe9c893ee 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -19228,6 +19228,11 @@ InstructionCost BoUpSLP::getTreeCost(InstructionCost TreeCost,
Instruction *ReductionRoot = RdxRoot;
if (UserIgnoreList) {
// Scale reduction cost to the factor of the loop nest trip count.
+ const Loop *L = LI->getLoopFor(ReductionRoot->getParent());
+ if (TreeCost > 0 && L &&
+ !isa<SCEVCouldNotCompute>(SE->getBackedgeTakenCount(L)))
+ Cost = ScaleCost(TreeCost, *VectorizableTree.front().get(),
+ /*Scalar=*/nullptr, ReductionRoot);
ReductionCost = ScaleCost(ReductionCost, *VectorizableTree.front().get(),
/*Scalar=*/nullptr, ReductionRoot);
Is something like below inline with what you were thinking? We can scale the cost of TreeCost similar to how the ReductionCost does it in
getTreeCost.diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp index 6bd595c86d55..506fe9c893ee 100644 --- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp +++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp @@ -19228,6 +19228,11 @@ InstructionCost BoUpSLP::getTreeCost(InstructionCost TreeCost, Instruction *ReductionRoot = RdxRoot; if (UserIgnoreList) { // Scale reduction cost to the factor of the loop nest trip count.
- const Loop *L = LI->getLoopFor(ReductionRoot->getParent());
- if (TreeCost > 0 && L &&
!isa<SCEVCouldNotCompute>(SE->getBackedgeTakenCount(L)))Cost = ScaleCost(TreeCost, *VectorizableTree.front().get(), ReductionCost = ScaleCost(ReductionCost, *VectorizableTree.front().get(), /Scalar=/nullptr, ReductionRoot);/*Scalar=*/nullptr, ReductionRoot);
No, need to pass ReductionRoot as user to getGatherNodeEffectiveScale and then to getScaleToLoopIterations
Do you mean to call getGatherNodeEffectiveScale in getTreeCost since calculateTreeCostAndTrimNonProfitable does not have access to ReductionRoot?
Do you mean to call getGatherNodeEffectiveScale in
getTreeCostsince calculateTreeCostAndTrimNonProfitable does not have access to ReductionRoot?
Need to extend calculateTreeCostAndTrimNonProfitable and pass ReductionRoot