See attached patch, 10/10 of v2 of the DO CONCURRENT to OpenMP series.
Changes since v1: dc_add_mapped_var takes the symtree of the reference,
so a renamed use-associated variable no longer ICEs; dc_mapped_var_r
leaves out firstprivate scalars, which are not mapped; and dc_any_present
skips the presence check when the default device is the initial device,
on which omp_target_is_present is always true.
Start at the dc_trans_omp hunk in trans-openmp.cc. For =device it
builds the condition "at least dc_device_min_iterations (32)
iterations, or some mapped variable already present on the device",
from dc_min_iterations and dc_any_present. For =host the condition is
at least two iterations.
dc_add_if walks the translated statement with dc_add_if_r and puts the
condition in an if clause on both the target and the parallel
construct. With the clause on target alone, each host fallback still
starts a team of threads (560 us per construct on this machine).
dc_any_present collects the variables the target construct would map,
through dc_mapped_var_r and dc_add_mapped_var, and tests each with
omp_target_is_present; the trans-decl.cc and trans.h hunks declare that
function. It returns true outright when the body calls a procedure
other than an intrinsic one, which may use declare target variables,
or references an assumed-rank array or a function result, which it
does not check.
32 is a constant for now; it could become a --param
(e.g. --param=dc-device-min-iterations=) if reviewers want it tunable.
Regards,
Jerry
---
fortran: [PR128590]-10 Run DO CONCURRENT with few iterations on the host
Offloading a DO CONCURRENT construct costs a kernel launch and copies of
its data, a few hundred microseconds on an amdgcn device, which a
construct with few iterations cannot recover; one that runs many times
then makes the program hundreds of times slower than the serial
lowering.
Give the target construct an if clause that is false when the construct
has fewer than 32 iterations and none of the variables that it would map
is present on the default device, so that it then runs in the
encountering thread, and data that an enclosing target data region keeps
on the device is still used there. Presence is not checked if the
default device is the initial device, on which omp_target_is_present is
always true. The parallel construct inside gets
the same clause, because otherwise the host fallback starts a team of
threads each time. A construct that calls a procedure other than an
intrinsic one, which may use declare target variables, is always
offloaded. With =host, a construct with fewer than two iterations no
longer starts a team of threads.
Assisted-by: Claude Opus 5.5
PR fortran/128590
gcc/fortran/ChangeLog:
* invoke.texi: Document that constructs with few iterations run on
the host.
* trans-decl.cc (gfor_fndecl_omp_target_is_present): New.
(gfc_build_builtin_function_decls): Build it.
* trans-openmp.cc (dc_device_min_iterations, dc_min_iterations)
(struct dc_mapped_vars, dc_add_mapped_var, dc_mapped_var_r)
(dc_any_present, dc_add_if_r, dc_add_if): New.
(dc_trans_omp): Add if clauses to the target and parallel
constructs of a construct with few iterations.
(gfc_trans_omp_do_concurrent): Update comment.
* trans.h (gfor_fndecl_omp_target_is_present): Declare.
gcc/testsuite/ChangeLog:
* gfortran.dg/gomp/do_concurrent_to_openmp_6.f90: Expect an if
clause.
* gfortran.dg/gomp/do_concurrent_to_openmp_12.f90: New test.
* gfortran.dg/gomp/do_concurrent_to_openmp_20.f90: New test.
libgomp/ChangeLog:
* testsuite/libgomp.fortran/do_concurrent_to_openmp_9.f90: New test.
>From f109e420fc6622a54a60464ce8829c2de7376484 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Wed, 25 Nov 2026 16:00:44 -0800
Subject: [PATCH v2 10/10] fortran: [PR128590]-10 Run DO CONCURRENT with few
iterations on the host
Offloading a DO CONCURRENT construct costs a kernel launch and copies of
its data, a few hundred microseconds on an amdgcn device, which a
construct with few iterations cannot recover; one that runs many times
then makes the program hundreds of times slower than the serial
lowering.
Give the target construct an if clause that is false when the construct
has fewer than 32 iterations and none of the variables that it would map
is present on the default device, so that it then runs in the
encountering thread, and data that an enclosing target data region keeps
on the device is still used there. Presence is not checked if the
default device is the initial device, on which omp_target_is_present is
always true. The parallel construct inside gets
the same clause, because otherwise the host fallback starts a team of
threads each time. A construct that calls a procedure other than an
intrinsic one, which may use declare target variables, is always
offloaded. With =host, a construct with fewer than two iterations no
longer starts a team of threads.
Assisted-by: Claude Opus 5.5
PR fortran/128590
gcc/fortran/ChangeLog:
* invoke.texi: Document that constructs with few iterations run on
the host.
* trans-decl.cc (gfor_fndecl_omp_target_is_present): New.
(gfc_build_builtin_function_decls): Build it.
* trans-openmp.cc (dc_device_min_iterations, dc_min_iterations)
(struct dc_mapped_vars, dc_add_mapped_var, dc_mapped_var_r)
(dc_any_present, dc_add_if_r, dc_add_if): New.
(dc_trans_omp): Add if clauses to the target and parallel
constructs of a construct with few iterations.
(gfc_trans_omp_do_concurrent): Update comment.
* trans.h (gfor_fndecl_omp_target_is_present): Declare.
gcc/testsuite/ChangeLog:
* gfortran.dg/gomp/do_concurrent_to_openmp_6.f90: Expect an if
clause.
* gfortran.dg/gomp/do_concurrent_to_openmp_12.f90: New test.
* gfortran.dg/gomp/do_concurrent_to_openmp_20.f90: New test.
libgomp/ChangeLog:
* testsuite/libgomp.fortran/do_concurrent_to_openmp_9.f90: New test.
---
gcc/fortran/invoke.texi | 5 +-
gcc/fortran/trans-decl.cc | 8 +
gcc/fortran/trans-openmp.cc | 251 +++++++++++++++++-
gcc/fortran/trans.h | 3 +
.../gomp/do_concurrent_to_openmp_12.f90 | 62 +++++
.../gomp/do_concurrent_to_openmp_20.f90 | 21 ++
.../gomp/do_concurrent_to_openmp_6.f90 | 2 +-
.../do_concurrent_to_openmp_9.f90 | 80 ++++++
8 files changed, 428 insertions(+), 4 deletions(-)
create mode 100644 gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_12.f90
create mode 100644 gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_20.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_9.f90
diff --git a/gcc/fortran/invoke.texi b/gcc/fortran/invoke.texi
index 29913c9d581..082167dabd5 100644
--- a/gcc/fortran/invoke.texi
+++ b/gcc/fortran/invoke.texi
@@ -1910,7 +1910,10 @@ 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 in a @code{PURE}, @code{ELEMENTAL} or
+them. A construct with fewer than 32 iterations runs on the host, in
+the encountering thread, unless a variable that it references is present
+on the device, as in such a region, or it references a procedure other
+than an intrinsic one. 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
diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc
index 77908653511..028ec6e7bef 100644
--- a/gcc/fortran/trans-decl.cc
+++ b/gcc/fortran/trans-decl.cc
@@ -176,6 +176,9 @@ tree gfor_fndecl_co_sum;
tree gfor_fndecl_caf_is_present_on_remote;
tree gfor_fndecl_caf_random_init;
+/* OpenMP runtime routine used by the DO CONCURRENT lowering. */
+tree gfor_fndecl_omp_target_is_present;
+
/* Math functions. Many other math functions are handled in
trans-intrinsic.cc. */
@@ -4552,6 +4555,11 @@ gfc_build_builtin_function_decls (void)
void_type_node, 2, logical_type_node, logical_type_node);
}
+ if (flag_openmp)
+ gfor_fndecl_omp_target_is_present = gfc_build_library_function_decl (
+ get_identifier ("omp_target_is_present"), integer_type_node, 2,
+ const_ptr_type_node, integer_type_node);
+
gfc_build_intrinsic_function_decls ();
gfc_build_intrinsic_lib_fndecls ();
gfc_build_io_library_fndecls ();
diff --git a/gcc/fortran/trans-openmp.cc b/gcc/fortran/trans-openmp.cc
index 93a597b4561..272963e1adb 100644
--- a/gcc/fortran/trans-openmp.cc
+++ b/gcc/fortran/trans-openmp.cc
@@ -12476,6 +12476,238 @@ dc_has_iterations (gfc_code *code, stmtblock_t *block)
}
+/* Fewest iterations for which an offloaded construct runs on the device
+ when its data is not there. */
+
+static const unsigned dc_device_min_iterations = 32;
+
+
+/* A condition that is true if the DO CONCURRENT construct CODE has at
+ least LIMIT iterations. Its limits and steps are evaluated into
+ BLOCK. */
+
+static tree
+dc_min_iterations (gfc_code *code, unsigned limit, stmtblock_t *block)
+{
+ tree itype = gfc_array_index_type;
+ tree lim = build_int_cst (itype, limit);
+ tree total = build_int_cst (itype, 1);
+
+ for (gfc_forall_iterator *fa = code->ext.concur.forall_iterator;
+ fa; fa = fa->next)
+ {
+ tree type = gfc_typenode_for_spec (&fa->var->symtree->n.sym->ts);
+ tree utype = unsigned_type_for (type);
+ tree val[3];
+ gfc_expr *e[3] = { fa->start, fa->end, fa->stride };
+
+ for (int i = 0; i < 3; i++)
+ {
+ gfc_se se;
+ gfc_init_se (&se, NULL);
+ gfc_conv_expr_val (&se, e[i]);
+ gfc_add_block_to_block (block, &se.pre);
+ val[i] = gfc_evaluate_now (fold_convert (type, se.expr), block);
+ gfc_add_block_to_block (block, &se.post);
+ }
+
+ /* The number of steps, computed without overflow. */
+ tree pos = fold_build2 (GT_EXPR, logical_type_node, val[2],
+ build_zero_cst (type));
+ tree start = fold_convert (utype, val[0]);
+ tree end = fold_convert (utype, val[1]);
+ tree step = fold_convert (utype, val[2]);
+ tree nonempty
+ = fold_build3 (COND_EXPR, logical_type_node, pos,
+ fold_build2 (LE_EXPR, logical_type_node, val[0], val[1]),
+ fold_build2 (GE_EXPR, logical_type_node, val[0],
+ val[1]));
+ tree span = fold_build3 (COND_EXPR, utype, pos,
+ fold_build2 (MINUS_EXPR, utype, end, start),
+ fold_build2 (MINUS_EXPR, utype, start, end));
+ step = fold_build3 (COND_EXPR, utype, pos, step,
+ fold_build1 (NEGATE_EXPR, utype, step));
+ tree steps = fold_build2 (TRUNC_DIV_EXPR, utype, span, step);
+ steps = fold_build2 (MIN_EXPR, utype, steps,
+ build_int_cst (utype, limit - 1));
+ tree count = fold_build2 (PLUS_EXPR, itype,
+ fold_convert (itype, steps),
+ build_int_cst (itype, 1));
+ count = fold_build3 (COND_EXPR, itype, nonempty, count,
+ build_zero_cst (itype));
+ total = fold_build2 (MIN_EXPR, itype,
+ fold_build2 (MULT_EXPR, itype, total, count), lim);
+ total = gfc_evaluate_now (total, block);
+ }
+
+ return fold_build2 (GE_EXPR, logical_type_node, total, lim);
+}
+
+
+/* The variables of a DO CONCURRENT construct that a target construct for
+ it would map, in the order of their first appearance. */
+
+struct dc_mapped_vars
+{
+ dc_scan *scan;
+ hash_set<gfc_symbol *> seen;
+ /* The symtrees of the first references, which name a renamed
+ use-associated variable by its local name. */
+ auto_vec<gfc_symtree *> vars;
+ /* A variable whose presence is not checked is referenced. */
+ bool unchecked;
+};
+
+static void
+dc_add_mapped_var (dc_mapped_vars *m, gfc_symtree *st)
+{
+ if (!m->seen.add (st->n.sym))
+ m->vars.safe_push (st);
+}
+
+static int
+dc_mapped_var_r (gfc_expr **ep, int *, void *data)
+{
+ dc_mapped_vars *m = (dc_mapped_vars *) data;
+ if ((*ep)->expr_type == EXPR_VARIABLE)
+ {
+ gfc_symbol *sym = (*ep)->symtree->n.sym;
+ if (sym->attr.flavor == FL_VARIABLE && !dc_locality_known (m->scan, sym))
+ {
+ if (sym->as && sym->as->type == AS_ASSUMED_RANK)
+ m->unchecked = true;
+ /* A firstprivate scalar is not mapped. */
+ else if (m->scan->defined.contains (sym)
+ || sym->attr.omp_declare_target
+ || dc_mapped_implicitly (sym))
+ dc_add_mapped_var (m, (*ep)->symtree);
+ }
+ else if (sym->attr.flavor == FL_PROCEDURE)
+ /* The result of the function, referenced by its name. */
+ m->unchecked = true;
+ }
+ return 0;
+}
+
+/* A condition that is true if a variable that the DO CONCURRENT construct
+ CODE, scanned into SCAN, would map is present on the default device;
+ the addresses are evaluated into BLOCK. A construct that calls a
+ procedure may reach declare target variables through it, so for it
+ the condition is true. */
+
+static tree
+dc_any_present (gfc_code *code, dc_scan *scan, stmtblock_t *block)
+{
+ if (scan->calls)
+ return logical_true_node;
+
+ dc_mapped_vars m;
+ m.scan = scan;
+ m.unchecked = false;
+ static const int mapped[] = { LOCALITY_SHARED, LOCALITY_REDUCE };
+ for (int i : mapped)
+ for (gfc_expr_list *el = code->ext.concur.locality[i]; el; el = el->next)
+ if (el->expr->expr_type == EXPR_VARIABLE)
+ dc_add_mapped_var (&m, el->expr->symtree);
+ if (code->expr1)
+ gfc_expr_walker (&code->expr1, dc_mapped_var_r, &m);
+ gfc_code_walker (&code->block->next, gfc_dummy_code_callback,
+ dc_mapped_var_r, &m);
+ if (m.unchecked)
+ return logical_true_node;
+
+ tree cond = NULL_TREE;
+ for (gfc_symtree *st : m.vars)
+ {
+ gfc_symbol *sym = st->n.sym;
+ gfc_expr *e = gfc_get_variable_expr (st);
+ gfc_se se;
+ gfc_init_se (&se, NULL);
+ tree addr;
+ if (e->rank)
+ {
+ gfc_conv_expr_descriptor (&se, e);
+ addr = gfc_conv_descriptor_data_get (se.expr);
+ }
+ else
+ {
+ gfc_conv_expr_reference (&se, e);
+ addr = se.expr;
+ }
+ gfc_free_expr (e);
+
+ tree tmp = gfc_create_var (const_ptr_type_node, "dc_addr");
+ stmtblock_t inner;
+ gfc_init_block (&inner);
+ gfc_add_block_to_block (&inner, &se.pre);
+ gfc_add_modify (&inner, tmp, fold_convert (const_ptr_type_node, addr));
+ gfc_add_block_to_block (&inner, &se.post);
+ tree set = gfc_finish_block (&inner);
+ if (sym->attr.dummy && sym->attr.optional)
+ set = build3_v (COND_EXPR, gfc_conv_expr_present (sym), set,
+ build2_v (MODIFY_EXPR, tmp,
+ build_zero_cst (const_ptr_type_node)));
+ gfc_add_expr_to_block (block, set);
+
+ /* omp_default_device (OpenMP 6.0, 3.8.1). */
+ tree c = build_call_expr_loc (input_location,
+ gfor_fndecl_omp_target_is_present, 2,
+ tmp, build_int_cst (integer_type_node, -5));
+ c = fold_build2 (NE_EXPR, logical_type_node, c, integer_zero_node);
+ cond = (cond ? fold_build2 (TRUTH_ORIF_EXPR, logical_type_node, cond, c)
+ : c);
+ }
+
+ if (!cond)
+ return logical_false_node;
+
+ /* On the initial device, omp_target_is_present is always true. */
+ tree dev = build_call_expr_loc (input_location,
+ builtin_decl_explicit
+ (BUILT_IN_OMP_GET_DEFAULT_DEVICE), 0);
+ tree host = build_call_expr_loc (input_location,
+ builtin_decl_explicit
+ (BUILT_IN_OMP_GET_INITIAL_DEVICE), 0);
+ return fold_build2 (TRUTH_ANDIF_EXPR, logical_type_node,
+ fold_build2 (NE_EXPR, logical_type_node, dev, host),
+ cond);
+}
+
+
+static tree
+dc_add_if_r (tree *tp, int *walk_subtrees, void *data)
+{
+ tree t = *tp;
+ if (TREE_CODE (t) == OMP_TARGET || TREE_CODE (t) == OMP_PARALLEL)
+ {
+ tree c = build_omp_clause (EXPR_LOCATION (t), OMP_CLAUSE_IF);
+ OMP_CLAUSE_IF_MODIFIER (c) = ERROR_MARK;
+ OMP_CLAUSE_IF_EXPR (c) = (tree) data;
+ tree *cp = (TREE_CODE (t) == OMP_TARGET ? &OMP_TARGET_CLAUSES (t)
+ : &OMP_PARALLEL_CLAUSES (t));
+ while (*cp)
+ cp = &OMP_CLAUSE_CHAIN (*cp);
+ *cp = c;
+ }
+ else if (TYPE_P (t) || DECL_P (t) || CONSTANT_CLASS_P (t))
+ *walk_subtrees = 0;
+ return NULL_TREE;
+}
+
+/* Add an if clause with condition COND, evaluated into BLOCK, to the
+ target and parallel constructs in STMT, so that, where it is false, the
+ loop runs in the encountering thread. */
+
+static void
+dc_add_if (tree stmt, tree cond, stmtblock_t *block)
+{
+ if (integer_nonzerop (cond))
+ return;
+ cond = gfc_evaluate_now (cond, block);
+ walk_tree_without_duplicates (&stmt, dc_add_if_r, cond);
+}
+
+
/* Translate the DO CONCURRENT construct CODE into an OpenMP loop on the
host, or on a device if DEVICE. */
@@ -12569,13 +12801,27 @@ dc_trans_omp (gfc_code *code, bool device)
= (enum omp_requires) (omp_requires_mask
| int (OMP_REQUIRES_TARGET_USED));
+ /* Run a construct with few iterations on the host, where it is
+ faster, unless its data is on the device. */
+ tree enough = dc_min_iterations (code, dc_device_min_iterations,
+ &block);
+ if (!integer_nonzerop (enough))
+ dc_add_if (stmt,
+ fold_build2 (TRUTH_ORIF_EXPR, logical_type_node, enough,
+ dc_any_present (code, &scan, &block)),
+ &block);
+
/* See dc_build_clauses. */
if (par->ext.omp_clauses->lists[OMP_LIST_LASTPRIVATE])
stmt = build3_v (COND_EXPR, dc_has_iterations (code, &block), stmt,
build_empty_stmt (input_location));
}
else
- stmt = gfc_trans_omp_parallel_do_simd (par, NULL, NULL);
+ {
+ stmt = gfc_trans_omp_parallel_do_simd (par, NULL, NULL);
+ /* A single iteration gains nothing from a team of threads. */
+ dc_add_if (stmt, dc_min_iterations (code, 2, &block), &block);
+ }
gfc_omp_construct_depth--;
gfc_add_expr_to_block (&block, stmt);
@@ -12689,7 +12935,8 @@ gfc_trans_omp_do_concurrent (gfc_code *code, enum gfc_dc_to_omp kind)
/* 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. */
+ resolves to a constant in each compiler, which removes the other
+ branch also without optimization. */
auto_vec<dc_saved_attr> saved;
gfc_code_walker (&code->block->next, dc_save_code_attrs, dc_ignore_expr,
&saved);
diff --git a/gcc/fortran/trans.h b/gcc/fortran/trans.h
index 3a64da41ab0..4a359c1d58e 100644
--- a/gcc/fortran/trans.h
+++ b/gcc/fortran/trans.h
@@ -937,6 +937,9 @@ extern GTY(()) tree gfor_fndecl_co_reduce;
extern GTY(()) tree gfor_fndecl_co_sum;
extern GTY(()) tree gfor_fndecl_caf_is_present_on_remote;
+/* OpenMP runtime routine used by the DO CONCURRENT lowering. */
+extern GTY(()) tree gfor_fndecl_omp_target_is_present;
+
/* Math functions. Many other math functions are handled in
trans-intrinsic.cc. */
diff --git a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_12.f90 b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_12.f90
new file mode 100644
index 00000000000..1039894c051
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_12.f90
@@ -0,0 +1,62 @@
+! { dg-do compile }
+! { dg-additional-options "-fdo-concurrent-to-openmp=device -fdump-tree-original" }
+!
+! PR 128590
+
+module m
+ implicit none
+contains
+ subroutine f (a, b, n)
+ integer, intent(in) :: n
+ real, intent(in) :: a(:)
+ real, intent(inout) :: b(:)
+ integer :: i
+ do concurrent (i = 1:n)
+ b(i) = b(i) + a(i)
+ end do
+ end subroutine
+
+ subroutine g (c)
+ real :: c(1000)
+ integer :: i
+ do concurrent (i = 1:1000)
+ c(i) = 1.0
+ end do
+ end subroutine
+
+ pure real function h (x)
+ !$omp declare target
+ real, intent(in) :: x
+ h = 2 * x
+ end function
+
+ subroutine k (c, n)
+ integer, intent(in) :: n
+ real :: c(n)
+ integer :: i
+ do concurrent (i = 1:n)
+ c(i) = h (c(i))
+ end do
+ end subroutine
+end module
+
+subroutine p (c, n)
+ integer, intent(in) :: n
+ real :: c(n)
+ integer :: i
+ do concurrent (i = 1:3)
+ c(i) = 0.0
+ end do
+end subroutine
+
+! In f, a is only referenced, so it is mapped 'to'; the construct runs on
+! the host unless it has 32 iterations or a or b is present on the device.
+! f is implicitly pure, so it also has a version for the device, where
+! the construct runs in place, in one thread unless it has two
+! iterations.
+! { dg-final { scan-tree-dump-times "#pragma omp target map\\(to:\\*\\(?a\\.0" 1 "original" } }
+! { dg-final { scan-tree-dump-times "omp_target_is_present \\(dc_addr\\.\[0-9\]+, -5\\)" 3 "original" } }
+! { dg-final { scan-tree-dump-times "#pragma omp target \[^\n\r\]*if\\(D\\.\[0-9\]+\\)\n" 2 "original" } }
+! { dg-final { scan-tree-dump-times "#pragma omp parallel default\\(shared\\) if\\(D\\.\[0-9\]+\\)" 3 "original" } }
+! g has 1000 iterations and k calls a procedure: no if clause.
+! { dg-final { scan-tree-dump-times "#pragma omp target" 4 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_20.f90 b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_20.f90
new file mode 100644
index 00000000000..1cd53516d26
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_20.f90
@@ -0,0 +1,21 @@
+! { dg-do compile }
+! { dg-additional-options "-fdo-concurrent-to-openmp=device -fdump-tree-original" }
+!
+! PR 128590
+
+module m
+ integer :: a(100)
+end module m
+
+subroutine rename (b, n)
+ use m, only: la => a
+ integer :: n, b(100), i, k
+ k = 3
+ do concurrent (i = 1:n)
+ b(i) = la(i) + k
+ end do
+end subroutine rename
+
+! The presence of b and la is checked, not that of the firstprivate k.
+! { dg-final { scan-tree-dump-times "omp_target_is_present" 2 "original" } }
+! { dg-final { scan-tree-dump-times "__builtin_omp_get_default_device \\(\\) != __builtin_omp_get_initial_device \\(\\)" 1 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_6.f90 b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_6.f90
index 39bd161e2b2..18c71ef2511 100644
--- a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_6.f90
+++ b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_6.f90
@@ -93,7 +93,7 @@ end subroutine read_only
! { dg-final { scan-tree-dump-times "#pragma omp simd" 8 "original" } }
! { dg-final { scan-tree-dump-times "collapse\\(2\\)" 3 "original" } }
! { dg-final { scan-tree-dump "target map\\(tofrom:\\*flag\\)" "original" } }
-! { dg-final { scan-tree-dump "map\\(tofrom:\\*s\\) map\\(alloc:s \\\[pointer assign, bias: 0\\\]\\)\n" "original" } }
+! { dg-final { scan-tree-dump "map\\(tofrom:\\*s\\) map\\(alloc:s \\\[pointer assign, bias: 0\\\]\\) if\\(\[^\n\r\]*\\)\n" "original" } }
! { dg-final { scan-tree-dump "target map\\(to:\\*a\\) map\\(alloc:a \\\[pointer assign, bias: 0\\\]\\) map\\(tofrom:t\\)" "original" } }
! { dg-final { scan-tree-dump "target map\\(to:\[^\n\r\]*x\\.0\[^\n\r\]* map\\(to:\\*y\\) map\\(alloc:y \\\[pointer assign, bias: 0\\\]\\) map\\(to:\\*str\\)" "original" } }
! { dg-final { scan-tree-dump "distribute lastprivate\\(t\\)" "original" } }
diff --git a/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_9.f90 b/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_9.f90
new file mode 100644
index 00000000000..d6a1c1a611f
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_9.f90
@@ -0,0 +1,80 @@
+! { dg-do run }
+! { dg-additional-options "-fdo-concurrent-to-openmp=device" }
+!
+! PR 128590
+
+module m
+ implicit none
+contains
+ subroutine small (a, n, x)
+ integer, intent(in) :: n
+ real, intent(inout) :: a(:)
+ real, intent(in), optional :: x(:)
+ integer :: i
+ do concurrent (i = 1:n)
+ a(i) = a(i) + 100
+ if (present (x)) a(i) = a(i) + x(i)
+ end do
+ end subroutine
+end module
+
+program p
+ use m
+ implicit none
+ type t
+ real :: v(4)
+ end type
+ real :: a(1000), c(1000)
+ real, allocatable :: d(:)
+ type(t) :: s
+ integer :: i
+
+ a = 0
+ c = 0
+ !$omp target data map(tofrom: a)
+ do concurrent (i = 1:1000)
+ a(i) = i
+ end do
+ ! a is present on the device, so these small constructs run there.
+ do concurrent (i = 1:3)
+ a(i) = a(i) + 100
+ end do
+ call small (a, 2)
+ !$omp end target data
+ if (any (a(1:2) /= [201., 202.]) .or. a(3) /= 103. .or. a(4) /= 4.) stop 1
+
+ ! Not present: on the host.
+ do concurrent (i = 1:3)
+ c(i) = c(i) + i
+ end do
+ call small (c, 3)
+ call small (c, 2, [1., 1.])
+ if (any (c(1:3) /= [202., 203., 103.]) .or. c(4) /= 0.) stop 2
+
+ allocate (d(10))
+ d = 1
+ s%v = 2
+ !$omp target data map(tofrom: d, s)
+ do concurrent (i = 1:10)
+ d(i) = 5
+ end do
+ do concurrent (i = 1:2)
+ d(i) = d(i) + s%v(i)
+ end do
+ !$omp target
+ s%v(3) = 9
+ !$omp end target
+ do concurrent (i = 3:3)
+ d(i) = d(i) + s%v(i)
+ end do
+ !$omp end target data
+ if (any (d(1:3) /= [7., 7., 14.]) .or. d(4) /= 5.) stop 3
+
+ do concurrent (i = 1:0)
+ c(i) = -1
+ end do
+ do concurrent (i = 5:5)
+ c(i) = -5
+ end do
+ if (c(5) /= -5. .or. any (c(6:) /= 0.)) stop 4
+end program
--
2.56.1