This is an archive of the discontinued LLVM Phabricator instance.

[flang] Avoid double finalization when intrinsic assignment is done in the runtime
ClosedPublic

Authored by clementval on Feb 2 2023, 7:31 AM.

Details

Summary

genRecordAssignment is emitting code to call Assign in the runtime for some cases.
In these cases, the finalization is done by the runtime so we do not need to do it in
a separate cal to avoid multiple finalization..
Also refactor the code in Bridge so the actual finalization of allocatable
is done before any reallocation. We might need to push this into ReallocIfNeeded.
It is not clear if the allocatable lhs needs to be finalized in any cases or only if it is
reallocated.

Diff Detail

Event Timeline

clementval created this revision.Feb 2 2023, 7:31 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald Transcript
clementval requested review of this revision.Feb 2 2023, 7:31 AM
clementval edited the summary of this revision. (Show Details)Feb 2 2023, 7:35 AM
clementval edited the summary of this revision. (Show Details)Feb 2 2023, 7:38 AM
clementval edited the summary of this revision. (Show Details)

Add tests for func return

jeanPerier accepted this revision.Feb 3 2023, 1:00 AM

Other than my question, LGTM.

flang/lib/Lower/Bridge.cpp
2821

Why ruling out the left hand side function result case?

This revision is now accepted and ready to land.Feb 3 2023, 1:00 AM
clementval added inline comments.Feb 3 2023, 1:11 AM
flang/lib/Lower/Bridge.cpp
2821

I guess this is debatable.

In this case you would have the f finalized

type :: t
  integer :: a
contains
 final :: finalize
end type
type(t) function f(n)
 integer, intent(in) :: n
 f = t(n)
end function

and if you write it like that you don't obviously

type(t) function f(n)
 integer, intent(in) :: n
 f%a = n
end function

Is the func result part of 7.5.6.3 point 1. I guess since there is mention of func result we should treat it as any other variable. What do you think?

jeanPerier added inline comments.Feb 3 2023, 1:23 AM
flang/lib/Lower/Bridge.cpp
2821

Thanks for the explanation. Reading at the standard, I would expect f to be finalized before the assignment. It fits in the point 1 (it's a normal variable that can be read/written several times inside the function).

I think this would happen anyway if t is a type with allocatable components where we always use genAssign to deal with the assignment. Then the finalizer would be called in genAssign.

clementval updated this revision to Diff 494550.Feb 3 2023, 1:45 AM

Keep finalizing function result

flang/lib/Lower/Bridge.cpp
2821

Let's keep finalization for function result. We can revise this in case it should not.