Bugreport for nvfortran: Treatment of array bounds in the specification part

Hi all, recently I’ve faced an interesting difference in behaviour between nvfortran and e.g. gfortran.

Briefly, if there is a function, where in the specification part the ubounds/lbounds functions are used in the specification part, e.g.

 function justcopy(arr_in) 
   real, intent(in) :: arr_in(0:,:)
   real :: justcopy(0:ubound(arr_in,dim=1), 2:5) 
....

the actual size of the declared array will be different, as if ubound is calculated before the declaration of the array. See details An interesting difference between compilers - Help - Fortran Discourse
My assumption is that such a behaviour is a bug.

Best regards,
Andrii

Thanks Andrii. I filed a report, TPR #34980, and sent it to engineering for investigation.

-Mat

Hi, just checking in to see if there are any other updates. I’m also attaching a simpler version of the program that illustrates the bug. It still seems to be present in nvfortran 24.11

program aocc_run_test
  implicit none
  real :: arr_in1(0:10, 2:5), arr1(0:10,2:5)
  integer  i, j
  do i=0,10 
  do j=2,5
    arr_in1(i,j) = 1000*i+j
  end do
  end do

  call evaluate(arr_in1,arr1)
contains

  function justcopy(arr_in)
   implicit none
   real, intent(in) :: arr_in(0:,:)
   real :: justcopy(0:ubound(arr_in,dim=1), 2:5)  !This works for "A" GNU,Intel/InteLLVM,lfortran,NAG,IBM. Does not work for "B" AOCC, NVidia, ARM.
   !real :: justcopy(0:size(arr_in,dim=1)-1, 2:5) !This works for all compilers
   write(*,*) "arr_in, dim 1 (lb,ub,sz):", lbound(arr_in,dim=1),ubound(arr_in,dim=1),size(arr_in,1)
   write(*,*) "arr_in, dim 2 (lb,ub,sz):", lbound(arr_in,dim=2),ubound(arr_in,dim=2),size(arr_in,2)
   write(*,*) "justcp, dim 1 (lb,ub,sz):", lbound(justcopy,dim=1),ubound(justcopy,dim=1),size(justcopy,1)
   write(*,*) "justcp, dim 2 (lb,ub,sz):", lbound(justcopy,dim=2),ubound(justcopy,dim=2),size(justcopy,2)
   justcopy=arr_in
  end function justcopy
  
  subroutine evaluate(arr_in1,arr_out1)
    implicit none
    real,    intent(in)  :: arr_in1(:,:)
    real,    intent(out) :: arr_out1(:,:)
    real, allocatable  :: X(:,:)

    !arr_out1 = justcopy(arr_in1)
    X = justcopy(arr_in1)       
    write(*,*) "X     , dim 1 (lb,ub,sz):", lbound(X,dim=1),ubound(X,dim=1),size(X,1)
    write(*,*) "X     , dim 2 (lb,ub,sz):", lbound(X,dim=2),ubound(X,dim=2),size(X,2)

    if (size(X,1) /= size(arr_in1,1)) then
       print "(A,I2,A,I2)","Error: size of X(dim1)=",size(X,1),&
                           " was not equal to size of arr_in1(dim1)=",size(arr_in1,1)
      error stop 1
    end if
    
    arr_out1 = X
  end subroutine evaluate  
end program aocc_run_test

Hi Gavin,

Engineering has been deferring all but the most critical bugs in nvfortran. We’ve been focusing our efforts in working with the LLVM community on a new flang compiler which we plan on using to replace the current nvfortran. I checked your example with the development version of this compiler and it worked as expected.

So while this will unlikely be fixed in the current nvfortran, it should be fixed when we release this new compiler.

-Mat