[flang-commits] [flang] [flang] Fix crash in HLFIR generation (PR #118399)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Tue Dec 3 10:19:03 PST 2024


https://github.com/klausler updated https://github.com/llvm/llvm-project/pull/118399

>From 4ae74083610d60b0cf3066d813cd0e67500a2d4f Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Mon, 2 Dec 2024 13:47:09 -0800
Subject: [PATCH] [flang] Fix crash in HLFIR generation

Structure constructors with multiple components would crash when
components were from parent types.  The search for the right parent
component must be done anew for each component.

Fixes https://github.com/llvm/llvm-project/issues/118270,
https://github.com/llvm/llvm-project/issues/96994, and
https://github.com/llvm/llvm-project/issues/105848.
---
 flang/lib/Lower/ConvertExprToHLFIR.cpp | 11 +++++------
 1 file changed, 5 insertions(+), 6 deletions(-)

diff --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp
index e93fbc562f9b13..4ab319b016caf7 100644
--- a/flang/lib/Lower/ConvertExprToHLFIR.cpp
+++ b/flang/lib/Lower/ConvertExprToHLFIR.cpp
@@ -1696,18 +1696,17 @@ class HlfirBuilder {
     // required chains of hlfir.designate to address the parent components so
     // that the StructureConstructor can later be lowered by addressing these
     // parent components if needed. Note: the front-end orders the components in
-    // structure constructors. The code below relies on the component to appear
-    // in order.
+    // structure constructors.
     using ValueAndParent = std::tuple<const Fortran::lower::SomeExpr &,
                                       const Fortran::semantics::Symbol &,
                                       hlfir::EntityWithAttributes>;
     llvm::SmallVector<ValueAndParent> valuesAndParents;
-    Fortran::lower::ComponentReverseIterator compIterator(
-        ctor.result().derivedTypeSpec());
-    hlfir::EntityWithAttributes currentParent = varOp;
     for (const auto &value : llvm::reverse(ctor.values())) {
       const Fortran::semantics::Symbol &compSym = *value.first;
-      while (!compIterator.lookup(compSym.name())) {
+      hlfir::EntityWithAttributes currentParent = varOp;
+      for (Fortran::lower::ComponentReverseIterator compIterator(
+               ctor.result().derivedTypeSpec());
+           !compIterator.lookup(compSym.name());) {
         const auto &parentType = compIterator.advanceToParentType();
         llvm::StringRef parentName = toStringRef(parentType.name());
         auto baseRecTy = mlir::cast<fir::RecordType>(



More information about the flang-commits mailing list