[Mlir-commits] [mlir] [mlir] Make `extraClassOf` compile with attribute and type interfaces (PR #66292)

Markus Böck llvmlistbot at llvm.org
Wed Sep 13 14:27:43 PDT 2023


https://github.com/zero9178 created https://github.com/llvm/llvm-project/pull/66292:

Using `extraClassOf` currently does not work with attribute or type interfaces as the generated code calls `getInterfaceFor`, a private method of `AttributeInterface` and `TypeInterface` respectively.

This PR fixes this by applying the same change that has been done to `OpInterface` in the past: Make `getInterfaceFor` a protected member of the class, allowing the generated code in subclasses to use it.

An attribute and type interface with `extraClassOf` have been added as interfaces in the test dialect to ensure it compiles without errors.

>From 7fdeb44bedf2c71795078ea2dfbb7ce4292280ed Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Markus=20B=C3=B6ck?= <markus.boeck02 at gmail.com>
Date: Wed, 13 Sep 2023 23:25:13 +0200
Subject: [PATCH] [mlir] Make `extraClassOf` compile with attribute and type
 interfaces

Using `extraClassOf` currently does not work with attribute or type interfaces as the generated code calls `getInterfaceFor`, a private method of `AttributeInterface` and `TypeInterface` respectively.

This PR fixes this by applying the same change that has been done to `OpInterface` in the past: Make `getInterfaceFor` a protected member of the class, allowing the generated code in subclasses to use it.

An attribute and type interface with `extraClassOf` have been added as interfaces in the test dialect to ensure it compiles without errors.
---
 mlir/include/mlir/IR/Attributes.h            |  2 +-
 mlir/include/mlir/IR/Types.h                 |  2 +-
 mlir/test/lib/Dialect/Test/TestInterfaces.td | 26 ++++++++++++++++++++
 3 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/mlir/include/mlir/IR/Attributes.h b/mlir/include/mlir/IR/Attributes.h
index 53a2a1f2ad59f9e..ac5dd49f0ba3970 100644
--- a/mlir/include/mlir/IR/Attributes.h
+++ b/mlir/include/mlir/IR/Attributes.h
@@ -291,7 +291,7 @@ class AttributeInterface
                                           Attribute, AttributeTrait::TraitBase>;
   using InterfaceBase::InterfaceBase;
 
-private:
+protected:
   /// Returns the impl interface instance for the given type.
   static typename InterfaceBase::Concept *getInterfaceFor(Attribute attr) {
 #ifndef NDEBUG
diff --git a/mlir/include/mlir/IR/Types.h b/mlir/include/mlir/IR/Types.h
index 8443518027c0b62..832794928a44198 100644
--- a/mlir/include/mlir/IR/Types.h
+++ b/mlir/include/mlir/IR/Types.h
@@ -276,7 +276,7 @@ class TypeInterface : public detail::Interface<ConcreteType, Type, Traits, Type,
       detail::Interface<ConcreteType, Type, Traits, Type, TypeTrait::TraitBase>;
   using InterfaceBase::InterfaceBase;
 
-private:
+protected:
   /// Returns the impl interface instance for the given type.
   static typename InterfaceBase::Concept *getInterfaceFor(Type type) {
 #ifndef NDEBUG
diff --git a/mlir/test/lib/Dialect/Test/TestInterfaces.td b/mlir/test/lib/Dialect/Test/TestInterfaces.td
index e2ed27bdf0203e3..79a6c86493d418c 100644
--- a/mlir/test/lib/Dialect/Test/TestInterfaces.td
+++ b/mlir/test/lib/Dialect/Test/TestInterfaces.td
@@ -147,4 +147,30 @@ def TestOptionallyImplementedOpInterface
   }];
 }
 
+def TestOptionallyImplementedAttrInterface
+    : AttrInterface<"TestOptionallyImplementedAttrInterface"> {
+  let cppNamespace = "::mlir";
+
+  let methods = [
+    InterfaceMethod<"", "bool", "getImplementsInterface", (ins)>,
+  ];
+
+  let extraClassOf = [{
+    return $_attr.getImplementsInterface();
+  }];
+}
+
+def TestOptionallyImplementedTypeInterface
+    : TypeInterface<"TestOptionallyImplementedTypeInterface"> {
+  let cppNamespace = "::mlir";
+
+  let methods = [
+    InterfaceMethod<"", "bool", "getImplementsInterface", (ins)>,
+  ];
+
+  let extraClassOf = [{
+    return $_type.getImplementsInterface();
+  }];
+}
+
 #endif // MLIR_TEST_DIALECT_TEST_INTERFACES



More information about the Mlir-commits mailing list