Skip to content

Commit c6600b6

Browse files
committed
[LoopInterchange] Prevent interchange for constant index with loop carried dependence
1 parent f3698af commit c6600b6

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

llvm/lib/Transforms/Scalar/LoopInterchange.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,34 @@ static void printDepMatrix(CharMatrix &DepMatrix) {
8282
}
8383
#endif
8484

85+
static unsigned getAffectedLoopNum(const SCEV *Expr, ScalarEvolution &SE) {
86+
const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr);
87+
if (!AddRec)
88+
return 0;
89+
90+
const SCEV *Start = AddRec->getStart();
91+
const SCEV *Step = AddRec->getStepRecurrence(SE);
92+
return getAffectedLoopNum(Start, SE) + getAffectedLoopNum(Step, SE) + 1;
93+
}
94+
95+
static bool hasConstantIndex(Instruction *Src, Instruction *Dst,
96+
ScalarEvolution *SE) {
97+
Value *PtrSrc = getLoadStorePointerOperand(Src);
98+
Value *PtrDst = getLoadStorePointerOperand(Dst);
99+
const SCEV *SrcSCEV = SE->getSCEV(PtrSrc);
100+
const SCEV *DstSCEV = SE->getSCEV(PtrDst);
101+
102+
unsigned SrcLoops = getAffectedLoopNum(SrcSCEV, *SE);
103+
unsigned DstLoops = getAffectedLoopNum(DstSCEV, *SE);
104+
// Loop interchange would need at least two loops. If the SCEV form
105+
// only has one loop or the loop numbers are not equal. There will be
106+
// at least one constant index.
107+
if (SrcLoops == 1 || DstLoops == 1 || SrcLoops != DstLoops)
108+
return true;
109+
110+
return false;
111+
}
112+
85113
static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
86114
Loop *L, DependenceInfo *DI,
87115
ScalarEvolution *SE) {
@@ -150,6 +178,13 @@ static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
150178
Direction = '=';
151179
else
152180
Direction = '*';
181+
182+
if (hasConstantIndex(Src, Dst, SE) &&
183+
(Direction == '>' || Direction == '<')) {
184+
LLVM_DEBUG(dbgs() << "Has constant index with loop carried"
185+
<< " dependencies inside loop\n");
186+
return false;
187+
}
153188
Dep.push_back(Direction);
154189
}
155190
}

llvm/test/Transforms/LoopInterchange/pr54176.ll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
;; cc[i][j] = aa[1][j];
1313
;; }
1414

15-
; CHECK: Loops interchanged.
15+
; CHECK: Has constant index with loop carried dependencies inside loop
16+
; CHECK: Populating dependency matrix failed
1617

1718
define void @pr54176() {
1819
entry:

0 commit comments

Comments
 (0)