[Flang] lbound for a dummy argument corresponding to an interoperable formal parameter
flang version 23.0.0git (https://github.com/llvm/llvm-project.git f3b96cbf20137562ff069e315f7915524a350d08)
Target: aarch64-unknown-linux-gnu
Issue Summary:
lbound returns an incorrect value (1) for a dummy argument corresponding to an interoperable formal parameter with the bind(c) attribute. According to the Fortran 2023 standard (18.5.3), it should return 0.
Expected behavior: lbound(x) should return 0
Actual behavior: lbound(x) returns 1
test.f90
module m1
interface
subroutine sss(x) bind(c)
integer, target, dimension(..):: x
end subroutine
end interface
end module
program main
use m1
integer :: a(5:7) = [10, 20, 30]
print *,lbound(a)
call sss(a)
end
subroutine sss(x) bind(c)
integer, target, dimension(..):: x
print *,lbound(x)
end subroutine
$ flang --version
flang version 23.0.0git (https://github.com/llvm/llvm-project.git f3b96cbf20137562ff069e315f7915524a350d08)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /work/groups/ssoft/compiler/llvm/aarch64/main-20260216-f3b96cbf2013/bin
Build config: +assertions
$ flang test.f90 && ./a.out
5
1
The standard says:
18.3.7 Interoperability of procedures and procedure interfaces
A Fortran procedure interface is interoperable with a C function prototype if ...
(5) any dummy argument without the VALUE attribute corresponds to a formal parameter of the
prototype that is of a pointer type, and either ...
* the dummy argument is allocatable, assumed-shape, assumed-rank, or a pointer without the
CONTIGUOUS attribute, and the formal parameter is a pointer to CFI_cdesc_t ...
18.5.3 The CFI_cdesc_t structure type
For a C descriptor of an array pointer or allocatable array, the value of the lower_bound member of each element
of the dim member of the descriptor is determined by argument association, allocation, or pointer association.
For a C descriptor of a nonallocatable nonpointer object, the value of the lower_bound member of each element
of the dim member of the descriptor is zero.
In subroutine sss, dummy argument x is assumed-rank without the CONTIGUOUS attribute, and it corresponds
to a formal parameter that is a pointer to CFI_cdesc_t. So the lower_bound member of x should be 0.
Intel ifx returns 0.
$ ifx --version
ifx (IFX) 2024.2.1 20240711
Copyright (C) 1985-2024 Intel Corporation. All rights reserved.
$ ifx test.f90 && ./a.out
5
0
Fujitsu Fortran also returns 0.
$ frt --version
frt (FRT) 4.12.1 20250711
Copyright FUJITSU LIMITED 2019-2025
$ frt test.f90 && ./a.out
5
0
FYI, GNU Fortran returns 1, same as Flang.
$ gfortran --version
GNU Fortran (GCC) 11.5.0 20240719 (Red Hat 11.5.0-5)
Copyright (C) 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ gfortran test.f90 && ./a.out
5
1
Lower bound values for dummy arguments to Fortran functions and subroutines default to 1 when not specified (which they cannot be in the case of assumed-rank arguments. So one should always observe LBOUND(a) to be all ones for assumed-rank dummy arguments in Fortran code. (See 16.9.119 paragraph 9, item (i), and note that an assumed-rank entity is not a "whole array".). The text that you quote from the standard pertains to the lower bound values passed in the interoperable descriptor, and those can be observed only from C++ code.
module m1
interface
subroutine sssf(x) bind(c)
integer, target, dimension(..):: x
end subroutine
subroutine sssc(x) bind(c)
integer, target, dimension(..):: x
end subroutine
end interface
end module
program main
use m1
integer :: a(5:7) = [10, 20, 30]
print *,lbound(a)
call sssf(a)
call sssc(a)
end
subroutine sssf(x) bind(c)
integer, target, dimension(..):: x
print *, 'in sssf:', lbound(x)
end subroutine
#include <ISO_Fortran_binding.h>
#include <cstdio>
extern "C" {
void sssc(const CFI_cdesc_t *x) {
int rank{x->rank};
for (int j{0}; j < rank; j++) {
std::printf("in sssc, [%d] lb %ld, extent %ld, sm %ld\n", j,
x->dim[j].lower_bound, x->dim[j].extent, x->dim[j].sm);
}
}
} // extern "C"
In short, the bug is in IFX, FRT, and Cray, which are extracting irrelevant lower bounds from the descriptor to implement LBOUND. GNU Fortran, flang-new, nvfortran, NAG, and XLF are correct.