// RUN: fir-opt --opt-bufferization %s | FileCheck %s // Fortran F2023 15.5.2.14 point 4. ensures that _QPfoo cannot access _QFtestEx // and the temporary storage for the result can be avoided. func.func @_QPtest(%arg0: !fir.ref> {fir.bindc_name = "x"}) { %c10 = arith.constant 10 : index %0 = fir.dummy_scope : !fir.dscope %1 = fir.shape %c10 : (index) -> !fir.shape<1> %2:2 = hlfir.declare %arg0(%1) dummy_scope %0 {uniq_name = "_QFtestEx"} : (!fir.ref>, !fir.shape<1>, !fir.dscope) -> (!fir.ref>, !fir.ref>) %3 = hlfir.eval_in_mem shape %1 : (!fir.shape<1>) -> !hlfir.expr<10xf32> { ^bb0(%arg1: !fir.ref>): %4 = fir.call @_QPfoo() fastmath : () -> !fir.array<10xf32> fir.save_result %4 to %arg1(%1) : !fir.array<10xf32>, !fir.ref>, !fir.shape<1> } hlfir.assign %3 to %2#0 : !hlfir.expr<10xf32>, !fir.ref> hlfir.destroy %3 : !hlfir.expr<10xf32> return } func.func private @_QPfoo() -> !fir.array<10xf32> // CHECK-LABEL: func.func @_QPtest( // CHECK-SAME: %[[VAL_0:.*]]: !fir.ref> {fir.bindc_name = "x"}) { // CHECK: %[[VAL_1:.*]] = arith.constant 10 : index // CHECK: %[[VAL_2:.*]] = fir.dummy_scope : !fir.dscope // CHECK: %[[VAL_3:.*]] = fir.shape %[[VAL_1]] : (index) -> !fir.shape<1> // CHECK: %[[VAL_4:.*]]:2 = hlfir.declare %[[VAL_0]](%[[VAL_3]]) dummy_scope %[[VAL_2]] {uniq_name = "_QFtestEx"} : (!fir.ref>, !fir.shape<1>, !fir.dscope) -> (!fir.ref>, !fir.ref>) // CHECK: %[[VAL_5:.*]] = fir.call @_QPfoo() fastmath : () -> !fir.array<10xf32> // CHECK: fir.save_result %[[VAL_5]] to %[[VAL_4]]#0(%[[VAL_3]]) : !fir.array<10xf32>, !fir.ref>, !fir.shape<1> // CHECK: return // CHECK: } // Temporary storage cannot be avoided in this case since // _QFnegative_test_is_targetEx has the TARGET attribute. func.func @_QPnegative_test_is_target(%arg0: !fir.ref> {fir.bindc_name = "x", fir.target}) { %c10 = arith.constant 10 : index %0 = fir.dummy_scope : !fir.dscope %1 = fir.shape %c10 : (index) -> !fir.shape<1> %2:2 = hlfir.declare %arg0(%1) dummy_scope %0 {fortran_attrs = #fir.var_attrs, uniq_name = "_QFnegative_test_is_targetEx"} : (!fir.ref>, !fir.shape<1>, !fir.dscope) -> (!fir.ref>, !fir.ref>) %3 = hlfir.eval_in_mem shape %1 : (!fir.shape<1>) -> !hlfir.expr<10xf32> { ^bb0(%arg1: !fir.ref>): %4 = fir.call @_QPfoo() fastmath : () -> !fir.array<10xf32> fir.save_result %4 to %arg1(%1) : !fir.array<10xf32>, !fir.ref>, !fir.shape<1> } hlfir.assign %3 to %2#0 : !hlfir.expr<10xf32>, !fir.ref> hlfir.destroy %3 : !hlfir.expr<10xf32> return } // CHECK-LABEL: func.func @_QPnegative_test_is_target( // CHECK-SAME: %[[VAL_0:.*]]: !fir.ref> {fir.bindc_name = "x", fir.target}) { // CHECK: %[[VAL_2:.*]] = arith.constant false // CHECK: %[[VAL_3:.*]] = arith.constant 10 : index // CHECK: %[[VAL_4:.*]] = fir.alloca !fir.array<10xf32> // CHECK: %[[VAL_7:.*]]:2 = hlfir.declare %[[VAL_0]]{{.*}} // CHECK: %[[VAL_8:.*]]:2 = hlfir.declare %[[VAL_4]]{{.*}} // CHECK: %[[VAL_9:.*]] = fir.call @_QPfoo() fastmath : () -> !fir.array<10xf32> // CHECK: fir.save_result %[[VAL_9]] to %[[VAL_8]]#1{{.*}} // CHECK: %[[VAL_10:.*]] = hlfir.as_expr %[[VAL_8]]#0 move %[[VAL_2]] : (!fir.ref>, i1) -> !hlfir.expr<10xf32> // CHECK: hlfir.assign %[[VAL_10]] to %[[VAL_7]]#0 : !hlfir.expr<10xf32>, !fir.ref> // CHECK: hlfir.destroy %[[VAL_10]] : !hlfir.expr<10xf32> // CHECK: return // CHECK: } // A hlfir.shape_of user of the eval_in_mem (such as the one left behind by // SeparateAllocatableAssign when sizing a reallocation) must not block the // in-place rewrite: it is redirected to the eval_in_mem shape operand. func.func @_QPtest_shape_of_user(%arg0: !fir.ref> {fir.bindc_name = "x"}, %arg1: !fir.ref) { %c10 = arith.constant 10 : index %0 = fir.dummy_scope : !fir.dscope %1 = fir.shape %c10 : (index) -> !fir.shape<1> %2:2 = hlfir.declare %arg0(%1) dummy_scope %0 {uniq_name = "_QFtestEx"} : (!fir.ref>, !fir.shape<1>, !fir.dscope) -> (!fir.ref>, !fir.ref>) %3 = hlfir.eval_in_mem shape %1 : (!fir.shape<1>) -> !hlfir.expr<10xf32> { ^bb0(%arg2: !fir.ref>): %4 = fir.call @_QPfoo() fastmath : () -> !fir.array<10xf32> fir.save_result %4 to %arg2(%1) : !fir.array<10xf32>, !fir.ref>, !fir.shape<1> } %shp = hlfir.shape_of %3 : (!hlfir.expr<10xf32>) -> !fir.shape<1> %ext = hlfir.get_extent %shp {dim = 0 : index} : (!fir.shape<1>) -> index hlfir.assign %3 to %2#0 : !hlfir.expr<10xf32>, !fir.ref> hlfir.destroy %3 : !hlfir.expr<10xf32> fir.store %ext to %arg1 : !fir.ref return } // CHECK-LABEL: func.func @_QPtest_shape_of_user( // CHECK: %[[SHAPE:.*]] = fir.shape %{{.*}} : (index) -> !fir.shape<1> // The shape_of is redirected to the eval_in_mem shape operand... // CHECK: hlfir.get_extent %[[SHAPE]] // ...and the result is still evaluated directly into the LHS (no temporary). // CHECK: %[[CALL:.*]] = fir.call @_QPfoo() fastmath : () -> !fir.array<10xf32> // CHECK: fir.save_result %[[CALL]] to %{{.*}}#0(%[[SHAPE]]) // CHECK-NOT: hlfir.eval_in_mem // CHECK-NOT: hlfir.shape_of