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

Michael Klemm via flang-commits flang-commits at lists.llvm.org
Mon Jan 19 06:38:41 PST 2026


https://github.com/mjklemm updated https://github.com/llvm/llvm-project/pull/176337

>From ed5f9644a653b66ffde92c9a9118e9ba9dbefd08 Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Thu, 15 Jan 2026 18:58:05 +0100
Subject: [PATCH 1/2] [Flang][OpenMP] Permit EXTERNAL with DECLARE TARGET

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.
---
 flang/lib/Semantics/check-omp-structure.cpp      |  3 ++-
 flang/test/Semantics/OpenMP/declare-target08.f90 | 11 +++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

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..e5010758e68c9 100644
--- a/flang/test/Semantics/OpenMP/declare-target08.f90
+++ b/flang/test/Semantics/OpenMP/declare-target08.f90
@@ -13,12 +13,23 @@ 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)
+end subroutine importer
+
 program main
 real a
+external ext_routine
 !CHECK: bar (Subroutine, OmpDeclareTarget): HostAssoc
 !CHECK: baz (Function, OmpDeclareTarget): HostAssoc
+!CHECK: ext_routine, EXTERNAL (OmpDeclareTarget): ProcEntity
 !$omp declare target(bar)
 !$omp declare target(baz)
+!$omp declare target(ext_routine)
 
 a = baz(a)
 call bar(2,a)

>From f04ad1ffab37bd9b65ffda14be77253bf31b435a Mon Sep 17 00:00:00 2001
From: Michael Klemm <michael.klemm at amd.com>
Date: Mon, 19 Jan 2026 15:38:18 +0100
Subject: [PATCH 2/2] Update test to cover all new cases

---
 flang/test/Semantics/OpenMP/declare-target08.f90 | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/flang/test/Semantics/OpenMP/declare-target08.f90 b/flang/test/Semantics/OpenMP/declare-target08.f90
index e5010758e68c9..91598d9528ae9 100644
--- a/flang/test/Semantics/OpenMP/declare-target08.f90
+++ b/flang/test/Semantics/OpenMP/declare-target08.f90
@@ -19,16 +19,22 @@ subroutine importer()
     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)



More information about the flang-commits mailing list