Debian Codegraph @

test/val/val_data_test.cpp

raw
1 // Copyright (c) 2016 Google Inc.
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 // Validation tests for Data Rules.
16
17 #include <string>
18 #include <utility>
19
20 #include "gmock/gmock.h"
21 #include "test/unit_spirv.h"
22 #include "test/val/val_fixtures.h"
23
24 namespace spvtools {
25 namespace val {
26 namespace {
27
28 using ::testing::HasSubstr;
29 using ::testing::MatchesRegex;
30
31 using ValidateData = spvtest::ValidateBase<std::pair<std::string, bool>>;
32
33 std::string HeaderWith(std::string cap) {
34 return std::string("OpCapability Shader OpCapability Linkage OpCapability ") +
35 cap + " OpMemoryModel Logical GLSL450 ";
36 }
37
38 std::string header = R"(
39 OpCapability Shader
40 OpCapability Linkage
41 OpMemoryModel Logical GLSL450
42 )";
43 std::string header_with_addresses = R"(
44 OpCapability Addresses
45 OpCapability Kernel
46 OpCapability GenericPointer
47 OpCapability Linkage
48 OpMemoryModel Physical32 OpenCL
49 )";
50 std::string header_with_vec16_cap = R"(
51 OpCapability Shader
52 OpCapability Vector16
53 OpCapability Linkage
54 OpMemoryModel Logical GLSL450
55 )";
56 std::string header_with_int8 = R"(
57 OpCapability Shader
58 OpCapability Linkage
59 OpCapability Int8
60 OpMemoryModel Logical GLSL450
61 )";
62 std::string header_with_int16 = R"(
63 OpCapability Shader
64 OpCapability Linkage
65 OpCapability Int16
66 OpMemoryModel Logical GLSL450
67 )";
68 std::string header_with_int64 = R"(
69 OpCapability Shader
70 OpCapability Linkage
71 OpCapability Int64
72 OpMemoryModel Logical GLSL450
73 )";
74 std::string header_with_bfloat16 = R"(
75 OpCapability Shader
76 OpCapability Linkage
77 OpCapability BFloat16TypeKHR
78 OpCapability BFloat16DotProductKHR
79 OpCapability BFloat16CooperativeMatrixKHR
80 OpExtension "SPV_KHR_bfloat16"
81 OpMemoryModel Logical GLSL450
82 )";
83 std::string header_with_float8 = R"(
84 OpCapability Shader
85 OpCapability Linkage
86 OpCapability Float8EXT
87 OpCapability Float8CooperativeMatrixEXT
88 OpExtension "SPV_EXT_float8"
89 OpMemoryModel Logical GLSL450
90 )";
91 std::string header_with_float8_and_bfloat16 = R"(
92 OpCapability Shader
93 OpCapability Linkage
94 OpCapability Float8EXT
95 OpCapability Float8CooperativeMatrixEXT
96 OpCapability BFloat16TypeKHR
97 OpExtension "SPV_EXT_float8"
98 OpExtension "SPV_KHR_bfloat16"
99 OpMemoryModel Logical GLSL450
100 )";
101 std::string header_with_float8_no_coop_matrix = R"(
102 OpCapability Shader
103 OpCapability Linkage
104 OpCapability Float8EXT
105 OpCapability CooperativeMatrixKHR
106 OpCapability VulkanMemoryModel
107 OpExtension "SPV_EXT_float8"
108 OpExtension "SPV_KHR_cooperative_matrix"
109 OpExtension "SPV_KHR_vulkan_memory_model"
110 OpMemoryModel Logical VulkanKHR
111 )";
112 std::string header_with_float16 = R"(
113 OpCapability Shader
114 OpCapability Linkage
115 OpCapability Float16
116 OpMemoryModel Logical GLSL450
117 )";
118 std::string header_with_float16_buffer = R"(
119 OpCapability Shader
120 OpCapability Linkage
121 OpCapability Float16Buffer
122 OpMemoryModel Logical GLSL450
123 )";
124 std::string header_with_float64 = R"(
125 OpCapability Shader
126 OpCapability Linkage
127 OpCapability Float64
128 OpMemoryModel Logical GLSL450
129 )";
130
131 std::string invalid_comp_error = "Illegal number of components";
132 std::string missing_cap_error = "requires the Vector16 capability";
133 std::string missing_int8_cap_error = "requires the Int8 capability";
134 std::string missing_int16_cap_error =
135 "requires the Int16 capability,"
136 " or an extension that explicitly enables 16-bit integers.";
137 std::string missing_int64_cap_error = "requires the Int64 capability";
138 std::string missing_float16_cap_error =
139 "requires the Float16 or Float16Buffer capability,"
140 " or an extension that explicitly enables 16-bit floating point.";
141 std::string missing_float64_cap_error = "requires the Float64 capability";
142 std::string invalid_num_bits_error = "Invalid number of bits";
143
144 TEST_F(ValidateData, vec0) {
145 std::string str = header + R"(
146 %1 = OpTypeFloat 32
147 %2 = OpTypeVector %1 0
148 )";
149 CompileSuccessfully(str.c_str());
150 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
151 EXPECT_THAT(getDiagnosticString(), HasSubstr(invalid_comp_error));
152 }
153
154 TEST_F(ValidateData, vec1) {
155 std::string str = header + R"(
156 %1 = OpTypeFloat 32
157 %2 = OpTypeVector %1 1
158 )";
159 CompileSuccessfully(str.c_str());
160 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
161 EXPECT_THAT(getDiagnosticString(), HasSubstr(invalid_comp_error));
162 }
163
164 TEST_F(ValidateData, vec2) {
165 std::string str = header + R"(
166 %1 = OpTypeFloat 32
167 %2 = OpTypeVector %1 2
168 )";
169 CompileSuccessfully(str.c_str());
170 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
171 }
172
173 TEST_F(ValidateData, vec3) {
174 std::string str = header + R"(
175 %1 = OpTypeFloat 32
176 %2 = OpTypeVector %1 3
177 )";
178 CompileSuccessfully(str.c_str());
179 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
180 }
181
182 TEST_F(ValidateData, vec4) {
183 std::string str = header + R"(
184 %1 = OpTypeFloat 32
185 %2 = OpTypeVector %1 4
186 )";
187 CompileSuccessfully(str.c_str());
188 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
189 }
190
191 TEST_F(ValidateData, vec5) {
192 std::string str = header + R"(
193 %1 = OpTypeFloat 32
194 %2 = OpTypeVector %1 5
195 )";
196 CompileSuccessfully(str.c_str());
197 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
198 EXPECT_THAT(getDiagnosticString(), HasSubstr(invalid_comp_error));
199 }
200
201 TEST_F(ValidateData, vec8) {
202 std::string str = header + R"(
203 %1 = OpTypeFloat 32
204 %2 = OpTypeVector %1 8
205 )";
206 CompileSuccessfully(str.c_str());
207 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
208 EXPECT_THAT(getDiagnosticString(), HasSubstr(missing_cap_error));
209 }
210
211 TEST_F(ValidateData, vec8_with_capability) {
212 std::string str = header_with_vec16_cap + R"(
213 %1 = OpTypeFloat 32
214 %2 = OpTypeVector %1 8
215 )";
216 CompileSuccessfully(str.c_str());
217 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
218 }
219
220 TEST_F(ValidateData, vec16) {
221 std::string str = header + R"(
222 %1 = OpTypeFloat 32
223 %2 = OpTypeVector %1 8
224 )";
225 CompileSuccessfully(str.c_str());
226 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
227 EXPECT_THAT(getDiagnosticString(), HasSubstr(missing_cap_error));
228 }
229
230 TEST_F(ValidateData, vec16_with_capability) {
231 std::string str = header_with_vec16_cap + R"(
232 %1 = OpTypeFloat 32
233 %2 = OpTypeVector %1 16
234 )";
235 CompileSuccessfully(str.c_str());
236 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
237 }
238
239 TEST_F(ValidateData, vec15) {
240 std::string str = header + R"(
241 %1 = OpTypeFloat 32
242 %2 = OpTypeVector %1 15
243 )";
244 CompileSuccessfully(str.c_str());
245 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
246 EXPECT_THAT(getDiagnosticString(), HasSubstr(invalid_comp_error));
247 }
248
249 TEST_F(ValidateData, int8_good) {
250 std::string str = header_with_int8 + "%2 = OpTypeInt 8 0";
251 CompileSuccessfully(str.c_str());
252 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
253 }
254
255 TEST_F(ValidateData, int8_bad) {
256 std::string str = header + "%2 = OpTypeInt 8 1";
257 CompileSuccessfully(str.c_str());
258 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
259 EXPECT_THAT(getDiagnosticString(), HasSubstr(missing_int8_cap_error));
260 }
261
262 TEST_F(ValidateData, int8_with_storage_buffer_8bit_access_good) {
263 std::string str = HeaderWith(
264 "StorageBuffer8BitAccess "
265 "OpExtension \"SPV_KHR_8bit_storage\"") +
266 " %2 = OpTypeInt 8 0";
267 CompileSuccessfully(str.c_str());
268 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions()) << getDiagnosticString();
269 }
270
271 TEST_F(ValidateData, int8_with_uniform_and_storage_buffer_8bit_access_good) {
272 std::string str = HeaderWith(
273 "UniformAndStorageBuffer8BitAccess "
274 "OpExtension \"SPV_KHR_8bit_storage\"") +
275 " %2 = OpTypeInt 8 0";
276 CompileSuccessfully(str.c_str());
277 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions()) << getDiagnosticString();
278 }
279
280 TEST_F(ValidateData, int8_with_storage_push_constant_8_good) {
281 std::string str = HeaderWith(
282 "StoragePushConstant8 "
283 "OpExtension \"SPV_KHR_8bit_storage\"") +
284 " %2 = OpTypeInt 8 0";
285 CompileSuccessfully(str.c_str());
286 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions()) << getDiagnosticString();
287 }
288
289 TEST_F(ValidateData, int16_good) {
290 std::string str = header_with_int16 + "%2 = OpTypeInt 16 1";
291 CompileSuccessfully(str.c_str());
292 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
293 }
294
295 TEST_F(ValidateData, storage_uniform_buffer_block_16_good) {
296 std::string str = HeaderWith(
297 "StorageUniformBufferBlock16 "
298 "OpExtension \"SPV_KHR_16bit_storage\"") +
299 "%2 = OpTypeInt 16 1 %3 = OpTypeFloat 16";
300 CompileSuccessfully(str.c_str());
301 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
302 }
303
304 TEST_F(ValidateData, storage_uniform_16_good) {
305 std::string str =
306 HeaderWith("StorageUniform16 OpExtension \"SPV_KHR_16bit_storage\"") +
307 "%2 = OpTypeInt 16 1 %3 = OpTypeFloat 16";
308 CompileSuccessfully(str.c_str());
309 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
310 }
311
312 TEST_F(ValidateData, storage_push_constant_16_good) {
313 std::string str = HeaderWith(
314 "StoragePushConstant16 "
315 "OpExtension \"SPV_KHR_16bit_storage\"") +
316 "%2 = OpTypeInt 16 1 %3 = OpTypeFloat 16";
317 CompileSuccessfully(str.c_str());
318 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
319 }
320
321 TEST_F(ValidateData, storage_input_output_16_good) {
322 std::string str = HeaderWith(
323 "StorageInputOutput16 "
324 "OpExtension \"SPV_KHR_16bit_storage\"") +
325 "%2 = OpTypeInt 16 1 %3 = OpTypeFloat 16";
326 CompileSuccessfully(str.c_str());
327 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
328 }
329
330 TEST_F(ValidateData, amd_gpu_shader_half_float_fetch_16_good) {
331 std::string str = R"(
332 OpCapability Shader
333 OpCapability Linkage
334 OpExtension "SPV_AMD_gpu_shader_half_float_fetch"
335 OpMemoryModel Logical GLSL450
336 %2 = OpTypeFloat 16)";
337 CompileSuccessfully(str.c_str());
338 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
339 }
340
341 TEST_F(ValidateData, int16_bad) {
342 std::string str = header + "%2 = OpTypeInt 16 1";
343 CompileSuccessfully(str.c_str());
344 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
345 EXPECT_THAT(getDiagnosticString(), HasSubstr(missing_int16_cap_error));
346 }
347
348 TEST_F(ValidateData, int64_good) {
349 std::string str = header_with_int64 + "%2 = OpTypeInt 64 1";
350 CompileSuccessfully(str.c_str());
351 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
352 }
353
354 TEST_F(ValidateData, int64_bad) {
355 std::string str = header + "%2 = OpTypeInt 64 1";
356 CompileSuccessfully(str.c_str());
357 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
358 EXPECT_THAT(getDiagnosticString(), HasSubstr(missing_int64_cap_error));
359 }
360
361 // Number of bits in an integer may be only one of: {8,16,32,64}
362 TEST_F(ValidateData, int_invalid_num_bits) {
363 std::string str = header + "%2 = OpTypeInt 48 1";
364 CompileSuccessfully(str.c_str());
365 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
366 EXPECT_THAT(getDiagnosticString(), HasSubstr(invalid_num_bits_error));
367 }
368
369 TEST_F(ValidateData, float16_good) {
370 std::string str = header_with_float16 + "%2 = OpTypeFloat 16";
371 CompileSuccessfully(str.c_str());
372 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
373 }
374
375 TEST_F(ValidateData, float8_good) {
376 std::string str = header_with_float8 +
377 R"(%2 = OpTypeFloat 8 Float8E4M3EXT
378 %3 = OpTypeFloat 8 Float8E5M2EXT
379 )";
380 CompileSuccessfully(str.c_str());
381 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
382 }
383
384 TEST_F(ValidateData, bfloat16_good) {
385 std::string str = header_with_bfloat16 + "%2 = OpTypeFloat 16 BFloat16KHR";
386 CompileSuccessfully(str.c_str());
387 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
388 }
389
390 TEST_F(ValidateData, cooperative_matrix_bfloat16_good) {
391 std::string str = header_with_bfloat16 + R"(
392 %u32 = OpTypeInt 32 0
393 %u32_16 = OpConstant %u32 16
394 %useA = OpConstant %u32 0
395 %subgroup = OpConstant %u32 3
396 %bf16 = OpTypeFloat 16 BFloat16KHR
397 %bf16matA = OpTypeCooperativeMatrixKHR %bf16 %subgroup %u32_16 %u32_16 %useA
398 )";
399 CompileSuccessfully(str.c_str());
400 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
401 }
402
403 TEST_F(ValidateData, cooperative_matrix_float8_good) {
404 std::string str = header_with_float8 + R"(
405 %u32 = OpTypeInt 32 0
406 %u32_16 = OpConstant %u32 16
407 %useA = OpConstant %u32 0
408 %subgroup = OpConstant %u32 3
409 %fp8e4m3 = OpTypeFloat 8 Float8E4M3EXT
410 %fp8e5m2 = OpTypeFloat 8 Float8E5M2EXT
411 %fp8e4m3_matA = OpTypeCooperativeMatrixKHR %fp8e4m3 %subgroup %u32_16 %u32_16 %useA
412 %fp8e5m2_matA = OpTypeCooperativeMatrixKHR %fp8e5m2 %subgroup %u32_16 %u32_16 %useA
413 )";
414 CompileSuccessfully(str.c_str());
415 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
416 }
417
418 TEST_F(ValidateData, cooperative_matrix_float8_no_capability_bad) {
419 std::string str = header_with_float8_no_coop_matrix + R"(
420 %u32 = OpTypeInt 32 0
421 %u32_16 = OpConstant %u32 16
422 %useA = OpConstant %u32 0
423 %subgroup = OpConstant %u32 3
424 %fp8e4m3 = OpTypeFloat 8 Float8E4M3EXT
425 %fp8e5m2 = OpTypeFloat 8 Float8E5M2EXT
426 %fp8e4m3_matA = OpTypeCooperativeMatrixKHR %fp8e4m3 %subgroup %u32_16 %u32_16 %useA
427 %fp8e5m2_matA = OpTypeCooperativeMatrixKHR %fp8e5m2 %subgroup %u32_16 %u32_16 %useA
428 )";
429 CompileSuccessfully(str.c_str(), SPV_ENV_UNIVERSAL_1_3);
430 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
431 EXPECT_THAT(getDiagnosticString(),
432 HasSubstr("require Float8CooperativeMatrixEXT be declared"));
433 }
434
435 TEST_F(ValidateData, float16_buffer_good) {
436 std::string str = header_with_float16_buffer + "%2 = OpTypeFloat 16";
437 CompileSuccessfully(str.c_str());
438 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
439 }
440
441 TEST_F(ValidateData, float16_bad) {
442 std::string str = header + "%2 = OpTypeFloat 16";
443 CompileSuccessfully(str.c_str());
444 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
445 EXPECT_THAT(getDiagnosticString(), HasSubstr(missing_float16_cap_error));
446 }
447
448 TEST_F(ValidateData, bfloat16_bad) {
449 std::string str = header + "%2 = OpTypeFloat 16 BFloat16KHR";
450 CompileSuccessfully(str.c_str());
451 ASSERT_EQ(SPV_ERROR_INVALID_CAPABILITY, ValidateInstructions());
452 EXPECT_THAT(getDiagnosticString(),
453 HasSubstr("requires one of these capabilities: BFloat16TypeKHR"));
454 }
455
456 TEST_F(ValidateData, float8_bad) {
457 std::string str = header +
458 R"(%2 = OpTypeFloat 8 Float8E4M3EXT
459 %3 = OpTypeFloat 8 Float8E5M2EXT
460 )";
461 CompileSuccessfully(str.c_str());
462 ASSERT_EQ(SPV_ERROR_INVALID_CAPABILITY, ValidateInstructions());
463 EXPECT_THAT(getDiagnosticString(),
464 HasSubstr("requires one of these capabilities: Float8EXT"));
465 }
466
467 TEST_F(ValidateData, float8_no_encoding_bad) {
468 std::string str = header_with_float8 + "%2 = OpTypeFloat 8";
469 CompileSuccessfully(str.c_str());
470 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
471 EXPECT_THAT(getDiagnosticString(),
472 HasSubstr("8-bit floating point type requires an encoding"));
473 }
474
475 TEST_F(ValidateData, float8_bad_encoding) {
476 std::string str =
477 header_with_float8_and_bfloat16 + "%2 = OpTypeFloat 8 BFloat16KHR";
478 CompileSuccessfully(str.c_str());
479 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
480 EXPECT_THAT(getDiagnosticString(),
481 HasSubstr("Unsupported 8-bit floating point encoding"));
482 }
483
484 TEST_F(ValidateData, dot_bfloat16_bad) {
485 std::string str = R"(
486 OpCapability Shader
487 OpCapability BFloat16TypeKHR
488 OpExtension "SPV_KHR_bfloat16"
489 %1 = OpExtInstImport "GLSL.std.450"
490 OpMemoryModel Logical GLSL450
491 OpEntryPoint GLCompute %main "main"
492 OpExecutionMode %main LocalSize 1 1 1
493 OpSource GLSL 450
494 OpName %main "main"
495 %void = OpTypeVoid
496 %3 = OpTypeFunction %void
497 %bfloat16 = OpTypeFloat 16 BFloat16KHR
498 %_ptr_Function_bfloat16 = OpTypePointer Function %bfloat16
499 %v2bfloat16 = OpTypeVector %bfloat16 2
500 %_ptr_Function_v2bfloat16 = OpTypePointer Function %v2bfloat16
501 %main = OpFunction %void None %3
502 %5 = OpLabel
503 %v1 = OpVariable %_ptr_Function_v2bfloat16 Function
504 %v2 = OpVariable %_ptr_Function_v2bfloat16 Function
505 %12 = OpLoad %v2bfloat16 %v1
506 %14 = OpLoad %v2bfloat16 %v2
507 %15 = OpDot %bfloat16 %12 %14
508 OpReturn
509 OpFunctionEnd
510 )";
511 CompileSuccessfully(str.c_str());
512 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
513 EXPECT_THAT(getDiagnosticString(),
514 HasSubstr("requires BFloat16DotProductKHR be declared."));
515 }
516
517 TEST_F(ValidateData, bfloat16_without_float16_capability_good) {
518 std::string str = header_with_bfloat16 + "%2 = OpTypeFloat 16 BFloat16KHR";
519 CompileSuccessfully(str.c_str());
520 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
521 }
522
523 TEST_F(ValidateData, float64_good) {
524 std::string str = header_with_float64 + "%2 = OpTypeFloat 64";
525
526 CompileSuccessfully(str.c_str());
527 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
528 }
529
530 TEST_F(ValidateData, float64_bad) {
531 std::string str = header + "%2 = OpTypeFloat 64";
532 CompileSuccessfully(str.c_str());
533 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
534 EXPECT_THAT(getDiagnosticString(), HasSubstr(missing_float64_cap_error));
535 }
536
537 // Number of bits in a float may be only one of: {16,32,64}
538 TEST_F(ValidateData, float_invalid_num_bits) {
539 std::string str = header + "%2 = OpTypeFloat 48";
540 CompileSuccessfully(str.c_str());
541 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
542 EXPECT_THAT(getDiagnosticString(), HasSubstr(invalid_num_bits_error));
543 }
544
545 TEST_F(ValidateData, matrix_data_type_float) {
546 std::string str = header + R"(
547 %f32 = OpTypeFloat 32
548 %vec3 = OpTypeVector %f32 3
549 %mat33 = OpTypeMatrix %vec3 3
550 )";
551 CompileSuccessfully(str.c_str());
552 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
553 }
554
555 TEST_F(ValidateData, ids_should_be_validated_before_data) {
556 std::string str = header + R"(
557 %f32 = OpTypeFloat 32
558 %mat33 = OpTypeMatrix %vec3 3
559 )";
560 CompileSuccessfully(str.c_str());
561 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
562 EXPECT_THAT(getDiagnosticString(),
563 HasSubstr("Operand '3[%3]' requires a previous definition"));
564 }
565
566 TEST_F(ValidateData, matrix_bad_column_type) {
567 std::string str = header + R"(
568 %f32 = OpTypeFloat 32
569 %mat33 = OpTypeMatrix %f32 3
570 )";
571 CompileSuccessfully(str.c_str());
572 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
573 EXPECT_THAT(getDiagnosticString(),
574 HasSubstr("Columns in a matrix must be of type vector"));
575 }
576
577 TEST_F(ValidateData, matrix_data_type_int) {
578 std::string str = header + R"(
579 %int32 = OpTypeInt 32 1
580 %vec3 = OpTypeVector %int32 3
581 %mat33 = OpTypeMatrix %vec3 3
582 )";
583 CompileSuccessfully(str.c_str());
584 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
585 EXPECT_THAT(getDiagnosticString(),
586 HasSubstr("can only be parameterized with floating-point types"));
587 }
588
589 TEST_F(ValidateData, matrix_data_type_bool) {
590 std::string str = header + R"(
591 %boolt = OpTypeBool
592 %vec3 = OpTypeVector %boolt 3
593 %mat33 = OpTypeMatrix %vec3 3
594 )";
595 CompileSuccessfully(str.c_str());
596 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
597 EXPECT_THAT(getDiagnosticString(),
598 HasSubstr("can only be parameterized with floating-point types"));
599 }
600
601 TEST_F(ValidateData, matrix_with_0_columns) {
602 std::string str = header + R"(
603 %f32 = OpTypeFloat 32
604 %vec3 = OpTypeVector %f32 3
605 %mat33 = OpTypeMatrix %vec3 0
606 )";
607 CompileSuccessfully(str.c_str());
608 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
609 EXPECT_THAT(
610 getDiagnosticString(),
611 HasSubstr("can only be parameterized as having only 2, 3, or 4 columns"));
612 }
613
614 TEST_F(ValidateData, matrix_with_1_column) {
615 std::string str = header + R"(
616 %f32 = OpTypeFloat 32
617 %vec3 = OpTypeVector %f32 3
618 %mat33 = OpTypeMatrix %vec3 1
619 )";
620 CompileSuccessfully(str.c_str());
621 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
622 EXPECT_THAT(
623 getDiagnosticString(),
624 HasSubstr("can only be parameterized as having only 2, 3, or 4 columns"));
625 }
626
627 TEST_F(ValidateData, matrix_with_2_columns) {
628 std::string str = header + R"(
629 %f32 = OpTypeFloat 32
630 %vec3 = OpTypeVector %f32 3
631 %mat33 = OpTypeMatrix %vec3 2
632 )";
633 CompileSuccessfully(str.c_str());
634 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
635 }
636
637 TEST_F(ValidateData, matrix_with_3_columns) {
638 std::string str = header + R"(
639 %f32 = OpTypeFloat 32
640 %vec3 = OpTypeVector %f32 3
641 %mat33 = OpTypeMatrix %vec3 3
642 )";
643 CompileSuccessfully(str.c_str());
644 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
645 }
646
647 TEST_F(ValidateData, matrix_with_4_columns) {
648 std::string str = header + R"(
649 %f32 = OpTypeFloat 32
650 %vec3 = OpTypeVector %f32 3
651 %mat33 = OpTypeMatrix %vec3 4
652 )";
653 CompileSuccessfully(str.c_str());
654 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
655 }
656
657 TEST_F(ValidateData, matrix_with_5_column) {
658 std::string str = header + R"(
659 %f32 = OpTypeFloat 32
660 %vec3 = OpTypeVector %f32 3
661 %mat33 = OpTypeMatrix %vec3 5
662 )";
663 CompileSuccessfully(str.c_str());
664 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
665 EXPECT_THAT(
666 getDiagnosticString(),
667 HasSubstr("can only be parameterized as having only 2, 3, or 4 columns"));
668 }
669
670 TEST_F(ValidateData, specialize_int) {
671 std::string str = header + R"(
672 %i32 = OpTypeInt 32 1
673 %len = OpSpecConstant %i32 2)";
674 CompileSuccessfully(str.c_str());
675 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
676 }
677
678 TEST_F(ValidateData, specialize_float) {
679 std::string str = header + R"(
680 %f32 = OpTypeFloat 32
681 %len = OpSpecConstant %f32 2)";
682 CompileSuccessfully(str.c_str());
683 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
684 }
685
686 TEST_F(ValidateData, specialize_boolean) {
687 std::string str = header + R"(
688 %2 = OpTypeBool
689 %3 = OpSpecConstantTrue %2
690 %4 = OpSpecConstantFalse %2)";
691 CompileSuccessfully(str.c_str());
692 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
693 }
694
695 TEST_F(ValidateData, specialize_boolean_true_to_int) {
696 std::string str = header + R"(
697 %2 = OpTypeInt 32 1
698 %3 = OpSpecConstantTrue %2)";
699 CompileSuccessfully(str.c_str());
700 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
701 EXPECT_THAT(getDiagnosticString(),
702 HasSubstr("OpSpecConstantTrue Result Type <id> '1[%int]' is not "
703 "a boolean type"));
704 }
705
706 TEST_F(ValidateData, specialize_boolean_false_to_int) {
707 std::string str = header + R"(
708 %2 = OpTypeInt 32 1
709 %4 = OpSpecConstantFalse %2)";
710 CompileSuccessfully(str.c_str());
711 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
712 EXPECT_THAT(getDiagnosticString(),
713 HasSubstr("OpSpecConstantFalse Result Type <id> '1[%int]' is not "
714 "a boolean type"));
715 }
716
717 TEST_F(ValidateData, missing_forward_pointer_decl) {
718 std::string str = header_with_addresses + R"(
719 %uintt = OpTypeInt 32 0
720 %3 = OpTypeStruct %fwd_ptrt %uintt
721 )";
722 CompileSuccessfully(str.c_str());
723 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
724 EXPECT_THAT(getDiagnosticString(),
725 HasSubstr("Operand '3[%3]' requires a previous definition"));
726 }
727
728 TEST_F(ValidateData, missing_forward_pointer_decl_self_reference) {
729 std::string str = header_with_addresses + R"(
730 %uintt = OpTypeInt 32 0
731 %3 = OpTypeStruct %3 %uintt
732 )";
733 CompileSuccessfully(str.c_str());
734 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
735 EXPECT_THAT(
736 getDiagnosticString(),
737 HasSubstr("Operand '2[%_struct_2]' requires a previous definition"));
738 }
739
740 TEST_F(ValidateData, forward_pointer_missing_definition) {
741 std::string str = header_with_addresses + R"(
742 OpTypeForwardPointer %_ptr_Generic_struct_A Generic
743 %uintt = OpTypeInt 32 0
744 %struct_B = OpTypeStruct %uintt %_ptr_Generic_struct_A
745 )";
746 CompileSuccessfully(str.c_str());
747 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
748 EXPECT_THAT(getDiagnosticString(),
749 HasSubstr("forward referenced IDs have not been defined"));
750 }
751
752 TEST_F(ValidateData, forward_ref_bad_type) {
753 std::string str = header_with_addresses + R"(
754 OpTypeForwardPointer %_ptr_Generic_struct_A Generic
755 %uintt = OpTypeInt 32 0
756 %struct_B = OpTypeStruct %uintt %_ptr_Generic_struct_A
757 %_ptr_Generic_struct_A = OpTypeFloat 32
758 )";
759 CompileSuccessfully(str.c_str());
760 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
761 EXPECT_THAT(getDiagnosticString(),
762 HasSubstr("Pointer type in OpTypeForwardPointer is not a pointer "
763 "type.\n OpTypeForwardPointer %float Generic\n"));
764 }
765
766 TEST_F(ValidateData, forward_ref_points_to_non_struct) {
767 std::string str = header_with_addresses + R"(
768 OpTypeForwardPointer %_ptr_Generic_struct_A Generic
769 %uintt = OpTypeInt 32 0
770 %struct_B = OpTypeStruct %uintt %_ptr_Generic_struct_A
771 %_ptr_Generic_struct_A = OpTypePointer Generic %uintt
772 )";
773 CompileSuccessfully(str.c_str());
774 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
775 EXPECT_THAT(getDiagnosticString(),
776 HasSubstr("Forward pointers must point to a structure"));
777 }
778
779 TEST_F(ValidateData, struct_forward_pointer_good) {
780 std::string str = header_with_addresses + R"(
781 OpTypeForwardPointer %_ptr_Generic_struct_A Generic
782 %uintt = OpTypeInt 32 0
783 %struct_B = OpTypeStruct %uintt %_ptr_Generic_struct_A
784 %struct_C = OpTypeStruct %uintt %struct_B
785 %struct_A = OpTypeStruct %uintt %struct_C
786 %_ptr_Generic_struct_A = OpTypePointer Generic %struct_C
787 )";
788 CompileSuccessfully(str.c_str());
789 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
790 }
791
792 TEST_F(ValidateData, ext_16bit_storage_caps_allow_free_fp_rounding_mode) {
793 for (const char* cap : {"StorageUniform16", "StorageUniformBufferBlock16"}) {
794 for (const char* mode : {"RTE", "RTZ", "RTP", "RTN"}) {
795 std::string str = std::string(R"(
796 OpCapability Shader
797 OpCapability Linkage
798 OpCapability )") +
799 cap + R"(
800 OpExtension "SPV_KHR_storage_buffer_storage_class"
801 OpExtension "SPV_KHR_variable_pointers"
802 OpExtension "SPV_KHR_16bit_storage"
803 OpMemoryModel Logical GLSL450
804 OpDecorate %_ FPRoundingMode )" +
805 mode + R"(
806 %half = OpTypeFloat 16
807 %float = OpTypeFloat 32
808 %float_1_25 = OpConstant %float 1.25
809 %half_ptr = OpTypePointer StorageBuffer %half
810 %half_ptr_var = OpVariable %half_ptr StorageBuffer
811 %void = OpTypeVoid
812 %func = OpTypeFunction %void
813 %main = OpFunction %void None %func
814 %main_entry = OpLabel
815 %_ = OpFConvert %half %float_1_25
816 OpStore %half_ptr_var %_
817 OpReturn
818 OpFunctionEnd
819 )";
820 CompileSuccessfully(str.c_str());
821 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
822 }
823 }
824 }
825
826 TEST_F(ValidateData, vulkan_disallow_free_fp_rounding_mode) {
827 for (const char* mode : {"RTE", "RTZ"}) {
828 for (const auto env : {SPV_ENV_VULKAN_1_0, SPV_ENV_VULKAN_1_1}) {
829 std::string str = std::string(R"(
830 OpCapability Shader
831 OpExtension "SPV_KHR_storage_buffer_storage_class"
832 OpExtension "SPV_KHR_variable_pointers"
833 OpMemoryModel Logical GLSL450
834 OpDecorate %_ FPRoundingMode )") +
835 mode + R"(
836 %half = OpTypeFloat 16
837 %float = OpTypeFloat 32
838 %float_1_25 = OpConstant %float 1.25
839 %half_ptr = OpTypePointer StorageBuffer %half
840 %half_ptr_var = OpVariable %half_ptr StorageBuffer
841 %void = OpTypeVoid
842 %func = OpTypeFunction %void
843 %main = OpFunction %void None %func
844 %main_entry = OpLabel
845 %_ = OpFConvert %half %float_1_25
846 OpStore %half_ptr_var %_
847 OpReturn
848 OpFunctionEnd
849 )";
850 CompileSuccessfully(str.c_str());
851 ASSERT_EQ(SPV_ERROR_INVALID_CAPABILITY, ValidateInstructions(env));
852 EXPECT_THAT(
853 getDiagnosticString(),
854 HasSubstr(
855 "Operand 2 of Decorate requires one of these capabilities: "
856 "StorageBuffer16BitAccess UniformAndStorageBuffer16BitAccess "
857 "StoragePushConstant16 StorageInputOutput16"));
858 }
859 }
860 }
861
862 TEST_F(ValidateData, void_array) {
863 std::string str = header + R"(
864 %void = OpTypeVoid
865 %int = OpTypeInt 32 0
866 %int_5 = OpConstant %int 5
867 %array = OpTypeArray %void %int_5
868 )";
869
870 CompileSuccessfully(str.c_str());
871 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
872 EXPECT_THAT(
873 getDiagnosticString(),
874 HasSubstr("OpTypeArray Element Type <id> '1[%void]' is a void type."));
875 }
876
877 TEST_F(ValidateData, void_runtime_array) {
878 std::string str = header + R"(
879 %void = OpTypeVoid
880 %array = OpTypeRuntimeArray %void
881 )";
882
883 CompileSuccessfully(str.c_str());
884 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
885 EXPECT_THAT(
886 getDiagnosticString(),
887 HasSubstr(
888 "OpTypeRuntimeArray Element Type <id> '1[%void]' is a void type."));
889 }
890
891 TEST_F(ValidateData, vulkan_RTA_array_at_end_of_struct) {
892 std::string str = R"(
893 OpCapability Shader
894 OpMemoryModel Logical GLSL450
895 OpEntryPoint Fragment %func "func"
896 OpExecutionMode %func OriginUpperLeft
897 OpDecorate %array_t ArrayStride 4
898 OpMemberDecorate %struct_t 0 Offset 0
899 OpMemberDecorate %struct_t 1 Offset 4
900 OpDecorate %struct_t Block
901 OpDecorate %2 DescriptorSet 0
902 OpDecorate %2 Binding 0
903 %uint_t = OpTypeInt 32 0
904 %array_t = OpTypeRuntimeArray %uint_t
905 %struct_t = OpTypeStruct %uint_t %array_t
906 %struct_ptr = OpTypePointer StorageBuffer %struct_t
907 %2 = OpVariable %struct_ptr StorageBuffer
908 %void = OpTypeVoid
909 %func_t = OpTypeFunction %void
910 %func = OpFunction %void None %func_t
911 %1 = OpLabel
912 OpReturn
913 OpFunctionEnd
914 )";
915
916 CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_1);
917 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
918 }
919
920 TEST_F(ValidateData, vulkan_RTA_not_at_end_of_struct) {
921 std::string str = R"(
922 OpCapability Shader
923 OpMemoryModel Logical GLSL450
924 OpEntryPoint Fragment %func "func"
925 OpExecutionMode %func OriginUpperLeft
926 OpDecorate %array_t ArrayStride 4
927 OpMemberDecorate %struct_t 0 Offset 0
928 OpMemberDecorate %struct_t 1 Offset 4
929 OpDecorate %struct_t Block
930 %uint_t = OpTypeInt 32 0
931 %array_t = OpTypeRuntimeArray %uint_t
932 %struct_t = OpTypeStruct %array_t %uint_t
933 %struct_ptr = OpTypePointer StorageBuffer %struct_t
934 %2 = OpVariable %struct_ptr StorageBuffer
935 %void = OpTypeVoid
936 %func_t = OpTypeFunction %void
937 %func = OpFunction %void None %func_t
938 %1 = OpLabel
939 OpReturn
940 OpFunctionEnd
941 )";
942
943 CompileSuccessfully(str.c_str(), SPV_ENV_VULKAN_1_1);
944 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
945 EXPECT_THAT(getDiagnosticString(),
946 AnyVUID("VUID-StandaloneSpirv-OpTypeRuntimeArray-04680"));
947 EXPECT_THAT(getDiagnosticString(),
948 HasSubstr("In Vulkan, OpTypeRuntimeArray must only be used for "
949 "the last member of an OpTypeStruct\n %_struct_3 = "
950 "OpTypeStruct %_runtimearr_uint %uint\n"));
951 }
952
953 TEST_F(ValidateData, TypeForwardReference) {
954 std::string test = R"(
955 OpCapability Shader
956 OpCapability PhysicalStorageBufferAddresses
957 OpCapability Linkage
958 OpMemoryModel Logical GLSL450
959 OpTypeForwardPointer %1 PhysicalStorageBuffer
960 %2 = OpTypeStruct
961 %3 = OpTypeRuntimeArray %1
962 %1 = OpTypePointer PhysicalStorageBuffer %2
963 )";
964
965 CompileSuccessfully(test, SPV_ENV_UNIVERSAL_1_5);
966 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_5));
967 }
968
969 TEST_F(ValidateData, VulkanTypeForwardStorageClass) {
970 std::string test = R"(
971 OpCapability Shader
972 OpCapability PhysicalStorageBufferAddresses
973 OpMemoryModel Logical GLSL450
974 OpTypeForwardPointer %1 Uniform
975 %2 = OpTypeStruct
976 %3 = OpTypeRuntimeArray %1
977 %1 = OpTypePointer Uniform %2
978 )";
979
980 CompileSuccessfully(test, SPV_ENV_VULKAN_1_2);
981 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
982 EXPECT_THAT(getDiagnosticString(),
983 AnyVUID("VUID-StandaloneSpirv-OpTypeForwardPointer-04711"));
984 EXPECT_THAT(getDiagnosticString(),
985 HasSubstr("In Vulkan, OpTypeForwardPointer must have "
986 "a storage class of PhysicalStorageBuffer."));
987 }
988
989 TEST_F(ValidateData, TypeForwardReferenceMustBeForwardPointer) {
990 std::string test = R"(
991 OpCapability Shader
992 OpCapability PhysicalStorageBufferAddresses
993 OpCapability Linkage
994 OpMemoryModel Logical GLSL450
995 %1 = OpTypeStruct
996 %2 = OpTypeRuntimeArray %3
997 %3 = OpTypePointer PhysicalStorageBuffer %1
998 )";
999
1000 CompileSuccessfully(test, SPV_ENV_UNIVERSAL_1_5);
1001 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_5));
1002 EXPECT_THAT(getDiagnosticString(),
1003 HasSubstr("Operand '3[%_ptr_PhysicalStorageBuffer__struct_1]' "
1004 "requires a previous definition"));
1005 }
1006
1007 } // namespace
1008 } // namespace val
1009 } // namespace spvtools