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

Carlos Seo via flang-commits flang-commits at lists.llvm.org
Thu Sep 18 11:59:12 PDT 2025


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

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

Fixes #133669

>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] [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



More information about the flang-commits mailing list