Skip to content

Commit 0a52401

Browse files
author
Whitney Tsang
committed
[LoopUnrollAndJam] Changed safety checks to consider more than 2-levels
loop nest. Summary: As discussed in https://reviews.llvm.org/D73129. Example Before unroll and jam: for A for B for C D E After unroll and jam (currently): for A A' for B for C D B' for C' D' E E' After unroll and jam (Ideal): for A A' for B B' for C C' D D' E E' This is the first patch to change unroll and jam to work in the ideal way. This patch change the safety checks needed to make sure is safe to unroll and jam in the ideal way. Reviewer: dmgreen, jdoerfert, Meinersbur, kbarton, bmahjour, etiotto Reviewed By: Meinersbur Subscribers: fhahn, hiraditya, zzheng, llvm-commits, anhtuyen, prithayan Tag: LLVM Differential Revision: https://reviews.llvm.org/D76132
1 parent 314f99e commit 0a52401

File tree

5 files changed

+504
-142
lines changed

5 files changed

+504
-142
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ LoopUnrollResult UnrollAndJamLoop(Loop *L, unsigned Count, unsigned TripCount,
110110
Loop **EpilogueLoop = nullptr);
111111

112112
bool isSafeToUnrollAndJam(Loop *L, ScalarEvolution &SE, DominatorTree &DT,
113-
DependenceInfo &DI);
113+
DependenceInfo &DI, LoopInfo &LI);
114114

115115
bool computeUnrollCount(Loop *L, const TargetTransformInfo &TTI,
116116
DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,

llvm/lib/Transforms/Scalar/LoopUnrollAndJamPass.cpp

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -280,21 +280,6 @@ tryToUnrollAndJamLoop(Loop *L, DominatorTree &DT, LoopInfo *LI,
280280
ScalarEvolution &SE, const TargetTransformInfo &TTI,
281281
AssumptionCache &AC, DependenceInfo &DI,
282282
OptimizationRemarkEmitter &ORE, int OptLevel) {
283-
// Quick checks of the correct loop form
284-
if (!L->isLoopSimplifyForm() || L->getSubLoops().size() != 1)
285-
return LoopUnrollResult::Unmodified;
286-
Loop *SubLoop = L->getSubLoops()[0];
287-
if (!SubLoop->isLoopSimplifyForm())
288-
return LoopUnrollResult::Unmodified;
289-
290-
BasicBlock *Latch = L->getLoopLatch();
291-
BasicBlock *Exit = L->getExitingBlock();
292-
BasicBlock *SubLoopLatch = SubLoop->getLoopLatch();
293-
BasicBlock *SubLoopExit = SubLoop->getExitingBlock();
294-
295-
if (Latch != Exit || SubLoopLatch != SubLoopExit)
296-
return LoopUnrollResult::Unmodified;
297-
298283
TargetTransformInfo::UnrollingPreferences UP =
299284
gatherUnrollingPreferences(L, SE, TTI, nullptr, nullptr, OptLevel, None,
300285
None, None, None, None, None, None, None);
@@ -324,7 +309,7 @@ tryToUnrollAndJamLoop(Loop *L, DominatorTree &DT, LoopInfo *LI,
324309
return LoopUnrollResult::Unmodified;
325310
}
326311

327-
if (!isSafeToUnrollAndJam(L, SE, DT, DI)) {
312+
if (!isSafeToUnrollAndJam(L, SE, DT, DI, *LI)) {
328313
LLVM_DEBUG(dbgs() << " Disabled due to not being safe.\n");
329314
return LoopUnrollResult::Unmodified;
330315
}
@@ -335,6 +320,7 @@ tryToUnrollAndJamLoop(Loop *L, DominatorTree &DT, LoopInfo *LI,
335320
bool Convergent;
336321
SmallPtrSet<const Value *, 32> EphValues;
337322
CodeMetrics::collectEphemeralValues(L, &AC, EphValues);
323+
Loop *SubLoop = L->getSubLoops()[0];
338324
unsigned InnerLoopSize =
339325
ApproximateLoopSize(SubLoop, NumInlineCandidates, NotDuplicatable,
340326
Convergent, TTI, EphValues, UP.BEInsns);
@@ -372,6 +358,8 @@ tryToUnrollAndJamLoop(Loop *L, DominatorTree &DT, LoopInfo *LI,
372358
SubLoop->setLoopID(NewInnerEpilogueLoopID.getValue());
373359

374360
// Find trip count and trip multiple
361+
BasicBlock *Latch = L->getLoopLatch();
362+
BasicBlock *SubLoopLatch = SubLoop->getLoopLatch();
375363
unsigned OuterTripCount = SE.getSmallConstantTripCount(L, Latch);
376364
unsigned OuterTripMultiple = SE.getSmallConstantTripMultiple(L, Latch);
377365
unsigned InnerTripCount = SE.getSmallConstantTripCount(SubLoop, SubLoopLatch);

0 commit comments

Comments
 (0)