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

Changes since v1: dc_locality_ok keeps a construct serial if a SHARED
or REDUCE variable is an associate name, a Cray pointee or
threadprivate, or if a REDUCE variable is a character variable or a
pointer.  REDUCE(MAX:) on a character variable ICEd, and a
threadprivate one gave wrong results.

Start at dc_build_clauses in trans-openmp.cc, which now adds the shared
and reduction clauses, then dc_namelist_from_locality,
dc_namelist_from_reduce and dc_reduction_op, which build them.
dc_locality_known, renamed from dc_construct_local, lets a body define a
variable that has a locality spec, and gfc_dc_omp_lowering falls back
for locality specs only where dc_locality_ok says so.  LOCAL and
LOCAL_INIT need nothing new, because gfc_trans_do_concurrent_iteration
from 1/10 already declares them inside the iteration.

Regards,

Jerry

---

fortran: [PR128590]-2 Map DO CONCURRENT locality specs to OpenMP

Lower DO CONCURRENT constructs with locality specs to OpenMP.  SHARED
becomes a shared clause and REDUCE a reduction clause, whose operators
map one to one.  LOCAL and LOCAL_INIT need no clause: the serial
lowering already declares those variables inside the iteration, which
the OpenMP region makes private, and LOCAL_INIT sets them from the outer
variable in every iteration as F2018 11.1.7.5 requires.  A construct
stays serial if a SHARED or REDUCE variable could not appear in its
clause: an associate name, a Cray pointee or a threadprivate variable,
or a character variable or a pointer to reduce.

Assisted-by: Claude Opus 5.5

        PR fortran/128590

gcc/fortran/ChangeLog:

        * invoke.texi: Document the lowering of locality specs.
        * trans-openmp.cc (dc_construct_local): Rename to...
        (dc_locality_known): ...this.  Also accept variables with a
        locality spec.
        (dc_scan_def): Use it.
        (dc_locality_ok): New.
        (gfc_dc_omp_lowering): Accept locality specs that dc_locality_ok
        accepts.
        (dc_namelist_from_locality, dc_reduction_op)
        (dc_namelist_from_reduce): New.
        (dc_build_clauses): Add shared and reduction clauses.

gcc/testsuite/ChangeLog:

        * gfortran.dg/gomp/do_concurrent_to_openmp_3.f90: New test.
        * gfortran.dg/gomp/do_concurrent_to_openmp_14.f90: New test.

libgomp/ChangeLog:

        * testsuite/libgomp.fortran/do_concurrent_to_openmp_2.f90: New test.
>From eb3e54e04b71ca7a48ba3e0a13fdc18edacb35b0 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Tue, 10 Nov 2026 12:30:50 -0800
Subject: [PATCH v2 2/10] fortran: [PR128590]-2 Map DO CONCURRENT locality
 specs to OpenMP

Lower DO CONCURRENT constructs with locality specs to OpenMP.  SHARED
becomes a shared clause and REDUCE a reduction clause, whose operators
map one to one.  LOCAL and LOCAL_INIT need no clause: the serial
lowering already declares those variables inside the iteration, which
the OpenMP region makes private, and LOCAL_INIT sets them from the outer
variable in every iteration as F2018 11.1.7.5 requires.  A construct
stays serial if a SHARED or REDUCE variable could not appear in its
clause: an associate name, a Cray pointee or a threadprivate variable,
or a character variable or a pointer to reduce.

Assisted-by: Claude Opus 5.5

	PR fortran/128590

gcc/fortran/ChangeLog:

	* invoke.texi: Document the lowering of locality specs.
	* trans-openmp.cc (dc_construct_local): Rename to...
	(dc_locality_known): ...this.  Also accept variables with a
	locality spec.
	(dc_scan_def): Use it.
	(dc_locality_ok): New.
	(gfc_dc_omp_lowering): Accept locality specs that dc_locality_ok
	accepts.
	(dc_namelist_from_locality, dc_reduction_op)
	(dc_namelist_from_reduce): New.
	(dc_build_clauses): Add shared and reduction clauses.

gcc/testsuite/ChangeLog:

	* gfortran.dg/gomp/do_concurrent_to_openmp_3.f90: New test.
	* gfortran.dg/gomp/do_concurrent_to_openmp_14.f90: New test.

libgomp/ChangeLog:

	* testsuite/libgomp.fortran/do_concurrent_to_openmp_2.f90: New test.
---
 gcc/fortran/invoke.texi                       |   5 +-
 gcc/fortran/trans-openmp.cc                   | 140 ++++++++++++++++--
 .../gomp/do_concurrent_to_openmp_14.f90       |  47 ++++++
 .../gomp/do_concurrent_to_openmp_3.f90        |  62 ++++++++
 .../do_concurrent_to_openmp_2.f90             |  83 +++++++++++
 5 files changed, 326 insertions(+), 11 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_14.f90
 create mode 100644 gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_3.f90
 create mode 100644 libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_2.f90

diff --git a/gcc/fortran/invoke.texi b/gcc/fortran/invoke.texi
index 7f780feb9f7..3af2373a450 100644
--- a/gcc/fortran/invoke.texi
+++ b/gcc/fortran/invoke.texi
@@ -1880,7 +1880,10 @@ Translate @code{DO CONCURRENT} constructs serially.  (Default)
 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.
+@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.
 @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 11c68032308..88166b67fc9 100644
--- a/gcc/fortran/trans-openmp.cc
+++ b/gcc/fortran/trans-openmp.cc
@@ -11053,12 +11053,23 @@ dc_indexed_designator (dc_scan *scan, gfc_expr *e)
 }
 
 
-/* True if SYM is local to each iteration of the construct.  */
+/* True if the scoping or a locality spec of the construct decides the
+   locality of SYM.  */
 
 static bool
-dc_construct_local (dc_scan *scan, gfc_symbol *sym)
+dc_locality_known (dc_scan *scan, gfc_symbol *sym)
 {
-  return scan->inner.contains (sym->ns) && sym->attr.save == SAVE_NONE;
+  if (scan->inner.contains (sym->ns) && sym->attr.save == SAVE_NONE)
+    return true;
+
+  for (int i = 0; i < LOCALITY_NUM; i++)
+    for (gfc_expr_list *el = scan->dc->ext.concur.locality[i];
+	 el; el = el->next)
+      if (el->expr->expr_type == EXPR_VARIABLE
+	  && el->expr->symtree->n.sym == sym)
+	return true;
+
+  return false;
 }
 
 
@@ -11091,7 +11102,7 @@ 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))
+      || dc_locality_known (scan, e->symtree->n.sym))
     return;
 
   if (!dc_indexed_designator (scan, e))
@@ -11260,6 +11271,32 @@ dc_branch_into (gfc_code *code)
 }
 
 
+/* True if the OpenMP clauses that dc_build_clauses makes of the SHARED
+   and REDUCE locality specs of the DO CONCURRENT construct CODE are valid.
+   An associate name, a Cray pointee or a threadprivate variable cannot
+   appear in them, and a character variable or a pointer in a reduction.  */
+
+static bool
+dc_locality_ok (gfc_code *code)
+{
+  static const int listed[] = { LOCALITY_SHARED, LOCALITY_REDUCE };
+  for (int i : listed)
+    for (gfc_expr_list *el = code->ext.concur.locality[i]; el; el = el->next)
+      {
+	if (el->expr->expr_type != EXPR_VARIABLE)
+	  continue;
+	gfc_symbol *sym = el->expr->symtree->n.sym;
+	if (sym->attr.associate_var || sym->attr.cray_pointee
+	    || sym->attr.threadprivate
+	    || (i == LOCALITY_REDUCE
+		&& (sym->attr.pointer || sym->ts.type == BT_CHARACTER)))
+	  return false;
+      }
+
+  return true;
+}
+
+
 /* Return how the DO CONCURRENT construct CODE is lowered to OpenMP;
    GFC_DC_OMP_NONE keeps the serial lowering.  */
 
@@ -11271,13 +11308,10 @@ gfc_dc_omp_lowering (gfc_code *code)
   if (flag_do_concurrent_to_openmp == GFC_DC_OMP_NONE
       || gfc_omp_construct_depth > 0
       || code->ext.concur.forall_iterator == NULL
-      || code->expr1)
+      || code->expr1
+      || !dc_locality_ok (code))
     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;
@@ -11296,9 +11330,91 @@ gfc_dc_omp_lowering (gfc_code *code)
 }
 
 
+/* Convert a locality-spec list of variables into an OpenMP namelist.  */
+
+static gfc_omp_namelist *
+dc_namelist_from_locality (gfc_expr_list *list)
+{
+  gfc_omp_namelist *head = NULL, **tail = &head;
+
+  for (; list; list = list->next)
+    {
+      gfc_omp_namelist *n = gfc_get_omp_namelist ();
+
+      n->sym = list->expr->symtree->n.sym;
+      n->where = list->expr->where;
+      *tail = n;
+      tail = &n->next;
+    }
+
+  return head;
+}
+
+
+/* Map the reduce-operation OP of a REDUCE locality spec to OpenMP.  */
+
+static gfc_omp_reduction_op
+dc_reduction_op (gfc_expr *op)
+{
+  if (op->expr_type == EXPR_OP)
+    switch (op->value.op.op)
+      {
+      case INTRINSIC_PLUS: return OMP_REDUCTION_PLUS;
+      case INTRINSIC_TIMES: return OMP_REDUCTION_TIMES;
+      case INTRINSIC_AND: return OMP_REDUCTION_AND;
+      case INTRINSIC_OR: return OMP_REDUCTION_OR;
+      case INTRINSIC_EQV: return OMP_REDUCTION_EQV;
+      case INTRINSIC_NEQV: return OMP_REDUCTION_NEQV;
+      default: gcc_unreachable ();
+      }
+
+  switch (op->value.function.isym->id)
+    {
+    case GFC_ISYM_MIN: return OMP_REDUCTION_MIN;
+    case GFC_ISYM_MAX: return OMP_REDUCTION_MAX;
+    case GFC_ISYM_IAND: return OMP_REDUCTION_IAND;
+    case GFC_ISYM_IOR: return OMP_REDUCTION_IOR;
+    case GFC_ISYM_IEOR: return OMP_REDUCTION_IEOR;
+    default: gcc_unreachable ();
+    }
+}
+
+
+/* Convert the REDUCE locality specs LIST into an OpenMP reduction
+   namelist.  Each reduce-operation in LIST precedes its variables.  */
+
+static gfc_omp_namelist *
+dc_namelist_from_reduce (gfc_expr_list *list)
+{
+  gfc_omp_namelist *head = NULL, **tail = &head;
+  gfc_omp_reduction_op op = OMP_REDUCTION_NONE;
+
+  for (; list; list = list->next)
+    {
+      if (list->expr->expr_type != EXPR_VARIABLE)
+	{
+	  op = dc_reduction_op (list->expr);
+	  continue;
+	}
+
+      gfc_omp_namelist *n = gfc_get_omp_namelist ();
+
+      n->sym = list->expr->symtree->n.sym;
+      n->where = list->expr->where;
+      n->u.reduction_op = op;
+      *tail = n;
+      tail = &n->next;
+    }
+
+  return head;
+}
+
+
 /* 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.  */
+   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.  */
 
 static gfc_omp_clauses *
 dc_build_clauses (gfc_code *code, int collapse)
@@ -11308,6 +11424,10 @@ dc_build_clauses (gfc_code *code, int collapse)
   clauses->collapse = collapse;
   clauses->default_sharing = OMP_DEFAULT_SHARED;
   clauses->loc = code->loc;
+  clauses->lists[OMP_LIST_SHARED]
+    = dc_namelist_from_locality (code->ext.concur.locality[LOCALITY_SHARED]);
+  clauses->lists[OMP_LIST_REDUCTION]
+    = dc_namelist_from_reduce (code->ext.concur.locality[LOCALITY_REDUCE]);
 
   return clauses;
 }
diff --git a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_14.f90 b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_14.f90
new file mode 100644
index 00000000000..5c1a3a42cb9
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_14.f90
@@ -0,0 +1,47 @@
+! { dg-do compile }
+! { dg-additional-options "-fdo-concurrent-to-openmp=host -fdump-tree-original" }
+!
+! PR 128590
+
+module m
+  integer :: tp = 0
+  !$omp threadprivate (tp)
+end module m
+
+subroutine reduce_char (w, c)
+  character(len=3) :: w(5), c
+  integer :: i
+  do concurrent (i = 1:5) reduce(max:c)
+    c = max (c, w(i))
+  end do
+end subroutine reduce_char
+
+subroutine reduce_pointer (a, q)
+  integer :: a(100), i
+  integer, pointer :: q
+  do concurrent (i = 1:100) reduce(+:q)
+    q = q + a(i)
+  end do
+end subroutine reduce_pointer
+
+subroutine reduce_threadprivate (a)
+  use m
+  integer :: a(100), i
+  do concurrent (i = 1:100) reduce(+:tp)
+    tp = tp + a(i)
+  end do
+end subroutine reduce_threadprivate
+
+subroutine associate_name (a, t)
+  integer :: a(100), i, t
+  associate (u => t)
+    do concurrent (i = 1:100) reduce(+:u)
+      u = u + a(i)
+    end do
+    do concurrent (i = 1:100) shared(u)
+      a(i) = a(i) + u
+    end do
+  end associate
+end subroutine associate_name
+
+! { dg-final { scan-tree-dump-not "#pragma omp parallel" "original" } }
diff --git a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_3.f90 b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_3.f90
new file mode 100644
index 00000000000..f77ee4d274c
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_3.f90
@@ -0,0 +1,62 @@
+! { dg-do compile }
+! { dg-additional-options "-fdo-concurrent-to-openmp=host -fdump-tree-original" }
+! PR 128590
+!
+! SHARED and REDUCE locality specs become OpenMP data-sharing clauses.
+! LOCAL and LOCAL_INIT variables are declared inside the loop body, so
+! they need no clause.
+
+subroutine reductions (a, n, s, p, rmax, rmin, iacc, iand_acc, ieor_acc, &
+		       lall, lany, leqv, lneqv)
+  integer :: n, i, iacc, iand_acc, ieor_acc
+  real :: a(n), s, p, rmax, rmin
+  logical :: lall, lany, leqv, lneqv
+
+  do concurrent (i = 1:n) reduce(+:s) reduce(*:p)
+     s = s + a(i)
+     p = p * a(i)
+  end do
+  do concurrent (i = 1:n) reduce(max:rmax) reduce(min:rmin)
+     rmax = max (rmax, a(i))
+     rmin = min (a(i), rmin)
+  end do
+  do concurrent (i = 1:n) reduce(ior:iacc) reduce(iand:iand_acc) &
+			  reduce(ieor:ieor_acc)
+     iacc = ior (iacc, i)
+     iand_acc = iand (iand_acc, i)
+     ieor_acc = ieor (ieor_acc, i)
+  end do
+  do concurrent (i = 1:n) reduce(.and.:lall) reduce(.or.:lany) &
+			  reduce(.eqv.:leqv) reduce(.neqv.:lneqv)
+     lall = lall .and. (a(i) > 0.0)
+     lany = lany .or. (a(i) > 0.0)
+     leqv = leqv .eqv. (a(i) > 0.0)
+     lneqv = lneqv .neqv. (a(i) > 0.0)
+  end do
+end subroutine reductions
+
+subroutine locals (a, b, n)
+  integer :: n, i, scratch, seed
+  real :: a(n), b(n)
+  seed = 7
+  do concurrent (i = 1:n) local(scratch) local_init(seed) shared(a, b)
+     scratch = i + seed
+     seed = seed + 1
+     b(i) = a(scratch - seed + 1)
+  end do
+end subroutine locals
+
+! { dg-final { scan-tree-dump "reduction\\(\\+:s\\)" "original" } }
+! { dg-final { scan-tree-dump "reduction\\(\\*:p\\)" "original" } }
+! { dg-final { scan-tree-dump "reduction\\(max:rmax\\)" "original" } }
+! { dg-final { scan-tree-dump "reduction\\(min:rmin\\)" "original" } }
+! { dg-final { scan-tree-dump "reduction\\(\\|:iacc\\)" "original" } }
+! { dg-final { scan-tree-dump "reduction\\(&:iand_acc\\)" "original" } }
+! { dg-final { scan-tree-dump "reduction\\(\\^:ieor_acc\\)" "original" } }
+! { dg-final { scan-tree-dump "reduction\\(&&:lall\\)" "original" } }
+! { dg-final { scan-tree-dump "reduction\\(\\|\\|:lany\\)" "original" } }
+! { dg-final { scan-tree-dump "reduction\\(==:leqv\\)" "original" } }
+! { dg-final { scan-tree-dump "reduction\\(!=:lneqv\\)" "original" } }
+! { dg-final { scan-tree-dump "shared\\(a\\) shared\\(b\\)" "original" } }
+! { dg-final { scan-tree-dump-times "#pragma omp parallel" 5 "original" } }
+! { dg-final { scan-tree-dump-not "private\\((scratch|seed)\\)" "original" } }
diff --git a/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_2.f90 b/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_2.f90
new file mode 100644
index 00000000000..9d96f16f541
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_2.f90
@@ -0,0 +1,83 @@
+! { dg-do run }
+! { dg-additional-options "-fdo-concurrent-to-openmp=host" }
+! { dg-set-target-env-var OMP_NUM_THREADS "4" }
+! PR 128590
+!
+! Locality specs of DO CONCURRENT constructs lowered to OpenMP loops.
+! LOCAL_INIT gives every iteration, not every thread, the outer value
+! (F2018 11.1.7.5), and the outer LOCAL and LOCAL_INIT variables are left
+! unchanged.
+
+program main
+  implicit none
+  integer, parameter :: n = 1000
+  integer :: i, j, scratch, seed, iacc, iand_acc, ieor_acc, expect
+  integer :: seen(n)
+  real :: a(n), b(n), s, p, rmax, rmin
+  logical :: lall, lany
+
+  do i = 1, n
+     a(i) = real(mod(i * 7, 13)) + 1.0
+  end do
+
+  s = 10.0
+  p = 1.0
+  do concurrent (i = 1:20) reduce(+:s) reduce(*:p)
+     s = s + a(i)
+     p = p * (a(i) / a(i))
+  end do
+  if (s /= 10.0 + sum (a(1:20))) stop 1
+  if (p /= 1.0) stop 2
+
+  rmax = -1.0
+  rmin = 100.0
+  do concurrent (i = 1:n) reduce(max:rmax) reduce(min:rmin)
+     rmax = max (rmax, a(i))
+     rmin = min (a(i), rmin)
+  end do
+  if (rmax /= maxval (a) .or. rmin /= minval (a)) stop 3
+
+  iacc = 0
+  iand_acc = -1
+  ieor_acc = 0
+  do concurrent (i = 1:n) reduce(ior:iacc) reduce(iand:iand_acc) &
+			  reduce(ieor:ieor_acc)
+     iacc = ior (iacc, i)
+     iand_acc = iand (iand_acc, i + 1024)
+     ieor_acc = ieor (ieor_acc, i)
+  end do
+  expect = 0
+  do i = 1, n
+     expect = ieor (expect, i)
+  end do
+  if (iacc /= 1023 .or. iand_acc /= 1024 .or. ieor_acc /= expect) stop 4
+
+  lall = .true.
+  lany = .false.
+  do concurrent (i = 1:n) reduce(.and.:lall) reduce(.or.:lany)
+     lall = lall .and. (a(i) > 0.0)
+     lany = lany .or. (a(i) > 13.0)
+  end do
+  if (.not. lall .or. lany) stop 5
+
+  scratch = -1
+  seed = 7
+  do concurrent (i = 1:n) local(scratch) local_init(seed) shared(seen)
+     scratch = 2 * i
+     seen(i) = seed + scratch
+     seed = seed + 1000
+  end do
+  if (scratch /= -1 .or. seed /= 7) stop 6
+  do j = 1, n
+     if (seen(j) /= 7 + 2 * j) stop 7
+  end do
+
+  b = 0.0
+  do concurrent (i = 1:n) local(s)
+     s = a(i) * 2.0
+     b(i) = s + 1.0
+  end do
+  do j = 1, n
+     if (b(j) /= a(j) * 2.0 + 1.0) stop 8
+  end do
+end program main
-- 
2.56.1

Reply via email to