1// RUN: mlir-opt %s -split-input-file -verify-diagnostics
2
3func.func @reduce_op_args_num_mismatch(%shape : !shape.shape, %init : !shape.size) {
4  // expected-error@+1 {{ReduceOp body is expected to have 3 arguments}}
5  %num_elements = shape.reduce(%shape, %init) : !shape.shape -> !shape.size {
6    ^bb0(%index: index, %dim: !shape.size):
7      shape.yield %dim : !shape.size
8  }
9  return
10}
11
12// -----
13
14func.func @reduce_op_arg0_wrong_type(%shape : !shape.shape, %init : !shape.size) {
15  // expected-error@+1 {{argument 0 of ReduceOp body is expected to be of IndexType}}
16  %num_elements = shape.reduce(%shape, %init) : !shape.shape -> !shape.size {
17    ^bb0(%index: f32, %dim: !shape.size, %acc: !shape.size):
18      %new_acc = "shape.add"(%acc, %dim)
19          : (!shape.size, !shape.size) -> !shape.size
20      shape.yield %new_acc : !shape.size
21  }
22  return
23}
24
25// -----
26
27func.func @reduce_op_arg1_wrong_type(%shape : !shape.shape, %init : !shape.size) {
28  // expected-error@+1 {{argument 1 of ReduceOp body is expected to be of SizeType if the ReduceOp operates on a ShapeType}}
29  %num_elements = shape.reduce(%shape, %init) : !shape.shape -> !shape.size {
30    ^bb0(%index: index, %dim: f32, %lci: !shape.size):
31      shape.yield
32  }
33  return
34}
35
36// -----
37
38func.func @reduce_op_arg1_wrong_type(%shape : tensor<?xindex>, %init : index) {
39  // expected-error@+1 {{argument 1 of ReduceOp body is expected to be of IndexType if the ReduceOp operates on an extent tensor}}
40  %num_elements = shape.reduce(%shape, %init) : tensor<?xindex> -> index {
41    ^bb0(%index: index, %dim: f32, %lci: index):
42      shape.yield
43  }
44  return
45}
46
47// -----
48
49func.func @reduce_op_init_type_mismatch(%shape : !shape.shape, %init : f32) {
50  // expected-error@+1 {{type mismatch between argument 2 of ReduceOp body and initial value 0}}
51  %num_elements = shape.reduce(%shape, %init) : !shape.shape -> f32 {
52    ^bb0(%index: index, %dim: !shape.size, %lci: !shape.size):
53      shape.yield
54  }
55  return
56}
57
58// -----
59
60func.func @yield_op_args_num_mismatch(%shape : !shape.shape, %init : !shape.size) {
61  // expected-error@+3 {{number of operands does not match number of results of its parent}}
62  %num_elements = shape.reduce(%shape, %init) : !shape.shape -> !shape.size {
63    ^bb0(%index: index, %dim: !shape.size, %lci: !shape.size):
64      shape.yield %dim, %dim : !shape.size, !shape.size
65  }
66  return
67}
68
69// -----
70
71func.func @yield_op_type_mismatch(%shape : !shape.shape, %init : !shape.size) {
72  // expected-error@+4 {{types mismatch between yield op and its parent}}
73  %num_elements = shape.reduce(%shape, %init) : !shape.shape -> !shape.size {
74    ^bb0(%index: index, %dim: !shape.size, %lci: !shape.size):
75      %c0 = arith.constant 1 : index
76      shape.yield %c0 : index
77  }
78  return
79}
80
81// -----
82
83func.func @assuming_all_op_too_few_operands() {
84  // expected-error@+1 {{no operands specified}}
85  %w0 = shape.assuming_all
86  return
87}
88
89// -----
90
91func.func @shape_of(%value_arg : !shape.value_shape,
92               %shaped_arg : tensor<?x3x4xf32>) {
93  // expected-error@+1 {{if at least one of the operands can hold error values then the result must be of type `shape` to propagate them}}
94  %0 = shape.shape_of %value_arg : !shape.value_shape -> tensor<?xindex>
95  return
96}
97
98// -----
99
100func.func @shape_of_incompatible_return_types(%value_arg : tensor<1x2xindex>) {
101  // expected-error@+1 {{'shape.shape_of' op inferred type(s) 'tensor<2xindex>' are incompatible with return type(s) of operation 'tensor<3xindex>'}}
102  %0 = shape.shape_of %value_arg : tensor<1x2xindex> -> tensor<3xindex>
103  return
104}
105
106// -----
107
108func.func @rank(%arg : !shape.shape) {
109  // expected-error@+1 {{if at least one of the operands can hold error values then the result must be of type `size` to propagate them}}
110  %0 = shape.rank %arg : !shape.shape -> index
111  return
112}
113
114// -----
115
116func.func @get_extent(%arg : tensor<?xindex>) -> index {
117  %c0 = shape.const_size 0
118  // expected-error@+1 {{if at least one of the operands can hold error values then the result must be of type `size` to propagate them}}
119  %result = shape.get_extent %arg, %c0 : tensor<?xindex>, !shape.size -> index
120  return %result : index
121}
122
123// -----
124
125func.func @mul(%lhs : !shape.size, %rhs : index) -> index {
126  // expected-error@+1 {{if at least one of the operands can hold error values then the result must be of type `size` to propagate them}}
127  %result = shape.mul %lhs, %rhs : !shape.size, index -> index
128  return %result : index
129}
130
131// -----
132
133func.func @num_elements(%arg : !shape.shape) -> index {
134  // expected-error@+1 {{if at least one of the operands can hold error values then the result must be of type `size` to propagate them}}
135  %result = shape.num_elements %arg : !shape.shape -> index
136  return %result : index
137}
138
139// -----
140
141func.func @add(%lhs : !shape.size, %rhs : index) -> index {
142  // expected-error@+1 {{if at least one of the operands can hold error values then the result must be of type `size` to propagate them}}
143  %result = shape.add %lhs, %rhs : !shape.size, index -> index
144  return %result : index
145}
146
147// -----
148
149func.func @broadcast(%arg0 : !shape.shape, %arg1 : !shape.shape) -> tensor<?xindex> {
150  // expected-error@+1 {{if at least one of the operands can hold error values then the result must be of type `shape` to propagate them}}
151  %result = shape.broadcast %arg0, %arg1
152      : !shape.shape, !shape.shape -> tensor<?xindex>
153  return %result : tensor<?xindex>
154}
155
156
157// -----
158
159func.func @broadcast(%arg0 : !shape.shape, %arg1 : tensor<?xindex>) -> tensor<?xindex> {
160  // expected-error@+1 {{if at least one of the operands can hold error values then the result must be of type `shape` to propagate them}}
161  %result = shape.broadcast %arg0, %arg1
162      : !shape.shape, tensor<?xindex> -> tensor<?xindex>
163  return %result : tensor<?xindex>
164}
165
166// -----
167
168// Test using an unsupported shape.lib attribute type.
169
170// expected-error@+1 {{only SymbolRefAttr allowed in shape.lib attribute array}}
171module attributes {shape.lib = [@shape_lib, "shape_lib"]} {
172
173shape.function_library @shape_lib {
174  // Test shape function that returns the shape of input arg as result shape.
175  func.func @same_result_shape(%arg: !shape.value_shape) -> !shape.shape {
176    %0 = shape.shape_of %arg : !shape.value_shape -> !shape.shape
177    return %0 : !shape.shape
178  }
179} mapping {
180  test.same_operand_result_type = @same_result_shape
181}
182
183}
184
185// -----
186
187// Test that duplicate op to shape function mappings are flagged, this uses
188// the same library twice for easy overlap.
189
190// expected-error@+1 {{only one op to shape mapping allowed}}
191module attributes {shape.lib = [@shape_lib, @shape_lib]} {
192
193shape.function_library @shape_lib {
194  // Test shape function that returns the shape of input arg as result shape.
195  func.func @same_result_shape(%arg: !shape.value_shape) -> !shape.shape {
196    %0 = shape.shape_of %arg : !shape.value_shape -> !shape.shape
197    return %0 : !shape.shape
198  }
199} mapping {
200  test.same_operand_result_type = @same_result_shape
201}
202
203}
204
205// -----
206
207// Test that duplicate op to shape function mappings are flagged (this is
208// more an invariant of using the dictionary attribute here than anything
209// specific to function library op).
210
211module attributes {shape.lib = [@shape_lib]} {
212
213shape.function_library @shape_lib {
214  // Test shape function that returns the shape of input arg as result shape.
215  func.func @same_result_shape(%arg: !shape.value_shape) -> !shape.shape {
216    %0 = shape.shape_of %arg : !shape.value_shape -> !shape.shape
217    return %0 : !shape.shape
218  }
219} mapping {
220  // expected-error @+2 {{duplicate key}}
221  test.same_operand_result_type = @same_result_shape,
222  test.same_operand_result_type = @same_result_shape
223}
224
225}
226
227// -----
228
229// Test that op referred to by shape lib is a shape function library.
230
231// expected-error@+1 {{required to be shape function library}}
232module attributes {shape.lib = @fn} {
233
234func.func @fn(%arg: !shape.value_shape) -> !shape.shape {
235  %0 = shape.shape_of %arg : !shape.value_shape -> !shape.shape
236  return %0 : !shape.shape
237}
238
239}
240
241// -----
242
243// Test that op referred to by shape lib is a shape function library.
244
245func.func @fn(%arg: !shape.value_shape) -> !shape.shape {
246  // expected-error@+1 {{SymbolTable}}
247  %0 = shape.shape_of %arg {shape.lib = @fn} : !shape.value_shape -> !shape.shape
248  return %0 : !shape.shape
249}
250
251// -----
252
253// Test that shape function library is defined.
254
255// expected-error@+1 {{@fn not found}}
256module attributes {shape.lib = @fn} { }
257
258// -----
259
260func.func @fn(%arg: !shape.shape) -> !shape.witness {
261  // expected-error@+1 {{required at least 2 input shapes}}
262  %0 = shape.cstr_broadcastable %arg : !shape.shape
263  return %0 : !shape.witness
264}
265
266// -----
267
268// Test that type inference flags the wrong return type.
269
270func.func @const_shape() {
271  // expected-error@+1 {{'tensor<3xindex>' are incompatible with return type(s) of operation 'tensor<2xindex>'}}
272  %0 = shape.const_shape [4, 5, 6] : tensor<2xindex>
273  return
274}
275