[Mlir-commits] [mlir] f3ebf82 - [mlir] Generate Dialect constructors in .cpp instead of .h

River Riddle llvmlistbot at llvm.org
Sat Apr 23 01:33:42 PDT 2022


Author: River Riddle
Date: 2022-04-23T00:44:54-07:00
New Revision: f3ebf828dc54bf488ad49d5ec2f070ea36a31e6f

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

LOG: [mlir] Generate Dialect constructors in .cpp instead of .h

By generating in the .h file, we were forcing dialects to include
a lot of additional header files because:

* Fields of the dialect, e.g. std::unique_ptr<>, were unable to use
  forward declarations.
* Dependent dialects are loaded in the constructor, requiring the
  full definition of each dependent dialect (which, depending on
  the file structure of the dialect, may include the operations).

By generating in the .cpp we get much faster builds, and also
better align with the rest of the code base.

Fixes #55044

Differential Revision: https://reviews.llvm.org/D124297

Added: 
    

Modified: 
    mlir/tools/mlir-tblgen/DialectGen.cpp

Removed: 
    


################################################################################
diff  --git a/mlir/tools/mlir-tblgen/DialectGen.cpp b/mlir/tools/mlir-tblgen/DialectGen.cpp
index c9f036b6a32cb..b6497eb8e1f5e 100644
--- a/mlir/tools/mlir-tblgen/DialectGen.cpp
+++ b/mlir/tools/mlir-tblgen/DialectGen.cpp
@@ -87,16 +87,9 @@ findSelectedDialect(ArrayRef<const llvm::Record *> dialectDefs) {
 ///
 /// {0}: The name of the dialect class.
 /// {1}: The dialect namespace.
-/// {2}: initialization code that is emitted in the ctor body before calling
-/// initialize()
 static const char *const dialectDeclBeginStr = R"(
 class {0} : public ::mlir::Dialect {
-  explicit {0}(::mlir::MLIRContext *context)
-    : ::mlir::Dialect(getDialectNamespace(), context,
-      ::mlir::TypeID::get<{0}>()) {{
-    {2}
-    initialize();
-  }
+  explicit {0}(::mlir::MLIRContext *context);
 
   void initialize();
   friend class ::mlir::MLIRContext;
@@ -190,23 +183,13 @@ emitDialectDecl(Dialect &dialect,
                 const iterator_range<DialectFilterIterator> &dialectAttrs,
                 const iterator_range<DialectFilterIterator> &dialectTypes,
                 raw_ostream &os) {
-  /// Build the list of dependent dialects
-  std::string dependentDialectRegistrations;
-  {
-    llvm::raw_string_ostream dialectsOs(dependentDialectRegistrations);
-    for (StringRef dependentDialect : dialect.getDependentDialects())
-      dialectsOs << llvm::formatv(dialectRegistrationTemplate,
-                                  dependentDialect);
-  }
-
   // Emit all nested namespaces.
   {
     NamespaceEmitter nsEmitter(os, dialect);
 
     // Emit the start of the decl.
     std::string cppName = dialect.getCppClassName();
-    os << llvm::formatv(dialectDeclBeginStr, cppName, dialect.getName(),
-                        dependentDialectRegistrations);
+    os << llvm::formatv(dialectDeclBeginStr, cppName, dialect.getName());
 
     // Check for any attributes/types registered to this dialect.  If there are,
     // add the hooks for parsing/printing.
@@ -262,6 +245,19 @@ static bool emitDialectDecls(const llvm::RecordKeeper &recordKeeper,
 // GEN: Dialect definitions
 //===----------------------------------------------------------------------===//
 
+/// The code block to generate a dialect constructor definition.
+///
+/// {0}: The name of the dialect class.
+/// {1}: initialization code that is emitted in the ctor body before calling
+///      initialize().
+static const char *const dialectConstructorStr = R"(
+{0}::{0}(::mlir::MLIRContext *context) 
+    : ::mlir::Dialect(getDialectNamespace(), context, ::mlir::TypeID::get<{0}>()) {{
+  {1}
+  initialize();
+}
+)";
+
 /// The code block to generate a default desturctor definition.
 ///
 /// {0}: The name of the dialect class.
@@ -271,16 +267,30 @@ static const char *const dialectDestructorStr = R"(
 )";
 
 static void emitDialectDef(Dialect &dialect, raw_ostream &os) {
+  std::string cppClassName = dialect.getCppClassName();
+
   // Emit the TypeID explicit specializations to have a single symbol def.
   if (!dialect.getCppNamespace().empty())
     os << "MLIR_DEFINE_EXPLICIT_TYPE_ID(" << dialect.getCppNamespace()
-       << "::" << dialect.getCppClassName() << ")\n";
+       << "::" << cppClassName << ")\n";
 
   // Emit all nested namespaces.
   NamespaceEmitter nsEmitter(os, dialect);
 
+  /// Build the list of dependent dialects.
+  std::string dependentDialectRegistrations;
+  {
+    llvm::raw_string_ostream dialectsOs(dependentDialectRegistrations);
+    for (StringRef dependentDialect : dialect.getDependentDialects())
+      dialectsOs << llvm::formatv(dialectRegistrationTemplate,
+                                  dependentDialect);
+  }
+
+  // Emit the constructor and destructor.
+  os << llvm::formatv(dialectConstructorStr, cppClassName,
+                      dependentDialectRegistrations);
   if (!dialect.hasNonDefaultDestructor())
-    os << llvm::formatv(dialectDestructorStr, dialect.getCppClassName());
+    os << llvm::formatv(dialectDestructorStr, cppClassName);
 }
 
 static bool emitDialectDefs(const llvm::RecordKeeper &recordKeeper,


        


More information about the Mlir-commits mailing list