1// RUN: mlir-opt %s -allow-unregistered-dialect -one-shot-bufferize="bufferize-function-boundaries=1" -split-input-file -verify-diagnostics
2
3func.func private @foo() -> tensor<?xf32>
4
5func.func @bar() -> tensor<?xf32> {
6  %foo = constant @foo : () -> (tensor<?xf32>)
7// expected-error @+1 {{expected a CallOp}}
8  %res = call_indirect %foo() : () -> (tensor<?xf32>)
9  return %res : tensor<?xf32>
10}
11
12// -----
13
14// expected-error @+2 {{op was not bufferized}}
15// expected-error @+1 {{cannot bufferize bodiless function that returns a tensor}}
16func.func private @foo() -> tensor<?xf32>
17
18// -----
19
20// expected-error @+1 {{cannot bufferize a FuncOp with tensors and without a unique ReturnOp}}
21func.func @swappy(%cond1 : i1, %cond2 : i1, %t1 : tensor<f32>, %t2 : tensor<f32>)
22    -> (tensor<f32>, tensor<f32>)
23{
24  cf.cond_br %cond1, ^bb1, ^bb2
25
26  ^bb1:
27    %T:2 = scf.if %cond2 -> (tensor<f32>, tensor<f32>) {
28      scf.yield %t1, %t2 : tensor<f32>, tensor<f32>
29    } else {
30      scf.yield %t2, %t1 : tensor<f32>, tensor<f32>
31    }
32    return %T#0, %T#1 : tensor<f32>, tensor<f32>
33  ^bb2:
34    return %t2, %t1 : tensor<f32>, tensor<f32>
35}
36
37// -----
38
39func.func @scf_if_not_equivalent(
40    %cond: i1, %t1: tensor<?xf32> {bufferization.writable = true},
41    %idx: index) -> tensor<?xf32> {
42  %r = scf.if %cond -> (tensor<?xf32>) {
43    scf.yield %t1 : tensor<?xf32>
44  } else {
45    // This buffer aliases, but it is not equivalent.
46    %t2 = tensor.extract_slice %t1 [%idx] [%idx] [1] : tensor<?xf32> to tensor<?xf32>
47    // expected-error @+1 {{operand #0 of ReturnLike op does not satisfy destination passing style}}
48    scf.yield %t2 : tensor<?xf32>
49  }
50  // expected-error @+1 {{operand #0 of ReturnLike op does not satisfy destination passing style}}
51  return %r : tensor<?xf32>
52}
53
54// -----
55
56func.func @scf_if_not_aliasing(
57    %cond: i1, %t1: tensor<?xf32> {bufferization.writable = true},
58    %idx: index) -> f32 {
59  %r = scf.if %cond -> (tensor<?xf32>) {
60    scf.yield %t1 : tensor<?xf32>
61  } else {
62    // This buffer aliases.
63    %t2 = linalg.init_tensor [%idx] : tensor<?xf32>
64    // expected-error @+1 {{operand #0 of ReturnLike op does not satisfy destination passing style}}
65    scf.yield %t2 : tensor<?xf32>
66  }
67  %f = tensor.extract %r[%idx] : tensor<?xf32>
68  return %f : f32
69}
70
71// -----
72
73// expected-error @-3 {{expected callgraph to be free of circular dependencies}}
74
75func.func @foo() {
76  call @bar() : () -> ()
77  return
78}
79
80func.func @bar() {
81  call @foo() : () -> ()
82  return
83}
84
85// -----
86
87func.func @scf_for(%A : tensor<?xf32>,
88              %B : tensor<?xf32> {bufferization.writable = true},
89              %C : tensor<4xf32>,
90              %lb : index, %ub : index, %step : index)
91  -> (f32, f32)
92{
93  %r0:2 = scf.for %i = %lb to %ub step %step iter_args(%tA = %A, %tB = %B)
94      -> (tensor<?xf32>, tensor<?xf32>)
95  {
96    %ttA = tensor.insert_slice %C into %tA[0][4][1] : tensor<4xf32> into tensor<?xf32>
97    %ttB = tensor.insert_slice %C into %tB[0][4][1] : tensor<4xf32> into tensor<?xf32>
98
99    // Throw a wrench in the system by swapping yielded values: this result in a
100    // ping-pong of values at each iteration on which we currently want to fail.
101
102    // expected-error @+1 {{Yield operand #0 does not bufferize to a buffer that is aliasing}}
103    scf.yield %ttB, %ttA : tensor<?xf32>, tensor<?xf32>
104  }
105
106  %f0 = tensor.extract %r0#0[%step] : tensor<?xf32>
107  %f1 = tensor.extract %r0#1[%step] : tensor<?xf32>
108  return %f0, %f1: f32, f32
109}
110
111// -----
112
113func.func private @fun_with_side_effects(%A: tensor<?xf32> {bufferization.writable = true})
114
115func.func @foo(%A: tensor<?xf32> {bufferization.writable = true}) -> (tensor<?xf32>) {
116  call @fun_with_side_effects(%A) : (tensor<?xf32>) -> ()
117  return %A: tensor<?xf32>
118}
119
120func.func @scf_yield_needs_copy(%A : tensor<?xf32> {bufferization.writable = true}, %iters : index) {
121  %c0 = arith.constant 0 : index
122  %c1 = arith.constant 1 : index
123  %res = scf.for %arg0 = %c0 to %iters step %c1 iter_args(%bbarg = %A) -> (tensor<?xf32>) {
124    %r = func.call @foo(%A) : (tensor<?xf32>) -> (tensor<?xf32>)
125    // expected-error @+1 {{Yield operand #0 does not bufferize to a buffer that is aliasing}}
126    scf.yield %r : tensor<?xf32>
127  }
128  call @fun_with_side_effects(%res) : (tensor<?xf32>) -> ()
129  return
130}
131
132// -----
133
134func.func @extract_slice_fun(%A : tensor<?xf32> {bufferization.writable = true})
135  ->  tensor<4xf32>
136{
137  // This bufferizes to a pattern that the cross-function boundary pass needs to
138  // convert into a new memref argument at all call site; this may be either:
139  //   - an externally created aliasing subview (if we want to allow aliasing
140  //     function arguments).
141  //   - a new alloc + copy (more expensive but does not create new function
142  //     argument aliasing).
143  %r0 = tensor.extract_slice %A[0][4][1] : tensor<?xf32> to tensor<4xf32>
144
145  // expected-error @+1 {{operand #0 of ReturnLike op does not satisfy destination passing style}}
146  return %r0: tensor<4xf32>
147}
148
149// -----
150
151func.func @scf_yield(%b : i1, %A : tensor<4xf32>, %B : tensor<4xf32>) -> tensor<4xf32>
152{
153  %r = scf.if %b -> (tensor<4xf32>) {
154    scf.yield %A : tensor<4xf32>
155  } else {
156    scf.yield %B : tensor<4xf32>
157  }
158  // expected-error @+1 {{operand #0 of ReturnLike op does not satisfy destination passing style}}
159  return %r: tensor<4xf32>
160}
161
162// -----
163
164func.func @unknown_op(%A : tensor<4xf32>) -> tensor<4xf32>
165{
166  // expected-error: @+1 {{op was not bufferized}}
167  %r = "marklar"(%A) : (tensor<4xf32>) -> (tensor<4xf32>)
168  // expected-error @+1 {{operand #0 of ReturnLike op does not satisfy destination passing style}}
169  return %r: tensor<4xf32>
170}
171
172// -----
173
174func.func @mini_test_case1() -> tensor<10x20xf32> {
175  %f0 = arith.constant 0.0 : f32
176  %t = linalg.init_tensor [10, 20] : tensor<10x20xf32>
177  %r = linalg.fill ins(%f0 : f32) outs(%t : tensor<10x20xf32>) -> tensor<10x20xf32>
178  // expected-error @+1 {{operand #0 of ReturnLike op does not satisfy destination passing style}}
179  return %r : tensor<10x20xf32>
180}
181
182// -----
183
184func.func @main() -> tensor<4xi32> {
185  %r = scf.execute_region -> tensor<4xi32> {
186    %A = arith.constant dense<[1, 2, 3, 4]> : tensor<4xi32>
187    // expected-error @+1 {{operand #0 of ReturnLike op does not satisfy destination passing style}}
188    scf.yield %A: tensor<4xi32>
189  }
190
191  // expected-error @+1 {{operand #0 of ReturnLike op does not satisfy destination passing style}}
192  return %r: tensor<4xi32>
193}
194
195// -----
196
197func.func @to_memref_op_is_writing(
198    %t1: tensor<?xf32> {bufferization.writable = true}, %idx1: index,
199    %idx2: index, %idx3: index, %v1: vector<5xf32>) -> (vector<5xf32>, vector<5xf32>) {
200  // This is a RaW conflict because to_memref is an inplace write and %t1 is
201  // read further down. This will likely have to change with partial
202  // bufferization.
203
204  // expected-error @+1 {{input IR has RaW conflict}}
205  %0 = bufferization.to_memref %t1 : memref<?xf32>
206
207  // Read from both.
208  %cst = arith.constant 0.0 : f32
209  %r1 = vector.transfer_read %t1[%idx3], %cst : tensor<?xf32>, vector<5xf32>
210  %r2 = vector.transfer_read %0[%idx3], %cst : memref<?xf32>, vector<5xf32>
211
212  return %r1, %r2 : vector<5xf32>, vector<5xf32>
213}
214
215// -----
216
217// expected-error @+2 {{op was not bufferized}}
218// expected-error @+1 {{cannot bufferize bodiless function that returns a tensor}}
219func.func private @foo(%t : tensor<?xf32>) -> (f32, tensor<?xf32>, f32)
220
221func.func @call_to_unknown_tensor_returning_func(%t : tensor<?xf32>) {
222  call @foo(%t) : (tensor<?xf32>) -> (f32, tensor<?xf32>, f32)
223  return
224}
225
226// -----
227
228func.func @foo(%t : tensor<5xf32>) -> (tensor<5xf32>) {
229  %0 = linalg.init_tensor [5] : tensor<5xf32>
230  // expected-error @+1 {{operand #0 of ReturnLike op does not satisfy destination passing style}}
231  return %0 : tensor<5xf32>
232}
233
234// Note: This function is not analyzed because there was an error in the
235// previous one.
236func.func @call_to_func_returning_non_equiv_tensor(%t : tensor<5xf32>) {
237  call @foo(%t) : (tensor<5xf32>) -> (tensor<5xf32>)
238  return
239}
240
241// -----
242
243func.func @destination_passing_style_dominance_test_1(%cst : f32, %idx : index,
244                                                 %idx2 : index) -> f32 {
245  %0 = scf.execute_region -> tensor<?xf32> {
246    %1 = linalg.init_tensor [%idx] : tensor<?xf32>
247    // expected-error @+1 {{operand #0 of ReturnLike op does not satisfy destination passing style}}
248    scf.yield %1 : tensor<?xf32>
249  }
250  %2 = tensor.insert %cst into %0[%idx] : tensor<?xf32>
251  %r = tensor.extract %2[%idx2] : tensor<?xf32>
252  return %r : f32
253}
254
255// -----
256
257func.func @destination_passing_style_dominance_test_2(%cst : f32, %idx : index,
258                                                 %idx2 : index) -> f32 {
259  %1 = linalg.init_tensor [%idx] : tensor<?xf32>
260
261  %0 = scf.execute_region -> tensor<?xf32> {
262    // This YieldOp is in destination-passing style, thus no error.
263    scf.yield %1 : tensor<?xf32>
264  }
265  %2 = tensor.insert %cst into %0[%idx] : tensor<?xf32>
266  %r = tensor.extract %2[%idx2] : tensor<?xf32>
267  return %r : f32
268}
269