// Test hlfir.destroy code generation and hlfir.yield_element "implicit // hlfir.destroy" aspect. // RUN: fir-opt %s -bufferize-hlfir | FileCheck %s func.func @test_move_with_cleanup(%arg0 : !fir.ref>) { %must_free = arith.constant true %expr = hlfir.as_expr %arg0 move %must_free: (!fir.ref>, i1) -> !hlfir.expr<100xi32> hlfir.destroy %expr : !hlfir.expr<100xi32> return } // CHECK-LABEL: func.func @test_move_with_cleanup( // CHECK-SAME: %[[VAL_0:.*]]: !fir.ref>) { // CHECK: %[[VAL_5:.*]] = fir.convert %[[VAL_0]] : (!fir.ref>) -> !fir.heap> // CHECK: fir.freemem %[[VAL_5]] : !fir.heap> func.func @test_move_no_cleanup(%arg0 : !fir.ref>) { %must_free = arith.constant false %expr = hlfir.as_expr %arg0 move %must_free: (!fir.ref>, i1) -> !hlfir.expr<100xi32> hlfir.destroy %expr : !hlfir.expr<100xi32> return } // CHECK-LABEL: func.func @test_move_no_cleanup( // CHECK-NOT: fir.freemem // CHECK: return func.func @test_elemental() { %c100 = arith.constant 100 : index %c20 = arith.constant 20 : index %0 = fir.shape %c100 : (index) -> !fir.shape<1> %3 = hlfir.elemental %0 typeparams %c20 : (!fir.shape<1>, index) -> !hlfir.expr<100x!fir.char<1,20>> { ^bb0(%i: index): %buffer = fir.allocmem !fir.char<1,20> %must_free = arith.constant true %expr = hlfir.as_expr %buffer move %must_free: (!fir.heap>, i1) -> !hlfir.expr> hlfir.yield_element %expr : !hlfir.expr> } return } // CHECK-LABEL: func.func @test_elemental( // CHECK: fir.do_loop // CHECK: %[[VAL_9:.*]] = fir.allocmem !fir.char<1,20> // CHECK: hlfir.assign %[[VAL_9]] to %{{.*}} : !fir.heap>, !fir.ref> // CHECK: fir.freemem %[[VAL_9]] : !fir.heap> // CHECK: } // CHECK: return func.func @test_elemental_expr_created_outside_of_loops() { %buffer = fir.allocmem !fir.char<1,20> %must_free = arith.constant true %expr = hlfir.as_expr %buffer move %must_free: (!fir.heap>, i1) -> !hlfir.expr> %c100 = arith.constant 100 : index %c20 = arith.constant 20 : index %0 = fir.shape %c100 : (index) -> !fir.shape<1> %3 = hlfir.elemental %0 typeparams %c20 : (!fir.shape<1>, index) -> !hlfir.expr<100x!fir.char<1,20>> { ^bb0(%i: index): // No freemem should be inserted inside the loops. hlfir.yield_element %expr : !hlfir.expr> } hlfir.destroy %expr : !hlfir.expr> return } // CHECK-LABEL: func.func @test_elemental_expr_created_outside_of_loops() { // CHECK: %[[VAL_9:.*]] = fir.allocmem !fir.char<1,20> // CHECK: fir.do_loop // CHECK: hlfir.assign %[[VAL_9]] to %{{.*}} : !fir.heap>, !fir.ref> // CHECK-NOT: fir.freemem // CHECK: } // CHECK: fir.freemem %[[VAL_9]] : !fir.heap> // CHECK: return