Silverfrost Forums

Welcome to our forums

Labelled DO loop and /opt /64 leads to optimisation bug

9 Mar 2024 5:35 #31248

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.

10 Mar 2024 4:31 #31254

The following update identifies that ij is not being updated in the do i loop if /opt is selected

program labDObug
 use iso_fortran_env
   implicit none
   integer, parameter :: nx = 5, ny = 1, nz = 1
   integer i, j, k, ij, ik, n
   integer :: np(nx,ny), ktop(nx)

   write (*,*) 'Vern : ',compiler_version ()
   write (*,*) 'Opts : ',compiler_options ()

   np(:,1) = (/ (i, i=1,nx) /)
   ktop = 0
   n = 0
   do j = 1,ny
      iLoop: DO i = 1, nx
         ij = (j-1)*nx + i
         do k = nz, 1, -1
            n = n+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
   print '(1x,10i5)',n,ij

end program
18 Mar 2024 9:28 #31286

mecej4 and John

Thank you for the feedback. This failure has now been fixed for the next release of FTN95. The optimisation in question is number 30.

Please login to reply.