https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128791
Bug ID: 128791
Summary: [OpenMP] DO variable of collapsed SIMD loop with
non-constant step is shared
Product: gcc
Version: 16.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: jvdelisle at gcc dot gnu.org
Target Milestone: ---
When a loop in a collapsed SIMD, DO SIMD or PARALLEL DO SIMD nest has a
step that is not a compile-time constant, gfc_trans_omp_do iterates a
separate counter and assigns the DO variable from it in the body. The
DO variable is then no longer the predetermined iteration variable, but
nothing privatizes it either, so it stays shared and the threads race
on it.
program main
implicit none
integer :: i, j, n, st
integer :: a(50,3)
n = 50
st = 3
a = 0
!$omp parallel do simd collapse(2)
do i = 1, n, st
do j = 1, 3
a(i,j) = a(i,j) + 1
end do
end do
do i = 1, 50
if (any (a(i,:) /= merge (1, 0, mod (i - 1, st) == 0))) stop 1
end do
end program main
$ gfortran -fopenmp -O0 pr.f90 && OMP_NUM_THREADS=4 ./a.out
STOP 1 (20 of 20 runs)
-fdump-tree-omplower shows the problem at every -O level:
#pragma omp parallel ... lastprivate(j) shared(i) shared(a)
The race only produces wrong results at -O0; at -O1 and above the DO
variable usually lives in a register, which hides it. It passes with
OMP_NUM_THREADS=1, with a constant step (PARAMETER st = 3), without
SIMD (parallel do collapse(2)), or without collapse. A single SIMD
loop makes the DO variable linear and the non-SIMD path makes it
private; only the collapsed SIMD path misses it.
Fix: make the DO variable lastprivate in that case (POINTER and
ALLOCATABLE DO variables already use a temporary and are left alone).