From 13ed6958df40b85fcc80250bb3f819863904ecee Mon Sep 17 00:00:00 2001 From: Michele Scuttari Date: Tue, 30 Aug 2022 09:48:11 +0200 Subject: [PATCH] [MLIR] Unique autogenerated file for tablegen passes Being the generated code macro-guarded, the autogenerated `.cpp.inc` file has been merged into the `.h.inc` to reduce the build steps. Reviewed By: mehdi_amini, rriddle Differential Revision: https://reviews.llvm.org/D132884 --- mlir/docs/PassManagement.md | 29 ++++++--------- mlir/tools/mlir-tblgen/PassGen.cpp | 65 ++++++++++++++------------------- mlir/unittests/TableGen/CMakeLists.txt | 1 - mlir/unittests/TableGen/PassGenTest.cpp | 2 +- 4 files changed, 41 insertions(+), 56 deletions(-) diff --git a/mlir/docs/PassManagement.md b/mlir/docs/PassManagement.md index e92de3c..409a15e 100644 --- a/mlir/docs/PassManagement.md +++ b/mlir/docs/PassManagement.md @@ -829,12 +829,10 @@ def MyPass : Pass<"my-pass", "ModuleOp"> { } ``` -Using the `gen-pass-decls` and `gen-pass-defs` generators, we can generate most -of the boilerplate above automatically. - -The `gen-pass-decls` generator takes as an input a `-name` parameter, that +Using the `gen-pass-decls` generator, we can generate most of the boilerplate +above automatically. This generator takes as an input a `-name` parameter, that provides a tag for the group of passes that are being generated. This generator -produces code with two purposes: +produces code with multiple purposes: The first is to register the declared passes with the global registry. For each pass, the generator produces a `registerPassName` where @@ -902,14 +900,11 @@ std::unique_ptr<::mlir::Pass> createMyPass(const MyPassOptions &options); #endif // GEN_PASS_DECL_MYPASS ``` -The `gen-pass-defs` generator produces the definitions to be used for the pass -implementation. - -It generates a base class for each of the passes, containing most of the boiler -plate related to pass definitions. These classes are named in the form of -`MyPassBase` and are declared inside the `impl` namespace, where `MyPass` is -the name of the pass definition in tablegen. We can update the original C++ -pass definition as so: +The last purpose of this generator is to emit a base class for each of the +passes, containing most of the boiler plate related to pass definitions. These +classes are named in the form of `MyPassBase` and are declared inside the +`impl` namespace, where `MyPass` is the name of the pass definition in +tablegen. We can update the original C++ pass definition as so: ```c++ // MyPass.cpp @@ -917,7 +912,7 @@ pass definition as so: /// Include the generated base pass class definitions. namespace foo { #define GEN_PASS_DEF_MYPASS -#include "Passes.cpp.inc" +#include "Passes.h.inc" } /// Define the main class as deriving from the generated base class. @@ -929,9 +924,9 @@ struct MyPass : foo::impl::MyPassBase { }; ``` -Similarly to the previous generator, the definitions can be enabled on a -per-pass basis by defining the appropriate preprocessor `GEN_PASS_DEF_PASSNAME` -macro, with `PASSNAME` equal to the uppercase version of the name of the pass +Similarly to the previous cases, the definitions can be enabled on a per-pass +basis by defining the appropriate preprocessor `GEN_PASS_DEF_PASSNAME` macro, +with `PASSNAME` equal to the uppercase version of the name of the pass definition in tablegen. If the `constructor` field has not been specified in tablegen, then the default constructors are also defined and expect the name of the actual pass class to diff --git a/mlir/tools/mlir-tblgen/PassGen.cpp b/mlir/tools/mlir-tblgen/PassGen.cpp index 6515955..801db20 100644 --- a/mlir/tools/mlir-tblgen/PassGen.cpp +++ b/mlir/tools/mlir-tblgen/PassGen.cpp @@ -118,8 +118,6 @@ static void emitPassDecls(const Pass &pass, raw_ostream &os) { std::string enableVarName = "GEN_PASS_DECL_" + passName.upper(); os << "#ifdef " << enableVarName << "\n"; - os << llvm::formatv(passHeader, passName); - emitPassOptionsStruct(pass, os); if (StringRef constructor = pass.getConstructor(); constructor.empty()) { @@ -164,24 +162,6 @@ static void emitRegistrations(llvm::ArrayRef passes, raw_ostream &os) { os << "#endif // GEN_PASS_REGISTRATION\n"; } -static void emitDecls(const llvm::RecordKeeper &recordKeeper, raw_ostream &os) { - std::vector passes = getPasses(recordKeeper); - os << "/* Autogenerated by mlir-tblgen; don't manually edit */\n"; - - for (const Pass &pass : passes) - emitPassDecls(pass, os); - - emitRegistrations(passes, os); - - // TODO drop old pass declarations - // Emit the old code until all the passes have switched to the new design. - os << "#ifdef GEN_PASS_CLASSES\n"; - for (const Pass &pass : passes) - emitOldPassDecl(pass, os); - os << "#undef GEN_PASS_CLASSES\n"; - os << "#endif // GEN_PASS_CLASSES\n"; -} - //===----------------------------------------------------------------------===// // GEN: Pass base class generation //===----------------------------------------------------------------------===// @@ -313,7 +293,6 @@ static void emitPassDefs(const Pass &pass, raw_ostream &os) { bool emitDefaultConstructorWithOptions = !pass.getOptions().empty(); os << "#ifdef " << enableVarName << "\n"; - os << llvm::formatv(passHeader, passName); if (emitDefaultConstructors) { os << llvm::formatv(friendDefaultConstructorDeclTemplate, passName); @@ -377,14 +356,6 @@ static void emitPassDefs(const Pass &pass, raw_ostream &os) { os << "#endif // " << enableVarName << "\n"; } -static void emitDefs(const llvm::RecordKeeper &recordKeeper, raw_ostream &os) { - std::vector passes = getPasses(recordKeeper); - os << "/* Autogenerated by mlir-tblgen; don't manually edit */\n"; - - for (const Pass &pass : passes) - emitPassDefs(pass, os); -} - // TODO drop old pass declarations // The old pass base class is being kept until all the passes have switched to // the new decls/defs design. @@ -452,16 +423,36 @@ static void emitOldPassDecl(const Pass &pass, raw_ostream &os) { os << "};\n"; } +static void emitPass(const Pass &pass, raw_ostream &os) { + StringRef passName = pass.getDef()->getName(); + os << llvm::formatv(passHeader, passName); + + emitPassDecls(pass, os); + emitPassDefs(pass, os); +} + +static void emitPasses(const llvm::RecordKeeper &recordKeeper, + raw_ostream &os) { + std::vector passes = getPasses(recordKeeper); + os << "/* Autogenerated by mlir-tblgen; don't manually edit */\n"; + + for (const Pass &pass : passes) + emitPass(pass, os); + + emitRegistrations(passes, os); + + // TODO: Drop old pass declarations. + // Emit the old code until all the passes have switched to the new design. + os << "#ifdef GEN_PASS_CLASSES\n"; + for (const Pass &pass : passes) + emitOldPassDecl(pass, os); + os << "#undef GEN_PASS_CLASSES\n"; + os << "#endif // GEN_PASS_CLASSES\n"; +} + static mlir::GenRegistration genPassDecls("gen-pass-decls", "Generate pass declarations", [](const llvm::RecordKeeper &records, raw_ostream &os) { - emitDecls(records, os); + emitPasses(records, os); return false; }); - -static mlir::GenRegistration - genPassDefs("gen-pass-defs", "Generate pass definitions", - [](const llvm::RecordKeeper &records, raw_ostream &os) { - emitDefs(records, os); - return false; - }); diff --git a/mlir/unittests/TableGen/CMakeLists.txt b/mlir/unittests/TableGen/CMakeLists.txt index 436c903..c51bda6 100644 --- a/mlir/unittests/TableGen/CMakeLists.txt +++ b/mlir/unittests/TableGen/CMakeLists.txt @@ -5,7 +5,6 @@ add_public_tablegen_target(MLIRTableGenEnumsIncGen) set(LLVM_TARGET_DEFINITIONS passes.td) mlir_tablegen(PassGenTest.h.inc -gen-pass-decls -name TableGenTest) -mlir_tablegen(PassGenTest.cpp.inc -gen-pass-defs -name TableGenTest) add_public_tablegen_target(MLIRTableGenTestPassIncGen) add_mlir_unittest(MLIRTableGenTests diff --git a/mlir/unittests/TableGen/PassGenTest.cpp b/mlir/unittests/TableGen/PassGenTest.cpp index 9b752fc..b73e4bd 100644 --- a/mlir/unittests/TableGen/PassGenTest.cpp +++ b/mlir/unittests/TableGen/PassGenTest.cpp @@ -22,7 +22,7 @@ std::unique_ptr createTestPassWithCustomConstructor(int v = 0); #define GEN_PASS_DEF_TESTPASS #define GEN_PASS_DEF_TESTPASSWITHOPTIONS #define GEN_PASS_DEF_TESTPASSWITHCUSTOMCONSTRUCTOR -#include "PassGenTest.cpp.inc" +#include "PassGenTest.h.inc" struct TestPass : public impl::TestPassBase { using TestPassBase::TestPassBase; -- 2.7.4