From 868a8fd88f84b26e810ccf18af8e621947031cbb Mon Sep 17 00:00:00 2001 From: rkayaith Date: Tue, 27 Sep 2022 17:23:59 -0400 Subject: [PATCH] [mlir] Add macro for enabling all generated pass declarations Currently the generated pass declarations have to be enabled per-pass using multiple `GEN_PASS_DECL_{PASSNAME}` defines. This adds `GEN_PASS_DECL`, which enables the declarations for all passes in the group with a single macro. This is convenient for cases where a single header is used for all passes in the group. Reviewed By: mehdi_amini, mscuttari Differential Revision: https://reviews.llvm.org/D134766 --- mlir/docs/PassManagement.md | 17 ++++++++--------- mlir/tools/mlir-tblgen/PassGen.cpp | 14 +++++++++++++- mlir/unittests/TableGen/PassGenTest.cpp | 4 +--- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/mlir/docs/PassManagement.md b/mlir/docs/PassManagement.md index 0fe7851..7748448 100644 --- a/mlir/docs/PassManagement.md +++ b/mlir/docs/PassManagement.md @@ -863,11 +863,11 @@ void registerMyPasses() { The second is to provide a way to configure the pass options. These classes are named in the form of `MyPassOptions`, where `MyPass` is the name of the pass -definition in tablegen. The configurable parameters reflect the options -declared in the tablegen file. Differently from the registration hooks, these -classes can be enabled on a per-pass basis by defining the -`GEN_PASS_DECL_PASSNAME` macro, where `PASSNAME` is the uppercase version of -the name specified in tablegen. +definition in tablegen. The configurable parameters reflect the options declared +in the tablegen file. These declarations can be enabled for the whole group of +passes by defining the `GEN_PASS_DECL` macro, or on a per-pass basis by defining +`GEN_PASS_DECL_PASSNAME` where `PASSNAME` is the uppercase version of the name +specified in tablegen. ```c++ // .h.inc @@ -924,10 +924,9 @@ struct MyPass : foo::impl::MyPassBase { }; ``` -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. +These 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 be equal to the name defined in tablegen. diff --git a/mlir/tools/mlir-tblgen/PassGen.cpp b/mlir/tools/mlir-tblgen/PassGen.cpp index d81f9d7..8bcb110 100644 --- a/mlir/tools/mlir-tblgen/PassGen.cpp +++ b/mlir/tools/mlir-tblgen/PassGen.cpp @@ -110,10 +110,14 @@ static void emitPassOptionsStruct(const Pass &pass, raw_ostream &os) { os << "};\n"; } +static std::string getPassDeclVarName(const Pass &pass) { + return "GEN_PASS_DECL_" + pass.getDef()->getName().upper(); +} + /// Emit the code to be included in the public header of the pass. static void emitPassDecls(const Pass &pass, raw_ostream &os) { StringRef passName = pass.getDef()->getName(); - std::string enableVarName = "GEN_PASS_DECL_" + passName.upper(); + std::string enableVarName = getPassDeclVarName(pass); os << "#ifdef " << enableVarName << "\n"; emitPassOptionsStruct(pass, os); @@ -435,6 +439,14 @@ static void emitPasses(const llvm::RecordKeeper &recordKeeper, std::vector passes = getPasses(recordKeeper); os << "/* Autogenerated by mlir-tblgen; don't manually edit */\n"; + os << "\n"; + os << "#ifdef GEN_PASS_DECL\n"; + os << "// Generate declarations for all passes.\n"; + for (const Pass &pass : passes) + os << "#define " << getPassDeclVarName(pass) << "\n"; + os << "#undef GEN_PASS_DECL\n"; + os << "#endif // GEN_PASS_DECL\n"; + for (const Pass &pass : passes) emitPass(pass, os); diff --git a/mlir/unittests/TableGen/PassGenTest.cpp b/mlir/unittests/TableGen/PassGenTest.cpp index b73e4bd..859eba7 100644 --- a/mlir/unittests/TableGen/PassGenTest.cpp +++ b/mlir/unittests/TableGen/PassGenTest.cpp @@ -13,9 +13,7 @@ std::unique_ptr createTestPassWithCustomConstructor(int v = 0); -#define GEN_PASS_DECL_TESTPASS -#define GEN_PASS_DECL_TESTPASSWITHOPTIONS -#define GEN_PASS_DECL_TESTPASSWITHCUSTOMCONSTRUCTOR +#define GEN_PASS_DECL #define GEN_PASS_REGISTRATION #include "PassGenTest.h.inc" -- 2.7.4