Skip to content

Commit 855e02e

Browse files
[SVE] Fix invalid usage of getNumElements() in InstCombineMulDivRem
Summary: getLogBase2 tries to iterate over the number of vector elements. Since the number of elements of a scalable vector is unknown at compile time, we must return null if the input type is scalable. Identified by test LLVM.Transforms/InstCombine::nsw.ll Reviewers: efriedma, fpetrogalli, kmclaughlin, spatel Reviewed By: efriedma, fpetrogalli Subscribers: tschuett, hiraditya, rkruppe, psnobl, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D79197
1 parent 645ad5b commit 855e02e

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,21 @@ static Value *simplifyValueKnownNonZero(Value *V, InstCombiner &IC,
9696

9797
/// A helper routine of InstCombiner::visitMul().
9898
///
99-
/// If C is a scalar/vector of known powers of 2, then this function returns
100-
/// a new scalar/vector obtained from logBase2 of C.
99+
/// If C is a scalar/fixed width vector of known powers of 2, then this
100+
/// function returns a new scalar/fixed width vector obtained from logBase2
101+
/// of C.
101102
/// Return a null pointer otherwise.
102103
static Constant *getLogBase2(Type *Ty, Constant *C) {
103104
const APInt *IVal;
104105
if (match(C, m_APInt(IVal)) && IVal->isPowerOf2())
105106
return ConstantInt::get(Ty, IVal->logBase2());
106107

107-
if (!Ty->isVectorTy())
108+
// FIXME: We can extract pow of 2 of splat constant for scalable vectors.
109+
if (!isa<FixedVectorType>(Ty))
108110
return nullptr;
109111

110112
SmallVector<Constant *, 4> Elts;
111-
for (unsigned I = 0, E = cast<VectorType>(Ty)->getNumElements(); I != E;
113+
for (unsigned I = 0, E = cast<FixedVectorType>(Ty)->getNumElements(); I != E;
112114
++I) {
113115
Constant *Elt = C->getAggregateElement(I);
114116
if (!Elt)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
; RUN: opt -instcombine -S < %s | FileCheck %s
2+
3+
; This vscale udiv with a power-of-2 spalt on the rhs should not crash opt
4+
5+
; CHECK: define <vscale x 2 x i32> @udiv_pow2_vscale(<vscale x 2 x i32> %lhs)
6+
define <vscale x 2 x i32> @udiv_pow2_vscale(<vscale x 2 x i32> %lhs) {
7+
%splatter = insertelement <vscale x 2 x i32> undef, i32 2, i32 0
8+
%rhs = shufflevector <vscale x 2 x i32> %splatter,
9+
<vscale x 2 x i32> undef,
10+
<vscale x 2 x i32> zeroinitializer
11+
%res = udiv <vscale x 2 x i32> %lhs, %rhs
12+
ret <vscale x 2 x i32> %res
13+
}
14+
15+
; This fixed width udiv with a power-of-2 splat on the rhs should also not
16+
; crash, and instcombine should eliminate the udiv
17+
18+
; CHECK-LABEL: define <2 x i32> @udiv_pow2_fixed(<2 x i32> %lhs)
19+
; CHECK-NOT: udiv
20+
define <2 x i32> @udiv_pow2_fixed(<2 x i32> %lhs) {
21+
%splatter = insertelement <2 x i32> undef, i32 2, i32 0
22+
%rhs = shufflevector <2 x i32> %splatter,
23+
<2 x i32> undef,
24+
<2 x i32> zeroinitializer
25+
%res = udiv <2 x i32> %lhs, %rhs
26+
ret <2 x i32> %res
27+
}

0 commit comments

Comments
 (0)