[flang-commits] [flang] 8ba629f - [Flang][OpenMP] Permit EXTERNAL with DECLARE TARGET (#176337)

via flang-commits flang-commits at lists.llvm.org
Mon Jan 19 07:51:56 PST 2026


Author: Michael Klemm
Date: 2026-01-19T16:51:52+01:00
New Revision: 8ba629f8c33a3a006297f8ccba5df08a253098ec

URL: https://github.com/llvm/llvm-project/commit/8ba629f8c33a3a006297f8ccba5df08a253098ec
DIFF: https://github.com/llvm/llvm-project/commit/8ba629f8c33a3a006297f8ccba5df08a253098ec.diff

LOG: [Flang][OpenMP] Permit EXTERNAL with DECLARE TARGET (#176337)

When DECLARE TARGET was used with a symbol that has the EXTERNAL
attribute, the Flang was rejecting the code as invalid. This PR fixes
this by allowing names with the EXTERNAL attribute to appear in DECLARE
TARGET directives.

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/check-omp-structure.cpp b/flang/lib/Semantics/check-omp-structure.cpp
index 23871ca04bccb..b9d1382b0c76a 100644
--- a/flang/lib/Semantics/check-omp-structure.cpp
+++ b/flang/lib/Semantics/check-omp-structure.cpp
@@ -1322,7 +1322,8 @@ void OmpStructureChecker::CheckThreadprivateOrDeclareTargetVar(
             ContextDirectiveAsFortran());
       } else if (!IsSaved(*name->symbol) &&
           declScope.kind() != Scope::Kind::MainProgram &&
-          declScope.kind() != Scope::Kind::Module) {
+          declScope.kind() != Scope::Kind::Module &&
+          !name->symbol->attrs().test(Attr::EXTERNAL)) {
         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());

diff  --git a/flang/test/Semantics/OpenMP/declare-target08.f90 b/flang/test/Semantics/OpenMP/declare-target08.f90
index 1438d79d37348..91598d9528ae9 100644
--- a/flang/test/Semantics/OpenMP/declare-target08.f90
+++ b/flang/test/Semantics/OpenMP/declare-target08.f90
@@ -13,12 +13,29 @@ function baz(a)
     baz = a
 end function baz
 
+subroutine importer()
+    external ext_routine
+    integer ext_function
+    external ext_function
+    !$omp declare target(ext_routine)
+    !$omp declare target(ext_function)
+    !CHECK: ext_function, EXTERNAL (Function, OmpDeclareTarget): ProcEntity {{.*}}
+    !CHECK: ext_routine, EXTERNAL (OmpDeclareTarget): ProcEntity
+end subroutine importer
+
 program main
 real a
+external ext_routine
+integer ext_function
+external ext_function
 !CHECK: bar (Subroutine, OmpDeclareTarget): HostAssoc
 !CHECK: baz (Function, OmpDeclareTarget): HostAssoc
+!CHECK: ext_function, EXTERNAL (Function, OmpDeclareTarget): ProcEntity {{.*}}
+!CHECK: ext_routine, EXTERNAL (OmpDeclareTarget): ProcEntity
 !$omp declare target(bar)
 !$omp declare target(baz)
+!$omp declare target(ext_function)
+!$omp declare target(ext_routine)
 
 a = baz(a)
 call bar(2,a)


        


More information about the flang-commits mailing list