[llvm-branch-commits] [flang] [flang][OpenMP] Use new modifiers in IF/LASTPRIVATE (PR #118128)

Tom Eccles via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Mon Dec 2 04:10:32 PST 2024


================
@@ -16,13 +16,58 @@
 #include "token-parsers.h"
 #include "type-parser-implementation.h"
 #include "flang/Parser/parse-tree.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/STLExtras.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Frontend/OpenMP/OMP.h"
 
 // OpenMP Directives and Clauses
 namespace Fortran::parser {
 
 constexpr auto startOmpLine = skipStuffBeforeStatement >> "!$OMP "_sptok;
 constexpr auto endOmpLine = space >> endOfLine;
 
+/// Parse OpenMP directive name (this includes compound directives).
+struct OmpDirectiveNameParser {
+  using resultType = llvm::omp::Directive;
+  using Token = TokenStringMatch<false, false>;
+
+  std::optional<resultType> Parse(ParseState &state) const {
+    for (const NameWithId &nid : directives()) {
+      if (attempt(Token(nid.first.data())).Parse(state)) {
+        return nid.second;
+      }
+    }
+    return std::nullopt;
+  }
+
+private:
+  using NameWithId = std::pair<std::string, llvm::omp::Directive>;
+
+  llvm::iterator_range<const NameWithId *> directives() const;
+  void initTokens(NameWithId *) const;
+};
+
+llvm::iterator_range<const OmpDirectiveNameParser::NameWithId *>
+OmpDirectiveNameParser::directives() const {
+  static NameWithId table[llvm::omp::Directive_enumSize];
+  [[maybe_unused]] static bool init = (initTokens(table), true);
----------------
tblah wrote:

If I understand correctly, this is a trick to run `initTokens` along with static variable initializers.

This is quite clever but I had to stare at it for a while to understand what was going on. I would prefer something very simple like, but I will leave this up to you - maybe this is a widely understood idiom I am just unfamiliar with.
```c++
static bool init{false};
if (!init) {
  init_tokens(table);
  init = true;
}
```
This isn't semantically identical but I don't think running initTokens a bit later should matter.

https://github.com/llvm/llvm-project/pull/118128


More information about the llvm-branch-commits mailing list