Skip to content

Commit 5a20141

Browse files
authored
[LSR] Provide TTI hook to enable dropping solutions deemed to be unprofitable (llvm#89924)
<https://reviews.llvm.org/D126043> introduced a flag to drop solutions if deemed unprofitable. As noted there, introducing a TTI hook enables backends to individually opt into this behaviour. This will be used by llvm#89927.
1 parent 163cb1f commit 5a20141

File tree

5 files changed

+33
-3
lines changed

5 files changed

+33
-3
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,10 @@ class TargetTransformInfo {
740740
/// When successful, makes the primary IV dead.
741741
bool shouldFoldTerminatingConditionAfterLSR() const;
742742

743+
/// Return true if LSR should drop a found solution if it's calculated to be
744+
/// less profitable than the baseline.
745+
bool shouldDropLSRSolutionIfLessProfitable() const;
746+
743747
/// \returns true if LSR should not optimize a chain that includes \p I.
744748
bool isProfitableLSRChainElement(Instruction *I) const;
745749

@@ -1864,6 +1868,7 @@ class TargetTransformInfo::Concept {
18641868
const TargetTransformInfo::LSRCost &C2) = 0;
18651869
virtual bool isNumRegsMajorCostOfLSR() = 0;
18661870
virtual bool shouldFoldTerminatingConditionAfterLSR() const = 0;
1871+
virtual bool shouldDropLSRSolutionIfLessProfitable() const = 0;
18671872
virtual bool isProfitableLSRChainElement(Instruction *I) = 0;
18681873
virtual bool canMacroFuseCmp() = 0;
18691874
virtual bool canSaveCmp(Loop *L, BranchInst **BI, ScalarEvolution *SE,
@@ -2337,6 +2342,9 @@ class TargetTransformInfo::Model final : public TargetTransformInfo::Concept {
23372342
bool shouldFoldTerminatingConditionAfterLSR() const override {
23382343
return Impl.shouldFoldTerminatingConditionAfterLSR();
23392344
}
2345+
bool shouldDropLSRSolutionIfLessProfitable() const override {
2346+
return Impl.shouldDropLSRSolutionIfLessProfitable();
2347+
}
23402348
bool isProfitableLSRChainElement(Instruction *I) override {
23412349
return Impl.isProfitableLSRChainElement(I);
23422350
}

llvm/include/llvm/Analysis/TargetTransformInfoImpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ class TargetTransformInfoImplBase {
241241

242242
bool shouldFoldTerminatingConditionAfterLSR() const { return false; }
243243

244+
bool shouldDropLSRSolutionIfLessProfitable() const { return false; }
245+
244246
bool isProfitableLSRChainElement(Instruction *I) const { return false; }
245247

246248
bool canMacroFuseCmp() const { return false; }

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -399,6 +399,10 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
399399
shouldFoldTerminatingConditionAfterLSR();
400400
}
401401

402+
bool shouldDropLSRSolutionIfLessProfitable() const {
403+
return TargetTransformInfoImplBase::shouldDropLSRSolutionIfLessProfitable();
404+
}
405+
402406
bool isProfitableLSRChainElement(Instruction *I) {
403407
return TargetTransformInfoImplBase::isProfitableLSRChainElement(I);
404408
}

llvm/lib/Analysis/TargetTransformInfo.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,10 @@ bool TargetTransformInfo::shouldFoldTerminatingConditionAfterLSR() const {
427427
return TTIImpl->shouldFoldTerminatingConditionAfterLSR();
428428
}
429429

430+
bool TargetTransformInfo::shouldDropLSRSolutionIfLessProfitable() const {
431+
return TTIImpl->shouldDropLSRSolutionIfLessProfitable();
432+
}
433+
430434
bool TargetTransformInfo::isProfitableLSRChainElement(Instruction *I) const {
431435
return TTIImpl->isProfitableLSRChainElement(I);
432436
}

llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,8 @@ static cl::opt<cl::boolOrDefault> AllowTerminatingConditionFoldingAfterLSR(
193193
"lsr-term-fold", cl::Hidden,
194194
cl::desc("Attempt to replace primary IV with other IV."));
195195

196-
static cl::opt<bool> AllowDropSolutionIfLessProfitable(
197-
"lsr-drop-solution", cl::Hidden, cl::init(false),
196+
static cl::opt<cl::boolOrDefault> AllowDropSolutionIfLessProfitable(
197+
"lsr-drop-solution", cl::Hidden,
198198
cl::desc("Attempt to drop solution if it is less profitable"));
199199

200200
STATISTIC(NumTermFold,
@@ -5250,8 +5250,20 @@ void LSRInstance::Solve(SmallVectorImpl<const Formula *> &Solution) const {
52505250

52515251
assert(Solution.size() == Uses.size() && "Malformed solution!");
52525252

5253+
const bool EnableDropUnprofitableSolution = [&] {
5254+
switch (AllowDropSolutionIfLessProfitable) {
5255+
case cl::BOU_TRUE:
5256+
return true;
5257+
case cl::BOU_FALSE:
5258+
return false;
5259+
case cl::BOU_UNSET:
5260+
return TTI.shouldDropLSRSolutionIfLessProfitable();
5261+
}
5262+
llvm_unreachable("Unhandled cl::boolOrDefault enum");
5263+
}();
5264+
52535265
if (BaselineCost.isLess(SolutionCost)) {
5254-
if (!AllowDropSolutionIfLessProfitable)
5266+
if (!EnableDropUnprofitableSolution)
52555267
LLVM_DEBUG(
52565268
dbgs() << "Baseline is more profitable than chosen solution, "
52575269
"add option 'lsr-drop-solution' to drop LSR solution.\n");

0 commit comments

Comments
 (0)