Skip to content

Commit 5c7b461

Browse files
committed
[LoopUnroll] Add UnrollMultiExit to loop unrolling options (NFC)
Add an extra knob to UnrollingPreferences to let backends control whether to allow multi-exit unrolling on a per-loop basis. This gives backends more fine-grained control on deciding if multi-exit unrolling is profitable for a given loop and uarch. Similar to 4226e0a.
1 parent 8035d38 commit 5c7b461

File tree

6 files changed

+17
-9
lines changed

6 files changed

+17
-9
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,9 @@ class TargetTransformInfo {
622622
/// Don't allow runtime unrolling if expanding the trip count takes more
623623
/// than SCEVExpansionBudget.
624624
unsigned SCEVExpansionBudget;
625+
/// Allow runtime unrolling multi-exit loops. Should only be set if the
626+
/// target determined that multi-exit unrolling is profitable for the loop.
627+
bool UnrollMultiExit;
625628
};
626629

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

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ struct UnrollLoopOptions {
7676
bool ForgetAllSCEV;
7777
const Instruction *Heart = nullptr;
7878
unsigned SCEVExpansionBudget;
79+
bool UnrollMultiExit = false;
7980
};
8081

8182
LoopUnrollResult UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
@@ -91,7 +92,8 @@ bool UnrollRuntimeLoopRemainder(
9192
bool UseEpilogRemainder, bool UnrollRemainder, bool ForgetAllSCEV,
9293
LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC,
9394
const TargetTransformInfo *TTI, bool PreserveLCSSA,
94-
unsigned SCEVExpansionBudget, Loop **ResultLoop = nullptr);
95+
unsigned SCEVExpansionBudget, bool UnrollMultiExit,
96+
Loop **ResultLoop = nullptr);
9597

9698
LoopUnrollResult UnrollAndJamLoop(Loop *L, unsigned Count, unsigned TripCount,
9799
unsigned TripMultiple, bool UnrollRemainder,

llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ TargetTransformInfo::UnrollingPreferences llvm::gatherUnrollingPreferences(
220220
UP.UnrollAndJamInnerLoopThreshold = 60;
221221
UP.MaxIterationsCountToAnalyze = UnrollMaxIterationsCountToAnalyze;
222222
UP.SCEVExpansionBudget = SCEVCheapExpansionBudget;
223+
UP.UnrollMultiExit = false;
223224

224225
// Override with any target specific settings
225226
TTI.getUnrollingPreferences(L, SE, UP, &ORE);
@@ -1352,6 +1353,7 @@ tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
13521353
ULO.ForgetAllSCEV = ForgetAllSCEV;
13531354
ULO.Heart = getLoopConvergenceHeart(L);
13541355
ULO.SCEVExpansionBudget = UP.SCEVExpansionBudget;
1356+
ULO.UnrollMultiExit = UP.UnrollMultiExit;
13551357
LoopUnrollResult UnrollResult = UnrollLoop(
13561358
L, ULO, LI, &SE, &DT, &AC, &TTI, &ORE, PreserveLCSSA, &RemainderLoop, AA);
13571359
if (UnrollResult == LoopUnrollResult::Unmodified)

llvm/lib/Transforms/Utils/LoopUnroll.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -590,10 +590,11 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
590590
: isEpilogProfitable(L);
591591

592592
if (ULO.Runtime &&
593-
!UnrollRuntimeLoopRemainder(
594-
L, ULO.Count, ULO.AllowExpensiveTripCount, EpilogProfitability,
595-
ULO.UnrollRemainder, ULO.ForgetAllSCEV, LI, SE, DT, AC, TTI,
596-
PreserveLCSSA, ULO.SCEVExpansionBudget, RemainderLoop)) {
593+
!UnrollRuntimeLoopRemainder(L, ULO.Count, ULO.AllowExpensiveTripCount,
594+
EpilogProfitability, ULO.UnrollRemainder,
595+
ULO.ForgetAllSCEV, LI, SE, DT, AC, TTI,
596+
PreserveLCSSA, ULO.SCEVExpansionBudget,
597+
ULO.UnrollMultiExit, RemainderLoop)) {
597598
if (ULO.Force)
598599
ULO.Runtime = false;
599600
else {

llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,7 @@ bool llvm::UnrollRuntimeLoopRemainder(
583583
bool UseEpilogRemainder, bool UnrollRemainder, bool ForgetAllSCEV,
584584
LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC,
585585
const TargetTransformInfo *TTI, bool PreserveLCSSA,
586-
unsigned SCEVExpansionBudget, Loop **ResultLoop) {
586+
unsigned SCEVExpansionBudget, bool UnrollMultiExit, Loop **ResultLoop) {
587587
LLVM_DEBUG(dbgs() << "Trying runtime unrolling on Loop: \n");
588588
LLVM_DEBUG(L->dump());
589589
LLVM_DEBUG(UseEpilogRemainder ? dbgs() << "Using epilog remainder.\n"
@@ -632,8 +632,8 @@ bool llvm::UnrollRuntimeLoopRemainder(
632632
if (!PreserveLCSSA)
633633
return false;
634634

635-
if (!canProfitablyUnrollMultiExitLoop(L, OtherExits, LatchExit,
636-
UseEpilogRemainder)) {
635+
if (!UnrollMultiExit && !canProfitablyUnrollMultiExitLoop(
636+
L, OtherExits, LatchExit, UseEpilogRemainder)) {
637637
LLVM_DEBUG(
638638
dbgs()
639639
<< "Multiple exit/exiting blocks in loop and multi-exit unrolling not "

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, 4);
76+
&AC, /*TTI=*/nullptr, PreserveLCSSA, 4, false);
7777
EXPECT_FALSE(ret);
7878
}

0 commit comments

Comments
 (0)