[mlir] Add macro for enabling all generated pass declarations
authorrkayaith <rkayaith@gmail.com>
Tue, 27 Sep 2022 21:23:59 +0000 (17:23 -0400)
committerrkayaith <rkayaith@gmail.com>
Wed, 28 Sep 2022 15:49:28 +0000 (11:49 -0400)
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
mlir/tools/mlir-tblgen/PassGen.cpp
mlir/unittests/TableGen/PassGenTest.cpp

index 0fe7851..7748448 100644 (file)
@@ -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<MyPass> {
 };
 ```
 
-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.
index d81f9d7..8bcb110 100644 (file)
@@ -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<Pass> 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);
 
index b73e4bd..859eba7 100644 (file)
@@ -13,9 +13,7 @@
 
 std::unique_ptr<mlir::Pass> 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"