-
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
Conversation
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";
|
Ping |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM.
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.