From 3c51d92d3e7c8e2da41f0d1e8055fa44d7369b7d Mon Sep 17 00:00:00 2001 From: Andrzej Warzynski Date: Mon, 8 Apr 2024 11:32:13 +0100 Subject: [PATCH 1/5] [mlir][vector] Add convenience types for scalable vectors This PR adds two small convenience Vector types: * `ScalableVectorType` and `FixedWidthVectorType`. The goal of these new types is two-fold: * enable idiomatic checks like `isa(...)`, * make the split into "Scalable" and "Fixed-wdith" vectors a bit more explicit and more visible in the code-base. Depends on #87999 --- mlir/include/mlir/IR/VectorTypes.h | 35 ++++++++++++++++++++++++++ mlir/lib/Dialect/Arith/IR/ArithOps.cpp | 6 +++-- mlir/lib/IR/CMakeLists.txt | 1 + mlir/lib/IR/VectorTypes.cpp | 27 ++++++++++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 mlir/include/mlir/IR/VectorTypes.h create mode 100644 mlir/lib/IR/VectorTypes.cpp diff --git a/mlir/include/mlir/IR/VectorTypes.h b/mlir/include/mlir/IR/VectorTypes.h new file mode 100644 index 0000000000000..384969779d8f6 --- /dev/null +++ b/mlir/include/mlir/IR/VectorTypes.h @@ -0,0 +1,35 @@ +//===- 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 +// +//===----------------------------------------------------------------------===// + +#ifndef MLIR_DIALECT_VECTOR_IR_VECTORTYPES_H_ +#define MLIR_DIALECT_VECTOR_IR_VECTORTYPES_H_ + +#include "mlir/IR/BuiltinTypes.h" +#include "mlir/IR/Diagnostics.h" +#include "mlir/IR/Types.h" + +namespace mlir { +namespace vector { + +class ScalableVectorType : public VectorType { +public: + using VectorType::VectorType; + + static bool classof(Type type); +}; + +class FixedWidthVectorType : public VectorType { +public: + using VectorType::VectorType; + static bool classof(Type type); +}; + +} // namespace vector +} // namespace mlir + +#endif // MLIR_DIALECT_VECTOR_IR_VECTORTYPES_H_ diff --git a/mlir/lib/Dialect/Arith/IR/ArithOps.cpp b/mlir/lib/Dialect/Arith/IR/ArithOps.cpp index 16b4e8eb4f022..e65258786a768 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(type); - if (vecType && vecType.isScalable() && !isa(getValue())) + if (isa(type) && + !isa(getValue())) return emitOpError( "intializing scalable vectors with elements attribute is not supported" " unless it's a vector splat"); diff --git a/mlir/lib/IR/CMakeLists.txt b/mlir/lib/IR/CMakeLists.txt index 4cabac185171c..cf0aeedcde57b 100644 --- a/mlir/lib/IR/CMakeLists.txt +++ b/mlir/lib/IR/CMakeLists.txt @@ -40,6 +40,7 @@ add_mlir_library(MLIRIR Unit.cpp Value.cpp ValueRange.cpp + VectorTypes.cpp Verifier.cpp Visitors.cpp ${pdl_src} diff --git a/mlir/lib/IR/VectorTypes.cpp b/mlir/lib/IR/VectorTypes.cpp new file mode 100644 index 0000000000000..4ce1d606a3f2f --- /dev/null +++ b/mlir/lib/IR/VectorTypes.cpp @@ -0,0 +1,27 @@ +//===- VectorTypes.cpp - MLIR Vector Types --------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "mlir/IR/VectorTypes.h" +#include "mlir/IR/BuiltinTypes.h" + +using namespace mlir; +using namespace mlir::vector; + +bool ScalableVectorType::classof(Type type) { + auto vecTy = dyn_cast(type); + if (!vecTy) + return false; + return vecTy.isScalable(); +} + +bool FixedWidthVectorType::classof(Type type) { + auto vecTy = llvm::dyn_cast(type); + if (!vecTy) + return false; + return !vecTy.isScalable(); +} From 3cf83b128fbd47db1fda3fd0064208b77b8a6c3d Mon Sep 17 00:00:00 2001 From: Andrzej Warzynski Date: Tue, 16 Apr 2024 16:32:15 +0100 Subject: [PATCH 2/5] fixup! [mlir][vector] Add convenience types for scalable vectors Inline implementation, add comment. --- mlir/include/mlir/IR/VectorTypes.h | 16 ++++++++++++++-- mlir/lib/IR/CMakeLists.txt | 1 - mlir/lib/IR/VectorTypes.cpp | 27 --------------------------- 3 files changed, 14 insertions(+), 30 deletions(-) delete mode 100644 mlir/lib/IR/VectorTypes.cpp diff --git a/mlir/include/mlir/IR/VectorTypes.h b/mlir/include/mlir/IR/VectorTypes.h index 384969779d8f6..769daa8f9417d 100644 --- a/mlir/include/mlir/IR/VectorTypes.h +++ b/mlir/include/mlir/IR/VectorTypes.h @@ -16,17 +16,29 @@ 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); + static bool classof(Type type) { + auto vecTy = llvm::dyn_cast(type); + if (!vecTy) + return false; + return vecTy.isScalable(); + } }; +/// A vector type with no scalable dimensions class FixedWidthVectorType : public VectorType { public: using VectorType::VectorType; - static bool classof(Type type); + static bool classof(Type type) { + auto vecTy = llvm::dyn_cast(type); + if (!vecTy) + return false; + return !vecTy.isScalable(); + } }; } // namespace vector diff --git a/mlir/lib/IR/CMakeLists.txt b/mlir/lib/IR/CMakeLists.txt index cf0aeedcde57b..4cabac185171c 100644 --- a/mlir/lib/IR/CMakeLists.txt +++ b/mlir/lib/IR/CMakeLists.txt @@ -40,7 +40,6 @@ add_mlir_library(MLIRIR Unit.cpp Value.cpp ValueRange.cpp - VectorTypes.cpp Verifier.cpp Visitors.cpp ${pdl_src} diff --git a/mlir/lib/IR/VectorTypes.cpp b/mlir/lib/IR/VectorTypes.cpp deleted file mode 100644 index 4ce1d606a3f2f..0000000000000 --- a/mlir/lib/IR/VectorTypes.cpp +++ /dev/null @@ -1,27 +0,0 @@ -//===- VectorTypes.cpp - MLIR Vector Types --------------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -#include "mlir/IR/VectorTypes.h" -#include "mlir/IR/BuiltinTypes.h" - -using namespace mlir; -using namespace mlir::vector; - -bool ScalableVectorType::classof(Type type) { - auto vecTy = dyn_cast(type); - if (!vecTy) - return false; - return vecTy.isScalable(); -} - -bool FixedWidthVectorType::classof(Type type) { - auto vecTy = llvm::dyn_cast(type); - if (!vecTy) - return false; - return !vecTy.isScalable(); -} From 8c5e54a94abe564b917d703c34b03e07fcd6853c Mon Sep 17 00:00:00 2001 From: Andrzej Warzynski Date: Fri, 29 Nov 2024 12:12:49 +0000 Subject: [PATCH 3/5] fixup! fixup! [mlir][vector] Add convenience types for scalable vectors Add missing . --- mlir/include/mlir/IR/VectorTypes.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mlir/include/mlir/IR/VectorTypes.h b/mlir/include/mlir/IR/VectorTypes.h index 769daa8f9417d..5fa0b08999412 100644 --- a/mlir/include/mlir/IR/VectorTypes.h +++ b/mlir/include/mlir/IR/VectorTypes.h @@ -16,7 +16,7 @@ namespace mlir { namespace vector { -/// A vector type containing at least one scalable dimension +/// A vector type containing at least one scalable dimension. class ScalableVectorType : public VectorType { public: using VectorType::VectorType; @@ -29,7 +29,7 @@ class ScalableVectorType : public VectorType { } }; -/// A vector type with no scalable dimensions +/// A vector type with no scalable dimensions. class FixedWidthVectorType : public VectorType { public: using VectorType::VectorType; From aab6b8e4ac21b8855bfb2fd95d653aa0a8adc0c2 Mon Sep 17 00:00:00 2001 From: Andrzej Warzynski Date: Fri, 29 Nov 2024 16:24:09 +0000 Subject: [PATCH 4/5] fixup! fixup! fixup! [mlir][vector] Add convenience types for scalable vectors FixedWidthVectorType -> FixedVectorType for consistency with LLVM --- mlir/include/mlir/IR/VectorTypes.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mlir/include/mlir/IR/VectorTypes.h b/mlir/include/mlir/IR/VectorTypes.h index 5fa0b08999412..4d89222efe03e 100644 --- a/mlir/include/mlir/IR/VectorTypes.h +++ b/mlir/include/mlir/IR/VectorTypes.h @@ -30,7 +30,7 @@ class ScalableVectorType : public VectorType { }; /// A vector type with no scalable dimensions. -class FixedWidthVectorType : public VectorType { +class FixedVectorType : public VectorType { public: using VectorType::VectorType; static bool classof(Type type) { From 1baa7eeba42917e1886c8da25e3712042da47630 Mon Sep 17 00:00:00 2001 From: Andrzej Warzynski Date: Mon, 2 Dec 2024 08:10:58 +0000 Subject: [PATCH 5/5] fixup! fixup! fixup! fixup! [mlir][vector] Add convenience types for scalable vectors Fix typos, add comments, remove include --- mlir/include/mlir/IR/VectorTypes.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/mlir/include/mlir/IR/VectorTypes.h b/mlir/include/mlir/IR/VectorTypes.h index 4d89222efe03e..c209f869a579d 100644 --- a/mlir/include/mlir/IR/VectorTypes.h +++ b/mlir/include/mlir/IR/VectorTypes.h @@ -5,12 +5,16 @@ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// +// +// Convenience wrappers for `VectorType` to allow idiomatic code like +// * isa(type) +// +//===----------------------------------------------------------------------===// -#ifndef MLIR_DIALECT_VECTOR_IR_VECTORTYPES_H_ -#define MLIR_DIALECT_VECTOR_IR_VECTORTYPES_H_ +#ifndef MLIR_IR_VECTORTYPES_H +#define MLIR_IR_VECTORTYPES_H #include "mlir/IR/BuiltinTypes.h" -#include "mlir/IR/Diagnostics.h" #include "mlir/IR/Types.h" namespace mlir { @@ -44,4 +48,4 @@ class FixedVectorType : public VectorType { } // namespace vector } // namespace mlir -#endif // MLIR_DIALECT_VECTOR_IR_VECTORTYPES_H_ +#endif // MLIR_IR_VECTORTYPES_H