Skip to content

Commit d95d964

Browse files
committed
Fix crash with scalable shifts by handling scalable vectors in getSafeVectorConstantForBinop
1 parent 689b762 commit d95d964

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

llvm/include/llvm/Transforms/InstCombine/InstCombiner.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
279279
static Constant *
280280
getSafeVectorConstantForBinop(BinaryOperator::BinaryOps Opcode, Constant *In,
281281
bool IsRHSConstant) {
282-
auto *InVTy = cast<FixedVectorType>(In->getType());
282+
auto *InVTy = cast<VectorType>(In->getType());
283283

284284
Type *EltTy = InVTy->getElementType();
285285
auto *SafeC = ConstantExpr::getBinOpIdentity(Opcode, EltTy, IsRHSConstant);
@@ -320,7 +320,13 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
320320
}
321321
}
322322
assert(SafeC && "Must have safe constant for binop");
323-
unsigned NumElts = InVTy->getNumElements();
323+
if (isa<ScalableVectorType>(InVTy)) {
324+
if (isa<UndefValue>(In->getSplatValue()))
325+
return ConstantVector::getSplat(InVTy->getElementCount(), SafeC);
326+
return In;
327+
}
328+
329+
unsigned NumElts = cast<FixedVectorType>(InVTy)->getNumElements();
324330
SmallVector<Constant *, 16> Out(NumElts);
325331
for (unsigned i = 0; i != NumElts; ++i) {
326332
Constant *C = In->getAggregateElement(i);

llvm/test/Transforms/InstCombine/vec_shuffle.ll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,28 @@ define <2 x i32> @shl_splat_constant1(<2 x i32> %x) {
938938
ret <2 x i32> %r
939939
}
940940

941+
define <vscale x 2 x i32> @shl_splat_constant0_scalable(<vscale x 2 x i32> %x) {
942+
; CHECK-LABEL: @shl_splat_constant0_scalable(
943+
; CHECK-NEXT: [[TMP1:%.*]] = shl <vscale x 2 x i32> splat (i32 5), [[X:%.*]]
944+
; CHECK-NEXT: [[R:%.*]] = shufflevector <vscale x 2 x i32> [[TMP1]], <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer
945+
; CHECK-NEXT: ret <vscale x 2 x i32> [[R]]
946+
;
947+
%splat = shufflevector <vscale x 2 x i32> %x, <vscale x 2 x i32> undef, <vscale x 2 x i32> zeroinitializer
948+
%r = shl <vscale x 2 x i32> splat (i32 5), %splat
949+
ret <vscale x 2 x i32> %r
950+
}
951+
952+
define <vscale x 2 x i32> @shl_splat_constant1_scalable(<vscale x 2 x i32> %x) {
953+
; CHECK-LABEL: @shl_splat_constant1_scalable(
954+
; CHECK-NEXT: [[TMP1:%.*]] = shl <vscale x 2 x i32> [[X:%.*]], splat (i32 5)
955+
; CHECK-NEXT: [[R:%.*]] = shufflevector <vscale x 2 x i32> [[TMP1]], <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer
956+
; CHECK-NEXT: ret <vscale x 2 x i32> [[R]]
957+
;
958+
%splat = shufflevector <vscale x 2 x i32> %x, <vscale x 2 x i32> undef, <vscale x 2 x i32> zeroinitializer
959+
%r = shl <vscale x 2 x i32> %splat, splat (i32 5)
960+
ret <vscale x 2 x i32> %r
961+
}
962+
941963
define <2 x i32> @ashr_splat_constant0(<2 x i32> %x) {
942964
; CHECK-LABEL: @ashr_splat_constant0(
943965
; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i32> <i32 5, i32 poison>, [[X:%.*]]

0 commit comments

Comments
 (0)