This is an archive of the discontinued LLVM Phabricator instance.

[flang] Fix element indexing in Destroy component deallocation
ClosedPublic

Authored by clementval on Feb 22 2023, 5:03 AM.

Details

Summary

Derived type Destroy function does not take step into consideration
when indexing the component element for deallocation. This leads to
incorrect deallocation in case like:

module mod1
  type :: t
    real, allocatable :: r(:)
  end type
contains
  subroutine do_smth(c)
    class(t), intent(out) :: c(:)
    do i = 1, size(c)
      if (allocated(c(i)%r)) then
        print*, i, 'not deallocated'
      end if
    end do
  end subroutine
end module

program test
  use mod1
  type(t) :: z(6)
  integer :: i
  do i = 1, 6
    Allocate(z(i)%r(i))
  end do
  call do_smth(z(::2))
end

Similar change was done in D142527

Diff Detail

Event Timeline

clementval created this revision.Feb 22 2023, 5:03 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptFeb 22 2023, 5:03 AM
Herald added a subscriber: arphaman. · View Herald Transcript
clementval requested review of this revision.Feb 22 2023, 5:03 AM

The bug here is that the original code assumes that descriptor's array is contiguous. ZeroBasedIndexedElement is slow and we don't want to have to call it on every iteration. When the original descriptor is not contiguous, it would be better to use a vector of subscripts and adjust them with IncrementSubscripts on each iteration.

Use subscripts and increment them.

klausler accepted this revision.Feb 23 2023, 7:00 AM
klausler added inline comments.
flang/runtime/derived.cpp
229

descriptor.GetLowerBounds(at); is the usage that's most common in the runtime, and might be faster.

This revision is now accepted and ready to land.Feb 23 2023, 7:00 AM
clementval added inline comments.Feb 23 2023, 7:04 AM
flang/runtime/derived.cpp
229

Ok thanks. I'll update that.

Use GetLowerBounds