Skip to content

Conversation

@andidr
Copy link
Contributor

@andidr andidr commented Nov 4, 2025

Since commit 842622b adding support for overloading interface methods, a using directive is emitted for any interface method that does not require emission of a trait method, including for methods that define a method body.

However, methods directly specifying a body (e.g., via the methodBody parameter of InterfaceMethod) are implemented directly in the interface class and are therefore not present in the associated trait. The generated using directive then referes to a non-existent method of the trait, resulting in an error upon compilation of the generated code.

This patch changes DefGen::emitTraitMethods(), such that genTraitMethodUsingDecl() is not invoked for interface methods with a body anymore.

@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Nov 4, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 4, 2025

@llvm/pr-subscribers-mlir-core

@llvm/pr-subscribers-mlir

Author: Andi Drebes (andidr)

Changes

Since commit 842622b adding support for overloading interface methods, a using directive is emitted for any interface method that does not require emission of a trait method, including for methods that define a method body.

However, methods directly specifying a body (e.g., via the methodBody parameter of InterfaceMethod) are implemented directly in the interface class and are therefore not present in the associated trait. The generated using directive then referes to a non-existent method of the trait, resulting in an error upon compilation of the generated code.

This patch changes DefGen::emitTraitMethods(), such that genTraitMethodUsingDecl() is not invoked for interface methods with a body anymore.


Full diff: https://github.com/llvm/llvm-project/pull/166335.diff

2 Files Affected:

  • (modified) mlir/test/lib/Dialect/Test/TestTypeDefs.td (+7)
  • (modified) mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp (+6-3)
diff --git a/mlir/test/lib/Dialect/Test/TestTypeDefs.td b/mlir/test/lib/Dialect/Test/TestTypeDefs.td
index ea20597231d58..9859bd06cb526 100644
--- a/mlir/test/lib/Dialect/Test/TestTypeDefs.td
+++ b/mlir/test/lib/Dialect/Test/TestTypeDefs.td
@@ -470,4 +470,11 @@ def TestMemrefType : Test_Type<"TestMemref",
   }];
 }
 
+// Test implementation of an interface with methods specifying a
+// method body
+def TestBaseBody : Test_Type<"TestBaseBody",
+    [DeclareTypeInterfaceMethods<TestBaseTypeInterfacePrintTypeA>]> {
+  let mnemonic = "test_base_body";
+}
+
 #endif // TEST_TYPEDEFS
diff --git a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
index 8ec2e03095423..b87a3247d2965 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
+++ b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
@@ -637,12 +637,15 @@ void DefGen::emitTraitMethods(const InterfaceTrait &trait) {
   for (auto &method : iface.getMethods()) {
     // Don't declare if the method has a body. Or if the method has a default
     // implementation and the def didn't request that it always be declared.
-    if (method.getBody() || (method.getDefaultImplementation() &&
-                             !alwaysDeclared.count(method.getName()))) {
+    if (method.getBody()) {
+      continue;
+    } else if (method.getDefaultImplementation() &&
+               !alwaysDeclared.count(method.getName())) {
       genTraitMethodUsingDecl(trait, method);
       continue;
+    } else {
+      emitTraitMethod(method);
     }
-    emitTraitMethod(method);
   }
 }
 

@andidr andidr force-pushed the ods-typeinterfaces branch from 102abd9 to 190e00b Compare November 4, 2025 08:57
@andidr andidr requested a review from joker-eph November 4, 2025 10:11
@andidr andidr force-pushed the ods-typeinterfaces branch from 190e00b to ca5e4c9 Compare November 5, 2025 04:44
Copy link
Member

@jpienaar jpienaar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks

…ethod bodies

Since commit 842622b adding support
for overloading interface methods, a `using` directive is emitted for
any interface method that does not require emission of a trait method,
including for methods that define a method body.

However, methods directly specifying a body (e.g., via the
`methodBody` parameter of `InterfaceMethod`) are implemented directly
in the interface class and are therefore not present in the associated
trait. The generated `using` directive then referes to a non-existent
method of the trait, resulting in an error upon compilation of the
generated code.

This patch changes `DefGen::emitTraitMethods()`, such that
`genTraitMethodUsingDecl()` is not invoked for interface methods with
a body anymore.
@andidr andidr force-pushed the ods-typeinterfaces branch from ca5e4c9 to 20f4f5b Compare November 5, 2025 08:45
@joker-eph joker-eph merged commit 8eacea9 into llvm:main Nov 5, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

mlir:core MLIR Core Infrastructure mlir

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants