| Issue |
243702
|
| Summary |
[flang] SIZE of an explicit-shape array bounded by SIZE of a module or host pointer is re-evaluated after the pointer is re-associated
|
| Labels |
flang
|
| Assignees |
|
| Reporter |
eugeneepshteyn
|
When the bound of an explicit-shape local array is `size(p)` for a pointer `p` that is not a dummy argument (a module variable, a host-associated variable, or a host variable used in a BLOCK), a later `size(a)` returns the size of whatever `p` points to at that time, not the bound fixed on entry:
```fortran
module m
integer, pointer :: mp(:)
contains
subroutine from_module(t)
integer, target :: t(:)
integer :: a(size(mp))
mp => t
print *, 'module pointer: ', size(a), ' (expect 3)'
end subroutine
subroutine from_block(t, u)
integer, target :: t(:), u(:)
integer, pointer :: lp(:)
lp => t
block
integer :: a(size(lp))
lp => u
print *, 'BLOCK, host local:', size(a), ' (expect 3)'
end block
end subroutine
subroutine from_host(t, u)
integer, target :: t(:), u(:)
integer, pointer :: hp(:)
hp => t
call inner
contains
subroutine inner
integer :: a(size(hp))
hp => u
print *, 'host-associated: ', size(a), ' (expect 3)'
end subroutine
end subroutine
end module
program main
use m
integer, target :: three(3), nine(9)
mp => three
call from_module(nine)
call from_block(three, nine)
call from_host(three, nine)
end program
```
```console
$ flang -O0 repro.f90 -o repro && ./repro
module pointer: 9 (expect 3)
BLOCK, host local: 9 (expect 3)
host-associated: 9 (expect 3)
```
gfortran 16.1, ifx 2026.1, NAG Fortran 7.2 and nvfortran 26.9 all print 3 in all three cases. F2023 8.5.8.2 p5 says the bounds of such an array "are determined on entry to a procedure defined by the subprogram, or on execution of the BLOCK statement" and "are unaffected by the redefinition or undefinition of any variable during execution of the procedure or BLOCK construct".
It can also corrupt memory. Here the array constructor's temporary is sized from the re-evaluated extent, while the copy uses the extent fixed on entry:
```fortran
module m
integer, pointer :: mp(:)
contains
subroutine s(t)
integer, target :: t(:)
integer :: a(size(mp))
integer, allocatable :: r(:)
a = 1
mp => t
r = [a, a]
print *, size(r), ' (expect 6)'
end subroutine
end module
program main
use m
integer, target :: three(3), one(1)
mp => three
call s(one)
end program
```
flang prints `2` instead of `6`, and valgrind reports invalid writes. gfortran, ifx, NAG Fortran and nvfortran 26.9 print `6`.
`-fdebug-unparse` shows that `size(a)` is folded to `max(0_8,size(mp,dim=1,kind=8))`, which re-reads the pointer's descriptor at the point of use. The folding is allowed because `IsConstantExprHelper<INVARIANT=true>::operator()(const DescriptorInquiry &)` in `flang/lib/Evaluate/check-_expression_.cpp` treats a descriptor inquiry on any non-dummy, non-allocatable symbol as scope-invariant. A pointer's descriptor is not invariant unless its association is protected. For an INTENT(IN) pointer dummy it is (F2023 8.5.10 p2), so `size(p)` of such a dummy can stay foldable.
Reproduced on main at b7b5b15a0741e899a881fe6c6ed02a9c1d22dcdb, with and without PR 243466. That PR stops treating INTENT(IN) pointer targets as invariant, and its review notes the same shortcut also lets `size(b%p%q)` through a pointer component fold.
(This issue was filed with the help of AI)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs