-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[utils][TableGen] Make some non-bitmask enums iterable #148647
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
Open
kparzysz
wants to merge
2
commits into
users/kparzysz/e01-isprivatizing
Choose a base branch
from
users/kparzysz/e02-iterable-enum
base: users/kparzysz/e01-isprivatizing
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[utils][TableGen] Make some non-bitmask enums iterable #148647
kparzysz
wants to merge
2
commits into
users/kparzysz/e01-isprivatizing
from
users/kparzysz/e02-iterable-enum
+76
−17
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Additionally, add sentinel values <Enum>::First_ and <Enum>::Last_ to each one of those enums. This will allow using `enum_seq_inclusive` to generate the list of enum-typed values of any generated scoped (non-bitmask) enum.
@llvm/pr-subscribers-tablegen @llvm/pr-subscribers-flang-openmp Author: Krzysztof Parzyszek (kparzysz) ChangesAdditionally, add sentinel values <Enum>::First_ and <Enum>::Last_ to each one of those enums. This will allow using Full diff: https://github.com/llvm/llvm-project/pull/148647.diff 4 Files Affected:
diff --git a/llvm/test/TableGen/directive1.td b/llvm/test/TableGen/directive1.td
index 1d2bd51204e4f..3eda077eeabf7 100644
--- a/llvm/test/TableGen/directive1.td
+++ b/llvm/test/TableGen/directive1.td
@@ -53,6 +53,7 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-EMPTY:
// CHECK-NEXT: #include "llvm/ADT/ArrayRef.h"
// CHECK-NEXT: #include "llvm/ADT/BitmaskEnum.h"
+// CHECK-NEXT: #include "llvm/ADT/Sequence.h"
// CHECK-NEXT: #include "llvm/ADT/StringRef.h"
// CHECK-NEXT: #include "llvm/Frontend/Directive/Spelling.h"
// CHECK-NEXT: #include "llvm/Support/Compiler.h"
@@ -66,22 +67,26 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-EMPTY:
// CHECK-NEXT: enum class Association {
// CHECK-NEXT: Block,
+// CHECK-NEXT: First_ = Block,
// CHECK-NEXT: Declaration,
// CHECK-NEXT: Delimited,
// CHECK-NEXT: Loop,
// CHECK-NEXT: None,
// CHECK-NEXT: Separating,
+// CHECK-NEXT: Last_ = Separating,
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: static constexpr std::size_t Association_enumSize = 6;
// CHECK-EMPTY:
// CHECK-NEXT: enum class Category {
// CHECK-NEXT: Declarative,
+// CHECK-NEXT: First_ = Declarative,
// CHECK-NEXT: Executable,
// CHECK-NEXT: Informational,
// CHECK-NEXT: Meta,
// CHECK-NEXT: Subsidiary,
// CHECK-NEXT: Utility,
+// CHECK-NEXT: Last_ = Utility,
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: static constexpr std::size_t Category_enumSize = 6;
@@ -96,6 +101,8 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-EMPTY:
// CHECK-NEXT: enum class Directive {
// CHECK-NEXT: TDLD_dira,
+// CHECK-NEXT: First_ = TDLD_dira,
+// CHECK-NEXT: Last_ = TDLD_dira,
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: static constexpr std::size_t Directive_enumSize = 1;
@@ -104,8 +111,10 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-EMPTY:
// CHECK-NEXT: enum class Clause {
// CHECK-NEXT: TDLC_clausea,
+// CHECK-NEXT: First_ = TDLC_clausea,
// CHECK-NEXT: TDLC_clauseb,
// CHECK-NEXT: TDLC_clausec,
+// CHECK-NEXT: Last_ = TDLC_clausec,
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: static constexpr std::size_t Clause_enumSize = 3;
@@ -151,6 +160,22 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-NEXT: LLVM_ABI StringRef getTdlAKindName(AKind x);
// CHECK-EMPTY:
// CHECK-NEXT: } // namespace tdl
+// CHECK-EMPTY:
+// CHECK-NEXT: template <> struct enum_iteration_traits<tdl::Association> {
+// CHECK-NEXT: static constexpr bool is_iterable = true;
+// CHECK-NEXT: };
+// CHECK-EMPTY:
+// CHECK-NEXT: template <> struct enum_iteration_traits<tdl::Category> {
+// CHECK-NEXT: static constexpr bool is_iterable = true;
+// CHECK-NEXT: };
+// CHECK-EMPTY:
+// CHECK-NEXT: template <> struct enum_iteration_traits<tdl::Directive> {
+// CHECK-NEXT: static constexpr bool is_iterable = true;
+// CHECK-NEXT: };
+// CHECK-EMPTY:
+// CHECK-NEXT: template <> struct enum_iteration_traits<tdl::Clause> {
+// CHECK-NEXT: static constexpr bool is_iterable = true;
+// CHECK-NEXT: };
// CHECK-NEXT: } // namespace llvm
// CHECK-NEXT: #endif // LLVM_Tdl_INC
diff --git a/llvm/test/TableGen/directive2.td b/llvm/test/TableGen/directive2.td
index 3a64bb3900a31..a25197c3efd93 100644
--- a/llvm/test/TableGen/directive2.td
+++ b/llvm/test/TableGen/directive2.td
@@ -46,6 +46,7 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-NEXT: #define LLVM_Tdl_INC
// CHECK-EMPTY:
// CHECK-NEXT: #include "llvm/ADT/ArrayRef.h"
+// CHECK-NEXT: #include "llvm/ADT/Sequence.h"
// CHECK-NEXT: #include "llvm/ADT/StringRef.h"
// CHECK-NEXT: #include "llvm/Frontend/Directive/Spelling.h"
// CHECK-NEXT: #include "llvm/Support/Compiler.h"
@@ -57,22 +58,26 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-EMPTY:
// CHECK-NEXT: enum class Association {
// CHECK-NEXT: Block,
+// CHECK-NEXT: First_ = Block,
// CHECK-NEXT: Declaration,
// CHECK-NEXT: Delimited,
// CHECK-NEXT: Loop,
// CHECK-NEXT: None,
// CHECK-NEXT: Separating,
+// CHECK-NEXT: Last_ = Separating,
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: static constexpr std::size_t Association_enumSize = 6;
// CHECK-EMPTY:
// CHECK-NEXT: enum class Category {
// CHECK-NEXT: Declarative,
+// CHECK-NEXT: First_ = Declarative,
// CHECK-NEXT: Executable,
// CHECK-NEXT: Informational,
// CHECK-NEXT: Meta,
// CHECK-NEXT: Subsidiary,
// CHECK-NEXT: Utility,
+// CHECK-NEXT: Last_ = Utility,
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: static constexpr std::size_t Category_enumSize = 6;
@@ -87,15 +92,19 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-EMPTY:
// CHECK-NEXT: enum class Directive {
// CHECK-NEXT: TDLD_dira,
+// CHECK-NEXT: First_ = TDLD_dira,
+// CHECK-NEXT: Last_ = TDLD_dira,
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: static constexpr std::size_t Directive_enumSize = 1;
// CHECK-EMPTY:
// CHECK-NEXT: enum class Clause {
// CHECK-NEXT: TDLC_clausea,
+// CHECK-NEXT: First_ = TDLC_clausea,
// CHECK-NEXT: TDLC_clauseb,
// CHECK-NEXT: TDLC_clausec,
// CHECK-NEXT: TDLC_claused,
+// CHECK-NEXT: Last_ = TDLC_claused,
// CHECK-NEXT: };
// CHECK-EMPTY:
// CHECK-NEXT: static constexpr std::size_t Clause_enumSize = 4;
@@ -124,6 +133,22 @@ def TDL_DirA : Directive<[Spelling<"dira">]> {
// CHECK-NEXT: LLVM_ABI Category getDirectiveCategory(Directive D);
// CHECK-NEXT: LLVM_ABI SourceLanguage getDirectiveLanguages(Directive D);
// CHECK-NEXT: } // namespace tdl
+// CHECK-EMPTY:
+// CHECK-NEXT: template <> struct enum_iteration_traits<tdl::Association> {
+// CHECK-NEXT: static constexpr bool is_iterable = true;
+// CHECK-NEXT: };
+// CHECK-EMPTY:
+// CHECK-NEXT: template <> struct enum_iteration_traits<tdl::Category> {
+// CHECK-NEXT: static constexpr bool is_iterable = true;
+// CHECK-NEXT: };
+// CHECK-EMPTY:
+// CHECK-NEXT: template <> struct enum_iteration_traits<tdl::Directive> {
+// CHECK-NEXT: static constexpr bool is_iterable = true;
+// CHECK-NEXT: };
+// CHECK-EMPTY:
+// CHECK-NEXT: template <> struct enum_iteration_traits<tdl::Clause> {
+// CHECK-NEXT: static constexpr bool is_iterable = true;
+// CHECK-NEXT: };
// CHECK-NEXT: } // namespace llvm
// CHECK-NEXT: #endif // LLVM_Tdl_INC
diff --git a/llvm/unittests/Frontend/OpenMPDirectiveNameParserTest.cpp b/llvm/unittests/Frontend/OpenMPDirectiveNameParserTest.cpp
index 0363a08cc0f03..10329820bef76 100644
--- a/llvm/unittests/Frontend/OpenMPDirectiveNameParserTest.cpp
+++ b/llvm/unittests/Frontend/OpenMPDirectiveNameParserTest.cpp
@@ -48,12 +48,6 @@ static std::string &prepareParamName(std::string &Name) {
return Name;
}
-namespace llvm {
-template <> struct enum_iteration_traits<omp::Directive> {
- static constexpr bool is_iterable = true;
-};
-} // namespace llvm
-
// Test tokenizing.
class Tokenize : public testing::TestWithParam<omp::Directive> {};
@@ -87,12 +81,10 @@ getParamName1(const testing::TestParamInfo<Tokenize::ParamType> &Info) {
return prepareParamName(Name);
}
-INSTANTIATE_TEST_SUITE_P(
- DirectiveNameParserTest, Tokenize,
- testing::ValuesIn(
- llvm::enum_seq(static_cast<omp::Directive>(0),
- static_cast<omp::Directive>(omp::Directive_enumSize))),
- getParamName1);
+INSTANTIATE_TEST_SUITE_P(DirectiveNameParserTest, Tokenize,
+ testing::ValuesIn(llvm::enum_seq_inclusive(
+ omp::Directive::First_, omp::Directive::Last_)),
+ getParamName1);
// Test parsing of valid names.
@@ -131,9 +123,8 @@ getParamName2(const testing::TestParamInfo<ParseValid::ParamType> &Info) {
INSTANTIATE_TEST_SUITE_P(
DirectiveNameParserTest, ParseValid,
- testing::Combine(testing::ValuesIn(llvm::enum_seq(
- static_cast<omp::Directive>(0),
- static_cast<omp::Directive>(omp::Directive_enumSize))),
+ testing::Combine(testing::ValuesIn(llvm::enum_seq_inclusive(
+ omp::Directive::First_, omp::Directive::Last_)),
testing::ValuesIn(omp::getOpenMPVersions())),
getParamName2);
diff --git a/llvm/utils/TableGen/Basic/DirectiveEmitter.cpp b/llvm/utils/TableGen/Basic/DirectiveEmitter.cpp
index 177eecebce9a5..b48b9f6b7c41c 100644
--- a/llvm/utils/TableGen/Basic/DirectiveEmitter.cpp
+++ b/llvm/utils/TableGen/Basic/DirectiveEmitter.cpp
@@ -106,9 +106,15 @@ static void generateEnumClass(ArrayRef<const Record *> Records, raw_ostream &OS,
bool ExportEnums) {
OS << "\n";
OS << "enum class " << Enum << " {\n";
- for (const Record *R : Records) {
- OS << " " << getIdentifierName(R, Prefix) << ",\n";
+ std::string N;
+ for (auto [I, R] : llvm::enumerate(Records)) {
+ N = getIdentifierName(R, Prefix);
+ OS << " " << N << ",\n";
+ // Make the sentinel names less likely to conflict with actual names...
+ if (I == 0)
+ OS << " First_ = " << N << ",\n";
}
+ OS << " Last_ = " << N << ",\n";
OS << "};\n";
OS << "\n";
OS << "static constexpr std::size_t " << Enum
@@ -282,6 +288,7 @@ static void emitDirectivesDecl(const RecordKeeper &Records, raw_ostream &OS) {
if (DirLang.hasEnableBitmaskEnumInNamespace())
OS << "#include \"llvm/ADT/BitmaskEnum.h\"\n";
+ OS << "#include \"llvm/ADT/Sequence.h\"\n";
OS << "#include \"llvm/ADT/StringRef.h\"\n";
OS << "#include \"llvm/Frontend/Directive/Spelling.h\"\n";
OS << "#include \"llvm/Support/Compiler.h\"\n";
@@ -375,6 +382,15 @@ static void emitDirectivesDecl(const RecordKeeper &Records, raw_ostream &OS) {
for (auto Ns : reverse(Namespaces))
OS << "} // namespace " << Ns << "\n";
+ // These specializations need to be in ::llvm.
+ for (StringRef Enum : {"Association", "Category", "Directive", "Clause"}) {
+ OS << "\n";
+ OS << "template <> struct enum_iteration_traits<"
+ << DirLang.getCppNamespace() << "::" << Enum << "> {\n";
+ OS << " static constexpr bool is_iterable = true;\n";
+ OS << "};\n";
+ }
+
OS << "} // namespace llvm\n";
OS << "#endif // LLVM_" << Lang << "_INC\n";
|
This was referenced Jul 14, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Additionally, add sentinel values ::First_ and ::Last_ to each one of those enums.
This will allow using
enum_seq_inclusive
to generate the list of enum-typed values of any generated scoped (non-bitmask) enum.