[llvm] 5d92979 - [llvm-opt] Bug fix within combining FP vectors

Nashe Mncube via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 23 05:13:58 PDT 2021


Author: Nashe Mncube
Date: 2021-03-23T12:13:41Z
New Revision: 5d929794a87602cfd873381e11cc99149196bb49

URL: https://github.com/llvm/llvm-project/commit/5d929794a87602cfd873381e11cc99149196bb49
DIFF: https://github.com/llvm/llvm-project/commit/5d929794a87602cfd873381e11cc99149196bb49.diff

LOG: [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

Added: 
    llvm/test/Transforms/InstCombine/AArch64/sve-const-fp-splat.ll

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
index 75621da20a5dd..d24428e05ebfc 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -1569,13 +1569,16 @@ static Type *shrinkFPConstant(ConstantFP *CFP) {
 // TODO: Make these support undef elements.
 static Type *shrinkFPConstantVector(Value *V) {
   auto *CV = dyn_cast<Constant>(V);
-  auto *CVVTy = dyn_cast<VectorType>(V->getType());
+  auto *CVVTy = dyn_cast<FixedVectorType>(V->getType());
   if (!CV || !CVVTy)
     return nullptr;
 
   Type *MinType = nullptr;
 
-  unsigned NumElts = cast<FixedVectorType>(CVVTy)->getNumElements();
+  unsigned NumElts = CVVTy->getNumElements();
+
+  // For fixed-width vectors we find the minimal type by looking
+  // through the constant values of the vector.
   for (unsigned i = 0; i != NumElts; ++i) {
     auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i));
     if (!CFP)
@@ -1607,7 +1610,15 @@ static Type *getMinimumFPType(Value *V) {
     if (Type *T = shrinkFPConstant(CFP))
       return T;
 
-  // Try to shrink a vector of FP constants.
+  // We can only correctly find a minimum type for a scalable vector when it is
+  // a splat. For splats of constant values the fpext is wrapped up as a
+  // ConstantExpr.
+  if (auto *FPCExt = dyn_cast<ConstantExpr>(V))
+    if (FPCExt->getOpcode() == Instruction::FPExt)
+      return FPCExt->getOperand(0)->getType();
+
+  // Try to shrink a vector of FP constants. This returns nullptr on scalable
+  // vectors
   if (Type *T = shrinkFPConstantVector(V))
     return T;
 

diff  --git a/llvm/test/Transforms/InstCombine/AArch64/sve-const-fp-splat.ll b/llvm/test/Transforms/InstCombine/AArch64/sve-const-fp-splat.ll
new file mode 100644
index 0000000000000..a944be4ee70f1
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/AArch64/sve-const-fp-splat.ll
@@ -0,0 +1,17 @@
+; RUN: opt -instcombine -mtriple aarch64-linux-gnu -mattr=+sve -S -o - < %s 2>%t | FileCheck %s
+; RUN: FileCheck --check-prefix=WARN --allow-empty %s <%t
+
+; If this check fails please read test/CodeGen/AArch64/README for instructions on how to resolve it.
+; WARN-NOT: warning
+
+define <vscale x 2 x float> @shrink_splat_scalable_extend(<vscale x 2 x float> %a) {
+  ; CHECK-LABEL: @shrink_splat_scalable_extend
+  ; 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)
+  ; CHECK-NEXT:  ret <vscale x 2 x float> %[[FADD]]
+  %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
+  %2 = fpext <vscale x 2 x float> %a to <vscale x 2 x double>
+  %3 = fpext <vscale x 2 x float> %1 to <vscale x 2 x double>
+  %4 = fadd <vscale x 2 x double> %2, %3
+  %5 = fptrunc <vscale x 2 x double> %4 to <vscale x 2 x float>
+  ret <vscale x 2 x float> %5
+}


        


More information about the llvm-commits mailing list