1// RUN: mlir-opt -allow-unregistered-dialect -split-input-file %s -verify-diagnostics
2
3func.func @dma_start_not_enough_operands() {
4  // expected-error@+1 {{expected at least 4 operands}}
5  "memref.dma_start"() : () -> ()
6}
7
8// -----
9
10func.func @dma_no_src_memref(%m : f32, %tag : f32, %c0 : index) {
11  // expected-error@+1 {{expected source to be of memref type}}
12  memref.dma_start %m[%c0], %m[%c0], %c0, %tag[%c0] : f32, f32, f32
13}
14
15// -----
16
17func.func @dma_start_not_enough_operands_for_src(
18    %src: memref<2x2x2xf32>, %idx: index) {
19  // expected-error@+1 {{expected at least 7 operands}}
20  "memref.dma_start"(%src, %idx, %idx, %idx) : (memref<2x2x2xf32>, index, index, index) -> ()
21}
22
23// -----
24
25func.func @dma_start_src_index_wrong_type(
26    %src: memref<2x2xf32>, %idx: index, %dst: memref<2xf32,1>,
27    %tag: memref<i32,2>, %flt: f32) {
28  // expected-error@+1 {{expected source indices to be of index type}}
29  "memref.dma_start"(%src, %idx, %flt, %dst, %idx, %tag, %idx)
30      : (memref<2x2xf32>, index, f32, memref<2xf32,1>, index, memref<i32,2>, index) -> ()
31}
32
33// -----
34
35func.func @dma_no_dst_memref(%m : f32, %tag : f32, %c0 : index) {
36  %mref = memref.alloc() : memref<8 x f32>
37  // expected-error@+1 {{expected destination to be of memref type}}
38  memref.dma_start %mref[%c0], %m[%c0], %c0, %tag[%c0] : memref<8 x f32>, f32, f32
39}
40
41// -----
42
43func.func @dma_start_not_enough_operands_for_dst(
44    %src: memref<2x2xf32>, %idx: index, %dst: memref<2xf32,1>,
45    %tag: memref<i32,2>) {
46  // expected-error@+1 {{expected at least 7 operands}}
47  "memref.dma_start"(%src, %idx, %idx, %dst, %idx, %idx)
48      : (memref<2x2xf32>, index, index, memref<2xf32,1>, index, index) -> ()
49}
50
51// -----
52
53func.func @dma_start_dst_index_wrong_type(
54    %src: memref<2x2xf32>, %idx: index, %dst: memref<2xf32,1>,
55    %tag: memref<i32,2>, %flt: f32) {
56  // expected-error@+1 {{expected destination indices to be of index type}}
57  "memref.dma_start"(%src, %idx, %idx, %dst, %flt, %tag, %idx)
58      : (memref<2x2xf32>, index, index, memref<2xf32,1>, f32, memref<i32,2>, index) -> ()
59}
60
61// -----
62
63func.func @dma_start_dst_index_wrong_type(
64    %src: memref<2x2xf32>, %idx: index, %dst: memref<2xf32,1>,
65    %tag: memref<i32,2>, %flt: f32) {
66  // expected-error@+1 {{expected num elements to be of index type}}
67  "memref.dma_start"(%src, %idx, %idx, %dst, %idx, %flt, %tag)
68      : (memref<2x2xf32>, index, index, memref<2xf32,1>, index, f32, memref<i32,2>) -> ()
69}
70
71// -----
72
73func.func @dma_no_tag_memref(%tag : f32, %c0 : index) {
74  %mref = memref.alloc() : memref<8 x f32>
75  // expected-error@+1 {{expected tag to be of memref type}}
76  memref.dma_start %mref[%c0], %mref[%c0], %c0, %tag[%c0] : memref<8 x f32>, memref<8 x f32>, f32
77}
78
79// -----
80
81func.func @dma_start_not_enough_operands_for_tag(
82    %src: memref<2x2xf32>, %idx: index, %dst: memref<2xf32,1>,
83    %tag: memref<2xi32,2>) {
84  // expected-error@+1 {{expected at least 8 operands}}
85  "memref.dma_start"(%src, %idx, %idx, %dst, %idx, %idx, %tag)
86      : (memref<2x2xf32>, index, index, memref<2xf32,1>, index, index, memref<2xi32,2>) -> ()
87}
88
89// -----
90
91func.func @dma_start_dst_index_wrong_type(
92    %src: memref<2x2xf32>, %idx: index, %dst: memref<2xf32,1>,
93    %tag: memref<2xi32,2>, %flt: f32) {
94  // expected-error@+1 {{expected tag indices to be of index type}}
95  "memref.dma_start"(%src, %idx, %idx, %dst, %idx, %idx, %tag, %flt)
96      : (memref<2x2xf32>, index, index, memref<2xf32,1>, index, index, memref<2xi32,2>, f32) -> ()
97}
98
99// -----
100
101func.func @dma_start_too_many_operands(
102    %src: memref<2x2xf32>, %idx: index, %dst: memref<2xf32,1>,
103    %tag: memref<i32,2>) {
104  // expected-error@+1 {{incorrect number of operands}}
105  "memref.dma_start"(%src, %idx, %idx, %dst, %idx, %idx, %tag, %idx, %idx, %idx)
106      : (memref<2x2xf32>, index, index, memref<2xf32,1>, index, index, memref<i32,2>, index, index, index) -> ()
107}
108
109
110// -----
111
112func.func @dma_start_wrong_stride_type(
113    %src: memref<2x2xf32>, %idx: index, %dst: memref<2xf32,1>,
114    %tag: memref<i32,2>, %flt: f32) {
115  // expected-error@+1 {{expected stride and num elements per stride to be of type index}}
116  "memref.dma_start"(%src, %idx, %idx, %dst, %idx, %idx, %tag, %idx, %flt)
117      : (memref<2x2xf32>, index, index, memref<2xf32,1>, index, index, memref<i32,2>, index, f32) -> ()
118}
119
120// -----
121
122func.func @dma_wait_wrong_index_type(%tag : memref<2x2xi32>, %idx: index, %flt: index) {
123  // expected-error@+1 {{expected tagIndices to have the same number of elements as the tagMemRef rank, expected 2, but got 1}}
124  "memref.dma_wait"(%tag, %flt, %idx) : (memref<2x2xi32>, index, index) -> ()
125  return
126}
127
128// -----
129
130func.func @transpose_not_permutation(%v : memref<?x?xf32, affine_map<(i, j)[off, M]->(off + M * i + j)>>) {
131  // expected-error @+1 {{expected a permutation map}}
132  memref.transpose %v (i, j) -> (i, i) : memref<?x?xf32, affine_map<(i, j)[off, M]->(off + M * i + j)>> to memref<?x?xf32, affine_map<(i, j)[off, M]->(off + M * i + j)>>
133}
134
135// -----
136
137func.func @transpose_bad_rank(%v : memref<?x?xf32, affine_map<(i, j)[off, M]->(off + M * i + j)>>) {
138  // expected-error @+1 {{expected a permutation map of same rank as the input}}
139  memref.transpose %v (i) -> (i) : memref<?x?xf32, affine_map<(i, j)[off, M]->(off + M * i + j)>> to memref<?x?xf32, affine_map<(i, j)[off, M]->(off + M * i + j)>>
140}
141
142// -----
143
144func.func @transpose_wrong_type(%v : memref<?x?xf32, affine_map<(i, j)[off, M]->(off + M * i + j)>>) {
145  // expected-error @+1 {{output type 'memref<?x?xf32, affine_map<(d0, d1)[s0, s1] -> (d0 * s1 + s0 + d1)>>' does not match transposed input type 'memref<?x?xf32, affine_map<(d0, d1)[s0, s1] -> (d0 * s1 + s0 + d1)>>'}}
146  memref.transpose %v (i, j) -> (j, i) : memref<?x?xf32, affine_map<(i, j)[off, M]->(off + M * i + j)>> to memref<?x?xf32, affine_map<(i, j)[off, M]->(off + M * i + j)>>
147}
148
149// -----
150
151func.func @memref_reinterpret_cast_too_many_offsets(%in: memref<?xf32>) {
152  // expected-error @+1 {{expected 1 offset values}}
153  %out = memref.reinterpret_cast %in to
154           offset: [0, 0], sizes: [10, 10], strides: [10, 1]
155           : memref<?xf32> to memref<10x10xf32, offset: 0, strides: [10, 1]>
156  return
157}
158
159// -----
160
161func.func @memref_reinterpret_cast_incompatible_element_types(%in: memref<*xf32>) {
162  // expected-error @+1 {{different element types specified}}
163  %out = memref.reinterpret_cast %in to
164           offset: [0], sizes: [10], strides: [1]
165         : memref<*xf32> to memref<10xi32, offset: 0, strides: [1]>
166  return
167}
168
169// -----
170
171func.func @memref_reinterpret_cast_incompatible_memory_space(%in: memref<*xf32>) {
172  // expected-error @+1 {{different memory spaces specified}}
173  %out = memref.reinterpret_cast %in to
174           offset: [0], sizes: [10], strides: [1]
175         : memref<*xf32> to memref<10xi32, offset: 0, strides: [1], 2>
176  return
177}
178
179// -----
180
181func.func @memref_reinterpret_cast_offset_mismatch(%in: memref<?xf32>) {
182  // expected-error @+1 {{expected result type with offset = 2 instead of 1}}
183  %out = memref.reinterpret_cast %in to
184           offset: [1], sizes: [10], strides: [1]
185         : memref<?xf32> to memref<10xf32, offset: 2, strides: [1]>
186  return
187}
188
189// -----
190
191func.func @memref_reinterpret_cast_size_mismatch(%in: memref<*xf32>) {
192  // expected-error @+1 {{expected result type with size = 10 instead of 1 in dim = 0}}
193  %out = memref.reinterpret_cast %in to
194           offset: [0], sizes: [10], strides: [1]
195         : memref<*xf32> to memref<1xf32, offset: 0, strides: [1]>
196  return
197}
198
199// -----
200
201func.func @memref_reinterpret_cast_offset_mismatch(%in: memref<?xf32>) {
202  // expected-error @+1 {{expected result type with stride = 2 instead of 1 in dim = 0}}
203  %out = memref.reinterpret_cast %in to
204           offset: [2], sizes: [10], strides: [2]
205         : memref<?xf32> to memref<10xf32, offset: 2, strides: [1]>
206  return
207}
208
209// -----
210
211func.func @memref_reinterpret_cast_no_map_but_offset(%in: memref<?xf32>) {
212  // expected-error @+1 {{expected result type with offset = 0 instead of 2}}
213  %out = memref.reinterpret_cast %in to offset: [2], sizes: [10], strides: [1]
214         : memref<?xf32> to memref<10xf32>
215  return
216}
217
218// -----
219
220func.func @memref_reinterpret_cast_no_map_but_stride(%in: memref<?xf32>) {
221  // expected-error @+1 {{expected result type with stride = 10 instead of 1 in dim = 0}}
222  %out = memref.reinterpret_cast %in to offset: [0], sizes: [10], strides: [10]
223         : memref<?xf32> to memref<10xf32>
224  return
225}
226
227// -----
228
229func.func @memref_reinterpret_cast_no_map_but_strides(%in: memref<?x?xf32>) {
230  // expected-error @+1 {{expected result type with stride = 42 instead of 10 in dim = 0}}
231  %out = memref.reinterpret_cast %in to
232           offset: [0], sizes: [9, 10], strides: [42, 1]
233         : memref<?x?xf32> to memref<9x10xf32>
234  return
235}
236
237// -----
238
239func.func @memref_reinterpret_cast_non_strided_layout(%in: memref<?x?xf32>) {
240  // expected-error @+1 {{expected result type to have strided layout but found 'memref<9x10xf32, affine_map<(d0, d1) -> (d0)>>}}
241  %out = memref.reinterpret_cast %in to
242           offset: [0], sizes: [9, 10], strides: [42, 1]
243         : memref<?x?xf32> to memref<9x10xf32, affine_map<(d0, d1) -> (d0)>>
244  return
245}
246
247// -----
248
249func.func @memref_reshape_element_type_mismatch(
250       %buf: memref<*xf32>, %shape: memref<1xi32>) {
251  // expected-error @+1 {{element types of source and destination memref types should be the same}}
252  memref.reshape %buf(%shape) : (memref<*xf32>, memref<1xi32>) -> memref<?xi32>
253}
254
255// -----
256
257func.func @memref_reshape_dst_ranked_shape_unranked(
258       %buf: memref<*xf32>, %shape: memref<?xi32>) {
259  // expected-error @+1 {{cannot use shape operand with dynamic length to reshape to statically-ranked memref type}}
260  memref.reshape %buf(%shape) : (memref<*xf32>, memref<?xi32>) -> memref<?xf32>
261}
262
263// -----
264
265func.func @memref_reshape_dst_shape_rank_mismatch(
266       %buf: memref<*xf32>, %shape: memref<1xi32>) {
267  // expected-error @+1 {{length of shape operand differs from the result's memref rank}}
268  memref.reshape %buf(%shape)
269    : (memref<*xf32>, memref<1xi32>) -> memref<?x?xf32>
270}
271
272// -----
273
274func.func @memref_reshape_src_affine_map_is_not_identity(
275        %buf: memref<4x4xf32, offset: 0, strides: [3, 2]>,
276        %shape: memref<1xi32>) {
277  // expected-error @+1 {{source memref type should have identity affine map}}
278  memref.reshape %buf(%shape)
279    : (memref<4x4xf32, offset: 0, strides: [3, 2]>, memref<1xi32>)
280    -> memref<8xf32>
281}
282
283// -----
284
285func.func @memref_reshape_result_affine_map_is_not_identity(
286        %buf: memref<4x4xf32>, %shape: memref<1xi32>) {
287  // expected-error @+1 {{result memref type should have identity affine map}}
288  memref.reshape %buf(%shape)
289    : (memref<4x4xf32>, memref<1xi32>) -> memref<8xf32, offset: 0, strides: [2]>
290}
291
292// -----
293
294// expected-error @+1 {{type should be static shaped memref}}
295memref.global @foo : i32
296
297// -----
298
299// expected-error @+1 {{type should be static shaped memref}}
300memref.global @foo : i32 = 5
301
302// -----
303
304// expected-error @+1 {{type should be static shaped memref}}
305memref.global @foo : memref<*xf32>
306
307// -----
308
309// expected-error @+1 {{type should be static shaped memref}}
310memref.global @foo : memref<?x?xf32>
311
312// -----
313
314// expected-error @+1 {{initial value should be a unit or elements attribute}}
315memref.global @foo : memref<2x2xf32>  = "foo"
316
317// -----
318
319// expected-error @+1 {{inferred shape of elements literal ([2]) does not match type ([2, 2])}}
320memref.global @foo : memref<2x2xf32> = dense<[0.0, 1.0]>
321
322// -----
323
324// expected-error @+1 {{expected valid '@'-identifier for symbol name}}
325memref.global "private" "public" @foo : memref<2x2xf32>  = "foo"
326
327// -----
328
329// expected-error @+1 {{expected valid '@'-identifier for symbol name}}
330memref.global constant external @foo : memref<2x2xf32>  = "foo"
331
332// -----
333
334// constant qualifier must be after visibility.
335// expected-error @+1 {{expected valid '@'-identifier for symbol name}}
336memref.global constant "private" @foo : memref<2x2xf32>  = "foo"
337
338
339// -----
340
341// expected-error @+1 {{op visibility expected to be one of ["public", "private", "nested"], but got "priate"}}
342memref.global "priate" constant @memref5 : memref<2xf32>  = uninitialized
343
344// -----
345
346func.func @nonexistent_global_memref() {
347  // expected-error @+1 {{'gv' does not reference a valid global memref}}
348  %0 = memref.get_global @gv : memref<3xf32>
349  return
350}
351
352// -----
353
354func.func private @foo()
355
356func.func @nonexistent_global_memref() {
357  // expected-error @+1 {{'foo' does not reference a valid global memref}}
358  %0 = memref.get_global @foo : memref<3xf32>
359  return
360}
361
362// -----
363
364memref.global @gv : memref<3xi32>
365
366func.func @mismatched_types() {
367  // expected-error @+1 {{result type 'memref<3xf32>' does not match type 'memref<3xi32>' of the global memref @gv}}
368  %0 = memref.get_global @gv : memref<3xf32>
369  return
370}
371
372// -----
373
374// expected-error @+1 {{alignment attribute value 63 is not a power of 2}}
375memref.global "private" @gv : memref<4xf32> = dense<1.0> { alignment = 63 }
376
377// -----
378
379func.func @copy_different_shape(%arg0: memref<2xf32>, %arg1: memref<3xf32>) {
380  // expected-error @+1 {{op requires the same shape for all operands}}
381  memref.copy %arg0, %arg1 : memref<2xf32> to memref<3xf32>
382  return
383}
384
385// -----
386
387func.func @copy_different_eltype(%arg0: memref<2xf32>, %arg1: memref<2xf16>) {
388  // expected-error @+1 {{op requires the same element type for all operands}}
389  memref.copy %arg0, %arg1 : memref<2xf32> to memref<2xf16>
390  return
391}
392
393// -----
394
395func.func @expand_shape(%arg0: memref<f32>) {
396  // expected-error @+1 {{invalid number of reassociation groups: found 1, expected 0}}
397  %0 = memref.expand_shape %arg0 [[0]] : memref<f32> into memref<f32>
398  return
399}
400
401// -----
402
403func.func @expand_shape(%arg0: memref<f32>) {
404  // expected-error @+1 {{rank 0 memrefs can only be extended/collapsed with/from ones}}
405  %0 = memref.expand_shape %arg0 [] : memref<f32> into memref<1x2xf32>
406  return
407}
408
409// -----
410
411func.func @collapse_shape_to_higher_rank(%arg0: memref<f32>) {
412  // expected-error @+1 {{op reassociation index 0 is out of bounds}}
413  %0 = memref.collapse_shape %arg0 [[0]] : memref<f32> into memref<1xf32>
414}
415
416// -----
417
418func.func @expand_shape_to_smaller_rank(%arg0: memref<1xf32>) {
419  // expected-error @+1 {{op reassociation index 0 is out of bounds}}
420  %0 = memref.expand_shape %arg0 [[0]] : memref<1xf32> into memref<f32>
421}
422
423// -----
424
425func.func @expand_shape_invalid_result_layout(
426    %arg0: memref<30x20xf32, offset : 100, strides : [4000, 2]>) {
427  // expected-error @+1 {{expected expanded type to be 'memref<2x15x20xf32, affine_map<(d0, d1, d2) -> (d0 * 60000 + d1 * 4000 + d2 * 2 + 100)>>' but found 'memref<2x15x20xf32, affine_map<(d0, d1, d2) -> (d0 * 5000 + d1 * 4000 + d2 * 2 + 100)>>'}}
428  %0 = memref.expand_shape %arg0 [[0, 1], [2]] :
429      memref<30x20xf32, offset : 100, strides : [4000, 2]>
430      into memref<2x15x20xf32, offset : 100, strides : [5000, 4000, 2]>
431}
432
433// -----
434
435func.func @collapse_shape_mismatch_indices_num(%arg0: memref<?x?x?xf32>) {
436  // expected-error @+1 {{invalid number of reassociation groups: found 1, expected 2}}
437  %0 = memref.collapse_shape %arg0 [[0, 1]] :
438    memref<?x?x?xf32> into memref<?x?xf32, offset: 0, strides: [?, 1]>
439}
440
441// -----
442
443func.func @collapse_shape_invalid_reassociation(%arg0: memref<?x?x?xf32>) {
444  // expected-error @+1 {{reassociation indices must be contiguous}}
445  %0 = memref.collapse_shape %arg0 [[0, 1], [1, 2]] :
446    memref<?x?x?xf32> into memref<?x?xf32, offset: 0, strides: [?, 1]>
447}
448
449// -----
450
451func.func @collapse_shape_reshaping_non_contiguous(
452    %arg0: memref<3x4x5xf32, offset: 0, strides: [270, 50, 10]>) {
453  // expected-error @+1 {{invalid source layout map or collapsing non-contiguous dims}}
454  %0 = memref.collapse_shape %arg0 [[0, 1], [2]] :
455      memref<3x4x5xf32, offset: 0, strides: [270, 50, 10]>
456      into memref<12x5xf32, offset: 0, strides: [50, 1]>
457  return
458}
459
460// -----
461
462func.func @collapse_shape_wrong_collapsed_type(%arg0: memref<?x?x?xf32>) {
463  // expected-error @+1 {{expected collapsed type to be 'memref<?x?xf32>' but found 'memref<?x?xf32, affine_map<(d0, d1)[s0] -> (d0 * s0 + d1)>>'}}
464  %0 = memref.collapse_shape %arg0 [[0, 1], [2]] :
465    memref<?x?x?xf32> into memref<?x?xf32, affine_map<(d0, d1)[s0] -> (d0 * s0 + d1)>>
466}
467
468// -----
469
470func.func @expand_shape_illegal_dynamic_memref
471  (%arg0: memref<?x?x?xf32>) -> memref<?x?x?x4x?xf32> {
472  // expected-error @+1 {{at most one dimension in a reassociation group may be dynamic}}
473  %0 = memref.expand_shape %arg0 [[0], [1], [2, 3, 4]]
474      : memref<?x?x?xf32> into memref<?x?x?x4x?xf32>
475  return %0 : memref<?x?x?x4x?xf32>
476}
477
478// -----
479
480func.func @expand_shape_illegal_static_memref
481  (%arg0: memref<2x3x20xf32>) -> memref<2x3x2x4x5xf32> {
482  // expected-error @+1 {{collapsed dim size (20) must equal reassociation group size (40)}}
483  %0 = memref.expand_shape %arg0 [[0], [1], [2, 3, 4]]
484      : memref<2x3x20xf32> into memref<2x3x2x4x5xf32>
485  return %0 : memref<2x3x2x4x5xf32>
486}
487
488// -----
489
490func.func @collapse_shape_illegal_static_memref
491  (%arg0: memref<2x3x2x4x5xf32>) -> memref<2x3x20xf32> {
492  // expected-error @+1 {{collapsed dim size (20) must equal reassociation group size (40)}}
493  %0 = memref.collapse_shape %arg0 [[0], [1], [2, 3, 4]]
494      : memref<2x3x2x4x5xf32> into memref<2x3x20xf32>
495  return %0 : memref<2x3x20xf32>
496}
497
498// -----
499
500func.func @expand_shape_illegal_mixed_memref(%arg0 : memref<?x?xf32>)
501    -> memref<?x4x5xf32> {
502  // expected-error @+1 {{collapsed dim (1) must be dynamic if and only if reassociation group is dynamic}}
503  %0 = memref.expand_shape %arg0 [[0, 1], [2]]
504      : memref<?x?xf32> into memref<?x4x5xf32>
505  return %0 : memref<?x4x5xf32>
506}
507
508// -----
509
510func.func @expand_shape_illegal_mixed_memref_2(%arg0 : memref<?x?xf32>)
511    -> memref<?x4x5xf32> {
512  // expected-error @+1 {{collapsed dim (1) must be dynamic if and only if reassociation group is dynamic}}
513  %0 = memref.expand_shape %arg0 [[0], [1, 2]]
514      : memref<?x?xf32> into memref<?x4x5xf32>
515  return %0 : memref<?x4x5xf32>
516}
517
518// -----
519
520func.func @expand_shape_invalid_static_dim_size(%arg0 : memref<?x21xf32>)
521    -> memref<?x4x5xf32> {
522  // expected-error @+1 {{collapsed dim size (21) must equal reassociation group size (20)}}
523  %0 = memref.expand_shape %arg0 [[0], [1, 2]]
524      : memref<?x21xf32> into memref<?x4x5xf32>
525  return %0 : memref<?x4x5xf32>
526}
527
528// -----
529
530func.func @collapse_shape_illegal_mixed_memref(%arg0 : memref<?x4x5xf32>)
531    -> memref<?x?xf32> {
532  // expected-error @+1 {{collapsed dim (1) must be dynamic if and only if reassociation group is dynamic}}
533  %0 = memref.collapse_shape %arg0 [[0, 1], [2]]
534      : memref<?x4x5xf32> into memref<?x?xf32>
535  return %0 : memref<?x?xf32>
536}
537
538// -----
539
540func.func @collapse_shape_illegal_mixed_memref_2(%arg0 : memref<?x4x5xf32>)
541    -> memref<?x?xf32> {
542  // expected-error @+1 {{collapsed dim (1) must be dynamic if and only if reassociation group is dynamic}}
543  %0 = memref.collapse_shape %arg0 [[0], [1, 2]]
544      : memref<?x4x5xf32> into memref<?x?xf32>
545  return %0 : memref<?x?xf32>
546}
547
548// -----
549
550func.func @invalid_view(%arg0 : index, %arg1 : index, %arg2 : index) {
551  %0 = memref.alloc() : memref<2048xi8>
552  // expected-error@+1 {{expected SSA operand}}
553  %1 = memref.view %0[][%arg0, %arg1]
554    : memref<2048xi8> to memref<?x?xf32>
555  return
556}
557
558// -----
559
560func.func @invalid_view(%arg0 : index, %arg1 : index, %arg2 : index) {
561  %0 = memref.alloc() : memref<2048xi8, affine_map<(d0) -> (d0 floordiv 8, d0 mod 8)>>
562  // expected-error@+1 {{unsupported map for base memref type}}
563  %1 = memref.view %0[%arg2][%arg0, %arg1]
564    : memref<2048xi8, affine_map<(d0) -> (d0 floordiv 8, d0 mod 8)>> to
565      memref<?x?xf32, affine_map<(d0, d1)[s0] -> (d0 * 4 + d1 + s0)>>
566  return
567}
568
569// -----
570
571func.func @invalid_view(%arg0 : index, %arg1 : index, %arg2 : index) {
572  %0 = memref.alloc() : memref<2048xi8>
573  // expected-error@+1 {{unsupported map for result memref type}}
574  %1 = memref.view %0[%arg2][%arg0, %arg1]
575    : memref<2048xi8> to memref<?x?xf32, affine_map<(d0, d1)[s0] -> (d0, d1, s0)>>
576  return
577}
578
579// -----
580
581func.func @invalid_view(%arg0 : index, %arg1 : index, %arg2 : index) {
582  %0 = memref.alloc() : memref<2048xi8, 2>
583  // expected-error@+1 {{different memory spaces}}
584  %1 = memref.view %0[%arg2][%arg0, %arg1] :  memref<2048xi8, 2> to memref<?x?xf32, 1>
585  return
586}
587
588// -----
589
590func.func @invalid_view(%arg0 : index, %arg1 : index, %arg2 : index) {
591  %0 = memref.alloc() : memref<2048xi8>
592  // expected-error@+1 {{incorrect number of size operands for type}}
593  %1 = memref.view %0[%arg2][%arg0]
594    : memref<2048xi8> to memref<?x?xf32>
595  return
596}
597
598// -----
599
600func.func @invalid_subview(%arg0 : index, %arg1 : index, %arg2 : index) {
601  %0 = memref.alloc() : memref<8x16x4xf32>
602  // expected-error@+1 {{expected mixed offsets rank to match mixed sizes rank (2 vs 3) so the rank of the result type is well-formed}}
603  %1 = memref.subview %0[0, 0][2, 2, 2][1, 1, 1]
604    : memref<8x16x4xf32> to memref<8x16x4xf32>
605  return
606}
607
608// -----
609
610func.func @invalid_subview(%arg0 : index, %arg1 : index, %arg2 : index) {
611  %0 = memref.alloc() : memref<8x16x4xf32>
612  // expected-error@+1 {{expected mixed sizes rank to match mixed strides rank (3 vs 2) so the rank of the result type is well-formed}}
613  %1 = memref.subview %0[0, 0, 0][2, 2, 2][1, 1]
614    : memref<8x16x4xf32> to memref<8x16x4xf32>
615  return
616}
617
618// -----
619
620func.func @invalid_subview(%arg0 : index, %arg1 : index, %arg2 : index) {
621  %0 = memref.alloc() : memref<8x16x4xf32>
622  // expected-error@+1 {{expected mixed sizes rank to match mixed strides rank (3 vs 2) so the rank of the result type is well-formed}}
623  %1 = memref.reinterpret_cast %0 to offset: [0], sizes: [2, 2, 2], strides:[1, 1]
624    : memref<8x16x4xf32> to memref<8x16x4xf32>
625  return
626}
627
628// -----
629
630func.func @invalid_subview(%arg0 : index, %arg1 : index, %arg2 : index) {
631  %0 = memref.alloc() : memref<8x16x4xf32, offset: 0, strides: [64, 4, 1], 2>
632  // expected-error@+1 {{different memory spaces}}
633  %1 = memref.subview %0[0, 0, 0][%arg2, %arg2, %arg2][1, 1, 1]
634    : memref<8x16x4xf32, offset: 0, strides: [64, 4, 1], 2> to
635      memref<8x?x4xf32, affine_map<(d0, d1, d2)[s0] -> (d0 * s0 + d1 * 4 + d2)>>
636  return
637}
638
639// -----
640
641func.func @invalid_subview(%arg0 : index, %arg1 : index, %arg2 : index) {
642  %0 = memref.alloc() : memref<8x16x4xf32, affine_map<(d0, d1, d2) -> (d0 + d1, d1 + d2, d2)>>
643  // expected-error@+1 {{is not strided}}
644  %1 = memref.subview %0[0, 0, 0][%arg2, %arg2, %arg2][1, 1, 1]
645    : memref<8x16x4xf32, affine_map<(d0, d1, d2) -> (d0 + d1, d1 + d2, d2)>> to
646      memref<8x?x4xf32, offset: 0, strides: [?, 4, 1]>
647  return
648}
649
650// -----
651
652func.func @invalid_subview(%arg0 : index, %arg1 : index, %arg2 : index) {
653  %0 = memref.alloc() : memref<8x16x4xf32>
654  // expected-error@+1 {{expected 3 offset values}}
655  %1 = memref.subview %0[%arg0, %arg1, 0, 0][%arg2, 0, 0, 0][1, 1, 1, 1]
656    : memref<8x16x4xf32> to
657      memref<8x?x4xf32, offset: 0, strides:[?, ?, 4]>
658  return
659}
660
661// -----
662
663func.func @invalid_subview(%arg0 : index, %arg1 : index, %arg2 : index) {
664  %0 = memref.alloc() : memref<8x16x4xf32>
665  // expected-error@+1 {{expected result element type to be 'f32'}}
666  %1 = memref.subview %0[0, 0, 0][8, 16, 4][1, 1, 1]
667    : memref<8x16x4xf32> to
668      memref<8x16x4xi32>
669  return
670}
671
672// -----
673
674func.func @invalid_subview(%arg0 : index, %arg1 : index, %arg2 : index) {
675  %0 = memref.alloc() : memref<8x16x4xf32>
676  // expected-error@+1 {{expected result rank to be smaller or equal to the source rank.}}
677  %1 = memref.subview %0[0, 0, 0][8, 16, 4][1, 1, 1]
678    : memref<8x16x4xf32> to
679      memref<8x16x4x3xi32>
680  return
681}
682
683// -----
684
685func.func @invalid_rank_reducing_subview(%arg0 : index, %arg1 : index, %arg2 : index) {
686  %0 = memref.alloc() : memref<8x16x4xf32>
687  // expected-error@+1 {{expected result type to be 'memref<8x16x4xf32, affine_map<(d0, d1, d2) -> (d0 * 64 + d1 * 4 + d2)>>' or a rank-reduced version. (mismatch of result sizes)}}
688  %1 = memref.subview %0[0, 0, 0][8, 16, 4][1, 1, 1]
689    : memref<8x16x4xf32> to memref<16x4xf32>
690  return
691}
692
693// -----
694
695func.func @invalid_rank_reducing_subview(%arg0 : index, %arg1 : index, %arg2 : index) {
696  %0 = memref.alloc() : memref<8x16x4xf32>
697  // expected-error@+1 {{expected result type to be 'memref<8x16x4xf32, affine_map<(d0, d1, d2) -> (d0 * 64 + d1 * 4 + d2 + 8)>>' or a rank-reduced version. (mismatch of result sizes)}}
698  %1 = memref.subview %0[0, 2, 0][8, 16, 4][1, 1, 1]
699    : memref<8x16x4xf32> to memref<16x4xf32>
700  return
701}
702
703// -----
704
705func.func @invalid_rank_reducing_subview(%arg0 : memref<?x?xf32>, %arg1 : index, %arg2 : index) {
706  // expected-error@+1 {{expected result type to be 'memref<?x1xf32, affine_map<(d0, d1)[s0, s1] -> (d0 * s1 + s0 + d1)>>' or a rank-reduced version. (mismatch of result layout)}}
707  %0 = memref.subview %arg0[0, %arg1][%arg2, 1][1, 1] : memref<?x?xf32> to memref<?xf32>
708  return
709}
710
711// -----
712
713#map0 = affine_map<(d0, d1)[s0] -> (d0 * 16 + d1)>
714
715func.func @subview_bad_offset_1(%arg0: memref<16x16xf32>) {
716  %c0 = arith.constant 0 : index
717  %c8 = arith.constant 8 : index
718  // expected-error @+1 {{expected result type to be 'memref<8x8xf32, affine_map<(d0, d1)[s0] -> (d0 * 16 + s0 + d1)>>' or a rank-reduced version}}
719  %s2 = memref.subview %arg0[%c8, %c8][8, 8][1, 1]  : memref<16x16xf32> to memref<8x8xf32, #map0>
720  return
721}
722
723// -----
724
725#map0 = affine_map<(d0, d1)[s0] -> (d0 * 16 + d1 + 136)>
726
727func.func @subview_bad_offset_2(%arg0: memref<16x16xf32>) {
728  %c0 = arith.constant 0 : index
729  %c8 = arith.constant 8 : index
730  // expected-error @+1 {{expected result type to be 'memref<8x8xf32, affine_map<(d0, d1)[s0] -> (d0 * 16 + s0 + d1)>>' or a rank-reduced version}}
731  %s2 = memref.subview %arg0[%c8, 8][8, 8][1, 1]  : memref<16x16xf32> to memref<8x8xf32, #map0>
732  return
733}
734
735// -----
736
737#map0 = affine_map<(d0, d1)[s0] -> (d0 * 16 + d1 + s0 * 437)>
738
739func.func @subview_bad_offset_3(%arg0: memref<16x16xf32>) {
740  %c0 = arith.constant 0 : index
741  %c8 = arith.constant 8 : index
742  // expected-error @+1 {{expected result type to be 'memref<8x8xf32, affine_map<(d0, d1)[s0] -> (d0 * 16 + s0 + d1)>>' or a rank-reduced version}}
743  %s2 = memref.subview %arg0[%c8, 8][8, 8][1, 1]  : memref<16x16xf32> to memref<8x8xf32, #map0>
744  return
745}
746
747// -----
748
749func.func @invalid_memref_cast(%arg0 : memref<12x4x16xf32, offset:0, strides:[64, 16, 1]>) {
750  // expected-error@+1{{operand type 'memref<12x4x16xf32, affine_map<(d0, d1, d2) -> (d0 * 64 + d1 * 16 + d2)>>' and result type 'memref<12x4x16xf32, affine_map<(d0, d1, d2) -> (d0 * 128 + d1 * 32 + d2 * 2)>>' are cast incompatible}}
751  %0 = memref.cast %arg0 : memref<12x4x16xf32, offset:0, strides:[64, 16, 1]> to memref<12x4x16xf32, offset:0, strides:[128, 32, 2]>
752  return
753}
754
755// -----
756
757func.func @invalid_memref_cast(%arg0 : memref<12x4x16xf32, offset:0, strides:[64, 16, 1]>) {
758  // expected-error@+1{{operand type 'memref<12x4x16xf32, affine_map<(d0, d1, d2) -> (d0 * 64 + d1 * 16 + d2)>>' and result type 'memref<12x4x16xf32, affine_map<(d0, d1, d2) -> (d0 * 64 + d1 * 16 + d2 + 16)>>' are cast incompatible}}
759  %0 = memref.cast %arg0 : memref<12x4x16xf32, offset:0, strides:[64, 16, 1]> to memref<12x4x16xf32, offset:16, strides:[64, 16, 1]>
760  return
761}
762
763// -----
764
765// incompatible element types
766func.func @invalid_memref_cast() {
767  %0 = memref.alloc() : memref<2x5xf32, 0>
768  // expected-error@+1 {{operand type 'memref<2x5xf32>' and result type 'memref<*xi32>' are cast incompatible}}
769  %1 = memref.cast %0 : memref<2x5xf32, 0> to memref<*xi32>
770  return
771}
772
773// -----
774
775func.func @invalid_prefetch_rw(%i : index) {
776  %0 = memref.alloc() : memref<10xf32>
777  // expected-error@+1 {{rw specifier has to be 'read' or 'write'}}
778  memref.prefetch %0[%i], rw, locality<0>, data  : memref<10xf32>
779  return
780}
781
782// -----
783
784func.func @invalid_prefetch_cache_type(%i : index) {
785  %0 = memref.alloc() : memref<10xf32>
786  // expected-error@+1 {{cache type has to be 'data' or 'instr'}}
787  memref.prefetch %0[%i], read, locality<0>, false  : memref<10xf32>
788  return
789}
790
791// -----
792
793func.func @invalid_prefetch_locality_hint(%i : index) {
794  %0 = memref.alloc() : memref<10xf32>
795  // expected-error@+1 {{32-bit signless integer attribute whose minimum value is 0 whose maximum value is 3}}
796  memref.prefetch %0[%i], read, locality<5>, data  : memref<10xf32>
797  return
798}
799
800// -----
801
802// incompatible memory space
803func.func @invalid_memref_cast() {
804  %0 = memref.alloc() : memref<2x5xf32, 0>
805  // expected-error@+1 {{operand type 'memref<2x5xf32>' and result type 'memref<*xf32, 1>' are cast incompatible}}
806  %1 = memref.cast %0 : memref<2x5xf32, 0> to memref<*xf32, 1>
807  return
808}
809
810// -----
811
812// unranked to unranked
813func.func @invalid_memref_cast() {
814  %0 = memref.alloc() : memref<2x5xf32, 0>
815  %1 = memref.cast %0 : memref<2x5xf32, 0> to memref<*xf32, 0>
816  // expected-error@+1 {{operand type 'memref<*xf32>' and result type 'memref<*xf32>' are cast incompatible}}
817  %2 = memref.cast %1 : memref<*xf32, 0> to memref<*xf32, 0>
818  return
819}
820
821// -----
822
823// alignment is not power of 2.
824func.func @assume_alignment(%0: memref<4x4xf16>) {
825  // expected-error@+1 {{alignment must be power of 2}}
826  memref.assume_alignment %0, 12 : memref<4x4xf16>
827  return
828}
829
830// -----
831
832// 0 alignment value.
833func.func @assume_alignment(%0: memref<4x4xf16>) {
834  // expected-error@+1 {{attribute 'alignment' failed to satisfy constraint: 32-bit signless integer attribute whose value is positive}}
835  memref.assume_alignment %0, 0 : memref<4x4xf16>
836  return
837}
838
839// -----
840
841"alloca_without_scoped_alloc_parent"() ({
842  memref.alloca() : memref<1xf32>
843  // expected-error@-1 {{requires an ancestor op with AutomaticAllocationScope trait}}
844  func.return
845}) : () -> ()
846
847// -----
848
849func.func @bad_alloc_wrong_dynamic_dim_count() {
850^bb0:
851  %0 = arith.constant 7 : index
852  // Test alloc with wrong number of dynamic dimensions.
853  // expected-error@+1 {{dimension operand count does not equal memref dynamic dimension count}}
854  %1 = memref.alloc(%0)[%0] : memref<2x4xf32, affine_map<(d0, d1)[s0] -> ((d0 + s0), d1)>, 1>
855  return
856}
857
858// -----
859
860func.func @bad_alloc_wrong_symbol_count() {
861^bb0:
862  %0 = arith.constant 7 : index
863  // Test alloc with wrong number of symbols
864  // expected-error@+1 {{symbol operand count does not equal memref symbol count}}
865  %1 = memref.alloc(%0) : memref<2x?xf32, affine_map<(d0, d1)[s0] -> ((d0 + s0), d1)>, 1>
866  return
867}
868
869// -----
870
871func.func @test_store_zero_results() {
872^bb0:
873  %0 = memref.alloc() : memref<1024x64xf32, affine_map<(d0, d1) -> (d0, d1)>, 1>
874  %1 = arith.constant 0 : index
875  %2 = arith.constant 1 : index
876  %3 = memref.load %0[%1, %2] : memref<1024x64xf32, affine_map<(d0, d1) -> (d0, d1)>, 1>
877  // Test that store returns zero results.
878  %4 = memref.store %3, %0[%1, %2] : memref<1024x64xf32, affine_map<(d0, d1) -> (d0, d1)>, 1> // expected-error {{cannot name an operation with no results}}
879  return
880}
881
882// -----
883
884func.func @test_store_zero_results2(%x: i32, %p: memref<i32>) {
885  "memref.store"(%x,%p) : (i32, memref<i32>) -> i32  // expected-error {{'memref.store' op requires zero results}}
886  return
887}
888
889// -----
890
891func.func @test_alloc_memref_map_rank_mismatch() {
892^bb0:
893  // expected-error@+1 {{memref layout mismatch between rank and affine map: 2 != 1}}
894  %0 = memref.alloc() : memref<1024x64xf32, affine_map<(d0) -> (d0)>, 1>
895  return
896}
897
898// -----
899
900func.func @rank(%0: f32) {
901  // expected-error@+1 {{'memref.rank' op operand #0 must be unranked.memref of any type values or memref of any type values}}
902  "memref.rank"(%0): (f32)->index
903  return
904}
905
906// -----
907
908#map = affine_map<(d0, d1, d2)[s0, s1, s2, s3] -> (s0 + d0 * s1 + d1 * s2 + d2 * s3)>
909func.func @illegal_num_offsets(%arg0 : memref<?x?x?xf32>, %arg1 : index, %arg2 : index) {
910  // expected-error@+1 {{expected 3 offset values}}
911  %0 = memref.subview %arg0[0, 0] [%arg1, %arg2] [1, 1] : memref<?x?x?xf32> to memref<?x?x?xf32, #map>
912}
913
914// -----
915
916func.func @atomic_rmw_idxs_rank_mismatch(%I: memref<16x10xf32>, %i : index, %val : f32) {
917  // expected-error@+1 {{expects the number of subscripts to be equal to memref rank}}
918  %x = memref.atomic_rmw addf %val, %I[%i] : (f32, memref<16x10xf32>) -> f32
919  return
920}
921
922// -----
923
924func.func @atomic_rmw_expects_float(%I: memref<16x10xi32>, %i : index, %val : i32) {
925  // expected-error@+1 {{expects a floating-point type}}
926  %x = memref.atomic_rmw addf %val, %I[%i, %i] : (i32, memref<16x10xi32>) -> i32
927  return
928}
929
930// -----
931
932func.func @atomic_rmw_expects_int(%I: memref<16x10xf32>, %i : index, %val : f32) {
933  // expected-error@+1 {{expects an integer type}}
934  %x = memref.atomic_rmw addi %val, %I[%i, %i] : (f32, memref<16x10xf32>) -> f32
935  return
936}
937
938// -----
939
940func.func @generic_atomic_rmw_wrong_arg_num(%I: memref<10xf32>, %i : index) {
941  // expected-error@+1 {{expected single number of entry block arguments}}
942  %x = memref.generic_atomic_rmw %I[%i] : memref<10xf32> {
943    ^bb0(%arg0 : f32, %arg1 : f32):
944      %c1 = arith.constant 1.0 : f32
945      memref.atomic_yield %c1 : f32
946  }
947  return
948}
949
950// -----
951
952func.func @generic_atomic_rmw_wrong_arg_type(%I: memref<10xf32>, %i : index) {
953  // expected-error@+1 {{expected block argument of the same type result type}}
954  %x = memref.generic_atomic_rmw %I[%i] : memref<10xf32> {
955    ^bb0(%old_value : i32):
956      %c1 = arith.constant 1.0 : f32
957      memref.atomic_yield %c1 : f32
958  }
959  return
960}
961
962// -----
963
964func.func @generic_atomic_rmw_result_type_mismatch(%I: memref<10xf32>, %i : index) {
965 // expected-error@+1 {{failed to verify that result type matches element type of memref}}
966 %0 = "memref.generic_atomic_rmw"(%I, %i) ({
967    ^bb0(%old_value: f32):
968      %c1 = arith.constant 1.0 : f32
969      memref.atomic_yield %c1 : f32
970    }) : (memref<10xf32>, index) -> i32
971  return
972}
973
974// -----
975
976func.func @generic_atomic_rmw_has_side_effects(%I: memref<10xf32>, %i : index) {
977  // expected-error@+4 {{should contain only operations with no side effects}}
978  %x = memref.generic_atomic_rmw %I[%i] : memref<10xf32> {
979    ^bb0(%old_value : f32):
980      %c1 = arith.constant 1.0 : f32
981      %buf = memref.alloc() : memref<2048xf32>
982      memref.atomic_yield %c1 : f32
983  }
984}
985
986// -----
987
988func.func @atomic_yield_type_mismatch(%I: memref<10xf32>, %i : index) {
989  // expected-error@+4 {{op types mismatch between yield op: 'i32' and its parent: 'f32'}}
990  %x = memref.generic_atomic_rmw %I[%i] : memref<10xf32> {
991    ^bb0(%old_value : f32):
992      %c1 = arith.constant 1 : i32
993      memref.atomic_yield %c1 : i32
994  }
995  return
996}
997