From dfc4c69dd7eb151162f21abeb24a4f1b3e412bee Mon Sep 17 00:00:00 2001 From: Stef Date: Tue, 4 Feb 2025 21:26:16 +0000 Subject: [PATCH 1/8] [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 6a39424bd463f..5134ccac86714 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 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 a970cbc5caceb..e3c87e1e578eb 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 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 Date: Tue, 4 Feb 2025 21:38:28 +0000 Subject: [PATCH 2/8] 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 5134ccac86714..5ebd0b654744e 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 e3c87e1e578eb..1e37ec2f9f862 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 Date: Tue, 4 Feb 2025 21:39:40 +0000 Subject: [PATCH 3/8] 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 5ebd0b654744e..954e1ce378612 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 Date: Tue, 4 Feb 2025 23:03:13 +0000 Subject: [PATCH 4/8] 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 954e1ce378612..9109c549dfb67 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 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 1e37ec2f9f862..79b9a0e2ab374 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 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 Date: Fri, 7 Feb 2025 00:44:37 +0000 Subject: [PATCH 5/8] 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 414cad5e1dcc2..c83c785b2c837 100644 --- a/mlir/tools/mlir-tblgen/DialectGen.cpp +++ b/mlir/tools/mlir-tblgen/DialectGen.cpp @@ -38,6 +38,12 @@ llvm::cl::opt selectedDialect("dialect", llvm::cl::desc("The dialect to gen for"), llvm::cl::cat(dialectGenCat), llvm::cl::CommaSeparated); +static llvm::cl::opt 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"; From fe7e0465e5292dd9eccccc4efd27911a6ce2025e Mon Sep 17 00:00:00 2001 From: Stef Date: Tue, 11 Feb 2025 23:22:44 +0000 Subject: [PATCH 6/8] use one shared flag --- mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp | 13 +----------- mlir/tools/mlir-tblgen/DialectGen.cpp | 7 +------ mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp | 13 +----------- mlir/tools/mlir-tblgen/SharedCL.cpp | 21 ++++++++++++++++++++ mlir/tools/mlir-tblgen/SharedCL.h | 22 +++++++++++++++++++++ 5 files changed, 46 insertions(+), 30 deletions(-) create mode 100644 mlir/tools/mlir-tblgen/SharedCL.cpp create mode 100644 mlir/tools/mlir-tblgen/SharedCL.h diff --git a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp index 9109c549dfb67..6aeaf3ae4780c 100644 --- a/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp +++ b/mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp @@ -7,6 +7,7 @@ //===----------------------------------------------------------------------===// #include "AttrOrTypeFormatGen.h" +#include "SharedCL.h" #include "mlir/TableGen/AttrOrTypeDef.h" #include "mlir/TableGen/Class.h" #include "mlir/TableGen/CodeGenHelpers.h" @@ -25,18 +26,6 @@ using namespace mlir::tblgen; using llvm::Record; using llvm::RecordKeeper; -//===----------------------------------------------------------------------===// -// CL Options -//===----------------------------------------------------------------------===// - -static llvm::cl::OptionCategory clAttrOrTypeDefs("Options for -gen-*def-*"); - -static llvm::cl::opt 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 //===----------------------------------------------------------------------===// diff --git a/mlir/tools/mlir-tblgen/DialectGen.cpp b/mlir/tools/mlir-tblgen/DialectGen.cpp index c83c785b2c837..e230100f34d0d 100644 --- a/mlir/tools/mlir-tblgen/DialectGen.cpp +++ b/mlir/tools/mlir-tblgen/DialectGen.cpp @@ -10,6 +10,7 @@ // //===----------------------------------------------------------------------===// +#include "SharedCL.h" #include "DialectGenUtilities.h" #include "mlir/TableGen/Class.h" #include "mlir/TableGen/CodeGenHelpers.h" @@ -38,12 +39,6 @@ llvm::cl::opt selectedDialect("dialect", llvm::cl::desc("The dialect to gen for"), llvm::cl::cat(dialectGenCat), llvm::cl::CommaSeparated); -static llvm::cl::opt 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 = diff --git a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp index 79b9a0e2ab374..eaa26e545eec6 100644 --- a/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp +++ b/mlir/tools/mlir-tblgen/OpDefinitionsGen.cpp @@ -14,6 +14,7 @@ #include "OpClass.h" #include "OpFormatGen.h" #include "OpGenHelpers.h" +#include "SharedCL.h" #include "mlir/TableGen/Argument.h" #include "mlir/TableGen/Attribute.h" #include "mlir/TableGen/Class.h" @@ -230,18 +231,6 @@ static const char *const opCommentHeader = R"( )"; -//===----------------------------------------------------------------------===// -// CL Options -//===----------------------------------------------------------------------===// - -static llvm::cl::OptionCategory clOpDefs("Options for op definitions"); - -static llvm::cl::opt 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 //===----------------------------------------------------------------------===// diff --git a/mlir/tools/mlir-tblgen/SharedCL.cpp b/mlir/tools/mlir-tblgen/SharedCL.cpp new file mode 100644 index 0000000000000..ee473100a2702 --- /dev/null +++ b/mlir/tools/mlir-tblgen/SharedCL.cpp @@ -0,0 +1,21 @@ +//===- SharedCL.cpp - tblgen command line arguments -----------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#include "SharedCL.h" + +using namespace mlir; +using namespace mlir::tblgen; +namespace cl = llvm::cl; + +static llvm::cl::OptionCategory clShared("Options for all -gen-*"); + +cl::opt mlir::tblgen::clUseFallbackTypeIDs = cl::opt( + "use-fallback-type-ids", + cl::desc( + "Don't generate static TypeID decls; fall back to string comparison."), + cl::init(false), cl::cat(clShared)); diff --git a/mlir/tools/mlir-tblgen/SharedCL.h b/mlir/tools/mlir-tblgen/SharedCL.h new file mode 100644 index 0000000000000..a4610822f91f0 --- /dev/null +++ b/mlir/tools/mlir-tblgen/SharedCL.h @@ -0,0 +1,22 @@ +//===- SharedCL.h - tblgen command line arguments -------------------------===// +// +// 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_TOOLS_MLIRTBLGEN_SHARED_CL_H_ +#define MLIR_TOOLS_MLIRTBLGEN_SHARED_CL_H_ + +#include "llvm/Support/CommandLine.h" + +namespace mlir { +namespace tblgen { + +extern llvm::cl::opt clUseFallbackTypeIDs; + +} // namespace tblgen +} // namespace mlir + +#endif // MLIR_TOOLS_MLIRTBLGEN_SHARED_CL_H_ From b06ab32bb5af45cfe5340af60789a988a3f9ebdb Mon Sep 17 00:00:00 2001 From: Stef Date: Wed, 12 Feb 2025 00:06:48 +0000 Subject: [PATCH 7/8] clang-formt --- mlir/tools/mlir-tblgen/SharedCL.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/mlir/tools/mlir-tblgen/SharedCL.cpp b/mlir/tools/mlir-tblgen/SharedCL.cpp index ee473100a2702..733fc3a34f164 100644 --- a/mlir/tools/mlir-tblgen/SharedCL.cpp +++ b/mlir/tools/mlir-tblgen/SharedCL.cpp @@ -15,7 +15,7 @@ namespace cl = llvm::cl; static llvm::cl::OptionCategory clShared("Options for all -gen-*"); cl::opt mlir::tblgen::clUseFallbackTypeIDs = cl::opt( - "use-fallback-type-ids", - cl::desc( - "Don't generate static TypeID decls; fall back to string comparison."), - cl::init(false), cl::cat(clShared)); + "use-fallback-type-ids", + cl::desc( + "Don't generate static TypeID decls; fall back to string comparison."), + cl::init(false), cl::cat(clShared)); From 5379ad651f9f82f8d35374fa4c46f4a511eedc88 Mon Sep 17 00:00:00 2001 From: Stef Date: Wed, 12 Feb 2025 00:38:41 +0000 Subject: [PATCH 8/8] update CMakeLists.txt --- mlir/tools/mlir-tblgen/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/mlir/tools/mlir-tblgen/CMakeLists.txt b/mlir/tools/mlir-tblgen/CMakeLists.txt index fb507dc7f8c3c..6c6c74deb9938 100644 --- a/mlir/tools/mlir-tblgen/CMakeLists.txt +++ b/mlir/tools/mlir-tblgen/CMakeLists.txt @@ -31,6 +31,7 @@ add_tablegen(mlir-tblgen MLIR PassDocGen.cpp PassGen.cpp RewriterGen.cpp + SharedCL.cpp SPIRVUtilsGen.cpp )