Debian Codegraph @

test/val/val_graph_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 // TODO(kpet) Tests with spec constants
20
21 namespace spvtools {
22 namespace val {
23 namespace {
24
25 using ::testing::ContainsRegex;
26 using ::testing::HasSubstr;
27
28 using ValidateGraph = spvtest::ValidateBase<std::string>;
29
30 constexpr spv_target_env SPVENV = SPV_ENV_VULKAN_1_3;
31
32 std::string GenerateModule(const std::string& src) {
33 const std::string boilerplate = R"(
34 OpCapability Shader
35 OpCapability VulkanMemoryModel
36 OpCapability Int8
37 OpCapability GraphARM
38 OpCapability TensorsARM
39 OpCapability RuntimeDescriptorArray
40 OpExtension "SPV_ARM_graph"
41 OpExtension "SPV_ARM_tensors"
42 OpMemoryModel Logical Vulkan
43 OpDecorate %var_int8tensor DescriptorSet 0
44 OpDecorate %var_int8tensor Binding 0
45 OpDecorate %var_int32tensor DescriptorSet 0
46 OpDecorate %var_int32tensor Binding 1
47 OpDecorate %var_int8tensor_array3 DescriptorSet 0
48 OpDecorate %var_int8tensor_array3 Binding 2
49 OpDecorate %var_int8tensor_runtime_array DescriptorSet 0
50 OpDecorate %var_int8tensor_runtime_array Binding 3
51 %void = OpTypeVoid
52 %uint = OpTypeInt 32 0
53 %int8 = OpTypeInt 8 1
54 %int32 = OpTypeInt 32 1
55 %float = OpTypeFloat 32
56 %uint_0 = OpConstant %uint 0
57 %uint_1 = OpConstant %uint 1
58 %uint_2 = OpConstant %uint 2
59 %uint_3 = OpConstant %uint 3
60 %uint_4 = OpConstant %uint 4
61 %float_1 = OpConstant %float 1.0
62 %int8tensor = OpTypeTensorARM %int8 %uint_4
63 %int8r3tensor = OpTypeTensorARM %int8 %uint_3
64 %int32tensor = OpTypeTensorARM %int32 %uint_4
65 %int8tensor_array3 = OpTypeArray %int8tensor %uint_3
66 %int32tensor_array3 = OpTypeArray %int32tensor %uint_3
67 %int8tensor_runtime_array = OpTypeRuntimeArray %int8tensor
68 %int32tensor_runtime_array = OpTypeRuntimeArray %int32tensor
69 %ptr_Input_int8tensor = OpTypePointer Input %int8tensor
70 %var_int8tensor_wrong_storage_class = OpVariable %ptr_Input_int8tensor Input
71 %ptr_UniformConstant_int8tensor = OpTypePointer UniformConstant %int8tensor
72 %ptr_UniformConstant_int32tensor = OpTypePointer UniformConstant %int32tensor
73 %ptr_UniformConstant_int8tensor_array3 = OpTypePointer UniformConstant %int8tensor_array3
74 %ptr_UniformConstant_int8tensor_runtime_array = OpTypePointer UniformConstant %int8tensor_runtime_array
75 %var_int8tensor = OpVariable %ptr_UniformConstant_int8tensor UniformConstant
76 %var_int32tensor = OpVariable %ptr_UniformConstant_int32tensor UniformConstant
77 %var_int8tensor_array3 = OpVariable %ptr_UniformConstant_int8tensor_array3 UniformConstant
78 %var_int8tensor_runtime_array = OpVariable %ptr_UniformConstant_int8tensor_runtime_array UniformConstant
79 )";
80 return boilerplate + src;
81 }
82
83 std::string GenerateModuleWithGraphEntryPoint(const std::string& header) {
84 const std::string src = R"(
85 %default_graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
86 OpGraphEntryPointARM %default_graph "default_entry_point" %var_int8tensor %var_int8tensor
87 %default_graph = OpGraphARM %default_graph_type
88 %in = OpGraphInputARM %int8tensor %uint_0
89 OpGraphSetOutputARM %in %uint_0
90 OpGraphEndARM
91 )";
92 return GenerateModule(header) + src;
93 }
94
95 //
96 // Layout tests
97 //
98
99 TEST_F(ValidateGraph, InvalidNoGraphEntryPoint) {
100 std::string spvasm = GenerateModule("");
101 CompileSuccessfully(spvasm, SPVENV);
102 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPVENV));
103 EXPECT_THAT(getDiagnosticString(),
104 HasSubstr("No OpGraphEntryPointARM instruction was found but the "
105 "GraphARM capability is declared."));
106 }
107
108 TEST_F(ValidateGraph, InvalidGraphInGraph) {
109 const std::string src = R"(
110 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
111 %graph = OpGraphARM %graph_type
112 %graph2 = OpGraphARM %graph_type
113 )";
114 std::string spvasm = GenerateModule(src);
115
116 CompileSuccessfully(spvasm, SPVENV);
117 EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, ValidateInstructions(SPVENV));
118 EXPECT_THAT(getDiagnosticString(),
119 HasSubstr("Cannot define a graph in a graph"));
120 }
121
122 TEST_F(ValidateGraph, InvalidGraphEndOutsideGraph) {
123 const std::string src = R"(
124 OpGraphEndARM
125 )";
126 std::string spvasm = GenerateModule(src);
127
128 CompileSuccessfully(spvasm, SPVENV);
129 EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, ValidateInstructions(SPVENV));
130 EXPECT_THAT(getDiagnosticString(),
131 HasSubstr("GraphEndARM must be preceded by at least one "
132 "OpGraphSetOutputARM instruction"));
133 }
134
135 TEST_F(ValidateGraph, InvalidGraphEntryPointInsideGraph) {
136 const std::string src = R"(
137 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
138 %graph = OpGraphARM %graph_type
139 %in = OpGraphInputARM %int8tensor %uint_0
140 OpGraphEntryPointARM %graph "main" %in %in
141 )";
142 std::string spvasm = GenerateModule(src);
143
144 CompileSuccessfully(spvasm, SPVENV);
145 EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, ValidateInstructions(SPVENV));
146 EXPECT_THAT(
147 getDiagnosticString(),
148 HasSubstr(
149 "GraphEntryPointARM cannot appear in the definition of a graph"));
150 }
151
152 TEST_F(ValidateGraph, InvalidNonGraphInstructionInGraphSection) {
153 const std::string src = R"(
154 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
155 %ftype = OpTypeFunction %void %void
156 %graph = OpGraphARM %graph_type
157 %in = OpGraphInputARM %int8tensor %uint_0
158 OpGraphSetOutputARM %in %uint_0
159 OpGraphEndARM
160 %fn = OpFunction %void None %ftype
161 )";
162 std::string spvasm = GenerateModule(src);
163
164 CompileSuccessfully(spvasm, SPVENV);
165 EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, ValidateInstructions(SPVENV));
166 EXPECT_THAT(
167 getDiagnosticString(),
168 HasSubstr("Function cannot appear in the graph definitions section"));
169 }
170
171 TEST_F(ValidateGraph, InvalidGraphInputOusideGraph) {
172 const std::string src = R"(
173 %in = OpGraphInputARM %int8tensor %uint_0
174 )";
175 std::string spvasm = GenerateModule(src);
176
177 CompileSuccessfully(spvasm, SPVENV);
178 EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, ValidateInstructions(SPVENV));
179 EXPECT_THAT(getDiagnosticString(),
180 HasSubstr("OpGraphInputARM must immediately follow an OpGraphARM "
181 "or OpGraphInputARM instruction."));
182 }
183
184 TEST_F(ValidateGraph, InvalidGraphSetOutputsOusideGraph) {
185 const std::string src = R"(
186 OpGraphSetOutputARM %uint_0 %uint_0
187 )";
188 std::string spvasm = GenerateModule(src);
189
190 CompileSuccessfully(spvasm, SPVENV);
191 EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, ValidateInstructions(SPVENV));
192 EXPECT_THAT(getDiagnosticString(),
193 HasSubstr("OpGraphSetOutputARM must immediately precede an "
194 "OpGraphEndARM or OpGraphSetOutputARM instruction"));
195 }
196
197 TEST_F(ValidateGraph, ValidGraphConstantOusideGraph) {
198 const std::string src = R"(
199 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
200 %cst = OpGraphConstantARM %int8tensor 1
201 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor
202 %graph = OpGraphARM %graph_type
203 %in = OpGraphInputARM %int8tensor %uint_0
204 OpGraphSetOutputARM %in %uint_0
205 OpGraphEndARM
206 )";
207 std::string spvasm = GenerateModule(src);
208
209 CompileSuccessfully(spvasm, SPVENV);
210 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
211 }
212
213 TEST_F(ValidateGraph, ValidGraphWithNoInputsNoBody) {
214 const std::string src = R"(
215 %cst = OpGraphConstantARM %int8tensor 1
216 %graph_type = OpTypeGraphARM 0 %int8tensor
217 OpGraphEntryPointARM %graph "longname" %var_int8tensor
218 %graph = OpGraphARM %graph_type
219 OpGraphSetOutputARM %cst %uint_0
220 OpGraphEndARM
221 )";
222 std::string spvasm = GenerateModule(src);
223
224 CompileSuccessfully(spvasm, SPVENV);
225 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
226 }
227
228 TEST_F(ValidateGraph, InvalidGraphWithDisallowedBodyInstructions) {
229 const std::string src = R"(
230 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
231 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor
232 %graph = OpGraphARM %graph_type
233 %in = OpGraphInputARM %int8tensor %uint_0
234 %val = OpCompositeExtract %int8r3tensor %in 0
235 %out = OpCompositeInsert %int8tensor %val %in 0
236 OpGraphSetOutputARM %out %uint_0
237 OpGraphEndARM
238 )";
239 std::string spvasm = GenerateModule(src);
240
241 CompileSuccessfully(spvasm, SPVENV);
242 EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, ValidateInstructions(SPVENV));
243 EXPECT_THAT(
244 getDiagnosticString(),
245 HasSubstr(
246 "OpCompositeInsert cannot appear in the graph definitions section"));
247 }
248
249 TEST_F(ValidateGraph, InvalidInstructionOutsideGraphAfterGraph) {
250 const std::string src = R"(
251 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
252 %cst = OpGraphConstantARM %int8tensor 1
253 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor
254 %graph = OpGraphARM %graph_type
255 %in = OpGraphInputARM %int8tensor %uint_0
256 OpGraphSetOutputARM %in %uint_0
257 OpGraphEndARM
258 %val = OpCompositeExtract %int8r3tensor %cst 0
259 )";
260 std::string spvasm = GenerateModule(src);
261
262 CompileSuccessfully(spvasm, SPVENV);
263 EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, ValidateInstructions(SPVENV));
264 EXPECT_THAT(getDiagnosticString(),
265 HasSubstr("OpCompositeExtract must appear in a graph body"));
266 }
267
268 //
269 // Type tests
270 //
271 TEST_F(ValidateGraph, ValidGraphTypeOneTensorOutputNoInputs) {
272 const std::string src = R"(
273 %graph_type = OpTypeGraphARM 0 %int8tensor
274 )";
275 std::string spvasm = GenerateModuleWithGraphEntryPoint(src);
276
277 CompileSuccessfully(spvasm, SPVENV);
278 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
279 }
280
281 TEST_F(ValidateGraph, ValidGraphTypeOneTensorArrayOutputNoInputs) {
282 const std::string src = R"(
283 %graph_type = OpTypeGraphARM 0 %int8tensor_array3
284 )";
285 std::string spvasm = GenerateModuleWithGraphEntryPoint(src);
286
287 CompileSuccessfully(spvasm, SPVENV);
288 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
289 }
290
291 TEST_F(ValidateGraph, ValidGraphTypeOneTensorRuntimeArrayOutputNoInputs) {
292 const std::string src = R"(
293 %graph_type = OpTypeGraphARM 0 %int8tensor_runtime_array
294 )";
295 std::string spvasm = GenerateModuleWithGraphEntryPoint(src);
296
297 CompileSuccessfully(spvasm, SPVENV);
298 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
299 }
300
301 TEST_F(ValidateGraph, ValidGraphTypeOneTensorOutputOneTensorInput) {
302 const std::string src = R"(
303 %graph_type = OpTypeGraphARM 1 %int8tensor %int32tensor
304 )";
305 std::string spvasm = GenerateModuleWithGraphEntryPoint(src);
306
307 CompileSuccessfully(spvasm, SPVENV);
308 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
309 }
310
311 TEST_F(ValidateGraph, ValidGraphTypeOneTensorOutputOneTensorArrayInput) {
312 const std::string src = R"(
313 %graph_type = OpTypeGraphARM 1 %int8tensor_array3 %int32tensor
314 )";
315 std::string spvasm = GenerateModuleWithGraphEntryPoint(src);
316
317 CompileSuccessfully(spvasm, SPVENV);
318 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
319 }
320
321 TEST_F(ValidateGraph, ValidGraphTypeOneTensorOutputOneTensorRuntimeArrayInput) {
322 const std::string src = R"(
323 %graph_type = OpTypeGraphARM 1 %int8tensor_runtime_array %int32tensor
324 )";
325 std::string spvasm = GenerateModuleWithGraphEntryPoint(src);
326
327 CompileSuccessfully(spvasm, SPVENV);
328 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
329 }
330
331 TEST_F(ValidateGraph,
332 ValidGraphTypeOneTensorOutputOneTensorInputOneTensorArrayInput) {
333 const std::string src = R"(
334 %graph_type = OpTypeGraphARM 2 %int8tensor %int8tensor_array3 %int32tensor
335 )";
336 std::string spvasm = GenerateModuleWithGraphEntryPoint(src);
337
338 CompileSuccessfully(spvasm, SPVENV);
339 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
340 }
341
342 TEST_F(
343 ValidateGraph,
344 ValidGraphTypeOneTensorOutputOneTensorRuntimeArrayInputOneTensorArrayInput) {
345 const std::string src = R"(
346 %graph_type = OpTypeGraphARM 1 %int8tensor_runtime_array %int8tensor_array3 %int32tensor
347 )";
348 std::string spvasm = GenerateModuleWithGraphEntryPoint(src);
349
350 CompileSuccessfully(spvasm, SPVENV);
351 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
352 }
353
354 TEST_F(ValidateGraph, ValidGraphTypeOneTensorArrayOutputOneTensorInput) {
355 const std::string src = R"(
356 %graph_type = OpTypeGraphARM 1 %int8tensor %int32tensor_array3
357 )";
358 std::string spvasm = GenerateModuleWithGraphEntryPoint(src);
359
360 CompileSuccessfully(spvasm, SPVENV);
361 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
362 }
363
364 TEST_F(ValidateGraph, ValidGraphTypeOneTensorRuntimeArrayOutputOneTensorInput) {
365 const std::string src = R"(
366 %graph_type = OpTypeGraphARM 1 %int8tensor %int32tensor_runtime_array
367 )";
368 std::string spvasm = GenerateModuleWithGraphEntryPoint(src);
369
370 CompileSuccessfully(spvasm, SPVENV);
371 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
372 }
373
374 TEST_F(ValidateGraph,
375 ValidGraphTypeOneTensorRuntimeArrayOutputOneTensorOuputOneTensorInput) {
376 const std::string src = R"(
377 %graph_type = OpTypeGraphARM 1 %int8tensor %int32tensor_runtime_array %int32tensor
378 )";
379 std::string spvasm = GenerateModuleWithGraphEntryPoint(src);
380
381 CompileSuccessfully(spvasm, SPVENV);
382 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
383 }
384
385 TEST_F(
386 ValidateGraph,
387 ValidGraphTypeOneTensorRuntimeArrayOutputOneTensorArrayOutputOneTensorOuputOneTensorInput) {
388 const std::string src = R"(
389 %graph_type = OpTypeGraphARM 1 %int8tensor %int32tensor_runtime_array %int32tensor_array3 %int32tensor
390 )";
391 std::string spvasm = GenerateModuleWithGraphEntryPoint(src);
392
393 CompileSuccessfully(spvasm, SPVENV);
394 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
395 }
396 TEST_F(ValidateGraph,
397 ValidGraphTypeOneTensorRuntimeArrayOutputOnteTensorOuputOneTensorInput) {
398 const std::string src = R"(
399 %graph_type = OpTypeGraphARM 1 %int8tensor_array3 %int32tensor_array3
400 )";
401 std::string spvasm = GenerateModuleWithGraphEntryPoint(src);
402
403 CompileSuccessfully(spvasm, SPVENV);
404 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
405 }
406
407 TEST_F(ValidateGraph, InvalidGraphTypeWithNotEnoughIOTypes) {
408 const std::string src = R"(
409 %graph_type = OpTypeGraphARM 10 %int8tensor %int8tensor
410 )";
411 std::string spvasm = GenerateModule(src);
412
413 CompileSuccessfully(spvasm, SPVENV);
414 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
415 EXPECT_THAT(
416 getDiagnosticString(),
417 HasSubstr("2 I/O types were provided but the graph has 10 inputs"));
418 }
419
420 TEST_F(ValidateGraph, InvalidGraphTypeWithNonGraphInterfaceTypeIO) {
421 const std::string src = R"(
422 %graph_type = OpTypeGraphARM 1 %int8tensor %uint
423 )";
424 std::string spvasm = GenerateModule(src);
425
426 CompileSuccessfully(spvasm, SPVENV);
427 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
428 EXPECT_THAT(getDiagnosticString(),
429 ContainsRegex("I/O type '.*' is not a Graph Interface Type.*"));
430 }
431
432 TEST_F(ValidateGraph, InvalidGraphTypeWithOneInputZeroOutputs) {
433 const std::string src = R"(
434 %graph_type = OpTypeGraphARM 1 %int8tensor
435 )";
436 std::string spvasm = GenerateModule(src);
437
438 CompileSuccessfully(spvasm, SPVENV);
439 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
440 EXPECT_THAT(getDiagnosticString(),
441 HasSubstr("A graph type must have at least one output"));
442 }
443
444 TEST_F(ValidateGraph, InvalidGraphTypeWithZeroInputsZeroOutputs) {
445 const std::string src = R"(
446 %graph_type = OpTypeGraphARM 0
447 )";
448 std::string spvasm = GenerateModule(src);
449
450 CompileSuccessfully(spvasm, SPVENV);
451 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
452 EXPECT_THAT(getDiagnosticString(),
453 HasSubstr("A graph type must have at least one output"));
454 }
455
456 //
457 // Constant tests
458 //
459 TEST_F(ValidateGraph, ValidGraphConstantTensorUnranked) {
460 const std::string src = R"(
461 %tensor_type = OpTypeTensorARM %uint
462 %cst = OpGraphConstantARM %tensor_type 25
463 )";
464 std::string spvasm = GenerateModuleWithGraphEntryPoint(src);
465
466 CompileSuccessfully(spvasm, SPVENV);
467 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
468 }
469
470 TEST_F(ValidateGraph, ValidGraphConstantTensorRanked) {
471 const std::string src = R"(
472 %tensor_type = OpTypeTensorARM %uint %uint_4
473 %cst = OpGraphConstantARM %tensor_type 25
474 )";
475 std::string spvasm = GenerateModuleWithGraphEntryPoint(src);
476
477 CompileSuccessfully(spvasm, SPVENV);
478 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
479 }
480
481 TEST_F(ValidateGraph, ValidGraphConstantTensorShaped) {
482 const std::string src = R"(
483 %uint_array2 = OpTypeArray %uint %uint_2
484 %tensor_shape = OpConstantComposite %uint_array2 %uint_1 %uint_4
485 %tensor_type = OpTypeTensorARM %uint %uint_2 %tensor_shape
486 %cst = OpGraphConstantARM %tensor_type 25
487 )";
488 std::string spvasm = GenerateModuleWithGraphEntryPoint(src);
489
490 CompileSuccessfully(spvasm, SPVENV);
491 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
492 }
493
494 TEST_F(ValidateGraph, InvalidGraphConstantDuplicateIDs) {
495 const std::string src = R"(
496 %cst = OpGraphConstantARM %int8tensor 25
497 %cst2 = OpGraphConstantARM %int32tensor 25
498 )";
499 std::string spvasm = GenerateModuleWithGraphEntryPoint(src);
500
501 CompileSuccessfully(spvasm, SPVENV);
502 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
503 EXPECT_THAT(getDiagnosticString(),
504 HasSubstr("No two OpGraphConstantARM instructions may have the "
505 "same GraphConstantID"));
506 }
507
508 TEST_F(ValidateGraph, InvalidGraphConstantWithNonTensorType) {
509 const std::string src = R"(
510 %cst = OpGraphConstantARM %uint 25
511 )";
512 std::string spvasm = GenerateModule(src);
513
514 CompileSuccessfully(spvasm, SPVENV);
515 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
516 EXPECT_THAT(
517 getDiagnosticString(),
518 HasSubstr(
519 "GraphConstantARM must have a Result Type that is a tensor type"));
520 }
521
522 //
523 // EntryPoint tests
524 //
525 TEST_F(ValidateGraph, InvalidModuleWithNoGraphEntryPoint) {
526 std::string spvasm = GenerateModule("");
527
528 CompileSuccessfully(spvasm, SPVENV);
529 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPVENV));
530 EXPECT_THAT(getDiagnosticString(),
531 HasSubstr("No OpGraphEntryPointARM instruction was found but the "
532 "GraphARM capability is declared."));
533 }
534 TEST_F(ValidateGraph, InvalidGraphEntryPointNotAGraph) {
535 const std::string src = R"(
536 OpGraphEntryPointARM %uint_0 "longname"
537 )";
538 std::string spvasm = GenerateModule(src);
539
540 CompileSuccessfully(spvasm, SPVENV);
541 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
542 EXPECT_THAT(
543 getDiagnosticString(),
544 HasSubstr(
545 "GraphEntryPointARM Graph must be a OpGraphARM but found Constant"));
546 }
547
548 TEST_F(ValidateGraph, InvalidGraphEntryPointInterfaceIDNotOpVariable) {
549 const std::string src = R"(
550 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
551 OpGraphEntryPointARM %graph "longname" %uint_0 %uint_0
552 %graph = OpGraphARM %graph_type
553 %in = OpGraphInputARM %int8tensor %uint_0
554 OpGraphSetOutputARM %in %uint_0
555 OpGraphEndARM
556 )";
557 std::string spvasm = GenerateModule(src);
558
559 CompileSuccessfully(spvasm, SPVENV);
560 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
561 EXPECT_THAT(getDiagnosticString(),
562 ContainsRegex(
563 "GraphEntryPointARM Interface ID '.*' must "
564 "come from OpVariable with UniformConstant Storage Class.*"));
565 }
566
567 TEST_F(ValidateGraph, InvalidGraphEntryPointInterfaceIDWrongStorageClass) {
568 const std::string src = R"(
569 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
570 OpGraphEntryPointARM %graph "longname" %var_int8tensor_wrong_storage_class %var_int8tensor_wrong_storage_class
571 %graph = OpGraphARM %graph_type
572 %in = OpGraphInputARM %int8tensor %uint_0
573 OpGraphSetOutputARM %in %uint_0
574 OpGraphEndARM
575 )";
576 std::string spvasm = GenerateModule(src);
577
578 CompileSuccessfully(spvasm, SPVENV);
579 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
580 EXPECT_THAT(getDiagnosticString(),
581 ContainsRegex(
582 "GraphEntryPointARM Interface ID '.*' must "
583 "come from OpVariable with UniformConstant Storage Class.*"));
584 }
585
586 TEST_F(ValidateGraph, InvalidGraphEntryPointNotEnoughInterfaceIDs) {
587 const std::string src = R"(
588 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
589 OpGraphEntryPointARM %graph "longname" %var_int8tensor
590 %graph = OpGraphARM %graph_type
591 %in = OpGraphInputARM %int8tensor %uint_0
592 OpGraphSetOutputARM %in %uint_0
593 OpGraphEndARM
594 )";
595 std::string spvasm = GenerateModule(src);
596
597 CompileSuccessfully(spvasm, SPVENV);
598 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
599 EXPECT_THAT(
600 getDiagnosticString(),
601 ContainsRegex(
602 "GraphEntryPointARM Interface list contains 1 IDs but Graph's type "
603 "'.*' has 2 inputs and outputs.*"));
604 }
605
606 TEST_F(ValidateGraph, InvalidGraphEntryPointTooManyInterfaceIDs) {
607 const std::string src = R"(
608 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
609 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor %var_int8tensor
610 %graph = OpGraphARM %graph_type
611 %in = OpGraphInputARM %int8tensor %uint_0
612 OpGraphSetOutputARM %in %uint_0
613 OpGraphEndARM
614 )";
615 std::string spvasm = GenerateModule(src);
616
617 CompileSuccessfully(spvasm, SPVENV);
618 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
619 EXPECT_THAT(
620 getDiagnosticString(),
621 ContainsRegex(
622 "GraphEntryPointARM Interface list contains 3 IDs but Graph's type "
623 "'.*' has 2 inputs and outputs.*"));
624 }
625
626 TEST_F(ValidateGraph,
627 InvalidGraphEntryPointInterfaceIDTypeMismatchesGraphIOType) {
628 const std::string src = R"(
629 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
630 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int32tensor
631 %graph = OpGraphARM %graph_type
632 %in = OpGraphInputARM %int8tensor %uint_0
633 OpGraphSetOutputARM %in %uint_0
634 OpGraphEndARM
635 )";
636 std::string spvasm = GenerateModule(src);
637
638 CompileSuccessfully(spvasm, SPVENV);
639 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
640 EXPECT_THAT(getDiagnosticString(),
641 ContainsRegex("GraphEntryPointARM Interface ID type "
642 "'.*' must match the type of the "
643 "corresponding graph I/O '.*'.*"));
644 }
645
646 //
647 // Graph tests
648 //
649 TEST_F(ValidateGraph, InvalidGraphResultType) {
650 const std::string src = R"(
651 OpGraphEntryPointARM %graph "foo"
652 %graph = OpGraphARM %uint
653 %in = OpGraphInputARM %int8tensor %uint_0
654 OpGraphSetOutputARM %in %uint_0
655 OpGraphEndARM
656 )";
657 std::string spvasm = GenerateModule(src);
658
659 CompileSuccessfully(spvasm, SPVENV);
660 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
661 EXPECT_THAT(getDiagnosticString(),
662 HasSubstr("GraphARM Result Type must be an OpTypeGraphARM"));
663 }
664
665 //
666 // Input tests
667 //
668 TEST_F(ValidateGraph, ValidTensorInput) {
669 const std::string src = R"(
670 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
671 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor
672 %graph = OpGraphARM %graph_type
673 %in = OpGraphInputARM %int8tensor %uint_0
674 OpGraphSetOutputARM %in %uint_0
675 OpGraphEndARM
676 )";
677 std::string spvasm = GenerateModule(src);
678
679 CompileSuccessfully(spvasm, SPVENV);
680 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
681 }
682
683 TEST_F(ValidateGraph, ValidTensorArrayInputCompositeExtract) {
684 const std::string src = R"(
685 %graph_type = OpTypeGraphARM 1 %int8tensor_array3 %int8tensor
686 OpGraphEntryPointARM %graph "longname" %var_int8tensor_array3 %var_int8tensor
687 %graph = OpGraphARM %graph_type
688 %in_tensors = OpGraphInputARM %int8tensor_array3 %uint_0
689 %out = OpCompositeExtract %int8tensor %in_tensors 1
690 OpGraphSetOutputARM %out %uint_0
691 OpGraphEndARM
692 )";
693 std::string spvasm = GenerateModule(src);
694
695 CompileSuccessfully(spvasm, SPVENV);
696 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
697 }
698
699 TEST_F(ValidateGraph, ValidTensorArrayInputWithElementIndex) {
700 const std::string src = R"(
701 %graph_type = OpTypeGraphARM 1 %int8tensor_array3 %int8tensor
702 OpGraphEntryPointARM %graph "longname" %var_int8tensor_array3 %var_int8tensor
703 %graph = OpGraphARM %graph_type
704 %out = OpGraphInputARM %int8tensor %uint_0 %uint_1
705 OpGraphSetOutputARM %out %uint_0
706 OpGraphEndARM
707 )";
708 std::string spvasm = GenerateModule(src);
709
710 CompileSuccessfully(spvasm, SPVENV);
711 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
712 }
713
714 TEST_F(ValidateGraph, ValidTensorRuntimeArrayInputCompositeExtract) {
715 const std::string src = R"(
716 %graph_type = OpTypeGraphARM 1 %int8tensor_runtime_array %int8tensor
717 OpGraphEntryPointARM %graph "longname" %var_int8tensor_runtime_array %var_int8tensor
718 %graph = OpGraphARM %graph_type
719 %in_tensors = OpGraphInputARM %int8tensor_runtime_array %uint_0
720 %out = OpCompositeExtract %int8tensor %in_tensors 1
721 OpGraphSetOutputARM %out %uint_0
722 OpGraphEndARM
723 )";
724 std::string spvasm = GenerateModule(src);
725
726 CompileSuccessfully(spvasm, SPVENV);
727 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
728 }
729
730 TEST_F(ValidateGraph, ValidTensorRuntimeArrayInputWithElementIndex) {
731 const std::string src = R"(
732 %graph_type = OpTypeGraphARM 1 %int8tensor_runtime_array %int8tensor
733 OpGraphEntryPointARM %graph "longname" %var_int8tensor_runtime_array %var_int8tensor
734 %graph = OpGraphARM %graph_type
735 %out = OpGraphInputARM %int8tensor %uint_0 %uint_1
736 OpGraphSetOutputARM %out %uint_0
737 OpGraphEndARM
738 )";
739 std::string spvasm = GenerateModule(src);
740
741 CompileSuccessfully(spvasm, SPVENV);
742 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
743 }
744
745 TEST_F(ValidateGraph, InvalidGraphInputIndexWrongType) {
746 const std::string src = R"(
747 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
748 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor
749 %graph = OpGraphARM %graph_type
750 %in = OpGraphInputARM %int8tensor %float_1
751 OpGraphSetOutputARM %in %uint_0
752 OpGraphEndARM
753 )";
754 std::string spvasm = GenerateModule(src);
755
756 CompileSuccessfully(spvasm, SPVENV);
757 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
758 EXPECT_THAT(getDiagnosticString(),
759 HasSubstr("GraphInputARM InputIndex must be a 32-bit integer"));
760 }
761
762 TEST_F(ValidateGraph, InvalidGraphInputElementIndexWrongType) {
763 const std::string src = R"(
764 %graph_type = OpTypeGraphARM 1 %int8tensor_array3 %int8tensor
765 OpGraphEntryPointARM %graph "longname" %var_int8tensor_array3 %var_int8tensor
766 %graph = OpGraphARM %graph_type
767 %in = OpGraphInputARM %int8tensor %uint_0 %float_1
768 OpGraphSetOutputARM %in %uint_0
769 OpGraphEndARM
770 )";
771 std::string spvasm = GenerateModule(src);
772
773 CompileSuccessfully(spvasm, SPVENV);
774 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
775 EXPECT_THAT(getDiagnosticString(),
776 HasSubstr("GraphInputARM ElementIndex must be a 32-bit integer"));
777 }
778
779 TEST_F(ValidateGraph, InvalidGraphInputIndexDuplicate) {
780 const std::string src = R"(
781 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
782 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor
783 %graph = OpGraphARM %graph_type
784 %in = OpGraphInputARM %int8tensor %uint_0
785 %in2 = OpGraphInputARM %int8tensor %uint_0
786 OpGraphSetOutputARM %in %uint_0
787 OpGraphEndARM
788 )";
789 std::string spvasm = GenerateModule(src);
790
791 CompileSuccessfully(spvasm, SPVENV);
792 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
793 EXPECT_THAT(getDiagnosticString(),
794 HasSubstr("Two OpGraphInputARM instructions with the same "
795 "InputIndex must not be part of the same "
796 "graph definition unless ElementIndex is present in "
797 "both with different values."));
798 }
799
800 TEST_F(ValidateGraph, InvalidGraphInputAndElementIndexDuplicate) {
801 const std::string src = R"(
802 %graph_type = OpTypeGraphARM 1 %int8tensor_array3 %int8tensor
803 OpGraphEntryPointARM %graph "longname" %var_int8tensor_array3 %var_int8tensor
804 %graph = OpGraphARM %graph_type
805 %in = OpGraphInputARM %int8tensor %uint_0 %uint_1
806 %in2 = OpGraphInputARM %int8tensor %uint_0 %uint_1
807 OpGraphSetOutputARM %in %uint_0
808 OpGraphEndARM
809 )";
810 std::string spvasm = GenerateModule(src);
811
812 CompileSuccessfully(spvasm, SPVENV);
813 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
814 EXPECT_THAT(getDiagnosticString(),
815 HasSubstr("Two OpGraphInputARM instructions with the same "
816 "InputIndex must not be part of the same "
817 "graph definition unless ElementIndex is present in "
818 "both with different values."));
819 }
820
821 TEST_F(ValidateGraph,
822 InvalidGraphInputIndexDuplicateWithAndWithoutElementIndex) {
823 const std::string src = R"(
824 %graph_type = OpTypeGraphARM 1 %int8tensor_array3 %int8tensor
825 OpGraphEntryPointARM %graph "longname" %var_int8tensor_array3 %var_int8tensor
826 %graph = OpGraphARM %graph_type
827 %in = OpGraphInputARM %int8tensor_array3 %uint_0
828 %in2 = OpGraphInputARM %int8tensor %uint_0 %uint_1
829 OpGraphSetOutputARM %in2 %uint_0
830 OpGraphEndARM
831 )";
832 std::string spvasm = GenerateModule(src);
833
834 CompileSuccessfully(spvasm, SPVENV);
835 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
836 EXPECT_THAT(getDiagnosticString(),
837 HasSubstr("Two OpGraphInputARM instructions with the same "
838 "InputIndex must not be part of the same "
839 "graph definition unless ElementIndex is present in "
840 "both with different values."));
841 }
842
843 TEST_F(ValidateGraph, ValidGraphInputIndexDuplicateWithDifferentElementIndex) {
844 const std::string src = R"(
845 %graph_type = OpTypeGraphARM 1 %int8tensor_runtime_array %int8tensor
846 OpGraphEntryPointARM %graph "longname" %var_int8tensor_runtime_array %var_int8tensor
847 %graph = OpGraphARM %graph_type
848 %in = OpGraphInputARM %int8tensor %uint_0 %uint_2
849 %in2 = OpGraphInputARM %int8tensor %uint_0 %uint_1
850 OpGraphSetOutputARM %in2 %uint_0
851 OpGraphEndARM
852 )";
853 std::string spvasm = GenerateModule(src);
854
855 CompileSuccessfully(spvasm, SPVENV);
856 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
857 }
858
859 TEST_F(ValidateGraph, InvalidGraphInputElementIndexWithNonArrayInput) {
860 const std::string src = R"(
861 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
862 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor
863 %graph = OpGraphARM %graph_type
864 %in = OpGraphInputARM %int8tensor %uint_0 %uint_1
865 OpGraphSetOutputARM %in %uint_0
866 OpGraphEndARM
867 )";
868 std::string spvasm = GenerateModule(src);
869
870 CompileSuccessfully(spvasm, SPVENV);
871 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
872 EXPECT_THAT(
873 getDiagnosticString(),
874 HasSubstr("OpGraphInputARM ElementIndex not allowed when the graph input "
875 "selected by "
876 "InputIndex is not an OpTypeArray or OpTypeRuntimeArray"));
877 }
878
879 TEST_F(ValidateGraph, InvalidGraphInputElementIndexOutOfRange) {
880 const std::string src = R"(
881 %graph_type = OpTypeGraphARM 1 %int8tensor_array3 %int8tensor
882 OpGraphEntryPointARM %graph "longname" %var_int8tensor_array3 %var_int8tensor
883 %graph = OpGraphARM %graph_type
884 %in = OpGraphInputARM %int8tensor %uint_0 %uint_3
885 OpGraphSetOutputARM %in %uint_0
886 OpGraphEndARM
887 )";
888 std::string spvasm = GenerateModule(src);
889
890 CompileSuccessfully(spvasm, SPVENV);
891 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
892 EXPECT_THAT(getDiagnosticString(),
893 ContainsRegex("OpGraphInputARM ElementIndex out of range. The "
894 "type of the graph input being accessed '.*' is an "
895 "array of 3 elements but ElementIndex is 3.*"));
896 }
897
898 TEST_F(ValidateGraph, InvalidGraphInputIndexOutOfRange) {
899 const std::string src = R"(
900 %graph_type = OpTypeGraphARM 2 %int8tensor %int8tensor %int8tensor
901 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor %var_int8tensor
902 %graph = OpGraphARM %graph_type
903 %in0 = OpGraphInputARM %int8tensor %uint_0
904 %in1 = OpGraphInputARM %int8tensor %uint_1
905 %in2 = OpGraphInputARM %int8tensor %uint_2
906 OpGraphSetOutputARM %in0 %uint_0
907 OpGraphEndARM
908 )";
909 std::string spvasm = GenerateModule(src);
910
911 CompileSuccessfully(spvasm, SPVENV);
912 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
913 EXPECT_THAT(getDiagnosticString(),
914 ContainsRegex("Type '.*' for graph '.*' "
915 "has 2 inputs but found an OpGraphInputARM "
916 "instruction with an InputIndex that is 2.*"));
917 }
918
919 TEST_F(ValidateGraph, InvalidGraphFirstInputTypeDoesNotMatchGraphType) {
920 const std::string src = R"(
921 %graph_type = OpTypeGraphARM 2 %int8tensor %int8tensor %int8tensor
922 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor %var_int8tensor
923 %graph = OpGraphARM %graph_type
924 %in0 = OpGraphInputARM %int32tensor %uint_0
925 %in1 = OpGraphInputARM %int8tensor %uint_1
926 OpGraphSetOutputARM %in1 %uint_0
927 OpGraphEndARM
928 )";
929 std::string spvasm = GenerateModule(src);
930
931 CompileSuccessfully(spvasm, SPVENV);
932 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
933 EXPECT_THAT(getDiagnosticString(),
934 ContainsRegex("Result Type '.*' of graph input instruction "
935 "'.*' does not match the type "
936 "'.*' of input 0 in the graph type.*"));
937 }
938
939 TEST_F(ValidateGraph, InvalidGraphLastInputTypeDoesNotMatchGraphType) {
940 const std::string src = R"(
941 %graph_type = OpTypeGraphARM 2 %int8tensor %int8tensor %int8tensor
942 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor %var_int8tensor
943 %graph = OpGraphARM %graph_type
944 %in0 = OpGraphInputARM %int8tensor %uint_0
945 %in1 = OpGraphInputARM %int32tensor %uint_1
946 OpGraphSetOutputARM %in0 %uint_0
947 OpGraphEndARM
948 )";
949 std::string spvasm = GenerateModule(src);
950
951 CompileSuccessfully(spvasm, SPVENV);
952 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
953 EXPECT_THAT(getDiagnosticString(),
954 ContainsRegex("Result Type '.*' of graph input instruction "
955 "'.*' does not match the type "
956 "'.*' of input 1 in the graph type.*"));
957 }
958
959 TEST_F(ValidateGraph, InvalidGraphInputAfterNonGraphInputOrGraph) {
960 const std::string src = R"(
961 %graph_type = OpTypeGraphARM 2 %int8tensor_array3 %int8tensor %int8tensor
962 OpGraphEntryPointARM %graph "longname" %var_int8tensor_array3 %var_int8tensor %var_int8tensor
963 %graph = OpGraphARM %graph_type
964 %in = OpGraphInputARM %int8tensor_array3 %uint_0
965 %val = OpCompositeExtract %int8tensor %in 0
966 %in2 = OpGraphInputARM %int8tensor %uint_1
967 OpGraphSetOutputARM %val %uint_0
968 OpGraphEndARM
969 )";
970 std::string spvasm = GenerateModule(src);
971
972 CompileSuccessfully(spvasm, SPVENV);
973 EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, ValidateInstructions(SPVENV));
974 EXPECT_THAT(getDiagnosticString(),
975 HasSubstr("OpGraphInputARM must immediately follow an OpGraphARM "
976 "or OpGraphInputARM instruction"));
977 }
978
979 //
980 // Output tests
981 //
982 TEST_F(ValidateGraph, ValidTensorOutput) {
983 const std::string src = R"(
984 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
985 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor
986 %graph = OpGraphARM %graph_type
987 %in = OpGraphInputARM %int8tensor %uint_0
988 OpGraphSetOutputARM %in %uint_0
989 OpGraphEndARM
990 )";
991 std::string spvasm = GenerateModule(src);
992
993 CompileSuccessfully(spvasm, SPVENV);
994 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
995 }
996
997 TEST_F(ValidateGraph, ValidTensorOutputWholeArray) {
998 const std::string src = R"(
999 %graph_type = OpTypeGraphARM 1 %int8tensor_array3 %int8tensor_array3
1000 OpGraphEntryPointARM %graph "longname" %var_int8tensor_array3 %var_int8tensor_array3
1001 %graph = OpGraphARM %graph_type
1002 %in = OpGraphInputARM %int8tensor_array3 %uint_0
1003 OpGraphSetOutputARM %in %uint_0
1004 OpGraphEndARM
1005 )";
1006 std::string spvasm = GenerateModule(src);
1007
1008 CompileSuccessfully(spvasm, SPVENV);
1009 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
1010 }
1011
1012 TEST_F(ValidateGraph, ValidTensorOutputArraySingleElement) {
1013 const std::string src = R"(
1014 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor_array3
1015 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor_array3
1016 %graph = OpGraphARM %graph_type
1017 %in = OpGraphInputARM %int8tensor %uint_0
1018 OpGraphSetOutputARM %in %uint_0 %uint_1
1019 OpGraphEndARM
1020 )";
1021 std::string spvasm = GenerateModule(src);
1022
1023 CompileSuccessfully(spvasm, SPVENV);
1024 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
1025 }
1026
1027 TEST_F(ValidateGraph, ValidTensorOutputArrayMultipleElements) {
1028 const std::string src = R"(
1029 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor_array3
1030 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor_array3
1031 %graph = OpGraphARM %graph_type
1032 %in = OpGraphInputARM %int8tensor %uint_0
1033 OpGraphSetOutputARM %in %uint_0 %uint_1
1034 OpGraphSetOutputARM %in %uint_0 %uint_2
1035 OpGraphEndARM
1036 )";
1037 std::string spvasm = GenerateModule(src);
1038
1039 CompileSuccessfully(spvasm, SPVENV);
1040 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
1041 }
1042
1043 TEST_F(ValidateGraph, ValidTensorOutputWholeRuntimeArray) {
1044 const std::string src = R"(
1045 %graph_type = OpTypeGraphARM 1 %int8tensor_runtime_array %int8tensor_runtime_array
1046 OpGraphEntryPointARM %graph "longname" %var_int8tensor_runtime_array %var_int8tensor_runtime_array
1047 %graph = OpGraphARM %graph_type
1048 %in = OpGraphInputARM %int8tensor_runtime_array %uint_0
1049 OpGraphSetOutputARM %in %uint_0
1050 OpGraphEndARM
1051 )";
1052 std::string spvasm = GenerateModule(src);
1053
1054 CompileSuccessfully(spvasm, SPVENV);
1055 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
1056 }
1057
1058 TEST_F(ValidateGraph, ValidTensorOutputRuntimeArraySingleElement) {
1059 const std::string src = R"(
1060 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor_runtime_array
1061 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor_runtime_array
1062 %graph = OpGraphARM %graph_type
1063 %in = OpGraphInputARM %int8tensor %uint_0
1064 OpGraphSetOutputARM %in %uint_0 %uint_1
1065 OpGraphEndARM
1066 )";
1067 std::string spvasm = GenerateModule(src);
1068
1069 CompileSuccessfully(spvasm, SPVENV);
1070 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
1071 }
1072
1073 TEST_F(ValidateGraph, ValidTensorOutputRuntimeArrayMultipleElements) {
1074 const std::string src = R"(
1075 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor_runtime_array
1076 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor_runtime_array
1077 %graph = OpGraphARM %graph_type
1078 %in = OpGraphInputARM %int8tensor %uint_0
1079 OpGraphSetOutputARM %in %uint_0 %uint_1
1080 OpGraphSetOutputARM %in %uint_0 %uint_2
1081 OpGraphEndARM
1082 )";
1083 std::string spvasm = GenerateModule(src);
1084
1085 CompileSuccessfully(spvasm, SPVENV);
1086 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
1087 }
1088
1089 TEST_F(ValidateGraph, InvalidGraphOutputIndexWrongType) {
1090 const std::string src = R"(
1091 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
1092 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor
1093 %graph = OpGraphARM %graph_type
1094 %in = OpGraphInputARM %int8tensor %uint_0
1095 OpGraphSetOutputARM %in %float_1
1096 OpGraphEndARM
1097 )";
1098 std::string spvasm = GenerateModule(src);
1099
1100 CompileSuccessfully(spvasm, SPVENV);
1101 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
1102 EXPECT_THAT(
1103 getDiagnosticString(),
1104 HasSubstr("GraphSetOutputARM OutputIndex must be a 32-bit integer"));
1105 }
1106
1107 TEST_F(ValidateGraph, InvalidGraphOutputElementIndexWrongType) {
1108 const std::string src = R"(
1109 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor_array3
1110 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor_array3
1111 %graph = OpGraphARM %graph_type
1112 %in = OpGraphInputARM %int8tensor %uint_0
1113 OpGraphSetOutputARM %in %uint_0 %float_1
1114 OpGraphEndARM
1115 )";
1116 std::string spvasm = GenerateModule(src);
1117
1118 CompileSuccessfully(spvasm, SPVENV);
1119 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
1120 EXPECT_THAT(
1121 getDiagnosticString(),
1122 HasSubstr("GraphSetOutputARM ElementIndex must be a 32-bit integer"));
1123 }
1124
1125 TEST_F(ValidateGraph, InvalidGraphOutputIndexDuplicate) {
1126 const std::string src = R"(
1127 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
1128 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor
1129 %graph = OpGraphARM %graph_type
1130 %in = OpGraphInputARM %int8tensor %uint_0
1131 OpGraphSetOutputARM %in %uint_0
1132 OpGraphSetOutputARM %in %uint_0
1133 OpGraphEndARM
1134 )";
1135 std::string spvasm = GenerateModule(src);
1136
1137 CompileSuccessfully(spvasm, SPVENV);
1138 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
1139 EXPECT_THAT(getDiagnosticString(),
1140 HasSubstr("Two OpGraphSetOutputARM instructions with the same "
1141 "OutputIndex must not be part of the same "
1142 "graph definition unless ElementIndex is present in "
1143 "both with different values."));
1144 }
1145
1146 TEST_F(ValidateGraph, InvalidGraphOutputAndElementIndexDuplicate) {
1147 const std::string src = R"(
1148 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor_array3
1149 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor_array3
1150 %graph = OpGraphARM %graph_type
1151 %in = OpGraphInputARM %int8tensor %uint_0
1152 OpGraphSetOutputARM %in %uint_0 %uint_1
1153 OpGraphSetOutputARM %in %uint_0 %uint_1
1154 OpGraphEndARM
1155 )";
1156 std::string spvasm = GenerateModule(src);
1157
1158 CompileSuccessfully(spvasm, SPVENV);
1159 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
1160 EXPECT_THAT(getDiagnosticString(),
1161 HasSubstr("Two OpGraphSetOutputARM instructions with the same "
1162 "OutputIndex must not be part of the same "
1163 "graph definition unless ElementIndex is present in "
1164 "both with different values."));
1165 }
1166
1167 TEST_F(ValidateGraph,
1168 InvalidGraphOutputIndexDuplicateWithAndWithoutElementIndex) {
1169 const std::string src = R"(
1170 %graph_type = OpTypeGraphARM 1 %int8tensor_array3 %int8tensor_array3
1171 OpGraphEntryPointARM %graph "longname" %var_int8tensor_array3 %var_int8tensor_array3
1172 %graph = OpGraphARM %graph_type
1173 %in = OpGraphInputARM %int8tensor_array3 %uint_0
1174 %in_single = OpCompositeExtract %int8tensor %in 0
1175 OpGraphSetOutputARM %in %uint_0
1176 OpGraphSetOutputARM %in_single %uint_0 %uint_1
1177 OpGraphEndARM
1178 )";
1179 std::string spvasm = GenerateModule(src);
1180
1181 CompileSuccessfully(spvasm, SPVENV);
1182 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
1183 EXPECT_THAT(getDiagnosticString(),
1184 HasSubstr("Two OpGraphSetOutputARM instructions with the same "
1185 "OutputIndex must not be part of the same "
1186 "graph definition unless ElementIndex is present in "
1187 "both with different values."));
1188 }
1189
1190 TEST_F(ValidateGraph, ValidGraphOutputIndexDuplicateWithDifferentElementIndex) {
1191 const std::string src = R"(
1192 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor_runtime_array
1193 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor_runtime_array
1194 %graph = OpGraphARM %graph_type
1195 %in = OpGraphInputARM %int8tensor %uint_0
1196 OpGraphSetOutputARM %in %uint_0 %uint_1
1197 OpGraphSetOutputARM %in %uint_0 %uint_4
1198 OpGraphEndARM
1199 )";
1200 std::string spvasm = GenerateModule(src);
1201
1202 CompileSuccessfully(spvasm, SPVENV);
1203 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPVENV));
1204 }
1205
1206 TEST_F(ValidateGraph, InvalidGraphOutputElementIndexWithNonArrayOutput) {
1207 const std::string src = R"(
1208 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
1209 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor
1210 %graph = OpGraphARM %graph_type
1211 %in = OpGraphInputARM %int8tensor %uint_0
1212 OpGraphSetOutputARM %in %uint_0 %uint_0
1213 OpGraphEndARM
1214 )";
1215 std::string spvasm = GenerateModule(src);
1216
1217 CompileSuccessfully(spvasm, SPVENV);
1218 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
1219 EXPECT_THAT(
1220 getDiagnosticString(),
1221 HasSubstr("OpGraphSetOutputARM ElementIndex not allowed when the graph "
1222 "output selected by "
1223 "OutputIndex is not an OpTypeArray or OpTypeRuntimeArray"));
1224 }
1225
1226 TEST_F(ValidateGraph, InvalidGraphOutputElementIndexOutOfRange) {
1227 const std::string src = R"(
1228 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor_array3
1229 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor_array3
1230 %graph = OpGraphARM %graph_type
1231 %in = OpGraphInputARM %int8tensor %uint_0
1232 OpGraphSetOutputARM %in %uint_0 %uint_3
1233 OpGraphEndARM
1234 )";
1235 std::string spvasm = GenerateModule(src);
1236
1237 CompileSuccessfully(spvasm, SPVENV);
1238 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
1239 EXPECT_THAT(
1240 getDiagnosticString(),
1241 ContainsRegex("OpGraphSetOutputARM ElementIndex out of range. The "
1242 "type of the graph output being accessed '.*' is an "
1243 "array of 3 elements but ElementIndex is 3.*"));
1244 }
1245
1246 TEST_F(ValidateGraph, InvalidGraphOutputNotBeforeEndOrOutput) {
1247 const std::string src = R"(
1248 %graph_type = OpTypeGraphARM 1 %int8tensor_array3 %int8tensor_array3 %int8tensor
1249 OpGraphEntryPointARM %graph "longname" %var_int8tensor_array3 %var_int8tensor_array3 %var_int8tensor
1250 %graph = OpGraphARM %graph_type
1251 %in = OpGraphInputARM %int8tensor_array3 %uint_0
1252 OpGraphSetOutputARM %in %uint_0
1253 %val = OpCompositeExtract %int8tensor %in 0
1254 OpGraphSetOutputARM %val %uint_1
1255 OpGraphEndARM
1256 )";
1257 std::string spvasm = GenerateModule(src);
1258
1259 CompileSuccessfully(spvasm, SPVENV);
1260 EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, ValidateInstructions(SPVENV));
1261 EXPECT_THAT(
1262 getDiagnosticString(),
1263 HasSubstr(
1264 "CompositeExtract cannot appear after a graph output instruction"));
1265 }
1266
1267 TEST_F(ValidateGraph, InvalidGraphOutputIndexOutOfRange) {
1268 const std::string src = R"(
1269 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor %int8tensor
1270 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor %var_int8tensor
1271 %graph = OpGraphARM %graph_type
1272 %in = OpGraphInputARM %int8tensor %uint_0
1273 OpGraphSetOutputARM %in %uint_2
1274 OpGraphEndARM
1275 )";
1276 std::string spvasm = GenerateModule(src);
1277
1278 CompileSuccessfully(spvasm, SPVENV);
1279 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
1280 EXPECT_THAT(getDiagnosticString(),
1281 HasSubstr("GraphSetOutputARM setting OutputIndex 2 but graph "
1282 "only has 2 outputs."));
1283 }
1284
1285 TEST_F(ValidateGraph, InvalidGraphFirstOutputValueTypeDoesNotMatchOutputType) {
1286 const std::string src = R"(
1287 %graph_type = OpTypeGraphARM 1 %int32tensor %int8tensor %int8tensor
1288 OpGraphEntryPointARM %graph "longname" %var_int32tensor %var_int8tensor %var_int8tensor
1289 %graph = OpGraphARM %graph_type
1290 %in = OpGraphInputARM %int32tensor %uint_0
1291 OpGraphSetOutputARM %in %uint_0
1292 OpGraphEndARM
1293 )";
1294 std::string spvasm = GenerateModule(src);
1295
1296 CompileSuccessfully(spvasm, SPVENV);
1297 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
1298 EXPECT_THAT(
1299 getDiagnosticString(),
1300 ContainsRegex(
1301 "The type '.*' of Value provided to the graph output instruction "
1302 "'.*' does not match the type '.*' of output 0 in the graph type.*"));
1303 }
1304
1305 TEST_F(ValidateGraph, InvalidGraphLastOutputValueTypeDoesNotMatchOutputType) {
1306 const std::string src = R"(
1307 %graph_type = OpTypeGraphARM 1 %int32tensor %int32tensor %int8tensor
1308 OpGraphEntryPointARM %graph "longname" %var_int32tensor %var_int32tensor %var_int8tensor
1309 %graph = OpGraphARM %graph_type
1310 %in = OpGraphInputARM %int32tensor %uint_0
1311 OpGraphSetOutputARM %in %uint_1
1312 OpGraphEndARM
1313 )";
1314 std::string spvasm = GenerateModule(src);
1315
1316 CompileSuccessfully(spvasm, SPVENV);
1317 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPVENV));
1318 EXPECT_THAT(
1319 getDiagnosticString(),
1320 ContainsRegex(
1321 "The type '.*' of Value provided to the graph output instruction "
1322 "'.*' does not match the type '.*' of output 1 in the graph type.*"));
1323 }
1324
1325 TEST_F(ValidateGraph, InvalidGraphNoSetOuputBeforeEnd) {
1326 const std::string src = R"(
1327 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
1328 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor
1329 %graph = OpGraphARM %graph_type
1330 %in = OpGraphInputARM %int8tensor %uint_0
1331 OpGraphEndARM
1332 )";
1333 std::string spvasm = GenerateModule(src);
1334
1335 CompileSuccessfully(spvasm, SPVENV);
1336 EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, ValidateInstructions(SPVENV));
1337 EXPECT_THAT(getDiagnosticString(),
1338 HasSubstr("GraphEndARM must be preceded by at least one "
1339 "OpGraphSetOutputARM instruction"));
1340 }
1341
1342 //
1343 // End tests
1344 //
1345 TEST_F(ValidateGraph, InvalidGraphNoEnd) {
1346 const std::string src = R"(
1347 %graph_type = OpTypeGraphARM 1 %int8tensor %int8tensor
1348 OpGraphEntryPointARM %graph "longname" %var_int8tensor %var_int8tensor
1349 %graph = OpGraphARM %graph_type
1350 %in = OpGraphInputARM %int8tensor %uint_0
1351 )";
1352 std::string spvasm = GenerateModule(src);
1353
1354 CompileSuccessfully(spvasm, SPVENV);
1355 EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, ValidateInstructions(SPVENV));
1356 EXPECT_THAT(getDiagnosticString(),
1357 HasSubstr("Missing OpGraphEndARM at end of module"));
1358 }
1359
1360 } // namespace
1361 } // namespace val
1362 } // namespace spvtools