[llvm-branch-commits] [flang] release/23.x: [Flang] Coarray allocation, update error for pointer component #193829 (#194651) (PR #214807)

Douglas Yung via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Sat Aug 8 22:55:39 PDT 2026


https://github.com/dyung updated https://github.com/llvm/llvm-project/pull/214807

>From d8145e71418fb1e0a936adfb07dc5317113fc3b6 Mon Sep 17 00:00:00 2001
From: Jean-Didier PAILLEUX <jean-didier.pailleux at sipearl.com>
Date: Fri, 7 Aug 2026 08:06:06 +0200
Subject: [PATCH] [Flang] Coarray allocation, update error for pointer
 component #193829 (#194651)

The TODO message in `verify()` was not completly displayed, so it has
been replaced by `emitErrorOp`.
In addition, the test in `ConvertVariable` has been updated by adding
pointer direct component case.
Fix #193829

(cherry picked from commit 9adc2537177d0813301b3eab1bae906735c984a4)
---
 flang/include/flang/Semantics/tools.h        |  2 ++
 flang/lib/Lower/ConvertVariable.cpp          | 18 +++++++++++++++---
 flang/lib/Optimizer/Dialect/MIF/MIFOps.cpp   |  6 +++---
 flang/lib/Semantics/tools.cpp                |  5 +++++
 flang/test/Lower/MIF/coarray_allocation3.f90 |  3 ++-
 5 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/flang/include/flang/Semantics/tools.h b/flang/include/flang/Semantics/tools.h
index b54204c8328d5..1b4e3d27f28f1 100644
--- a/flang/include/flang/Semantics/tools.h
+++ b/flang/include/flang/Semantics/tools.h
@@ -195,6 +195,8 @@ const Symbol *HasImpureFinal(
 bool MayRequireFinalization(const DerivedTypeSpec &);
 // Does this type have an allocatable direct component?
 bool HasAllocatableDirectComponent(const DerivedTypeSpec &);
+// Does this type have a pointer direct component?
+bool HasPointerDirectComponent(const DerivedTypeSpec &);
 // Does this type have any defined assignment at any level (or any polymorphic
 // allocatable)?
 bool MayHaveDefinedAssignment(const DerivedTypeSpec &);
diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp
index f971feb0f01df..d4ffb398194dc 100644
--- a/flang/lib/Lower/ConvertVariable.cpp
+++ b/flang/lib/Lower/ConvertVariable.cpp
@@ -119,6 +119,15 @@ hasAllocatableDirectComponent(const Fortran::semantics::Symbol &sym) {
             *derivedTypeSpec);
   return false;
 }
+// Does this variable have a pointer  direct component?
+static bool hasPointerDirectComponent(const Fortran::semantics::Symbol &sym) {
+  if (sym.has<Fortran::semantics::ObjectEntityDetails>())
+    if (const Fortran::semantics::DeclTypeSpec *declTypeSpec = sym.GetType())
+      if (const Fortran::semantics::DerivedTypeSpec *derivedTypeSpec =
+              declTypeSpec->AsDerived())
+        return Fortran::semantics::HasPointerDirectComponent(*derivedTypeSpec);
+  return false;
+}
 //===----------------------------------------------------------------===//
 // Global variables instantiation (not for alias and common)
 //===----------------------------------------------------------------===//
@@ -699,9 +708,12 @@ static void instantiateGlobal(Fortran::lower::AbstractConverter &converter,
   fir::GlobalOp global;
 
   if (Fortran::evaluate::IsCoarray(sym)) {
-    if (hasFinalization(sym) || hasAllocatableDirectComponent(sym))
-      TODO(loc, "coarray: coarray with an allocatable direct component and/or "
-                "requiring finalization");
+    if (hasFinalization(sym) || hasAllocatableDirectComponent(sym) ||
+        hasPointerDirectComponent(sym))
+      TODO(
+          loc,
+          "coarray: coarray with a pointer/allocatable direct component and/or "
+          "requiring finalization.");
     const auto *details =
         sym.detailsIf<Fortran::semantics::ObjectEntityDetails>();
     if (details && details->init())
diff --git a/flang/lib/Optimizer/Dialect/MIF/MIFOps.cpp b/flang/lib/Optimizer/Dialect/MIF/MIFOps.cpp
index 2bafc33450392..223f7f44205f5 100644
--- a/flang/lib/Optimizer/Dialect/MIF/MIFOps.cpp
+++ b/flang/lib/Optimizer/Dialect/MIF/MIFOps.cpp
@@ -267,9 +267,9 @@ void mif::AllocCoarrayOp::build(mlir::OpBuilder &builder,
 
 llvm::LogicalResult mif::AllocCoarrayOp::verify() {
   if (hasAllocatableOrPointerComponent(getBox().getType()))
-    TODO(getLoc(),
-         "Derived type coarray with at least one ALLOCATABLE or POINTER "
-         "component");
+    return emitOpError(
+        "not implemented: Derived type coarray with at least one ALLOCATABLE"
+        " or POINTER component");
 
   fir::BoxType lcElemType =
       mlir::dyn_cast<fir::BoxType>(getLcobounds().getType());
diff --git a/flang/lib/Semantics/tools.cpp b/flang/lib/Semantics/tools.cpp
index 92bd599fdab2f..64e76aa02fe6d 100644
--- a/flang/lib/Semantics/tools.cpp
+++ b/flang/lib/Semantics/tools.cpp
@@ -841,6 +841,11 @@ bool HasAllocatableDirectComponent(const DerivedTypeSpec &derived) {
   return std::any_of(directs.begin(), directs.end(), IsAllocatable);
 }
 
+bool HasPointerDirectComponent(const DerivedTypeSpec &derived) {
+  DirectComponentIterator directs{derived};
+  return std::any_of(directs.begin(), directs.end(), IsPointer);
+}
+
 static bool MayHaveDefinedAssignment(
     const DerivedTypeSpec &derived, std::set<const Scope *> &checked) {
   if (const Scope *scope{derived.GetScope()};
diff --git a/flang/test/Lower/MIF/coarray_allocation3.f90 b/flang/test/Lower/MIF/coarray_allocation3.f90
index b4fc02baee064..0aad53428abd0 100644
--- a/flang/test/Lower/MIF/coarray_allocation3.f90
+++ b/flang/test/Lower/MIF/coarray_allocation3.f90
@@ -1,6 +1,6 @@
 ! RUN: not %flang_fc1 -emit-hlfir -fcoarray %s -o - 2>&1 | FileCheck %s
 
-!CHECK: not yet implemented: coarray: coarray with an allocatable direct component and/or requiring finalization
+!CHECK: not yet implemented: coarray: coarray with a pointer/allocatable direct component and/or requiring finalization.
 
 module m_test
     implicit none
@@ -8,6 +8,7 @@ module m_test
     type :: test_type
         integer :: id
         real, allocatable :: arr(:)
+        real, pointer :: ptr
     contains
         final :: finalize_func
     end type test_type



More information about the llvm-branch-commits mailing list