Skip to content

[MLIR][ODS] Allow operations to specify interfaces using the HasParent trait constraint #66196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions mlir/test/mlir-tblgen/op-interface.td
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,17 @@ def DeclareMethodsOp : Op<TestDialect, "declare_methods_op",
def DeclareMethodsWithDefaultOp : Op<TestDialect, "declare_methods_op",
[DeclareOpInterfaceMethods<TestOpInterface, ["default_foo"]>]>;


// DECL-LABEL: TestOpInterfaceInterfaceTraits
// DECL: class TestOpInterface : public ::mlir::OpInterface<TestOpInterface, detail::TestOpInterfaceInterfaceTraits>

// DECL: /// Returns the name of this interface.
// DECL: static ::llvm::StringLiteral getOperationName() { return ::llvm::StringLiteral( "TestOpInterface"); }

// DECL: /// some function comment
// DECL: int foo(int input);


// DECL-LABEL: struct TestOpInterfaceVerifyTrait
// DECL: verifyTrait

Expand Down
16 changes: 15 additions & 1 deletion mlir/tools/mlir-tblgen/OpInterfacesGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,16 @@ void InterfaceGenerator::emitTraitDecl(const Interface &interface,
os << " };\n";
}

static void emitInterfaceNameGetter(const Interface &interface,
raw_ostream &os) {
if (!isa<OpInterface>(interface))
return;
os << " /// Returns the name of this interface.\n"
<< " static ::llvm::StringLiteral getOperationName() { return "
"::llvm::StringLiteral( \""
<< interface.getName() << "\"); }\n";
}

static void emitInterfaceDeclMethods(const Interface &interface,
raw_ostream &os, StringRef valueType,
bool isOpInterface,
Expand Down Expand Up @@ -553,6 +563,9 @@ void InterfaceGenerator::emitInterfaceDecl(const Interface &interface) {
" struct Trait : public detail::{0}Trait<{1}> {{};\n",
interfaceName, valueTemplate);

// Emit the name of the interface.
emitInterfaceNameGetter(interface, os);

// Insert the method declarations.
bool isOpInterface = isa<OpInterface>(interface);
emitInterfaceDeclMethods(interface, os, valueType, isOpInterface,
Expand Down Expand Up @@ -588,7 +601,8 @@ void InterfaceGenerator::emitInterfaceDecl(const Interface &interface) {
<< " auto* interface = getInterfaceFor(base);\n"
<< " if (!interface)\n"
" return false;\n"
" " << interfaceName << " odsInterfaceInstance(base, interface);\n"
" "
<< interfaceName << " odsInterfaceInstance(base, interface);\n"
<< " " << tblgen::tgfmt(extraClassOf->trim(), &extraClassOfFmt)
<< "\n }\n";
}
Expand Down