The following is what is left after trimming a 25,000 LOC Fortran 95 program to pin down a suspected optimisation bug.
program labDObug
implicit none
integer, parameter :: nx = 5, ny = 1, nz = 1
integer i, j, k, ij, ik
integer :: np(nx,ny), ktop(nx)
np(:,1) = (/ (i, i=1,nx) /)
ktop = 0
do j = 1,ny
iLoop: DO i = 1, nx
ij = (j-1)*nx + i
do k = nz, 1, -1
ik = (k-1)*nx + i
if (np(ik,j) /= 0) then
ktop(ij) = k
cycle iLoop
endif
end do !k
end do iLoop
end do !j
print '(1x,10i5)',ktop
end program
The output when the options /opt /64 are used:
1 0 0 0 0
The correct output, which is obtained with /64 alone, or in 32-bit compilations with or without /opt:
1 1 1 1 1
In this tiny program, replacing 'cycle iLoop' by 'exit' makes the bug go away.