1// RUN: mlir-opt <%s -split-input-file -verify-diagnostics
2
3func.func @dim(%arg : tensor<1x?xf32>) {
4  %c2 = arith.constant 2 : index
5  tensor.dim %arg, %c2 : tensor<1x?xf32> // expected-error {{'tensor.dim' op index is out of range}}
6  return
7}
8
9// -----
10
11func.func @tensor.cast_mismatching_constants(%arg0: tensor<1xf32>) {
12  // expected-error@+1 {{operand type 'tensor<1xf32>' and result type 'tensor<2xf32>' are cast incompatible}}
13  %0 = tensor.cast %arg0 : tensor<1xf32> to tensor<2xf32>
14  return
15}
16
17// -----
18
19func.func @extract_too_many_indices(%arg0: tensor<?xf32>) {
20  // expected-error@+1 {{incorrect number of indices for extract_element}}
21  %0 = tensor.extract %arg0[] : tensor<?xf32>
22  return
23}
24
25// -----
26
27func.func @insert_too_many_indices(%arg0: f32, %arg1: tensor<?xf32>) {
28  // expected-error@+1 {{incorrect number of indices}}
29  %0 = tensor.insert %arg0 into %arg1[] : tensor<?xf32>
30  return
31}
32
33// -----
34
35func.func @tensor.from_elements_wrong_result_type() {
36  // expected-error@+2 {{'result' must be statically shaped tensor of any type values, but got 'tensor<*xi32>'}}
37  %c0 = arith.constant 0 : i32
38  %0 = tensor.from_elements %c0 : tensor<*xi32>
39  return
40}
41
42// -----
43
44func.func @tensor.from_elements_wrong_elements_count() {
45  // expected-error@+2 {{1 operands present, but expected 2}}
46  %c0 = arith.constant 0 : index
47  %0 = tensor.from_elements %c0 : tensor<2xindex>
48  return
49}
50
51// -----
52
53func.func @tensor.generate(%m : index)
54    -> tensor<?x3x?xf32> {
55  // expected-error @+1 {{must have as many index operands as dynamic extents in the result type}}
56  %tnsr = tensor.generate %m {
57    ^bb0(%i : index, %j : index, %k : index):
58      %elem = arith.constant 8.0 : f32
59      tensor.yield %elem : f32
60  } : tensor<?x3x?xf32>
61  return %tnsr : tensor<?x3x?xf32>
62}
63
64// -----
65
66func.func @tensor.generate(%m : index, %n : index)
67    -> tensor<?x3x?xf32> {
68  // expected-error @+1 {{must have one body argument per input dimension}}
69  %tnsr = tensor.generate %m, %n {
70    ^bb0(%i : index, %j : index):
71      %elem = arith.constant 8.0 : f32
72      tensor.yield %elem : f32
73  } : tensor<?x3x?xf32>
74  return %tnsr : tensor<?x3x?xf32>
75}
76
77// -----
78
79func.func @tensor.generate(%m : index, %n : index)
80    -> tensor<?x3x?xf32> {
81  // expected-error @+1 {{all body arguments must be index}}
82  %tnsr = tensor.generate %m, %n {
83    ^bb0(%i : index, %j : index, %k : i64):
84      %elem = arith.constant 8.0 : f32
85      tensor.yield %elem : f32
86  } : tensor<?x3x?xf32>
87  return %tnsr : tensor<?x3x?xf32>
88}
89
90// -----
91
92func.func @tensor.generate(%m : index, %n : index)
93    -> tensor<?x3x?xf32> {
94  // expected-error @+4 {{'func.return' op expects parent op 'func.func'}}
95  %tnsr = tensor.generate %m, %n {
96    ^bb0(%i : index, %j : index, %k : index):
97      %elem = arith.constant 8.0 : f32
98      func.return %elem : f32
99  } : tensor<?x3x?xf32>
100  return %tnsr : tensor<?x3x?xf32>
101}
102
103// -----
104
105func.func @tensor.generate(%m : index, %n : index)
106    -> tensor<?x3x?xf32> {
107  // expected-error @+1 {{body must be terminated with a `yield` operation of the tensor element type}}
108  %tnsr = tensor.generate %m, %n {
109    ^bb0(%i : index, %j : index, %k : index):
110      %elem = arith.constant 8 : i32
111      tensor.yield %elem : i32
112  } : tensor<?x3x?xf32>
113  return %tnsr : tensor<?x3x?xf32>
114}
115// -----
116
117func.func @tensor.reshape_element_type_mismatch(
118       %buf: tensor<*xf32>, %shape: tensor<1xi32>) {
119  // expected-error @+1 {{element types of source and destination tensor types should be the same}}
120  tensor.reshape %buf(%shape) : (tensor<*xf32>, tensor<1xi32>) -> tensor<?xi32>
121}
122
123// -----
124
125func.func @tensor.reshape_dst_ranked_shape_unranked(
126       %buf: tensor<*xf32>, %shape: tensor<?xi32>) {
127  // expected-error @+1 {{cannot use shape operand with dynamic length to reshape to statically-ranked tensor type}}
128  tensor.reshape %buf(%shape) : (tensor<*xf32>, tensor<?xi32>) -> tensor<?xf32>
129}
130
131// -----
132
133func.func @tensor.reshape_dst_shape_rank_mismatch(
134       %buf: tensor<*xf32>, %shape: tensor<1xi32>) {
135  // expected-error @+1 {{length of shape operand differs from the result's tensor rank}}
136  tensor.reshape %buf(%shape)
137    : (tensor<*xf32>, tensor<1xi32>) -> tensor<?x?xf32>
138}
139
140// -----
141
142func.func @tensor.reshape_num_elements_mismatch(
143       %buf: tensor<1xf32>, %shape: tensor<1xi32>) {
144  // expected-error @+1 {{source and destination tensor should have the same number of elements}}
145  tensor.reshape %buf(%shape)
146    : (tensor<1xf32>, tensor<1xi32>) -> tensor<10xf32>
147}
148
149// -----
150
151func.func @extract_slice_wrong_result_rank(%t: tensor<?xf32>, %idx : index) {
152  // expected-error @+1 {{expected rank to be smaller or equal to the other rank.}}
153  %0 = tensor.extract_slice %t[0][4][1] : tensor<?xf32> to tensor<?x?xf32>
154
155  return
156}
157
158// -----
159
160func.func @extract_slice_wrong_result_rank(%t: tensor<?xf32>, %idx : index) {
161  // expected-error @+1 {{expected element type to be 'f32'}}
162  %0 = tensor.extract_slice %t[0][4][1] : tensor<?xf32> to tensor<4xi8>
163
164  return
165}
166
167// -----
168
169func.func @extract_slice_wrong_static_type(%t: tensor<8x16x4xf32>, %idx : index) {
170  // expected-error @+1 {{expected type to be 'tensor<?x4x4xf32>' or a rank-reduced version. (size mismatch)}}
171  %0 = tensor.extract_slice %t[0, 0, 0][%idx, 4, 4][1, 1, 1]
172    : tensor<8x16x4xf32> to tensor<4x4x4xf32>
173
174  return
175}
176
177// -----
178
179func.func @extract_slice_wrong_dynamic_type(%t: tensor<8x16x4xf32>, %idx : index) {
180  // expected-error @+1 {{expected type to be 'tensor<4x4x4xf32>' or a rank-reduced version. (size mismatch)}}
181  %0 = tensor.extract_slice %t[0, 2, 0][4, 4, 4][1, 1, 1]
182    : tensor<8x16x4xf32> to tensor<?x4x4xf32>
183
184  return
185}
186
187// -----
188
189func.func @insert_slice_wrong_result_rank(%t1: tensor<?xf32>, %t2: tensor<?x?xf32>, %idx : index) {
190  // expected-error @+1 {{expected rank to be smaller or equal to the other rank.}}
191  %0 = tensor.insert_slice %t2 into %t1[0][4][1] : tensor<?x?xf32> into tensor<?xf32>
192
193  return
194}
195
196// -----
197
198func.func @insert_slice_wrong_result_rank(%t1: tensor<4xi8>, %t2: tensor<?xf32>, %idx : index) {
199  // expected-error @+1 {{expected element type to be 'f32'}}
200  %0 = tensor.insert_slice %t1 into %t2[0][4][1] : tensor<4xi8> into tensor<?xf32>
201
202  return
203}
204
205// -----
206
207func.func @insert_slice_wrong_static_type(%t1: tensor<4x4x4xf32>, %t2: tensor<8x16x4xf32>, %idx : index) {
208  // expected-error @+1 {{expected type to be 'tensor<?x4x4xf32>' or a rank-reduced version. (size mismatch)}}
209  %0 = tensor.insert_slice %t1 into %t2[0, 0, 0][%idx, 4, 4][1, 1, 1]
210    : tensor<4x4x4xf32> into tensor<8x16x4xf32>
211
212  return
213}
214
215// -----
216
217func.func @insert_slice_wrong_dynamic_type(%t1: tensor<?x4x4xf32>, %t2: tensor<8x16x4xf32>, %idx : index) {
218  // expected-error @+1 {{expected type to be 'tensor<4x4x4xf32>' or a rank-reduced version. (size mismatch)}}
219  %0 = tensor.insert_slice %t1 into %t2[0, 2, 0][4, 4, 4][1, 1, 1]
220    : tensor<?x4x4xf32> into tensor<8x16x4xf32>
221
222  return
223}
224
225// -----
226
227func.func @illegal_expanding_reshape_dynamic_tensor
228  (%arg0: tensor<?x?x?xf32>) -> tensor<?x?x?x4x?xf32> {
229  // expected-error @+1 {{invalid to have a single dimension (2) expanded into multiple dynamic dims (2,4)}}
230  %0 = tensor.expand_shape %arg0 [[0], [1], [2, 3, 4]]
231      : tensor<?x?x?xf32> into tensor<?x?x?x4x?xf32>
232  return %0 : tensor<?x?x?x4x?xf32>
233}
234
235// -----
236
237
238func.func @illegal_expanding_reshape_static_tensor
239    (%arg0: tensor<2x3x20xf32>) -> tensor<2x3x2x4x5xf32> {
240  // expected-error @+1 {{expected dimension 2 of collapsed type to be static value of 40}}
241  %0 = tensor.expand_shape %arg0 [[0], [1], [2, 3, 4]]
242      : tensor<2x3x20xf32> into tensor<2x3x2x4x5xf32>
243  return %0 : tensor<2x3x2x4x5xf32>
244}
245
246// -----
247
248func.func @illegal_collapsing_reshape_static_tensor
249    (%arg0: tensor<2x3x2x4x5xf32>) -> tensor<2x3x20xf32> {
250  // expected-error @+1 {{expected dimension 2 of collapsed type to be static value of 40}}
251  %0 = tensor.collapse_shape %arg0 [[0], [1], [2, 3, 4]]
252      : tensor<2x3x2x4x5xf32> into tensor<2x3x20xf32>
253  return %0 : tensor<2x3x20xf32>
254}
255
256// -----
257
258func.func @illegal_expanding_reshape_mixed_tensor(%arg0 : tensor<?x?xf32>)
259    -> tensor<?x4x5xf32> {
260  // expected-error @+1 {{expected dimension 1 of collapsed type to be static value of 5}}
261  %0 = tensor.expand_shape %arg0 [[0, 1], [2]]
262      : tensor<?x?xf32> into tensor<?x4x5xf32>
263  return %0 : tensor<?x4x5xf32>
264}
265
266// -----
267
268func.func @illegal_expanding_reshape_mixed_tensor_2(%arg0 : tensor<?x?xf32>)
269    -> tensor<?x4x5xf32> {
270  // expected-error @+1 {{expected dimension 1 of collapsed type to be static value of 20}}
271  %0 = tensor.expand_shape %arg0 [[0], [1, 2]]
272      : tensor<?x?xf32> into tensor<?x4x5xf32>
273  return %0 : tensor<?x4x5xf32>
274}
275
276// -----
277
278func.func @illegal_collapsing_reshape_mixed_tensor(%arg0 : tensor<?x4x5xf32>) -> tensor<?x?xf32> {
279  // expected-error @+1 {{expected dimension 1 of collapsed type to be static value of 5}}
280  %0 = tensor.collapse_shape %arg0 [[0, 1], [2]]
281      : tensor<?x4x5xf32> into tensor<?x?xf32>
282  return %0 : tensor<?x?xf32>
283}
284
285// -----
286
287func.func @illegal_collapsing_reshape_mixed_tensor_2(%arg0 : tensor<?x4x5xf32>)
288    -> tensor<?x?xf32> {
289  // expected-error @+1 {{expected dimension 1 of collapsed type to be static value of 20}}
290  %0 = tensor.collapse_shape %arg0 [[0], [1, 2]]
291      : tensor<?x4x5xf32> into tensor<?x?xf32>
292  return %0 : tensor<?x?xf32>
293}
294
295// -----
296
297func.func @rank(%0: f32) {
298  // expected-error@+1 {{'tensor.rank' op operand #0 must be tensor of any type values}}
299  "tensor.rank"(%0): (f32)->index
300  return
301}
302
303// -----
304
305func.func @illegal_num_offsets(%arg0 : tensor<?x?x?xf32>, %arg1 : index, %arg2 : index) {
306  // expected-error@+1 {{expected 3 offset values}}
307  %0 = tensor.extract_slice %arg0[0, 0] [%arg1, %arg2] [1, 1] : tensor<?x?x?xf32> to tensor<?x?x?xf32>
308  return
309}
310
311// -----
312
313func.func @illegal_num_offsets(%arg0 : tensor<?x?xf32>, %arg1 : tensor<?x?x?xf32>,
314    %arg2 : index, %arg3 : index) {
315  // expected-error@+1 {{expected 3 offset values}}
316  %0 = tensor.insert_slice %arg0 into %arg1[0, 0] [%arg2, %arg3] [1, 1] : tensor<?x?xf32> into tensor<?x?x?xf32>
317  return
318}
319
320// -----
321
322
323func.func @pad_result_type(%arg0: tensor<?x2x3x4xi32>, %arg1: index, %arg2: i32) -> tensor<?x?x?x8xf32> {
324  // expected-error @+1 {{specified type 'tensor<?x?x?x8xf32>' does not match the inferred type 'tensor<?x?x?x9xi32>}}
325  %0 = tensor.pad %arg0 low[1, %arg1, 2, 2] high[1, 2, %arg1, 3] {
326  ^bb0(%arg3: index, %arg4: index):
327    tensor.yield %arg2 : i32
328  } : tensor<?x2x3x4xi32> to tensor<?x?x?x8xf32>
329  return %0 : tensor<?x?x?x8xf32>
330}
331
332// -----
333
334func.func @pad_number_of_block_args(%arg0: tensor<?x4xi32>, %arg1: i32) -> tensor<?x9xi32> {
335  // expected-error @+1 {{expected the block to have 2 arguments}}
336  %0 = tensor.pad %arg0 low[1, 2] high[2, 3] {
337  ^bb0(%arg2: index, %arg3: index, %arg4: index):
338    tensor.yield %arg1 : i32
339  } : tensor<?x4xi32> to tensor<?x9xi32>
340  return %0 : tensor<?x9xi32>
341}
342
343// -----
344
345func.func @pad_block_args(%arg0: tensor<?x4xi32>, %arg1: i32) -> tensor<?x9xi32> {
346  // expected-error @+1 {{op expected block argument 1 to be an index}}
347  %0 = tensor.pad %arg0 low[1, 2] high[2, 3] {
348  ^bb0(%arg2: i32, %arg3: i32):
349    tensor.yield %arg1 : i32
350  } : tensor<?x4xi32> to tensor<?x9xi32>
351  return %0 : tensor<?x9xi32>
352}
353
354// -----
355
356func.func @pad_yield_type(%arg0: tensor<?x4xi32>, %arg1: i8) -> tensor<?x9xi32> {
357  // expected-error @+1 {{op expected yield type to match shape element type}}
358  %0 = tensor.pad %arg0 low[1, 2] high[2, 3] {
359  ^bb0(%arg2: index, %arg3: index):
360    tensor.yield %arg1 : i8
361  } : tensor<?x4xi32> to tensor<?x9xi32>
362  return %0 : tensor<?x9xi32>
363}
364
365// -----
366
367func.func @invalid_splat(%v : f32) {
368  // expected-error@+1 {{invalid kind of type specified}}
369  tensor.splat %v : memref<8xf32>
370  return
371}
372
373// -----
374
375func.func @invalid_splat(%v : vector<8xf32>) {
376  // expected-error@+1 {{must be integer/index/float type}}
377  %w = tensor.splat %v : tensor<8xvector<8xf32>>
378  return
379}
380