[flang][hlfir] Add move semantics to hlfir.as_expr.
authorJean Perier <jperier@nvidia.com>
Tue, 17 Jan 2023 10:22:33 +0000 (11:22 +0100)
committerJean Perier <jperier@nvidia.com>
Tue, 17 Jan 2023 10:23:23 +0000 (11:23 +0100)
commitc373eeb1c5cc99cad338af083b106c27d2779a53
treef2e6758892b5bfb390c6958a443bcebef95d99de
parent777784d9cff46768cc30214152ba7dc8ff9de0a7
[flang][hlfir] Add move semantics to hlfir.as_expr.

hlfir.as_expr allows turning an array, character, or derived type
variable into a value when it the usage require an hlfir.expr (e.g,
when returning the element value inside and hlfir.elemental).

The default implementation of this operation in bufferization is to
make a copy of the variable into a temporary buffer.
This adds a time and memory overhead in cases where such copy is not
needed because the variable is already a temporary that was created
in lowering to compute the expression value, and the "as_expr" is
the sole usage of the variable.

This is for instance the case for many transformational intrinsics
that do not have hlfir.expr operation (at least for now, but some may
never benefit from having one) and must be implemented "on memory"
in lowering.

This patch adds a way to "move" the variable storage along its value.
It allows the bufferization to re-use the variable storage for the
hlfir.expr created by hlfir.as_expr, and in exchange, the
responsibility of deallocating the buffer (if the variable was heap
allocated) if passed along to the hlfir.expr, and will need to be
done after the last hlfir.expr usage.

Differential Revision: https://reviews.llvm.org/D141832
flang/include/flang/Optimizer/HLFIR/HLFIROps.td
flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
flang/test/HLFIR/as_expr-codegen.fir
flang/test/HLFIR/as_expr.fir