[flang-commits] [flang] [llvm] [Flang] Adding lowering for the allocation and deallocation of coarrays (PR #182110)

Dan Bonachea via flang-commits flang-commits at lists.llvm.org
Thu Mar 5 21:01:45 PST 2026


https://github.com/bonachea requested changes to this pull request.

Continuing this discussion of coarray finalization (which I'll again emphasize is _outside_ the scope of this PR):

> wonder if it would not be best to call the PRIF runtime via the Fortran runtime to deal with the allocation/deallocation and leave the Fortran runtime in charge of the rest. But for that the Fortran runtime needs to know it is dealing with coarrays.
> ...
>  the Fortran runtime is in C/C++ and what PRIF offers is in Fortran (non-interoperable), which will cause problems when trying to match the two together

As explained in my comments, the plan is for this to be handled through the `final_func` callback (which has a `bind(C)` interface, so interoperability is not a concern there).

Below is the envisioned coarray life cycle, for the most general case of a derived type that is finalizable and/or has finalizable potential subobject components and/or has ALLOCATABLE ultimate components (again, _outside_ the scope of the current PR):

* **Coarray Allocation** (via `ALLOCATE` or program initiation)
  * Compiler obtains a "cleanup" callback procedure pointer
     * this could be a fixed cleanup procedure in flang-rt, 
     * or possibly a cleanup procedure generated on-the-fly for this specific coarray type and/or allocation site
  * Allocate the coarray storage, codegen calls:
     * `prif_allocate_coarray(..., final_func=cleanup, coarray_handle)`
  * Compiler arranges to save any metadata needed for later finalization
     * this could be stored in some "hidden" metadata before or after the user elements in the coarray storage
     * or it could live in a separate heap object, storing away the pointer using `prif_set_context_data(coarray_handle)`
  * Compiler arranges to initialize the coarray element storage (e.g. from SOURCE=)
 
* **Coarray Deallocation** (via `DEALLOCATE  -> prif_deallocate` or `END TEAM  -> prif_end_team`)
  * PRIF library performs appropriate image synchronization
  * PRIF library calls `cleanup(coarray_handle)` invoking the `final_func` callback provided at coarray allocation time
  * `cleanup` callback optionally calls `prif_local_data_pointer(coarray_handle)` to retrieve the base address of the coarray storage
  * `cleanup` callback optionally calls `prif_size_bytes(coarray_handle)` to retrieve the size of the coarray storage
  * `cleanup` callback optionally calls `prif_get_context_data(coarray_handle)` to retrieve finalization metadata stored at allocation time
  * `cleanup` traverses the elements and components of the coarray user data, invoking final subroutines for any finalizable object (following the rules in F23 7.5.6.2) and deallocating ALLOCATABLE components
  * `cleanup` returns to the PRIF library
  * PRIF library optionally performs image synchronization, reclaims the coarray storage and returns to the caller


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


More information about the flang-commits mailing list