[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
================
@@ -425,4 +426,59 @@ def mif_TeamNumberOp : mif_Op<"team_number", []> {
}];
}
+//===----------------------------------------------------------------------===//
+// Allocation and Deallocation
+//===----------------------------------------------------------------------===//
+
+def mif_AllocCoarrayOp
+ : mif_Op<"alloc_coarray", [AttrSizedOperandSegments,
+ MemoryEffects<[MemAlloc<DefaultResource>]>]> {
+ let summary = "Perform the allocation of a coarray and provide a "
+ "corresponding coarray descriptor";
+
+ let description = [{
+ This operation allocates a coarray and provides the corresponding
+ coarray descriptor. This call is collective over the current team.
+ }];
+
+ let arguments = (ins StrAttr:$uniq_name,
+ Arg<fir_ReferenceType, "", [MemRead, MemWrite]>:$box,
+ DenseI64ArrayAttr:$lcobounds, DenseI64ArrayAttr:$ucobounds,
+ Arg<Optional<AnyReferenceLike>, "", [MemWrite]>:$stat,
+ Arg<Optional<AnyRefOrBoxType>, "", [MemWrite]>:$errmsg);
+
+ let builders = [OpBuilder<(ins "mlir::Value":$box, "llvm::StringRef":$symName,
+ "mlir::DenseI64ArrayAttr":$lcobounds,
+ "mlir::DenseI64ArrayAttr":$ucobounds, "mlir::Value":$stat,
+ "mlir::Value":$errmsg)>,
+ OpBuilder<(ins "mlir::Value":$box, "llvm::StringRef":$symName,
+ "mlir::DenseI64ArrayAttr":$lcobounds,
+ "mlir::DenseI64ArrayAttr":$ucobounds)>];
+
+ let hasVerifier = 1;
+ let assemblyFormat = [{
+ $box
+ (`stat` $stat^ )?
+ (`errmsg` $errmsg^ )?
+ attr-dict `:` functional-type(operands, results)
+ }];
+}
+
+def mif_DeallocCoarrayOp
+ : mif_Op<"dealloc_coarray", [AttrSizedOperandSegments,
+ MemoryEffects<[MemFree<DefaultResource>]>]> {
+ let summary = "Perform the deallocation of a coarray";
+ let description = [{
+ This call releases memory allocated by `mif_AllocCoarrayOp` for a coarray.
----------------
bonachea wrote:
@jeanPerier despite the complexity of finalization, it all _begins_ with one call (e.g. an invocation of DEALLOCATE) that kicks it all off. The `final_func` callback is _only_ serving that purpose: notifying the compiler runtime that a deallocation has begun for a given coarray, regardless of whether it was triggered by `prif_deallocate_coarray` or `prif_end_team`.
All the tricky details of traversing the elements and components of the coarray to perform component finalization and deallocation are left to the compiler/flang-rt, and should be mostly unchanged from the non-coarray case (except possibly handling the entry from callback).
Any runtime type information and flags needed by the finalization logic can be passed through to the `final_func` callback using the coarray context data. After allocation, the compiler/flang-rt can use `prif_set_context_data` to save a pointer to a heap struct containing arbitrary information needed for finalization, and the callback retrieves that from `prif_get_context_data`. Alternatively, the compiler could choose to pad out the coarray storage allocation with additional space to hold finalization metadata (i.e. before or after the element data). Note that a dynamic-driven scheme of this flavor is basically mandatory to correctly handle the END TEAM case in general (where the set of coarrays being deallocated cannot be statically known); this is fundamental complexity arising from the Fortran standard, not PRIF.
Of course in the simple/common/fast case where no cleanup work is needed (e.g. coarray of intrinsic type, or coarray of derived type with no finalizable or allocatable components), then none of this is needed and `final_func` can just be passed a null pointer to disable the callback. That's exactly what the current PR is doing, and is sufficient to cover an important fraction of real multi-image codes.
https://github.com/llvm/llvm-project/pull/182110
More information about the flang-commits
mailing list