Nvfortran shape() intrinsic produces bad results for 64bit element types

Although it isn’t documented in the NVIDIA HPC information, the shape() intrinsic function supports a kind=... argument to specify the kind of elements returned by the function. Unfortunately, it seems that when the element-type is 64-bit, the results appear to be incorrect.

Here is a test program:

program badshape
   use iso_c_binding
   implicit none

   integer :: test_array(5, 2)

   integer :: dims(2)
   integer(kind=c_size_t) :: dims_csizet(2)
   integer(c_int16_t) :: dims_int16(2)
   integer(c_int32_t) :: dims_int32(2)
   integer(c_int64_t) :: dims_int64(2)

   dims = shape(test_array)
   dims_csizet = shape(test_array, kind=c_size_t)
   dims_int16 = shape(test_array, kind=c_int16_t)
   dims_int32 = shape(test_array, kind=c_int32_t)
   dims_int64 = shape(test_array, kind=c_int64_t)

   write(*,*) "dims=", dims
   write(*,*) "dims_csizet=", dims_csizet
   write(*,*) "dims_int16=", dims_int16
   write(*,*) "dims_int32=", dims_int32
   write(*,*) "dims_int64=", dims_int64

end program badshape

When compiled with nvfortran and run, I get this result:

$ nvfortran badshape.f90 -o badshape
$ ./badshape
 dims=            5            2
 dims_csizet=               8589934597          139812351164816
 dims_int16=       5       2
 dims_int32=            5            2
 dims_int64=               8589934597          139812351164816

When compiled with gfortran and run, I get this result:

$ gfortran badshape.f90 -o gf_badshape
$ ./gf_badshape 
 dims=           5           2
 dims_csizet=                    5                    2
 dims_int16=      5      2
 dims_int32=           5           2
 dims_int64=                    5                    2

Please let me know if I have some misunderstanding in the construction of the program.


Thanks Donnie. I was able to recreate the error here and filed an issue report (TPR #32053).


I should probably also mention that further testing revealed that this seems to produce the correct values:

   integer :: test_array(5, 2)
   integer(c_int64_t) :: dims_int64(2)
   dims_int64 = shape(test_array)
   write(*,*) "dims_int64=", dims_int64

(Note no kind= argument to shape().)

I’m not sure if this can be trusted to work in optimized code; I compiled that code in debug mode.