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

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Sep 13 14:28:45 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir
            
<details>
<summary>Changes</summary>
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.
--
Full diff: https://github.com/llvm/llvm-project/pull/66292.diff

3 Files Affected:

- (modified) mlir/include/mlir/IR/Attributes.h (+1-1) 
- (modified) mlir/include/mlir/IR/Types.h (+1-1) 
- (modified) mlir/test/lib/Dialect/Test/TestInterfaces.td (+26) 


<pre>
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
</pre>
</details>


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


More information about the Mlir-commits mailing list