See attached patch, 8/10 of the DO CONCURRENT to OpenMP series.
Start at the new gfc_trans_omp_do_concurrent at the end of
trans-openmp.cc. In a PURE, ELEMENTAL or declare target procedure,
which dc_maybe_device_proc detects, it translates the construct twice
with dc_trans_omp, the old gfc_trans_omp_do_concurrent renamed: once as
a target construct and once as with =host. It selects between them
with an OMP_TARGET_DEVICE_MATCHES cookie for device={kind(host)},
which ompdevlow resolves in each compiler.
Between the two translations, dc_reset_body_decls clears the decls that
the first gave to labels and to BLOCK and ASSOCIATE entities, along
with their charlen decls and the BLOCK's tlink. Static entities keep
theirs.
dc_device_ok loses the rule that kept these procedures on the host, and
with it DC_WHY_DEVICE_PROC. The test changes follow from that.
Regards,
Jerry
---
fortran: [PR128590]-8 Offload DO CONCURRENT in PURE procedures
A DO CONCURRENT construct in a PURE, ELEMENTAL or declare target
procedure ran on the host under -fdo-concurrent-to-openmp=device,
because the procedure may itself run on the device, where a target
construct must not be encountered. Translate such a construct twice
instead: as a target construct for where the procedure runs on the
host, and as with =host for where it runs on the device, selected by a
device={kind(host)} condition that resolves to a constant in each
compiler. The labels of the body and the automatic entities of its
BLOCK and ASSOCIATE constructs get new decls for the second translation.
Assisted-by: Claude Opus 5.5
PR fortran/128590
gcc/fortran/ChangeLog:
* invoke.texi: Document the offloading of constructs in PURE,
ELEMENTAL and declare target procedures.
* trans-openmp.cc (enum dc_why): Remove DC_WHY_DEVICE_PROC.
(dc_maybe_device_proc): New, split out of dc_device_ok.
(dc_device_ok): Accept constructs in procedures that may run on
the device.
(dc_warn): Remove DC_WHY_DEVICE_PROC.
(dc_trans_omp): Renamed from gfc_trans_omp_do_concurrent; take a
bool DEVICE.
(dc_reset_symtree, dc_reset_code_decls, dc_reset_body_decls): New.
(gfc_trans_omp_do_concurrent): In a procedure that may run on the
device, translate the construct for the device and for the host,
and select the translation by a device={kind(host)} condition.
gcc/testsuite/ChangeLog:
* gfortran.dg/gomp/do_concurrent_to_openmp_7.f90: Remove the PURE
procedure.
* gfortran.dg/gomp/do_concurrent_to_openmp_9.f90: Expect no warning
for a construct in a PURE procedure.
* gfortran.dg/gomp/do_concurrent_to_openmp_11.f90: New test.
libgomp/ChangeLog:
* testsuite/libgomp.fortran/do_concurrent_to_openmp_8.f90: New test.
>From 41a72ab3bd3b3b66ab9dcd5012d7d0499d2536c0 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Tue, 24 Nov 2026 17:23:14 -0800
Subject: [PATCH] fortran: [PR128590]-8 Offload DO CONCURRENT in PURE
procedures
A DO CONCURRENT construct in a PURE, ELEMENTAL or declare target
procedure ran on the host under -fdo-concurrent-to-openmp=device,
because the procedure may itself run on the device, where a target
construct must not be encountered. Translate such a construct twice
instead: as a target construct for where the procedure runs on the
host, and as with =host for where it runs on the device, selected by a
device={kind(host)} condition that resolves to a constant in each
compiler. The labels of the body and the automatic entities of its
BLOCK and ASSOCIATE constructs get new decls for the second translation.
Assisted-by: Claude Opus 5.5
PR fortran/128590
gcc/fortran/ChangeLog:
* invoke.texi: Document the offloading of constructs in PURE,
ELEMENTAL and declare target procedures.
* trans-openmp.cc (enum dc_why): Remove DC_WHY_DEVICE_PROC.
(dc_maybe_device_proc): New, split out of dc_device_ok.
(dc_device_ok): Accept constructs in procedures that may run on
the device.
(dc_warn): Remove DC_WHY_DEVICE_PROC.
(dc_trans_omp): Renamed from gfc_trans_omp_do_concurrent; take a
bool DEVICE.
(dc_reset_symtree, dc_reset_code_decls, dc_reset_body_decls): New.
(gfc_trans_omp_do_concurrent): In a procedure that may run on the
device, translate the construct for the device and for the host,
and select the translation by a device={kind(host)} condition.
gcc/testsuite/ChangeLog:
* gfortran.dg/gomp/do_concurrent_to_openmp_7.f90: Remove the PURE
procedure.
* gfortran.dg/gomp/do_concurrent_to_openmp_9.f90: Expect no warning
for a construct in a PURE procedure.
* gfortran.dg/gomp/do_concurrent_to_openmp_11.f90: New test.
libgomp/ChangeLog:
* testsuite/libgomp.fortran/do_concurrent_to_openmp_8.f90: New test.
---
gcc/fortran/invoke.texi | 19 +--
gcc/fortran/trans-openmp.cc | 116 ++++++++++++++----
.../gomp/do_concurrent_to_openmp_11.f90 | 42 +++++++
.../gomp/do_concurrent_to_openmp_7.f90 | 17 +--
.../gomp/do_concurrent_to_openmp_9.f90 | 2 +-
.../do_concurrent_to_openmp_8.f90 | 47 +++++++
6 files changed, 194 insertions(+), 49 deletions(-)
create mode 100644 gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_11.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_8.f90
diff --git a/gcc/fortran/invoke.texi b/gcc/fortran/invoke.texi
index 1770b507fba..b41bc320153 100644
--- a/gcc/fortran/invoke.texi
+++ b/gcc/fortran/invoke.texi
@@ -1910,15 +1910,16 @@ body defines in place, such as @code{SHARED} variables, which are mapped
@code{tofrom}, and variables that the body does not define, which are
mapped @code{to} rather than @code{tofrom}. Enclosing constructs in an
@code{!$omp target data} region keeps their arrays on the device between
-them. A construct that cannot be offloaded is translated as with
-@samp{host}; this applies to a construct in a @code{PURE},
-@code{ELEMENTAL} or @code{declare target} procedure, which may itself run
-on the device, to one that references a procedure that is not
-@code{declare target}, an assumed-size array, a pointer, a polymorphic
-variable, or a variable of derived type with allocatable or pointer
-components, and to one in which only some iterations define a variable
-of unspecified locality. A procedure containing a construct translated
-this way must not be called in a @code{target} region.
+them. A construct in a @code{PURE}, @code{ELEMENTAL} or
+@code{declare target} procedure, which may itself run on the device, is
+offloaded only when the procedure runs on the host, and otherwise runs
+as with @samp{host}. A construct that cannot be offloaded is translated
+as with @samp{host}; this applies to one that references a procedure
+that is not @code{declare target}, an assumed-size array, a pointer, a
+polymorphic variable, or a variable of derived type with allocatable or
+pointer components, and to one in which only some iterations define a
+variable of unspecified locality. Any other procedure containing an
+offloaded construct must not be called in a @code{target} region.
@end table
The standard permits the iterations of a @code{DO CONCURRENT} construct
diff --git a/gcc/fortran/trans-openmp.cc b/gcc/fortran/trans-openmp.cc
index 8c5cde57faa..f819e48804c 100644
--- a/gcc/fortran/trans-openmp.cc
+++ b/gcc/fortran/trans-openmp.cc
@@ -11031,7 +11031,6 @@ enum dc_why
DC_WHY_SHARED_PART,
DC_WHY_REF_BEFORE_DEF,
DC_WHY_NOT_PRIVATIZABLE,
- DC_WHY_DEVICE_PROC,
DC_WHY_DEVICE_CALL,
DC_WHY_DEVICE_VAR,
DC_WHY_DEVICE_CONDITIONAL
@@ -11727,16 +11726,12 @@ dc_scan_body (gfc_code *code, dc_scan *scan)
}
-/* True if the DO CONCURRENT construct whose body SCAN describes can be
- offloaded. A procedure that is PURE, ELEMENTAL or declare target may
- itself run on the device, where a nested target construct does not run
- its body (libgomp/config/gcn/target.c), so constructs in it run on the
- host; and a procedure that the body calls must be declare target, to be
- available on the device. Conditional lastprivate is not allowed on
- DISTRIBUTE. */
+/* The procedure containing the current construct if it is PURE,
+ ELEMENTAL or declare target, so that it may itself run on the device,
+ or NULL. */
-static bool
-dc_device_ok (dc_scan *scan)
+static gfc_symbol *
+dc_maybe_device_proc (void)
{
gfc_symbol *proc = NULL;
for (gfc_namespace *ns = gfc_current_ns; ns && !proc; ns = ns->parent)
@@ -11746,13 +11741,19 @@ dc_device_ok (dc_scan *scan)
if (proc
&& (proc->attr.pure || proc->attr.implicit_pure
|| proc->attr.elemental || proc->attr.omp_declare_target))
- {
- scan->device_why.why = DC_WHY_NONE;
- dc_note (&scan->device_why, DC_WHY_DEVICE_PROC, proc,
- &scan->dc->loc);
- return false;
- }
+ return proc;
+ return NULL;
+}
+
+
+/* True if the DO CONCURRENT construct whose body SCAN describes can be
+ offloaded. A procedure that the body calls must be declare target, to
+ be available on the device. Conditional lastprivate is not allowed on
+ DISTRIBUTE. */
+static bool
+dc_device_ok (dc_scan *scan)
+{
if (scan->device_bad || scan->host_calls || scan->internal_calls)
return false;
@@ -11825,11 +11826,6 @@ dc_warn (dc_reason *r, bool device)
"given a private copy, so it stays serial; consider a "
"LOCAL locality spec", &r->where, name);
break;
- case DC_WHY_DEVICE_PROC:
- gfc_warning (opt, "DO CONCURRENT at %L runs on the host because "
- "procedure %qs may itself run on the device", &r->where,
- name);
- break;
case DC_WHY_DEVICE_CALL:
if (name)
gfc_warning (opt, "Reference to %qs at %L keeps DO CONCURRENT on "
@@ -12258,10 +12254,10 @@ dc_has_iterations (gfc_code *code, stmtblock_t *block)
/* Translate the DO CONCURRENT construct CODE into an OpenMP loop on the
- host, or on a device if KIND is GFC_DC_OMP_DEVICE. */
+ host, or on a device if DEVICE. */
-tree
-gfc_trans_omp_do_concurrent (gfc_code *code, enum gfc_dc_to_omp kind)
+static tree
+dc_trans_omp (gfc_code *code, bool device)
{
gfc_forall_iterator *fa;
gfc_code *par, *iter, *inner = NULL, *first = NULL;
@@ -12332,7 +12328,6 @@ gfc_trans_omp_do_concurrent (gfc_code *code, enum gfc_dc_to_omp kind)
/* 'parallel do simd': the SIMD part keeps the vectorization that the
serial lowering gets from its implicit IVDEP annotation. On a
device, the same under 'target teams distribute'. */
- bool device = kind == GFC_DC_OMP_DEVICE;
gfc_exec_op op = (device ? EXEC_OMP_TARGET_TEAMS_DISTRIBUTE_PARALLEL_DO_SIMD
: EXEC_OMP_PARALLEL_DO_SIMD);
@@ -12380,3 +12375,74 @@ gfc_trans_omp_do_concurrent (gfc_code *code, enum gfc_dc_to_omp kind)
return gfc_finish_block (&block);
}
+
+
+static void
+dc_reset_symtree (gfc_symtree *st, gfc_namespace *ns)
+{
+ if (!st)
+ return;
+ dc_reset_symtree (st->left, ns);
+ dc_reset_symtree (st->right, ns);
+
+ gfc_symbol *sym = st->n.sym;
+ if (sym && sym->ns == ns && sym->backend_decl
+ && !TREE_STATIC (sym->backend_decl))
+ sym->backend_decl = NULL_TREE;
+}
+
+static int
+dc_reset_code_decls (gfc_code **c, int *, void *)
+{
+ if ((*c)->here)
+ (*c)->here->backend_decl = NULL_TREE;
+
+ if ((*c)->op == EXEC_BLOCK)
+ {
+ gfc_namespace *ns = (*c)->ext.block.ns;
+ dc_reset_symtree (ns->sym_root, ns);
+ for (gfc_charlen *cl = ns->cl_list; cl; cl = cl->next)
+ cl->backend_decl = NULL_TREE;
+ ns->proc_name->tlink = NULL;
+ }
+ return 0;
+}
+
+/* Clear the decls that translating the body of CODE gave its labels and
+ the automatic entities of its BLOCK and ASSOCIATE constructs, so that
+ it can be translated again. */
+
+static void
+dc_reset_body_decls (gfc_code *code)
+{
+ gfc_code_walker (&code->block->next, dc_reset_code_decls, dc_ignore_expr,
+ NULL);
+}
+
+
+/* Translate the DO CONCURRENT construct CODE into an OpenMP loop on the
+ host, or on a device if KIND is GFC_DC_OMP_DEVICE. */
+
+tree
+gfc_trans_omp_do_concurrent (gfc_code *code, enum gfc_dc_to_omp kind)
+{
+ if (kind != GFC_DC_OMP_DEVICE || !dc_maybe_device_proc ())
+ return dc_trans_omp (code, kind == GFC_DC_OMP_DEVICE);
+
+ /* A target construct must not be encountered in a target region
+ (OpenMP 5.2, 13.8), so where the procedure runs on the device, run
+ the loop there as on the host. The condition, device={kind(host)},
+ resolves to a constant in each compiler. */
+ tree device = dc_trans_omp (code, true);
+ dc_reset_body_decls (code);
+ tree host = dc_trans_omp (code, false);
+ tree cond = build2 (OMP_TARGET_DEVICE_MATCHES, integer_type_node,
+ build_int_cst (integer_type_node,
+ (int) OMP_TRAIT_DEVICE_KIND),
+ make_trait_property (OMP_TP_NAMELIST_NODE,
+ get_identifier ("host"),
+ NULL_TREE));
+ cond = build2_loc (input_location, NE_EXPR, logical_type_node, cond,
+ integer_zero_node);
+ return build3_v (COND_EXPR, cond, device, host);
+}
diff --git a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_11.f90 b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_11.f90
new file mode 100644
index 00000000000..4f243c61aab
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_11.f90
@@ -0,0 +1,42 @@
+! { dg-do compile }
+! { dg-additional-options "-fdo-concurrent-to-openmp=device -fdump-tree-original" }
+!
+! PR 128590
+
+module m
+ implicit none
+contains
+ pure subroutine in_pure (a, n)
+ integer, intent(in) :: n
+ real, intent(inout) :: a(n)
+ integer :: i
+ do concurrent (i = 1:n)
+ block
+ real :: t
+ t = a(i)
+ associate (s => 2.0 * t)
+ a(i) = s
+ end associate
+ end block
+ end do
+ do concurrent (i = 1:n)
+ if (a(i) < 0.0) goto 10
+ a(i) = sqrt (a(i))
+10 continue
+ end do
+ end subroutine in_pure
+
+ elemental real function in_elemental (x)
+ real, intent(in) :: x
+ real :: v(4)
+ integer :: i
+ do concurrent (i = 1:4)
+ v(i) = x * i
+ end do
+ in_elemental = sum (v)
+ end function in_elemental
+end module m
+
+! { dg-final { scan-tree-dump-times "if \\(OMP_TARGET_DEVICE_MATCHES <0, host\\)> != 0\\)" 3 "original" } }
+! { dg-final { scan-tree-dump-times "#pragma omp target" 3 "original" } }
+! { dg-final { scan-tree-dump-times "#pragma omp parallel" 6 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_7.f90 b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_7.f90
index 6535a7986f6..70347143fb4 100644
--- a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_7.f90
+++ b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_7.f90
@@ -7,10 +7,8 @@
! variable defined by some iterations needs, is not allowed on DISTRIBUTE;
! a called procedure would have to be available on the device; an
! assumed-size array cannot be mapped implicitly, nor a pointer without
-! attaching it; and in a PURE procedure, which may itself be compiled for
-! the device, a nested target construct would not run its body. A
-! definition after a GOTO or after an EXIT from a BLOCK construct may be
-! skipped, so it is conditional too.
+! attaching it. A definition after a GOTO or after an EXIT from a BLOCK
+! construct may be skipped, so it is conditional too.
module m
implicit none
@@ -19,15 +17,6 @@ contains
real, intent(in) :: x
f = x + 1.0
end function f
-
- pure subroutine in_pure (a, n)
- integer, intent(in) :: n
- real, intent(inout) :: a(n)
- integer :: i
- do concurrent (i = 1:n)
- a(i) = a(i) * 2.0
- end do
- end subroutine in_pure
end module m
subroutine conditional_def (a, n, key, idx)
@@ -87,4 +76,4 @@ subroutine skipped_by_exit (a, n, x)
end subroutine skipped_by_exit
! { dg-final { scan-tree-dump-not "#pragma omp target" "original" } }
-! { dg-final { scan-tree-dump-times "#pragma omp parallel" 7 "original" } }
+! { dg-final { scan-tree-dump-times "#pragma omp parallel" 6 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_9.f90 b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_9.f90
index 9fe74687e36..7a975ae41a3 100644
--- a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_9.f90
+++ b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_9.f90
@@ -17,7 +17,7 @@ contains
integer, intent(in) :: n
real, intent(inout) :: a(n)
integer :: i
- do concurrent (i = 1:n) ! { dg-warning "runs on the host because procedure 'in_pure' may itself run on the device" }
+ do concurrent (i = 1:n)
a(i) = a(i) * 2.0
end do
end subroutine in_pure
diff --git a/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_8.f90 b/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_8.f90
new file mode 100644
index 00000000000..b3905da75f1
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_8.f90
@@ -0,0 +1,47 @@
+! { dg-do run }
+! { dg-additional-options "-fdo-concurrent-to-openmp=device" }
+!
+! PR 128590
+
+module scale_m
+ implicit none
+contains
+ pure subroutine scale_row (a, n, f)
+ !$omp declare target
+ integer, intent(in) :: n
+ real, intent(inout) :: a(n)
+ real, intent(in) :: f
+ integer :: i
+ do concurrent (i = 1:n)
+ block
+ real :: t
+ t = a(i)
+ associate (s => f * t)
+ a(i) = s
+ end associate
+ end block
+ end do
+ end subroutine scale_row
+end module scale_m
+
+program p
+ use scale_m
+ implicit none
+ integer, parameter :: n = 64, m = 32
+ real :: a(n, m), b(n)
+ integer :: i, j
+
+ b = [(real (i), i = 1, n)]
+ call scale_row (b, n, 3.0)
+ if (any (b /= [(3.0 * i, i = 1, n)])) stop 1
+
+ a = reshape ([(real (i), i = 1, n * m)], [n, m])
+ do concurrent (j = 1:m)
+ call scale_row (a(:, j), n, 2.0)
+ end do
+ do j = 1, m
+ do i = 1, n
+ if (a(i, j) /= 2.0 * (i + n * (j - 1))) stop 2
+ end do
+ end do
+end program p
--
2.56.1