[Mlir-commits] [mlir] 13ed695 - [MLIR] Unique autogenerated file for tablegen passes

Michele Scuttari llvmlistbot at llvm.org
Tue Aug 30 00:53:11 PDT 2022


Author: Michele Scuttari
Date: 2022-08-30T09:48:11+02:00
New Revision: 13ed6958df40b85fcc80250bb3f819863904ecee

URL: https://github.com/llvm/llvm-project/commit/13ed6958df40b85fcc80250bb3f819863904ecee
DIFF: https://github.com/llvm/llvm-project/commit/13ed6958df40b85fcc80250bb3f819863904ecee.diff

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/mlir/docs/PassManagement.md b/mlir/docs/PassManagement.md
index e92de3cb592c4..409a15e276693 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<MyPass> {
 };
 ```
 
-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 65159559206eb..801db200fe7b1 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<Pass> passes, raw_ostream &os) {
   os << "#endif // GEN_PASS_REGISTRATION\n";
 }
 
-static void emitDecls(const llvm::RecordKeeper &recordKeeper, raw_ostream &os) {
-  std::vector<Pass> 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<Pass> 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<Pass> 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 436c903e3d973..c51bda6e8d6cc 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 9b752fc6d7616..b73e4bd715cfd 100644
--- a/mlir/unittests/TableGen/PassGenTest.cpp
+++ b/mlir/unittests/TableGen/PassGenTest.cpp
@@ -22,7 +22,7 @@ std::unique_ptr<mlir::Pass> 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<TestPass> {
   using TestPassBase::TestPassBase;


        


More information about the Mlir-commits mailing list