1// RUN: mlir-opt %s -one-shot-bufferize="bufferize-function-boundaries test-analysis-only allow-return-allocs" -split-input-file | FileCheck %s
2
3// Run fuzzer with different seeds.
4// RUN: mlir-opt %s -one-shot-bufferize="bufferize-function-boundaries test-analysis-only allow-return-allocs analysis-fuzzer-seed=23" -split-input-file -o /dev/null
5// RUN: mlir-opt %s -one-shot-bufferize="bufferize-function-boundaries test-analysis-only allow-return-allocs analysis-fuzzer-seed=59" -split-input-file -o /dev/null
6// RUN: mlir-opt %s -one-shot-bufferize="bufferize-function-boundaries test-analysis-only allow-return-allocs analysis-fuzzer-seed=91" -split-input-file -o /dev/null
7
8// CHECK-LABEL: func @scf_for_yield_only
9func.func @scf_for_yield_only(
10    %A : tensor<?xf32> {bufferization.writable = false},
11    %B : tensor<?xf32> {bufferization.writable = true},
12    %lb : index,
13    %ub : index,
14    %step : index)
15  -> (tensor<?xf32>, tensor<?xf32>)
16{
17  //      CHECK: scf.for
18  // CHECK-NEXT: scf.yield
19  // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
20  //      CHECK: } {__inplace_operands_attr__ = ["none", "none", "none", "false"]}
21  %r0 = scf.for %i = %lb to %ub step %step iter_args(%t = %A) -> (tensor<?xf32>) {
22    scf.yield %t : tensor<?xf32>
23  }
24
25  //      CHECK: scf.for
26  // CHECK-NEXT: scf.yield
27  // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
28  //      CHECK: } {__inplace_operands_attr__ = ["none", "none", "none", "true"]}
29  %r1 = scf.for %i = %lb to %ub step %step iter_args(%t = %B) -> (tensor<?xf32>) {
30    scf.yield %t : tensor<?xf32>
31  }
32
33  //      CHECK: return
34  // CHECK-SAME: __equivalent_func_args__ = [-1, 1]
35  return %r0, %r1: tensor<?xf32>, tensor<?xf32>
36}
37
38// -----
39
40// CHECK-LABEL: func @scf_for_with_tensor.insert_slice
41func.func @scf_for_with_tensor.insert_slice(
42    %A : tensor<?xf32> {bufferization.writable = false},
43    %B : tensor<?xf32> {bufferization.writable = true},
44    %C : tensor<4xf32> {bufferization.writable = false},
45    %lb : index,
46    %ub : index,
47    %step : index)
48  -> (tensor<?xf32>, tensor<?xf32>)
49{
50  //      CHECK: scf.for
51  // scf.for bbArgs are always inplaceable seen from ops inside the body:
52  //   1. Either the matching tensor is not inplaceable and an alloc occurs
53  //      which makes bbArg inplaceable.
54  //   2. Or it is already inplaceable and so is bbArg.
55  // CHECK-NEXT:   tensor.insert_slice
56  // CHECK-SAME:     {__inplace_operands_attr__ = ["true", "true"]}
57  // CHECK-NEXT:   tensor.insert_slice
58  // CHECK-SAME:     {__inplace_operands_attr__ = ["true", "true"]}
59  // CHECK-NEXT:   scf.yield {__inplace_operands_attr__ = ["true", "true"]}
60  // CHECK-NEXT: } {__inplace_operands_attr__ = ["none", "none", "none", "false", "true"]}
61  %r0:2 = scf.for %i = %lb to %ub step %step iter_args(%tA = %A, %tB = %B)
62      -> (tensor<?xf32>, tensor<?xf32>)
63  {
64    %ttA = tensor.insert_slice %C into %tA[0][4][1] : tensor<4xf32> into tensor<?xf32>
65    %ttB = tensor.insert_slice %C into %tB[0][4][1] : tensor<4xf32> into tensor<?xf32>
66    scf.yield %ttA, %ttB : tensor<?xf32>, tensor<?xf32>
67  }
68
69  //      CHECK: return
70  // CHECK-SAME: __equivalent_func_args__ = [-1, 1]
71  return %r0#0, %r0#1: tensor<?xf32>, tensor<?xf32>
72}
73
74// -----
75
76func.func private @some_use(tensor<?xf32>) -> ()
77
78// CHECK-LABEL: func @scf_for_deps
79func.func @scf_for_deps(
80    %A : tensor<?xf32> {bufferization.writable = true},
81    %B : tensor<?xf32> {bufferization.writable = true},
82    %lb : index,
83    %ub : index,
84    %step : index)
85  -> (tensor<?xf32>)
86{
87  // %r0 must be out of place because one use of %t in the subsequent production
88  // of %r1 is read.
89  //      CHECK: scf.for
90  // CHECK-NEXT: call
91  // CHECK-SAME: {__inplace_operands_attr__ = ["false"]}
92  // CHECK-NEXT: scf.yield
93  // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
94  //      CHECK: } {__inplace_operands_attr__ = ["none", "none", "none", "false"]}
95  %r0 = scf.for %i = %lb to %ub step %step iter_args(%t = %A) -> (tensor<?xf32>) {
96    func.call @some_use(%t) : (tensor<?xf32>) -> ()
97    scf.yield %t : tensor<?xf32>
98  }
99
100  // %r1 bufferizes inplace fine.
101  //      CHECK: scf.for
102  // CHECK-NEXT: call
103  // CHECK-SAME: {__inplace_operands_attr__ = ["false"]}
104  // CHECK-NEXT: scf.yield
105  // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
106  //      CHECK: } {__inplace_operands_attr__ = ["none", "none", "none", "true"]}
107  %r1 = scf.for %i = %lb to %ub step %step iter_args(%t = %A) -> (tensor<?xf32>) {
108    func.call @some_use(%t) : (tensor<?xf32>) -> ()
109    scf.yield %t : tensor<?xf32>
110  }
111
112  //      CHECK: return
113  // CHECK-SAME: __equivalent_func_args__ = [0]
114  return %r1: tensor<?xf32>
115}
116
117// -----
118
119#accesses = [
120  affine_map<(i) -> (i)>
121]
122#trait = {
123  indexing_maps = #accesses,
124  iterator_types = ["parallel"]
125}
126
127// CHECK-LABEL: func @reading_scf_for
128func.func @reading_scf_for(%t1: tensor<?xf32> {bufferization.writable = true},
129                           %s: index, %v: vector<5xf32>) -> (tensor<?xf32>, vector<5xf32>) {
130
131  %c0 = arith.constant 0 : index
132  %c1 = arith.constant 1 : index
133  %cst = arith.constant 0.0 : f32
134
135  // Write to %t1.
136  // CHECK:      vector.transfer_write
137  // CHECK-SAME: __inplace_operands_attr__ = ["none", "false", "none"]
138  %t3 = vector.transfer_write %v, %t1[%s] : vector<5xf32>, tensor<?xf32>
139
140  // Read the old value of %t1 inside the loop via an alias.
141  // CHECK: scf.for {{.*}} {
142  %r, %v3 = scf.for %i = %c0 to %s step %c1 iter_args(%t2 = %t1, %v0 = %v) -> (tensor<?xf32>, vector<5xf32>) {
143    // CHECK:      tensor.extract_slice
144    // CHECK-SAME: __inplace_operands_attr__ = ["true", "none", "none"]
145    %e = tensor.extract_slice %t2[%s][%s][1] : tensor<?xf32> to tensor<?xf32>
146
147    // Read from %t1 via alias %e.
148    %v2 = vector.transfer_read %e[%s], %cst : tensor<?xf32>, vector<5xf32>
149    scf.yield %t2, %v2 : tensor<?xf32>, vector<5xf32>
150  }
151  // CHECK: } {__inplace_operands_attr__ = ["none", "none", "none", "true", "none"]}
152
153  // Use %t3 in some way without reading it, so that it does not get DCE'd.
154  // CHECK:      linalg.generic
155  // CHECK-SAME: __inplace_operands_attr__ = ["true"]
156  %o = linalg.generic #trait outs (%t3 : tensor<?xf32>) {
157      ^bb(%0: f32) :
158        linalg.yield %cst : f32
159    } -> (tensor<?xf32>)
160
161  return %o, %v3 : tensor<?xf32>, vector<5xf32>
162}
163
164// -----
165
166#accesses = [
167  affine_map<(i) -> (i)>
168]
169#trait = {
170  indexing_maps = #accesses,
171  iterator_types = ["parallel"]
172}
173
174// CHECK-LABEL: func @non_reading_scf_for
175func.func @non_reading_scf_for(%t1: tensor<?xf32> {bufferization.writable = true},
176                               %s: index, %v: vector<5xf32>) -> (tensor<?xf32>, vector<5xf32>) {
177
178  %c0 = arith.constant 0 : index
179  %c1 = arith.constant 1 : index
180  %cst = arith.constant 0.0 : f32
181
182  // Write to %t1.
183  // CHECK:      vector.transfer_write
184  // CHECK-SAME: __inplace_operands_attr__ = ["none", "true", "none"]
185  %t3 = vector.transfer_write %v, %t1[%s] : vector<5xf32>, tensor<?xf32>
186
187  // This loop does not read from %t1. It only writes to it.
188  // CHECK:      scf.for
189  %r, %v3 = scf.for %i = %c0 to %s step %c1 iter_args(%t2 = %t1, %v0 = %v) -> (tensor<?xf32>, vector<5xf32>) {
190    // Write to %t1 via %t2. (Overwrite %t3.)
191    // CHECK:      linalg.generic
192    // CHECK-SAME: __inplace_operands_attr__ = ["true"]
193    %o2 = linalg.generic #trait outs (%t2 : tensor<?xf32>) {
194        ^bb(%0: f32) :
195          linalg.yield %cst : f32
196      } -> (tensor<?xf32>)
197
198    // Read overwritten value. This is not a read of %t1.
199    %v2 = vector.transfer_read %o2[%s], %cst : tensor<?xf32>, vector<5xf32>
200    scf.yield %o2, %v2 : tensor<?xf32>, vector<5xf32>
201  }
202
203  // Use %t3 in some way without reading it, so that it does not get DCE'd.
204  // CHECK:      linalg.generic
205  // CHECK-SAME: __inplace_operands_attr__ = ["true"]
206  %o = linalg.generic #trait outs (%t3 : tensor<?xf32>) {
207      ^bb(%0: f32) :
208        linalg.yield %cst : f32
209    } -> (tensor<?xf32>)
210
211  //      CHECK: return
212  // CHECK-SAME: __equivalent_func_args__ = [0, -1]
213  return %o, %v3 : tensor<?xf32>, vector<5xf32>
214}
215
216// -----
217
218//===----------------------------------------------------------------------===//
219// scf.if cases
220//===----------------------------------------------------------------------===//
221
222// This example passes analysis, but it fails when bufferizing.
223// CHECK-LABEL: func @scf_if_inplace1
224func.func @scf_if_inplace1(%t1: tensor<?xf32> {bufferization.writable = true},
225                           %t2: tensor<?xf32> {bufferization.writable = true},
226                           %cond: i1) -> tensor<?xf32> {
227  %r = scf.if %cond -> (tensor<?xf32>) {
228    // CHECK:      scf.yield
229    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
230    scf.yield %t1 : tensor<?xf32>
231  } else {
232    // CHECK:      scf.yield
233    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
234    scf.yield %t2 : tensor<?xf32>
235  }
236  return %r : tensor<?xf32>
237}
238
239// -----
240
241// CHECK-LABEL: func @scf_if_inplace2
242func.func @scf_if_inplace2(%t1: tensor<?xf32> {bufferization.writable = true},
243                           %v: vector<5xf32>, %idx: index,
244                           %cond: i1) -> tensor<?xf32> {
245  %r = scf.if %cond -> (tensor<?xf32>) {
246    // CHECK:      scf.yield
247    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
248    scf.yield %t1 : tensor<?xf32>
249  } else {
250    //      CHECK: vector.transfer_write
251    // CHECK-SAME: {__inplace_operands_attr__ = ["none", "true", "none"]
252    %t2 = vector.transfer_write %v, %t1[%idx] : vector<5xf32>, tensor<?xf32>
253    scf.yield %t2 : tensor<?xf32>
254  }
255  //      CHECK: return
256  // CHECK-SAME: __equivalent_func_args__ = [0]
257  return %r : tensor<?xf32>
258}
259
260// -----
261
262// CHECK-LABEL: func @scf_if_inplace3
263func.func @scf_if_inplace3(%t1: tensor<?xf32> {bufferization.writable = true},
264                           %v1: vector<5xf32>, %v2: vector<5xf32>, %idx: index,
265                           %cond: i1) -> tensor<?xf32> {
266  //      CHECK: tensor.extract_slice
267  // CHECK-SAME: {__inplace_operands_attr__ = ["true", "none", "none"]
268  %e = tensor.extract_slice %t1[%idx][%idx][1] : tensor<?xf32> to tensor<?xf32>
269  %r = scf.if %cond -> (tensor<?xf32>) {
270    //      CHECK: vector.transfer_write
271    // CHECK-SAME: {__inplace_operands_attr__ = ["none", "true", "none"]
272    %t2 = vector.transfer_write %v1, %e[%idx] : vector<5xf32>, tensor<?xf32>
273    //      CHECK: scf.yield
274    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
275    scf.yield %t2 : tensor<?xf32>
276  } else {
277    // Writing the same tensor through an alias. This is OK.
278    //      CHECK: vector.transfer_write
279    // CHECK-SAME: {__inplace_operands_attr__ = ["none", "true", "none"]
280    %t3 = vector.transfer_write %v2, %t1[%idx] : vector<5xf32>, tensor<?xf32>
281    //      CHECK: scf.yield
282    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
283    scf.yield %t3 : tensor<?xf32>
284  }
285  return %r : tensor<?xf32>
286}
287
288// -----
289
290// CHECK-LABEL: func @scf_if_in_place4
291func.func @scf_if_in_place4(%t1: tensor<?xf32> {bufferization.writable = true},
292                            %v: vector<5xf32>, %idx: index,
293                            %cond: i1, %cond2: i1) -> (tensor<?xf32>, vector<10xf32>) {
294  %cst = arith.constant 0.0 : f32
295  %r = scf.if %cond -> (tensor<?xf32>) {
296    //      CHECK: scf.yield
297    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
298    scf.yield %t1 : tensor<?xf32>
299  } else {
300    //      CHECK: vector.transfer_write
301    // CHECK-SAME: {__inplace_operands_attr__ = ["none", "true", "none"]
302    %t2 = vector.transfer_write %v, %t1[%idx] : vector<5xf32>, tensor<?xf32>
303    //      CHECK: scf.yield
304    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
305    scf.yield %t2 : tensor<?xf32>
306  }
307  %r_alias = scf.if %cond2 -> (tensor<?xf32>) {
308    // Reading %r is OK. No conflict.
309    //      CHECK: scf.yield
310    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
311    scf.yield %r : tensor<?xf32>
312  } else {
313    //      CHECK: scf.yield
314    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
315    scf.yield %r : tensor<?xf32>
316  }
317  %v2 = vector.transfer_read %r_alias[%idx], %cst : tensor<?xf32>, vector<10xf32>
318
319  //      CHECK: return
320  // CHECK-SAME: __equivalent_func_args__ = [0, -1]
321  return %r_alias, %v2 : tensor<?xf32>, vector<10xf32>
322}
323
324// -----
325
326// CHECK-LABEL: func @scf_if_inplace5
327func.func @scf_if_inplace5(%t1: tensor<?xf32> {bufferization.writable = true},
328                           %idx: index, %cond: i1) -> tensor<?xf32> {
329  %r = scf.if %cond -> (tensor<?xf32>) {
330    //      CHECK: tensor.extract_slice
331    // CHECK-SAME: {__inplace_operands_attr__ = ["true", "none", "none"]
332    %e = tensor.extract_slice %t1[%idx][%idx][1] : tensor<?xf32> to tensor<?xf32>
333    //      CHECK: scf.yield
334    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
335    scf.yield %e : tensor<?xf32>
336  } else {
337    //      CHECK: tensor.extract_slice
338    // CHECK-SAME: {__inplace_operands_attr__ = ["true", "none", "none"]
339    %f = tensor.extract_slice %t1[%idx][%idx][1] : tensor<?xf32> to tensor<?xf32>
340    //      CHECK: scf.yield
341    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
342    scf.yield %f : tensor<?xf32>
343  }
344
345  // Inserting into an equivalent tensor at the same offset. This bufferizes
346  // inplace.
347  //      CHECK: tensor.insert_slice
348  // CHECK-SAME: {__inplace_operands_attr__ = ["true", "true", "none", "none"]
349  %r2 = tensor.insert_slice %r into %t1[%idx][%idx][1] : tensor<?xf32> into tensor<?xf32>
350
351  //      CHECK: return
352  // CHECK-SAME: __equivalent_func_args__ = [0]
353  return %r2 : tensor<?xf32>
354}
355
356// -----
357
358// CHECK-LABEL: func @scf_if_inplace6
359func.func @scf_if_inplace6(%t1: tensor<?xf32> {bufferization.writable = true},
360                           %v1: vector<5xf32>, %v2: vector<5xf32>,
361                           %v3: vector<5xf32>, %idx: index,
362                           %cond: i1, %cond2: i1) -> tensor<?xf32> {
363  // Test nested scf.if ops.
364  %r = scf.if %cond -> (tensor<?xf32>) {
365    %t2 = scf.if %cond2 -> (tensor<?xf32>) {
366      //      CHECK: vector.transfer_write
367      // CHECK-SAME: {__inplace_operands_attr__ = ["none", "true", "none"]
368      %t3 = vector.transfer_write %v1, %t1[%idx] : vector<5xf32>, tensor<?xf32>
369      //      CHECK: scf.yield
370      // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
371      scf.yield %t3 : tensor<?xf32>
372    } else {
373      //      CHECK: vector.transfer_write
374      // CHECK-SAME: {__inplace_operands_attr__ = ["none", "true", "none"]
375      %t4 = vector.transfer_write %v3, %t1[%idx] : vector<5xf32>, tensor<?xf32>
376      //      CHECK: scf.yield
377      // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
378      scf.yield %t4 : tensor<?xf32>
379    }
380    //      CHECK: scf.yield
381    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
382    scf.yield %t2 : tensor<?xf32>
383  } else {
384    //      CHECK: vector.transfer_write
385    // CHECK-SAME: {__inplace_operands_attr__ = ["none", "true", "none"]
386    %t3 = vector.transfer_write %v2, %t1[%idx] : vector<5xf32>, tensor<?xf32>
387    //      CHECK: scf.yield
388    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
389    scf.yield %t3 : tensor<?xf32>
390  }
391
392  //      CHECK: return
393  // CHECK-SAME: __equivalent_func_args__ = [0]
394  return %r : tensor<?xf32>
395}
396
397// -----
398
399// CHECK-LABEL: func @scf_if_inplace7
400func.func @scf_if_inplace7(%t1: tensor<?xf32> {bufferization.writable = true},
401                           %v1: vector<5xf32>, %v2: vector<5xf32>, %idx: index,
402                           %idx2: index, %cond: i1) -> (tensor<?xf32>, vector<5xf32>) {
403  %cst = arith.constant 0.0 : f32
404  %r, %v_r2 = scf.if %cond -> (tensor<?xf32>, vector<5xf32>) {
405    //      CHECK: vector.transfer_write
406    // CHECK-SAME: {__inplace_operands_attr__ = ["none", "true", "none"]
407    %t2 = vector.transfer_write %v1, %t1[%idx] : vector<5xf32>, tensor<?xf32>
408    //      CHECK: scf.yield
409    // CHECK-SAME: {__inplace_operands_attr__ = ["true", "none"]}
410    scf.yield %t2, %v1 : tensor<?xf32>, vector<5xf32>
411  } else {
412    // Writing the same tensor through an alias.
413    //      CHECK: vector.transfer_write
414    // CHECK-SAME: {__inplace_operands_attr__ = ["none", "false", "none"]
415    %t3 = vector.transfer_write %v2, %t1[%idx] : vector<5xf32>, tensor<?xf32>
416    // Read the original value of %t1. This requires the write in this branch
417    // to be out-of-place. But the write in the other branch can still be
418    // inplace.
419    %v_r = vector.transfer_read %t1[%idx2], %cst : tensor<?xf32>, vector<5xf32>
420    //      CHECK: scf.yield
421    // CHECK-SAME: {__inplace_operands_attr__ = ["true", "none"]}
422    scf.yield %t3, %v_r : tensor<?xf32>, vector<5xf32>
423  }
424  return %r, %v_r2 : tensor<?xf32>, vector<5xf32>
425}
426
427// -----
428
429// CHECK-LABEL: func @scf_if_out_of_place1a
430func.func @scf_if_out_of_place1a(%t1: tensor<?xf32> {bufferization.writable = true},
431                                 %idx: index, %idx2: index,
432                                 %cond: i1) -> tensor<?xf32> {
433  %r = scf.if %cond -> (tensor<?xf32>) {
434    //      CHECK: tensor.extract_slice
435    // CHECK-SAME: {__inplace_operands_attr__ = ["true", "none", "none"]
436    %e = tensor.extract_slice %t1[%idx][%idx][1] : tensor<?xf32> to tensor<?xf32>
437    //      CHECK: scf.yield
438    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
439    scf.yield %e : tensor<?xf32>
440  } else {
441    //      CHECK: scf.yield
442    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
443    scf.yield %t1 : tensor<?xf32>
444  }
445
446  // Reading from and writing to the same tensor via different args. This is a
447  // conflict.
448  //      CHECK: tensor.insert_slice
449  // CHECK-SAME: {__inplace_operands_attr__ = ["true", "false", "none", "none"]
450  %r2 = tensor.insert_slice %r into %t1[%idx2][%idx2][1] : tensor<?xf32> into tensor<?xf32>
451  return %r2 : tensor<?xf32>
452}
453
454// -----
455
456// CHECK-LABEL: func @scf_if_out_of_place1b
457func.func @scf_if_out_of_place1b(%t1: tensor<?xf32> {bufferization.writable = true},
458                                 %idx: index, %idx2: index, %idx3: index,
459                                 %cond: i1) -> tensor<?xf32> {
460  %r = scf.if %cond -> (tensor<?xf32>) {
461    //      CHECK: tensor.extract_slice
462    // CHECK-SAME: {__inplace_operands_attr__ = ["false", "none", "none"]
463    %e = tensor.extract_slice %t1[%idx][%idx][1] : tensor<?xf32> to tensor<?xf32>
464    //      CHECK: scf.yield
465    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
466    scf.yield %e : tensor<?xf32>
467  } else {
468    //      CHECK: tensor.extract_slice
469    // CHECK-SAME: {__inplace_operands_attr__ = ["false", "none", "none"]
470    %f = tensor.extract_slice %t1[%idx2][%idx2][1] : tensor<?xf32> to tensor<?xf32>
471    //      CHECK: scf.yield
472    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
473    scf.yield %f : tensor<?xf32>
474  }
475
476  // Reading from and writing to the same tensor via different args. This is a
477  // conflict. In contrast to scf_if_out_of_place1a, the fact that %r aliases
478  // with %t1 is only detected when analyzing the tensor.extract_slices. That's
479  // why the tensor.insert_slice is inplace and the two extract_slices are
480  // out-of-place.
481  //      CHECK: tensor.insert_slice
482  // CHECK-SAME: {__inplace_operands_attr__ = ["true", "true", "none", "none"]
483  %r2 = tensor.insert_slice %r into %t1[%idx3][%idx3][1] : tensor<?xf32> into tensor<?xf32>
484
485  //      CHECK: return
486  // CHECK-SAME: __equivalent_func_args__ = [0]
487  return %r2 : tensor<?xf32>
488}
489
490// -----
491
492// CHECK-LABEL: func @scf_if_out_of_place1c
493func.func @scf_if_out_of_place1c(%t1: tensor<?xf32> {bufferization.writable = true},
494                                 %idx: index, %idx2: index, %cond: i1) -> tensor<?xf32> {
495  %r = scf.if %cond -> (tensor<?xf32>) {
496    //      CHECK: tensor.extract_slice
497    // CHECK-SAME: {__inplace_operands_attr__ = ["false", "none", "none"]
498    %e = tensor.extract_slice %t1[%idx][%idx][1] : tensor<?xf32> to tensor<?xf32>
499    //      CHECK: scf.yield
500    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
501    scf.yield %e : tensor<?xf32>
502  } else {
503    // TODO: This one could bufferize inplace, but the analysis is too restrictive.
504    //      CHECK: tensor.extract_slice
505    // CHECK-SAME: {__inplace_operands_attr__ = ["false", "none", "none"]
506    %f = tensor.extract_slice %t1[%idx2][%idx2][1] : tensor<?xf32> to tensor<?xf32>
507    //      CHECK: scf.yield
508    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
509    scf.yield %f : tensor<?xf32>
510  }
511
512  //      CHECK: tensor.insert_slice
513  // CHECK-SAME: {__inplace_operands_attr__ = ["true", "true", "none", "none"]
514  %r2 = tensor.insert_slice %r into %t1[%idx2][%idx2][1] : tensor<?xf32> into tensor<?xf32>
515
516  //      CHECK: return
517  // CHECK-SAME: __equivalent_func_args__ = [0]
518  return %r2 : tensor<?xf32>
519}
520
521// -----
522
523// CHECK-LABEL: func @scf_if_out_of_place2
524func.func @scf_if_out_of_place2(%t1: tensor<?xf32> {bufferization.writable = true},
525                                %v: vector<5xf32>, %idx: index,
526                                %cond: i1) -> (tensor<?xf32>, vector<10xf32>) {
527  %cst = arith.constant 0.0 : f32
528  %r = scf.if %cond -> (tensor<?xf32>) {
529    scf.yield %t1 : tensor<?xf32>
530  } else {
531    //      CHECK: vector.transfer_write
532    // CHECK-SAME: {__inplace_operands_attr__ = ["none", "false", "none"]
533    %t2 = vector.transfer_write %v, %t1[%idx] : vector<5xf32>, tensor<?xf32>
534    //      CHECK: scf.yield
535    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
536    scf.yield %t2 : tensor<?xf32>
537  }
538
539  // Read the old value of %t1. Forces the transfer_write to bufferize
540  // out-of-place.
541  %v2 = vector.transfer_read %t1[%idx], %cst : tensor<?xf32>, vector<10xf32>
542  return %r, %v2 : tensor<?xf32>, vector<10xf32>
543}
544
545// -----
546
547// CHECK-LABEL: func @scf_if_out_of_place3
548func.func @scf_if_out_of_place3(%t1: tensor<?xf32> {bufferization.writable = true},
549                                %v: vector<5xf32>, %idx: index,
550                                %cond: i1, %cond2: i1) -> (tensor<?xf32>, vector<10xf32>) {
551  %cst = arith.constant 0.0 : f32
552  %r = scf.if %cond -> (tensor<?xf32>) {
553    scf.yield %t1 : tensor<?xf32>
554  } else {
555    //      CHECK: vector.transfer_write
556    // CHECK-SAME: {__inplace_operands_attr__ = ["none", "false", "none"]
557    %t2 = vector.transfer_write %v, %t1[%idx] : vector<5xf32>, tensor<?xf32>
558    //      CHECK: scf.yield
559    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
560    scf.yield %t2 : tensor<?xf32>
561  }
562  %t1_alias = scf.if %cond2 -> (tensor<?xf32>) {
563    // scf.yield bufferizes to a read. That is a conflict in this example.
564    //      CHECK: scf.yield
565    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
566    scf.yield %t1 : tensor<?xf32>
567  } else {
568    //      CHECK: scf.yield
569    // CHECK-SAME: {__inplace_operands_attr__ = ["true"]}
570    scf.yield %t1 : tensor<?xf32>
571  }
572  %v2 = vector.transfer_read %t1_alias[%idx], %cst : tensor<?xf32>, vector<10xf32>
573  return %r, %v2 : tensor<?xf32>, vector<10xf32>
574}
575
576// -----
577
578// CHECK-LABEL: func @write_to_same_tensor_in_loop_in_place(
579func.func @write_to_same_tensor_in_loop_in_place(
580    %A : tensor<?xf32> {linalg.inplaceable = true},
581    %lb : index, %ub : index, %step : index, %sz: index)
582  -> (tensor<?xf32>)
583{
584  // CHECK: scf.for {{.*}} {
585  %r0 = scf.for %i = %lb to %ub step %step iter_args(%t = %A) -> (tensor<?xf32>) {
586    %B = linalg.init_tensor [%sz] : tensor<?xf32>
587    %i2 = arith.index_cast %i : index to i32
588    %i3 = arith.sitofp %i2 : i32 to f32
589    // The tensor.insert is in-place because the %B is defined inside the loop.
590    //      CHECK: tensor.insert
591    // CHECK-SAME:   {__inplace_operands_attr__ = ["none", "true", "none"]}
592    %B2 = tensor.insert %i3 into %B[%i] : tensor<?xf32>
593    //      CHECK: tensor.insert_slice
594    // CHECK-SAME:   {__inplace_operands_attr__ = ["true", "true", "none", "none"]}
595    %A2 = tensor.insert_slice %B2 into %t[%i][%sz][1] : tensor<?xf32> into tensor<?xf32>
596    scf.yield %A2 : tensor<?xf32>
597  }
598  // CHECK: } {__inplace_operands_attr__ = ["none", "none", "none", "true"]}
599
600  return %r0 : tensor<?xf32>
601}
602