This is an archive of the discontinued LLVM Phabricator instance.

[flang][hlfir] Do not reuse hlfir.expr mask when saving RHS.
ClosedPublic

Authored by jeanPerier on Jun 27 2023, 8:00 AM.

Details

Summary

In WHERE and masked FORALL assignment, both the mask and the
RHS may need to be saved in some temporary storage before evaluating
the assignment.

The code was trying to "optimize" that case when evaluating the RHS
by not fetching the mask temporary that was just created, but in simple
cases of WHERE construct where the evaluated mask is an hlfir.expr,
this caused the hlfir.expr to be both used in an hlfir.associate and
later in an hlfir.apply to create the fir.if to mask the RHS evaluation.
This double usage prevents codegen from inlining the hlfir.expr at the
hlfir.apply, and from "moving" the hlfir.expr storage into the temp
during hlfir.associate bufferization. So this is pessimizing the code:
this would lead to created two mask array temporary storages

This was caught by the unexpectedly high number of "not yet implemented:
hlfir.associate of hlfir.expr with more than one use" that were firing.

Use the mask temporary instead (the hlfir.associate result) when possible.
Some temporary (the "inlined stack") do not support fetching and pushing
in the same run (a single counter is used to keep track of the fetching
and pushing position). Add a canBeFetchedAfterPush() for safety,
but this limitation is anyway not relevant for hlfir.expr since the
inlined stack is only used to save "trivial" scalars.

Also update the temporary storage name to only indicate "forall" if
the top level construct is a FORALL. This is not a very precise name,
but it should at least give a correct context to indicate in the IR
why some temporary array storage was created.

Diff Detail

Event Timeline

jeanPerier created this revision.Jun 27 2023, 8:00 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 27 2023, 8:00 AM
jeanPerier requested review of this revision.Jun 27 2023, 8:00 AM
vzakhari accepted this revision.Jun 27 2023, 10:48 AM

Looks good to me. Thank you!

This revision is now accepted and ready to land.Jun 27 2023, 10:48 AM
tblah accepted this revision.Jun 27 2023, 10:51 AM

LGTM, thanks!