Skip to content

Commit 0eeaec2

Browse files
committed
[NFC] Refactor LoopInterchange into a loop-nest pass
This is the preliminary patch of converting `LoopInterchange` pass to a loop-nest pass and has no intended functional change. Changes that are not loop-nest related are split to D96650. Reviewed By: Whitney Differential Revision: https://reviews.llvm.org/D96644
1 parent 872efb0 commit 0eeaec2

File tree

4 files changed

+80
-13
lines changed

4 files changed

+80
-13
lines changed

llvm/include/llvm/Analysis/LoopNestAnalysis.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ class LoopNest {
139139
return all_of(Loops, [](const Loop *L) { return L->isRotatedForm(); });
140140
}
141141

142+
/// Return the function to which the loop-nest belongs.
143+
Function *getParent() const {
144+
return Loops.front()->getHeader()->getParent();
145+
}
146+
142147
StringRef getName() const { return Loops.front()->getName(); }
143148

144149
protected:

llvm/include/llvm/Transforms/Scalar/LoopInterchange.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
namespace llvm {
1616

1717
struct LoopInterchangePass : public PassInfoMixin<LoopInterchangePass> {
18-
PreservedAnalyses run(Loop &L, LoopAnalysisManager &AM,
18+
PreservedAnalyses run(LoopNest &L, LoopAnalysisManager &AM,
1919
LoopStandardAnalysisResults &AR, LPMUpdater &U);
2020
};
2121

llvm/lib/Transforms/Scalar/LoopInterchange.cpp

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,15 @@ struct LoopInterchange {
449449
return processLoopList(populateWorklist(*L));
450450
}
451451

452-
bool isComputableLoopNest(LoopVector LoopList) {
452+
bool run(LoopNest &LN) {
453+
const auto &LoopList = LN.getLoops();
454+
for (unsigned I = 1; I < LoopList.size(); ++I)
455+
if (LoopList[I]->getParentLoop() != LoopList[I - 1])
456+
return false;
457+
return processLoopList(LoopList);
458+
}
459+
460+
bool isComputableLoopNest(ArrayRef<Loop *> LoopList) {
453461
for (Loop *L : LoopList) {
454462
const SCEV *ExitCountOuter = SE->getBackedgeTakenCount(L);
455463
if (isa<SCEVCouldNotCompute>(ExitCountOuter)) {
@@ -468,13 +476,13 @@ struct LoopInterchange {
468476
return true;
469477
}
470478

471-
unsigned selectLoopForInterchange(const LoopVector &LoopList) {
479+
unsigned selectLoopForInterchange(ArrayRef<Loop *> LoopList) {
472480
// TODO: Add a better heuristic to select the loop to be interchanged based
473481
// on the dependence matrix. Currently we select the innermost loop.
474482
return LoopList.size() - 1;
475483
}
476484

477-
bool processLoopList(LoopVector LoopList) {
485+
bool processLoopList(ArrayRef<Loop *> LoopList) {
478486
bool Changed = false;
479487
unsigned LoopNestDepth = LoopList.size();
480488
if (LoopNestDepth < 2) {
@@ -515,14 +523,12 @@ struct LoopInterchange {
515523

516524
unsigned SelecLoopId = selectLoopForInterchange(LoopList);
517525
// Move the selected loop outwards to the best possible position.
526+
Loop *LoopToBeInterchanged = LoopList[SelecLoopId];
518527
for (unsigned i = SelecLoopId; i > 0; i--) {
519-
bool Interchanged = processLoop(LoopList[i], LoopList[i - 1], i, i - 1,
520-
LoopNestExit, DependencyMatrix);
528+
bool Interchanged = processLoop(LoopToBeInterchanged, LoopList[i - 1], i,
529+
i - 1, LoopNestExit, DependencyMatrix);
521530
if (!Interchanged)
522531
return Changed;
523-
// Loops interchanged reflect the same in LoopList
524-
std::swap(LoopList[i - 1], LoopList[i]);
525-
526532
// Update the DependencyMatrix
527533
interChangeDependencies(DependencyMatrix, i, i - 1);
528534
#ifdef DUMP_DEP_MATRICIES
@@ -539,7 +545,6 @@ struct LoopInterchange {
539545
std::vector<std::vector<char>> &DependencyMatrix) {
540546
LLVM_DEBUG(dbgs() << "Processing InnerLoopId = " << InnerLoopId
541547
<< " and OuterLoopId = " << OuterLoopId << "\n");
542-
543548
LoopInterchangeLegality LIL(OuterLoop, InnerLoop, SE, ORE);
544549
if (!LIL.canInterchangeLoops(InnerLoopId, OuterLoopId, DependencyMatrix)) {
545550
LLVM_DEBUG(dbgs() << "Not interchanging loops. Cannot prove legality.\n");
@@ -1680,14 +1685,15 @@ Pass *llvm::createLoopInterchangePass() {
16801685
return new LoopInterchangeLegacyPass();
16811686
}
16821687

1683-
PreservedAnalyses LoopInterchangePass::run(Loop &L, LoopAnalysisManager &AM,
1688+
PreservedAnalyses LoopInterchangePass::run(LoopNest &LN,
1689+
LoopAnalysisManager &AM,
16841690
LoopStandardAnalysisResults &AR,
16851691
LPMUpdater &U) {
1686-
Function &F = *L.getHeader()->getParent();
1692+
Function &F = *LN.getParent();
16871693

16881694
DependenceInfo DI(&F, &AR.AA, &AR.SE, &AR.LI);
16891695
OptimizationRemarkEmitter ORE(&F);
1690-
if (!LoopInterchange(&AR.SE, &AR.LI, &DI, &AR.DT, &ORE).run(&L))
1696+
if (!LoopInterchange(&AR.SE, &AR.LI, &DI, &AR.DT, &ORE).run(LN))
16911697
return PreservedAnalyses::all();
16921698
return getLoopPassPreservedAnalyses();
16931699
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
; REQUIRES: asserts
2+
; RUN: opt < %s -basic-aa -loop-interchange -verify-dom-info -verify-loop-info \
3+
; RUN: -S -debug 2>&1 | FileCheck %s
4+
5+
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
6+
target triple = "x86_64-unknown-linux-gnu"
7+
8+
@D = common global [100 x [100 x [100 x i32]]] zeroinitializer
9+
10+
;; Test for interchange in loop nest greater than 2.
11+
;; for(int i=0;i<100;i++)
12+
;; for(int j=0;j<100;j++)
13+
;; for(int k=0;k<100;k++)
14+
;; D[k][j][i] = D[k][j][i]+t;
15+
16+
; CHECK: Processing InnerLoopId = 2 and OuterLoopId = 1
17+
; CHECK: Loops interchanged.
18+
19+
; CHECK: Processing InnerLoopId = 1 and OuterLoopId = 0
20+
; CHECK: Loops interchanged.
21+
22+
define void @interchange_08(i32 %t){
23+
entry:
24+
br label %for.cond1.preheader
25+
26+
for.cond1.preheader: ; preds = %for.inc15, %entry
27+
%i.028 = phi i32 [ 0, %entry ], [ %inc16, %for.inc15 ]
28+
br label %for.cond4.preheader
29+
30+
for.cond4.preheader: ; preds = %for.inc12, %for.cond1.preheader
31+
%j.027 = phi i32 [ 0, %for.cond1.preheader ], [ %inc13, %for.inc12 ]
32+
br label %for.body6
33+
34+
for.body6: ; preds = %for.body6, %for.cond4.preheader
35+
%k.026 = phi i32 [ 0, %for.cond4.preheader ], [ %inc, %for.body6 ]
36+
%arrayidx8 = getelementptr inbounds [100 x [100 x [100 x i32]]], [100 x [100 x [100 x i32]]]* @D, i32 0, i32 %k.026, i32 %j.027, i32 %i.028
37+
%0 = load i32, i32* %arrayidx8
38+
%add = add nsw i32 %0, %t
39+
store i32 %add, i32* %arrayidx8
40+
%inc = add nuw nsw i32 %k.026, 1
41+
%exitcond = icmp eq i32 %inc, 100
42+
br i1 %exitcond, label %for.inc12, label %for.body6
43+
44+
for.inc12: ; preds = %for.body6
45+
%inc13 = add nuw nsw i32 %j.027, 1
46+
%exitcond29 = icmp eq i32 %inc13, 100
47+
br i1 %exitcond29, label %for.inc15, label %for.cond4.preheader
48+
49+
for.inc15: ; preds = %for.inc12
50+
%inc16 = add nuw nsw i32 %i.028, 1
51+
%exitcond30 = icmp eq i32 %inc16, 100
52+
br i1 %exitcond30, label %for.end17, label %for.cond1.preheader
53+
54+
for.end17: ; preds = %for.inc15
55+
ret void
56+
}

0 commit comments

Comments
 (0)