Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[flang][runtime] Finalize polymorphic components using dynamic type #67040

Merged
merged 2 commits into from
Sep 22, 2023

Conversation

jeanPerier
Copy link
Contributor

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.

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.
@llvmbot llvmbot added flang:runtime flang Flang issues not falling into any other category labels Sep 21, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 21, 2023

@llvm/pr-subscribers-flang-runtime

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/67040.diff

1 Files Affected:

  • (modified) flang/runtime/derived.cpp (+19-1)
diff --git a/flang/runtime/derived.cpp b/flang/runtime/derived.cpp
index 5224c1426479a28..55545b41da8d479 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 if 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()) {

Copy link
Contributor

@klausler klausler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great; thanks.

flang/runtime/derived.cpp Outdated Show resolved Hide resolved
@jeanPerier jeanPerier merged commit efd5cde into llvm:main Sep 22, 2023
2 checks passed
@jeanPerier jeanPerier deleted the jpr-polymorphic-component branch September 22, 2023 09:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:runtime flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants