From 802bf02a738e091d5bf22c03e83204a38d2c7950 Mon Sep 17 00:00:00 2001 From: Mathieu Fehr Date: Mon, 13 Sep 2021 06:22:19 +0000 Subject: [PATCH] [mlir] Allows to query traits from types and attributes Types and attributes now have a `hasTrait` function that allow users to check if a type defines a trait. Also, AbstractType and AbstractAttribute has now a `hasTraitFn` field to carry the implementation of the `hasTrait` function of the concrete type or attribute. This patch also adds the remaining functions to access type and attribute traits in TableGen. Reviewed By: rriddle Differential Revision: https://reviews.llvm.org/D105202 --- mlir/include/mlir/IR/AttributeSupport.h | 27 +++++++++++++++---- mlir/include/mlir/IR/Attributes.h | 6 +++++ mlir/include/mlir/IR/OpBase.td | 34 ++++++++++++++++++++++++ mlir/include/mlir/IR/StorageUniquerSupport.h | 28 ++++++++++++++++++++ mlir/include/mlir/IR/TypeSupport.h | 29 ++++++++++++++++----- mlir/include/mlir/IR/Types.h | 6 +++++ mlir/test/IR/traits.mlir | 36 +++++++++++++++++++++++++ mlir/test/lib/Dialect/Test/TestAttrDefs.td | 11 +++++++- mlir/test/lib/Dialect/Test/TestAttributes.h | 1 + mlir/test/lib/Dialect/Test/TestOps.td | 24 +++++++++++++++++ mlir/test/lib/Dialect/Test/TestTraits.h | 39 ++++++++++++++++++++++++++++ mlir/test/lib/Dialect/Test/TestTypeDefs.td | 7 +++++ mlir/test/lib/Dialect/Test/TestTypes.h | 1 + 13 files changed, 237 insertions(+), 12 deletions(-) create mode 100644 mlir/test/lib/Dialect/Test/TestTraits.h diff --git a/mlir/include/mlir/IR/AttributeSupport.h b/mlir/include/mlir/IR/AttributeSupport.h index c84be62..d18f0ab 100644 --- a/mlir/include/mlir/IR/AttributeSupport.h +++ b/mlir/include/mlir/IR/AttributeSupport.h @@ -30,14 +30,18 @@ class Type; /// a registered Attribute. class AbstractAttribute { public: + using HasTraitFn = llvm::unique_function; + /// Look up the specified abstract attribute in the MLIRContext and return a /// reference to it. static const AbstractAttribute &lookup(TypeID typeID, MLIRContext *context); /// This method is used by Dialect objects when they register the list of /// attributes they contain. - template static AbstractAttribute get(Dialect &dialect) { - return AbstractAttribute(dialect, T::getInterfaceMap(), T::getTypeID()); + template + static AbstractAttribute get(Dialect &dialect) { + return AbstractAttribute(dialect, T::getInterfaceMap(), T::getHasTraitFn(), + T::getTypeID()); } /// Return the dialect this attribute was registered to. @@ -46,7 +50,8 @@ public: /// Returns an instance of the concept object for the given interface if it /// was registered to this attribute, null otherwise. This should not be used /// directly. - template typename T::Concept *getInterface() const { + template + typename T::Concept *getInterface() const { return interfaceMap.lookup(); } @@ -56,14 +61,23 @@ public: return interfaceMap.contains(interfaceID); } + /// Returns true if the attribute has a particular trait. + template