1// RUN: mlir-opt %s -one-shot-bufferize="allow-return-allocs bufferize-function-boundaries" -split-input-file | FileCheck %s
2
3// Run fuzzer with different seeds.
4// RUN: mlir-opt %s -one-shot-bufferize="allow-return-allocs test-analysis-only analysis-fuzzer-seed=23 bufferize-function-boundaries" -split-input-file -o /dev/null
5// RUN: mlir-opt %s -one-shot-bufferize="allow-return-allocs test-analysis-only analysis-fuzzer-seed=59 bufferize-function-boundaries" -split-input-file -o /dev/null
6// RUN: mlir-opt %s -one-shot-bufferize="allow-return-allocs test-analysis-only analysis-fuzzer-seed=91 bufferize-function-boundaries" -split-input-file -o /dev/null
7
8// Test bufferization using memref types that have no layout map.
9// RUN: mlir-opt %s -one-shot-bufferize="allow-return-allocs fully-dynamic-layout-maps=0 bufferize-function-boundaries" -split-input-file -o /dev/null
10
11// CHECK-DAG: #[[$map_1d_dyn:.*]] = affine_map<(d0)[s0, s1] -> (d0 * s1 + s0)>
12
13// CHECK-LABEL: func @scf_for_yield_only(
14//  CHECK-SAME:   %[[A:[a-zA-Z0-9]*]]: memref<?xf32, #[[$map_1d_dyn]]>,
15//  CHECK-SAME:   %[[t:[a-zA-Z0-9]*]]: memref<?xf32, #[[$map_1d_dyn]]>
16//  CHECK-SAME:   ) -> memref<?xf32> {
17func.func @scf_for_yield_only(
18    %A : tensor<?xf32> {bufferization.writable = false},
19    %B : tensor<?xf32> {bufferization.writable = true},
20    %lb : index, %ub : index, %step : index)
21  -> (tensor<?xf32>, tensor<?xf32>)
22{
23  //     CHECK:   %[[ALLOC_FOR_A:.*]] = memref.alloc
24  //     CHECK:   memref.copy %[[A]], %[[ALLOC_FOR_A]]
25
26  // The first scf.for remains but just turns into dead code.
27  %r0 = scf.for %i = %lb to %ub step %step iter_args(%t = %A) -> (tensor<?xf32>) {
28    scf.yield %t : tensor<?xf32>
29  }
30
31  // The second scf.for remains but just turns into dead code.
32  %r1 = scf.for %i = %lb to %ub step %step iter_args(%t = %B) -> (tensor<?xf32>) {
33    scf.yield %t : tensor<?xf32>
34  }
35
36  //     CHECK:   return %[[ALLOC_FOR_A]] : memref<?xf32>
37  // CHECK-NOT:   dealloc
38  return %r0, %r1: tensor<?xf32>, tensor<?xf32>
39}
40
41// -----
42
43// Ensure that the function bufferizes without error. This tests pre-order
44// traversal of scf.for loops during bufferization. No need to check the IR,
45// just want to make sure that it does not crash.
46
47// CHECK-LABEL: func @nested_scf_for
48func.func @nested_scf_for(%A : tensor<?xf32> {bufferization.writable = true},
49                          %v : vector<5xf32>) -> tensor<?xf32> {
50  %c0 = arith.constant 0 : index
51  %c1 = arith.constant 1 : index
52  %c10 = arith.constant 10 : index
53  %r1 = scf.for %i = %c0 to %c10 step %c1 iter_args(%B = %A) -> tensor<?xf32> {
54    %r2 = scf.for %j = %c0 to %c10 step %c1 iter_args(%C = %B) -> tensor<?xf32> {
55      %w = vector.transfer_write %v, %C[%c0] : vector<5xf32>, tensor<?xf32>
56      scf.yield %w : tensor<?xf32>
57    }
58    scf.yield %r2 : tensor<?xf32>
59  }
60  return %r1 : tensor<?xf32>
61}
62
63// -----
64
65// CHECK-DAG: #[[$map_1d_dyn:.*]] = affine_map<(d0)[s0, s1] -> (d0 * s1 + s0)>
66
67// CHECK-LABEL: func @scf_for_with_tensor.insert_slice
68//  CHECK-SAME:   %[[A:[a-zA-Z0-9]*]]: memref<?xf32, #[[$map_1d_dyn]]>
69//  CHECK-SAME:   %[[B:[a-zA-Z0-9]*]]: memref<?xf32, #[[$map_1d_dyn]]>
70//  CHECK-SAME:   %[[C:[a-zA-Z0-9]*]]: memref<4xf32, #[[$map_1d_dyn]]>
71func.func @scf_for_with_tensor.insert_slice(
72    %A : tensor<?xf32> {bufferization.writable = false},
73    %B : tensor<?xf32> {bufferization.writable = true},
74    %C : tensor<4xf32> {bufferization.writable = false},
75    %lb : index, %ub : index, %step : index)
76  -> (tensor<?xf32>, tensor<?xf32>)
77{
78  //     CHECK:   %[[ALLOC_FOR_A:.*]] = memref.alloc
79  //     CHECK:   memref.copy %[[A]], %[[ALLOC_FOR_A]]
80
81  //     CHECK: %[[svA:.*]] = memref.subview %[[ALLOC_FOR_A]][0] [4] [1]
82  //     CHECK: %[[svB:.*]] = memref.subview %[[B]][0] [4] [1]
83
84  //     CHECK:   scf.for {{.*}}
85  // CHECK-NOT: iter_args
86  %r0:2 = scf.for %i = %lb to %ub step %step iter_args(%tA = %A, %tB = %B)
87      -> (tensor<?xf32>, tensor<?xf32>)
88  {
89    // %ttA bufferizes to direct copy of %BUFFER_CAST_C into %svA
90    //     CHECK: memref.copy %[[C]], %[[svA]]
91    %ttA = tensor.insert_slice %C into %tA[0][4][1] : tensor<4xf32> into tensor<?xf32>
92
93    // %ttB bufferizes to direct copy of %BUFFER_CAST_C into %BUFFER_CAST_B
94    //     CHECK:   memref.copy %[[C]], %[[svB]]
95    %ttB = tensor.insert_slice %C into %tB[0][4][1] : tensor<4xf32> into tensor<?xf32>
96
97    // CHECK-NOT:   scf.yield
98    scf.yield %ttA, %ttB : tensor<?xf32>, tensor<?xf32>
99  }
100
101  //     CHECK:  return %[[ALLOC_FOR_A]] : memref<?xf32>
102  return %r0#0, %r0#1: tensor<?xf32>, tensor<?xf32>
103}
104
105// -----
106
107// CHECK-LABEL: func @execute_region_with_conflict(
108//  CHECK-SAME:     %[[m1:.*]]: memref<?xf32
109func.func @execute_region_with_conflict(
110    %t1 : tensor<?xf32> {bufferization.writable = "true"})
111  -> (f32, tensor<?xf32>, f32)
112{
113  %f1 = arith.constant 0.0 : f32
114  %idx = arith.constant 7 : index
115
116  // scf.execute_region is canonicalized away after bufferization. So just the
117  // memref.store is left over.
118
119  // CHECK: %[[alloc:.*]] = memref.alloc
120  // CHECK: memref.copy %[[m1]], %[[alloc]]
121  // CHECK: memref.store %{{.*}}, %[[alloc]][%{{.*}}]
122  %0, %1, %2 = scf.execute_region -> (f32, tensor<?xf32>, f32) {
123    %t2 = tensor.insert %f1 into %t1[%idx] : tensor<?xf32>
124    scf.yield %f1, %t2, %f1 : f32, tensor<?xf32>, f32
125  }
126
127  // CHECK: %[[casted:.*]] = memref.cast %[[alloc]]
128  // CHECK: %[[load:.*]] = memref.load %[[m1]]
129  %3 = tensor.extract %t1[%idx] : tensor<?xf32>
130
131  // CHECK: return %{{.*}}, %[[casted]], %[[load]] : f32, memref<?xf32, #{{.*}}>, f32
132  return %0, %1, %3 : f32, tensor<?xf32>, f32
133}
134
135// -----
136
137// CHECK-LABEL: func @scf_if_inplace(
138//  CHECK-SAME:     %[[cond:.*]]: i1, %[[t1:.*]]: memref<?xf32{{.*}}>, %[[v:.*]]: vector
139func.func @scf_if_inplace(%cond: i1,
140                          %t1: tensor<?xf32> {bufferization.writable = true},
141                          %v: vector<5xf32>, %idx: index) -> tensor<?xf32> {
142
143  //      CHECK: scf.if %[[cond]] {
144  // CHECK-NEXT: } else {
145  // CHECK-NEXT:   vector.transfer_write %[[v]], %[[t1]]
146  // CHECK-NEXT: }
147  // CHECK-NEXT: return
148  %r = scf.if %cond -> (tensor<?xf32>) {
149    scf.yield %t1 : tensor<?xf32>
150  } else {
151    %t2 = vector.transfer_write %v, %t1[%idx] : vector<5xf32>, tensor<?xf32>
152    scf.yield %t2 : tensor<?xf32>
153  }
154  return %r : tensor<?xf32>
155}
156
157// -----
158
159// CHECK-LABEL: func @scf_if_inside_scf_for
160//   CHECK-DAG:   %[[c0:.*]] = arith.constant 0 : index
161//   CHECK-DAG:   %[[c1:.*]] = arith.constant 1 : index
162//   CHECK-DAG:   %[[c10:.*]] = arith.constant 10 : index
163//       CHECK:   scf.for %{{.*}} = %[[c0]] to %[[c10]] step %[[c1]] {
164//       CHECK:     scf.if %{{.*}} {
165//       CHECK:     } else {
166//       CHECK:       vector.transfer_write
167//       CHECK:     }
168//       CHECK:   }
169func.func @scf_if_inside_scf_for(
170    %t1: tensor<?xf32> {bufferization.writable = true},
171    %v: vector<5xf32>, %idx: index,
172    %cond: i1)
173  -> tensor<?xf32>
174{
175  %c0 = arith.constant 0 : index
176  %c1 = arith.constant 1 : index
177  %c10 = arith.constant 10 : index
178  %r = scf.for %iv = %c0 to %c10 step %c1 iter_args(%bb = %t1) -> (tensor<?xf32>) {
179    %r2 = scf.if %cond -> (tensor<?xf32>) {
180      scf.yield %bb : tensor<?xf32>
181    } else {
182      %t2 = vector.transfer_write %v, %bb[%idx] : vector<5xf32>, tensor<?xf32>
183      scf.yield %t2 : tensor<?xf32>
184    }
185    scf.yield %r2 : tensor<?xf32>
186  }
187  return %r : tensor<?xf32>
188}
189
190// -----
191
192// CHECK-LABEL: func @scf_if_non_equiv_yields(
193//  CHECK-SAME:     %[[cond:.*]]: i1, %[[A:.*]]: memref<{{.*}}>, %[[B:.*]]: memref<{{.*}}>) -> memref<{{.*}}>
194func.func @scf_if_non_equiv_yields(
195    %b : i1,
196    %A : tensor<4xf32> {bufferization.writable = false},
197    %B : tensor<4xf32> {bufferization.writable = false})
198  -> tensor<4xf32>
199{
200  // CHECK: %[[r:.*]] = arith.select %[[cond]], %[[A]], %[[B]]
201  %r = scf.if %b -> (tensor<4xf32>) {
202    scf.yield %A : tensor<4xf32>
203  } else {
204    scf.yield %B : tensor<4xf32>
205  }
206  // CHECK: return %[[r]]
207  return %r: tensor<4xf32>
208}
209
210// -----
211
212// Note: This bufferization is inefficient, but it bufferizes correctly.
213
214// CHECK-LABEL: func @scf_execute_region_yield_non_equivalent(
215//       CHECK:   %[[alloc:.*]] = memref.alloc(%{{.*}})
216//       CHECK:   %[[clone:.*]] = bufferization.clone %[[alloc]]
217//       CHECK:   memref.dealloc %[[alloc]]
218//       CHECK:   %[[r:.*]] = memref.load %[[clone]][%{{.*}}]
219//       CHECK:   memref.dealloc %[[clone]]
220//       CHECK:   return %[[r]]
221func.func @scf_execute_region_yield_non_equivalent(%i: index, %j: index) -> f32 {
222  %r = scf.execute_region -> (tensor<?xf32>) {
223    %t2 = linalg.init_tensor [%i] : tensor<?xf32>
224    scf.yield %t2 : tensor<?xf32>
225  }
226  %f = tensor.extract %r[%j] : tensor<?xf32>
227  return %f : f32
228}
229
230// -----
231
232// Note: This bufferizes to inefficient code, but bufferization should not see
233// such IR in the first place. The iter_arg would canonicalize away. This test
234// case is just to ensure that the bufferization generates correct code.
235
236// CHECK-LABEL: func @scf_for_yield_non_equivalent(
237//  CHECK-SAME:     %[[t:.*]]: memref<?xf32
238//       CHECK:   %[[alloc:.*]] = memref.alloc(%{{.*}})
239//       CHECK:   %[[for:.*]] = scf.for {{.*}} iter_args(%[[iter:.*]] = %[[alloc]])
240//       CHECK:     memref.dealloc %[[iter]]
241//       CHECK:     %[[alloc2:.*]] = memref.alloc(%{{.*}})
242//       CHECK:     memref.copy %[[t]], %[[alloc2]]
243//       CHECK:     scf.yield %[[alloc2]]
244//       CHECK:   return %[[for]]
245func.func @scf_for_yield_non_equivalent(
246    %t: tensor<?xf32>, %lb : index, %ub : index, %step : index) -> tensor<?xf32> {
247  %r = scf.for %i = %lb to %ub step %step iter_args(%a = %t) -> tensor<?xf32> {
248    scf.yield %t : tensor<?xf32>
249  }
250
251  return %r : tensor<?xf32>
252}
253
254// -----
255
256// Note: This bufferizes to inefficient code, but bufferization should not see
257// such IR in the first place. The iter_arg would canonicalize away. This test
258// case is just to ensure that the bufferization generates correct code.
259
260// CHECK-LABEL: func @scf_for_yield_allocation(
261//  CHECK-SAME:     %[[t:.*]]: memref<?xf32
262//       CHECK:   %[[cloned:.*]] = bufferization.clone %[[t]]
263//       CHECK:   %[[for:.*]] = scf.for {{.*}} iter_args(%[[iter:.*]] = %[[cloned]])
264// This alloc is for the linalg.init_tensor.
265//   CHECK-DAG:     %[[alloc2:.*]] = memref.alloc(%{{.*}})
266//   CHECK-DAG:     memref.dealloc %[[iter]]
267// This alloc is for the scf.yield.
268//       CHECK:     %[[alloc3:.*]] = memref.alloc(%{{.*}})
269//       CHECK:     memref.copy %[[alloc2]], %[[alloc3]]
270//       CHECK:     memref.dealloc %[[alloc2]]
271//       CHECK:     %[[casted3:.*]] = memref.cast %[[alloc3]]
272//       CHECK:     scf.yield %[[casted3]]
273//       CHECK:   return %[[for]]
274func.func @scf_for_yield_allocation(%t: tensor<?xf32>, %lb : index, %ub : index,
275                               %step : index) -> tensor<?xf32> {
276  %r = scf.for %i = %lb to %ub step %step iter_args(%a = %t) -> tensor<?xf32> {
277    %t2 = linalg.init_tensor [%i] : tensor<?xf32>
278    scf.yield %t2 : tensor<?xf32>
279  }
280
281  return %r : tensor<?xf32>
282}
283
284// -----
285
286// TODO: The scf.yield could bufferize to 1 alloc and 2 copies (instead of
287// 2 allocs and 2 copies).
288
289// CHECK-LABEL: func @scf_for_swapping_yields(
290//  CHECK-SAME:     %[[A:.*]]: memref<?xf32, #{{.*}}>, %[[B:.*]]: memref<?xf32, #{{.*}}>
291func.func @scf_for_swapping_yields(
292    %A : tensor<?xf32>, %B : tensor<?xf32> {bufferization.writable = true},
293    %C : tensor<4xf32>, %lb : index, %ub : index, %step : index)
294  -> (f32, f32)
295{
296//   CHECK-DAG:   %[[clone1:.*]] = bufferization.clone %[[A]]
297//   CHECK-DAG:   %[[clone2:.*]] = bufferization.clone %[[B]]
298//       CHECK:   %[[for:.*]]:2 = scf.for {{.*}} iter_args(%[[iter1:.*]] = %[[clone1]], %[[iter2:.*]] = %[[clone2]])
299  %r0:2 = scf.for %i = %lb to %ub step %step iter_args(%tA = %A, %tB = %B)
300      -> (tensor<?xf32>, tensor<?xf32>)
301  {
302//       CHECK:     %[[sv1:.*]] = memref.subview %[[iter1]]
303//       CHECK:     memref.copy %{{.*}}, %[[sv1]]
304    %ttA = tensor.insert_slice %C into %tA[0][4][1] : tensor<4xf32> into tensor<?xf32>
305//       CHECK:     %[[sv2:.*]] = memref.subview %[[iter2]]
306//       CHECK:     memref.copy %{{.*}}, %[[sv2]]
307    %ttB = tensor.insert_slice %C into %tB[0][4][1] : tensor<4xf32> into tensor<?xf32>
308
309//       CHECK:     %[[alloc2:.*]] = memref.alloc(%{{.*}})
310//       CHECK:     memref.copy %[[iter2]], %[[alloc2]]
311//       CHECK:     memref.dealloc %[[iter2]]
312//       CHECK:     %[[alloc1:.*]] = memref.alloc(%{{.*}})
313//       CHECK:     memref.copy %[[iter1]], %[[alloc1]]
314//       CHECK:     memref.dealloc %[[iter1]]
315//       CHECK:     %[[casted1:.*]] = memref.cast %[[alloc1]]
316//       CHECK:     %[[casted2:.*]] = memref.cast %[[alloc2]]
317//       CHECK:     scf.yield %[[casted2]], %[[casted1]]
318    // Yield tensors in different order.
319    scf.yield %ttB, %ttA : tensor<?xf32>, tensor<?xf32>
320  }
321
322//       CHECK:     %[[r0:.*]] = memref.load %[[for]]#0
323//       CHECK:     memref.dealloc %[[for]]#0
324//       CHECK:     %[[r1:.*]] = memref.load %[[for]]#1
325//       CHECK:     memref.dealloc %[[for]]#1
326  %f0 = tensor.extract %r0#0[%step] : tensor<?xf32>
327  %f1 = tensor.extract %r0#1[%step] : tensor<?xf32>
328//       CHECK:     return %[[r0]], %[[r1]]
329  return %f0, %f1: f32, f32
330}
331