Skip to content

Commit cfb1619

Browse files
author
Marek Sedlacek
committed
Added flag to enable loop rotation in UnrollRuntimeLoopRemainder
1 parent c8bd1d2 commit cfb1619

File tree

5 files changed

+26
-22
lines changed

5 files changed

+26
-22
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ LLVM_ABI LoopReminderUnrollResult UnrollRuntimeLoopRemainder(
111111
LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC,
112112
const TargetTransformInfo *TTI, bool PreserveLCSSA,
113113
unsigned SCEVExpansionBudget, bool RuntimeUnrollMultiExit,
114-
Loop **ResultLoop = nullptr);
114+
bool AllowLoopRotation, Loop **ResultLoop = nullptr);
115115

116116
LLVM_ABI LoopUnrollResult UnrollAndJamLoop(
117117
Loop *L, unsigned Count, unsigned TripCount, unsigned TripMultiple,

llvm/lib/Transforms/Utils/LoopUnroll.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
542542
L, ULO.Count, ULO.AllowExpensiveTripCount, EpilogProfitability,
543543
ULO.UnrollRemainder, ULO.ForgetAllSCEV, LI, SE, DT, AC, TTI,
544544
PreserveLCSSA, ULO.SCEVExpansionBudget, ULO.RuntimeUnrollMultiExit,
545-
RemainderLoop);
545+
/*AllowLoopRotation*/ true, RemainderLoop);
546546
LatchBlock = L->getLoopLatch();
547547
LatchIsExiting = L->isLoopExiting(LatchBlock);
548548
}

llvm/lib/Transforms/Utils/LoopUnrollAndJam.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,12 @@ llvm::UnrollAndJamLoop(Loop *L, unsigned Count, unsigned TripCount,
237237
// Are we eliminating the loop control altogether?
238238
bool CompletelyUnroll = (Count == TripCount);
239239

240-
// We use the runtime remainder in cases where we don't know trip multiple
241240
if (TripMultiple % Count != 0) {
242241
auto UnrollReminderResult = UnrollRuntimeLoopRemainder(
243242
L, Count, /*AllowExpensiveTripCount*/ false,
244243
/*UseEpilogRemainder*/ true, UnrollRemainder, /*ForgetAllSCEV*/ false,
245-
LI, SE, DT, AC, TTI, true, SCEVCheapExpansionBudget, EpilogueLoop);
244+
LI, SE, DT, AC, TTI, /*PreserveLCSSA*/ true, SCEVCheapExpansionBudget,
245+
/*AllowLoopRotation*/ false, EpilogueLoop);
246246
if (UnrollReminderResult != LoopReminderUnrollResult::Unrolled) {
247247
LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; remainder loop could not be "
248248
"generated when assuming runtime trip count\n");

llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ LoopReminderUnrollResult llvm::UnrollRuntimeLoopRemainder(
581581
LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC,
582582
const TargetTransformInfo *TTI, bool PreserveLCSSA,
583583
unsigned SCEVExpansionBudget, bool RuntimeUnrollMultiExit,
584-
Loop **ResultLoop) {
584+
bool AllowLoopRotation, Loop **ResultLoop) {
585585
LLVM_DEBUG(dbgs() << "Trying runtime unrolling on Loop: \n");
586586
LLVM_DEBUG(L->dump());
587587
LLVM_DEBUG(UseEpilogRemainder ? dbgs() << "Using epilog remainder.\n"
@@ -590,22 +590,25 @@ LoopReminderUnrollResult llvm::UnrollRuntimeLoopRemainder(
590590
LoopReminderUnrollResult Result = LoopReminderUnrollResult::Unmodified;
591591

592592
// Rotate loop if it makes the exit count from the latch computable.
593-
BasicBlock *OrigHeader = L->getHeader();
594-
BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator());
595-
if (BI && !BI->isUnconditional() &&
596-
isa<SCEVCouldNotCompute>(SE->getExitCount(L, L->getLoopLatch())) &&
597-
!isa<SCEVCouldNotCompute>(SE->getExitCount(L, OrigHeader))) {
598-
LLVM_DEBUG(
599-
dbgs() << " Rotating loop to make the exit count computable.\n");
600-
SimplifyQuery SQ{OrigHeader->getDataLayout()};
601-
SQ.TLI = nullptr;
602-
SQ.DT = DT;
603-
SQ.AC = AC;
604-
if (llvm::LoopRotation(L, LI, TTI, AC, DT, SE, nullptr /*MemorySSAUpdater*/,
605-
SQ, false /*RotationOnly*/, 16 /*Threshold*/,
606-
false /*IsUtilMode*/, false /*PrepareForLTO*/,
607-
[](Loop *, ScalarEvolution *) { return true; }))
608-
Result = LoopReminderUnrollResult::Rotated;
593+
if (AllowLoopRotation) {
594+
BasicBlock *OrigHeader = L->getHeader();
595+
BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator());
596+
if (BI && !BI->isUnconditional() &&
597+
isa<SCEVCouldNotCompute>(SE->getExitCount(L, L->getLoopLatch())) &&
598+
!isa<SCEVCouldNotCompute>(SE->getExitCount(L, OrigHeader))) {
599+
LLVM_DEBUG(
600+
dbgs() << " Rotating loop to make the exit count computable.\n");
601+
SimplifyQuery SQ{OrigHeader->getDataLayout()};
602+
SQ.TLI = nullptr;
603+
SQ.DT = DT;
604+
SQ.AC = AC;
605+
if (llvm::LoopRotation(L, LI, TTI, AC, DT, SE,
606+
/*MemorySSAUpdater*/ nullptr, SQ,
607+
/*RotationOnly*/ false, /*Threshold*/ 16,
608+
/*IsUtilMode*/ false, /*PrepareForLTO*/ false,
609+
[](Loop *, ScalarEvolution *) { return true; }))
610+
Result = LoopReminderUnrollResult::Rotated;
611+
}
609612
}
610613

611614
// Make sure the loop is in canonical form.

llvm/unittests/Transforms/Utils/UnrollLoopTest.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ 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, false);
76+
&AC, /*TTI=*/nullptr, PreserveLCSSA, 4, false,
77+
false);
7778
EXPECT_FALSE(ret);
7879
}

0 commit comments

Comments
 (0)