See attached patch, 3/10 of the DO CONCURRENT to OpenMP series. This
is the part to review closely.
Start at dc_scan_body in trans-openmp.cc. It walks the body with
dc_scan_stmt, which handles a statement without the statements it
contains and then walks those, keeping the set of variables whose whole
definition precedes that point on every path through the iteration.
dc_dom_restore trims the set on leaving a construct.
A definition inside an IF, SELECT or loop construct counts only inside
it, one inside a BLOCK or ASSOCIATE construct also after it, and a
label clears the set.
For each variable of unspecified locality that the body defines as a
whole, struct dc_use records whether some reference is outside that set
and whether every iteration defines it. After a CYCLE, a GOTO or an
EXIT from a BLOCK construct, no definition counts as made by every
iteration.
The end of dc_scan_body keeps the construct serial when a reference
comes first or dc_privatizable rejects the variable (not an intrinsic
scalar, or visible to a called procedure). Otherwise dc_build_clauses
adds the lastprivate clauses.
dc_scan_index_def and the loop in dc_scan_body let b(k) stay shared
after k = 2*i. The dc_scan_def, dc_scan_call and dc_scan_code changes
feed definitions, calls and allocations into the scan.
Regards,
Jerry
---
fortran: [PR128590]-3 Privatize DO CONCURRENT iteration-local variables
A variable of unspecified locality that the body of a DO CONCURRENT
defines as a whole must not be shared between threads, yet it keeps the
value from the one iteration that defines it, or its old value if none
does (F2018 11.1.7.5). Give such scalars of intrinsic type a conditional
lastprivate copy; without the modifier their value would be unspecified
after a construct without iterations. Assignments, DO variables, and
INTENT(OUT) and INTENT(INOUT) actual arguments are tracked as definitions;
one counts for the references that follow it in its own construct, and
after a BLOCK construct that contains it, but not past a label. A part of
a variable selected by an index-name, by a substring bound or subscript
referencing one, or by a scalar integer set from one, stays shared.
A construct stays serial if a path through its body references such a
variable before defining it, which is usually an accumulation lacking
REDUCE; if it defines another part of a variable, a whole non-scalar
variable, or a variable that a called procedure might see; or if it
allocates or pointer-assigns a variable. So does a construct with a !GCC$
UNROLL or NOVECTOR directive, which the OpenMP loop would not honor.
Assisted-by: Claude Opus 5.5
PR fortran/128590
gcc/fortran/ChangeLog:
* invoke.texi: Document the privatization.
* trans-openmp.cc (struct dc_use, enum dc_def_kind): New.
(struct dc_scan): Record the uses of variables, index-derived
variables and calls.
(dc_indexed_expr, dc_whole_designator, dc_use_of)
(dc_scan_index_def, dc_scan_call, dc_scan_call_args, dc_scan_expr)
(dc_privatizable, dc_dom_restore, dc_scan_stmt, dc_scan_list)
(dc_scan_body): New.
(dc_locality_known): Also accept the index-names.
(dc_scan_def): Record whole and part definitions.
(dc_scan_code): Handle calls, allocations, pointer assignments,
branches and the iteration variables of DO loops.
(gfc_dc_omp_lowering): Keep constructs with UNROLL or NOVECTOR
directives serial. Use dc_scan_body.
(dc_build_clauses): Add lastprivate clauses.
(gfc_trans_omp_do_concurrent): Pass the walk of the body to it.
gcc/testsuite/ChangeLog:
* gfortran.dg/gomp/do_concurrent_to_openmp_4.f90: New test.
* gfortran.dg/gomp/do_concurrent_to_openmp_5.f90: New test.
libgomp/ChangeLog:
* testsuite/libgomp.fortran/do_concurrent_to_openmp_3.f90: New test.
>From ade4ce126c150ba35d9c9a748ea9b4c6e33aab09 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Tue, 10 Nov 2026 12:31:03 -0800
Subject: [PATCH] fortran: [PR128590]-3 Privatize DO CONCURRENT iteration-local
variables
A variable of unspecified locality that the body of a DO CONCURRENT
defines as a whole must not be shared between threads, yet it keeps the
value from the one iteration that defines it, or its old value if none
does (F2018 11.1.7.5). Give such scalars of intrinsic type a conditional
lastprivate copy; without the modifier their value would be unspecified
after a construct without iterations. Assignments, DO variables, and
INTENT(OUT) and INTENT(INOUT) actual arguments are tracked as definitions;
one counts for the references that follow it in its own construct, and
after a BLOCK construct that contains it, but not past a label. A part of
a variable selected by an index-name, by a substring bound or subscript
referencing one, or by a scalar integer set from one, stays shared.
A construct stays serial if a path through its body references such a
variable before defining it, which is usually an accumulation lacking
REDUCE; if it defines another part of a variable, a whole non-scalar
variable, or a variable that a called procedure might see; or if it
allocates or pointer-assigns a variable. So does a construct with a !GCC$
UNROLL or NOVECTOR directive, which the OpenMP loop would not honor.
Assisted-by: Claude Opus 5.5
PR fortran/128590
gcc/fortran/ChangeLog:
* invoke.texi: Document the privatization.
* trans-openmp.cc (struct dc_use, enum dc_def_kind): New.
(struct dc_scan): Record the uses of variables, index-derived
variables and calls.
(dc_indexed_expr, dc_whole_designator, dc_use_of)
(dc_scan_index_def, dc_scan_call, dc_scan_call_args, dc_scan_expr)
(dc_privatizable, dc_dom_restore, dc_scan_stmt, dc_scan_list)
(dc_scan_body): New.
(dc_locality_known): Also accept the index-names.
(dc_scan_def): Record whole and part definitions.
(dc_scan_code): Handle calls, allocations, pointer assignments,
branches and the iteration variables of DO loops.
(gfc_dc_omp_lowering): Keep constructs with UNROLL or NOVECTOR
directives serial. Use dc_scan_body.
(dc_build_clauses): Add lastprivate clauses.
(gfc_trans_omp_do_concurrent): Pass the walk of the body to it.
gcc/testsuite/ChangeLog:
* gfortran.dg/gomp/do_concurrent_to_openmp_4.f90: New test.
* gfortran.dg/gomp/do_concurrent_to_openmp_5.f90: New test.
libgomp/ChangeLog:
* testsuite/libgomp.fortran/do_concurrent_to_openmp_3.f90: New test.
---
gcc/fortran/invoke.texi | 7 +-
gcc/fortran/trans-openmp.cc | 553 ++++++++++++++++--
.../gomp/do_concurrent_to_openmp_4.f90 | 124 ++++
.../gomp/do_concurrent_to_openmp_5.f90 | 194 ++++++
.../do_concurrent_to_openmp_3.f90 | 149 +++++
5 files changed, 992 insertions(+), 35 deletions(-)
create mode 100644 gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_4.f90
create mode 100644 gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_5.f90
create mode 100644 libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_3.f90
diff --git a/gcc/fortran/invoke.texi b/gcc/fortran/invoke.texi
index 3af2373a450..a228275cb66 100644
--- a/gcc/fortran/invoke.texi
+++ b/gcc/fortran/invoke.texi
@@ -1883,7 +1883,12 @@ controlled by the usual OpenMP mechanisms, such as the
@env{OMP_NUM_THREADS} environment variable. Variables with
@code{LOCAL} or @code{LOCAL_INIT} locality are local to each iteration,
@code{SHARED} variables are shared, and @code{REDUCE} specifiers become
-OpenMP reductions.
+OpenMP reductions. A scalar variable of intrinsic type with unspecified
+locality that the body defines as a whole gets a private copy in each
+thread, and afterwards has the value from the iteration that defined it
+last. A construct whose body references such a variable before
+defining it, as an accumulation without a @code{REDUCE} locality spec
+does, is translated serially.
@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 2f125daea02..66250786e76 100644
--- a/gcc/fortran/trans-openmp.cc
+++ b/gcc/fortran/trans-openmp.cc
@@ -11005,15 +11005,58 @@ gfc_trans_omp_metadirective (gfc_code *code)
int gfc_omp_construct_depth;
+/* How the body of a DO CONCURRENT construct uses a variable of unspecified
+ locality: whether it defines the whole variable, whether every
+ iteration executes such a definition, and whether some reference is not
+ preceded by one on every path from the start of the iteration. */
+
+struct dc_use
+{
+ bool defined;
+ bool udef;
+ bool ref_first;
+};
+
+
/* State of the walk over the body of a DO CONCURRENT construct. */
struct dc_scan
{
gfc_code *dc;
- /* Index-names of DC. */
+ /* Index-names of DC, and scalar integer variables that every
+ definition in the body sets from members of this set. */
hash_set<gfc_symbol *> index_syms;
+ /* Candidates for INDEX_SYMS found by this walk, and variables that a
+ definition sets otherwise. */
+ auto_vec<gfc_symbol *> index_defs;
+ hash_set<gfc_symbol *> nonindex_defs;
/* Scopes of BLOCK constructs inside DC. */
hash_set<gfc_namespace *> inner;
+ /* Designators that the body defines without referencing them. */
+ hash_set<gfc_expr *> defs;
+ /* Uses of variables of unspecified locality, and the variables in the
+ order of their first use. */
+ hash_map<gfc_symbol *, dc_use> uses;
+ auto_vec<gfc_symbol *> order;
+ /* Variables whose whole definition precedes the code being walked on
+ every path from the start of the iteration, in the order added. */
+ hash_set<gfc_symbol *> dom;
+ auto_vec<gfc_symbol *> dom_order;
+ /* Whole variables that the statement being walked defines. */
+ auto_vec<gfc_symbol *> pending;
+ /* Number of constructs around the code being walked whose body an
+ iteration may skip or repeat. */
+ int cond_depth;
+ /* A CYCLE of DC, a GOTO, an arithmetic IF or an EXIT from a construct
+ other than a loop appears in the statement being walked, or before
+ it; the statements after it may be skipped. */
+ bool branch_here, branched;
+ /* The body defines a part of a variable that does not depend on
+ INDEX_SYMS. */
+ bool unindexed_def;
+ /* 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;
bool ok;
};
@@ -11035,7 +11078,17 @@ dc_index_ref (gfc_expr **e, int *, void *data)
}
-/* True if a subscript of designator E references an index-name. */
+/* True if expression E references a member of INDEX_SYMS. */
+
+static bool
+dc_indexed_expr (dc_scan *scan, gfc_expr *e)
+{
+ return e && gfc_expr_walker (&e, dc_index_ref, scan) != 0;
+}
+
+
+/* True if a subscript or substring bound of designator E references a
+ member of INDEX_SYMS. */
static bool
dc_indexed_designator (dc_scan *scan, gfc_expr *e)
@@ -11043,22 +11096,44 @@ 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;
+ {
+ for (int i = 0; i < r->u.ar.dimen; i++)
+ if (dc_indexed_expr (scan, r->u.ar.start[i])
+ || dc_indexed_expr (scan, r->u.ar.end[i])
+ || dc_indexed_expr (scan, r->u.ar.stride[i]))
+ return true;
+ }
+ else if (r->type == REF_SUBSTRING
+ && (dc_indexed_expr (scan, r->u.ss.start)
+ || dc_indexed_expr (scan, r->u.ss.end)))
+ return true;
return false;
}
+/* True if designator E denotes a whole variable. */
+
+static bool
+dc_whole_designator (gfc_expr *e)
+{
+ return (e->ref == NULL
+ || (e->ref->next == NULL && e->ref->type == REF_ARRAY
+ && e->ref->u.ar.type == AR_FULL));
+}
+
+
/* True if the scoping or a locality spec of the construct decides the
locality of SYM. */
static bool
dc_locality_known (dc_scan *scan, gfc_symbol *sym)
{
+ for (gfc_forall_iterator *fa = scan->dc->ext.concur.forall_iterator;
+ fa; fa = fa->next)
+ if (fa->var->symtree->n.sym == sym)
+ return true;
+
if (scan->inner.contains (sym->ns) && sym->attr.save == SAVE_NONE)
return true;
@@ -11073,18 +11148,158 @@ dc_locality_known (dc_scan *scan, gfc_symbol *sym)
}
-/* Record that the body defines the designator E. Only a definition of a
- part that depends on an index-name is supported. */
+/* The use record of SYM. */
+
+static dc_use &
+dc_use_of (dc_scan *scan, gfc_symbol *sym)
+{
+ bool existed;
+ dc_use &use = scan->uses.get_or_insert (sym, &existed);
+
+ if (!existed)
+ {
+ use.defined = use.udef = use.ref_first = false;
+ scan->order.safe_push (sym);
+ }
+
+ return use;
+}
+
+
+/* How a definition of a designator relates to its old value. */
+
+enum dc_def_kind
+{
+ /* An assignment, or an INTENT(OUT) actual argument. */
+ DC_DEF,
+ /* An INTENT(INOUT) actual argument, which also references the value. */
+ DC_DEF_REF,
+ /* An allocation, a pointer assignment or an associate-name, whose
+ effect on a whole variable is not tracked. */
+ DC_DEF_UNTRACKED
+};
+
+
+/* Record that the body defines designator E. */
static void
-dc_scan_def (dc_scan *scan, gfc_expr *e)
+dc_scan_def (dc_scan *scan, gfc_expr *e, enum dc_def_kind kind)
{
- if (e->expr_type != EXPR_VARIABLE
- || dc_locality_known (scan, e->symtree->n.sym))
+ if (e == NULL || e->expr_type != EXPR_VARIABLE)
return;
- if (!dc_indexed_designator (scan, e))
- scan->ok = false;
+ gfc_symbol *sym = e->symtree->n.sym;
+
+ if (kind != DC_DEF_REF)
+ scan->defs.add (e);
+
+ if (dc_locality_known (scan, sym))
+ return;
+
+ /* The array spec of an allocation and the bounds of a pointer
+ assignment do not select a part. */
+ bool whole = dc_whole_designator (e);
+ if (kind == DC_DEF_UNTRACKED && !whole)
+ {
+ whole = true;
+ for (gfc_ref *r = e->ref; r; r = r->next)
+ if (r->type == REF_COMPONENT)
+ whole = false;
+ }
+
+ if (!whole)
+ {
+ /* A part that depends on an index-name is defined by its own
+ iteration; any other part may be shared between iterations. */
+ if (!dc_indexed_designator (scan, e))
+ scan->unindexed_def = true;
+ return;
+ }
+
+ if (kind == DC_DEF_UNTRACKED)
+ {
+ scan->ok = false;
+ return;
+ }
+
+ dc_use &use = dc_use_of (scan, sym);
+ use.defined = true;
+ if (kind == DC_DEF_REF && !scan->dom.contains (sym))
+ use.ref_first = true;
+ scan->pending.safe_push (sym);
+}
+
+
+/* Record that the body sets scalar integer variable E from VALUE, or from
+ an unknown value if VALUE is NULL. */
+
+static void
+dc_scan_index_def (dc_scan *scan, gfc_expr *e, gfc_expr *value)
+{
+ if (e->expr_type != EXPR_VARIABLE || !dc_whole_designator (e))
+ return;
+
+ gfc_symbol *sym = e->symtree->n.sym;
+
+ if (sym->ts.type != BT_INTEGER || e->rank != 0 || !value
+ || !dc_indexed_expr (scan, value))
+ scan->nonindex_defs.add (sym);
+ else
+ scan->index_defs.safe_push (sym);
+}
+
+
+/* Record a reference to procedure SYM. */
+
+static void
+dc_scan_call (dc_scan *scan, gfc_symbol *sym)
+{
+ if (sym == NULL
+ || sym->attr.proc == PROC_INTERNAL
+ || sym->attr.proc == PROC_ST_FUNCTION
+ || sym->attr.dummy
+ || sym->attr.proc_pointer)
+ scan->internal_calls = true;
+ else if (!sym->attr.intrinsic)
+ scan->calls = true;
+}
+
+
+/* Record the definitions made by the actual arguments of a call to the
+ subroutine CODE. */
+
+static void
+dc_scan_call_args (dc_scan *scan, gfc_code *code)
+{
+ gfc_actual_arglist *a = code->ext.actual;
+
+ if (code->resolved_isym)
+ {
+ for (gfc_intrinsic_arg *f = code->resolved_isym->formal;
+ a && f; a = a->next, f = f->next)
+ if (f->intent == INTENT_OUT)
+ dc_scan_def (scan, a->expr, DC_DEF);
+ else if (f->intent != INTENT_IN)
+ dc_scan_def (scan, a->expr, DC_DEF_REF);
+ return;
+ }
+
+ gfc_symbol *proc = code->resolved_sym ? code->resolved_sym
+ : code->symtree->n.sym;
+ dc_scan_call (scan, proc);
+
+ gfc_formal_arglist *f = gfc_sym_get_dummy_args (proc);
+ for (; a; a = a->next, f = f ? f->next : NULL)
+ {
+ enum sym_intent intent = f && f->sym ? f->sym->attr.intent
+ : INTENT_UNKNOWN;
+ if (f && f->sym && f->sym->attr.value)
+ intent = INTENT_IN;
+ if (intent == INTENT_OUT)
+ dc_scan_def (scan, a->expr, DC_DEF);
+ else if (intent != INTENT_IN)
+ dc_scan_def (scan, a->expr, DC_DEF_REF);
+ }
}
@@ -11104,30 +11319,60 @@ dc_scan_code (gfc_code **cp, int *, void *data)
{
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);
+ dc_scan_def (scan, a->target, DC_DEF_UNTRACKED);
break;
case EXEC_ASSIGN:
- dc_scan_def (scan, c->expr1);
+ dc_scan_def (scan, c->expr1, DC_DEF);
+ dc_scan_index_def (scan, c->expr1, c->expr2);
break;
case EXEC_DO:
- dc_scan_def (scan, c->ext.iterator->var);
+ dc_scan_def (scan, c->ext.iterator->var, DC_DEF);
+ dc_scan_index_def (scan, c->ext.iterator->var,
+ (dc_indexed_expr (scan, c->ext.iterator->start)
+ && dc_indexed_expr (scan, c->ext.iterator->end))
+ ? c->ext.iterator->start : NULL);
+ break;
+
+ case EXEC_CALL:
+ case EXEC_ASSIGN_CALL:
+ dc_scan_call_args (scan, c);
break;
case EXEC_POINTER_ASSIGN:
+ dc_scan_def (scan, c->expr1, DC_DEF_UNTRACKED);
+ break;
+
case EXEC_ALLOCATE:
case EXEC_DEALLOCATE:
- case EXEC_CALL:
+ for (gfc_alloc *a = c->ext.alloc.list; a; a = a->next)
+ dc_scan_def (scan, a->expr, DC_DEF_UNTRACKED);
+ break;
+
case EXEC_COMPCALL:
- case EXEC_ASSIGN_CALL:
case EXEC_CALL_PPC:
scan->ok = false;
break;
+ case EXEC_CYCLE:
+ if (c->ext.which_construct == scan->dc)
+ scan->branch_here = true;
+ break;
+
+ case EXEC_EXIT:
+ if (c->ext.which_construct->op != EXEC_DO
+ && c->ext.which_construct->op != EXEC_DO_WHILE
+ && c->ext.which_construct->op != EXEC_DO_CONCURRENT)
+ scan->branch_here = true;
+ break;
+
+ case EXEC_GOTO:
+ case EXEC_ARITHMETIC_IF:
+ scan->branch_here = true;
+ break;
+
default:
break;
}
@@ -11136,6 +11381,224 @@ dc_scan_code (gfc_code **cp, int *, void *data)
}
+static int
+dc_scan_expr (gfc_expr **ep, int *, void *data)
+{
+ dc_scan *scan = (dc_scan *) data;
+ gfc_expr *e = *ep;
+
+ switch (e->expr_type)
+ {
+ case EXPR_FUNCTION:
+ if (!e->value.function.isym)
+ dc_scan_call (scan, e->value.function.esym
+ ? e->value.function.esym : e->symtree->n.sym);
+ break;
+
+ case EXPR_COMPCALL:
+ case EXPR_PPC:
+ scan->internal_calls = true;
+ break;
+
+ case EXPR_VARIABLE:
+ if (e->symtree->n.sym->attr.flavor == FL_VARIABLE
+ && !scan->defs.contains (e)
+ && !dc_locality_known (scan, e->symtree->n.sym))
+ {
+ dc_use &use = dc_use_of (scan, e->symtree->n.sym);
+ if (!scan->dom.contains (e->symtree->n.sym))
+ use.ref_first = true;
+ }
+ break;
+
+ default:
+ break;
+ }
+
+ return 0;
+}
+
+
+/* True if SYM can be given a private copy of its own in each thread, with
+ firstprivate and lastprivate(conditional:) semantics. Without calls to
+ procedures that can see it, a local variable is accessed only through
+ the body. */
+
+static bool
+dc_privatizable (dc_scan *scan, gfc_symbol *sym)
+{
+ if (sym->attr.dimension || sym->attr.codimension
+ || sym->attr.allocatable || sym->attr.pointer
+ || sym->attr.optional || sym->attr.threadprivate
+ || sym->attr.cray_pointee
+ || (sym->ts.type != BT_INTEGER && sym->ts.type != BT_REAL
+ && sym->ts.type != BT_COMPLEX && sym->ts.type != BT_LOGICAL))
+ return false;
+
+ if (scan->internal_calls)
+ return false;
+
+ return (!scan->calls
+ || DECL_CONTEXT (gfc_get_symbol_decl (sym)) == current_function_decl);
+}
+
+
+/* Forget the variables added to SCAN->dom since its length was MARK. */
+
+static void
+dc_dom_restore (dc_scan *scan, unsigned mark)
+{
+ for (unsigned i = mark; i < scan->dom_order.length (); i++)
+ scan->dom.remove (scan->dom_order[i]);
+ scan->dom_order.truncate (mark);
+}
+
+
+static bool dc_scan_list (dc_scan *, gfc_code *);
+
+/* Walk statement C of the body: first C without the statements it
+ contains, whose definitions then precede what follows, and then the
+ contained statements. Only the body of a BLOCK or ASSOCIATE construct
+ runs exactly once. */
+
+static bool
+dc_scan_stmt (dc_scan *scan, gfc_code *c)
+{
+ gfc_code *next = c->next, *stmt = c, *ns_code = NULL;
+ bool block = c->op == EXEC_BLOCK || c->op == EXEC_CHANGE_TEAM;
+ auto_vec<gfc_code *, 4> bodies;
+
+ /* A label may be reached by a branch that skips definitions. */
+ if (c->here)
+ scan->dom.empty ();
+
+ if (block)
+ {
+ ns_code = c->ext.block.ns->code;
+ c->ext.block.ns->code = NULL;
+ }
+ for (gfc_code *b = c->block; b; b = b->block)
+ {
+ bodies.safe_push (b->next);
+ b->next = NULL;
+ }
+ c->next = NULL;
+
+ scan->branch_here = false;
+ scan->pending.truncate (0);
+ gfc_code_walker (&stmt, dc_scan_code, dc_scan_expr, scan);
+
+ c->next = next;
+ if (block)
+ c->ext.block.ns->code = ns_code;
+ unsigned i = 0;
+ for (gfc_code *b = c->block; b; b = b->block)
+ b->next = bodies[i++];
+
+ if (!scan->ok)
+ return false;
+
+ for (gfc_symbol *sym : scan->pending)
+ {
+ if (scan->cond_depth == 0 && !scan->branched)
+ scan->uses.get (sym)->udef = true;
+ if (!scan->dom.add (sym))
+ scan->dom_order.safe_push (sym);
+ }
+ scan->branched |= scan->branch_here;
+
+ if (block)
+ return dc_scan_list (scan, ns_code);
+
+ for (gfc_code *body : bodies)
+ {
+ unsigned mark = scan->dom_order.length ();
+ scan->cond_depth++;
+ bool ok = dc_scan_list (scan, body);
+ scan->cond_depth--;
+ dc_dom_restore (scan, mark);
+ if (!ok)
+ return false;
+ }
+
+ return true;
+}
+
+
+/* Walk the statements LIST of the body. */
+
+static bool
+dc_scan_list (dc_scan *scan, gfc_code *list)
+{
+ for (gfc_code *c = list; c; c = c->next)
+ if (!dc_scan_stmt (scan, c))
+ return false;
+
+ return true;
+}
+
+
+/* Walk the body of the DO CONCURRENT construct CODE into SCAN, and return
+ whether its variables of unspecified locality can be handled. */
+
+static bool
+dc_scan_body (gfc_code *code, dc_scan *scan)
+{
+ scan->dc = code;
+ for (gfc_forall_iterator *fa = code->ext.concur.forall_iterator;
+ fa; fa = fa->next)
+ scan->index_syms.add (fa->var->symtree->n.sym);
+
+ /* Each walk adds the variables that the previous one found to be set
+ from INDEX_SYMS only, until none are added. */
+ for (;;)
+ {
+ scan->index_defs.truncate (0);
+ scan->nonindex_defs.empty ();
+ scan->inner.empty ();
+ scan->defs.empty ();
+ scan->uses.empty ();
+ scan->order.truncate (0);
+ scan->dom.empty ();
+ scan->dom_order.truncate (0);
+ scan->cond_depth = 0;
+ scan->branched = scan->calls = scan->internal_calls = false;
+ scan->unindexed_def = false;
+ scan->ok = true;
+
+ if (!dc_scan_list (scan, code->block->next))
+ return false;
+
+ bool grew = false;
+ for (gfc_symbol *sym : scan->index_defs)
+ if (!scan->nonindex_defs.contains (sym)
+ && !scan->index_syms.add (sym))
+ grew = true;
+ if (!grew)
+ break;
+ }
+
+ if (scan->unindexed_def)
+ return false;
+
+ /* An iteration that references a variable before defining it may only
+ do so if no other iteration defines it (F2018 11.1.7.5). A reference
+ that a path through the iteration reaches before any definition is
+ most often an accumulation that lacks a REDUCE locality spec, which
+ runs correctly only serially. */
+ for (gfc_symbol *sym : scan->order)
+ {
+ dc_use *use = scan->uses.get (sym);
+ if (use->defined
+ && (use->ref_first || !dc_privatizable (scan, sym)))
+ return false;
+ }
+
+ return true;
+}
+
+
+
/* State of the search for a branch into a DO CONCURRENT construct. */
struct dc_entry
@@ -11227,15 +11690,14 @@ gfc_dc_omp_lowering (gfc_code *code)
|| code->expr1)
return GFC_DC_OMP_NONE;
- dc_scan scan;
- scan.dc = code;
- scan.ok = true;
+ /* The OpenMP loop would not honor these directives. */
for (gfc_forall_iterator *fa = code->ext.concur.forall_iterator;
fa; fa = fa->next)
- scan.index_syms.add (fa->var->symtree->n.sym);
+ if (fa->annot.unroll || fa->annot.novector)
+ return GFC_DC_OMP_NONE;
- gfc_code_walker (&code->block->next, dc_scan_code, dc_ignore_expr, &scan);
- if (!scan.ok)
+ dc_scan scan;
+ if (!dc_scan_body (code, &scan))
return GFC_DC_OMP_NONE;
if (dc_branch_into (code))
@@ -11325,14 +11787,21 @@ dc_namelist_from_reduce (gfc_expr_list *list)
}
-/* 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. LOCAL
- and LOCAL_INIT need no clause: gfc_trans_do_concurrent_iteration
- declares those variables inside the loop body. */
+/* Build the gfc_omp_clauses of the OpenMP loop for CODE, whose body SCAN
+ describes. F2018 C1123 forbids a concurrent-limit or concurrent-step
+ from referencing an index-name of the same construct, so the nest is
+ rectangular. LOCAL and LOCAL_INIT need no clause:
+ gfc_trans_do_concurrent_iteration declares those variables inside the
+ loop body.
+
+ A variable of unspecified locality that the body defines as a whole
+ needs a copy per iteration, but keeps the value from the one iteration
+ that defines it, or its value if none does (F2018 11.1.7.5):
+ conditional lastprivate. Without the modifier, its value would be
+ unspecified after a construct without iterations. */
static gfc_omp_clauses *
-dc_build_clauses (gfc_code *code, int collapse)
+dc_build_clauses (gfc_code *code, dc_scan *scan, int collapse)
{
gfc_omp_clauses *clauses = gfc_get_omp_clauses ();
@@ -11344,6 +11813,20 @@ dc_build_clauses (gfc_code *code, int collapse)
clauses->lists[OMP_LIST_REDUCTION]
= dc_namelist_from_reduce (code->ext.concur.locality[LOCALITY_REDUCE]);
+ gfc_omp_namelist **lp = &clauses->lists[OMP_LIST_LASTPRIVATE];
+ for (gfc_symbol *sym : scan->order)
+ {
+ dc_use *use = scan->uses.get (sym);
+ if (!use->defined)
+ continue;
+
+ *lp = gfc_get_omp_namelist ();
+ (*lp)->sym = sym;
+ (*lp)->where = code->loc;
+ (*lp)->u.lastprivate_conditional = true;
+ lp = &(*lp)->next;
+ }
+
return clauses;
}
@@ -11378,7 +11861,9 @@ gfc_trans_omp_do_concurrent (gfc_code *code)
int collapse = 0, n = 0;
stmtblock_t block;
tree stmt;
+ dc_scan scan;
+ dc_scan_body (code, &scan);
for (fa = code->ext.concur.forall_iterator; fa; fa = fa->next)
collapse++;
@@ -11440,7 +11925,7 @@ gfc_trans_omp_do_concurrent (gfc_code *code)
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->ext.omp_clauses = dc_build_clauses (code, &scan, collapse);
par->block = gfc_get_code (EXEC_OMP_PARALLEL_DO_SIMD);
par->block->loc = code->loc;
par->block->next = first;
diff --git a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_4.f90 b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_4.f90
new file mode 100644
index 00000000000..a6f5f768f87
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_4.f90
@@ -0,0 +1,124 @@
+! { dg-do compile }
+! { dg-additional-options "-fdo-concurrent-to-openmp=host -fdump-tree-original" }
+! PR 128590
+!
+! Variables of unspecified locality that the body defines as a whole get
+! a private copy per thread. The one iteration that defines such a
+! variable gives it its value after the construct, and if none does it
+! keeps its value (F2018 11.1.7.5): conditional lastprivate, as plain
+! lastprivate leaves the value unspecified when there are no iterations.
+! A definition inside a BLOCK or IF construct precedes the references
+! that follow it in that construct.
+
+module m
+ implicit none
+contains
+ pure subroutine split (x, hi, lo)
+ real, intent(in) :: x
+ real, intent(out) :: hi, lo
+ hi = aint (x)
+ lo = x - hi
+ end subroutine split
+end module m
+
+subroutine temp_first (a, b, n)
+ integer :: n, i
+ real :: a(n), b(n), t1
+ do concurrent (i = 1:n)
+ t1 = a(i) * 2.0
+ b(i) = t1 + 1.0
+ end do
+end subroutine temp_first
+
+subroutine find (a, n, key, idx)
+ integer :: n, i, idx
+ real :: a(n), key
+ do concurrent (i = 1:n)
+ if (a(i) == key) idx = i
+ end do
+end subroutine find
+
+subroutine inner_do (a, n)
+ integer :: n, i, k
+ real :: a(n,3)
+ do concurrent (i = 1:n)
+ do k = 1, 3
+ a(i,k) = real(k)
+ end do
+ end do
+end subroutine inner_do
+
+subroutine by_call (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 (a(i), h, l)
+ b(i) = h
+ c(i) = l
+ end do
+end subroutine by_call
+
+subroutine index_derived (a, b, n)
+ integer :: n, i, k
+ real :: a(n), b(2*n)
+ do concurrent (i = 1:n)
+ k = 2 * i
+ b(k) = a(i)
+ end do
+end subroutine index_derived
+
+subroutine substring (s, n)
+ integer :: n, i
+ character(len=100) :: s
+ do concurrent (i = 1:n)
+ s(i:i) = 'x'
+ end do
+end subroutine substring
+
+subroutine do_in_block (a, b, n)
+ integer :: n, i, jb
+ real :: a(n), b(n)
+ do concurrent (i = 1:n)
+ block
+ do jb = 1, 3
+ b(i) = b(i) + a(i) * jb
+ end do
+ end block
+ end do
+end subroutine do_in_block
+
+subroutine do_in_if (a, b, n)
+ integer :: n, i, ji
+ real :: a(n), b(n)
+ do concurrent (i = 1:n)
+ if (a(i) > 0.0) then
+ do ji = 1, 3
+ b(i) = b(i) + ji
+ end do
+ end if
+ end do
+end subroutine do_in_if
+
+subroutine def_in_block (a, b, n)
+ integer :: n, i
+ real :: a(n), b(n), tb
+ do concurrent (i = 1:n)
+ block
+ tb = 2.0 * a(i)
+ end block
+ b(i) = tb
+ end do
+end subroutine def_in_block
+
+! { dg-final { scan-tree-dump "lastprivate\\(conditional:t1\\)" "original" } }
+! { dg-final { scan-tree-dump "lastprivate\\(conditional:idx\\)" "original" } }
+! { dg-final { scan-tree-dump "lastprivate\\(conditional:k\\)" "original" } }
+! { dg-final { scan-tree-dump "lastprivate\\(conditional:h\\)" "original" } }
+! { dg-final { scan-tree-dump "lastprivate\\(conditional:l\\)" "original" } }
+! { dg-final { scan-tree-dump "lastprivate\\(conditional:jb\\)" "original" } }
+! { dg-final { scan-tree-dump "lastprivate\\(conditional:ji\\)" "original" } }
+! { dg-final { scan-tree-dump "lastprivate\\(conditional:tb\\)" "original" } }
+! { dg-final { scan-tree-dump-times "#pragma omp parallel" 9 "original" } }
+! { dg-final { scan-tree-dump-times "#pragma omp simd" 9 "original" } }
+! { dg-final { scan-tree-dump-not "firstprivate" "original" } }
diff --git a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_5.f90 b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_5.f90
new file mode 100644
index 00000000000..5092c6278c9
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_5.f90
@@ -0,0 +1,194 @@
+! { dg-do compile }
+! { dg-additional-options "-fdo-concurrent-to-openmp=host -fdump-tree-original" }
+! PR 128590
+!
+! Definitions that keep the serial lowering, because a shared variable
+! would race and a private copy cannot be given the right value: a whole
+! array, a part that does not depend on an index-name, an allocation, a
+! pointer assignment, a variable of derived or character type, a variable
+! that a called internal procedure can see, and a module variable when
+! the body calls a procedure. An iteration may reference a variable
+! before defining it only if no other iteration defines it (F2018
+! 11.1.7.5), which is usually an accumulation without REDUCE that only
+! the serial lowering computes as intended, also inside a nested
+! construct; a definition does not count for the references after a label
+! that a branch can reach. The inner construct of inner_reduce, though,
+! can still be lowered. The OpenMP loop would ignore !GCC$ UNROLL and
+! NOVECTOR.
+
+module m
+ implicit none
+ real :: modvar
+contains
+ pure real function f (x)
+ real, intent(in) :: x
+ f = x + 1.0
+ end function f
+ pure subroutine acc (x, y)
+ real, intent(inout) :: x
+ real, intent(in) :: y
+ x = x + y
+ end subroutine acc
+end module m
+
+subroutine whole_array (a, b, n)
+ integer :: n, i
+ real :: a(3,n), b(n), tmp(3)
+ do concurrent (i = 1:n)
+ tmp = a(:,i)
+ b(i) = sum (tmp)
+ end do
+end subroutine whole_array
+
+subroutine unindexed_part (a, b, n)
+ integer :: n, i
+ real :: a(n), b(n), tmp(3)
+ do concurrent (i = 1:n)
+ tmp(1) = a(i)
+ b(i) = tmp(1)
+ end do
+end subroutine unindexed_part
+
+subroutine component (a, b, n)
+ type t
+ real :: x
+ end type t
+ integer :: n, i
+ real :: a(n), b(n)
+ type(t) :: v
+ do concurrent (i = 1:n)
+ v%x = a(i)
+ b(i) = v%x
+ end do
+end subroutine component
+
+subroutine allocation (a, b, n)
+ integer :: n, i
+ real :: a(n), b(n)
+ real, allocatable :: w(:)
+ do concurrent (i = 1:n)
+ allocate (w(2))
+ w = a(i)
+ b(i) = sum (w)
+ deallocate (w)
+ end do
+end subroutine allocation
+
+subroutine allocation_indexed_spec (a, b, n)
+ integer :: n, i
+ real :: a(n), b(n)
+ real, allocatable :: w(:)
+ do concurrent (i = 1:n)
+ allocate (w(i))
+ w = a(i)
+ b(i) = sum (w)
+ deallocate (w)
+ end do
+end subroutine allocation_indexed_spec
+
+subroutine pointer_assign (a, b, n)
+ integer :: n, i
+ real, target :: a(n)
+ real :: b(n)
+ real, pointer :: p
+ do concurrent (i = 1:n)
+ p => a(i)
+ b(i) = p
+ end do
+end subroutine pointer_assign
+
+subroutine character_var (c, n)
+ integer :: n, i
+ character(len=4) :: c(n), s
+ do concurrent (i = 1:n)
+ s = 'abcd'
+ c(i) = s
+ end do
+end subroutine character_var
+
+subroutine internal_call (a, b, n)
+ integer :: n, i
+ real :: a(n), b(n), t
+ do concurrent (i = 1:n)
+ t = a(i)
+ b(i) = g ()
+ end do
+contains
+ pure real function g ()
+ g = t * 2.0
+ end function g
+end subroutine internal_call
+
+subroutine module_var (a, b, n)
+ use m
+ integer :: n, i
+ real :: a(n), b(n)
+ do concurrent (i = 1:n)
+ modvar = a(i)
+ b(i) = f (modvar)
+ end do
+end subroutine module_var
+
+subroutine missing_reduce (a, n, s)
+ integer :: n, i
+ real :: a(n), s
+ do concurrent (i = 1:n)
+ if (a(i) > 0.0) s = s + a(i)
+ end do
+end subroutine missing_reduce
+
+subroutine inner_reduce (a, n, total)
+ integer :: n, i, j
+ real :: a(n,n), total
+ do concurrent (i = 1:n)
+ do concurrent (j = 1:n) reduce(+:total)
+ total = total + a(i,j)
+ end do
+ end do
+end subroutine inner_reduce
+
+subroutine inout_arg (a, n, s)
+ use m
+ integer :: n, i
+ real :: a(n), s
+ do concurrent (i = 1:n)
+ call acc (s, a(i))
+ end do
+end subroutine inout_arg
+
+subroutine nested_accumulation (a, n, s)
+ integer :: n, i
+ real :: a(n), s
+ do concurrent (i = 1:n)
+ block
+ if (a(i) > 0.0) s = s + a(i)
+ end block
+ end do
+end subroutine nested_accumulation
+
+subroutine branch_loop (a, n)
+ integer :: n, i, k
+ real :: a(n)
+ do concurrent (i = 1:n)
+ k = 0
+10 k = k + 1
+ a(i) = a(i) + k
+ if (k < 3) goto 10
+ end do
+end subroutine branch_loop
+
+subroutine annotated (a, n)
+ integer :: n, i
+ real :: a(n)
+ !GCC$ unroll 4
+ do concurrent (i = 1:n)
+ a(i) = 0.0
+ end do
+ !GCC$ novector
+ do concurrent (i = 1:n)
+ a(i) = 1.0
+ end do
+end subroutine annotated
+
+! { dg-final { scan-tree-dump-times "#pragma omp parallel reduction\\(\\+:total\\)" 1 "original" } }
+! { dg-final { scan-tree-dump-times "#pragma omp parallel" 1 "original" } }
diff --git a/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_3.f90 b/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_3.f90
new file mode 100644
index 00000000000..3b5e489c2dd
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_3.f90
@@ -0,0 +1,149 @@
+! { dg-do run }
+! { dg-additional-options "-fdo-concurrent-to-openmp=host" }
+! { dg-set-target-env-var OMP_NUM_THREADS "4" }
+! PR 128590
+!
+! Variables of unspecified locality that the body of a DO CONCURRENT
+! construct lowered to OpenMP defines as a whole. A variable that only
+! one iteration defines keeps that value after the construct, and one
+! that no other iteration defines may be referenced with its value from
+! before the construct (F2018 11.1.7.5). Without per-thread copies with
+! that semantics, the temporaries race or the values are lost.
+
+module m
+ implicit none
+contains
+ pure subroutine split (x, hi, lo)
+ 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 = 10000
+ integer :: i, j, k, idx
+ real :: a(n), b(2*n), c(n), t, h, l, s
+
+ do i = 1, n
+ a(i) = real(i) + 0.25
+ end do
+
+ ! A temporary defined by every iteration.
+ do concurrent (i = 1:n)
+ t = a(i) * 2.0
+ do k = 1, 3
+ t = t + 1.0
+ end do
+ b(i) = t
+ end do
+ do j = 1, n
+ if (b(j) /= a(j) * 2.0 + 3.0) stop 1
+ end do
+
+ ! The one iteration that defines IDX gives it its value.
+ idx = -1
+ do concurrent (i = 1:n)
+ if (a(i) == 7777.25) idx = i
+ end do
+ if (idx /= 7777) stop 2
+
+ ! No iteration defines it, so it keeps its value.
+ idx = -1
+ do concurrent (i = 1:n)
+ if (a(i) < 0.0) idx = i
+ end do
+ if (idx /= -1) stop 3
+
+ ! A single iteration may reference the value from before the construct;
+ ! many may not, but an accumulation that lacks REDUCE still gets the
+ ! result of the serial lowering.
+ s = 10.0
+ do concurrent (i = 5:5)
+ s = s + a(i)
+ end do
+ if (s /= 10.0 + a(5)) stop 4
+ s = 0.0
+ do concurrent (i = 1:100)
+ if (a(i) > 50.0) s = s + 1.0
+ end do
+ if (s /= 51.0) stop 8
+
+ ! A single iteration defines the temporaries, so they keep their values.
+ do concurrent (i = n:n)
+ t = a(i)
+ do k = 1, 2
+ b(i) = t
+ end do
+ end do
+ if (t /= a(n) .or. k /= 3) stop 5
+
+ ! Without iterations, nothing defines the temporaries.
+ t = -2.0
+ k = -3
+ do concurrent (i = 1:n-n)
+ t = a(i)
+ do k = 1, 2
+ b(i) = t
+ end do
+ end do
+ if (t /= -2.0 .or. k /= -3) stop 9
+
+ ! INTENT(OUT) actual arguments define their variables.
+ do concurrent (i = 1:n)
+ call split (a(i), h, l)
+ b(i) = h
+ c(i) = l
+ end do
+ do j = 1, n
+ if (b(j) /= real(j) .or. c(j) /= 0.25) stop 6
+ end do
+
+ ! A subscript set from an index-name.
+ b = 0.0
+ do concurrent (i = 1:n)
+ k = 2 * i
+ b(k) = a(i)
+ b(k-1) = -a(i)
+ end do
+ do j = 1, n
+ if (b(2*j) /= a(j) .or. b(2*j-1) /= -a(j)) stop 7
+ end do
+
+ ! A DO variable and a temporary defined in a BLOCK construct.
+ b = 0.0
+ do concurrent (i = 1:n)
+ block
+ do k = 1, 3
+ b(i) = b(i) + a(i) * k
+ end do
+ t = b(i)
+ end block
+ c(i) = t
+ end do
+ do j = 1, n
+ if (b(j) /= 6.0 * a(j) .or. c(j) /= b(j)) stop 10
+ end do
+
+ ! A GOTO, an EXIT from a BLOCK construct and a CYCLE skip each
+ ! definition in all iterations but one.
+ idx = -1
+ k = -1
+ j = -1
+ do concurrent (i = 1:n)
+ b(i) = a(i)
+ if (i /= 17) goto 10
+ idx = i
+10 continue
+ blk: block
+ if (i /= 23) exit blk
+ k = i
+ end block blk
+ if (i /= 31) cycle
+ j = i
+ end do
+ if (idx /= 17 .or. k /= 23 .or. j /= 31) stop 11
+end program main
--
2.56.1