[llvm] [Frontend][OpenMP] Add functions for checking construct type (PR #87076)

Krzysztof Parzyszek via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 29 08:35:57 PDT 2024


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

>From 9935ba849b55dfa2bc622b628c2e1609b5fdf10d Mon Sep 17 00:00:00 2001
From: Krzysztof Parzyszek <Krzysztof.Parzyszek at amd.com>
Date: Mon, 11 Mar 2024 12:55:38 -0500
Subject: [PATCH 1/2] [Frontend][OpenMP] Add functions for checking construct
 type

Implement helper functions to identify leaf, composite, and combined
constructs.
---
 llvm/include/llvm/Frontend/OpenMP/OMP.h |  6 ++++++
 llvm/lib/Frontend/OpenMP/OMP.cpp        | 22 ++++++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/llvm/include/llvm/Frontend/OpenMP/OMP.h b/llvm/include/llvm/Frontend/OpenMP/OMP.h
index a85cd9d344c6d7..36fe77a94de7c7 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMP.h
+++ b/llvm/include/llvm/Frontend/OpenMP/OMP.h
@@ -15,4 +15,10 @@
 
 #include "llvm/Frontend/OpenMP/OMP.h.inc"
 
+namespace llvm::omp {
+bool isLeafConstruct(Directive D);
+bool isCompositeConstruct(Directive D);
+bool isCombinedConstruct(Directive D);
+} // namespace llvm::omp
+
 #endif // LLVM_FRONTEND_OPENMP_OMP_H
diff --git a/llvm/lib/Frontend/OpenMP/OMP.cpp b/llvm/lib/Frontend/OpenMP/OMP.cpp
index 4f2f95392648b3..0ef12cb106426e 100644
--- a/llvm/lib/Frontend/OpenMP/OMP.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMP.cpp
@@ -8,6 +8,7 @@
 
 #include "llvm/Frontend/OpenMP/OMP.h"
 
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -17,3 +18,24 @@ using namespace omp;
 
 #define GEN_DIRECTIVES_IMPL
 #include "llvm/Frontend/OpenMP/OMP.inc"
+
+namespace llvm::omp {
+bool isLeafConstruct(Directive D) { return getLeafConstructs(D).empty(); }
+
+bool isCompositeConstruct(Directive D) {
+  // OpenMP Spec 5.2: [17.3, 8-9]
+  // If directive-name-A and directive-name-B both correspond to loop-
+  // associated constructs then directive-name is a composite construct
+  size_t numLoopConstructs =
+      llvm::count_if(getLeafConstructs(D), [](Directive L) {
+        return getDirectiveAssociation(L) == Association::Loop;
+      });
+  return numLoopConstructs > 1;
+}
+
+bool isCombinedConstruct(Directive D) {
+  // OpenMP Spec 5.2: [17.3, 9-10]
+  // Otherwise directive-name is a combined construct.
+  return !getLeafConstructs(D).empty() && !isCompositeConstruct(D);
+}
+} // namespace llvm::omp

>From 400ae0834ab793ec8e0022b6aa7cc1bc36b9db02 Mon Sep 17 00:00:00 2001
From: Krzysztof Parzyszek <Krzysztof.Parzyszek at amd.com>
Date: Fri, 29 Mar 2024 10:35:11 -0500
Subject: [PATCH 2/2] Correct interpretation of directive-name-A and
 directive-name-B

---
 llvm/lib/Frontend/OpenMP/OMP.cpp | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Frontend/OpenMP/OMP.cpp b/llvm/lib/Frontend/OpenMP/OMP.cpp
index 0ef12cb106426e..6cbb060234771b 100644
--- a/llvm/lib/Frontend/OpenMP/OMP.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMP.cpp
@@ -8,6 +8,7 @@
 
 #include "llvm/Frontend/OpenMP/OMP.h"
 
+#include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/StringSwitch.h"
@@ -26,11 +27,17 @@ bool isCompositeConstruct(Directive D) {
   // OpenMP Spec 5.2: [17.3, 8-9]
   // If directive-name-A and directive-name-B both correspond to loop-
   // associated constructs then directive-name is a composite construct
+  llvm::ArrayRef<Directive> Leafs{getLeafConstructs(D)};
+  if (Leafs.empty())
+    return false;
+  if (getDirectiveAssociation(Leafs.front()) != Association::Loop)
+    return false;
+
   size_t numLoopConstructs =
-      llvm::count_if(getLeafConstructs(D), [](Directive L) {
+      llvm::count_if(Leafs.drop_front(), [](Directive L) {
         return getDirectiveAssociation(L) == Association::Loop;
       });
-  return numLoopConstructs > 1;
+  return numLoopConstructs != 0;
 }
 
 bool isCombinedConstruct(Directive D) {



More information about the llvm-commits mailing list