1// RUN: mlir-opt -split-input-file -verify-diagnostics %s | FileCheck %s
2
3//===----------------------------------------------------------------------===//
4// spv.CompositeConstruct
5//===----------------------------------------------------------------------===//
6
7func @composite_construct_vector(%arg0: f32, %arg1: f32, %arg2 : f32) -> vector<3xf32> {
8  // CHECK: spv.CompositeConstruct {{%.*}}, {{%.*}}, {{%.*}} : vector<3xf32>
9  %0 = spv.CompositeConstruct %arg0, %arg1, %arg2 : vector<3xf32>
10  return %0: vector<3xf32>
11}
12
13// -----
14
15func @composite_construct_struct(%arg0: vector<3xf32>, %arg1: !spv.array<4xf32>, %arg2 : !spv.struct<f32>) -> !spv.struct<vector<3xf32>, !spv.array<4xf32>, !spv.struct<f32>> {
16  // CHECK: spv.CompositeConstruct %arg0, %arg1, %arg2 : !spv.struct<vector<3xf32>, !spv.array<4 x f32>, !spv.struct<f32>>
17  %0 = spv.CompositeConstruct %arg0, %arg1, %arg2 : !spv.struct<vector<3xf32>, !spv.array<4xf32>, !spv.struct<f32>>
18  return %0: !spv.struct<vector<3xf32>, !spv.array<4xf32>, !spv.struct<f32>>
19}
20
21// -----
22
23func @composite_construct_empty_struct() -> !spv.struct<> {
24  // CHECK: spv.CompositeConstruct : !spv.struct<>
25  %0 = spv.CompositeConstruct : !spv.struct<>
26  return %0: !spv.struct<>
27}
28
29// -----
30
31func @composite_construct_invalid_num_of_elements(%arg0: f32) -> f32 {
32  // expected-error @+1 {{result type must be a composite type, but provided 'f32'}}
33  %0 = spv.CompositeConstruct %arg0 : f32
34  return %0: f32
35}
36
37// -----
38
39func @composite_construct_invalid_result_type(%arg0: f32, %arg1: f32, %arg2 : f32) -> vector<3xf32> {
40  // expected-error @+1 {{has incorrect number of operands: expected 3, but provided 2}}
41  %0 = spv.CompositeConstruct %arg0, %arg2 : vector<3xf32>
42  return %0: vector<3xf32>
43}
44
45// -----
46
47func @composite_construct_invalid_operand_type(%arg0: f32, %arg1: f32, %arg2 : f32) -> vector<3xi32> {
48  // expected-error @+1 {{operand type mismatch: expected operand type 'i32', but provided 'f32'}}
49  %0 = "spv.CompositeConstruct" (%arg0, %arg1, %arg2) : (f32, f32, f32) -> vector<3xi32>
50  return %0: vector<3xi32>
51}
52
53// -----
54
55//===----------------------------------------------------------------------===//
56// spv.CompositeExtractOp
57//===----------------------------------------------------------------------===//
58
59func @composite_extract_array(%arg0: !spv.array<4xf32>) -> f32 {
60  // CHECK: {{%.*}} = spv.CompositeExtract {{%.*}}[1 : i32] : !spv.array<4 x f32>
61  %0 = spv.CompositeExtract %arg0[1 : i32] : !spv.array<4xf32>
62  return %0: f32
63}
64
65// -----
66
67func @composite_extract_struct(%arg0 : !spv.struct<f32, !spv.array<4xf32>>) -> f32 {
68  // CHECK: {{%.*}} = spv.CompositeExtract {{%.*}}[1 : i32, 2 : i32] : !spv.struct<f32, !spv.array<4 x f32>>
69  %0 = spv.CompositeExtract %arg0[1 : i32, 2 : i32] : !spv.struct<f32, !spv.array<4xf32>>
70  return %0 : f32
71}
72
73// -----
74
75func @composite_extract_vector(%arg0 : vector<4xf32>) -> f32 {
76  // CHECK: {{%.*}} = spv.CompositeExtract {{%.*}}[1 : i32] : vector<4xf32>
77  %0 = spv.CompositeExtract %arg0[1 : i32] : vector<4xf32>
78  return %0 : f32
79}
80
81// -----
82
83func @composite_extract_no_ssa_operand() -> () {
84  // expected-error @+1 {{expected SSA operand}}
85  %0 = spv.CompositeExtract [4 : i32, 1 : i32] : !spv.array<4x!spv.array<4xf32>>
86  return
87}
88
89// -----
90
91func @composite_extract_invalid_index_type_1() -> () {
92  %0 = spv.constant 10 : i32
93  %1 = spv.Variable : !spv.ptr<!spv.array<4x!spv.array<4xf32>>, Function>
94  %2 = spv.Load "Function" %1 ["Volatile"] : !spv.array<4x!spv.array<4xf32>>
95  // expected-error @+1 {{expected non-function type}}
96  %3 = spv.CompositeExtract %2[%0] : !spv.array<4x!spv.array<4xf32>>
97  return
98}
99
100// -----
101
102func @composite_extract_invalid_index_type_2(%arg0 : !spv.array<4x!spv.array<4xf32>>) -> () {
103  // expected-error @+1 {{op attribute 'indices' failed to satisfy constraint: 32-bit integer array attribute}}
104  %0 = spv.CompositeExtract %arg0[1] : !spv.array<4x!spv.array<4xf32>>
105  return
106}
107
108// -----
109
110func @composite_extract_invalid_index_identifier(%arg0 : !spv.array<4x!spv.array<4xf32>>) -> () {
111  // expected-error @+1 {{expected non-function type}}
112  %0 = spv.CompositeExtract %arg0 ]1 : i32) : !spv.array<4x!spv.array<4xf32>>
113  return
114}
115
116// -----
117
118func @composite_extract_2D_array_out_of_bounds_access_1(%arg0: !spv.array<4x!spv.array<4xf32>>) -> () {
119  // expected-error @+1 {{index 4 out of bounds for '!spv.array<4 x !spv.array<4 x f32>>'}}
120  %0 = spv.CompositeExtract %arg0[4 : i32, 1 : i32] : !spv.array<4x!spv.array<4xf32>>
121  return
122}
123
124// -----
125
126func @composite_extract_2D_array_out_of_bounds_access_2(%arg0: !spv.array<4x!spv.array<4xf32>>
127) -> () {
128  // expected-error @+1 {{index 4 out of bounds for '!spv.array<4 x f32>'}}
129  %0 = spv.CompositeExtract %arg0[1 : i32, 4 : i32] : !spv.array<4x!spv.array<4xf32>>
130  return
131}
132
133// -----
134
135func @composite_extract_struct_element_out_of_bounds_access(%arg0 : !spv.struct<f32, !spv.array<4xf32>>) -> () {
136  // expected-error @+1 {{index 2 out of bounds for '!spv.struct<f32, !spv.array<4 x f32>>'}}
137  %0 = spv.CompositeExtract %arg0[2 : i32, 0 : i32] : !spv.struct<f32, !spv.array<4xf32>>
138  return
139}
140
141// -----
142
143func @composite_extract_vector_out_of_bounds_access(%arg0: vector<4xf32>) -> () {
144  // expected-error @+1 {{index 4 out of bounds for 'vector<4xf32>'}}
145  %0 = spv.CompositeExtract %arg0[4 : i32] : vector<4xf32>
146  return
147}
148
149// -----
150
151func @composite_extract_invalid_types_1(%arg0: !spv.array<4x!spv.array<4xf32>>) -> () {
152  // expected-error @+1 {{cannot extract from non-composite type 'f32' with index 3}}
153  %0 = spv.CompositeExtract %arg0[1 : i32, 2 : i32, 3 : i32] : !spv.array<4x!spv.array<4xf32>>
154  return
155}
156
157// -----
158
159func @composite_extract_invalid_types_2(%arg0: f32) -> () {
160  // expected-error @+1 {{cannot extract from non-composite type 'f32' with index 1}}
161  %0 = spv.CompositeExtract %arg0[1 : i32] : f32
162  return
163}
164
165// -----
166
167func @composite_extract_invalid_extracted_type(%arg0: !spv.array<4x!spv.array<4xf32>>) -> () {
168  // expected-error @+1 {{expected at least one index for spv.CompositeExtract}}
169  %0 = spv.CompositeExtract %arg0[] : !spv.array<4x!spv.array<4xf32>>
170  return
171}
172
173// -----
174
175func @composite_extract_result_type_mismatch(%arg0: !spv.array<4xf32>) -> i32 {
176  // expected-error @+1 {{invalid result type: expected 'f32' but provided 'i32'}}
177  %0 = "spv.CompositeExtract"(%arg0) {indices = [2: i32]} : (!spv.array<4xf32>) -> (i32)
178  return %0: i32
179}
180
181// -----
182
183//===----------------------------------------------------------------------===//
184// spv.CompositeInsert
185//===----------------------------------------------------------------------===//
186
187func @composite_insert_array(%arg0: !spv.array<4xf32>, %arg1: f32) -> !spv.array<4xf32> {
188  // CHECK: {{%.*}} = spv.CompositeInsert {{%.*}}, {{%.*}}[1 : i32] : f32 into !spv.array<4 x f32>
189  %0 = spv.CompositeInsert %arg1, %arg0[1 : i32] : f32 into !spv.array<4xf32>
190  return %0: !spv.array<4xf32>
191}
192
193// -----
194
195func @composite_insert_struct(%arg0: !spv.struct<!spv.array<4xf32>, f32>, %arg1: !spv.array<4xf32>) -> !spv.struct<!spv.array<4xf32>, f32> {
196  // CHECK: {{%.*}} = spv.CompositeInsert {{%.*}}, {{%.*}}[0 : i32] : !spv.array<4 x f32> into !spv.struct<!spv.array<4 x f32>, f32>
197  %0 = spv.CompositeInsert %arg1, %arg0[0 : i32] : !spv.array<4xf32> into !spv.struct<!spv.array<4xf32>, f32>
198  return %0: !spv.struct<!spv.array<4xf32>, f32>
199}
200
201// -----
202
203func @composite_insert_no_indices(%arg0: !spv.array<4xf32>, %arg1: f32) -> !spv.array<4xf32> {
204  // expected-error @+1 {{expected at least one index}}
205  %0 = spv.CompositeInsert %arg1, %arg0[] : f32 into !spv.array<4xf32>
206  return %0: !spv.array<4xf32>
207}
208
209// -----
210
211func @composite_insert_out_of_bounds(%arg0: !spv.array<4xf32>, %arg1: f32) -> !spv.array<4xf32> {
212  // expected-error @+1 {{index 4 out of bounds}}
213  %0 = spv.CompositeInsert %arg1, %arg0[4 : i32] : f32 into !spv.array<4xf32>
214  return %0: !spv.array<4xf32>
215}
216
217// -----
218
219func @composite_insert_invalid_object_type(%arg0: !spv.array<4xf32>, %arg1: f64) -> !spv.array<4xf32> {
220  // expected-error @+1 {{object operand type should be 'f32', but found 'f64'}}
221  %0 = spv.CompositeInsert %arg1, %arg0[3 : i32] : f64 into !spv.array<4xf32>
222  return %0: !spv.array<4xf32>
223}
224
225// -----
226
227func @composite_insert_invalid_result_type(%arg0: !spv.array<4xf32>, %arg1 : f32) -> !spv.array<4xf64> {
228  // expected-error @+1 {{result type should be the same as the composite type, but found '!spv.array<4 x f32>' vs '!spv.array<4 x f64>'}}
229  %0 = "spv.CompositeInsert"(%arg1, %arg0) {indices = [0: i32]} : (f32, !spv.array<4xf32>) -> !spv.array<4xf64>
230  return %0: !spv.array<4xf64>
231}
232