This is an archive of the discontinued LLVM Phabricator instance.

[flang] support fir.unreachable in stack arrays pass
ClosedPublic

Authored by tblah on Feb 13 2023, 7:55 AM.

Details

Summary

Some functions (e.g. the main function) end with a call to the STOP
statement instead of a func.return. This is lowered as a call to the
stop runtime function followed by a fir.unreachable. fir.unreachable is
a terminator and so this can cause functions to have no func.return.

The stack arrays pass looks to see which heap allocations have always
been freed by the time a function returns. Without any returns, the pass
does not detect any freed allocations. This patch changes this behaviour
so that fir.unreachable is checked as well as func.return.

This allows 15 heap allocations for array temporaries in spec2017
exchange2's main function to be moved to the stack.

Diff Detail

Event Timeline

tblah created this revision.Feb 13 2023, 7:55 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptFeb 13 2023, 7:55 AM
tblah requested review of this revision.Feb 13 2023, 7:55 AM
jeanPerier added inline comments.Feb 14 2023, 12:09 AM
flang/lib/Optimizer/Transforms/StackArrays.cpp
440–441

I was trying to think if an interface trait could be used instead of doing a walk for each operation that may terminate a function. Would the Terminator trait work or is it too broad ?

tblah added inline comments.Feb 14 2023, 2:28 AM
flang/lib/Optimizer/Transforms/StackArrays.cpp
440–441

I did try Terminator, but scf branch operations are also Terminators. Terminator means something that terminates a block, not a function. There is a "ReturnLike" trait, but fir.unreachable does not implement that.

jeanPerier accepted this revision.Feb 14 2023, 5:17 AM
jeanPerier added inline comments.
flang/lib/Optimizer/Transforms/StackArrays.cpp
440–441

Ok, too bad. I think it might be weird for fir.unreachable to implement "ReturnLike", so I am good with your patch.

This revision is now accepted and ready to land.Feb 14 2023, 5:17 AM
This revision was automatically updated to reflect the committed changes.