[flang-commits] [flang] 5789bcc - [Flang][OpenMP] Fix when the toplevel unit is present in declare target

Kiran Chandramohan via flang-commits flang-commits at lists.llvm.org
Tue Feb 21 08:02:11 PST 2023


Author: Kiran Chandramohan
Date: 2023-02-21T16:01:58Z
New Revision: 5789bcc92e93ae54ce9253c12f2b2850cc3501dd

URL: https://github.com/llvm/llvm-project/commit/5789bcc92e93ae54ce9253c12f2b2850cc3501dd
DIFF: https://github.com/llvm/llvm-project/commit/5789bcc92e93ae54ce9253c12f2b2850cc3501dd.diff

LOG: [Flang][OpenMP] Fix when the toplevel unit is present in declare target

If the toplevel unit is present in a declare target construct, do not
try to again find the enclosing construct.
Patch also re-arranges the checks.

Reviewed By: peixin

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

Added: 
    

Modified: 
    flang/lib/Semantics/check-omp-structure.cpp
    flang/test/Semantics/OpenMP/omp-declare-target03.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index 2c77467a455ec..c29e9a9502104 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -963,20 +963,7 @@ void OmpStructureChecker::CheckThreadprivateOrDeclareTargetVar(
         common::visitors{
             [&](const parser::Designator &) {
               if (const auto *name{parser::Unwrap<parser::Name>(ompObject)}) {
-                const auto &useScope{
-                    context_.FindScope(GetContext().directiveSource)};
-                const auto &declScope{
-                    GetProgramUnitContaining(name->symbol->GetUltimate())};
-                const auto *sym =
-                    declScope.parent().FindSymbol(name->symbol->name());
-                if (sym &&
-                    (sym->has<MainProgramDetails>() ||
-                        sym->has<ModuleDetails>())) {
-                  context_.Say(name->source,
-                      "The module name or main program name cannot be in a %s "
-                      "directive"_err_en_US,
-                      ContextDirectiveAsFortran());
-                } else if (name->symbol->GetUltimate().IsSubprogram()) {
+                if (name->symbol->GetUltimate().IsSubprogram()) {
                   if (GetContext().directive ==
                       llvm::omp::Directive::OMPD_threadprivate)
                     context_.Say(name->source,
@@ -1002,20 +989,6 @@ void OmpStructureChecker::CheckThreadprivateOrDeclareTargetVar(
                       "A variable in a %s directive cannot be an element of a "
                       "common block"_err_en_US,
                       ContextDirectiveAsFortran());
-                } else if (!IsSave(*name->symbol) &&
-                    declScope.kind() != Scope::Kind::MainProgram &&
-                    declScope.kind() != Scope::Kind::Module) {
-                  context_.Say(name->source,
-                      "A variable that appears in a %s directive must be "
-                      "declared in the scope of a module or have the SAVE "
-                      "attribute, either explicitly or implicitly"_err_en_US,
-                      ContextDirectiveAsFortran());
-                } else if (useScope != declScope) {
-                  context_.Say(name->source,
-                      "The %s directive and the common block or variable in it "
-                      "must appear in the same declaration section of a "
-                      "scoping unit"_err_en_US,
-                      ContextDirectiveAsFortran());
                 } else if (FindEquivalenceSet(*name->symbol)) {
                   context_.Say(name->source,
                       "A variable in a %s directive cannot appear in an "
@@ -1028,6 +1001,40 @@ void OmpStructureChecker::CheckThreadprivateOrDeclareTargetVar(
                       "A THREADPRIVATE variable cannot appear in a %s "
                       "directive"_err_en_US,
                       ContextDirectiveAsFortran());
+                } else {
+                  const semantics::Scope &useScope{
+                      context_.FindScope(GetContext().directiveSource)};
+                  const semantics::Scope &curScope =
+                      name->symbol->GetUltimate().owner();
+                  if (!curScope.IsTopLevel()) {
+                    const semantics::Scope &declScope =
+                        GetProgramUnitContaining(curScope);
+                    const semantics::Symbol *sym{
+                        declScope.parent().FindSymbol(name->symbol->name())};
+                    if (sym &&
+                        (sym->has<MainProgramDetails>() ||
+                            sym->has<ModuleDetails>())) {
+                      context_.Say(name->source,
+                          "The module name or main program name cannot be in a "
+                          "%s "
+                          "directive"_err_en_US,
+                          ContextDirectiveAsFortran());
+                    } else if (!IsSave(*name->symbol) &&
+                        declScope.kind() != Scope::Kind::MainProgram &&
+                        declScope.kind() != Scope::Kind::Module) {
+                      context_.Say(name->source,
+                          "A variable that appears in a %s directive must be "
+                          "declared in the scope of a module or have the SAVE "
+                          "attribute, either explicitly or implicitly"_err_en_US,
+                          ContextDirectiveAsFortran());
+                    } else if (useScope != declScope) {
+                      context_.Say(name->source,
+                          "The %s directive and the common block or variable "
+                          "in it must appear in the same declaration section "
+                          "of a scoping unit"_err_en_US,
+                          ContextDirectiveAsFortran());
+                    }
+                  }
                 }
               }
             },

diff  --git a/flang/test/Semantics/OpenMP/omp-declare-target03.f90 b/flang/test/Semantics/OpenMP/omp-declare-target03.f90
index c437a3e7bccc0..bfc20049963c1 100644
--- a/flang/test/Semantics/OpenMP/omp-declare-target03.f90
+++ b/flang/test/Semantics/OpenMP/omp-declare-target03.f90
@@ -6,6 +6,10 @@
 module mod1
 end
 
+subroutine bar
+  !$omp declare target (bar)
+end subroutine
+
 program main
   use mod1
 


        


More information about the flang-commits mailing list