[Mlir-commits] [mlir] 868a8fd - [mlir] Add macro for enabling all generated pass declarations

llvmlistbot at llvm.org llvmlistbot at llvm.org
Wed Sep 28 08:49:37 PDT 2022


Author: rkayaith
Date: 2022-09-28T11:49:28-04:00
New Revision: 868a8fd88f84b26e810ccf18af8e621947031cbb

URL: https://github.com/llvm/llvm-project/commit/868a8fd88f84b26e810ccf18af8e621947031cbb
DIFF: https://github.com/llvm/llvm-project/commit/868a8fd88f84b26e810ccf18af8e621947031cbb.diff

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

Added: 
    

Modified: 
    mlir/docs/PassManagement.md
    mlir/tools/mlir-tblgen/PassGen.cpp
    mlir/unittests/TableGen/PassGenTest.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/docs/PassManagement.md b/mlir/docs/PassManagement.md
index 0fe78510bcc37..7748448ba1df4 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<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.

diff  --git a/mlir/tools/mlir-tblgen/PassGen.cpp b/mlir/tools/mlir-tblgen/PassGen.cpp
index d81f9d7d327d1..8bcb1100cd71b 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<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);
 

diff  --git a/mlir/unittests/TableGen/PassGenTest.cpp b/mlir/unittests/TableGen/PassGenTest.cpp
index b73e4bd715cfd..859eba7e51786 100644
--- a/mlir/unittests/TableGen/PassGenTest.cpp
+++ b/mlir/unittests/TableGen/PassGenTest.cpp
@@ -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"
 


        


More information about the Mlir-commits mailing list