[flang-commits] [flang] e89a00d - [flang] Fix element indexing in Destroy component deallocation

Valentin Clement via flang-commits flang-commits at lists.llvm.org
Thu Feb 23 08:27:29 PST 2023


Author: Valentin Clement
Date: 2023-02-23T17:27:21+01:00
New Revision: e89a00db6d0596fe0263ca0a1052e5e6859744e9

URL: https://github.com/llvm/llvm-project/commit/e89a00db6d0596fe0263ca0a1052e5e6859744e9
DIFF: https://github.com/llvm/llvm-project/commit/e89a00db6d0596fe0263ca0a1052e5e6859744e9.diff

LOG: [flang] Fix element indexing in Destroy component deallocation

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

Reviewed By: klausler

Differential Revision: https://reviews.llvm.org/D144553

Added: 
    

Modified: 
    flang/runtime/derived.cpp

Removed: 
    


################################################################################
diff  --git a/flang/runtime/derived.cpp b/flang/runtime/derived.cpp
index 05c5b5264b9d4..5bfecdc8f070f 100644
--- a/flang/runtime/derived.cpp
+++ b/flang/runtime/derived.cpp
@@ -225,14 +225,18 @@ void Destroy(const Descriptor &descriptor, bool finalize,
   std::size_t myComponents{componentDesc.Elements()};
   std::size_t elements{descriptor.Elements()};
   std::size_t byteStride{descriptor.ElementBytes()};
+  SubscriptValue at[maxRank];
+  descriptor.GetLowerBounds(at);
   for (std::size_t k{0}; k < myComponents; ++k) {
     const auto &comp{
         *componentDesc.ZeroBasedIndexedElement<typeInfo::Component>(k)};
     if (comp.genre() == typeInfo::Component::Genre::Allocatable ||
         comp.genre() == typeInfo::Component::Genre::Automatic) {
       for (std::size_t j{0}; j < elements; ++j) {
-        descriptor.OffsetElement<Descriptor>(j * byteStride + comp.offset())
-            ->Deallocate();
+        Descriptor *d{reinterpret_cast<Descriptor *>(
+            descriptor.Element<char>(at) + comp.offset())};
+        d->Deallocate();
+        descriptor.IncrementSubscripts(at);
       }
     }
   }


        


More information about the flang-commits mailing list