[PATCH] D98351: [llvm-opt] Bug fix within combining FP vectors

Nashe Mncube via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 18 09:55:31 PDT 2021


nasherm updated this revision to Diff 331596.
nasherm added a comment.

In response to Sander and David's approach, I have
refactored the solution to use the simpler check
for splat vectors i.e. checking with constant
fpext expressions


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D98351/new/

https://reviews.llvm.org/D98351

Files:
  llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
  llvm/test/Transforms/InstCombine/AArch64/sve-splat.ll


Index: llvm/test/Transforms/InstCombine/AArch64/sve-splat.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InstCombine/AArch64/sve-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:  %1 = 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> %1
+  %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
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
@@ -1575,13 +1575,22 @@
 
   Type *MinType = nullptr;
 
-  unsigned NumElts = cast<FixedVectorType>(CVVTy)->getNumElements();
-  for (unsigned i = 0; i != NumElts; ++i) {
+  auto EC = cast<VectorType>(CVVTy)->getElementCount();
+
+  // We only can correctly find a MinType for a ScalableVector if the vector
+  // is a splat-vector, otherwise we can't shrink due to the runtime-defined
+  // vscale value.
+  if (EC.isScalable())
+    return nullptr;
+
+  // For fixed-width vectors we find the min-type by looking
+  // through the constant values of the vector.
+  for (unsigned i = 0; i != EC.getFixedValue(); ++i) {
     auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i));
     if (!CFP)
       return nullptr;
 
-    Type *T = shrinkFPConstant(CFP);
+    auto *T = shrinkFPConstant(CFP);
     if (!T)
       return nullptr;
 
@@ -1591,8 +1600,7 @@
       MinType = T;
   }
 
-  // Make a vector type from the minimal type.
-  return FixedVectorType::get(MinType, NumElts);
+  return FixedVectorType::get(MinType, EC.getFixedValue());
 }
 
 /// Find the minimum FP type we can safely truncate to.
@@ -1607,7 +1615,15 @@
     if (Type *T = shrinkFPConstant(CFP))
       return T;
 
-  // Try to shrink a vector of FP constants.
+  // We only can correctly find a MinType for a ScalableVector if the vector
+  // is a splat-vector, otherwise we can't shrink due to the runtime-defined
+  // vscale value. Vectors of this type will be wrapped in a fpext constant.
+  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;
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D98351.331596.patch
Type: text/x-patch
Size: 3381 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210318/ca5f0ada/attachment.bin>


More information about the llvm-commits mailing list