I believe this is a bug in the Fortran-C interop features of nvfortran
. Take the following program as an example (program.F90
):
PROGRAM main
USE, INTRINSIC :: ISO_C_BINDING, ONLY: c_float
IMPLICIT NONE
INTERFACE
SUBROUTINE check_array(res) BIND(C)
IMPORT :: c_float
REAL(c_float), INTENT(inout), ALLOCATABLE :: res(:, :, :)
END SUBROUTINE check_array
END INTERFACE
REAL, ALLOCATABLE :: my_array(:, :, :)
! Everything works when the array is allocated - when the array is not
! allocated it fails
! ALLOCATE(my_array(3, 3, 3))
CALL wrapper(my_array)
CONTAINS
! When check_array is called through this wrapper it does not work as
! expected
SUBROUTINE wrapper(vertices)
REAL, ALLOCATABLE, INTENT(inout) :: vertices(:, :, :)
CALL check_array(vertices)
END SUBROUTINE wrapper
END PROGRAM main
with the accompanying C++ code (check_array.cpp
):
#include <ISO_Fortran_binding.h>
#include <iostream>
#include <cassert>
extern "C" {
void check_array(CFI_cdesc_t* data) {
std::cout << "Data rank: " << static_cast<int>(data->rank) << std::endl;
std::cout << "Data type: " << static_cast<int>(data->type) << std::endl;
assert (data->rank == 3);
assert (data->type == CFI_type_float || data->type == CFI_type_double);
}
}
Compiling this with nvc++ and nvfortran:
nvc++ -c check_array.cpp
nvfortran program.F90 check_array.o -lstdc++ -o test_cfi
and running it gives the following:
$ ./test_cfi
Data rank: 0
Data type: -1
test_cfi: check_array.cpp:11: void check_array(CFI_cdesc_t *): Assertion `data->rank == 3' failed.
Aborted (core dumped)
In the example program, everything works when the call to check_array
is performed directly in the same scope as the array declaration (i.e. replace wrapper
with check_array
). When the intermediate subroutine wrapper
is used it does not work.
It also works if I first call check_array
and then afterwards call wrapper
from the main program.
The example works in all other “major” compilers (GNU, Intel, NAG Fortran). I am using the following nvfortran version:
$ nvfortran --version
nvfortran 24.3-0 64-bit target on x86-64 Linux -tp alderlake
NVIDIA Compilers and Tools
Copyright (c) 2024, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
Any comments?