Skip to content

Commit f572a59

Browse files
committed
[VectorCombine] Ensure canScalarizeAccess handles cases where the index type can't represent all inbounds values
Fixes #132563
1 parent 57530c2 commit f572a59

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1438,14 +1438,18 @@ static ScalarizationResult canScalarizeAccess(VectorType *VecTy, Value *Idx,
14381438
// This is the number of elements of fixed vector types,
14391439
// or the minimum number of elements of scalable vector types.
14401440
uint64_t NumElements = VecTy->getElementCount().getKnownMinValue();
1441+
unsigned IntWidth = Idx->getType()->getScalarSizeInBits();
14411442

14421443
if (auto *C = dyn_cast<ConstantInt>(Idx)) {
14431444
if (C->getValue().ult(NumElements))
14441445
return ScalarizationResult::safe();
14451446
return ScalarizationResult::unsafe();
14461447
}
14471448

1448-
unsigned IntWidth = Idx->getType()->getScalarSizeInBits();
1449+
// Always unsafe if the index type can't handle all inbound values.
1450+
if (!llvm::isUIntN(IntWidth, NumElements))
1451+
return ScalarizationResult::unsafe();
1452+
14491453
APInt Zero(IntWidth, 0);
14501454
APInt MaxElts(IntWidth, NumElements);
14511455
ConstantRange ValidIndices(Zero, MaxElts);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt -passes=vector-combine -S %s | FileCheck %s
3+
4+
; Ensure canScalarizeAccess handles cases where the index type can't represent all inbounds values
5+
6+
define void @src_1_idx(ptr %q, i8 zeroext %s, i1 %idx) {
7+
; CHECK-LABEL: @src_1_idx(
8+
; CHECK-NEXT: [[LD:%.*]] = load <16 x i8>, ptr [[Q:%.*]], align 16
9+
; CHECK-NEXT: [[V1:%.*]] = insertelement <16 x i8> [[LD]], i8 [[S:%.*]], i1 [[IDX:%.*]]
10+
; CHECK-NEXT: store <16 x i8> [[V1]], ptr [[Q]], align 16
11+
; CHECK-NEXT: ret void
12+
;
13+
%ld = load <16 x i8>, ptr %q
14+
%v1 = insertelement <16 x i8> %ld, i8 %s, i1 %idx
15+
store <16 x i8> %v1, ptr %q
16+
ret void
17+
}
18+
19+
define void @src_2_idx(ptr %q, i8 zeroext %s, i8 %idx) {
20+
; CHECK-LABEL: @src_2_idx(
21+
; CHECK-NEXT: [[LD:%.*]] = load <256 x i8>, ptr [[Q:%.*]], align 256
22+
; CHECK-NEXT: [[V1:%.*]] = insertelement <256 x i8> [[LD]], i8 [[S:%.*]], i8 [[IDX:%.*]]
23+
; CHECK-NEXT: store <256 x i8> [[V1]], ptr [[Q]], align 256
24+
; CHECK-NEXT: ret void
25+
;
26+
%ld = load <256 x i8>, ptr %q
27+
%v1 = insertelement <256 x i8> %ld, i8 %s, i8 %idx
28+
store <256 x i8> %v1, ptr %q
29+
ret void
30+
}

0 commit comments

Comments
 (0)