[Mlir-commits] [mlir] [tblgen] Add command line flags for using fallback TypeIDs in generation (PR #125767)

Stef Lindall llvmlistbot at llvm.org
Thu Feb 6 16:45:36 PST 2025


https://github.com/bethebunny updated https://github.com/llvm/llvm-project/pull/125767

>From dfc4c69dd7eb151162f21abeb24a4f1b3e412bee Mon Sep 17 00:00:00 2001
From: Stef <stef at modular.com>
Date: Tue, 4 Feb 2025 21:26:16 +0000
Subject: [PATCH 1/5] [tblgen] Add command line flags for using fallback
 TypeIDs in generation

---
 mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp | 15 +++++++++++++--
 mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp | 15 +++++++++++++--
 2 files changed, 26 insertions(+), 4 deletions(-)

diff --git a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
index 6a39424bd463fd7..5134ccac8671446 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
+++ b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
@@ -4,6 +4,7 @@
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
+//
 //===----------------------------------------------------------------------===//
 
 #include "AttrOrTypeFormatGen.h"
@@ -25,6 +26,16 @@ using namespace mlir::tblgen;
 using llvm::Record;
 using llvm::RecordKeeper;
 
+//===----------------------------------------------------------------------===//
+// Utility structs and functions
+//===----------------------------------------------------------------------===//
+
+static llvm::cl::OptionCategory clAttrOrTypeDefs("Options for -gen-*def-*");
+
+static llvm::cl::opt<bool> clUseFallbackTypeIDs(
+    "gen-attr-or-type-use-fallback-type-ids", llvm::cl::desc("Don't generate static TypeID decls; fall back to string comparison."),
+    llvm::cl::init(false), llvm::cl::cat(clAttrOrTypeDefs));
+
 //===----------------------------------------------------------------------===//
 // Utility Functions
 //===----------------------------------------------------------------------===//
@@ -773,7 +784,7 @@ bool DefGenerator::emitDecls(StringRef selectedDialect) {
   // Emit the TypeID explicit specializations to have a single definition for
   // each of these.
   for (const AttrOrTypeDef &def : defs)
-    if (!def.getDialect().getCppNamespace().empty())
+    if (!clUseFallbackTypeIDs && !def.getDialect().getCppNamespace().empty())
       os << "MLIR_DECLARE_EXPLICIT_TYPE_ID("
          << def.getDialect().getCppNamespace() << "::" << def.getCppClassName()
          << ")\n";
@@ -986,7 +997,7 @@ bool DefGenerator::emitDefs(StringRef selectedDialect) {
       gen.emitDef(os);
     }
     // Emit the TypeID explicit specializations to have a single symbol def.
-    if (!def.getDialect().getCppNamespace().empty())
+    if (!clUseFallbackTypeIDs && !def.getDialect().getCppNamespace().empty())
       os << "MLIR_DEFINE_EXPLICIT_TYPE_ID("
          << def.getDialect().getCppNamespace() << "::" << def.getCppClassName()
          << ")\n";
diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
index a970cbc5cacebe3..e3c87e1e578ebdc 100644
--- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
@@ -31,6 +31,7 @@
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/ADT/StringSet.h"
+#include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/Signals.h"
@@ -233,6 +234,16 @@ static const char *const opCommentHeader = R"(
 // Utility structs and functions
 //===----------------------------------------------------------------------===//
 
+static llvm::cl::OptionCategory clOpDefs("Options for op definitions");
+
+static llvm::cl::opt<bool> clUseFallbackTypeIDs(
+    "gen-op-use-fallback-type-ids", llvm::cl::desc("Don't generate static TypeID decls; fall back to string comparison."),
+    llvm::cl::init(false), llvm::cl::cat(clOpDefs));
+
+//===----------------------------------------------------------------------===//
+// Utility structs and functions
+//===----------------------------------------------------------------------===//
+
 // Replaces all occurrences of `match` in `str` with `substitute`.
 static std::string replaceAllSubstrs(std::string str, const std::string &match,
                                      const std::string &substitute) {
@@ -4625,7 +4636,7 @@ emitOpClasses(const RecordKeeper &records,
         OpEmitter::emitDecl(op, os, staticVerifierEmitter);
       }
       // Emit the TypeID explicit specialization to have a single definition.
-      if (!op.getCppNamespace().empty())
+      if (!clUseFallbackTypeIDs && !op.getCppNamespace().empty())
         os << "MLIR_DECLARE_EXPLICIT_TYPE_ID(" << op.getCppNamespace()
            << "::" << op.getCppClassName() << ")\n\n";
     } else {
@@ -4636,7 +4647,7 @@ emitOpClasses(const RecordKeeper &records,
         OpEmitter::emitDef(op, os, staticVerifierEmitter);
       }
       // Emit the TypeID explicit specialization to have a single definition.
-      if (!op.getCppNamespace().empty())
+      if (!clUseFallbackTypeIDs && !op.getCppNamespace().empty())
         os << "MLIR_DEFINE_EXPLICIT_TYPE_ID(" << op.getCppNamespace()
            << "::" << op.getCppClassName() << ")\n\n";
     }

>From bda3e2429e3ddf81109659ba27f207d5870d2a73 Mon Sep 17 00:00:00 2001
From: Stef <stef at modular.com>
Date: Tue, 4 Feb 2025 21:38:28 +0000
Subject: [PATCH 2/5] update comment blocks

---
 mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp | 2 +-
 mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
index 5134ccac8671446..5ebd0b654744e0e 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
+++ b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
@@ -27,7 +27,7 @@ using llvm::Record;
 using llvm::RecordKeeper;
 
 //===----------------------------------------------------------------------===//
-// Utility structs and functions
+// CL Options
 //===----------------------------------------------------------------------===//
 
 static llvm::cl::OptionCategory clAttrOrTypeDefs("Options for -gen-*def-*");
diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
index e3c87e1e578ebdc..1e37ec2f9f86201 100644
--- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
@@ -231,7 +231,7 @@ static const char *const opCommentHeader = R"(
 )";
 
 //===----------------------------------------------------------------------===//
-// Utility structs and functions
+// CL Options
 //===----------------------------------------------------------------------===//
 
 static llvm::cl::OptionCategory clOpDefs("Options for op definitions");

>From 2e41a67c262ff38fe1d17500c39ae67f43102eb3 Mon Sep 17 00:00:00 2001
From: Stef <stef at modular.com>
Date: Tue, 4 Feb 2025 21:39:40 +0000
Subject: [PATCH 3/5] remove extraneous edit

---
 mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp | 1 -
 1 file changed, 1 deletion(-)

diff --git a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
index 5ebd0b654744e0e..954e1ce3786122b 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
+++ b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
@@ -4,7 +4,6 @@
 // See https://llvm.org/LICENSE.txt for license information.
 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
 //
-//
 //===----------------------------------------------------------------------===//
 
 #include "AttrOrTypeFormatGen.h"

>From 74d7667e0d0136b673cd48c6fef1b2651c7b44a7 Mon Sep 17 00:00:00 2001
From: Stef <stef at modular.com>
Date: Tue, 4 Feb 2025 23:03:13 +0000
Subject: [PATCH 4/5] clang-format

---
 mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp | 4 +++-
 mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp | 4 +++-
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
index 954e1ce3786122b..9109c549dfb67d3 100644
--- a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
+++ b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp
@@ -32,7 +32,9 @@ using llvm::RecordKeeper;
 static llvm::cl::OptionCategory clAttrOrTypeDefs("Options for -gen-*def-*");
 
 static llvm::cl::opt<bool> clUseFallbackTypeIDs(
-    "gen-attr-or-type-use-fallback-type-ids", llvm::cl::desc("Don't generate static TypeID decls; fall back to string comparison."),
+    "gen-attr-or-type-use-fallback-type-ids",
+    llvm::cl::desc(
+        "Don't generate static TypeID decls; fall back to string comparison."),
     llvm::cl::init(false), llvm::cl::cat(clAttrOrTypeDefs));
 
 //===----------------------------------------------------------------------===//
diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
index 1e37ec2f9f86201..79b9a0e2ab3743c 100644
--- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
+++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
@@ -237,7 +237,9 @@ static const char *const opCommentHeader = R"(
 static llvm::cl::OptionCategory clOpDefs("Options for op definitions");
 
 static llvm::cl::opt<bool> clUseFallbackTypeIDs(
-    "gen-op-use-fallback-type-ids", llvm::cl::desc("Don't generate static TypeID decls; fall back to string comparison."),
+    "gen-op-use-fallback-type-ids",
+    llvm::cl::desc(
+        "Don't generate static TypeID decls; fall back to string comparison."),
     llvm::cl::init(false), llvm::cl::cat(clOpDefs));
 
 //===----------------------------------------------------------------------===//

>From d15ff21007899955ffd453d734492ddd217d187f Mon Sep 17 00:00:00 2001
From: Stef <stef at modular.com>
Date: Fri, 7 Feb 2025 00:44:37 +0000
Subject: [PATCH 5/5] add similar flag for DialectGen

---
 mlir/tools/mlir-tblgen/DialectGen.cpp | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/mlir/tools/mlir-tblgen/DialectGen.cpp b/mlir/tools/mlir-tblgen/DialectGen.cpp
index 414cad5e1dcc2e7..c83c785b2c837cc 100644
--- a/mlir/tools/mlir-tblgen/DialectGen.cpp
+++ b/mlir/tools/mlir-tblgen/DialectGen.cpp
@@ -38,6 +38,12 @@ 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::opt<bool> clUseFallbackTypeIDs(
+    "gen-dialect-use-fallback-type-ids",
+    llvm::cl::desc(
+        "Don't generate static TypeID decls; fall back to string comparison."),
+    llvm::cl::init(false), llvm::cl::cat(dialectGenCat));
+
 /// Utility iterator used for filtering records for a specific dialect.
 namespace {
 using DialectFilterIterator =
@@ -293,7 +299,7 @@ static void emitDialectDecl(Dialect &dialect, raw_ostream &os) {
     // End the dialect decl.
     os << "};\n";
   }
-  if (!dialect.getCppNamespace().empty())
+  if (!clUseFallbackTypeIDs && !dialect.getCppNamespace().empty())
     os << "MLIR_DECLARE_EXPLICIT_TYPE_ID(" << dialect.getCppNamespace()
        << "::" << dialect.getCppClassName() << ")\n";
 }
@@ -347,7 +353,7 @@ static void emitDialectDef(Dialect &dialect, const RecordKeeper &records,
   std::string cppClassName = dialect.getCppClassName();
 
   // Emit the TypeID explicit specializations to have a single symbol def.
-  if (!dialect.getCppNamespace().empty())
+  if (!clUseFallbackTypeIDs && !dialect.getCppNamespace().empty())
     os << "MLIR_DEFINE_EXPLICIT_TYPE_ID(" << dialect.getCppNamespace()
        << "::" << cppClassName << ")\n";
 



More information about the Mlir-commits mailing list