[flang-commits] [flang] [Flang] Fix handling of unlimited polymorphic arrays (PR #159624)

Carlos Seo via flang-commits flang-commits at lists.llvm.org
Mon Sep 22 08:00:41 PDT 2025


https://github.com/ceseo updated https://github.com/llvm/llvm-project/pull/159624

>From 7fe645df7e4f3eb9ff60d8c8e9e097723ecc91f5 Mon Sep 17 00:00:00 2001
From: Carlos Seo <carlos.seo at linaro.org>
Date: Thu, 18 Sep 2025 15:50:04 -0300
Subject: [PATCH 1/2] [Flang] Fix handling of unlimited polymorphic arrays

Make sure that unlimited polymorphic arrays in WHERE constructs are
processed through the runtime assignment path.

Fixes #133669
---
 .../HLFIR/Transforms/ConvertToFIR.cpp         |  3 ++-
 .../unlimited-polymorphic-where-construct.f90 | 25 +++++++++++++++++++
 2 files changed, 27 insertions(+), 1 deletion(-)
 create mode 100644 flang/test/HLFIR/unlimited-polymorphic-where-construct.f90

diff --git a/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp
index 8104e53920c27..127dbd40f39d8 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp
@@ -127,7 +127,8 @@ class AssignOpConversion : public mlir::OpRewritePattern<hlfir::AssignOp> {
                // types of the LHS and the RHS must match already. We use the
                // runtime in this case so that the polymorphic (including
                // unlimited) content is copied properly.
-               (lhs.isPolymorphic() && assignOp.isTemporaryLHS())) {
+               (lhs.isPolymorphic() && assignOp.isTemporaryLHS()) ||
+               lhs.isPolymorphic() || rhs.isPolymorphic()) {
       // Use the runtime for simplicity. An optimization pass will be added to
       // inline array assignment when profitable.
       mlir::Value from = emboxRHS(rhsExv);
diff --git a/flang/test/HLFIR/unlimited-polymorphic-where-construct.f90 b/flang/test/HLFIR/unlimited-polymorphic-where-construct.f90
new file mode 100644
index 0000000000000..8fdcfaca4e612
--- /dev/null
+++ b/flang/test/HLFIR/unlimited-polymorphic-where-construct.f90
@@ -0,0 +1,25 @@
+! RUN: flang -fc1 -emit-hlfir %s -o - | FileCheck %s
+
+module m1
+  type x
+  end type x
+  logical,parameter::t=.true.,f=.false.
+  logical::mask(3)=[t,f,t]
+end module m1
+
+subroutine s1
+  use m1
+  class(*),allocatable::v(:),u(:)
+  allocate(x::v(3))
+  allocate(x::u(3))
+  where(mask)
+     u=v
+  end where
+! CHECK: hlfir.region_assign
+! CHECK: !fir.ref<!fir.class<!fir.heap<!fir.array<?xnone>>>>
+end subroutine s1
+
+program main
+  call s1
+  print *,'pass'
+end program main

>From b982f6f7f1614bd0c19e5083089c3eef434850d0 Mon Sep 17 00:00:00 2001
From: Carlos Seo <carlos.seo at linaro.org>
Date: Mon, 22 Sep 2025 11:17:17 -0300
Subject: [PATCH 2/2] Simplified condition and updated comments in
 AssignOpConversion

---
 flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp
index 127dbd40f39d8..4636cc05421a1 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp
@@ -126,9 +126,9 @@ class AssignOpConversion : public mlir::OpRewritePattern<hlfir::AssignOp> {
                // assignment is the copy of the contents, because the dynamic
                // types of the LHS and the RHS must match already. We use the
                // runtime in this case so that the polymorphic (including
-               // unlimited) content is copied properly.
-               (lhs.isPolymorphic() && assignOp.isTemporaryLHS()) ||
-               lhs.isPolymorphic() || rhs.isPolymorphic()) {
+               // unlimited) content is copied properly. This is also needed
+               // for unlimited polymorphic arrays in WHERE statements.
+               (lhs.isPolymorphic())) {
       // Use the runtime for simplicity. An optimization pass will be added to
       // inline array assignment when profitable.
       mlir::Value from = emboxRHS(rhsExv);



More information about the flang-commits mailing list