From 14026e6aa539f376316fc8e80263cf6fbb05211a Mon Sep 17 00:00:00 2001 From: Carlos Seo Date: Tue, 22 Jul 2025 18:24:41 +0000 Subject: [PATCH] [Flang] Fix a crash when equivalence and namelist statements are used Check for equivalence when generating namelist descriptors in IO.cpp. --- flang/lib/Lower/IO.cpp | 4 +++- flang/test/Lower/equivalence-3.f | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 flang/test/Lower/equivalence-3.f diff --git a/flang/lib/Lower/IO.cpp b/flang/lib/Lower/IO.cpp index c95c3404a8e26..51f192ec08fe6 100644 --- a/flang/lib/Lower/IO.cpp +++ b/flang/lib/Lower/IO.cpp @@ -468,8 +468,10 @@ getNamelistGroup(Fortran::lower::AbstractConverter &converter, fir::BoxType boxTy = fir::BoxType::get(fir::PointerType::get(converter.genType(s))); auto descFunc = [&](fir::FirOpBuilder &b) { + bool couldBeInEquivalence = + Fortran::semantics::FindEquivalenceSet(s) != nullptr; auto box = Fortran::lower::genInitialDataTarget( - converter, loc, boxTy, *expr, /*couldBeInEquivalence=*/true); + converter, loc, boxTy, *expr, couldBeInEquivalence); fir::HasValueOp::create(b, loc, box); }; builder.createGlobalConstant(loc, boxTy, mangleName, descFunc, linkOnce); diff --git a/flang/test/Lower/equivalence-3.f b/flang/test/Lower/equivalence-3.f new file mode 100644 index 0000000000000..19f8880189f0c --- /dev/null +++ b/flang/test/Lower/equivalence-3.f @@ -0,0 +1,19 @@ +! RUN: bbc -emit-fir -o - %s | FileCheck %s + + ! CHECK-LABEL: func @_QQmain + program main + real a1,a2 + equivalence (a1,a2) + ! A fir.alloca should never appear in a global constant initialization. + ! CHECK: fir.global linkonce @_QFEx1.desc constant : !fir.box>> + ! CHECK: arith.constant 5 : index + ! CHECK:fir.address_of(@_QFEx1) : !fir.ref> + ! CHECK: fir.shape %c5 : (index) -> !fir.shape<1> + ! CHECK: fir.declare %0(%1) {uniq_name = "_QFEx1"} : (!fir.ref>, !fir.shape<1>) -> !fir.ref> + ! CHECK: fir.embox %2(%1) : (!fir.ref>, !fir.shape<1>) -> !fir.box> + ! CHECK: fir.rebox %3 : (!fir.box>) -> !fir.box>> + ! CHECK: fir.has_value %4 : !fir.box>> + real*8 x1(5) + namelist /y1/x1 + read (5,y1) + end