[Mlir-commits] [mlir] 485cc55 - [mlir] Generare .cpp.inc files for dialects.

Stella Laurenzo llvmlistbot at llvm.org
Tue Jun 29 13:12:37 PDT 2021


Author: Stella Laurenzo
Date: 2021-06-29T20:10:30Z
New Revision: 485cc55edfb875628e19bb6d9de4706af2865d3e

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

LOG: [mlir] Generare .cpp.inc files for dialects.

* Previously, we were only generating .h.inc files. We foresee the need to also generate implementations and this is a step towards that.
* Discussed in https://llvm.discourse.group/t/generating-cpp-inc-files-for-dialects/3732/2
* Deviates from the discussion above by generating a default constructor in the .cpp.inc file (and adding a tablegen bit that disables this in case if this is user provided).
* Generating the destructor started as a way to flush out the missing includes (produces a link error), but it is a strict improvement on its own that is worth doing (i.e. by emitting key methods in the .cpp file, we root vtables in one translation unit, which is a non-controversial improvement).

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

Added: 
    

Modified: 
    mlir/cmake/modules/AddMLIR.cmake
    mlir/examples/standalone/lib/Standalone/StandaloneDialect.cpp
    mlir/examples/toy/Ch2/include/toy/CMakeLists.txt
    mlir/examples/toy/Ch2/mlir/Dialect.cpp
    mlir/examples/toy/Ch3/include/toy/CMakeLists.txt
    mlir/examples/toy/Ch3/mlir/Dialect.cpp
    mlir/examples/toy/Ch4/include/toy/CMakeLists.txt
    mlir/examples/toy/Ch4/mlir/Dialect.cpp
    mlir/examples/toy/Ch5/include/toy/CMakeLists.txt
    mlir/examples/toy/Ch5/mlir/Dialect.cpp
    mlir/examples/toy/Ch6/include/toy/CMakeLists.txt
    mlir/examples/toy/Ch6/mlir/Dialect.cpp
    mlir/examples/toy/Ch7/include/toy/CMakeLists.txt
    mlir/examples/toy/Ch7/mlir/Dialect.cpp
    mlir/include/mlir/Dialect/LLVMIR/CMakeLists.txt
    mlir/include/mlir/Dialect/OpenACC/CMakeLists.txt
    mlir/include/mlir/Dialect/OpenMP/CMakeLists.txt
    mlir/include/mlir/Dialect/StandardOps/IR/CMakeLists.txt
    mlir/include/mlir/IR/CMakeLists.txt
    mlir/include/mlir/IR/OpBase.td
    mlir/include/mlir/TableGen/Dialect.h
    mlir/lib/Dialect/AMX/IR/AMXDialect.cpp
    mlir/lib/Dialect/Affine/IR/AffineOps.cpp
    mlir/lib/Dialect/ArmNeon/IR/ArmNeonDialect.cpp
    mlir/lib/Dialect/ArmSVE/IR/ArmSVEDialect.cpp
    mlir/lib/Dialect/Async/IR/Async.cpp
    mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp
    mlir/lib/Dialect/DLTI/DLTI.cpp
    mlir/lib/Dialect/EmitC/IR/EmitC.cpp
    mlir/lib/Dialect/GPU/IR/GPUDialect.cpp
    mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
    mlir/lib/Dialect/LLVMIR/IR/NVVMDialect.cpp
    mlir/lib/Dialect/LLVMIR/IR/ROCDLDialect.cpp
    mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
    mlir/lib/Dialect/Math/IR/MathDialect.cpp
    mlir/lib/Dialect/MemRef/IR/MemRefDialect.cpp
    mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
    mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
    mlir/lib/Dialect/PDL/IR/PDL.cpp
    mlir/lib/Dialect/PDLInterp/IR/PDLInterp.cpp
    mlir/lib/Dialect/Quant/IR/QuantOps.cpp
    mlir/lib/Dialect/SCF/SCF.cpp
    mlir/lib/Dialect/SPIRV/IR/SPIRVDialect.cpp
    mlir/lib/Dialect/Shape/IR/Shape.cpp
    mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
    mlir/lib/Dialect/StandardOps/IR/Ops.cpp
    mlir/lib/Dialect/Tensor/IR/TensorDialect.cpp
    mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
    mlir/lib/Dialect/Vector/VectorOps.cpp
    mlir/lib/Dialect/X86Vector/IR/X86VectorDialect.cpp
    mlir/lib/IR/BuiltinDialect.cpp
    mlir/lib/TableGen/Dialect.cpp
    mlir/test/lib/Dialect/Test/CMakeLists.txt
    mlir/test/lib/Dialect/Test/TestDialect.cpp
    mlir/test/lib/Dialect/Test/TestOps.td
    mlir/tools/mlir-tblgen/DialectGen.cpp
    utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
    utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel

Removed: 
    


################################################################################
diff  --git a/mlir/cmake/modules/AddMLIR.cmake b/mlir/cmake/modules/AddMLIR.cmake
index 81bb528bc11d3..109ac46ab6d26 100644
--- a/mlir/cmake/modules/AddMLIR.cmake
+++ b/mlir/cmake/modules/AddMLIR.cmake
@@ -15,6 +15,7 @@ function(add_mlir_dialect dialect dialect_namespace)
   mlir_tablegen(${dialect}Types.h.inc -gen-typedef-decls)
   mlir_tablegen(${dialect}Types.cpp.inc -gen-typedef-defs)
   mlir_tablegen(${dialect}Dialect.h.inc -gen-dialect-decls -dialect=${dialect_namespace})
+  mlir_tablegen(${dialect}Dialect.cpp.inc -gen-dialect-defs -dialect=${dialect_namespace})
   add_public_tablegen_target(MLIR${dialect}IncGen)
   add_dependencies(mlir-headers MLIR${dialect}IncGen)
 endfunction()

diff  --git a/mlir/examples/standalone/lib/Standalone/StandaloneDialect.cpp b/mlir/examples/standalone/lib/Standalone/StandaloneDialect.cpp
index acdf88ab9b43c..cdd9337fcf98b 100644
--- a/mlir/examples/standalone/lib/Standalone/StandaloneDialect.cpp
+++ b/mlir/examples/standalone/lib/Standalone/StandaloneDialect.cpp
@@ -12,6 +12,8 @@
 using namespace mlir;
 using namespace mlir::standalone;
 
+#include "Standalone/StandaloneOpsDialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // Standalone dialect.
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/examples/toy/Ch2/include/toy/CMakeLists.txt b/mlir/examples/toy/Ch2/include/toy/CMakeLists.txt
index 26a0eb1f8e1b4..301baafb4412d 100644
--- a/mlir/examples/toy/Ch2/include/toy/CMakeLists.txt
+++ b/mlir/examples/toy/Ch2/include/toy/CMakeLists.txt
@@ -2,4 +2,5 @@ set(LLVM_TARGET_DEFINITIONS Ops.td)
 mlir_tablegen(Ops.h.inc -gen-op-decls)
 mlir_tablegen(Ops.cpp.inc -gen-op-defs)
 mlir_tablegen(Dialect.h.inc -gen-dialect-decls)
+mlir_tablegen(Dialect.cpp.inc -gen-dialect-defs)
 add_public_tablegen_target(ToyCh2OpsIncGen)

diff  --git a/mlir/examples/toy/Ch2/mlir/Dialect.cpp b/mlir/examples/toy/Ch2/mlir/Dialect.cpp
index 9327aaf8431a4..5213d336d5482 100644
--- a/mlir/examples/toy/Ch2/mlir/Dialect.cpp
+++ b/mlir/examples/toy/Ch2/mlir/Dialect.cpp
@@ -20,6 +20,8 @@
 using namespace mlir;
 using namespace mlir::toy;
 
+#include "toy/Dialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // ToyDialect
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/examples/toy/Ch3/include/toy/CMakeLists.txt b/mlir/examples/toy/Ch3/include/toy/CMakeLists.txt
index 15bb98ca9600a..a233f5d6da08f 100644
--- a/mlir/examples/toy/Ch3/include/toy/CMakeLists.txt
+++ b/mlir/examples/toy/Ch3/include/toy/CMakeLists.txt
@@ -2,4 +2,5 @@ set(LLVM_TARGET_DEFINITIONS Ops.td)
 mlir_tablegen(Ops.h.inc -gen-op-decls)
 mlir_tablegen(Ops.cpp.inc -gen-op-defs)
 mlir_tablegen(Dialect.h.inc -gen-dialect-decls)
+mlir_tablegen(Dialect.cpp.inc -gen-dialect-defs)
 add_public_tablegen_target(ToyCh3OpsIncGen)

diff  --git a/mlir/examples/toy/Ch3/mlir/Dialect.cpp b/mlir/examples/toy/Ch3/mlir/Dialect.cpp
index 9327aaf8431a4..5213d336d5482 100644
--- a/mlir/examples/toy/Ch3/mlir/Dialect.cpp
+++ b/mlir/examples/toy/Ch3/mlir/Dialect.cpp
@@ -20,6 +20,8 @@
 using namespace mlir;
 using namespace mlir::toy;
 
+#include "toy/Dialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // ToyDialect
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/examples/toy/Ch4/include/toy/CMakeLists.txt b/mlir/examples/toy/Ch4/include/toy/CMakeLists.txt
index f2de562aaa353..acf7e311ffd92 100644
--- a/mlir/examples/toy/Ch4/include/toy/CMakeLists.txt
+++ b/mlir/examples/toy/Ch4/include/toy/CMakeLists.txt
@@ -3,6 +3,7 @@ set(LLVM_TARGET_DEFINITIONS Ops.td)
 mlir_tablegen(Ops.h.inc -gen-op-decls)
 mlir_tablegen(Ops.cpp.inc -gen-op-defs)
 mlir_tablegen(Dialect.h.inc -gen-dialect-decls)
+mlir_tablegen(Dialect.cpp.inc -gen-dialect-defs)
 add_public_tablegen_target(ToyCh4OpsIncGen)
 
 # Most dialects should use add_mlir_interfaces().

diff  --git a/mlir/examples/toy/Ch4/mlir/Dialect.cpp b/mlir/examples/toy/Ch4/mlir/Dialect.cpp
index dd82e0409ab16..ff1d4cdcd2a9a 100644
--- a/mlir/examples/toy/Ch4/mlir/Dialect.cpp
+++ b/mlir/examples/toy/Ch4/mlir/Dialect.cpp
@@ -21,6 +21,8 @@
 using namespace mlir;
 using namespace mlir::toy;
 
+#include "toy/Dialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // ToyInlinerInterface
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/examples/toy/Ch5/include/toy/CMakeLists.txt b/mlir/examples/toy/Ch5/include/toy/CMakeLists.txt
index 10313c9d91ccd..7d51dd9c17c73 100644
--- a/mlir/examples/toy/Ch5/include/toy/CMakeLists.txt
+++ b/mlir/examples/toy/Ch5/include/toy/CMakeLists.txt
@@ -3,6 +3,7 @@ set(LLVM_TARGET_DEFINITIONS Ops.td)
 mlir_tablegen(Ops.h.inc -gen-op-decls)
 mlir_tablegen(Ops.cpp.inc -gen-op-defs)
 mlir_tablegen(Dialect.h.inc -gen-dialect-decls)
+mlir_tablegen(Dialect.cpp.inc -gen-dialect-defs)
 add_public_tablegen_target(ToyCh5OpsIncGen)
 
 # Most dialects should use add_mlir_interfaces().

diff  --git a/mlir/examples/toy/Ch5/mlir/Dialect.cpp b/mlir/examples/toy/Ch5/mlir/Dialect.cpp
index 18d5985042faa..89e7529a4a948 100644
--- a/mlir/examples/toy/Ch5/mlir/Dialect.cpp
+++ b/mlir/examples/toy/Ch5/mlir/Dialect.cpp
@@ -21,6 +21,8 @@
 using namespace mlir;
 using namespace mlir::toy;
 
+#include "toy/Dialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // ToyInlinerInterface
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/examples/toy/Ch6/include/toy/CMakeLists.txt b/mlir/examples/toy/Ch6/include/toy/CMakeLists.txt
index 4c54020302a5d..03e8a2a470078 100644
--- a/mlir/examples/toy/Ch6/include/toy/CMakeLists.txt
+++ b/mlir/examples/toy/Ch6/include/toy/CMakeLists.txt
@@ -3,6 +3,7 @@ set(LLVM_TARGET_DEFINITIONS Ops.td)
 mlir_tablegen(Ops.h.inc -gen-op-decls)
 mlir_tablegen(Ops.cpp.inc -gen-op-defs)
 mlir_tablegen(Dialect.h.inc -gen-dialect-decls)
+mlir_tablegen(Dialect.cpp.inc -gen-dialect-defs)
 add_public_tablegen_target(ToyCh6OpsIncGen)
 
 # Most dialects should use add_mlir_interfaces().

diff  --git a/mlir/examples/toy/Ch6/mlir/Dialect.cpp b/mlir/examples/toy/Ch6/mlir/Dialect.cpp
index 18d5985042faa..89e7529a4a948 100644
--- a/mlir/examples/toy/Ch6/mlir/Dialect.cpp
+++ b/mlir/examples/toy/Ch6/mlir/Dialect.cpp
@@ -21,6 +21,8 @@
 using namespace mlir;
 using namespace mlir::toy;
 
+#include "toy/Dialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // ToyInlinerInterface
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/examples/toy/Ch7/include/toy/CMakeLists.txt b/mlir/examples/toy/Ch7/include/toy/CMakeLists.txt
index 3ff7633f8ddef..7712e42ad8093 100644
--- a/mlir/examples/toy/Ch7/include/toy/CMakeLists.txt
+++ b/mlir/examples/toy/Ch7/include/toy/CMakeLists.txt
@@ -3,6 +3,7 @@ set(LLVM_TARGET_DEFINITIONS Ops.td)
 mlir_tablegen(Ops.h.inc -gen-op-decls)
 mlir_tablegen(Ops.cpp.inc -gen-op-defs)
 mlir_tablegen(Dialect.h.inc -gen-dialect-decls)
+mlir_tablegen(Dialect.cpp.inc -gen-dialect-defs)
 add_public_tablegen_target(ToyCh7OpsIncGen)
 
 # Most dialects should use add_mlir_interfaces().

diff  --git a/mlir/examples/toy/Ch7/mlir/Dialect.cpp b/mlir/examples/toy/Ch7/mlir/Dialect.cpp
index 28f5435f7e982..30d473f7bec20 100644
--- a/mlir/examples/toy/Ch7/mlir/Dialect.cpp
+++ b/mlir/examples/toy/Ch7/mlir/Dialect.cpp
@@ -22,6 +22,8 @@
 using namespace mlir;
 using namespace mlir::toy;
 
+#include "toy/Dialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // ToyInlinerInterface
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/include/mlir/Dialect/LLVMIR/CMakeLists.txt b/mlir/include/mlir/Dialect/LLVMIR/CMakeLists.txt
index ff3c8e2eeba54..91754f16e8a37 100644
--- a/mlir/include/mlir/Dialect/LLVMIR/CMakeLists.txt
+++ b/mlir/include/mlir/Dialect/LLVMIR/CMakeLists.txt
@@ -9,6 +9,7 @@ set(LLVM_TARGET_DEFINITIONS LLVMOps.td)
 mlir_tablegen(LLVMOps.h.inc -gen-op-decls)
 mlir_tablegen(LLVMOps.cpp.inc -gen-op-defs)
 mlir_tablegen(LLVMOpsDialect.h.inc -gen-dialect-decls)
+mlir_tablegen(LLVMOpsDialect.cpp.inc -gen-dialect-defs)
 mlir_tablegen(LLVMOpsEnums.h.inc -gen-enum-decls)
 mlir_tablegen(LLVMOpsEnums.cpp.inc -gen-enum-defs)
 add_public_tablegen_target(MLIRLLVMOpsIncGen)

diff  --git a/mlir/include/mlir/Dialect/OpenACC/CMakeLists.txt b/mlir/include/mlir/Dialect/OpenACC/CMakeLists.txt
index 525851588562a..241f939aedbfe 100644
--- a/mlir/include/mlir/Dialect/OpenACC/CMakeLists.txt
+++ b/mlir/include/mlir/Dialect/OpenACC/CMakeLists.txt
@@ -4,6 +4,7 @@ add_public_tablegen_target(acc_common_td)
 
 set(LLVM_TARGET_DEFINITIONS OpenACCOps.td)
 mlir_tablegen(OpenACCOpsDialect.h.inc -gen-dialect-decls -dialect=acc)
+mlir_tablegen(OpenACCOpsDialect.cpp.inc -gen-dialect-defs -dialect=acc)
 mlir_tablegen(OpenACCOps.h.inc -gen-op-decls)
 mlir_tablegen(OpenACCOps.cpp.inc -gen-op-defs)
 mlir_tablegen(OpenACCOpsEnums.h.inc -gen-enum-decls)

diff  --git a/mlir/include/mlir/Dialect/OpenMP/CMakeLists.txt b/mlir/include/mlir/Dialect/OpenMP/CMakeLists.txt
index bbcebc023c7fb..90614993cacf6 100644
--- a/mlir/include/mlir/Dialect/OpenMP/CMakeLists.txt
+++ b/mlir/include/mlir/Dialect/OpenMP/CMakeLists.txt
@@ -4,6 +4,7 @@ add_public_tablegen_target(omp_common_td)
 
 set(LLVM_TARGET_DEFINITIONS OpenMPOps.td)
 mlir_tablegen(OpenMPOpsDialect.h.inc -gen-dialect-decls -dialect=omp)
+mlir_tablegen(OpenMPOpsDialect.cpp.inc -gen-dialect-defs -dialect=omp)
 mlir_tablegen(OpenMPOps.h.inc -gen-op-decls)
 mlir_tablegen(OpenMPOps.cpp.inc -gen-op-defs)
 mlir_tablegen(OpenMPOpsEnums.h.inc -gen-enum-decls)

diff  --git a/mlir/include/mlir/Dialect/StandardOps/IR/CMakeLists.txt b/mlir/include/mlir/Dialect/StandardOps/IR/CMakeLists.txt
index 7bc39e93d913b..c1756e94ba128 100644
--- a/mlir/include/mlir/Dialect/StandardOps/IR/CMakeLists.txt
+++ b/mlir/include/mlir/Dialect/StandardOps/IR/CMakeLists.txt
@@ -2,6 +2,7 @@ set(LLVM_TARGET_DEFINITIONS Ops.td)
 mlir_tablegen(Ops.h.inc -gen-op-decls)
 mlir_tablegen(Ops.cpp.inc -gen-op-defs)
 mlir_tablegen(OpsDialect.h.inc -gen-dialect-decls)
+mlir_tablegen(OpsDialect.cpp.inc -gen-dialect-defs)
 mlir_tablegen(OpsEnums.h.inc -gen-enum-decls)
 mlir_tablegen(OpsEnums.cpp.inc -gen-enum-defs)
 add_public_tablegen_target(MLIRStandardOpsIncGen)

diff  --git a/mlir/include/mlir/IR/CMakeLists.txt b/mlir/include/mlir/IR/CMakeLists.txt
index 1a6b9c942d3f6..2757f3d6ead59 100644
--- a/mlir/include/mlir/IR/CMakeLists.txt
+++ b/mlir/include/mlir/IR/CMakeLists.txt
@@ -9,6 +9,7 @@ add_public_tablegen_target(MLIRBuiltinAttributesIncGen)
 
 set(LLVM_TARGET_DEFINITIONS BuiltinDialect.td)
 mlir_tablegen(BuiltinDialect.h.inc -gen-dialect-decls)
+mlir_tablegen(BuiltinDialect.cpp.inc -gen-dialect-defs)
 add_public_tablegen_target(MLIRBuiltinDialectIncGen)
 
 set(LLVM_TARGET_DEFINITIONS BuiltinLocationAttributes.td)

diff  --git a/mlir/include/mlir/IR/OpBase.td b/mlir/include/mlir/IR/OpBase.td
index 2b17731e95d02..59890190d2c66 100644
--- a/mlir/include/mlir/IR/OpBase.td
+++ b/mlir/include/mlir/IR/OpBase.td
@@ -267,6 +267,10 @@ class Dialect {
   // If this dialect overrides the hook for materializing constants.
   bit hasConstantMaterializer = 0;
 
+  /// If the dialect definition provides a non-default destructor.
+  /// If false, a default destructor implementation will be generated.
+  bit hasNonDefaultDestructor = 0;
+
   // If this dialect overrides the hook for verifying operation attributes.
   bit hasOperationAttrVerify = 0;
 

diff  --git a/mlir/include/mlir/TableGen/Dialect.h b/mlir/include/mlir/TableGen/Dialect.h
index 609bf4e2ec466..4c5af8eba7d14 100644
--- a/mlir/include/mlir/TableGen/Dialect.h
+++ b/mlir/include/mlir/TableGen/Dialect.h
@@ -54,9 +54,13 @@ class Dialect {
   /// Returns true if this dialect has a canonicalizer.
   bool hasCanonicalizer() const;
 
-  // Returns true if this dialect has a constant materializer.
+  /// Returns true if this dialect has a constant materializer.
   bool hasConstantMaterializer() const;
 
+  /// Returns true if the destructor definition is provided explicitly or
+  /// false if a default should be generated.
+  bool hasNonDefaultDestructor() const;
+
   /// Returns true if this dialect has an operation attribute verifier.
   bool hasOperationAttrVerify() const;
 

diff  --git a/mlir/lib/Dialect/AMX/IR/AMXDialect.cpp b/mlir/lib/Dialect/AMX/IR/AMXDialect.cpp
index ab98820b2ecbc..c5cf1f41d7098 100644
--- a/mlir/lib/Dialect/AMX/IR/AMXDialect.cpp
+++ b/mlir/lib/Dialect/AMX/IR/AMXDialect.cpp
@@ -18,6 +18,8 @@
 
 using namespace mlir;
 
+#include "mlir/Dialect/AMX/AMXDialect.cpp.inc"
+
 void amx::AMXDialect::initialize() {
   addOperations<
 #define GET_OP_LIST

diff  --git a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
index 1106636ea1437..5d4db0f1039a8 100644
--- a/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
+++ b/mlir/lib/Dialect/Affine/IR/AffineOps.cpp
@@ -26,6 +26,8 @@ using namespace mlir;
 
 #define DEBUG_TYPE "affine-analysis"
 
+#include "mlir/Dialect/Affine/IR/AffineOpsDialect.cpp.inc"
+
 /// A utility function to check if a value is defined at the top level of
 /// `region` or is an argument of `region`. A value of index type defined at the
 /// top level of a `AffineScope` region is always a valid symbol for all

diff  --git a/mlir/lib/Dialect/ArmNeon/IR/ArmNeonDialect.cpp b/mlir/lib/Dialect/ArmNeon/IR/ArmNeonDialect.cpp
index b8b8ebd35e688..b4f85adcad87c 100644
--- a/mlir/lib/Dialect/ArmNeon/IR/ArmNeonDialect.cpp
+++ b/mlir/lib/Dialect/ArmNeon/IR/ArmNeonDialect.cpp
@@ -18,6 +18,8 @@
 
 using namespace mlir;
 
+#include "mlir/Dialect/ArmNeon/ArmNeonDialect.cpp.inc"
+
 void arm_neon::ArmNeonDialect::initialize() {
   addOperations<
 #define GET_OP_LIST

diff  --git a/mlir/lib/Dialect/ArmSVE/IR/ArmSVEDialect.cpp b/mlir/lib/Dialect/ArmSVE/IR/ArmSVEDialect.cpp
index 5e5ce6ed63bc4..6a89c4ea57bf5 100644
--- a/mlir/lib/Dialect/ArmSVE/IR/ArmSVEDialect.cpp
+++ b/mlir/lib/Dialect/ArmSVE/IR/ArmSVEDialect.cpp
@@ -22,6 +22,8 @@
 using namespace mlir;
 using namespace arm_sve;
 
+#include "mlir/Dialect/ArmSVE/ArmSVEDialect.cpp.inc"
+
 static Type getI1SameShape(Type type);
 static void buildScalableCmpIOp(OpBuilder &build, OperationState &result,
                                 CmpIPredicate predicate, Value lhs, Value rhs);

diff  --git a/mlir/lib/Dialect/Async/IR/Async.cpp b/mlir/lib/Dialect/Async/IR/Async.cpp
index bd627edbd4271..3e325e21b1c56 100644
--- a/mlir/lib/Dialect/Async/IR/Async.cpp
+++ b/mlir/lib/Dialect/Async/IR/Async.cpp
@@ -14,6 +14,8 @@
 using namespace mlir;
 using namespace mlir::async;
 
+#include "mlir/Dialect/Async/IR/AsyncOpsDialect.cpp.inc"
+
 void AsyncDialect::initialize() {
   addOperations<
 #define GET_OP_LIST

diff  --git a/mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp b/mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp
index 44330361e95df..0a61ceadcfa9c 100644
--- a/mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp
+++ b/mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp
@@ -8,6 +8,8 @@
 
 #include "mlir/Dialect/Complex/IR/Complex.h"
 
+#include "mlir/Dialect/Complex/IR/ComplexOpsDialect.cpp.inc"
+
 void mlir::complex::ComplexDialect::initialize() {
   addOperations<
 #define GET_OP_LIST

diff  --git a/mlir/lib/Dialect/DLTI/DLTI.cpp b/mlir/lib/Dialect/DLTI/DLTI.cpp
index 2567be64ac1ad..20615124bd91d 100644
--- a/mlir/lib/Dialect/DLTI/DLTI.cpp
+++ b/mlir/lib/Dialect/DLTI/DLTI.cpp
@@ -16,6 +16,8 @@
 
 using namespace mlir;
 
+#include "mlir/Dialect/DLTI/DLTIDialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // DataLayoutEntryAttr
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
index 364c247f75e4f..c0fa74908c40e 100644
--- a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
+++ b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
@@ -14,6 +14,8 @@
 using namespace mlir;
 using namespace mlir::emitc;
 
+#include "mlir/Dialect/EmitC/IR/EmitCDialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // EmitCDialect
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/GPU/IR/GPUDialect.cpp b/mlir/lib/Dialect/GPU/IR/GPUDialect.cpp
index f0845c45cd63e..12730d78d2530 100644
--- a/mlir/lib/Dialect/GPU/IR/GPUDialect.cpp
+++ b/mlir/lib/Dialect/GPU/IR/GPUDialect.cpp
@@ -29,6 +29,8 @@
 using namespace mlir;
 using namespace mlir::gpu;
 
+#include "mlir/Dialect/GPU/GPUOpsDialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // MMAMatrixType
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
index 8b9a8fa74b8c7..856c41c2d0e9b 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
@@ -36,6 +36,8 @@
 using namespace mlir;
 using namespace mlir::LLVM;
 
+#include "mlir/Dialect/LLVMIR/LLVMOpsDialect.cpp.inc"
+
 static constexpr const char kVolatileAttrName[] = "volatile_";
 static constexpr const char kNonTemporalAttrName[] = "nontemporal";
 

diff  --git a/mlir/lib/Dialect/LLVMIR/IR/NVVMDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/NVVMDialect.cpp
index c16e1c2f7af4f..da5c07abe6d8b 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/NVVMDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/NVVMDialect.cpp
@@ -30,6 +30,8 @@
 using namespace mlir;
 using namespace NVVM;
 
+#include "mlir/Dialect/LLVMIR/NVVMOpsDialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // Printing/parsing for NVVM ops
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/LLVMIR/IR/ROCDLDialect.cpp b/mlir/lib/Dialect/LLVMIR/IR/ROCDLDialect.cpp
index f54fcdbca3190..fcce8902d8404 100644
--- a/mlir/lib/Dialect/LLVMIR/IR/ROCDLDialect.cpp
+++ b/mlir/lib/Dialect/LLVMIR/IR/ROCDLDialect.cpp
@@ -30,6 +30,8 @@
 using namespace mlir;
 using namespace ROCDL;
 
+#include "mlir/Dialect/LLVMIR/ROCDLOpsDialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // Parsing for ROCDL ops
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp b/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
index f4524f19f3f14..904f6dbede70b 100644
--- a/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
+++ b/mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp
@@ -36,6 +36,8 @@
 using namespace mlir;
 using namespace mlir::linalg;
 
+#include "mlir/Dialect/Linalg/IR/LinalgOpsDialect.cpp.inc"
+
 /// Forward declarations.
 
 /// Generic entry point to create the block for the region of a LinalgOp.

diff  --git a/mlir/lib/Dialect/Math/IR/MathDialect.cpp b/mlir/lib/Dialect/Math/IR/MathDialect.cpp
index 98ab368988d1d..eb21400491745 100644
--- a/mlir/lib/Dialect/Math/IR/MathDialect.cpp
+++ b/mlir/lib/Dialect/Math/IR/MathDialect.cpp
@@ -12,6 +12,8 @@
 using namespace mlir;
 using namespace mlir::math;
 
+#include "mlir/Dialect/Math/IR/MathOpsDialect.cpp.inc"
+
 namespace {
 /// This class defines the interface for handling inlining with math
 /// operations.

diff  --git a/mlir/lib/Dialect/MemRef/IR/MemRefDialect.cpp b/mlir/lib/Dialect/MemRef/IR/MemRefDialect.cpp
index ed82a4beefd1c..b6d5754fde0d0 100644
--- a/mlir/lib/Dialect/MemRef/IR/MemRefDialect.cpp
+++ b/mlir/lib/Dialect/MemRef/IR/MemRefDialect.cpp
@@ -12,6 +12,8 @@
 using namespace mlir;
 using namespace mlir::memref;
 
+#include "mlir/Dialect/MemRef/IR/MemRefOpsDialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // MemRefDialect Dialect Interfaces
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
index f823041f29221..92f8aaa8126a2 100644
--- a/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
+++ b/mlir/lib/Dialect/OpenACC/IR/OpenACC.cpp
@@ -17,6 +17,8 @@
 using namespace mlir;
 using namespace acc;
 
+#include "mlir/Dialect/OpenACC/OpenACCOpsDialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // OpenACC operations
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
index 9160ab9318b6a..30a138e6a5a27 100644
--- a/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
+++ b/mlir/lib/Dialect/OpenMP/IR/OpenMPDialect.cpp
@@ -22,6 +22,7 @@
 #include "llvm/ADT/StringSwitch.h"
 #include <cstddef>
 
+#include "mlir/Dialect/OpenMP/OpenMPOpsDialect.cpp.inc"
 #include "mlir/Dialect/OpenMP/OpenMPOpsEnums.cpp.inc"
 
 using namespace mlir;

diff  --git a/mlir/lib/Dialect/PDL/IR/PDL.cpp b/mlir/lib/Dialect/PDL/IR/PDL.cpp
index 8164c89dac544..3cb5b1bd0d082 100644
--- a/mlir/lib/Dialect/PDL/IR/PDL.cpp
+++ b/mlir/lib/Dialect/PDL/IR/PDL.cpp
@@ -16,6 +16,8 @@
 using namespace mlir;
 using namespace mlir::pdl;
 
+#include "mlir/Dialect/PDL/IR/PDLOpsDialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // PDLDialect
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/PDLInterp/IR/PDLInterp.cpp b/mlir/lib/Dialect/PDLInterp/IR/PDLInterp.cpp
index a93f3c48503cf..d149ef58586e5 100644
--- a/mlir/lib/Dialect/PDLInterp/IR/PDLInterp.cpp
+++ b/mlir/lib/Dialect/PDLInterp/IR/PDLInterp.cpp
@@ -14,6 +14,8 @@
 using namespace mlir;
 using namespace mlir::pdl_interp;
 
+#include "mlir/Dialect/PDLInterp/IR/PDLInterpOpsDialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // PDLInterp Dialect
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Quant/IR/QuantOps.cpp b/mlir/lib/Dialect/Quant/IR/QuantOps.cpp
index fa64e4dd5411e..fd0beadcd192f 100644
--- a/mlir/lib/Dialect/Quant/IR/QuantOps.cpp
+++ b/mlir/lib/Dialect/Quant/IR/QuantOps.cpp
@@ -23,6 +23,8 @@ using namespace mlir;
 using namespace mlir::quant;
 using namespace mlir::quant::detail;
 
+#include "mlir/Dialect/Quant/QuantOpsDialect.cpp.inc"
+
 void QuantizationDialect::initialize() {
   addTypes<AnyQuantizedType, CalibratedQuantizedType, UniformQuantizedType,
            UniformQuantizedPerAxisType>();

diff  --git a/mlir/lib/Dialect/SCF/SCF.cpp b/mlir/lib/Dialect/SCF/SCF.cpp
index 99d2386ced1b1..75f8430bd3e03 100644
--- a/mlir/lib/Dialect/SCF/SCF.cpp
+++ b/mlir/lib/Dialect/SCF/SCF.cpp
@@ -18,6 +18,8 @@
 using namespace mlir;
 using namespace mlir::scf;
 
+#include "mlir/Dialect/SCF/SCFOpsDialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // SCFDialect Dialect Interfaces
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/SPIRV/IR/SPIRVDialect.cpp b/mlir/lib/Dialect/SPIRV/IR/SPIRVDialect.cpp
index 7da2d5ab9f2c1..6537710b92e09 100644
--- a/mlir/lib/Dialect/SPIRV/IR/SPIRVDialect.cpp
+++ b/mlir/lib/Dialect/SPIRV/IR/SPIRVDialect.cpp
@@ -33,6 +33,8 @@
 using namespace mlir;
 using namespace mlir::spirv;
 
+#include "mlir/Dialect/SPIRV/IR/SPIRVOpsDialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // InlinerInterface
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Shape/IR/Shape.cpp b/mlir/lib/Dialect/Shape/IR/Shape.cpp
index 158a6d5763fbb..f75bfc5894b6a 100644
--- a/mlir/lib/Dialect/Shape/IR/Shape.cpp
+++ b/mlir/lib/Dialect/Shape/IR/Shape.cpp
@@ -24,6 +24,8 @@
 using namespace mlir;
 using namespace mlir::shape;
 
+#include "mlir/Dialect/Shape/IR/ShapeOpsDialect.cpp.inc"
+
 namespace {
 #include "ShapeCanonicalization.inc"
 }

diff  --git a/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp b/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
index 7bde51d2dbab1..e07dfdcb7f0cd 100644
--- a/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
+++ b/mlir/lib/Dialect/SparseTensor/IR/SparseTensorDialect.cpp
@@ -16,6 +16,8 @@
 using namespace mlir;
 using namespace mlir::sparse_tensor;
 
+#include "mlir/Dialect/SparseTensor/IR/SparseTensorOpsDialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // TensorDialect Attribute Methods.
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/StandardOps/IR/Ops.cpp b/mlir/lib/Dialect/StandardOps/IR/Ops.cpp
index f6abfc4060d0a..49c285b7fd63c 100644
--- a/mlir/lib/Dialect/StandardOps/IR/Ops.cpp
+++ b/mlir/lib/Dialect/StandardOps/IR/Ops.cpp
@@ -28,6 +28,8 @@
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/raw_ostream.h"
 
+#include "mlir/Dialect/StandardOps/IR/OpsDialect.cpp.inc"
+
 // Pull in all enum type definitions and utility function declarations.
 #include "mlir/Dialect/StandardOps/IR/OpsEnums.cpp.inc"
 

diff  --git a/mlir/lib/Dialect/Tensor/IR/TensorDialect.cpp b/mlir/lib/Dialect/Tensor/IR/TensorDialect.cpp
index 46a348bca8f9a..860e0b84cfe66 100644
--- a/mlir/lib/Dialect/Tensor/IR/TensorDialect.cpp
+++ b/mlir/lib/Dialect/Tensor/IR/TensorDialect.cpp
@@ -12,6 +12,8 @@
 using namespace mlir;
 using namespace mlir::tensor;
 
+#include "mlir/Dialect/Tensor/IR/TensorOpsDialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // TensorDialect Dialect Interfaces
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
index 83a89f3af80d6..39b864ff62c02 100644
--- a/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
+++ b/mlir/lib/Dialect/Tosa/IR/TosaOps.cpp
@@ -23,6 +23,8 @@
 using namespace mlir;
 using namespace mlir::tosa;
 
+#include "mlir/Dialect/Tosa/IR/TosaOpsDialect.cpp.inc"
+
 //===----------------------------------------------------------------------===//
 // Tosa dialect structs and interface includes.
 //===----------------------------------------------------------------------===//

diff  --git a/mlir/lib/Dialect/Vector/VectorOps.cpp b/mlir/lib/Dialect/Vector/VectorOps.cpp
index 92a672050a19e..c35413289b195 100644
--- a/mlir/lib/Dialect/Vector/VectorOps.cpp
+++ b/mlir/lib/Dialect/Vector/VectorOps.cpp
@@ -32,6 +32,7 @@
 #include "llvm/ADT/bit.h"
 #include <numeric>
 
+#include "mlir/Dialect/Vector/VectorOpsDialect.cpp.inc"
 // Pull in all enum type and utility function definitions.
 #include "mlir/Dialect/Vector/VectorOpsEnums.cpp.inc"
 

diff  --git a/mlir/lib/Dialect/X86Vector/IR/X86VectorDialect.cpp b/mlir/lib/Dialect/X86Vector/IR/X86VectorDialect.cpp
index 5c80fa3692fef..a9de16d5cb8a0 100644
--- a/mlir/lib/Dialect/X86Vector/IR/X86VectorDialect.cpp
+++ b/mlir/lib/Dialect/X86Vector/IR/X86VectorDialect.cpp
@@ -18,6 +18,8 @@
 
 using namespace mlir;
 
+#include "mlir/Dialect/X86Vector/X86VectorDialect.cpp.inc"
+
 void x86vector::X86VectorDialect::initialize() {
   addOperations<
 #define GET_OP_LIST

diff  --git a/mlir/lib/IR/BuiltinDialect.cpp b/mlir/lib/IR/BuiltinDialect.cpp
index 728443e8b64fb..25e853f141bec 100644
--- a/mlir/lib/IR/BuiltinDialect.cpp
+++ b/mlir/lib/IR/BuiltinDialect.cpp
@@ -27,6 +27,8 @@ using namespace mlir;
 // Builtin Dialect
 //===----------------------------------------------------------------------===//
 
+#include "mlir/IR/BuiltinDialect.cpp.inc"
+
 namespace {
 struct BuiltinOpAsmDialectInterface : public OpAsmDialectInterface {
   using OpAsmDialectInterface::OpAsmDialectInterface;

diff  --git a/mlir/lib/TableGen/Dialect.cpp b/mlir/lib/TableGen/Dialect.cpp
index 0cdd9d6d856e1..59e7593c9425a 100644
--- a/mlir/lib/TableGen/Dialect.cpp
+++ b/mlir/lib/TableGen/Dialect.cpp
@@ -69,6 +69,10 @@ bool Dialect::hasConstantMaterializer() const {
   return def->getValueAsBit("hasConstantMaterializer");
 }
 
+bool Dialect::hasNonDefaultDestructor() const {
+  return def->getValueAsBit("hasNonDefaultDestructor");
+}
+
 bool Dialect::hasOperationAttrVerify() const {
   return def->getValueAsBit("hasOperationAttrVerify");
 }

diff  --git a/mlir/test/lib/Dialect/Test/CMakeLists.txt b/mlir/test/lib/Dialect/Test/CMakeLists.txt
index a591ab5dd543a..5f37b09dda4c8 100644
--- a/mlir/test/lib/Dialect/Test/CMakeLists.txt
+++ b/mlir/test/lib/Dialect/Test/CMakeLists.txt
@@ -28,6 +28,7 @@ set(LLVM_TARGET_DEFINITIONS TestOps.td)
 mlir_tablegen(TestOps.h.inc -gen-op-decls)
 mlir_tablegen(TestOps.cpp.inc -gen-op-defs)
 mlir_tablegen(TestOpsDialect.h.inc -gen-dialect-decls -dialect=test)
+mlir_tablegen(TestOpsDialect.cpp.inc -gen-dialect-defs -dialect=test)
 mlir_tablegen(TestOpEnums.h.inc -gen-enum-decls)
 mlir_tablegen(TestOpEnums.cpp.inc -gen-enum-defs)
 mlir_tablegen(TestOpStructs.h.inc -gen-struct-attr-decls)

diff  --git a/mlir/test/lib/Dialect/Test/TestDialect.cpp b/mlir/test/lib/Dialect/Test/TestDialect.cpp
index ca6f18019a2ac..991d8fa48093f 100644
--- a/mlir/test/lib/Dialect/Test/TestDialect.cpp
+++ b/mlir/test/lib/Dialect/Test/TestDialect.cpp
@@ -26,6 +26,8 @@
 using namespace mlir;
 using namespace mlir::test;
 
+#include "TestOpsDialect.cpp.inc"
+
 void mlir::test::registerTestDialect(DialectRegistry &registry) {
   registry.insert<TestDialect>();
 }

diff  --git a/mlir/test/lib/Dialect/Test/TestOps.td b/mlir/test/lib/Dialect/Test/TestOps.td
index 0f1775f7a78f1..c59b9faa47fdd 100644
--- a/mlir/test/lib/Dialect/Test/TestOps.td
+++ b/mlir/test/lib/Dialect/Test/TestOps.td
@@ -31,6 +31,7 @@ def Test_Dialect : Dialect {
   let hasRegionArgAttrVerify = 1;
   let hasRegionResultAttrVerify = 1;
   let hasOperationInterfaceFallback = 1;
+  let hasNonDefaultDestructor = 1;
   let dependentDialects = ["::mlir::DLTIDialect"];
 
   let extraClassDeclaration = [{
@@ -47,8 +48,6 @@ def Test_Dialect : Dialect {
       getParseOperationHook(StringRef opName) const override;
     LogicalResult printOperation(Operation *op,
                                  OpAsmPrinter &printer) const override;
-
-    ~TestDialect();
   private:
     // Storage for a custom fallback interface.
     void *fallbackEffectOpInterfaces;

diff  --git a/mlir/tools/mlir-tblgen/DialectGen.cpp b/mlir/tools/mlir-tblgen/DialectGen.cpp
index dbbc32b3b0f4e..2ebabc5dd171c 100644
--- a/mlir/tools/mlir-tblgen/DialectGen.cpp
+++ b/mlir/tools/mlir-tblgen/DialectGen.cpp
@@ -17,6 +17,7 @@
 #include "mlir/TableGen/OpClass.h"
 #include "mlir/TableGen/Operator.h"
 #include "mlir/TableGen/Trait.h"
+#include "llvm/ADT/Optional.h"
 #include "llvm/ADT/Sequence.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/CommandLine.h"
@@ -54,6 +55,29 @@ filterForDialect(ArrayRef<llvm::Record *> records, Dialect &dialect) {
           DialectFilterIterator(records.end(), records.end(), filterFn)};
 }
 
+static Optional<Dialect>
+findSelectedDialect(ArrayRef<const llvm::Record *> dialectDefs) {
+  // Select the dialect to gen for.
+  if (dialectDefs.size() == 1 && selectedDialect.getNumOccurrences() == 0) {
+    return Dialect(dialectDefs.front());
+  }
+
+  if (selectedDialect.getNumOccurrences() == 0) {
+    llvm::errs() << "when more than 1 dialect is present, one must be selected "
+                    "via '-dialect'\n";
+    return llvm::None;
+  }
+
+  auto dialectIt = llvm::find_if(dialectDefs, [](const llvm::Record *def) {
+    return Dialect(def).getName() == selectedDialect;
+  });
+  if (dialectIt == dialectDefs.end()) {
+    llvm::errs() << "selected dialect with '-dialect' does not exist\n";
+    return llvm::None;
+  }
+  return Dialect(*dialectIt);
+}
+
 //===----------------------------------------------------------------------===//
 // GEN: Dialect declarations
 //===----------------------------------------------------------------------===//
@@ -72,9 +96,11 @@ class {0} : public ::mlir::Dialect {
     {2}
     initialize();
   }
+
   void initialize();
   friend class ::mlir::MLIRContext;
 public:
+  ~{0}() override;
   static constexpr ::llvm::StringLiteral getDialectNamespace() {
     return ::llvm::StringLiteral("{1}");
   }
@@ -210,34 +236,52 @@ static bool emitDialectDecls(const llvm::RecordKeeper &recordKeeper,
                              raw_ostream &os) {
   emitSourceFileHeader("Dialect Declarations", os);
 
-  auto defs = recordKeeper.getAllDerivedDefinitions("Dialect");
-  if (defs.empty())
+  auto dialectDefs = recordKeeper.getAllDerivedDefinitions("Dialect");
+  if (dialectDefs.empty())
     return false;
 
-  // Select the dialect to gen for.
-  const llvm::Record *dialectDef = nullptr;
-  if (defs.size() == 1 && selectedDialect.getNumOccurrences() == 0) {
-    dialectDef = defs.front();
-  } else if (selectedDialect.getNumOccurrences() == 0) {
-    llvm::errs() << "when more than 1 dialect is present, one must be selected "
-                    "via '-dialect'";
+  Optional<Dialect> dialect = findSelectedDialect(dialectDefs);
+  if (!dialect)
     return true;
-  } else {
-    auto dialectIt = llvm::find_if(defs, [](const llvm::Record *def) {
-      return Dialect(def).getName() == selectedDialect;
-    });
-    if (dialectIt == defs.end()) {
-      llvm::errs() << "selected dialect with '-dialect' does not exist";
-      return true;
-    }
-    dialectDef = *dialectIt;
-  }
-
   auto attrDefs = recordKeeper.getAllDerivedDefinitions("DialectAttr");
   auto typeDefs = recordKeeper.getAllDerivedDefinitions("DialectType");
-  Dialect dialect(dialectDef);
-  emitDialectDecl(dialect, filterForDialect<Attribute>(attrDefs, dialect),
-                  filterForDialect<Type>(typeDefs, dialect), os);
+  emitDialectDecl(*dialect, filterForDialect<Attribute>(attrDefs, *dialect),
+                  filterForDialect<Type>(typeDefs, *dialect), os);
+  return false;
+}
+
+//===----------------------------------------------------------------------===//
+// GEN: Dialect definitions
+//===----------------------------------------------------------------------===//
+
+/// The code block to generate a default desturctor definition.
+///
+/// {0}: The name of the dialect class.
+static const char *const dialectDestructorStr = R"(
+{0}::~{0}() = default;
+
+)";
+
+static void emitDialectDef(Dialect &dialect, raw_ostream &os) {
+  // Emit all nested namespaces.
+  NamespaceEmitter nsEmitter(os, dialect);
+
+  if (!dialect.hasNonDefaultDestructor())
+    os << llvm::formatv(dialectDestructorStr, dialect.getCppClassName());
+}
+
+static bool emitDialectDefs(const llvm::RecordKeeper &recordKeeper,
+                            raw_ostream &os) {
+  emitSourceFileHeader("Dialect Definitions", os);
+
+  auto dialectDefs = recordKeeper.getAllDerivedDefinitions("Dialect");
+  if (dialectDefs.empty())
+    return false;
+
+  Optional<Dialect> dialect = findSelectedDialect(dialectDefs);
+  if (!dialect)
+    return true;
+  emitDialectDef(*dialect, os);
   return false;
 }
 
@@ -250,3 +294,9 @@ static mlir::GenRegistration
                     [](const llvm::RecordKeeper &records, raw_ostream &os) {
                       return emitDialectDecls(records, os);
                     });
+
+static mlir::GenRegistration
+    genDialectDefs("gen-dialect-defs", "Generate dialect definitions",
+                   [](const llvm::RecordKeeper &records, raw_ostream &os) {
+                     return emitDialectDefs(records, os);
+                   });

diff  --git a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
index a9211f1216495..e1119061e12c3 100644
--- a/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/mlir/BUILD.bazel
@@ -129,6 +129,10 @@ gentbl_cc_library(
             ["-gen-dialect-decls"],
             "include/mlir/IR/BuiltinDialect.h.inc",
         ),
+        (
+            ["-gen-dialect-defs"],
+            "include/mlir/IR/BuiltinDialect.cpp.inc",
+        ),
     ],
     tblgen = ":mlir-tblgen",
     td_file = "include/mlir/IR/BuiltinDialect.td",
@@ -778,6 +782,10 @@ gentbl_cc_library(
             ["-gen-dialect-decls"],
             "include/mlir/Dialect/Affine/IR/AffineOpsDialect.h.inc",
         ),
+        (
+            ["-gen-dialect-defs"],
+            "include/mlir/Dialect/Affine/IR/AffineOpsDialect.cpp.inc",
+        ),
     ],
     tblgen = ":mlir-tblgen",
     td_file = "include/mlir/Dialect/Affine/IR/AffineOps.td",
@@ -847,6 +855,10 @@ gentbl_cc_library(
             ["-gen-dialect-decls"],
             "include/mlir/Dialect/EmitC/IR/EmitCDialect.h.inc",
         ),
+        (
+            ["-gen-dialect-defs"],
+            "include/mlir/Dialect/EmitC/IR/EmitCDialect.cpp.inc",
+        ),
         (
             ["-gen-op-decls"],
             "include/mlir/Dialect/EmitC/IR/EmitC.h.inc",
@@ -904,6 +916,10 @@ gentbl_cc_library(
             ["-gen-dialect-decls"],
             "include/mlir/Dialect/Async/IR/AsyncOpsDialect.h.inc",
         ),
+        (
+            ["-gen-dialect-defs"],
+            "include/mlir/Dialect/Async/IR/AsyncOpsDialect.cpp.inc",
+        ),
         (
             ["-gen-typedef-decls"],
             "include/mlir/Dialect/Async/IR/AsyncOpsTypes.h.inc",
@@ -975,6 +991,13 @@ gentbl_cc_library(
             ],
             "include/mlir/Dialect/ArmNeon/ArmNeonDialect.h.inc",
         ),
+        (
+            [
+                "-gen-dialect-defs",
+                "-dialect=arm_neon",
+            ],
+            "include/mlir/Dialect/ArmNeon/ArmNeonDialect.cpp.inc",
+        ),
         (
             ["-gen-op-decls"],
             "include/mlir/Dialect/ArmNeon/ArmNeon.h.inc",
@@ -1093,6 +1116,13 @@ gentbl_cc_library(
             ],
             "include/mlir/Dialect/ArmSVE/ArmSVEDialect.h.inc",
         ),
+        (
+            [
+                "-gen-dialect-defs",
+                "-dialect=arm_sve",
+            ],
+            "include/mlir/Dialect/ArmSVE/ArmSVEDialect.cpp.inc",
+        ),
     ],
     tblgen = ":mlir-tblgen",
     td_file = "include/mlir/Dialect/ArmSVE/ArmSVE.td",
@@ -1173,6 +1203,13 @@ gentbl_cc_library(
             ],
             "include/mlir/Dialect/AMX/AMXDialect.h.inc",
         ),
+        (
+            [
+                "-gen-dialect-defs",
+                "-dialect=amx",
+            ],
+            "include/mlir/Dialect/AMX/AMXDialect.cpp.inc",
+        ),
         (
             ["-gen-op-decls"],
             "include/mlir/Dialect/AMX/AMX.h.inc",
@@ -1261,6 +1298,13 @@ gentbl_cc_library(
             ],
             "include/mlir/Dialect/X86Vector/X86VectorDialect.h.inc",
         ),
+        (
+            [
+                "-gen-dialect-defs",
+                "-dialect=x86vector",
+            ],
+            "include/mlir/Dialect/X86Vector/X86VectorDialect.cpp.inc",
+        ),
         (
             ["-gen-op-decls"],
             "include/mlir/Dialect/X86Vector/X86Vector.h.inc",
@@ -1355,6 +1399,10 @@ gentbl_cc_library(
             ["-gen-dialect-decls"],
             "include/mlir/Dialect/SCF/SCFOpsDialect.h.inc",
         ),
+        (
+            ["-gen-dialect-defs"],
+            "include/mlir/Dialect/SCF/SCFOpsDialect.cpp.inc",
+        ),
     ],
     tblgen = ":mlir-tblgen",
     td_file = "include/mlir/Dialect/SCF/SCFOps.td",
@@ -1446,6 +1494,13 @@ gentbl_cc_library(
             ],
             "include/mlir/Dialect/SparseTensor/IR/SparseTensorOpsDialect.h.inc",
         ),
+        (
+            [
+                "-gen-dialect-defs",
+                "-dialect=sparse_tensor",
+            ],
+            "include/mlir/Dialect/SparseTensor/IR/SparseTensorOpsDialect.cpp.inc",
+        ),
         (
             ["-gen-op-decls"],
             "include/mlir/Dialect/SparseTensor/IR/SparseTensorOps.h.inc",
@@ -1586,6 +1641,10 @@ gentbl_cc_library(
             ["-gen-dialect-decls"],
             "include/mlir/Dialect/StandardOps/IR/OpsDialect.h.inc",
         ),
+        (
+            ["-gen-dialect-defs"],
+            "include/mlir/Dialect/StandardOps/IR/OpsDialect.cpp.inc",
+        ),
         (
             ["-gen-enum-decls"],
             "include/mlir/Dialect/StandardOps/IR/OpsEnums.h.inc",
@@ -2060,6 +2119,10 @@ gentbl_cc_library(
             ["-gen-dialect-decls"],
             "include/mlir/Dialect/Shape/IR/ShapeOpsDialect.h.inc",
         ),
+        (
+            ["-gen-dialect-defs"],
+            "include/mlir/Dialect/Shape/IR/ShapeOpsDialect.cpp.inc",
+        ),
     ],
     tblgen = ":mlir-tblgen",
     td_file = "include/mlir/Dialect/Shape/IR/ShapeOps.td",
@@ -2516,6 +2579,13 @@ gentbl_cc_library(
             ],
             "include/mlir/Dialect/GPU/GPUOpsDialect.h.inc",
         ),
+        (
+            [
+                "-gen-dialect-defs",
+                "-dialect=gpu",
+            ],
+            "include/mlir/Dialect/GPU/GPUOpsDialect.cpp.inc",
+        ),
         (
             ["-gen-op-interface-decls"],
             "include/mlir/Dialect/GPU/GPUOpInterfaces.h.inc",
@@ -2953,6 +3023,10 @@ gentbl_cc_library(
             ["-gen-dialect-decls"],
             "include/mlir/Dialect/LLVMIR/LLVMOpsDialect.h.inc",
         ),
+        (
+            ["-gen-dialect-defs"],
+            "include/mlir/Dialect/LLVMIR/LLVMOpsDialect.cpp.inc",
+        ),
         (
             ["-gen-enum-decls"],
             "include/mlir/Dialect/LLVMIR/LLVMOpsEnums.h.inc",
@@ -3037,6 +3111,13 @@ gentbl_cc_library(
             ],
             "include/mlir/Dialect/LLVMIR/NVVMOpsDialect.h.inc",
         ),
+        (
+            [
+                "-gen-dialect-defs",
+                "-dialect=nvvm",
+            ],
+            "include/mlir/Dialect/LLVMIR/NVVMOpsDialect.cpp.inc",
+        ),
     ],
     tblgen = ":mlir-tblgen",
     td_file = "include/mlir/Dialect/LLVMIR/NVVMOps.td",
@@ -3105,6 +3186,13 @@ gentbl_cc_library(
             ],
             "include/mlir/Dialect/LLVMIR/ROCDLOpsDialect.h.inc",
         ),
+        (
+            [
+                "-gen-dialect-defs",
+                "-dialect=rocdl",
+            ],
+            "include/mlir/Dialect/LLVMIR/ROCDLOpsDialect.cpp.inc",
+        ),
     ],
     tblgen = ":mlir-tblgen",
     td_file = "include/mlir/Dialect/LLVMIR/ROCDLOps.td",
@@ -3175,6 +3263,10 @@ gentbl_cc_library(
             ["-gen-dialect-decls"],
             "include/mlir/Dialect/PDL/IR/PDLOpsDialect.h.inc",
         ),
+        (
+            ["-gen-dialect-defs"],
+            "include/mlir/Dialect/PDL/IR/PDLOpsDialect.cpp.inc",
+        ),
     ],
     tblgen = ":mlir-tblgen",
     td_file = "include/mlir/Dialect/PDL/IR/PDLOps.td",
@@ -3250,6 +3342,13 @@ gentbl_cc_library(
             ],
             "include/mlir/Dialect/PDLInterp/IR/PDLInterpOpsDialect.h.inc",
         ),
+        (
+            [
+                "-gen-dialect-defs",
+                "-dialect=pdl_interp",
+            ],
+            "include/mlir/Dialect/PDLInterp/IR/PDLInterpOpsDialect.cpp.inc",
+        ),
     ],
     tblgen = ":mlir-tblgen",
     td_file = "include/mlir/Dialect/PDLInterp/IR/PDLInterpOps.td",
@@ -3284,6 +3383,10 @@ gentbl_cc_library(
             ["-gen-dialect-decls"],
             "include/mlir/Dialect/SPIRV/IR/SPIRVOpsDialect.h.inc",
         ),
+        (
+            ["-gen-dialect-defs"],
+            "include/mlir/Dialect/SPIRV/IR/SPIRVOpsDialect.cpp.inc",
+        ),
         (
             ["-gen-op-doc"],
             "g3doc/Dialects/SPIRV/SPIRVOps.md",
@@ -3645,6 +3748,13 @@ gentbl_cc_library(
             ],
             "include/mlir/Dialect/Tensor/IR/TensorOpsDialect.h.inc",
         ),
+        (
+            [
+                "-gen-dialect-defs",
+                "-dialect=tensor",
+            ],
+            "include/mlir/Dialect/Tensor/IR/TensorOpsDialect.cpp.inc",
+        ),
         (
             ["-gen-op-decls"],
             "include/mlir/Dialect/Tensor/IR/TensorOps.h.inc",
@@ -5152,6 +5262,13 @@ gentbl_cc_library(
             ],
             "include/mlir/Dialect/OpenACC/OpenACCOpsDialect.h.inc",
         ),
+        (
+            [
+                "-gen-dialect-defs",
+                "-dialect=acc",
+            ],
+            "include/mlir/Dialect/OpenACC/OpenACCOpsDialect.cpp.inc",
+        ),
         (
             ["-gen-op-decls"],
             "include/mlir/Dialect/OpenACC/OpenACCOps.h.inc",
@@ -5259,6 +5376,13 @@ gentbl_cc_library(
             ],
             "include/mlir/Dialect/OpenMP/OpenMPOpsDialect.h.inc",
         ),
+        (
+            [
+                "-gen-dialect-defs",
+                "-dialect=omp",
+            ],
+            "include/mlir/Dialect/OpenMP/OpenMPOpsDialect.cpp.inc",
+        ),
         (
             ["-gen-op-doc"],
             "g3doc/Dialects/OpenMP/OpenMPOps.md",
@@ -5390,6 +5514,10 @@ gentbl_cc_library(
             ["-gen-dialect-decls"],
             "include/mlir/Dialect/Quant/QuantOpsDialect.h.inc",
         ),
+        (
+            ["-gen-dialect-defs"],
+            "include/mlir/Dialect/Quant/QuantOpsDialect.cpp.inc",
+        ),
         (
             ["-gen-op-doc"],
             "g3doc/Dialects/QuantOps/QuantOps.md",
@@ -5488,6 +5616,13 @@ gentbl_cc_library(
             ],
             "include/mlir/Dialect/Linalg/IR/LinalgOpsDialect.h.inc",
         ),
+        (
+            [
+                "-gen-dialect-defs",
+                "-dialect=linalg",
+            ],
+            "include/mlir/Dialect/Linalg/IR/LinalgOpsDialect.cpp.inc",
+        ),
     ],
     tblgen = ":mlir-tblgen",
     td_file = "include/mlir/Dialect/Linalg/IR/LinalgOps.td",
@@ -5826,6 +5961,13 @@ gentbl_cc_library(
             ],
             "include/mlir/Dialect/Vector/VectorOpsDialect.h.inc",
         ),
+        (
+            [
+                "-gen-dialect-defs",
+                "-dialect=vector",
+            ],
+            "include/mlir/Dialect/Vector/VectorOpsDialect.cpp.inc",
+        ),
         (
             ["-gen-enum-decls"],
             "include/mlir/Dialect/Vector/VectorOpsEnums.h.inc",
@@ -5973,6 +6115,10 @@ gentbl_cc_library(
             ["-gen-dialect-decls"],
             "include/mlir/Dialect/Tosa/IR/TosaOpsDialect.h.inc",
         ),
+        (
+            ["-gen-dialect-defs"],
+            "include/mlir/Dialect/Tosa/IR/TosaOpsDialect.cpp.inc",
+        ),
         (
             ["-gen-op-doc"],
             "g3doc/Dialects/Tosa/TosaOps.md",
@@ -6147,6 +6293,13 @@ gentbl_cc_library(
             ],
             "include/mlir/Dialect/Complex/IR/ComplexOpsDialect.h.inc",
         ),
+        (
+            [
+                "-gen-dialect-defs",
+                "-dialect=complex",
+            ],
+            "include/mlir/Dialect/Complex/IR/ComplexOpsDialect.cpp.inc",
+        ),
     ],
     tblgen = ":mlir-tblgen",
     td_file = "include/mlir/Dialect/Complex/IR/ComplexBase.td",
@@ -6295,6 +6448,13 @@ gentbl_cc_library(
             ],
             "include/mlir/Dialect/Math/IR/MathOpsDialect.h.inc",
         ),
+        (
+            [
+                "-gen-dialect-defs",
+                "-dialect=math",
+            ],
+            "include/mlir/Dialect/Math/IR/MathOpsDialect.cpp.inc",
+        ),
     ],
     tblgen = ":mlir-tblgen",
     td_file = "include/mlir/Dialect/Math/IR/MathBase.td",
@@ -6418,6 +6578,13 @@ gentbl_cc_library(
             ],
             "include/mlir/Dialect/MemRef/IR/MemRefOpsDialect.h.inc",
         ),
+        (
+            [
+                "-gen-dialect-defs",
+                "-dialect=memref",
+            ],
+            "include/mlir/Dialect/MemRef/IR/MemRefOpsDialect.cpp.inc",
+        ),
     ],
     tblgen = ":mlir-tblgen",
     td_file = "include/mlir/Dialect/MemRef/IR/MemRefBase.td",
@@ -6535,6 +6702,13 @@ gentbl_cc_library(
             ],
             "include/mlir/Dialect/DLTI/DLTIDialect.h.inc",
         ),
+        (
+            [
+                "-gen-dialect-defs",
+                "-dialect=dlti",
+            ],
+            "include/mlir/Dialect/DLTI/DLTIDialect.cpp.inc",
+        ),
     ],
     tblgen = ":mlir-tblgen",
     td_file = "include/mlir/Dialect/DLTI/DLTIBase.td",

diff  --git a/utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel b/utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel
index 4d20116b0d0da..b8f590a1b9841 100644
--- a/utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel
+++ b/utils/bazel/llvm-project-overlay/mlir/test/BUILD.bazel
@@ -71,6 +71,13 @@ gentbl_cc_library(
             ],
             "lib/Dialect/Test/TestOpsDialect.h.inc",
         ),
+        (
+            [
+                "-gen-dialect-defs",
+                "-dialect=test",
+            ],
+            "lib/Dialect/Test/TestOpsDialect.cpp.inc",
+        ),
         (
             ["-gen-enum-decls"],
             "lib/Dialect/Test/TestOpEnums.h.inc",


        


More information about the Mlir-commits mailing list