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

via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 4 06:58:22 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:

Outside of allocatables where I understand there is some need to keep track of the cobounds somewhere, I think the compiler should know the cobounds of coarrays based on the evaluation of the specification expressions.
I am not saying there is no point for the runtime to get that info, just that the compiler should also keep track of it during lowering, which probably requires an extension of the fir::ExtendedValue/fir.declare.

I am OK with this being outside of the scope of this PR. If this stays as an exposed API, please add some TODO() here for dummy or non constant cobounds so that when someone implements the dummy coarray part, this is does not risk being used without update.

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


More information about the llvm-commits mailing list