Skip to content

Commit

Permalink
[SLP][NFC]Do not try to revectorize instructions with constant operan…
Browse files Browse the repository at this point in the history
…ds, 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.
  • Loading branch information
alexey-bataev committed May 4, 2023
1 parent ed443d8 commit d726f99
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12895,7 +12895,7 @@ class HorizontalReduction {
HorizontalReduction() = default;

/// Try to find a reduction tree.
bool matchAssociativeReduction(Instruction *Root,
bool matchAssociativeReduction(BoUpSLP &R, Instruction *Root,
ScalarEvolution &SE, const DataLayout &DL,
const TargetLibraryInfo &TLI) {
RdxKind = HorizontalReduction::getRdxKind(Root);
Expand Down Expand Up @@ -12924,11 +12924,10 @@ class HorizontalReduction {
// Checks if the operands of the \p TreeN instruction are also reduction
// operations or should be treated as reduced values or an extra argument,
// which is not part of the reduction.
auto &&CheckOperands = [this, IsCmpSelMinMax,
BB](Instruction *TreeN,
SmallVectorImpl<Value *> &ExtraArgs,
SmallVectorImpl<Value *> &PossibleReducedVals,
SmallVectorImpl<Instruction *> &ReductionOps) {
auto CheckOperands = [&](Instruction *TreeN,
SmallVectorImpl<Value *> &ExtraArgs,
SmallVectorImpl<Value *> &PossibleReducedVals,
SmallVectorImpl<Instruction *> &ReductionOps) {
for (int I = getFirstOperandIndex(TreeN),
End = getNumberOfOperands(TreeN);
I < End; ++I) {
Expand All @@ -12943,10 +12942,14 @@ class HorizontalReduction {
}
// If the edge is not an instruction, or it is different from the main
// reduction opcode or has too many uses - possible reduced value.
// Also, do not try to reduce const values, if the operation is not
// foldable.
if (!EdgeInst || getRdxKind(EdgeInst) != RdxKind ||
IsCmpSelMinMax != isCmpSelMinMax(EdgeInst) ||
!hasRequiredNumberOfUses(IsCmpSelMinMax, EdgeInst) ||
!isVectorizable(RdxKind, EdgeInst)) {
!isVectorizable(RdxKind, EdgeInst) ||
(R.isAnalyzedReductionRoot(EdgeInst) &&
all_of(EdgeInst->operands(), Constant::classof))) {
PossibleReducedVals.push_back(EdgeVal);
continue;
}
Expand Down Expand Up @@ -13230,9 +13233,11 @@ class HorizontalReduction {
allConstant(Candidates)) {
Value *Res = Candidates.front();
++VectorizedVals.try_emplace(Candidates.front(), 0).first->getSecond();
for (Value *V : ArrayRef(Candidates).drop_front()) {
Res = createOp(Builder, RdxKind, Res, V, "const.rdx", ReductionOps);
++VectorizedVals.try_emplace(V, 0).first->getSecond();
for (Value *VC : ArrayRef(Candidates).drop_front()) {
Res = createOp(Builder, RdxKind, Res, VC, "const.rdx", ReductionOps);
++VectorizedVals.try_emplace(VC, 0).first->getSecond();
if (auto *ResI = dyn_cast<Instruction>(Res))
V.analyzedReductionRoot(ResI);
}
VectorizedTree = GetNewVectorizedTree(VectorizedTree, Res);
continue;
Expand Down Expand Up @@ -14143,7 +14148,7 @@ bool SLPVectorizerPass::vectorizeHorReduction(
if (!isReductionCandidate(Inst))
return nullptr;
HorizontalReduction HorRdx;
if (!HorRdx.matchAssociativeReduction(Inst, *SE, *DL, *TLI))
if (!HorRdx.matchAssociativeReduction(R, Inst, *SE, *DL, *TLI))
return nullptr;
return HorRdx.tryToReduce(R, TTI, *TLI);
};
Expand Down

0 comments on commit d726f99

Please sign in to comment.