[all-commits] [llvm/llvm-project] ab1db2: [flang][hlfir] Fixed some finalization/deallocatio...

Slava Zakharin via All-commits all-commits at lists.llvm.org
Fri Sep 22 10:48:06 PDT 2023

  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: ab1db262724ecbb87a3957a60faaca6974ea731f
  Author: Slava Zakharin <szakharin at nvidia.com>
  Date:   2023-09-22 (Fri, 22 Sep 2023)

  Changed paths:
    M flang/include/flang/Lower/ConvertCall.h
    M flang/include/flang/Optimizer/Builder/HLFIRTools.h
    M flang/include/flang/Optimizer/Builder/Runtime/Derived.h
    M flang/include/flang/Optimizer/HLFIR/HLFIRDialect.h
    M flang/include/flang/Optimizer/HLFIR/HLFIROps.td
    M flang/include/flang/Runtime/derived-api.h
    M flang/lib/Lower/ConvertCall.cpp
    M flang/lib/Lower/HlfirIntrinsics.cpp
    M flang/lib/Optimizer/Builder/HLFIRTools.cpp
    M flang/lib/Optimizer/Builder/Runtime/Derived.cpp
    M flang/lib/Optimizer/HLFIR/IR/HLFIRDialect.cpp
    M flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp
    M flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
    M flang/lib/Optimizer/HLFIR/Transforms/InlineElementals.cpp
    M flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp
    M flang/runtime/derived-api.cpp
    M flang/test/HLFIR/associate-codegen.fir
    A flang/test/HLFIR/bufferize-destroy-for-derived.fir
    A flang/test/HLFIR/bufferize-end-associate-for-derived.fir
    M flang/test/HLFIR/bufferize-poly-expr.fir
    M flang/test/HLFIR/destroy.fir
    M flang/test/HLFIR/elemental-codegen-nested.fir
    M flang/test/HLFIR/inline-elemental.fir
    M flang/test/HLFIR/invalid.fir
    M flang/test/HLFIR/mul_transpose.f90
    A flang/test/Lower/HLFIR/associate-for-args-with-alloc-components.f90
    A flang/test/Lower/HLFIR/elemental-call-with-finalization.f90
    M flang/test/Lower/HLFIR/poly_expr_for_nonpoly_dummy.f90
    M flang/test/Lower/HLFIR/polymorphic-expressions.f90

  Log Message:
  [flang][hlfir] Fixed some finalization/deallocation issues. (#67047)

This set of commits resolves some of the issues with elemental calls producing
results that may require finalization, and also some memory leak issues due to
the missing deallocation of allocatable components of the temporary buffers
created by the bufferization pass.

- [flang][runtime] Expose Finalize API for derived types.

- [flang][hlfir] Add 'finalize' attribute for DestroyOp.

- [flang][hlfir] Postpone result finalization for elemental calls.

    The results of elemental calls generated inside hlfir.elemental must not
    be finalized/destructed before they are copied into the resulting
    array. The finalization must be done on the array as a whole
    (e.g. there might be different scalar and array finalization routines).
    The finalization work is left to the hlfir.destroy corresponding
    to this hlfir.elemental.

- [flang][hlfir] Tighten requirements on hlfir.end_associate operand.

    If component deallocation might be required for the operand of
    hlfir.end_associate, we have to be able to get the variable
    shape/params to create a descriptor for calling the runtime.
    This commit adds verification that we can do so.

- [flang][hlfir] Lower argument clean-ups using valid hlfir.end_associate.

    The operand must be a Fortran entity, when allocatable component
    deallocation may be required.

- [flang][hlfir] Properly clean-up temporary buffers in bufferization pass.

    This commit combines changes for proper finalization and component
    deallocation of the temporary buffers. The finalization part
    relates to hlfir.destroy operations with 'finalize' attribute.
    The component deallocation might be invoked for both hlfir.destroy
    and hlfir.end_associate, if the operand is of a derived type
    with allocatable component(s).

The changes are mostly in one function, so I decided not to split them.

- [flang][hlfir] Disable optimizations for hlfir.elemental requiring finalization.

    If hlfir.elemental is coupled with hlfir.destroy with 'finalize' attribute,
    the temporary array result of hlfir.elemental needs to be created
    for the purpose of finalization. We cannot do certain optimizations
    on such hlfir.elemental operations.

    I was not able to come up with a test for the OptimizedBufferization pass,
    but I put the check there as well.

More information about the All-commits mailing list