Debian Codegraph @

test/val/val_tensor_test.cpp

raw
1 // Copyright (c) 2023-2025 Arm Ltd.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "gmock/gmock.h"
16 #include "test/unit_spirv.h"
17 #include "test/val/val_fixtures.h"
18
19 namespace spvtools {
20 namespace val {
21 namespace {
22
23 using ::testing::ContainsRegex;
24 using ::testing::HasSubstr;
25
26 using ValidateTensor = spvtest::ValidateBase<std::string>;
27
28 constexpr spv_target_env SPVENV = SPV_ENV_VULKAN_1_3;
29
30 std::string GenerateModule(const std::string& body) {
31 const std::string header = R"(
32 OpCapability Shader
33 OpCapability VulkanMemoryModel
34 OpCapability Int8
35 OpCapability TensorsARM
36 OpExtension "SPV_ARM_tensors"
37 OpMemoryModel Logical Vulkan
38 OpEntryPoint GLCompute %fnep "main"
39 OpExecutionMode %fnep LocalSize 1 1 1
40 OpDecorate %tensor_var DescriptorSet 0
41 OpDecorate %tensor_var Binding 0
42 OpDecorate %tensor_var_float_unranked DescriptorSet 0
43 OpDecorate %tensor_var_float_unranked Binding 1
44 OpDecorate %tensor_var_spec_rank DescriptorSet 0
45 OpDecorate %tensor_var_spec_rank Binding 2
46 %void = OpTypeVoid
47 %uint = OpTypeInt 32 0
48 %float = OpTypeFloat 32
49 %fnty = OpTypeFunction %void
50 %uint_vec4 = OpTypeVector %uint 4
51 %uint_0 = OpConstant %uint 0
52 %uint_0_spec = OpSpecConstant %uint 0
53 %uint_1 = OpConstant %uint 1
54 %uint_2 = OpConstant %uint 2
55 %uint_3 = OpConstant %uint 3
56 %uint_4 = OpConstant %uint 4
57 %uint_42 = OpConstant %uint 42
58 %uint_1_bis = OpConstant %uint 1
59 %float_1 = OpConstant %float 1
60 %uint_vec4_1_1_1_1 = OpConstantComposite %uint_vec4 %uint_1 %uint_1 %uint_1 %uint_1
61 %uint_arr1 = OpTypeArray %uint %uint_1
62 %uint_arr2 = OpTypeArray %uint %uint_2
63 %uint_arr3 = OpTypeArray %uint %uint_3
64 %uint_arr4 = OpTypeArray %uint %uint_4
65 %float_arr4 = OpTypeArray %float %uint_4
66 %uint_arr4_1_1_1_1 = OpConstantComposite %uint_arr4 %uint_1 %uint_1 %uint_1 %uint_1
67 %uint_arr4_0_0_0_0_spec = OpSpecConstantComposite %uint_arr4 %uint_0_spec %uint_0_spec %uint_0_spec %uint_0_spec
68 %uint_arr2_1_1 = OpConstantComposite %uint_arr2 %uint_1 %uint_1
69 %float_arr4_1_1_1_1 = OpConstantComposite %float_arr4 %float_1 %float_1 %float_1 %float_1
70 %uint_arr4_1_1_0_1 = OpConstantComposite %uint_arr4 %uint_1 %uint_1 %uint_0 %uint_1
71 %uint_ptr_Private = OpTypePointer Private %uint
72 %uint_arr4_ptr_Private = OpTypePointer Private %uint_arr4
73 %uint_var_1 = OpVariable %uint_ptr_Private Private %uint_1
74 %var_uint_arr4_1_1_1_1 = OpVariable %uint_arr4_ptr_Private Private %uint_arr4_1_1_1_1
75 %tensor_uint_4 = OpTypeTensorARM %uint %uint_4
76 %tensor_uint_spec = OpTypeTensorARM %uint %uint_0_spec
77 %tensor_float = OpTypeTensorARM %float
78 %tensor_uint_4_ptr_UniformConstant = OpTypePointer UniformConstant %tensor_uint_4
79 %tensor_var = OpVariable %tensor_uint_4_ptr_UniformConstant UniformConstant
80 %tensor_float_ptr_UniformConstant = OpTypePointer UniformConstant %tensor_float
81 %tensor_var_float_unranked = OpVariable %tensor_float_ptr_UniformConstant UniformConstant
82 %tensor_uint_spec_ptr_UniformConstant = OpTypePointer UniformConstant %tensor_uint_spec
83 %tensor_var_spec_rank = OpVariable %tensor_uint_spec_ptr_UniformConstant UniformConstant
84 )";
85 const std::string footer = R"(
86 %fnep = OpFunction %void None %fnty
87 %label = OpLabel
88 OpReturn
89 OpFunctionEnd
90 )";
91 return header + body + footer;
92 }
93
94 //
95 // Type tests
96 //
97
98 TEST_F(ValidateTensor, ValidTypeElementTypeOnly) {
99 const std::string src = R"(
100 %test_type = OpTypeTensorARM %uint
101 )";
102 std::string spvasm = GenerateModule(src);
103 CompileSuccessfully(spvasm, SPVENV);
104 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
105 }
106
107 TEST_F(ValidateTensor, InvalidTypeTensorElementTypeNotScalar) {
108 const std::string src = R"(
109 %test_type = OpTypeTensorARM %void
110 )";
111 std::string spvasm = GenerateModule(src);
112 CompileSuccessfully(spvasm, SPVENV);
113 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
114 EXPECT_THAT(getDiagnosticString(),
115 ContainsRegex("OpTypeTensorARM Element Type <id> "
116 "'.*' is not a scalar type.*"));
117 }
118
119 TEST_F(ValidateTensor, InvalidTypeDuplicateElementTypeTensorType) {
120 const std::string src = R"(
121 %typeA = OpTypeTensorARM %uint
122 %typeB = OpTypeTensorARM %uint
123 )";
124 std::string spvasm = GenerateModule(src);
125 CompileSuccessfully(spvasm, SPVENV);
126 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
127 EXPECT_THAT(
128 getDiagnosticString(),
129 HasSubstr("Duplicate non-aggregate type declarations are not allowed."));
130 }
131
132 TEST_F(ValidateTensor, ValidTypeElementTypeAndRank) {
133 const std::string src = R"(
134 %test_type = OpTypeTensorARM %uint %uint_1
135 )";
136 std::string spvasm = GenerateModule(src);
137 CompileSuccessfully(spvasm, SPVENV);
138 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
139 }
140
141 TEST_F(ValidateTensor, ValidTypeElementTypeAndRankUsingSpecConstant) {
142 const std::string src = R"(
143 %rank_spec = OpSpecConstant %uint 0
144 %test_type = OpTypeTensorARM %uint %rank_spec
145 )";
146 std::string spvasm = GenerateModule(src);
147 CompileSuccessfully(spvasm, SPVENV);
148 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
149 }
150
151 TEST_F(ValidateTensor, InvalidTypeDuplicateRankedTensorType) {
152 const std::string src = R"(
153 %typeA = OpTypeTensorARM %uint %uint_1
154 %typeB = OpTypeTensorARM %uint %uint_1
155 )";
156 std::string spvasm = GenerateModule(src);
157 CompileSuccessfully(spvasm, SPVENV);
158 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
159 EXPECT_THAT(
160 getDiagnosticString(),
161 HasSubstr("Duplicate non-aggregate type declarations are not allowed."));
162 }
163
164 TEST_F(ValidateTensor, ValidTypeEquivalentRankedTensorType) {
165 const std::string src = R"(
166 %typeA = OpTypeTensorARM %uint %uint_1
167 %typeB = OpTypeTensorARM %uint %uint_1_bis
168 )";
169 std::string spvasm = GenerateModule(src);
170 CompileSuccessfully(spvasm, SPVENV);
171 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
172 }
173
174 TEST_F(ValidateTensor, InvalidTypeRankNotConstant) {
175 const std::string src = R"(
176 %test_type = OpTypeTensorARM %uint %uint_var_1
177 )";
178 std::string spvasm = GenerateModule(src);
179 CompileSuccessfully(spvasm, SPVENV);
180 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
181 EXPECT_THAT(getDiagnosticString(),
182 ContainsRegex("OpTypeTensorARM Rank <id> '.*' "
183 "is not a constant instruction.*"));
184 }
185
186 TEST_F(ValidateTensor, InvalidTypeRankConstantButNotIntegerType) {
187 const std::string src = R"(
188 %test_type = OpTypeTensorARM %uint %float_1
189 )";
190 std::string spvasm = GenerateModule(src);
191 CompileSuccessfully(spvasm, SPVENV);
192 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
193 EXPECT_THAT(getDiagnosticString(),
194 ContainsRegex("OpTypeTensorARM Rank <id> '.*' "
195 "does not have a scalar integer type.*"));
196 }
197
198 TEST_F(ValidateTensor, InvalidTypeRankConstantIntegerTypeButNotScalar) {
199 const std::string src = R"(
200 %test_type = OpTypeTensorARM %uint %uint_vec4_1_1_1_1
201 )";
202 std::string spvasm = GenerateModule(src);
203 CompileSuccessfully(spvasm, SPVENV);
204 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
205 EXPECT_THAT(getDiagnosticString(),
206 ContainsRegex("OpTypeTensorARM Rank <id> '.*' "
207 "does not have a scalar integer type.*"));
208 }
209
210 TEST_F(ValidateTensor, InvalidTypeRank0) {
211 const std::string src = R"(
212 %test_type = OpTypeTensorARM %uint %uint_0
213 )";
214 std::string spvasm = GenerateModule(src);
215 CompileSuccessfully(spvasm, SPVENV);
216 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
217 EXPECT_THAT(getDiagnosticString(),
218 ContainsRegex("OpTypeTensorARM Rank <id> '.*' must "
219 "define a value greater than 0.*"));
220 }
221
222 TEST_F(ValidateTensor, ValidTypeElementTypeAndRankAndShape) {
223 const std::string src = R"(
224 %test_type = OpTypeTensorARM %uint %uint_4 %uint_arr4_1_1_1_1
225 )";
226 std::string spvasm = GenerateModule(src);
227 CompileSuccessfully(spvasm, SPVENV);
228 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
229 }
230
231 TEST_F(ValidateTensor, ValidTypeElementTypeAndRankAndShapeUsingSpecConstant) {
232 const std::string src = R"(
233 %test_type = OpTypeTensorARM %uint %uint_0_spec %uint_arr4_0_0_0_0_spec
234 )";
235 std::string spvasm = GenerateModule(src);
236 CompileSuccessfully(spvasm, SPVENV);
237 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
238 }
239
240 TEST_F(ValidateTensor, InvalidTypeShapeNotConstant) {
241 const std::string src = R"(
242 %test_type = OpTypeTensorARM %uint %uint_4 %var_uint_arr4_1_1_1_1
243 )";
244 std::string spvasm = GenerateModule(src);
245 CompileSuccessfully(spvasm, SPVENV);
246 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
247 EXPECT_THAT(getDiagnosticString(),
248 ContainsRegex("OpTypeTensorARM Shape <id> '.*' is not "
249 "a constant instruction.*"));
250 }
251
252 TEST_F(ValidateTensor, InvalidTypeShapeConstantButNotArrayOfInteger) {
253 const std::string src = R"(
254 %test_type = OpTypeTensorARM %uint %uint_4 %uint_vec4_1_1_1_1
255 )";
256 std::string spvasm = GenerateModule(src);
257 CompileSuccessfully(spvasm, SPVENV);
258 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
259 EXPECT_THAT(getDiagnosticString(),
260 ContainsRegex(
261 "OpTypeTensorARM Shape <id> '.*' is not "
262 "an array of integer type whose Length is equal to Rank.*"));
263 }
264
265 TEST_F(ValidateTensor, InvalidTypeShapeConstantArrayOfIntegerWrongLength) {
266 const std::string src = R"(
267 %test_type = OpTypeTensorARM %uint %uint_1 %uint_arr4_1_1_1_1
268 )";
269 std::string spvasm = GenerateModule(src);
270 CompileSuccessfully(spvasm, SPVENV);
271 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
272 EXPECT_THAT(getDiagnosticString(),
273 ContainsRegex(
274 "OpTypeTensorARM Shape <id> '.*' is not "
275 "an array of integer type whose Length is equal to Rank.*"));
276 }
277
278 TEST_F(ValidateTensor,
279 InvalidTypeShapeConstantArrayOfIntegerWithZeroConstituent) {
280 const std::string src = R"(
281 %test_type = OpTypeTensorARM %uint %uint_4 %uint_arr4_1_1_0_1
282 )";
283 std::string spvasm = GenerateModule(src);
284 CompileSuccessfully(spvasm, SPVENV);
285 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
286 EXPECT_THAT(
287 getDiagnosticString(),
288 ContainsRegex(
289 "OpTypeTensorARM Shape constituent 2 is not greater than 0.*"));
290 }
291
292 //
293 // Constants tests
294 //
295 TEST_F(ValidateTensor, ValidNullTensorConstantRank1) {
296 const std::string src = R"(
297 %uint_arr1_4 = OpConstantComposite %uint_arr1 %uint_4
298 %ts_uint_r1 = OpTypeTensorARM %uint %uint_1 %uint_arr1_4
299 %cst = OpConstantNull %ts_uint_r1
300 )";
301 std::string spvasm = GenerateModule(src);
302 CompileSuccessfully(spvasm, SPVENV);
303 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
304 }
305
306 TEST_F(ValidateTensor, ValidNullTensorConstantRank4) {
307 const std::string src = R"(
308 %uint_arr4_2_2_2_2 = OpConstantComposite %uint_arr4 %uint_2 %uint_2 %uint_2 %uint_2
309 %ts_uint_r4 = OpTypeTensorARM %uint %uint_4 %uint_arr4_2_2_2_2
310 %cst = OpConstantNull %ts_uint_r4
311 )";
312 std::string spvasm = GenerateModule(src);
313 CompileSuccessfully(spvasm, SPVENV);
314 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
315 }
316
317 TEST_F(ValidateTensor, InvalidNullTensorConstantRankedButNotShaped) {
318 const std::string src = R"(
319 %ts_uint_r1 = OpTypeTensorARM %uint %uint_1
320 %cst = OpConstantNull %ts_uint_r1
321 )";
322 std::string spvasm = GenerateModule(src);
323 CompileSuccessfully(spvasm, SPVENV);
324 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
325 EXPECT_THAT(getDiagnosticString(),
326 ContainsRegex("OpConstantNull Result Type <id> '.*' "
327 "cannot have a null value.*"));
328 }
329
330 TEST_F(ValidateTensor, InvalidNullTensorConstantNotRanked) {
331 const std::string src = R"(
332 %ts_uint_r1 = OpTypeTensorARM %uint
333 %cst = OpConstantNull %ts_uint_r1
334 )";
335 std::string spvasm = GenerateModule(src);
336 CompileSuccessfully(spvasm, SPVENV);
337 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
338 EXPECT_THAT(getDiagnosticString(),
339 ContainsRegex("OpConstantNull Result Type <id> '.*' "
340 "cannot have a null value.*"));
341 }
342
343 TEST_F(ValidateTensor, ValidTensorConstantRank1) {
344 const std::string src = R"(
345 %uint_arr1_4 = OpConstantComposite %uint_arr1 %uint_4
346 %ts_uint_r1 = OpTypeTensorARM %uint %uint_1 %uint_arr1_4
347 %cst = OpConstantComposite %ts_uint_r1 %uint_42 %uint_42 %uint_42 %uint_42
348 )";
349 std::string spvasm = GenerateModule(src);
350 CompileSuccessfully(spvasm, SPVENV);
351 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
352 }
353
354 TEST_F(ValidateTensor, ValidTensorConstantRank1SpecConstant) {
355 const std::string src = R"(
356 %uint_arr1_4 = OpSpecConstantComposite %uint_arr1 %uint_0_spec
357 %ts_uint_r1 = OpTypeTensorARM %uint %uint_0_spec %uint_arr1_4
358 %cst = OpSpecConstantComposite %ts_uint_r1 %uint_0_spec %uint_0_spec %uint_0_spec %uint_0_spec
359 )";
360 std::string spvasm = GenerateModule(src);
361 CompileSuccessfully(spvasm, SPVENV);
362 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
363 }
364
365 TEST_F(ValidateTensor, InvalidTensorConstantRank1NotEnoughConstituents) {
366 const std::string src = R"(
367 %uint_arr1_4 = OpConstantComposite %uint_arr1 %uint_4
368 %ts_uint_r1 = OpTypeTensorARM %uint %uint_1 %uint_arr1_4
369 %cst = OpConstantComposite %ts_uint_r1 %uint_42 %uint_42 %uint_42
370 )";
371 std::string spvasm = GenerateModule(src);
372 CompileSuccessfully(spvasm, SPVENV);
373 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
374 EXPECT_THAT(
375 getDiagnosticString(),
376 ContainsRegex("OpConstantComposite Constituent count does not match the "
377 "shape of Result Type <id> '.*' "
378 "along its outermost dimension, expected 4 but got 3.*"));
379 }
380
381 TEST_F(ValidateTensor, InvalidTensorConstantRank1TooManyConstituents) {
382 const std::string src = R"(
383 %uint_arr1_4 = OpConstantComposite %uint_arr1 %uint_4
384 %ts_uint_r1 = OpTypeTensorARM %uint %uint_1 %uint_arr1_4
385 %cst = OpConstantComposite %ts_uint_r1 %uint_42 %uint_42 %uint_42 %uint_42 %uint_42
386 )";
387 std::string spvasm = GenerateModule(src);
388 CompileSuccessfully(spvasm, SPVENV);
389 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
390 EXPECT_THAT(
391 getDiagnosticString(),
392 ContainsRegex("OpConstantComposite Constituent count does not match the "
393 "shape of Result Type <id> '.*' "
394 "along its outermost dimension, expected 4 but got 5.*"));
395 }
396
397 TEST_F(ValidateTensor,
398 InvalidTensorConstantRank1ConstituentNotConstantOrUndef) {
399 const std::string src = R"(
400 %uint_arr1_4 = OpConstantComposite %uint_arr1 %uint_4
401 %ts_uint_r1 = OpTypeTensorARM %uint %uint_1 %uint_arr1_4
402 %cst = OpConstantComposite %ts_uint_r1 %tensor_var %tensor_var %tensor_var %tensor_var
403 )";
404 std::string spvasm = GenerateModule(src);
405 CompileSuccessfully(spvasm, SPVENV);
406 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
407 EXPECT_THAT(getDiagnosticString(),
408 ContainsRegex("OpConstantComposite Constituent <id> "
409 "'.*' is not a constant or undef.*"));
410 }
411
412 TEST_F(ValidateTensor, InvalidTensorConstantRank1WrongConstituentType) {
413 const std::string src = R"(
414 %uint_arr1_4 = OpConstantComposite %uint_arr1 %uint_4
415 %ts_uint_r1 = OpTypeTensorARM %uint %uint_1 %uint_arr1_4
416 %cst = OpConstantComposite %ts_uint_r1 %uint_42 %uint_42 %float_1 %uint_42
417 )";
418 std::string spvasm = GenerateModule(src);
419 CompileSuccessfully(spvasm, SPVENV);
420 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
421 EXPECT_THAT(
422 getDiagnosticString(),
423 ContainsRegex("OpConstantComposite Constituent <id> '.*' "
424 "type does not match the element type of the tensor "
425 ".*"));
426 }
427
428 TEST_F(ValidateTensor, ValidTensorConstantRank4) {
429 const std::string src = R"(
430 %uint_arr1_2 = OpConstantComposite %uint_arr1 %uint_2
431 %uint_arr2_2_2 = OpConstantComposite %uint_arr2 %uint_2 %uint_2
432 %uint_arr3_2_2_2 = OpConstantComposite %uint_arr3 %uint_2 %uint_2 %uint_2
433 %uint_arr4_2_2_2_2 = OpConstantComposite %uint_arr4 %uint_2 %uint_2 %uint_2 %uint_2
434 %ts_uint_r1 = OpTypeTensorARM %uint %uint_1 %uint_arr1_2
435 %ts_uint_r2 = OpTypeTensorARM %uint %uint_2 %uint_arr2_2_2
436 %ts_uint_r3 = OpTypeTensorARM %uint %uint_3 %uint_arr3_2_2_2
437 %ts_uint_r4 = OpTypeTensorARM %uint %uint_4 %uint_arr4_2_2_2_2
438 %cst_r1 = OpConstantComposite %ts_uint_r1 %uint_42 %uint_42
439 %cst_r2 = OpConstantComposite %ts_uint_r2 %cst_r1 %cst_r1
440 %cst_r3 = OpConstantComposite %ts_uint_r3 %cst_r2 %cst_r2
441 %cst_r4 = OpConstantComposite %ts_uint_r4 %cst_r3 %cst_r3
442 )";
443 std::string spvasm = GenerateModule(src);
444 CompileSuccessfully(spvasm, SPVENV);
445 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
446 }
447
448 TEST_F(ValidateTensor, ValidTensorConstantRank2SpecConstantConstituent) {
449 const std::string src = R"(
450 %uint_arr1_2 = OpSpecConstantComposite %uint_arr1 %uint_0_spec
451 %uint_arr2_2_2 = OpSpecConstantComposite %uint_arr2 %uint_0_spec %uint_0_spec
452 %ts_uint_r1 = OpTypeTensorARM %uint %uint_0_spec %uint_arr1_2
453 %ts_uint_r2 = OpTypeTensorARM %uint %uint_2 %uint_arr2_2_2
454 %cst_r1 = OpSpecConstantComposite %ts_uint_r1 %uint_0_spec %uint_0_spec
455 %cst_r2 = OpSpecConstantComposite %ts_uint_r2 %cst_r1 %cst_r1
456 )";
457 std::string spvasm = GenerateModule(src);
458 CompileSuccessfully(spvasm, SPVENV);
459 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
460 }
461
462 TEST_F(ValidateTensor, InvalidTensorConstantRank2ConstituentNotATensor) {
463 const std::string src = R"(
464 %uint_arr1_2 = OpConstantComposite %uint_arr1 %uint_2
465 %uint_arr2_2_2 = OpConstantComposite %uint_arr2 %uint_2 %uint_2
466 %ts_uint_r1 = OpTypeTensorARM %uint %uint_1 %uint_arr1_2
467 %ts_uint_r2 = OpTypeTensorARM %uint %uint_2 %uint_arr2_2_2
468 %cst_r1 = OpConstantComposite %ts_uint_r1 %uint_42 %uint_42
469 %cst_r2 = OpConstantComposite %ts_uint_r2 %uint_1 %cst_r1
470 )";
471 std::string spvasm = GenerateModule(src);
472 CompileSuccessfully(spvasm, SPVENV);
473 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
474 EXPECT_THAT(getDiagnosticString(),
475 ContainsRegex("OpConstantComposite Constituent <id> "
476 "'.*' must be an OpTypeTensorARM.*"));
477 }
478
479 TEST_F(ValidateTensor,
480 InvalidTensorConstantRank2ConstituentTensorWrongElementType) {
481 const std::string src = R"(
482 %uint_arr1_2 = OpConstantComposite %uint_arr1 %uint_2
483 %uint_arr2_2_2 = OpConstantComposite %uint_arr2 %uint_2 %uint_2
484 %ts_float_r1 = OpTypeTensorARM %float %uint_1 %uint_arr1_2
485 %ts_uint_r2 = OpTypeTensorARM %uint %uint_2 %uint_arr2_2_2
486 %cst_r1 = OpConstantComposite %ts_float_r1 %float_1 %float_1
487 %cst_r2 = OpConstantComposite %ts_uint_r2 %cst_r1 %cst_r1
488 )";
489 std::string spvasm = GenerateModule(src);
490 CompileSuccessfully(spvasm, SPVENV);
491 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
492 EXPECT_THAT(getDiagnosticString(),
493 ContainsRegex("OpConstantComposite Constituent <id> "
494 "'.*' must have the same Element Type "
495 "as Result Type <id> .*"));
496 }
497
498 TEST_F(ValidateTensor, InvalidTensorConstantRank2ConstituentTensorWrongRank) {
499 const std::string src = R"(
500 %uint_arr1_2 = OpConstantComposite %uint_arr1 %uint_2
501 %uint_arr2_2_2 = OpConstantComposite %uint_arr2 %uint_2 %uint_2
502 %ts_uint_r1 = OpTypeTensorARM %uint %uint_1 %uint_arr1_2
503 %ts_uint_r2 = OpTypeTensorARM %uint %uint_2 %uint_arr2_2_2
504 %cst_r1 = OpConstantComposite %ts_uint_r1 %uint_42 %uint_42
505 %cst_r2 = OpConstantComposite %ts_uint_r2 %cst_r1 %cst_r1
506 %cst = OpConstantComposite %ts_uint_r2 %cst_r1 %cst_r2
507 )";
508 std::string spvasm = GenerateModule(src);
509 CompileSuccessfully(spvasm, SPVENV);
510 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
511 EXPECT_THAT(getDiagnosticString(),
512 ContainsRegex("OpConstantComposite Constituent <id> "
513 "'.*' must have a Rank that is "
514 "1 less than the Rank of Result Type <id> "
515 "'.*', expected 1 but got 2.*"));
516 }
517
518 TEST_F(ValidateTensor, InvalidTensorConstantRank2ConstituentTensorWrongShape) {
519 const std::string src = R"(
520 %uint_arr1_3 = OpConstantComposite %uint_arr1 %uint_3
521 %uint_arr2_2_2 = OpConstantComposite %uint_arr2 %uint_2 %uint_2
522 %ts_uint_r1 = OpTypeTensorARM %uint %uint_1 %uint_arr1_3
523 %ts_uint_r2 = OpTypeTensorARM %uint %uint_2 %uint_arr2_2_2
524 %cst_r1 = OpConstantComposite %ts_uint_r1 %uint_42 %uint_42 %uint_42
525 %cst_r2 = OpConstantComposite %ts_uint_r2 %cst_r1 %cst_r1
526 )";
527 std::string spvasm = GenerateModule(src);
528 CompileSuccessfully(spvasm, SPVENV);
529 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
530 EXPECT_THAT(
531 getDiagnosticString(),
532 ContainsRegex(
533 "OpConstantComposite Constituent <id> "
534 "'.*' must have a Shape that matches "
535 "that of Result Type <id> '.*' along "
536 "all inner dimensions of Result Type, expected 2 for dimension 0 of "
537 "Constituent but got 3.*"));
538 }
539
540 //
541 // Read tests
542 //
543
544 TEST_F(ValidateTensor, ValidTensorReadScalar) {
545 const std::string src = R"(
546 %fn = OpFunction %void None %fnty
547 %label1 = OpLabel
548 %tensor = OpLoad %tensor_uint_4 %tensor_var
549 %val = OpTensorReadARM %uint %tensor %uint_arr4_1_1_1_1
550 OpReturn
551 OpFunctionEnd
552 )";
553 std::string spvasm = GenerateModule(src);
554 CompileSuccessfully(spvasm, SPVENV);
555 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
556 }
557
558 TEST_F(ValidateTensor, ValidTensorReadArray) {
559 const std::string src = R"(
560 %fn = OpFunction %void None %fnty
561 %label1 = OpLabel
562 %tensor = OpLoad %tensor_uint_4 %tensor_var
563 %val = OpTensorReadARM %uint_arr2 %tensor %uint_arr4_1_1_1_1
564 OpReturn
565 OpFunctionEnd
566 )";
567 std::string spvasm = GenerateModule(src);
568 CompileSuccessfully(spvasm, SPVENV);
569 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
570 }
571
572 TEST_F(ValidateTensor, ValidTensorReadSpecConstantRank) {
573 const std::string src = R"(
574 %fn = OpFunction %void None %fnty
575 %label1 = OpLabel
576 %tensor = OpLoad %tensor_uint_spec %tensor_var_spec_rank
577 %val = OpTensorReadARM %uint %tensor %uint_arr4_1_1_1_1
578 OpReturn
579 OpFunctionEnd
580 )";
581 std::string spvasm = GenerateModule(src);
582 CompileSuccessfully(spvasm, SPVENV);
583 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
584 }
585
586 TEST_F(ValidateTensor, InvalidTensorReadResultTypeVoid) {
587 const std::string src = R"(
588 %fn = OpFunction %void None %fnty
589 %label1 = OpLabel
590 %tensor = OpLoad %tensor_uint_4 %tensor_var
591 %val = OpTensorReadARM %void %tensor %uint_arr4_1_1_1_1
592 OpReturn
593 OpFunctionEnd
594 )";
595 std::string spvasm = GenerateModule(src);
596 CompileSuccessfully(spvasm, SPVENV);
597 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
598 EXPECT_THAT(
599 getDiagnosticString(),
600 HasSubstr(
601 "Expected Result Type to be a scalar type or array of scalar type"));
602 }
603
604 TEST_F(ValidateTensor, InvalidTensorReadResultTypeVector) {
605 const std::string src = R"(
606 %fn = OpFunction %void None %fnty
607 %label1 = OpLabel
608 %tensor = OpLoad %tensor_uint_4 %tensor_var
609 %val = OpTensorReadARM %uint_vec4 %tensor %uint_arr4_1_1_1_1
610 OpReturn
611 OpFunctionEnd
612 )";
613 std::string spvasm = GenerateModule(src);
614 CompileSuccessfully(spvasm, SPVENV);
615 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
616 EXPECT_THAT(
617 getDiagnosticString(),
618 HasSubstr(
619 "Expected Result Type to be a scalar type or array of scalar type"));
620 }
621
622 TEST_F(ValidateTensor,
623 InvalidTensorReadResultTypeDoesNotMatchTensorElementType) {
624 const std::string src = R"(
625 %fn = OpFunction %void None %fnty
626 %label1 = OpLabel
627 %tensor = OpLoad %tensor_uint_4 %tensor_var
628 %val = OpTensorReadARM %float %tensor %uint_arr4_1_1_1_1
629 OpReturn
630 OpFunctionEnd
631 )";
632 std::string spvasm = GenerateModule(src);
633 CompileSuccessfully(spvasm, SPVENV);
634 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
635 EXPECT_THAT(
636 getDiagnosticString(),
637 HasSubstr(
638 "Expected Result Type to be the same as the Element Type of Tensor"));
639 }
640
641 TEST_F(ValidateTensor, InvalidTensorReadTensorNotRanked) {
642 const std::string src = R"(
643 %fn = OpFunction %void None %fnty
644 %label1 = OpLabel
645 %tensor = OpLoad %tensor_float %tensor_var_float_unranked
646 %val = OpTensorReadARM %float %tensor %uint_arr4_1_1_1_1
647 OpReturn
648 OpFunctionEnd
649 )";
650 std::string spvasm = GenerateModule(src);
651 CompileSuccessfully(spvasm, SPVENV);
652 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
653 EXPECT_THAT(
654 getDiagnosticString(),
655 HasSubstr(
656 "Expected Tensor to be an OpTypeTensorARM whose Rank is specified"));
657 }
658
659 TEST_F(ValidateTensor, InvalidTensorReadCoordinatesNotArray) {
660 const std::string src = R"(
661 %fn = OpFunction %void None %fnty
662 %label1 = OpLabel
663 %tensor = OpLoad %tensor_uint_4 %tensor_var
664 %val = OpTensorReadARM %uint %tensor %uint_1
665 OpReturn
666 OpFunctionEnd
667 )";
668 std::string spvasm = GenerateModule(src);
669 CompileSuccessfully(spvasm, SPVENV);
670 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
671 EXPECT_THAT(
672 getDiagnosticString(),
673 HasSubstr(
674 "Expected Coordinates to be an array whose Element Type is an "
675 "integer type and whose Length is equal to the Rank of Tensor."));
676 }
677
678 TEST_F(ValidateTensor, InvalidTensorReadCoordinatesArrayNotInteger) {
679 const std::string src = R"(
680 %fn = OpFunction %void None %fnty
681 %label1 = OpLabel
682 %tensor = OpLoad %tensor_uint_4 %tensor_var
683 %val = OpTensorReadARM %uint %tensor %float_arr4_1_1_1_1
684 OpReturn
685 OpFunctionEnd
686 )";
687 std::string spvasm = GenerateModule(src);
688 CompileSuccessfully(spvasm, SPVENV);
689 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
690 EXPECT_THAT(
691 getDiagnosticString(),
692 HasSubstr(
693 "Expected Coordinates to be an array whose Element Type is an "
694 "integer type and whose Length is equal to the Rank of Tensor."));
695 }
696
697 TEST_F(ValidateTensor, InvalidTensorReadCoordinatesArrayIntegerWrongLength) {
698 const std::string src = R"(
699 %fn = OpFunction %void None %fnty
700 %label1 = OpLabel
701 %tensor = OpLoad %tensor_uint_4 %tensor_var
702 %val = OpTensorReadARM %uint %tensor %uint_arr2_1_1
703 OpReturn
704 OpFunctionEnd
705 )";
706 std::string spvasm = GenerateModule(src);
707 CompileSuccessfully(spvasm, SPVENV);
708 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
709 EXPECT_THAT(
710 getDiagnosticString(),
711 HasSubstr(
712 "Expected Coordinates to be an array whose Element Type is an "
713 "integer type and whose Length is equal to the Rank of Tensor."));
714 }
715
716 TEST_F(ValidateTensor, ValidTensorReadScalarWithNoneTensorOperands) {
717 const std::string src = R"(
718 %fn = OpFunction %void None %fnty
719 %label1 = OpLabel
720 %tensor = OpLoad %tensor_uint_4 %tensor_var
721 %val = OpTensorReadARM %uint %tensor %uint_arr4_1_1_1_1 NoneARM
722 OpReturn
723 OpFunctionEnd
724 )";
725 std::string spvasm = GenerateModule(src);
726 CompileSuccessfully(spvasm, SPVENV);
727 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
728 }
729
730 TEST_F(ValidateTensor, ValidTensorReadScalarWithNontemporalTensorOperands) {
731 const std::string src = R"(
732 %fn = OpFunction %void None %fnty
733 %label1 = OpLabel
734 %tensor = OpLoad %tensor_uint_4 %tensor_var
735 %val = OpTensorReadARM %uint %tensor %uint_arr4_1_1_1_1 NontemporalARM
736 OpReturn
737 OpFunctionEnd
738 )";
739 std::string spvasm = GenerateModule(src);
740 CompileSuccessfully(spvasm, SPVENV);
741 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
742 }
743
744 TEST_F(ValidateTensor, ValidTensorReadScalarOutOfBoundsValueTensorOperands) {
745 const std::string src = R"(
746 %fn = OpFunction %void None %fnty
747 %label1 = OpLabel
748 %tensor = OpLoad %tensor_uint_4 %tensor_var
749 %val = OpTensorReadARM %uint %tensor %uint_arr4_1_1_1_1 OutOfBoundsValueARM %uint_42
750 OpReturn
751 OpFunctionEnd
752 )";
753 std::string spvasm = GenerateModule(src);
754 CompileSuccessfully(spvasm, SPVENV);
755 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
756 }
757
758 TEST_F(ValidateTensor,
759 InvalidTensorReadScalarOutOfBoundsValueTensorOperandsValueWrongType) {
760 const std::string src = R"(
761 %fn = OpFunction %void None %fnty
762 %label1 = OpLabel
763 %tensor = OpLoad %tensor_uint_4 %tensor_var
764 %val = OpTensorReadARM %uint %tensor %uint_arr4_1_1_1_1 OutOfBoundsValueARM %float_1
765 OpReturn
766 OpFunctionEnd
767 )";
768 std::string spvasm = GenerateModule(src);
769 CompileSuccessfully(spvasm, SPVENV);
770 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPVENV));
771 EXPECT_THAT(
772 getDiagnosticString(),
773 HasSubstr(
774 "Expected the type of the OutOfBoundsValueARM value to be the same "
775 "as the Element Type of Tensor."));
776 }
777
778 TEST_F(ValidateTensor,
779 InvalidTensorReadScalarMakeElementAvailableTensorOperands) {
780 const std::string src = R"(
781 %fn = OpFunction %void None %fnty
782 %label1 = OpLabel
783 %tensor = OpLoad %tensor_uint_4 %tensor_var
784 %val = OpTensorReadARM %uint %tensor %uint_arr4_1_1_1_1 MakeElementAvailableARM %uint_1
785 OpReturn
786 OpFunctionEnd
787 )";
788 std::string spvasm = GenerateModule(src);
789 CompileSuccessfully(spvasm, SPVENV);
790 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
791 EXPECT_THAT(
792 getDiagnosticString(),
793 HasSubstr(
794 "MakeElementAvailableARM cannot be used with OpTensorReadARM."));
795 }
796
797 TEST_F(ValidateTensor,
798 ValidTensorReadScalarWithMakeElementVisibleTensorOperands) {
799 const std::string src = R"(
800 %fn = OpFunction %void None %fnty
801 %label1 = OpLabel
802 %tensor = OpLoad %tensor_uint_4 %tensor_var
803 %val = OpTensorReadARM %uint %tensor %uint_arr4_1_1_1_1 MakeElementVisibleARM|NonPrivateElementARM %uint_1
804 OpReturn
805 OpFunctionEnd
806 )";
807 std::string spvasm = GenerateModule(src);
808 CompileSuccessfully(spvasm, SPVENV);
809 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
810 }
811
812 TEST_F(
813 ValidateTensor,
814 InvalidTensorReadScalarMakeElementVisibleTensorOperandsWithoutNonPrivateElement) {
815 const std::string src = R"(
816 %fn = OpFunction %void None %fnty
817 %label1 = OpLabel
818 %tensor = OpLoad %tensor_uint_4 %tensor_var
819 %val = OpTensorReadARM %uint %tensor %uint_arr4_1_1_1_1 MakeElementVisibleARM %uint_1
820 OpReturn
821 OpFunctionEnd
822 )";
823 std::string spvasm = GenerateModule(src);
824 CompileSuccessfully(spvasm, SPVENV);
825 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
826 EXPECT_THAT(
827 getDiagnosticString(),
828 HasSubstr("MakeElementAvailableARM requires NonPrivateElementARM"));
829 }
830
831 //
832 // Write tests
833 //
834
835 TEST_F(ValidateTensor, ValidTensorWriteScalar) {
836 const std::string src = R"(
837 %fn = OpFunction %void None %fnty
838 %label1 = OpLabel
839 %tensor = OpLoad %tensor_uint_4 %tensor_var
840 OpTensorWriteARM %tensor %uint_arr4_1_1_1_1 %uint_1
841 OpReturn
842 OpFunctionEnd
843 )";
844 std::string spvasm = GenerateModule(src);
845 CompileSuccessfully(spvasm, SPVENV);
846 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
847 }
848
849 TEST_F(ValidateTensor, ValidTensorWriteArray) {
850 const std::string src = R"(
851 %fn = OpFunction %void None %fnty
852 %label1 = OpLabel
853 %tensor = OpLoad %tensor_uint_4 %tensor_var
854 OpTensorWriteARM %tensor %uint_arr4_1_1_1_1 %uint_arr2_1_1
855 OpReturn
856 OpFunctionEnd
857 )";
858 std::string spvasm = GenerateModule(src);
859 CompileSuccessfully(spvasm, SPVENV);
860 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
861 }
862
863 TEST_F(ValidateTensor, ValidTensorWriteSpecConstantRank) {
864 const std::string src = R"(
865 %fn = OpFunction %void None %fnty
866 %label1 = OpLabel
867 %tensor = OpLoad %tensor_uint_spec %tensor_var_spec_rank
868 OpTensorWriteARM %tensor %uint_arr4_1_1_1_1 %uint_1
869 OpReturn
870 OpFunctionEnd
871 )";
872 std::string spvasm = GenerateModule(src);
873 CompileSuccessfully(spvasm, SPVENV);
874 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
875 }
876
877 TEST_F(ValidateTensor, InvalidTensorWriteObjectNotScalarOrArrayOfScalar) {
878 const std::string src = R"(
879 %fn = OpFunction %void None %fnty
880 %label1 = OpLabel
881 %tensor = OpLoad %tensor_uint_4 %tensor_var
882 OpTensorWriteARM %tensor %uint_arr4_1_1_1_1 %uint_vec4_1_1_1_1
883 OpReturn
884 OpFunctionEnd
885 )";
886 std::string spvasm = GenerateModule(src);
887 CompileSuccessfully(spvasm, SPVENV);
888 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
889 EXPECT_THAT(
890 getDiagnosticString(),
891 HasSubstr("Expected Object to be a scalar type or array of scalar type "
892 "that is the same as the Element Type of Tensor."));
893 }
894
895 TEST_F(ValidateTensor, InvalidTensorWriteObjectDoesNotMatchTensorElementType) {
896 const std::string src = R"(
897 %fn = OpFunction %void None %fnty
898 %label1 = OpLabel
899 %tensor = OpLoad %tensor_uint_4 %tensor_var
900 OpTensorWriteARM %tensor %uint_arr4_1_1_1_1 %float_1
901 OpReturn
902 OpFunctionEnd
903 )";
904 std::string spvasm = GenerateModule(src);
905 CompileSuccessfully(spvasm, SPVENV);
906 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
907 EXPECT_THAT(
908 getDiagnosticString(),
909 HasSubstr("Expected Object to be a scalar type or array of scalar type "
910 "that is the same as the Element Type of Tensor."));
911 }
912
913 TEST_F(ValidateTensor, InvalidTensorWriteTensorNotRanked) {
914 const std::string src = R"(
915 %fn = OpFunction %void None %fnty
916 %label1 = OpLabel
917 %tensor = OpLoad %tensor_float %tensor_var_float_unranked
918 OpTensorWriteARM %tensor %uint_arr4_1_1_1_1 %float_1
919 OpReturn
920 OpFunctionEnd
921 )";
922 std::string spvasm = GenerateModule(src);
923 CompileSuccessfully(spvasm, SPVENV);
924 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
925 EXPECT_THAT(
926 getDiagnosticString(),
927 HasSubstr(
928 "Expected Tensor to be an OpTypeTensorARM whose Rank is specified"));
929 }
930
931 TEST_F(ValidateTensor, InvalidTensorWriteCoordinatesNotArray) {
932 const std::string src = R"(
933 %fn = OpFunction %void None %fnty
934 %label1 = OpLabel
935 %tensor = OpLoad %tensor_uint_4 %tensor_var
936 OpTensorWriteARM %tensor %uint_1 %uint_1
937 OpReturn
938 OpFunctionEnd
939 )";
940 std::string spvasm = GenerateModule(src);
941 CompileSuccessfully(spvasm, SPVENV);
942 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
943 EXPECT_THAT(
944 getDiagnosticString(),
945 HasSubstr(
946 "Expected Coordinates to be an array whose Element Type is an "
947 "integer type and whose Length is equal to the Rank of Tensor."));
948 }
949
950 TEST_F(ValidateTensor, InvalidTensorWriteCoordinatesArrayNotInteger) {
951 const std::string src = R"(
952 %fn = OpFunction %void None %fnty
953 %label1 = OpLabel
954 %tensor = OpLoad %tensor_uint_4 %tensor_var
955 OpTensorWriteARM %tensor %float_arr4_1_1_1_1 %uint_1
956 OpReturn
957 OpFunctionEnd
958 )";
959 std::string spvasm = GenerateModule(src);
960 CompileSuccessfully(spvasm, SPVENV);
961 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
962 EXPECT_THAT(
963 getDiagnosticString(),
964 HasSubstr(
965 "Expected Coordinates to be an array whose Element Type is an "
966 "integer type and whose Length is equal to the Rank of Tensor."));
967 }
968
969 TEST_F(ValidateTensor, InvalidTensorWriteCoordinatesArrayIntegerWrongLength) {
970 const std::string src = R"(
971 %fn = OpFunction %void None %fnty
972 %label1 = OpLabel
973 %tensor = OpLoad %tensor_uint_4 %tensor_var
974 OpTensorWriteARM %tensor %uint_arr2_1_1 %uint_1
975 OpReturn
976 OpFunctionEnd
977 )";
978 std::string spvasm = GenerateModule(src);
979 CompileSuccessfully(spvasm, SPVENV);
980 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
981 EXPECT_THAT(
982 getDiagnosticString(),
983 HasSubstr(
984 "Expected Coordinates to be an array whose Element Type is an "
985 "integer type and whose Length is equal to the Rank of Tensor."));
986 }
987
988 TEST_F(ValidateTensor, ValidTensorWriteScalarWithNoneTensorOperands) {
989 const std::string src = R"(
990 %fn = OpFunction %void None %fnty
991 %label1 = OpLabel
992 %tensor = OpLoad %tensor_uint_4 %tensor_var
993 OpTensorWriteARM %tensor %uint_arr4_1_1_1_1 %uint_1 NoneARM
994 OpReturn
995 OpFunctionEnd
996 )";
997 std::string spvasm = GenerateModule(src);
998 CompileSuccessfully(spvasm, SPVENV);
999 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
1000 }
1001
1002 TEST_F(ValidateTensor, ValidTensorWriteScalarWithNontemporalTensorOperands) {
1003 const std::string src = R"(
1004 %fn = OpFunction %void None %fnty
1005 %label1 = OpLabel
1006 %tensor = OpLoad %tensor_uint_4 %tensor_var
1007 OpTensorWriteARM %tensor %uint_arr4_1_1_1_1 %uint_1 NontemporalARM
1008 OpReturn
1009 OpFunctionEnd
1010 )";
1011 std::string spvasm = GenerateModule(src);
1012 CompileSuccessfully(spvasm, SPVENV);
1013 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
1014 }
1015
1016 TEST_F(ValidateTensor,
1017 ValidTensorWriteScalarWithMakeElementAvailableTensorOperands) {
1018 const std::string src = R"(
1019 %fn = OpFunction %void None %fnty
1020 %label1 = OpLabel
1021 %tensor = OpLoad %tensor_uint_4 %tensor_var
1022 OpTensorWriteARM %tensor %uint_arr4_1_1_1_1 %uint_1 MakeElementAvailableARM|NonPrivateElementARM %uint_1
1023 OpReturn
1024 OpFunctionEnd
1025 )";
1026 std::string spvasm = GenerateModule(src);
1027 CompileSuccessfully(spvasm, SPVENV);
1028 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
1029 }
1030
1031 TEST_F(ValidateTensor,
1032 InvalidTensorWriteScalarWithOutOfVoundsValueTensorOperands) {
1033 const std::string src = R"(
1034 %fn = OpFunction %void None %fnty
1035 %label1 = OpLabel
1036 %tensor = OpLoad %tensor_uint_4 %tensor_var
1037 OpTensorWriteARM %tensor %uint_arr4_1_1_1_1 %uint_1 OutOfBoundsValueARM %uint_42
1038 OpReturn
1039 OpFunctionEnd
1040 )";
1041 std::string spvasm = GenerateModule(src);
1042 CompileSuccessfully(spvasm, SPVENV);
1043 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
1044 EXPECT_THAT(getDiagnosticString(),
1045 HasSubstr("OutOfBoundsValue Tensor Operand not allowed with "
1046 "OpTensorWriteARM."));
1047 }
1048
1049 TEST_F(ValidateTensor,
1050 InvalidTensorWriteScalarWithMakeElementVisibleTensorOperands) {
1051 const std::string src = R"(
1052 %fn = OpFunction %void None %fnty
1053 %label1 = OpLabel
1054 %tensor = OpLoad %tensor_uint_4 %tensor_var
1055 OpTensorWriteARM %tensor %uint_arr4_1_1_1_1 %uint_1 MakeElementVisibleARM %uint_1
1056 OpReturn
1057 OpFunctionEnd
1058 )";
1059 std::string spvasm = GenerateModule(src);
1060 CompileSuccessfully(spvasm, SPVENV);
1061 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
1062 EXPECT_THAT(
1063 getDiagnosticString(),
1064 HasSubstr("MakeElementVisibleARM not allowed with OpTensorWriteARM."));
1065 }
1066
1067 TEST_F(
1068 ValidateTensor,
1069 InvalidTensorWriteScalarWithMakeElementAvailableTensorOperandsWithoutNonPrivateElement) {
1070 const std::string src = R"(
1071 %fn = OpFunction %void None %fnty
1072 %label1 = OpLabel
1073 %tensor = OpLoad %tensor_uint_4 %tensor_var
1074 OpTensorWriteARM %tensor %uint_arr4_1_1_1_1 %uint_1 MakeElementAvailableARM %uint_1
1075 OpReturn
1076 OpFunctionEnd
1077 )";
1078 std::string spvasm = GenerateModule(src);
1079 CompileSuccessfully(spvasm, SPVENV);
1080 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
1081 EXPECT_THAT(
1082 getDiagnosticString(),
1083 HasSubstr("MakeElementAvailableARM requires NonPrivateElementARM."));
1084 }
1085
1086 //
1087 // Query Size tests
1088 //
1089
1090 TEST_F(ValidateTensor, ValidTensorQuerySize) {
1091 const std::string src = R"(
1092 %fn = OpFunction %void None %fnty
1093 %label1 = OpLabel
1094 %tensor = OpLoad %tensor_uint_4 %tensor_var
1095 %size = OpTensorQuerySizeARM %uint %tensor %uint_1
1096 OpReturn
1097 OpFunctionEnd
1098 )";
1099 std::string spvasm = GenerateModule(src);
1100 CompileSuccessfully(spvasm, SPVENV);
1101 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
1102 }
1103
1104 TEST_F(ValidateTensor, ValidTensorQuerySizeSpecConstant) {
1105 const std::string src = R"(
1106 %fn = OpFunction %void None %fnty
1107 %label1 = OpLabel
1108 %tensor = OpLoad %tensor_uint_spec %tensor_var_spec_rank
1109 %size = OpTensorQuerySizeARM %uint %tensor %uint_0_spec
1110 OpReturn
1111 OpFunctionEnd
1112 )";
1113 std::string spvasm = GenerateModule(src);
1114 CompileSuccessfully(spvasm, SPVENV);
1115 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
1116 }
1117
1118 TEST_F(ValidateTensor, InvalidTensorQuerySizeTensorNotRanked) {
1119 const std::string src = R"(
1120 %fn = OpFunction %void None %fnty
1121 %label1 = OpLabel
1122 %tensor = OpLoad %tensor_float %tensor_var_float_unranked
1123 %size = OpTensorQuerySizeARM %uint %tensor %uint_1
1124 OpReturn
1125 OpFunctionEnd
1126 )";
1127 std::string spvasm = GenerateModule(src);
1128 CompileSuccessfully(spvasm, SPVENV);
1129 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
1130 EXPECT_THAT(
1131 getDiagnosticString(),
1132 HasSubstr(
1133 "Expected Tensor to be an OpTypeTensorARM whose Rank is specified"));
1134 }
1135
1136 TEST_F(ValidateTensor, InvalidTensorQuerySizeResultNotIntegerScalarType) {
1137 const std::string src = R"(
1138 %fn = OpFunction %void None %fnty
1139 %label1 = OpLabel
1140 %tensor = OpLoad %tensor_float %tensor_var_float_unranked
1141 %size = OpTensorQuerySizeARM %float %tensor %uint_1
1142 OpReturn
1143 OpFunctionEnd
1144 )";
1145 std::string spvasm = GenerateModule(src);
1146 CompileSuccessfully(spvasm, SPVENV);
1147 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
1148 EXPECT_THAT(getDiagnosticString(),
1149 HasSubstr("Expected Result Type to be an integer type scalar"));
1150 }
1151
1152 TEST_F(ValidateTensor, InvalidTensorQuerySizeDimensionNotIntegerType) {
1153 const std::string src = R"(
1154 %fn = OpFunction %void None %fnty
1155 %label1 = OpLabel
1156 %tensor = OpLoad %tensor_uint_4 %tensor_var
1157 %size = OpTensorQuerySizeARM %uint %tensor %float_1
1158 OpReturn
1159 OpFunctionEnd
1160 )";
1161 std::string spvasm = GenerateModule(src);
1162 CompileSuccessfully(spvasm, SPVENV);
1163 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
1164 EXPECT_THAT(getDiagnosticString(),
1165 HasSubstr("Dimension must come from a constant instruction of "
1166 "scalar integer type."));
1167 }
1168
1169 TEST_F(ValidateTensor, InvalidTensorQuerySizeDimensionNotConstant) {
1170 const std::string src = R"(
1171 %fn = OpFunction %void None %fnty
1172 %label1 = OpLabel
1173 %tensor = OpLoad %tensor_uint_4 %tensor_var
1174 %dims = OpLoad %uint_arr4 %var_uint_arr4_1_1_1_1
1175 %dim = OpCompositeExtract %uint %dims 0
1176 %size = OpTensorQuerySizeARM %uint %tensor %dim
1177 OpReturn
1178 OpFunctionEnd
1179 )";
1180 std::string spvasm = GenerateModule(src);
1181 CompileSuccessfully(spvasm, SPVENV);
1182 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
1183 EXPECT_THAT(getDiagnosticString(),
1184 HasSubstr("Dimension must come from a constant instruction of "
1185 "scalar integer type."));
1186 }
1187
1188 TEST_F(ValidateTensor, InvalidTensorQuerySizeDimension) {
1189 const std::string src = R"(
1190 %fn = OpFunction %void None %fnty
1191 %label1 = OpLabel
1192 %tensor = OpLoad %tensor_uint_4 %tensor_var
1193 %size = OpTensorQuerySizeARM %uint %tensor %uint_42
1194 OpReturn
1195 OpFunctionEnd
1196 )";
1197 std::string spvasm = GenerateModule(src);
1198 CompileSuccessfully(spvasm, SPVENV);
1199 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
1200 EXPECT_THAT(
1201 getDiagnosticString(),
1202 HasSubstr("Dimension (42) must be less than the Rank of Tensor (4)"));
1203 }
1204
1205 } // namespace
1206 } // namespace val
1207 } // namespace spvtools