[flang-commits] [flang] [Flang] Fix crash in structure constructor lowering for PDT (PR #183543)

via flang-commits flang-commits at lists.llvm.org
Wed Mar 4 12:09:39 PST 2026


https://github.com/blazie2004 updated https://github.com/llvm/llvm-project/pull/183543

>From 02cadcafed4efd13ed6a78e5d26494b18f424c5b Mon Sep 17 00:00:00 2001
From: Jay Satish Kumar Patel <kumarpat at pe31.hpc.amslabs.hpecorp.net>
Date: Thu, 26 Feb 2026 08:54:37 -0600
Subject: [PATCH 1/3] [Flang] Fix crash in structure constructor lowering for
 PDT

---
 flang/lib/Lower/ConvertConstant.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/flang/lib/Lower/ConvertConstant.cpp b/flang/lib/Lower/ConvertConstant.cpp
index c44b9e8a3796b..2c6ca974eb3fa 100644
--- a/flang/lib/Lower/ConvertConstant.cpp
+++ b/flang/lib/Lower/ConvertConstant.cpp
@@ -532,6 +532,8 @@ static mlir::Value genInlinedStructureCtorLitImpl(
   for (const auto &[sym, expr] : ctor.values()) {
     const Fortran::semantics::DerivedTypeSpec *componentParentType =
         sym->owner().derivedTypeSpec();
+    if (!componentParentType)
+      componentParentType = &ctor.derivedTypeSpec();
     assert(componentParentType && "failed to retrieve component parent type");
     if (!res) {
       mlir::Type parentType = converter.genType(*componentParentType);

>From bec04746030307892c55d5bb693ae28e13390f82 Mon Sep 17 00:00:00 2001
From: Jay Satish Kumar Patel <kumarpat at pe31.hpc.amslabs.hpecorp.net>
Date: Tue, 3 Mar 2026 15:41:32 -0600
Subject: [PATCH 2/3] added LIT test case and TODO comment

---
 flang/lib/Lower/ConvertConstant.cpp            |  4 ++++
 .../test/Lower/pdt-struct-constructor-init.f90 | 18 ++++++++++++++++++
 2 files changed, 22 insertions(+)
 create mode 100644 flang/test/Lower/pdt-struct-constructor-init.f90

diff --git a/flang/lib/Lower/ConvertConstant.cpp b/flang/lib/Lower/ConvertConstant.cpp
index 2c6ca974eb3fa..a9b65cda6bb21 100644
--- a/flang/lib/Lower/ConvertConstant.cpp
+++ b/flang/lib/Lower/ConvertConstant.cpp
@@ -532,6 +532,10 @@ static mlir::Value genInlinedStructureCtorLitImpl(
   for (const auto &[sym, expr] : ctor.values()) {
     const Fortran::semantics::DerivedTypeSpec *componentParentType =
         sym->owner().derivedTypeSpec();
+    // TODO: This is not a complete fix. For some parameterized derived type
+    // component initializations, the component symbol owner does not have a
+    // derived type spec. Falling back to ctor.derivedTypeSpec() avoids the
+    // crash, but may not always represent the correct parent type.
     if (!componentParentType)
       componentParentType = &ctor.derivedTypeSpec();
     assert(componentParentType && "failed to retrieve component parent type");
diff --git a/flang/test/Lower/pdt-struct-constructor-init.f90 b/flang/test/Lower/pdt-struct-constructor-init.f90
new file mode 100644
index 0000000000000..922f98a494b98
--- /dev/null
+++ b/flang/test/Lower/pdt-struct-constructor-init.f90
@@ -0,0 +1,18 @@
+! RUN: not bbc -emit-hlfir %s 2>&1 | FileCheck %s
+
+program main
+  type ty0(k)
+    integer,kind::k
+    integer :: ii
+  end type
+
+  type ty(k,l)
+    integer,kind::k
+    integer,len ::l
+    type(ty0(2)) :: cmp(1) = [ty0(2)(4)]
+  end type
+
+  type(ty(2,4)) :: obj
+end program
+
+! CHECK: not yet implemented: parameterized derived types
\ No newline at end of file

>From 58a8313e301bb2679c9a2d21b1012cbeeec6d8c6 Mon Sep 17 00:00:00 2001
From: Jay Satish Kumar Patel <kumarpat at pe31.hpc.amslabs.hpecorp.net>
Date: Wed, 4 Mar 2026 14:01:21 -0600
Subject: [PATCH 3/3] Add  TODO for PDT structure constructor lowering

---
 flang/lib/Lower/ConvertConstant.cpp              | 2 +-
 flang/test/Lower/pdt-struct-constructor-init.f90 | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/flang/lib/Lower/ConvertConstant.cpp b/flang/lib/Lower/ConvertConstant.cpp
index a9b65cda6bb21..b4aa811f9e5ee 100644
--- a/flang/lib/Lower/ConvertConstant.cpp
+++ b/flang/lib/Lower/ConvertConstant.cpp
@@ -537,7 +537,7 @@ static mlir::Value genInlinedStructureCtorLitImpl(
     // derived type spec. Falling back to ctor.derivedTypeSpec() avoids the
     // crash, but may not always represent the correct parent type.
     if (!componentParentType)
-      componentParentType = &ctor.derivedTypeSpec();
+       TODO(loc, "parameterized derived types");
     assert(componentParentType && "failed to retrieve component parent type");
     if (!res) {
       mlir::Type parentType = converter.genType(*componentParentType);
diff --git a/flang/test/Lower/pdt-struct-constructor-init.f90 b/flang/test/Lower/pdt-struct-constructor-init.f90
index 922f98a494b98..cffbc03d46a0f 100644
--- a/flang/test/Lower/pdt-struct-constructor-init.f90
+++ b/flang/test/Lower/pdt-struct-constructor-init.f90
@@ -15,4 +15,4 @@ program main
   type(ty(2,4)) :: obj
 end program
 
-! CHECK: not yet implemented: parameterized derived types
\ No newline at end of file
+! CHECK: not yet implemented: parameterized derived types



More information about the flang-commits mailing list