Issue 168978
Summary [Flang] Incorrect result of SIZE intrinsic function when the actual argument is LBOUND, UBOUND or SHAPE intrinsic function
Labels flang
Assignees
Reporter boo-aboutme
    ```
Version of flang : 22.0.0(7761a89e1230d8ccbb8e6ee15dc14ea74792a53c)/AArch64
```

On line 12 in the attached program (`test.f90`), `size(lbound(dd))` incorrectly returns 1 with flang version 22.0.0, while it returns 0 with version 21.1.2 or earlier.
According to the standard, the result of the LBOUND intrinsic function is an array of rank one and size 0 when the actual parameter for ARRAY is a scalar variable. Therefore, the result of SIZE with the actual parameter of the result of the LBOUND should be 0.
As far as we investigated, this problem happens not only with LBOUND but also with UBOUND or SHAPE.

More precisely, when the following conditions, the result of the intrinsic function SIZE is incorrect:  
1) The intrinsic function LBOUND, UBOUND, or SHAPE reference appears in the actual argument of intrinsic function SIZE reference. And,  
2) The actual argument of the intrinsic function LBOUND, UBOUND, or SHAPE reference of 1) is a variable of dummy argument with assumed type and assumed rank. and,  
3) The actual argument is a scalar that is corresponding to the dummy argument with assumed type and assumed rank of 2).  

```text
16.9.119 LBOUND (ARRAY [, DIM, KIND])
:
Result Characteristics. Integer. If KIND is present, the kind type parameter is that specified by the value of
KIND; otherwise the kind type parameter is that of default integer type. The result is scalar if DIM is present;
otherwise, the result is an array of rank one and size n, where n is the rank of ARRAY.
```

test.f90
```fortran
module m
implicit none
contains

subroutine sub(dd)
 implicit none
 type(*)::dd(..)
 print *,'rank(dd)        :',rank(dd)
  print *,'size(dd) :',size(dd)
  print *,'lbound(dd)      : ',lbound(dd)
  print *,'rank(lbound(dd)):',rank(lbound(dd))
  print *,'size(lbound(dd)):',size(lbound(dd))  ! Incorrect
  if(size(lbound(dd)) /= 0) print*,'NG size(lbound(dd)):' ,size(lbound(dd))
  if(size(ubound(dd)) /= 0) print*,'NG size(ubound(dd)):' ,size(ubound(dd))
  if(size(shape(dd))/=0 ) print*,'NG size(shape(dd)):',size(shape(dd))
end subroutine sub

end module m

use m
integer::scalar
call sub(scalar)
print *,'pass'
end
```

```
$ flang --version
flang version 22.0.0git (https://github.com/llvm/llvm-project.git 7761a89e1230d8ccbb8e6ee15dc14ea74792a53c)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /work/groups/ssoft/compiler/llvm/aarch64/main-20251117-7761a89e1230/bin
Build config: +assertions
$ flang test.f90
$ ./a.out
 rank(dd)        : 0
 size(dd)        : 1
 lbound(dd)      :
 rank(lbound(dd)): 1
 size(lbound(dd)): 1
 NG size(lbound(dd)): 1
 NG size(ubound(dd)): 1
 NG size(shape(dd)): 1
 pass
```

```
$ /work/groups/ssoft/compiler/llvm/aarch64/21.1.6/bin/flang --version
flang version 21.1.6 (https://github.com/llvm/llvm-project.git a832a5222e489298337fbb5876f8dcaf072c5cca)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /work/groups/ssoft/compiler/llvm/aarch64/21.1.6/bin
$ /work/groups/ssoft/compiler/llvm/aarch64/21.1.6/bin/flang test.f90
$ ./a.out
 rank(dd)        : 0
 size(dd)        : 1
 lbound(dd)      :
 rank(lbound(dd)): 1
 size(lbound(dd)): 0
 pass
```



```
$ ifx --version
ifx (IFX) 2024.2.1 20240711
Copyright (C) 1985-2024 Intel Corporation. All rights reserved.
$ ifx test.f90
$ ./a.out
 rank(dd) :           0
 size(dd)        :           1
 lbound(dd)      :
 rank(lbound(dd)):           1
 size(lbound(dd)):           0
 pass
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to