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] Call finalization on empty type #66010

Merged
merged 1 commit into from
Sep 11, 2023

Conversation

clementval
Copy link
Contributor

According to 7.5.6.3 point 3, finalization occurs when

A nonpointer, nonallocatable object that is not a dummy argument or function result is finalized immediately before it would become undefined due to execution of a RETURN or END statement (19.6.6, item (3)).

We were not calling the finalization on empty derived-type. There is no such restriction so this patch updates the code so the finalization is called for empty type as well.

@clementval clementval requested a review from a team as a code owner September 11, 2023 21:15
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Sep 11, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 11, 2023

@llvm/pr-subscribers-flang-fir-hlfir

Changes

According to 7.5.6.3 point 3, finalization occurs when

A nonpointer, nonallocatable object that is not a dummy argument or function result is finalized immediately before it would become undefined due to execution of a RETURN or END statement (19.6.6, item (3)).

We were not calling the finalization on empty derived-type. There is no such restriction so this patch updates the code so the finalization is called for empty type as well.

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

2 Files Affected:

  • (modified) flang/lib/Lower/ConvertVariable.cpp (+1-1)
  • (modified) flang/test/Lower/derived-type-finalization.f90 (+16)
diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp
index 726b8489409ecb4..847ea468f447bc5 100644
--- a/flang/lib/Lower/ConvertVariable.cpp
+++ b/flang/lib/Lower/ConvertVariable.cpp
@@ -88,7 +88,7 @@ static bool hasDefaultInitialization(const Fortran::semantics::Symbol &sym) {
 
 // Does this variable have a finalization?
 static bool hasFinalization(const Fortran::semantics::Symbol &sym) {
-  if (sym.has() && sym.size())
+  if (sym.has())
     if (const Fortran::semantics::DeclTypeSpec *declTypeSpec = sym.GetType())
       if (const Fortran::semantics::DerivedTypeSpec *derivedTypeSpec =
               declTypeSpec->AsDerived())
diff --git a/flang/test/Lower/derived-type-finalization.f90 b/flang/test/Lower/derived-type-finalization.f90
index 4f223ab7511d19c..2194fd8d96451d0 100644
--- a/flang/test/Lower/derived-type-finalization.f90
+++ b/flang/test/Lower/derived-type-finalization.f90
@@ -23,6 +23,11 @@ module derived_type_finalization
     type(t2) :: t
   end type
 
+  type t4
+  contains
+    final :: t4_final
+  end type
+
 contains
 
   subroutine t1_final(this)
@@ -227,6 +232,17 @@ subroutine test_avoid_double_free()
 ! CHECK: %[[RES_CONV:.*]] = fir.convert %[[RES]] : (!fir.ref>>>>) -> !fir.box
 ! CHECK: %{{.*}} = fir.call @_FortranADestroy(%[[RES_CONV]]) {{.*}} : (!fir.box) -> none
 
+  subroutine t4_final(this)
+    type(t4) :: this
+  end subroutine
+
+  subroutine local_t4()
+    type(t4) :: t
+  end subroutine
+
+! CHECK-LABEL: func.func @_QMderived_type_finalizationPlocal_t4()
+! CHECK: %{{.*}} = fir.call @_FortranADestroy(%2) fastmath : (!fir.box) -> none
+
 end module
 
 program p

Copy link
Contributor

@vzakhari vzakhari left a comment

Choose a reason for hiding this comment

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

Looks good! Thank you for the prompt fix!

@clementval clementval merged commit 973ca4e into llvm:main Sep 11, 2023
3 of 4 checks passed
@clementval clementval deleted the empty_type_finalization branch September 11, 2023 21:34
AntonRydahl pushed a commit to AntonRydahl/llvm-project that referenced this pull request Sep 11, 2023
According to 7.5.6.3 point 3, finalization occurs when

> A nonpointer, nonallocatable object that is not a dummy argument or
function result is finalized immediately before it would become
undefined due to execution of a RETURN or END statement (19.6.6, item
(3)).

We were not calling the finalization on empty derived-type. There is no
such restriction so this patch updates the code so the finalization is
called for empty type as well.
ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Sep 19, 2023
According to 7.5.6.3 point 3, finalization occurs when

> A nonpointer, nonallocatable object that is not a dummy argument or
function result is finalized immediately before it would become
undefined due to execution of a RETURN or END statement (19.6.6, item
(3)).

We were not calling the finalization on empty derived-type. There is no
such restriction so this patch updates the code so the finalization is
called for empty type as well.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir 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