The following looks to be a bug in the ifort compiler. The example program is:
module srdt_mod implicit none type simple_struct complex(8) c real(8) r end type type special_struct type(simple_struct) s end type contains subroutine calc(srdt) implicit none complex(8) sm type(simple_struct) srdt type(special_struct), allocatable :: eles(:) allocate(eles(17)) !16 does not crash sm = (1.0d0, 1.0d0) eles%s%c = sm sm = sum(eles%s%c) print *, 'Sum: ', sm end subroutine end module !-------------- program prog use srdt_mod implicit none type(simple_struct) srdt call calc(srdt) end program
Running the program gives:
> ifort prog.f90; ./a.out
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image PC Routine Line Source
a.out 0000000000404983 Unknown Unknown Unknown
libpthread-2.17.s 00007F045884E680 Unknown Unknown Unknown
a.out 000000000040378E Unknown Unknown Unknown
a.out 0000000000403632 Unknown Unknown Unknown
libc-2.17.so 00007F04584943D5 __libc_start_main Unknown Unknown
a.out 0000000000403529 Unknown Unknown Unknown
1) Bug appears with ifort V17, V18, V19.
2) If eles is allocated to size 16 there is no problem
3) If "-g" debug is used on the compile line there is no problem.
This is an optimization problem. -O0 and -O1 work, -O2 and higher fails. The funny thing is that the code works also with kind type 4 instead of 8, and also with kind type 16. Also removal of the unused argument srdt makes the segfault go away. Looks like that is really an Intel bug. Also adding debug symbols (-g) makes the bug go away.
I know this issue is reported for the Intel compiler; however, I was curious how gfortran would behave. I just tested out this code with gfortran 7.3.0 on a Linux machine. I can confirm that the code works fine, regardless of the optimization level
The optimizer bug is also present in the Windows version of the 18.0.5 compiler. Please file a formal report at https://supporttickets.intel.com/?lang=en-US .
The following version displays the same kind of optimizer bug and involves a much smaller array.
subroutine calc(srdt) implicit none type(simple_struct) srdt type(special_struct), allocatable :: eles(:) integer i,n n = 2 allocate(eles(n)) do i=1,n/2 eles(2*i-1)%s%c = i*(1.d0,1.d0) eles(2*i)%s%c = i*(1.d0,-1.d0) end do print *, 'Sum: ', sum(eles%s%c) end subroutine