[Flang][OpenMP] Fix a corner case where target region is empty
authorKiran Chandramohan <kiran.chandramohan@arm.com>
Thu, 16 Feb 2023 09:23:16 +0000 (09:23 +0000)
committerKiran Chandramohan <kiran.chandramohan@arm.com>
Thu, 16 Feb 2023 11:07:47 +0000 (11:07 +0000)
Reviewed By: psoni2628, raghavendhra

Differential Revision: https://reviews.llvm.org/D144110

flang/lib/Semantics/check-omp-structure.cpp
flang/test/Semantics/OpenMP/omp-target.f90 [new file with mode: 0644]

index 98b367d..2c77467 100644 (file)
@@ -2634,24 +2634,28 @@ void OmpStructureChecker::CheckPrivateSymbolsInOuterCxt(
 bool OmpStructureChecker::CheckTargetBlockOnlyTeams(
     const parser::Block &block) {
   bool nestedTeams{false};
-  auto it{block.begin()};
-
-  if (const auto *ompConstruct{parser::Unwrap<parser::OpenMPConstruct>(*it)}) {
-    if (const auto *ompBlockConstruct{
-            std::get_if<parser::OpenMPBlockConstruct>(&ompConstruct->u)}) {
-      const auto &beginBlockDir{
-          std::get<parser::OmpBeginBlockDirective>(ompBlockConstruct->t)};
-      const auto &beginDir{
-          std::get<parser::OmpBlockDirective>(beginBlockDir.t)};
-      if (beginDir.v == llvm::omp::Directive::OMPD_teams) {
-        nestedTeams = true;
+
+  if (!block.empty()) {
+    auto it{block.begin()};
+    if (const auto *ompConstruct{
+            parser::Unwrap<parser::OpenMPConstruct>(*it)}) {
+      if (const auto *ompBlockConstruct{
+              std::get_if<parser::OpenMPBlockConstruct>(&ompConstruct->u)}) {
+        const auto &beginBlockDir{
+            std::get<parser::OmpBeginBlockDirective>(ompBlockConstruct->t)};
+        const auto &beginDir{
+            std::get<parser::OmpBlockDirective>(beginBlockDir.t)};
+        if (beginDir.v == llvm::omp::Directive::OMPD_teams) {
+          nestedTeams = true;
+        }
       }
     }
-  }
 
-  if (nestedTeams && ++it == block.end()) {
-    return true;
+    if (nestedTeams && ++it == block.end()) {
+      return true;
+    }
   }
+
   return false;
 }
 
diff --git a/flang/test/Semantics/OpenMP/omp-target.f90 b/flang/test/Semantics/OpenMP/omp-target.f90
new file mode 100644 (file)
index 0000000..994c040
--- /dev/null
@@ -0,0 +1,11 @@
+! RUN: %python %S/../test_errors.py %s %flang_fc1 -fopenmp -Werror
+
+! Corner cases in OpenMP target directives
+
+subroutine empty_target()
+  integer :: i
+
+  !$omp target map(i)
+  !$omp end target
+
+end subroutine