[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


================
@@ -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
----------------
bonachea wrote:

I certainly have no objection to eventually representing any available information about the cobounds in the MLIR. However as you imply, that might not be necessary until there is PR adding a consumer of that information to the compiler.

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


More information about the flang-commits mailing list