Skip to content

Commit 690f251

Browse files
authored
[LoopInterchange] Handle LE and GE correctly (#124901)
LoopInterchange have converted `DVEntry::LE` and `DVEntry::GE` in direction vectors to '<' and '>' respectively. This handling is incorrect because the information about the '=' it lost. This leads to miscompilation in some cases. To resolve this issue, convert them to '*' instead. Resolve #123920
1 parent 89ca3e7 commit 690f251

File tree

2 files changed

+83
-3
lines changed

2 files changed

+83
-3
lines changed

llvm/lib/Transforms/Scalar/LoopInterchange.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,16 @@ static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
160160
unsigned Levels = D->getLevels();
161161
char Direction;
162162
for (unsigned II = 1; II <= Levels; ++II) {
163+
// `DVEntry::LE` is converted to `*`. This is because `LE` means `<`
164+
// or `=`, for which we don't have an equivalent representation, so
165+
// that the conservative approximation is necessary. The same goes for
166+
// `DVEntry::GE`.
167+
// TODO: Use of fine-grained expressions allows for more accurate
168+
// analysis.
163169
unsigned Dir = D->getDirection(II);
164-
if (Dir == Dependence::DVEntry::LT || Dir == Dependence::DVEntry::LE)
170+
if (Dir == Dependence::DVEntry::LT)
165171
Direction = '<';
166-
else if (Dir == Dependence::DVEntry::GT ||
167-
Dir == Dependence::DVEntry::GE)
172+
else if (Dir == Dependence::DVEntry::GT)
168173
Direction = '>';
169174
else if (Dir == Dependence::DVEntry::EQ)
170175
Direction = '=';
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
; RUN: opt < %s -passes=loop-interchange -pass-remarks-missed='loop-interchange' -pass-remarks-output=%t \
2+
; RUN: -verify-dom-info -verify-loop-info -verify-loop-lcssa
3+
; RUN: FileCheck --input-file=%t %s
4+
5+
;; The original code:
6+
;;
7+
;; #define N 4
8+
;; int a[N*N][N*N][N*N];
9+
;; void f() {
10+
;; for (int i = 0; i < N; i++)
11+
;; for (int j = 1; j < 2*N; j++)
12+
;; for (int k = 1; k < 2*N; k++)
13+
;; a[2*i][k+1][j-1] -= a[i+N-1][k][j];
14+
;; }
15+
;;
16+
;; The entry of the direction vector for the outermost loop is `DVEntry::LE`.
17+
;; We need to treat this as `*`, not `<`. See issue #123920 for details.
18+
19+
; CHECK: --- !Missed
20+
; CHECK-NEXT: Pass: loop-interchange
21+
; CHECK-NEXT: Name: Dependence
22+
; CHECK-NEXT: Function: f
23+
; CHECK: --- !Missed
24+
; CHECK-NEXT: Pass: loop-interchange
25+
; CHECK-NEXT: Name: Dependence
26+
; CHECK-NEXT: Function: f
27+
28+
@a = dso_local global [16 x [16 x [16 x i32]]] zeroinitializer, align 4
29+
30+
define dso_local void @f() {
31+
entry:
32+
br label %for.cond1.preheader
33+
34+
for.cond1.preheader:
35+
%i.039 = phi i32 [ 0, %entry ], [ %inc26, %for.cond.cleanup3 ]
36+
%sub = add nuw nsw i32 %i.039, 3
37+
%idxprom = zext nneg i32 %sub to i64
38+
%mul = shl nuw nsw i32 %i.039, 1
39+
%idxprom13 = zext nneg i32 %mul to i64
40+
br label %for.cond5.preheader
41+
42+
for.cond.cleanup:
43+
ret void
44+
45+
for.cond5.preheader:
46+
%j.038 = phi i32 [ 1, %for.cond1.preheader ], [ %inc23, %for.cond.cleanup7 ]
47+
%idxprom11 = zext nneg i32 %j.038 to i64
48+
%sub18 = add nsw i32 %j.038, -1
49+
%idxprom19 = sext i32 %sub18 to i64
50+
br label %for.body8
51+
52+
for.cond.cleanup3:
53+
%inc26 = add nuw nsw i32 %i.039, 1
54+
%cmp = icmp samesign ult i32 %i.039, 3
55+
br i1 %cmp, label %for.cond1.preheader, label %for.cond.cleanup
56+
57+
for.cond.cleanup7:
58+
%inc23 = add nuw nsw i32 %j.038, 1
59+
%cmp2 = icmp samesign ult i32 %j.038, 7
60+
br i1 %cmp2, label %for.cond5.preheader, label %for.cond.cleanup3
61+
62+
for.body8:
63+
%k.037 = phi i32 [ 1, %for.cond5.preheader ], [ %add15, %for.body8 ]
64+
%idxprom9 = zext nneg i32 %k.037 to i64
65+
%arrayidx12 = getelementptr inbounds nuw [16 x [16 x [16 x i32]]], ptr @a, i64 0, i64 %idxprom, i64 %idxprom9, i64 %idxprom11
66+
%0 = load i32, ptr %arrayidx12, align 4
67+
%add15 = add nuw nsw i32 %k.037, 1
68+
%idxprom16 = zext nneg i32 %add15 to i64
69+
%arrayidx20 = getelementptr inbounds [16 x [16 x [16 x i32]]], ptr @a, i64 0, i64 %idxprom13, i64 %idxprom16, i64 %idxprom19
70+
%1 = load i32, ptr %arrayidx20, align 4
71+
%sub21 = sub nsw i32 %1, %0
72+
store i32 %sub21, ptr %arrayidx20, align 4
73+
%cmp6 = icmp samesign ult i32 %k.037, 7
74+
br i1 %cmp6, label %for.body8, label %for.cond.cleanup7
75+
}

0 commit comments

Comments
 (0)