Skip to content

[mlir] Add ScalableVectorType support class #96236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from

Conversation

MacDue
Copy link
Member

@MacDue MacDue commented Jun 20, 2024

This adds a pseudo-type that wraps a VectorType that aims to provide safe APIs for working with scalable vectors. Slightly contrary to the name, this class can represent both fixed and scalable vectors, however, if you are only dealing with fixed vectors the plain VectorType is likely more convenient.

The main difference from the regular VectorType is that vector dimensions are not represented as int64_t, which does not allow encoding the scalability into the dimension. Instead, vector dimensions are represented by a VectorDim class. A VectorDim stores both the size and scalability of a dimension. This makes common errors like only checking the size (but not the scalability) impossible (without being explicit with your intention).

To make this convenient to work with there is VectorDimList which provides ArrayRef-like helper methods along with an iterator for VectorDims.

ScalableVectorType can freely converted to VectorType (and vice versa), though there are two main ways to acquire a ScalableVectorType.

Assignment:

This does not check the scalability of myVectorType. This is valid and the helpers on ScalableVectorType will function as normal.

VectorType myVectorType = ...;
ScalableVectorType scalableVector = myVectorType;

Casting:

This checks the scalability of myVectorType. In this case, scalableVector will be falsy if myVectorType contains no scalable dims.

VectorType myVectorType = ...;
auto scalableVector = dyn_cast<ScalableVectorType>(myVectorType);

Note: The use of this class is entirely optional! It only aims to make writing scalable-aware patterns safer and easier.

@llvmbot
Copy link
Member

llvmbot commented Jun 20, 2024

@llvm/pr-subscribers-mlir-vector
@llvm/pr-subscribers-mlir
@llvm/pr-subscribers-mlir-math

@llvm/pr-subscribers-mlir-core

Author: Benjamin Maxwell (MacDue)

Changes

This adds a pseudo-type that wraps a VectorType that aims to provide safe APIs for working with scalable vectors. Slightly contrary to the name, this class can represent both fixed and scalable vectors, however, if you are only dealing with fixed vectors the plain VectorType is likely more convenient.

The main difference from the regular VectorType is that vector dimensions are not represented as int64_t, which does not allow encoding the scalability into the dimension. Instead, vector dimensions are represented by a VectorDim class. A VectorDim stores both the size and scalability of a dimension. This makes common errors like only checking the size (but not the scalability) impossible (without being explicit with your intention).

To make this convenient to work with there is VectorDimList which provides ArrayRef-like helper methods along with an iterator for VectorDims.

ScalableVectorType can freely converted to VectorType (and vice versa), though there are two main ways to acquire a ScalableVectorType.

Assignment:

This does not check the scalability of myVectorType. This is valid and the helpers on ScalableVectorType will function as normal.

VectorType myVectorType = ...;
ScalableVectorType scalableVector = myVectorType;

Casting:

This checks the scalability of myVectorType. In this case, scalableVector will be falsy if myVectorType contains no scalable dims.

VectorType myVectorType = ...;
auto scalableVector = dyn_cast&lt;ScalableVectorType&gt;(myVectorType);

Note: The use of this class is entirely optional! It only aims to make writing scalable-aware patterns safer and easier.


Patch is 40.94 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/96236.diff

11 Files Affected:

  • (added) mlir/include/mlir/Support/ScalableVectorType.h (+360)
  • (modified) mlir/lib/Dialect/Math/Transforms/PolynomialApproximation.cpp (+23-34)
  • (modified) mlir/lib/Dialect/Vector/IR/VectorOps.cpp (+22-41)
  • (modified) mlir/lib/Dialect/Vector/Transforms/LowerVectorTransfer.cpp (+8-9)
  • (modified) mlir/lib/Dialect/Vector/Transforms/LowerVectorTranspose.cpp (+6-6)
  • (modified) mlir/lib/Dialect/Vector/Transforms/VectorDropLeadUnitDim.cpp (+8-17)
  • (modified) mlir/lib/Dialect/Vector/Transforms/VectorTransferOpTransforms.cpp (+11-14)
  • (modified) mlir/lib/Dialect/Vector/Transforms/VectorTransforms.cpp (+5-6)
  • (modified) mlir/lib/IR/AsmPrinter.cpp (+4-10)
  • (modified) mlir/unittests/Support/CMakeLists.txt (+2-1)
  • (added) mlir/unittests/Support/ScalableVectorTypeTest.cpp (+76)
diff --git a/mlir/include/mlir/Support/ScalableVectorType.h b/mlir/include/mlir/Support/ScalableVectorType.h
new file mode 100644
index 0000000000000..0fa7716ea2bcb
--- /dev/null
+++ b/mlir/include/mlir/Support/ScalableVectorType.h
@@ -0,0 +1,360 @@
+//===- ScalableVectorType.h - Scalable Vector Helpers -----------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_SUPPORT_SCALABLEVECTORTYPE_H
+#define MLIR_SUPPORT_SCALABLEVECTORTYPE_H
+
+#include "mlir/IR/BuiltinTypes.h"
+#include "mlir/Support/LLVM.h"
+
+namespace mlir {
+
+//===----------------------------------------------------------------------===//
+// VectorDim
+//===----------------------------------------------------------------------===//
+
+/// This class represents a dimension of a vector type. Unlike other ShapedTypes
+/// vector dimensions can have scalable quantities, which means the dimension
+/// has a known minimum size, which is scaled by a constant that is only
+/// known at runtime.
+class VectorDim {
+public:
+  explicit constexpr VectorDim(int64_t quantity, bool scalable)
+      : quantity(quantity), scalable(scalable) {};
+
+  /// Constructs a new fixed dimension.
+  constexpr static VectorDim getFixed(int64_t quantity) {
+    return VectorDim(quantity, false);
+  }
+
+  /// Constructs a new scalable dimension.
+  constexpr static VectorDim getScalable(int64_t quantity) {
+    return VectorDim(quantity, true);
+  }
+
+  /// Returns true if this dimension is scalable;
+  constexpr bool isScalable() const { return scalable; }
+
+  /// Returns true if this dimension is fixed.
+  constexpr bool isFixed() const { return !isScalable(); }
+
+  /// Returns the minimum number of elements this dimension can contain.
+  constexpr int64_t getMinSize() const { return quantity; }
+
+  /// If this dimension is fixed returns the number of elements, otherwise
+  /// aborts.
+  constexpr int64_t getFixedSize() const {
+    assert(isFixed());
+    return quantity;
+  }
+
+  constexpr bool operator==(VectorDim const &dim) const {
+    return quantity == dim.quantity && scalable == dim.scalable;
+  }
+
+  constexpr bool operator!=(VectorDim const &dim) const {
+    return !(*this == dim);
+  }
+
+  /// Print the dim.
+  void print(raw_ostream &os) {
+    if (isScalable())
+      os << '[';
+    os << getMinSize();
+    if (isScalable())
+      os << ']';
+  }
+
+  /// Helper class for indexing into a list of sizes (and possibly empty) list
+  /// of scalable dimensions, extracting VectorDim elements.
+  struct Indexer {
+    explicit Indexer(ArrayRef<int64_t> sizes, ArrayRef<bool> scalableDims)
+        : sizes(sizes), scalableDims(scalableDims) {
+      assert(
+          scalableDims.empty() ||
+          sizes.size() == scalableDims.size() &&
+              "expected `scalableDims` to be empty or match `sizes` in length");
+    }
+
+    VectorDim operator[](size_t idx) const {
+      int64_t size = sizes[idx];
+      bool scalable = scalableDims.empty() ? false : scalableDims[idx];
+      return VectorDim(size, scalable);
+    }
+
+    ArrayRef<int64_t> sizes;
+    ArrayRef<bool> scalableDims;
+  };
+
+private:
+  int64_t quantity;
+  bool scalable;
+};
+
+inline raw_ostream &operator<<(raw_ostream &os, VectorDim dim) {
+  dim.print(os);
+  return os;
+}
+
+//===----------------------------------------------------------------------===//
+// VectorDimList
+//===----------------------------------------------------------------------===//
+
+/// Represents a non-owning list of vector dimensions. The underlying dimension
+/// sizes and scalability flags are stored a two seperate lists to match the
+/// storage of a VectorType.
+class VectorDimList : public VectorDim::Indexer {
+public:
+  using VectorDim::Indexer::Indexer;
+
+  class Iterator : public llvm::iterator_facade_base<
+                       Iterator, std::random_access_iterator_tag, VectorDim,
+                       std::ptrdiff_t, VectorDim, VectorDim> {
+  public:
+    Iterator(VectorDim::Indexer indexer, size_t index)
+        : indexer(indexer), index(index) {};
+
+    // Iterator boilerplate.
+    ptrdiff_t operator-(const Iterator &rhs) const { return index - rhs.index; }
+    bool operator==(const Iterator &rhs) const { return index == rhs.index; }
+    bool operator<(const Iterator &rhs) const { return index < rhs.index; }
+    Iterator &operator+=(ptrdiff_t offset) {
+      index += offset;
+      return *this;
+    }
+    Iterator &operator-=(ptrdiff_t offset) {
+      index -= offset;
+      return *this;
+    }
+    VectorDim operator*() const { return indexer[index]; }
+
+    VectorDim::Indexer getIndexer() const { return indexer; }
+    ptrdiff_t getIndex() const { return index; }
+
+  private:
+    VectorDim::Indexer indexer;
+    ptrdiff_t index;
+  };
+
+  // Generic definitions.
+  using value_type = VectorDim;
+  using iterator = Iterator;
+  using const_iterator = Iterator;
+  using reverse_iterator = std::reverse_iterator<iterator>;
+  using const_reverse_iterator = std::reverse_iterator<const_iterator>;
+  using size_type = size_t;
+  using difference_type = ptrdiff_t;
+
+  /// Construct from iterator pair.
+  VectorDimList(Iterator begin, Iterator end)
+      : VectorDimList(VectorDimList(begin.getIndexer())
+                          .slice(begin.getIndex(), end - begin)) {}
+
+  VectorDimList(VectorDim::Indexer indexer) : VectorDim::Indexer(indexer) {};
+
+  /// Construct from a VectorType.
+  static VectorDimList from(VectorType vectorType) {
+    if (!vectorType)
+      return VectorDimList({}, {});
+    return VectorDimList(vectorType.getShape(), vectorType.getScalableDims());
+  }
+
+  Iterator begin() const { return Iterator(*this, 0); }
+  Iterator end() const { return Iterator(*this, size()); }
+
+  /// Check if the dims are empty.
+  bool empty() const { return sizes.empty(); }
+
+  /// Get the number of dims.
+  size_t size() const { return sizes.size(); }
+
+  /// Return the first dim.
+  VectorDim front() const { return (*this)[0]; }
+
+  /// Return the last dim.
+  VectorDim back() const { return (*this)[size() - 1]; }
+
+  /// Chop of thie first \p n dims, and keep the remaining \p m
+  /// dims.
+  VectorDimList slice(size_t n, size_t m) const {
+    ArrayRef<int64_t> newSizes = sizes.slice(n, m);
+    ArrayRef<bool> newScalableDims =
+        scalableDims.empty() ? ArrayRef<bool>{} : scalableDims.slice(n, m);
+    return VectorDimList(newSizes, newScalableDims);
+  }
+
+  /// Drop the first \p n dims.
+  VectorDimList dropFront(size_t n = 1) const { return slice(n, size() - n); }
+
+  /// Drop the last \p n dims.
+  VectorDimList dropBack(size_t n = 1) const { return slice(0, size() - n); }
+
+  /// Return a copy of *this with only the first \p n elements.
+  VectorDimList takeFront(size_t n = 1) const {
+    if (n >= size())
+      return *this;
+    return dropBack(size() - n);
+  }
+
+  /// Return a copy of *this with only the last \p n elements.
+  VectorDimList takeBack(size_t n = 1) const {
+    if (n >= size())
+      return *this;
+    return dropFront(size() - n);
+  }
+
+  /// Return copy of *this with the first n dims matching the predicate removed.
+  template <class PredicateT>
+  VectorDimList dropWhile(PredicateT predicate) const {
+    return VectorDimList(llvm::find_if_not(*this, predicate), end());
+  }
+
+  /// Returns true if one or more of the dims are scalable.
+  bool hasScalableDims() const {
+    return llvm::is_contained(getScalableDims(), true);
+  }
+
+  /// Check for dim equality.
+  bool equals(VectorDimList rhs) const {
+    if (size() != rhs.size())
+      return false;
+    return std::equal(begin(), end(), rhs.begin());
+  }
+
+  /// Check for dim equality.
+  bool equals(ArrayRef<VectorDim> rhs) const {
+    if (size() != rhs.size())
+      return false;
+    return std::equal(begin(), end(), rhs.begin());
+  }
+
+  /// Return the underlying sizes.
+  ArrayRef<int64_t> getSizes() const { return sizes; }
+
+  /// Return the underlying scalable dims.
+  ArrayRef<bool> getScalableDims() const { return scalableDims; }
+};
+
+inline bool operator==(VectorDimList lhs, VectorDimList rhs) {
+  return lhs.equals(rhs);
+}
+
+inline bool operator!=(VectorDimList lhs, VectorDimList rhs) {
+  return !(lhs == rhs);
+}
+
+inline bool operator==(VectorDimList lhs, ArrayRef<VectorDim> rhs) {
+  return lhs.equals(rhs);
+}
+
+inline bool operator!=(VectorDimList lhs, ArrayRef<VectorDim> rhs) {
+  return !(lhs == rhs);
+}
+
+//===----------------------------------------------------------------------===//
+// ScalableVectorType
+//===----------------------------------------------------------------------===//
+
+/// A pseudo-type that wraps a VectorType that aims to provide safe APIs for
+/// working with scalable vectors. Slightly contrary to the name this class can
+/// represent both fixed and scalable vectors, however, if you only are always
+/// dealing with fixed vectors the plain VectorType is likely more convenient.
+///
+/// The main difference from the regular VectorType is that vector dimensions
+/// are _not_ represented as `int64_t`, which does not allow encoding the
+/// scalability into the dimension. Instead, vector dimensions are represented
+/// by a VectorDim class. A VectorDim stores both the size and scalability of a
+/// dimension. Makes common errors like only checking the size (but not the
+/// scalability) impossible (without being explicit with your intention).
+///
+/// To make this convenient to work with there's VectorDimList provides
+/// ArrayRef-like helper methods along with an iterator for VectorDims.
+///
+/// ScalableVectorType and VectorType can be freely converted between. However,
+/// there is one thing to note:
+///
+/// Assignment from a VectorType always succeeds (scalability is checked):
+/// ```
+/// VectorType someVectorType = ...;
+/// ScalableVectorType vector = someVectorType;
+/// ```
+///
+/// Casting from a Type/VectorType via dyn_cast (or cast) checks scalability:
+/// ```
+/// if (auto scalableVector = dyn_cast<ScalableVectorType>(someVectorType)) {
+///   <vector type has scalable dims>
+/// }
+/// ```
+class ScalableVectorType {
+public:
+  using Dim = VectorDim;
+  using DimList = VectorDimList;
+
+  ScalableVectorType(VectorType vectorType) : vectorType(vectorType) {};
+
+  /// Construct a new ScalableVectorType.
+  static ScalableVectorType get(DimList shape, Type elementType) {
+    return VectorType::get(shape.getSizes(), elementType,
+                           shape.getScalableDims());
+  }
+
+  /// Construct a new ScalableVectorType.
+  static ScalableVectorType get(ArrayRef<Dim> shape, Type elementType) {
+    SmallVector<int64_t> sizes;
+    SmallVector<bool> scalableDims;
+    sizes.reserve(shape.size());
+    scalableDims.reserve(shape.size());
+    for (Dim dim : shape) {
+      sizes.push_back(dim.getMinSize());
+      scalableDims.push_back(dim.isScalable());
+    }
+    return VectorType::get(sizes, elementType, scalableDims);
+  }
+
+  inline static bool classof(Type type) {
+    auto vectorType = dyn_cast_if_present<VectorType>(type);
+    return vectorType && vectorType.isScalable();
+  }
+
+  /// Returns the value of the specified dimension (including scalability).
+  Dim getDim(unsigned idx) const {
+    assert(idx < getRank() && "invalid dim index for vector type");
+    return getDims()[idx];
+  }
+
+  /// Returns the dimensions of this vector type (including scalability).
+  DimList getDims() const {
+    return DimList(vectorType.getShape(), vectorType.getScalableDims());
+  }
+
+  /// Returns the rank of this vector type.
+  int64_t getRank() const { return vectorType.getRank(); }
+
+  /// Returns true if the vector contains scalable dimensions.
+  bool isScalable() const { return vectorType.isScalable(); }
+  bool allDimsScalable() const { return vectorType.allDimsScalable(); }
+
+  /// Returns the element type of this vector type.
+  Type getElementType() const { return vectorType.getElementType(); }
+
+  /// Clones this vector type with a new element type.
+  ScalableVectorType clone(Type elementType) {
+    return vectorType.clone(elementType);
+  }
+
+  operator VectorType() const { return vectorType; }
+
+  explicit operator bool() const { return bool(vectorType); }
+
+private:
+  VectorType vectorType;
+};
+
+} // namespace mlir
+
+#endif
diff --git a/mlir/lib/Dialect/Math/Transforms/PolynomialApproximation.cpp b/mlir/lib/Dialect/Math/Transforms/PolynomialApproximation.cpp
index f4fae68da63b3..7c694ca7d55c8 100644
--- a/mlir/lib/Dialect/Math/Transforms/PolynomialApproximation.cpp
+++ b/mlir/lib/Dialect/Math/Transforms/PolynomialApproximation.cpp
@@ -29,6 +29,7 @@
 #include "mlir/IR/OpDefinition.h"
 #include "mlir/IR/PatternMatch.h"
 #include "mlir/IR/TypeUtilities.h"
+#include "mlir/Support/ScalableVectorType.h"
 #include "mlir/Transforms/DialectConversion.h"
 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
 #include "llvm/ADT/ArrayRef.h"
@@ -39,24 +40,14 @@ using namespace mlir;
 using namespace mlir::math;
 using namespace mlir::vector;
 
-// Helper to encapsulate a vector's shape (including scalable dims).
-struct VectorShape {
-  ArrayRef<int64_t> sizes;
-  ArrayRef<bool> scalableFlags;
-
-  bool empty() const { return sizes.empty(); }
-};
-
 // Returns vector shape if the type is a vector. Returns an empty shape if it is
 // not a vector.
-static VectorShape vectorShape(Type type) {
+static VectorDimList vectorShape(Type type) {
   auto vectorType = dyn_cast<VectorType>(type);
-  return vectorType
-             ? VectorShape{vectorType.getShape(), vectorType.getScalableDims()}
-             : VectorShape{};
+  return VectorDimList::from(vectorType);
 }
 
-static VectorShape vectorShape(Value value) {
+static VectorDimList vectorShape(Value value) {
   return vectorShape(value.getType());
 }
 
@@ -65,16 +56,14 @@ static VectorShape vectorShape(Value value) {
 //----------------------------------------------------------------------------//
 
 // Broadcasts scalar type into vector type (iff shape is non-scalar).
-static Type broadcast(Type type, VectorShape shape) {
+static Type broadcast(Type type, VectorDimList shape) {
   assert(!isa<VectorType>(type) && "must be scalar type");
-  return !shape.empty()
-             ? VectorType::get(shape.sizes, type, shape.scalableFlags)
-             : type;
+  return !shape.empty() ? ScalableVectorType::get(shape, type) : type;
 }
 
 // Broadcasts scalar value into vector (iff shape is non-scalar).
 static Value broadcast(ImplicitLocOpBuilder &builder, Value value,
-                       VectorShape shape) {
+                       VectorDimList shape) {
   assert(!isa<VectorType>(value.getType()) && "must be scalar value");
   auto type = broadcast(value.getType(), shape);
   return !shape.empty() ? builder.create<BroadcastOp>(type, value) : value;
@@ -227,7 +216,7 @@ static Value clamp(ImplicitLocOpBuilder &builder, Value value, Value lowerBound,
 static std::pair<Value, Value> frexp(ImplicitLocOpBuilder &builder, Value arg,
                                      bool isPositive = false) {
   assert(getElementTypeOrSelf(arg).isF32() && "arg must be f32 type");
-  VectorShape shape = vectorShape(arg);
+  VectorDimList shape = vectorShape(arg);
 
   auto bcast = [&](Value value) -> Value {
     return broadcast(builder, value, shape);
@@ -267,7 +256,7 @@ static std::pair<Value, Value> frexp(ImplicitLocOpBuilder &builder, Value arg,
 // Computes exp2 for an i32 argument.
 static Value exp2I32(ImplicitLocOpBuilder &builder, Value arg) {
   assert(getElementTypeOrSelf(arg).isInteger(32) && "arg must be i32 type");
-  VectorShape shape = vectorShape(arg);
+  VectorDimList shape = vectorShape(arg);
 
   auto bcast = [&](Value value) -> Value {
     return broadcast(builder, value, shape);
@@ -293,7 +282,7 @@ Value makePolynomialCalculation(ImplicitLocOpBuilder &builder,
   Type elementType = getElementTypeOrSelf(x);
   assert((elementType.isF32() || elementType.isF16()) &&
          "x must be f32 or f16 type");
-  VectorShape shape = vectorShape(x);
+  VectorDimList shape = vectorShape(x);
 
   if (coeffs.empty())
     return broadcast(builder, floatCst(builder, 0.0f, elementType), shape);
@@ -391,7 +380,7 @@ AtanApproximation::matchAndRewrite(math::AtanOp op,
   if (!getElementTypeOrSelf(operand).isF32())
     return rewriter.notifyMatchFailure(op, "unsupported operand type");
 
-  VectorShape shape = vectorShape(op.getOperand());
+  VectorDimList shape = vectorShape(op.getOperand());
 
   ImplicitLocOpBuilder builder(op->getLoc(), rewriter);
   Value abs = builder.create<math::AbsFOp>(operand);
@@ -490,7 +479,7 @@ Atan2Approximation::matchAndRewrite(math::Atan2Op op,
     return rewriter.notifyMatchFailure(op, "unsupported operand type");
 
   ImplicitLocOpBuilder builder(op->getLoc(), rewriter);
-  VectorShape shape = vectorShape(op.getResult());
+  VectorDimList shape = vectorShape(op.getResult());
 
   // Compute atan in the valid range.
   auto div = builder.create<arith::DivFOp>(y, x);
@@ -556,7 +545,7 @@ TanhApproximation::matchAndRewrite(math::TanhOp op,
   if (!getElementTypeOrSelf(op.getOperand()).isF32())
     return rewriter.notifyMatchFailure(op, "unsupported operand type");
 
-  VectorShape shape = vectorShape(op.getOperand());
+  VectorDimList shape = vectorShape(op.getOperand());
 
   ImplicitLocOpBuilder builder(op->getLoc(), rewriter);
   auto bcast = [&](Value value) -> Value {
@@ -644,7 +633,7 @@ LogApproximationBase<Op>::logMatchAndRewrite(Op op, PatternRewriter &rewriter,
   if (!getElementTypeOrSelf(op.getOperand()).isF32())
     return rewriter.notifyMatchFailure(op, "unsupported operand type");
 
-  VectorShape shape = vectorShape(op.getOperand());
+  VectorDimList shape = vectorShape(op.getOperand());
 
   ImplicitLocOpBuilder builder(op->getLoc(), rewriter);
   auto bcast = [&](Value value) -> Value {
@@ -791,7 +780,7 @@ Log1pApproximation::matchAndRewrite(math::Log1pOp op,
   if (!getElementTypeOrSelf(op.getOperand()).isF32())
     return rewriter.notifyMatchFailure(op, "unsupported operand type");
 
-  VectorShape shape = vectorShape(op.getOperand());
+  VectorDimList shape = vectorShape(op.getOperand());
 
   ImplicitLocOpBuilder builder(op->getLoc(), rewriter);
   auto bcast = [&](Value value) -> Value {
@@ -846,7 +835,7 @@ AsinPolynomialApproximation::matchAndRewrite(math::AsinOp op,
   if (!(elementType.isF32() || elementType.isF16()))
     return rewriter.notifyMatchFailure(op,
                                        "only f32 and f16 type is supported.");
-  VectorShape shape = vectorShape(operand);
+  VectorDimList shape = vectorShape(operand);
 
   ImplicitLocOpBuilder builder(op->getLoc(), rewriter);
   auto bcast = [&](Value value) -> Value {
@@ -910,7 +899,7 @@ AcosPolynomialApproximation::matchAndRewrite(math::AcosOp op,
   if (!(elementType.isF32() || elementType.isF16()))
     return rewriter.notifyMatchFailure(op,
                                        "only f32 and f16 type is supported.");
-  VectorShape shape = vectorShape(operand);
+  VectorDimList shape = vectorShape(operand);
 
   ImplicitLocOpBuilder builder(op->getLoc(), rewriter);
   auto bcast = [&](Value value) -> Value {
@@ -988,7 +977,7 @@ ErfPolynomialApproximation::matchAndRewrite(math::ErfOp op,
   if (!(elementType.isF32() || elementType.isF16()))
     return rewriter.notifyMatchFailure(op,
                                        "only f32 and f16 type is supported.");
-  VectorShape shape = vectorShape(operand);
+  VectorDimList shape = vectorShape(operand);
 
   ImplicitLocOpBuilder builder(op->getLoc(), rewriter);
   auto bcast = [&](Value value) -> Value {
@@ -1097,7 +1086,7 @@ ErfPolynomialApproximation::matchAndRewrite(math::ErfOp op,
 
 namespace {
 
-Value clampWithNormals(ImplicitLocOpBuilder &builder, const VectorShape shape,
+Value clampWithNormals(ImplicitLocOpBuilder &builder, const VectorDimList shape,
                        Value value, float lowerBound, float upperBound) {
   assert(!std::isnan(lowerBound));
   assert(!std::isnan(upperBound));
@@ -1289,7 +1278,7 @@ ExpM1Approximation::matchAndRewrite(math::ExpM1Op op,
   if (!getElementTypeOrSelf(op.getOperand()).isF32())
     return re...
[truncated]

@MacDue
Copy link
Member Author

MacDue commented Jun 20, 2024

This is a follow-up to #74251 and https://discourse.llvm.org/t/rfc-hardening-the-vectortype-api/75646. The main difference is this does not in any way modify the built-in VectorType. This only adds a support class (ScalableVectorType in mlir/Support/ScalableVectorType.h), that aims to make some aspects of working with scalable vectors easier and safer.

The use of this class is entirely optional though (and is probably best only used where it makes things simpler). See the last commit for an idea of how this class could be used.

MacDue added 3 commits June 21, 2024 09:28
This adds a pseudo-type that wraps a VectorType that aims to provide
safe APIs for working with scalable vectors. Slightly contrary to the
name this class can represent both fixed and scalable vectors, however,
if you only are always dealing with fixed vectors the plain VectorType
is likely more convenient.

The main difference from the regular VectorType is that vector
dimensions are _not_ represented as `int64_t`, which does not allow
encoding the scalability into the dimension. Instead, vector dimensions
are represented by a VectorDim class. A VectorDim stores both the size a
nd scalability of a dimension. Makes common errors like only checking
the size (but not the scalability) impossible (without being explicit
with your intention).

To make this convenient to work with there is VectorDimList which
provides ArrayRef-like helper methods along with an iterator for
VectorDims.

ScalableVectorType can freely converted to VectorType (and vice versa),
though there are two main ways to acquire a ScalableVectorType.

Assignment:

This does not check the scalability of myVectorType. This is valid and
the helpers on ScalableVectorType will function as normal.
```c++
VectorType myVectorType = ...;
ScalableVectorType scalableVector = myVectorType;
```

Casting:

This checks the scalability of myVectorType. In the case scalableVector
will be falsy if myVectorType contains no scalable dims.
```c++
VectorType myVectorType = ...;
auto scalableVector = dyn_cast<ScalableVectorType>(myVectorType);
```

Note: The use of this class is entirely optional! It only aims to make
writing scalable-aware patterns safer and easier.
This updates a few places to make use of the new support classes. This
hopefully shows (at least a little) how these classes make scalability
easier.
@MacDue MacDue force-pushed the scalable_vector_support branch from e29097f to dbfbad1 Compare June 21, 2024 09:36
@MacDue MacDue closed this Jun 27, 2024
@nujaa
Copy link
Contributor

nujaa commented Aug 5, 2024

Hello, what happened with this MR ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants