[flang-commits] [flang] [flang][hlfir] Fixed some finalization/deallocation issues. (PR #67047)
Slava Zakharin via flang-commits
flang-commits at lists.llvm.org
Fri Sep 22 09:25:35 PDT 2023
================
@@ -439,8 +440,20 @@ static bool allOtherUsesAreSafeForAssociate(mlir::Value value,
value.getParentRegion() != endAssociate->getParentRegion()))
return false;
- for (mlir::Operation *useOp : value.getUsers())
- if (!mlir::isa<hlfir::DestroyOp>(useOp) && useOp != currentUse) {
+ for (mlir::Operation *useOp : value.getUsers()) {
+ // Ignore DestroyOp's that do not imply finalization.
+ // If finalization is implied, then we must delegate
+ // the finalization to the correspoding EndAssociateOp,
+ // but we currently do not; so we disable the buffer
+ // reuse in this case.
+ if (auto destroy = mlir::dyn_cast<hlfir::DestroyOp>(useOp)) {
+ if (destroy.mustFinalizeExpr())
+ return false;
----------------
vzakhari wrote:
I agree! I think we will need `finalize` attribute for `hlfir.end_associate` to communicate the "theft" of the buffer that needs finalization.
I also agree that we have to be careful about stealing the buffer from the elemental op, when the buffer might be modified inside the association scope, e.g.:
```
module types
type t
integer :: i
contains
final :: finalize
end type t
contains
subroutine finalize(x)
type(t), intent(inout) :: x
print *, x%i
end subroutine finalize
end module types
subroutine test(x)
use types
type(t) :: x(10)
call callee(elem(x))
contains
subroutine callee(x)
type(t), value :: x(10)
x%i = 10
end subroutine callee
impure elemental function elem(x)
type(t), intent(in) :: x
type(t) :: elem
elem = x
end function elem
end subroutine test
```
Since the `callee` might modify the buffer, the modification can be made visible via an impure finalization routine. I guess it may only be the case for impure elemental calls, so we can probably rely on the `unordered` attribute to decide if it is safe to steal the buffer.
https://github.com/llvm/llvm-project/pull/67047
More information about the flang-commits
mailing list