Skip to content

[tblgen] Add command line flag for using fallback TypeIDs in generation #125767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from

Conversation

bethebunny
Copy link
Contributor

@bethebunny bethebunny commented Feb 4, 2025

Adds a new -use-fallback-type-ids flag to tblgen.

When specified, this flag causes type, attr, op, and dialect 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.

Copy link

github-actions bot commented Feb 4, 2025

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir labels Feb 4, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 4, 2025

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-core

Author: Stef Lindall (bethebunny)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/125767.diff

2 Files Affected:

  • (modified) mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp (+13-2)
  • (modified) mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp (+13-2)
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";
     }

@dukebw dukebw requested a review from River707 February 4, 2025 21:45
Copy link

github-actions bot commented Feb 4, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff 2a84e1e65ad7f84c2dbcf37241a7d1805a523e0d 5379ad651f9f82f8d35374fa4c46f4a511eedc88 --extensions cpp,h -- mlir/tools/mlir-tblgen/SharedCL.cpp mlir/tools/mlir-tblgen/SharedCL.h mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp mlir/tools/mlir-tblgen/DialectGen.cpp mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp
View the diff from clang-format here.
diff --git a/mlir/tools/mlir-tblgen/DialectGen.cpp b/mlir/tools/mlir-tblgen/DialectGen.cpp
index e230100f34..bd5a89f56d 100644
--- a/mlir/tools/mlir-tblgen/DialectGen.cpp
+++ b/mlir/tools/mlir-tblgen/DialectGen.cpp
@@ -10,8 +10,8 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "SharedCL.h"
 #include "DialectGenUtilities.h"
+#include "SharedCL.h"
 #include "mlir/TableGen/Class.h"
 #include "mlir/TableGen/CodeGenHelpers.h"
 #include "mlir/TableGen/Format.h"

@bethebunny bethebunny changed the title [tblgen] Add command line flags for using fallback TypeIDs in generation [tblgen] Add command line flag for using fallback TypeIDs in generation Feb 11, 2025
@joker-eph
Copy link
Collaborator

That's an interesting direction!
But this alone will not address everything (we have C++ code not generated by TableGen. Can we handle this in the macro implementation instead?

@bethebunny
Copy link
Contributor Author

bethebunny commented Feb 12, 2025

Huh I hadn't thought of that! Before updating the PR I wanted to make sure I'm understanding your suggestion. Is this kindof what you had in mind? https://gist.github.com/bethebunny/a12b4e021029b02ce822520ca62e3bc0

This isn't specifically in the macro definition, but adds an additional MLIR_USE_FALLBACK_TYPE_IDS macro which can be set to true, in which case specialization routes around the explicitly-defined TypeIDResolvers.

@joker-eph
Copy link
Collaborator

This is what I had in mind indeed (I would think you'd need to guard line 52 as well?).
You could also put the specializations for class TypeIDResolver in between #if ! MLIR_USE_FALLBACK_TYPE_IDS or something like that.

Now the comment line 29 seems like a hurdle:

is not usable by classes defined in anonymous contexts.

In general I'm unsure about the risk of name conflicts with this?

@bethebunny
Copy link
Contributor Author

In general I'm unsure about the risk of name conflicts with this?

I think this is OK. Types in anonymous contexts shouldn't be able to be shared across DSOs anyway, so it's fine for them to continue to define as self-owned.

@bethebunny
Copy link
Contributor Author

Sent as a separate PR here: #126999

@bethebunny bethebunny closed this Feb 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants