Open junweizeng opened 1 year ago
The code below is another reduced code that only crashes on clang-trunk
.
int a[];
int b, c, e, f, g;
void h() {
int d[][5] = {1, 5, 5, 5, 10, 10, 5};
for (; e < 2;) {
for (; e < 5; e++)
b = 5;
g = 4;
for (; g; g--)
if (d[g][g])
;
else
for (;;)
;
c = 0;
for (; c <= 7; c++) {
f = 7;
for (; f >= 2; f--)
for (int i = 0; i < 6; i++)
for (int j = 0; j < 7; j++) {
b = b & 5 ^ 5;
b = 8 ^ a[b];
b = a[b & 15];
}
}
}
}
Compiler explorer: https://godbolt.org/z/ozsaGKrnq
llvm-reduce IR:
@b = external global i32
@__const.i.f = constant [2 x [5 x i32]] [[5 x i32] [i32 1, i32 5, i32 5, i32 5, i32 10], [5 x i32] [i32 10, i32 5, i32 0, i32 0, i32 0]]
@c = external global i32
@d = external global i32
@a = external global [1 x i32]
define void @h() {
entry:
%0 = load i32, ptr @b, align 4
%idxprom = sext i32 %0 to i64
%arrayidx = getelementptr [0 x i32], ptr @a, i64 0, i64 %idxprom
%1 = load i32, ptr %arrayidx, align 4
%idxprom4 = sext i32 %1 to i64
%arrayidx5 = getelementptr [0 x i32], ptr @a, i64 0, i64 %idxprom4
%2 = load i32, ptr %arrayidx5, align 4
store i32 %2, ptr @b, align 4
ret void
}
define void @i(i1 %tobool) {
entry:
br label %for.cond
for.cond: ; preds = %for.cond8, %entry
br i1 %tobool, label %for.body, label %common.ret
for.body: ; preds = %for.cond
call void @h()
br label %for.cond1
for.cond1: ; preds = %for.body3, %for.body
%tobool2.not = phi i1 [ false, %for.body ], [ true, %for.body3 ]
%0 = phi i32 [ 4, %for.body ], [ 0, %for.body3 ]
br i1 %tobool2.not, label %for.cond8, label %for.body3
for.body3: ; preds = %for.cond1
%idxprom = zext i32 %0 to i64
%arrayidx = getelementptr [2 x [5 x i32]], ptr @__const.i.f, i64 0, i64 %idxprom
%1 = load i32, ptr %arrayidx, align 4
%tobool6.not = icmp eq i32 %1, 0
br i1 %tobool6.not, label %common.ret, label %for.cond1
common.ret: ; preds = %for.body3, %for.cond
ret void
for.cond8: ; preds = %for.inc27, %for.cond1
%storemerge = phi i32 [ %inc28, %for.inc27 ], [ 0, %for.cond1 ]
%2 = phi i32 [ %7, %for.inc27 ], [ 0, %for.cond1 ]
store i32 %storemerge, ptr @c, align 4
%cmp = icmp slt i32 %2, 8
br i1 %cmp, label %for.cond10, label %for.cond
for.cond10: ; preds = %for.inc24, %for.cond8
%storemerge1 = phi i32 [ %dec25, %for.inc24 ], [ 7, %for.cond8 ]
%3 = phi i32 [ %6, %for.inc24 ], [ 0, %for.cond8 ]
store i32 %storemerge1, ptr @d, align 4
%cmp11 = icmp sgt i32 %3, -1
br i1 %cmp11, label %for.cond13, label %for.inc27
for.cond13: ; preds = %for.inc21, %for.cond10
%4 = phi i32 [ %inc22, %for.inc21 ], [ 0, %for.cond10 ]
%cmp14 = icmp slt i32 %4, 6
br i1 %cmp14, label %for.cond16, label %for.inc24
for.cond16: ; preds = %for.body18, %for.cond13
%5 = phi i32 [ %inc, %for.body18 ], [ 0, %for.cond13 ]
%cmp17 = icmp slt i32 %5, 7
br i1 %cmp17, label %for.body18, label %for.inc21
for.body18: ; preds = %for.cond16
call void @h()
%inc = add i32 %5, 1
br label %for.cond16
for.inc21: ; preds = %for.cond16
%inc22 = add i32 %4, 1
br label %for.cond13
for.inc24: ; preds = %for.cond13
%6 = load i32, ptr @d, align 4
%dec25 = add i32 %6, -1
br label %for.cond10
for.inc27: ; preds = %for.cond10
%7 = load i32, ptr @c, align 4
%inc28 = add i32 %7, 1
br label %for.cond8
}
Clang at -O2/3 crashes.
When I compiled this code with different optimization levels on different versions of clang, I found that
-O2
caused ICE starting fromclang-13.0.0
, while-O3
caused ICE starting fromclang-15.0.0
.Compiler explorer: https://godbolt.org/z/s9rWe464P