This is an archive of the discontinued LLVM Phabricator instance.

[flang] Make sure derived-type finalization is done before return
ClosedPublic

Authored by clementval on Feb 1 2023, 1:45 AM.

Details

Summary

Finalization needs to be done before the terminator. In case
of end program, this was done after it and trigger a verifier error.
This patch fixes this case.

Diff Detail

Event Timeline

clementval created this revision.Feb 1 2023, 1:45 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptFeb 1 2023, 1:45 AM
clementval requested review of this revision.Feb 1 2023, 1:45 AM

Would it also work to make genFIR(const Fortran::parser::EndProgramStmt &) { genExitRoutine(); } a no-op and centralize dealing with exits in endNewFunction ?
That way, it seems it would not be needed to do any special case (EndSubroutineStmt and the others are all nops I think).

BTW, there is a genExitRoutine call in case of early returns (Fortran::parser::ReturnStmt) here: https://github.com/llvm/llvm-project/blob/main/flang/lib/Lower/Bridge.cpp#L3062
Does it fits with finalizations ?

jeanPerier accepted this revision.Feb 1 2023, 5:18 AM

Approving this fix for now since this is fixing regressions, my suggestion can be revisited/tested in another patch.

This revision is now accepted and ready to land.Feb 1 2023, 5:18 AM

Would it also work to make genFIR(const Fortran::parser::EndProgramStmt &) { genExitRoutine(); } a no-op and centralize dealing with exits in endNewFunction ?
That way, it seems it would not be needed to do any special case (EndSubroutineStmt and the others are all nops I think).

Yes it works and make things simpler. I have made a new diff here D143065.

BTW, there is a genExitRoutine call in case of early returns (Fortran::parser::ReturnStmt) here: https://github.com/llvm/llvm-project/blob/main/flang/lib/Lower/Bridge.cpp#L3062
Does it fits with finalizations ?

We need a bridge.fctCtx().finalizeAndKeep(); before generating the return. It's added to the same patch