Skip to content

Commit 5d92979

Browse files
committed
[llvm-opt] Bug fix within combining FP vectors
A bug was found within InstCombineCasts where a function call is only implemented to work with FixedVectors. This caused a crash when a ScalableVector was passed to this function. This commit introduces a regression test which recreates the failure and a bug fix. Differential Revision: https://reviews.llvm.org/D98351
1 parent 2f18e51 commit 5d92979

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,13 +1569,16 @@ static Type *shrinkFPConstant(ConstantFP *CFP) {
15691569
// TODO: Make these support undef elements.
15701570
static Type *shrinkFPConstantVector(Value *V) {
15711571
auto *CV = dyn_cast<Constant>(V);
1572-
auto *CVVTy = dyn_cast<VectorType>(V->getType());
1572+
auto *CVVTy = dyn_cast<FixedVectorType>(V->getType());
15731573
if (!CV || !CVVTy)
15741574
return nullptr;
15751575

15761576
Type *MinType = nullptr;
15771577

1578-
unsigned NumElts = cast<FixedVectorType>(CVVTy)->getNumElements();
1578+
unsigned NumElts = CVVTy->getNumElements();
1579+
1580+
// For fixed-width vectors we find the minimal type by looking
1581+
// through the constant values of the vector.
15791582
for (unsigned i = 0; i != NumElts; ++i) {
15801583
auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i));
15811584
if (!CFP)
@@ -1607,7 +1610,15 @@ static Type *getMinimumFPType(Value *V) {
16071610
if (Type *T = shrinkFPConstant(CFP))
16081611
return T;
16091612

1610-
// Try to shrink a vector of FP constants.
1613+
// We can only correctly find a minimum type for a scalable vector when it is
1614+
// a splat. For splats of constant values the fpext is wrapped up as a
1615+
// ConstantExpr.
1616+
if (auto *FPCExt = dyn_cast<ConstantExpr>(V))
1617+
if (FPCExt->getOpcode() == Instruction::FPExt)
1618+
return FPCExt->getOperand(0)->getType();
1619+
1620+
// Try to shrink a vector of FP constants. This returns nullptr on scalable
1621+
// vectors
16111622
if (Type *T = shrinkFPConstantVector(V))
16121623
return T;
16131624

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
; RUN: opt -instcombine -mtriple aarch64-linux-gnu -mattr=+sve -S -o - < %s 2>%t | FileCheck %s
2+
; RUN: FileCheck --check-prefix=WARN --allow-empty %s <%t
3+
4+
; If this check fails please read test/CodeGen/AArch64/README for instructions on how to resolve it.
5+
; WARN-NOT: warning
6+
7+
define <vscale x 2 x float> @shrink_splat_scalable_extend(<vscale x 2 x float> %a) {
8+
; CHECK-LABEL: @shrink_splat_scalable_extend
9+
; CHECK-NEXT: %[[FADD:.*]] = fadd <vscale x 2 x float> %a, shufflevector (<vscale x 2 x float> insertelement (<vscale x 2 x float> undef, float -1.000000e+00, i32 0), <vscale x 2 x float> undef, <vscale x 2 x i32> zeroinitializer)
10+
; CHECK-NEXT: ret <vscale x 2 x float> %[[FADD]]
11+
%1 = shufflevector <vscale x 2 x float> insertelement (<vscale x 2 x float> undef, float -1.000000e+00, i32 0), <vscale x 2 x float> undef, <vscale x 2 x i32> zeroinitializer
12+
%2 = fpext <vscale x 2 x float> %a to <vscale x 2 x double>
13+
%3 = fpext <vscale x 2 x float> %1 to <vscale x 2 x double>
14+
%4 = fadd <vscale x 2 x double> %2, %3
15+
%5 = fptrunc <vscale x 2 x double> %4 to <vscale x 2 x float>
16+
ret <vscale x 2 x float> %5
17+
}

0 commit comments

Comments
 (0)