[llvm] [mlir] [mlir][tblgen] Expose all of MLIRTableGen as library (PR #92709)

Maksim Levental via llvm-commits llvm-commits at lists.llvm.org
Sun May 19 21:51:47 PDT 2024


https://github.com/makslevental updated https://github.com/llvm/llvm-project/pull/92709

>From d75d60f51365972935d2f76a428510801ccfcaca Mon Sep 17 00:00:00 2001
From: makslevental <maksim.levental at gmail.com>
Date: Sun, 19 May 2024 17:57:55 -0500
Subject: [PATCH 1/3] [mlir][tblgen] Expose all as library

---
 .../mlir/TableGen}/AttrOrTypeFormatGen.h      |  0
 .../mlir/TableGen}/DialectGenUtilities.h      |  0
 .../mlir/TableGen}/DocGenUtilities.h          |  0
 .../mlir/TableGen}/FormatGen.h                |  0
 .../mlir/TableGen}/OpClass.h                  |  0
 .../mlir/TableGen}/OpFormatGen.h              |  0
 .../mlir/TableGen}/OpGenHelpers.h             |  0
 .../TableGen}/AttrOrTypeDefGen.cpp            |  2 +-
 .../TableGen}/AttrOrTypeFormatGen.cpp         |  4 ++--
 .../TableGen}/BytecodeDialectGen.cpp          |  0
 mlir/lib/TableGen/CMakeLists.txt              | 24 ++++++++++++++++++-
 .../TableGen}/DialectGen.cpp                  |  2 +-
 .../TableGen}/DirectiveCommonGen.cpp          |  0
 .../TableGen}/EnumPythonBindingGen.cpp        |  2 +-
 .../mlir-tblgen => lib/TableGen}/EnumsGen.cpp |  2 +-
 .../TableGen}/FormatGen.cpp                   |  2 +-
 .../TableGen}/LLVMIRConversionGen.cpp         |  0
 .../TableGen}/LLVMIRIntrinsicGen.cpp          |  0
 .../mlir-tblgen => lib/TableGen}/OpClass.cpp  |  2 +-
 .../TableGen}/OpDefinitionsGen.cpp            |  6 ++---
 .../mlir-tblgen => lib/TableGen}/OpDocGen.cpp |  6 ++---
 .../TableGen}/OpFormatGen.cpp                 |  6 ++---
 .../TableGen}/OpGenHelpers.cpp                |  2 +-
 .../TableGen}/OpInterfacesGen.cpp             |  2 +-
 .../TableGen}/OpPythonBindingGen.cpp          |  2 +-
 .../TableGen}/PassCAPIGen.cpp                 |  0
 .../TableGen}/PassDocGen.cpp                  |  2 +-
 .../mlir-tblgen => lib/TableGen}/PassGen.cpp  |  0
 .../TableGen}/RewriterGen.cpp                 |  0
 .../TableGen}/SPIRVUtilsGen.cpp               |  0
 mlir/lib/Tools/mlir-tblgen/CMakeLists.txt     |  2 +-
 mlir/tools/mlir-tblgen/CMakeLists.txt         | 24 +------------------
 32 files changed, 46 insertions(+), 46 deletions(-)
 rename mlir/{tools/mlir-tblgen => include/mlir/TableGen}/AttrOrTypeFormatGen.h (100%)
 rename mlir/{tools/mlir-tblgen => include/mlir/TableGen}/DialectGenUtilities.h (100%)
 rename mlir/{tools/mlir-tblgen => include/mlir/TableGen}/DocGenUtilities.h (100%)
 rename mlir/{tools/mlir-tblgen => include/mlir/TableGen}/FormatGen.h (100%)
 rename mlir/{tools/mlir-tblgen => include/mlir/TableGen}/OpClass.h (100%)
 rename mlir/{tools/mlir-tblgen => include/mlir/TableGen}/OpFormatGen.h (100%)
 rename mlir/{tools/mlir-tblgen => include/mlir/TableGen}/OpGenHelpers.h (100%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/AttrOrTypeDefGen.cpp (99%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/AttrOrTypeFormatGen.cpp (99%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/BytecodeDialectGen.cpp (100%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/DialectGen.cpp (99%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/DirectiveCommonGen.cpp (100%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/EnumPythonBindingGen.cpp (99%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/EnumsGen.cpp (99%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/FormatGen.cpp (99%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/LLVMIRConversionGen.cpp (100%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/LLVMIRIntrinsicGen.cpp (100%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/OpClass.cpp (97%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/OpDefinitionsGen.cpp (99%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/OpDocGen.cpp (99%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/OpFormatGen.cpp (99%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/OpGenHelpers.cpp (98%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/OpInterfacesGen.cpp (99%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/OpPythonBindingGen.cpp (99%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/PassCAPIGen.cpp (100%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/PassDocGen.cpp (98%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/PassGen.cpp (100%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/RewriterGen.cpp (100%)
 rename mlir/{tools/mlir-tblgen => lib/TableGen}/SPIRVUtilsGen.cpp (100%)

diff --git a/mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.h b/mlir/include/mlir/TableGen/AttrOrTypeFormatGen.h
similarity index 100%
rename from mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.h
rename to mlir/include/mlir/TableGen/AttrOrTypeFormatGen.h
diff --git a/mlir/tools/mlir-tblgen/DialectGenUtilities.h b/mlir/include/mlir/TableGen/DialectGenUtilities.h
similarity index 100%
rename from mlir/tools/mlir-tblgen/DialectGenUtilities.h
rename to mlir/include/mlir/TableGen/DialectGenUtilities.h
diff --git a/mlir/tools/mlir-tblgen/DocGenUtilities.h b/mlir/include/mlir/TableGen/DocGenUtilities.h
similarity index 100%
rename from mlir/tools/mlir-tblgen/DocGenUtilities.h
rename to mlir/include/mlir/TableGen/DocGenUtilities.h
diff --git a/mlir/tools/mlir-tblgen/FormatGen.h b/mlir/include/mlir/TableGen/FormatGen.h
similarity index 100%
rename from mlir/tools/mlir-tblgen/FormatGen.h
rename to mlir/include/mlir/TableGen/FormatGen.h
diff --git a/mlir/tools/mlir-tblgen/OpClass.h b/mlir/include/mlir/TableGen/OpClass.h
similarity index 100%
rename from mlir/tools/mlir-tblgen/OpClass.h
rename to mlir/include/mlir/TableGen/OpClass.h
diff --git a/mlir/tools/mlir-tblgen/OpFormatGen.h b/mlir/include/mlir/TableGen/OpFormatGen.h
similarity index 100%
rename from mlir/tools/mlir-tblgen/OpFormatGen.h
rename to mlir/include/mlir/TableGen/OpFormatGen.h
diff --git a/mlir/tools/mlir-tblgen/OpGenHelpers.h b/mlir/include/mlir/TableGen/OpGenHelpers.h
similarity index 100%
rename from mlir/tools/mlir-tblgen/OpGenHelpers.h
rename to mlir/include/mlir/TableGen/OpGenHelpers.h
diff --git a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp b/mlir/lib/TableGen/AttrOrTypeDefGen.cpp
similarity index 99%
rename from mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
rename to mlir/lib/TableGen/AttrOrTypeDefGen.cpp
index 55bc0714c20ec..4f792b2cfa076 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
+++ b/mlir/lib/TableGen/AttrOrTypeDefGen.cpp
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "AttrOrTypeFormatGen.h"
+#include "mlir/TableGen/AttrOrTypeFormatGen.h"
 #include "mlir/TableGen/AttrOrTypeDef.h"
 #include "mlir/TableGen/Class.h"
 #include "mlir/TableGen/CodeGenHelpers.h"
diff --git a/mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp b/mlir/lib/TableGen/AttrOrTypeFormatGen.cpp
similarity index 99%
rename from mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp
rename to mlir/lib/TableGen/AttrOrTypeFormatGen.cpp
index abd1fbdaf8c64..bba237ddb47f1 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeFormatGen.cpp
+++ b/mlir/lib/TableGen/AttrOrTypeFormatGen.cpp
@@ -6,8 +6,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "AttrOrTypeFormatGen.h"
-#include "FormatGen.h"
+#include "mlir/TableGen/AttrOrTypeFormatGen.h"
+#include "mlir/TableGen/FormatGen.h"
 #include "mlir/Support/LLVM.h"
 #include "mlir/Support/LogicalResult.h"
 #include "mlir/TableGen/AttrOrTypeDef.h"
diff --git a/mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp b/mlir/lib/TableGen/BytecodeDialectGen.cpp
similarity index 100%
rename from mlir/tools/mlir-tblgen/BytecodeDialectGen.cpp
rename to mlir/lib/TableGen/BytecodeDialectGen.cpp
diff --git a/mlir/lib/TableGen/CMakeLists.txt b/mlir/lib/TableGen/CMakeLists.txt
index 61e14feb6dc12..e7a3ec433c926 100644
--- a/mlir/lib/TableGen/CMakeLists.txt
+++ b/mlir/lib/TableGen/CMakeLists.txt
@@ -13,22 +13,44 @@
 # this means that MLIRSupport (outside of header-only access) cannot be used.
 llvm_add_library(MLIRTableGen STATIC
   Argument.cpp
-  Attribute.cpp
   AttrOrTypeDef.cpp
+  AttrOrTypeDefGen.cpp
+  AttrOrTypeFormatGen.cpp
+  Attribute.cpp
   Builder.cpp
+  BytecodeDialectGen.cpp
   Class.cpp
   CodeGenHelpers.cpp
   Constraint.cpp
   Dialect.cpp
+  DialectGen.cpp
+  DirectiveCommonGen.cpp
+  EnumPythonBindingGen.cpp
+  EnumsGen.cpp
   Format.cpp
+  FormatGen.cpp
   GenInfo.cpp
   Interfaces.cpp
+  LLVMIRConversionGen.cpp
+  LLVMIRIntrinsicGen.cpp
+  OpClass.cpp
+  OpDefinitionsGen.cpp
+  OpDocGen.cpp
+  OpFormatGen.cpp
+  OpGenHelpers.cpp
+  OpInterfacesGen.cpp
+  OpPythonBindingGen.cpp
   Operator.cpp
   Pass.cpp
+  PassCAPIGen.cpp
+  PassDocGen.cpp
+  PassGen.cpp
   Pattern.cpp
   Predicate.cpp
   Property.cpp
   Region.cpp
+  RewriterGen.cpp
+  SPIRVUtilsGen.cpp
   SideEffects.cpp
   Successor.cpp
   Trait.cpp
diff --git a/mlir/tools/mlir-tblgen/DialectGen.cpp b/mlir/lib/TableGen/DialectGen.cpp
similarity index 99%
rename from mlir/tools/mlir-tblgen/DialectGen.cpp
rename to mlir/lib/TableGen/DialectGen.cpp
index 46e585a351a2b..8b4cdc52adc8a 100644
--- a/mlir/tools/mlir-tblgen/DialectGen.cpp
+++ b/mlir/lib/TableGen/DialectGen.cpp
@@ -10,7 +10,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "DialectGenUtilities.h"
+#include "mlir/TableGen/DialectGenUtilities.h"
 #include "mlir/TableGen/Class.h"
 #include "mlir/TableGen/CodeGenHelpers.h"
 #include "mlir/TableGen/Format.h"
diff --git a/mlir/tools/mlir-tblgen/DirectiveCommonGen.cpp b/mlir/lib/TableGen/DirectiveCommonGen.cpp
similarity index 100%
rename from mlir/tools/mlir-tblgen/DirectiveCommonGen.cpp
rename to mlir/lib/TableGen/DirectiveCommonGen.cpp
diff --git a/mlir/tools/mlir-tblgen/EnumPythonBindingGen.cpp b/mlir/lib/TableGen/EnumPythonBindingGen.cpp
similarity index 99%
rename from mlir/tools/mlir-tblgen/EnumPythonBindingGen.cpp
rename to mlir/lib/TableGen/EnumPythonBindingGen.cpp
index f4ced0803772e..63f4ad6ccbdd1 100644
--- a/mlir/tools/mlir-tblgen/EnumPythonBindingGen.cpp
+++ b/mlir/lib/TableGen/EnumPythonBindingGen.cpp
@@ -10,7 +10,7 @@
 // generate the corresponding Python binding classes.
 //
 //===----------------------------------------------------------------------===//
-#include "OpGenHelpers.h"
+#include "mlir/TableGen/OpGenHelpers.h"
 
 #include "mlir/TableGen/AttrOrTypeDef.h"
 #include "mlir/TableGen/Attribute.h"
diff --git a/mlir/tools/mlir-tblgen/EnumsGen.cpp b/mlir/lib/TableGen/EnumsGen.cpp
similarity index 99%
rename from mlir/tools/mlir-tblgen/EnumsGen.cpp
rename to mlir/lib/TableGen/EnumsGen.cpp
index f1d7a233b66a9..85bd7e8c59e01 100644
--- a/mlir/tools/mlir-tblgen/EnumsGen.cpp
+++ b/mlir/lib/TableGen/EnumsGen.cpp
@@ -10,7 +10,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "FormatGen.h"
+#include "mlir/TableGen/FormatGen.h"
 #include "mlir/TableGen/Attribute.h"
 #include "mlir/TableGen/Format.h"
 #include "mlir/TableGen/GenInfo.h"
diff --git a/mlir/tools/mlir-tblgen/FormatGen.cpp b/mlir/lib/TableGen/FormatGen.cpp
similarity index 99%
rename from mlir/tools/mlir-tblgen/FormatGen.cpp
rename to mlir/lib/TableGen/FormatGen.cpp
index 7540e584b8fac..417f7ff89187f 100644
--- a/mlir/tools/mlir-tblgen/FormatGen.cpp
+++ b/mlir/lib/TableGen/FormatGen.cpp
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "FormatGen.h"
+#include "mlir/TableGen/FormatGen.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/TableGen/Error.h"
diff --git a/mlir/tools/mlir-tblgen/LLVMIRConversionGen.cpp b/mlir/lib/TableGen/LLVMIRConversionGen.cpp
similarity index 100%
rename from mlir/tools/mlir-tblgen/LLVMIRConversionGen.cpp
rename to mlir/lib/TableGen/LLVMIRConversionGen.cpp
diff --git a/mlir/tools/mlir-tblgen/LLVMIRIntrinsicGen.cpp b/mlir/lib/TableGen/LLVMIRIntrinsicGen.cpp
similarity index 100%
rename from mlir/tools/mlir-tblgen/LLVMIRIntrinsicGen.cpp
rename to mlir/lib/TableGen/LLVMIRIntrinsicGen.cpp
diff --git a/mlir/tools/mlir-tblgen/OpClass.cpp b/mlir/lib/TableGen/OpClass.cpp
similarity index 97%
rename from mlir/tools/mlir-tblgen/OpClass.cpp
rename to mlir/lib/TableGen/OpClass.cpp
index 60fa1833ce625..7d4529835cdb4 100644
--- a/mlir/tools/mlir-tblgen/OpClass.cpp
+++ b/mlir/lib/TableGen/OpClass.cpp
@@ -6,7 +6,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "OpClass.h"
+#include "mlir/TableGen/OpClass.h"
 
 using namespace mlir;
 using namespace mlir::tblgen;
diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/lib/TableGen/OpDefinitionsGen.cpp
similarity index 99%
rename from mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
rename to mlir/lib/TableGen/OpDefinitionsGen.cpp
index e013ccac5dd0f..a4e702f4b24ac 100644
--- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
+++ b/mlir/lib/TableGen/OpDefinitionsGen.cpp
@@ -11,9 +11,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "OpClass.h"
-#include "OpFormatGen.h"
-#include "OpGenHelpers.h"
+#include "mlir/TableGen/OpClass.h"
+#include "mlir/TableGen/OpFormatGen.h"
+#include "mlir/TableGen/OpGenHelpers.h"
 #include "mlir/TableGen/Argument.h"
 #include "mlir/TableGen/Attribute.h"
 #include "mlir/TableGen/Class.h"
diff --git a/mlir/tools/mlir-tblgen/OpDocGen.cpp b/mlir/lib/TableGen/OpDocGen.cpp
similarity index 99%
rename from mlir/tools/mlir-tblgen/OpDocGen.cpp
rename to mlir/lib/TableGen/OpDocGen.cpp
index 7cd2690ea8155..de77d0a3614ae 100644
--- a/mlir/tools/mlir-tblgen/OpDocGen.cpp
+++ b/mlir/lib/TableGen/OpDocGen.cpp
@@ -11,9 +11,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "DialectGenUtilities.h"
-#include "DocGenUtilities.h"
-#include "OpGenHelpers.h"
+#include "mlir/TableGen/DialectGenUtilities.h"
+#include "mlir/TableGen/DocGenUtilities.h"
+#include "mlir/TableGen/OpGenHelpers.h"
 #include "mlir/Support/IndentedOstream.h"
 #include "mlir/TableGen/AttrOrTypeDef.h"
 #include "mlir/TableGen/GenInfo.h"
diff --git a/mlir/tools/mlir-tblgen/OpFormatGen.cpp b/mlir/lib/TableGen/OpFormatGen.cpp
similarity index 99%
rename from mlir/tools/mlir-tblgen/OpFormatGen.cpp
rename to mlir/lib/TableGen/OpFormatGen.cpp
index f7cc0a292b8c5..4e157d4092d0e 100644
--- a/mlir/tools/mlir-tblgen/OpFormatGen.cpp
+++ b/mlir/lib/TableGen/OpFormatGen.cpp
@@ -6,9 +6,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "OpFormatGen.h"
-#include "FormatGen.h"
-#include "OpClass.h"
+#include "mlir/TableGen/OpFormatGen.h"
+#include "mlir/TableGen/FormatGen.h"
+#include "mlir/TableGen/OpClass.h"
 #include "mlir/Support/LLVM.h"
 #include "mlir/TableGen/Class.h"
 #include "mlir/TableGen/Format.h"
diff --git a/mlir/tools/mlir-tblgen/OpGenHelpers.cpp b/mlir/lib/TableGen/OpGenHelpers.cpp
similarity index 98%
rename from mlir/tools/mlir-tblgen/OpGenHelpers.cpp
rename to mlir/lib/TableGen/OpGenHelpers.cpp
index c2a2423a24026..2dd4cd66b6b66 100644
--- a/mlir/tools/mlir-tblgen/OpGenHelpers.cpp
+++ b/mlir/lib/TableGen/OpGenHelpers.cpp
@@ -10,7 +10,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "OpGenHelpers.h"
+#include "mlir/TableGen/OpGenHelpers.h"
 #include "llvm/ADT/StringSet.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FormatVariadic.h"
diff --git a/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp b/mlir/lib/TableGen/OpInterfacesGen.cpp
similarity index 99%
rename from mlir/tools/mlir-tblgen/OpInterfacesGen.cpp
rename to mlir/lib/TableGen/OpInterfacesGen.cpp
index 2a7406f42f34b..a76fbbbbc7a8d 100644
--- a/mlir/tools/mlir-tblgen/OpInterfacesGen.cpp
+++ b/mlir/lib/TableGen/OpInterfacesGen.cpp
@@ -10,7 +10,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "DocGenUtilities.h"
+#include "mlir/TableGen/DocGenUtilities.h"
 #include "mlir/TableGen/Format.h"
 #include "mlir/TableGen/GenInfo.h"
 #include "mlir/TableGen/Interfaces.h"
diff --git a/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp b/mlir/lib/TableGen/OpPythonBindingGen.cpp
similarity index 99%
rename from mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
rename to mlir/lib/TableGen/OpPythonBindingGen.cpp
index 052020acdcb76..43389791a4069 100644
--- a/mlir/tools/mlir-tblgen/OpPythonBindingGen.cpp
+++ b/mlir/lib/TableGen/OpPythonBindingGen.cpp
@@ -11,7 +11,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "OpGenHelpers.h"
+#include "mlir/TableGen/OpGenHelpers.h"
 
 #include "mlir/TableGen/GenInfo.h"
 #include "mlir/TableGen/Operator.h"
diff --git a/mlir/tools/mlir-tblgen/PassCAPIGen.cpp b/mlir/lib/TableGen/PassCAPIGen.cpp
similarity index 100%
rename from mlir/tools/mlir-tblgen/PassCAPIGen.cpp
rename to mlir/lib/TableGen/PassCAPIGen.cpp
diff --git a/mlir/tools/mlir-tblgen/PassDocGen.cpp b/mlir/lib/TableGen/PassDocGen.cpp
similarity index 98%
rename from mlir/tools/mlir-tblgen/PassDocGen.cpp
rename to mlir/lib/TableGen/PassDocGen.cpp
index 8febba1915625..ac63d2126b785 100644
--- a/mlir/tools/mlir-tblgen/PassDocGen.cpp
+++ b/mlir/lib/TableGen/PassDocGen.cpp
@@ -10,7 +10,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "DocGenUtilities.h"
+#include "mlir/TableGen/DocGenUtilities.h"
 #include "mlir/TableGen/GenInfo.h"
 #include "mlir/TableGen/Pass.h"
 #include "llvm/Support/FormatVariadic.h"
diff --git a/mlir/tools/mlir-tblgen/PassGen.cpp b/mlir/lib/TableGen/PassGen.cpp
similarity index 100%
rename from mlir/tools/mlir-tblgen/PassGen.cpp
rename to mlir/lib/TableGen/PassGen.cpp
diff --git a/mlir/tools/mlir-tblgen/RewriterGen.cpp b/mlir/lib/TableGen/RewriterGen.cpp
similarity index 100%
rename from mlir/tools/mlir-tblgen/RewriterGen.cpp
rename to mlir/lib/TableGen/RewriterGen.cpp
diff --git a/mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp b/mlir/lib/TableGen/SPIRVUtilsGen.cpp
similarity index 100%
rename from mlir/tools/mlir-tblgen/SPIRVUtilsGen.cpp
rename to mlir/lib/TableGen/SPIRVUtilsGen.cpp
diff --git a/mlir/lib/Tools/mlir-tblgen/CMakeLists.txt b/mlir/lib/Tools/mlir-tblgen/CMakeLists.txt
index ed0033ff6d7cb..dae3ff9b082d0 100644
--- a/mlir/lib/Tools/mlir-tblgen/CMakeLists.txt
+++ b/mlir/lib/Tools/mlir-tblgen/CMakeLists.txt
@@ -13,7 +13,7 @@ llvm_add_library(MLIRTblgenLib STATIC
 
   LINK_LIBS PUBLIC
   MLIRTableGen
-  )
+)
 
 mlir_check_all_link_libraries(MLIRTblgenLib)
 
diff --git a/mlir/tools/mlir-tblgen/CMakeLists.txt b/mlir/tools/mlir-tblgen/CMakeLists.txt
index f2c5e4b3f87af..b8512b912fb7b 100644
--- a/mlir/tools/mlir-tblgen/CMakeLists.txt
+++ b/mlir/tools/mlir-tblgen/CMakeLists.txt
@@ -8,30 +8,8 @@ set(LLVM_LINK_COMPONENTS
 add_tablegen(mlir-tblgen MLIR
   DESTINATION "${MLIR_TOOLS_INSTALL_DIR}"
   EXPORT MLIR
-  AttrOrTypeDefGen.cpp
-  AttrOrTypeFormatGen.cpp
-  BytecodeDialectGen.cpp
-  DialectGen.cpp
-  DirectiveCommonGen.cpp
-  EnumsGen.cpp
-  EnumPythonBindingGen.cpp
-  FormatGen.cpp
-  LLVMIRConversionGen.cpp
-  LLVMIRIntrinsicGen.cpp
   mlir-tblgen.cpp
-  OpClass.cpp
-  OpDefinitionsGen.cpp
-  OpDocGen.cpp
-  OpFormatGen.cpp
-  OpGenHelpers.cpp
-  OpInterfacesGen.cpp
-  OpPythonBindingGen.cpp
-  PassCAPIGen.cpp
-  PassDocGen.cpp
-  PassGen.cpp
-  RewriterGen.cpp
-  SPIRVUtilsGen.cpp
-  )
+)
 
 set_target_properties(mlir-tblgen PROPERTIES FOLDER "Tablegenning")
 target_link_libraries(mlir-tblgen

>From 35f91b77c047ea147e6039136c6ed90f946e6750 Mon Sep 17 00:00:00 2001
From: makslevental <maksim.levental at gmail.com>
Date: Sun, 19 May 2024 20:47:39 -0500
Subject: [PATCH 2/3] move all emitters to header

---
 .../WebAssembly/WebAssemblyAsmPrinter.cpp     |   2 +-
 mlir/include/mlir/TableGen/AttrOrTypeDef.h    |  62 +++
 .../mlir/TableGen/AttrOrTypeFormatGen.h       |   2 +-
 .../mlir/TableGen/DialectGenUtilities.h       |   5 +-
 mlir/include/mlir/TableGen/FormatGen.h        |   3 -
 mlir/include/mlir/TableGen/GenInfo.h          |  82 ++++
 mlir/include/mlir/TableGen/OpFormatGen.h      |   3 +-
 mlir/include/mlir/TableGen/OpGenHelpers.h     |   7 +-
 mlir/include/mlir/TableGen/OpInterfacesGen.h  | 142 ++++++
 mlir/lib/TableGen/AttrOrTypeDefGen.cpp        | 122 +----
 mlir/lib/TableGen/AttrOrTypeFormatGen.cpp     |   7 +-
 mlir/lib/TableGen/BytecodeDialectGen.cpp      |  19 +-
 mlir/lib/TableGen/Constraint.cpp              |   2 +-
 mlir/lib/TableGen/DialectGen.cpp              |  53 +--
 mlir/lib/TableGen/DirectiveCommonGen.cpp      |  30 +-
 mlir/lib/TableGen/EnumPythonBindingGen.cpp    |  10 +-
 mlir/lib/TableGen/EnumsGen.cpp                |  22 +-
 mlir/lib/TableGen/FormatGen.cpp               |   9 -
 mlir/lib/TableGen/LLVMIRConversionGen.cpp     |  52 +-
 mlir/lib/TableGen/LLVMIRIntrinsicGen.cpp      |  46 +-
 mlir/lib/TableGen/OpDefinitionsGen.cpp        |  99 ++--
 mlir/lib/TableGen/OpDocGen.cpp                | 100 ++--
 mlir/lib/TableGen/OpFormatGen.cpp             |   7 +-
 mlir/lib/TableGen/OpGenHelpers.cpp            |  23 +-
 mlir/lib/TableGen/OpInterfacesGen.cpp         | 157 +-----
 mlir/lib/TableGen/OpPythonBindingGen.cpp      |  31 +-
 mlir/lib/TableGen/Operator.cpp                |   3 +-
 mlir/lib/TableGen/PassCAPIGen.cpp             |  31 +-
 mlir/lib/TableGen/PassDocGen.cpp              |  10 +-
 mlir/lib/TableGen/PassGen.cpp                 |  22 +-
 mlir/lib/TableGen/RewriterGen.cpp             |  20 +-
 mlir/lib/TableGen/SPIRVUtilsGen.cpp           | 128 +----
 mlir/tools/mlir-tblgen/mlir-tblgen.cpp        | 445 ++++++++++++++++++
 33 files changed, 1001 insertions(+), 755 deletions(-)
 create mode 100644 mlir/include/mlir/TableGen/OpInterfacesGen.h

diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
index 3524abba8990a..2ba3bb6310df9 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
@@ -387,7 +387,7 @@ void WebAssemblyAsmPrinter::emitDecls(const Module &M) {
 void WebAssemblyAsmPrinter::emitEndOfAsmFile(Module &M) {
   // This is required to emit external declarations (like .functypes) when
   // no functions are defined in the compilation unit and therefore,
-  // emitDecls() is not called until now.
+  // emitDirectiveDecls() is not called until now.
   emitDecls(M);
 
   // When a function's address is taken, a TABLE_INDEX relocation is emitted
diff --git a/mlir/include/mlir/TableGen/AttrOrTypeDef.h b/mlir/include/mlir/TableGen/AttrOrTypeDef.h
index 19c3a9183ec2c..56fd0264774be 100644
--- a/mlir/include/mlir/TableGen/AttrOrTypeDef.h
+++ b/mlir/include/mlir/TableGen/AttrOrTypeDef.h
@@ -17,6 +17,7 @@
 #include "mlir/Support/LLVM.h"
 #include "mlir/TableGen/Builder.h"
 #include "mlir/TableGen/Trait.h"
+#include "llvm/TableGen/Record.h"
 
 namespace llvm {
 class DagInit;
@@ -277,6 +278,67 @@ class TypeDef : public AttrOrTypeDef {
   StringRef getTypeName() const;
 };
 
+//===----------------------------------------------------------------------===//
+// DefGenerator
+//===----------------------------------------------------------------------===//
+
+/// This struct is the base generator used when processing tablegen interfaces.
+class DefGenerator {
+public:
+  bool emitDecls(StringRef selectedDialect);
+  bool emitDefs(StringRef selectedDialect);
+
+protected:
+  DefGenerator(std::vector<llvm::Record *> &&defs, raw_ostream &os,
+               StringRef defType, StringRef valueType, bool isAttrGenerator,
+               bool formatErrorIsFatal)
+      : defRecords(std::move(defs)), os(os), defType(defType),
+        valueType(valueType), isAttrGenerator(isAttrGenerator),
+        formatErrorIsFatal(formatErrorIsFatal) {
+    // Sort by occurrence in file.
+    llvm::sort(defRecords, [](llvm::Record *lhs, llvm::Record *rhs) {
+      return lhs->getID() < rhs->getID();
+    });
+  }
+
+  /// Emit the list of def type names.
+  void emitTypeDefList(ArrayRef<AttrOrTypeDef> defs);
+  /// Emit the code to dispatch between different defs during parsing/printing.
+  void emitParsePrintDispatch(ArrayRef<AttrOrTypeDef> defs);
+
+  /// The set of def records to emit.
+  std::vector<llvm::Record *> defRecords;
+  /// The attribute or type class to emit.
+  /// The stream to emit to.
+  raw_ostream &os;
+  /// The prefix of the tablegen def name, e.g. Attr or Type.
+  StringRef defType;
+  /// The C++ base value type of the def, e.g. Attribute or Type.
+  StringRef valueType;
+  /// Flag indicating if this generator is for Attributes. False if the
+  /// generator is for types.
+  bool isAttrGenerator;
+  /// Whether a failure in parsing the assembly format should be a fatal error.
+  bool formatErrorIsFatal;
+};
+
+/// A specialized generator for AttrDefs.
+struct AttrDefGenerator : public DefGenerator {
+  AttrDefGenerator(const llvm::RecordKeeper &records, raw_ostream &os,
+                   bool formatErrorIsFatal)
+      : DefGenerator(records.getAllDerivedDefinitionsIfDefined("AttrDef"), os,
+                     "Attr", "Attribute", /*isAttrGenerator=*/true,
+                     formatErrorIsFatal) {}
+};
+/// A specialized generator for TypeDefs.
+struct TypeDefGenerator : public DefGenerator {
+  TypeDefGenerator(const llvm::RecordKeeper &records, raw_ostream &os,
+                   bool formatErrorIsFatal)
+      : DefGenerator(records.getAllDerivedDefinitionsIfDefined("TypeDef"), os,
+                     "Type", "Type", /*isAttrGenerator=*/false,
+                     formatErrorIsFatal) {}
+};
+
 } // namespace tblgen
 } // namespace mlir
 
diff --git a/mlir/include/mlir/TableGen/AttrOrTypeFormatGen.h b/mlir/include/mlir/TableGen/AttrOrTypeFormatGen.h
index d4711532a79bb..8a3b38f7a1235 100644
--- a/mlir/include/mlir/TableGen/AttrOrTypeFormatGen.h
+++ b/mlir/include/mlir/TableGen/AttrOrTypeFormatGen.h
@@ -18,7 +18,7 @@ class AttrOrTypeDef;
 /// Generate a parser and printer based on a custom assembly format for an
 /// attribute or type.
 void generateAttrOrTypeFormat(const AttrOrTypeDef &def, MethodBody &parser,
-                              MethodBody &printer);
+                              MethodBody &printer, bool formatErrorIsFatal);
 
 } // namespace tblgen
 } // namespace mlir
diff --git a/mlir/include/mlir/TableGen/DialectGenUtilities.h b/mlir/include/mlir/TableGen/DialectGenUtilities.h
index 979a9d67b4047..d0f7f3f6f8819 100644
--- a/mlir/include/mlir/TableGen/DialectGenUtilities.h
+++ b/mlir/include/mlir/TableGen/DialectGenUtilities.h
@@ -10,6 +10,7 @@
 #define MLIR_TOOLS_MLIRTBLGEN_DIALECTGENUTILITIES_H_
 
 #include "mlir/Support/LLVM.h"
+#include "llvm/Support/CommandLine.h"
 
 namespace mlir {
 namespace tblgen {
@@ -17,7 +18,9 @@ class Dialect;
 
 /// Find the dialect selected by the user to generate for. Returns std::nullopt
 /// if no dialect was found, or if more than one potential dialect was found.
-std::optional<Dialect> findDialectToGenerate(ArrayRef<Dialect> dialects);
+std::optional<Dialect>
+findDialectToGenerate(ArrayRef<Dialect> dialects,
+                      const std::string &selectedDialect);
 } // namespace tblgen
 } // namespace mlir
 
diff --git a/mlir/include/mlir/TableGen/FormatGen.h b/mlir/include/mlir/TableGen/FormatGen.h
index b061d4d8ea7f0..2a05414e90f29 100644
--- a/mlir/include/mlir/TableGen/FormatGen.h
+++ b/mlir/include/mlir/TableGen/FormatGen.h
@@ -596,9 +596,6 @@ bool canFormatStringAsKeyword(StringRef value,
 bool isValidLiteral(StringRef value,
                     function_ref<void(Twine)> emitError = nullptr);
 
-/// Whether a failure in parsing the assembly format should be a fatal error.
-extern llvm::cl::opt<bool> formatErrorIsFatal;
-
 } // namespace tblgen
 } // namespace mlir
 
diff --git a/mlir/include/mlir/TableGen/GenInfo.h b/mlir/include/mlir/TableGen/GenInfo.h
index ef2e12f07df16..57fc7f911eaef 100644
--- a/mlir/include/mlir/TableGen/GenInfo.h
+++ b/mlir/include/mlir/TableGen/GenInfo.h
@@ -11,6 +11,7 @@
 
 #include "mlir/Support/LLVM.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/Support/CommandLine.h"
 #include <functional>
 #include <utility>
 
@@ -69,6 +70,87 @@ struct GenRegistration {
                   const GenFunction &function);
 };
 
+namespace tblgen {
+bool emitBCRW(const llvm::RecordKeeper &records, raw_ostream &os,
+              const std::string &selectedBcDialect);
+
+bool emitOpDecls(const llvm::RecordKeeper &recordKeeper, raw_ostream &os,
+                 const std::string &opIncFilter, const std::string &opExcFilter,
+                 unsigned opShardCount, bool formatErrorIsFatal);
+bool emitOpDefs(const llvm::RecordKeeper &recordKeeper, raw_ostream &os,
+                const std::string &opIncFilter, const std::string &opExcFilter,
+                unsigned opShardCount, bool formatErrorIsFatal);
+
+bool emitDialectDecls(const llvm::RecordKeeper &recordKeeper, raw_ostream &os,
+                      const std::string &selectedDialect);
+bool emitDialectDefs(const llvm::RecordKeeper &recordKeeper, raw_ostream &os,
+                     const std::string &selectedDialect);
+bool emitDirectiveDecls(const llvm::RecordKeeper &recordKeeper,
+                        llvm::StringRef dialect, raw_ostream &os);
+
+bool emitPythonEnums(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
+bool emitAllPythonOps(const llvm::RecordKeeper &records, raw_ostream &os,
+                      const std::string &clDialectName,
+                      const std::string &clDialectExtensionName);
+
+bool emitEnumDecls(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
+bool emitEnumDefs(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
+
+bool emitLLVMBuilders(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
+bool emitLLVMOpMLIRBuilders(const llvm::RecordKeeper &recordKeeper,
+                            raw_ostream &os);
+bool emitLLVMIntrMLIRBuilders(const llvm::RecordKeeper &recordKeeper,
+                              raw_ostream &os);
+template <bool ConvertTo>
+bool emitLLVMEnumConversionDefs(const llvm::RecordKeeper &recordKeeper,
+                                raw_ostream &os);
+bool emitLLVMConvertibleIntrinsics(const llvm::RecordKeeper &recordKeeper,
+                                   raw_ostream &os);
+bool emitLLVMIntrinsics(const llvm::RecordKeeper &records,
+                        llvm::raw_ostream &os, const std::string &nameFilter,
+                        const std::string &accessGroupRegexp,
+                        const std::string &aliasAnalysisRegexp,
+                        const std::string &opBaseClass);
+
+void emitAttrOrTypeDefDoc(const llvm::RecordKeeper &recordKeeper,
+                          raw_ostream &os, StringRef recordTypeName);
+void emitOpDoc(const llvm::RecordKeeper &recordKeeper, raw_ostream &os,
+               const std::string &emitOpDoc, bool allowHugoSpecificFeatures,
+               const std::string &opIncFilter, const std::string &opExcFilter);
+bool emitDialectDoc(const llvm::RecordKeeper &recordKeeper, raw_ostream &os,
+                    const std::string &selectedDialect,
+                    const std::string &opIncFilter,
+                    const std::string &opExcFilter,
+                    const std::string &stripPrefix,
+                    bool allowHugoSpecificFeatures);
+void emitDocs(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
+
+bool emitCAPIHeader(const llvm::RecordKeeper &records, raw_ostream &os,
+                    std::string groupPrefix);
+bool emitCAPIImpl(const llvm::RecordKeeper &records, raw_ostream &os,
+                  std::string groupPrefix);
+void emitPasses(const llvm::RecordKeeper &recordKeeper, raw_ostream &os,
+                const std::string &opIncFilter, const std::string &groupName);
+void emitRewriters(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
+
+bool emitSPIRVInterfaceDefs(const llvm::RecordKeeper &recordKeeper,
+                            raw_ostream &os);
+bool emitSPIRVInterfaceDecls(const llvm::RecordKeeper &recordKeeper,
+                             raw_ostream &os);
+bool emitSPIRVEnumDecls(const llvm::RecordKeeper &recordKeeper,
+                        raw_ostream &os);
+bool emitSPIRVEnumDefs(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
+bool emitSPIRVCapabilityImplication(const llvm::RecordKeeper &recordKeeper,
+                                    raw_ostream &os);
+bool emitSPIRVSerializationFns(const llvm::RecordKeeper &recordKeeper,
+                               raw_ostream &os);
+bool emitSPIRVAttrUtils(const llvm::RecordKeeper &recordKeeper,
+                        raw_ostream &os);
+bool emitSPIRVAvailabilityImpl(const llvm::RecordKeeper &recordKeeper,
+                               raw_ostream &os);
+
+} // namespace tblgen
+
 } // namespace mlir
 
 #endif // MLIR_TABLEGEN_GENINFO_H_
diff --git a/mlir/include/mlir/TableGen/OpFormatGen.h b/mlir/include/mlir/TableGen/OpFormatGen.h
index 88dbc99d9f78e..fc5306031ebe6 100644
--- a/mlir/include/mlir/TableGen/OpFormatGen.h
+++ b/mlir/include/mlir/TableGen/OpFormatGen.h
@@ -20,7 +20,8 @@ class OpClass;
 class Operator;
 
 // Generate the assembly format for the given operator.
-void generateOpFormat(const Operator &constOp, OpClass &opClass);
+void generateOpFormat(const Operator &constOp, OpClass &opClass,
+                      bool formatErrorIsFatal);
 
 } // namespace tblgen
 } // namespace mlir
diff --git a/mlir/include/mlir/TableGen/OpGenHelpers.h b/mlir/include/mlir/TableGen/OpGenHelpers.h
index 1b43d5d3ce3a7..537a42770cfac 100644
--- a/mlir/include/mlir/TableGen/OpGenHelpers.h
+++ b/mlir/include/mlir/TableGen/OpGenHelpers.h
@@ -23,7 +23,9 @@ namespace tblgen {
 /// Returns all the op definitions filtered by the user. The filtering is via
 /// command-line option "op-include-regex" and "op-exclude-regex".
 std::vector<llvm::Record *>
-getRequestedOpDefinitions(const llvm::RecordKeeper &recordKeeper);
+getRequestedOpDefinitions(const llvm::RecordKeeper &recordKeeper,
+                          const std::string &opIncFilter,
+                          const std::string &opExcFilter);
 
 /// Checks whether `str` is a Python keyword or would shadow builtin function.
 /// Regenerate using python -c"print(set(sorted(__import__('keyword').kwlist)))"
@@ -31,7 +33,8 @@ bool isPythonReserved(llvm::StringRef str);
 
 /// Shard the op defintions into the number of shards set by "op-shard-count".
 void shardOpDefinitions(ArrayRef<llvm::Record *> defs,
-                        SmallVectorImpl<ArrayRef<llvm::Record *>> &shardedDefs);
+                        SmallVectorImpl<ArrayRef<llvm::Record *>> &shardedDefs,
+                        unsigned shardOpDefinitions);
 
 } // namespace tblgen
 } // namespace mlir
diff --git a/mlir/include/mlir/TableGen/OpInterfacesGen.h b/mlir/include/mlir/TableGen/OpInterfacesGen.h
new file mode 100644
index 0000000000000..f5d087941f5d6
--- /dev/null
+++ b/mlir/include/mlir/TableGen/OpInterfacesGen.h
@@ -0,0 +1,142 @@
+//===- OpInterfacesGen.h - MLIR operation generator helpers -----*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This file defines helpers used in the op interface generators.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_TOOLS_MLIRTBLGEN_OPINTERFACESGEN_H_
+#define MLIR_TOOLS_MLIRTBLGEN_OPINTERFACESGEN_H_
+
+#include "mlir/Support/LLVM.h"
+#include "mlir/TableGen/Format.h"
+#include "mlir/TableGen/GenInfo.h"
+#include "mlir/TableGen/Interfaces.h"
+#include "llvm/TableGen/Record.h"
+#include <vector>
+
+namespace mlir {
+namespace tblgen {
+
+template <typename GeneratorT>
+struct InterfaceGenRegistration {
+  InterfaceGenRegistration(StringRef genArg, StringRef genDesc)
+      : genDeclArg(("gen-" + genArg + "-interface-decls").str()),
+        genDefArg(("gen-" + genArg + "-interface-defs").str()),
+        genDocArg(("gen-" + genArg + "-interface-docs").str()),
+        genDeclDesc(("Generate " + genDesc + " interface declarations").str()),
+        genDefDesc(("Generate " + genDesc + " interface definitions").str()),
+        genDocDesc(("Generate " + genDesc + " interface documentation").str()),
+        genDecls(genDeclArg, genDeclDesc,
+                 [](const llvm::RecordKeeper &records, raw_ostream &os) {
+                   return GeneratorT(records, os).emitInterfaceDecls();
+                 }),
+        genDefs(genDefArg, genDefDesc,
+                [](const llvm::RecordKeeper &records, raw_ostream &os) {
+                  return GeneratorT(records, os).emitInterfaceDefs();
+                }),
+        genDocs(genDocArg, genDocDesc,
+                [](const llvm::RecordKeeper &records, raw_ostream &os) {
+                  return GeneratorT(records, os).emitInterfaceDocs();
+                }) {}
+
+  std::string genDeclArg, genDefArg, genDocArg;
+  std::string genDeclDesc, genDefDesc, genDocDesc;
+  mlir::GenRegistration genDecls, genDefs, genDocs;
+};
+
+/// This struct is the base generator used when processing tablegen interfaces.
+class InterfaceGenerator {
+public:
+  bool emitInterfaceDefs();
+  bool emitInterfaceDecls();
+  bool emitInterfaceDocs();
+
+protected:
+  InterfaceGenerator(std::vector<llvm::Record *> &&defs, raw_ostream &os)
+      : defs(std::move(defs)), os(os) {}
+
+  void emitConceptDecl(const Interface &interface);
+  void emitModelDecl(const Interface &interface);
+  void emitModelMethodsDef(const Interface &interface);
+  void emitTraitDecl(const Interface &interface, StringRef interfaceName,
+                     StringRef interfaceTraitsName);
+  void emitInterfaceDecl(const Interface &interface);
+
+  /// The set of interface records to emit.
+  std::vector<llvm::Record *> defs;
+  // The stream to emit to.
+  raw_ostream &os;
+  /// The C++ value type of the interface, e.g. Operation*.
+  StringRef valueType;
+  /// The C++ base interface type.
+  StringRef interfaceBaseType;
+  /// The name of the typename for the value template.
+  StringRef valueTemplate;
+  /// The name of the substituion variable for the value.
+  StringRef substVar;
+  /// The format context to use for methods.
+  tblgen::FmtContext nonStaticMethodFmt;
+  tblgen::FmtContext traitMethodFmt;
+  tblgen::FmtContext extraDeclsFmt;
+};
+
+std::vector<llvm::Record *>
+getAllInterfaceDefinitions(const llvm::RecordKeeper &recordKeeper,
+                           StringRef name);
+
+/// A specialized generator for attribute interfaces.
+struct AttrInterfaceGenerator : public InterfaceGenerator {
+  AttrInterfaceGenerator(const llvm::RecordKeeper &records, raw_ostream &os)
+      : InterfaceGenerator(getAllInterfaceDefinitions(records, "Attr"), os) {
+    valueType = "::mlir::Attribute";
+    interfaceBaseType = "AttributeInterface";
+    valueTemplate = "ConcreteAttr";
+    substVar = "_attr";
+    StringRef castCode = "(::llvm::cast<ConcreteAttr>(tablegen_opaque_val))";
+    nonStaticMethodFmt.addSubst(substVar, castCode).withSelf(castCode);
+    traitMethodFmt.addSubst(substVar,
+                            "(*static_cast<const ConcreteAttr *>(this))");
+    extraDeclsFmt.addSubst(substVar, "(*this)");
+  }
+};
+/// A specialized generator for operation interfaces.
+struct OpInterfaceGenerator : public InterfaceGenerator {
+  OpInterfaceGenerator(const llvm::RecordKeeper &records, raw_ostream &os)
+      : InterfaceGenerator(getAllInterfaceDefinitions(records, "Op"), os) {
+    valueType = "::mlir::Operation *";
+    interfaceBaseType = "OpInterface";
+    valueTemplate = "ConcreteOp";
+    substVar = "_op";
+    StringRef castCode = "(llvm::cast<ConcreteOp>(tablegen_opaque_val))";
+    nonStaticMethodFmt.addSubst("_this", "impl")
+        .addSubst(substVar, castCode)
+        .withSelf(castCode);
+    traitMethodFmt.addSubst(substVar, "(*static_cast<ConcreteOp *>(this))");
+    extraDeclsFmt.addSubst(substVar, "(*this)");
+  }
+};
+/// A specialized generator for type interfaces.
+struct TypeInterfaceGenerator : public InterfaceGenerator {
+  TypeInterfaceGenerator(const llvm::RecordKeeper &records, raw_ostream &os)
+      : InterfaceGenerator(getAllInterfaceDefinitions(records, "Type"), os) {
+    valueType = "::mlir::Type";
+    interfaceBaseType = "TypeInterface";
+    valueTemplate = "ConcreteType";
+    substVar = "_type";
+    StringRef castCode = "(::llvm::cast<ConcreteType>(tablegen_opaque_val))";
+    nonStaticMethodFmt.addSubst(substVar, castCode).withSelf(castCode);
+    traitMethodFmt.addSubst(substVar,
+                            "(*static_cast<const ConcreteType *>(this))");
+    extraDeclsFmt.addSubst(substVar, "(*this)");
+  }
+};
+} // namespace tblgen
+} // namespace mlir
+
+#endif //  MLIR_TOOLS_MLIRTBLGEN_OPINTERFACESGEN_H_
diff --git a/mlir/lib/TableGen/AttrOrTypeDefGen.cpp b/mlir/lib/TableGen/AttrOrTypeDefGen.cpp
index 4f792b2cfa076..82208093a87a2 100644
--- a/mlir/lib/TableGen/AttrOrTypeDefGen.cpp
+++ b/mlir/lib/TableGen/AttrOrTypeDefGen.cpp
@@ -6,12 +6,11 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "mlir/TableGen/AttrOrTypeFormatGen.h"
 #include "mlir/TableGen/AttrOrTypeDef.h"
+#include "mlir/TableGen/AttrOrTypeFormatGen.h"
 #include "mlir/TableGen/Class.h"
 #include "mlir/TableGen/CodeGenHelpers.h"
 #include "mlir/TableGen/Format.h"
-#include "mlir/TableGen/GenInfo.h"
 #include "mlir/TableGen/Interfaces.h"
 #include "llvm/ADT/StringSet.h"
 #include "llvm/Support/CommandLine.h"
@@ -64,7 +63,7 @@ namespace {
 class DefGen {
 public:
   /// Create the attribute or type class.
-  DefGen(const AttrOrTypeDef &def);
+  DefGen(const AttrOrTypeDef &def, bool formatErrorIsFatal);
 
   void emitDecl(raw_ostream &os) const {
     if (storageCls && def.genStorageClass()) {
@@ -159,13 +158,16 @@ class DefGen {
   StringRef valueType;
   /// The prefix/suffix of the TableGen def name, either "Attr" or "Type".
   StringRef defType;
+  /// Whether a failure in parsing the assembly format should be a fatal error.
+  bool formatErrorIsFatal;
 };
 } // namespace
 
-DefGen::DefGen(const AttrOrTypeDef &def)
+DefGen::DefGen(const AttrOrTypeDef &def, bool formatErrorIsFatal)
     : def(def), params(def.getParameters()), defCls(def.getCppClassName()),
       valueType(isa<AttrDef>(def) ? "Attribute" : "Type"),
-      defType(isa<AttrDef>(def) ? "Attr" : "Type") {
+      defType(isa<AttrDef>(def) ? "Attr" : "Type"),
+      formatErrorIsFatal(formatErrorIsFatal) {
   // Check that all parameters have names.
   for (const AttrOrTypeParameter &param : def.getParameters())
     if (param.isAnonymous())
@@ -328,7 +330,8 @@ void DefGen::emitParserPrinter() {
                        MethodParameter("::mlir::AsmPrinter &", "odsPrinter"));
   // Emit the bodies if we are using the declarative format.
   if (hasAssemblyFormat)
-    return generateAttrOrTypeFormat(def, parser->body(), printer->body());
+    return generateAttrOrTypeFormat(def, parser->body(), printer->body(),
+                                    formatErrorIsFatal);
 }
 
 void DefGen::emitAccessors() {
@@ -589,61 +592,6 @@ void DefGen::emitStorageClass() {
     storageCls->declare<Field>(param.getCppType(), param.getName());
 }
 
-//===----------------------------------------------------------------------===//
-// DefGenerator
-//===----------------------------------------------------------------------===//
-
-namespace {
-/// This struct is the base generator used when processing tablegen interfaces.
-class DefGenerator {
-public:
-  bool emitDecls(StringRef selectedDialect);
-  bool emitDefs(StringRef selectedDialect);
-
-protected:
-  DefGenerator(std::vector<llvm::Record *> &&defs, raw_ostream &os,
-               StringRef defType, StringRef valueType, bool isAttrGenerator)
-      : defRecords(std::move(defs)), os(os), defType(defType),
-        valueType(valueType), isAttrGenerator(isAttrGenerator) {
-    // Sort by occurrence in file.
-    llvm::sort(defRecords, [](llvm::Record *lhs, llvm::Record *rhs) {
-      return lhs->getID() < rhs->getID();
-    });
-  }
-
-  /// Emit the list of def type names.
-  void emitTypeDefList(ArrayRef<AttrOrTypeDef> defs);
-  /// Emit the code to dispatch between different defs during parsing/printing.
-  void emitParsePrintDispatch(ArrayRef<AttrOrTypeDef> defs);
-
-  /// The set of def records to emit.
-  std::vector<llvm::Record *> defRecords;
-  /// The attribute or type class to emit.
-  /// The stream to emit to.
-  raw_ostream &os;
-  /// The prefix of the tablegen def name, e.g. Attr or Type.
-  StringRef defType;
-  /// The C++ base value type of the def, e.g. Attribute or Type.
-  StringRef valueType;
-  /// Flag indicating if this generator is for Attributes. False if the
-  /// generator is for types.
-  bool isAttrGenerator;
-};
-
-/// A specialized generator for AttrDefs.
-struct AttrDefGenerator : public DefGenerator {
-  AttrDefGenerator(const llvm::RecordKeeper &records, raw_ostream &os)
-      : DefGenerator(records.getAllDerivedDefinitionsIfDefined("AttrDef"), os,
-                     "Attr", "Attribute", /*isAttrGenerator=*/true) {}
-};
-/// A specialized generator for TypeDefs.
-struct TypeDefGenerator : public DefGenerator {
-  TypeDefGenerator(const llvm::RecordKeeper &records, raw_ostream &os)
-      : DefGenerator(records.getAllDerivedDefinitionsIfDefined("TypeDef"), os,
-                     "Type", "Type", /*isAttrGenerator=*/false) {}
-};
-} // namespace
-
 //===----------------------------------------------------------------------===//
 // GEN: Declarations
 //===----------------------------------------------------------------------===//
@@ -677,7 +625,7 @@ bool DefGenerator::emitDecls(StringRef selectedDialect) {
 
     // Emit the declarations.
     for (const AttrOrTypeDef &def : defs)
-      DefGen(def).emitDecl(os);
+      DefGen(def, formatErrorIsFatal).emitDecl(os);
   }
   // Emit the TypeID explicit specializations to have a single definition for
   // each of these.
@@ -891,7 +839,7 @@ bool DefGenerator::emitDefs(StringRef selectedDialect) {
   for (const AttrOrTypeDef &def : defs) {
     {
       NamespaceEmitter ns(os, def.getDialect());
-      DefGen gen(def);
+      DefGen gen(def, formatErrorIsFatal);
       gen.emitDef(os);
     }
     // Emit the TypeID explicit specializations to have a single symbol def.
@@ -933,51 +881,3 @@ bool DefGenerator::emitDefs(StringRef selectedDialect) {
 
   return false;
 }
-
-//===----------------------------------------------------------------------===//
-// GEN: Registration hooks
-//===----------------------------------------------------------------------===//
-
-//===----------------------------------------------------------------------===//
-// AttrDef
-
-static llvm::cl::OptionCategory attrdefGenCat("Options for -gen-attrdef-*");
-static llvm::cl::opt<std::string>
-    attrDialect("attrdefs-dialect",
-                llvm::cl::desc("Generate attributes for this dialect"),
-                llvm::cl::cat(attrdefGenCat), llvm::cl::CommaSeparated);
-
-static mlir::GenRegistration
-    genAttrDefs("gen-attrdef-defs", "Generate AttrDef definitions",
-                [](const llvm::RecordKeeper &records, raw_ostream &os) {
-                  AttrDefGenerator generator(records, os);
-                  return generator.emitDefs(attrDialect);
-                });
-static mlir::GenRegistration
-    genAttrDecls("gen-attrdef-decls", "Generate AttrDef declarations",
-                 [](const llvm::RecordKeeper &records, raw_ostream &os) {
-                   AttrDefGenerator generator(records, os);
-                   return generator.emitDecls(attrDialect);
-                 });
-
-//===----------------------------------------------------------------------===//
-// TypeDef
-
-static llvm::cl::OptionCategory typedefGenCat("Options for -gen-typedef-*");
-static llvm::cl::opt<std::string>
-    typeDialect("typedefs-dialect",
-                llvm::cl::desc("Generate types for this dialect"),
-                llvm::cl::cat(typedefGenCat), llvm::cl::CommaSeparated);
-
-static mlir::GenRegistration
-    genTypeDefs("gen-typedef-defs", "Generate TypeDef definitions",
-                [](const llvm::RecordKeeper &records, raw_ostream &os) {
-                  TypeDefGenerator generator(records, os);
-                  return generator.emitDefs(typeDialect);
-                });
-static mlir::GenRegistration
-    genTypeDecls("gen-typedef-decls", "Generate TypeDef declarations",
-                 [](const llvm::RecordKeeper &records, raw_ostream &os) {
-                   TypeDefGenerator generator(records, os);
-                   return generator.emitDecls(typeDialect);
-                 });
diff --git a/mlir/lib/TableGen/AttrOrTypeFormatGen.cpp b/mlir/lib/TableGen/AttrOrTypeFormatGen.cpp
index bba237ddb47f1..5fc3194d615ae 100644
--- a/mlir/lib/TableGen/AttrOrTypeFormatGen.cpp
+++ b/mlir/lib/TableGen/AttrOrTypeFormatGen.cpp
@@ -7,12 +7,11 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/TableGen/AttrOrTypeFormatGen.h"
-#include "mlir/TableGen/FormatGen.h"
 #include "mlir/Support/LLVM.h"
 #include "mlir/Support/LogicalResult.h"
 #include "mlir/TableGen/AttrOrTypeDef.h"
 #include "mlir/TableGen/Format.h"
-#include "mlir/TableGen/GenInfo.h"
+#include "mlir/TableGen/FormatGen.h"
 #include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringSwitch.h"
@@ -21,7 +20,6 @@
 #include "llvm/Support/SaveAndRestore.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/TableGen/Error.h"
-#include "llvm/TableGen/TableGenBackend.h"
 
 using namespace mlir;
 using namespace mlir::tblgen;
@@ -1191,7 +1189,8 @@ FailureOr<FormatElement *> DefFormatParser::parseStructDirective(SMLoc loc,
 
 void mlir::tblgen::generateAttrOrTypeFormat(const AttrOrTypeDef &def,
                                             MethodBody &parser,
-                                            MethodBody &printer) {
+                                            MethodBody &printer,
+                                            bool formatErrorIsFatal) {
   llvm::SourceMgr mgr;
   mgr.AddNewSourceBuffer(
       llvm::MemoryBuffer::getMemBuffer(*def.getAssemblyFormat()), SMLoc());
diff --git a/mlir/lib/TableGen/BytecodeDialectGen.cpp b/mlir/lib/TableGen/BytecodeDialectGen.cpp
index 66a3750d7c826..115e817ae0ce2 100644
--- a/mlir/lib/TableGen/BytecodeDialectGen.cpp
+++ b/mlir/lib/TableGen/BytecodeDialectGen.cpp
@@ -18,12 +18,6 @@
 
 using namespace llvm;
 
-static llvm::cl::OptionCategory dialectGenCat("Options for -gen-bytecode");
-static llvm::cl::opt<std::string>
-    selectedBcDialect("bytecode-dialect",
-                      llvm::cl::desc("The dialect to gen for"),
-                      llvm::cl::cat(dialectGenCat), llvm::cl::CommaSeparated);
-
 namespace {
 
 /// Helper class to generate C++ bytecode parser helpers.
@@ -134,7 +128,9 @@ void Generator::emitParse(StringRef kind, Record &x) {
       R"(static {0} read{1}(MLIRContext* context, DialectBytecodeReader &reader) )";
   mlir::raw_indented_ostream os(output);
   std::string returnType = getCType(&x);
-  os << formatv(head, kind == "attribute" ? "::mlir::Attribute" : "::mlir::Type", x.getName());
+  os << formatv(head,
+                kind == "attribute" ? "::mlir::Attribute" : "::mlir::Type",
+                x.getName());
   DagInit *members = x.getValueAsDag("members");
   SmallVector<std::string> argNames =
       llvm::to_vector(map_range(members->getArgNames(), [](StringInit *init) {
@@ -427,7 +423,8 @@ struct AttrOrType {
 };
 } // namespace
 
-static bool emitBCRW(const RecordKeeper &records, raw_ostream &os) {
+bool mlir::tblgen::emitBCRW(const RecordKeeper &records, raw_ostream &os,
+                            const std::string &selectedBcDialect) {
   MapVector<StringRef, AttrOrType> dialectAttrOrType;
   for (auto &it : records.getAllDerivedDefinitions("DialectAttributes")) {
     if (!selectedBcDialect.empty() &&
@@ -478,9 +475,3 @@ static bool emitBCRW(const RecordKeeper &records, raw_ostream &os) {
 
   return false;
 }
-
-static mlir::GenRegistration
-    genBCRW("gen-bytecode", "Generate dialect bytecode readers/writers",
-            [](const RecordKeeper &records, raw_ostream &os) {
-              return emitBCRW(records, os);
-            });
diff --git a/mlir/lib/TableGen/Constraint.cpp b/mlir/lib/TableGen/Constraint.cpp
index 4ccbd0a685e09..bc18420223805 100644
--- a/mlir/lib/TableGen/Constraint.cpp
+++ b/mlir/lib/TableGen/Constraint.cpp
@@ -30,7 +30,7 @@ Constraint::Constraint(const llvm::Record *record)
     kind = CK_Region;
   } else if (def->isSubClassOf("SuccessorConstraint")) {
     kind = CK_Successor;
-  } else if(!def->isSubClassOf("Constraint")) {
+  } else if (!def->isSubClassOf("Constraint")) {
     llvm::errs() << "Expected a constraint but got: \n" << *def << "\n";
     llvm::report_fatal_error("Abort");
   }
diff --git a/mlir/lib/TableGen/DialectGen.cpp b/mlir/lib/TableGen/DialectGen.cpp
index 8b4cdc52adc8a..cae701bbfba55 100644
--- a/mlir/lib/TableGen/DialectGen.cpp
+++ b/mlir/lib/TableGen/DialectGen.cpp
@@ -10,9 +10,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "mlir/TableGen/DialectGenUtilities.h"
 #include "mlir/TableGen/Class.h"
 #include "mlir/TableGen/CodeGenHelpers.h"
+#include "mlir/TableGen/DialectGenUtilities.h"
 #include "mlir/TableGen/Format.h"
 #include "mlir/TableGen/GenInfo.h"
 #include "mlir/TableGen/Interfaces.h"
@@ -31,11 +31,6 @@
 using namespace mlir;
 using namespace mlir::tblgen;
 
-static llvm::cl::OptionCategory dialectGenCat("Options for -gen-dialect-*");
-llvm::cl::opt<std::string>
-    selectedDialect("dialect", llvm::cl::desc("The dialect to gen for"),
-                    llvm::cl::cat(dialectGenCat), llvm::cl::CommaSeparated);
-
 /// Utility iterator used for filtering records for a specific dialect.
 namespace {
 using DialectFilterIterator =
@@ -71,25 +66,27 @@ filterForDialect(ArrayRef<llvm::Record *> records, Dialect &dialect) {
 }
 
 std::optional<Dialect>
-tblgen::findDialectToGenerate(ArrayRef<Dialect> dialects) {
+tblgen::findDialectToGenerate(ArrayRef<Dialect> dialects,
+                              const std::string &selectedDialect) {
   if (dialects.empty()) {
     llvm::errs() << "no dialect was found\n";
     return std::nullopt;
   }
 
   // Select the dialect to gen for.
-  if (dialects.size() == 1 && selectedDialect.getNumOccurrences() == 0)
+  if (dialects.size() == 1 && selectedDialect.empty())
     return dialects.front();
 
-  if (selectedDialect.getNumOccurrences() == 0) {
+  if (selectedDialect.empty()) {
     llvm::errs() << "when more than 1 dialect is present, one must be selected "
                     "via '-dialect'\n";
     return std::nullopt;
   }
 
-  const auto *dialectIt = llvm::find_if(dialects, [](const Dialect &dialect) {
-    return dialect.getName() == selectedDialect;
-  });
+  const auto *dialectIt =
+      llvm::find_if(dialects, [&selectedDialect](const Dialect &dialect) {
+        return dialect.getName() == selectedDialect;
+      });
   if (dialectIt == dialects.end()) {
     llvm::errs() << "selected dialect with '-dialect' does not exist\n";
     return std::nullopt;
@@ -295,8 +292,9 @@ static void emitDialectDecl(Dialect &dialect, raw_ostream &os) {
        << "::" << dialect.getCppClassName() << ")\n";
 }
 
-static bool emitDialectDecls(const llvm::RecordKeeper &recordKeeper,
-                             raw_ostream &os) {
+bool mlir::tblgen::emitDialectDecls(const llvm::RecordKeeper &recordKeeper,
+                                    raw_ostream &os,
+                                    const std::string &selectedDialect) {
   emitSourceFileHeader("Dialect Declarations", os, recordKeeper);
 
   auto dialectDefs = recordKeeper.getAllDerivedDefinitions("Dialect");
@@ -304,7 +302,8 @@ static bool emitDialectDecls(const llvm::RecordKeeper &recordKeeper,
     return false;
 
   SmallVector<Dialect> dialects(dialectDefs.begin(), dialectDefs.end());
-  std::optional<Dialect> dialect = findDialectToGenerate(dialects);
+  std::optional<Dialect> dialect =
+      findDialectToGenerate(dialects, selectedDialect);
   if (!dialect)
     return true;
   emitDialectDecl(*dialect, os);
@@ -389,8 +388,9 @@ static void emitDialectDef(Dialect &dialect,
     os << llvm::formatv(dialectDestructorStr, cppClassName);
 }
 
-static bool emitDialectDefs(const llvm::RecordKeeper &recordKeeper,
-                            raw_ostream &os) {
+bool mlir::tblgen::emitDialectDefs(const llvm::RecordKeeper &recordKeeper,
+                                   raw_ostream &os,
+                                   const std::string &selectedDialect) {
   emitSourceFileHeader("Dialect Definitions", os, recordKeeper);
 
   auto dialectDefs = recordKeeper.getAllDerivedDefinitions("Dialect");
@@ -398,25 +398,10 @@ static bool emitDialectDefs(const llvm::RecordKeeper &recordKeeper,
     return false;
 
   SmallVector<Dialect> dialects(dialectDefs.begin(), dialectDefs.end());
-  std::optional<Dialect> dialect = findDialectToGenerate(dialects);
+  std::optional<Dialect> dialect =
+      findDialectToGenerate(dialects, selectedDialect);
   if (!dialect)
     return true;
   emitDialectDef(*dialect, recordKeeper, os);
   return false;
 }
-
-//===----------------------------------------------------------------------===//
-// GEN: Dialect registration hooks
-//===----------------------------------------------------------------------===//
-
-static mlir::GenRegistration
-    genDialectDecls("gen-dialect-decls", "Generate dialect declarations",
-                    [](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/mlir/lib/TableGen/DirectiveCommonGen.cpp b/mlir/lib/TableGen/DirectiveCommonGen.cpp
index 337b6a5e5d5bd..394427fa08b02 100644
--- a/mlir/lib/TableGen/DirectiveCommonGen.cpp
+++ b/mlir/lib/TableGen/DirectiveCommonGen.cpp
@@ -36,13 +36,14 @@ using llvm::RecordKeeper;
 //
 // Some OpenMP/OpenACC clauses accept only a fixed set of values as inputs.
 // These can be represented as a Enum Attributes (EnumAttrDef) in MLIR
-// ODS. The emitDecls function below currently generates these enumerations. The
-// name of the enumeration is specified in the enumClauseValue field of
-// Clause record in OMP.td. This name can be used to specify the type of the
-// OpenMP operation's operand. The allowedClauseValues field provides the list
-// of ClauseValues which are part of the enumeration.
-static bool emitDecls(const RecordKeeper &recordKeeper, llvm::StringRef dialect,
-                      raw_ostream &os) {
+// ODS. The emitDirectiveDecls function below currently generates these
+// enumerations. The name of the enumeration is specified in the enumClauseValue
+// field of Clause record in OMP.td. This name can be used to specify the type
+// of the OpenMP operation's operand. The allowedClauseValues field provides the
+// list of ClauseValues which are part of the enumeration.
+bool mlir::tblgen::emitDirectiveDecls(const RecordKeeper &recordKeeper,
+                                      llvm::StringRef dialect,
+                                      raw_ostream &os) {
   // A dialect must be selected for the generated attributes.
   if (dialect.empty()) {
     llvm::PrintFatalError("a dialect must be selected for the directives via "
@@ -102,18 +103,3 @@ static bool emitDecls(const RecordKeeper &recordKeeper, llvm::StringRef dialect,
   }
   return false;
 }
-
-static llvm::cl::OptionCategory
-    directiveGenCat("Options for gen-directive-decl");
-static llvm::cl::opt<std::string>
-    dialect("directives-dialect",
-            llvm::cl::desc("Generate directives for this dialect"),
-            llvm::cl::cat(directiveGenCat), llvm::cl::CommaSeparated);
-
-// Registers the generator to mlir-tblgen.
-static mlir::GenRegistration genDirectiveDecls(
-    "gen-directive-decl",
-    "Generate declarations for directives (OpenMP/OpenACC etc.)",
-    [](const RecordKeeper &records, raw_ostream &os) {
-      return emitDecls(records, dialect, os);
-    });
diff --git a/mlir/lib/TableGen/EnumPythonBindingGen.cpp b/mlir/lib/TableGen/EnumPythonBindingGen.cpp
index 63f4ad6ccbdd1..eca60b45423ec 100644
--- a/mlir/lib/TableGen/EnumPythonBindingGen.cpp
+++ b/mlir/lib/TableGen/EnumPythonBindingGen.cpp
@@ -133,8 +133,8 @@ static bool emitDialectEnumAttributeBuilder(StringRef attrDefName,
 
 /// Emits Python bindings for all enums in the record keeper. Returns
 /// `false` on success, `true` on failure.
-static bool emitPythonEnums(const llvm::RecordKeeper &recordKeeper,
-                            raw_ostream &os) {
+bool mlir::tblgen::emitPythonEnums(const llvm::RecordKeeper &recordKeeper,
+                                   raw_ostream &os) {
   os << fileHeader;
   for (auto &it :
        recordKeeper.getAllDerivedDefinitionsIfDefined("EnumAttrInfo")) {
@@ -169,9 +169,3 @@ static bool emitPythonEnums(const llvm::RecordKeeper &recordKeeper,
 
   return false;
 }
-
-// Registers the enum utility generator to mlir-tblgen.
-static mlir::GenRegistration
-    genPythonEnumBindings("gen-python-enum-bindings",
-                          "Generate Python bindings for enum attributes",
-                          &emitPythonEnums);
diff --git a/mlir/lib/TableGen/EnumsGen.cpp b/mlir/lib/TableGen/EnumsGen.cpp
index 85bd7e8c59e01..4eef7c79e704a 100644
--- a/mlir/lib/TableGen/EnumsGen.cpp
+++ b/mlir/lib/TableGen/EnumsGen.cpp
@@ -10,9 +10,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "mlir/TableGen/FormatGen.h"
 #include "mlir/TableGen/Attribute.h"
 #include "mlir/TableGen/Format.h"
+#include "mlir/TableGen/FormatGen.h"
 #include "mlir/TableGen/GenInfo.h"
 #include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/SmallVector.h"
@@ -642,7 +642,8 @@ class {1} : public ::mlir::{2} {
   emitDenseMapInfo(qualName, underlyingType, cppNamespace, os);
 }
 
-static bool emitEnumDecls(const RecordKeeper &recordKeeper, raw_ostream &os) {
+bool mlir::tblgen::emitEnumDecls(const RecordKeeper &recordKeeper,
+                                 raw_ostream &os) {
   llvm::emitSourceFileHeader("Enum Utility Declarations", os, recordKeeper);
 
   auto defs = recordKeeper.getAllDerivedDefinitionsIfDefined("EnumAttrInfo");
@@ -680,7 +681,8 @@ static void emitEnumDef(const Record &enumDef, raw_ostream &os) {
   os << "\n";
 }
 
-static bool emitEnumDefs(const RecordKeeper &recordKeeper, raw_ostream &os) {
+bool mlir::tblgen::emitEnumDefs(const RecordKeeper &recordKeeper,
+                                raw_ostream &os) {
   llvm::emitSourceFileHeader("Enum Utility Definitions", os, recordKeeper);
 
   auto defs = recordKeeper.getAllDerivedDefinitionsIfDefined("EnumAttrInfo");
@@ -689,17 +691,3 @@ static bool emitEnumDefs(const RecordKeeper &recordKeeper, raw_ostream &os) {
 
   return false;
 }
-
-// Registers the enum utility generator to mlir-tblgen.
-static mlir::GenRegistration
-    genEnumDecls("gen-enum-decls", "Generate enum utility declarations",
-                 [](const RecordKeeper &records, raw_ostream &os) {
-                   return emitEnumDecls(records, os);
-                 });
-
-// Registers the enum utility generator to mlir-tblgen.
-static mlir::GenRegistration
-    genEnumDefs("gen-enum-defs", "Generate enum utility definitions",
-                [](const RecordKeeper &records, raw_ostream &os) {
-                  return emitEnumDefs(records, os);
-                });
diff --git a/mlir/lib/TableGen/FormatGen.cpp b/mlir/lib/TableGen/FormatGen.cpp
index 417f7ff89187f..6adcf6ffee965 100644
--- a/mlir/lib/TableGen/FormatGen.cpp
+++ b/mlir/lib/TableGen/FormatGen.cpp
@@ -531,12 +531,3 @@ bool mlir::tblgen::isValidLiteral(StringRef value,
   // Otherwise, this must be an identifier.
   return canFormatStringAsKeyword(value, emitError);
 }
-
-//===----------------------------------------------------------------------===//
-// Commandline Options
-//===----------------------------------------------------------------------===//
-
-llvm::cl::opt<bool> mlir::tblgen::formatErrorIsFatal(
-    "asmformat-error-is-fatal",
-    llvm::cl::desc("Emit a fatal error if format parsing fails"),
-    llvm::cl::init(true));
diff --git a/mlir/lib/TableGen/LLVMIRConversionGen.cpp b/mlir/lib/TableGen/LLVMIRConversionGen.cpp
index 2c7acec3b1b85..c8b9e3680577d 100644
--- a/mlir/lib/TableGen/LLVMIRConversionGen.cpp
+++ b/mlir/lib/TableGen/LLVMIRConversionGen.cpp
@@ -24,7 +24,6 @@
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/TableGen/Error.h"
 #include "llvm/TableGen/Record.h"
-#include "llvm/TableGen/TableGenBackend.h"
 
 using namespace llvm;
 using namespace mlir;
@@ -178,7 +177,8 @@ static LogicalResult emitOneBuilder(const Record &record, raw_ostream &os) {
 
 // Emit all builders.  Returns false on success because of the generator
 // registration requirements.
-static bool emitBuilders(const RecordKeeper &recordKeeper, raw_ostream &os) {
+bool mlir::tblgen::emitLLVMBuilders(const RecordKeeper &recordKeeper,
+                                    raw_ostream &os) {
   for (const Record *def :
        recordKeeper.getAllDerivedDefinitions("LLVM_OpBase")) {
     if (failed(emitOneBuilder(*def, os)))
@@ -306,8 +306,8 @@ static LogicalResult emitOneMLIRBuilder(const Record &record, raw_ostream &os,
 
 // Emit all intrinsic MLIR builders. Returns false on success because of the
 // generator registration requirements.
-static bool emitIntrMLIRBuilders(const RecordKeeper &recordKeeper,
-                                 raw_ostream &os) {
+bool mlir::tblgen::emitLLVMIntrMLIRBuilders(
+    const llvm::RecordKeeper &recordKeeper, raw_ostream &os) {
   // Emit condition to check if "llvmEnumName" matches the intrinsic id.
   auto emitIntrCond = [](const Record &record) {
     return "intrinsicID == llvm::Intrinsic::" +
@@ -323,8 +323,8 @@ static bool emitIntrMLIRBuilders(const RecordKeeper &recordKeeper,
 
 // Emit all op builders. Returns false on success because of the
 // generator registration requirements.
-static bool emitOpMLIRBuilders(const RecordKeeper &recordKeeper,
-                               raw_ostream &os) {
+bool mlir::tblgen::emitLLVMOpMLIRBuilders(
+    const llvm::RecordKeeper &recordKeeper, raw_ostream &os) {
   // Emit condition to check if "llvmInstName" matches the instruction opcode.
   auto emitOpcodeCond = [](const Record &record) {
     return "inst->getOpcode() == llvm::Instruction::" +
@@ -541,8 +541,8 @@ static void emitOneCEnumFromConversion(const llvm::Record *record,
 // Emits conversion functions between MLIR enum attribute case and corresponding
 // LLVM API enumerants for all registered LLVM dialect enum attributes.
 template <bool ConvertTo>
-static bool emitEnumConversionDefs(const RecordKeeper &recordKeeper,
-                                   raw_ostream &os) {
+bool mlir::tblgen::emitLLVMEnumConversionDefs(
+    const llvm::RecordKeeper &recordKeeper, raw_ostream &os) {
   for (const Record *def :
        recordKeeper.getAllDerivedDefinitions("LLVM_EnumAttr"))
     if (ConvertTo)
@@ -560,6 +560,11 @@ static bool emitEnumConversionDefs(const RecordKeeper &recordKeeper,
   return false;
 }
 
+template bool mlir::tblgen::emitLLVMEnumConversionDefs<true>(
+    const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
+template bool mlir::tblgen::emitLLVMEnumConversionDefs<false>(
+    const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
+
 static void emitOneIntrinsic(const Record &record, raw_ostream &os) {
   auto op = tblgen::Operator(record);
   os << "llvm::Intrinsic::" << record.getValueAsString("llvmEnumName") << ",\n";
@@ -567,38 +572,11 @@ static void emitOneIntrinsic(const Record &record, raw_ostream &os) {
 
 // Emit the list of LLVM IR intrinsics identifiers that are convertible to a
 // matching MLIR LLVM dialect intrinsic operation.
-static bool emitConvertibleIntrinsics(const RecordKeeper &recordKeeper,
-                                      raw_ostream &os) {
+bool mlir::tblgen::emitLLVMConvertibleIntrinsics(
+    const llvm::RecordKeeper &recordKeeper, raw_ostream &os) {
   for (const Record *def :
        recordKeeper.getAllDerivedDefinitions("LLVM_IntrOpBase"))
     emitOneIntrinsic(*def, os);
 
   return false;
 }
-
-static mlir::GenRegistration
-    genLLVMIRConversions("gen-llvmir-conversions",
-                         "Generate LLVM IR conversions", emitBuilders);
-
-static mlir::GenRegistration genOpFromLLVMIRConversions(
-    "gen-op-from-llvmir-conversions",
-    "Generate conversions of operations from LLVM IR", emitOpMLIRBuilders);
-
-static mlir::GenRegistration genIntrFromLLVMIRConversions(
-    "gen-intr-from-llvmir-conversions",
-    "Generate conversions of intrinsics from LLVM IR", emitIntrMLIRBuilders);
-
-static mlir::GenRegistration
-    genEnumToLLVMConversion("gen-enum-to-llvmir-conversions",
-                            "Generate conversions of EnumAttrs to LLVM IR",
-                            emitEnumConversionDefs</*ConvertTo=*/true>);
-
-static mlir::GenRegistration
-    genEnumFromLLVMConversion("gen-enum-from-llvmir-conversions",
-                              "Generate conversions of EnumAttrs from LLVM IR",
-                              emitEnumConversionDefs</*ConvertTo=*/false>);
-
-static mlir::GenRegistration genConvertibleLLVMIRIntrinsics(
-    "gen-convertible-llvmir-intrinsics",
-    "Generate list of convertible LLVM IR intrinsics",
-    emitConvertibleIntrinsics);
diff --git a/mlir/lib/TableGen/LLVMIRIntrinsicGen.cpp b/mlir/lib/TableGen/LLVMIRIntrinsicGen.cpp
index 1e3cd8b86d567..849165c462990 100644
--- a/mlir/lib/TableGen/LLVMIRIntrinsicGen.cpp
+++ b/mlir/lib/TableGen/LLVMIRIntrinsicGen.cpp
@@ -24,32 +24,6 @@
 #include "llvm/TableGen/Record.h"
 #include "llvm/TableGen/TableGenBackend.h"
 
-static llvm::cl::OptionCategory intrinsicGenCat("Intrinsics Generator Options");
-
-static llvm::cl::opt<std::string>
-    nameFilter("llvmir-intrinsics-filter",
-               llvm::cl::desc("Only keep the intrinsics with the specified "
-                              "substring in their record name"),
-               llvm::cl::cat(intrinsicGenCat));
-
-static llvm::cl::opt<std::string>
-    opBaseClass("dialect-opclass-base",
-                llvm::cl::desc("The base class for the ops in the dialect we "
-                               "are planning to emit"),
-                llvm::cl::init("LLVM_IntrOp"), llvm::cl::cat(intrinsicGenCat));
-
-static llvm::cl::opt<std::string> accessGroupRegexp(
-    "llvmir-intrinsics-access-group-regexp",
-    llvm::cl::desc("Mark intrinsics that match the specified "
-                   "regexp as taking an access group metadata"),
-    llvm::cl::cat(intrinsicGenCat));
-
-static llvm::cl::opt<std::string> aliasAnalysisRegexp(
-    "llvmir-intrinsics-alias-analysis-regexp",
-    llvm::cl::desc("Mark intrinsics that match the specified "
-                   "regexp as taking alias.scopes, noalias, and tbaa metadata"),
-    llvm::cl::cat(intrinsicGenCat));
-
 // Used to represent the indices of overloadable operands/results.
 using IndicesTy = llvm::SmallBitVector;
 
@@ -195,7 +169,10 @@ void printBracketedRange(const Range &range, llvm::raw_ostream &os) {
 
 /// Emits ODS (TableGen-based) code for `record` representing an LLVM intrinsic.
 /// Returns true on error, false on success.
-static bool emitIntrinsic(const llvm::Record &record, llvm::raw_ostream &os) {
+static bool emitIntrinsic(const llvm::Record &record, llvm::raw_ostream &os,
+                          const std::string &accessGroupRegexp,
+                          const std::string &aliasAnalysisRegexp,
+                          const std::string &opBaseClass) {
   LLVMIntrinsic intr(record);
 
   llvm::Regex accessGroupMatcher(accessGroupRegexp);
@@ -247,8 +224,12 @@ static bool emitIntrinsic(const llvm::Record &record, llvm::raw_ostream &os) {
 /// Traverses the list of TableGen definitions derived from the "Intrinsic"
 /// class and generates MLIR ODS definitions for those intrinsics that have
 /// the name matching the filter.
-static bool emitIntrinsics(const llvm::RecordKeeper &records,
-                           llvm::raw_ostream &os) {
+bool mlir::tblgen::emitLLVMIntrinsics(const llvm::RecordKeeper &records,
+                                      llvm::raw_ostream &os,
+                                      const std::string &nameFilter,
+                                      const std::string &accessGroupRegexp,
+                                      const std::string &aliasAnalysisRegexp,
+                                      const std::string &opBaseClass) {
   llvm::emitSourceFileHeader("Operations for LLVM intrinsics", os, records);
   os << "include \"mlir/Dialect/LLVMIR/LLVMOpBase.td\"\n";
   os << "include \"mlir/Interfaces/SideEffectInterfaces.td\"\n\n";
@@ -257,13 +238,10 @@ static bool emitIntrinsics(const llvm::RecordKeeper &records,
   for (const llvm::Record *r : defs) {
     if (!nameFilter.empty() && !r->getName().contains(nameFilter))
       continue;
-    if (emitIntrinsic(*r, os))
+    if (emitIntrinsic(*r, os, accessGroupRegexp, aliasAnalysisRegexp,
+                      opBaseClass))
       return true;
   }
 
   return false;
 }
-
-static mlir::GenRegistration genLLVMIRIntrinsics("gen-llvmir-intrinsics",
-                                                 "Generate LLVM IR intrinsics",
-                                                 emitIntrinsics);
diff --git a/mlir/lib/TableGen/OpDefinitionsGen.cpp b/mlir/lib/TableGen/OpDefinitionsGen.cpp
index a4e702f4b24ac..1900304a21b45 100644
--- a/mlir/lib/TableGen/OpDefinitionsGen.cpp
+++ b/mlir/lib/TableGen/OpDefinitionsGen.cpp
@@ -11,9 +11,6 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "mlir/TableGen/OpClass.h"
-#include "mlir/TableGen/OpFormatGen.h"
-#include "mlir/TableGen/OpGenHelpers.h"
 #include "mlir/TableGen/Argument.h"
 #include "mlir/TableGen/Attribute.h"
 #include "mlir/TableGen/Class.h"
@@ -21,6 +18,9 @@
 #include "mlir/TableGen/Format.h"
 #include "mlir/TableGen/GenInfo.h"
 #include "mlir/TableGen/Interfaces.h"
+#include "mlir/TableGen/OpClass.h"
+#include "mlir/TableGen/OpFormatGen.h"
+#include "mlir/TableGen/OpGenHelpers.h"
 #include "mlir/TableGen/Operator.h"
 #include "mlir/TableGen/Property.h"
 #include "mlir/TableGen/SideEffects.h"
@@ -544,14 +544,17 @@ class OpEmitter {
 public:
   static void
   emitDecl(const Operator &op, raw_ostream &os,
-           const StaticVerifierFunctionEmitter &staticVerifierEmitter);
+           const StaticVerifierFunctionEmitter &staticVerifierEmitter,
+           bool formatErrorIsFatal);
   static void
   emitDef(const Operator &op, raw_ostream &os,
-          const StaticVerifierFunctionEmitter &staticVerifierEmitter);
+          const StaticVerifierFunctionEmitter &staticVerifierEmitter,
+          bool formatErrorIsFatal);
 
 private:
   OpEmitter(const Operator &op,
-            const StaticVerifierFunctionEmitter &staticVerifierEmitter);
+            const StaticVerifierFunctionEmitter &staticVerifierEmitter,
+            bool formatErrorIsFatal);
 
   void emitDecl(raw_ostream &os);
   void emitDef(raw_ostream &os);
@@ -733,6 +736,9 @@ class OpEmitter {
 
   // Helper for emitting op code.
   OpOrAdaptorHelper emitHelper;
+
+  /// Whether a failure in parsing the assembly format should be a fatal error.
+  bool formatErrorIsFatal;
 };
 
 } // namespace
@@ -1019,12 +1025,14 @@ static std::string formatExtraDefinitions(const Operator &op) {
 }
 
 OpEmitter::OpEmitter(const Operator &op,
-                     const StaticVerifierFunctionEmitter &staticVerifierEmitter)
+                     const StaticVerifierFunctionEmitter &staticVerifierEmitter,
+                     bool formatErrorIsFatal)
     : def(op.getDef()), op(op),
       opClass(op.getCppClassName(), formatExtraDeclarations(op),
               formatExtraDefinitions(op)),
       staticVerifierEmitter(staticVerifierEmitter),
-      emitHelper(op, /*emitForOp=*/true) {
+      emitHelper(op, /*emitForOp=*/true),
+      formatErrorIsFatal(formatErrorIsFatal) {
   verifyCtx.addSubst("_op", "(*this->getOperation())");
   verifyCtx.addSubst("_ctxt", "this->getOperation()->getContext()");
 
@@ -1054,19 +1062,22 @@ OpEmitter::OpEmitter(const Operator &op,
   genFolderDecls();
   genTypeInterfaceMethods();
   genOpInterfaceMethods();
-  generateOpFormat(op, opClass);
+  generateOpFormat(op, opClass, formatErrorIsFatal);
   genSideEffectInterfaceMethods();
 }
+
 void OpEmitter::emitDecl(
     const Operator &op, raw_ostream &os,
-    const StaticVerifierFunctionEmitter &staticVerifierEmitter) {
-  OpEmitter(op, staticVerifierEmitter).emitDecl(os);
+    const StaticVerifierFunctionEmitter &staticVerifierEmitter,
+    bool formatErrorIsFatal) {
+  OpEmitter(op, staticVerifierEmitter, formatErrorIsFatal).emitDecl(os);
 }
 
 void OpEmitter::emitDef(
     const Operator &op, raw_ostream &os,
-    const StaticVerifierFunctionEmitter &staticVerifierEmitter) {
-  OpEmitter(op, staticVerifierEmitter).emitDef(os);
+    const StaticVerifierFunctionEmitter &staticVerifierEmitter,
+    bool formatErrorIsFatal) {
+  OpEmitter(op, staticVerifierEmitter, formatErrorIsFatal).emitDef(os);
 }
 
 void OpEmitter::emitDecl(raw_ostream &os) {
@@ -2481,8 +2492,7 @@ void OpEmitter::genSeparateArgParamBuilder() {
 
       // Avoid emitting "resultTypes.size() >= 0u" which is always true.
       if (!hasVariadicResult || numNonVariadicResults != 0)
-        body << "  "
-             << "assert(resultTypes.size() "
+        body << "  " << "assert(resultTypes.size() "
              << (hasVariadicResult ? ">=" : "==") << " "
              << numNonVariadicResults
              << "u && \"mismatched number of results\");\n";
@@ -4362,7 +4372,7 @@ static void
 emitOpClasses(const RecordKeeper &recordKeeper,
               const std::vector<Record *> &defs, raw_ostream &os,
               const StaticVerifierFunctionEmitter &staticVerifierEmitter,
-              bool emitDecl) {
+              bool emitDecl, bool formatErrorIsFatal) {
   if (defs.empty())
     return;
 
@@ -4374,7 +4384,7 @@ emitOpClasses(const RecordKeeper &recordKeeper,
         os << formatv(opCommentHeader, op.getQualCppClassName(),
                       "declarations");
         OpOperandAdaptorEmitter::emitDecl(op, staticVerifierEmitter, os);
-        OpEmitter::emitDecl(op, os, staticVerifierEmitter);
+        OpEmitter::emitDecl(op, os, staticVerifierEmitter, formatErrorIsFatal);
       }
       // Emit the TypeID explicit specialization to have a single definition.
       if (!op.getCppNamespace().empty())
@@ -4385,7 +4395,7 @@ emitOpClasses(const RecordKeeper &recordKeeper,
         NamespaceEmitter emitter(os, op.getCppNamespace());
         os << formatv(opCommentHeader, op.getQualCppClassName(), "definitions");
         OpOperandAdaptorEmitter::emitDef(op, staticVerifierEmitter, os);
-        OpEmitter::emitDef(op, os, staticVerifierEmitter);
+        OpEmitter::emitDef(op, os, staticVerifierEmitter, formatErrorIsFatal);
       }
       // Emit the TypeID explicit specialization to have a single definition.
       if (!op.getCppNamespace().empty())
@@ -4398,7 +4408,7 @@ emitOpClasses(const RecordKeeper &recordKeeper,
 /// Emit the declarations for the provided op classes.
 static void emitOpClassDecls(const RecordKeeper &recordKeeper,
                              const std::vector<Record *> &defs,
-                             raw_ostream &os) {
+                             bool formatErrorIsFatal, raw_ostream &os) {
   // First emit forward declaration for each class, this allows them to refer
   // to each others in traits for example.
   for (auto *def : defs) {
@@ -4414,13 +4424,13 @@ static void emitOpClassDecls(const RecordKeeper &recordKeeper,
   StaticVerifierFunctionEmitter staticVerifierEmitter(os, recordKeeper);
   staticVerifierEmitter.collectOpConstraints(defs);
   emitOpClasses(recordKeeper, defs, os, staticVerifierEmitter,
-                /*emitDecl=*/true);
+                /*emitDecl=*/true, formatErrorIsFatal);
 }
 
 /// Emit the definitions for the provided op classes.
 static void emitOpClassDefs(const RecordKeeper &recordKeeper,
-                            ArrayRef<Record *> defs, raw_ostream &os,
-                            StringRef constraintPrefix = "") {
+                            ArrayRef<Record *> defs, bool formatErrorIsFatal,
+                            raw_ostream &os, StringRef constraintPrefix = "") {
   if (defs.empty())
     return;
 
@@ -4433,20 +4443,24 @@ static void emitOpClassDefs(const RecordKeeper &recordKeeper,
 
   // Emit the classes.
   emitOpClasses(recordKeeper, defs, os, staticVerifierEmitter,
-                /*emitDecl=*/false);
+                /*emitDecl=*/false, formatErrorIsFatal);
 }
 
 /// Emit op declarations for all op records.
-static bool emitOpDecls(const RecordKeeper &recordKeeper, raw_ostream &os) {
+bool mlir::tblgen::emitOpDecls(const RecordKeeper &recordKeeper,
+                               raw_ostream &os, const std::string &opIncFilter,
+                               const std::string &opExcFilter,
+                               unsigned opShardCount, bool formatErrorIsFatal) {
   emitSourceFileHeader("Op Declarations", os, recordKeeper);
 
-  std::vector<Record *> defs = getRequestedOpDefinitions(recordKeeper);
-  emitOpClassDecls(recordKeeper, defs, os);
+  std::vector<Record *> defs =
+      getRequestedOpDefinitions(recordKeeper, opIncFilter, opExcFilter);
+  emitOpClassDecls(recordKeeper, defs, formatErrorIsFatal, os);
 
   // If we are generating sharded op definitions, emit the sharded op
   // registration hooks.
   SmallVector<ArrayRef<Record *>, 4> shardedDefs;
-  shardOpDefinitions(defs, shardedDefs);
+  shardOpDefinitions(defs, shardedDefs, opShardCount);
   if (defs.empty() || shardedDefs.size() <= 1)
     return false;
 
@@ -4470,7 +4484,7 @@ static bool emitOpDecls(const RecordKeeper &recordKeeper, raw_ostream &os) {
 static void emitOpDefShard(const RecordKeeper &recordKeeper,
                            ArrayRef<Record *> defs, const Dialect &dialect,
                            unsigned shardIndex, unsigned shardCount,
-                           raw_ostream &os) {
+                           bool formatErrorIsFatal, raw_ostream &os) {
   std::string shardGuard = "GET_OP_DEFS_";
   std::string indexStr = std::to_string(shardIndex);
   shardGuard += indexStr;
@@ -4501,16 +4515,20 @@ static void emitOpDefShard(const RecordKeeper &recordKeeper,
   os << "}\n";
 
   // Generate the per-shard op definitions.
-  emitOpClassDefs(recordKeeper, defs, os, indexStr);
+  emitOpClassDefs(recordKeeper, defs, formatErrorIsFatal, os, indexStr);
 }
 
 /// Emit op definitions for all op records.
-static bool emitOpDefs(const RecordKeeper &recordKeeper, raw_ostream &os) {
+bool mlir::tblgen::emitOpDefs(const RecordKeeper &recordKeeper, raw_ostream &os,
+                              const std::string &opIncFilter,
+                              const std::string &opExcFilter,
+                              unsigned opShardCount, bool formatErrorIsFatal) {
   emitSourceFileHeader("Op Definitions", os, recordKeeper);
 
-  std::vector<Record *> defs = getRequestedOpDefinitions(recordKeeper);
+  std::vector<Record *> defs =
+      getRequestedOpDefinitions(recordKeeper, opIncFilter, opExcFilter);
   SmallVector<ArrayRef<Record *>, 4> shardedDefs;
-  shardOpDefinitions(defs, shardedDefs);
+  shardOpDefinitions(defs, shardedDefs, opShardCount);
 
   // If no shard was requested, emit the regular op list and class definitions.
   if (shardedDefs.size() == 1) {
@@ -4523,7 +4541,7 @@ static bool emitOpDefs(const RecordKeeper &recordKeeper, raw_ostream &os) {
     }
     {
       IfDefScope scope("GET_OP_CLASSES", os);
-      emitOpClassDefs(recordKeeper, defs, os);
+      emitOpClassDefs(recordKeeper, defs, formatErrorIsFatal, os);
     }
     return false;
   }
@@ -4532,19 +4550,8 @@ static bool emitOpDefs(const RecordKeeper &recordKeeper, raw_ostream &os) {
     return false;
   Dialect dialect = Operator(defs.front()).getDialect();
   for (auto [idx, value] : llvm::enumerate(shardedDefs)) {
-    emitOpDefShard(recordKeeper, value, dialect, idx, shardedDefs.size(), os);
+    emitOpDefShard(recordKeeper, value, dialect, idx, shardedDefs.size(),
+                   formatErrorIsFatal, os);
   }
   return false;
 }
-
-static mlir::GenRegistration
-    genOpDecls("gen-op-decls", "Generate op declarations",
-               [](const RecordKeeper &records, raw_ostream &os) {
-                 return emitOpDecls(records, os);
-               });
-
-static mlir::GenRegistration genOpDefs("gen-op-defs", "Generate op definitions",
-                                       [](const RecordKeeper &records,
-                                          raw_ostream &os) {
-                                         return emitOpDefs(records, os);
-                                       });
diff --git a/mlir/lib/TableGen/OpDocGen.cpp b/mlir/lib/TableGen/OpDocGen.cpp
index de77d0a3614ae..46e4582229542 100644
--- a/mlir/lib/TableGen/OpDocGen.cpp
+++ b/mlir/lib/TableGen/OpDocGen.cpp
@@ -11,12 +11,12 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "mlir/TableGen/DialectGenUtilities.h"
-#include "mlir/TableGen/DocGenUtilities.h"
-#include "mlir/TableGen/OpGenHelpers.h"
 #include "mlir/Support/IndentedOstream.h"
 #include "mlir/TableGen/AttrOrTypeDef.h"
+#include "mlir/TableGen/DialectGenUtilities.h"
+#include "mlir/TableGen/DocGenUtilities.h"
 #include "mlir/TableGen/GenInfo.h"
+#include "mlir/TableGen/OpGenHelpers.h"
 #include "mlir/TableGen/Operator.h"
 #include "llvm/ADT/DenseMap.h"
 #include "llvm/ADT/SetVector.h"
@@ -33,20 +33,6 @@
 #include <set>
 #include <string>
 
-//===----------------------------------------------------------------------===//
-// Commandline Options
-//===----------------------------------------------------------------------===//
-static llvm::cl::OptionCategory
-    docCat("Options for -gen-(attrdef|typedef|op|dialect)-doc");
-llvm::cl::opt<std::string>
-    stripPrefix("strip-prefix",
-                llvm::cl::desc("Strip prefix of the fully qualified names"),
-                llvm::cl::init("::mlir::"), llvm::cl::cat(docCat));
-llvm::cl::opt<bool> allowHugoSpecificFeatures(
-    "allow-hugo-specific-features",
-    llvm::cl::desc("Allows using features specific to Hugo"),
-    llvm::cl::init(false), llvm::cl::cat(docCat));
-
 using namespace llvm;
 using namespace mlir;
 using namespace mlir::tblgen;
@@ -191,7 +177,9 @@ static StringRef resolveAttrDescription(const Attribute &attr) {
   return description;
 }
 
-static void emitOpDoc(const Operator &op, raw_ostream &os) {
+static void emitOpDoc(const Operator &op, raw_ostream &os,
+                      const std::string &stripPrefix,
+                      bool allowHugoSpecificFeatures) {
   std::string classNameStr = op.getQualCppClassName();
   StringRef className = classNameStr;
   (void)className.consume_front(stripPrefix);
@@ -228,8 +216,7 @@ static void emitOpDoc(const Operator &op, raw_ostream &os) {
         // Expandable description.
         // This appears as just the summary, but when clicked shows the full
         // description.
-        os << "<details>"
-           << "<summary>" << it.attr.getSummary() << "</summary>"
+        os << "<details>" << "<summary>" << it.attr.getSummary() << "</summary>"
            << "{{% markdown %}}" << description << "{{% /markdown %}}"
            << "</details>";
       } else {
@@ -282,13 +269,18 @@ static void emitSourceLink(StringRef inputFilename, raw_ostream &os) {
      << inputFromMlirInclude << ")\n\n";
 }
 
-static void emitOpDoc(const RecordKeeper &recordKeeper, raw_ostream &os) {
-  auto opDefs = getRequestedOpDefinitions(recordKeeper);
+void mlir::tblgen::emitOpDoc(const RecordKeeper &recordKeeper, raw_ostream &os,
+                             const std::string &stripPrefix,
+                             bool allowHugoSpecificFeatures,
+                             const std::string &opIncFilter,
+                             const std::string &opExcFilter) {
+  auto opDefs =
+      getRequestedOpDefinitions(recordKeeper, opIncFilter, opExcFilter);
 
   os << "<!-- Autogenerated by mlir-tblgen; don't manually edit -->\n";
   emitSourceLink(recordKeeper.getInputFilename(), os);
   for (const llvm::Record *opDef : opDefs)
-    emitOpDoc(Operator(opDef), os);
+    ::emitOpDoc(Operator(opDef), os, stripPrefix, allowHugoSpecificFeatures);
 }
 
 //===----------------------------------------------------------------------===//
@@ -371,14 +363,15 @@ static void emitAttrOrTypeDefDoc(const AttrOrTypeDef &def, raw_ostream &os) {
   os << "\n";
 }
 
-static void emitAttrOrTypeDefDoc(const RecordKeeper &recordKeeper,
-                                 raw_ostream &os, StringRef recordTypeName) {
+void mlir::tblgen::emitAttrOrTypeDefDoc(const RecordKeeper &recordKeeper,
+                                        raw_ostream &os,
+                                        StringRef recordTypeName) {
   std::vector<llvm::Record *> defs =
       recordKeeper.getAllDerivedDefinitions(recordTypeName);
 
   os << "<!-- Autogenerated by mlir-tblgen; don't manually edit -->\n";
   for (const llvm::Record *def : defs)
-    emitAttrOrTypeDefDoc(AttrOrTypeDef(def), os);
+    ::emitAttrOrTypeDefDoc(AttrOrTypeDef(def), os);
 }
 
 //===----------------------------------------------------------------------===//
@@ -413,7 +406,8 @@ static void maybeNest(bool nest, llvm::function_ref<void(raw_ostream &os)> fn,
 static void emitBlock(ArrayRef<Attribute> attributes, StringRef inputFilename,
                       ArrayRef<AttrDef> attrDefs, ArrayRef<OpDocGroup> ops,
                       ArrayRef<Type> types, ArrayRef<TypeDef> typeDefs,
-                      raw_ostream &os) {
+                      const std::string &stripPrefix,
+                      bool allowHugoSpecificFeatures, raw_ostream &os) {
   if (!ops.empty()) {
     os << "## Operations\n\n";
     emitSourceLink(inputFilename, os);
@@ -428,7 +422,7 @@ static void emitBlock(ArrayRef<Attribute> attributes, StringRef inputFilename,
               os << "\n\n";
             }
             for (const Operator &op : grouping.ops) {
-              emitOpDoc(op, os);
+              emitOpDoc(op, os, stripPrefix, allowHugoSpecificFeatures);
             }
           },
           os);
@@ -465,7 +459,8 @@ static void emitDialectDoc(const Dialect &dialect, StringRef inputFilename,
                            ArrayRef<Attribute> attributes,
                            ArrayRef<AttrDef> attrDefs, ArrayRef<OpDocGroup> ops,
                            ArrayRef<Type> types, ArrayRef<TypeDef> typeDefs,
-                           raw_ostream &os) {
+                           const std::string &stripPrefix,
+                           bool allowHugoSpecificFeatures, raw_ostream &os) {
   os << "# '" << dialect.getName() << "' Dialect\n\n";
   emitIfNotEmpty(dialect.getSummary(), os);
   emitIfNotEmpty(dialect.getDescription(), os);
@@ -475,18 +470,27 @@ static void emitDialectDoc(const Dialect &dialect, StringRef inputFilename,
   if (!r.match(dialect.getDescription()))
     os << "[TOC]\n\n";
 
-  emitBlock(attributes, inputFilename, attrDefs, ops, types, typeDefs, os);
+  emitBlock(attributes, inputFilename, attrDefs, ops, types, typeDefs,
+            stripPrefix, allowHugoSpecificFeatures, os);
 }
 
-static bool emitDialectDoc(const RecordKeeper &recordKeeper, raw_ostream &os) {
+bool mlir::tblgen::emitDialectDoc(const RecordKeeper &recordKeeper,
+                                  raw_ostream &os,
+                                  const std::string &selectedDialect,
+                                  const std::string &opIncFilter,
+                                  const std::string &opExcFilter,
+                                  const std::string &stripPrefix,
+                                  bool allowHugoSpecificFeatures) {
   std::vector<Record *> dialectDefs =
       recordKeeper.getAllDerivedDefinitionsIfDefined("Dialect");
   SmallVector<Dialect> dialects(dialectDefs.begin(), dialectDefs.end());
-  std::optional<Dialect> dialect = findDialectToGenerate(dialects);
+  std::optional<Dialect> dialect =
+      findDialectToGenerate(dialects, selectedDialect);
   if (!dialect)
     return true;
 
-  std::vector<Record *> opDefs = getRequestedOpDefinitions(recordKeeper);
+  std::vector<Record *> opDefs =
+      getRequestedOpDefinitions(recordKeeper, opIncFilter, opExcFilter);
   std::vector<Record *> attrDefs =
       recordKeeper.getAllDerivedDefinitionsIfDefined("DialectAttr");
   std::vector<Record *> typeDefs =
@@ -557,38 +561,10 @@ static bool emitDialectDoc(const RecordKeeper &recordKeeper, raw_ostream &os) {
   os << "<!-- Autogenerated by mlir-tblgen; don't manually edit -->\n";
   emitDialectDoc(*dialect, recordKeeper.getInputFilename(), dialectAttrs,
                  dialectAttrDefs, dialectOps, dialectTypes, dialectTypeDefs,
-                 os);
+                 stripPrefix, allowHugoSpecificFeatures, os);
   return false;
 }
 
 //===----------------------------------------------------------------------===//
 // Gen Registration
 //===----------------------------------------------------------------------===//
-
-static mlir::GenRegistration
-    genAttrRegister("gen-attrdef-doc",
-                    "Generate dialect attribute documentation",
-                    [](const RecordKeeper &records, raw_ostream &os) {
-                      emitAttrOrTypeDefDoc(records, os, "AttrDef");
-                      return false;
-                    });
-
-static mlir::GenRegistration
-    genOpRegister("gen-op-doc", "Generate dialect documentation",
-                  [](const RecordKeeper &records, raw_ostream &os) {
-                    emitOpDoc(records, os);
-                    return false;
-                  });
-
-static mlir::GenRegistration
-    genTypeRegister("gen-typedef-doc", "Generate dialect type documentation",
-                    [](const RecordKeeper &records, raw_ostream &os) {
-                      emitAttrOrTypeDefDoc(records, os, "TypeDef");
-                      return false;
-                    });
-
-static mlir::GenRegistration
-    genRegister("gen-dialect-doc", "Generate dialect documentation",
-                [](const RecordKeeper &records, raw_ostream &os) {
-                  return emitDialectDoc(records, os);
-                });
diff --git a/mlir/lib/TableGen/OpFormatGen.cpp b/mlir/lib/TableGen/OpFormatGen.cpp
index 4e157d4092d0e..f78b7d722b30f 100644
--- a/mlir/lib/TableGen/OpFormatGen.cpp
+++ b/mlir/lib/TableGen/OpFormatGen.cpp
@@ -7,11 +7,11 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/TableGen/OpFormatGen.h"
-#include "mlir/TableGen/FormatGen.h"
-#include "mlir/TableGen/OpClass.h"
 #include "mlir/Support/LLVM.h"
 #include "mlir/TableGen/Class.h"
 #include "mlir/TableGen/Format.h"
+#include "mlir/TableGen/FormatGen.h"
+#include "mlir/TableGen/OpClass.h"
 #include "mlir/TableGen/Operator.h"
 #include "mlir/TableGen/Trait.h"
 #include "llvm/ADT/MapVector.h"
@@ -3621,7 +3621,8 @@ LogicalResult OpFormatParser::verifyOptionalGroupElement(SMLoc loc,
 // Interface
 //===----------------------------------------------------------------------===//
 
-void mlir::tblgen::generateOpFormat(const Operator &constOp, OpClass &opClass) {
+void mlir::tblgen::generateOpFormat(const Operator &constOp, OpClass &opClass,
+                                    bool formatErrorIsFatal) {
   // TODO: Operator doesn't expose all necessary functionality via
   // the const interface.
   Operator &op = const_cast<Operator &>(constOp);
diff --git a/mlir/lib/TableGen/OpGenHelpers.cpp b/mlir/lib/TableGen/OpGenHelpers.cpp
index 2dd4cd66b6b66..658a78b6960d2 100644
--- a/mlir/lib/TableGen/OpGenHelpers.cpp
+++ b/mlir/lib/TableGen/OpGenHelpers.cpp
@@ -12,7 +12,6 @@
 
 #include "mlir/TableGen/OpGenHelpers.h"
 #include "llvm/ADT/StringSet.h"
-#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/Regex.h"
 #include "llvm/TableGen/Error.h"
@@ -21,21 +20,6 @@ using namespace llvm;
 using namespace mlir;
 using namespace mlir::tblgen;
 
-cl::OptionCategory opDefGenCat("Options for op definition generators");
-
-static cl::opt<std::string> opIncFilter(
-    "op-include-regex",
-    cl::desc("Regex of name of op's to include (no filter if empty)"),
-    cl::cat(opDefGenCat));
-static cl::opt<std::string> opExcFilter(
-    "op-exclude-regex",
-    cl::desc("Regex of name of op's to exclude (no filter if empty)"),
-    cl::cat(opDefGenCat));
-static cl::opt<unsigned> opShardCount(
-    "op-shard-count",
-    cl::desc("The number of shards into which the op classes will be divided"),
-    cl::cat(opDefGenCat), cl::init(1));
-
 static std::string getOperationName(const Record &def) {
   auto prefix = def.getValueAsDef("opDialect")->getValueAsString("name");
   auto opName = def.getValueAsString("opName");
@@ -45,7 +29,9 @@ static std::string getOperationName(const Record &def) {
 }
 
 std::vector<Record *>
-mlir::tblgen::getRequestedOpDefinitions(const RecordKeeper &recordKeeper) {
+mlir::tblgen::getRequestedOpDefinitions(const RecordKeeper &recordKeeper,
+                                        const std::string &opIncFilter,
+                                        const std::string &opExcFilter) {
   Record *classDef = recordKeeper.getClass("Op");
   if (!classDef)
     PrintFatalError("ERROR: Couldn't find the 'Op' class!\n");
@@ -87,7 +73,8 @@ bool mlir::tblgen::isPythonReserved(StringRef str) {
 
 void mlir::tblgen::shardOpDefinitions(
     ArrayRef<llvm::Record *> defs,
-    SmallVectorImpl<ArrayRef<llvm::Record *>> &shardedDefs) {
+    SmallVectorImpl<ArrayRef<llvm::Record *>> &shardedDefs,
+    unsigned opShardCount) {
   assert(opShardCount > 0 && "expected a positive shard count");
   if (opShardCount == 1) {
     shardedDefs.push_back(defs);
diff --git a/mlir/lib/TableGen/OpInterfacesGen.cpp b/mlir/lib/TableGen/OpInterfacesGen.cpp
index a76fbbbbc7a8d..a2fbb41064311 100644
--- a/mlir/lib/TableGen/OpInterfacesGen.cpp
+++ b/mlir/lib/TableGen/OpInterfacesGen.cpp
@@ -10,6 +10,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "mlir/TableGen/OpInterfacesGen.h"
 #include "mlir/TableGen/DocGenUtilities.h"
 #include "mlir/TableGen/Format.h"
 #include "mlir/TableGen/GenInfo.h"
@@ -61,9 +62,9 @@ static void emitMethodNameAndArgs(const InterfaceMethod &method,
 
 /// Get an array of all OpInterface definitions but exclude those subclassing
 /// "DeclareOpInterfaceMethods".
-static std::vector<llvm::Record *>
-getAllInterfaceDefinitions(const llvm::RecordKeeper &recordKeeper,
-                           StringRef name) {
+std::vector<llvm::Record *>
+mlir::tblgen::getAllInterfaceDefinitions(const llvm::RecordKeeper &recordKeeper,
+                                         StringRef name) {
   std::vector<llvm::Record *> defs =
       recordKeeper.getAllDerivedDefinitions((name + "Interface").str());
 
@@ -79,91 +80,6 @@ getAllInterfaceDefinitions(const llvm::RecordKeeper &recordKeeper,
   return defs;
 }
 
-namespace {
-/// This struct is the base generator used when processing tablegen interfaces.
-class InterfaceGenerator {
-public:
-  bool emitInterfaceDefs();
-  bool emitInterfaceDecls();
-  bool emitInterfaceDocs();
-
-protected:
-  InterfaceGenerator(std::vector<llvm::Record *> &&defs, raw_ostream &os)
-      : defs(std::move(defs)), os(os) {}
-
-  void emitConceptDecl(const Interface &interface);
-  void emitModelDecl(const Interface &interface);
-  void emitModelMethodsDef(const Interface &interface);
-  void emitTraitDecl(const Interface &interface, StringRef interfaceName,
-                     StringRef interfaceTraitsName);
-  void emitInterfaceDecl(const Interface &interface);
-
-  /// The set of interface records to emit.
-  std::vector<llvm::Record *> defs;
-  // The stream to emit to.
-  raw_ostream &os;
-  /// The C++ value type of the interface, e.g. Operation*.
-  StringRef valueType;
-  /// The C++ base interface type.
-  StringRef interfaceBaseType;
-  /// The name of the typename for the value template.
-  StringRef valueTemplate;
-  /// The name of the substituion variable for the value.
-  StringRef substVar;
-  /// The format context to use for methods.
-  tblgen::FmtContext nonStaticMethodFmt;
-  tblgen::FmtContext traitMethodFmt;
-  tblgen::FmtContext extraDeclsFmt;
-};
-
-/// A specialized generator for attribute interfaces.
-struct AttrInterfaceGenerator : public InterfaceGenerator {
-  AttrInterfaceGenerator(const llvm::RecordKeeper &records, raw_ostream &os)
-      : InterfaceGenerator(getAllInterfaceDefinitions(records, "Attr"), os) {
-    valueType = "::mlir::Attribute";
-    interfaceBaseType = "AttributeInterface";
-    valueTemplate = "ConcreteAttr";
-    substVar = "_attr";
-    StringRef castCode = "(::llvm::cast<ConcreteAttr>(tablegen_opaque_val))";
-    nonStaticMethodFmt.addSubst(substVar, castCode).withSelf(castCode);
-    traitMethodFmt.addSubst(substVar,
-                            "(*static_cast<const ConcreteAttr *>(this))");
-    extraDeclsFmt.addSubst(substVar, "(*this)");
-  }
-};
-/// A specialized generator for operation interfaces.
-struct OpInterfaceGenerator : public InterfaceGenerator {
-  OpInterfaceGenerator(const llvm::RecordKeeper &records, raw_ostream &os)
-      : InterfaceGenerator(getAllInterfaceDefinitions(records, "Op"), os) {
-    valueType = "::mlir::Operation *";
-    interfaceBaseType = "OpInterface";
-    valueTemplate = "ConcreteOp";
-    substVar = "_op";
-    StringRef castCode = "(llvm::cast<ConcreteOp>(tablegen_opaque_val))";
-    nonStaticMethodFmt.addSubst("_this", "impl")
-        .addSubst(substVar, castCode)
-        .withSelf(castCode);
-    traitMethodFmt.addSubst(substVar, "(*static_cast<ConcreteOp *>(this))");
-    extraDeclsFmt.addSubst(substVar, "(*this)");
-  }
-};
-/// A specialized generator for type interfaces.
-struct TypeInterfaceGenerator : public InterfaceGenerator {
-  TypeInterfaceGenerator(const llvm::RecordKeeper &records, raw_ostream &os)
-      : InterfaceGenerator(getAllInterfaceDefinitions(records, "Type"), os) {
-    valueType = "::mlir::Type";
-    interfaceBaseType = "TypeInterface";
-    valueTemplate = "ConcreteType";
-    substVar = "_type";
-    StringRef castCode = "(::llvm::cast<ConcreteType>(tablegen_opaque_val))";
-    nonStaticMethodFmt.addSubst(substVar, castCode).withSelf(castCode);
-    traitMethodFmt.addSubst(substVar,
-                            "(*static_cast<const ConcreteType *>(this))");
-    extraDeclsFmt.addSubst(substVar, "(*this)");
-  }
-};
-} // namespace
-
 //===----------------------------------------------------------------------===//
 // GEN: Interface definitions
 //===----------------------------------------------------------------------===//
@@ -217,7 +133,7 @@ static void emitInterfaceDef(const Interface &interface, StringRef valueType,
   }
 }
 
-bool InterfaceGenerator::emitInterfaceDefs() {
+bool mlir::tblgen::InterfaceGenerator::emitInterfaceDefs() {
   llvm::emitSourceFileHeader("Interface Definitions", os);
 
   for (const auto *def : defs)
@@ -229,7 +145,8 @@ bool InterfaceGenerator::emitInterfaceDefs() {
 // GEN: Interface declarations
 //===----------------------------------------------------------------------===//
 
-void InterfaceGenerator::emitConceptDecl(const Interface &interface) {
+void mlir::tblgen::InterfaceGenerator::emitConceptDecl(
+    const Interface &interface) {
   os << "  struct Concept {\n";
 
   // Insert each of the pure virtual concept methods.
@@ -277,7 +194,8 @@ void InterfaceGenerator::emitConceptDecl(const Interface &interface) {
   os << "  };\n";
 }
 
-void InterfaceGenerator::emitModelDecl(const Interface &interface) {
+void mlir::tblgen::InterfaceGenerator::emitModelDecl(
+    const Interface &interface) {
   // Emit the basic model and the fallback model.
   for (const char *modelClass : {"Model", "FallbackModel"}) {
     os << "  template<typename " << valueTemplate << ">\n";
@@ -337,7 +255,8 @@ void InterfaceGenerator::emitModelDecl(const Interface &interface) {
   os << "  };\n";
 }
 
-void InterfaceGenerator::emitModelMethodsDef(const Interface &interface) {
+void mlir::tblgen::InterfaceGenerator::emitModelMethodsDef(
+    const Interface &interface) {
   llvm::SmallVector<StringRef, 2> namespaces;
   llvm::SplitString(interface.getCppNamespace(), namespaces, "::");
   for (StringRef ns : namespaces)
@@ -443,9 +362,9 @@ void InterfaceGenerator::emitModelMethodsDef(const Interface &interface) {
     os << "} // namespace " << ns << "\n";
 }
 
-void InterfaceGenerator::emitTraitDecl(const Interface &interface,
-                                       StringRef interfaceName,
-                                       StringRef interfaceTraitsName) {
+void mlir::tblgen::InterfaceGenerator::emitTraitDecl(
+    const Interface &interface, StringRef interfaceName,
+    StringRef interfaceTraitsName) {
   os << llvm::formatv("  template <typename {3}>\n"
                       "  struct {0}Trait : public ::mlir::{2}<{0},"
                       " detail::{1}>::Trait<{3}> {{\n",
@@ -515,7 +434,8 @@ static void emitInterfaceDeclMethods(const Interface &interface,
     os << tblgen::tgfmt(extraDecls->rtrim(), &extraDeclsFmt) << "\n";
 }
 
-void InterfaceGenerator::emitInterfaceDecl(const Interface &interface) {
+void mlir::tblgen::InterfaceGenerator::emitInterfaceDecl(
+    const Interface &interface) {
   llvm::SmallVector<StringRef, 2> namespaces;
   llvm::SplitString(interface.getCppNamespace(), namespaces, "::");
   for (StringRef ns : namespaces)
@@ -588,7 +508,8 @@ void InterfaceGenerator::emitInterfaceDecl(const Interface &interface) {
        << "    auto* interface = getInterfaceFor(base);\n"
        << "    if (!interface)\n"
           "      return false;\n"
-          "    " << interfaceName << " odsInterfaceInstance(base, interface);\n"
+          "    "
+       << interfaceName << " odsInterfaceInstance(base, interface);\n"
        << "    " << tblgen::tgfmt(extraClassOf->trim(), &extraClassOfFmt)
        << "\n  }\n";
   }
@@ -603,7 +524,7 @@ void InterfaceGenerator::emitInterfaceDecl(const Interface &interface) {
     os << "} // namespace " << ns << "\n";
 }
 
-bool InterfaceGenerator::emitInterfaceDecls() {
+bool mlir::tblgen::InterfaceGenerator::emitInterfaceDecls() {
   llvm::emitSourceFileHeader("Interface Declarations", os);
   // Sort according to ID, so defs are emitted in the order in which they appear
   // in the Tablegen file.
@@ -660,7 +581,7 @@ static void emitInterfaceDoc(const llvm::Record &interfaceDef,
   }
 }
 
-bool InterfaceGenerator::emitInterfaceDocs() {
+bool mlir::tblgen::InterfaceGenerator::emitInterfaceDocs() {
   os << "<!-- Autogenerated by mlir-tblgen; don't manually edit -->\n";
   os << "# " << interfaceBaseType << " definitions\n";
 
@@ -668,41 +589,3 @@ bool InterfaceGenerator::emitInterfaceDocs() {
     emitInterfaceDoc(*def, os);
   return false;
 }
-
-//===----------------------------------------------------------------------===//
-// GEN: Interface registration hooks
-//===----------------------------------------------------------------------===//
-
-namespace {
-template <typename GeneratorT>
-struct InterfaceGenRegistration {
-  InterfaceGenRegistration(StringRef genArg, StringRef genDesc)
-      : genDeclArg(("gen-" + genArg + "-interface-decls").str()),
-        genDefArg(("gen-" + genArg + "-interface-defs").str()),
-        genDocArg(("gen-" + genArg + "-interface-docs").str()),
-        genDeclDesc(("Generate " + genDesc + " interface declarations").str()),
-        genDefDesc(("Generate " + genDesc + " interface definitions").str()),
-        genDocDesc(("Generate " + genDesc + " interface documentation").str()),
-        genDecls(genDeclArg, genDeclDesc,
-                 [](const llvm::RecordKeeper &records, raw_ostream &os) {
-                   return GeneratorT(records, os).emitInterfaceDecls();
-                 }),
-        genDefs(genDefArg, genDefDesc,
-                [](const llvm::RecordKeeper &records, raw_ostream &os) {
-                  return GeneratorT(records, os).emitInterfaceDefs();
-                }),
-        genDocs(genDocArg, genDocDesc,
-                [](const llvm::RecordKeeper &records, raw_ostream &os) {
-                  return GeneratorT(records, os).emitInterfaceDocs();
-                }) {}
-
-  std::string genDeclArg, genDefArg, genDocArg;
-  std::string genDeclDesc, genDefDesc, genDocDesc;
-  mlir::GenRegistration genDecls, genDefs, genDocs;
-};
-} // namespace
-
-static InterfaceGenRegistration<AttrInterfaceGenerator> attrGen("attr",
-                                                                "attribute");
-static InterfaceGenRegistration<OpInterfaceGenerator> opGen("op", "op");
-static InterfaceGenRegistration<TypeInterfaceGenerator> typeGen("type", "type");
diff --git a/mlir/lib/TableGen/OpPythonBindingGen.cpp b/mlir/lib/TableGen/OpPythonBindingGen.cpp
index 43389791a4069..e30809e6cb572 100644
--- a/mlir/lib/TableGen/OpPythonBindingGen.cpp
+++ b/mlir/lib/TableGen/OpPythonBindingGen.cpp
@@ -16,7 +16,6 @@
 #include "mlir/TableGen/GenInfo.h"
 #include "mlir/TableGen/Operator.h"
 #include "llvm/ADT/StringSet.h"
-#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/TableGen/Error.h"
 #include "llvm/TableGen/Record.h"
@@ -271,18 +270,6 @@ def {0}({2}) -> {4}:
   return _get_op_result_or_op_results({1}({3}))
 )Py";
 
-static llvm::cl::OptionCategory
-    clOpPythonBindingCat("Options for -gen-python-op-bindings");
-
-static llvm::cl::opt<std::string>
-    clDialectName("bind-dialect",
-                  llvm::cl::desc("The dialect to run the generator for"),
-                  llvm::cl::init(""), llvm::cl::cat(clOpPythonBindingCat));
-
-static llvm::cl::opt<std::string> clDialectExtensionName(
-    "dialect-extension", llvm::cl::desc("The prefix of the dialect extension"),
-    llvm::cl::init(""), llvm::cl::cat(clOpPythonBindingCat));
-
 using AttributeClasses = DenseMap<StringRef, StringRef>;
 
 /// Checks whether `str` would shadow a generated variable or attribute
@@ -1038,24 +1025,20 @@ static void emitOpBindings(const Operator &op, raw_ostream &os) {
 /// Emits bindings for the dialect specified in the command line, including file
 /// headers and utilities. Returns `false` on success to comply with Tablegen
 /// registration requirements.
-static bool emitAllOps(const llvm::RecordKeeper &records, raw_ostream &os) {
-  if (clDialectName.empty())
-    llvm::PrintFatalError("dialect name not provided");
-
+bool mlir::tblgen::emitAllPythonOps(const llvm::RecordKeeper &records,
+                                    raw_ostream &os,
+                                    const std::string &clDialectName,
+                                    const std::string &clDialectExtensionName) {
   os << fileHeader;
   if (!clDialectExtensionName.empty())
-    os << llvm::formatv(dialectExtensionTemplate, clDialectName.getValue());
+    os << llvm::formatv(dialectExtensionTemplate, clDialectName);
   else
-    os << llvm::formatv(dialectClassTemplate, clDialectName.getValue());
+    os << llvm::formatv(dialectClassTemplate, clDialectName);
 
   for (const llvm::Record *rec : records.getAllDerivedDefinitions("Op")) {
     Operator op(rec);
-    if (op.getDialectName() == clDialectName.getValue())
+    if (op.getDialectName() == clDialectName)
       emitOpBindings(op, os);
   }
   return false;
 }
-
-static GenRegistration
-    genPythonBindings("gen-python-op-bindings",
-                      "Generate Python bindings for MLIR Ops", &emitAllOps);
diff --git a/mlir/lib/TableGen/Operator.cpp b/mlir/lib/TableGen/Operator.cpp
index bd3e3b1c6b7cc..41b32b4181243 100644
--- a/mlir/lib/TableGen/Operator.cpp
+++ b/mlir/lib/TableGen/Operator.cpp
@@ -384,7 +384,8 @@ void Operator::populateTypeInferenceInfo(
   if (getTrait("::mlir::OpTrait::SameOperandsAndResultType")) {
     // Check for a non-variable length operand to use as the type anchor.
     auto *operandI = llvm::find_if(arguments, [](const Argument &arg) {
-      NamedTypeConstraint *operand = llvm::dyn_cast_if_present<NamedTypeConstraint *>(arg);
+      NamedTypeConstraint *operand =
+          llvm::dyn_cast_if_present<NamedTypeConstraint *>(arg);
       return operand && !operand->isVariableLength();
     });
     if (operandI == arguments.end())
diff --git a/mlir/lib/TableGen/PassCAPIGen.cpp b/mlir/lib/TableGen/PassCAPIGen.cpp
index 9f33a4129aacc..8f2f41e672289 100644
--- a/mlir/lib/TableGen/PassCAPIGen.cpp
+++ b/mlir/lib/TableGen/PassCAPIGen.cpp
@@ -21,15 +21,6 @@
 using namespace mlir;
 using namespace mlir::tblgen;
 
-static llvm::cl::OptionCategory
-    passGenCat("Options for -gen-pass-capi-header and -gen-pass-capi-impl");
-static llvm::cl::opt<std::string>
-    groupName("prefix",
-              llvm::cl::desc("The prefix to use for this group of passes. The "
-                             "form will be mlirCreate<prefix><passname>, the "
-                             "prefix can avoid conflicts across libraries."),
-              llvm::cl::cat(passGenCat));
-
 const char *const passDecl = R"(
 /* Create {0} Pass. */
 MLIR_CAPI_EXPORTED MlirPass mlirCreate{0}{1}(void);
@@ -56,15 +47,16 @@ const char *const fileFooter = R"(
 )";
 
 /// Emit TODO
-static bool emitCAPIHeader(const llvm::RecordKeeper &records, raw_ostream &os) {
+bool mlir::tblgen::emitCAPIHeader(const llvm::RecordKeeper &records,
+                                  raw_ostream &os, std::string groupPrefix) {
   os << fileHeader;
   os << "// Registration for the entire group\n";
-  os << "MLIR_CAPI_EXPORTED void mlirRegister" << groupName
+  os << "MLIR_CAPI_EXPORTED void mlirRegister" << groupPrefix
      << "Passes(void);\n\n";
   for (const auto *def : records.getAllDerivedDefinitions("PassBase")) {
     Pass pass(def);
     StringRef defName = pass.getDef()->getName();
-    os << llvm::formatv(passDecl, groupName, defName);
+    os << llvm::formatv(passDecl, groupPrefix, defName);
   }
   os << fileFooter;
   return false;
@@ -91,9 +83,10 @@ void mlirRegister{0}Passes(void) {{
 }
 )";
 
-static bool emitCAPIImpl(const llvm::RecordKeeper &records, raw_ostream &os) {
+bool mlir::tblgen::emitCAPIImpl(const llvm::RecordKeeper &records,
+                                raw_ostream &os, std::string groupPrefix) {
   os << "/* Autogenerated by mlir-tblgen; don't manually edit. */";
-  os << llvm::formatv(passGroupRegistrationCode, groupName);
+  os << llvm::formatv(passGroupRegistrationCode, groupPrefix);
 
   for (const auto *def : records.getAllDerivedDefinitions("PassBase")) {
     Pass pass(def);
@@ -106,15 +99,7 @@ static bool emitCAPIImpl(const llvm::RecordKeeper &records, raw_ostream &os) {
       constructorCall =
           llvm::formatv("create{0}()", pass.getDef()->getName()).str();
 
-    os << llvm::formatv(passCreateDef, groupName, defName, constructorCall);
+    os << llvm::formatv(passCreateDef, groupPrefix, defName, constructorCall);
   }
   return false;
 }
-
-static mlir::GenRegistration genCAPIHeader("gen-pass-capi-header",
-                                           "Generate pass C API header",
-                                           &emitCAPIHeader);
-
-static mlir::GenRegistration genCAPIImpl("gen-pass-capi-impl",
-                                         "Generate pass C API implementation",
-                                         &emitCAPIImpl);
diff --git a/mlir/lib/TableGen/PassDocGen.cpp b/mlir/lib/TableGen/PassDocGen.cpp
index ac63d2126b785..a1c3981f12c39 100644
--- a/mlir/lib/TableGen/PassDocGen.cpp
+++ b/mlir/lib/TableGen/PassDocGen.cpp
@@ -56,7 +56,8 @@ static void emitDoc(const Pass &pass, raw_ostream &os) {
   }
 }
 
-static void emitDocs(const llvm::RecordKeeper &recordKeeper, raw_ostream &os) {
+void mlir::tblgen::emitDocs(const llvm::RecordKeeper &recordKeeper,
+                            raw_ostream &os) {
   os << "<!-- Autogenerated by mlir-tblgen; don't manually edit -->\n";
   auto passDefs = recordKeeper.getAllDerivedDefinitions("PassBase");
 
@@ -71,10 +72,3 @@ static void emitDocs(const llvm::RecordKeeper &recordKeeper, raw_ostream &os) {
   for (Pass *pass : sortedPasses)
     emitDoc(*pass, os);
 }
-
-static mlir::GenRegistration
-    genRegister("gen-pass-doc", "Generate pass documentation",
-                [](const llvm::RecordKeeper &records, raw_ostream &os) {
-                  emitDocs(records, os);
-                  return false;
-                });
diff --git a/mlir/lib/TableGen/PassGen.cpp b/mlir/lib/TableGen/PassGen.cpp
index 90aa67115a400..cc96d4bff76c3 100644
--- a/mlir/lib/TableGen/PassGen.cpp
+++ b/mlir/lib/TableGen/PassGen.cpp
@@ -22,11 +22,6 @@
 using namespace mlir;
 using namespace mlir::tblgen;
 
-static llvm::cl::OptionCategory passGenCat("Options for -gen-pass-decls");
-static llvm::cl::opt<std::string>
-    groupName("name", llvm::cl::desc("The name of this group of passes"),
-              llvm::cl::cat(passGenCat));
-
 /// Extract the list of passes from the TableGen records.
 static std::vector<Pass> getPasses(const llvm::RecordKeeper &recordKeeper) {
   std::vector<Pass> passes;
@@ -139,7 +134,8 @@ static void emitPassDecls(const Pass &pass, raw_ostream &os) {
 
 /// Emit the code for registering each of the given passes with the global
 /// PassRegistry.
-static void emitRegistrations(llvm::ArrayRef<Pass> passes, raw_ostream &os) {
+static void emitRegistrations(llvm::ArrayRef<Pass> passes, raw_ostream &os,
+                              const std::string &groupName) {
   os << "#ifdef GEN_PASS_REGISTRATION\n";
 
   for (const Pass &pass : passes) {
@@ -449,8 +445,9 @@ static void emitOldPassDecl(const Pass &pass, raw_ostream &os) {
   os << "};\n";
 }
 
-static void emitPasses(const llvm::RecordKeeper &recordKeeper,
-                       raw_ostream &os) {
+void mlir::tblgen::emitPasses(const llvm::RecordKeeper &recordKeeper,
+                              raw_ostream &os, const std::string &opIncFilter,
+                              const std::string &groupName) {
   std::vector<Pass> passes = getPasses(recordKeeper);
   os << "/* Autogenerated by mlir-tblgen; don't manually edit */\n";
 
@@ -465,7 +462,7 @@ static void emitPasses(const llvm::RecordKeeper &recordKeeper,
   for (const Pass &pass : passes)
     emitPass(pass, os);
 
-  emitRegistrations(passes, os);
+  emitRegistrations(passes, os, groupName);
 
   // TODO: Drop old pass declarations.
   // Emit the old code until all the passes have switched to the new design.
@@ -476,10 +473,3 @@ static void emitPasses(const llvm::RecordKeeper &recordKeeper,
   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) {
-                   emitPasses(records, os);
-                   return false;
-                 });
diff --git a/mlir/lib/TableGen/RewriterGen.cpp b/mlir/lib/TableGen/RewriterGen.cpp
index e63a065a07084..5c084df053c3e 100644
--- a/mlir/lib/TableGen/RewriterGen.cpp
+++ b/mlir/lib/TableGen/RewriterGen.cpp
@@ -610,7 +610,8 @@ void PatternEmitter::emitOpMatch(DagNode tree, StringRef opName, int depth) {
         ++opArgIdx;
         continue;
       }
-      if (auto *operand = llvm::dyn_cast_if_present<NamedTypeConstraint *>(opArg)) {
+      if (auto *operand =
+              llvm::dyn_cast_if_present<NamedTypeConstraint *>(opArg)) {
         if (argTree.isVariadic()) {
           if (!operand->isVariadic()) {
             auto error = formatv("variadic DAG construct can't match op {0}'s "
@@ -1669,8 +1670,8 @@ void PatternEmitter::createSeparateLocalVarsForOpArgs(
 
   int valueIndex = 0; // An index for uniquing local variable names.
   for (int argIndex = 0, e = resultOp.getNumArgs(); argIndex < e; ++argIndex) {
-    const auto *operand =
-        llvm::dyn_cast_if_present<NamedTypeConstraint *>(resultOp.getArg(argIndex));
+    const auto *operand = llvm::dyn_cast_if_present<NamedTypeConstraint *>(
+        resultOp.getArg(argIndex));
     // We do not need special handling for attributes.
     if (!operand)
       continue;
@@ -1725,7 +1726,8 @@ void PatternEmitter::supplyValuesForOpArgs(
 
     Argument opArg = resultOp.getArg(argIndex);
     // Handle the case of operand first.
-    if (auto *operand = llvm::dyn_cast_if_present<NamedTypeConstraint *>(opArg)) {
+    if (auto *operand =
+            llvm::dyn_cast_if_present<NamedTypeConstraint *>(opArg)) {
       if (!operand->name.empty())
         os << "/*" << operand->name << "=*/";
       os << childNodeNames.lookup(argIndex);
@@ -1921,7 +1923,8 @@ StringRef StaticMatcherHelper::getVerifierName(DagLeaf leaf) {
   return staticVerifierEmitter.getTypeConstraintFn(leaf.getAsConstraint());
 }
 
-static void emitRewriters(const RecordKeeper &recordKeeper, raw_ostream &os) {
+void mlir::tblgen::emitRewriters(const RecordKeeper &recordKeeper,
+                                 raw_ostream &os) {
   emitSourceFileHeader("Rewriters", os, recordKeeper);
 
   const auto &patterns = recordKeeper.getAllDerivedDefinitions("Pattern");
@@ -1968,10 +1971,3 @@ static void emitRewriters(const RecordKeeper &recordKeeper, raw_ostream &os) {
   }
   os << "}\n";
 }
-
-static mlir::GenRegistration
-    genRewriters("gen-rewriters", "Generate pattern rewriters",
-                 [](const RecordKeeper &records, raw_ostream &os) {
-                   emitRewriters(records, os);
-                   return false;
-                 });
diff --git a/mlir/lib/TableGen/SPIRVUtilsGen.cpp b/mlir/lib/TableGen/SPIRVUtilsGen.cpp
index 9aeb14d14eeca..36095adc62d72 100644
--- a/mlir/lib/TableGen/SPIRVUtilsGen.cpp
+++ b/mlir/lib/TableGen/SPIRVUtilsGen.cpp
@@ -195,8 +195,8 @@ static void emitInterfaceDef(const Availability &availability,
      << "}\n";
 }
 
-static bool emitInterfaceDefs(const RecordKeeper &recordKeeper,
-                              raw_ostream &os) {
+bool mlir::tblgen::emitSPIRVInterfaceDefs(
+    const llvm::RecordKeeper &recordKeeper, raw_ostream &os) {
   llvm::emitSourceFileHeader("Availability Interface Definitions", os,
                              recordKeeper);
 
@@ -285,8 +285,8 @@ static void emitInterfaceDecl(const Availability &availability,
   os << "};\n\n";
 }
 
-static bool emitInterfaceDecls(const RecordKeeper &recordKeeper,
-                               raw_ostream &os) {
+bool mlir::tblgen::emitSPIRVInterfaceDecls(
+    const llvm::RecordKeeper &recordKeeper, raw_ostream &os) {
   llvm::emitSourceFileHeader("Availability Interface Declarations", os,
                              recordKeeper);
 
@@ -309,26 +309,6 @@ static bool emitInterfaceDecls(const RecordKeeper &recordKeeper,
   return false;
 }
 
-//===----------------------------------------------------------------------===//
-// Availability Interface Hook Registration
-//===----------------------------------------------------------------------===//
-
-// Registers the operation interface generator to mlir-tblgen.
-static mlir::GenRegistration
-    genInterfaceDecls("gen-avail-interface-decls",
-                      "Generate availability interface declarations",
-                      [](const RecordKeeper &records, raw_ostream &os) {
-                        return emitInterfaceDecls(records, os);
-                      });
-
-// Registers the operation interface generator to mlir-tblgen.
-static mlir::GenRegistration
-    genInterfaceDefs("gen-avail-interface-defs",
-                     "Generate op interface definitions",
-                     [](const RecordKeeper &records, raw_ostream &os) {
-                       return emitInterfaceDefs(records, os);
-                     });
-
 //===----------------------------------------------------------------------===//
 // Enum Availability Query AutoGen
 //===----------------------------------------------------------------------===//
@@ -399,10 +379,9 @@ static void emitAvailabilityQueryForBitEnum(const Record &enumDef,
                   avail.getMergeInstanceType(), avail.getQueryFnName(),
                   enumName);
 
-    os << formatv(
-        "  assert(::llvm::popcount(static_cast<{0}>(value)) <= 1"
-        " && \"cannot have more than one bit set\");\n",
-        underlyingType);
+    os << formatv("  assert(::llvm::popcount(static_cast<{0}>(value)) <= 1"
+                  " && \"cannot have more than one bit set\");\n",
+                  underlyingType);
 
     os << "  switch (value) {\n";
     for (const auto &caseSpecPair : classCasePair.getValue()) {
@@ -419,7 +398,7 @@ static void emitAvailabilityQueryForBitEnum(const Record &enumDef,
   }
 }
 
-static void emitEnumDecl(const Record &enumDef, raw_ostream &os) {
+void emitEnumDecl(const Record &enumDef, raw_ostream &os) {
   EnumAttr enumAttr(enumDef);
   StringRef enumName = enumAttr.getEnumClassName();
   StringRef cppNamespace = enumAttr.getCppNamespace();
@@ -450,7 +429,8 @@ static void emitEnumDecl(const Record &enumDef, raw_ostream &os) {
     os << "} // namespace " << ns << "\n";
 }
 
-static bool emitEnumDecls(const RecordKeeper &recordKeeper, raw_ostream &os) {
+bool mlir::tblgen::emitSPIRVEnumDecls(const RecordKeeper &recordKeeper,
+                                      raw_ostream &os) {
   llvm::emitSourceFileHeader("SPIR-V Enum Availability Declarations", os,
                              recordKeeper);
 
@@ -482,7 +462,8 @@ static void emitEnumDef(const Record &enumDef, raw_ostream &os) {
   os << "\n";
 }
 
-static bool emitEnumDefs(const RecordKeeper &recordKeeper, raw_ostream &os) {
+bool mlir::tblgen::emitSPIRVEnumDefs(const RecordKeeper &recordKeeper,
+                                     raw_ostream &os) {
   llvm::emitSourceFileHeader("SPIR-V Enum Availability Definitions", os,
                              recordKeeper);
 
@@ -493,26 +474,6 @@ static bool emitEnumDefs(const RecordKeeper &recordKeeper, raw_ostream &os) {
   return false;
 }
 
-//===----------------------------------------------------------------------===//
-// Enum Availability Query Hook Registration
-//===----------------------------------------------------------------------===//
-
-// Registers the enum utility generator to mlir-tblgen.
-static mlir::GenRegistration
-    genEnumDecls("gen-spirv-enum-avail-decls",
-                 "Generate SPIR-V enum availability declarations",
-                 [](const RecordKeeper &records, raw_ostream &os) {
-                   return emitEnumDecls(records, os);
-                 });
-
-// Registers the enum utility generator to mlir-tblgen.
-static mlir::GenRegistration
-    genEnumDefs("gen-spirv-enum-avail-defs",
-                "Generate SPIR-V enum availability definitions",
-                [](const RecordKeeper &records, raw_ostream &os) {
-                  return emitEnumDefs(records, os);
-                });
-
 //===----------------------------------------------------------------------===//
 // Serialization AutoGen
 //===----------------------------------------------------------------------===//
@@ -936,7 +897,8 @@ static void emitOperandDeserialization(const Operator &op, ArrayRef<SMLoc> loc,
   // Process operands/attributes
   for (unsigned i = 0, e = op.getNumArgs(); i < e; ++i) {
     auto argument = op.getArg(i);
-    if (auto *valueArg = llvm::dyn_cast_if_present<NamedTypeConstraint *>(argument)) {
+    if (auto *valueArg =
+            llvm::dyn_cast_if_present<NamedTypeConstraint *>(argument)) {
       if (valueArg->isVariableLength()) {
         if (i != e - 1) {
           PrintFatalError(
@@ -1180,8 +1142,8 @@ emitExtendedSetDeserializationDispatch(const RecordKeeper &recordKeeper,
 
 /// Emits all the autogenerated serialization/deserializations functions for the
 /// SPIRV_Ops.
-static bool emitSerializationFns(const RecordKeeper &recordKeeper,
-                                 raw_ostream &os) {
+bool mlir::tblgen::emitSPIRVSerializationFns(const RecordKeeper &recordKeeper,
+                                             raw_ostream &os) {
   llvm::emitSourceFileHeader("SPIR-V Serialization Utilities/Functions", os,
                              recordKeeper);
 
@@ -1229,17 +1191,6 @@ static bool emitSerializationFns(const RecordKeeper &recordKeeper,
   return false;
 }
 
-//===----------------------------------------------------------------------===//
-// Serialization Hook Registration
-//===----------------------------------------------------------------------===//
-
-static mlir::GenRegistration genSerialization(
-    "gen-spirv-serialization",
-    "Generate SPIR-V (de)serialization utilities and functions",
-    [](const RecordKeeper &records, raw_ostream &os) {
-      return emitSerializationFns(records, os);
-    });
-
 //===----------------------------------------------------------------------===//
 // Op Utils AutoGen
 //===----------------------------------------------------------------------===//
@@ -1261,7 +1212,8 @@ static void emitEnumGetAttrNameFnDefn(const EnumAttr &enumAttr,
   os << "}\n";
 }
 
-static bool emitAttrUtils(const RecordKeeper &recordKeeper, raw_ostream &os) {
+bool mlir::tblgen::emitSPIRVAttrUtils(const RecordKeeper &recordKeeper,
+                                      raw_ostream &os) {
   llvm::emitSourceFileHeader("SPIR-V Attribute Utilities", os, recordKeeper);
 
   auto defs = recordKeeper.getAllDerivedDefinitions("EnumAttrInfo");
@@ -1276,22 +1228,11 @@ static bool emitAttrUtils(const RecordKeeper &recordKeeper, raw_ostream &os) {
   return false;
 }
 
-//===----------------------------------------------------------------------===//
-// Op Utils Hook Registration
-//===----------------------------------------------------------------------===//
-
-static mlir::GenRegistration
-    genOpUtils("gen-spirv-attr-utils",
-               "Generate SPIR-V attribute utility definitions",
-               [](const RecordKeeper &records, raw_ostream &os) {
-                 return emitAttrUtils(records, os);
-               });
-
 //===----------------------------------------------------------------------===//
 // SPIR-V Availability Impl AutoGen
 //===----------------------------------------------------------------------===//
 
-static void emitAvailabilityImpl(const Operator &srcOp, raw_ostream &os) {
+void emitAvailabilityImpl(const Operator &srcOp, raw_ostream &os) {
   mlir::tblgen::FmtContext fctx;
   fctx.addSubst("overall", "tblgen_overall");
 
@@ -1405,8 +1346,8 @@ static void emitAvailabilityImpl(const Operator &srcOp, raw_ostream &os) {
   }
 }
 
-static bool emitAvailabilityImpl(const RecordKeeper &recordKeeper,
-                                 raw_ostream &os) {
+bool mlir::tblgen::emitSPIRVAvailabilityImpl(const RecordKeeper &recordKeeper,
+                                             raw_ostream &os) {
   llvm::emitSourceFileHeader("SPIR-V Op Availability Implementations", os,
                              recordKeeper);
 
@@ -1419,23 +1360,12 @@ static bool emitAvailabilityImpl(const RecordKeeper &recordKeeper,
   return false;
 }
 
-//===----------------------------------------------------------------------===//
-// Op Availability Implementation Hook Registration
-//===----------------------------------------------------------------------===//
-
-static mlir::GenRegistration
-    genOpAvailabilityImpl("gen-spirv-avail-impls",
-                          "Generate SPIR-V operation utility definitions",
-                          [](const RecordKeeper &records, raw_ostream &os) {
-                            return emitAvailabilityImpl(records, os);
-                          });
-
 //===----------------------------------------------------------------------===//
 // SPIR-V Capability Implication AutoGen
 //===----------------------------------------------------------------------===//
 
-static bool emitCapabilityImplication(const RecordKeeper &recordKeeper,
-                                      raw_ostream &os) {
+bool mlir::tblgen::emitSPIRVCapabilityImplication(
+    const RecordKeeper &recordKeeper, raw_ostream &os) {
   llvm::emitSourceFileHeader("SPIR-V Capability Implication", os, recordKeeper);
 
   EnumAttr enumAttr(
@@ -1465,15 +1395,3 @@ static bool emitCapabilityImplication(const RecordKeeper &recordKeeper,
 
   return false;
 }
-
-//===----------------------------------------------------------------------===//
-// SPIR-V Capability Implication Hook Registration
-//===----------------------------------------------------------------------===//
-
-static mlir::GenRegistration
-    genCapabilityImplication("gen-spirv-capability-implication",
-                             "Generate utility function to return implied "
-                             "capabilities for a given capability",
-                             [](const RecordKeeper &records, raw_ostream &os) {
-                               return emitCapabilityImplication(records, os);
-                             });
diff --git a/mlir/tools/mlir-tblgen/mlir-tblgen.cpp b/mlir/tools/mlir-tblgen/mlir-tblgen.cpp
index 6c4b619598eb6..2e9082278abd5 100644
--- a/mlir/tools/mlir-tblgen/mlir-tblgen.cpp
+++ b/mlir/tools/mlir-tblgen/mlir-tblgen.cpp
@@ -10,12 +10,457 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "mlir/TableGen/AttrOrTypeDef.h"
 #include "mlir/TableGen/GenInfo.h"
+#include "mlir/TableGen/OpInterfacesGen.h"
 #include "mlir/Tools/mlir-tblgen/MlirTblgenMain.h"
+#include "llvm/Support/CommandLine.h"
+#include "llvm/TableGen/Error.h"
 #include "llvm/TableGen/Record.h"
 
 using namespace llvm;
 using namespace mlir;
+using namespace mlir::tblgen;
+
+//===----------------------------------------------------------------------===//
+// Commandline Options
+//===----------------------------------------------------------------------===//
+
+static llvm::cl::opt<bool> formatErrorIsFatal(
+    "asmformat-error-is-fatal",
+    llvm::cl::desc("Emit a fatal error if format parsing fails"),
+    llvm::cl::init(true));
+
+cl::OptionCategory opDefGenCat("Options for op definition generators");
+
+static cl::opt<std::string> opIncFilter(
+    "op-include-regex",
+    cl::desc("Regex of name of op's to include (no filter if empty)"),
+    cl::cat(opDefGenCat));
+static cl::opt<std::string> opExcFilter(
+    "op-exclude-regex",
+    cl::desc("Regex of name of op's to exclude (no filter if empty)"),
+    cl::cat(opDefGenCat));
+static cl::opt<unsigned> opShardCount(
+    "op-shard-count",
+    cl::desc("The number of shards into which the op classes will be divided"),
+    cl::cat(opDefGenCat), cl::init(1));
+
+//===----------------------------------------------------------------------===//
+// GEN: Registration hooks
+//===----------------------------------------------------------------------===//
+
+//===----------------------------------------------------------------------===//
+// AttrDef
+//===----------------------------------------------------------------------===//
+
+static llvm::cl::OptionCategory attrdefGenCat("Options for -gen-attrdef-*");
+static llvm::cl::opt<std::string>
+    attrDialect("attrdefs-dialect",
+                llvm::cl::desc("Generate attributes for this dialect"),
+                llvm::cl::cat(attrdefGenCat), llvm::cl::CommaSeparated);
+
+static mlir::GenRegistration
+    genAttrDefs("gen-attrdef-defs", "Generate AttrDef definitions",
+                [](const llvm::RecordKeeper &records, raw_ostream &os) {
+                  AttrDefGenerator generator(records, os, formatErrorIsFatal);
+                  return generator.emitDefs(attrDialect);
+                });
+static mlir::GenRegistration
+    genAttrDecls("gen-attrdef-decls", "Generate AttrDef declarations",
+                 [](const llvm::RecordKeeper &records, raw_ostream &os) {
+                   AttrDefGenerator generator(records, os, formatErrorIsFatal);
+                   return generator.emitDecls(attrDialect);
+                 });
+
+//===----------------------------------------------------------------------===//
+// TypeDef
+//===----------------------------------------------------------------------===//
+
+static llvm::cl::OptionCategory typedefGenCat("Options for -gen-typedef-*");
+static llvm::cl::opt<std::string>
+    typeDialect("typedefs-dialect",
+                llvm::cl::desc("Generate types for this dialect"),
+                llvm::cl::cat(typedefGenCat), llvm::cl::CommaSeparated);
+
+static mlir::GenRegistration
+    genTypeDefs("gen-typedef-defs", "Generate TypeDef definitions",
+                [](const llvm::RecordKeeper &records, raw_ostream &os) {
+                  TypeDefGenerator generator(records, os, formatErrorIsFatal);
+                  return generator.emitDefs(typeDialect);
+                });
+static mlir::GenRegistration
+    genTypeDecls("gen-typedef-decls", "Generate TypeDef declarations",
+                 [](const llvm::RecordKeeper &records, raw_ostream &os) {
+                   TypeDefGenerator generator(records, os, formatErrorIsFatal);
+                   return generator.emitDecls(typeDialect);
+                 });
+static mlir::GenRegistration
+    genOpDecls("gen-op-decls", "Generate op declarations",
+               [](const RecordKeeper &records, raw_ostream &os) {
+                 return emitOpDecls(records, os, opIncFilter, opExcFilter,
+                                    opShardCount, formatErrorIsFatal);
+               });
+
+//===----------------------------------------------------------------------===//
+// OpDef
+//===----------------------------------------------------------------------===//
+
+static mlir::GenRegistration
+    genOpDefs("gen-op-defs", "Generate op definitions",
+              [](const RecordKeeper &records, raw_ostream &os) {
+                return emitOpDefs(records, os, opIncFilter, opExcFilter,
+                                  opShardCount, formatErrorIsFatal);
+              });
+
+//===----------------------------------------------------------------------===//
+// Bytecode
+//===----------------------------------------------------------------------===//
+
+static llvm::cl::OptionCategory
+    byteCodeDialectGenCat("Options for -gen-bytecode");
+static llvm::cl::opt<std::string> selectedBcDialect(
+    "bytecode-dialect", llvm::cl::desc("The dialect to gen for"),
+    llvm::cl::cat(byteCodeDialectGenCat), llvm::cl::CommaSeparated);
+
+static mlir::GenRegistration
+    genBCRW("gen-bytecode", "Generate dialect bytecode readers/writers",
+            [](const RecordKeeper &records, raw_ostream &os) {
+              return emitBCRW(records, os, selectedBcDialect);
+            });
+
+//===----------------------------------------------------------------------===//
+// Dialect
+//===----------------------------------------------------------------------===//
+
+static llvm::cl::OptionCategory dialectGenCat("Options for -gen-dialect-*");
+llvm::cl::opt<std::string>
+    selectedDialect("dialect", llvm::cl::desc("The dialect to gen for"),
+                    llvm::cl::cat(dialectGenCat), llvm::cl::CommaSeparated);
+
+static llvm::cl::OptionCategory
+    directiveGenCat("Options for gen-directive-decl");
+
+static llvm::cl::opt<std::string>
+    dialect("directives-dialect",
+            llvm::cl::desc("Generate directives for this dialect"),
+            llvm::cl::cat(directiveGenCat), llvm::cl::CommaSeparated);
+
+static mlir::GenRegistration
+    genDialectDecls("gen-dialect-decls", "Generate dialect declarations",
+                    [](const llvm::RecordKeeper &records, raw_ostream &os) {
+                      return emitDialectDecls(records, os, selectedDialect);
+                    });
+
+static mlir::GenRegistration
+    genDialectDefs("gen-dialect-defs", "Generate dialect definitions",
+                   [](const llvm::RecordKeeper &records, raw_ostream &os) {
+                     return emitDialectDefs(records, os, selectedDialect);
+                   });
+
+// Registers the generator to mlir-tblgen.
+static mlir::GenRegistration genDirectiveDecls(
+    "gen-directive-decl",
+    "Generate declarations for directives (OpenMP/OpenACC etc.)",
+    [](const RecordKeeper &records, raw_ostream &os) {
+      return emitDirectiveDecls(records, dialect, os);
+    });
+
+//===----------------------------------------------------------------------===//
+// Python
+//===----------------------------------------------------------------------===//
+
+static llvm::cl::OptionCategory
+    clOpPythonBindingCat("Options for -gen-python-op-bindings");
+
+static llvm::cl::opt<std::string>
+    clDialectName("bind-dialect",
+                  llvm::cl::desc("The dialect to run the generator for"),
+                  llvm::cl::init(""), llvm::cl::cat(clOpPythonBindingCat));
+
+static llvm::cl::opt<std::string> clDialectExtensionName(
+    "dialect-extension", llvm::cl::desc("The prefix of the dialect extension"),
+    llvm::cl::init(""), llvm::cl::cat(clOpPythonBindingCat));
+
+// Registers the enum utility generator to mlir-tblgen.
+static mlir::GenRegistration
+    genPythonEnumBindings("gen-python-enum-bindings",
+                          "Generate Python bindings for enum attributes",
+                          &emitPythonEnums);
+
+static GenRegistration
+    genPythonBindings("gen-python-op-bindings",
+                      "Generate Python bindings for MLIR Ops",
+                      [](const RecordKeeper &records, raw_ostream &os) {
+                        if (clDialectName.empty())
+                          llvm::PrintFatalError("dialect name not provided");
+                        return emitAllPythonOps(records, os, clDialectName,
+                                                clDialectExtensionName);
+                      });
+
+//===----------------------------------------------------------------------===//
+// Enums
+//===----------------------------------------------------------------------===//
+
+// Registers the enum utility generator to mlir-tblgen.
+static mlir::GenRegistration
+    genEnumDecls("gen-enum-decls", "Generate enum utility declarations",
+                 [](const RecordKeeper &records, raw_ostream &os) {
+                   return emitEnumDecls(records, os);
+                 });
+
+// Registers the enum utility generator to mlir-tblgen.
+static mlir::GenRegistration
+    genEnumDefs("gen-enum-defs", "Generate enum utility definitions",
+                [](const RecordKeeper &records, raw_ostream &os) {
+                  return emitEnumDefs(records, os);
+                });
+
+//===----------------------------------------------------------------------===//
+// LLVMIRConversion
+//===----------------------------------------------------------------------===//
+
+static llvm::cl::OptionCategory intrinsicGenCat("Intrinsics Generator Options");
+
+static llvm::cl::opt<std::string>
+    nameFilter("llvmir-intrinsics-filter",
+               llvm::cl::desc("Only keep the intrinsics with the specified "
+                              "substring in their record name"),
+               llvm::cl::cat(intrinsicGenCat));
+
+static llvm::cl::opt<std::string>
+    opBaseClass("dialect-opclass-base",
+                llvm::cl::desc("The base class for the ops in the dialect we "
+                               "are planning to emit"),
+                llvm::cl::init("LLVM_IntrOp"), llvm::cl::cat(intrinsicGenCat));
+
+static llvm::cl::opt<std::string> accessGroupRegexp(
+    "llvmir-intrinsics-access-group-regexp",
+    llvm::cl::desc("Mark intrinsics that match the specified "
+                   "regexp as taking an access group metadata"),
+    llvm::cl::cat(intrinsicGenCat));
+
+static llvm::cl::opt<std::string> aliasAnalysisRegexp(
+    "llvmir-intrinsics-alias-analysis-regexp",
+    llvm::cl::desc("Mark intrinsics that match the specified "
+                   "regexp as taking alias.scopes, noalias, and tbaa metadata"),
+    llvm::cl::cat(intrinsicGenCat));
+
+static mlir::GenRegistration
+    genLLVMIRConversions("gen-llvmir-conversions",
+                         "Generate LLVM IR conversions", emitLLVMBuilders);
+
+static mlir::GenRegistration genOpFromLLVMIRConversions(
+    "gen-op-from-llvmir-conversions",
+    "Generate conversions of operations from LLVM IR", emitLLVMOpMLIRBuilders);
+
+static mlir::GenRegistration genIntrFromLLVMIRConversions(
+    "gen-intr-from-llvmir-conversions",
+    "Generate conversions of intrinsics from LLVM IR",
+    emitLLVMIntrMLIRBuilders);
+
+static mlir::GenRegistration
+    genEnumToLLVMConversion("gen-enum-to-llvmir-conversions",
+                            "Generate conversions of EnumAttrs to LLVM IR",
+                            emitLLVMEnumConversionDefs</*ConvertTo=*/true>);
+
+static mlir::GenRegistration
+    genEnumFromLLVMConversion("gen-enum-from-llvmir-conversions",
+                              "Generate conversions of EnumAttrs from LLVM IR",
+                              emitLLVMEnumConversionDefs</*ConvertTo=*/false>);
+
+static mlir::GenRegistration genConvertibleLLVMIRIntrinsics(
+    "gen-convertible-llvmir-intrinsics",
+    "Generate list of convertible LLVM IR intrinsics",
+    emitLLVMConvertibleIntrinsics);
+
+static mlir::GenRegistration genLLVMIRIntrinsics(
+    "gen-llvmir-intrinsics", "Generate LLVM IR intrinsics",
+    [](const RecordKeeper &records, raw_ostream &os) {
+      return emitLLVMIntrinsics(records, os, nameFilter, accessGroupRegexp,
+                                aliasAnalysisRegexp, opBaseClass);
+    });
+
+//===----------------------------------------------------------------------===//
+// Docs
+//===----------------------------------------------------------------------===//
+
+static llvm::cl::OptionCategory
+    docCat("Options for -gen-(attrdef|typedef|op|dialect)-doc");
+llvm::cl::opt<std::string>
+    stripPrefix("strip-prefix",
+                llvm::cl::desc("Strip prefix of the fully qualified names"),
+                llvm::cl::init("::mlir::"), llvm::cl::cat(docCat));
+llvm::cl::opt<bool> allowHugoSpecificFeatures(
+    "allow-hugo-specific-features",
+    llvm::cl::desc("Allows using features specific to Hugo"),
+    llvm::cl::init(false), llvm::cl::cat(docCat));
+
+static mlir::GenRegistration
+    genAttrRegister("gen-attrdef-doc",
+                    "Generate dialect attribute documentation",
+                    [](const RecordKeeper &records, raw_ostream &os) {
+                      emitAttrOrTypeDefDoc(records, os, "AttrDef");
+                      return false;
+                    });
+
+static mlir::GenRegistration
+    genOpRegister("gen-op-doc", "Generate dialect documentation",
+                  [](const RecordKeeper &records, raw_ostream &os) {
+                    emitOpDoc(records, os, stripPrefix,
+                              allowHugoSpecificFeatures, opIncFilter,
+                              opExcFilter);
+                    return false;
+                  });
+
+static mlir::GenRegistration
+    genTypeRegister("gen-typedef-doc", "Generate dialect type documentation",
+                    [](const RecordKeeper &records, raw_ostream &os) {
+                      emitAttrOrTypeDefDoc(records, os, "TypeDef");
+                      return false;
+                    });
+
+static mlir::GenRegistration
+    genDialectDocRegister("gen-dialect-doc", "Generate dialect documentation",
+                          [](const RecordKeeper &records, raw_ostream &os) {
+                            return emitDialectDoc(records, os, selectedDialect,
+                                                  opIncFilter, opExcFilter,
+                                                  stripPrefix,
+                                                  allowHugoSpecificFeatures);
+                          });
+
+static mlir::GenRegistration
+    genPassDocRegister("gen-pass-doc", "Generate pass documentation",
+                       [](const llvm::RecordKeeper &records, raw_ostream &os) {
+                         emitDocs(records, os);
+                         return false;
+                       });
+//===----------------------------------------------------------------------===//
+// Interfaces
+//===----------------------------------------------------------------------===//
+
+static mlir::tblgen::InterfaceGenRegistration<
+    mlir::tblgen::AttrInterfaceGenerator>
+    attrGen("attr", "attribute");
+static InterfaceGenRegistration<OpInterfaceGenerator> opGen("op", "op");
+static InterfaceGenRegistration<TypeInterfaceGenerator> typeGen("type", "type");
+
+//===----------------------------------------------------------------------===//
+// CAPI
+//===----------------------------------------------------------------------===//
+
+static llvm::cl::OptionCategory
+    passGenCat("Options for -gen-pass-capi-header and -gen-pass-capi-impl");
+static llvm::cl::opt<std::string> groupPrefix(
+    "prefix",
+    llvm::cl::desc("The prefix to use for this group of passes. The "
+                   "form will be mlirCreate<prefix><passname>, the "
+                   "prefix can avoid conflicts across libraries."),
+    llvm::cl::cat(passGenCat));
+static mlir::GenRegistration
+    genCAPIHeader("gen-pass-capi-header", "Generate pass C API header",
+                  [](const RecordKeeper &records, raw_ostream &os) {
+                    return emitCAPIHeader(records, os, groupPrefix.getValue());
+                  });
+
+static mlir::GenRegistration
+    genCAPIImpl("gen-pass-capi-impl", "Generate pass C API implementation",
+                [](const RecordKeeper &records, raw_ostream &os) {
+                  return emitCAPIImpl(records, os, groupPrefix.getValue());
+                });
+
+//===----------------------------------------------------------------------===//
+// Pass
+//===----------------------------------------------------------------------===//
+
+static llvm::cl::opt<std::string>
+    groupName("name", llvm::cl::desc("The name of this group of passes"),
+              llvm::cl::cat(passGenCat));
+static mlir::GenRegistration
+    genPassDecls("gen-pass-decls", "Generate pass declarations",
+                 [](const llvm::RecordKeeper &records, raw_ostream &os) {
+                   emitPasses(records, os, opIncFilter, groupName);
+                   return false;
+                 });
+
+//===----------------------------------------------------------------------===//
+// Rewriter
+//===----------------------------------------------------------------------===//
+
+static mlir::GenRegistration
+    genRewriters("gen-rewriters", "Generate pattern rewriters",
+                 [](const RecordKeeper &records, raw_ostream &os) {
+                   emitRewriters(records, os);
+                   return false;
+                 });
+
+// Registers the operation interface generator to mlir-tblgen.
+static mlir::GenRegistration
+    genInterfaceDecls("gen-avail-interface-decls",
+                      "Generate availability interface declarations",
+                      [](const RecordKeeper &records, raw_ostream &os) {
+                        return emitSPIRVInterfaceDecls(records, os);
+                      });
+
+// Registers the operation interface generator to mlir-tblgen.
+static mlir::GenRegistration
+    genInterfaceDefs("gen-avail-interface-defs",
+                     "Generate op interface definitions",
+                     [](const RecordKeeper &records, raw_ostream &os) {
+                       return emitSPIRVInterfaceDefs(records, os);
+                     });
+
+//===----------------------------------------------------------------------===//
+// SPIRV
+//===----------------------------------------------------------------------===//
+
+// Registers the enum utility generator to mlir-tblgen.
+static mlir::GenRegistration
+    genSPIRVEnumDecls("gen-spirv-enum-avail-decls",
+                      "Generate SPIR-V enum availability declarations",
+                      [](const RecordKeeper &records, raw_ostream &os) {
+                        return emitSPIRVEnumDecls(records, os);
+                      });
+
+// Registers the enum utility generator to mlir-tblgen.
+static mlir::GenRegistration
+    genSPIRVEnumDefs("gen-spirv-enum-avail-defs",
+                     "Generate SPIR-V enum availability definitions",
+                     [](const RecordKeeper &records, raw_ostream &os) {
+                       return emitSPIRVEnumDefs(records, os);
+                     });
+
+static mlir::GenRegistration genSPIRVCapabilityImplication(
+    "gen-spirv-capability-implication",
+    "Generate utility function to return implied "
+    "capabilities for a given capability",
+    [](const RecordKeeper &records, raw_ostream &os) {
+      return emitSPIRVCapabilityImplication(records, os);
+    });
+
+static mlir::GenRegistration genSPIRVSerialization(
+    "gen-spirv-serialization",
+    "Generate SPIR-V (de)serialization utilities and functions",
+    [](const RecordKeeper &records, raw_ostream &os) {
+      return emitSPIRVSerializationFns(records, os);
+    });
+
+static mlir::GenRegistration
+    genSPIRVOpUtils("gen-spirv-attr-utils",
+                    "Generate SPIR-V attribute utility definitions",
+                    [](const RecordKeeper &records, raw_ostream &os) {
+                      return emitSPIRVAttrUtils(records, os);
+                    });
+
+static mlir::GenRegistration genSPIRVOpAvailabilityImpl(
+    "gen-spirv-avail-impls", "Generate SPIR-V operation utility definitions",
+    [](const RecordKeeper &records, raw_ostream &os) {
+      return emitSPIRVAvailabilityImpl(records, os);
+    });
+
+//===----------------------------------------------------------------------===//
+//
+//===----------------------------------------------------------------------===//
 
 // Generator that prints records.
 GenRegistration printRecords("print-records", "Print all records to stdout",

>From 9e507c8a56dfb3fff143fba27a1090ce42d39aa4 Mon Sep 17 00:00:00 2001
From: max <maksim.levental at gmail.com>
Date: Sun, 19 May 2024 23:29:31 -0500
Subject: [PATCH 3/3] factor header

---
 .../WebAssembly/WebAssemblyAsmPrinter.cpp     |  2 +-
 mlir/include/mlir/TableGen/AttrOrTypeDef.h    |  1 +
 mlir/include/mlir/TableGen/ByteCodeGen.h      | 24 ++++++
 mlir/include/mlir/TableGen/CAPIGen.h          | 26 ++++++
 .../mlir/TableGen/DialectGenUtilities.h       |  9 ++
 mlir/include/mlir/TableGen/DocGenUtilities.h  | 17 ++++
 mlir/include/mlir/TableGen/EnumGen.h          | 24 ++++++
 mlir/include/mlir/TableGen/FormatGen.h        |  2 +
 mlir/include/mlir/TableGen/GenInfo.h          | 84 +------------------
 mlir/include/mlir/TableGen/LLVMGen.h          | 37 ++++++++
 mlir/include/mlir/TableGen/OpGenHelpers.h     | 10 +++
 mlir/include/mlir/TableGen/OpInterfacesGen.h  |  5 ++
 mlir/include/mlir/TableGen/PassGen.h          | 25 ++++++
 mlir/include/mlir/TableGen/PythonGen.h        | 26 ++++++
 mlir/include/mlir/TableGen/SPIRVGen.h         | 38 +++++++++
 mlir/lib/TableGen/AttrOrTypeDefGen.cpp        |  2 +-
 mlir/lib/TableGen/AttrOrTypeFormatGen.cpp     |  1 +
 mlir/lib/TableGen/BytecodeDialectGen.cpp      |  5 +-
 mlir/lib/TableGen/Constraint.cpp              |  2 +-
 mlir/lib/TableGen/DirectiveCommonGen.cpp      |  2 +-
 mlir/lib/TableGen/EnumPythonBindingGen.cpp    |  6 +-
 mlir/lib/TableGen/EnumsGen.cpp                |  3 +-
 mlir/lib/TableGen/LLVMIRConversionGen.cpp     |  2 +-
 mlir/lib/TableGen/LLVMIRIntrinsicGen.cpp      |  2 +-
 mlir/lib/TableGen/OpDefinitionsGen.cpp        | 10 +--
 mlir/lib/TableGen/OpDocGen.cpp                |  3 +-
 mlir/lib/TableGen/OpFormatGen.cpp             |  1 +
 mlir/lib/TableGen/OpGenHelpers.cpp            |  1 +
 mlir/lib/TableGen/OpInterfacesGen.cpp         |  1 +
 mlir/lib/TableGen/OpPythonBindingGen.cpp      |  4 +-
 mlir/lib/TableGen/Operator.cpp                |  4 +-
 mlir/lib/TableGen/PassCAPIGen.cpp             |  5 +-
 mlir/lib/TableGen/PassDocGen.cpp              |  2 +-
 mlir/lib/TableGen/PassGen.cpp                 |  5 +-
 mlir/lib/TableGen/RewriterGen.cpp             |  4 +-
 mlir/lib/TableGen/SPIRVUtilsGen.cpp           |  4 +-
 mlir/tools/mlir-tblgen/mlir-tblgen.cpp        | 63 +++++++-------
 37 files changed, 315 insertions(+), 147 deletions(-)
 create mode 100644 mlir/include/mlir/TableGen/ByteCodeGen.h
 create mode 100644 mlir/include/mlir/TableGen/CAPIGen.h
 create mode 100644 mlir/include/mlir/TableGen/EnumGen.h
 create mode 100644 mlir/include/mlir/TableGen/LLVMGen.h
 create mode 100644 mlir/include/mlir/TableGen/PassGen.h
 create mode 100644 mlir/include/mlir/TableGen/PythonGen.h
 create mode 100644 mlir/include/mlir/TableGen/SPIRVGen.h

diff --git a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
index 2ba3bb6310df9..3524abba8990a 100644
--- a/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
+++ b/llvm/lib/Target/WebAssembly/WebAssemblyAsmPrinter.cpp
@@ -387,7 +387,7 @@ void WebAssemblyAsmPrinter::emitDecls(const Module &M) {
 void WebAssemblyAsmPrinter::emitEndOfAsmFile(Module &M) {
   // This is required to emit external declarations (like .functypes) when
   // no functions are defined in the compilation unit and therefore,
-  // emitDirectiveDecls() is not called until now.
+  // emitDecls() is not called until now.
   emitDecls(M);
 
   // When a function's address is taken, a TABLE_INDEX relocation is emitted
diff --git a/mlir/include/mlir/TableGen/AttrOrTypeDef.h b/mlir/include/mlir/TableGen/AttrOrTypeDef.h
index 56fd0264774be..cf9193f1e97b2 100644
--- a/mlir/include/mlir/TableGen/AttrOrTypeDef.h
+++ b/mlir/include/mlir/TableGen/AttrOrTypeDef.h
@@ -17,6 +17,7 @@
 #include "mlir/Support/LLVM.h"
 #include "mlir/TableGen/Builder.h"
 #include "mlir/TableGen/Trait.h"
+
 #include "llvm/TableGen/Record.h"
 
 namespace llvm {
diff --git a/mlir/include/mlir/TableGen/ByteCodeGen.h b/mlir/include/mlir/TableGen/ByteCodeGen.h
new file mode 100644
index 0000000000000..a3f7f1929914b
--- /dev/null
+++ b/mlir/include/mlir/TableGen/ByteCodeGen.h
@@ -0,0 +1,24 @@
+//===- ByteCodeGen.h - Generator info ---------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_TABLEGEN_BYTECODEGEN_H_
+#define MLIR_TABLEGEN_BYTECODEGEN_H_
+
+#include "mlir/Support/LLVM.h"
+
+#include "llvm/ADT/StringRef.h"
+#include "llvm/TableGen/Record.h"
+
+namespace mlir::tblgen {
+
+bool emitBCRW(const llvm::RecordKeeper &records, raw_ostream &os,
+              const std::string &selectedBcDialect);
+
+} // namespace mlir::tblgen
+
+#endif // MLIR_TABLEGEN_BYTECODEGEN_H_
diff --git a/mlir/include/mlir/TableGen/CAPIGen.h b/mlir/include/mlir/TableGen/CAPIGen.h
new file mode 100644
index 0000000000000..595d72f116377
--- /dev/null
+++ b/mlir/include/mlir/TableGen/CAPIGen.h
@@ -0,0 +1,26 @@
+//===- CAPIGen.h - Generator info -------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_TABLEGEN_CAPIGEN_H_
+#define MLIR_TABLEGEN_CAPIGEN_H_
+
+#include "mlir/Support/LLVM.h"
+
+#include "llvm/ADT/StringRef.h"
+#include "llvm/TableGen/Record.h"
+
+namespace mlir::tblgen {
+
+bool emitCAPIHeader(const llvm::RecordKeeper &records, raw_ostream &os,
+                    std::string groupPrefix);
+bool emitCAPIImpl(const llvm::RecordKeeper &records, raw_ostream &os,
+                  std::string groupPrefix);
+
+} // namespace mlir::tblgen
+
+#endif // MLIR_TABLEGEN_CAPIGEN_H_
diff --git a/mlir/include/mlir/TableGen/DialectGenUtilities.h b/mlir/include/mlir/TableGen/DialectGenUtilities.h
index d0f7f3f6f8819..483b5ba43efc6 100644
--- a/mlir/include/mlir/TableGen/DialectGenUtilities.h
+++ b/mlir/include/mlir/TableGen/DialectGenUtilities.h
@@ -10,7 +10,9 @@
 #define MLIR_TOOLS_MLIRTBLGEN_DIALECTGENUTILITIES_H_
 
 #include "mlir/Support/LLVM.h"
+
 #include "llvm/Support/CommandLine.h"
+#include "llvm/TableGen/Record.h"
 
 namespace mlir {
 namespace tblgen {
@@ -21,6 +23,13 @@ class Dialect;
 std::optional<Dialect>
 findDialectToGenerate(ArrayRef<Dialect> dialects,
                       const std::string &selectedDialect);
+bool emitDialectDecls(const llvm::RecordKeeper &recordKeeper, raw_ostream &os,
+                      const std::string &selectedDialect);
+bool emitDialectDefs(const llvm::RecordKeeper &recordKeeper, raw_ostream &os,
+                     const std::string &selectedDialect);
+bool emitDirectiveDecls(const llvm::RecordKeeper &recordKeeper,
+                        llvm::StringRef dialect, raw_ostream &os);
+
 } // namespace tblgen
 } // namespace mlir
 
diff --git a/mlir/include/mlir/TableGen/DocGenUtilities.h b/mlir/include/mlir/TableGen/DocGenUtilities.h
index dd1dbbe243911..90296062917bb 100644
--- a/mlir/include/mlir/TableGen/DocGenUtilities.h
+++ b/mlir/include/mlir/TableGen/DocGenUtilities.h
@@ -15,6 +15,7 @@
 #define MLIR_TOOLS_MLIRTBLGEN_DOCGENUTILITIES_H_
 
 #include "llvm/ADT/StringRef.h"
+#include "llvm/TableGen/Record.h"
 
 namespace llvm {
 class raw_ostream;
@@ -39,6 +40,22 @@ void emitDescription(llvm::StringRef description, llvm::raw_ostream &os);
 void emitDescriptionComment(llvm::StringRef description, llvm::raw_ostream &os,
                             llvm::StringRef prefix = "");
 
+void emitAttrOrTypeDefDoc(const llvm::RecordKeeper &recordKeeper,
+                          llvm::raw_ostream &os,
+                          llvm::StringRef recordTypeName);
+
+void emitOpDoc(const llvm::RecordKeeper &recordKeeper, llvm::raw_ostream &os,
+               const std::string &emitOpDoc, bool allowHugoSpecificFeatures,
+               const std::string &opIncFilter, const std::string &opExcFilter);
+
+bool emitDialectDoc(const llvm::RecordKeeper &recordKeeper,
+                    llvm::raw_ostream &os, const std::string &selectedDialect,
+                    const std::string &opIncFilter,
+                    const std::string &opExcFilter,
+                    const std::string &stripPrefix,
+                    bool allowHugoSpecificFeatures);
+void emitDocs(const llvm::RecordKeeper &recordKeeper, llvm::raw_ostream &os);
+
 } // namespace tblgen
 } // namespace mlir
 
diff --git a/mlir/include/mlir/TableGen/EnumGen.h b/mlir/include/mlir/TableGen/EnumGen.h
new file mode 100644
index 0000000000000..824f2b1bb9d1e
--- /dev/null
+++ b/mlir/include/mlir/TableGen/EnumGen.h
@@ -0,0 +1,24 @@
+//===- EnumGen.h - Generator info -------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_TABLEGEN_ENUMGEN_H_
+#define MLIR_TABLEGEN_ENUMGEN_H_
+
+#include "mlir/Support/LLVM.h"
+
+#include "llvm/ADT/StringRef.h"
+#include "llvm/TableGen/Record.h"
+
+namespace mlir::tblgen {
+
+bool emitEnumDecls(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
+bool emitEnumDefs(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
+
+} // namespace mlir::tblgen
+
+#endif // MLIR_TABLEGEN_ENUMGEN_H_
diff --git a/mlir/include/mlir/TableGen/FormatGen.h b/mlir/include/mlir/TableGen/FormatGen.h
index 2a05414e90f29..5fc790ebc24a1 100644
--- a/mlir/include/mlir/TableGen/FormatGen.h
+++ b/mlir/include/mlir/TableGen/FormatGen.h
@@ -16,11 +16,13 @@
 
 #include "mlir/Support/LLVM.h"
 #include "mlir/Support/LogicalResult.h"
+
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/StringSet.h"
 #include "llvm/Support/Allocator.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/SMLoc.h"
+
 #include <vector>
 
 namespace llvm {
diff --git a/mlir/include/mlir/TableGen/GenInfo.h b/mlir/include/mlir/TableGen/GenInfo.h
index 57fc7f911eaef..dff934a195e19 100644
--- a/mlir/include/mlir/TableGen/GenInfo.h
+++ b/mlir/include/mlir/TableGen/GenInfo.h
@@ -10,8 +10,9 @@
 #define MLIR_TABLEGEN_GENINFO_H_
 
 #include "mlir/Support/LLVM.h"
+
 #include "llvm/ADT/StringRef.h"
-#include "llvm/Support/CommandLine.h"
+
 #include <functional>
 #include <utility>
 
@@ -70,87 +71,6 @@ struct GenRegistration {
                   const GenFunction &function);
 };
 
-namespace tblgen {
-bool emitBCRW(const llvm::RecordKeeper &records, raw_ostream &os,
-              const std::string &selectedBcDialect);
-
-bool emitOpDecls(const llvm::RecordKeeper &recordKeeper, raw_ostream &os,
-                 const std::string &opIncFilter, const std::string &opExcFilter,
-                 unsigned opShardCount, bool formatErrorIsFatal);
-bool emitOpDefs(const llvm::RecordKeeper &recordKeeper, raw_ostream &os,
-                const std::string &opIncFilter, const std::string &opExcFilter,
-                unsigned opShardCount, bool formatErrorIsFatal);
-
-bool emitDialectDecls(const llvm::RecordKeeper &recordKeeper, raw_ostream &os,
-                      const std::string &selectedDialect);
-bool emitDialectDefs(const llvm::RecordKeeper &recordKeeper, raw_ostream &os,
-                     const std::string &selectedDialect);
-bool emitDirectiveDecls(const llvm::RecordKeeper &recordKeeper,
-                        llvm::StringRef dialect, raw_ostream &os);
-
-bool emitPythonEnums(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
-bool emitAllPythonOps(const llvm::RecordKeeper &records, raw_ostream &os,
-                      const std::string &clDialectName,
-                      const std::string &clDialectExtensionName);
-
-bool emitEnumDecls(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
-bool emitEnumDefs(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
-
-bool emitLLVMBuilders(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
-bool emitLLVMOpMLIRBuilders(const llvm::RecordKeeper &recordKeeper,
-                            raw_ostream &os);
-bool emitLLVMIntrMLIRBuilders(const llvm::RecordKeeper &recordKeeper,
-                              raw_ostream &os);
-template <bool ConvertTo>
-bool emitLLVMEnumConversionDefs(const llvm::RecordKeeper &recordKeeper,
-                                raw_ostream &os);
-bool emitLLVMConvertibleIntrinsics(const llvm::RecordKeeper &recordKeeper,
-                                   raw_ostream &os);
-bool emitLLVMIntrinsics(const llvm::RecordKeeper &records,
-                        llvm::raw_ostream &os, const std::string &nameFilter,
-                        const std::string &accessGroupRegexp,
-                        const std::string &aliasAnalysisRegexp,
-                        const std::string &opBaseClass);
-
-void emitAttrOrTypeDefDoc(const llvm::RecordKeeper &recordKeeper,
-                          raw_ostream &os, StringRef recordTypeName);
-void emitOpDoc(const llvm::RecordKeeper &recordKeeper, raw_ostream &os,
-               const std::string &emitOpDoc, bool allowHugoSpecificFeatures,
-               const std::string &opIncFilter, const std::string &opExcFilter);
-bool emitDialectDoc(const llvm::RecordKeeper &recordKeeper, raw_ostream &os,
-                    const std::string &selectedDialect,
-                    const std::string &opIncFilter,
-                    const std::string &opExcFilter,
-                    const std::string &stripPrefix,
-                    bool allowHugoSpecificFeatures);
-void emitDocs(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
-
-bool emitCAPIHeader(const llvm::RecordKeeper &records, raw_ostream &os,
-                    std::string groupPrefix);
-bool emitCAPIImpl(const llvm::RecordKeeper &records, raw_ostream &os,
-                  std::string groupPrefix);
-void emitPasses(const llvm::RecordKeeper &recordKeeper, raw_ostream &os,
-                const std::string &opIncFilter, const std::string &groupName);
-void emitRewriters(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
-
-bool emitSPIRVInterfaceDefs(const llvm::RecordKeeper &recordKeeper,
-                            raw_ostream &os);
-bool emitSPIRVInterfaceDecls(const llvm::RecordKeeper &recordKeeper,
-                             raw_ostream &os);
-bool emitSPIRVEnumDecls(const llvm::RecordKeeper &recordKeeper,
-                        raw_ostream &os);
-bool emitSPIRVEnumDefs(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
-bool emitSPIRVCapabilityImplication(const llvm::RecordKeeper &recordKeeper,
-                                    raw_ostream &os);
-bool emitSPIRVSerializationFns(const llvm::RecordKeeper &recordKeeper,
-                               raw_ostream &os);
-bool emitSPIRVAttrUtils(const llvm::RecordKeeper &recordKeeper,
-                        raw_ostream &os);
-bool emitSPIRVAvailabilityImpl(const llvm::RecordKeeper &recordKeeper,
-                               raw_ostream &os);
-
-} // namespace tblgen
-
 } // namespace mlir
 
 #endif // MLIR_TABLEGEN_GENINFO_H_
diff --git a/mlir/include/mlir/TableGen/LLVMGen.h b/mlir/include/mlir/TableGen/LLVMGen.h
new file mode 100644
index 0000000000000..030fbc2862932
--- /dev/null
+++ b/mlir/include/mlir/TableGen/LLVMGen.h
@@ -0,0 +1,37 @@
+//===- LLVMGen.h - Generator info -------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_TABLEGEN_LLVMGEN_H_
+#define MLIR_TABLEGEN_LLVMGEN_H_
+
+#include "mlir/Support/LLVM.h"
+
+#include "llvm/ADT/StringRef.h"
+#include "llvm/TableGen/Record.h"
+
+namespace mlir::tblgen {
+
+bool emitLLVMBuilders(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
+bool emitLLVMOpMLIRBuilders(const llvm::RecordKeeper &recordKeeper,
+                            raw_ostream &os);
+bool emitLLVMIntrMLIRBuilders(const llvm::RecordKeeper &recordKeeper,
+                              raw_ostream &os);
+template <bool ConvertTo>
+bool emitLLVMEnumConversionDefs(const llvm::RecordKeeper &recordKeeper,
+                                raw_ostream &os);
+bool emitLLVMConvertibleIntrinsics(const llvm::RecordKeeper &recordKeeper,
+                                   raw_ostream &os);
+bool emitLLVMIntrinsics(const llvm::RecordKeeper &records,
+                        llvm::raw_ostream &os, const std::string &nameFilter,
+                        const std::string &accessGroupRegexp,
+                        const std::string &aliasAnalysisRegexp,
+                        const std::string &opBaseClass);
+
+} // namespace mlir::tblgen
+
+#endif // MLIR_TABLEGEN_LLVMGEN_H_
diff --git a/mlir/include/mlir/TableGen/OpGenHelpers.h b/mlir/include/mlir/TableGen/OpGenHelpers.h
index 537a42770cfac..da5b8a89c4ed8 100644
--- a/mlir/include/mlir/TableGen/OpGenHelpers.h
+++ b/mlir/include/mlir/TableGen/OpGenHelpers.h
@@ -14,7 +14,9 @@
 #define MLIR_TOOLS_MLIRTBLGEN_OPGENHELPERS_H_
 
 #include "mlir/Support/LLVM.h"
+
 #include "llvm/TableGen/Record.h"
+
 #include <vector>
 
 namespace mlir {
@@ -36,6 +38,14 @@ void shardOpDefinitions(ArrayRef<llvm::Record *> defs,
                         SmallVectorImpl<ArrayRef<llvm::Record *>> &shardedDefs,
                         unsigned shardOpDefinitions);
 
+bool emitOpDecls(const llvm::RecordKeeper &recordKeeper, raw_ostream &os,
+                 const std::string &opIncFilter, const std::string &opExcFilter,
+                 unsigned opShardCount, bool formatErrorIsFatal);
+
+bool emitOpDefs(const llvm::RecordKeeper &recordKeeper, raw_ostream &os,
+                const std::string &opIncFilter, const std::string &opExcFilter,
+                unsigned opShardCount, bool formatErrorIsFatal);
+
 } // namespace tblgen
 } // namespace mlir
 
diff --git a/mlir/include/mlir/TableGen/OpInterfacesGen.h b/mlir/include/mlir/TableGen/OpInterfacesGen.h
index f5d087941f5d6..eb8491ce5e535 100644
--- a/mlir/include/mlir/TableGen/OpInterfacesGen.h
+++ b/mlir/include/mlir/TableGen/OpInterfacesGen.h
@@ -17,7 +17,9 @@
 #include "mlir/TableGen/Format.h"
 #include "mlir/TableGen/GenInfo.h"
 #include "mlir/TableGen/Interfaces.h"
+
 #include "llvm/TableGen/Record.h"
+
 #include <vector>
 
 namespace mlir {
@@ -105,6 +107,7 @@ struct AttrInterfaceGenerator : public InterfaceGenerator {
     extraDeclsFmt.addSubst(substVar, "(*this)");
   }
 };
+
 /// A specialized generator for operation interfaces.
 struct OpInterfaceGenerator : public InterfaceGenerator {
   OpInterfaceGenerator(const llvm::RecordKeeper &records, raw_ostream &os)
@@ -121,6 +124,7 @@ struct OpInterfaceGenerator : public InterfaceGenerator {
     extraDeclsFmt.addSubst(substVar, "(*this)");
   }
 };
+
 /// A specialized generator for type interfaces.
 struct TypeInterfaceGenerator : public InterfaceGenerator {
   TypeInterfaceGenerator(const llvm::RecordKeeper &records, raw_ostream &os)
@@ -136,6 +140,7 @@ struct TypeInterfaceGenerator : public InterfaceGenerator {
     extraDeclsFmt.addSubst(substVar, "(*this)");
   }
 };
+
 } // namespace tblgen
 } // namespace mlir
 
diff --git a/mlir/include/mlir/TableGen/PassGen.h b/mlir/include/mlir/TableGen/PassGen.h
new file mode 100644
index 0000000000000..d57ade7b0c20d
--- /dev/null
+++ b/mlir/include/mlir/TableGen/PassGen.h
@@ -0,0 +1,25 @@
+//===- Passgen.h - Generator info -------------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_TABLEGEN_PASSGEN_H_
+#define MLIR_TABLEGEN_PASSGEN_H_
+
+#include "mlir/Support/LLVM.h"
+
+#include "llvm/ADT/StringRef.h"
+#include "llvm/TableGen/Record.h"
+
+namespace mlir::tblgen {
+
+void emitPasses(const llvm::RecordKeeper &recordKeeper, raw_ostream &os,
+                const std::string &opIncFilter, const std::string &groupName);
+void emitRewriters(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
+
+} // namespace mlir::tblgen
+
+#endif // MLIR_TABLEGEN_PASSGEN_H_
diff --git a/mlir/include/mlir/TableGen/PythonGen.h b/mlir/include/mlir/TableGen/PythonGen.h
new file mode 100644
index 0000000000000..c9001cce323f6
--- /dev/null
+++ b/mlir/include/mlir/TableGen/PythonGen.h
@@ -0,0 +1,26 @@
+//===- PythonGen.h - Generator info -----------------------------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_TABLEGEN_PYTHONGEN_H_
+#define MLIR_TABLEGEN_PYTHONGEN_H_
+
+#include "mlir/Support/LLVM.h"
+
+#include "llvm/ADT/StringRef.h"
+#include "llvm/TableGen/Record.h"
+
+namespace mlir::tblgen {
+
+bool emitPythonEnums(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
+bool emitAllPythonOps(const llvm::RecordKeeper &records, raw_ostream &os,
+                      const std::string &clDialectName,
+                      const std::string &clDialectExtensionName);
+
+} // namespace mlir::tblgen
+
+#endif // MLIR_TABLEGEN_PYTHONGEN_H_
diff --git a/mlir/include/mlir/TableGen/SPIRVGen.h b/mlir/include/mlir/TableGen/SPIRVGen.h
new file mode 100644
index 0000000000000..cec6bde3d9b5f
--- /dev/null
+++ b/mlir/include/mlir/TableGen/SPIRVGen.h
@@ -0,0 +1,38 @@
+//===- SPIRVGen.h - Generator info -------------------------------*- C++
+//-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef MLIR_TABLEGEN_SPIRVGEN_H_
+#define MLIR_TABLEGEN_SPIRVGEN_H_
+
+#include "mlir/Support/LLVM.h"
+
+#include "llvm/ADT/StringRef.h"
+#include "llvm/TableGen/Record.h"
+
+namespace mlir::tblgen {
+
+bool emitSPIRVInterfaceDefs(const llvm::RecordKeeper &recordKeeper,
+                            raw_ostream &os);
+bool emitSPIRVInterfaceDecls(const llvm::RecordKeeper &recordKeeper,
+                             raw_ostream &os);
+bool emitSPIRVEnumDecls(const llvm::RecordKeeper &recordKeeper,
+                        raw_ostream &os);
+bool emitSPIRVEnumDefs(const llvm::RecordKeeper &recordKeeper, raw_ostream &os);
+bool emitSPIRVCapabilityImplication(const llvm::RecordKeeper &recordKeeper,
+                                    raw_ostream &os);
+bool emitSPIRVSerializationFns(const llvm::RecordKeeper &recordKeeper,
+                               raw_ostream &os);
+bool emitSPIRVAttrUtils(const llvm::RecordKeeper &recordKeeper,
+                        raw_ostream &os);
+bool emitSPIRVAvailabilityImpl(const llvm::RecordKeeper &recordKeeper,
+                               raw_ostream &os);
+
+} // namespace mlir::tblgen
+
+#endif // MLIR_TABLEGEN_SPIRVGEN_H_
diff --git a/mlir/lib/TableGen/AttrOrTypeDefGen.cpp b/mlir/lib/TableGen/AttrOrTypeDefGen.cpp
index 82208093a87a2..c533940c09e23 100644
--- a/mlir/lib/TableGen/AttrOrTypeDefGen.cpp
+++ b/mlir/lib/TableGen/AttrOrTypeDefGen.cpp
@@ -12,7 +12,7 @@
 #include "mlir/TableGen/CodeGenHelpers.h"
 #include "mlir/TableGen/Format.h"
 #include "mlir/TableGen/Interfaces.h"
-#include "llvm/ADT/StringSet.h"
+
 #include "llvm/Support/CommandLine.h"
 #include "llvm/TableGen/Error.h"
 #include "llvm/TableGen/TableGenBackend.h"
diff --git a/mlir/lib/TableGen/AttrOrTypeFormatGen.cpp b/mlir/lib/TableGen/AttrOrTypeFormatGen.cpp
index 5fc3194d615ae..ca1b77c2eac0d 100644
--- a/mlir/lib/TableGen/AttrOrTypeFormatGen.cpp
+++ b/mlir/lib/TableGen/AttrOrTypeFormatGen.cpp
@@ -12,6 +12,7 @@
 #include "mlir/TableGen/AttrOrTypeDef.h"
 #include "mlir/TableGen/Format.h"
 #include "mlir/TableGen/FormatGen.h"
+
 #include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringSwitch.h"
diff --git a/mlir/lib/TableGen/BytecodeDialectGen.cpp b/mlir/lib/TableGen/BytecodeDialectGen.cpp
index 115e817ae0ce2..acbdfb11d1c09 100644
--- a/mlir/lib/TableGen/BytecodeDialectGen.cpp
+++ b/mlir/lib/TableGen/BytecodeDialectGen.cpp
@@ -7,13 +7,14 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/Support/IndentedOstream.h"
-#include "mlir/TableGen/GenInfo.h"
+#include "mlir/TableGen/ByteCodeGen.h"
+
 #include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/STLExtras.h"
-#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/TableGen/Error.h"
 #include "llvm/TableGen/Record.h"
+
 #include <regex>
 
 using namespace llvm;
diff --git a/mlir/lib/TableGen/Constraint.cpp b/mlir/lib/TableGen/Constraint.cpp
index bc18420223805..4ccbd0a685e09 100644
--- a/mlir/lib/TableGen/Constraint.cpp
+++ b/mlir/lib/TableGen/Constraint.cpp
@@ -30,7 +30,7 @@ Constraint::Constraint(const llvm::Record *record)
     kind = CK_Region;
   } else if (def->isSubClassOf("SuccessorConstraint")) {
     kind = CK_Successor;
-  } else if (!def->isSubClassOf("Constraint")) {
+  } else if(!def->isSubClassOf("Constraint")) {
     llvm::errs() << "Expected a constraint but got: \n" << *def << "\n";
     llvm::report_fatal_error("Abort");
   }
diff --git a/mlir/lib/TableGen/DirectiveCommonGen.cpp b/mlir/lib/TableGen/DirectiveCommonGen.cpp
index 394427fa08b02..63cff39929ecb 100644
--- a/mlir/lib/TableGen/DirectiveCommonGen.cpp
+++ b/mlir/lib/TableGen/DirectiveCommonGen.cpp
@@ -11,7 +11,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "mlir/TableGen/GenInfo.h"
+#include "mlir/TableGen/DialectGenUtilities.h"
 
 #include "llvm/ADT/Twine.h"
 #include "llvm/Support/CommandLine.h"
diff --git a/mlir/lib/TableGen/EnumPythonBindingGen.cpp b/mlir/lib/TableGen/EnumPythonBindingGen.cpp
index eca60b45423ec..26a76b96cdb0d 100644
--- a/mlir/lib/TableGen/EnumPythonBindingGen.cpp
+++ b/mlir/lib/TableGen/EnumPythonBindingGen.cpp
@@ -10,12 +10,12 @@
 // generate the corresponding Python binding classes.
 //
 //===----------------------------------------------------------------------===//
-#include "mlir/TableGen/OpGenHelpers.h"
-
 #include "mlir/TableGen/AttrOrTypeDef.h"
 #include "mlir/TableGen/Attribute.h"
 #include "mlir/TableGen/Dialect.h"
-#include "mlir/TableGen/GenInfo.h"
+#include "mlir/TableGen/OpGenHelpers.h"
+#include "mlir/TableGen/PythonGen.h"
+
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/TableGen/Record.h"
 
diff --git a/mlir/lib/TableGen/EnumsGen.cpp b/mlir/lib/TableGen/EnumsGen.cpp
index 4eef7c79e704a..2556fcf577fbe 100644
--- a/mlir/lib/TableGen/EnumsGen.cpp
+++ b/mlir/lib/TableGen/EnumsGen.cpp
@@ -11,9 +11,10 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/TableGen/Attribute.h"
+#include "mlir/TableGen/EnumGen.h"
 #include "mlir/TableGen/Format.h"
 #include "mlir/TableGen/FormatGen.h"
-#include "mlir/TableGen/GenInfo.h"
+
 #include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringExtras.h"
diff --git a/mlir/lib/TableGen/LLVMIRConversionGen.cpp b/mlir/lib/TableGen/LLVMIRConversionGen.cpp
index c8b9e3680577d..fe3ff51fc9d29 100644
--- a/mlir/lib/TableGen/LLVMIRConversionGen.cpp
+++ b/mlir/lib/TableGen/LLVMIRConversionGen.cpp
@@ -14,7 +14,7 @@
 #include "mlir/Support/LogicalResult.h"
 #include "mlir/TableGen/Argument.h"
 #include "mlir/TableGen/Attribute.h"
-#include "mlir/TableGen/GenInfo.h"
+#include "mlir/TableGen/LLVMGen.h"
 #include "mlir/TableGen/Operator.h"
 
 #include "llvm/ADT/Sequence.h"
diff --git a/mlir/lib/TableGen/LLVMIRIntrinsicGen.cpp b/mlir/lib/TableGen/LLVMIRIntrinsicGen.cpp
index 849165c462990..97a5c5023d4b2 100644
--- a/mlir/lib/TableGen/LLVMIRIntrinsicGen.cpp
+++ b/mlir/lib/TableGen/LLVMIRIntrinsicGen.cpp
@@ -11,7 +11,7 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "mlir/TableGen/GenInfo.h"
+#include "mlir/TableGen/LLVMGen.h"
 
 #include "llvm/ADT/SmallBitVector.h"
 #include "llvm/CodeGenTypes/MachineValueType.h"
diff --git a/mlir/lib/TableGen/OpDefinitionsGen.cpp b/mlir/lib/TableGen/OpDefinitionsGen.cpp
index 1900304a21b45..2adebb0781496 100644
--- a/mlir/lib/TableGen/OpDefinitionsGen.cpp
+++ b/mlir/lib/TableGen/OpDefinitionsGen.cpp
@@ -25,6 +25,7 @@
 #include "mlir/TableGen/Property.h"
 #include "mlir/TableGen/SideEffects.h"
 #include "mlir/TableGen/Trait.h"
+
 #include "llvm/ADT/BitVector.h"
 #include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/Sequence.h"
@@ -736,9 +737,6 @@ class OpEmitter {
 
   // Helper for emitting op code.
   OpOrAdaptorHelper emitHelper;
-
-  /// Whether a failure in parsing the assembly format should be a fatal error.
-  bool formatErrorIsFatal;
 };
 
 } // namespace
@@ -1031,8 +1029,7 @@ OpEmitter::OpEmitter(const Operator &op,
       opClass(op.getCppClassName(), formatExtraDeclarations(op),
               formatExtraDefinitions(op)),
       staticVerifierEmitter(staticVerifierEmitter),
-      emitHelper(op, /*emitForOp=*/true),
-      formatErrorIsFatal(formatErrorIsFatal) {
+      emitHelper(op, /*emitForOp=*/true) {
   verifyCtx.addSubst("_op", "(*this->getOperation())");
   verifyCtx.addSubst("_ctxt", "this->getOperation()->getContext()");
 
@@ -2492,7 +2489,8 @@ void OpEmitter::genSeparateArgParamBuilder() {
 
       // Avoid emitting "resultTypes.size() >= 0u" which is always true.
       if (!hasVariadicResult || numNonVariadicResults != 0)
-        body << "  " << "assert(resultTypes.size() "
+        body << "  "
+             << "assert(resultTypes.size() "
              << (hasVariadicResult ? ">=" : "==") << " "
              << numNonVariadicResults
              << "u && \"mismatched number of results\");\n";
diff --git a/mlir/lib/TableGen/OpDocGen.cpp b/mlir/lib/TableGen/OpDocGen.cpp
index 46e4582229542..64a68f000c993 100644
--- a/mlir/lib/TableGen/OpDocGen.cpp
+++ b/mlir/lib/TableGen/OpDocGen.cpp
@@ -216,7 +216,8 @@ static void emitOpDoc(const Operator &op, raw_ostream &os,
         // Expandable description.
         // This appears as just the summary, but when clicked shows the full
         // description.
-        os << "<details>" << "<summary>" << it.attr.getSummary() << "</summary>"
+        os << "<details>"
+           << "<summary>" << it.attr.getSummary() << "</summary>"
            << "{{% markdown %}}" << description << "{{% /markdown %}}"
            << "</details>";
       } else {
diff --git a/mlir/lib/TableGen/OpFormatGen.cpp b/mlir/lib/TableGen/OpFormatGen.cpp
index f78b7d722b30f..a80170f64c455 100644
--- a/mlir/lib/TableGen/OpFormatGen.cpp
+++ b/mlir/lib/TableGen/OpFormatGen.cpp
@@ -14,6 +14,7 @@
 #include "mlir/TableGen/OpClass.h"
 #include "mlir/TableGen/Operator.h"
 #include "mlir/TableGen/Trait.h"
+
 #include "llvm/ADT/MapVector.h"
 #include "llvm/ADT/Sequence.h"
 #include "llvm/ADT/SetVector.h"
diff --git a/mlir/lib/TableGen/OpGenHelpers.cpp b/mlir/lib/TableGen/OpGenHelpers.cpp
index 658a78b6960d2..40d5a75f79c6a 100644
--- a/mlir/lib/TableGen/OpGenHelpers.cpp
+++ b/mlir/lib/TableGen/OpGenHelpers.cpp
@@ -11,6 +11,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/TableGen/OpGenHelpers.h"
+
 #include "llvm/ADT/StringSet.h"
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/Support/Regex.h"
diff --git a/mlir/lib/TableGen/OpInterfacesGen.cpp b/mlir/lib/TableGen/OpInterfacesGen.cpp
index a2fbb41064311..cdc3d20943dca 100644
--- a/mlir/lib/TableGen/OpInterfacesGen.cpp
+++ b/mlir/lib/TableGen/OpInterfacesGen.cpp
@@ -15,6 +15,7 @@
 #include "mlir/TableGen/Format.h"
 #include "mlir/TableGen/GenInfo.h"
 #include "mlir/TableGen/Interfaces.h"
+
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/FormatVariadic.h"
diff --git a/mlir/lib/TableGen/OpPythonBindingGen.cpp b/mlir/lib/TableGen/OpPythonBindingGen.cpp
index e30809e6cb572..a88acc2b57c1f 100644
--- a/mlir/lib/TableGen/OpPythonBindingGen.cpp
+++ b/mlir/lib/TableGen/OpPythonBindingGen.cpp
@@ -12,9 +12,9 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/TableGen/OpGenHelpers.h"
-
-#include "mlir/TableGen/GenInfo.h"
 #include "mlir/TableGen/Operator.h"
+#include "mlir/TableGen/PythonGen.h"
+
 #include "llvm/ADT/StringSet.h"
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/TableGen/Error.h"
diff --git a/mlir/lib/TableGen/Operator.cpp b/mlir/lib/TableGen/Operator.cpp
index 41b32b4181243..31a60bf379a2a 100644
--- a/mlir/lib/TableGen/Operator.cpp
+++ b/mlir/lib/TableGen/Operator.cpp
@@ -12,9 +12,9 @@
 
 #include "mlir/TableGen/Operator.h"
 #include "mlir/TableGen/Argument.h"
-#include "mlir/TableGen/Predicate.h"
 #include "mlir/TableGen/Trait.h"
 #include "mlir/TableGen/Type.h"
+
 #include "llvm/ADT/EquivalenceClasses.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/Sequence.h"
@@ -22,11 +22,9 @@
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/TypeSwitch.h"
 #include "llvm/Support/Debug.h"
-#include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/TableGen/Error.h"
 #include "llvm/TableGen/Record.h"
-#include <list>
 
 #define DEBUG_TYPE "mlir-tblgen-operator"
 
diff --git a/mlir/lib/TableGen/PassCAPIGen.cpp b/mlir/lib/TableGen/PassCAPIGen.cpp
index 8f2f41e672289..d0e7c7a2571f8 100644
--- a/mlir/lib/TableGen/PassCAPIGen.cpp
+++ b/mlir/lib/TableGen/PassCAPIGen.cpp
@@ -10,12 +10,11 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "mlir/TableGen/GenInfo.h"
+#include "mlir/TableGen/CAPIGen.h"
 #include "mlir/TableGen/Pass.h"
+
 #include "llvm/ADT/StringExtras.h"
-#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FormatVariadic.h"
-#include "llvm/TableGen/Error.h"
 #include "llvm/TableGen/Record.h"
 
 using namespace mlir;
diff --git a/mlir/lib/TableGen/PassDocGen.cpp b/mlir/lib/TableGen/PassDocGen.cpp
index a1c3981f12c39..cb52c34afa4ae 100644
--- a/mlir/lib/TableGen/PassDocGen.cpp
+++ b/mlir/lib/TableGen/PassDocGen.cpp
@@ -11,8 +11,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/TableGen/DocGenUtilities.h"
-#include "mlir/TableGen/GenInfo.h"
 #include "mlir/TableGen/Pass.h"
+
 #include "llvm/Support/FormatVariadic.h"
 #include "llvm/TableGen/Record.h"
 
diff --git a/mlir/lib/TableGen/PassGen.cpp b/mlir/lib/TableGen/PassGen.cpp
index cc96d4bff76c3..03a3ec88362b1 100644
--- a/mlir/lib/TableGen/PassGen.cpp
+++ b/mlir/lib/TableGen/PassGen.cpp
@@ -11,12 +11,11 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "mlir/TableGen/GenInfo.h"
+#include "mlir/TableGen/PassGen.h"
 #include "mlir/TableGen/Pass.h"
+
 #include "llvm/ADT/StringExtras.h"
-#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/FormatVariadic.h"
-#include "llvm/TableGen/Error.h"
 #include "llvm/TableGen/Record.h"
 
 using namespace mlir;
diff --git a/mlir/lib/TableGen/RewriterGen.cpp b/mlir/lib/TableGen/RewriterGen.cpp
index 5c084df053c3e..3c325cc891b57 100644
--- a/mlir/lib/TableGen/RewriterGen.cpp
+++ b/mlir/lib/TableGen/RewriterGen.cpp
@@ -15,12 +15,12 @@
 #include "mlir/TableGen/Attribute.h"
 #include "mlir/TableGen/CodeGenHelpers.h"
 #include "mlir/TableGen/Format.h"
-#include "mlir/TableGen/GenInfo.h"
 #include "mlir/TableGen/Operator.h"
+#include "mlir/TableGen/PassGen.h"
 #include "mlir/TableGen/Pattern.h"
 #include "mlir/TableGen/Predicate.h"
-#include "mlir/TableGen/Property.h"
 #include "mlir/TableGen/Type.h"
+
 #include "llvm/ADT/FunctionExtras.h"
 #include "llvm/ADT/SetVector.h"
 #include "llvm/ADT/StringExtras.h"
diff --git a/mlir/lib/TableGen/SPIRVUtilsGen.cpp b/mlir/lib/TableGen/SPIRVUtilsGen.cpp
index 36095adc62d72..172467258fc64 100644
--- a/mlir/lib/TableGen/SPIRVUtilsGen.cpp
+++ b/mlir/lib/TableGen/SPIRVUtilsGen.cpp
@@ -14,8 +14,9 @@
 #include "mlir/TableGen/Attribute.h"
 #include "mlir/TableGen/CodeGenHelpers.h"
 #include "mlir/TableGen/Format.h"
-#include "mlir/TableGen/GenInfo.h"
 #include "mlir/TableGen/Operator.h"
+#include "mlir/TableGen/SPIRVGen.h"
+
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/Sequence.h"
 #include "llvm/ADT/SmallVector.h"
@@ -30,7 +31,6 @@
 #include "llvm/TableGen/TableGenBackend.h"
 
 #include <list>
-#include <optional>
 
 using llvm::ArrayRef;
 using llvm::formatv;
diff --git a/mlir/tools/mlir-tblgen/mlir-tblgen.cpp b/mlir/tools/mlir-tblgen/mlir-tblgen.cpp
index 2e9082278abd5..248679cbe1a8c 100644
--- a/mlir/tools/mlir-tblgen/mlir-tblgen.cpp
+++ b/mlir/tools/mlir-tblgen/mlir-tblgen.cpp
@@ -11,9 +11,19 @@
 //===----------------------------------------------------------------------===//
 
 #include "mlir/TableGen/AttrOrTypeDef.h"
-#include "mlir/TableGen/GenInfo.h"
+#include "mlir/TableGen/ByteCodeGen.h"
+#include "mlir/TableGen/CAPIGen.h"
+#include "mlir/TableGen/DialectGenUtilities.h"
+#include "mlir/TableGen/DocGenUtilities.h"
+#include "mlir/TableGen/EnumGen.h"
+#include "mlir/TableGen/LLVMGen.h"
+#include "mlir/TableGen/OpGenHelpers.h"
 #include "mlir/TableGen/OpInterfacesGen.h"
+#include "mlir/TableGen/PassGen.h"
+#include "mlir/TableGen/PythonGen.h"
+#include "mlir/TableGen/SPIRVGen.h"
 #include "mlir/Tools/mlir-tblgen/MlirTblgenMain.h"
+
 #include "llvm/Support/CommandLine.h"
 #include "llvm/TableGen/Error.h"
 #include "llvm/TableGen/Record.h"
@@ -22,44 +32,22 @@ using namespace llvm;
 using namespace mlir;
 using namespace mlir::tblgen;
 
-//===----------------------------------------------------------------------===//
-// Commandline Options
-//===----------------------------------------------------------------------===//
-
-static llvm::cl::opt<bool> formatErrorIsFatal(
-    "asmformat-error-is-fatal",
-    llvm::cl::desc("Emit a fatal error if format parsing fails"),
-    llvm::cl::init(true));
-
-cl::OptionCategory opDefGenCat("Options for op definition generators");
-
-static cl::opt<std::string> opIncFilter(
-    "op-include-regex",
-    cl::desc("Regex of name of op's to include (no filter if empty)"),
-    cl::cat(opDefGenCat));
-static cl::opt<std::string> opExcFilter(
-    "op-exclude-regex",
-    cl::desc("Regex of name of op's to exclude (no filter if empty)"),
-    cl::cat(opDefGenCat));
-static cl::opt<unsigned> opShardCount(
-    "op-shard-count",
-    cl::desc("The number of shards into which the op classes will be divided"),
-    cl::cat(opDefGenCat), cl::init(1));
-
-//===----------------------------------------------------------------------===//
-// GEN: Registration hooks
-//===----------------------------------------------------------------------===//
-
 //===----------------------------------------------------------------------===//
 // AttrDef
 //===----------------------------------------------------------------------===//
 
 static llvm::cl::OptionCategory attrdefGenCat("Options for -gen-attrdef-*");
+
 static llvm::cl::opt<std::string>
     attrDialect("attrdefs-dialect",
                 llvm::cl::desc("Generate attributes for this dialect"),
                 llvm::cl::cat(attrdefGenCat), llvm::cl::CommaSeparated);
 
+static llvm::cl::opt<bool> formatErrorIsFatal(
+    "asmformat-error-is-fatal",
+    llvm::cl::desc("Emit a fatal error if format parsing fails"),
+    llvm::cl::init(true));
+
 static mlir::GenRegistration
     genAttrDefs("gen-attrdef-defs", "Generate AttrDef definitions",
                 [](const llvm::RecordKeeper &records, raw_ostream &os) {
@@ -77,6 +65,21 @@ static mlir::GenRegistration
 // TypeDef
 //===----------------------------------------------------------------------===//
 
+cl::OptionCategory opDefGenCat("Options for op definition generators");
+
+static cl::opt<std::string> opIncFilter(
+    "op-include-regex",
+    cl::desc("Regex of name of op's to include (no filter if empty)"),
+    cl::cat(opDefGenCat));
+static cl::opt<std::string> opExcFilter(
+    "op-exclude-regex",
+    cl::desc("Regex of name of op's to exclude (no filter if empty)"),
+    cl::cat(opDefGenCat));
+static cl::opt<unsigned> opShardCount(
+    "op-shard-count",
+    cl::desc("The number of shards into which the op classes will be divided"),
+    cl::cat(opDefGenCat), cl::init(1));
+
 static llvm::cl::OptionCategory typedefGenCat("Options for -gen-typedef-*");
 static llvm::cl::opt<std::string>
     typeDialect("typedefs-dialect",
@@ -459,7 +462,7 @@ static mlir::GenRegistration genSPIRVOpAvailabilityImpl(
     });
 
 //===----------------------------------------------------------------------===//
-//
+// Generic
 //===----------------------------------------------------------------------===//
 
 // Generator that prints records.



More information about the llvm-commits mailing list