Skip to content

Commit 07f0e75

Browse files
authored
[LoopVectorize] Fix bug with code to hoist runtime checks (#70937)
There was a silly mistake in the expandBounds function that was using the wrong type when calling expandCodeFor and always assuming the stride is 64 bits. I've added the following test to defend this fix: Transforms/LoopVectorize/ARM/mve-hoist-runtime-checks.ll
1 parent 41cf94e commit 07f0e75

File tree

2 files changed

+134
-1
lines changed

2 files changed

+134
-1
lines changed

llvm/lib/Transforms/Utils/LoopUtils.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1690,7 +1690,7 @@ static PointerBounds expandBounds(const RuntimeCheckingPtrGroup *CG,
16901690
End = Builder.CreateFreeze(End, End->getName() + ".fr");
16911691
}
16921692
Value *StrideVal =
1693-
Stride ? Exp.expandCodeFor(Stride, Type::getInt64Ty(Ctx), Loc) : nullptr;
1693+
Stride ? Exp.expandCodeFor(Stride, Stride->getType(), Loc) : nullptr;
16941694
LLVM_DEBUG(dbgs() << "Start: " << *Low << " End: " << *High << "\n");
16951695
return {Start, End, StrideVal};
16961696
}
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
2+
; REQUIRES: asserts
3+
; RUN: opt < %s -hoist-runtime-checks -p 'loop-vectorize' -force-vector-interleave=1 -S \
4+
; RUN: -force-vector-width=4 -debug-only=loop-accesses,loop-vectorize,loop-utils 2> %t | FileCheck %s
5+
; RUN: cat %t | FileCheck %s --check-prefix=DEBUG
6+
7+
target datalayout = "e-m:e-p:32:32-Fi8-i64:64-v128:64:128-a:0:32-n32-S64"
8+
target triple = "thumbv8.1m.main-none-unknown-eabi"
9+
10+
; Equivalent example in C:
11+
; void diff_checks(int32_t *dst, int32_t *src, int m, int n) {
12+
; for (int i = 0; i < m; i++) {
13+
; for (int j = 0; j < n; j++) {
14+
; dst[(i * (n + 1)) + j] = src[(i * n) + j];
15+
; }
16+
; }
17+
; }
18+
; NOTE: The strides of the starting address values in the inner loop differ, i.e.
19+
; '(i * (n + 1))' vs '(i * n)'.
20+
21+
; DEBUG-LABEL: LAA: Found a loop in diff_checks:
22+
; DEBUG: LAA: Not creating diff runtime check, since these cannot be hoisted out of the outer loop
23+
; DEBUG: LAA: Adding RT check for range:
24+
; DEBUG-NEXT: LAA: Expanded RT check for range to include outer loop in order to permit hoisting
25+
; DEBUG-NEXT: LAA: ... but need to check stride is positive: (4 + (4 * %n))
26+
; DEBUG-NEXT: Start: %dst End: ((4 * %n) + ((4 + (4 * %n)) * (-1 + %m)) + %dst)
27+
; DEBUG-NEXT: LAA: Adding RT check for range:
28+
; DEBUG-NEXT: LAA: Expanded RT check for range to include outer loop in order to permit hoisting
29+
; DEBUG-NEXT: LAA: ... but need to check stride is positive: (4 * %n)
30+
; DEBUG-NEXT: Start: %src End: ((4 * %m * %n) + %src)
31+
32+
define void @diff_checks(ptr nocapture noundef writeonly %dst, ptr nocapture noundef readonly %src, i32 noundef %m, i32 noundef %n) #0 {
33+
; CHECK-LABEL: define void @diff_checks
34+
; CHECK-SAME: (ptr nocapture noundef writeonly [[DST:%.*]], ptr nocapture noundef readonly [[SRC:%.*]], i32 noundef [[M:%.*]], i32 noundef [[N:%.*]]) #[[ATTR0:[0-9]+]] {
35+
; CHECK-NEXT: entry:
36+
; CHECK-NEXT: [[ADD5:%.*]] = add nsw i32 [[N]], 1
37+
; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[M]], -1
38+
; CHECK-NEXT: [[TMP1:%.*]] = shl i32 [[N]], 2
39+
; CHECK-NEXT: [[TMP2:%.*]] = add i32 [[TMP1]], 4
40+
; CHECK-NEXT: [[TMP3:%.*]] = mul i32 [[TMP0]], [[TMP2]]
41+
; CHECK-NEXT: [[TMP4:%.*]] = add i32 [[TMP3]], [[TMP1]]
42+
; CHECK-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr [[DST]], i32 [[TMP4]]
43+
; CHECK-NEXT: [[TMP5:%.*]] = mul i32 [[N]], [[M]]
44+
; CHECK-NEXT: [[TMP6:%.*]] = shl i32 [[TMP5]], 2
45+
; CHECK-NEXT: [[SCEVGEP1:%.*]] = getelementptr i8, ptr [[SRC]], i32 [[TMP6]]
46+
; CHECK-NEXT: br label [[OUTER_LOOP:%.*]]
47+
; CHECK: outer.loop:
48+
; CHECK-NEXT: [[I_023_US:%.*]] = phi i32 [ [[INC10_US:%.*]], [[INNER_LOOP_EXIT:%.*]] ], [ 0, [[ENTRY:%.*]] ]
49+
; CHECK-NEXT: [[MUL_US:%.*]] = mul nsw i32 [[I_023_US]], [[N]]
50+
; CHECK-NEXT: [[TMP7:%.*]] = getelementptr i32, ptr [[SRC]], i32 [[MUL_US]]
51+
; CHECK-NEXT: [[MUL6_US:%.*]] = mul nsw i32 [[I_023_US]], [[ADD5]]
52+
; CHECK-NEXT: [[TMP8:%.*]] = getelementptr i32, ptr [[DST]], i32 [[MUL6_US]]
53+
; CHECK-NEXT: br i1 false, label [[SCALAR_PH:%.*]], label [[VECTOR_MEMCHECK:%.*]]
54+
; CHECK: vector.memcheck:
55+
; CHECK-NEXT: [[BOUND0:%.*]] = icmp ult ptr [[DST]], [[SCEVGEP1]]
56+
; CHECK-NEXT: [[BOUND1:%.*]] = icmp ult ptr [[SRC]], [[SCEVGEP]]
57+
; CHECK-NEXT: [[FOUND_CONFLICT:%.*]] = and i1 [[BOUND0]], [[BOUND1]]
58+
; CHECK-NEXT: [[STRIDE_CHECK:%.*]] = icmp slt i32 [[TMP2]], 0
59+
; CHECK-NEXT: [[TMP9:%.*]] = or i1 [[FOUND_CONFLICT]], [[STRIDE_CHECK]]
60+
; CHECK-NEXT: [[STRIDE_CHECK2:%.*]] = icmp slt i32 [[TMP1]], 0
61+
; CHECK-NEXT: [[TMP10:%.*]] = or i1 [[TMP9]], [[STRIDE_CHECK2]]
62+
; CHECK-NEXT: br i1 [[TMP10]], label [[SCALAR_PH]], label [[VECTOR_PH:%.*]]
63+
; CHECK: vector.ph:
64+
; CHECK-NEXT: [[N_RND_UP:%.*]] = add i32 [[N]], 3
65+
; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i32 [[N_RND_UP]], 4
66+
; CHECK-NEXT: [[N_VEC:%.*]] = sub i32 [[N_RND_UP]], [[N_MOD_VF]]
67+
; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
68+
; CHECK: vector.body:
69+
; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
70+
; CHECK-NEXT: [[TMP11:%.*]] = add i32 [[INDEX]], 0
71+
; CHECK-NEXT: [[ACTIVE_LANE_MASK:%.*]] = call <4 x i1> @llvm.get.active.lane.mask.v4i1.i32(i32 [[TMP11]], i32 [[N]])
72+
; CHECK-NEXT: [[TMP12:%.*]] = getelementptr i32, ptr [[TMP7]], i32 [[TMP11]]
73+
; CHECK-NEXT: [[TMP13:%.*]] = getelementptr i32, ptr [[TMP12]], i32 0
74+
; CHECK-NEXT: [[WIDE_MASKED_LOAD:%.*]] = call <4 x i32> @llvm.masked.load.v4i32.p0(ptr [[TMP13]], i32 4, <4 x i1> [[ACTIVE_LANE_MASK]], <4 x i32> poison), !alias.scope !0
75+
; CHECK-NEXT: [[TMP14:%.*]] = getelementptr i32, ptr [[TMP8]], i32 [[TMP11]]
76+
; CHECK-NEXT: [[TMP15:%.*]] = getelementptr i32, ptr [[TMP14]], i32 0
77+
; CHECK-NEXT: call void @llvm.masked.store.v4i32.p0(<4 x i32> [[WIDE_MASKED_LOAD]], ptr [[TMP15]], i32 4, <4 x i1> [[ACTIVE_LANE_MASK]]), !alias.scope !3, !noalias !0
78+
; CHECK-NEXT: [[INDEX_NEXT]] = add i32 [[INDEX]], 4
79+
; CHECK-NEXT: [[TMP16:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
80+
; CHECK-NEXT: br i1 [[TMP16]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP5:![0-9]+]]
81+
; CHECK: middle.block:
82+
; CHECK-NEXT: br i1 true, label [[INNER_LOOP_EXIT]], label [[SCALAR_PH]]
83+
; CHECK: scalar.ph:
84+
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[OUTER_LOOP]] ], [ 0, [[VECTOR_MEMCHECK]] ]
85+
; CHECK-NEXT: br label [[INNER_LOOP:%.*]]
86+
; CHECK: inner.loop:
87+
; CHECK-NEXT: [[J_021_US:%.*]] = phi i32 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[INC_US:%.*]], [[INNER_LOOP]] ]
88+
; CHECK-NEXT: [[ARRAYIDX_US:%.*]] = getelementptr i32, ptr [[TMP7]], i32 [[J_021_US]]
89+
; CHECK-NEXT: [[TMP17:%.*]] = load i32, ptr [[ARRAYIDX_US]], align 4
90+
; CHECK-NEXT: [[ARRAYIDX8_US:%.*]] = getelementptr i32, ptr [[TMP8]], i32 [[J_021_US]]
91+
; CHECK-NEXT: store i32 [[TMP17]], ptr [[ARRAYIDX8_US]], align 4
92+
; CHECK-NEXT: [[INC_US]] = add nuw nsw i32 [[J_021_US]], 1
93+
; CHECK-NEXT: [[EXITCOND_NOT:%.*]] = icmp eq i32 [[INC_US]], [[N]]
94+
; CHECK-NEXT: br i1 [[EXITCOND_NOT]], label [[INNER_LOOP_EXIT]], label [[INNER_LOOP]], !llvm.loop [[LOOP8:![0-9]+]]
95+
; CHECK: inner.loop.exit:
96+
; CHECK-NEXT: [[INC10_US]] = add nuw nsw i32 [[I_023_US]], 1
97+
; CHECK-NEXT: [[EXITCOND26_NOT:%.*]] = icmp eq i32 [[INC10_US]], [[M]]
98+
; CHECK-NEXT: br i1 [[EXITCOND26_NOT]], label [[EXIT:%.*]], label [[OUTER_LOOP]]
99+
; CHECK: exit:
100+
; CHECK-NEXT: ret void
101+
;
102+
entry:
103+
%add5 = add nsw i32 %n, 1
104+
br label %outer.loop
105+
106+
outer.loop:
107+
%i.023.us = phi i32 [ %inc10.us, %inner.loop.exit ], [ 0, %entry ]
108+
%mul.us = mul nsw i32 %i.023.us, %n
109+
%0 = getelementptr i32, ptr %src, i32 %mul.us
110+
%mul6.us = mul nsw i32 %i.023.us, %add5
111+
%1 = getelementptr i32, ptr %dst, i32 %mul6.us
112+
br label %inner.loop
113+
114+
inner.loop:
115+
%j.021.us = phi i32 [ 0, %outer.loop ], [ %inc.us, %inner.loop ]
116+
%arrayidx.us = getelementptr i32, ptr %0, i32 %j.021.us
117+
%2 = load i32, ptr %arrayidx.us, align 4
118+
%arrayidx8.us = getelementptr i32, ptr %1, i32 %j.021.us
119+
store i32 %2, ptr %arrayidx8.us, align 4
120+
%inc.us = add nuw nsw i32 %j.021.us, 1
121+
%exitcond.not = icmp eq i32 %inc.us, %n
122+
br i1 %exitcond.not, label %inner.loop.exit, label %inner.loop
123+
124+
inner.loop.exit:
125+
%inc10.us = add nuw nsw i32 %i.023.us, 1
126+
%exitcond26.not = icmp eq i32 %inc10.us, %m
127+
br i1 %exitcond26.not, label %exit, label %outer.loop
128+
129+
exit:
130+
ret void
131+
}
132+
133+
attributes #0 = { "target-cpu"="cortex-m55" }

0 commit comments

Comments
 (0)