Skip to content

Commit 360f82f

Browse files
committed
[Lint] Fix crash for insert/extract on scalable vector
Don't assume the vector is fixed size. For scalable vectors, do not report an error, as indices outside the minimum range may be valid.
1 parent 5a6926c commit 360f82f

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

llvm/lib/Analysis/Lint.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -590,19 +590,20 @@ void Lint::visitIndirectBrInst(IndirectBrInst &I) {
590590

591591
void Lint::visitExtractElementInst(ExtractElementInst &I) {
592592
if (ConstantInt *CI = dyn_cast<ConstantInt>(findValue(I.getIndexOperand(),
593-
/*OffsetOk=*/false)))
594-
Check(
595-
CI->getValue().ult(
596-
cast<FixedVectorType>(I.getVectorOperandType())->getNumElements()),
597-
"Undefined result: extractelement index out of range", &I);
593+
/*OffsetOk=*/false))) {
594+
ElementCount EC = I.getVectorOperandType()->getElementCount();
595+
Check(EC.isScalable() || CI->getValue().ult(EC.getFixedValue()),
596+
"Undefined result: extractelement index out of range", &I);
597+
}
598598
}
599599

600600
void Lint::visitInsertElementInst(InsertElementInst &I) {
601601
if (ConstantInt *CI = dyn_cast<ConstantInt>(findValue(I.getOperand(2),
602-
/*OffsetOk=*/false)))
603-
Check(CI->getValue().ult(
604-
cast<FixedVectorType>(I.getType())->getNumElements()),
602+
/*OffsetOk=*/false))) {
603+
ElementCount EC = I.getType()->getElementCount();
604+
Check(EC.isScalable() || CI->getValue().ult(EC.getFixedValue()),
605605
"Undefined result: insertelement index out of range", &I);
606+
}
606607
}
607608

608609
void Lint::visitUnreachableInst(UnreachableInst &I) {

llvm/test/Analysis/Lint/scalable.ll

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
1-
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2-
; RUN: opt -S -passes=lint < %s | FileCheck %s
1+
; RUN: opt -S -passes=lint -disable-output < %s 2>&1 | FileCheck %s --allow-empty
32

4-
; Make sure we don't crash.
5-
6-
define <vscale x 8 x i8> @test() {
7-
; CHECK-LABEL: define <vscale x 8 x i8> @test() {
8-
; CHECK-NEXT: [[A:%.*]] = alloca <vscale x 8 x i8>, align 8
9-
; CHECK-NEXT: [[V:%.*]] = load <vscale x 8 x i8>, ptr [[A]], align 8
10-
; CHECK-NEXT: ret <vscale x 8 x i8> [[V]]
11-
;
3+
; CHECK-NOT: Buffer overflow
4+
define <vscale x 8 x i8> @alloca_access() {
125
%a = alloca <vscale x 8 x i8>
136
%v = load <vscale x 8 x i8>, ptr %a
147
ret <vscale x 8 x i8> %v
158
}
9+
10+
; CHECK-NOT: insertelement index out of range
11+
define <vscale x 8 x half> @insertelement() {
12+
%insert = insertelement <vscale x 8 x half> poison, half 0xH0000, i64 100
13+
ret <vscale x 8 x half> %insert
14+
}
15+
16+
; CHECK-NOT: extract index out of range
17+
define half @extractelement(<vscale x 8 x half> %v) {
18+
%insert = extractelement <vscale x 8 x half> %v, i64 100
19+
ret half %insert
20+
}

0 commit comments

Comments
 (0)