Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit e28729b

Browse files
authored
[MLIR][ODS] Add support for overloading interface methods (#161828)
This allows to define multiple interface methods with the same name but different arguments.
1 parent ea6ec1e commit e28729b

File tree

3 files changed

+62
-16
lines changed

3 files changed

+62
-16
lines changed

mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,9 @@ class DefGen {
130130
void emitTraitMethods(const InterfaceTrait &trait);
131131
/// Emit a trait method.
132132
void emitTraitMethod(const InterfaceMethod &method);
133+
/// Generate a using declaration for a trait method.
134+
void genTraitMethodUsingDecl(const InterfaceTrait &trait,
135+
const InterfaceMethod &method);
133136

134137
//===--------------------------------------------------------------------===//
135138
// OpAsm{Type,Attr}Interface Default Method Emission
@@ -176,6 +179,9 @@ class DefGen {
176179
StringRef valueType;
177180
/// The prefix/suffix of the TableGen def name, either "Attr" or "Type".
178181
StringRef defType;
182+
183+
/// The set of using declarations for trait methods.
184+
llvm::StringSet<> interfaceUsingNames;
179185
};
180186
} // namespace
181187

@@ -632,8 +638,10 @@ void DefGen::emitTraitMethods(const InterfaceTrait &trait) {
632638
// Don't declare if the method has a body. Or if the method has a default
633639
// implementation and the def didn't request that it always be declared.
634640
if (method.getBody() || (method.getDefaultImplementation() &&
635-
!alwaysDeclared.count(method.getName())))
641+
!alwaysDeclared.count(method.getName()))) {
642+
genTraitMethodUsingDecl(trait, method);
636643
continue;
644+
}
637645
emitTraitMethod(method);
638646
}
639647
}
@@ -649,6 +657,15 @@ void DefGen::emitTraitMethod(const InterfaceMethod &method) {
649657
std::move(params));
650658
}
651659

660+
void DefGen::genTraitMethodUsingDecl(const InterfaceTrait &trait,
661+
const InterfaceMethod &method) {
662+
std::string name = (llvm::Twine(trait.getFullyQualifiedTraitName()) + "<" +
663+
def.getCppClassName() + ">::" + method.getName())
664+
.str();
665+
if (interfaceUsingNames.insert(name).second)
666+
defCls.declare<UsingDeclaration>(std::move(name));
667+
}
668+
652669
//===----------------------------------------------------------------------===//
653670
// OpAsm{Type,Attr}Interface Default Method Emission
654671

mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,14 @@ class OpEmitter {
790790
Method *genOpInterfaceMethod(const tblgen::InterfaceMethod &method,
791791
bool declaration = true);
792792

793+
// Generate a `using` declaration for the op interface method to include
794+
// the default implementation from the interface trait.
795+
// This is needed when the interface defines multiple methods with the same
796+
// name, but some have a default implementation and some don't.
797+
UsingDeclaration *
798+
genOpInterfaceMethodUsingDecl(const tblgen::InterfaceTrait *opTrait,
799+
const tblgen::InterfaceMethod &method);
800+
793801
// Generate the side effect interface methods.
794802
void genSideEffectInterfaceMethods();
795803

@@ -816,6 +824,10 @@ class OpEmitter {
816824

817825
// Helper for emitting op code.
818826
OpOrAdaptorHelper emitHelper;
827+
828+
// Keep track of the interface using declarations that have been generated to
829+
// avoid duplicates.
830+
llvm::StringSet<> interfaceUsingNames;
819831
};
820832

821833
} // namespace
@@ -3673,8 +3685,10 @@ void OpEmitter::genOpInterfaceMethods(const tblgen::InterfaceTrait *opTrait) {
36733685
// Don't declare if the method has a default implementation and the op
36743686
// didn't request that it always be declared.
36753687
if (method.getDefaultImplementation() &&
3676-
!alwaysDeclaredMethods.count(method.getName()))
3688+
!alwaysDeclaredMethods.count(method.getName())) {
3689+
genOpInterfaceMethodUsingDecl(opTrait, method);
36773690
continue;
3691+
}
36783692
// Interface methods are allowed to overlap with existing methods, so don't
36793693
// check if pruned.
36803694
(void)genOpInterfaceMethod(method);
@@ -3693,6 +3707,17 @@ Method *OpEmitter::genOpInterfaceMethod(const InterfaceMethod &method,
36933707
std::move(paramList));
36943708
}
36953709

3710+
UsingDeclaration *
3711+
OpEmitter::genOpInterfaceMethodUsingDecl(const tblgen::InterfaceTrait *opTrait,
3712+
const InterfaceMethod &method) {
3713+
std::string name = (llvm::Twine(opTrait->getFullyQualifiedTraitName()) + "<" +
3714+
op.getCppClassName() + ">::" + method.getName())
3715+
.str();
3716+
if (interfaceUsingNames.insert(name).second)
3717+
return opClass.declare<UsingDeclaration>(std::move(name));
3718+
return nullptr;
3719+
}
3720+
36963721
void OpEmitter::genOpInterfaceMethods() {
36973722
for (const auto &trait : op.getTraits()) {
36983723
if (const auto *opTrait = dyn_cast<tblgen::InterfaceTrait>(&trait))

mlir/tools/mlir-tblgen/OpInterfacesGen.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ static raw_ostream &emitCPPType(StringRef type, raw_ostream &os) {
4242
/// Emit the method name and argument list for the given method. If 'addThisArg'
4343
/// is true, then an argument is added to the beginning of the argument list for
4444
/// the concrete value.
45-
static void emitMethodNameAndArgs(const InterfaceMethod &method,
45+
static void emitMethodNameAndArgs(const InterfaceMethod &method, StringRef name,
4646
raw_ostream &os, StringRef valueType,
4747
bool addThisArg, bool addConst) {
48-
os << method.getName() << '(';
48+
os << name << '(';
4949
if (addThisArg) {
5050
if (addConst)
5151
os << "const ";
@@ -183,11 +183,13 @@ static void emitInterfaceDefMethods(StringRef interfaceQualName,
183183
emitInterfaceMethodDoc(method, os);
184184
emitCPPType(method.getReturnType(), os);
185185
os << interfaceQualName << "::";
186-
emitMethodNameAndArgs(method, os, valueType, /*addThisArg=*/false,
186+
emitMethodNameAndArgs(method, method.getName(), os, valueType,
187+
/*addThisArg=*/false,
187188
/*addConst=*/!isOpInterface);
188189

189190
// Forward to the method on the concrete operation type.
190-
os << " {\n return " << implValue << "->" << method.getName() << '(';
191+
os << " {\n return " << implValue << "->" << method.getUniqueName()
192+
<< '(';
191193
if (!method.isStatic()) {
192194
os << implValue << ", ";
193195
os << (isOpInterface ? "getOperation()" : "*this");
@@ -239,7 +241,7 @@ void InterfaceGenerator::emitConceptDecl(const Interface &interface) {
239241
for (auto &method : interface.getMethods()) {
240242
os << " ";
241243
emitCPPType(method.getReturnType(), os);
242-
os << "(*" << method.getName() << ")(";
244+
os << "(*" << method.getUniqueName() << ")(";
243245
if (!method.isStatic()) {
244246
os << "const Concept *impl, ";
245247
emitCPPType(valueType, os) << (method.arg_empty() ? "" : ", ");
@@ -289,13 +291,13 @@ void InterfaceGenerator::emitModelDecl(const Interface &interface) {
289291
os << " " << modelClass << "() : Concept{";
290292
llvm::interleaveComma(
291293
interface.getMethods(), os,
292-
[&](const InterfaceMethod &method) { os << method.getName(); });
294+
[&](const InterfaceMethod &method) { os << method.getUniqueName(); });
293295
os << "} {}\n\n";
294296

295297
// Insert each of the virtual method overrides.
296298
for (auto &method : interface.getMethods()) {
297299
emitCPPType(method.getReturnType(), os << " static inline ");
298-
emitMethodNameAndArgs(method, os, valueType,
300+
emitMethodNameAndArgs(method, method.getUniqueName(), os, valueType,
299301
/*addThisArg=*/!method.isStatic(),
300302
/*addConst=*/false);
301303
os << ";\n";
@@ -319,7 +321,7 @@ void InterfaceGenerator::emitModelDecl(const Interface &interface) {
319321
if (method.isStatic())
320322
os << "static ";
321323
emitCPPType(method.getReturnType(), os);
322-
os << method.getName() << "(";
324+
os << method.getUniqueName() << "(";
323325
if (!method.isStatic()) {
324326
emitCPPType(valueType, os);
325327
os << "tablegen_opaque_val";
@@ -350,7 +352,7 @@ void InterfaceGenerator::emitModelMethodsDef(const Interface &interface) {
350352
emitCPPType(method.getReturnType(), os);
351353
os << "detail::" << interface.getName() << "InterfaceTraits::Model<"
352354
<< valueTemplate << ">::";
353-
emitMethodNameAndArgs(method, os, valueType,
355+
emitMethodNameAndArgs(method, method.getUniqueName(), os, valueType,
354356
/*addThisArg=*/!method.isStatic(),
355357
/*addConst=*/false);
356358
os << " {\n ";
@@ -384,7 +386,7 @@ void InterfaceGenerator::emitModelMethodsDef(const Interface &interface) {
384386
emitCPPType(method.getReturnType(), os);
385387
os << "detail::" << interface.getName() << "InterfaceTraits::FallbackModel<"
386388
<< valueTemplate << ">::";
387-
emitMethodNameAndArgs(method, os, valueType,
389+
emitMethodNameAndArgs(method, method.getUniqueName(), os, valueType,
388390
/*addThisArg=*/!method.isStatic(),
389391
/*addConst=*/false);
390392
os << " {\n ";
@@ -396,7 +398,7 @@ void InterfaceGenerator::emitModelMethodsDef(const Interface &interface) {
396398
os << "return static_cast<const " << valueTemplate << " *>(impl)->";
397399

398400
// Add the arguments to the call.
399-
os << method.getName() << '(';
401+
os << method.getUniqueName() << '(';
400402
if (!method.isStatic())
401403
os << "tablegen_opaque_val" << (method.arg_empty() ? "" : ", ");
402404
llvm::interleaveComma(
@@ -416,7 +418,7 @@ void InterfaceGenerator::emitModelMethodsDef(const Interface &interface) {
416418
<< "InterfaceTraits::ExternalModel<ConcreteModel, " << valueTemplate
417419
<< ">::";
418420

419-
os << method.getName() << "(";
421+
os << method.getUniqueName() << "(";
420422
if (!method.isStatic()) {
421423
emitCPPType(valueType, os);
422424
os << "tablegen_opaque_val";
@@ -477,7 +479,8 @@ void InterfaceGenerator::emitInterfaceTraitDecl(const Interface &interface) {
477479
emitInterfaceMethodDoc(method, os, " ");
478480
os << " " << (method.isStatic() ? "static " : "");
479481
emitCPPType(method.getReturnType(), os);
480-
emitMethodNameAndArgs(method, os, valueType, /*addThisArg=*/false,
482+
emitMethodNameAndArgs(method, method.getName(), os, valueType,
483+
/*addThisArg=*/false,
481484
/*addConst=*/!isOpInterface && !method.isStatic());
482485
os << " {\n " << tblgen::tgfmt(defaultImpl->trim(), &traitMethodFmt)
483486
<< "\n }\n";
@@ -514,7 +517,8 @@ static void emitInterfaceDeclMethods(const Interface &interface,
514517
for (auto &method : interface.getMethods()) {
515518
emitInterfaceMethodDoc(method, os, " ");
516519
emitCPPType(method.getReturnType(), os << " ");
517-
emitMethodNameAndArgs(method, os, valueType, /*addThisArg=*/false,
520+
emitMethodNameAndArgs(method, method.getName(), os, valueType,
521+
/*addThisArg=*/false,
518522
/*addConst=*/!isOpInterface);
519523
os << ";\n";
520524
}

0 commit comments

Comments
 (0)