Skip to content

Commit c55bf52

Browse files
committed
[flang] Make sure allocatable components are initialzed for temp derived-type
Runtime functions expect clean unallocated state for descriptor. This patch adds a call to the runtime function to initialize the temporary derived-type created. Reviewed By: vdonaldson Differential Revision: https://reviews.llvm.org/D133189
1 parent 10194a5 commit c55bf52

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

flang/lib/Lower/ConvertExpr.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "flang/Optimizer/Builder/Complex.h"
3535
#include "flang/Optimizer/Builder/Factory.h"
3636
#include "flang/Optimizer/Builder/Runtime/Character.h"
37+
#include "flang/Optimizer/Builder/Runtime/Derived.h"
3738
#include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
3839
#include "flang/Optimizer/Builder/Runtime/Ragged.h"
3940
#include "flang/Optimizer/Builder/Todo.h"
@@ -1056,6 +1057,8 @@ class ScalarExprLowering {
10561057
auto recTy = ty.cast<fir::RecordType>();
10571058
auto fieldTy = fir::FieldType::get(ty.getContext());
10581059
mlir::Value res = builder.createTemporary(loc, recTy);
1060+
mlir::Value box = builder.createBox(loc, fir::ExtendedValue{res});
1061+
fir::runtime::genDerivedTypeInitialize(builder, loc, box);
10591062

10601063
for (const auto &value : ctor.values()) {
10611064
const Fortran::semantics::Symbol &sym = *value.first;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
! Test lowering of derived type temporary creation and init
2+
! RUN: bbc -emit-fir %s -o - | FileCheck %s
3+
4+
program derived_temp_init
5+
type t1
6+
integer, allocatable :: i
7+
end type
8+
type t2
9+
type(t1) :: c
10+
end type
11+
type(t1) :: x
12+
type(t2) :: y
13+
y = t2(x)
14+
end
15+
16+
! CHECK: %[[temp:.*]] = fir.alloca !fir.type<_QFTt1{i:!fir.box<!fir.heap<i32>>}> {bindc_name = "x", uniq_name = "_QFEx"}
17+
! CHECK: %[[box:.*]] = fir.embox %[[temp]] : (!fir.ref<!fir.type<_QFTt1{i:!fir.box<!fir.heap<i32>>}>>) -> !fir.box<!fir.type<_QFTt1{i:!fir.box<!fir.heap<i32>>}>>
18+
! CHECK: %[[box_none:.*]] = fir.convert %[[box]] : (!fir.box<!fir.type<_QFTt1{i:!fir.box<!fir.heap<i32>>}>>) -> !fir.box<none>
19+
! CHECK: %{{.*}} = fir.call @_FortranAInitialize(%[[box_none]], %{{.*}}, %{{.*}}) : (!fir.box<none>, !fir.ref<i8>, i32) -> none

0 commit comments

Comments
 (0)