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

Stef Lindall llvmlistbot at llvm.org
Tue Feb 4 13:37:26 PST 2025


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

Adds two new CL flags (I didn't find a simple way to just add one):
- `-gen-attr-or-type-use-fallback-type-ids`
- `-gen-op-use-fallback-type-ids`

When specified, these modify type, attr, and op decl+definition generation to not emit `MLIR_*_EXPLICIT_TYPE_ID` macros. This falls back to string TypeIDs which use string comparison, and a potential avenue in avoiding complications related to https://discourse.llvm.org/t/more-work-needed-for-typeid-duplication-help-wanted/4041/14.

>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] [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 6a39424bd463fd..5134ccac867144 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 a970cbc5cacebe..e3c87e1e578ebd 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";
     }



More information about the Mlir-commits mailing list