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

Start at gfc_trans_omp_do_concurrent at the end of trans-openmp.cc: it
shadows the index-names, builds the EXEC_DO chain, and puts one
"iteration" node -- a DO CONCURRENT without concurrent-controls -- in
the innermost loop.  gfc_trans_do_concurrent in trans-stmt.cc translates
that node with gfc_trans_do_concurrent_iteration, which is the DO
CONCURRENT branch of gfc_trans_forall_1 moved out unchanged.
gfc_dc_omp_lowering decides whether a construct is lowered, using
dc_scan_code for the body and dc_branch_into for branches from outside;
dc_retarget_cycle points CYCLE statements at the iteration node while
it is translated.  The flag-types.h, lang.opt and options.cc hunks are
mechanical, and the trans.cc hunk only counts enclosing OpenMP and
OpenACC constructs.

Regards,

Jerry

---

fortran: [PR128590]-1 Add -fdo-concurrent-to-openmp=host

Add an option, with the name and values of flang's, that translates DO
CONCURRENT into an OpenMP 'parallel do simd' loop nest with one collapsed
loop per concurrent-control.  The front end synthesizes the gfc_code of
that construct and hands it to gfc_trans_omp_parallel_do_simd, the last
control outermost as in the serial lowering.  The innermost loop runs one
iteration of the construct through gfc_trans_do_concurrent_iteration,
factored out of gfc_trans_forall_1, and each index-name gets a fresh decl
as in the serial lowering.  Like flang's, the option requires -fopenmp.

This patch lowers constructs whose body defines only parts of variables
selected by an index-name.  A construct with a mask, a locality spec,
input/output, an OpenMP or OpenACC directive or any other definition, one
inside an OpenMP or OpenACC construct other than TARGET DATA, or one that
a branch from outside enters, a legacy extension, is translated serially
as before.

Assisted-by: Claude Opus 5.5

        PR fortran/128590

gcc/ChangeLog:

        * flag-types.h (enum gfc_dc_to_omp): New.

gcc/fortran/ChangeLog:

        * invoke.texi: Document -fdo-concurrent-to-openmp.
        * lang.opt (fdo-concurrent-to-openmp=): New option.
        * options.cc (gfc_post_options): Ignore -fdo-concurrent-to-openmp
        with a warning without -fopenmp.
        * trans-openmp.cc (gfc_omp_construct_depth): New variable.
        (struct dc_scan, dc_ignore_expr, dc_index_ref)
        (dc_indexed_designator, dc_construct_local, dc_scan_def)
        (dc_scan_code, struct dc_entry, dc_add_label, dc_entry_label)
        (dc_find_entry, dc_branch_into, dc_build_clauses)
        (dc_retarget_cycle): New.
        (gfc_dc_omp_lowering, gfc_trans_omp_do_concurrent): New functions.
        * trans-stmt.cc (gfc_trans_forall_1): Use
        gfc_trans_do_concurrent_iteration.
        (gfc_trans_do_concurrent_iteration): New function, factored out of
        gfc_trans_forall_1.
        (gfc_trans_do_concurrent): Translate an iteration of a construct
        lowered to OpenMP, and lower a construct to OpenMP if possible.
        * trans-stmt.h (gfc_trans_do_concurrent_iteration): Declare.
        * trans.cc (trans_code): Count the enclosing OpenMP and OpenACC
        constructs in gfc_omp_construct_depth.
        * trans.h (gfc_omp_construct_depth, gfc_dc_omp_lowering)
        (gfc_trans_omp_do_concurrent): Declare.

gcc/testsuite/ChangeLog:

        * gfortran.dg/do_concurrent_to_openmp_1.f90: New test.
        * gfortran.dg/gomp/do_concurrent_to_openmp_1.f90: New test.
        * gfortran.dg/gomp/do_concurrent_to_openmp_2.f90: New test.

libgomp/ChangeLog:

        * testsuite/libgomp.fortran/do_concurrent_to_openmp_1.f90: New test.
>From 11bca801826dc967bd57d6da764cb4d246dfb35c Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Tue, 10 Nov 2026 12:30:42 -0800
Subject: [PATCH] fortran: [PR128590]-1 Add -fdo-concurrent-to-openmp=host

Add an option, with the name and values of flang's, that translates DO
CONCURRENT into an OpenMP 'parallel do simd' loop nest with one collapsed
loop per concurrent-control.  The front end synthesizes the gfc_code of
that construct and hands it to gfc_trans_omp_parallel_do_simd, the last
control outermost as in the serial lowering.  The innermost loop runs one
iteration of the construct through gfc_trans_do_concurrent_iteration,
factored out of gfc_trans_forall_1, and each index-name gets a fresh decl
as in the serial lowering.  Like flang's, the option requires -fopenmp.

This patch lowers constructs whose body defines only parts of variables
selected by an index-name.  A construct with a mask, a locality spec,
input/output, an OpenMP or OpenACC directive or any other definition, one
inside an OpenMP or OpenACC construct other than TARGET DATA, or one that
a branch from outside enters, a legacy extension, is translated serially
as before.

Assisted-by: Claude Opus 5.5

	PR fortran/128590

gcc/ChangeLog:

	* flag-types.h (enum gfc_dc_to_omp): New.

gcc/fortran/ChangeLog:

	* invoke.texi: Document -fdo-concurrent-to-openmp.
	* lang.opt (fdo-concurrent-to-openmp=): New option.
	* options.cc (gfc_post_options): Ignore -fdo-concurrent-to-openmp
	with a warning without -fopenmp.
	* trans-openmp.cc (gfc_omp_construct_depth): New variable.
	(struct dc_scan, dc_ignore_expr, dc_index_ref)
	(dc_indexed_designator, dc_construct_local, dc_scan_def)
	(dc_scan_code, struct dc_entry, dc_add_label, dc_entry_label)
	(dc_find_entry, dc_branch_into, dc_build_clauses)
	(dc_retarget_cycle): New.
	(gfc_dc_omp_lowering, gfc_trans_omp_do_concurrent): New functions.
	* trans-stmt.cc (gfc_trans_forall_1): Use
	gfc_trans_do_concurrent_iteration.
	(gfc_trans_do_concurrent_iteration): New function, factored out of
	gfc_trans_forall_1.
	(gfc_trans_do_concurrent): Translate an iteration of a construct
	lowered to OpenMP, and lower a construct to OpenMP if possible.
	* trans-stmt.h (gfc_trans_do_concurrent_iteration): Declare.
	* trans.cc (trans_code): Count the enclosing OpenMP and OpenACC
	constructs in gfc_omp_construct_depth.
	* trans.h (gfc_omp_construct_depth, gfc_dc_omp_lowering)
	(gfc_trans_omp_do_concurrent): Declare.

gcc/testsuite/ChangeLog:

	* gfortran.dg/do_concurrent_to_openmp_1.f90: New test.
	* gfortran.dg/gomp/do_concurrent_to_openmp_1.f90: New test.
	* gfortran.dg/gomp/do_concurrent_to_openmp_2.f90: New test.

libgomp/ChangeLog:

	* testsuite/libgomp.fortran/do_concurrent_to_openmp_1.f90: New test.
---
 gcc/flag-types.h                              |   9 +
 gcc/fortran/invoke.texi                       |  32 +-
 gcc/fortran/lang.opt                          |  13 +
 gcc/fortran/options.cc                        |   7 +
 gcc/fortran/trans-openmp.cc                   | 384 ++++++++++++++++++
 gcc/fortran/trans-stmt.cc                     |  64 +--
 gcc/fortran/trans-stmt.h                      |   1 +
 gcc/fortran/trans.cc                          |  11 +-
 gcc/fortran/trans.h                           |   3 +
 .../gfortran.dg/do_concurrent_to_openmp_1.f90 |  17 +
 .../gomp/do_concurrent_to_openmp_1.f90        |  54 +++
 .../gomp/do_concurrent_to_openmp_2.f90        |  83 ++++
 .../do_concurrent_to_openmp_1.f90             |  72 ++++
 13 files changed, 724 insertions(+), 26 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/do_concurrent_to_openmp_1.f90
 create mode 100644 gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_1.f90
 create mode 100644 gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_2.f90
 create mode 100644 libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_1.f90

diff --git a/gcc/flag-types.h b/gcc/flag-types.h
index c4eb8a0520a..6c2ba66d6df 100644
--- a/gcc/flag-types.h
+++ b/gcc/flag-types.h
@@ -461,6 +461,15 @@ enum gfc_fcoarray
 };
 
 
+/* gfortran -fdo-concurrent-to-openmp= values.  */
+
+enum gfc_dc_to_omp
+{
+  GFC_DC_OMP_NONE = 0,
+  GFC_DC_OMP_HOST
+};
+
+
 /* gfortran -fconvert= values; used for unformatted I/O.
    Keep in sync with GFC_CONVERT_* in gcc/fortran/libgfortran.h.   */
 enum gfc_convert
diff --git a/gcc/fortran/invoke.texi b/gcc/fortran/invoke.texi
index 8d8762b828d..7f780feb9f7 100644
--- a/gcc/fortran/invoke.texi
+++ b/gcc/fortran/invoke.texi
@@ -192,7 +192,9 @@ and warnings}.
 -fbounds-check -ftail-call-workaround -ftail-call-workaround=@var{n}
 -fcheck-array-temporaries
 -fcheck=<all|array-temps|bits|bounds|do|mem|pointer|recursion>
--fcoarray=<none|single|shared|lib> -fexternal-blas -fexternal-blas64 -ff2c
+-fcoarray=<none|single|shared|lib>
+-fdo-concurrent-to-openmp=<none|host>
+-fexternal-blas -fexternal-blas64 -ff2c
 -ffrontend-loop-interchange -ffrontend-optimize
 -finit-character=@var{n} -finit-integer=@var{n} -finit-local-zero
 -finit-derived -finit-logical=<true|false>
@@ -1863,6 +1865,34 @@ albeit it only supports a single image.
 @end table
 
 
+@opindex fdo-concurrent-to-openmp
+@cindex DO CONCURRENT, parallelization
+@cindex parallelization, DO CONCURRENT
+@item -fdo-concurrent-to-openmp=@var{<keyword>}
+Select whether @code{DO CONCURRENT} constructs are translated to OpenMP
+loops.  The option has an effect only together with @option{-fopenmp}.
+
+@table @asis
+@item @samp{none}
+Translate @code{DO CONCURRENT} constructs serially.  (Default)
+
+@item @samp{host}
+Translate @code{DO CONCURRENT} constructs into OpenMP @code{parallel do
+simd} loops that run on multiple host threads.  The number of threads is
+controlled by the usual OpenMP mechanisms, such as the
+@env{OMP_NUM_THREADS} environment variable.
+@end table
+
+The standard permits the iterations of a @code{DO CONCURRENT} construct
+to be executed in any order, so no dependence analysis is done; a
+construct whose iterations are not in fact independent does not conform,
+and this option may expose that as a data race.  A construct is still
+translated serially when it is nested in another @code{DO CONCURRENT}
+construct translated to OpenMP or in an OpenMP or OpenACC construct
+other than @code{TARGET DATA}, when its body contains input/output
+statements or OpenMP or OpenACC directives, and when its translation is
+not yet supported.
+
 @opindex fcheck
 @cindex array, bounds checking
 @cindex bit intrinsics checking
diff --git a/gcc/fortran/lang.opt b/gcc/fortran/lang.opt
index 99713e5cf69..a5082eb5679 100644
--- a/gcc/fortran/lang.opt
+++ b/gcc/fortran/lang.opt
@@ -558,6 +558,19 @@ fdefault-real-16
 Fortran Var(flag_default_real_16)
 Set the default real kind to an 16 byte wide type.
 
+fdo-concurrent-to-openmp=
+Fortran RejectNegative Joined Enum(gfc_dc_to_omp) Var(flag_do_concurrent_to_openmp) Init(GFC_DC_OMP_NONE)
+-fdo-concurrent-to-openmp=<none|host>	Lower DO CONCURRENT constructs to OpenMP loops.
+
+Enum
+Name(gfc_dc_to_omp) Type(enum gfc_dc_to_omp) UnknownError(Unrecognized option: %qs)
+
+EnumValue
+Enum(gfc_dc_to_omp) String(none) Value(GFC_DC_OMP_NONE)
+
+EnumValue
+Enum(gfc_dc_to_omp) String(host) Value(GFC_DC_OMP_HOST)
+
 fdollar-ok
 Fortran Var(flag_dollar_ok)
 Allow dollar signs in entity names.
diff --git a/gcc/fortran/options.cc b/gcc/fortran/options.cc
index 925a1e38dcf..7aef353ddcd 100644
--- a/gcc/fortran/options.cc
+++ b/gcc/fortran/options.cc
@@ -451,6 +451,13 @@ gfc_post_options (const char **pfilename)
 		     "%<-frecursive%> implied by %qs", flag_max_stack_var_size,
 		     flag_openmp ? "-fopenmp" : "-fopenacc");
 
+  if (flag_do_concurrent_to_openmp != GFC_DC_OMP_NONE && !flag_openmp)
+    {
+      gfc_warning_now (0, "%<-fdo-concurrent-to-openmp=%> ignored without "
+		       "%<-fopenmp%>");
+      flag_do_concurrent_to_openmp = GFC_DC_OMP_NONE;
+    }
+
   /* Implement -frecursive as -fmax-stack-var-size=-1.  */
   if (flag_recursive)
     flag_max_stack_var_size = -1;
diff --git a/gcc/fortran/trans-openmp.cc b/gcc/fortran/trans-openmp.cc
index 03fe5099c09..ec7244701a7 100644
--- a/gcc/fortran/trans-openmp.cc
+++ b/gcc/fortran/trans-openmp.cc
@@ -10992,3 +10992,387 @@ gfc_trans_omp_metadirective (gfc_code *code)
 
   return metadirective_tree;
 }
+
+
+/* Lowering of DO CONCURRENT to OpenMP loops (-fdo-concurrent-to-openmp).
+   The front end synthesizes the gfc_code of an equivalent OpenMP construct
+   and hands it to the translation of that construct.  */
+
+/* Number of OpenMP and OpenACC constructs, other than TARGET DATA, around
+   the code being translated, counting DO CONCURRENT constructs lowered to
+   OpenMP.  Written by gfc_trans_code and gfc_trans_omp_do_concurrent.  */
+
+int gfc_omp_construct_depth;
+
+
+/* State of the walk over the body of a DO CONCURRENT construct.  */
+
+struct dc_scan
+{
+  gfc_code *dc;
+  /* Index-names of DC.  */
+  hash_set<gfc_symbol *> index_syms;
+  /* Scopes of BLOCK constructs inside DC.  */
+  hash_set<gfc_namespace *> inner;
+  bool ok;
+};
+
+
+static int
+dc_ignore_expr (gfc_expr **, int *, void *)
+{
+  return 0;
+}
+
+
+static int
+dc_index_ref (gfc_expr **e, int *, void *data)
+{
+  dc_scan *scan = (dc_scan *) data;
+
+  return ((*e)->expr_type == EXPR_VARIABLE
+	  && scan->index_syms.contains ((*e)->symtree->n.sym));
+}
+
+
+/* True if a subscript of designator E references an index-name.  */
+
+static bool
+dc_indexed_designator (dc_scan *scan, gfc_expr *e)
+{
+  for (gfc_ref *r = e->ref; r; r = r->next)
+    if (r->type == REF_ARRAY
+	&& (r->u.ar.type == AR_ELEMENT || r->u.ar.type == AR_SECTION))
+      for (int i = 0; i < r->u.ar.dimen; i++)
+	if (gfc_expr_walker (&r->u.ar.start[i], dc_index_ref, scan)
+	    || gfc_expr_walker (&r->u.ar.end[i], dc_index_ref, scan)
+	    || gfc_expr_walker (&r->u.ar.stride[i], dc_index_ref, scan))
+	  return true;
+
+  return false;
+}
+
+
+/* True if SYM is local to each iteration of the construct.  */
+
+static bool
+dc_construct_local (dc_scan *scan, gfc_symbol *sym)
+{
+  return scan->inner.contains (sym->ns) && sym->attr.save == SAVE_NONE;
+}
+
+
+/* Record that the body defines the designator E.  Only a definition of a
+   part that depends on an index-name is supported.  */
+
+static void
+dc_scan_def (dc_scan *scan, gfc_expr *e)
+{
+  if (e->expr_type != EXPR_VARIABLE
+      || dc_construct_local (scan, e->symtree->n.sym))
+    return;
+
+  if (!dc_indexed_designator (scan, e))
+    scan->ok = false;
+}
+
+
+static int
+dc_scan_code (gfc_code **cp, int *, void *data)
+{
+  dc_scan *scan = (dc_scan *) data;
+  gfc_code *c = *cp;
+
+  /* Parallel execution would reorder I/O, and an OpenMP or OpenACC
+     directive in the body could not nest in the synthesized construct.  */
+  if ((c->op >= EXEC_OPEN && c->op <= EXEC_FLUSH)
+      || c->op >= EXEC_OACC_KERNELS_LOOP)
+    scan->ok = false;
+
+  switch (c->op)
+    {
+    case EXEC_BLOCK:
+      scan->inner.add (c->ext.block.ns);
+      /* An associate-name of a variable selector aliases the selector.  */
+      for (gfc_association_list *a = c->ext.block.assoc; a; a = a->next)
+	if (a->target)
+	  dc_scan_def (scan, a->target);
+      break;
+
+    case EXEC_ASSIGN:
+      dc_scan_def (scan, c->expr1);
+      break;
+
+    case EXEC_DO:
+      dc_scan_def (scan, c->ext.iterator->var);
+      break;
+
+    case EXEC_POINTER_ASSIGN:
+    case EXEC_ALLOCATE:
+    case EXEC_DEALLOCATE:
+    case EXEC_CALL:
+    case EXEC_COMPCALL:
+    case EXEC_ASSIGN_CALL:
+    case EXEC_CALL_PPC:
+      scan->ok = false;
+      break;
+
+    default:
+      break;
+    }
+
+  return !scan->ok;
+}
+
+
+/* State of the search for a branch into a DO CONCURRENT construct.  */
+
+struct dc_entry
+{
+  gfc_code *dc;
+  hash_set<gfc_st_label *> labels;
+  gfc_code *branch;
+};
+
+
+static int
+dc_add_label (gfc_code **cp, int *, void *data)
+{
+  if ((*cp)->here)
+    ((dc_entry *) data)->labels.add ((*cp)->here);
+  return 0;
+}
+
+
+static bool
+dc_entry_label (dc_entry *s, gfc_st_label *label)
+{
+  return label && s->labels.contains (label);
+}
+
+
+static int
+dc_find_entry (gfc_code **cp, int *walk_subtrees, void *data)
+{
+  dc_entry *s = (dc_entry *) data;
+  gfc_code *c = *cp;
+
+  if (c == s->dc)
+    {
+      *walk_subtrees = 0;
+      return 0;
+    }
+
+  bool entry = (dc_entry_label (s, c->label1) || dc_entry_label (s, c->label2)
+		|| dc_entry_label (s, c->label3));
+  /* The label list of an assigned GOTO.  */
+  if (c->op == EXEC_GOTO)
+    for (gfc_code *b = c->block; b; b = b->block)
+      entry |= dc_entry_label (s, b->label1);
+  if (c->op == EXEC_READ || c->op == EXEC_WRITE)
+    entry |= (dc_entry_label (s, c->ext.dt->err)
+	      || dc_entry_label (s, c->ext.dt->end)
+	      || dc_entry_label (s, c->ext.dt->eor));
+
+  if (entry)
+    s->branch = c;
+  return entry;
+}
+
+
+/* A statement outside the DO CONCURRENT construct CODE that branches into
+   it, or NULL.  Branching into a block is a legacy extension; an OpenMP
+   construct cannot be entered that way.  */
+
+static gfc_code *
+dc_branch_into (gfc_code *code)
+{
+  dc_entry s;
+  s.dc = code;
+  s.branch = NULL;
+  gfc_code_walker (&code->block->next, dc_add_label, dc_ignore_expr, &s);
+  if (s.labels.is_empty ())
+    return NULL;
+
+  gfc_namespace *ns = gfc_current_ns;
+  while (ns->construct_entities && ns->parent)
+    ns = ns->parent;
+  gfc_code_walker (&ns->code, dc_find_entry, dc_ignore_expr, &s);
+  return s.branch;
+}
+
+
+/* Return how the DO CONCURRENT construct CODE is lowered to OpenMP;
+   GFC_DC_OMP_NONE keeps the serial lowering.  */
+
+enum gfc_dc_to_omp
+gfc_dc_omp_lowering (gfc_code *code)
+{
+  gcc_assert (code->op == EXEC_DO_CONCURRENT);
+
+  if (flag_do_concurrent_to_openmp == GFC_DC_OMP_NONE
+      || gfc_omp_construct_depth > 0
+      || code->ext.concur.forall_iterator == NULL
+      || code->expr1)
+    return GFC_DC_OMP_NONE;
+
+  for (int i = 0; i < LOCALITY_NUM; i++)
+    if (code->ext.concur.locality[i])
+      return GFC_DC_OMP_NONE;
+
+  dc_scan scan;
+  scan.dc = code;
+  scan.ok = true;
+  for (gfc_forall_iterator *fa = code->ext.concur.forall_iterator;
+       fa; fa = fa->next)
+    scan.index_syms.add (fa->var->symtree->n.sym);
+
+  gfc_code_walker (&code->block->next, dc_scan_code, dc_ignore_expr, &scan);
+  if (!scan.ok)
+    return GFC_DC_OMP_NONE;
+
+  if (dc_branch_into (code))
+    return GFC_DC_OMP_NONE;
+
+  return GFC_DC_OMP_HOST;
+}
+
+
+/* Build the gfc_omp_clauses of the OpenMP loop for CODE.  F2018 C1123
+   forbids a concurrent-limit or concurrent-step from referencing an
+   index-name of the same construct, so the nest is rectangular.  */
+
+static gfc_omp_clauses *
+dc_build_clauses (gfc_code *code, int collapse)
+{
+  gfc_omp_clauses *clauses = gfc_get_omp_clauses ();
+
+  clauses->collapse = collapse;
+  clauses->default_sharing = OMP_DEFAULT_SHARED;
+  clauses->loc = code->loc;
+
+  return clauses;
+}
+
+
+/* Point every CYCLE in the statement list C that targets construct FROM
+   at construct TO instead.  */
+
+static void
+dc_retarget_cycle (gfc_code *c, gfc_code *from, gfc_code *to)
+{
+  for (; c; c = c->next)
+    {
+      if (c->op == EXEC_CYCLE && c->ext.which_construct == from)
+	c->ext.which_construct = to;
+
+      if (c->op == EXEC_BLOCK && c->ext.block.ns)
+	dc_retarget_cycle (c->ext.block.ns->code, from, to);
+
+      for (gfc_code *b = c->block; b; b = b->block)
+	dc_retarget_cycle (b->next, from, to);
+    }
+}
+
+
+/* Translate the DO CONCURRENT construct CODE into an OpenMP loop.  */
+
+tree
+gfc_trans_omp_do_concurrent (gfc_code *code)
+{
+  gfc_forall_iterator *fa;
+  gfc_code *par, *iter, *inner = NULL, *first = NULL;
+  int collapse = 0, n = 0;
+  stmtblock_t block;
+  tree stmt;
+
+  for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
+    collapse++;
+
+  gfc_start_block (&block);
+
+  /* An index-name has the scope of the construct (F2018 19.4), so give
+     each one a fresh decl, as the serial lowering does.  */
+  gfc_saved_var *saved = XALLOCAVEC (gfc_saved_var, collapse);
+  for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next, n++)
+    {
+      gfc_symbol *sym = fa->var->symtree->n.sym;
+      tree var = gfc_create_var (gfc_typenode_for_spec (&sym->ts), sym->name);
+      gfc_shadow_sym (sym, var, &saved[n]);
+    }
+
+  /* Build the chain of EXEC_DO nodes, one per concurrent-control, that
+     the collapse walk expects.  As in the serial lowering, the last
+     control is the outermost loop; index_interchange orders the controls
+     for that.  The iterators alias expressions owned by CODE; only the
+     synthesized nodes are freed below.  */
+  auto_vec<gfc_forall_iterator *> controls;
+  for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
+    controls.safe_insert (0, fa);
+  for (gfc_forall_iterator *ctl : controls)
+    {
+      gfc_code *d = gfc_get_code (EXEC_DO);
+      gfc_iterator *it = gfc_get_iterator ();
+
+      it->var = ctl->var;
+      it->start = ctl->start;
+      it->end = ctl->end;
+      it->step = ctl->stride;
+      it->annot = ctl->annot;
+
+      d->loc = code->loc;
+      d->ext.iterator = it;
+      d->block = gfc_get_code (EXEC_DO);
+      d->block->loc = code->loc;
+
+      if (inner)
+	inner->block->next = d;
+      else
+	first = d;
+      inner = d;
+    }
+
+  /* The innermost loop runs one iteration of CODE: a DO CONCURRENT
+     without concurrent-controls that shares CODE's body and locality
+     specs, and that a CYCLE of CODE targets meanwhile.  */
+  iter = gfc_get_code (EXEC_DO_CONCURRENT);
+  iter->loc = code->loc;
+  iter->block = code->block;
+  memcpy (iter->ext.concur.locality, code->ext.concur.locality,
+	  sizeof (iter->ext.concur.locality));
+  inner->block->next = iter;
+  dc_retarget_cycle (code->block->next, code, iter);
+
+  /* 'parallel do simd': the SIMD part keeps the vectorization that the
+     serial lowering gets from its implicit IVDEP annotation.  */
+  par = gfc_get_code (EXEC_OMP_PARALLEL_DO_SIMD);
+  par->loc = code->loc;
+  par->ext.omp_clauses = dc_build_clauses (code, collapse);
+  par->block = gfc_get_code (EXEC_OMP_PARALLEL_DO_SIMD);
+  par->block->loc = code->loc;
+  par->block->next = first;
+
+  gfc_omp_construct_depth++;
+  stmt = gfc_trans_omp_parallel_do_simd (par, NULL, NULL);
+  gfc_omp_construct_depth--;
+  gfc_add_expr_to_block (&block, stmt);
+
+  dc_retarget_cycle (code->block->next, iter, code);
+  n = 0;
+  for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next, n++)
+    gfc_restore_sym (fa->var->symtree->n.sym, &saved[n]);
+
+  gfc_free_omp_clauses (par->ext.omp_clauses);
+  free (par->block);
+  free (par);
+  for (gfc_code *d = first, *nextd; d; d = nextd)
+    {
+      nextd = d == inner ? NULL : d->block->next;
+      free (d->ext.iterator);
+      free (d->block);
+      free (d);
+    }
+  free (iter);
+
+  return gfc_finish_block (&block);
+}
diff --git a/gcc/fortran/trans-stmt.cc b/gcc/fortran/trans-stmt.cc
index 9f5e408b3e0..10903505cac 100644
--- a/gcc/fortran/trans-stmt.cc
+++ b/gcc/fortran/trans-stmt.cc
@@ -5402,7 +5402,6 @@ gfc_trans_forall_1 (gfc_code * code, forall_info * nested_forall_info)
   tree maskindex;
   tree mask;
   tree pmask;
-  tree cycle_label = NULL_TREE;
   int n;
   int nvar;
   int need_temp;
@@ -5607,29 +5606,7 @@ gfc_trans_forall_1 (gfc_code * code, forall_info * nested_forall_info)
 
   if (code->op == EXEC_DO_CONCURRENT)
     {
-      gfc_init_block (&body);
-      cycle_label = gfc_build_label_decl (NULL_TREE);
-      code->cycle_label = cycle_label;
-
-      /* Handle LOCAL and LOCAL_INIT.  */
-      std::vector<symbol_and_tree_t> saved_decls;
-      gfc_trans_concurrent_locality_spec (false, &body, &saved_decls,
-					  code->ext.concur.locality);
-
-      /* Translate the body.  */
-      tmp = gfc_trans_code (code->block->next);
-      gfc_add_expr_to_block (&body, tmp);
-
-      /* Reset locality variables. */
-      gfc_trans_concurrent_locality_spec (true, &body, &saved_decls,
-					  code->ext.concur.locality);
-      if (TREE_USED (cycle_label))
-	{
-	  tmp = build1_v (LABEL_EXPR, cycle_label);
-	  gfc_add_expr_to_block (&body, tmp);
-	}
-
-      tmp = gfc_finish_block (&body);
+      tmp = gfc_trans_do_concurrent_iteration (code);
       nested_forall_info->do_concurrent = true;
       tmp = gfc_trans_nested_forall_loop (nested_forall_info, tmp, 1);
       gfc_add_expr_to_block (&block, tmp);
@@ -5770,8 +5747,47 @@ tree gfc_trans_forall (gfc_code * code)
 
 /* Translate the DO CONCURRENT construct.  */
 
+/* Translate one iteration of the DO CONCURRENT construct CODE: its body,
+   with the variables of LOCAL and LOCAL_INIT locality local to it.  */
+
+tree
+gfc_trans_do_concurrent_iteration (gfc_code *code)
+{
+  stmtblock_t body;
+  tree tmp;
+
+  gfc_init_block (&body);
+  code->cycle_label = gfc_build_label_decl (NULL_TREE);
+
+  std::vector<symbol_and_tree_t> saved_decls;
+  gfc_trans_concurrent_locality_spec (false, &body, &saved_decls,
+				      code->ext.concur.locality);
+
+  tmp = gfc_trans_code (code->block->next);
+  gfc_add_expr_to_block (&body, tmp);
+
+  gfc_trans_concurrent_locality_spec (true, &body, &saved_decls,
+				      code->ext.concur.locality);
+  if (TREE_USED (code->cycle_label))
+    {
+      tmp = build1_v (LABEL_EXPR, code->cycle_label);
+      gfc_add_expr_to_block (&body, tmp);
+    }
+
+  return gfc_finish_block (&body);
+}
+
+
 tree gfc_trans_do_concurrent (gfc_code * code)
 {
+  /* A construct without concurrent-controls is one iteration of a
+     construct lowered to OpenMP by gfc_trans_omp_do_concurrent.  */
+  if (code->ext.concur.forall_iterator == NULL)
+    return gfc_trans_do_concurrent_iteration (code);
+
+  if (gfc_dc_omp_lowering (code) != GFC_DC_OMP_NONE)
+    return gfc_trans_omp_do_concurrent (code);
+
   return gfc_trans_forall_1 (code, NULL);
 }
 
diff --git a/gcc/fortran/trans-stmt.h b/gcc/fortran/trans-stmt.h
index 9e4c3aa43f8..7d25b93a6df 100644
--- a/gcc/fortran/trans-stmt.h
+++ b/gcc/fortran/trans-stmt.h
@@ -50,6 +50,7 @@ tree gfc_trans_arithmetic_if (gfc_code *);
 tree gfc_trans_block_construct (gfc_code *);
 tree gfc_trans_do (gfc_code *, tree);
 tree gfc_trans_do_concurrent (gfc_code *);
+tree gfc_trans_do_concurrent_iteration (gfc_code *);
 tree gfc_trans_do_while (gfc_code *);
 tree gfc_trans_select (gfc_code *);
 tree gfc_trans_select_type (gfc_code *);
diff --git a/gcc/fortran/trans.cc b/gcc/fortran/trans.cc
index 7d6183a6c64..7afe396234f 100644
--- a/gcc/fortran/trans.cc
+++ b/gcc/fortran/trans.cc
@@ -2714,7 +2714,14 @@ trans_code (gfc_code * code, tree cond)
 	case EXEC_OMP_TILE:
 	case EXEC_OMP_UNROLL:
 	case EXEC_OMP_WORKSHARE:
-	  res = gfc_trans_omp_directive (code);
+	  {
+	    /* A DO CONCURRENT inside TARGET DATA may still become an
+	       OpenMP loop.  */
+	    int depth = code->op != EXEC_OMP_TARGET_DATA;
+	    gfc_omp_construct_depth += depth;
+	    res = gfc_trans_omp_directive (code);
+	    gfc_omp_construct_depth -= depth;
+	  }
 	  break;
 
 	case EXEC_OACC_CACHE:
@@ -2736,7 +2743,9 @@ trans_code (gfc_code * code, tree cond)
 	case EXEC_OACC_INIT:
 	case EXEC_OACC_SHUTDOWN:
 	case EXEC_OACC_SET:
+	  gfc_omp_construct_depth++;
 	  res = gfc_trans_oacc_directive (code);
+	  gfc_omp_construct_depth--;
 	  break;
 
 	default:
diff --git a/gcc/fortran/trans.h b/gcc/fortran/trans.h
index b0bc38fc3cc..c6b360ce2e6 100644
--- a/gcc/fortran/trans.h
+++ b/gcc/fortran/trans.h
@@ -857,6 +857,9 @@ bool gfc_omp_private_debug_clause (tree, bool);
 bool gfc_omp_private_outer_ref (tree);
 struct gimplify_omp_ctx;
 void gfc_omp_firstprivatize_type_sizes (struct gimplify_omp_ctx *, tree);
+extern int gfc_omp_construct_depth;
+enum gfc_dc_to_omp gfc_dc_omp_lowering (gfc_code *);
+tree gfc_trans_omp_do_concurrent (gfc_code *);
 
 /* In trans-intrinsic.cc.  */
 void gfc_conv_intrinsic_mvbits (gfc_se *, gfc_actual_arglist *,
diff --git a/gcc/testsuite/gfortran.dg/do_concurrent_to_openmp_1.f90 b/gcc/testsuite/gfortran.dg/do_concurrent_to_openmp_1.f90
new file mode 100644
index 00000000000..84d6623d568
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/do_concurrent_to_openmp_1.f90
@@ -0,0 +1,17 @@
+! { dg-do compile }
+! { dg-options "-fdo-concurrent-to-openmp=host -fdump-tree-original" }
+! PR 128590
+!
+! Without -fopenmp the option is ignored with a warning.
+
+! { dg-warning "'-fdo-concurrent-to-openmp=' ignored without '-fopenmp'" "" { target *-*-* } 0 }
+
+subroutine s (a, n)
+  integer :: n, i
+  real :: a(n)
+  do concurrent (i = 1:n)
+     a(i) = 0.0
+  end do
+end subroutine s
+
+! { dg-final { scan-tree-dump-not "#pragma omp" "original" } }
diff --git a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_1.f90 b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_1.f90
new file mode 100644
index 00000000000..87676cf449b
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_1.f90
@@ -0,0 +1,54 @@
+! { dg-do compile }
+! { dg-additional-options "-fdo-concurrent-to-openmp=host -fdump-tree-original" }
+! PR 128590
+!
+! -fdo-concurrent-to-openmp=host lowers DO CONCURRENT to an OpenMP
+! 'parallel do simd' that collapses one loop per concurrent-control, the
+! last one outermost as in the serial lowering.
+
+subroutine one_control (a, b, n)
+  integer :: n, i
+  real :: a(n), b(n)
+  do concurrent (i = 1:n)
+     b(i) = a(i) * a(i)
+  end do
+end subroutine one_control
+
+subroutine two_controls (c, m)
+  integer :: m, i, j
+  real :: c(m,m)
+  do concurrent (i = 1:m, j = 1:m)
+     c(i,j) = real(i) + real(j)
+  end do
+end subroutine two_controls
+
+subroutine strided (a, n)
+  integer :: n, i
+  real :: a(n)
+  do concurrent (i = n:1:-3)
+     a(i) = 0.0
+  end do
+end subroutine strided
+
+subroutine with_cycle (a, n)
+  integer :: n, i
+  real :: a(n)
+  do concurrent (i = 1:n)
+     if (a(i) < 0.0) cycle
+     a(i) = sqrt (a(i))
+  end do
+end subroutine with_cycle
+
+subroutine type_spec (a, n)
+  integer :: n
+  real :: a(n)
+  do concurrent (integer(8) :: k = 1:n)
+     a(k) = 1.0
+  end do
+end subroutine type_spec
+
+! { dg-final { scan-tree-dump-times "#pragma omp parallel" 5 "original" } }
+! { dg-final { scan-tree-dump-times "#pragma omp for" 5 "original" } }
+! { dg-final { scan-tree-dump-times "#pragma omp simd" 5 "original" } }
+! { dg-final { scan-tree-dump-times "collapse\\(2\\)" 2 "original" } }
+! { dg-final { scan-tree-dump "for \\(j\\.\[0-9\]+ = 1; j\\.\[0-9\]+ <= \[^\n\]*\n *for \\(i\\.\[0-9\]+ = 1" "original" } }
diff --git a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_2.f90 b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_2.f90
new file mode 100644
index 00000000000..e51461d1996
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_2.f90
@@ -0,0 +1,83 @@
+! { dg-do compile }
+! { dg-additional-options "-fdo-concurrent-to-openmp=host -fdump-tree-original" }
+! PR 128590
+!
+! DO CONCURRENT constructs that keep the serial lowering: parallel
+! execution would reorder I/O, an OpenMP directive in the body or around
+! the construct would be nested illegally or oversubscribe the threads,
+! and a construct nested in a lowered one would put a parallel region
+! inside a simd region.  TARGET DATA does not block the lowering.  A
+! branch from outside into the construct, a legacy extension, could not
+! enter an OpenMP construct.
+
+subroutine has_io (a, n)
+  integer :: n, i
+  real :: a(n)
+  do concurrent (i = 1:n)
+     write (*,*) a(i)
+  end do
+end subroutine has_io
+
+subroutine directive_in_body (a, n)
+  integer :: n, i
+  real :: a(n)
+  do concurrent (i = 1:n)
+     !$omp atomic
+     a(i) = a(i) + 1.0
+  end do
+end subroutine directive_in_body
+
+subroutine inside_parallel (a, n)
+  integer :: n, i
+  real :: a(n)
+  !$omp parallel
+  do concurrent (i = 1:n)
+     a(i) = 0.0
+  end do
+  !$omp end parallel
+end subroutine inside_parallel
+
+subroutine inside_target (a, n)
+  integer :: n, i
+  real :: a(n)
+  !$omp target
+  do concurrent (i = 1:n)
+     a(i) = 0.0
+  end do
+  !$omp end target
+end subroutine inside_target
+
+subroutine nested (a, n)
+  integer :: n, i, j
+  real :: a(n,n)
+  do concurrent (i = 1:n)
+     do concurrent (j = 1:n)
+        a(i,j) = real(i) * real(j)
+     end do
+  end do
+end subroutine nested
+
+subroutine inside_target_data (a, n)
+  integer :: n, i
+  real :: a(n)
+  !$omp target data map(tofrom: a)
+  do concurrent (i = 1:n)
+     a(i) = 1.0
+  end do
+  !$omp end target data
+end subroutine inside_target_data
+
+subroutine branch_into (a, n)
+  integer :: n, i
+  real :: a(n)
+  if (n > 2) goto 10 ! { dg-warning "Label at .1. is not in the same block" }
+  do concurrent (i = 1:n)
+     a(i) = 2.0
+10 end do ! { dg-warning "Label at .1. is not in the same block" }
+! { dg-warning "ignoring loop annotation" "" { target *-*-* } .-1 }
+end subroutine branch_into
+
+! One 'parallel' each from inside_parallel, nested and inside_target_data.
+! { dg-final { scan-tree-dump-times "#pragma omp parallel" 3 "original" } }
+! { dg-final { scan-tree-dump-times "#pragma omp simd" 2 "original" } }
+! { dg-final { scan-tree-dump-times "#pragma omp atomic" 1 "original" } }
diff --git a/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_1.f90 b/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_1.f90
new file mode 100644
index 00000000000..f3412c3be02
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_1.f90
@@ -0,0 +1,72 @@
+! { dg-do run }
+! { dg-additional-options "-fdo-concurrent-to-openmp=host" }
+! PR 128590
+!
+! Results of DO CONCURRENT constructs lowered to OpenMP loops, including
+! an index-name that shadows an outer variable, which the construct must
+! leave unchanged (F2018 19.4).
+
+program main
+  implicit none
+  integer, parameter :: n = 1000, m = 37
+  integer :: i, j
+  real :: a(n), b(n), c(m,m)
+
+  do i = 1, n
+     a(i) = real(i) - 500.0
+  end do
+
+  i = -7
+  do concurrent (i = 1:n)
+     b(i) = 2.0 * a(i)
+  end do
+  if (i /= -7) stop 1
+  do j = 1, n
+     if (b(j) /= 2.0 * a(j)) stop 2
+  end do
+
+  i = -7
+  j = -8
+  do concurrent (i = 1:m, j = 1:m)
+     c(i,j) = real(100 * i + j)
+  end do
+  if (i /= -7 .or. j /= -8) stop 3
+  do j = 1, m
+     do i = 1, m
+        if (c(i,j) /= real(100 * i + j)) stop 4
+     end do
+  end do
+
+  b = -1.0
+  do concurrent (i = n:1:-3)
+     b(i) = real(i)
+  end do
+  do j = 1, n
+     if (mod(n - j, 3) == 0) then
+        if (b(j) /= real(j)) stop 5
+     else if (b(j) /= -1.0) then
+        stop 6
+     end if
+  end do
+
+  b = 0.0
+  do concurrent (i = 1:n)
+     if (a(i) < 0.0) cycle
+     b(i) = sqrt (a(i))
+  end do
+  do j = 1, n
+     if (a(j) < 0.0) then
+        if (b(j) /= 0.0) stop 7
+     else if (b(j) /= sqrt (a(j))) then
+        stop 8
+     end if
+  end do
+
+  b = 0.0
+  do concurrent (integer(8) :: k = 1:n)
+     b(k) = real(k)
+  end do
+  do j = 1, n
+     if (b(j) /= real(j)) stop 9
+  end do
+end program main
-- 
2.56.1

Reply via email to