See attached patch, 5/10 of the DO CONCURRENT to OpenMP series.

The change is in dc_scan_call in trans-openmp.cc, which now sets
host_calls only for a procedure that is not in a declare target
directive, 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 procedure that is declare target 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 for procedures that are not declare target.
        (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 869c91b74e4c5f61de63e776c38d496ab071f3e4 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Tue, 10 Nov 2026 12:31:27 -0800
Subject: [PATCH] fortran: [PR128590]-5 Allow declare target calls in offloaded
 loops

A procedure that is declare target 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 for procedures that are not declare target.
	(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                       | 12 ++---
 gcc/fortran/trans-openmp.cc                   | 21 +++++---
 .../gomp/do_concurrent_to_openmp_8.f90        | 53 +++++++++++++++++++
 .../do_concurrent_to_openmp_6.f90             | 45 ++++++++++++++++
 4 files changed, 118 insertions(+), 13 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..4b95b835d4d 100644
--- a/gcc/fortran/invoke.texi
+++ b/gcc/fortran/invoke.texi
@@ -1902,12 +1902,12 @@ 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
-iterations define a variable of unspecified locality.  A procedure
-containing a construct translated this way must not be called in a
-@code{target} region.
+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.
 @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 8569d5db47c..c27d28503b5 100644
--- a/gcc/fortran/trans-openmp.cc
+++ b/gcc/fortran/trans-openmp.cc
@@ -11061,9 +11061,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
+     that is not declare target.  */
+  bool calls, internal_calls, host_calls;
   bool ok;
 };
 
@@ -11272,7 +11273,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;
+    }
 }
 
 
@@ -11605,6 +11610,7 @@ dc_scan_body (gfc_code *code, dc_scan *scan)
       scan->dom_order.truncate (0);
       scan->cond_depth = 0;
       scan->branched = scan->calls = scan->internal_calls = false;
+      scan->host_calls = false;
       scan->unindexed_def = scan->device_bad = false;
       scan->part_def_seen.empty ();
       scan->part_defs.truncate (0);
@@ -11646,8 +11652,9 @@ dc_scan_body (gfc_code *code, dc_scan *scan)
    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 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 declare target, to be
+   available on the device.  Conditional lastprivate is not allowed on
+   DISTRIBUTE.  */
 
 static bool
 dc_device_ok (dc_scan *scan)
@@ -11662,7 +11669,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..c6cd834d6ad
--- /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 procedure that is declare target,
+! 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

Reply via email to