Skip to content

Commit d65c32f

Browse files
[LoopUnrollAndJam] Change LoopUnrollAndJamPass to LoopNest pass
This patch changes LoopUnrollAndJamPass from FunctionPass to LoopNest pass. The next patch will utilize LoopNest to effectively handle loop nests. Reviewed By: Whitney Differential Revision: https://reviews.llvm.org/D99149
1 parent d4abbcf commit d65c32f

File tree

7 files changed

+68
-60
lines changed

7 files changed

+68
-60
lines changed

llvm/include/llvm/Transforms/Scalar/LoopUnrollAndJamPass.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#define LLVM_TRANSFORMS_SCALAR_LOOPUNROLLANDJAMPASS_H
1111

1212
#include "llvm/IR/PassManager.h"
13+
#include "llvm/Transforms/Scalar/LoopPassManager.h"
1314

1415
namespace llvm {
1516
class Function;
@@ -20,7 +21,8 @@ class LoopUnrollAndJamPass : public PassInfoMixin<LoopUnrollAndJamPass> {
2021

2122
public:
2223
explicit LoopUnrollAndJamPass(int OptLevel = 2) : OptLevel(OptLevel) {}
23-
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);
24+
PreservedAnalyses run(LoopNest &L, LoopAnalysisManager &AM,
25+
LoopStandardAnalysisResults &AR, LPMUpdater &U);
2426
};
2527

2628
} // end namespace llvm

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "llvm/ADT/DenseMap.h"
1919
#include "llvm/Analysis/TargetTransformInfo.h"
20+
#include "llvm/Transforms/Scalar/LoopPassManager.h"
2021

2122
namespace llvm {
2223

@@ -97,7 +98,7 @@ LoopUnrollResult UnrollAndJamLoop(Loop *L, unsigned Count, unsigned TripCount,
9798
LoopInfo *LI, ScalarEvolution *SE,
9899
DominatorTree *DT, AssumptionCache *AC,
99100
const TargetTransformInfo *TTI,
100-
OptimizationRemarkEmitter *ORE,
101+
OptimizationRemarkEmitter *ORE, LPMUpdater *U,
101102
Loop **EpilogueLoop = nullptr);
102103

103104
bool isSafeToUnrollAndJam(Loop *L, ScalarEvolution &SE, DominatorTree &DT,

llvm/lib/Passes/PassBuilder.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,8 @@ void PassBuilder::addVectorPasses(OptimizationLevel Level,
12071207
// across the loop nests.
12081208
// We do UnrollAndJam in a separate LPM to ensure it happens before unroll
12091209
if (EnableUnrollAndJam && PTO.LoopUnrolling)
1210-
FPM.addPass(LoopUnrollAndJamPass(Level.getSpeedupLevel()));
1210+
FPM.addPass(createFunctionToLoopPassAdaptor(
1211+
LoopUnrollAndJamPass(Level.getSpeedupLevel())));
12111212
FPM.addPass(LoopUnrollPass(LoopUnrollOptions(
12121213
Level.getSpeedupLevel(), /*OnlyWhenForced=*/!PTO.LoopUnrolling,
12131214
PTO.ForgetAllSCEVInLoopUnroll)));
@@ -1290,7 +1291,8 @@ void PassBuilder::addVectorPasses(OptimizationLevel Level,
12901291
// across the loop nests.
12911292
// We do UnrollAndJam in a separate LPM to ensure it happens before unroll
12921293
if (EnableUnrollAndJam && PTO.LoopUnrolling) {
1293-
FPM.addPass(LoopUnrollAndJamPass(Level.getSpeedupLevel()));
1294+
FPM.addPass(createFunctionToLoopPassAdaptor(
1295+
LoopUnrollAndJamPass(Level.getSpeedupLevel())));
12941296
}
12951297
FPM.addPass(LoopUnrollPass(LoopUnrollOptions(
12961298
Level.getSpeedupLevel(), /*OnlyWhenForced=*/!PTO.LoopUnrolling,

llvm/lib/Passes/PassRegistry.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ FUNCTION_PASS("guard-widening", GuardWideningPass())
247247
FUNCTION_PASS("load-store-vectorizer", LoadStoreVectorizerPass())
248248
FUNCTION_PASS("loop-simplify", LoopSimplifyPass())
249249
FUNCTION_PASS("loop-sink", LoopSinkPass())
250-
FUNCTION_PASS("loop-unroll-and-jam", LoopUnrollAndJamPass())
251250
FUNCTION_PASS("loop-flatten", LoopFlattenPass())
252251
FUNCTION_PASS("lowerinvoke", LowerInvokePass())
253252
FUNCTION_PASS("lowerswitch", LowerSwitchPass())
@@ -399,6 +398,7 @@ LOOP_PASS("loop-deletion", LoopDeletionPass())
399398
LOOP_PASS("loop-simplifycfg", LoopSimplifyCFGPass())
400399
LOOP_PASS("loop-reduce", LoopStrengthReducePass())
401400
LOOP_PASS("indvars", IndVarSimplifyPass())
401+
LOOP_PASS("loop-unroll-and-jam", LoopUnrollAndJamPass())
402402
LOOP_PASS("loop-unroll-full", LoopFullUnrollPass())
403403
LOOP_PASS("print-access-info", LoopAccessInfoPrinterPass(dbgs()))
404404
LOOP_PASS("print<ddg>", DDGAnalysisPrinterPass(dbgs()))

llvm/lib/Transforms/Scalar/LoopUnrollAndJamPass.cpp

Lines changed: 47 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "llvm/Analysis/DependenceAnalysis.h"
2323
#include "llvm/Analysis/LoopAnalysisManager.h"
2424
#include "llvm/Analysis/LoopInfo.h"
25+
#include "llvm/Analysis/LoopPass.h"
2526
#include "llvm/Analysis/OptimizationRemarkEmitter.h"
2627
#include "llvm/Analysis/ScalarEvolution.h"
2728
#include "llvm/Analysis/TargetTransformInfo.h"
@@ -278,11 +279,10 @@ static bool computeUnrollAndJamCount(
278279
return false;
279280
}
280281

281-
static LoopUnrollResult
282-
tryToUnrollAndJamLoop(Loop *L, DominatorTree &DT, LoopInfo *LI,
283-
ScalarEvolution &SE, const TargetTransformInfo &TTI,
284-
AssumptionCache &AC, DependenceInfo &DI,
285-
OptimizationRemarkEmitter &ORE, int OptLevel) {
282+
static LoopUnrollResult tryToUnrollAndJamLoop(
283+
Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
284+
const TargetTransformInfo &TTI, AssumptionCache &AC, DependenceInfo &DI,
285+
OptimizationRemarkEmitter &ORE, int OptLevel, LPMUpdater *U) {
286286
TargetTransformInfo::UnrollingPreferences UP =
287287
gatherUnrollingPreferences(L, SE, TTI, nullptr, nullptr, OptLevel, None,
288288
None, None, None, None, None);
@@ -385,7 +385,7 @@ tryToUnrollAndJamLoop(Loop *L, DominatorTree &DT, LoopInfo *LI,
385385
Loop *EpilogueOuterLoop = nullptr;
386386
LoopUnrollResult UnrollResult = UnrollAndJamLoop(
387387
L, UP.Count, OuterTripCount, OuterTripMultiple, UP.UnrollRemainder, LI,
388-
&SE, &DT, &AC, &TTI, &ORE, &EpilogueOuterLoop);
388+
&SE, &DT, &AC, &TTI, &ORE, U, &EpilogueOuterLoop);
389389

390390
// Assign new loop attributes.
391391
if (EpilogueOuterLoop) {
@@ -424,33 +424,23 @@ tryToUnrollAndJamLoop(Loop *L, DominatorTree &DT, LoopInfo *LI,
424424
return UnrollResult;
425425
}
426426

427-
static bool tryToUnrollAndJamLoop(Function &F, DominatorTree &DT, LoopInfo &LI,
427+
static bool tryToUnrollAndJamLoop(LoopNest &LN, DominatorTree &DT, LoopInfo &LI,
428428
ScalarEvolution &SE,
429429
const TargetTransformInfo &TTI,
430430
AssumptionCache &AC, DependenceInfo &DI,
431-
OptimizationRemarkEmitter &ORE,
432-
int OptLevel) {
431+
OptimizationRemarkEmitter &ORE, int OptLevel,
432+
LPMUpdater &U) {
433433
bool DidSomething = false;
434+
ArrayRef<Loop *> Loops = LN.getLoops();
434435

435-
// The loop unroll and jam pass requires loops to be in simplified form, and
436-
// also needs LCSSA. Since simplification may add new inner loops, it has to
437-
// run before the legality and profitability checks. This means running the
438-
// loop unroll and jam pass will simplify all loops, regardless of whether
439-
// anything end up being unroll and jammed.
440-
for (auto &L : LI) {
441-
DidSomething |=
442-
simplifyLoop(L, &DT, &LI, &SE, &AC, nullptr, false /* PreserveLCSSA */);
443-
DidSomething |= formLCSSARecursively(*L, DT, &LI, &SE);
444-
}
445-
446-
// Add the loop nests in the reverse order of LoopInfo. See method
436+
// Add the loop nests in the reverse order of LN. See method
447437
// declaration.
448438
SmallPriorityWorklist<Loop *, 4> Worklist;
449-
appendLoopsToWorklist(LI, Worklist);
439+
appendLoopsToWorklist(Loops, Worklist);
450440
while (!Worklist.empty()) {
451441
Loop *L = Worklist.pop_back_val();
452442
LoopUnrollResult Result =
453-
tryToUnrollAndJamLoop(L, DT, &LI, SE, TTI, AC, DI, ORE, OptLevel);
443+
tryToUnrollAndJamLoop(L, DT, &LI, SE, TTI, AC, DI, ORE, OptLevel, &U);
454444
if (Result != LoopUnrollResult::Unmodified)
455445
DidSomething = true;
456446
}
@@ -460,29 +450,35 @@ static bool tryToUnrollAndJamLoop(Function &F, DominatorTree &DT, LoopInfo &LI,
460450

461451
namespace {
462452

463-
class LoopUnrollAndJam : public FunctionPass {
453+
class LoopUnrollAndJam : public LoopPass {
464454
public:
465455
static char ID; // Pass ID, replacement for typeid
466456
unsigned OptLevel;
467457

468-
LoopUnrollAndJam(int OptLevel = 2) : FunctionPass(ID), OptLevel(OptLevel) {
458+
LoopUnrollAndJam(int OptLevel = 2) : LoopPass(ID), OptLevel(OptLevel) {
469459
initializeLoopUnrollAndJamPass(*PassRegistry::getPassRegistry());
470460
}
471461

472-
bool runOnFunction(Function &F) override {
473-
if (skipFunction(F))
462+
bool runOnLoop(Loop *L, LPPassManager &LPM) override {
463+
if (skipLoop(L))
474464
return false;
475465

476-
auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
477-
LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
478-
ScalarEvolution &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
479-
const TargetTransformInfo &TTI =
480-
getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F);
481-
auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
466+
auto *F = L->getHeader()->getParent();
467+
auto &SE = getAnalysis<ScalarEvolutionWrapperPass>().getSE();
468+
auto *LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
482469
auto &DI = getAnalysis<DependenceAnalysisWrapperPass>().getDI();
470+
auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
471+
auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(*F);
483472
auto &ORE = getAnalysis<OptimizationRemarkEmitterWrapperPass>().getORE();
473+
auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(*F);
484474

485-
return tryToUnrollAndJamLoop(F, DT, LI, SE, TTI, AC, DI, ORE, OptLevel);
475+
LoopUnrollResult Result = tryToUnrollAndJamLoop(L, DT, LI, SE, TTI, AC, DI,
476+
ORE, OptLevel, nullptr);
477+
478+
if (Result == LoopUnrollResult::FullyUnrolled)
479+
LPM.markLoopAsDeleted(*L);
480+
481+
return Result != LoopUnrollResult::Unmodified;
486482
}
487483

488484
/// This transformation requires natural loop information & requires that
@@ -505,7 +501,10 @@ char LoopUnrollAndJam::ID = 0;
505501
INITIALIZE_PASS_BEGIN(LoopUnrollAndJam, "loop-unroll-and-jam",
506502
"Unroll and Jam loops", false, false)
507503
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
504+
INITIALIZE_PASS_DEPENDENCY(LoopPass)
508505
INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass)
506+
INITIALIZE_PASS_DEPENDENCY(LoopSimplify)
507+
INITIALIZE_PASS_DEPENDENCY(LCSSAWrapperPass)
509508
INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
510509
INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass)
511510
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
@@ -518,19 +517,20 @@ Pass *llvm::createLoopUnrollAndJamPass(int OptLevel) {
518517
return new LoopUnrollAndJam(OptLevel);
519518
}
520519

521-
PreservedAnalyses LoopUnrollAndJamPass::run(Function &F,
522-
FunctionAnalysisManager &AM) {
523-
ScalarEvolution &SE = AM.getResult<ScalarEvolutionAnalysis>(F);
524-
LoopInfo &LI = AM.getResult<LoopAnalysis>(F);
525-
TargetTransformInfo &TTI = AM.getResult<TargetIRAnalysis>(F);
526-
AssumptionCache &AC = AM.getResult<AssumptionAnalysis>(F);
527-
DominatorTree &DT = AM.getResult<DominatorTreeAnalysis>(F);
528-
DependenceInfo &DI = AM.getResult<DependenceAnalysis>(F);
529-
OptimizationRemarkEmitter &ORE =
530-
AM.getResult<OptimizationRemarkEmitterAnalysis>(F);
531-
532-
if (!tryToUnrollAndJamLoop(F, DT, LI, SE, TTI, AC, DI, ORE, OptLevel))
520+
PreservedAnalyses LoopUnrollAndJamPass::run(LoopNest &LN,
521+
LoopAnalysisManager &AM,
522+
LoopStandardAnalysisResults &AR,
523+
LPMUpdater &U) {
524+
Function &F = *LN.getParent();
525+
526+
DependenceInfo DI(&F, &AR.AA, &AR.SE, &AR.LI);
527+
OptimizationRemarkEmitter ORE(&F);
528+
529+
if (!tryToUnrollAndJamLoop(LN, AR.DT, AR.LI, AR.SE, AR.TTI, AR.AC, DI, ORE,
530+
OptLevel, U))
533531
return PreservedAnalyses::all();
534532

535-
return getLoopPassPreservedAnalyses();
533+
auto PA = getLoopPassPreservedAnalyses();
534+
PA.preserve<LoopNestAnalysis>();
535+
return PA;
536536
}

llvm/lib/Transforms/Utils/LoopUnrollAndJam.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
#include "llvm/Support/ErrorHandling.h"
5050
#include "llvm/Support/GenericDomTree.h"
5151
#include "llvm/Support/raw_ostream.h"
52+
#include "llvm/Transforms/Scalar/LoopPassManager.h"
5253
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
5354
#include "llvm/Transforms/Utils/Cloning.h"
5455
#include "llvm/Transforms/Utils/LoopUtils.h"
@@ -221,12 +222,11 @@ static void moveHeaderPhiOperandsToForeBlocks(BasicBlock *Header,
221222
If EpilogueLoop is non-null, it receives the epilogue loop (if it was
222223
necessary to create one and not fully unrolled).
223224
*/
224-
LoopUnrollResult
225-
llvm::UnrollAndJamLoop(Loop *L, unsigned Count, unsigned TripCount,
226-
unsigned TripMultiple, bool UnrollRemainder,
227-
LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT,
228-
AssumptionCache *AC, const TargetTransformInfo *TTI,
229-
OptimizationRemarkEmitter *ORE, Loop **EpilogueLoop) {
225+
LoopUnrollResult llvm::UnrollAndJamLoop(
226+
Loop *L, unsigned Count, unsigned TripCount, unsigned TripMultiple,
227+
bool UnrollRemainder, LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT,
228+
AssumptionCache *AC, const TargetTransformInfo *TTI,
229+
OptimizationRemarkEmitter *ORE, LPMUpdater *U, Loop **EpilogueLoop) {
230230

231231
// When we enter here we should have already checked that it is safe
232232
BasicBlock *Header = L->getHeader();
@@ -605,8 +605,11 @@ llvm::UnrollAndJamLoop(Loop *L, unsigned Count, unsigned TripCount,
605605
++NumUnrolledAndJammed;
606606

607607
// Update LoopInfo if the loop is completely removed.
608-
if (CompletelyUnroll)
608+
if (CompletelyUnroll) {
609+
if (U)
610+
U->markLoopAsDeleted(*L, std::string(L->getName()));
609611
LI->erase(L);
612+
}
610613

611614
#ifndef NDEBUG
612615
// We shouldn't have done anything to break loop simplify form or LCSSA.

llvm/test/Transforms/LoopUnrollAndJam/innerloop.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
; RUN: opt -loop-unroll-and-jam -allow-unroll-and-jam -verify-loop-info < %s -S | FileCheck %s
2-
; RUN: opt -passes='loop-unroll-and-jam,verify<loops>' -allow-unroll-and-jam < %s -S | FileCheck %s
2+
; RUN: opt -passes='loop(loop-unroll-and-jam),verify<loops>' -allow-unroll-and-jam < %s -S | FileCheck %s
33

44
; Check that the newly created loops to not fail to be added to LI
55
; This test deliberately disables UnJ on the middle loop, performing it instead on the

0 commit comments

Comments
 (0)