Skip to content

Commit 1930b81

Browse files
committed
[TTI] Add SCEVExpansionBudget to loop unrolling options. (llvm#118316)
Add an extra know to UnrollingPreferences to let backends control the maximum budget for SCEV expansions. This gives backends more fine-grained control on the cost of the runtime checks for runtime unrolling. PR: llvm#118316 (cherry picked from commit 4226e0a)
1 parent 06a77c5 commit 1930b81

File tree

7 files changed

+21
-10
lines changed

7 files changed

+21
-10
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,9 @@ class TargetTransformInfo {
614614
unsigned MaxIterationsCountToAnalyze;
615615
/// Don't disable runtime unroll for the loops which were vectorized.
616616
bool UnrollVectorizedLoop = false;
617+
/// Don't allow runtime unrolling if expanding the trip count takes more
618+
/// than SCEVExpansionBudget.
619+
unsigned SCEVExpansionBudget;
617620
};
618621

619622
/// Get target-customized preferences for the generic loop unrolling

llvm/include/llvm/Transforms/Utils/UnrollLoop.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ struct UnrollLoopOptions {
7575
bool UnrollRemainder;
7676
bool ForgetAllSCEV;
7777
const Instruction *Heart = nullptr;
78+
unsigned SCEVExpansionBudget;
7879
};
7980

8081
LoopUnrollResult UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
@@ -90,7 +91,7 @@ bool UnrollRuntimeLoopRemainder(
9091
bool UseEpilogRemainder, bool UnrollRemainder, bool ForgetAllSCEV,
9192
LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC,
9293
const TargetTransformInfo *TTI, bool PreserveLCSSA,
93-
Loop **ResultLoop = nullptr);
94+
unsigned SCEVExpansionBudget, Loop **ResultLoop = nullptr);
9495

9596
LoopUnrollResult UnrollAndJamLoop(Loop *L, unsigned Count, unsigned TripCount,
9697
unsigned TripMultiple, bool UnrollRemainder,

llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
#include "llvm/Transforms/Utils/LoopPeel.h"
5959
#include "llvm/Transforms/Utils/LoopSimplify.h"
6060
#include "llvm/Transforms/Utils/LoopUtils.h"
61+
#include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
6162
#include "llvm/Transforms/Utils/SizeOpts.h"
6263
#include "llvm/Transforms/Utils/UnrollLoop.h"
6364
#include <algorithm>
@@ -219,6 +220,7 @@ TargetTransformInfo::UnrollingPreferences llvm::gatherUnrollingPreferences(
219220
UP.UnrollAndJam = false;
220221
UP.UnrollAndJamInnerLoopThreshold = 60;
221222
UP.MaxIterationsCountToAnalyze = UnrollMaxIterationsCountToAnalyze;
223+
UP.SCEVExpansionBudget = SCEVCheapExpansionBudget;
222224

223225
// Override with any target specific settings
224226
TTI.getUnrollingPreferences(L, SE, UP, &ORE);
@@ -1350,6 +1352,7 @@ tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
13501352
ULO.Runtime = UP.Runtime;
13511353
ULO.ForgetAllSCEV = ForgetAllSCEV;
13521354
ULO.Heart = getLoopConvergenceHeart(L);
1355+
ULO.SCEVExpansionBudget = UP.SCEVExpansionBudget;
13531356
LoopUnrollResult UnrollResult = UnrollLoop(
13541357
L, ULO, LI, &SE, &DT, &AC, &TTI, &ORE, PreserveLCSSA, &RemainderLoop, AA);
13551358
if (UnrollResult == LoopUnrollResult::Unmodified)

llvm/lib/Transforms/Utils/LoopUnroll.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
#include "llvm/Transforms/Utils/Local.h"
6464
#include "llvm/Transforms/Utils/LoopSimplify.h"
6565
#include "llvm/Transforms/Utils/LoopUtils.h"
66+
#include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
6667
#include "llvm/Transforms/Utils/SimplifyIndVar.h"
6768
#include "llvm/Transforms/Utils/UnrollLoop.h"
6869
#include "llvm/Transforms/Utils/ValueMapper.h"
@@ -592,10 +593,10 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
592593
: isEpilogProfitable(L);
593594

594595
if (ULO.Runtime &&
595-
!UnrollRuntimeLoopRemainder(L, ULO.Count, ULO.AllowExpensiveTripCount,
596-
EpilogProfitability, ULO.UnrollRemainder,
597-
ULO.ForgetAllSCEV, LI, SE, DT, AC, TTI,
598-
PreserveLCSSA, RemainderLoop)) {
596+
!UnrollRuntimeLoopRemainder(
597+
L, ULO.Count, ULO.AllowExpensiveTripCount, EpilogProfitability,
598+
ULO.UnrollRemainder, ULO.ForgetAllSCEV, LI, SE, DT, AC, TTI,
599+
PreserveLCSSA, ULO.SCEVExpansionBudget, RemainderLoop)) {
599600
if (ULO.Force)
600601
ULO.Runtime = false;
601602
else {

llvm/lib/Transforms/Utils/LoopUnrollAndJam.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
4949
#include "llvm/Transforms/Utils/Cloning.h"
5050
#include "llvm/Transforms/Utils/LoopUtils.h"
51+
#include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
5152
#include "llvm/Transforms/Utils/UnrollLoop.h"
5253
#include "llvm/Transforms/Utils/ValueMapper.h"
5354
#include <assert.h>
@@ -243,7 +244,8 @@ llvm::UnrollAndJamLoop(Loop *L, unsigned Count, unsigned TripCount,
243244
if (!UnrollRuntimeLoopRemainder(L, Count, /*AllowExpensiveTripCount*/ false,
244245
/*UseEpilogRemainder*/ true,
245246
UnrollRemainder, /*ForgetAllSCEV*/ false,
246-
LI, SE, DT, AC, TTI, true, EpilogueLoop)) {
247+
LI, SE, DT, AC, TTI, true,
248+
SCEVCheapExpansionBudget, EpilogueLoop)) {
247249
LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; remainder loop could not be "
248250
"generated when assuming runtime trip count\n");
249251
return LoopUnrollResult::Unmodified;

llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,8 @@ bool llvm::UnrollRuntimeLoopRemainder(
583583
Loop *L, unsigned Count, bool AllowExpensiveTripCount,
584584
bool UseEpilogRemainder, bool UnrollRemainder, bool ForgetAllSCEV,
585585
LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC,
586-
const TargetTransformInfo *TTI, bool PreserveLCSSA, Loop **ResultLoop) {
586+
const TargetTransformInfo *TTI, bool PreserveLCSSA,
587+
unsigned SCEVExpansionBudget, Loop **ResultLoop) {
587588
LLVM_DEBUG(dbgs() << "Trying runtime unrolling on Loop: \n");
588589
LLVM_DEBUG(L->dump());
589590
LLVM_DEBUG(UseEpilogRemainder ? dbgs() << "Using epilog remainder.\n"
@@ -673,8 +674,8 @@ bool llvm::UnrollRuntimeLoopRemainder(
673674
const DataLayout &DL = Header->getDataLayout();
674675
SCEVExpander Expander(*SE, DL, "loop-unroll");
675676
if (!AllowExpensiveTripCount &&
676-
Expander.isHighCostExpansion(TripCountSC, L, SCEVCheapExpansionBudget,
677-
TTI, PreHeaderBR)) {
677+
Expander.isHighCostExpansion(TripCountSC, L, SCEVExpansionBudget, TTI,
678+
PreHeaderBR)) {
678679
LLVM_DEBUG(dbgs() << "High cost for expanding trip count scev!\n");
679680
return false;
680681
}

llvm/unittests/Transforms/Utils/UnrollLoopTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,6 @@ while.end: ; preds = %while.cond
7373

7474
bool ret =
7575
UnrollRuntimeLoopRemainder(L, 4, true, false, false, false, &LI, &SE, &DT,
76-
&AC, /*TTI=*/nullptr, PreserveLCSSA);
76+
&AC, /*TTI=*/nullptr, PreserveLCSSA, 4);
7777
EXPECT_FALSE(ret);
7878
}

0 commit comments

Comments
 (0)