diff --git a/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp b/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp --- a/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp +++ b/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp @@ -602,10 +602,15 @@ 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 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; }