https://gcc.gnu.org/g:563fa2230a5eed94c5771b38b2e140e6f6885393
commit r17-6371-g563fa2230a5eed94c5771b38b2e140e6f6885393 Author: Jerry DeLisle <[email protected]> Date: Mon Nov 16 17:54:02 2026 -0800 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. Diff: --- gcc/fortran/trans-stmt.cc | 10 ++++++ gcc/testsuite/gfortran.dg/do_concurrent_16.f90 | 46 ++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/gcc/fortran/trans-stmt.cc b/gcc/fortran/trans-stmt.cc index 74fd5317f02a..79c3d1e1fe09 100644 --- a/gcc/fortran/trans-stmt.cc +++ b/gcc/fortran/trans-stmt.cc @@ -5477,6 +5477,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 000000000000..4fc10025eecd --- /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
