[mlir][tblgen] Emit interface decls in definition order in the .td file
authorMatthias Springer <springerm@google.com>
Fri, 7 Apr 2023 03:02:17 +0000 (12:02 +0900)
committerMatthias Springer <springerm@google.com>
Fri, 7 Apr 2023 03:09:23 +0000 (12:09 +0900)
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

mlir/tools/mlir-tblgen/OpInterfacesGen.cpp

index 279edfd..b9507b3 100644 (file)
@@ -602,10 +602,15 @@ void InterfaceGenerator::emitInterfaceDecl(const Interface &interface) {
 
 bool InterfaceGenerator::emitInterfaceDecls() {
   llvm::emitSourceFileHeader("Interface Declarations", os);
-
-  for (const llvm::Record *def : defs)
+  // Sort according to ID, so defs are emitted in the order in which they appear
+  // in the Tablegen file.
+  std::vector<llvm::Record *> sortedDefs(defs);
+  llvm::sort(sortedDefs, [](llvm::Record *lhs, llvm::Record *rhs) {
+    return lhs->getID() < rhs->getID();
+  });
+  for (const llvm::Record *def : sortedDefs)
     emitInterfaceDecl(Interface(def));
-  for (const llvm::Record *def : defs)
+  for (const llvm::Record *def : sortedDefs)
     emitModelMethodsDef(Interface(def));
   return false;
 }