https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122284
Bug ID: 122284
Summary: fortran: openmp private variables of types with
default initialization are not initialized
Product: gcc
Version: 15.2.0
Status: UNCONFIRMED
Keywords: openmp
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: roland_wirth at web dot de
Target Milestone: ---
The private copies of derived-type variables with default
initialization do not get initialized on entry into an OpenMP
parallel region. This is specified behavior until OpenMP 5.0. In
5.1, the respective section 2.21.3 has been revised, stating that
"If the type of the list item has default initialization, the new
list item has default initialization. Otherwise, the initial
value of the new list item is undefined."
Unfortunately, the behavior change is not recorded in Appendix B
of the OpenMP spec and there is no mention on GCC's OpenMP
implementation status page. So, this report is more of a request
to update to the newer behavior and for raising awareness rather
than reporting an actual bug.
The pre-5.1 behavior is surprising and leads to bugs when OpenMP
is enabled, especially when finalizers or destructor-like
functions are called, which have to determine if the object is
in a used state or not to, e.g., deallocate memory.
Example (https://gcc.godbolt.org/z/c5ec8nq47):
```
module test_mod
type test_type
real, pointer :: p => null()
end type
contains
subroutine do_something(a)
use omp_lib
type(test_type), intent(inout) :: a
if (associated(a%p)) then
print *, omp_get_thread_num(), a%p
else
print *, omp_get_thread_num(), 'not associated'
end if
end subroutine
subroutine use_test(a)
type(test_type), intent(inout) :: a
!$OMP PARALLEL PRIVATE(a)
call do_something(a)
!$OMP END PARALLEL
!$OMP PARALLEL PRIVATE(a)
call do_something(a)
!$OMP END PARALLEL
end subroutine
end module
program test_prg
use test_mod
type(test_type) :: aa
call use_test(aa)
end program
```
Expected output: all threads should print "not associated"
Actual output:
```
1 not associated
0 -1.15775987E-25
1 6.11371304E-24
0 -1.15775987E-25
```