[flang-commits] [flang] [flang] Fix incorrect reallocation for host-associated main-program coarrays #231233 (PR #233361)

via flang-commits flang-commits at lists.llvm.org
Fri Oct 23 12:49:58 PDT 2026


================
@@ -3187,7 +3187,17 @@ void Fortran::lower::mapSymbolAttributes(
     }
   }
 
-  if (Fortran::evaluate::IsCoarray(sym)) {
+  // A non-allocatable coarray is lowered as a global.
+  // Internal procedures that access it through host association instantiate
+  // the same global, but must not allocate it again and must be executed
+  // exactly once by every image, in the scope that owns the coarray.
+  bool isHostAssociated = false;
+  if (const Fortran::lower::pft::FunctionLikeUnit *funit =
+          converter.getCurrentFunctionUnit()) {
+    const Fortran::semantics::Scope &curScope = funit->getScope();
+    isHostAssociated = Fortran::semantics::IsHostAssociated(sym, curScope);
+  }
+  if (Fortran::evaluate::IsCoarray(sym) && !isHostAssociated) {
----------------
MattPD wrote:

The added `!isHostAssociated` condition also suppresses allocation for a module coarray referenced by its module procedure. Module declaration lowering only defines the global, so the condition removes its startup allocation and coarray-handle definition.

Saving the following as `repro.f90` and running `flang -fcoarray -S -emit-llvm repro.f90 -o /dev/null` succeeds on the parent but aborts on this head with an undefined coarray-handle reference. Could the host-association exclusion preserve startup allocation for module coarrays?

```fortran
module m
  integer, private :: c[*]
contains
  integer function ub(d)
    integer, intent(in) :: d
    ub = ucobound(c, dim=d)
  end function
end module
```

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


More information about the flang-commits mailing list