Skip to content

Commit 8a115b6

Browse files
committed
[LV] Mark checks as never succeeding for high cost cutoff.
When GeneratedRTChecks::create bails out due to exceeding the cost threshold, no runtime checks are generated and we must not proceed assuming checks have been generated. Mark the checks as never succeeding, to make sure we don't try to vectorize assuming the runtime checks hold. This fixes a case where we previously incorrectly vectorized assuming runtime checks had been generated when forcing vectorization via metadate. Fixes the mis-compile mentioned in #166247 (comment)
1 parent 06f0758 commit 8a115b6

File tree

2 files changed

+19
-26
lines changed

2 files changed

+19
-26
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1827,8 +1827,12 @@ class GeneratedRTChecks {
18271827
// profile info.
18281828
CostTooHigh =
18291829
LAI.getNumRuntimePointerChecks() > VectorizeMemoryCheckThreshold;
1830-
if (CostTooHigh)
1830+
if (CostTooHigh) {
1831+
// Mark runtime checks as never succeeding when they exceed the threshold.
1832+
MemRuntimeCheckCond = ConstantInt::getTrue(L->getHeader()->getContext());
1833+
SCEVCheckCond = ConstantInt::getTrue(L->getHeader()->getContext());
18311834
return;
1835+
}
18321836

18331837
BasicBlock *LoopHeader = L->getHeader();
18341838
BasicBlock *Preheader = L->getLoopPreheader();

llvm/test/Transforms/LoopVectorize/runtime-check-threshold-with-force-metadata.ll

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,23 @@
22
; RUN: opt -p loop-vectorize -vectorize-memory-check-threshold=0 -S %s | FileCheck --check-prefix=LIMIT0 %s
33
; RUN: opt -p loop-vectorize -vectorize-memory-check-threshold=1 -S %s | FileCheck --check-prefix=LIMIT1 %s
44

5-
; FIXME: Currently this miscompiles with -vectorize-memory-check-threshold=0;
6-
; no runtime check is generated even though one is needed and !noalias
7-
; annotations are added.
5+
; Make sure we do not incorrectly vectorize with -vectorize-memory-check-threshold=0;
6+
; no runtime check is generated and the loop should not be vectorized.
87
define i16 @runtime_checks_needed(ptr %src, ptr %dst) {
98
; LIMIT0-LABEL: define i16 @runtime_checks_needed(
109
; LIMIT0-SAME: ptr [[SRC:%.*]], ptr [[DST:%.*]]) {
11-
; LIMIT0-NEXT: [[ENTRY:.*:]]
12-
; LIMIT0-NEXT: br label %[[VECTOR_PH:.*]]
13-
; LIMIT0: [[VECTOR_PH]]:
14-
; LIMIT0-NEXT: [[TMP0:%.*]] = load i16, ptr [[SRC]], align 1, !alias.scope [[META0:![0-9]+]]
15-
; LIMIT0-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <2 x i16> poison, i16 [[TMP0]], i64 0
16-
; LIMIT0-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <2 x i16> [[BROADCAST_SPLATINSERT]], <2 x i16> poison, <2 x i32> zeroinitializer
17-
; LIMIT0-NEXT: br label %[[VECTOR_BODY:.*]]
18-
; LIMIT0: [[VECTOR_BODY]]:
19-
; LIMIT0-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
10+
; LIMIT0-NEXT: [[ENTRY:.*]]:
11+
; LIMIT0-NEXT: br label %[[LOOP:.*]]
12+
; LIMIT0: [[LOOP]]:
13+
; LIMIT0-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[ENTRY]] ], [ [[INDEX_NEXT:%.*]], %[[LOOP]] ]
14+
; LIMIT0-NEXT: [[L:%.*]] = load i16, ptr [[SRC]], align 1
2015
; LIMIT0-NEXT: [[TMP1:%.*]] = getelementptr inbounds i16, ptr [[DST]], i64 [[INDEX]]
21-
; LIMIT0-NEXT: store <2 x i16> [[BROADCAST_SPLAT]], ptr [[TMP1]], align 1, !alias.scope [[META3:![0-9]+]], !noalias [[META0]]
22-
; LIMIT0-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 2
16+
; LIMIT0-NEXT: store i16 [[L]], ptr [[TMP1]], align 1
17+
; LIMIT0-NEXT: [[INDEX_NEXT]] = add nuw nsw i64 [[INDEX]], 1
2318
; LIMIT0-NEXT: [[TMP2:%.*]] = icmp eq i64 [[INDEX_NEXT]], 1000
24-
; LIMIT0-NEXT: br i1 [[TMP2]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP5:![0-9]+]]
25-
; LIMIT0: [[MIDDLE_BLOCK]]:
26-
; LIMIT0-NEXT: br label %[[EXIT:.*]]
19+
; LIMIT0-NEXT: br i1 [[TMP2]], label %[[EXIT:.*]], label %[[LOOP]], !llvm.loop [[LOOP0:![0-9]+]]
2720
; LIMIT0: [[EXIT]]:
21+
; LIMIT0-NEXT: [[TMP0:%.*]] = phi i16 [ [[L]], %[[LOOP]] ]
2822
; LIMIT0-NEXT: ret i16 [[TMP0]]
2923
;
3024
; LIMIT1-LABEL: define i16 @runtime_checks_needed(
@@ -88,14 +82,9 @@ exit:
8882
!3 = !{!"llvm.loop.vectorize.enable", i1 true}
8983

9084
;.
91-
; LIMIT0: [[META0]] = !{[[META1:![0-9]+]]}
92-
; LIMIT0: [[META1]] = distinct !{[[META1]], [[META2:![0-9]+]]}
93-
; LIMIT0: [[META2]] = distinct !{[[META2]], !"LVerDomain"}
94-
; LIMIT0: [[META3]] = !{[[META4:![0-9]+]]}
95-
; LIMIT0: [[META4]] = distinct !{[[META4]], [[META2]]}
96-
; LIMIT0: [[LOOP5]] = distinct !{[[LOOP5]], [[META6:![0-9]+]], [[META7:![0-9]+]]}
97-
; LIMIT0: [[META6]] = !{!"llvm.loop.isvectorized", i32 1}
98-
; LIMIT0: [[META7]] = !{!"llvm.loop.unroll.runtime.disable"}
85+
; LIMIT0: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]], [[META2:![0-9]+]]}
86+
; LIMIT0: [[META1]] = !{!"llvm.loop.vectorize.width", i32 2}
87+
; LIMIT0: [[META2]] = !{!"llvm.loop.vectorize.enable", i1 true}
9988
;.
10089
; LIMIT1: [[META0]] = !{[[META1:![0-9]+]]}
10190
; LIMIT1: [[META1]] = distinct !{[[META1]], [[META2:![0-9]+]]}

0 commit comments

Comments
 (0)