1 // RUN: mlir-opt %s -remove-dead-values -split-input-file -verify-diagnostics | FileCheck %s
3 // The IR remains untouched because of the presence of a non-function-like
4 // symbol op inside the module (const @__dont_touch_unacceptable_ir).
7 // expected-error @+1 {{cannot optimize an IR with non-function symbol ops, non-call symbol user ops or branch ops}}
8 memref.global "private" constant @__dont_touch_unacceptable_ir : memref<i32> = dense<0>
9 func.func @main(%arg0: i32) -> i32 {
16 // Dead values are removed from the IR even if the module has a name
18 module @named_module_acceptable {
19 func.func @main(%arg0: tensor<10xf32>) -> tensor<10xf32> {
20 %0 = tensor.empty() : tensor<10xbf16>
21 // CHECK-NOT: tensor.empty
22 return %arg0 : tensor<10xf32>
28 // The IR remains untouched because of the presence of a branch op `cf.cond_br`.
30 func.func @dont_touch_unacceptable_ir_has_cleanable_simple_op_with_branch_op(%arg0: i1) {
31 %non_live = arith.constant 0 : i32
32 // expected-error @+1 {{cannot optimize an IR with non-function symbol ops, non-call symbol user ops or branch ops}}
33 cf.cond_br %arg0, ^bb1(%non_live : i32), ^bb2(%non_live : i32)
34 ^bb1(%non_live_0 : i32):
36 ^bb2(%non_live_1 : i32):
44 // Note that this cleanup cannot be done by the `canonicalize` pass.
46 // CHECK-LABEL: func.func private @clean_func_op_remove_argument_and_return_value() {
49 // CHECK: func.func @main(%[[arg0:.*]]: i32) {
50 // CHECK-NEXT: call @clean_func_op_remove_argument_and_return_value() : () -> ()
53 func.func private @clean_func_op_remove_argument_and_return_value(%arg0: i32) -> (i32) {
56 func.func @main(%arg0 : i32) {
57 %non_live = func.call @clean_func_op_remove_argument_and_return_value(%arg0) : (i32) -> (i32)
63 // %arg0 is not live because it is never used. %arg1 is not live because its
64 // user `arith.addi` doesn't have any uses and the value that it is forwarded to
65 // (%non_live_0) also doesn't have any uses.
67 // Note that this cleanup cannot be done by the `canonicalize` pass.
69 // CHECK-LABEL: func.func private @clean_func_op_remove_arguments() -> i32 {
70 // CHECK-NEXT: %[[c0:.*]] = arith.constant 0
71 // CHECK-NEXT: return %[[c0]]
73 // CHECK: func.func @main(%[[arg2:.*]]: memref<i32>, %[[arg3:.*]]: i32, %[[DEVICE:.*]]: i32) -> (i32, memref<i32>) {
74 // CHECK-NEXT: %[[live:.*]] = test.call_on_device @clean_func_op_remove_arguments(), %[[DEVICE]] : (i32) -> i32
75 // CHECK-NEXT: return %[[live]], %[[arg2]]
77 func.func private @clean_func_op_remove_arguments(%arg0 : memref<i32>, %arg1 : i32) -> (i32, i32) {
78 %c0 = arith.constant 0 : i32
79 %non_live = arith.addi %arg1, %arg1 : i32
80 return %c0, %arg1 : i32, i32
82 func.func @main(%arg2 : memref<i32>, %arg3 : i32, %device : i32) -> (i32, memref<i32>) {
83 %live, %non_live_0 = test.call_on_device @clean_func_op_remove_arguments(%arg2, %arg3), %device : (memref<i32>, i32, i32) -> (i32, i32)
84 return %live, %arg2 : i32, memref<i32>
89 // Even though %non_live_0 is not live, the first return value of
90 // @clean_func_op_remove_return_values isn't removed because %live is live
91 // (liveness is checked across all callers).
93 // Also, the second return value of @clean_func_op_remove_return_values is
94 // removed despite %c0 being live because neither %non_live nor %non_live_1 were
95 // live (removal doesn't depend on the liveness of the operand itself but on the
96 // liveness of where it is forwarded).
98 // Note that this cleanup cannot be done by the `canonicalize` pass.
100 // CHECK: func.func private @clean_func_op_remove_return_values(%[[arg0:.*]]: memref<i32>) -> i32 {
101 // CHECK-NEXT: %[[c0]] = arith.constant 0
102 // CHECK-NEXT: memref.store %[[c0]], %[[arg0]][]
103 // CHECK-NEXT: return %[[c0]]
105 // CHECK: func.func @main(%[[arg1:.*]]: memref<i32>) -> i32 {
106 // CHECK-NEXT: %[[live:.*]] = call @clean_func_op_remove_return_values(%[[arg1]]) : (memref<i32>) -> i32
107 // CHECK-NEXT: %[[non_live_0:.*]] = call @clean_func_op_remove_return_values(%[[arg1]]) : (memref<i32>) -> i32
108 // CHECK-NEXT: return %[[live]] : i32
110 func.func private @clean_func_op_remove_return_values(%arg0 : memref<i32>) -> (i32, i32) {
111 %c0 = arith.constant 0 : i32
112 memref.store %c0, %arg0[] : memref<i32>
113 return %c0, %c0 : i32, i32
115 func.func @main(%arg1 : memref<i32>) -> (i32) {
116 %live, %non_live = func.call @clean_func_op_remove_return_values(%arg1) : (memref<i32>) -> (i32, i32)
117 %non_live_0, %non_live_1 = func.call @clean_func_op_remove_return_values(%arg1) : (memref<i32>) -> (i32, i32)
123 // None of the return values of @clean_func_op_dont_remove_return_values can be
124 // removed because the first one is forwarded to a live value %live and the
125 // second one is forwarded to a live value %live_0.
127 // CHECK-LABEL: func.func private @clean_func_op_dont_remove_return_values() -> (i32, i32) {
128 // CHECK-NEXT: %[[c0:.*]] = arith.constant 0 : i32
129 // CHECK-NEXT: return %[[c0]], %[[c0]] : i32, i32
131 // CHECK-LABEL: func.func @main() -> (i32, i32) {
132 // CHECK-NEXT: %[[live_and_non_live:.*]]:2 = call @clean_func_op_dont_remove_return_values() : () -> (i32, i32)
133 // CHECK-NEXT: %[[non_live_0_and_live_0:.*]]:2 = call @clean_func_op_dont_remove_return_values() : () -> (i32, i32)
134 // CHECK-NEXT: return %[[live_and_non_live]]#0, %[[non_live_0_and_live_0]]#1 : i32, i32
136 func.func private @clean_func_op_dont_remove_return_values() -> (i32, i32) {
137 %c0 = arith.constant 0 : i32
138 return %c0, %c0 : i32, i32
140 func.func @main() -> (i32, i32) {
141 %live, %non_live = func.call @clean_func_op_dont_remove_return_values() : () -> (i32, i32)
142 %non_live_0, %live_0 = func.call @clean_func_op_dont_remove_return_values() : () -> (i32, i32)
143 return %live, %live_0 : i32, i32
149 // (1) %non_live is not live. Yet, it is kept because %arg4 in `scf.condition`
150 // forwards to it, which has to be kept. %arg4 in `scf.condition` has to be
151 // kept because it forwards to %arg6 which is live.
153 // (2) %arg5 is not live. Yet, it is kept because %live_0 forwards to it, which
154 // also forwards to %live, which is live.
157 // (1) %arg1 is not kept as an operand of `scf.while` because it only forwards
158 // to %arg3, which is not kept. %arg3 is not kept because %arg3 is not live and
159 // only %arg1 and %arg7 forward to it, such that neither of them forward
160 // anywhere else. Thus, %arg7 is also not kept in the `scf.yield` op.
162 // Note that this cleanup cannot be done by the `canonicalize` pass.
164 // CHECK: func.func @clean_region_branch_op_dont_remove_first_2_results_but_remove_first_operand(%[[arg0:.*]]: i1, %[[arg1:.*]]: i32, %[[arg2:.*]]: i32) -> i32 {
165 // CHECK-NEXT: %[[live_and_non_live:.*]]:2 = scf.while (%[[arg4:.*]] = %[[arg2]]) : (i32) -> (i32, i32) {
166 // CHECK-NEXT: %[[live_0:.*]] = arith.addi %[[arg4]], %[[arg4]]
167 // CHECK-NEXT: scf.condition(%arg0) %[[live_0]], %[[arg4]] : i32, i32
168 // CHECK-NEXT: } do {
169 // CHECK-NEXT: ^bb0(%[[arg5:.*]]: i32, %[[arg6:.*]]: i32):
170 // CHECK-NEXT: %[[live_1:.*]] = arith.addi %[[arg6]], %[[arg6]]
171 // CHECK-NEXT: scf.yield %[[live_1]] : i32
173 // CHECK-NEXT: return %[[live_and_non_live]]#0
175 func.func @clean_region_branch_op_dont_remove_first_2_results_but_remove_first_operand(%arg0: i1, %arg1: i32, %arg2: i32) -> (i32) {
176 %live, %non_live, %non_live_0 = scf.while (%arg3 = %arg1, %arg4 = %arg2) : (i32, i32) -> (i32, i32, i32) {
177 %live_0 = arith.addi %arg4, %arg4 : i32
178 %non_live_1 = arith.addi %arg3, %arg3 : i32
179 scf.condition(%arg0) %live_0, %arg4, %non_live_1 : i32, i32, i32
181 ^bb0(%arg5: i32, %arg6: i32, %arg7: i32):
182 %live_1 = arith.addi %arg6, %arg6 : i32
183 scf.yield %arg7, %live_1 : i32, i32
191 // (1) %live is kept because it is live.
193 // (2) %non_live is not live. Yet, it is kept because %arg3 in `scf.condition`
194 // forwards to it and this %arg3 has to be kept. This %arg3 in `scf.condition`
195 // has to be kept because it forwards to %arg6, which forwards to %arg4, which
196 // forwards to %live, which is live.
199 // (1) %non_live_0 is not kept because %non_live_2 in `scf.condition` forwards
200 // to it, which forwards to only %non_live_0 and %arg7, where both these are
201 // not live and have no other value forwarding to them.
203 // (2) %non_live_1 is not kept because %non_live_3 in `scf.condition` forwards
204 // to it, which forwards to only %non_live_1 and %arg8, where both these are
205 // not live and have no other value forwarding to them.
207 // (3) %c2 is not kept because it only forwards to %arg10, which is not kept.
209 // (4) %arg10 is not kept because only %c2 and %non_live_4 forward to it, none
210 // of them forward anywhere else, and %arg10 is not.
212 // (5) %arg7 and %arg8 are not kept because they are not live, %non_live_2 and
213 // %non_live_3 forward to them, and both only otherwise forward to %non_live_0
214 // and %non_live_1 which are not live and have no other predecessors.
216 // Note that this cleanup cannot be done by the `canonicalize` pass.
218 // CHECK: func.func @clean_region_branch_op_remove_last_2_results_last_2_arguments_and_last_operand(%[[arg2:.*]]: i1) -> i32 {
219 // CHECK-NEXT: %[[c0:.*]] = arith.constant 0
220 // CHECK-NEXT: %[[c1:.*]] = arith.constant 1
221 // CHECK-NEXT: %[[live_and_non_live:.*]]:2 = scf.while (%[[arg3:.*]] = %[[c0]], %[[arg4:.*]] = %[[c1]]) : (i32, i32) -> (i32, i32) {
222 // CHECK-NEXT: func.call @identity() : () -> ()
223 // CHECK-NEXT: scf.condition(%[[arg2]]) %[[arg4]], %[[arg3]] : i32, i32
224 // CHECK-NEXT: } do {
225 // CHECK-NEXT: ^bb0(%[[arg5:.*]]: i32, %[[arg6:.*]]: i32):
226 // CHECK-NEXT: scf.yield %[[arg5]], %[[arg6]] : i32, i32
228 // CHECK-NEXT: return %[[live_and_non_live]]#0 : i32
230 // CHECK: func.func private @identity() {
231 // CHECK-NEXT: return
233 func.func @clean_region_branch_op_remove_last_2_results_last_2_arguments_and_last_operand(%arg2: i1) -> (i32) {
234 %c0 = arith.constant 0 : i32
235 %c1 = arith.constant 1 : i32
236 %c2 = arith.constant 2 : i32
237 %live, %non_live, %non_live_0, %non_live_1 = scf.while (%arg3 = %c0, %arg4 = %c1, %arg10 = %c2) : (i32, i32, i32) -> (i32, i32, i32, i32) {
238 %non_live_2 = arith.addi %arg10, %arg10 : i32
239 %non_live_3 = func.call @identity(%arg10) : (i32) -> (i32)
240 scf.condition(%arg2) %arg4, %arg3, %non_live_2, %non_live_3 : i32, i32, i32, i32
242 ^bb0(%arg5: i32, %arg6: i32, %arg7: i32, %arg8: i32):
243 %non_live_4 = arith.addi %arg7, %arg8 :i32
244 scf.yield %arg5, %arg6, %non_live_4 : i32, i32, i32
248 func.func private @identity(%arg1 : i32) -> (i32) {
254 // The op isn't erased because it has memory effects but its unnecessary result
257 // Note that this cleanup cannot be done by the `canonicalize` pass.
259 // CHECK: func.func @clean_region_branch_op_remove_result(%[[arg0:.*]]: index, %[[arg1:.*]]: memref<i32>) {
260 // CHECK-NEXT: scf.index_switch %[[arg0]]
261 // CHECK-NEXT: case 1 {
262 // CHECK-NEXT: %[[c10:.*]] = arith.constant 10
263 // CHECK-NEXT: memref.store %[[c10]], %[[arg1]][]
264 // CHECK-NEXT: scf.yield
266 // CHECK-NEXT: default {
268 // CHECK-NEXT: return
270 func.func @clean_region_branch_op_remove_result(%arg0 : index, %arg1 : memref<i32>) {
271 %non_live = scf.index_switch %arg0 -> i32
273 %c10 = arith.constant 10 : i32
274 memref.store %c10, %arg1[] : memref<i32>
278 %c11 = arith.constant 11 : i32
286 // The simple ops which don't have memory effects or live results get removed.
287 // %arg5 doesn't get removed from the @main even though it isn't live because
288 // the signature of a public function is always left untouched.
290 // Note that this cleanup cannot be done by the `canonicalize` pass.
292 // CHECK: func.func private @clean_simple_ops(%[[arg0:.*]]: i32, %[[arg1:.*]]: memref<i32>)
293 // CHECK-NEXT: %[[live_0:.*]] = arith.addi %[[arg0]], %[[arg0]]
294 // CHECK-NEXT: %[[c2:.*]] = arith.constant 2
295 // CHECK-NEXT: %[[live_1:.*]] = arith.muli %[[live_0]], %[[c2]]
296 // CHECK-NEXT: %[[c3:.*]] = arith.constant 3
297 // CHECK-NEXT: %[[live_2:.*]] = arith.addi %[[arg0]], %[[c3]]
298 // CHECK-NEXT: memref.store %[[live_2]], %[[arg1]][]
299 // CHECK-NEXT: return %[[live_1]]
301 // CHECK: func.func @main(%[[arg3:.*]]: i32, %[[arg4:.*]]: memref<i32>, %[[arg5:.*]]
302 // CHECK-NEXT: %[[live:.*]] = call @clean_simple_ops(%[[arg3]], %[[arg4]])
303 // CHECK-NEXT: return %[[live]]
305 func.func private @clean_simple_ops(%arg0 : i32, %arg1 : memref<i32>, %arg2 : i32) -> (i32, i32, i32, i32) {
306 %live_0 = arith.addi %arg0, %arg0 : i32
307 %c2 = arith.constant 2 : i32
308 %live_1 = arith.muli %live_0, %c2 : i32
309 %non_live_1 = arith.addi %live_1, %live_0 : i32
310 %non_live_2 = arith.constant 7 : i32
311 %non_live_3 = arith.subi %arg0, %non_live_1 : i32
312 %c3 = arith.constant 3 : i32
313 %live_2 = arith.addi %arg0, %c3 : i32
314 memref.store %live_2, %arg1[] : memref<i32>
315 return %live_1, %non_live_1, %non_live_2, %non_live_3 : i32, i32, i32, i32
318 func.func @main(%arg3 : i32, %arg4 : memref<i32>, %arg5 : i32) -> (i32) {
319 %live, %non_live_1, %non_live_2, %non_live_3 = func.call @clean_simple_ops(%arg3, %arg4, %arg5) : (i32, memref<i32>, i32) -> (i32, i32, i32, i32)
325 // The scf.while op has no memory effects and its result isn't live.
327 // Note that this cleanup cannot be done by the `canonicalize` pass.
329 // CHECK-LABEL: func.func private @clean_region_branch_op_erase_it() {
330 // CHECK-NEXT: return
332 // CHECK: func.func @main(%[[arg3:.*]]: i32, %[[arg4:.*]]: i1) {
333 // CHECK-NEXT: call @clean_region_branch_op_erase_it() : () -> ()
334 // CHECK-NEXT: return
336 func.func private @clean_region_branch_op_erase_it(%arg0 : i32, %arg1 : i1) -> (i32) {
337 %non_live = scf.while (%arg2 = %arg0) : (i32) -> (i32) {
338 scf.condition(%arg1) %arg2 : i32
341 scf.yield %arg2 : i32
343 return %non_live : i32
346 func.func @main(%arg3 : i32, %arg4 : i1) {
347 %non_live_0 = func.call @clean_region_branch_op_erase_it(%arg3, %arg4) : (i32, i1) -> (i32)
353 #map = affine_map<(d0)[s0, s1] -> (d0 * s0 + s1)>
354 func.func @kernel(%arg0: memref<18xf32>) {
355 %c1 = arith.constant 1 : index
356 %c18 = arith.constant 18 : index
357 gpu.launch blocks(%arg3, %arg4, %arg5) in (%arg9 = %c18, %arg10 = %c18, %arg11 = %c18) threads(%arg6, %arg7, %arg8) in (%arg12 = %c1, %arg13 = %c1, %arg14 = %c1) {
358 %c1_0 = arith.constant 1 : index
359 %c0_1 = arith.constant 0 : index
360 %cst_2 = arith.constant 25.4669495 : f32
361 %6 = affine.apply #map(%arg3)[%c1_0, %c0_1]
362 memref.store %cst_2, %arg0[%6] : memref<18xf32>
368 // CHECK-LABEL: func.func @kernel(%arg0: memref<18xf32>) {
369 // CHECK: gpu.launch blocks
370 // CHECK: memref.store
371 // CHECK-NEXT: gpu.terminator
375 // CHECK: func.func private @no_block_func_declaration()
376 func.func private @no_block_func_declaration() -> ()