[flang-commits] [flang] efd5cde - [flang][runtime] Finalize polymorphic components using dynamic type (#67040)

via flang-commits flang-commits at lists.llvm.org
Fri Sep 22 02:22:53 PDT 2023


Author: jeanPerier
Date: 2023-09-22T11:22:48+02:00
New Revision: efd5cdeea238fc0f3e5b945fe3a5b46fca8ee066

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

LOG: [flang][runtime] Finalize polymorphic components using dynamic type (#67040)

Previous code was finalizing polymorphic components according to static
type (calling the static type final routine, if any).

There is no way (I think) to know from a
Fortran::runtime::typeInfo::Component if an allocatable component is
polymorphic or not. So this patch just always uses the dynamic type
descriptor to check for derived type allocatable component finalization.

Added: 
    

Modified: 
    flang/runtime/derived.cpp

Removed: 
    


################################################################################
diff  --git a/flang/runtime/derived.cpp b/flang/runtime/derived.cpp
index 5224c1426479a28..5edc47c4a948918 100644
--- a/flang/runtime/derived.cpp
+++ b/flang/runtime/derived.cpp
@@ -209,7 +209,25 @@ void Finalize(const Descriptor &descriptor,
        k < myComponents; ++k) {
     const auto &comp{
         *componentDesc.ZeroBasedIndexedElement<typeInfo::Component>(k)};
-    if (comp.genre() == typeInfo::Component::Genre::Allocatable ||
+    if (comp.genre() == typeInfo::Component::Genre::Allocatable &&
+        comp.category() == TypeCategory::Derived) {
+      // Component may be polymorphic or unlimited polymorphic. Need to use the
+      // dynamic type to check whether finalization is needed.
+      for (std::size_t j{0}; j < elements; ++j) {
+        const Descriptor &compDesc{*descriptor.OffsetElement<Descriptor>(
+            j * byteStride + comp.offset())};
+        if (compDesc.IsAllocated()) {
+          if (const DescriptorAddendum * addendum{compDesc.Addendum()}) {
+            if (const typeInfo::DerivedType *
+                compDynamicType{addendum->derivedType()}) {
+              if (!compDynamicType->noFinalizationNeeded()) {
+                Finalize(compDesc, *compDynamicType, terminator);
+              }
+            }
+          }
+        }
+      }
+    } else if (comp.genre() == typeInfo::Component::Genre::Allocatable ||
         comp.genre() == typeInfo::Component::Genre::Automatic) {
       if (const typeInfo::DerivedType * compType{comp.derivedType()}) {
         if (!compType->noFinalizationNeeded()) {


        


More information about the flang-commits mailing list