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

via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 3 06:55:13 PST 2026


================
@@ -257,6 +258,135 @@ void Fortran::lower::genFormTeamStatement(
                           errMsgAddr);
 }
 
+//===----------------------------------------------------------------------===//
+// COARRAY utils
+//===----------------------------------------------------------------------===//
+
+mlir::DenseI64ArrayAttr
+Fortran::lower::genLowerCoBounds(Fortran::lower::AbstractConverter &converter,
+                                 mlir::Location loc,
+                                 const Fortran::semantics::Symbol &sym) {
+  fir::FirOpBuilder &builder = converter.getFirOpBuilder();
+  mlir::DenseI64ArrayAttr lcobounds;
+
+  if (Fortran::semantics::IsAllocatableOrObjectPointer(&sym))
+    return {};
+  if (const auto *object =
+          sym.GetUltimate()
+              .detailsIf<Fortran::semantics::ObjectEntityDetails>()) {
+    llvm::SmallVector<std::int64_t> lcbs;
+    for (const Fortran::semantics::ShapeSpec &cobounds : object->coshape()) {
+      if (auto lb = cobounds.lbound().GetExplicit()) {
+        if (auto constant = Fortran::evaluate::ToInt64(*lb))
+          lcbs.push_back(*constant);
+        else
+          lcbs.push_back(1); // default lcobounds
+      }
+    }
+    lcobounds = mlir::DenseI64ArrayAttr::get(builder.getContext(), lcbs);
+  }
+  return lcobounds;
+}
+
+mlir::DenseI64ArrayAttr
+Fortran::lower::genUpperCoBounds(Fortran::lower::AbstractConverter &converter,
+                                 mlir::Location loc,
+                                 const Fortran::semantics::Symbol &sym) {
+  fir::FirOpBuilder &builder = converter.getFirOpBuilder();
+  mlir::DenseI64ArrayAttr ucobounds;
+
+  if (Fortran::semantics::IsAllocatableOrObjectPointer(&sym))
+    return {};
+  if (const auto *object =
+          sym.GetUltimate()
+              .detailsIf<Fortran::semantics::ObjectEntityDetails>()) {
+    llvm::SmallVector<std::int64_t> ucbs;
+    for (const Fortran::semantics::ShapeSpec &cobounds : object->coshape()) {
+      if (cobounds.ubound().isStar()) {
+        ucbs.push_back(-1);
+      } else if (auto ub = cobounds.ubound().GetExplicit()) {
+        if (auto constant = Fortran::evaluate::ToInt64(*ub))
+          ucbs.push_back(*constant);
+        else {
+          if (auto lb = cobounds.lbound().GetExplicit()) {
+            if (auto constant2 = Fortran::evaluate::ToInt64(*lb))
+              ucbs.push_back(*constant2);
+            else
+              ucbs.push_back(1); // use lcobound as default value
----------------
jeanPerier wrote:

cobounds do not have to be constant value, how will this deal with cases like:

```
subroutine test(n, co)
 integer :: n
 real :: co[n, *]
 n = n +1
 print *, ucobound(co)
end
```

In the contexts of local coarrays that must be SAVE, which seems to be the need for this helper currently, this is OK, but the limitation should be clearly documented and the function named something else. I would also advise not exposing it as an API if it is not needed outside of `genAllocateCoarray`.

I expect the other usage will need to retrieve the cobounds for all kind of coarray variables (duummy, allocatables).


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


More information about the llvm-commits mailing list