[flang] preserve pointer rank in polymorphic_pointer => NULL()
authorJean Perier <jperier@nvidia.com>
Mon, 3 Apr 2023 07:18:41 +0000 (09:18 +0200)
committerJean Perier <jperier@nvidia.com>
Mon, 3 Apr 2023 07:19:01 +0000 (09:19 +0200)
commit04a920b76acf0a52a3eb957c6331ba81a1173e2a
treee2def04fce0fea33e519663af1b345a4a27b01fa
parent9d69bca18787e0861c1bc6db8e82d4f21b77ae75
[flang] preserve pointer rank in polymorphic_pointer => NULL()

The current lowering for polymorphic pointer association was not
dealing with NULL in a "context aware" fashion: it was calling the
`PointerAssociate` runtime entry point with a fir.box<none> target.
But the fir.box<none> is a descriptor for a scalar, this lead the
runtime to set the pointer rank to zero, regardless of its actual
rank.

I do not think there is a way to expose this problem with the Fortran
code currently supported by flang, because most further manipulation of
the pointer would either set the rank correctly, or do not rely on the
rank in the runtime descriptor.

However, this is incorrect, and when assumed rank are supported, the
following would have failed:

```
subroutine check_rank(p)
  class(*), pointer :: p(..)
  p => null()
  select rank(p)
  rank (1)
   print *, "OK"
  rank default
   print *, "FAILED"
  end select
end subroutine
  class(*), pointer :: p(:)
  p => null()
  call check_rank(p)
end
```

Instead, detect NULL() in polymorphic pointer lowering and trigger the
deallocation of the pointer.

Differential Revision: https://reviews.llvm.org/D147317
flang/lib/Lower/Bridge.cpp
flang/lib/Optimizer/Builder/MutableBox.cpp
flang/test/Lower/pointer-disassociate.f90