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

Nashe Mncube via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 17 03:00:10 PDT 2021


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

Response to Carol and Sander's comments. I have gone for an
approach which implements an optimisation for the case
where we have a trunc op of the following form

fptrunc (OpI (fpextend v) (fpextend u))

and in the case where one or both of u and v
are scalable splat vectors, we truncate to the lowest
common FP width which maintains precision. I have also
implemented a test which showcases this optimisation.


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/instcombine-vectors.ll


Index: llvm/test/Transforms/InstCombine/AArch64/instcombine-vectors.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/InstCombine/AArch64/instcombine-vectors.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,24 +1575,64 @@
 
   Type *MinType = nullptr;
 
-  unsigned NumElts = cast<FixedVectorType>(CVVTy)->getNumElements();
-  for (unsigned i = 0; i != NumElts; ++i) {
-    auto *CFP = dyn_cast_or_null<ConstantFP>(CV->getAggregateElement(i));
+  auto EC = cast<VectorType>(CVVTy)->getElementCount();
+
+  auto getMinTypeInConstant = [](Constant *constant, Type *MinType) -> Type * {
+    auto *CFP = dyn_cast_or_null<ConstantFP>(constant);
     if (!CFP)
       return nullptr;
 
-    Type *T = shrinkFPConstant(CFP);
+    auto *T = shrinkFPConstant(CFP);
     if (!T)
       return nullptr;
 
     // If we haven't found a type yet or this type has a larger mantissa than
     // our previous type, this is our new minimal type.
     if (!MinType || T->getFPMantissaWidth() > MinType->getFPMantissaWidth())
+      return T;
+
+    return MinType;
+  };
+
+  // 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()) {
+    if (CV->getSplatValue()) {
+      if (auto *T = getMinTypeInConstant(CV->getSplatValue(), MinType))
+        return VectorType::get(T, EC);
+      return nullptr;
+    }
+
+    // scalable splat vectors can be nested within an instruction, and as such
+    // we must search instruction operands for the minimum type i.e. for an
+    // fpext we may have the following: <vscale x n x ty> fpext (<vscale x n x
+    // ty2> ... to <vscale x n x ty>) we must search the first operand for the
+    // minimum type.
+    for (unsigned i = 0; i < CV->getNumOperands(); i++) {
+      auto *CV2 = dyn_cast<Constant>(CV->getOperand(i));
+      if (!CV2 || !CV2->getSplatValue())
+        return nullptr;
+
+      if (auto *T = getMinTypeInConstant(CV2->getSplatValue(), MinType))
+        MinType = T;
+      else
+        return nullptr;
+    }
+
+    return VectorType::get(MinType, EC);
+  }
+
+  // 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) {
+    if (auto *T = getMinTypeInConstant(CV->getAggregateElement(i), MinType))
       MinType = T;
+    else
+      return nullptr;
   }
 
-  // Make a vector type from the minimal type.
-  return FixedVectorType::get(MinType, NumElts);
+  return VectorType::get(MinType, EC);
 }
 
 /// Find the minimum FP type we can safely truncate to.


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


More information about the llvm-commits mailing list