1 // Test hlfir.destroy code generation and hlfir.yield_element "implicit
2 // hlfir.destroy" aspect.
4 // RUN: fir-opt %s -bufferize-hlfir | FileCheck %s
6 func.func @test_move_with_cleanup(%arg0 : !fir.ref<!fir.array<100xi32>>) {
7 %must_free = arith.constant true
8 %expr = hlfir.as_expr %arg0 move %must_free: (!fir.ref<!fir.array<100xi32>>, i1) -> !hlfir.expr<100xi32>
9 hlfir.destroy %expr : !hlfir.expr<100xi32>
12 // CHECK-LABEL: func.func @test_move_with_cleanup(
13 // CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.array<100xi32>>) {
14 // CHECK: %[[VAL_5:.*]] = fir.convert %[[VAL_0]] : (!fir.ref<!fir.array<100xi32>>) -> !fir.heap<!fir.array<100xi32>>
15 // CHECK: fir.freemem %[[VAL_5]] : !fir.heap<!fir.array<100xi32>>
17 func.func @test_move_no_cleanup(%arg0 : !fir.ref<!fir.array<100xi32>>) {
18 %must_free = arith.constant false
19 %expr = hlfir.as_expr %arg0 move %must_free: (!fir.ref<!fir.array<100xi32>>, i1) -> !hlfir.expr<100xi32>
20 hlfir.destroy %expr : !hlfir.expr<100xi32>
23 // CHECK-LABEL: func.func @test_move_no_cleanup(
24 // CHECK-NOT: fir.freemem
27 func.func @test_elemental() {
28 %c100 = arith.constant 100 : index
29 %c20 = arith.constant 20 : index
30 %0 = fir.shape %c100 : (index) -> !fir.shape<1>
31 %3 = hlfir.elemental %0 typeparams %c20 : (!fir.shape<1>, index) -> !hlfir.expr<100x!fir.char<1,20>> {
33 %buffer = fir.allocmem !fir.char<1,20>
34 %must_free = arith.constant true
35 %expr = hlfir.as_expr %buffer move %must_free: (!fir.heap<!fir.char<1,20>>, i1) -> !hlfir.expr<!fir.char<1,20>>
36 hlfir.yield_element %expr : !hlfir.expr<!fir.char<1,20>>
40 // CHECK-LABEL: func.func @test_elemental(
42 // CHECK: %[[VAL_9:.*]] = fir.allocmem !fir.char<1,20>
43 // CHECK: hlfir.assign %[[VAL_9]] to %{{.*}} : !fir.heap<!fir.char<1,20>>, !fir.ref<!fir.char<1,20>>
44 // CHECK: fir.freemem %[[VAL_9]] : !fir.heap<!fir.char<1,20>>
48 func.func @test_elemental_expr_created_outside_of_loops() {
49 %buffer = fir.allocmem !fir.char<1,20>
50 %must_free = arith.constant true
51 %expr = hlfir.as_expr %buffer move %must_free: (!fir.heap<!fir.char<1,20>>, i1) -> !hlfir.expr<!fir.char<1,20>>
52 %c100 = arith.constant 100 : index
53 %c20 = arith.constant 20 : index
54 %0 = fir.shape %c100 : (index) -> !fir.shape<1>
55 %3 = hlfir.elemental %0 typeparams %c20 : (!fir.shape<1>, index) -> !hlfir.expr<100x!fir.char<1,20>> {
57 // No freemem should be inserted inside the loops.
58 hlfir.yield_element %expr : !hlfir.expr<!fir.char<1,20>>
60 hlfir.destroy %expr : !hlfir.expr<!fir.char<1,20>>
63 // CHECK-LABEL: func.func @test_elemental_expr_created_outside_of_loops() {
64 // CHECK: %[[VAL_9:.*]] = fir.allocmem !fir.char<1,20>
66 // CHECK: hlfir.assign %[[VAL_9]] to %{{.*}} : !fir.heap<!fir.char<1,20>>, !fir.ref<!fir.char<1,20>>
67 // CHECK-NOT: fir.freemem
69 // CHECK: fir.freemem %[[VAL_9]] : !fir.heap<!fir.char<1,20>>