Skip to content

Commit d1170db

Browse files
author
Sjoerd Meijer
committed
[LV] Emitting SCEV checks with OptForSize
When optimising for size and SCEV runtime checks need to be emitted to check overflow behaviour, the loop vectorizer can run in this assert: LoopVectorize.cpp:2699: void llvm::InnerLoopVectorizer::emitSCEVChecks( llvm::Loop *, llvm::BasicBlock *): Assertion `!BB->getParent()->hasOptSize() && "Cannot SCEV check stride or overflow when opt We should not generate predicates while optimising for size because code will be generated for predicates such as these SCEV overflow runtime checks. This should fix PR43371. Differential Revision: https://reviews.llvm.org/D68082 llvm-svn: 374166
1 parent dde0fe5 commit d1170db

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorizationLegality.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,8 @@ int LoopVectorizationLegality::isConsecutivePtr(Value *Ptr) {
409409
const ValueToValueMap &Strides =
410410
getSymbolicStrides() ? *getSymbolicStrides() : ValueToValueMap();
411411

412-
int Stride = getPtrStride(PSE, Ptr, TheLoop, Strides, true, false);
412+
bool CanAddPredicate = !TheLoop->getHeader()->getParent()->hasOptSize();
413+
int Stride = getPtrStride(PSE, Ptr, TheLoop, Strides, CanAddPredicate, false);
413414
if (Stride == 1 || Stride == -1)
414415
return Stride;
415416
return 0;

llvm/test/Transforms/LoopVectorize/optsize.ll

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,43 @@ for.end: ; preds = %for.body
8484
ret i32 0
8585
}
8686

87+
; PR43371: don't run into an assert due to emitting SCEV runtime checks
88+
; with OptForSize.
89+
;
90+
@cm_array = external global [2592 x i16], align 1
91+
92+
define void @pr43371() optsize {
93+
;
94+
; CHECK-LABEL: @pr43371
95+
; CHECK-NOT: vector.scevcheck
96+
;
97+
; We do not want to generate SCEV predicates when optimising for size, because
98+
; that will lead to extra code generation such as the SCEV overflow runtime
99+
; checks. Not generating SCEV predicates can still result in vectorisation as
100+
; the non-consecutive loads/stores can be scalarized:
101+
;
102+
; CHECK: vector.body:
103+
; CHECK: store i16 0, i16* %{{.*}}, align 1
104+
; CHECK: store i16 0, i16* %{{.*}}, align 1
105+
; CHECK: br i1 {{.*}}, label %vector.body
106+
;
107+
entry:
108+
br label %for.body29
109+
110+
for.cond.cleanup28:
111+
unreachable
112+
113+
for.body29:
114+
%i24.0170 = phi i16 [ 0, %entry], [ %inc37, %for.body29]
115+
%add33 = add i16 undef, %i24.0170
116+
%idxprom34 = zext i16 %add33 to i32
117+
%arrayidx35 = getelementptr [2592 x i16], [2592 x i16] * @cm_array, i32 0, i32 %idxprom34
118+
store i16 0, i16 * %arrayidx35, align 1
119+
%inc37 = add i16 %i24.0170, 1
120+
%cmp26 = icmp ult i16 %inc37, 756
121+
br i1 %cmp26, label %for.body29, label %for.cond.cleanup28
122+
}
123+
87124
!llvm.module.flags = !{!0}
88125
!0 = !{i32 1, !"ProfileSummary", !1}
89126
!1 = !{!2, !3, !4, !5, !6, !7, !8, !9}

0 commit comments

Comments
 (0)