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

Changes since v1: dc_scan_body treats a masked body as conditional, so
on a device no variable that it defines gets plain lastprivate.
dc_mask_conflict also rejects a mask that references what the body
defines through an associate name, which dc_assoc_base resolves, or
through a pointer, one that references a REDUCE variable, and one that
passes a procedure as an argument.

Start at dc_mask_inline_ok and dc_mask_conflict in trans-openmp.cc.  A
mask may move into the iteration only if it references no procedure
other than an intrinsic one, and no variable that the body defines or
that has a LOCAL, LOCAL_INIT or REDUCE spec: inside the iteration that
name would denote the construct entity.  gfc_dc_omp_lowering calls it
in place of the old blanket fallback for masks.  The dc_scan_body hunk
walks the mask too, so its references count as uses.

gfc_trans_omp_do_concurrent passes the mask to the iteration node, and
gfc_trans_do_concurrent in trans-stmt.cc wraps the iteration in a test
of it.

Regards,

Jerry

---

fortran: [PR128590]-7 Lower masked DO CONCURRENT to OpenMP

The mask of a DO CONCURRENT is evaluated for all index values before the
block runs (F2018 11.1.7.4.2).  Evaluating it at the start of each
iteration instead is equivalent if the body defines nothing that the
mask references, also through an associate name or a pointer, if the
mask references no LOCAL, LOCAL_INIT or REDUCE variable, which denotes
another entity in the body, and if it references no procedure other than
an intrinsic one, so do that in the OpenMP lowering, and keep the serial
lowering otherwise.  Only the iterations whose mask is true execute the
body, so on a device none of its definitions counts as made by every
iteration.

Assisted-by: Claude Opus 5.5

        PR fortran/128590

gcc/fortran/ChangeLog:

        * invoke.texi: Document the lowering of masks.
        * trans-openmp.cc (dc_scan_body): Walk the mask first, and the body
        as conditional if there is a mask.
        (dc_assoc_base, dc_mask_conflict, dc_mask_inline_ok): New.
        (gfc_dc_omp_lowering): Accept a mask that can be evaluated inline.
        (gfc_trans_omp_do_concurrent): Pass the mask to the iteration.
        * trans-stmt.cc (gfc_trans_do_concurrent): Evaluate the mask of an
        iteration of a construct lowered to OpenMP.

gcc/testsuite/ChangeLog:

        * gfortran.dg/gomp/do_concurrent_to_openmp_9.f90: Update for masks
        that are lowered.
        * gfortran.dg/gomp/do_concurrent_to_openmp_10.f90: New test.
        * gfortran.dg/gomp/do_concurrent_to_openmp_18.f90: New test.

libgomp/ChangeLog:

        * testsuite/libgomp.fortran/do_concurrent_to_openmp_7.f90: New test.
>From c65a5588e8e6a4ffdc315a0bbb6921de8bf9ff4a Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Wed, 25 Nov 2026 15:57:57 -0800
Subject: [PATCH v2 7/10] fortran: [PR128590]-7 Lower masked DO CONCURRENT to
 OpenMP

The mask of a DO CONCURRENT is evaluated for all index values before the
block runs (F2018 11.1.7.4.2).  Evaluating it at the start of each
iteration instead is equivalent if the body defines nothing that the
mask references, also through an associate name or a pointer, if the
mask references no LOCAL, LOCAL_INIT or REDUCE variable, which denotes
another entity in the body, and if it references no procedure other than
an intrinsic one, so do that in the OpenMP lowering, and keep the serial
lowering otherwise.  Only the iterations whose mask is true execute the
body, so on a device none of its definitions counts as made by every
iteration.

Assisted-by: Claude Opus 5.5

	PR fortran/128590

gcc/fortran/ChangeLog:

	* invoke.texi: Document the lowering of masks.
	* trans-openmp.cc (dc_scan_body): Walk the mask first, and the body
	as conditional if there is a mask.
	(dc_assoc_base, dc_mask_conflict, dc_mask_inline_ok): New.
	(gfc_dc_omp_lowering): Accept a mask that can be evaluated inline.
	(gfc_trans_omp_do_concurrent): Pass the mask to the iteration.
	* trans-stmt.cc (gfc_trans_do_concurrent): Evaluate the mask of an
	iteration of a construct lowered to OpenMP.

gcc/testsuite/ChangeLog:

	* gfortran.dg/gomp/do_concurrent_to_openmp_9.f90: Update for masks
	that are lowered.
	* gfortran.dg/gomp/do_concurrent_to_openmp_10.f90: New test.
	* gfortran.dg/gomp/do_concurrent_to_openmp_18.f90: New test.

libgomp/ChangeLog:

	* testsuite/libgomp.fortran/do_concurrent_to_openmp_7.f90: New test.
---
 gcc/fortran/invoke.texi                       |  5 +-
 gcc/fortran/trans-openmp.cc                   | 97 +++++++++++++++++--
 gcc/fortran/trans-stmt.cc                     | 22 ++++-
 .../gomp/do_concurrent_to_openmp_10.f90       | 53 ++++++++++
 .../gomp/do_concurrent_to_openmp_18.f90       | 36 +++++++
 .../gomp/do_concurrent_to_openmp_9.f90        |  4 +-
 .../do_concurrent_to_openmp_7.f90             | 48 +++++++++
 7 files changed, 251 insertions(+), 14 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_10.f90
 create mode 100644 gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_18.f90
 create mode 100644 libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_7.f90

diff --git a/gcc/fortran/invoke.texi b/gcc/fortran/invoke.texi
index 81fde058c57..1911b3f225f 100644
--- a/gcc/fortran/invoke.texi
+++ b/gcc/fortran/invoke.texi
@@ -1896,7 +1896,10 @@ 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.
+does, is translated serially.  The mask of a construct is evaluated at
+the start of each iteration; a construct whose body defines a variable
+that its mask references, or whose mask references a procedure other
+than an intrinsic one, is translated serially.
 
 @item @samp{device}
 Translate @code{DO CONCURRENT} constructs into OpenMP @code{target teams
diff --git a/gcc/fortran/trans-openmp.cc b/gcc/fortran/trans-openmp.cc
index ceab04260a0..caaa88dbc39 100644
--- a/gcc/fortran/trans-openmp.cc
+++ b/gcc/fortran/trans-openmp.cc
@@ -11792,6 +11792,14 @@ dc_scan_body (gfc_code *code, dc_scan *scan)
       scan->part_defs.truncate (0);
       scan->ok = true;
 
+      /* The mask is evaluated before the body, which only some
+	 iterations then execute.  */
+      if (code->expr1)
+	{
+	  gfc_expr_walker (&code->expr1, dc_scan_expr, scan);
+	  scan->cond_depth = 1;
+	}
+
       if (!dc_scan_list (scan, code->block->next))
 	return false;
 
@@ -11966,6 +11974,74 @@ dc_warn (dc_reason *r, bool device)
 }
 
 
+/* The variable that SYM stands for if it is an associate name of one,
+   or SYM.  */
+
+static gfc_symbol *
+dc_assoc_base (gfc_symbol *sym)
+{
+  while (sym->assoc && sym->assoc->target
+	 && sym->assoc->target->expr_type == EXPR_VARIABLE)
+    sym = sym->assoc->target->symtree->n.sym;
+  return sym;
+}
+
+
+static int
+dc_mask_conflict (gfc_expr **ep, int *, void *data)
+{
+  dc_scan *scan = (dc_scan *) data;
+  gfc_expr *e = *ep;
+
+  if (e->expr_type == EXPR_FUNCTION && !e->value.function.isym)
+    return 1;
+  if (e->expr_type == EXPR_COMPCALL || e->expr_type == EXPR_PPC)
+    return 1;
+  if (e->expr_type != EXPR_VARIABLE)
+    return 0;
+
+  gfc_symbol *sym = e->symtree->n.sym;
+  /* A procedure actual argument may be called.  */
+  if (sym->attr.flavor == FL_PROCEDURE && !dc_result_var (sym))
+    return 1;
+
+  /* The body may define the variable through another name.  */
+  gfc_symbol *base = dc_assoc_base (sym);
+  bool may_alias = base->attr.pointer || base->attr.target;
+  for (gfc_symbol *d : scan->defined)
+    {
+      gfc_symbol *dbase = dc_assoc_base (d);
+      if (dbase == base
+	  || (may_alias && (dbase->attr.pointer || dbase->attr.target)))
+	return 1;
+    }
+
+  /* In the body, the name denotes the construct entity.  */
+  static const int specs[] = { LOCALITY_LOCAL, LOCALITY_LOCAL_INIT,
+			       LOCALITY_REDUCE };
+  for (int i : specs)
+    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 1;
+
+  return 0;
+}
+
+
+/* True if the mask of the DO CONCURRENT construct whose body SCAN
+   describes may be evaluated at the start of each iteration.  The mask
+   is evaluated for all index values before the body executes (F2018
+   11.1.7.4.2), so the body must not define what the mask references.  */
+
+static bool
+dc_mask_inline_ok (dc_scan *scan)
+{
+  return gfc_expr_walker (&scan->dc->expr1, dc_mask_conflict, scan) == 0;
+}
+
+
 /* State of the search for a branch into a DO CONCURRENT construct.  */
 
 struct dc_entry
@@ -12114,13 +12190,6 @@ gfc_dc_omp_lowering (gfc_code *code)
       || code->ext.concur.forall_iterator == NULL)
     return GFC_DC_OMP_NONE;
 
-  if (code->expr1)
-    {
-      gfc_warning (OPT_Wdo_concurrent_to_openmp, "DO CONCURRENT at %L with "
-		   "a mask stays serial", &code->loc);
-      return GFC_DC_OMP_NONE;
-    }
-
   if (!dc_locality_ok (code))
     {
       gfc_warning (OPT_Wdo_concurrent_to_openmp, "DO CONCURRENT at %L stays "
@@ -12155,6 +12224,15 @@ gfc_dc_omp_lowering (gfc_code *code)
       return GFC_DC_OMP_NONE;
     }
 
+  if (code->expr1 && !dc_mask_inline_ok (&scan))
+    {
+      gfc_warning (OPT_Wdo_concurrent_to_openmp, "DO CONCURRENT at %L stays "
+		   "serial because its body defines a variable that its mask "
+		   "references, or the mask references a procedure",
+		   &code->loc);
+      return GFC_DC_OMP_NONE;
+    }
+
   if (flag_do_concurrent_to_openmp == GFC_DC_OMP_DEVICE)
     {
       if (dc_device_ok (&scan))
@@ -12463,10 +12541,11 @@ gfc_trans_omp_do_concurrent (gfc_code *code, enum gfc_dc_to_omp kind)
     }
 
   /* 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.  */
+     without concurrent-controls that shares CODE's mask, body and
+     locality specs, and that a CYCLE of CODE targets meanwhile.  */
   iter = gfc_get_code (EXEC_DO_CONCURRENT);
   iter->loc = code->loc;
+  iter->expr1 = code->expr1;
   iter->block = code->block;
   memcpy (iter->ext.concur.locality, code->ext.concur.locality,
 	  sizeof (iter->ext.concur.locality));
diff --git a/gcc/fortran/trans-stmt.cc b/gcc/fortran/trans-stmt.cc
index ef49446d98d..9e0f81840c2 100644
--- a/gcc/fortran/trans-stmt.cc
+++ b/gcc/fortran/trans-stmt.cc
@@ -5781,9 +5781,27 @@ gfc_trans_do_concurrent_iteration (gfc_code *code)
 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.  */
+     construct lowered to OpenMP by gfc_trans_omp_do_concurrent, which
+     evaluates the mask in the iteration.  */
   if (code->ext.concur.forall_iterator == NULL)
-    return gfc_trans_do_concurrent_iteration (code);
+    {
+      tree body = gfc_trans_do_concurrent_iteration (code);
+      if (!code->expr1)
+	return body;
+
+      stmtblock_t block;
+      gfc_se se;
+      gfc_init_block (&block);
+      gfc_init_se (&se, NULL);
+      gfc_conv_expr_val (&se, code->expr1);
+      gfc_add_block_to_block (&block, &se.pre);
+      tree tmp = build3_v (COND_EXPR,
+			   fold_convert (logical_type_node, se.expr), body,
+			   build_empty_stmt (input_location));
+      gfc_add_expr_to_block (&block, tmp);
+      gfc_add_block_to_block (&block, &se.post);
+      return gfc_finish_block (&block);
+    }
 
   enum gfc_dc_to_omp kind = gfc_dc_omp_lowering (code);
   if (kind != GFC_DC_OMP_NONE)
diff --git a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_10.f90 b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_10.f90
new file mode 100644
index 00000000000..72be40e2109
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_10.f90
@@ -0,0 +1,53 @@
+! { dg-do compile }
+! { dg-additional-options "-fdo-concurrent-to-openmp=host -fdump-tree-original" }
+! PR 128590
+!
+! A masked DO CONCURRENT is lowered to OpenMP with the mask evaluated at
+! the start of each iteration, which is equivalent to evaluating it for
+! all index values first (F2018 11.1.7.4.2) only if the body defines
+! nothing that the mask references.
+
+module m
+  implicit none
+contains
+  pure logical function positive (x)
+    real, intent(in) :: x
+    positive = x > 0.0
+  end function positive
+end module m
+
+subroutine masked (a, b, n)
+  integer :: n, i, j
+  real :: a(n,n), b(n,n)
+  do concurrent (i = 1:n, j = 1:n, a(i,j) > 0.0 .and. i /= j)
+     b(i,j) = sqrt (a(i,j))
+  end do
+end subroutine masked
+
+subroutine mask_reads_body_def (a, n)
+  integer :: n, i
+  real :: a(n)
+  do concurrent (i = 1:n-1, a(i+1) > 0.0)
+     a(i) = -a(i)
+  end do
+end subroutine mask_reads_body_def
+
+subroutine mask_reads_reduce (a, n, s)
+  integer :: n, i
+  real :: a(n), s
+  do concurrent (i = 1:n, s < 100.0) reduce(+:s)
+     s = s + a(i)
+  end do
+end subroutine mask_reads_reduce
+
+subroutine mask_calls (a, b, n)
+  use m
+  integer :: n, i
+  real :: a(n), b(n)
+  do concurrent (i = 1:n, positive (a(i)))
+     b(i) = a(i)
+  end do
+end subroutine mask_calls
+
+! { dg-final { scan-tree-dump-times "#pragma omp parallel" 1 "original" } }
+! { dg-final { scan-tree-dump-times "collapse\\(2\\)" 2 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_18.f90 b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_18.f90
new file mode 100644
index 00000000000..0513da37341
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_18.f90
@@ -0,0 +1,36 @@
+! { dg-do compile }
+! { dg-additional-options "-fdo-concurrent-to-openmp=device -fdump-tree-original" }
+!
+! PR 128590
+
+module m
+contains
+  subroutine mask_def (a, b, t)
+    integer :: a(100), b(100), i, t
+    do concurrent (i = 1:100, a(i) > 0)
+      t = a(i)
+      b(i) = t
+    end do
+  end subroutine
+
+  subroutine mask_alias (a, n)
+    integer :: n, i
+    integer :: a(n)
+    associate (v => a)
+      do concurrent (i = 2:n, v(i-1) > 0)
+        a(i) = -a(i)
+      end do
+    end associate
+  end subroutine
+
+  subroutine mask_reduce (a, b, s)
+    integer :: a(10), b(10), i, s
+    do concurrent (i = 1:10, s > 0) reduce(+:s)
+      b(i) = a(i) + s - s
+    end do
+  end subroutine
+end module m
+
+! Only mask_def is lowered, on the host.
+! { dg-final { scan-tree-dump-not "#pragma omp target" "original" } }
+! { dg-final { scan-tree-dump-times "#pragma omp parallel" 1 "original" } }
diff --git a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_9.f90 b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_9.f90
index e74cfae08fa..9fe74687e36 100644
--- a/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_9.f90
+++ b/gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_9.f90
@@ -30,8 +30,8 @@ subroutine reasons (a, b, n, s, idx, p)
   real, pointer :: p(:)
   real, allocatable :: w(:)
 
-  do concurrent (i = 1:n, a(i) > 0.0) ! { dg-warning "with a mask stays serial" }
-     b(i) = a(i)
+  do concurrent (i = 1:n-1, a(i+1) > 0.0) ! { dg-warning "stays serial because its body defines a variable that its mask references" }
+     a(i) = -a(i)
   end do
 
   do concurrent (i = 1:n)
diff --git a/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_7.f90 b/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_7.f90
new file mode 100644
index 00000000000..3dce7655525
--- /dev/null
+++ b/libgomp/testsuite/libgomp.fortran/do_concurrent_to_openmp_7.f90
@@ -0,0 +1,48 @@
+! { dg-do run }
+! { dg-additional-options "-fdo-concurrent-to-openmp=device" }
+! PR 128590
+!
+! Masked DO CONCURRENT constructs lowered to OpenMP loops, on the device
+! and on the host, compute the same as the serial lowering, including the
+! one whose body defines what its mask references, which stays serial
+! (F2018 11.1.7.4.2).
+
+program main
+  implicit none
+  integer, parameter :: n = 300
+  integer :: i, j
+  real :: a(n,n), b(n,n), v(n), s
+
+  do j = 1, n
+     do i = 1, n
+        a(i,j) = real(mod(i * 7 + j * 3, 11)) - 5.0
+     end do
+  end do
+
+  b = -1.0
+  do concurrent (i = 1:n, j = 1:n, a(i,j) > 0.0 .and. i /= j)
+     b(i,j) = sqrt (a(i,j))
+  end do
+  do j = 1, n
+     do i = 1, n
+        if (a(i,j) > 0.0 .and. i /= j) then
+           if (b(i,j) /= sqrt (a(i,j))) stop 1
+        else if (b(i,j) /= -1.0) then
+           stop 2
+        end if
+     end do
+  end do
+
+  s = 0.0
+  do concurrent (i = 1:n, a(i,1) < 0.0) reduce(+:s)
+     s = s + 1.0
+  end do
+  if (s /= real(count (a(:,1) < 0.0))) stop 3
+
+  ! Every mask value is fixed before any iteration negates an element.
+  v = 1.0
+  do concurrent (i = 1:n-1, v(i+1) > 0.0)
+     v(i) = -v(i)
+  end do
+  if (any (v(1:n-1) /= -1.0) .or. v(n) /= 1.0) stop 4
+end program main
-- 
2.56.1

Reply via email to