[flang-commits] [flang] [llvm] [OpenMP] Add constexpr enum ranges for all clauses and all directives (PR #210406)

Krzysztof Parzyszek via flang-commits flang-commits at lists.llvm.org
Sat Jul 18 08:43:13 PDT 2026


https://github.com/kparzysz updated https://github.com/llvm/llvm-project/pull/210406

>From 6e4eadc20e8519b3ecc92ff20f41cca3b4e8641e Mon Sep 17 00:00:00 2001
From: Krzysztof Parzyszek <Krzysztof.Parzyszek at amd.com>
Date: Fri, 17 Jul 2026 11:39:16 -0500
Subject: [PATCH] [OpenMP] Add constexpr enum ranges for all clauses and all
 directives

---
 flang/lib/Parser/openmp-parsers.cpp                      | 7 ++-----
 llvm/include/llvm/Frontend/OpenMP/OMP.h                  | 9 +++++++++
 llvm/lib/Frontend/OpenMP/OMP.cpp                         | 6 ++----
 .../unittests/Frontend/OpenMPDirectiveNameParserTest.cpp | 6 ++----
 4 files changed, 15 insertions(+), 13 deletions(-)

diff --git a/flang/lib/Parser/openmp-parsers.cpp b/flang/lib/Parser/openmp-parsers.cpp
index 105a72de5cb29..9051d5863df07 100644
--- a/flang/lib/Parser/openmp-parsers.cpp
+++ b/flang/lib/Parser/openmp-parsers.cpp
@@ -2622,19 +2622,16 @@ TYPE_CONTEXT_PARSER("OpenMP construct"_en_US,
                     Parser<OmpDelimitedMetadirectiveDirective>{}))))
 
 static constexpr DirectiveSet GetLoopDirectives() {
-  using Directive = llvm::omp::Directive;
   using SourceLanguage = llvm::omp::SourceLanguage;
   DirectiveSet loopDirectives;
 
-  for (auto dirVal{llvm::to_underlying(Directive::First_)};
-      dirVal != llvm::to_underlying(Directive::Last_) + 1; ++dirVal) {
-    auto dirId{static_cast<Directive>(dirVal)};
+  for (auto dirId : llvm::omp::directives()) {
     auto assoc{getDirectiveAssociation(dirId)};
     if (assoc == llvm::omp::Association::LoopNest ||
         assoc == llvm::omp::Association::LoopSeq) {
       auto langs{getDirectiveLanguages(dirId)};
       if (llvm::to_underlying(langs & SourceLanguage::Fortran) != 0) {
-        loopDirectives.set(dirVal);
+        loopDirectives.set(llvm::to_underlying(dirId));
       }
     }
   }
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMP.h b/llvm/include/llvm/Frontend/OpenMP/OMP.h
index 530f3f1410212..f54352adaa8b8 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMP.h
+++ b/llvm/include/llvm/Frontend/OpenMP/OMP.h
@@ -17,6 +17,7 @@
 #include "llvm/Support/Compiler.h"
 
 #include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/Sequence.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
 
@@ -33,6 +34,14 @@ LLVM_ABI bool isLeafConstruct(Directive D);
 LLVM_ABI bool isCompositeConstruct(Directive D);
 LLVM_ABI bool isCombinedConstruct(Directive D);
 
+static constexpr inline auto clauses() {
+  return llvm::enum_seq_inclusive(Clause::First_, Clause::Last_);
+}
+
+static constexpr inline auto directives() {
+  return llvm::enum_seq_inclusive(Directive::First_, Directive::Last_);
+}
+
 /// Can clause C have an iterator-modifier.
 static constexpr inline bool canHaveIterator(Clause C) {
   // [5.2:67:5]
diff --git a/llvm/lib/Frontend/OpenMP/OMP.cpp b/llvm/lib/Frontend/OpenMP/OMP.cpp
index 86e144624211c..4e18f49395e4c 100644
--- a/llvm/lib/Frontend/OpenMP/OMP.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMP.cpp
@@ -81,14 +81,12 @@ static void
 collectPrivatizingConstructs(llvm::SmallSet<Directive, 16> &Constructs,
                              unsigned Version) {
   llvm::SmallSet<Clause, 16> Privatizing;
-  for (auto C :
-       llvm::enum_seq_inclusive<Clause>(Clause::First_, Clause::Last_)) {
+  for (auto C : clauses()) {
     if (isPrivatizingClause(C, Version))
       Privatizing.insert(C);
   }
 
-  for (auto D : llvm::enum_seq_inclusive<Directive>(Directive::First_,
-                                                    Directive::Last_)) {
+  for (auto D : directives()) {
     bool AllowsPrivatizing = llvm::any_of(Privatizing, [&](Clause C) {
       return isAllowedClauseForDirective(D, C, Version);
     });
diff --git a/llvm/unittests/Frontend/OpenMPDirectiveNameParserTest.cpp b/llvm/unittests/Frontend/OpenMPDirectiveNameParserTest.cpp
index 10329820bef76..3d6ccc744b362 100644
--- a/llvm/unittests/Frontend/OpenMPDirectiveNameParserTest.cpp
+++ b/llvm/unittests/Frontend/OpenMPDirectiveNameParserTest.cpp
@@ -82,8 +82,7 @@ getParamName1(const testing::TestParamInfo<Tokenize::ParamType> &Info) {
 }
 
 INSTANTIATE_TEST_SUITE_P(DirectiveNameParserTest, Tokenize,
-                         testing::ValuesIn(llvm::enum_seq_inclusive(
-                             omp::Directive::First_, omp::Directive::Last_)),
+                         testing::ValuesIn(llvm::omp::directives()),
                          getParamName1);
 
 // Test parsing of valid names.
@@ -123,8 +122,7 @@ getParamName2(const testing::TestParamInfo<ParseValid::ParamType> &Info) {
 
 INSTANTIATE_TEST_SUITE_P(
     DirectiveNameParserTest, ParseValid,
-    testing::Combine(testing::ValuesIn(llvm::enum_seq_inclusive(
-                         omp::Directive::First_, omp::Directive::Last_)),
+    testing::Combine(testing::ValuesIn(llvm::omp::directives()),
                      testing::ValuesIn(omp::getOpenMPVersions())),
     getParamName2);
 



More information about the flang-commits mailing list