[clang] [clang][OpenMP] Fix teams nesting of region check (PR #94806)

via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 7 14:50:40 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Mike Rice (mikerice1969)

<details>
<summary>Changes</summary>

The static verifier flagged dead code in the check since the loop will only execute once and never reach the iterator increment.

The loop needs to iterate twice to correctly diagnose when a statement is after the teams.

Since there are two iterations again, reset the iterator to the first teams directive when the double teams case is seen so the diagnostic can report both locations.

---
Full diff: https://github.com/llvm/llvm-project/pull/94806.diff


2 Files Affected:

- (modified) clang/lib/Sema/SemaOpenMP.cpp (+7-3) 
- (modified) clang/test/OpenMP/Inputs/nesting_of_regions.cpp (+12) 


``````````diff
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 6e6815328e913..0fe04094c5912 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -13434,10 +13434,14 @@ StmtResult SemaOpenMP::ActOnOpenMPTargetDirective(ArrayRef<OMPClause *> Clauses,
       auto I = CS->body_begin();
       while (I != CS->body_end()) {
         const auto *OED = dyn_cast<OMPExecutableDirective>(*I);
-        if (!OED || !isOpenMPTeamsDirective(OED->getDirectiveKind()) ||
-            OMPTeamsFound) {
-
+        bool IsTeams = OED && isOpenMPTeamsDirective(OED->getDirectiveKind());
+        if (!IsTeams || I != CS->body_begin()) {
           OMPTeamsFound = false;
+          if (IsTeams && I != CS->body_begin()) {
+            // This is the two teams case. Since the InnerTeamsRegionLoc will
+            // point to this second one reset the iterator to the other teams.
+            --I;
+          }
           break;
         }
         ++I;
diff --git a/clang/test/OpenMP/Inputs/nesting_of_regions.cpp b/clang/test/OpenMP/Inputs/nesting_of_regions.cpp
index e671f9b0cf412..969ddfcce4cb0 100644
--- a/clang/test/OpenMP/Inputs/nesting_of_regions.cpp
+++ b/clang/test/OpenMP/Inputs/nesting_of_regions.cpp
@@ -4880,6 +4880,12 @@ void foo() {
 #pragma omp teams  // expected-note {{nested teams construct here}}
     ++a;
   }
+#pragma omp target // expected-error {{target construct with nested teams region contains statements outside of the teams construct}}
+  {
+#pragma omp teams  // expected-note {{nested teams construct here}}
+    ++a;
+    ++a;           // expected-note {{statement outside teams construct here}}
+  }
 #pragma omp target // expected-error {{target construct with nested teams region contains statements outside of the teams construct}}
   {
     while (0)      // expected-note {{statement outside teams construct here}}
@@ -14133,6 +14139,12 @@ void foo() {
 #pragma omp teams // expected-note {{nested teams construct here}}
     ++a;
   }
+#pragma omp target // expected-error {{target construct with nested teams region contains statements outside of the teams construct}}
+  {
+#pragma omp teams // expected-note {{nested teams construct here}}
+    ++a;
+    ++a;          // expected-note {{statement outside teams construct here}}
+  }
 #pragma omp target
   {
 #pragma omp taskloop

``````````

</details>


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


More information about the cfe-commits mailing list