See attached patch, 5/10 of v2 of the DO CONCURRENT to OpenMP series.
Changed since v1 only in wording: the comments, invoke.texi and the
commit message say "a declare target procedure".
The change is in dc_scan_call in trans-openmp.cc, which now sets
host_calls only for a procedure other than an intrinsic or declare
target procedure, and dc_device_ok, which tests host_calls instead of
calls. The rest is the new field, its reset in dc_scan_body,
invoke.texi and the tests.
Regards,
Jerry
---
fortran: [PR128590]-5 Allow declare target calls in offloaded loops
A declare target procedure is available on the device, so an offloaded
DO CONCURRENT may call it. Only calls to other procedures now keep a
construct on the host.
Assisted-by: Claude Opus 5.5
PR fortran/128590
gcc/fortran/ChangeLog:
* invoke.texi: Document that declare target procedures may be
called.
* trans-openmp.cc (struct dc_scan): Add host_calls.
(dc_scan_call): Set it unless the procedure is a declare target
procedure.
(dc_scan_body): Reset it for each walk.
(dc_device_ok): Test it instead of calls.
gcc/testsuite/ChangeLog:
* gfortran.dg/gomp/do_concurrent_to_openmp_8.f90: New test.
libgomp/ChangeLog:
* testsuite/libgomp.fortran/do_concurrent_to_openmp_6.f90: New test.
>From 6e0185530b2993c39e5ca1322ce48e716943c340 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Thu, 26 Nov 2026 12:26:04 -0800
Subject: [PATCH v2 5/10] fortran: [PR128590]-5 Allow declare target calls in
offloaded loops
A declare target procedure is available on the device, so an offloaded
DO CONCURRENT may call it. Only calls to other procedures now keep a
construct on the host.
Assisted-by: Claude Opus 5.5
PR fortran/128590
gcc/fortran/ChangeLog:
* invoke.texi: Document that declare target procedures may be
called.
* trans-openmp.cc (struct dc_scan): Add host_calls.
(dc_scan_call): Set it unless the procedure is a declare target
procedure.
(dc_scan_body): Reset it for each walk.
(dc_device_ok): Test it instead of calls.
gcc/testsuite/ChangeLog:
* gfortran.dg/gomp/do_concurrent_to_openmp_8.f90: New test.
libgomp/ChangeLog:
* testsuite/libgomp.fortran/do_concurrent_to_openmp_6.f90: New test.
---
gcc/fortran/invoke.texi | 7 +--
gcc/fortran/trans-openmp.cc | 21 +++++---
.../gomp/do_concurrent_to_openmp_8.f90 | 53 +++++++++++++++++++
.../do_concurrent_to_openmp_6.f90 | 45 ++++++++++++++++
4 files changed, 116 insertions(+), 10 deletions(-)
create mode 100644 gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_8.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_6.f90
diff --git a/gcc/fortran/invoke.texi b/gcc/fortran/invoke.texi
index 986a7f88485..a61167eaeb7 100644
--- a/gcc/fortran/invoke.texi
+++ b/gcc/fortran/invoke.texi
@@ -1902,9 +1902,10 @@ mapped @code{to} rather than @code{tofrom}. Enclosing constructs in an
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, 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
+on the device, to one that references a procedure other than an
+intrinsic or @code{declare target} procedure, 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.
diff --git a/gcc/fortran/trans-openmp.cc b/gcc/fortran/trans-openmp.cc
index 2c5ed8aa6c3..abfdbb22774 100644
--- a/gcc/fortran/trans-openmp.cc
+++ b/gcc/fortran/trans-openmp.cc
@@ -11063,9 +11063,10 @@ struct dc_scan
auto_vec<gfc_symbol *> part_defs;
/* The body references a variable that offloading does not support. */
bool device_bad;
- /* The body references a procedure other than an intrinsic one, and one
- that can see the variables of the procedure containing DC. */
- bool calls, internal_calls;
+ /* The body references a procedure other than an intrinsic one, one
+ that can see the variables of the procedure containing DC, and one
+ other than an intrinsic or declare target procedure. */
+ bool calls, internal_calls, host_calls;
bool ok;
};
@@ -11332,7 +11333,11 @@ dc_scan_call (dc_scan *scan, gfc_symbol *sym)
|| sym->attr.proc_pointer)
scan->internal_calls = true;
else if (!sym->attr.intrinsic)
- scan->calls = true;
+ {
+ scan->calls = true;
+ if (!sym->attr.omp_declare_target)
+ scan->host_calls = true;
+ }
}
@@ -11702,6 +11707,7 @@ dc_scan_body (gfc_code *code, dc_scan *scan)
scan->cond_depth = 0;
scan->branched = scan->calls = scan->internal_calls = false;
scan->exits = 0;
+ scan->host_calls = false;
scan->unindexed_def = scan->device_bad = false;
scan->part_def_seen.empty ();
scan->part_defs.truncate (0);
@@ -11743,8 +11749,9 @@ dc_scan_body (gfc_code *code, dc_scan *scan)
offloaded. A PURE, ELEMENTAL or declare target procedure 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 would have to be available on
- the device. Conditional lastprivate is not allowed on DISTRIBUTE. */
+ host; and a procedure that the body calls must be a declare target
+ procedure, to be available on the device. Conditional lastprivate is
+ not allowed on DISTRIBUTE. */
static bool
dc_device_ok (dc_scan *scan)
@@ -11759,7 +11766,7 @@ dc_device_ok (dc_scan *scan)
|| proc->attr.elemental || proc->attr.omp_declare_target))
return false;
- if (scan->device_bad || scan->calls || scan->internal_calls)
+ if (scan->device_bad || scan->host_calls || scan->internal_calls)
return false;
for (int i = 0; i < LOCALITY_NUM; i++)
diff --git a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_8.f90 b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_8.f90
new file mode 100644
index 00000000000..c9be6b94067
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_8.f90
@@ -0,0 +1,53 @@
+! { dg-do compile }
+! { dg-additional-options "-fdo-concurrent-to-openmp=device -fdump-tree-original" }
+! PR 128590
+!
+! An offloaded DO CONCURRENT may call a declare target procedure, which
+! is available on the device; one that calls any other procedure runs on
+! the host.
+
+module m
+ implicit none
+contains
+ pure real function on_device (x)
+ !$omp declare target
+ real, intent(in) :: x
+ on_device = 2.0 * x
+ end function on_device
+
+ pure subroutine split (x, hi, lo)
+ !$omp declare target
+ real, intent(in) :: x
+ real, intent(out) :: hi, lo
+ hi = aint (x)
+ lo = x - hi
+ end subroutine split
+
+ pure real function host_only (x)
+ real, intent(in) :: x
+ host_only = x + 1.0
+ end function host_only
+end module m
+
+subroutine calls_declare_target (a, b, c, n)
+ use m
+ integer :: n, i
+ real :: a(n), b(n), c(n), h, l
+ do concurrent (i = 1:n)
+ call split (on_device (a(i)), h, l)
+ b(i) = h
+ c(i) = l
+ end do
+end subroutine calls_declare_target
+
+subroutine calls_host_only (a, b, n)
+ use m
+ integer :: n, i
+ real :: a(n), b(n)
+ do concurrent (i = 1:n)
+ b(i) = on_device (host_only (a(i)))
+ end do
+end subroutine calls_host_only
+
+! { dg-final { scan-tree-dump-times "#pragma omp target" 1 "original" } }
+! { dg-final { scan-tree-dump-times "#pragma omp parallel" 2 "original" } }
diff --git a/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_6.f90 b/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_6.f90
new file mode 100644
index 00000000000..a9165c4d7ae
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_6.f90
@@ -0,0 +1,45 @@
+! { dg-do run }
+! { dg-additional-options "-fdo-concurrent-to-openmp=device" }
+! PR 128590
+!
+! An offloaded DO CONCURRENT that calls declare target procedures, with
+! an INTENT(OUT) argument defining privatized temporaries.
+
+module m
+ implicit none
+contains
+ pure real function scale (x)
+ !$omp declare target
+ real, intent(in) :: x
+ scale = 2.0 * x
+ end function scale
+
+ pure subroutine split (x, hi, lo)
+ !$omp declare target
+ real, intent(in) :: x
+ real, intent(out) :: hi, lo
+ hi = aint (x)
+ lo = x - hi
+ end subroutine split
+end module m
+
+program main
+ use m
+ implicit none
+ integer, parameter :: n = 5000
+ integer :: i, j
+ real :: a(n), b(n), c(n), h, l
+
+ do j = 1, n
+ a(j) = real(j) + 0.125
+ end do
+
+ do concurrent (i = 1:n)
+ call split (scale (a(i)), h, l)
+ b(i) = h
+ c(i) = l
+ end do
+ do j = 1, n
+ if (b(j) /= real(2 * j) .or. c(j) /= 0.25) stop 1
+ end do
+end program main
--
2.56.1