See attached patch, 7/10 of the DO CONCURRENT to OpenMP series.
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 or LOCAL_INIT spec: inside the iteration that name
would be the iteration's own copy. 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 and the mask references no procedure other than an
intrinsic one, so do that in the OpenMP lowering, and keep the serial
lowering otherwise.
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.
(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.
libgomp/ChangeLog:
* testsuite/libgomp.fortran/do_concurrent_to_openmp_7.f90: New test.
>From beb1460b8bf44ef6d72e6cb5cab2e9164ad3e1a8 Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Tue, 10 Nov 2026 12:31:46 -0800
Subject: [PATCH] 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 and the mask references no procedure other than an
intrinsic one, so do that in the OpenMP lowering, and keep the serial
lowering otherwise.
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.
(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.
libgomp/ChangeLog:
* testsuite/libgomp.fortran/do_concurrent_to_openmp_7.f90: New test.
---
gcc/fortran/invoke.texi | 5 +-
gcc/fortran/trans-openmp.cc | 63 ++++++++++++++++---
gcc/fortran/trans-stmt.cc | 22 ++++++-
.../gomp/do_concurrent_to_openmp_10.f90 | 53 ++++++++++++++++
.../gomp/do_concurrent_to_openmp_9.f90 | 4 +-
.../do_concurrent_to_openmp_7.f90 | 48 ++++++++++++++
6 files changed, 181 insertions(+), 14 deletions(-)
create mode 100644 gcc/testsuite/gfortran.dg/gomp/do_concurrent_to_openmp_10.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 316a4e98ac4..1770b507fba 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 674f712442a..8c5cde57faa 100644
--- a/gcc/fortran/trans-openmp.cc
+++ b/gcc/fortran/trans-openmp.cc
@@ -11684,6 +11684,10 @@ dc_scan_body (gfc_code *code, dc_scan *scan)
scan->part_defs.truncate (0);
scan->ok = true;
+ /* The mask is evaluated before the body. */
+ if (code->expr1)
+ gfc_expr_walker (&code->expr1, dc_scan_expr, scan);
+
if (!dc_scan_list (scan, code->block->next))
return false;
@@ -11849,6 +11853,44 @@ dc_warn (dc_reason *r, bool device)
}
+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;
+ if (scan->defined.contains (sym))
+ return 1;
+ for (int i = LOCALITY_LOCAL; i <= LOCALITY_LOCAL_INIT; i++)
+ for (gfc_expr_list *el = scan->dc->ext.concur.locality[i];
+ el; el = el->next)
+ if (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
@@ -11939,13 +11981,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;
- }
-
/* The OpenMP loop would not honor these directives. */
for (gfc_forall_iterator *fa = code->ext.concur.forall_iterator;
fa; fa = fa->next)
@@ -11971,6 +12006,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))
@@ -12274,10 +12318,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_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