[Mlir-commits] [mlir] 5f03679 - [mlir][tblgen] Emit interface decls in definition order in the .td file

Matthias Springer llvmlistbot at llvm.org
Thu Apr 6 20:09:31 PDT 2023


Author: Matthias Springer
Date: 2023-04-07T12:09:23+09:00
New Revision: 5f036799f4d39c1aa5a6fac0e972050373e9fa78

URL: https://github.com/llvm/llvm-project/commit/5f036799f4d39c1aa5a6fac0e972050373e9fa78
DIFF: https://github.com/llvm/llvm-project/commit/5f036799f4d39c1aa5a6fac0e972050373e9fa78.diff

LOG: [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

Added: 
    

Modified: 
    mlir/tools/mlir-tblgen/OpInterfacesGen.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp b/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp
index 279edfd21443c..b9507b377afd5 100644
--- a/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp
@@ -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;
 }


        


More information about the Mlir-commits mailing list