Issue 183234
Summary [Flang] lbound for a dummy argument corresponding to an interoperable formal parameter
Labels flang
Assignees
Reporter boo-aboutme
    ``` text
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

``` fortran
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
```


``` text
$ 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:

``` text
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 ...

```

``` text
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.

``` text
$ 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.

``` text
$ 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.

``` text
$ 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
```


_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to