Skip to content

Commit 6113bc9

Browse files
committed
[LoopInterchange] Prevent interchange if one index is constant and the other has loop carried dependence
Consider the following loop from llvm#54176 for (int j = 1; j < M; j++) for (int i = 1; i < N; i++) { aa[1][j-1] += bb[i][j]; cc[i][j] = aa[1][j]; } Loops should not be interchanged in this case as it will change the cc array results.
1 parent 8350471 commit 6113bc9

File tree

4 files changed

+20
-6
lines changed

4 files changed

+20
-6
lines changed

llvm/include/llvm/Analysis/DependenceAnalysis.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,9 +306,9 @@ namespace llvm {
306306
/// The flag PossiblyLoopIndependent should be set by the caller
307307
/// if it appears that control flow can reach from Src to Dst
308308
/// without traversing a loop back edge.
309-
std::unique_ptr<Dependence> depends(Instruction *Src,
310-
Instruction *Dst,
311-
bool PossiblyLoopIndependent);
309+
std::unique_ptr<Dependence> depends(Instruction *Src, Instruction *Dst,
310+
bool PossiblyLoopIndependent,
311+
bool *HasConstantIndex = nullptr);
312312

313313
/// getSplitIteration - Give a dependence that's splittable at some
314314
/// particular level, return the iteration that should be used to split

llvm/lib/Analysis/DependenceAnalysis.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3588,7 +3588,7 @@ bool DependenceInfo::invalidate(Function &F, const PreservedAnalyses &PA,
35883588
// up to date with respect to this routine.
35893589
std::unique_ptr<Dependence>
35903590
DependenceInfo::depends(Instruction *Src, Instruction *Dst,
3591-
bool PossiblyLoopIndependent) {
3591+
bool PossiblyLoopIndependent, bool *HasConstantIndex) {
35923592
if (Src == Dst)
35933593
PossiblyLoopIndependent = false;
35943594

@@ -3674,6 +3674,9 @@ DependenceInfo::depends(Instruction *Src, Instruction *Dst,
36743674
LLVM_DEBUG(dbgs() << "\tclass = " << Pair[P].Classification << "\n");
36753675
LLVM_DEBUG(dbgs() << "\tloops = ");
36763676
LLVM_DEBUG(dumpSmallBitVector(Pair[P].Loops));
3677+
if (HasConstantIndex)
3678+
if (isa<SCEVConstant>(Pair[P].Src) || isa<SCEVConstant>(Pair[P].Dst))
3679+
*HasConstantIndex = true;
36773680
}
36783681

36793682
SmallBitVector Separable(Pairs);

llvm/lib/Transforms/Scalar/LoopInterchange.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,12 @@ static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
117117
std::vector<char> Dep;
118118
Instruction *Src = cast<Instruction>(*I);
119119
Instruction *Dst = cast<Instruction>(*J);
120+
bool HasConstantIndex = false;
120121
// Ignore Input dependencies.
121122
if (isa<LoadInst>(Src) && isa<LoadInst>(Dst))
122123
continue;
123124
// Track Output, Flow, and Anti dependencies.
124-
if (auto D = DI->depends(Src, Dst, true)) {
125+
if (auto D = DI->depends(Src, Dst, true, &HasConstantIndex)) {
125126
assert(D->isOrdered() && "Expected an output, flow or anti dep.");
126127
// If the direction vector is negative, normalize it to
127128
// make it non-negative.
@@ -150,6 +151,13 @@ static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
150151
Direction = '=';
151152
else
152153
Direction = '*';
154+
155+
// Bail out if there is constant index and the other has loop
156+
// carried dependence.
157+
if (HasConstantIndex && (Direction == '>' || Direction == '<')) {
158+
dbgs() << "Has Constant Index and loop carried dependence\n";
159+
return false;
160+
}
153161
Dep.push_back(Direction);
154162
}
155163
}

llvm/test/Transforms/LoopInterchange/pr54176.ll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,16 @@
66
@cc = global [1024 x [128 x float]] zeroinitializer, align 4
77

88

9+
;; Loops should not be interchanged in this case as it will change the
10+
;; cc array results.
11+
;;
912
;; for (int j = 1; j < M; j++)
1013
;; for (int i = 1; i < N; i++) {
1114
;; aa[1][j-1] += bb[i][j];
1215
;; cc[i][j] = aa[1][j];
1316
;; }
1417

15-
; CHECK: Loops interchanged.
18+
; CHECK-NOT: Loops interchanged.
1619

1720
define void @pr54176() {
1821
entry:

0 commit comments

Comments
 (0)