Skip to content

Commit

Permalink
[Lint] Fix crash for insert/extract on scalable vector
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikic committed Sep 4, 2024
1 parent 5a6926c commit 360f82f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 18 deletions.
17 changes: 9 additions & 8 deletions llvm/lib/Analysis/Lint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -590,19 +590,20 @@ void Lint::visitIndirectBrInst(IndirectBrInst &I) {

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

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

void Lint::visitUnreachableInst(UnreachableInst &I) {
Expand Down
25 changes: 15 additions & 10 deletions llvm/test/Analysis/Lint/scalable.ll
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -S -passes=lint < %s | FileCheck %s
; RUN: opt -S -passes=lint -disable-output < %s 2>&1 | FileCheck %s --allow-empty

; Make sure we don't crash.

define <vscale x 8 x i8> @test() {
; CHECK-LABEL: define <vscale x 8 x i8> @test() {
; CHECK-NEXT: [[A:%.*]] = alloca <vscale x 8 x i8>, align 8
; CHECK-NEXT: [[V:%.*]] = load <vscale x 8 x i8>, ptr [[A]], align 8
; CHECK-NEXT: ret <vscale x 8 x i8> [[V]]
;
; CHECK-NOT: Buffer overflow
define <vscale x 8 x i8> @alloca_access() {
%a = alloca <vscale x 8 x i8>
%v = load <vscale x 8 x i8>, ptr %a
ret <vscale x 8 x i8> %v
}

; CHECK-NOT: insertelement index out of range
define <vscale x 8 x half> @insertelement() {
%insert = insertelement <vscale x 8 x half> poison, half 0xH0000, i64 100
ret <vscale x 8 x half> %insert
}

; CHECK-NOT: extract index out of range
define half @extractelement(<vscale x 8 x half> %v) {
%insert = extractelement <vscale x 8 x half> %v, i64 100
ret half %insert
}

0 comments on commit 360f82f

Please sign in to comment.