Skip to content

Commit d726f99

Browse files
committed
[SLP][NFC]Do not try to revectorize instructions with constant operands, NFC.
The pass should not try to revectorize instructions with constant operands, which were not folded by the IRBuilder. It prevents the non-terminating loop in the SLP vectorizer for non foldable constant operations.
1 parent ed443d8 commit d726f99

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12895,7 +12895,7 @@ class HorizontalReduction {
1289512895
HorizontalReduction() = default;
1289612896

1289712897
/// Try to find a reduction tree.
12898-
bool matchAssociativeReduction(Instruction *Root,
12898+
bool matchAssociativeReduction(BoUpSLP &R, Instruction *Root,
1289912899
ScalarEvolution &SE, const DataLayout &DL,
1290012900
const TargetLibraryInfo &TLI) {
1290112901
RdxKind = HorizontalReduction::getRdxKind(Root);
@@ -12924,11 +12924,10 @@ class HorizontalReduction {
1292412924
// Checks if the operands of the \p TreeN instruction are also reduction
1292512925
// operations or should be treated as reduced values or an extra argument,
1292612926
// which is not part of the reduction.
12927-
auto &&CheckOperands = [this, IsCmpSelMinMax,
12928-
BB](Instruction *TreeN,
12929-
SmallVectorImpl<Value *> &ExtraArgs,
12930-
SmallVectorImpl<Value *> &PossibleReducedVals,
12931-
SmallVectorImpl<Instruction *> &ReductionOps) {
12927+
auto CheckOperands = [&](Instruction *TreeN,
12928+
SmallVectorImpl<Value *> &ExtraArgs,
12929+
SmallVectorImpl<Value *> &PossibleReducedVals,
12930+
SmallVectorImpl<Instruction *> &ReductionOps) {
1293212931
for (int I = getFirstOperandIndex(TreeN),
1293312932
End = getNumberOfOperands(TreeN);
1293412933
I < End; ++I) {
@@ -12943,10 +12942,14 @@ class HorizontalReduction {
1294312942
}
1294412943
// If the edge is not an instruction, or it is different from the main
1294512944
// reduction opcode or has too many uses - possible reduced value.
12945+
// Also, do not try to reduce const values, if the operation is not
12946+
// foldable.
1294612947
if (!EdgeInst || getRdxKind(EdgeInst) != RdxKind ||
1294712948
IsCmpSelMinMax != isCmpSelMinMax(EdgeInst) ||
1294812949
!hasRequiredNumberOfUses(IsCmpSelMinMax, EdgeInst) ||
12949-
!isVectorizable(RdxKind, EdgeInst)) {
12950+
!isVectorizable(RdxKind, EdgeInst) ||
12951+
(R.isAnalyzedReductionRoot(EdgeInst) &&
12952+
all_of(EdgeInst->operands(), Constant::classof))) {
1295012953
PossibleReducedVals.push_back(EdgeVal);
1295112954
continue;
1295212955
}
@@ -13230,9 +13233,11 @@ class HorizontalReduction {
1323013233
allConstant(Candidates)) {
1323113234
Value *Res = Candidates.front();
1323213235
++VectorizedVals.try_emplace(Candidates.front(), 0).first->getSecond();
13233-
for (Value *V : ArrayRef(Candidates).drop_front()) {
13234-
Res = createOp(Builder, RdxKind, Res, V, "const.rdx", ReductionOps);
13235-
++VectorizedVals.try_emplace(V, 0).first->getSecond();
13236+
for (Value *VC : ArrayRef(Candidates).drop_front()) {
13237+
Res = createOp(Builder, RdxKind, Res, VC, "const.rdx", ReductionOps);
13238+
++VectorizedVals.try_emplace(VC, 0).first->getSecond();
13239+
if (auto *ResI = dyn_cast<Instruction>(Res))
13240+
V.analyzedReductionRoot(ResI);
1323613241
}
1323713242
VectorizedTree = GetNewVectorizedTree(VectorizedTree, Res);
1323813243
continue;
@@ -14143,7 +14148,7 @@ bool SLPVectorizerPass::vectorizeHorReduction(
1414314148
if (!isReductionCandidate(Inst))
1414414149
return nullptr;
1414514150
HorizontalReduction HorRdx;
14146-
if (!HorRdx.matchAssociativeReduction(Inst, *SE, *DL, *TLI))
14151+
if (!HorRdx.matchAssociativeReduction(R, Inst, *SE, *DL, *TLI))
1414714152
return nullptr;
1414814153
return HorRdx.tryToReduce(R, TTI, *TLI);
1414914154
};

0 commit comments

Comments
 (0)