[flang-commits] [flang] 28fde68 - [Flang] - Enhance testing for strictly-nested teams in target regions. (#168437)

via flang-commits flang-commits at lists.llvm.org
Mon Nov 24 21:36:46 PST 2025


Author: Pranav Bhandarkar
Date: 2025-11-24T23:36:41-06:00
New Revision: 28fde68501032b292f91246c0e79872558d0e74b

URL: https://github.com/llvm/llvm-project/commit/28fde68501032b292f91246c0e79872558d0e74b
DIFF: https://github.com/llvm/llvm-project/commit/28fde68501032b292f91246c0e79872558d0e74b.diff

LOG: [Flang] - Enhance testing for strictly-nested teams in target regions. (#168437)

This patch enhances the semantics test for checking that teams
directives are strictly nested inside target directives.

Fixes https://github.com/llvm/llvm-project/issues/153173

Added: 
    flang/test/Semantics/OpenMP/target-teams-nesting.f90

Modified: 
    flang/lib/Semantics/check-omp-loop.cpp
    flang/lib/Semantics/check-omp-structure.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/check-omp-loop.cpp b/flang/lib/Semantics/check-omp-loop.cpp
index ef237a01d0f7a..9a78209369949 100644
--- a/flang/lib/Semantics/check-omp-loop.cpp
+++ b/flang/lib/Semantics/check-omp-loop.cpp
@@ -335,6 +335,15 @@ void OmpStructureChecker::Enter(const parser::OpenMPLoopConstruct &x) {
     EnterDirectiveNest(SIMDNest);
   }
 
+  if (CurrentDirectiveIsNested() &&
+      llvm::omp::topTeamsSet.test(GetContext().directive) &&
+      GetContextParent().directive == llvm::omp::Directive::OMPD_target &&
+      !GetDirectiveNest(TargetBlockOnlyTeams)) {
+    context_.Say(GetContextParent().directiveSource,
+        "TARGET construct with nested TEAMS region contains statements or "
+        "directives outside of the TEAMS construct"_err_en_US);
+  }
+
   // Combined target loop constructs are target device constructs. Keep track of
   // whether any such construct has been visited to later check that REQUIRES
   // directives for target-related options don't appear after them.

diff  --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index b2cc86a5260b4..f597eaa4711dc 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -5217,6 +5217,13 @@ bool OmpStructureChecker::CheckTargetBlockOnlyTeams(
         if (dirId == llvm::omp::Directive::OMPD_teams) {
           nestedTeams = true;
         }
+      } else if (const auto *ompLoopConstruct{
+                     std::get_if<parser::OpenMPLoopConstruct>(
+                         &ompConstruct->u)}) {
+        llvm::omp::Directive dirId{ompLoopConstruct->BeginDir().DirId()};
+        if (llvm::omp::topTeamsSet.test(dirId)) {
+          nestedTeams = true;
+        }
       }
     }
 

diff  --git a/flang/test/Semantics/OpenMP/target-teams-nesting.f90 b/flang/test/Semantics/OpenMP/target-teams-nesting.f90
new file mode 100644
index 0000000000000..c1760afdd7752
--- /dev/null
+++ b/flang/test/Semantics/OpenMP/target-teams-nesting.f90
@@ -0,0 +1,20 @@
+! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp
+
+program main
+  implicit none
+  integer, parameter :: n = 100
+  integer, parameter :: expected = n+2
+  integer :: i
+  integer :: counter
+
+  counter = 0
+  !ERROR: TARGET construct with nested TEAMS region contains statements or directives outside of the TEAMS construct
+  !$omp target map(tofrom:counter)
+  counter = counter+1
+  !$omp teams distribute reduction(+:counter)
+  do i=1, n
+     counter = counter+1
+  end do
+  counter = counter+1
+  !$omp end target
+ end program


        


More information about the flang-commits mailing list