Skip to content

Commit 5193d19

Browse files
committed
[Flang] Fix ALLOCATE with MOLD for scalars
When we allocate a variable using a MOLD argument, the function that applies the type of the MOLD argument first checks to see if the variable is already allocated by looking at its descriptor. But in the case of allocating a scalar, the descriptor was not yet been created and the associated memory is uninitialized. This change fixes that. Differential Revision: https://reviews.llvm.org/D144761
1 parent 96d035c commit 5193d19

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

flang/lib/Lower/Allocatable.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ static void genRuntimeAllocateApplyMold(fir::FirOpBuilder &builder,
220220
: fir::runtime::getRuntimeFunc<mkRTKey(AllocatableApplyMold)>(
221221
loc, builder);
222222
llvm::SmallVector<mlir::Value> args{
223-
box.getAddr(), fir::getBase(mold),
223+
fir::factory::getMutableIRBox(builder, loc, box), fir::getBase(mold),
224224
builder.createIntegerConstant(
225225
loc, callee.getFunctionType().getInputs()[2], rank)};
226226
llvm::SmallVector<mlir::Value> operands;

flang/test/Lower/allocate-mold.f90

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
! RUN: bbc -emit-fir %s -o - | FileCheck %s
2+
3+
! Test lowering of ALLOCATE statement with a MOLD argument for scalars
4+
5+
subroutine scalar_mold_allocation()
6+
integer, allocatable :: a
7+
allocate(a, mold=9)
8+
end subroutine
9+
10+
! CHECK-LABEL: func.func @_QPscalar_mold_allocation() {
11+
! CHECK: %[[A:.*]] = fir.alloca !fir.box<!fir.heap<i32>> {bindc_name = "a", uniq_name = "_QFscalar_mold_allocationEa"}
12+
! CHECK: %[[HEAP_A:.*]] = fir.alloca !fir.heap<i32> {uniq_name = "_QFscalar_mold_allocationEa.addr"}
13+
! CHECK: %[[ADDR_A:.*]] = fir.load %[[HEAP_A]] : !fir.ref<!fir.heap<i32>>
14+
! CHECK: %[[BOX_ADDR_A:.*]] = fir.embox %[[ADDR_A]] : (!fir.heap<i32>) -> !fir.box<!fir.heap<i32>>
15+
! CHECK: fir.store %[[BOX_ADDR_A]] to %[[A]] : !fir.ref<!fir.box<!fir.heap<i32>>>
16+
! CHECK: %[[A_REF_BOX_NONE1:.*]] = fir.convert %[[A]] : (!fir.ref<!fir.box<!fir.heap<i32>>>) -> !fir.ref<!fir.box<none>>
17+
! CHECK: %{{.*}} = fir.call @_FortranAAllocatableApplyMold(%[[A_REF_BOX_NONE1]], %{{.*}}, %{{.*}}) {{.*}} : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32) -> none
18+
! CHECK: %[[A_REF_BOX_NONE2:.*]] = fir.convert %[[A]] : (!fir.ref<!fir.box<!fir.heap<i32>>>) -> !fir.ref<!fir.box<none>>
19+
! CHECK: %{{.*}} = fir.call @_FortranAAllocatableAllocate(%[[A_REF_BOX_NONE2]], %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}) {{.*}} : (!fir.ref<!fir.box<none>>, i1, !fir.box<none>, !fir.ref<i8>, i32) -> i32

0 commit comments

Comments
 (0)