Skip to content

Commit fd41700

Browse files
authored
[InstCombine] visitShuffleVectorInst assert with vector of pointers fix. (#152341)
In visitShuffleVectorInst there's an if block that's meant to turn shufflevector followed by bitcast into extractelement where possible. It assumes that there will never be bitcasts performed on vectors of ptr as such operations are almost always illegal, and ptrtoint instructions should be used instead. There is however an edge case where a bitcast instruction can be performed on a vector of type `<1 x ptr>` to turn it into type `ptr` In this edge case, the code initializes the variable `VecBitWidth` to 0. Then, when iterating over users that are bitcasts, an attempt is made to create a vector of size 0, which triggers and assert. This commit changes initialization of `VecBitWidth` to use datalayout to find the the size of the vector instead of getPrimitiveSizeInBits method which results in 0 for ptr and vectors of ptr.
1 parent 7f0e407 commit fd41700

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

llvm/lib/Transforms/InstCombine/InstCombineVectorOps.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3042,7 +3042,7 @@ Instruction *InstCombinerImpl::visitShuffleVectorInst(ShuffleVectorInst &SVI) {
30423042
Value *V = LHS;
30433043
unsigned MaskElems = Mask.size();
30443044
auto *SrcTy = cast<FixedVectorType>(V->getType());
3045-
unsigned VecBitWidth = SrcTy->getPrimitiveSizeInBits().getFixedValue();
3045+
unsigned VecBitWidth = DL.getTypeSizeInBits(SrcTy);
30463046
unsigned SrcElemBitWidth = DL.getTypeSizeInBits(SrcTy->getElementType());
30473047
assert(SrcElemBitWidth && "vector elements must have a bitwidth");
30483048
unsigned SrcNumElems = SrcTy->getNumElements();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
2+
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
3+
4+
; Make sure that we don't crash when optimizing shufflevector of <N x ptr> with <1 x i32> mask followed by bitcast of <1 x ptr> to ptr
5+
6+
define ptr @test(<3 x ptr> %vptr) {
7+
; CHECK-LABEL: define ptr @test(
8+
; CHECK-SAME: <3 x ptr> [[VPTR:%.*]]) {
9+
; CHECK-NEXT: [[SV_EXTRACT:%.*]] = extractelement <3 x ptr> [[VPTR]], i64 0
10+
; CHECK-NEXT: ret ptr [[SV_EXTRACT]]
11+
;
12+
%SV = shufflevector <3 x ptr> %vptr, <3 x ptr> zeroinitializer, <1 x i32> zeroinitializer
13+
%BC = bitcast <1 x ptr> %SV to ptr
14+
ret ptr %BC
15+
}

0 commit comments

Comments
 (0)