[Mlir-commits] [mlir] fed3a9b - [mlir] Add ScalableVectorType and FixedVectorType (#87986)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Dec 3 01:07:35 PST 2024


Author: Andrzej WarzyƄski
Date: 2024-12-03T09:07:32Z
New Revision: fed3a9b8f81f5f4450e515f4499ecfda95804e95

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

LOG: [mlir] Add ScalableVectorType and FixedVectorType (#87986)

This PR adds two small convenience Vector types:

  * `ScalableVectorType` and `FixedVectorType`.

The goal of these new types is two-fold:
  * Enable idiomatic checks like `isa<ScalableVectorType>(...)`.
  * Make the split into "Scalable" and "Fixed-wdith" vectors a bit more
    explicit and more visible in the code-base.


The new types are added in mlir/include/mlir/IR (instead of e.g.
mlir/include/mlir/Dialect/Vector) so that the new types can be used
without requiring any new dependency (e.g. on the Vector dialect).

Added: 
    mlir/include/mlir/IR/VectorTypes.h

Modified: 
    mlir/lib/Dialect/Arith/IR/ArithOps.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/IR/VectorTypes.h b/mlir/include/mlir/IR/VectorTypes.h
new file mode 100644
index 00000000000000..c209f869a579d8
--- /dev/null
+++ b/mlir/include/mlir/IR/VectorTypes.h
@@ -0,0 +1,51 @@
+//===- VectorTypes.h - MLIR Vector Types ------------------------*- 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
+//
+//===----------------------------------------------------------------------===//
+//
+// Convenience wrappers for `VectorType` to allow idiomatic code like
+//  * isa<vector::ScalableVectorType>(type)
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_IR_VECTORTYPES_H
+#define MLIR_IR_VECTORTYPES_H
+
+#include "mlir/IR/BuiltinTypes.h"
+#include "mlir/IR/Types.h"
+
+namespace mlir {
+namespace vector {
+
+/// A vector type containing at least one scalable dimension.
+class ScalableVectorType : public VectorType {
+public:
+  using VectorType::VectorType;
+
+  static bool classof(Type type) {
+    auto vecTy = llvm::dyn_cast<VectorType>(type);
+    if (!vecTy)
+      return false;
+    return vecTy.isScalable();
+  }
+};
+
+/// A vector type with no scalable dimensions.
+class FixedVectorType : public VectorType {
+public:
+  using VectorType::VectorType;
+  static bool classof(Type type) {
+    auto vecTy = llvm::dyn_cast<VectorType>(type);
+    if (!vecTy)
+      return false;
+    return !vecTy.isScalable();
+  }
+};
+
+} // namespace vector
+} // namespace mlir
+
+#endif // MLIR_IR_VECTORTYPES_H

diff  --git a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
index 74c64761565d66..fe7646140db7ea 100644
--- a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
+++ b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp
@@ -21,6 +21,8 @@
 #include "mlir/IR/OpImplementation.h"
 #include "mlir/IR/PatternMatch.h"
 #include "mlir/IR/TypeUtilities.h"
+#include "mlir/IR/VectorTypes.h"
+#include "mlir/Support/LogicalResult.h"
 
 #include "llvm/ADT/APFloat.h"
 #include "llvm/ADT/APInt.h"
@@ -224,8 +226,8 @@ LogicalResult arith::ConstantOp::verify() {
   // Note, we could relax this for vectors with 1 scalable dim, e.g.:
   //  * arith.constant dense<[[3, 3], [1, 1]]> : vector<2 x [2] x i32>
   // However, this would most likely require updating the lowerings to LLVM.
-  auto vecType = dyn_cast<VectorType>(type);
-  if (vecType && vecType.isScalable() && !isa<SplatElementsAttr>(getValue()))
+  if (isa<vector::ScalableVectorType>(type) &&
+      !isa<SplatElementsAttr>(getValue()))
     return emitOpError(
         "intializing scalable vectors with elements attribute is not supported"
         " unless it's a vector splat");


        


More information about the Mlir-commits mailing list