See attached patch. When the body of a DO CONCURRENT defines a variable
that a concurrent-limit or concurrent-step references, the serial loops
use the new value, so the wrong iterations run. The start of an inner
concurrent-control is affected too, since its loop restarts on each
outer iteration. The fix has the loops of an outermost FORALL or DO
CONCURRENT use the values gfc_trans_forall_1 already evaluates up front.
A nested FORALL keeps the current code because its limits may use the
outer index-names.

The fix is one hunk in gfc_trans_forall_1 in trans-stmt.cc. The rest is
the new test. Its second case changes the limits of the inner loop. Its
third case changes those of the outer loop.

Regression tested on x86_64.

OK for mainline?

Regards,

Jerry

---

fortran: [PR128793] Evaluate DO CONCURRENT limits and step once

The serial loops of DO CONCURRENT read the variables referenced by the
concurrent-limits and concurrent-steps again during the iterations, so a
body that defined one changed which iterations ran, although F2018
11.1.7.4.2 evaluates them once.  This was because gfc_trans_forall_1
recorded the end and step for the loops before gfc_make_safe_expr copied
them into temporaries, and never made the start safe.  For an outermost
FORALL or DO CONCURRENT, evaluate the start once too and have the loops
use the evaluated start, end and step.  A nested FORALL is left alone,
since its limits may use the outer index-names.

Assisted-by: Claude Opus 5.5

        PR fortran/128793

gcc/fortran/ChangeLog:

        * trans-stmt.cc (gfc_trans_forall_1): For an outermost construct,
        evaluate the start once and use the evaluated start, end and step
        in the loops.

gcc/testsuite/ChangeLog:

        * gfortran.dg/do_concurrent_16.f90: New test.
>From 43c53aaa4642607047fc9d5719e248aaad74bafd Mon Sep 17 00:00:00 2001
From: Jerry DeLisle <[email protected]>
Date: Mon, 16 Nov 2026 17:54:02 -0800
Subject: [PATCH] fortran: [PR128793] Evaluate DO CONCURRENT limits and step
 once

The serial loops of DO CONCURRENT read the variables referenced by the
concurrent-limits and concurrent-steps again during the iterations, so a
body that defined one changed which iterations ran, although F2018
11.1.7.4.2 evaluates them once.  This was because gfc_trans_forall_1
recorded the end and step for the loops before gfc_make_safe_expr copied
them into temporaries, and never made the start safe.  For an outermost
FORALL or DO CONCURRENT, evaluate the start once too and have the loops
use the evaluated start, end and step.  A nested FORALL is left alone,
since its limits may use the outer index-names.

Assisted-by: Claude Opus 5.5

	PR fortran/128793

gcc/fortran/ChangeLog:

	* trans-stmt.cc (gfc_trans_forall_1): For an outermost construct,
	evaluate the start once and use the evaluated start, end and step
	in the loops.

gcc/testsuite/ChangeLog:

	* gfortran.dg/do_concurrent_16.f90: New test.
---
 gcc/fortran/trans-stmt.cc                     | 10 ++++
 .../gfortran.dg/do_concurrent_16.f90          | 46 +++++++++++++++++++
 2 files changed, 56 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/do_concurrent_16.f90

diff --git a/gcc/fortran/trans-stmt.cc b/gcc/fortran/trans-stmt.cc
index 534c2e67397..d022350cffc 100644
--- a/gcc/fortran/trans-stmt.cc
+++ b/gcc/fortran/trans-stmt.cc
@@ -5606,6 +5606,16 @@ gfc_trans_forall_1 (gfc_code * code, forall_info * nested_forall_info)
       gfc_add_block_to_block (&block, &se.pre);
       step[n] = se.expr;
 
+      /* Use the limits and step evaluated once (F2018 11.1.7.4.2), except
+	 in a nested FORALL, whose limits may use outer index-names.  */
+      if (!nested_forall_info)
+	{
+	  start[n] = gfc_evaluate_now (start[n], &block);
+	  this_forall->start = start[n];
+	  this_forall->end = end[n];
+	  this_forall->step = step[n];
+	}
+
       /* Copy loop annotations.  */
       this_forall->annot = fa->annot;
 
diff --git a/gcc/testsuite/gfortran.dg/do_concurrent_16.f90 b/gcc/testsuite/gfortran.dg/do_concurrent_16.f90
new file mode 100644
index 00000000000..4fc10025eec
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/do_concurrent_16.f90
@@ -0,0 +1,46 @@
+! { dg-do run }
+!
+! PR 128793
+
+program main
+  implicit none
+  integer :: s, lo, hi, st, i, j, k, a(5), b(5,5)
+
+  s = 2
+  a = 0
+  do concurrent (k = 1:5:s)
+     a(k) = k
+     if (k == 1) s = 1
+  end do
+  if (any (a /= [1, 0, 3, 0, 5])) stop 1
+
+  lo = 1
+  hi = 3
+  st = 1
+  b = 0
+  do concurrent (i = lo:hi:st, j = 1:3)
+     b(i,j) = 1
+     if (i == 1 .and. j == 1) call change (lo, hi, st)
+  end do
+  if (any (b(1:3,1:3) /= 1) .or. sum (b) /= 9) stop 2
+
+  lo = 1
+  hi = 3
+  st = 1
+  b = 0
+  do concurrent (j = 1:3, i = lo:hi:st)
+     b(i,j) = 1
+     if (i == 1 .and. j == 1) call change (lo, hi, st)
+  end do
+  if (any (b(1:3,1:3) /= 1) .or. sum (b) /= 9) stop 3
+
+contains
+
+  pure subroutine change (lo, hi, st)
+    integer, intent(out) :: lo, hi, st
+    lo = 2
+    hi = 2
+    st = 2
+  end subroutine change
+
+end program main
-- 
2.56.0

Reply via email to