https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129176
Bug ID: 129176
Summary: OpenMP: POINTER/ALLOCATABLE DO variable segfaults or
races
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: jvdelisle at gcc dot gnu.org
Target Milestone: ---
Reported by Harald Anlauf on fortran@ in his comments on PR128791.
A DO variable with the POINTER or ALLOCATABLE attribute fails in OpenMP
loop constructs:
POINTER: the loop iteration variable is predetermined private (linear or
lastprivate for SIMD), and "the initial status of a private pointer is
undefined" (OpenMP 4.0, 2.14.3.3), so the DO statement defines through
an unassociated pointer and the program is not conforming. gfortran
gives no diagnostic. DO, PARALLEL DO, SIMD, DO SIMD, TASKLOOP, an
explicit PRIVATE, and a sequential DO inside PARALLEL all segfault. A
collapsed SIMD nest is the exception: nothing privatizes the pointer, so
in DO SIMD and the other composite constructs the threads race on its
target and update wrong elements. A standalone SIMD with COLLAPSE
happens to work (libgomp.fortran/non-rectangular-loop-3.f90 and -4 use
it).
ALLOCATABLE: this is valid; the private copy stays allocated, and
LASTPRIVATE assigns as if by intrinsic assignment. gfortran does not
privatize the variable in a collapsed SIMD nest, so it is shared (race)
in a subroutine, firstprivate (final value lost) in a main program, and
holds the value of the last iteration, without the step, after a
serial SIMD. An explicit lastprivate(k) gives the right result.
Reproducer (gfortran -fopenmp, OMP_NUM_THREADS=4):
program p
implicit none
integer, pointer :: i
integer, allocatable :: k
integer :: j, n, st, a(60,60)
allocate (i, k)
n = 50; st = -3
a = 0
!$omp parallel do simd collapse(2)
do k = n, 1, st
do j = 1, 3
a(k,j) = a(k,j) + 1
end do
end do
if (k /= -1) stop 1
!$omp parallel do
do i = 1, n
a(i,1) = 0
end do
end program p
Observed: STOP 1 (k is still 0). With the first loop removed, the
second one segfaults.
Expected: k == -1 after the first loop. For the POINTER loop, a
diagnostic.