1 // Test memory allocation pass for fir.alloca outside of function entry block
2 // RUN: fir-opt --memory-allocation-opt="dynamic-array-on-heap=true" %s | FileCheck %s
4 func.func @test_loop() {
5 %c1 = arith.constant 1 : index
6 %c100 = arith.constant 100 : index
7 fir.do_loop %arg0 = %c1 to %c100 step %c1 {
8 %1 = fir.alloca !fir.array<?xf32>, %arg0
9 fir.call @bar(%1) : (!fir.ref<!fir.array<?xf32>>) -> ()
14 // CHECK-LABEL: func.func @test_loop() {
15 // CHECK: %[[VAL_0:.*]] = arith.constant 1 : index
16 // CHECK: %[[VAL_1:.*]] = arith.constant 100 : index
17 // CHECK: fir.do_loop %[[VAL_2:.*]] = %[[VAL_0]] to %[[VAL_1]] step %[[VAL_0]] {
18 // CHECK: %[[VAL_3:.*]] = fir.allocmem !fir.array<?xf32>, %[[VAL_2]] {bindc_name = "", uniq_name = ""}
19 // CHECK: %[[VAL_4:.*]] = fir.convert %[[VAL_3]] : (!fir.heap<!fir.array<?xf32>>) -> !fir.ref<!fir.array<?xf32>>
20 // CHECK: fir.call @bar(%[[VAL_4]]) : (!fir.ref<!fir.array<?xf32>>) -> ()
21 // CHECK: fir.freemem %[[VAL_3]] : !fir.heap<!fir.array<?xf32>>
26 func.func @test_unstructured(%n : index) {
27 %c0 = arith.constant 0 : index
28 %c1 = arith.constant 1 : index
29 %c100 = arith.constant 100 : index
31 fir.store %c100 to %0 : !fir.ref<index>
33 ^bb1: // 2 preds: ^bb0, ^bb4
34 %5 = fir.load %0 : !fir.ref<index>
35 %6 = arith.cmpi sgt, %5, %c0 : index
36 cf.cond_br %6, ^bb2, ^bb5
38 %1 = fir.alloca !fir.array<?xf32>, %5
39 fir.call @bar(%1) : (!fir.ref<!fir.array<?xf32>>) -> ()
40 %25 = arith.cmpi slt, %5, %n : index
41 cf.cond_br %25, ^bb3, ^bb4
43 fir.call @abort() : () -> ()
46 %28 = arith.subi %5, %c1 : index
47 fir.store %28 to %0 : !fir.ref<index>
52 // CHECK-LABEL: func.func @test_unstructured(
53 // CHECK-SAME: %[[VAL_0:.*]]: index) {
54 // CHECK: %[[VAL_1:.*]] = fir.alloca !fir.heap<!fir.array<?xf32>>
55 // CHECK: %[[VAL_2:.*]] = fir.zero_bits !fir.heap<!fir.array<?xf32>>
56 // CHECK: fir.store %[[VAL_2]] to %[[VAL_1]] : !fir.ref<!fir.heap<!fir.array<?xf32>>>
57 // CHECK: %[[VAL_3:.*]] = arith.constant 0 : i64
58 // CHECK: %[[VAL_4:.*]] = arith.constant 0 : index
59 // CHECK: %[[VAL_5:.*]] = arith.constant 1 : index
60 // CHECK: %[[VAL_6:.*]] = arith.constant 100 : index
61 // CHECK: %[[VAL_7:.*]] = fir.alloca index
62 // CHECK: fir.store %[[VAL_6]] to %[[VAL_7]] : !fir.ref<index>
65 // CHECK: %[[VAL_8:.*]] = fir.load %[[VAL_7]] : !fir.ref<index>
66 // CHECK: %[[VAL_9:.*]] = arith.cmpi sgt, %[[VAL_8]], %[[VAL_4]] : index
67 // CHECK: cf.cond_br %[[VAL_9]], ^bb2, ^bb5
69 // CHECK: %[[VAL_10:.*]] = fir.load %[[VAL_1]] : !fir.ref<!fir.heap<!fir.array<?xf32>>>
70 // CHECK: %[[VAL_11:.*]] = fir.convert %[[VAL_10]] : (!fir.heap<!fir.array<?xf32>>) -> i64
71 // CHECK: %[[VAL_12:.*]] = arith.cmpi ne, %[[VAL_11]], %[[VAL_3]] : i64
72 // CHECK: fir.if %[[VAL_12]] {
73 // CHECK: fir.freemem %[[VAL_10]] : !fir.heap<!fir.array<?xf32>>
75 // CHECK: %[[VAL_13:.*]] = fir.allocmem !fir.array<?xf32>, %[[VAL_8]] {bindc_name = "", uniq_name = ""}
76 // CHECK: %[[VAL_14:.*]] = fir.convert %[[VAL_13]] : (!fir.heap<!fir.array<?xf32>>) -> !fir.ref<!fir.array<?xf32>>
77 // CHECK: fir.store %[[VAL_13]] to %[[VAL_1]] : !fir.ref<!fir.heap<!fir.array<?xf32>>>
78 // CHECK: fir.call @bar(%[[VAL_14]]) : (!fir.ref<!fir.array<?xf32>>) -> ()
79 // CHECK: %[[VAL_15:.*]] = arith.cmpi slt, %[[VAL_8]], %[[VAL_0]] : index
80 // CHECK: cf.cond_br %[[VAL_15]], ^bb3, ^bb4
82 // CHECK: fir.call @abort() : () -> ()
83 // CHECK: %[[VAL_16:.*]] = fir.load %[[VAL_1]] : !fir.ref<!fir.heap<!fir.array<?xf32>>>
84 // CHECK: %[[VAL_17:.*]] = fir.convert %[[VAL_16]] : (!fir.heap<!fir.array<?xf32>>) -> i64
85 // CHECK: %[[VAL_18:.*]] = arith.cmpi ne, %[[VAL_17]], %[[VAL_3]] : i64
86 // CHECK: fir.if %[[VAL_18]] {
87 // CHECK: fir.freemem %[[VAL_16]] : !fir.heap<!fir.array<?xf32>>
89 // CHECK: fir.unreachable
91 // CHECK: %[[VAL_19:.*]] = arith.subi %[[VAL_8]], %[[VAL_5]] : index
92 // CHECK: fir.store %[[VAL_19]] to %[[VAL_7]] : !fir.ref<index>
95 // CHECK: %[[VAL_20:.*]] = fir.load %[[VAL_1]] : !fir.ref<!fir.heap<!fir.array<?xf32>>>
96 // CHECK: %[[VAL_21:.*]] = fir.convert %[[VAL_20]] : (!fir.heap<!fir.array<?xf32>>) -> i64
97 // CHECK: %[[VAL_22:.*]] = arith.cmpi ne, %[[VAL_21]], %[[VAL_3]] : i64
98 // CHECK: fir.if %[[VAL_22]] {
99 // CHECK: fir.freemem %[[VAL_20]] : !fir.heap<!fir.array<?xf32>>
104 func.func @alloca_dominate_return_in_cycle(%arg0: index) {
105 %0 = fir.alloca index
106 %c1 = arith.constant 1 : index
107 fir.store %c1 to %0 : !fir.ref<index>
109 ^bb1: // 2 preds: ^bb0, ^bb2
110 %1 = fir.load %0 : !fir.ref<index>
111 %2 = fir.alloca !fir.array<?xf32>, %1
112 fir.call @bar(%2) : (!fir.ref<!fir.array<?xf32>>) -> ()
113 %3 = arith.addi %1, %c1 : index
114 fir.store %3 to %0 : !fir.ref<index>
115 %4 = arith.cmpi slt, %3, %arg0 : index
116 cf.cond_br %4, ^bb2, ^bb3
122 // CHECK-LABEL: func.func @alloca_dominate_return_in_cycle(
123 // CHECK-SAME: %[[VAL_0:.*]]: index) {
124 // CHECK: %[[VAL_1:.*]] = fir.alloca !fir.heap<!fir.array<?xf32>>
125 // CHECK: %[[VAL_2:.*]] = fir.zero_bits !fir.heap<!fir.array<?xf32>>
126 // CHECK: fir.store %[[VAL_2]] to %[[VAL_1]] : !fir.ref<!fir.heap<!fir.array<?xf32>>>
127 // CHECK: %[[VAL_3:.*]] = arith.constant 0 : i64
128 // CHECK: %[[VAL_4:.*]] = fir.alloca index
129 // CHECK: %[[VAL_5:.*]] = arith.constant 1 : index
130 // CHECK: fir.store %[[VAL_5]] to %[[VAL_4]] : !fir.ref<index>
133 // CHECK: %[[VAL_6:.*]] = fir.load %[[VAL_4]] : !fir.ref<index>
134 // CHECK: %[[VAL_7:.*]] = fir.load %[[VAL_1]] : !fir.ref<!fir.heap<!fir.array<?xf32>>>
135 // CHECK: %[[VAL_8:.*]] = fir.convert %[[VAL_7]] : (!fir.heap<!fir.array<?xf32>>) -> i64
136 // CHECK: %[[VAL_9:.*]] = arith.cmpi ne, %[[VAL_8]], %[[VAL_3]] : i64
137 // CHECK: fir.if %[[VAL_9]] {
138 // CHECK: fir.freemem %[[VAL_7]] : !fir.heap<!fir.array<?xf32>>
140 // CHECK: %[[VAL_10:.*]] = fir.allocmem !fir.array<?xf32>, %[[VAL_6]] {bindc_name = "", uniq_name = ""}
141 // CHECK: %[[VAL_11:.*]] = fir.convert %[[VAL_10]] : (!fir.heap<!fir.array<?xf32>>) -> !fir.ref<!fir.array<?xf32>>
142 // CHECK: fir.store %[[VAL_10]] to %[[VAL_1]] : !fir.ref<!fir.heap<!fir.array<?xf32>>>
143 // CHECK: fir.call @bar(%[[VAL_11]]) : (!fir.ref<!fir.array<?xf32>>) -> ()
144 // CHECK: %[[VAL_12:.*]] = arith.addi %[[VAL_6]], %[[VAL_5]] : index
145 // CHECK: fir.store %[[VAL_12]] to %[[VAL_4]] : !fir.ref<index>
146 // CHECK: %[[VAL_13:.*]] = arith.cmpi slt, %[[VAL_12]], %[[VAL_0]] : index
147 // CHECK: cf.cond_br %[[VAL_13]], ^bb2, ^bb3
151 // CHECK: %[[VAL_14:.*]] = fir.load %[[VAL_1]] : !fir.ref<!fir.heap<!fir.array<?xf32>>>
152 // CHECK: %[[VAL_15:.*]] = fir.convert %[[VAL_14]] : (!fir.heap<!fir.array<?xf32>>) -> i64
153 // CHECK: %[[VAL_16:.*]] = arith.cmpi ne, %[[VAL_15]], %[[VAL_3]] : i64
154 // CHECK: fir.if %[[VAL_16]] {
155 // CHECK: fir.freemem %[[VAL_14]] : !fir.heap<!fir.array<?xf32>>
160 func.func private @bar(!fir.ref<!fir.array<?xf32>>)
161 func.func private @abort()