Skip to content

Commit 137a9b3

Browse files
DanielCChengithub-actions[bot]
authored andcommitted
Automerge: [flang] To use the fir::ClassType for namelist item if it is local polymorphic entity. (#162701)
This patch is to use the namelist item descriptor instead of creating a temporary one when the item is a local polymorphic entity so it's dynamic type is preserved. Fixes #152527 Fixes #154130
2 parents fd9034c + 474ed53 commit 137a9b3

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

flang/lib/Lower/IO.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,12 +524,18 @@ getNamelistGroup(Fortran::lower::AbstractConverter &converter,
524524
descAddr =
525525
builder.createConvert(loc, builder.getRefType(symType), varAddr);
526526
} else {
527+
fir::BaseBoxType boxType;
527528
const auto expr = Fortran::evaluate::AsGenericExpr(s);
528529
fir::ExtendedValue exv = converter.genExprAddr(*expr, stmtCtx);
529530
mlir::Type type = fir::getBase(exv).getType();
531+
bool isClassType = mlir::isa<fir::ClassType>(type);
530532
if (mlir::Type baseTy = fir::dyn_cast_ptrOrBoxEleTy(type))
531533
type = baseTy;
532-
fir::BoxType boxType = fir::BoxType::get(fir::PointerType::get(type));
534+
535+
if (isClassType)
536+
boxType = fir::ClassType::get(fir::PointerType::get(type));
537+
else
538+
boxType = fir::BoxType::get(fir::PointerType::get(type));
533539
descAddr = builder.createTemporary(loc, boxType);
534540
fir::MutableBoxValue box = fir::MutableBoxValue(descAddr, {}, {});
535541
fir::factory::associateMutableBox(builder, loc, box, exv,

flang/test/Lower/namelist.f90

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,45 @@ subroutine global_pointer
123123
write(10, nml=mygroup)
124124
end
125125

126+
module m
127+
type base
128+
real :: r1
129+
end type
130+
interface write(formatted)
131+
subroutine writeformatted(dtv, unit, iotype, v_list, iostat, iomsg )
132+
import base
133+
class(base), intent(in) :: dtv
134+
integer, intent(in) :: unit
135+
character(*), intent(in) :: iotype
136+
integer, intent(in) :: v_list(:)
137+
integer, intent(out) :: iostat
138+
character(*), intent(inout) :: iomsg
139+
end subroutine
140+
end interface
141+
end module
142+
143+
! CHECK-LABEL: c.func @_QPlocal_poly_namelist
144+
subroutine local_poly_namelist
145+
use m
146+
class(base), allocatable :: b1
147+
! CHECK: %[[V_0:[0-9]+]] = fir.alloca !fir.class<!fir.ptr<!fir.type<_QMmTbase{r1:f32}>>>
148+
! CHECK: %[[V_2:[0-9]+]] = fir.alloca !fir.class<!fir.heap<!fir.type<_QMmTbase{r1:f32}>>> {bindc_name = "b1", uniq_name = "_QFlocal_poly_namelistEb1"}
149+
! CHECK: %[[V_5:[0-9]+]] = fir.declare %[[V_2]] {fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QFlocal_poly_namelistEb1"} : (!fir.ref<!fir.class<!fir.heap<!fir.type<_QMmTbase{r1:f32}>>>>) -> !fir.ref<!fir.class<!fir.heap<!fir.type<_QMmTbase{r1:f32}>>>>
150+
! CHECK: %[[V_9:[0-9]+]] = fir.alloca !fir.array<1xtuple<!fir.ref<i8>, !fir.ref<!fir.box<none>>>>
151+
! CHECK: %[[V_10:[0-9]+]] = fir.undefined !fir.array<1xtuple<!fir.ref<i8>, !fir.ref<!fir.box<none>>>>
152+
! CHECK: %[[V_11:[0-9]+]] = fir.address_of(@_QQclX623100) : !fir.ref<!fir.char<1,3>>
153+
! CHECK: %[[V_12:[0-9]+]] = fir.convert %[[V_11]] : (!fir.ref<!fir.char<1,3>>) -> !fir.ref<i8>
154+
! CHECK: %[[V_13:[0-9]+]] = fir.insert_value %[[V_10]], %[[V_12]], [0 : index, 0 : index] : (!fir.array<1xtuple<!fir.ref<i8>, !fir.ref<!fir.box<none>>>>, !fir.ref<i8>) -> !fir.array<1xtuple<!fir.ref<i8>, !fir.ref<!fir.box<none>>>>
155+
! CHECK: %[[V_14:[0-9]+]] = fir.load %[[V_5]] : !fir.ref<!fir.class<!fir.heap<!fir.type<_QMmTbase{r1:f32}>>>>
156+
! CHECK: %[[V_15:[0-9]+]] = fir.rebox %[[V_14]] : (!fir.class<!fir.heap<!fir.type<_QMmTbase{r1:f32}>>>) -> !fir.class<!fir.ptr<!fir.type<_QMmTbase{r1:f32}>>>
157+
! CHECK: fir.store %[[V_15]] to %[[V_0]] : !fir.ref<!fir.class<!fir.ptr<!fir.type<_QMmTbase{r1:f32}>>>>
158+
! CHECK: %[[V_16:[0-9]+]] = fir.convert %[[V_0]] : (!fir.ref<!fir.class<!fir.ptr<!fir.type<_QMmTbase{r1:f32}>>>>) -> !fir.ref<!fir.box<none>>
159+
! CHECK: %[[V_17:[0-9]+]] = fir.insert_value %[[V_13]], %[[V_16]], [0 : index, 1 : index] : (!fir.array<1xtuple<!fir.ref<i8>, !fir.ref<!fir.box<none>>>>, !fir.ref<!fir.box<none>>) -> !fir.array<1xtuple<!fir.ref<i8>, !fir.ref<!fir.box<none>>>>
160+
! CHECK: fir.store %[[V_17]] to %[[V_9]] : !fir.ref<!fir.array<1xtuple<!fir.ref<i8>, !fir.ref<!fir.box<none>>>>>
161+
namelist/mygroup/b1
162+
write(10, nml=mygroup)
163+
end subroutine
164+
126165
module mmm
127166
real rrr
128167
namelist /aaa/ rrr
@@ -142,3 +181,4 @@ subroutine rename_sub
142181

143182
! CHECK-NOT: ggg
144183
! CHECK: fir.string_lit "aaa\00"(4) : !fir.char<1,4>
184+

0 commit comments

Comments
 (0)