Skip to content

Commit 41096d1

Browse files
authored
[flang] Do not instantiate components in initial targets as objects (#75778)
Lowering was instantiating component symbols (but the last) in initial target designator as if they were whole objects, leading to collisions and bugs. Fixes #75728
1 parent 3068d27 commit 41096d1

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

flang/lib/Lower/ConvertVariable.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ mlir::Value Fortran::lower::genInitialDataTarget(
238238
/*nonDeferredParams=*/std::nullopt);
239239
// Pointer initial data target, and NULL(mold).
240240
for (const auto &sym : Fortran::evaluate::CollectSymbols(initialTarget)) {
241+
// Derived type component symbols should not be instantiated as objects
242+
// on their own.
243+
if (sym->owner().IsDerivedType())
244+
continue;
241245
// Length parameters processing will need care in global initializer
242246
// context.
243247
if (hasDerivedTypeWithLengthParameters(sym))
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
! Test https://github.com/llvm/llvm-project/issues/75728 fix.
2+
! RUN: bbc -emit-hlfir -o - -I nw %s | FileCheck %s
3+
4+
subroutine test()
5+
type t
6+
complex :: z
7+
end type
8+
type(t), target, save :: obj
9+
real, pointer :: p => obj%z%re
10+
end subroutine
11+
! CHECK-LABEL: fir.global internal @_QFtestEp : !fir.box<!fir.ptr<f32>> {
12+
! CHECK-NEXT: %[[VAL_0:.*]] = fir.address_of(@_QFtestEobj) : !fir.ref<!fir.type<_QFtestTt{z:!fir.complex<4>}>>
13+
! CHECK-NEXT: %[[VAL_1:.*]]:2 = hlfir.declare %[[VAL_0]] {fortran_attrs = #fir.var_attrs<target>, uniq_name = "_QFtestEobj"} : (!fir.ref<!fir.type<_QFtestTt{z:!fir.complex<4>}>>) -> (!fir.ref<!fir.type<_QFtestTt{z:!fir.complex<4>}>>, !fir.ref<!fir.type<_QFtestTt{z:!fir.complex<4>}>>)
14+
! CHECK-NEXT: %[[VAL_2:.*]] = hlfir.designate %[[VAL_1]]#0{"z"} real : (!fir.ref<!fir.type<_QFtestTt{z:!fir.complex<4>}>>) -> !fir.ref<f32>
15+
! CHECK-NEXT: %[[VAL_3:.*]] = fir.embox %[[VAL_2]] : (!fir.ref<f32>) -> !fir.box<f32>
16+
! CHECK-NEXT: %[[VAL_4:.*]] = fir.rebox %[[VAL_3]] : (!fir.box<f32>) -> !fir.box<!fir.ptr<f32>>
17+
! CHECK-NEXT: fir.has_value %[[VAL_4]] : !fir.box<!fir.ptr<f32>>

0 commit comments

Comments
 (0)