[clang] [compiler-rt] [libcxx] [polly] [llvm] [flang] [libcxxabi] [mlir] [lldb] [openmp] [libc] [clang-tools-extra] [OpenMP] Patch for Support to loop bind clause : Checking Parent Region (PR #76938)

via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 8 11:03:22 PST 2024


https://github.com/SunilKuravinakop updated https://github.com/llvm/llvm-project/pull/76938

>From 1dcd4703002acdde370a285089008e409043717b Mon Sep 17 00:00:00 2001
From: Sunil Kuravinakop <kuravina at pe28vega.us.cray.com>
Date: Thu, 4 Jan 2024 04:08:28 -0600
Subject: [PATCH 1/4] Changes uploaded to the phabricator on Dec 16th are lost
 because the phabricator is down. Hence re-uploading it to the github.com.

  Changes to be committed:
 	modified:   clang/include/clang/Sema/Sema.h
 	modified:   clang/lib/Sema/SemaOpenMP.cpp
 	modified:   clang/test/OpenMP/generic_loop_ast_print.cpp
 	modified:   clang/test/OpenMP/loop_bind_messages.cpp
 	modified:   clang/test/PCH/pragma-loop.cpp
---
 clang/include/clang/Sema/Sema.h              |   8 +-
 clang/lib/Sema/SemaOpenMP.cpp                |  62 ++++---
 clang/test/OpenMP/generic_loop_ast_print.cpp |   4 +-
 clang/test/OpenMP/loop_bind_messages.cpp     | 180 +++++++++++++++++--
 clang/test/PCH/pragma-loop.cpp               |   8 +-
 5 files changed, 226 insertions(+), 36 deletions(-)

diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 5e3b57ea33220b..3d8d94d200ac58 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -11335,6 +11335,7 @@ class Sema final {
   /// on the parameter of the bind clause. In the methods for the
   /// mapped directives, check the parameters of the lastprivate clause.
   bool checkLastPrivateForMappedDirectives(ArrayRef<OMPClause *> Clauses);
+
   /// Depending on the bind clause of OMPD_loop map the directive to new
   /// directives.
   ///    1) loop bind(parallel) --> OMPD_for
@@ -11344,9 +11345,12 @@ class Sema final {
   /// rigorous semantic checking in the new mapped directives.
   bool mapLoopConstruct(llvm::SmallVector<OMPClause *> &ClausesWithoutBind,
                         ArrayRef<OMPClause *> Clauses,
-                        OpenMPBindClauseKind BindKind,
+                        OpenMPBindClauseKind &BindKind,
                         OpenMPDirectiveKind &Kind,
-                        OpenMPDirectiveKind &PrevMappedDirective);
+                        OpenMPDirectiveKind &PrevMappedDirective,
+                        SourceLocation StartLoc, SourceLocation EndLoc,
+                        const DeclarationNameInfo &DirName,
+                        OpenMPDirectiveKind CancelRegion);
 
 public:
   /// The declarator \p D defines a function in the scope \p S which is nested
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index f34d2959dc6191..6e060e5b8ee268 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -5072,6 +5072,18 @@ static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
         CurrentRegion != OMPD_cancellation_point &&
         CurrentRegion != OMPD_cancel && CurrentRegion != OMPD_scan)
       return false;
+    // Checks needed for mapping "loop" construct. Please check mapLoopConstruct
+    // for a detailed explanation
+    if (SemaRef.LangOpts.OpenMP >= 50 && CurrentRegion == OMPD_loop &&
+        ((BindKind == OMPC_BIND_parallel) || (BindKind == OMPC_BIND_teams)) &&
+        (isOpenMPWorksharingDirective(ParentRegion) ||
+         ParentRegion == OMPD_loop)) {
+      int ErrorMsgNumber = (BindKind == OMPC_BIND_parallel) ? 1 : 4;
+      SemaRef.Diag(StartLoc, diag::err_omp_prohibited_region)
+          << true << getOpenMPDirectiveName(ParentRegion) << ErrorMsgNumber
+          << getOpenMPDirectiveName(CurrentRegion);
+      return true;
+    }
     if (CurrentRegion == OMPD_cancellation_point ||
         CurrentRegion == OMPD_cancel) {
       // OpenMP [2.16, Nesting of Regions]
@@ -6124,35 +6136,36 @@ processImplicitMapsWithDefaultMappers(Sema &S, DSAStackTy *Stack,
 
 bool Sema::mapLoopConstruct(llvm::SmallVector<OMPClause *> &ClausesWithoutBind,
                             ArrayRef<OMPClause *> Clauses,
-                            OpenMPBindClauseKind BindKind,
+                            OpenMPBindClauseKind &BindKind,
                             OpenMPDirectiveKind &Kind,
-                            OpenMPDirectiveKind &PrevMappedDirective) {
+                            OpenMPDirectiveKind &PrevMappedDirective,
+                            SourceLocation StartLoc, SourceLocation EndLoc,
+                            const DeclarationNameInfo &DirName,
+                            OpenMPDirectiveKind CancelRegion) {
 
   bool UseClausesWithoutBind = false;
 
   // Restricting to "#pragma omp loop bind"
   if (getLangOpts().OpenMP >= 50 && Kind == OMPD_loop) {
+
+    const OpenMPDirectiveKind ParentDirective = DSAStack->getParentDirective();
+
     if (BindKind == OMPC_BIND_unknown) {
       // Setting the enclosing teams or parallel construct for the loop
       // directive without bind clause.
       BindKind = OMPC_BIND_thread; // Default bind(thread) if binding is unknown
 
-      const OpenMPDirectiveKind ParentDirective =
-          DSAStack->getParentDirective();
       if (ParentDirective == OMPD_unknown) {
         Diag(DSAStack->getDefaultDSALocation(),
              diag::err_omp_bind_required_on_loop);
       } else if (ParentDirective == OMPD_parallel ||
-                 ParentDirective == OMPD_target_parallel) {
+                 ParentDirective == OMPD_target_parallel)
         BindKind = OMPC_BIND_parallel;
-      } else if (ParentDirective == OMPD_teams ||
-                 ParentDirective == OMPD_target_teams) {
-        BindKind = OMPC_BIND_teams;
-      }
     } else {
-      // bind clause is present, so we should set flag indicating to only
-      // use the clauses that aren't the bind clause for the new directive that
-      // loop is lowered to.
+      // bind clause is present in loop directive. When the loop directive is
+      // changed to a new directive the bind clause is not used. So, we should
+      // set flag indicating to only use the clauses that aren't the
+      // bind clause.
       UseClausesWithoutBind = true;
     }
 
@@ -6213,26 +6226,35 @@ StmtResult Sema::ActOnOpenMPExecutableDirective(
     OpenMPDirectiveKind PrevMappedDirective) {
   StmtResult Res = StmtError();
   OpenMPBindClauseKind BindKind = OMPC_BIND_unknown;
+  llvm::SmallVector<OMPClause *> ClausesWithoutBind;
+  bool UseClausesWithoutBind = false;
+
   if (const OMPBindClause *BC =
           OMPExecutableDirective::getSingleClause<OMPBindClause>(Clauses))
     BindKind = BC->getBindKind();
+
+  // Variable used to note down the DirectiveKind because mapLoopConstruct may
+  // change "Kind" variable, due to mapping of "omp loop" to other directives.
+  OpenMPDirectiveKind DK = Kind;
+  if ((Kind == OMPD_loop) || (PrevMappedDirective == OMPD_loop)) {
+    UseClausesWithoutBind = mapLoopConstruct(
+        ClausesWithoutBind, Clauses, BindKind, Kind, PrevMappedDirective,
+        StartLoc, EndLoc, DirName, CancelRegion);
+    DK = OMPD_loop;
+  }
+
   // First check CancelRegion which is then used in checkNestingOfRegions.
   if (checkCancelRegion(*this, Kind, CancelRegion, StartLoc) ||
-      checkNestingOfRegions(*this, DSAStack, Kind, DirName, CancelRegion,
-                            BindKind, StartLoc))
+      checkNestingOfRegions(*this, DSAStack, DK, DirName, CancelRegion,
+                            BindKind, StartLoc)) {
     return StmtError();
+  }
 
   // Report affected OpenMP target offloading behavior when in HIP lang-mode.
   if (getLangOpts().HIP && (isOpenMPTargetExecutionDirective(Kind) ||
                             isOpenMPTargetDataManagementDirective(Kind)))
     Diag(StartLoc, diag::warn_hip_omp_target_directives);
 
-  llvm::SmallVector<OMPClause *> ClausesWithoutBind;
-  bool UseClausesWithoutBind = false;
-
-  UseClausesWithoutBind = mapLoopConstruct(ClausesWithoutBind, Clauses,
-                                           BindKind, Kind, PrevMappedDirective);
-
   llvm::SmallVector<OMPClause *, 8> ClausesWithImplicit;
   VarsWithInheritedDSAType VarsWithInheritedDSA;
   bool ErrorFound = false;
diff --git a/clang/test/OpenMP/generic_loop_ast_print.cpp b/clang/test/OpenMP/generic_loop_ast_print.cpp
index df806405571cf7..30ade214289188 100644
--- a/clang/test/OpenMP/generic_loop_ast_print.cpp
+++ b/clang/test/OpenMP/generic_loop_ast_print.cpp
@@ -95,11 +95,11 @@ void test() {
   }
 
   //PRINT: #pragma omp target teams
-  //PRINT: #pragma omp distribute
+  //PRINT: #pragma omp simd
   //DUMP: OMPTargetTeamsDirective
   //DUMP: CapturedStmt
   //DUMP: ForStmt
-  //DUMP: OMPDistributeDirective
+  //DUMP: OMPSimdDirective
   #pragma omp target teams
   for (int i=0; i<1000; ++i) {
     #pragma omp loop
diff --git a/clang/test/OpenMP/loop_bind_messages.cpp b/clang/test/OpenMP/loop_bind_messages.cpp
index f7fdf289714328..9492a29ba69498 100644
--- a/clang/test/OpenMP/loop_bind_messages.cpp
+++ b/clang/test/OpenMP/loop_bind_messages.cpp
@@ -4,6 +4,7 @@
 
 #define NNN 50
 int aaa[NNN];
+int aaa2[NNN][NNN];
 
 void parallel_loop() {
   #pragma omp parallel
@@ -15,6 +16,91 @@ void parallel_loop() {
    }
 }
 
+void parallel_for_AND_loop_bind() {
+  #pragma omp parallel for
+  for (int i = 0 ; i < NNN ; i++) {
+    #pragma omp loop bind(parallel) // expected-error{{region cannot be closely nested inside 'parallel for' region; perhaps you forget to enclose 'omp loop' directive into a parallel region?}}
+    for (int j = 0 ; j < NNN ; j++) {
+      aaa2[i][j] = i+j;
+    }
+  }
+}
+
+void parallel_nowait() {
+  #pragma omp parallel
+  #pragma omp for nowait
+  for (int i = 0 ; i < NNN ; i++) {
+    #pragma omp loop bind(parallel) // expected-error{{region cannot be closely nested inside 'for' region; perhaps you forget to enclose 'omp loop' directive into a parallel region?}}
+    for (int j = 0 ; j < NNN ; j++) {
+      aaa2[i][j] = i+j;
+    }
+  }
+}
+
+void parallel_for_with_nothing() {
+  #pragma omp parallel for
+  for (int i = 0 ; i < NNN ; i++) {
+    #pragma omp nothing
+    #pragma omp loop
+    for (int j = 0 ; j < NNN ; j++) {
+      aaa2[i][j] = i+j;
+    }
+  }
+}
+
+void parallel_targetfor_with_loop_bind() {
+  #pragma omp target teams distribute parallel for
+  for (int i = 0 ; i < NNN ; i++) {
+    #pragma omp loop bind(parallel) // expected-error{{region cannot be closely nested inside 'target teams distribute parallel for' region; perhaps you forget to enclose 'omp loop' directive into a parallel region?}}
+    for (int j = 0 ; j < NNN ; j++) {
+      aaa2[i][j] = i+j;
+    }
+  }
+}
+
+void parallel_targetparallel_with_loop() {
+  #pragma omp target parallel
+  for (int i = 0 ; i < NNN ; i++) {
+    #pragma omp loop bind(parallel)
+    for (int j = 0 ; j < NNN ; j++) {
+      aaa2[i][j] = i+j;
+    }
+  }
+}
+
+void loop_bind_AND_loop_bind() {
+  #pragma omp parallel for
+  for (int i = 0; i < 100; ++i) {
+    #pragma omp loop bind(parallel) // expected-error{{region cannot be closely nested inside 'parallel for' region; perhaps you forget to enclose 'omp loop' directive into a parallel region?}}
+    for (int i = 0 ; i < NNN ; i++) {
+      #pragma omp loop bind(parallel) // expected-error{{region cannot be closely nested inside 'loop' region; perhaps you forget to enclose 'omp loop' directive into a parallel region?}}
+      for (int j = 0 ; j < NNN ; j++) {
+        aaa[j] = j*NNN;
+      }
+    }
+  }
+}
+
+void parallel_with_sections_loop() {
+  #pragma omp parallel
+  {
+     #pragma omp sections
+     {
+        for (int i = 0 ; i < NNN ; i++) {
+          #pragma omp loop bind(parallel) // expected-error{{region cannot be closely nested inside 'sections' region; perhaps you forget to enclose 'omp loop' directive into a parallel region?}}
+          for (int j = 0 ; j < NNN ; j++) {
+            aaa2[i][j] = i+j;
+          }
+        }
+
+        #pragma omp section
+	{
+          aaa[NNN-1] = NNN;
+        }
+     }
+  }
+}
+
 void teams_loop() {
   int var1, var2;
 
@@ -34,17 +120,23 @@ void teams_loop() {
    }
 }
 
-void orphan_loop_with_bind() {
-  #pragma omp loop bind(parallel) 
-  for (int j = 0 ; j < NNN ; j++) {
-    aaa[j] = j*NNN;
+void teams_targetteams_with_loop() {
+  #pragma omp target teams
+  for (int i = 0 ; i < NNN ; i++) {
+    #pragma omp loop bind(teams)
+    for (int j = 0 ; j < NNN ; j++) {
+      aaa2[i][j] = i+j;
+    }
   }
 }
 
-void orphan_loop_no_bind() {
-  #pragma omp loop  // expected-error{{expected 'bind' clause for 'loop' construct without an enclosing OpenMP construct}}
-  for (int j = 0 ; j < NNN ; j++) {
-    aaa[j] = j*NNN;
+void teams_targetfor_with_loop_bind() {
+  #pragma omp target teams distribute parallel for
+  for (int i = 0 ; i < NNN ; i++) {
+    #pragma omp loop bind(teams) // expected-error{{region cannot be closely nested inside 'target teams distribute parallel for' region; perhaps you forget to enclose 'omp loop' directive into a teams region?}}
+    for (int j = 0 ; j < NNN ; j++) {
+      aaa2[i][j] = i+j;
+    }
   }
 }
 
@@ -65,12 +157,80 @@ void teams_loop_reduction() {
    }
 }
 
+void teams_loop_distribute() {
+  int total = 0;
+
+  #pragma omp teams num_teams(8) thread_limit(256)
+  #pragma omp distribute parallel for dist_schedule(static, 1024) \
+                                      schedule(static, 64)
+  for (int i = 0; i < NNN; i++) {
+    #pragma omp loop bind(teams) // expected-error{{'distribute parallel for' region; perhaps you forget to enclose 'omp loop' directive into a teams region?}}
+    for (int j = 0; j < NNN; j++) {
+      aaa2[i][j] = i+j;
+    }
+  }
+}
+
+void parallel_for_with_loop_teams_bind(){
+  #pragma omp parallel for
+  for (int i = 0; i < NNN; i++) {
+    #pragma omp loop bind(teams) // expected-error{{region cannot be closely nested inside 'parallel for' region; perhaps you forget to enclose 'omp loop' directive into a teams region?}}
+    for (int j = 0 ; j < NNN ; j++) {
+      aaa[i] = i+i*NNN;
+    }
+  }
+}
+
+void teams_with_loop_thread_bind(){
+  #pragma omp teams
+  for (int i = 0; i < NNN; i++) {
+    #pragma omp loop bind(thread)
+    for (int j = 0 ; j < NNN ; j++) {
+      aaa[i] = i+i*NNN;
+    }
+  }
+}
+
+void orphan_loop_no_bind() {
+  #pragma omp loop  // expected-error{{expected 'bind' clause for 'loop' construct without an enclosing OpenMP construct}}
+  for (int j = 0 ; j < NNN ; j++) {
+    aaa[j] = j*NNN;
+  }
+}
+
+void orphan_loop_parallel_bind() {
+  #pragma omp loop bind(parallel)
+  for (int j = 0 ; j < NNN ; j++) {
+    aaa[j] = j*NNN;
+  }
+}
+
+void orphan_loop_teams_bind(){
+  #pragma omp loop bind(teams)
+  for (int i = 0; i < NNN; i++) {
+    aaa[i] = i+i*NNN;
+  }
+}
+
 int main(int argc, char *argv[]) {
   parallel_loop();
+  parallel_for_AND_loop_bind();
+  parallel_nowait();
+  parallel_for_with_nothing();
+  parallel_targetfor_with_loop_bind();
+  parallel_targetparallel_with_loop();
+  loop_bind_AND_loop_bind();
+  parallel_with_sections_loop();
   teams_loop();
-  orphan_loop_with_bind();
-  orphan_loop_no_bind();
+  teams_targetteams_with_loop();
+  teams_targetfor_with_loop_bind();
   teams_loop_reduction();
+  teams_loop_distribute();
+  parallel_for_with_loop_teams_bind();
+  teams_with_loop_thread_bind();
+  orphan_loop_no_bind();
+  orphan_loop_parallel_bind();
+  orphan_loop_teams_bind();
 }
 
 #endif
diff --git a/clang/test/PCH/pragma-loop.cpp b/clang/test/PCH/pragma-loop.cpp
index f5de630ffc9120..a3c6871041c0ee 100644
--- a/clang/test/PCH/pragma-loop.cpp
+++ b/clang/test/PCH/pragma-loop.cpp
@@ -116,9 +116,13 @@ class pragma_test {
 
   inline void run10(int *List, int Length) {
     int i = 0;
-#pragma omp loop bind(teams)
+    int j = 0;
+    #pragma omp teams
     for (int i = 0; i < Length; i++) {
-      List[i] = i;
+      #pragma omp loop bind(teams)
+      for (int j = 0; j < Length; j++) {
+        List[i] = i+j;
+      }
     }
   }
 

>From 1881663545cf9704095e2400015e97970cc11f7b Mon Sep 17 00:00:00 2001
From: Sunil Kuravinakop <kuravina at pe28vega.us.cray.com>
Date: Thu, 4 Jan 2024 10:53:16 -0600
Subject: [PATCH 2/4] Removing extra paranthesis. Removing an empty line which
 was added to seperate a function from another. This was the only change in
 the file and hence will be adding it as a separate NFC patch as suggested by
 Alexey.

 Changes to be committed:
	modified:   clang/include/clang/Sema/Sema.h
	modified:   clang/lib/Sema/SemaOpenMP.cpp
---
 clang/include/clang/Sema/Sema.h | 1 -
 clang/lib/Sema/SemaOpenMP.cpp   | 4 ++--
 2 files changed, 2 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 3d8d94d200ac58..72728c0b6d3c55 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -11335,7 +11335,6 @@ class Sema final {
   /// on the parameter of the bind clause. In the methods for the
   /// mapped directives, check the parameters of the lastprivate clause.
   bool checkLastPrivateForMappedDirectives(ArrayRef<OMPClause *> Clauses);
-
   /// Depending on the bind clause of OMPD_loop map the directive to new
   /// directives.
   ///    1) loop bind(parallel) --> OMPD_for
diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 6e060e5b8ee268..bb40cc36b1ab38 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -5075,7 +5075,7 @@ static bool checkNestingOfRegions(Sema &SemaRef, const DSAStackTy *Stack,
     // Checks needed for mapping "loop" construct. Please check mapLoopConstruct
     // for a detailed explanation
     if (SemaRef.LangOpts.OpenMP >= 50 && CurrentRegion == OMPD_loop &&
-        ((BindKind == OMPC_BIND_parallel) || (BindKind == OMPC_BIND_teams)) &&
+        (BindKind == OMPC_BIND_parallel || BindKind == OMPC_BIND_teams) &&
         (isOpenMPWorksharingDirective(ParentRegion) ||
          ParentRegion == OMPD_loop)) {
       int ErrorMsgNumber = (BindKind == OMPC_BIND_parallel) ? 1 : 4;
@@ -6236,7 +6236,7 @@ StmtResult Sema::ActOnOpenMPExecutableDirective(
   // Variable used to note down the DirectiveKind because mapLoopConstruct may
   // change "Kind" variable, due to mapping of "omp loop" to other directives.
   OpenMPDirectiveKind DK = Kind;
-  if ((Kind == OMPD_loop) || (PrevMappedDirective == OMPD_loop)) {
+  if (Kind == OMPD_loop || PrevMappedDirective == OMPD_loop) {
     UseClausesWithoutBind = mapLoopConstruct(
         ClausesWithoutBind, Clauses, BindKind, Kind, PrevMappedDirective,
         StartLoc, EndLoc, DirName, CancelRegion);

>From 835bca8f78ad1d44e906a71db09a677f4a6cadee Mon Sep 17 00:00:00 2001
From: Sunil Kuravinakop <kuravina at pe28vega.us.cray.com>
Date: Mon, 8 Jan 2024 04:36:37 -0600
Subject: [PATCH 3/4] In case of unspecified bind in "#pragma omp loop" and
 parent being "teams" or "target teams", I had modified the if condition to
 use "isOpenMPTeamsDirective()" for the checks. However, this proved too
 restrictive and hence removed the check. Now, on Alexey pointing out the
 error, re-introduced the check to use "teams" or "target teams". In the test
 case loop_bind_messages.cpp, Combined multiple tests into fewer functions.

  Changes to be committed:
 	modified:   clang/lib/Sema/SemaOpenMP.cpp
 	modified:   clang/test/OpenMP/loop_bind_messages.cpp
---
 clang/lib/Sema/SemaOpenMP.cpp            |  3 +
 clang/test/OpenMP/loop_bind_messages.cpp | 99 +++++++++++-------------
 2 files changed, 50 insertions(+), 52 deletions(-)

diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index bb40cc36b1ab38..8da4da7e5517d0 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -6161,6 +6161,9 @@ bool Sema::mapLoopConstruct(llvm::SmallVector<OMPClause *> &ClausesWithoutBind,
       } else if (ParentDirective == OMPD_parallel ||
                  ParentDirective == OMPD_target_parallel)
         BindKind = OMPC_BIND_parallel;
+    } else if (ParentDirective == OMPD_teams ||
+               ParentDirective == OMPD_target_teams) {
+      BindKind = OMPC_BIND_teams;
     } else {
       // bind clause is present in loop directive. When the loop directive is
       // changed to a new directive the bind clause is not used. So, we should
diff --git a/clang/test/OpenMP/loop_bind_messages.cpp b/clang/test/OpenMP/loop_bind_messages.cpp
index 9492a29ba69498..becd1f40c0c047 100644
--- a/clang/test/OpenMP/loop_bind_messages.cpp
+++ b/clang/test/OpenMP/loop_bind_messages.cpp
@@ -14,9 +14,7 @@ void parallel_loop() {
        aaa[j] = j*NNN;
      }
    }
-}
 
-void parallel_for_AND_loop_bind() {
   #pragma omp parallel for
   for (int i = 0 ; i < NNN ; i++) {
     #pragma omp loop bind(parallel) // expected-error{{region cannot be closely nested inside 'parallel for' region; perhaps you forget to enclose 'omp loop' directive into a parallel region?}}
@@ -24,9 +22,7 @@ void parallel_for_AND_loop_bind() {
       aaa2[i][j] = i+j;
     }
   }
-}
 
-void parallel_nowait() {
   #pragma omp parallel
   #pragma omp for nowait
   for (int i = 0 ; i < NNN ; i++) {
@@ -35,9 +31,7 @@ void parallel_nowait() {
       aaa2[i][j] = i+j;
     }
   }
-}
 
-void parallel_for_with_nothing() {
   #pragma omp parallel for
   for (int i = 0 ; i < NNN ; i++) {
     #pragma omp nothing
@@ -46,9 +40,7 @@ void parallel_for_with_nothing() {
       aaa2[i][j] = i+j;
     }
   }
-}
 
-void parallel_targetfor_with_loop_bind() {
   #pragma omp target teams distribute parallel for
   for (int i = 0 ; i < NNN ; i++) {
     #pragma omp loop bind(parallel) // expected-error{{region cannot be closely nested inside 'target teams distribute parallel for' region; perhaps you forget to enclose 'omp loop' directive into a parallel region?}}
@@ -56,9 +48,7 @@ void parallel_targetfor_with_loop_bind() {
       aaa2[i][j] = i+j;
     }
   }
-}
 
-void parallel_targetparallel_with_loop() {
   #pragma omp target parallel
   for (int i = 0 ; i < NNN ; i++) {
     #pragma omp loop bind(parallel)
@@ -66,9 +56,7 @@ void parallel_targetparallel_with_loop() {
       aaa2[i][j] = i+j;
     }
   }
-}
 
-void loop_bind_AND_loop_bind() {
   #pragma omp parallel for
   for (int i = 0; i < 100; ++i) {
     #pragma omp loop bind(parallel) // expected-error{{region cannot be closely nested inside 'parallel for' region; perhaps you forget to enclose 'omp loop' directive into a parallel region?}}
@@ -79,9 +67,7 @@ void loop_bind_AND_loop_bind() {
       }
     }
   }
-}
 
-void parallel_with_sections_loop() {
   #pragma omp parallel
   {
      #pragma omp sections
@@ -102,7 +88,8 @@ void parallel_with_sections_loop() {
 }
 
 void teams_loop() {
-  int var1, var2;
+  int var1;
+  int total = 0;
 
   #pragma omp teams
   {
@@ -118,9 +105,7 @@ void teams_loop() {
        }
      }
    }
-}
 
-void teams_targetteams_with_loop() {
   #pragma omp target teams
   for (int i = 0 ; i < NNN ; i++) {
     #pragma omp loop bind(teams)
@@ -128,9 +113,7 @@ void teams_targetteams_with_loop() {
       aaa2[i][j] = i+j;
     }
   }
-}
 
-void teams_targetfor_with_loop_bind() {
   #pragma omp target teams distribute parallel for
   for (int i = 0 ; i < NNN ; i++) {
     #pragma omp loop bind(teams) // expected-error{{region cannot be closely nested inside 'target teams distribute parallel for' region; perhaps you forget to enclose 'omp loop' directive into a teams region?}}
@@ -138,10 +121,6 @@ void teams_targetfor_with_loop_bind() {
       aaa2[i][j] = i+j;
     }
   }
-}
-
-void teams_loop_reduction() {
-  int total = 0;
 
   #pragma omp teams
   {
@@ -155,10 +134,6 @@ void teams_loop_reduction() {
        total+=aaa[j];
      }
    }
-}
-
-void teams_loop_distribute() {
-  int total = 0;
 
   #pragma omp teams num_teams(8) thread_limit(256)
   #pragma omp distribute parallel for dist_schedule(static, 1024) \
@@ -169,19 +144,41 @@ void teams_loop_distribute() {
       aaa2[i][j] = i+j;
     }
   }
-}
 
-void parallel_for_with_loop_teams_bind(){
-  #pragma omp parallel for
+  #pragma omp teams
   for (int i = 0; i < NNN; i++) {
-    #pragma omp loop bind(teams) // expected-error{{region cannot be closely nested inside 'parallel for' region; perhaps you forget to enclose 'omp loop' directive into a teams region?}}
+    #pragma omp loop bind(thread)
+    for (int j = 0 ; j < NNN ; j++) {
+      aaa[i] = i+i*NNN;
+    }
+  }
+
+  #pragma omp teams loop
+  for (int i = 0; i < NNN; i++) {
+    #pragma omp loop
+    for (int j = 0 ; j < NNN ; j++) {
+      aaa[i] = i+i*NNN;
+    }
+  }
+
+  #pragma omp teams loop
+  for (int i = 0; i < NNN; i++) {
+    #pragma omp loop bind(teams) // expected-error{{region cannot be closely nested inside 'teams loop' region; perhaps you forget to enclose 'omp loop' directive into a teams region?}}
     for (int j = 0 ; j < NNN ; j++) {
       aaa[i] = i+i*NNN;
     }
   }
 }
 
-void teams_with_loop_thread_bind(){
+void thread_loop() {
+  #pragma omp parallel
+  for (int i = 0; i < NNN; i++) {
+    #pragma omp loop bind(thread)
+    for (int j = 0 ; j < NNN ; j++) {
+      aaa[i] = i+i*NNN;
+    }
+  }
+
   #pragma omp teams
   for (int i = 0; i < NNN; i++) {
     #pragma omp loop bind(thread)
@@ -190,47 +187,45 @@ void teams_with_loop_thread_bind(){
     }
   }
 }
+  
+void parallel_for_with_loop_teams_bind(){
+  #pragma omp parallel for
+  for (int i = 0; i < NNN; i++) {
+    #pragma omp loop bind(teams) // expected-error{{region cannot be closely nested inside 'parallel for' region; perhaps you forget to enclose 'omp loop' directive into a teams region?}}
+    for (int j = 0 ; j < NNN ; j++) {
+      aaa[i] = i+i*NNN;
+    }
+  }
+}
 
-void orphan_loop_no_bind() {
+void orphan_loops() {
   #pragma omp loop  // expected-error{{expected 'bind' clause for 'loop' construct without an enclosing OpenMP construct}}
   for (int j = 0 ; j < NNN ; j++) {
     aaa[j] = j*NNN;
   }
-}
 
-void orphan_loop_parallel_bind() {
   #pragma omp loop bind(parallel)
   for (int j = 0 ; j < NNN ; j++) {
     aaa[j] = j*NNN;
   }
-}
 
-void orphan_loop_teams_bind(){
   #pragma omp loop bind(teams)
   for (int i = 0; i < NNN; i++) {
     aaa[i] = i+i*NNN;
   }
+
+  #pragma omp loop bind(thread)
+  for (int i = 0; i < NNN; i++) {
+    aaa[i] = i+i*NNN;
+  }
 }
 
 int main(int argc, char *argv[]) {
   parallel_loop();
-  parallel_for_AND_loop_bind();
-  parallel_nowait();
-  parallel_for_with_nothing();
-  parallel_targetfor_with_loop_bind();
-  parallel_targetparallel_with_loop();
-  loop_bind_AND_loop_bind();
-  parallel_with_sections_loop();
   teams_loop();
-  teams_targetteams_with_loop();
-  teams_targetfor_with_loop_bind();
-  teams_loop_reduction();
-  teams_loop_distribute();
+  thread_loop();
   parallel_for_with_loop_teams_bind();
-  teams_with_loop_thread_bind();
-  orphan_loop_no_bind();
-  orphan_loop_parallel_bind();
-  orphan_loop_teams_bind();
+  orphan_loops();
 }
 
 #endif

>From 8f9c1e3419902bb2927b893568efe27f950393f0 Mon Sep 17 00:00:00 2001
From: Sunil Kuravinakop <kuravina at pe28vega.us.cray.com>
Date: Mon, 8 Jan 2024 12:22:16 -0600
Subject: [PATCH 4/4] Correcting the brackets related to teams in SemaOpenMP.
 Changes to testcase to reflect the changed code.

  Changes to be committed:
	modified:   clang/lib/Sema/SemaOpenMP.cpp
 	modified:   clang/test/OpenMP/generic_loop_ast_print.cpp
---
 clang/lib/Sema/SemaOpenMP.cpp                | 9 +++++----
 clang/test/OpenMP/generic_loop_ast_print.cpp | 4 ++--
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Sema/SemaOpenMP.cpp b/clang/lib/Sema/SemaOpenMP.cpp
index 8da4da7e5517d0..365032c9642123 100644
--- a/clang/lib/Sema/SemaOpenMP.cpp
+++ b/clang/lib/Sema/SemaOpenMP.cpp
@@ -6159,11 +6159,12 @@ bool Sema::mapLoopConstruct(llvm::SmallVector<OMPClause *> &ClausesWithoutBind,
         Diag(DSAStack->getDefaultDSALocation(),
              diag::err_omp_bind_required_on_loop);
       } else if (ParentDirective == OMPD_parallel ||
-                 ParentDirective == OMPD_target_parallel)
+                 ParentDirective == OMPD_target_parallel) {
         BindKind = OMPC_BIND_parallel;
-    } else if (ParentDirective == OMPD_teams ||
-               ParentDirective == OMPD_target_teams) {
-      BindKind = OMPC_BIND_teams;
+      } else if (ParentDirective == OMPD_teams ||
+                 ParentDirective == OMPD_target_teams) {
+        BindKind = OMPC_BIND_teams;
+      }
     } else {
       // bind clause is present in loop directive. When the loop directive is
       // changed to a new directive the bind clause is not used. So, we should
diff --git a/clang/test/OpenMP/generic_loop_ast_print.cpp b/clang/test/OpenMP/generic_loop_ast_print.cpp
index 30ade214289188..df806405571cf7 100644
--- a/clang/test/OpenMP/generic_loop_ast_print.cpp
+++ b/clang/test/OpenMP/generic_loop_ast_print.cpp
@@ -95,11 +95,11 @@ void test() {
   }
 
   //PRINT: #pragma omp target teams
-  //PRINT: #pragma omp simd
+  //PRINT: #pragma omp distribute
   //DUMP: OMPTargetTeamsDirective
   //DUMP: CapturedStmt
   //DUMP: ForStmt
-  //DUMP: OMPSimdDirective
+  //DUMP: OMPDistributeDirective
   #pragma omp target teams
   for (int i=0; i<1000; ++i) {
     #pragma omp loop



More information about the cfe-commits mailing list