Skip to content

Commit 281028e

Browse files
authored
[LoopInterchange] Prevent from undoing its own transformation (#127473)
LoopInterchange uses the bubble-sort fashion algorithm to sort the loops in a loop nest. For two loops `A` and `B`, it calls `isProfitable(A, B)` to determine whether these loops should be exchanged. The result of `isProfitable(A, B)` should be conservative, that is, it should return true only when we are sure exchanging `A` and `B` will improve performance. If it's not conservative enough, it's possible that a subsequent `isProfitable(B, A)` will also return true, in which case LoopInterchange will undo its previous transformation. To avoid such cases, `isProfitable(B, A)` must not return true if `isProfitable(A, B)` returned true in the past. However, the current implementation can be in such a situation. This patch resolves it by correcting the handling of two loops that have the same cache cost. This resolve the problem mentioned in #118267 (comment).
1 parent ea03bde commit 281028e

File tree

2 files changed

+84
-5
lines changed

2 files changed

+84
-5
lines changed

llvm/lib/Transforms/Scalar/LoopInterchange.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,17 +1146,15 @@ LoopInterchangeProfitability::isProfitablePerLoopCacheAnalysis(
11461146
if (OuterLoopIt == CostMap.end())
11471147
return std::nullopt;
11481148

1149+
if (CC->getLoopCost(*OuterLoop) == CC->getLoopCost(*InnerLoop))
1150+
return std::nullopt;
11491151
unsigned InnerIndex = InnerLoopIt->second;
11501152
unsigned OuterIndex = OuterLoopIt->second;
11511153
LLVM_DEBUG(dbgs() << "InnerIndex = " << InnerIndex
11521154
<< ", OuterIndex = " << OuterIndex << "\n");
1153-
if (InnerIndex < OuterIndex)
1154-
return std::optional<bool>(true);
11551155
assert(InnerIndex != OuterIndex && "CostMap should assign unique "
11561156
"numbers to each loop");
1157-
if (CC->getLoopCost(*OuterLoop) == CC->getLoopCost(*InnerLoop))
1158-
return std::nullopt;
1159-
return std::optional<bool>(false);
1157+
return std::optional<bool>(InnerIndex < OuterIndex);
11601158
}
11611159

11621160
std::optional<bool>
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
; RUN: opt < %s -passes=loop-interchange -cache-line-size=1 -pass-remarks-output=%t -disable-output \
2+
; RUN: -verify-dom-info -verify-loop-info
3+
; RUN: FileCheck -input-file %t %s
4+
5+
6+
; Test that loop-interchange doesn't undo its own transoformation. This is
7+
; the case when the cost computed by CacheCost is the same for the loop of `j`
8+
; and `k`.
9+
;
10+
; #define N 4
11+
; int a[N*N][N*N][N*N];
12+
; void f() {
13+
; for (int i = 0; i < N; i++)
14+
; for (int j = 1; j < 2*N; j++)
15+
; for (int k = 1; k < 2*N; k++)
16+
; a[i][k+1][j-1] -= a[i+N-1][k][j];
17+
; }
18+
19+
; CHECK: --- !Passed
20+
; CHECK-NEXT: Pass: loop-interchange
21+
; CHECK-NEXT: Name: Interchanged
22+
; CHECK-NEXT: Function: f
23+
; CHECK-NEXT: Args:
24+
; CHECK-NEXT: - String: Loop interchanged with enclosing loop.
25+
; CHECK-NEXT: ...
26+
; CHECK-NEXT: --- !Missed
27+
; CHECK-NEXT: Pass: loop-interchange
28+
; CHECK-NEXT: Name: Dependence
29+
; CHECK-NEXT: Function: f
30+
; CHECK-NEXT: Args:
31+
; CHECK-NEXT: - String: Cannot interchange loops due to dependences.
32+
; CHECK-NEXT: ...
33+
; CHECK-NEXT: --- !Missed
34+
; CHECK-NEXT: Pass: loop-interchange
35+
; CHECK-NEXT: Name: InterchangeNotProfitable
36+
; CHECK-NEXT: Function: f
37+
; CHECK-NEXT: Args:
38+
; CHECK-NEXT: - String: Interchanging loops is not considered to improve cache locality nor vectorization.
39+
; CHECK-NEXT: ...
40+
41+
@a = dso_local local_unnamed_addr global [16 x [16 x [16 x i32]]] zeroinitializer, align 4
42+
43+
define dso_local void @f() {
44+
entry:
45+
br label %for.cond1.preheader
46+
47+
for.cond1.preheader:
48+
%indvars.iv46 = phi i64 [ 0, %entry ], [ %indvars.iv.next47, %for.cond.cleanup3 ]
49+
%0 = add nuw nsw i64 %indvars.iv46, 3
50+
br label %for.cond5.preheader
51+
52+
for.cond5.preheader:
53+
%indvars.iv41 = phi i64 [ 1, %for.cond1.preheader ], [ %indvars.iv.next42, %for.cond.cleanup7 ]
54+
%1 = add nsw i64 %indvars.iv41, -1
55+
br label %for.body8
56+
57+
for.cond.cleanup3:
58+
%indvars.iv.next47 = add nuw nsw i64 %indvars.iv46, 1
59+
%exitcond50 = icmp ne i64 %indvars.iv.next47, 4
60+
br i1 %exitcond50, label %for.cond1.preheader, label %for.cond.cleanup
61+
62+
for.cond.cleanup7:
63+
%indvars.iv.next42 = add nuw nsw i64 %indvars.iv41, 1
64+
%exitcond45 = icmp ne i64 %indvars.iv.next42, 8
65+
br i1 %exitcond45, label %for.cond5.preheader, label %for.cond.cleanup3
66+
67+
for.body8:
68+
%indvars.iv = phi i64 [ 1, %for.cond5.preheader ], [ %indvars.iv.next, %for.body8 ]
69+
%arrayidx12 = getelementptr inbounds nuw [16 x [16 x [16 x i32]]], ptr @a, i64 0, i64 %0, i64 %indvars.iv, i64 %indvars.iv41
70+
%2 = load i32, ptr %arrayidx12, align 4
71+
%indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
72+
%arrayidx20 = getelementptr inbounds [16 x [16 x [16 x i32]]], ptr @a, i64 0, i64 %indvars.iv46, i64 %indvars.iv.next, i64 %1
73+
%3 = load i32, ptr %arrayidx20, align 4
74+
%sub21 = sub nsw i32 %3, %2
75+
store i32 %sub21, ptr %arrayidx20, align 4
76+
%exitcond = icmp ne i64 %indvars.iv.next, 8
77+
br i1 %exitcond, label %for.body8, label %for.cond.cleanup7
78+
79+
for.cond.cleanup:
80+
ret void
81+
}

0 commit comments

Comments
 (0)