Skip to content

Commit 5f03679

Browse files
[mlir][tblgen] Emit interface decls in definition order in the .td file
Interface decls were previously sorted by name. This lead to problems when using interface inheritance when both interfaces are defined in the same .td file. The derived interface must appear after the base interface, otherwise the generated C++ will not compile. With this change, interfaces will compile as long as the base interface is defined before derived interfaces in the .td file. Differential Revision: https://reviews.llvm.org/D147689
1 parent 7557710 commit 5f03679

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

mlir/tools/mlir-tblgen/OpInterfacesGen.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -602,10 +602,15 @@ void InterfaceGenerator::emitInterfaceDecl(const Interface &interface) {
602602

603603
bool InterfaceGenerator::emitInterfaceDecls() {
604604
llvm::emitSourceFileHeader("Interface Declarations", os);
605-
606-
for (const llvm::Record *def : defs)
605+
// Sort according to ID, so defs are emitted in the order in which they appear
606+
// in the Tablegen file.
607+
std::vector<llvm::Record *> sortedDefs(defs);
608+
llvm::sort(sortedDefs, [](llvm::Record *lhs, llvm::Record *rhs) {
609+
return lhs->getID() < rhs->getID();
610+
});
611+
for (const llvm::Record *def : sortedDefs)
607612
emitInterfaceDecl(Interface(def));
608-
for (const llvm::Record *def : defs)
613+
for (const llvm::Record *def : sortedDefs)
609614
emitModelMethodsDef(Interface(def));
610615
return false;
611616
}

0 commit comments

Comments
 (0)