This is an archive of the discontinued LLVM Phabricator instance.

[flang] Fix lowering of array paths in elemental calls
ClosedPublic

Authored by luporl on Jun 21 2023, 11:52 AM.

Details

Summary

Elemental procedures may need their array arguments to be passed by
address. This is done by setting ArrayExprLowering::semant to a
value that corresponds to this semantics. Later, member functions
such as applyPathToArrayLoad() read this variable to generate FIR
instructions that match the needed behavior. The problem is that
the semant variable also affects how array paths are lowered. Thus,
if an index of the path is an array element, this will cause its
address to be used instead of its value, which usually results in a
segmentation fault at runtime.

Example: b(i:i) = elem_func(a(v(i):v(i)))

To fix this, ArrayExprLowering::nextPathSemant was added. When it's
set, the next array path is handled with the semantics specified by
it, while the elemental argument retains its original semantics.

Fixes https://github.com/llvm/llvm-project/issues/62981

Diff Detail

Event Timeline

luporl created this revision.Jun 21 2023, 11:52 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptJun 21 2023, 11:52 AM
luporl published this revision for review.Jun 21 2023, 11:57 AM
jeanPerier accepted this revision.Jun 22 2023, 11:59 PM

Looks good to me, thanks for the fix!
Note that this test is passing with HLFIR (flang-new -flang-experimental-hlfir) that is intended to simplify array expression lowering and fix issues there. I would not prioritize fix for bugs that are fixed with this option since we will transition to HLFIR by default as soon as we reach parity (we still have a few points that are behind like vector subscript support and optional argument in intrinsics).

This revision is now accepted and ready to land.Jun 22 2023, 11:59 PM

Looks good to me, thanks for the fix!
Note that this test is passing with HLFIR (flang-new -flang-experimental-hlfir) that is intended to simplify array expression lowering and fix issues there. I would not prioritize fix for bugs that are fixed with this option since we will transition to HLFIR by default as soon as we reach parity (we still have a few points that are behind like vector subscript support and optional argument in intrinsics).

Thanks for the review and for the information about HLFIR! I'll start to check and prioritize the issues that can also be reproduced with flang-new -flang-experimental-hlfir.

This revision was automatically updated to reflect the committed changes.