Skip to content

Commit 49eaf3d

Browse files
committed
Address review comments
* Add getSmallConstantMaxTripCount function to PredicatedScalarEvolution, and call this function instead so that we ensure the predicates are added.
1 parent 3cf65dd commit 49eaf3d

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

llvm/include/llvm/Analysis/ScalarEvolution.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2381,6 +2381,10 @@ class PredicatedScalarEvolution {
23812381
/// Get the (predicated) symbolic max backedge count for the analyzed loop.
23822382
const SCEV *getSymbolicMaxBackedgeTakenCount();
23832383

2384+
/// Returns the upper bound of the loop trip count as a normal unsigned
2385+
/// value, or 0 if the trip count is unknown.
2386+
unsigned getSmallConstantMaxTripCount();
2387+
23842388
/// Adds a new predicate.
23852389
void addPredicate(const SCEVPredicate &Pred);
23862390

@@ -2452,6 +2456,9 @@ class PredicatedScalarEvolution {
24522456

24532457
/// The symbolic backedge taken count.
24542458
const SCEV *SymbolicMaxBackedgeCount = nullptr;
2459+
2460+
/// The constant max trip count for the loop.
2461+
std::optional<unsigned> SmallConstantMaxTripCount;
24552462
};
24562463

24572464
template <> struct DenseMapInfo<ScalarEvolution::FoldID> {

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15025,6 +15025,16 @@ const SCEV *PredicatedScalarEvolution::getSymbolicMaxBackedgeTakenCount() {
1502515025
return SymbolicMaxBackedgeCount;
1502615026
}
1502715027

15028+
unsigned PredicatedScalarEvolution::getSmallConstantMaxTripCount() {
15029+
if (!SmallConstantMaxTripCount) {
15030+
SmallVector<const SCEVPredicate *, 4> Preds;
15031+
SmallConstantMaxTripCount = SE.getSmallConstantMaxTripCount(&L, &Preds);
15032+
for (const auto *P : Preds)
15033+
addPredicate(*P);
15034+
}
15035+
return *SmallConstantMaxTripCount;
15036+
}
15037+
1502815038
void PredicatedScalarEvolution::addPredicate(const SCEVPredicate &Pred) {
1502915039
if (Preds->implies(&Pred))
1503015040
return;

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,10 @@ static bool hasIrregularType(Type *Ty, const DataLayout &DL) {
410410
/// 2) Returns expected trip count according to profile data if any.
411411
/// 3) Returns upper bound estimate if it is known.
412412
/// 4) Returns std::nullopt if all of the above failed.
413-
static std::optional<unsigned> getSmallBestKnownTC(ScalarEvolution &SE,
414-
Loop *L) {
413+
static std::optional<unsigned>
414+
getSmallBestKnownTC(PredicatedScalarEvolution &PSE, Loop *L) {
415415
// Check if exact trip count is known.
416-
if (unsigned ExpectedTC = SE.getSmallConstantTripCount(L))
416+
if (unsigned ExpectedTC = PSE.getSE()->getSmallConstantTripCount(L))
417417
return ExpectedTC;
418418

419419
// Check if there is an expected trip count available from profile data.
@@ -422,8 +422,7 @@ static std::optional<unsigned> getSmallBestKnownTC(ScalarEvolution &SE,
422422
return *EstimatedTC;
423423

424424
// Check if upper bound estimate is known.
425-
SmallVector<const SCEVPredicate *, 2> Predicates;
426-
if (unsigned ExpectedTC = SE.getSmallConstantMaxTripCount(L, &Predicates))
425+
if (unsigned ExpectedTC = PSE.getSmallConstantMaxTripCount())
427426
return ExpectedTC;
428427

429428
return std::nullopt;
@@ -2292,9 +2291,7 @@ static bool isIndvarOverflowCheckKnownFalse(
22922291
// We know the runtime overflow check is known false iff the (max) trip-count
22932292
// is known and (max) trip-count + (VF * UF) does not overflow in the type of
22942293
// the vector loop induction variable.
2295-
SmallVector<const SCEVPredicate *, 2> Predicates;
2296-
if (unsigned TC = Cost->PSE.getSE()->getSmallConstantMaxTripCount(
2297-
Cost->TheLoop, &Predicates)) {
2294+
if (unsigned TC = Cost->PSE.getSmallConstantMaxTripCount()) {
22982295
uint64_t MaxVF = VF.getKnownMinValue();
22992296
if (VF.isScalable()) {
23002297
std::optional<unsigned> MaxVScale =
@@ -3989,10 +3986,7 @@ LoopVectorizationCostModel::computeMaxVF(ElementCount UserVF, unsigned UserIC) {
39893986
}
39903987

39913988
unsigned TC = PSE.getSE()->getSmallConstantTripCount(TheLoop);
3992-
3993-
SmallVector<const SCEVPredicate *, 2> Predicates;
3994-
unsigned MaxTC =
3995-
PSE.getSE()->getSmallConstantMaxTripCount(TheLoop, &Predicates);
3989+
unsigned MaxTC = PSE.getSmallConstantMaxTripCount();
39963990
LLVM_DEBUG(dbgs() << "LV: Found trip count: " << TC << '\n');
39973991
if (TC != MaxTC)
39983992
LLVM_DEBUG(dbgs() << "LV: Found maximum trip count: " << MaxTC << '\n');
@@ -4283,9 +4277,7 @@ bool LoopVectorizationPlanner::isMoreProfitable(
42834277
InstructionCost CostA = A.Cost;
42844278
InstructionCost CostB = B.Cost;
42854279

4286-
SmallVector<const SCEVPredicate *, 2> Predicates;
4287-
unsigned MaxTripCount =
4288-
PSE.getSE()->getSmallConstantMaxTripCount(OrigLoop, &Predicates);
4280+
unsigned MaxTripCount = PSE.getSmallConstantMaxTripCount();
42894281

42904282
// Improve estimate for the vector width if it is scalable.
42914283
unsigned EstimatedWidthA = A.Width.getKnownMinValue();
@@ -4873,7 +4865,7 @@ LoopVectorizationCostModel::selectInterleaveCount(ElementCount VF,
48734865
if (!Legal->isSafeForAnyVectorWidth())
48744866
return 1;
48754867

4876-
auto BestKnownTC = getSmallBestKnownTC(*PSE.getSE(), TheLoop);
4868+
auto BestKnownTC = getSmallBestKnownTC(PSE, TheLoop);
48774869
const bool HasReductions = !Legal->getReductionVars().empty();
48784870

48794871
// If we did not calculate the cost for VF (because the user selected the VF)
@@ -9643,7 +9635,7 @@ static void checkMixedPrecision(Loop *L, OptimizationRemarkEmitter *ORE) {
96439635
static bool areRuntimeChecksProfitable(GeneratedRTChecks &Checks,
96449636
VectorizationFactor &VF,
96459637
std::optional<unsigned> VScale, Loop *L,
9646-
ScalarEvolution &SE,
9638+
PredicatedScalarEvolution &PSE,
96479639
ScalarEpilogueLowering SEL) {
96489640
InstructionCost CheckCost = Checks.getCost();
96499641
if (!CheckCost.isValid())
@@ -9728,7 +9720,7 @@ static bool areRuntimeChecksProfitable(GeneratedRTChecks &Checks,
97289720

97299721
// Skip vectorization if the expected trip count is less than the minimum
97309722
// required trip count.
9731-
if (auto ExpectedTC = getSmallBestKnownTC(SE, L)) {
9723+
if (auto ExpectedTC = getSmallBestKnownTC(PSE, L)) {
97329724
if (ElementCount::isKnownLT(ElementCount::getFixed(*ExpectedTC),
97339725
VF.MinProfitableTripCount)) {
97349726
LLVM_DEBUG(dbgs() << "LV: Vectorization is not beneficial: expected "
@@ -9835,7 +9827,7 @@ bool LoopVectorizePass::processLoop(Loop *L) {
98359827

98369828
// Check the loop for a trip count threshold: vectorize loops with a tiny trip
98379829
// count by optimizing for size, to minimize overheads.
9838-
auto ExpectedTC = getSmallBestKnownTC(*SE, L);
9830+
auto ExpectedTC = getSmallBestKnownTC(PSE, L);
98399831
if (ExpectedTC && *ExpectedTC < TinyTripCountVectorThreshold) {
98409832
LLVM_DEBUG(dbgs() << "LV: Found a loop with a very small trip count. "
98419833
<< "This loop is worth vectorizing only if no scalar "
@@ -9950,7 +9942,7 @@ bool LoopVectorizePass::processLoop(Loop *L) {
99509942
Hints.getForce() == LoopVectorizeHints::FK_Enabled;
99519943
if (!ForceVectorization &&
99529944
!areRuntimeChecksProfitable(Checks, VF, getVScaleForTuning(L, *TTI), L,
9953-
*PSE.getSE(), SEL)) {
9945+
PSE, SEL)) {
99549946
ORE->emit([&]() {
99559947
return OptimizationRemarkAnalysisAliasing(
99569948
DEBUG_TYPE, "CantReorderMemOps", L->getStartLoc(),

0 commit comments

Comments
 (0)