Skip to content

Commit 459661e

Browse files
committed
[LoopVectorize] In LoopVectorize start using getSymbolicMaxBackedgeTakenCount
LoopVectorizationLegality currently only treats a loop as legal to vectorise if PredicatedScalarEvolution::getBackedgeTakenCount returns a valid SCEV, or more precisely that the loop must have an exact backedge taken count. Therefore, in LoopVectorize.cpp we can safely replace all calls to getBackedgeTakenCount with calls to getSymbolicMaxBackedgeTakenCount, since the result is the same. This also helps prepare the loop vectoriser for PR #88385.
1 parent 0df8880 commit 459661e

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4075,7 +4075,12 @@ LoopVectorizationCostModel::computeMaxVF(ElementCount UserVF, unsigned UserIC) {
40754075
unsigned MaxVFtimesIC =
40764076
UserIC ? *MaxPowerOf2RuntimeVF * UserIC : *MaxPowerOf2RuntimeVF;
40774077
ScalarEvolution *SE = PSE.getSE();
4078-
const SCEV *BackedgeTakenCount = PSE.getBackedgeTakenCount();
4078+
// Currently only loops with countable exits are vectorized so it's safe to
4079+
// use getSymbolicMaxBackedgeTakenCount as it should give the same result
4080+
// as getBackedgeTakenCount.
4081+
const SCEV *BackedgeTakenCount = PSE.getSymbolicMaxBackedgeTakenCount();
4082+
assert(!isa<SCEVCouldNotCompute>(BackedgeTakenCount) &&
4083+
"Invalid loop count");
40794084
const SCEV *ExitCount = SE->getAddExpr(
40804085
BackedgeTakenCount, SE->getOne(BackedgeTakenCount->getType()));
40814086
const SCEV *Rem = SE->getURemExpr(

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -880,11 +880,16 @@ VPlanPtr VPlan::createInitialVPlan(Type *InductionTy,
880880
auto Plan = std::make_unique<VPlan>(Entry, VecPreheader);
881881

882882
// Create SCEV and VPValue for the trip count.
883-
const SCEV *BackedgeTakenCount = PSE.getBackedgeTakenCount();
884-
assert(!isa<SCEVCouldNotCompute>(BackedgeTakenCount) && "Invalid loop count");
883+
884+
// Using getSymbolicMaxBackedgeTakenCount instead of getBackedgeTakenCount,
885+
// since they should be identical as we currently only vectorize loops when
886+
// all exits are countable.
887+
const SCEV *BackedgeTakenCountSCEV = PSE.getSymbolicMaxBackedgeTakenCount();
888+
assert(!isa<SCEVCouldNotCompute>(BackedgeTakenCountSCEV) &&
889+
"Invalid loop count");
885890
ScalarEvolution &SE = *PSE.getSE();
886-
const SCEV *TripCount =
887-
SE.getTripCountFromExitCount(BackedgeTakenCount, InductionTy, TheLoop);
891+
const SCEV *TripCount = SE.getTripCountFromExitCount(BackedgeTakenCountSCEV,
892+
InductionTy, TheLoop);
888893
Plan->TripCount =
889894
vputils::getOrCreateVPValueForSCEVExpr(*Plan, TripCount, SE);
890895

0 commit comments

Comments
 (0)