From 11760b3a27ea26c433b2df7b56798d918c62a813 Mon Sep 17 00:00:00 2001 From: Valentin Clement Date: Tue, 6 Dec 2022 15:02:14 +0100 Subject: [PATCH] [flang] Set correct box type when emboxing temporary in array value copy pass In some cases, the base type is already a fir.box type. This patch updates the code to set the result box type to fir.box, where T is the type of the allocated temp. This was reported in https://github.com/llvm/llvm-project/issues/59342 Reviewed By: jeanPerier, tblah Differential Revision: https://reviews.llvm.org/D139401 --- flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp | 2 +- flang/test/Fir/array-value-copy-3.fir | 6 +-- flang/test/Fir/array-value-copy-4.fir | 57 +++++++++++++++++++++++ 3 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 flang/test/Fir/array-value-copy-4.fir diff --git a/flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp b/flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp index bd6f738..91945b1 100644 --- a/flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp +++ b/flang/lib/Optimizer/Transforms/ArrayValueCopy.cpp @@ -1054,7 +1054,7 @@ allocateArrayTemp(mlir::Location loc, mlir::PatternRewriter &rewriter, // The allocatable component descriptors need to be set to a clean // deallocated status before anything is done with them. mlir::Value box = rewriter.create( - loc, fir::BoxType::get(baseType), allocmem, shape, + loc, fir::BoxType::get(allocmem.getType()), allocmem, shape, /*slice=*/mlir::Value{}, typeParams); auto module = load->getParentOfType(); fir::KindMapping kindMap = getKindMapping(module); diff --git a/flang/test/Fir/array-value-copy-3.fir b/flang/test/Fir/array-value-copy-3.fir index 465bdab..9103aa6 100644 --- a/flang/test/Fir/array-value-copy-3.fir +++ b/flang/test/Fir/array-value-copy-3.fir @@ -38,8 +38,8 @@ func.func @test_overlap_with_alloc_components(%arg0: !fir.ref !fir.shape<1> // CHECK: %[[VAL_10:.*]] = fir.slice %[[VAL_4]], %[[VAL_6]], %[[VAL_7]] : (index, index, index) -> !fir.slice<1> // CHECK: %[[VAL_11:.*]] = fir.allocmem !fir.array<10x!fir.type>>}>> -// CHECK: %[[VAL_12:.*]] = fir.embox %[[VAL_11]](%[[VAL_9]]) : (!fir.heap>>}>>>, !fir.shape<1>) -> !fir.box>>}>>>> -// CHECK: %[[VAL_16:.*]] = fir.convert %[[VAL_12]] : (!fir.box>>}>>>>) -> !fir.box +// CHECK: %[[VAL_12:.*]] = fir.embox %[[VAL_11]](%[[VAL_9]]) : (!fir.heap>>}>>>, !fir.shape<1>) -> !fir.box>>}>>>> +// CHECK: %[[VAL_16:.*]] = fir.convert %[[VAL_12]] : (!fir.box>>}>>>>) -> !fir.box // CHECK: fir.call @_FortranAInitialize(%[[VAL_16]], %{{.*}}, %{{.*}}) : (!fir.box, !fir.ref, i32) -> none // CHECK: fir.do_loop {{.*}} { // CHECK: fir.call @_FortranAAssign @@ -50,6 +50,6 @@ func.func @test_overlap_with_alloc_components(%arg0: !fir.ref>>}>>>>) -> !fir.box +// CHECK: %[[VAL_72:.*]] = fir.convert %[[VAL_12]] : (!fir.box>>}>>>>) -> !fir.box // CHECK: %[[VAL_73:.*]] = fir.call @_FortranADestroy(%[[VAL_72]]) : (!fir.box) -> none // CHECK: fir.freemem %[[VAL_11]] diff --git a/flang/test/Fir/array-value-copy-4.fir b/flang/test/Fir/array-value-copy-4.fir new file mode 100644 index 0000000..762e784 --- /dev/null +++ b/flang/test/Fir/array-value-copy-4.fir @@ -0,0 +1,57 @@ +// Test conversion of array_load with array of derived-type. +// Conversion was previously crashing as reported in +// https://github.com/llvm/llvm-project/issues/59342. + +// RUN: fir-opt --array-value-copy %s | FileCheck %s + +func.func @_QMmodPsub1(%arg0: !fir.box>>}>>> {fir.bindc_name = "x"}) { + %0 = fir.alloca !fir.box>>}>> + %c3_i64 = arith.constant 3 : i64 + %1 = fir.convert %c3_i64 : (i64) -> index + %c1_i64 = arith.constant 1 : i64 + %2 = fir.convert %c1_i64 : (i64) -> index + %c4_i64 = arith.constant 4 : i64 + %3 = fir.convert %c4_i64 : (i64) -> index + %c0 = arith.constant 0 : index + %4 = arith.subi %3, %1 : index + %5 = arith.addi %4, %2 : index + %6 = arith.divsi %5, %2 : index + %7 = arith.cmpi sgt, %6, %c0 : index + %8 = arith.select %7, %6, %c0 : index + %9 = fir.slice %1, %3, %2 : (index, index, index) -> !fir.slice<1> + %10 = fir.array_load %arg0 [%9] : (!fir.box>>}>>>, !fir.slice<1>) -> !fir.array>>}>> + %c1_i64_0 = arith.constant 1 : i64 + %11 = fir.convert %c1_i64_0 : (i64) -> index + %c1_i64_1 = arith.constant 1 : i64 + %12 = fir.convert %c1_i64_1 : (i64) -> index + %c2_i64 = arith.constant 2 : i64 + %13 = fir.convert %c2_i64 : (i64) -> index + %14 = fir.slice %11, %13, %12 : (index, index, index) -> !fir.slice<1> + %15 = fir.array_load %arg0 [%14] : (!fir.box>>}>>>, !fir.slice<1>) -> !fir.array>>}>> + %c1 = arith.constant 1 : index + %c0_2 = arith.constant 0 : index + %16 = arith.subi %8, %c1 : index + %17 = fir.do_loop %arg1 = %c0_2 to %16 step %c1 unordered iter_args(%arg2 = %10) -> (!fir.array>>}>>) { + %18 = fir.array_access %15, %arg1 : (!fir.array>>}>>, index) -> !fir.ref>>}>> + %19 = fir.array_access %arg2, %arg1 : (!fir.array>>}>>, index) -> !fir.ref>>}>> + %20 = fir.embox %19 : (!fir.ref>>}>>) -> !fir.box>>}>> + %21 = fir.embox %18 : (!fir.ref>>}>>) -> !fir.box>>}>> + fir.store %20 to %0 : !fir.ref>>}>>> + %22 = fir.address_of(@_QQcl.2E2F64756D6D792E66393000) : !fir.ref> + %c9_i32 = arith.constant 9 : i32 + %23 = fir.convert %0 : (!fir.ref>>}>>>) -> !fir.ref> + %24 = fir.convert %21 : (!fir.box>>}>>) -> !fir.box + %25 = fir.convert %22 : (!fir.ref>) -> !fir.ref + %26 = fir.call @_FortranAAssign(%23, %24, %25, %c9_i32) fastmath : (!fir.ref>, !fir.box, !fir.ref, i32) -> none + %27 = fir.array_amend %arg2, %19 : (!fir.array>>}>>, !fir.ref>>}>>) -> !fir.array>>}>> + fir.result %27 : !fir.array>>}>> + } + fir.array_merge_store %10, %17 to %arg0[%9] : !fir.array>>}>>, !fir.array>>}>>, !fir.box>>}>>>, !fir.slice<1> + return +} + +// CHECK-LABEL: func.func @_QMmodPsub1( +// CHECK-SAME: %[[ARG0:.*]]: !fir.box>>}>>> {fir.bindc_name = "x"}) { +// CHECK: %[[BOX_DIMS:.*]]:3 = fir.box_dims %[[ARG0]], %c0{{.*}} : (!fir.box>>}>>>, index) -> (index, index, index) +// CHECK: %[[ALLOCMEM:.*]] = fir.allocmem !fir.array>>}>>, %[[BOX_DIMS]]#1 +// CHECK: %{{.*}} = fir.embox %[[ALLOCMEM]](%{{.*}}) : (!fir.heap>>}>>>, !fir.shape<1>) -> !fir.box>>}>>>> -- 2.7.4