[flang-commits] [flang] 5193d19 - [Flang] Fix ALLOCATE with MOLD for scalars

Peter Steinfeld via flang-commits flang-commits at lists.llvm.org
Tue Feb 28 07:50:29 PST 2023


Author: Peter Steinfeld
Date: 2023-02-28T07:50:09-08:00
New Revision: 5193d19fb4ff5c26f7b7b3f7dc2aa2a2c816f1d3

URL: https://github.com/llvm/llvm-project/commit/5193d19fb4ff5c26f7b7b3f7dc2aa2a2c816f1d3
DIFF: https://github.com/llvm/llvm-project/commit/5193d19fb4ff5c26f7b7b3f7dc2aa2a2c816f1d3.diff

LOG: [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

Added: 
    flang/test/Lower/allocate-mold.f90

Modified: 
    flang/lib/Lower/Allocatable.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/Allocatable.cpp b/flang/lib/Lower/Allocatable.cpp
index cfda1ac62977..44956d65fc65 100644
--- a/flang/lib/Lower/Allocatable.cpp
+++ b/flang/lib/Lower/Allocatable.cpp
@@ -220,7 +220,7 @@ static void genRuntimeAllocateApplyMold(fir::FirOpBuilder &builder,
           : fir::runtime::getRuntimeFunc<mkRTKey(AllocatableApplyMold)>(
                 loc, builder);
   llvm::SmallVector<mlir::Value> args{
-      box.getAddr(), fir::getBase(mold),
+      fir::factory::getMutableIRBox(builder, loc, box), fir::getBase(mold),
       builder.createIntegerConstant(
           loc, callee.getFunctionType().getInputs()[2], rank)};
   llvm::SmallVector<mlir::Value> operands;

diff  --git a/flang/test/Lower/allocate-mold.f90 b/flang/test/Lower/allocate-mold.f90
new file mode 100644
index 000000000000..6a1b0aaf7381
--- /dev/null
+++ b/flang/test/Lower/allocate-mold.f90
@@ -0,0 +1,19 @@
+! RUN: bbc -emit-fir %s -o - | FileCheck %s
+
+! Test lowering of ALLOCATE statement with a MOLD argument for scalars
+
+subroutine scalar_mold_allocation()
+  integer, allocatable :: a
+  allocate(a, mold=9)
+end subroutine
+
+! CHECK-LABEL: func.func @_QPscalar_mold_allocation() {
+! CHECK: %[[A:.*]] = fir.alloca !fir.box<!fir.heap<i32>> {bindc_name = "a", uniq_name = "_QFscalar_mold_allocationEa"}
+! CHECK: %[[HEAP_A:.*]] = fir.alloca !fir.heap<i32> {uniq_name = "_QFscalar_mold_allocationEa.addr"}
+! CHECK: %[[ADDR_A:.*]] = fir.load %[[HEAP_A]] : !fir.ref<!fir.heap<i32>>
+! CHECK: %[[BOX_ADDR_A:.*]] = fir.embox %[[ADDR_A]] : (!fir.heap<i32>) -> !fir.box<!fir.heap<i32>>
+! CHECK: fir.store %[[BOX_ADDR_A]] to %[[A]] : !fir.ref<!fir.box<!fir.heap<i32>>>
+! CHECK: %[[A_REF_BOX_NONE1:.*]] = fir.convert %[[A]] : (!fir.ref<!fir.box<!fir.heap<i32>>>) -> !fir.ref<!fir.box<none>>
+! CHECK: %{{.*}} = fir.call @_FortranAAllocatableApplyMold(%[[A_REF_BOX_NONE1]], %{{.*}}, %{{.*}}) {{.*}} : (!fir.ref<!fir.box<none>>, !fir.box<none>, i32) -> none
+! CHECK: %[[A_REF_BOX_NONE2:.*]] = fir.convert %[[A]] : (!fir.ref<!fir.box<!fir.heap<i32>>>) -> !fir.ref<!fir.box<none>>
+! CHECK: %{{.*}} = fir.call @_FortranAAllocatableAllocate(%[[A_REF_BOX_NONE2]], %{{.*}}, %{{.*}}, %{{.*}}, %{{.*}}) {{.*}} : (!fir.ref<!fir.box<none>>, i1, !fir.box<none>, !fir.ref<i8>, i32) -> i32


        


More information about the flang-commits mailing list