Skip to content

Commit ad9da92

Browse files
authored
[LoopUnroll] Add RuntimeUnrollMultiExit to loop unroll options (NFC) (#124462)
Add an extra knob to RuntimeUnrollMultiExit 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. PR: #124462
1 parent 804b81d commit ad9da92

File tree

6 files changed

+22
-10
lines changed

6 files changed

+22
-10
lines changed

llvm/include/llvm/Analysis/TargetTransformInfo.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,11 @@ 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+
/// Fall back to the generic logic to determine whether multi-exit unrolling
628+
/// is profitable if set to false.
629+
bool RuntimeUnrollMultiExit;
625630
};
626631

627632
/// 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 RuntimeUnrollMultiExit = 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 RuntimeUnrollMultiExit,
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.RuntimeUnrollMultiExit = 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.RuntimeUnrollMultiExit = UP.RuntimeUnrollMultiExit;
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.RuntimeUnrollMultiExit, RemainderLoop)) {
597598
if (ULO.Force)
598599
ULO.Runtime = false;
599600
else {

llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ CloneLoopBlocks(Loop *L, Value *NewIter, const bool UseEpilogRemainder,
461461

462462
/// Returns true if we can profitably unroll the multi-exit loop L. Currently,
463463
/// we return true only if UnrollRuntimeMultiExit is set to true.
464-
static bool canProfitablyUnrollMultiExitLoop(
464+
static bool canProfitablyRuntimeUnrollMultiExitLoop(
465465
Loop *L, SmallVectorImpl<BasicBlock *> &OtherExits, BasicBlock *LatchExit,
466466
bool UseEpilogRemainder) {
467467

@@ -583,7 +583,8 @@ 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 RuntimeUnrollMultiExit,
587+
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"
@@ -632,8 +633,9 @@ bool llvm::UnrollRuntimeLoopRemainder(
632633
if (!PreserveLCSSA)
633634
return false;
634635

635-
if (!canProfitablyUnrollMultiExitLoop(L, OtherExits, LatchExit,
636-
UseEpilogRemainder)) {
636+
if (!RuntimeUnrollMultiExit &&
637+
!canProfitablyRuntimeUnrollMultiExitLoop(L, OtherExits, LatchExit,
638+
UseEpilogRemainder)) {
637639
LLVM_DEBUG(
638640
dbgs()
639641
<< "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)