This is an archive of the discontinued LLVM Phabricator instance.

[flang] Do not lose call in shape inquiry on function reference
ClosedPublic

Authored by jeanPerier on Jan 10 2022, 5:38 AM.

Details

Summary

Currently, something like print *, size(foo(n,m)) was rewritten
to print *, size(foo_result_symbol) when foo result is a non constant
shape array. This cannot be processed by lowering or reprocessed by a
Fortran compiler since the syntax is wrong (foo_result_symbol is
unknown on the caller side) and the arguments are lost when they might
be required to compute the result shape.

It is not possible (and probably not desired) to make GetShape fail in
general in such case since returning nullopt seems only expected for
scalars or assumed rank (see GetRank usage in lib/Semantics/check-call.cpp),
and returning a vector with nullopt extent may trigger some checks to
believe they are facing an assumed size (like here in intrinsic argument
checks: https://github.com/llvm/llvm-project/blob/196204c72c68a577c72af95d70f18e3550939a5e/flang/lib/Evaluate/intrinsics.cpp#L1530).

Hence, I went for a solution that limits the rewrite change to folding
(where the original expression is returned if the shape depends on a non
constant shape from a call).

I added a non default option to GetShapeHelper that prevents the rewrite
of shape inquiry on calls to descriptor inquiries. At first I wanted to
avoid touching GetShapeHelper, but it would require to re-implement all
its logic to determine if the shape comes from a function call or not
(the expression could be size(1+foo(n,m))). So added an alternate
entry point to GetShapeHelper seemed the cleanest solution to me.

Diff Detail

Event Timeline

jeanPerier created this revision.Jan 10 2022, 5:38 AM
jeanPerier requested review of this revision.Jan 10 2022, 5:38 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 10 2022, 5:38 AM
klausler accepted this revision.Jan 10 2022, 8:56 AM

LGTM and thanks!

flang/include/flang/Evaluate/shape.h
248

Comment would be better as "GetShape() variant that does not..."

This revision is now accepted and ready to land.Jan 10 2022, 8:56 AM