[flang-commits] [flang] [flang][OpenACC] Release declare-create allocatable on deallocate (PR #220541)

via flang-commits flang-commits at lists.llvm.org
Wed Sep 2 08:54:58 PDT 2026


================
@@ -5334,6 +5336,12 @@ void Fortran::lower::attachDeclarePostDeallocAction(
   if (isUnifiedMemoryMode(converter))
     return;
 
+  // A module variable has no post-dealloc recipe: it would run once the
+  // descriptor no longer holds the address the mapping is keyed on.
+  if (sym.GetUltimate().owner().kind() ==
+      Fortran::semantics::Scope::Kind::Module)
+    return;
+
----------------
khaki3 wrote:

This PR only changes the global (module variable) path, `createDeclareDeallocFunc` ; the local path, `createDeclareDeallocFuncWithArg`, is already correct.

The guard is there because module variables don't use post-dealloc, and to avoid emitting empty hooks.

```fortran
module m
  real(8), allocatable :: g(:)
  !$acc declare create(g)
end module

subroutine s()
  real(8), allocatable :: l(:)
  !$acc declare create(l)
  allocate(l(4)); deallocate(l)
end subroutine
```

Local `l`, from `createDeclareDeallocFuncWithArg`, already emits a pre-dealloc recipe, so the data is released while the host allocation is still live; its post recipe then releases the descriptor, which is still valid storage at that point:

```mlir
func.func private @_QFsEl_acc_declare_pre_dealloc(%arg0: T) {
  %0 = acc.getdeviceptr varPtr(%arg0 : T) dataClause(acc_create)
         structured(false) name("l") -> T
  acc.declare_exit dataOperands(%0 : T)
  acc.delete accPtr(%0 : T) dataClause(acc_create) structured(false) name("l")
  return
}
func.func private @_QFsEl_acc_declare_post_dealloc(%arg0: T) {
  %0 = acc.getdeviceptr varPtr(%arg0 : T) dataClause(acc_create)
         structured(false) implicit(true) name("l") -> T
  acc.declare_exit dataOperands(%0 : T)
  return
}
```

Module `g`, from `createDeclareDeallocFunc`, had no pre recipe at all, which is what this PR adds. Its descriptor is a global owned by the destructor, so there is nothing left for a post recipe to release:

```mlir
func.func @_QMmEg_acc_declare_pre_dealloc() attributes {acc.declare_action} {
  %0 = fir.address_of(@_QMmEg) : T
  %1 = acc.getdeviceptr varPtr(%0 : T) dataClause(acc_create)
         structured(false) name("g") -> T
  acc.declare_exit dataOperands(%1 : T)
  return
}
acc.global_dtor @_QMmEg_acc_dtor {
  %0 = fir.address_of(@_QMmEg) {acc.declare = #acc.declare<dataClause = acc_create>} : T
  %1 = acc.getdeviceptr varPtr(%0 : T) dataClause(acc_create)
         structured(false) name("g") -> T
  acc.declare_exit dataOperands(%1 : T)
  acc.delete accPtr(%1 : T) dataClause(acc_create) structured(false) name("g")
  acc.terminator
}
```

https://github.com/llvm/llvm-project/pull/220541


More information about the flang-commits mailing list