Debian Codegraph @

test/val/val_memory_test.cpp

raw
1 // Copyright (c) 2018 Google Inc.
2 // Modifications Copyright (C) 2024 Advanced Micro Devices, Inc. All rights
3 // reserved.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 // Validation tests for memory/storage
18
19 #include <string>
20 #include <vector>
21
22 #include "gmock/gmock.h"
23 #include "test/unit_spirv.h"
24 #include "test/val/val_code_generator.h"
25 #include "test/val/val_fixtures.h"
26
27 // For pretty-printing tuples with spv_target_env.
28 std::ostream& operator<<(std::ostream& stream, spv_target_env target) {
29 switch (target) {
30 case SPV_ENV_UNIVERSAL_1_3:
31 return stream << "SPV_ENV_UNIVERSAL_1_3";
32 case SPV_ENV_UNIVERSAL_1_4:
33 return stream << "SPV_ENV_UNIVERSAL_1_4";
34 default:
35 return stream << (unsigned)target;
36 }
37 }
38
39 namespace spvtools {
40 namespace val {
41 namespace {
42
43 using ::testing::Combine;
44 using ::testing::Eq;
45 using ::testing::HasSubstr;
46 using ::testing::Values;
47
48 using ValidateMemory = spvtest::ValidateBase<bool>;
49
50 TEST_F(ValidateMemory, VulkanUniformConstantOnNonOpaqueResourceBad) {
51 std::string spirv = R"(
52 OpCapability Shader
53 OpMemoryModel Logical GLSL450
54 OpEntryPoint Fragment %func "func"
55 OpExecutionMode %func OriginUpperLeft
56 %float = OpTypeFloat 32
57 %float_ptr = OpTypePointer UniformConstant %float
58 %2 = OpVariable %float_ptr UniformConstant
59 %void = OpTypeVoid
60 %functy = OpTypeFunction %void
61 %func = OpFunction %void None %functy
62 %1 = OpLabel
63 OpReturn
64 OpFunctionEnd
65 )";
66 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
67 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
68 EXPECT_THAT(getDiagnosticString(),
69 AnyVUID("VUID-StandaloneSpirv-UniformConstant-04655"));
70 EXPECT_THAT(
71 getDiagnosticString(),
72 HasSubstr("Variables identified with the UniformConstant storage class "
73 "are used only as handles to refer to opaque resources. Such "
74 "variables must be typed as OpTypeImage, OpTypeSampler, "
75 "OpTypeSampledImage, OpTypeAccelerationStructureKHR, "
76 "or an array of one of these types."));
77 }
78
79 TEST_F(ValidateMemory, VulkanUniformConstantOnOpaqueResourceGood) {
80 std::string spirv = R"(
81 OpCapability Shader
82 OpMemoryModel Logical GLSL450
83 OpEntryPoint Fragment %func "func"
84 OpExecutionMode %func OriginUpperLeft
85 OpDecorate %2 DescriptorSet 0
86 OpDecorate %2 Binding 0
87 %sampler = OpTypeSampler
88 %sampler_ptr = OpTypePointer UniformConstant %sampler
89 %2 = OpVariable %sampler_ptr UniformConstant
90 %void = OpTypeVoid
91 %functy = OpTypeFunction %void
92 %func = OpFunction %void None %functy
93 %1 = OpLabel
94 OpReturn
95 OpFunctionEnd
96 )";
97 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
98 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
99 }
100
101 TEST_F(ValidateMemory, VulkanUniformConstantOnNonOpaqueResourceArrayBad) {
102 std::string spirv = R"(
103 OpCapability Shader
104 OpMemoryModel Logical GLSL450
105 OpEntryPoint Fragment %func "func"
106 OpExecutionMode %func OriginUpperLeft
107 %float = OpTypeFloat 32
108 %uint = OpTypeInt 32 0
109 %array_size = OpConstant %uint 5
110 %array = OpTypeArray %float %array_size
111 %array_ptr = OpTypePointer UniformConstant %array
112 %2 = OpVariable %array_ptr UniformConstant
113 %void = OpTypeVoid
114 %functy = OpTypeFunction %void
115 %func = OpFunction %void None %functy
116 %1 = OpLabel
117 OpReturn
118 OpFunctionEnd
119 )";
120 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
121 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
122 EXPECT_THAT(getDiagnosticString(),
123 AnyVUID("VUID-StandaloneSpirv-UniformConstant-04655"));
124 EXPECT_THAT(
125 getDiagnosticString(),
126 HasSubstr("Variables identified with the UniformConstant storage class "
127 "are used only as handles to refer to opaque resources. Such "
128 "variables must be typed as OpTypeImage, OpTypeSampler, "
129 "OpTypeSampledImage, OpTypeAccelerationStructureKHR, "
130 "or an array of one of these types."));
131 }
132
133 TEST_F(ValidateMemory, VulkanUniformConstantOnOpaqueResourceArrayGood) {
134 std::string spirv = R"(
135 OpCapability Shader
136 OpMemoryModel Logical GLSL450
137 OpEntryPoint Fragment %func "func"
138 OpExecutionMode %func OriginUpperLeft
139 OpDecorate %2 DescriptorSet 0
140 OpDecorate %2 Binding 0
141 %sampler = OpTypeSampler
142 %uint = OpTypeInt 32 0
143 %array_size = OpConstant %uint 5
144 %array = OpTypeArray %sampler %array_size
145 %array_ptr = OpTypePointer UniformConstant %array
146 %2 = OpVariable %array_ptr UniformConstant
147 %void = OpTypeVoid
148 %functy = OpTypeFunction %void
149 %func = OpFunction %void None %functy
150 %1 = OpLabel
151 OpReturn
152 OpFunctionEnd
153 )";
154 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
155 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
156 }
157
158 TEST_F(ValidateMemory, VulkanUniformConstantOnOpaqueResourceRuntimeArrayGood) {
159 std::string spirv = R"(
160 OpCapability RuntimeDescriptorArrayEXT
161 OpCapability Shader
162 OpExtension "SPV_EXT_descriptor_indexing"
163 OpMemoryModel Logical GLSL450
164 OpEntryPoint Fragment %func "func"
165 OpExecutionMode %func OriginUpperLeft
166 OpDecorate %2 DescriptorSet 0
167 OpDecorate %2 Binding 0
168 %sampler = OpTypeSampler
169 %uint = OpTypeInt 32 0
170 %array = OpTypeRuntimeArray %sampler
171 %array_ptr = OpTypePointer UniformConstant %array
172 %2 = OpVariable %array_ptr UniformConstant
173 %void = OpTypeVoid
174 %functy = OpTypeFunction %void
175 %func = OpFunction %void None %functy
176 %1 = OpLabel
177 OpReturn
178 OpFunctionEnd
179 )";
180 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
181 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
182 }
183
184 TEST_F(ValidateMemory, VulkanUniformOnIntBad) {
185 char src[] = R"(
186 OpCapability Shader
187 OpMemoryModel Logical GLSL450
188 OpEntryPoint GLCompute %kernel "main"
189 OpExecutionMode %kernel LocalSize 1 1 1
190
191 OpDecorate %var DescriptorSet 0
192 OpDecorate %var Binding 0
193
194 %voidty = OpTypeVoid
195 %kernelty = OpTypeFunction %voidty
196 %intty = OpTypeInt 32 0
197 %varty = OpTypePointer Uniform %intty
198 %value = OpConstant %intty 42
199
200 %var = OpVariable %varty Uniform
201
202 %kernel = OpFunction %voidty None %kernelty
203 %label = OpLabel
204 OpStore %var %value
205 OpReturn
206 OpFunctionEnd
207 )";
208 CompileSuccessfully(src, SPV_ENV_VULKAN_1_1);
209 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
210 EXPECT_THAT(getDiagnosticString(),
211 AnyVUID("VUID-StandaloneSpirv-Uniform-06807"));
212 EXPECT_THAT(
213 getDiagnosticString(),
214 HasSubstr("From Vulkan spec:\n"
215 "Variables identified with the Uniform storage class are used "
216 "to access transparent buffer backed resources. Such variables "
217 "must be typed as OpTypeStruct, or an array of this type"));
218 }
219
220 // #version 440
221 // #extension GL_EXT_nonuniform_qualifier : enable
222 // layout(binding = 1) uniform sampler2D s2d[][2];
223 // layout(location = 0) in nonuniformEXT int i;
224 // void main()
225 // {
226 // vec4 v = texture(s2d[i][i], vec2(0.3));
227 // }
228 TEST_F(ValidateMemory, VulkanUniformOnRuntimeArrayOfArrayBad) {
229 char src[] = R"(
230 OpCapability Shader
231 OpCapability ShaderNonUniformEXT
232 OpCapability RuntimeDescriptorArrayEXT
233 OpCapability SampledImageArrayNonUniformIndexingEXT
234 OpExtension "SPV_EXT_descriptor_indexing"
235 %1 = OpExtInstImport "GLSL.std.450"
236 OpMemoryModel Logical GLSL450
237 OpEntryPoint Vertex %main "main" %i
238 OpSource GLSL 440
239 OpSourceExtension "GL_EXT_nonuniform_qualifier"
240 OpName %main "main"
241 OpName %v "v"
242 OpName %s2d "s2d"
243 OpName %i "i"
244 OpDecorate %s2d DescriptorSet 0
245 OpDecorate %s2d Binding 1
246 OpDecorate %i Location 0
247 OpDecorate %i NonUniformEXT
248 OpDecorate %21 NonUniformEXT
249 OpDecorate %22 NonUniformEXT
250 OpDecorate %25 NonUniformEXT
251 %void = OpTypeVoid
252 %3 = OpTypeFunction %void
253 %float = OpTypeFloat 32
254 %v4float = OpTypeVector %float 4
255 %_ptr_Function_v4float = OpTypePointer Function %v4float
256 %10 = OpTypeImage %float 2D 0 0 0 1 Unknown
257 %11 = OpTypeSampledImage %10
258 %uint = OpTypeInt 32 0
259 %uint_2 = OpConstant %uint 2
260 %_arr_11_uint_2 = OpTypeArray %11 %uint_2
261 %_runtimearr__arr_11_uint_2 = OpTypeRuntimeArray %_arr_11_uint_2
262 %_ptr_Uniform__runtimearr__arr_11_uint_2 = OpTypePointer Uniform %_runtimearr__arr_11_uint_2
263 %s2d = OpVariable %_ptr_Uniform__runtimearr__arr_11_uint_2 Uniform
264 %int = OpTypeInt 32 1
265 %_ptr_Input_int = OpTypePointer Input %int
266 %i = OpVariable %_ptr_Input_int Input
267 %_ptr_Uniform_11 = OpTypePointer Uniform %11
268 %v2float = OpTypeVector %float 2
269 %float_0_300000012 = OpConstant %float 0.300000012
270 %28 = OpConstantComposite %v2float %float_0_300000012 %float_0_300000012
271 %float_0 = OpConstant %float 0
272 %main = OpFunction %void None %3
273 %5 = OpLabel
274 %v = OpVariable %_ptr_Function_v4float Function
275 %21 = OpLoad %int %i
276 %22 = OpLoad %int %i
277 %24 = OpAccessChain %_ptr_Uniform_11 %s2d %21 %22
278 %25 = OpLoad %11 %24
279 %30 = OpImageSampleExplicitLod %v4float %25 %28 Lod %float_0
280 OpStore %v %30
281 OpReturn
282 OpFunctionEnd
283 )";
284 CompileSuccessfully(src, SPV_ENV_VULKAN_1_1);
285 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
286 EXPECT_THAT(getDiagnosticString(),
287 AnyVUID("VUID-StandaloneSpirv-Uniform-06807"));
288 EXPECT_THAT(
289 getDiagnosticString(),
290 HasSubstr("From Vulkan spec:\n"
291 "Variables identified with the Uniform storage class are used "
292 "to access transparent buffer backed resources. Such variables "
293 "must be typed as OpTypeStruct, or an array of this type"));
294 }
295
296 // #version 440
297 // layout (set=1, binding=1) uniform sampler2D variableName[2][2];
298 // void main() {
299 // }
300 TEST_F(ValidateMemory, VulkanUniformOnArrayOfArrayBad) {
301 char src[] = R"(
302 OpCapability Shader
303 %1 = OpExtInstImport "GLSL.std.450"
304 OpMemoryModel Logical GLSL450
305 OpEntryPoint Vertex %main "main"
306 OpSource GLSL 440
307 OpName %main "main"
308 OpName %variableName "variableName"
309 OpDecorate %variableName DescriptorSet 1
310 OpDecorate %variableName Binding 1
311 %void = OpTypeVoid
312 %3 = OpTypeFunction %void
313 %float = OpTypeFloat 32
314 %7 = OpTypeImage %float 2D 0 0 0 1 Unknown
315 %8 = OpTypeSampledImage %7
316 %uint = OpTypeInt 32 0
317 %uint_2 = OpConstant %uint 2
318 %_arr_8_uint_2 = OpTypeArray %8 %uint_2
319 %_arr__arr_8_uint_2_uint_2 = OpTypeArray %_arr_8_uint_2 %uint_2
320 %_ptr_Uniform__arr__arr_8_uint_2_uint_2 = OpTypePointer Uniform %_arr__arr_8_uint_2_uint_2
321 %variableName = OpVariable %_ptr_Uniform__arr__arr_8_uint_2_uint_2 Uniform
322 %main = OpFunction %void None %3
323 %5 = OpLabel
324 OpReturn
325 OpFunctionEnd
326 )";
327 CompileSuccessfully(src, SPV_ENV_VULKAN_1_1);
328 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
329 EXPECT_THAT(getDiagnosticString(),
330 AnyVUID("VUID-StandaloneSpirv-Uniform-06807"));
331 EXPECT_THAT(
332 getDiagnosticString(),
333 HasSubstr("From Vulkan spec:\n"
334 "Variables identified with the Uniform storage class are used "
335 "to access transparent buffer backed resources. Such variables "
336 "must be typed as OpTypeStruct, or an array of this type"));
337 }
338
339 TEST_F(ValidateMemory, MismatchingStorageClassesBad) {
340 std::string spirv = R"(
341 OpCapability Shader
342 OpMemoryModel Logical GLSL450
343 OpEntryPoint Fragment %func "func"
344 OpExecutionMode %func OriginUpperLeft
345 %float = OpTypeFloat 32
346 %float_ptr = OpTypePointer Uniform %float
347 %void = OpTypeVoid
348 %functy = OpTypeFunction %void
349 %func = OpFunction %void None %functy
350 %1 = OpLabel
351 %2 = OpVariable %float_ptr Function
352 OpReturn
353 OpFunctionEnd
354 )";
355 CompileSuccessfully(spirv.c_str());
356 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
357 EXPECT_THAT(getDiagnosticString(),
358 HasSubstr("Storage class must match result type storage class"));
359 }
360
361 TEST_F(ValidateMemory, MatchingStorageClassesGood) {
362 std::string spirv = R"(
363 OpCapability Shader
364 OpMemoryModel Logical GLSL450
365 OpEntryPoint Fragment %func "func"
366 OpExecutionMode %func OriginUpperLeft
367 %float = OpTypeFloat 32
368 %float_ptr = OpTypePointer Function %float
369 %void = OpTypeVoid
370 %functy = OpTypeFunction %void
371 %func = OpFunction %void None %functy
372 %1 = OpLabel
373 %2 = OpVariable %float_ptr Function
374 OpReturn
375 OpFunctionEnd
376 )";
377 CompileSuccessfully(spirv.c_str());
378 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
379 }
380
381 TEST_F(ValidateMemory, VulkanInitializerWithOutputStorageClassesGood) {
382 std::string spirv = R"(
383 OpCapability Shader
384 OpMemoryModel Logical GLSL450
385 OpEntryPoint Fragment %func "func"
386 OpExecutionMode %func OriginUpperLeft
387 %float = OpTypeFloat 32
388 %float_ptr = OpTypePointer Output %float
389 %init_val = OpConstant %float 1.0
390 %1 = OpVariable %float_ptr Output %init_val
391 %void = OpTypeVoid
392 %functy = OpTypeFunction %void
393 %func = OpFunction %void None %functy
394 %2 = OpLabel
395 OpReturn
396 OpFunctionEnd
397 )";
398 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
399 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
400 }
401
402 TEST_F(ValidateMemory, VulkanInitializerWithFunctionStorageClassesGood) {
403 std::string spirv = R"(
404 OpCapability Shader
405 OpMemoryModel Logical GLSL450
406 OpEntryPoint Fragment %func "func"
407 OpExecutionMode %func OriginUpperLeft
408 %float = OpTypeFloat 32
409 %float_ptr = OpTypePointer Function %float
410 %init_val = OpConstant %float 1.0
411 %void = OpTypeVoid
412 %functy = OpTypeFunction %void
413 %func = OpFunction %void None %functy
414 %1 = OpLabel
415 %2 = OpVariable %float_ptr Function %init_val
416 OpReturn
417 OpFunctionEnd
418 )";
419 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
420 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
421 }
422
423 TEST_F(ValidateMemory, VulkanInitializerWithPrivateStorageClassesGood) {
424 std::string spirv = R"(
425 OpCapability Shader
426 OpMemoryModel Logical GLSL450
427 OpEntryPoint Fragment %func "func"
428 OpExecutionMode %func OriginUpperLeft
429 %float = OpTypeFloat 32
430 %float_ptr = OpTypePointer Private %float
431 %init_val = OpConstant %float 1.0
432 %1 = OpVariable %float_ptr Private %init_val
433 %void = OpTypeVoid
434 %functy = OpTypeFunction %void
435 %func = OpFunction %void None %functy
436 %2 = OpLabel
437 OpReturn
438 OpFunctionEnd
439 )";
440 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
441 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
442 }
443
444 TEST_F(ValidateMemory, VulkanInitializerWithDisallowedStorageClassesBad) {
445 std::string spirv = R"(
446 OpCapability Shader
447 OpMemoryModel Logical GLSL450
448 OpEntryPoint Fragment %func "func"
449 OpExecutionMode %func OriginUpperLeft
450 %float = OpTypeFloat 32
451 %float_ptr = OpTypePointer Input %float
452 %init_val = OpConstant %float 1.0
453 %1 = OpVariable %float_ptr Input %init_val
454 %void = OpTypeVoid
455 %functy = OpTypeFunction %void
456 %func = OpFunction %void None %functy
457 %2 = OpLabel
458 OpReturn
459 OpFunctionEnd
460 )";
461 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
462 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
463 EXPECT_THAT(getDiagnosticString(),
464 AnyVUID("VUID-StandaloneSpirv-OpVariable-04651"));
465 EXPECT_THAT(
466 getDiagnosticString(),
467 HasSubstr("OpVariable, <id> '5[%5]', has a disallowed initializer & "
468 "storage class combination.\nFrom Vulkan spec:\nVariable "
469 "declarations that include initializers must have one of the "
470 "following storage classes: Output, Private, Function or "
471 "Workgroup\n %5 "
472 "= OpVariable %_ptr_Input_float Input %float_1\n"));
473 }
474
475 TEST_F(ValidateMemory, UniversalInitializerWithDisallowedStorageClassesBad) {
476 std::string spirv = R"(
477 OpCapability Shader
478 OpMemoryModel Logical GLSL450
479 OpEntryPoint Fragment %func "func"
480 OpExecutionMode %func OriginUpperLeft
481 %float = OpTypeFloat 32
482 %float_ptr = OpTypePointer Input %float
483 %init_val = OpConstant %float 1.0
484 %1 = OpVariable %float_ptr Input %init_val
485 %void = OpTypeVoid
486 %functy = OpTypeFunction %void
487 %func = OpFunction %void None %functy
488 %2 = OpLabel
489 OpReturn
490 OpFunctionEnd
491 )";
492 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
493 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
494 EXPECT_THAT(
495 getDiagnosticString(),
496 HasSubstr(
497 "OpVariable, <id> '5[%5]', initializer are not allowed for Input"));
498 }
499
500 TEST_F(ValidateMemory, InitializerWithTaskPayloadWorkgroupEXT) {
501 std::string spirv = R"(
502 OpCapability MeshShadingEXT
503 OpExtension "SPV_EXT_mesh_shader"
504 OpMemoryModel Logical GLSL450
505 OpEntryPoint TaskEXT %main "main" %payload
506 %void = OpTypeVoid
507 %func = OpTypeFunction %void
508 %uint = OpTypeInt 32 0
509 %_ptr_TaskPayloadWorkgroupEXT = OpTypePointer TaskPayloadWorkgroupEXT %uint
510 %uint_1 = OpConstant %uint 1
511 %payload = OpVariable %_ptr_TaskPayloadWorkgroupEXT TaskPayloadWorkgroupEXT %uint_1
512 %main = OpFunction %void None %func
513 %label = OpLabel
514 OpReturn
515 OpFunctionEnd
516 )";
517 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_5);
518 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_5));
519 EXPECT_THAT(getDiagnosticString(),
520 HasSubstr("OpVariable, <id> '2[%2]', initializer are not allowed "
521 "for TaskPayloadWorkgroupEXT"));
522 }
523
524 TEST_F(ValidateMemory, ArrayLenCorrectResultType) {
525 std::string spirv = R"(
526 OpCapability Shader
527 OpMemoryModel Logical GLSL450
528 OpEntryPoint Fragment %1 "main"
529 OpExecutionMode %1 OriginUpperLeft
530 %void = OpTypeVoid
531 %3 = OpTypeFunction %void
532 %float = OpTypeFloat 32
533 %uint = OpTypeInt 32 0
534 %_runtimearr_float = OpTypeRuntimeArray %float
535 %_struct_7 = OpTypeStruct %_runtimearr_float
536 %_ptr_Function__struct_7 = OpTypePointer Function %_struct_7
537 %1 = OpFunction %void None %3
538 %9 = OpLabel
539 %10 = OpVariable %_ptr_Function__struct_7 Function
540 %11 = OpArrayLength %uint %10 0
541 OpReturn
542 OpFunctionEnd
543
544 )";
545
546 CompileSuccessfully(spirv.c_str());
547 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
548 }
549
550 TEST_F(ValidateMemory, ArrayLenIndexCorrectWith2Members) {
551 std::string spirv = R"(
552 OpCapability Shader
553 OpMemoryModel Logical GLSL450
554 OpEntryPoint Fragment %1 "main"
555 OpExecutionMode %1 OriginUpperLeft
556 %void = OpTypeVoid
557 %3 = OpTypeFunction %void
558 %float = OpTypeFloat 32
559 %uint = OpTypeInt 32 0
560 %_runtimearr_float = OpTypeRuntimeArray %float
561 %_struct_7 = OpTypeStruct %float %_runtimearr_float
562 %_ptr_Function__struct_7 = OpTypePointer Function %_struct_7
563 %1 = OpFunction %void None %3
564 %9 = OpLabel
565 %10 = OpVariable %_ptr_Function__struct_7 Function
566 %11 = OpArrayLength %uint %10 1
567 OpReturn
568 OpFunctionEnd
569
570 )";
571
572 CompileSuccessfully(spirv.c_str());
573 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
574 }
575
576 TEST_F(ValidateMemory, ArrayLenResultNotIntType) {
577 std::string spirv = R"(
578 OpCapability Shader
579 OpMemoryModel Logical GLSL450
580 OpEntryPoint Fragment %1 "main"
581 OpExecutionMode %1 OriginUpperLeft
582 %void = OpTypeVoid
583 %3 = OpTypeFunction %void
584 %float = OpTypeFloat 32
585 %_runtimearr_float = OpTypeRuntimeArray %float
586 %_struct_6 = OpTypeStruct %_runtimearr_float
587 %_ptr_Function__struct_6 = OpTypePointer Function %_struct_6
588 %1 = OpFunction %void None %3
589 %8 = OpLabel
590 %9 = OpVariable %_ptr_Function__struct_6 Function
591 %10 = OpArrayLength %float %9 0
592 OpReturn
593 OpFunctionEnd
594 )";
595
596 CompileSuccessfully(spirv.c_str());
597 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
598 EXPECT_THAT(
599 getDiagnosticString(),
600 HasSubstr(
601 "The Result Type of OpArrayLength <id> '10[%10]' must be OpTypeInt "
602 "with width 32 or 64 and signedness 0.\n %10 = OpArrayLength %float "
603 "%9 "
604 "0\n"));
605 }
606
607 TEST_F(ValidateMemory, ArrayLenResultUnsizedPointer) {
608 std::string spirv = R"(
609 OpCapability ClipDistance
610 OpMemoryModel Logical GLSL450
611 OpName %65312 "arrayLen"
612 %void = OpTypeVoid
613 %8 = OpTypeFunction %void
614 %float = OpTypeFloat 32
615 %v2float = OpTypeVector %float 2
616 %_struct_5 = OpTypeStruct %v2float
617 %_ptr_Uniform__struct_5 = OpTypePointer Uniform %_struct_5
618 %6 = OpVariable %_ptr_Uniform__struct_5 Uniform
619 %_ptr_Uniform_v2float = OpTypePointer Uniform %v2float
620 %2105376 = OpFunction %void None %8
621 %8224 = OpLabel
622 %65312 = OpArrayLength %_ptr_Uniform_v2float %6 538976288
623 OpUnreachable
624 OpFunctionEnd
625 )";
626
627 CompileSuccessfully(spirv.c_str());
628 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
629 EXPECT_THAT(
630 getDiagnosticString(),
631 HasSubstr("The Result Type of OpArrayLength <id> '1[%arrayLen]' must be "
632 "OpTypeInt with width 32 or 64 and signedness 0."));
633 }
634
635 TEST_F(ValidateMemory, ArrayLenResultNot32bits) {
636 std::string spirv = R"(
637 OpCapability Shader
638 OpCapability Int16
639 OpMemoryModel Logical GLSL450
640 OpEntryPoint Fragment %1 "main"
641 OpExecutionMode %1 OriginUpperLeft
642 %void = OpTypeVoid
643 %3 = OpTypeFunction %void
644 %float = OpTypeFloat 32
645 %ushort = OpTypeInt 16 0
646 %_runtimearr_float = OpTypeRuntimeArray %float
647 %_struct_7 = OpTypeStruct %_runtimearr_float
648 %_ptr_Function__struct_7 = OpTypePointer Function %_struct_7
649 %1 = OpFunction %void None %3
650 %9 = OpLabel
651 %10 = OpVariable %_ptr_Function__struct_7 Function
652 %11 = OpArrayLength %ushort %10 0
653 OpReturn
654 OpFunctionEnd
655
656 )";
657
658 CompileSuccessfully(spirv.c_str());
659 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
660 EXPECT_THAT(
661 getDiagnosticString(),
662 HasSubstr(
663 "The Result Type of OpArrayLength <id> '11[%11]' must be OpTypeInt "
664 "with width 32 or 64 and signedness 0.\n %11 = OpArrayLength "
665 "%ushort %10 "
666 "0\n"));
667 }
668
669 TEST_F(ValidateMemory, ArrayLenResultSigned) {
670 std::string spirv = R"(
671 OpCapability Shader
672 OpMemoryModel Logical GLSL450
673 OpEntryPoint Fragment %1 "main"
674 OpExecutionMode %1 OriginUpperLeft
675 %void = OpTypeVoid
676 %3 = OpTypeFunction %void
677 %float = OpTypeFloat 32
678 %int = OpTypeInt 32 1
679 %_runtimearr_float = OpTypeRuntimeArray %float
680 %_struct_7 = OpTypeStruct %_runtimearr_float
681 %_ptr_Function__struct_7 = OpTypePointer Function %_struct_7
682 %1 = OpFunction %void None %3
683 %9 = OpLabel
684 %10 = OpVariable %_ptr_Function__struct_7 Function
685 %11 = OpArrayLength %int %10 0
686 OpReturn
687 OpFunctionEnd
688
689 )";
690
691 CompileSuccessfully(spirv.c_str());
692 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
693 EXPECT_THAT(
694 getDiagnosticString(),
695 HasSubstr(
696 "The Result Type of OpArrayLength <id> '11[%11]' must be OpTypeInt "
697 "with width 32 or 64 and signedness 0.\n %11 = OpArrayLength %int "
698 "%10 "
699 "0\n"));
700 }
701
702 TEST_F(ValidateMemory, ArrayLenInputNotStruct) {
703 std::string spirv = R"(
704 OpCapability Shader
705 OpMemoryModel Logical GLSL450
706 OpEntryPoint Fragment %1 "main"
707 OpExecutionMode %1 OriginUpperLeft
708 %void = OpTypeVoid
709 %3 = OpTypeFunction %void
710 %float = OpTypeFloat 32
711 %uint = OpTypeInt 32 0
712 %_runtimearr_float = OpTypeRuntimeArray %float
713 %_struct_7 = OpTypeStruct %_runtimearr_float
714 %_ptr_Function_float = OpTypePointer Function %float
715 %1 = OpFunction %void None %3
716 %9 = OpLabel
717 %10 = OpVariable %_ptr_Function_float Function
718 %11 = OpArrayLength %uint %10 0
719 OpReturn
720 OpFunctionEnd
721
722 )";
723
724 CompileSuccessfully(spirv.c_str());
725 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
726 EXPECT_THAT(getDiagnosticString(),
727 HasSubstr("The Structure's type in OpArrayLength <id> '11[%11]' "
728 "must be a pointer to an OpTypeStruct."));
729 }
730
731 TEST_F(ValidateMemory, ArrayLenInputLastMemberNoRTA) {
732 std::string spirv = R"(
733 OpCapability Shader
734 OpMemoryModel Logical GLSL450
735 OpEntryPoint Fragment %1 "main"
736 OpExecutionMode %1 OriginUpperLeft
737 %void = OpTypeVoid
738 %3 = OpTypeFunction %void
739 %float = OpTypeFloat 32
740 %uint = OpTypeInt 32 0
741 %_runtimearr_float = OpTypeRuntimeArray %float
742 %_struct_7 = OpTypeStruct %float
743 %_ptr_Function__struct_7 = OpTypePointer Function %_struct_7
744 %1 = OpFunction %void None %3
745 %9 = OpLabel
746 %10 = OpVariable %_ptr_Function__struct_7 Function
747 %11 = OpArrayLength %uint %10 0
748 OpReturn
749 OpFunctionEnd
750
751 )";
752
753 CompileSuccessfully(spirv.c_str());
754 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
755 EXPECT_THAT(
756 getDiagnosticString(),
757 HasSubstr("The Structure's last member in OpArrayLength <id> '11[%11]' "
758 "must be an OpTypeRuntimeArray.\n %11 = OpArrayLength %uint "
759 "%10 0\n"));
760 }
761
762 TEST_F(ValidateMemory, ArrayLenInputLastMemberNoRTA2) {
763 std::string spirv = R"(
764 OpCapability Shader
765 OpMemoryModel Logical GLSL450
766 OpEntryPoint Fragment %1 "main"
767 OpExecutionMode %1 OriginUpperLeft
768 %void = OpTypeVoid
769 %3 = OpTypeFunction %void
770 %float = OpTypeFloat 32
771 %uint = OpTypeInt 32 0
772 %_runtimearr_float = OpTypeRuntimeArray %float
773 %_struct_7 = OpTypeStruct %_runtimearr_float %float
774 %_ptr_Function__struct_7 = OpTypePointer Function %_struct_7
775 %1 = OpFunction %void None %3
776 %9 = OpLabel
777 %10 = OpVariable %_ptr_Function__struct_7 Function
778 %11 = OpArrayLength %uint %10 1
779 OpReturn
780 OpFunctionEnd
781
782 )";
783
784 CompileSuccessfully(spirv.c_str());
785 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
786 EXPECT_THAT(
787 getDiagnosticString(),
788 HasSubstr("The Structure's last member in OpArrayLength <id> '11[%11]' "
789 "must be an OpTypeRuntimeArray.\n %11 = OpArrayLength %uint "
790 "%10 1\n"));
791 }
792
793 TEST_F(ValidateMemory, ArrayLenIndexNotLastMember) {
794 std::string spirv = R"(
795 OpCapability Shader
796 OpMemoryModel Logical GLSL450
797 OpEntryPoint Fragment %1 "main"
798 OpExecutionMode %1 OriginUpperLeft
799 %void = OpTypeVoid
800 %3 = OpTypeFunction %void
801 %float = OpTypeFloat 32
802 %uint = OpTypeInt 32 0
803 %_runtimearr_float = OpTypeRuntimeArray %float
804 %_struct_7 = OpTypeStruct %float %_runtimearr_float
805 %_ptr_Function__struct_7 = OpTypePointer Function %_struct_7
806 %1 = OpFunction %void None %3
807 %9 = OpLabel
808 %10 = OpVariable %_ptr_Function__struct_7 Function
809 %11 = OpArrayLength %uint %10 0
810 OpReturn
811 OpFunctionEnd
812
813 )";
814
815 CompileSuccessfully(spirv.c_str());
816 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
817 EXPECT_THAT(
818 getDiagnosticString(),
819 HasSubstr(
820 "The array member in OpArrayLength <id> '11[%11]' must be the "
821 "last member of the struct.\n %11 = OpArrayLength %uint %10 0\n"));
822 }
823
824 TEST_F(ValidateMemory, ArrayLenIndexNotPointerToStruct) {
825 std::string spirv = R"(
826 OpCapability Shader
827 OpMemoryModel Logical GLSL450
828 OpEntryPoint Fragment %1 "main"
829 OpExecutionMode %1 OriginUpperLeft
830 %void = OpTypeVoid
831 %3 = OpTypeFunction %void
832 %float = OpTypeFloat 32
833 %uint = OpTypeInt 32 0
834 %_runtimearr_float = OpTypeRuntimeArray %float
835 %_struct_7 = OpTypeStruct %float
836 %_ptr_Function__struct_7 = OpTypePointer Function %_struct_7
837 %1 = OpFunction %void None %3
838 %9 = OpLabel
839 %10 = OpVariable %_ptr_Function__struct_7 Function
840 %11 = OpLoad %_struct_7 %10
841 %12 = OpArrayLength %uint %11 0
842 OpReturn
843 OpFunctionEnd
844
845 )";
846
847 CompileSuccessfully(spirv.c_str());
848 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
849 EXPECT_THAT(
850 getDiagnosticString(),
851 HasSubstr(
852 "The Structure's type in OpArrayLength <id> '12[%12]' must be a "
853 "pointer to an OpTypeStruct.\n %12 = OpArrayLength %uint %11 0\n"));
854 }
855
856 TEST_F(ValidateMemory, ArrayLenPointerIsAType) {
857 std::string spirv = R"(
858 OpCapability Shader
859 OpMemoryModel Logical GLSL450
860 OpEntryPoint Fragment %1 "main"
861 OpExecutionMode %1 OriginUpperLeft
862 %void = OpTypeVoid
863 %3 = OpTypeFunction %void
864 %float = OpTypeFloat 32
865 %uint = OpTypeInt 32 0
866 %1 = OpFunction %void None %3
867 %9 = OpLabel
868 %12 = OpArrayLength %uint %float 0
869 OpReturn
870 OpFunctionEnd
871
872 )";
873
874 CompileSuccessfully(spirv.c_str());
875 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
876 EXPECT_THAT(getDiagnosticString(),
877 HasSubstr("Operand '4[%float]' cannot be a "
878 "type"));
879 }
880
881 TEST_F(ValidateMemory, PushConstantNotStructGood) {
882 std::string spirv = R"(
883 OpCapability Shader
884 OpMemoryModel Logical GLSL450
885 OpEntryPoint Fragment %1 "main"
886 OpExecutionMode %1 OriginUpperLeft
887
888 %void = OpTypeVoid
889 %voidfn = OpTypeFunction %void
890 %float = OpTypeFloat 32
891 %ptr = OpTypePointer PushConstant %float
892 %pc = OpVariable %ptr PushConstant
893
894 %1 = OpFunction %void None %voidfn
895 %label = OpLabel
896 OpReturn
897 OpFunctionEnd
898 )";
899 CompileSuccessfully(spirv);
900 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
901 }
902
903 TEST_F(ValidateMemory, VulkanPushConstantNotStructBad) {
904 std::string spirv = R"(
905 OpCapability Shader
906 OpMemoryModel Logical GLSL450
907 OpEntryPoint Fragment %1 "main"
908 OpExecutionMode %1 OriginUpperLeft
909
910 %void = OpTypeVoid
911 %voidfn = OpTypeFunction %void
912 %float = OpTypeFloat 32
913 %ptr = OpTypePointer PushConstant %float
914 %pc = OpVariable %ptr PushConstant
915
916 %1 = OpFunction %void None %voidfn
917 %label = OpLabel
918 OpReturn
919 OpFunctionEnd
920 )";
921 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
922 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
923 EXPECT_THAT(getDiagnosticString(),
924 AnyVUID("VUID-StandaloneSpirv-PushConstant-06808"));
925 EXPECT_THAT(
926 getDiagnosticString(),
927 HasSubstr("PushConstant OpVariable <id> '6[%6]' has illegal "
928 "type.\nFrom Vulkan spec, Push Constant Interface section:\n"
929 "Such variables must be typed as OpTypeStruct"));
930 }
931
932 TEST_F(ValidateMemory, VulkanPushConstantArrayOfStructBad) {
933 std::string spirv = R"(
934 OpCapability Shader
935 OpMemoryModel Logical GLSL450
936 OpEntryPoint Fragment %1 "main"
937 OpExecutionMode %1 OriginUpperLeft
938
939 OpDecorate %struct Block
940 OpMemberDecorate %struct 0 Offset 0
941
942 %void = OpTypeVoid
943 %voidfn = OpTypeFunction %void
944 %float = OpTypeFloat 32
945 %int = OpTypeInt 32 0
946 %int_1 = OpConstant %int 1
947 %struct = OpTypeStruct %float
948 %array = OpTypeArray %struct %int_1
949 %ptr = OpTypePointer PushConstant %array
950 %pc = OpVariable %ptr PushConstant
951
952 %1 = OpFunction %void None %voidfn
953 %label = OpLabel
954 OpReturn
955 OpFunctionEnd
956 )";
957 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
958 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
959 EXPECT_THAT(getDiagnosticString(),
960 AnyVUID("VUID-StandaloneSpirv-PushConstant-06808"));
961 EXPECT_THAT(
962 getDiagnosticString(),
963 HasSubstr("PushConstant OpVariable <id> '10[%10]' has illegal "
964 "type.\nFrom Vulkan spec, Push Constant Interface section:\n"
965 "Such variables must be typed as OpTypeStruct"));
966 }
967
968 TEST_F(ValidateMemory, VulkanPushConstant) {
969 std::string spirv = R"(
970 OpCapability Shader
971 OpMemoryModel Logical GLSL450
972 OpEntryPoint Fragment %1 "main"
973 OpExecutionMode %1 OriginUpperLeft
974
975 OpDecorate %struct Block
976 OpMemberDecorate %struct 0 Offset 0
977
978 %void = OpTypeVoid
979 %voidfn = OpTypeFunction %void
980 %float = OpTypeFloat 32
981 %struct = OpTypeStruct %float
982 %ptr = OpTypePointer PushConstant %struct
983 %pc = OpVariable %ptr PushConstant
984
985 %1 = OpFunction %void None %voidfn
986 %label = OpLabel
987 OpReturn
988 OpFunctionEnd
989 )";
990 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
991 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
992 }
993
994 TEST_F(ValidateMemory, VulkanMemoryModelDeviceScopeLoadBad1) {
995 const std::string spirv = R"(
996 OpCapability Shader
997 OpCapability VulkanMemoryModelKHR
998 OpCapability Linkage
999 OpExtension "SPV_KHR_vulkan_memory_model"
1000 OpMemoryModel Logical VulkanKHR
1001 %void = OpTypeVoid
1002 %int = OpTypeInt 32 0
1003 %device = OpConstant %int 1
1004 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1005 %var = OpVariable %int_ptr_ssbo StorageBuffer
1006 %voidfn = OpTypeFunction %void
1007 %func = OpFunction %void None %voidfn
1008 %entry = OpLabel
1009 %load = OpLoad %int %var MakePointerVisibleKHR|NonPrivatePointerKHR %device
1010 OpReturn
1011 OpFunctionEnd
1012 )";
1013
1014 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1015 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1016 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1017 EXPECT_THAT(
1018 getDiagnosticString(),
1019 HasSubstr("Use of device scope with VulkanKHR memory model requires the "
1020 "VulkanMemoryModelDeviceScopeKHR capability"));
1021 }
1022
1023 TEST_F(ValidateMemory, VulkanMemoryModelDeviceScopeLoadBad2) {
1024 const std::string spirv = R"(
1025 OpCapability Shader
1026 OpCapability VulkanMemoryModelKHR
1027 OpCapability Linkage
1028 OpExtension "SPV_KHR_vulkan_memory_model"
1029 OpMemoryModel Logical VulkanKHR
1030 %void = OpTypeVoid
1031 %int = OpTypeInt 32 0
1032 %device = OpConstant %int 1
1033 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1034 %var = OpVariable %int_ptr_ssbo StorageBuffer
1035 %voidfn = OpTypeFunction %void
1036 %func = OpFunction %void None %voidfn
1037 %entry = OpLabel
1038 %load = OpLoad %int %var Aligned|MakePointerVisibleKHR|NonPrivatePointerKHR 4 %device
1039 OpReturn
1040 OpFunctionEnd
1041 )";
1042
1043 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1044 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1045 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1046 EXPECT_THAT(
1047 getDiagnosticString(),
1048 HasSubstr("Use of device scope with VulkanKHR memory model requires the "
1049 "VulkanMemoryModelDeviceScopeKHR capability"));
1050 }
1051
1052 TEST_F(ValidateMemory, VulkanMemoryModelDeviceScopeLoadGood1) {
1053 const std::string spirv = R"(
1054 OpCapability Shader
1055 OpCapability VulkanMemoryModelKHR
1056 OpCapability VulkanMemoryModelDeviceScopeKHR
1057 OpCapability Linkage
1058 OpExtension "SPV_KHR_vulkan_memory_model"
1059 OpMemoryModel Logical VulkanKHR
1060 %void = OpTypeVoid
1061 %int = OpTypeInt 32 0
1062 %device = OpConstant %int 1
1063 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1064 %var = OpVariable %int_ptr_ssbo StorageBuffer
1065 %voidfn = OpTypeFunction %void
1066 %func = OpFunction %void None %voidfn
1067 %entry = OpLabel
1068 %load = OpLoad %int %var MakePointerVisibleKHR|NonPrivatePointerKHR %device
1069 OpReturn
1070 OpFunctionEnd
1071 )";
1072
1073 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1074 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1075 }
1076
1077 TEST_F(ValidateMemory, VulkanMemoryModelDeviceScopeLoadGood2) {
1078 const std::string spirv = R"(
1079 OpCapability Shader
1080 OpCapability VulkanMemoryModelKHR
1081 OpCapability VulkanMemoryModelDeviceScopeKHR
1082 OpCapability Linkage
1083 OpExtension "SPV_KHR_vulkan_memory_model"
1084 OpMemoryModel Logical VulkanKHR
1085 %void = OpTypeVoid
1086 %int = OpTypeInt 32 0
1087 %device = OpConstant %int 1
1088 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1089 %var = OpVariable %int_ptr_ssbo StorageBuffer
1090 %voidfn = OpTypeFunction %void
1091 %func = OpFunction %void None %voidfn
1092 %entry = OpLabel
1093 %load = OpLoad %int %var Aligned|MakePointerVisibleKHR|NonPrivatePointerKHR 4 %device
1094 OpReturn
1095 OpFunctionEnd
1096 )";
1097
1098 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1099 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1100 }
1101
1102 TEST_F(ValidateMemory, VulkanMemoryModelDeviceScopeStoreBad1) {
1103 const std::string spirv = R"(
1104 OpCapability Shader
1105 OpCapability VulkanMemoryModelKHR
1106 OpCapability Linkage
1107 OpExtension "SPV_KHR_vulkan_memory_model"
1108 OpMemoryModel Logical VulkanKHR
1109 %void = OpTypeVoid
1110 %int = OpTypeInt 32 0
1111 %device = OpConstant %int 1
1112 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1113 %var = OpVariable %int_ptr_ssbo StorageBuffer
1114 %voidfn = OpTypeFunction %void
1115 %func = OpFunction %void None %voidfn
1116 %entry = OpLabel
1117 OpStore %var %device MakePointerAvailableKHR|NonPrivatePointerKHR %device
1118 OpReturn
1119 OpFunctionEnd
1120 )";
1121
1122 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1123 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1124 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1125 EXPECT_THAT(
1126 getDiagnosticString(),
1127 HasSubstr("Use of device scope with VulkanKHR memory model requires the "
1128 "VulkanMemoryModelDeviceScopeKHR capability"));
1129 }
1130
1131 TEST_F(ValidateMemory, VulkanMemoryModelDeviceScopeStoreBad2) {
1132 const std::string spirv = R"(
1133 OpCapability Shader
1134 OpCapability VulkanMemoryModelKHR
1135 OpCapability Linkage
1136 OpExtension "SPV_KHR_vulkan_memory_model"
1137 OpMemoryModel Logical VulkanKHR
1138 %void = OpTypeVoid
1139 %int = OpTypeInt 32 0
1140 %device = OpConstant %int 1
1141 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1142 %var = OpVariable %int_ptr_ssbo StorageBuffer
1143 %voidfn = OpTypeFunction %void
1144 %func = OpFunction %void None %voidfn
1145 %entry = OpLabel
1146 OpStore %var %device Aligned|MakePointerAvailableKHR|NonPrivatePointerKHR 4 %device
1147 OpReturn
1148 OpFunctionEnd
1149 )";
1150
1151 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1152 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1153 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1154 EXPECT_THAT(
1155 getDiagnosticString(),
1156 HasSubstr("Use of device scope with VulkanKHR memory model requires the "
1157 "VulkanMemoryModelDeviceScopeKHR capability"));
1158 }
1159
1160 TEST_F(ValidateMemory, VulkanMemoryModelDeviceScopeStoreGood1) {
1161 const std::string spirv = R"(
1162 OpCapability Shader
1163 OpCapability VulkanMemoryModelKHR
1164 OpCapability VulkanMemoryModelDeviceScopeKHR
1165 OpCapability Linkage
1166 OpExtension "SPV_KHR_vulkan_memory_model"
1167 OpMemoryModel Logical VulkanKHR
1168 %void = OpTypeVoid
1169 %int = OpTypeInt 32 0
1170 %device = OpConstant %int 1
1171 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1172 %var = OpVariable %int_ptr_ssbo StorageBuffer
1173 %voidfn = OpTypeFunction %void
1174 %func = OpFunction %void None %voidfn
1175 %entry = OpLabel
1176 OpStore %var %device MakePointerAvailableKHR|NonPrivatePointerKHR %device
1177 OpReturn
1178 OpFunctionEnd
1179 )";
1180
1181 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1182 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1183 }
1184
1185 TEST_F(ValidateMemory, VulkanMemoryModelDeviceScopeStoreGood2) {
1186 const std::string spirv = R"(
1187 OpCapability Shader
1188 OpCapability VulkanMemoryModelKHR
1189 OpCapability VulkanMemoryModelDeviceScopeKHR
1190 OpCapability Linkage
1191 OpExtension "SPV_KHR_vulkan_memory_model"
1192 OpMemoryModel Logical VulkanKHR
1193 %void = OpTypeVoid
1194 %int = OpTypeInt 32 0
1195 %device = OpConstant %int 1
1196 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1197 %var = OpVariable %int_ptr_ssbo StorageBuffer
1198 %voidfn = OpTypeFunction %void
1199 %func = OpFunction %void None %voidfn
1200 %entry = OpLabel
1201 OpStore %var %device Aligned|MakePointerAvailableKHR|NonPrivatePointerKHR 4 %device
1202 OpReturn
1203 OpFunctionEnd
1204 )";
1205
1206 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1207 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1208 }
1209
1210 TEST_F(ValidateMemory, VulkanMemoryModelDeviceScopeCopyMemoryBad1) {
1211 const std::string spirv = R"(
1212 OpCapability Shader
1213 OpCapability VulkanMemoryModelKHR
1214 OpCapability Linkage
1215 OpExtension "SPV_KHR_vulkan_memory_model"
1216 OpMemoryModel Logical VulkanKHR
1217 %void = OpTypeVoid
1218 %int = OpTypeInt 32 0
1219 %device = OpConstant %int 1
1220 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1221 %var1 = OpVariable %int_ptr_ssbo StorageBuffer
1222 %var2 = OpVariable %int_ptr_ssbo StorageBuffer
1223 %voidfn = OpTypeFunction %void
1224 %func = OpFunction %void None %voidfn
1225 %entry = OpLabel
1226 OpCopyMemory %var1 %var2 MakePointerAvailableKHR|NonPrivatePointerKHR %device
1227 OpReturn
1228 OpFunctionEnd
1229 )";
1230
1231 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1232 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1233 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1234 EXPECT_THAT(
1235 getDiagnosticString(),
1236 HasSubstr("Use of device scope with VulkanKHR memory model requires the "
1237 "VulkanMemoryModelDeviceScopeKHR capability"));
1238 }
1239
1240 TEST_F(ValidateMemory, VulkanMemoryModelDeviceScopeCopyMemoryBad2) {
1241 const std::string spirv = R"(
1242 OpCapability Shader
1243 OpCapability VulkanMemoryModelKHR
1244 OpCapability Linkage
1245 OpExtension "SPV_KHR_vulkan_memory_model"
1246 OpMemoryModel Logical VulkanKHR
1247 %void = OpTypeVoid
1248 %int = OpTypeInt 32 0
1249 %device = OpConstant %int 1
1250 %workgroup = OpConstant %int 1
1251 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1252 %var1 = OpVariable %int_ptr_ssbo StorageBuffer
1253 %var2 = OpVariable %int_ptr_ssbo StorageBuffer
1254 %voidfn = OpTypeFunction %void
1255 %func = OpFunction %void None %voidfn
1256 %entry = OpLabel
1257 OpCopyMemory %var1 %var2 Aligned|MakePointerVisibleKHR|MakePointerAvailableKHR|NonPrivatePointerKHR 4 %device %workgroup
1258 OpReturn
1259 OpFunctionEnd
1260 )";
1261
1262 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1263 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1264 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1265 EXPECT_THAT(
1266 getDiagnosticString(),
1267 HasSubstr("Use of device scope with VulkanKHR memory model requires the "
1268 "VulkanMemoryModelDeviceScopeKHR capability"));
1269 }
1270
1271 TEST_F(ValidateMemory, VulkanMemoryModelDeviceScopeCopyMemoryBad3) {
1272 const std::string spirv = R"(
1273 OpCapability Shader
1274 OpCapability VulkanMemoryModelKHR
1275 OpCapability Linkage
1276 OpExtension "SPV_KHR_vulkan_memory_model"
1277 OpMemoryModel Logical VulkanKHR
1278 %void = OpTypeVoid
1279 %int = OpTypeInt 32 0
1280 %device = OpConstant %int 1
1281 %workgroup = OpConstant %int 1
1282 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1283 %var1 = OpVariable %int_ptr_ssbo StorageBuffer
1284 %var2 = OpVariable %int_ptr_ssbo StorageBuffer
1285 %voidfn = OpTypeFunction %void
1286 %func = OpFunction %void None %voidfn
1287 %entry = OpLabel
1288 OpCopyMemory %var1 %var2 Aligned|MakePointerVisibleKHR|MakePointerAvailableKHR|NonPrivatePointerKHR 4 %workgroup %device
1289 OpReturn
1290 OpFunctionEnd
1291 )";
1292
1293 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1294 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1295 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1296 EXPECT_THAT(
1297 getDiagnosticString(),
1298 HasSubstr("Use of device scope with VulkanKHR memory model requires the "
1299 "VulkanMemoryModelDeviceScopeKHR capability"));
1300 }
1301
1302 TEST_F(ValidateMemory, VulkanMemoryModelDeviceScopeCopyMemoryGood2) {
1303 const std::string spirv = R"(
1304 OpCapability Shader
1305 OpCapability VulkanMemoryModelKHR
1306 OpCapability VulkanMemoryModelDeviceScopeKHR
1307 OpCapability Linkage
1308 OpExtension "SPV_KHR_vulkan_memory_model"
1309 OpMemoryModel Logical VulkanKHR
1310 %void = OpTypeVoid
1311 %int = OpTypeInt 32 0
1312 %device = OpConstant %int 1
1313 %workgroup = OpConstant %int 2
1314 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1315 %var1 = OpVariable %int_ptr_ssbo StorageBuffer
1316 %var2 = OpVariable %int_ptr_ssbo StorageBuffer
1317 %voidfn = OpTypeFunction %void
1318 %func = OpFunction %void None %voidfn
1319 %entry = OpLabel
1320 OpCopyMemory %var1 %var2 Aligned|MakePointerVisibleKHR|MakePointerAvailableKHR|NonPrivatePointerKHR 4 %device %workgroup
1321 OpReturn
1322 OpFunctionEnd
1323 )";
1324
1325 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1326 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1327 }
1328
1329 TEST_F(ValidateMemory, VulkanMemoryModelDeviceScopeCopyMemoryGood3) {
1330 const std::string spirv = R"(
1331 OpCapability Shader
1332 OpCapability VulkanMemoryModelKHR
1333 OpCapability VulkanMemoryModelDeviceScopeKHR
1334 OpCapability Linkage
1335 OpExtension "SPV_KHR_vulkan_memory_model"
1336 OpMemoryModel Logical VulkanKHR
1337 %void = OpTypeVoid
1338 %int = OpTypeInt 32 0
1339 %device = OpConstant %int 1
1340 %workgroup = OpConstant %int 2
1341 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1342 %var1 = OpVariable %int_ptr_ssbo StorageBuffer
1343 %var2 = OpVariable %int_ptr_ssbo StorageBuffer
1344 %voidfn = OpTypeFunction %void
1345 %func = OpFunction %void None %voidfn
1346 %entry = OpLabel
1347 OpCopyMemory %var1 %var2 Aligned|MakePointerVisibleKHR|MakePointerAvailableKHR|NonPrivatePointerKHR 4 %workgroup %device
1348 OpReturn
1349 OpFunctionEnd
1350 )";
1351
1352 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1353 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1354 }
1355
1356 TEST_F(ValidateMemory, VulkanMemoryModelCopyMemoryTwoAccessAvVisBadBinaryV13) {
1357 const std::string spirv = R"(
1358 OpCapability Shader
1359 OpCapability VulkanMemoryModelKHR
1360 OpCapability VulkanMemoryModelDeviceScopeKHR
1361 OpCapability Linkage
1362 OpExtension "SPV_KHR_vulkan_memory_model"
1363 OpMemoryModel Logical VulkanKHR
1364 %void = OpTypeVoid
1365 %int = OpTypeInt 32 0
1366 %device = OpConstant %int 1
1367 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1368 %var1 = OpVariable %int_ptr_ssbo StorageBuffer
1369 %var2 = OpVariable %int_ptr_ssbo StorageBuffer
1370 %voidfn = OpTypeFunction %void
1371 %func = OpFunction %void None %voidfn
1372 %entry = OpLabel
1373 OpCopyMemory %var1 %var2
1374 MakePointerAvailableKHR|NonPrivatePointerKHR %device
1375 MakePointerVisibleKHR|NonPrivatePointerKHR %device
1376 OpReturn
1377 OpFunctionEnd
1378 )";
1379
1380 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1381 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1382 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
1383 EXPECT_THAT(
1384 getDiagnosticString(),
1385 HasSubstr(
1386 "with two memory access operands requires SPIR-V 1.4 or later"));
1387 }
1388
1389 TEST_F(ValidateMemory, VulkanMemoryModelCopyMemoryTwoAccessAvVisGood) {
1390 const std::string spirv = R"(
1391 OpCapability Shader
1392 OpCapability VulkanMemoryModelKHR
1393 OpCapability VulkanMemoryModelDeviceScopeKHR
1394 OpCapability Linkage
1395 OpExtension "SPV_KHR_vulkan_memory_model"
1396 OpMemoryModel Logical VulkanKHR
1397 %void = OpTypeVoid
1398 %int = OpTypeInt 32 0
1399 %device = OpConstant %int 1
1400 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1401 %var1 = OpVariable %int_ptr_ssbo StorageBuffer
1402 %var2 = OpVariable %int_ptr_ssbo StorageBuffer
1403 %voidfn = OpTypeFunction %void
1404 %func = OpFunction %void None %voidfn
1405 %entry = OpLabel
1406 OpCopyMemory %var1 %var2
1407 MakePointerAvailableKHR|NonPrivatePointerKHR %device
1408 MakePointerVisibleKHR|NonPrivatePointerKHR %device
1409 OpReturn
1410 OpFunctionEnd
1411 )";
1412
1413 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
1414 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
1415 EXPECT_THAT(getDiagnosticString(), Eq(""));
1416 }
1417
1418 TEST_F(ValidateMemory, VulkanMemoryModelCopyMemoryTwoAccessFirstWithAvBad) {
1419 const std::string spirv = R"(
1420 OpCapability Shader
1421 OpCapability VulkanMemoryModelKHR
1422 OpCapability VulkanMemoryModelDeviceScopeKHR
1423 OpCapability Linkage
1424 OpExtension "SPV_KHR_vulkan_memory_model"
1425 OpMemoryModel Logical VulkanKHR
1426 %void = OpTypeVoid
1427 %int = OpTypeInt 32 0
1428 %device = OpConstant %int 1
1429 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1430 %var1 = OpVariable %int_ptr_ssbo StorageBuffer
1431 %var2 = OpVariable %int_ptr_ssbo StorageBuffer
1432 %voidfn = OpTypeFunction %void
1433 %func = OpFunction %void None %voidfn
1434 %entry = OpLabel
1435 OpCopyMemory %var1 %var2
1436 MakePointerAvailableKHR|NonPrivatePointerKHR %device
1437 MakePointerAvailableKHR|NonPrivatePointerKHR %device
1438 OpReturn
1439 OpFunctionEnd
1440 )";
1441
1442 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
1443 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1444 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
1445 EXPECT_THAT(
1446 getDiagnosticString(),
1447 HasSubstr(
1448 "Source memory access must not include MakePointerAvailableKHR\n"
1449 " OpCopyMemory %5 %6 MakePointerAvailable|NonPrivatePointer"
1450 " %uint_1 MakePointerAvailable|NonPrivatePointer %uint_1"));
1451 }
1452
1453 TEST_F(ValidateMemory, VulkanMemoryModelCopyMemoryTwoAccessSecondWithVisBad) {
1454 const std::string spirv = R"(
1455 OpCapability Shader
1456 OpCapability VulkanMemoryModelKHR
1457 OpCapability VulkanMemoryModelDeviceScopeKHR
1458 OpCapability Linkage
1459 OpExtension "SPV_KHR_vulkan_memory_model"
1460 OpMemoryModel Logical VulkanKHR
1461 %void = OpTypeVoid
1462 %int = OpTypeInt 32 0
1463 %device = OpConstant %int 1
1464 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1465 %var1 = OpVariable %int_ptr_ssbo StorageBuffer
1466 %var2 = OpVariable %int_ptr_ssbo StorageBuffer
1467 %voidfn = OpTypeFunction %void
1468 %func = OpFunction %void None %voidfn
1469 %entry = OpLabel
1470 OpCopyMemory %var1 %var2
1471 MakePointerVisibleKHR|NonPrivatePointerKHR %device
1472 MakePointerVisibleKHR|NonPrivatePointerKHR %device
1473 OpReturn
1474 OpFunctionEnd
1475 )";
1476
1477 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
1478 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1479 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
1480 EXPECT_THAT(
1481 getDiagnosticString(),
1482 HasSubstr("Target memory access must not include MakePointerVisibleKHR\n"
1483 " OpCopyMemory %5 %6 MakePointerVisible|NonPrivatePointer"
1484 " %uint_1 MakePointerVisible|NonPrivatePointer %uint_1"));
1485 }
1486
1487 TEST_F(ValidateMemory, VulkanMemoryModelDeviceScopeCopyMemorySizedBad1) {
1488 const std::string spirv = R"(
1489 OpCapability Shader
1490 OpCapability VulkanMemoryModelKHR
1491 OpCapability Linkage
1492 OpCapability Addresses
1493 OpExtension "SPV_KHR_vulkan_memory_model"
1494 OpMemoryModel Logical VulkanKHR
1495 %void = OpTypeVoid
1496 %int = OpTypeInt 32 0
1497 %int_4 = OpConstant %int 4
1498 %device = OpConstant %int 1
1499 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1500 %var1 = OpVariable %int_ptr_ssbo StorageBuffer
1501 %var2 = OpVariable %int_ptr_ssbo StorageBuffer
1502 %voidfn = OpTypeFunction %void
1503 %func = OpFunction %void None %voidfn
1504 %entry = OpLabel
1505 OpCopyMemorySized %var1 %var2 %int_4 MakePointerAvailableKHR|NonPrivatePointerKHR %device
1506 OpReturn
1507 OpFunctionEnd
1508 )";
1509
1510 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1511 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1512 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1513 EXPECT_THAT(
1514 getDiagnosticString(),
1515 HasSubstr("Use of device scope with VulkanKHR memory model requires the "
1516 "VulkanMemoryModelDeviceScopeKHR capability"));
1517 }
1518
1519 TEST_F(ValidateMemory, VulkanMemoryModelDeviceScopeCopyMemorySizedBad2) {
1520 const std::string spirv = R"(
1521 OpCapability Shader
1522 OpCapability VulkanMemoryModelKHR
1523 OpCapability Linkage
1524 OpCapability Addresses
1525 OpExtension "SPV_KHR_vulkan_memory_model"
1526 OpMemoryModel Logical VulkanKHR
1527 %void = OpTypeVoid
1528 %int = OpTypeInt 32 0
1529 %int_4 = OpConstant %int 4
1530 %device = OpConstant %int 1
1531 %workgroup = OpConstant %int 1
1532 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1533 %var1 = OpVariable %int_ptr_ssbo StorageBuffer
1534 %var2 = OpVariable %int_ptr_ssbo StorageBuffer
1535 %voidfn = OpTypeFunction %void
1536 %func = OpFunction %void None %voidfn
1537 %entry = OpLabel
1538 OpCopyMemorySized %var1 %var2 %int_4 Aligned|MakePointerVisibleKHR|MakePointerAvailableKHR|NonPrivatePointerKHR 4 %device %workgroup
1539 OpReturn
1540 OpFunctionEnd
1541 )";
1542
1543 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1544 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1545 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1546 EXPECT_THAT(
1547 getDiagnosticString(),
1548 HasSubstr("Use of device scope with VulkanKHR memory model requires the "
1549 "VulkanMemoryModelDeviceScopeKHR capability"));
1550 }
1551
1552 TEST_F(ValidateMemory, VulkanMemoryModelDeviceScopeCopyMemorySizedBad3) {
1553 const std::string spirv = R"(
1554 OpCapability Shader
1555 OpCapability VulkanMemoryModelKHR
1556 OpCapability Linkage
1557 OpCapability Addresses
1558 OpExtension "SPV_KHR_vulkan_memory_model"
1559 OpMemoryModel Logical VulkanKHR
1560 %void = OpTypeVoid
1561 %int = OpTypeInt 32 0
1562 %int_4 = OpConstant %int 4
1563 %device = OpConstant %int 1
1564 %workgroup = OpConstant %int 1
1565 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1566 %var1 = OpVariable %int_ptr_ssbo StorageBuffer
1567 %var2 = OpVariable %int_ptr_ssbo StorageBuffer
1568 %voidfn = OpTypeFunction %void
1569 %func = OpFunction %void None %voidfn
1570 %entry = OpLabel
1571 OpCopyMemorySized %var1 %var2 %int_4 Aligned|MakePointerVisibleKHR|MakePointerAvailableKHR|NonPrivatePointerKHR 4 %workgroup %device
1572 OpReturn
1573 OpFunctionEnd
1574 )";
1575
1576 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1577 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1578 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1579 EXPECT_THAT(
1580 getDiagnosticString(),
1581 HasSubstr("Use of device scope with VulkanKHR memory model requires the "
1582 "VulkanMemoryModelDeviceScopeKHR capability"));
1583 }
1584
1585 TEST_F(ValidateMemory, VulkanMemoryModelDeviceScopeCopyMemorySizedGood1) {
1586 const std::string spirv = R"(
1587 OpCapability Shader
1588 OpCapability VulkanMemoryModelKHR
1589 OpCapability VulkanMemoryModelDeviceScopeKHR
1590 OpCapability Linkage
1591 OpCapability Addresses
1592 OpExtension "SPV_KHR_vulkan_memory_model"
1593 OpMemoryModel Logical VulkanKHR
1594 %void = OpTypeVoid
1595 %int = OpTypeInt 32 0
1596 %int_4 = OpConstant %int 4
1597 %device = OpConstant %int 1
1598 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1599 %var1 = OpVariable %int_ptr_ssbo StorageBuffer
1600 %var2 = OpVariable %int_ptr_ssbo StorageBuffer
1601 %voidfn = OpTypeFunction %void
1602 %func = OpFunction %void None %voidfn
1603 %entry = OpLabel
1604 OpCopyMemorySized %var1 %var2 %int_4 MakePointerAvailableKHR|NonPrivatePointerKHR %device
1605 OpReturn
1606 OpFunctionEnd
1607 )";
1608
1609 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1610 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1611 }
1612
1613 TEST_F(ValidateMemory, VulkanMemoryModelDeviceScopeCopyMemorySizedGood2) {
1614 const std::string spirv = R"(
1615 OpCapability Shader
1616 OpCapability VulkanMemoryModelKHR
1617 OpCapability VulkanMemoryModelDeviceScopeKHR
1618 OpCapability Linkage
1619 OpCapability Addresses
1620 OpExtension "SPV_KHR_vulkan_memory_model"
1621 OpMemoryModel Logical VulkanKHR
1622 %void = OpTypeVoid
1623 %int = OpTypeInt 32 0
1624 %int_4 = OpConstant %int 4
1625 %device = OpConstant %int 1
1626 %workgroup = OpConstant %int 2
1627 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1628 %var1 = OpVariable %int_ptr_ssbo StorageBuffer
1629 %var2 = OpVariable %int_ptr_ssbo StorageBuffer
1630 %voidfn = OpTypeFunction %void
1631 %func = OpFunction %void None %voidfn
1632 %entry = OpLabel
1633 OpCopyMemorySized %var1 %var2 %int_4 Aligned|MakePointerVisibleKHR|MakePointerAvailableKHR|NonPrivatePointerKHR 4 %device %workgroup
1634 OpReturn
1635 OpFunctionEnd
1636 )";
1637
1638 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1639 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1640 }
1641
1642 TEST_F(ValidateMemory, VulkanMemoryModelDeviceScopeCopyMemorySizedGood3) {
1643 const std::string spirv = R"(
1644 OpCapability Shader
1645 OpCapability VulkanMemoryModelKHR
1646 OpCapability VulkanMemoryModelDeviceScopeKHR
1647 OpCapability Linkage
1648 OpCapability Addresses
1649 OpExtension "SPV_KHR_vulkan_memory_model"
1650 OpMemoryModel Logical VulkanKHR
1651 %void = OpTypeVoid
1652 %int = OpTypeInt 32 0
1653 %int_4 = OpConstant %int 4
1654 %device = OpConstant %int 1
1655 %workgroup = OpConstant %int 2
1656 %int_ptr_ssbo = OpTypePointer StorageBuffer %int
1657 %var1 = OpVariable %int_ptr_ssbo StorageBuffer
1658 %var2 = OpVariable %int_ptr_ssbo StorageBuffer
1659 %voidfn = OpTypeFunction %void
1660 %func = OpFunction %void None %voidfn
1661 %entry = OpLabel
1662 OpCopyMemorySized %var1 %var2 %int_4 Aligned|MakePointerVisibleKHR|MakePointerAvailableKHR|NonPrivatePointerKHR 4 %workgroup %device
1663 OpReturn
1664 OpFunctionEnd
1665 )";
1666
1667 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
1668 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1669 }
1670
1671 TEST_F(ValidateMemory, ArrayLengthStructIsLabel) {
1672 const std::string spirv = R"(
1673 OpCapability Tessellation
1674 OpMemoryModel Logical GLSL450
1675 OpName %20 "incorrect"
1676 %void = OpTypeVoid
1677 %3 = OpTypeFunction %void
1678 %float = OpTypeFloat 32
1679 %v4float = OpTypeVector %float 4
1680 %uint = OpTypeInt 32 0
1681 %4 = OpFunction %void None %3
1682 %20 = OpLabel
1683 %24 = OpArrayLength %uint %20 0
1684 %25 = OpLoad %v4float %24
1685 OpReturnValue %25
1686 OpFunctionEnd
1687 )";
1688
1689 CompileSuccessfully(spirv);
1690 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1691 EXPECT_THAT(getDiagnosticString(),
1692 HasSubstr("Operand '1[%incorrect]' requires a type"));
1693 }
1694
1695 TEST_F(ValidateMemory, PSBLoadAlignedSuccess) {
1696 const std::string body = R"(
1697 OpCapability PhysicalStorageBufferAddresses
1698 OpCapability Int64
1699 OpCapability Shader
1700 OpExtension "SPV_EXT_physical_storage_buffer"
1701 OpMemoryModel PhysicalStorageBuffer64 GLSL450
1702 OpEntryPoint Fragment %main "main"
1703 OpExecutionMode %main OriginUpperLeft
1704 OpDecorate %val1 AliasedPointer
1705 %uint64 = OpTypeInt 64 0
1706 %ptr = OpTypePointer PhysicalStorageBuffer %uint64
1707 %pptr_f = OpTypePointer Function %ptr
1708 %void = OpTypeVoid
1709 %voidfn = OpTypeFunction %void
1710 %main = OpFunction %void None %voidfn
1711 %entry = OpLabel
1712 %val1 = OpVariable %pptr_f Function
1713 %val2 = OpLoad %ptr %val1
1714 %val3 = OpLoad %uint64 %val2 Aligned 8
1715 OpReturn
1716 OpFunctionEnd
1717 )";
1718
1719 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
1720 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
1721 }
1722
1723 TEST_F(ValidateMemory, PSBLoadAlignedMissing) {
1724 const std::string body = R"(
1725 OpCapability PhysicalStorageBufferAddresses
1726 OpCapability Int64
1727 OpCapability Shader
1728 OpExtension "SPV_EXT_physical_storage_buffer"
1729 OpMemoryModel PhysicalStorageBuffer64 GLSL450
1730 OpEntryPoint Fragment %main "main"
1731 OpExecutionMode %main OriginUpperLeft
1732 OpDecorate %val1 AliasedPointer
1733 %uint64 = OpTypeInt 64 0
1734 %ptr = OpTypePointer PhysicalStorageBuffer %uint64
1735 %pptr_f = OpTypePointer Function %ptr
1736 %void = OpTypeVoid
1737 %voidfn = OpTypeFunction %void
1738 %main = OpFunction %void None %voidfn
1739 %entry = OpLabel
1740 %val1 = OpVariable %pptr_f Function
1741 %val2 = OpLoad %ptr %val1
1742 %val3 = OpLoad %uint64 %val2
1743 OpReturn
1744 OpFunctionEnd
1745 )";
1746
1747 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
1748 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
1749 EXPECT_THAT(getDiagnosticString(),
1750 AnyVUID("VUID-StandaloneSpirv-PhysicalStorageBuffer64-04708"));
1751 EXPECT_THAT(
1752 getDiagnosticString(),
1753 HasSubstr("Memory accesses with PhysicalStorageBuffer must use Aligned"));
1754 }
1755
1756 TEST_F(ValidateMemory, PSBLoadAlignedMissingWithOtherOperand) {
1757 const std::string body = R"(
1758 OpCapability PhysicalStorageBufferAddresses
1759 OpCapability Int64
1760 OpCapability Shader
1761 OpExtension "SPV_EXT_physical_storage_buffer"
1762 OpMemoryModel PhysicalStorageBuffer64 GLSL450
1763 OpEntryPoint Fragment %main "main"
1764 OpExecutionMode %main OriginUpperLeft
1765 OpDecorate %val1 AliasedPointer
1766 %uint64 = OpTypeInt 64 0
1767 %ptr = OpTypePointer PhysicalStorageBuffer %uint64
1768 %pptr_f = OpTypePointer Function %ptr
1769 %void = OpTypeVoid
1770 %voidfn = OpTypeFunction %void
1771 %main = OpFunction %void None %voidfn
1772 %entry = OpLabel
1773 %val1 = OpVariable %pptr_f Function
1774 %val2 = OpLoad %ptr %val1
1775 %val3 = OpLoad %uint64 %val2 Volatile
1776 OpReturn
1777 OpFunctionEnd
1778 )";
1779
1780 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
1781 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
1782 EXPECT_THAT(getDiagnosticString(),
1783 AnyVUID("VUID-StandaloneSpirv-PhysicalStorageBuffer64-04708"));
1784 EXPECT_THAT(
1785 getDiagnosticString(),
1786 HasSubstr("Memory accesses with PhysicalStorageBuffer must use Aligned"));
1787 }
1788
1789 TEST_F(ValidateMemory, PSBStoreAlignedSuccess) {
1790 const std::string body = R"(
1791 OpCapability PhysicalStorageBufferAddresses
1792 OpCapability Int64
1793 OpCapability Shader
1794 OpExtension "SPV_EXT_physical_storage_buffer"
1795 OpMemoryModel PhysicalStorageBuffer64 GLSL450
1796 OpEntryPoint Fragment %main "main"
1797 OpExecutionMode %main OriginUpperLeft
1798 OpDecorate %val1 AliasedPointer
1799 %uint64 = OpTypeInt 64 0
1800 %u64_1 = OpConstant %uint64 1
1801 %ptr = OpTypePointer PhysicalStorageBuffer %uint64
1802 %pptr_f = OpTypePointer Function %ptr
1803 %void = OpTypeVoid
1804 %voidfn = OpTypeFunction %void
1805 %main = OpFunction %void None %voidfn
1806 %entry = OpLabel
1807 %val1 = OpVariable %pptr_f Function
1808 %val2 = OpLoad %ptr %val1
1809 OpStore %val2 %u64_1 Aligned 8
1810 OpReturn
1811 OpFunctionEnd
1812 )";
1813
1814 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
1815 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
1816 }
1817
1818 TEST_F(ValidateMemory, PSBStoreAlignedMissing) {
1819 const std::string body = R"(
1820 OpCapability PhysicalStorageBufferAddresses
1821 OpCapability Int64
1822 OpCapability Shader
1823 OpExtension "SPV_EXT_physical_storage_buffer"
1824 OpMemoryModel PhysicalStorageBuffer64 GLSL450
1825 OpEntryPoint Fragment %main "main"
1826 OpExecutionMode %main OriginUpperLeft
1827 OpDecorate %val1 AliasedPointer
1828 %uint64 = OpTypeInt 64 0
1829 %u64_1 = OpConstant %uint64 1
1830 %ptr = OpTypePointer PhysicalStorageBuffer %uint64
1831 %pptr_f = OpTypePointer Function %ptr
1832 %void = OpTypeVoid
1833 %voidfn = OpTypeFunction %void
1834 %main = OpFunction %void None %voidfn
1835 %entry = OpLabel
1836 %val1 = OpVariable %pptr_f Function
1837 %val2 = OpLoad %ptr %val1
1838 OpStore %val2 %u64_1 None
1839 OpReturn
1840 OpFunctionEnd
1841 )";
1842
1843 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
1844 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
1845 EXPECT_THAT(getDiagnosticString(),
1846 AnyVUID("VUID-StandaloneSpirv-PhysicalStorageBuffer64-04708"));
1847 EXPECT_THAT(
1848 getDiagnosticString(),
1849 HasSubstr("Memory accesses with PhysicalStorageBuffer must use Aligned"));
1850 }
1851
1852 TEST_F(ValidateMemory, PSBStoreAlignedZero) {
1853 const std::string body = R"(
1854 OpCapability PhysicalStorageBufferAddresses
1855 OpCapability Shader
1856 OpExtension "SPV_EXT_physical_storage_buffer"
1857 OpMemoryModel PhysicalStorageBuffer64 GLSL450
1858 OpEntryPoint Fragment %main "main"
1859 OpExecutionMode %main OriginUpperLeft
1860 %uint = OpTypeInt 32 0
1861 %uint_1 = OpConstant %uint 1
1862 %ptr = OpTypePointer PhysicalStorageBuffer %uint
1863 %pptr_f = OpTypePointer Function %ptr
1864 %void = OpTypeVoid
1865 %voidfn = OpTypeFunction %void
1866 %main = OpFunction %void None %voidfn
1867 %entry = OpLabel
1868 %val1 = OpVariable %pptr_f Function
1869 %val2 = OpLoad %ptr %val1
1870 OpStore %val2 %uint_1 Aligned 0
1871 OpReturn
1872 OpFunctionEnd
1873 )";
1874
1875 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
1876 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
1877 EXPECT_THAT(
1878 getDiagnosticString(),
1879 HasSubstr(
1880 "Memory accesses Aligned operand value 0 is not a power of two"));
1881 }
1882
1883 TEST_F(ValidateMemory, PSBStoreAlignedNonPoT) {
1884 const std::string body = R"(
1885 OpCapability PhysicalStorageBufferAddresses
1886 OpCapability Shader
1887 OpExtension "SPV_EXT_physical_storage_buffer"
1888 OpMemoryModel PhysicalStorageBuffer64 GLSL450
1889 OpEntryPoint Fragment %main "main"
1890 OpExecutionMode %main OriginUpperLeft
1891 %uint = OpTypeInt 32 0
1892 %uint_1 = OpConstant %uint 1
1893 %ptr = OpTypePointer PhysicalStorageBuffer %uint
1894 %pptr_f = OpTypePointer Function %ptr
1895 %void = OpTypeVoid
1896 %voidfn = OpTypeFunction %void
1897 %main = OpFunction %void None %voidfn
1898 %entry = OpLabel
1899 %val1 = OpVariable %pptr_f Function
1900 %val2 = OpLoad %ptr %val1
1901 OpStore %val2 %uint_1 Aligned 3
1902 OpReturn
1903 OpFunctionEnd
1904 )";
1905
1906 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
1907 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
1908 EXPECT_THAT(
1909 getDiagnosticString(),
1910 HasSubstr(
1911 "Memory accesses Aligned operand value 3 is not a power of two."));
1912 }
1913
1914 TEST_F(ValidateMemory, PSBCopyMemoryAlignedSuccess) {
1915 const std::string body = R"(
1916 OpCapability PhysicalStorageBufferAddresses
1917 OpCapability Int64
1918 OpCapability Shader
1919 OpExtension "SPV_EXT_physical_storage_buffer"
1920 OpMemoryModel PhysicalStorageBuffer64 GLSL450
1921 OpEntryPoint Fragment %main "main"
1922 OpExecutionMode %main OriginUpperLeft
1923 OpDecorate %val1 AliasedPointer
1924 %uint64 = OpTypeInt 64 0
1925 %u64_1 = OpConstant %uint64 1
1926 %ptr = OpTypePointer PhysicalStorageBuffer %uint64
1927 %pptr_f = OpTypePointer Function %ptr
1928 %void = OpTypeVoid
1929 %voidfn = OpTypeFunction %void
1930 %main = OpFunction %void None %voidfn
1931 %entry = OpLabel
1932 %val1 = OpVariable %pptr_f Function
1933 %val2 = OpLoad %ptr %val1
1934 %val3 = OpLoad %ptr %val1
1935 OpCopyMemory %val2 %val3 Aligned 8
1936 OpCopyMemory %val3 %val2 Aligned 8 Aligned 8
1937 OpReturn
1938 OpFunctionEnd
1939 )";
1940
1941 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
1942 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
1943 }
1944
1945 TEST_F(ValidateMemory, PSBCopyMemoryAlignedMissingTarget) {
1946 const std::string body = R"(
1947 OpCapability PhysicalStorageBufferAddresses
1948 OpCapability Int64
1949 OpCapability Shader
1950 OpExtension "SPV_EXT_physical_storage_buffer"
1951 OpMemoryModel PhysicalStorageBuffer64 GLSL450
1952 OpEntryPoint Fragment %main "main"
1953 OpExecutionMode %main OriginUpperLeft
1954 OpDecorate %val1 AliasedPointer
1955 %uint64 = OpTypeInt 64 0
1956 %u64_1 = OpConstant %uint64 1
1957 %ptr = OpTypePointer PhysicalStorageBuffer %uint64
1958 %pptr_f = OpTypePointer Function %ptr
1959 %void = OpTypeVoid
1960 %voidfn = OpTypeFunction %void
1961 %main = OpFunction %void None %voidfn
1962 %entry = OpLabel
1963 %val1 = OpVariable %pptr_f Function
1964 %val2 = OpLoad %ptr %val1
1965 %val3 = OpLoad %ptr %val1
1966 OpCopyMemory %val2 %val3 Volatile Aligned 8
1967 OpReturn
1968 OpFunctionEnd
1969 )";
1970
1971 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
1972 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
1973 EXPECT_THAT(getDiagnosticString(),
1974 AnyVUID("VUID-StandaloneSpirv-PhysicalStorageBuffer64-04708"));
1975 EXPECT_THAT(
1976 getDiagnosticString(),
1977 HasSubstr("Memory accesses with PhysicalStorageBuffer must use Aligned"));
1978 }
1979
1980 TEST_F(ValidateMemory, PSBCopyMemoryAlignedMissingSource) {
1981 const std::string body = R"(
1982 OpCapability PhysicalStorageBufferAddresses
1983 OpCapability Int64
1984 OpCapability Shader
1985 OpExtension "SPV_EXT_physical_storage_buffer"
1986 OpMemoryModel PhysicalStorageBuffer64 GLSL450
1987 OpEntryPoint Fragment %main "main"
1988 OpExecutionMode %main OriginUpperLeft
1989 OpDecorate %val1 AliasedPointer
1990 %uint64 = OpTypeInt 64 0
1991 %u64_1 = OpConstant %uint64 1
1992 %ptr = OpTypePointer PhysicalStorageBuffer %uint64
1993 %pptr_f = OpTypePointer Function %ptr
1994 %void = OpTypeVoid
1995 %voidfn = OpTypeFunction %void
1996 %main = OpFunction %void None %voidfn
1997 %entry = OpLabel
1998 %val1 = OpVariable %pptr_f Function
1999 %val2 = OpLoad %ptr %val1
2000 %val3 = OpLoad %ptr %val1
2001 OpCopyMemory %val2 %val3 Aligned 8 Volatile
2002 OpReturn
2003 OpFunctionEnd
2004 )";
2005
2006 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
2007 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
2008 EXPECT_THAT(getDiagnosticString(),
2009 AnyVUID("VUID-StandaloneSpirv-PhysicalStorageBuffer64-04708"));
2010 EXPECT_THAT(
2011 getDiagnosticString(),
2012 HasSubstr("Memory accesses with PhysicalStorageBuffer must use Aligned"));
2013 }
2014
2015 TEST_F(ValidateMemory, PSBCopyMemoryAlignedMissingBoth) {
2016 const std::string body = R"(
2017 OpCapability PhysicalStorageBufferAddresses
2018 OpCapability Int64
2019 OpCapability Shader
2020 OpExtension "SPV_EXT_physical_storage_buffer"
2021 OpMemoryModel PhysicalStorageBuffer64 GLSL450
2022 OpEntryPoint Fragment %main "main"
2023 OpExecutionMode %main OriginUpperLeft
2024 OpDecorate %val1 AliasedPointer
2025 %uint64 = OpTypeInt 64 0
2026 %u64_1 = OpConstant %uint64 1
2027 %ptr = OpTypePointer PhysicalStorageBuffer %uint64
2028 %pptr_f = OpTypePointer Function %ptr
2029 %void = OpTypeVoid
2030 %voidfn = OpTypeFunction %void
2031 %main = OpFunction %void None %voidfn
2032 %entry = OpLabel
2033 %val1 = OpVariable %pptr_f Function
2034 %val2 = OpLoad %ptr %val1
2035 %val3 = OpLoad %ptr %val1
2036 OpCopyMemory %val2 %val3 Volatile
2037 OpReturn
2038 OpFunctionEnd
2039 )";
2040
2041 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
2042 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
2043 EXPECT_THAT(getDiagnosticString(),
2044 AnyVUID("VUID-StandaloneSpirv-PhysicalStorageBuffer64-04708"));
2045 EXPECT_THAT(
2046 getDiagnosticString(),
2047 HasSubstr("Memory accesses with PhysicalStorageBuffer must use Aligned"));
2048 }
2049
2050 TEST_F(ValidateMemory, PSBVariable) {
2051 const std::string body = R"(
2052 OpCapability PhysicalStorageBufferAddresses
2053 OpCapability Int64
2054 OpCapability Shader
2055 OpExtension "SPV_EXT_physical_storage_buffer"
2056 OpMemoryModel PhysicalStorageBuffer64 GLSL450
2057 OpEntryPoint Fragment %main "main"
2058 OpExecutionMode %main OriginUpperLeft
2059 OpDecorate %val1 AliasedPointer
2060 %uint64 = OpTypeInt 64 0
2061 %ptr = OpTypePointer PhysicalStorageBuffer %uint64
2062 %val1 = OpVariable %ptr PhysicalStorageBuffer
2063 %void = OpTypeVoid
2064 %voidfn = OpTypeFunction %void
2065 %main = OpFunction %void None %voidfn
2066 %entry = OpLabel
2067 OpReturn
2068 OpFunctionEnd
2069 )";
2070
2071 CompileSuccessfully(body);
2072 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2073 EXPECT_THAT(
2074 getDiagnosticString(),
2075 HasSubstr("PhysicalStorageBuffer must not be used with OpVariable"));
2076 }
2077
2078 TEST_F(ValidateMemory, PSBStoreAlignedOneWithUvec4) {
2079 const std::string body = R"(
2080 OpCapability Shader
2081 OpCapability PhysicalStorageBufferAddresses
2082 OpMemoryModel PhysicalStorageBuffer64 GLSL450
2083 OpEntryPoint GLCompute %main "main" %_
2084 OpExecutionMode %main LocalSize 1 1 1
2085 OpDecorate %SSBO Block
2086 OpMemberDecorate %SSBO 0 Offset 0
2087 OpDecorate %Ptr Block
2088 OpMemberDecorate %Ptr 0 Offset 0
2089 OpDecorate %_ Binding 0
2090 OpDecorate %_ DescriptorSet 0
2091 %void = OpTypeVoid
2092 %4 = OpTypeFunction %void
2093 OpTypeForwardPointer %_ptr_PhysicalStorageBuffer_Ptr PhysicalStorageBuffer
2094 %SSBO = OpTypeStruct %_ptr_PhysicalStorageBuffer_Ptr
2095 %uint = OpTypeInt 32 0
2096 %v4uint = OpTypeVector %uint 4
2097 %Ptr = OpTypeStruct %v4uint
2098 %_ptr_PhysicalStorageBuffer_Ptr = OpTypePointer PhysicalStorageBuffer %Ptr
2099 %_ptr_StorageBuffer_SSBO = OpTypePointer StorageBuffer %SSBO
2100 %_ = OpVariable %_ptr_StorageBuffer_SSBO StorageBuffer
2101 %int = OpTypeInt 32 1
2102 %int_0 = OpConstant %int 0
2103 %_ptr_StorageBuffer__ptr_PhysicalStorageBuffer_Ptr = OpTypePointer StorageBuffer %_ptr_PhysicalStorageBuffer_Ptr
2104 %uint_0 = OpConstant %uint 0
2105 %20 = OpConstantComposite %v4uint %uint_0 %uint_0 %uint_0 %uint_0
2106 %_ptr_PhysicalStorageBuffer_v4uint = OpTypePointer PhysicalStorageBuffer %v4uint
2107 %main = OpFunction %void None %4
2108 %6 = OpLabel
2109 %17 = OpAccessChain %_ptr_StorageBuffer__ptr_PhysicalStorageBuffer_Ptr %_ %int_0
2110 %18 = OpLoad %_ptr_PhysicalStorageBuffer_Ptr %17
2111 %22 = OpAccessChain %_ptr_PhysicalStorageBuffer_v4uint %18 %int_0
2112 OpStore %22 %20 Aligned 1
2113 OpReturn
2114 OpFunctionEnd
2115 )";
2116
2117 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
2118 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
2119 EXPECT_THAT(getDiagnosticString(),
2120 AnyVUID("VUID-StandaloneSpirv-PhysicalStorageBuffer64-06314"));
2121 EXPECT_THAT(getDiagnosticString(),
2122 HasSubstr("Memory accesses Aligned operand value 1 is too small, "
2123 "the largest scalar type is 4 bytes"));
2124 }
2125
2126 TEST_F(ValidateMemory, PSBStoreAlignedOneWithUint32) {
2127 const std::string body = R"(
2128 OpCapability Shader
2129 OpCapability PhysicalStorageBufferAddresses
2130 OpMemoryModel PhysicalStorageBuffer64 GLSL450
2131 OpEntryPoint GLCompute %main "main" %_
2132 OpExecutionMode %main LocalSize 1 1 1
2133 OpDecorate %SSBO Block
2134 OpMemberDecorate %SSBO 0 Offset 0
2135 OpDecorate %B Block
2136 OpMemberDecorate %B 0 Offset 0
2137 OpDecorate %_ Binding 0
2138 OpDecorate %_ DescriptorSet 0
2139 %void = OpTypeVoid
2140 %4 = OpTypeFunction %void
2141 OpTypeForwardPointer %_ptr_PhysicalStorageBuffer_B PhysicalStorageBuffer
2142 %SSBO = OpTypeStruct %_ptr_PhysicalStorageBuffer_B
2143 %uint = OpTypeInt 32 0
2144 %B = OpTypeStruct %uint
2145 %_ptr_PhysicalStorageBuffer_B = OpTypePointer PhysicalStorageBuffer %B
2146 %_ptr_StorageBuffer_SSBO = OpTypePointer StorageBuffer %SSBO
2147 %_ = OpVariable %_ptr_StorageBuffer_SSBO StorageBuffer
2148 %int = OpTypeInt 32 1
2149 %int_0 = OpConstant %int 0
2150 %_ptr_StorageBuffer__ptr_PhysicalStorageBuffer_B = OpTypePointer StorageBuffer %_ptr_PhysicalStorageBuffer_B
2151 %uint_0 = OpConstant %uint 0
2152 %_ptr_PhysicalStorageBuffer_uint = OpTypePointer PhysicalStorageBuffer %uint
2153 %main = OpFunction %void None %4
2154 %6 = OpLabel
2155 %16 = OpAccessChain %_ptr_StorageBuffer__ptr_PhysicalStorageBuffer_B %_ %int_0
2156 %17 = OpLoad %_ptr_PhysicalStorageBuffer_B %16
2157 %20 = OpAccessChain %_ptr_PhysicalStorageBuffer_uint %17 %int_0
2158 OpStore %20 %uint_0 Aligned 2
2159 OpReturn
2160 OpFunctionEnd
2161 )";
2162
2163 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
2164 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
2165 EXPECT_THAT(getDiagnosticString(),
2166 AnyVUID("VUID-StandaloneSpirv-PhysicalStorageBuffer64-06314"));
2167 EXPECT_THAT(getDiagnosticString(),
2168 HasSubstr("Memory accesses Aligned operand value 2 is too small, "
2169 "the largest scalar type is 4 bytes"));
2170 }
2171
2172 // https://github.com/KhronosGroup/glslang/issues/4024
2173 TEST_F(ValidateMemory, PSBStoreAlignedPointerNot8) {
2174 const std::string body = R"(
2175 OpCapability Shader
2176 OpCapability PhysicalStorageBufferAddresses
2177 OpMemoryModel PhysicalStorageBuffer64 GLSL450
2178 OpEntryPoint GLCompute %main "main" %_
2179 OpExecutionMode %main LocalSize 1 1 1
2180 OpDecorate %SSBO Block
2181 OpMemberDecorate %SSBO 0 Offset 0
2182 OpDecorate %B Block
2183 OpMemberDecorate %B 0 Offset 0
2184 OpDecorate %A Block
2185 OpMemberDecorate %A 0 Offset 0
2186 OpDecorate %_ Binding 0
2187 OpDecorate %_ DescriptorSet 0
2188 %void = OpTypeVoid
2189 %4 = OpTypeFunction %void
2190 OpTypeForwardPointer %_ptr_PhysicalStorageBuffer_B PhysicalStorageBuffer
2191 %SSBO = OpTypeStruct %_ptr_PhysicalStorageBuffer_B
2192 OpTypeForwardPointer %_ptr_PhysicalStorageBuffer_A PhysicalStorageBuffer
2193 %B = OpTypeStruct %_ptr_PhysicalStorageBuffer_A
2194 %uint = OpTypeInt 32 0
2195 %A = OpTypeStruct %uint
2196 %_ptr_PhysicalStorageBuffer_A = OpTypePointer PhysicalStorageBuffer %A
2197 %_ptr_PhysicalStorageBuffer_B = OpTypePointer PhysicalStorageBuffer %B
2198 %_ptr_StorageBuffer_SSBO = OpTypePointer StorageBuffer %SSBO
2199 %_ = OpVariable %_ptr_StorageBuffer_SSBO StorageBuffer
2200 %int = OpTypeInt 32 1
2201 %int_0 = OpConstant %int 0
2202 %_ptr_StorageBuffer__ptr_PhysicalStorageBuffer_B = OpTypePointer StorageBuffer %_ptr_PhysicalStorageBuffer_B
2203 %_ptr_PhysicalStorageBuffer__ptr_PhysicalStorageBuffer_A = OpTypePointer PhysicalStorageBuffer %_ptr_PhysicalStorageBuffer_A
2204 %uint_0 = OpConstant %uint 0
2205 %_ptr_PhysicalStorageBuffer_uint = OpTypePointer PhysicalStorageBuffer %uint
2206 %main = OpFunction %void None %4
2207 %6 = OpLabel
2208 %18 = OpAccessChain %_ptr_StorageBuffer__ptr_PhysicalStorageBuffer_B %_ %int_0
2209 %19 = OpLoad %_ptr_PhysicalStorageBuffer_B %18
2210 %21 = OpAccessChain %_ptr_PhysicalStorageBuffer__ptr_PhysicalStorageBuffer_A %19 %int_0
2211 %22 = OpLoad %_ptr_PhysicalStorageBuffer_A %21 Aligned 4
2212 %25 = OpAccessChain %_ptr_PhysicalStorageBuffer_uint %22 %int_0
2213 OpStore %25 %uint_0 Aligned 4
2214 OpReturn
2215 OpFunctionEnd
2216 )";
2217
2218 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
2219 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
2220 EXPECT_THAT(getDiagnosticString(),
2221 AnyVUID("VUID-StandaloneSpirv-PhysicalStorageBuffer64-06314"));
2222 EXPECT_THAT(getDiagnosticString(),
2223 HasSubstr("Memory accesses Aligned operand value 4 is too small, "
2224 "the largest scalar type is 8 bytes"));
2225 }
2226
2227 // https://godbolt.org/z/sbGv6a7os
2228 TEST_F(ValidateMemory, PSBStoreAlignedStructCopyWithDeepDouble) {
2229 const std::string body = R"(
2230 OpCapability Shader
2231 OpCapability Float64
2232 OpCapability PhysicalStorageBufferAddresses
2233 OpMemoryModel PhysicalStorageBuffer64 GLSL450
2234 OpEntryPoint GLCompute %main "main" %_
2235 OpExecutionMode %main LocalSize 1 1 1
2236 OpDecorate %SSBO Block
2237 OpMemberDecorate %SSBO 0 Offset 0
2238 OpMemberDecorate %A 0 Offset 0
2239 OpMemberDecorate %A 1 Offset 8
2240 OpMemberDecorate %B 0 Offset 0
2241 OpMemberDecorate %B 1 Offset 16
2242 OpMemberDecorate %C 0 Offset 0
2243 OpMemberDecorate %C 1 Offset 32
2244 OpDecorate %Ptr Block
2245 OpMemberDecorate %Ptr 0 Offset 0
2246 OpDecorate %_ Binding 0
2247 OpDecorate %_ DescriptorSet 0
2248 %void = OpTypeVoid
2249 %4 = OpTypeFunction %void
2250 OpTypeForwardPointer %_ptr_PhysicalStorageBuffer_Ptr PhysicalStorageBuffer
2251 %SSBO = OpTypeStruct %_ptr_PhysicalStorageBuffer_Ptr
2252 %float = OpTypeFloat 32
2253 %v3float = OpTypeVector %float 3
2254 %uint = OpTypeInt 32 0
2255 %double = OpTypeFloat 64
2256 %A = OpTypeStruct %uint %double
2257 %B = OpTypeStruct %v3float %A
2258 %C = OpTypeStruct %B %uint
2259 %Ptr = OpTypeStruct %C
2260 %_ptr_PhysicalStorageBuffer_Ptr = OpTypePointer PhysicalStorageBuffer %Ptr
2261 %_ptr_StorageBuffer_SSBO = OpTypePointer StorageBuffer %SSBO
2262 %_ = OpVariable %_ptr_StorageBuffer_SSBO StorageBuffer
2263 %int = OpTypeInt 32 1
2264 %int_0 = OpConstant %int 0
2265 %_ptr_StorageBuffer__ptr_PhysicalStorageBuffer_Ptr = OpTypePointer StorageBuffer %_ptr_PhysicalStorageBuffer_Ptr
2266 %A_0 = OpTypeStruct %uint %double
2267 %B_0 = OpTypeStruct %v3float %A_0
2268 %C_0 = OpTypeStruct %B_0 %uint
2269 %_ptr_Function_C_0 = OpTypePointer Function %C_0
2270 %_ptr_PhysicalStorageBuffer_C = OpTypePointer PhysicalStorageBuffer %C
2271 %main = OpFunction %void None %4
2272 %6 = OpLabel
2273 %newC = OpVariable %_ptr_Function_C_0 Function
2274 %22 = OpAccessChain %_ptr_StorageBuffer__ptr_PhysicalStorageBuffer_Ptr %_ %int_0
2275 %23 = OpLoad %_ptr_PhysicalStorageBuffer_Ptr %22
2276 %29 = OpLoad %C_0 %newC
2277 %31 = OpAccessChain %_ptr_PhysicalStorageBuffer_C %23 %int_0
2278 %32 = OpCopyLogical %C %29
2279 OpStore %31 %32 Aligned 4
2280 OpReturn
2281 OpFunctionEnd
2282 )";
2283
2284 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
2285 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
2286 EXPECT_THAT(getDiagnosticString(),
2287 AnyVUID("VUID-StandaloneSpirv-PhysicalStorageBuffer64-06314"));
2288 EXPECT_THAT(getDiagnosticString(),
2289 HasSubstr("Memory accesses Aligned operand value 4 is too small, "
2290 "the largest scalar type is 8 bytes"));
2291 }
2292
2293 TEST_F(ValidateMemory, PSBStoreAlignedPtrAccessChain) {
2294 const std::string body = R"(
2295 OpCapability PhysicalStorageBufferAddresses
2296 OpCapability Int64
2297 OpCapability Shader
2298 OpExtension "SPV_KHR_non_semantic_info"
2299 OpExtension "SPV_KHR_physical_storage_buffer"
2300 %2 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
2301 OpMemoryModel PhysicalStorageBuffer64 GLSL450
2302 OpEntryPoint GLCompute %computeMain "main" %globalParams
2303 OpExecutionMode %computeMain LocalSize 1 1 1
2304 OpDecorate %_ptr_PhysicalStorageBuffer_ulong ArrayStride 8
2305 OpDecorate %GlobalParams_std140 Block
2306 OpMemberDecorate %GlobalParams_std140 0 Offset 0
2307 OpDecorate %globalParams Binding 0
2308 OpDecorate %globalParams DescriptorSet 0
2309 %void = OpTypeVoid
2310 %uint = OpTypeInt 32 0
2311 %12 = OpTypeFunction %void
2312 %ulong = OpTypeInt 64 0
2313 %_ptr_PhysicalStorageBuffer_ulong = OpTypePointer PhysicalStorageBuffer %ulong
2314 %GlobalParams_std140 = OpTypeStruct %_ptr_PhysicalStorageBuffer_ulong
2315 %_ptr_Uniform_GlobalParams_std140 = OpTypePointer Uniform %GlobalParams_std140
2316 %int = OpTypeInt 32 1
2317 %int_0 = OpConstant %int 0
2318 %_ptr_Uniform__ptr_PhysicalStorageBuffer_ulong = OpTypePointer Uniform %_ptr_PhysicalStorageBuffer_ulong
2319 %ulong_1 = OpConstant %ulong 1
2320 %globalParams = OpVariable %_ptr_Uniform_GlobalParams_std140 Uniform
2321 %computeMain = OpFunction %void None %12
2322 %13 = OpLabel
2323 %36 = OpInBoundsAccessChain %_ptr_Uniform__ptr_PhysicalStorageBuffer_ulong %globalParams %int_0
2324 %37 = OpLoad %_ptr_PhysicalStorageBuffer_ulong %36
2325 %38 = OpPtrAccessChain %_ptr_PhysicalStorageBuffer_ulong %37 %int_0
2326 OpStore %38 %ulong_1 Aligned 4
2327 OpReturn
2328 OpFunctionEnd
2329 )";
2330
2331 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
2332 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
2333 EXPECT_THAT(getDiagnosticString(),
2334 AnyVUID("VUID-StandaloneSpirv-PhysicalStorageBuffer64-06314"));
2335 EXPECT_THAT(getDiagnosticString(),
2336 HasSubstr("Memory accesses Aligned operand value 4 is too small, "
2337 "the largest scalar type is 8 bytes"));
2338 }
2339
2340 TEST_F(ValidateMemory, PSBStoreAlignedUntypedStorageBuffer) {
2341 const std::string body = R"(
2342 OpCapability Shader
2343 OpCapability UntypedPointersKHR
2344 OpCapability PhysicalStorageBufferAddresses
2345 OpExtension "SPV_KHR_untyped_pointers"
2346 OpMemoryModel PhysicalStorageBuffer64 GLSL450
2347 OpEntryPoint GLCompute %main "main" %_
2348 OpExecutionMode %main LocalSize 1 1 1
2349 OpDecorate %SSBO Block
2350 OpMemberDecorate %SSBO 0 Offset 0
2351 OpDecorate %B Block
2352 OpMemberDecorate %B 0 Offset 0
2353 OpDecorate %_ Binding 0
2354 OpDecorate %_ DescriptorSet 0
2355 %void = OpTypeVoid
2356 %4 = OpTypeFunction %void
2357 OpTypeForwardPointer %_ptr_PhysicalStorageBuffer_B PhysicalStorageBuffer
2358 %SSBO = OpTypeStruct %_ptr_PhysicalStorageBuffer_B
2359 %uint = OpTypeInt 32 0
2360 %int = OpTypeInt 32 1
2361 %int_0 = OpConstant %int 0
2362 %uint_0 = OpConstant %uint 0
2363 %B = OpTypeStruct %uint
2364 %_ptr_PhysicalStorageBuffer_B = OpTypePointer PhysicalStorageBuffer %B
2365 %_ptr_StorageBuffer__ptr_PhysicalStorageBuffer_B = OpTypePointer StorageBuffer %_ptr_PhysicalStorageBuffer_B
2366 %untyped_ptr = OpTypeUntypedPointerKHR StorageBuffer
2367 %_ = OpUntypedVariableKHR %untyped_ptr StorageBuffer %SSBO
2368 %_ptr_PhysicalStorageBuffer_uint = OpTypePointer PhysicalStorageBuffer %uint
2369 %main = OpFunction %void None %4
2370 %6 = OpLabel
2371 %16 = OpUntypedAccessChainKHR %untyped_ptr %SSBO %_ %int_0
2372 %17 = OpLoad %_ptr_PhysicalStorageBuffer_B %16
2373 %20 = OpAccessChain %_ptr_PhysicalStorageBuffer_uint %17 %int_0
2374 OpStore %20 %uint_0 Aligned 2
2375 OpReturn
2376 OpFunctionEnd
2377 )";
2378
2379 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
2380 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
2381 EXPECT_THAT(getDiagnosticString(),
2382 AnyVUID("VUID-StandaloneSpirv-PhysicalStorageBuffer64-06314"));
2383 EXPECT_THAT(getDiagnosticString(),
2384 HasSubstr("Memory accesses Aligned operand value 2 is too small, "
2385 "the largest scalar type is 4 bytes"));
2386 }
2387
2388 TEST_F(ValidateMemory, PSBStoreAlignedUntypedPhysicalStorageBuffer) {
2389 const std::string body = R"(
2390 OpCapability Shader
2391 OpCapability UntypedPointersKHR
2392 OpCapability PhysicalStorageBufferAddresses
2393 OpExtension "SPV_KHR_untyped_pointers"
2394 OpMemoryModel PhysicalStorageBuffer64 GLSL450
2395 OpEntryPoint GLCompute %main "main" %_
2396 OpExecutionMode %main LocalSize 1 1 1
2397 OpDecorate %SSBO Block
2398 OpMemberDecorate %SSBO 0 Offset 0
2399 OpDecorate %B Block
2400 OpMemberDecorate %B 0 Offset 0
2401 OpDecorate %_ Binding 0
2402 OpDecorate %_ DescriptorSet 0
2403 %void = OpTypeVoid
2404 %4 = OpTypeFunction %void
2405 OpTypeForwardPointer %_ptr_PhysicalStorageBuffer_B PhysicalStorageBuffer
2406 %SSBO = OpTypeStruct %_ptr_PhysicalStorageBuffer_B
2407 %uint = OpTypeInt 32 0
2408 %int = OpTypeInt 32 1
2409 %int_0 = OpConstant %int 0
2410 %uint_0 = OpConstant %uint 0
2411 %B = OpTypeStruct %uint
2412 %_ptr_PhysicalStorageBuffer_B = OpTypePointer PhysicalStorageBuffer %B
2413 %_ptr_StorageBuffer__ptr_PhysicalStorageBuffer_B = OpTypePointer StorageBuffer %_ptr_PhysicalStorageBuffer_B
2414 %untyped_ptr = OpTypeUntypedPointerKHR StorageBuffer
2415 %_ = OpUntypedVariableKHR %untyped_ptr StorageBuffer %SSBO
2416 %_ptr_PhysicalStorageBuffer_uint = OpTypePointer PhysicalStorageBuffer %uint
2417 %main = OpFunction %void None %4
2418 %6 = OpLabel
2419 %16 = OpUntypedAccessChainKHR %untyped_ptr %SSBO %_ %int_0
2420 %17 = OpLoad %_ptr_PhysicalStorageBuffer_B %16
2421 %20 = OpAccessChain %_ptr_PhysicalStorageBuffer_uint %17 %int_0
2422 OpStore %20 %uint_0 Aligned 2
2423 OpReturn
2424 OpFunctionEnd
2425 )";
2426
2427 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
2428 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_2));
2429 EXPECT_THAT(getDiagnosticString(),
2430 AnyVUID("VUID-StandaloneSpirv-PhysicalStorageBuffer64-06314"));
2431 EXPECT_THAT(getDiagnosticString(),
2432 HasSubstr("Memory accesses Aligned operand value 2 is too small, "
2433 "the largest scalar type is 4 bytes"));
2434 }
2435
2436 TEST_F(ValidateMemory, PSBStoreAlignedVariousTypeSuccess) {
2437 const std::string body = R"(
2438 OpCapability Shader
2439 OpCapability Float64
2440 OpCapability Int8
2441 OpCapability StorageBuffer8BitAccess
2442 OpCapability PhysicalStorageBufferAddresses
2443 %2 = OpExtInstImport "GLSL.std.450"
2444 OpMemoryModel PhysicalStorageBuffer64 GLSL450
2445 OpEntryPoint GLCompute %main "main" %_
2446 OpExecutionMode %main LocalSize 1 1 1
2447 OpDecorate %SSBO Block
2448 OpMemberDecorate %SSBO 0 Offset 0
2449 OpDecorate %Ptr Block
2450 OpMemberDecorate %Ptr 0 Offset 0
2451 OpMemberDecorate %Ptr 1 Offset 4
2452 OpMemberDecorate %Ptr 2 Offset 8
2453 OpDecorate %_ Binding 0
2454 OpDecorate %_ DescriptorSet 0
2455 %void = OpTypeVoid
2456 %4 = OpTypeFunction %void
2457 OpTypeForwardPointer %_ptr_PhysicalStorageBuffer_Ptr PhysicalStorageBuffer
2458 %SSBO = OpTypeStruct %_ptr_PhysicalStorageBuffer_Ptr
2459 %uchar = OpTypeInt 8 0
2460 %uint = OpTypeInt 32 0
2461 %double = OpTypeFloat 64
2462 %Ptr = OpTypeStruct %uchar %uint %double
2463 %_ptr_PhysicalStorageBuffer_Ptr = OpTypePointer PhysicalStorageBuffer %Ptr
2464 %_ptr_StorageBuffer_SSBO = OpTypePointer StorageBuffer %SSBO
2465 %_ = OpVariable %_ptr_StorageBuffer_SSBO StorageBuffer
2466 %int = OpTypeInt 32 1
2467 %int_0 = OpConstant %int 0
2468 %_ptr_StorageBuffer__ptr_PhysicalStorageBuffer_Ptr = OpTypePointer StorageBuffer %_ptr_PhysicalStorageBuffer_Ptr
2469 %uchar_0 = OpConstant %uchar 0
2470 %_ptr_PhysicalStorageBuffer_uchar = OpTypePointer PhysicalStorageBuffer %uchar
2471 %int_1 = OpConstant %int 1
2472 %uint_0 = OpConstant %uint 0
2473 %_ptr_PhysicalStorageBuffer_uint = OpTypePointer PhysicalStorageBuffer %uint
2474 %int_2 = OpConstant %int 2
2475 %double_0 = OpConstant %double 0
2476 %_ptr_PhysicalStorageBuffer_double = OpTypePointer PhysicalStorageBuffer %double
2477 %main = OpFunction %void None %4
2478 %6 = OpLabel
2479 %18 = OpAccessChain %_ptr_StorageBuffer__ptr_PhysicalStorageBuffer_Ptr %_ %int_0
2480 %19 = OpLoad %_ptr_PhysicalStorageBuffer_Ptr %18
2481 %22 = OpAccessChain %_ptr_PhysicalStorageBuffer_uchar %19 %int_0
2482 OpStore %22 %uchar_0 Aligned 1
2483 %23 = OpAccessChain %_ptr_StorageBuffer__ptr_PhysicalStorageBuffer_Ptr %_ %int_0
2484 %24 = OpLoad %_ptr_PhysicalStorageBuffer_Ptr %23
2485 %28 = OpAccessChain %_ptr_PhysicalStorageBuffer_uint %24 %int_1
2486 OpStore %28 %uint_0 Aligned 4
2487 %29 = OpAccessChain %_ptr_StorageBuffer__ptr_PhysicalStorageBuffer_Ptr %_ %int_0
2488 %30 = OpLoad %_ptr_PhysicalStorageBuffer_Ptr %29
2489 %34 = OpAccessChain %_ptr_PhysicalStorageBuffer_double %30 %int_2
2490 OpStore %34 %double_0 Aligned 8
2491 OpReturn
2492 OpFunctionEnd
2493 )";
2494
2495 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_2);
2496 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
2497 }
2498
2499 // https://github.com/KhronosGroup/SPIRV-Tools/issues/6322
2500 TEST_F(ValidateMemory, PSBStoreAlignedUntypedBitcaseSuccess) {
2501 const std::string body = R"(
2502 OpCapability Shader
2503 OpCapability UntypedPointersKHR
2504 OpCapability VulkanMemoryModel
2505 OpCapability VulkanMemoryModelDeviceScopeKHR
2506 OpCapability Int8
2507 OpCapability PhysicalStorageBufferAddresses
2508 OpExtension "SPV_KHR_storage_buffer_storage_class"
2509 OpExtension "SPV_KHR_untyped_pointers"
2510 OpExtension "SPV_KHR_vulkan_memory_model"
2511 OpExtension "SPV_KHR_physical_storage_buffer"
2512 OpMemoryModel PhysicalStorageBuffer64 Vulkan
2513 OpEntryPoint GLCompute %main "main" %id
2514 OpExecutionMode %main LocalSize 1 1 1
2515 OpDecorate %id BuiltIn GlobalInvocationId
2516 OpDecorate %untyped_phys_ptr ArrayStride 1
2517 OpDecorate %data_buffer Block
2518 OpMemberDecorate %data_buffer 0 Offset 0
2519 OpDecorate %phys_ptrs_struct Block
2520 OpMemberDecorate %phys_ptrs_struct 0 Offset 0
2521 OpMemberDecorate %phys_ptrs_struct 1 Offset 8
2522 OpDecorate %all_data_var DescriptorSet 0
2523 OpDecorate %all_data_var Binding 0
2524
2525 %uint32 = OpTypeInt 32 0
2526 %void = OpTypeVoid
2527 %uint8 = OpTypeInt 8 0
2528 %vec3_uint32 = OpTypeVector %uint32 3
2529 %void_func = OpTypeFunction %void
2530 %c_uint32_0 = OpConstant %uint32 0
2531 %c_uint32_1 = OpConstant %uint32 1
2532 %c_uint32_32 = OpConstant %uint32 32
2533
2534 %uint32_input_ptr = OpTypePointer Input %uint32
2535 %vec3_uint32_input_ptr = OpTypePointer Input %vec3_uint32
2536 %data_buffer = OpTypeStruct %uint8
2537 %untyped_phys_ptr = OpTypeUntypedPointerKHR PhysicalStorageBuffer
2538 %uint8_phys_ptr = OpTypePointer PhysicalStorageBuffer %uint8
2539 %data_buffer_phys_ptr = OpTypePointer PhysicalStorageBuffer %data_buffer
2540 %data_buffer_phys_ptr_ptr = OpTypePointer StorageBuffer %data_buffer_phys_ptr
2541 %phys_ptrs_struct = OpTypeStruct %data_buffer_phys_ptr %data_buffer_phys_ptr
2542 %phys_ptrs_struct_ptr = OpTypePointer StorageBuffer %phys_ptrs_struct
2543 %all_data_var = OpVariable %phys_ptrs_struct_ptr StorageBuffer
2544 %id = OpVariable %vec3_uint32_input_ptr Input
2545 %main = OpFunction %void None %void_func
2546 %label_main = OpLabel
2547 %input_ptr = OpAccessChain %data_buffer_phys_ptr_ptr %all_data_var %c_uint32_0
2548 %input = OpLoad %data_buffer_phys_ptr %input_ptr
2549 %input_loc = OpAccessChain %uint8_phys_ptr %input %c_uint32_0
2550 %output_ptr = OpAccessChain %data_buffer_phys_ptr_ptr %all_data_var %c_uint32_1
2551 %output = OpLoad %data_buffer_phys_ptr %output_ptr
2552 %output_loc = OpUntypedAccessChainKHR %untyped_phys_ptr %data_buffer %output %c_uint32_0
2553 %bitcasted = OpBitcast %untyped_phys_ptr %input_loc
2554 %bitcasted_val = OpLoad %uint8 %bitcasted Aligned 1
2555 OpStore %output_loc %bitcasted_val Aligned 1
2556 OpReturn
2557 OpFunctionEnd
2558 )";
2559
2560 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_1);
2561 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
2562 }
2563
2564 // https://github.com/KhronosGroup/SPIRV-Tools/issues/6322
2565 TEST_F(ValidateMemory, PSBStoreAlignedUntypedBitcase) {
2566 const std::string body = R"(
2567 OpCapability Shader
2568 OpCapability UntypedPointersKHR
2569 OpCapability VulkanMemoryModel
2570 OpCapability VulkanMemoryModelDeviceScopeKHR
2571 OpCapability Int16
2572 OpCapability PhysicalStorageBufferAddresses
2573 OpExtension "SPV_KHR_storage_buffer_storage_class"
2574 OpExtension "SPV_KHR_untyped_pointers"
2575 OpExtension "SPV_KHR_vulkan_memory_model"
2576 OpExtension "SPV_KHR_physical_storage_buffer"
2577 OpMemoryModel PhysicalStorageBuffer64 Vulkan
2578 OpEntryPoint GLCompute %main "main" %id
2579 OpExecutionMode %main LocalSize 1 1 1
2580 OpDecorate %id BuiltIn GlobalInvocationId
2581 OpDecorate %untyped_phys_ptr ArrayStride 1
2582 OpDecorate %data_buffer Block
2583 OpMemberDecorate %data_buffer 0 Offset 0
2584 OpDecorate %phys_ptrs_struct Block
2585 OpMemberDecorate %phys_ptrs_struct 0 Offset 0
2586 OpMemberDecorate %phys_ptrs_struct 1 Offset 8
2587 OpDecorate %all_data_var DescriptorSet 0
2588 OpDecorate %all_data_var Binding 0
2589
2590 %uint32 = OpTypeInt 32 0
2591 %void = OpTypeVoid
2592 %uint16 = OpTypeInt 16 0
2593 %vec3_uint32 = OpTypeVector %uint32 3
2594 %void_func = OpTypeFunction %void
2595 %c_uint32_0 = OpConstant %uint32 0
2596 %c_uint32_1 = OpConstant %uint32 1
2597 %c_uint32_32 = OpConstant %uint32 32
2598
2599 %uint32_input_ptr = OpTypePointer Input %uint32
2600 %vec3_uint32_input_ptr = OpTypePointer Input %vec3_uint32
2601 %data_buffer = OpTypeStruct %uint16
2602 %untyped_phys_ptr = OpTypeUntypedPointerKHR PhysicalStorageBuffer
2603 %uint16_phys_ptr = OpTypePointer PhysicalStorageBuffer %uint16
2604 %data_buffer_phys_ptr = OpTypePointer PhysicalStorageBuffer %data_buffer
2605 %data_buffer_phys_ptr_ptr = OpTypePointer StorageBuffer %data_buffer_phys_ptr
2606 %phys_ptrs_struct = OpTypeStruct %data_buffer_phys_ptr %data_buffer_phys_ptr
2607 %phys_ptrs_struct_ptr = OpTypePointer StorageBuffer %phys_ptrs_struct
2608 %all_data_var = OpVariable %phys_ptrs_struct_ptr StorageBuffer
2609 %id = OpVariable %vec3_uint32_input_ptr Input
2610 %main = OpFunction %void None %void_func
2611 %label_main = OpLabel
2612 %input_ptr = OpAccessChain %data_buffer_phys_ptr_ptr %all_data_var %c_uint32_0
2613 %input = OpLoad %data_buffer_phys_ptr %input_ptr
2614 %input_loc = OpAccessChain %uint16_phys_ptr %input %c_uint32_0
2615 %output_ptr = OpAccessChain %data_buffer_phys_ptr_ptr %all_data_var %c_uint32_1
2616 %output = OpLoad %data_buffer_phys_ptr %output_ptr
2617 %output_loc = OpUntypedAccessChainKHR %untyped_phys_ptr %data_buffer %output %c_uint32_0
2618 %bitcasted = OpBitcast %untyped_phys_ptr %input_loc
2619 %bitcasted_val = OpLoad %uint16 %bitcasted Aligned 1
2620 OpStore %output_loc %bitcasted_val Aligned 1
2621 OpReturn
2622 OpFunctionEnd
2623 )";
2624
2625 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_1);
2626 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
2627 EXPECT_THAT(getDiagnosticString(),
2628 AnyVUID("VUID-StandaloneSpirv-PhysicalStorageBuffer64-06314"));
2629 EXPECT_THAT(getDiagnosticString(),
2630 HasSubstr("Memory accesses Aligned operand value 1 is too small, "
2631 "the largest scalar type is 2 bytes"));
2632 }
2633
2634 std::string GenCoopMatLoadStoreShader(const std::string& storeMemoryAccess,
2635 const std::string& loadMemoryAccess) {
2636 std::string s = R"(
2637 OpCapability Shader
2638 OpCapability GroupNonUniform
2639 OpCapability VulkanMemoryModelKHR
2640 OpCapability CooperativeMatrixNV
2641 OpExtension "SPV_KHR_vulkan_memory_model"
2642 OpExtension "SPV_NV_cooperative_matrix"
2643 %1 = OpExtInstImport "GLSL.std.450"
2644 OpMemoryModel Logical VulkanKHR
2645 OpEntryPoint GLCompute %4 "main" %11 %21
2646 OpExecutionMode %4 LocalSize 1 1 1
2647 OpDecorate %11 BuiltIn SubgroupId
2648 OpDecorate %21 BuiltIn WorkgroupId
2649 OpDecorate %74 ArrayStride 4
2650 OpMemberDecorate %75 0 Offset 0
2651 OpDecorate %75 Block
2652 OpDecorate %77 DescriptorSet 0
2653 OpDecorate %77 Binding 0
2654 OpDecorate %92 ArrayStride 4
2655 OpMemberDecorate %93 0 Offset 0
2656 OpDecorate %93 Block
2657 OpDecorate %95 DescriptorSet 0
2658 OpDecorate %95 Binding 1
2659 OpDecorate %102 ArrayStride 4
2660 OpMemberDecorate %103 0 Offset 0
2661 OpDecorate %103 Block
2662 OpDecorate %105 DescriptorSet 0
2663 OpDecorate %105 Binding 2
2664 OpDecorate %117 ArrayStride 4
2665 OpMemberDecorate %118 0 Offset 0
2666 OpDecorate %118 Block
2667 OpDecorate %120 DescriptorSet 0
2668 OpDecorate %120 Binding 3
2669 OpDecorate %123 SpecId 2
2670 OpDecorate %124 SpecId 3
2671 OpDecorate %125 SpecId 4
2672 OpDecorate %126 SpecId 5
2673 OpDecorate %127 SpecId 0
2674 OpDecorate %128 SpecId 1
2675 OpDecorate %129 BuiltIn WorkgroupSize
2676 %2 = OpTypeVoid
2677 %3 = OpTypeFunction %2
2678 %6 = OpTypeInt 32 0
2679 %7 = OpTypeVector %6 2
2680 %8 = OpTypePointer Function %7
2681 %10 = OpTypePointer Input %6
2682 %11 = OpVariable %10 Input
2683 %13 = OpConstant %6 2
2684 %19 = OpTypeVector %6 3
2685 %20 = OpTypePointer Input %19
2686 %21 = OpVariable %20 Input
2687 %27 = OpConstantComposite %7 %13 %13
2688 %31 = OpTypePointer Function %6
2689 %33 = OpConstant %6 1024
2690 %34 = OpConstant %6 1
2691 %38 = OpConstant %6 8
2692 %39 = OpConstant %6 0
2693 %68 = OpTypeFloat 32
2694 %69 = OpConstant %6 16
2695 %70 = OpConstant %6 3
2696 %71 = OpTypeCooperativeMatrixNV %68 %70 %69 %38
2697 %72 = OpTypePointer Function %71
2698 %74 = OpTypeRuntimeArray %68
2699 %75 = OpTypeStruct %74
2700 %76 = OpTypePointer StorageBuffer %75
2701 %77 = OpVariable %76 StorageBuffer
2702 %78 = OpTypeInt 32 1
2703 %79 = OpConstant %78 0
2704 %81 = OpConstant %6 5
2705 %82 = OpTypePointer StorageBuffer %68
2706 %84 = OpConstant %6 64
2707 %85 = OpTypeBool
2708 %86 = OpConstantFalse %85
2709 %88 = OpTypePointer Private %71
2710 %89 = OpVariable %88 Private
2711 %92 = OpTypeRuntimeArray %68
2712 %93 = OpTypeStruct %92
2713 %94 = OpTypePointer StorageBuffer %93
2714 %95 = OpVariable %94 StorageBuffer
2715 %99 = OpVariable %88 Private
2716 %102 = OpTypeRuntimeArray %68
2717 %103 = OpTypeStruct %102
2718 %104 = OpTypePointer StorageBuffer %103
2719 %105 = OpVariable %104 StorageBuffer
2720 %109 = OpVariable %88 Private
2721 %111 = OpVariable %88 Private
2722 %112 = OpSpecConstantOp %6 CooperativeMatrixLengthNV %71
2723 %113 = OpSpecConstantOp %78 IAdd %112 %79
2724 %117 = OpTypeRuntimeArray %68
2725 %118 = OpTypeStruct %117
2726 %119 = OpTypePointer StorageBuffer %118
2727 %120 = OpVariable %119 StorageBuffer
2728 %123 = OpSpecConstant %78 1
2729 %124 = OpSpecConstant %78 1
2730 %125 = OpSpecConstant %78 1
2731 %126 = OpSpecConstant %78 1
2732 %127 = OpSpecConstant %6 1
2733 %128 = OpSpecConstant %6 1
2734 %129 = OpSpecConstantComposite %19 %127 %128 %34
2735 %4 = OpFunction %2 None %3
2736 %5 = OpLabel
2737 %9 = OpVariable %8 Function
2738 %18 = OpVariable %8 Function
2739 %32 = OpVariable %31 Function
2740 %44 = OpVariable %31 Function
2741 %52 = OpVariable %31 Function
2742 %60 = OpVariable %31 Function
2743 %73 = OpVariable %72 Function
2744 %91 = OpVariable %72 Function
2745 %101 = OpVariable %72 Function
2746 %12 = OpLoad %6 %11
2747 %14 = OpUMod %6 %12 %13
2748 %15 = OpLoad %6 %11
2749 %16 = OpUDiv %6 %15 %13
2750 %17 = OpCompositeConstruct %7 %14 %16
2751 OpStore %9 %17
2752 %22 = OpLoad %19 %21
2753 %23 = OpVectorShuffle %7 %22 %22 0 1
2754 %24 = OpCompositeExtract %6 %23 0
2755 %25 = OpCompositeExtract %6 %23 1
2756 %26 = OpCompositeConstruct %7 %24 %25
2757 %28 = OpIMul %7 %26 %27
2758 %29 = OpLoad %7 %9
2759 %30 = OpIAdd %7 %28 %29
2760 OpStore %18 %30
2761 %35 = OpAccessChain %31 %18 %34
2762 %36 = OpLoad %6 %35
2763 %37 = OpIMul %6 %33 %36
2764 %40 = OpAccessChain %31 %18 %39
2765 %41 = OpLoad %6 %40
2766 %42 = OpIMul %6 %38 %41
2767 %43 = OpIAdd %6 %37 %42
2768 OpStore %32 %43
2769 %45 = OpAccessChain %31 %18 %34
2770 %46 = OpLoad %6 %45
2771 %47 = OpIMul %6 %33 %46
2772 %48 = OpAccessChain %31 %18 %39
2773 %49 = OpLoad %6 %48
2774 %50 = OpIMul %6 %38 %49
2775 %51 = OpIAdd %6 %47 %50
2776 OpStore %44 %51
2777 %53 = OpAccessChain %31 %18 %34
2778 %54 = OpLoad %6 %53
2779 %55 = OpIMul %6 %33 %54
2780 %56 = OpAccessChain %31 %18 %39
2781 %57 = OpLoad %6 %56
2782 %58 = OpIMul %6 %38 %57
2783 %59 = OpIAdd %6 %55 %58
2784 OpStore %52 %59
2785 %61 = OpAccessChain %31 %18 %34
2786 %62 = OpLoad %6 %61
2787 %63 = OpIMul %6 %33 %62
2788 %64 = OpAccessChain %31 %18 %39
2789 %65 = OpLoad %6 %64
2790 %66 = OpIMul %6 %38 %65
2791 %67 = OpIAdd %6 %63 %66
2792 OpStore %60 %67
2793 %80 = OpLoad %6 %32
2794 %83 = OpAccessChain %82 %77 %79 %80
2795 %87 = OpCooperativeMatrixLoadNV %71 %83 %84 %86 )" +
2796 loadMemoryAccess + R"( %81
2797 OpStore %73 %87
2798 %90 = OpLoad %71 %73
2799 OpStore %89 %90
2800 %96 = OpLoad %6 %44
2801 %97 = OpAccessChain %82 %95 %79 %96
2802 %98 = OpCooperativeMatrixLoadNV %71 %97 %84 %86 MakePointerVisibleKHR|NonPrivatePointerKHR %81
2803 OpStore %91 %98
2804 %100 = OpLoad %71 %91
2805 OpStore %99 %100
2806 %106 = OpLoad %6 %52
2807 %107 = OpAccessChain %82 %105 %79 %106
2808 %108 = OpCooperativeMatrixLoadNV %71 %107 %84 %86 MakePointerVisibleKHR|NonPrivatePointerKHR %81
2809 OpStore %101 %108
2810 %110 = OpLoad %71 %101
2811 OpStore %109 %110
2812 %114 = OpConvertSToF %68 %113
2813 %115 = OpCompositeConstruct %71 %114
2814 OpStore %111 %115
2815 %116 = OpLoad %71 %111
2816 %121 = OpLoad %6 %60
2817 %122 = OpAccessChain %82 %120 %79 %121
2818 OpCooperativeMatrixStoreNV %122 %116 %84 %86 )" +
2819 storeMemoryAccess + R"( %81
2820 OpReturn
2821 OpFunctionEnd
2822 )";
2823
2824 return s;
2825 }
2826
2827 TEST_F(ValidateMemory, CoopMatLoadStoreSuccess) {
2828 std::string spirv =
2829 GenCoopMatLoadStoreShader("MakePointerAvailableKHR|NonPrivatePointerKHR",
2830 "MakePointerVisibleKHR|NonPrivatePointerKHR");
2831
2832 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
2833 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
2834 }
2835
2836 TEST_F(ValidateMemory, CoopMatStoreMemoryAccessFail) {
2837 std::string spirv =
2838 GenCoopMatLoadStoreShader("MakePointerVisibleKHR|NonPrivatePointerKHR",
2839 "MakePointerVisibleKHR|NonPrivatePointerKHR");
2840
2841 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
2842 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
2843 EXPECT_THAT(getDiagnosticString(),
2844 HasSubstr("MakePointerVisibleKHR cannot be used with OpStore"));
2845 }
2846
2847 TEST_F(ValidateMemory, CoopMatLoadMemoryAccessFail) {
2848 std::string spirv =
2849 GenCoopMatLoadStoreShader("MakePointerAvailableKHR|NonPrivatePointerKHR",
2850 "MakePointerAvailableKHR|NonPrivatePointerKHR");
2851
2852 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
2853 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
2854 EXPECT_THAT(getDiagnosticString(),
2855 HasSubstr("MakePointerAvailableKHR cannot be used with OpLoad"));
2856 }
2857
2858 TEST_F(ValidateMemory, CoopMatInvalidStorageClassFail) {
2859 const std::string body =
2860 R"(
2861 OpCapability Shader
2862 OpCapability Float16
2863 OpCapability CooperativeMatrixNV
2864 OpExtension "SPV_NV_cooperative_matrix"
2865 OpMemoryModel Logical GLSL450
2866 OpEntryPoint GLCompute %main "main"
2867 %void = OpTypeVoid
2868 %func = OpTypeFunction %void
2869 %f16 = OpTypeFloat 16
2870 %u32 = OpTypeInt 32 0
2871
2872 %u32_8 = OpConstant %u32 8
2873 %subgroup = OpConstant %u32 3
2874
2875 %f16mat = OpTypeCooperativeMatrixNV %f16 %subgroup %u32_8 %u32_8
2876
2877 %str = OpTypeStruct %f16mat
2878 %str_ptr = OpTypePointer Workgroup %str
2879 %sh = OpVariable %str_ptr Workgroup
2880
2881 %main = OpFunction %void None %func
2882 %main_entry = OpLabel
2883
2884 OpReturn
2885 OpFunctionEnd)";
2886
2887 CompileSuccessfully(body.c_str());
2888 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2889 EXPECT_THAT(
2890 getDiagnosticString(),
2891 HasSubstr(
2892 "Cooperative matrix types (or types containing them) can only be "
2893 "allocated in Function or Private storage classes or as function "
2894 "parameters"));
2895 }
2896
2897 TEST_F(ValidateMemory, CoopMatMatrixLengthResultTypeBad) {
2898 const std::string body =
2899 R"(
2900 OpCapability Shader
2901 OpCapability Float16
2902 OpCapability CooperativeMatrixNV
2903 OpExtension "SPV_NV_cooperative_matrix"
2904 OpMemoryModel Logical GLSL450
2905 OpEntryPoint GLCompute %main "main"
2906 %void = OpTypeVoid
2907 %func = OpTypeFunction %void
2908 %f16 = OpTypeFloat 16
2909 %u32 = OpTypeInt 32 0
2910 %i32 = OpTypeInt 32 1
2911
2912 %u32_8 = OpConstant %u32 8
2913 %subgroup = OpConstant %u32 3
2914
2915 %f16mat = OpTypeCooperativeMatrixNV %f16 %subgroup %u32_8 %u32_8
2916
2917 %main = OpFunction %void None %func
2918 %main_entry = OpLabel
2919
2920 %1 = OpCooperativeMatrixLengthNV %i32 %f16mat
2921
2922 OpReturn
2923 OpFunctionEnd)";
2924
2925 CompileSuccessfully(body.c_str());
2926 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2927 EXPECT_THAT(
2928 getDiagnosticString(),
2929 HasSubstr("The Result Type of OpCooperativeMatrixLengthNV <id> "
2930 "'11[%11]' must be OpTypeInt with width 32 and signedness 0"));
2931 }
2932
2933 TEST_F(ValidateMemory, CoopMatMatrixLengthOperandTypeBad) {
2934 const std::string body =
2935 R"(
2936 OpCapability Shader
2937 OpCapability Float16
2938 OpCapability CooperativeMatrixNV
2939 OpExtension "SPV_NV_cooperative_matrix"
2940 OpMemoryModel Logical GLSL450
2941 OpEntryPoint GLCompute %main "main"
2942 %void = OpTypeVoid
2943 %func = OpTypeFunction %void
2944 %f16 = OpTypeFloat 16
2945 %u32 = OpTypeInt 32 0
2946 %i32 = OpTypeInt 32 1
2947
2948 %u32_8 = OpConstant %u32 8
2949 %subgroup = OpConstant %u32 3
2950
2951 %f16mat = OpTypeCooperativeMatrixNV %f16 %subgroup %u32_8 %u32_8
2952
2953 %main = OpFunction %void None %func
2954 %main_entry = OpLabel
2955
2956 %1 = OpCooperativeMatrixLengthNV %u32 %u32
2957
2958 OpReturn
2959 OpFunctionEnd)";
2960
2961 CompileSuccessfully(body.c_str());
2962 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2963 EXPECT_THAT(
2964 getDiagnosticString(),
2965 HasSubstr("The type in OpCooperativeMatrixLengthNV <id> '5[%uint]' "
2966 "must be OpTypeCooperativeMatrixNV"));
2967 }
2968
2969 TEST_F(ValidateMemory, CoopMatMatrixLengthGood) {
2970 const std::string body =
2971 R"(
2972 OpCapability Shader
2973 OpCapability Float16
2974 OpCapability CooperativeMatrixNV
2975 OpExtension "SPV_NV_cooperative_matrix"
2976 OpMemoryModel Logical GLSL450
2977 OpEntryPoint GLCompute %main "main"
2978 %void = OpTypeVoid
2979 %func = OpTypeFunction %void
2980 %f16 = OpTypeFloat 16
2981 %u32 = OpTypeInt 32 0
2982 %i32 = OpTypeInt 32 1
2983
2984 %u32_8 = OpConstant %u32 8
2985 %subgroup = OpConstant %u32 3
2986
2987 %f16mat = OpTypeCooperativeMatrixNV %f16 %subgroup %u32_8 %u32_8
2988
2989 %main = OpFunction %void None %func
2990 %main_entry = OpLabel
2991
2992 %1 = OpCooperativeMatrixLengthNV %u32 %f16mat
2993
2994 OpReturn
2995 OpFunctionEnd)";
2996
2997 CompileSuccessfully(body.c_str());
2998 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2999 }
3000
3001 std::string GenCoopMatLoadStoreShaderKHR(
3002 const std::string& storeMemoryAccess, const std::string& loadMemoryAccess,
3003 unsigned layout = 0, bool useSpecConstantLayout = false,
3004 bool useStoreStride = true, bool useLoadStride = true,
3005 bool useConstantStride = true, unsigned stride = 4, unsigned rows = 16,
3006 unsigned cols = 16) {
3007 std::string s = R"(
3008 OpCapability Shader
3009 OpCapability GroupNonUniform
3010 OpCapability VulkanMemoryModelKHR
3011 OpCapability CooperativeMatrixKHR
3012 OpCapability CooperativeMatrixLayoutsARM
3013 OpExtension "SPV_KHR_vulkan_memory_model"
3014 OpExtension "SPV_KHR_cooperative_matrix"
3015 OpExtension "SPV_ARM_cooperative_matrix_layouts"
3016 %1 = OpExtInstImport "GLSL.std.450"
3017 OpMemoryModel Logical VulkanKHR
3018 OpEntryPoint GLCompute %4 "main" %11 %21
3019 OpExecutionMode %4 LocalSize 1 1 1
3020 OpDecorate %11 BuiltIn SubgroupId
3021 OpDecorate %21 BuiltIn WorkgroupId
3022 OpDecorate %74 ArrayStride 4
3023 OpMemberDecorate %75 0 Offset 0
3024 OpDecorate %75 Block
3025 OpDecorate %77 DescriptorSet 0
3026 OpDecorate %77 Binding 0
3027 OpDecorate %92 ArrayStride 4
3028 OpMemberDecorate %93 0 Offset 0
3029 OpDecorate %93 Block
3030 OpDecorate %95 DescriptorSet 0
3031 OpDecorate %95 Binding 1
3032 OpDecorate %102 ArrayStride 4
3033 OpMemberDecorate %103 0 Offset 0
3034 OpDecorate %103 Block
3035 OpDecorate %105 DescriptorSet 0
3036 OpDecorate %105 Binding 2
3037 OpDecorate %117 ArrayStride 4
3038 OpMemberDecorate %118 0 Offset 0
3039 OpDecorate %118 Block
3040 OpDecorate %120 DescriptorSet 0
3041 OpDecorate %120 Binding 3
3042 OpDecorate %123 SpecId 2
3043 OpDecorate %124 SpecId 3
3044 OpDecorate %125 SpecId 4
3045 OpDecorate %126 SpecId 5
3046 OpDecorate %127 SpecId 0
3047 OpDecorate %128 SpecId 1
3048 OpDecorate %129 BuiltIn WorkgroupSize
3049 %2 = OpTypeVoid
3050 %3 = OpTypeFunction %2
3051 %6 = OpTypeInt 32 0
3052 %7 = OpTypeVector %6 2
3053 %8 = OpTypePointer Function %7
3054 %10 = OpTypePointer Input %6
3055 %ptr_uint_Function = OpTypePointer Function %6
3056 %11 = OpVariable %10 Input
3057 %13 = OpConstant %6 2
3058 %19 = OpTypeVector %6 3
3059 %20 = OpTypePointer Input %19
3060 %21 = OpVariable %20 Input
3061 %27 = OpConstantComposite %7 %13 %13
3062 %31 = OpTypePointer Function %6
3063 %33 = OpConstant %6 1024
3064 %34 = OpConstant %6 1
3065 %cols = OpConstant %6 )" +
3066 std::to_string(cols) + R"(
3067 %uint_0 = OpConstant %6 0
3068 )";
3069 if (useSpecConstantLayout) {
3070 s += "%layout = OpSpecConstant %6 " + std::to_string(layout);
3071 } else {
3072 s += "%layout = OpConstant %6 " + std::to_string(layout);
3073 }
3074 s += R"(
3075 %68 = OpTypeFloat 32
3076 %rows = OpConstant %6 )" +
3077 std::to_string(rows) + R"(
3078 %scope = OpConstant %6 3
3079 %71 = OpTypeCooperativeMatrixKHR %68 %scope %rows %cols %uint_0
3080 %72 = OpTypePointer Function %71
3081 %74 = OpTypeRuntimeArray %68
3082 %75 = OpTypeStruct %74
3083 %76 = OpTypePointer StorageBuffer %75
3084 %77 = OpVariable %76 StorageBuffer
3085 %78 = OpTypeInt 32 1
3086 %79 = OpConstant %78 0
3087 %81 = OpConstant %6 5
3088 %82 = OpTypePointer StorageBuffer %68
3089 )";
3090 if (useConstantStride) {
3091 s += "%stride = OpConstant %6 " + std::to_string(stride);
3092 }
3093 s += R"(
3094 %88 = OpTypePointer Private %71
3095 %89 = OpVariable %88 Private
3096 %92 = OpTypeRuntimeArray %68
3097 %93 = OpTypeStruct %92
3098 %94 = OpTypePointer StorageBuffer %93
3099 %95 = OpVariable %94 StorageBuffer
3100 %99 = OpVariable %88 Private
3101 %102 = OpTypeRuntimeArray %68
3102 %103 = OpTypeStruct %102
3103 %104 = OpTypePointer StorageBuffer %103
3104 %105 = OpVariable %104 StorageBuffer
3105 %109 = OpVariable %88 Private
3106 %111 = OpVariable %88 Private
3107 %112 = OpSpecConstantOp %6 CooperativeMatrixLengthKHR %71
3108 %113 = OpSpecConstantOp %78 IAdd %112 %79
3109 %117 = OpTypeRuntimeArray %68
3110 %118 = OpTypeStruct %117
3111 %119 = OpTypePointer StorageBuffer %118
3112 %120 = OpVariable %119 StorageBuffer
3113 %123 = OpSpecConstant %78 1
3114 %124 = OpSpecConstant %78 1
3115 %125 = OpSpecConstant %78 1
3116 %126 = OpSpecConstant %78 1
3117 %127 = OpSpecConstant %6 1
3118 %128 = OpSpecConstant %6 1
3119 %129 = OpSpecConstantComposite %19 %127 %128 %34
3120 %4 = OpFunction %2 None %3
3121 %5 = OpLabel
3122 %9 = OpVariable %8 Function
3123 %18 = OpVariable %8 Function
3124 %stride_var = OpVariable %ptr_uint_Function Function
3125 %32 = OpVariable %31 Function
3126 %44 = OpVariable %31 Function
3127 %52 = OpVariable %31 Function
3128 %60 = OpVariable %31 Function
3129 %73 = OpVariable %72 Function
3130 %91 = OpVariable %72 Function
3131 %101 = OpVariable %72 Function
3132 )";
3133 if (!useConstantStride) {
3134 s += "%stride = OpLoad %6 %stride_var";
3135 }
3136 s += R"(
3137 %12 = OpLoad %6 %11
3138 %14 = OpUMod %6 %12 %13
3139 %15 = OpLoad %6 %11
3140 %16 = OpUDiv %6 %15 %13
3141 %17 = OpCompositeConstruct %7 %14 %16
3142 OpStore %9 %17
3143 %22 = OpLoad %19 %21
3144 %23 = OpVectorShuffle %7 %22 %22 0 1
3145 %24 = OpCompositeExtract %6 %23 0
3146 %25 = OpCompositeExtract %6 %23 1
3147 %26 = OpCompositeConstruct %7 %24 %25
3148 %28 = OpIMul %7 %26 %27
3149 %29 = OpLoad %7 %9
3150 %30 = OpIAdd %7 %28 %29
3151 OpStore %18 %30
3152 %35 = OpAccessChain %31 %18 %34
3153 %36 = OpLoad %6 %35
3154 %37 = OpIMul %6 %33 %36
3155 %40 = OpAccessChain %31 %18 %uint_0
3156 %41 = OpLoad %6 %40
3157 %42 = OpIMul %6 %cols %41
3158 %43 = OpIAdd %6 %37 %42
3159 OpStore %32 %43
3160 %45 = OpAccessChain %31 %18 %34
3161 %46 = OpLoad %6 %45
3162 %47 = OpIMul %6 %33 %46
3163 %48 = OpAccessChain %31 %18 %uint_0
3164 %49 = OpLoad %6 %48
3165 %50 = OpIMul %6 %cols %49
3166 %51 = OpIAdd %6 %47 %50
3167 OpStore %44 %51
3168 %53 = OpAccessChain %31 %18 %34
3169 %54 = OpLoad %6 %53
3170 %55 = OpIMul %6 %33 %54
3171 %56 = OpAccessChain %31 %18 %uint_0
3172 %57 = OpLoad %6 %56
3173 %58 = OpIMul %6 %cols %57
3174 %59 = OpIAdd %6 %55 %58
3175 OpStore %52 %59
3176 %61 = OpAccessChain %31 %18 %34
3177 %62 = OpLoad %6 %61
3178 %63 = OpIMul %6 %33 %62
3179 %64 = OpAccessChain %31 %18 %uint_0
3180 %65 = OpLoad %6 %64
3181 %66 = OpIMul %6 %cols %65
3182 %67 = OpIAdd %6 %63 %66
3183 OpStore %60 %67
3184 %80 = OpLoad %6 %32
3185 %83 = OpAccessChain %82 %77 %79 %80
3186 )";
3187 if (useLoadStride) {
3188 s += "%87 = OpCooperativeMatrixLoadKHR %71 %83 %layout %stride " +
3189 loadMemoryAccess + " %81";
3190 } else {
3191 s += "%87 = OpCooperativeMatrixLoadKHR %71 %83 %layout";
3192 }
3193 s += R"(
3194 OpStore %73 %87
3195 %90 = OpLoad %71 %73
3196 OpStore %89 %90
3197 %96 = OpLoad %6 %44
3198 %97 = OpAccessChain %82 %95 %79 %96
3199 %98 = OpCooperativeMatrixLoadKHR %71 %97 %layout %stride MakePointerVisibleKHR|NonPrivatePointerKHR %81
3200 OpStore %91 %98
3201 %100 = OpLoad %71 %91
3202 OpStore %99 %100
3203 %106 = OpLoad %6 %52
3204 %107 = OpAccessChain %82 %105 %79 %106
3205 %108 = OpCooperativeMatrixLoadKHR %71 %107 %layout %stride MakePointerVisibleKHR|NonPrivatePointerKHR %81
3206 OpStore %101 %108
3207 %110 = OpLoad %71 %101
3208 OpStore %109 %110
3209 %114 = OpConvertSToF %68 %113
3210 %115 = OpCompositeConstruct %71 %114
3211 OpStore %111 %115
3212 %116 = OpLoad %71 %111
3213 %121 = OpLoad %6 %60
3214 %122 = OpAccessChain %82 %120 %79 %121
3215 )";
3216 if (useStoreStride) {
3217 s += "OpCooperativeMatrixStoreKHR %122 %116 %layout %stride " +
3218 storeMemoryAccess + " %81";
3219 } else {
3220 s += "OpCooperativeMatrixStoreKHR %122 %116 %layout";
3221 }
3222 s += R"(
3223 OpReturn
3224 OpFunctionEnd
3225 )";
3226
3227 return s;
3228 }
3229
3230 TEST_F(ValidateMemory, CoopMatKHRLoadStoreSuccess) {
3231 std::string spirv = GenCoopMatLoadStoreShaderKHR(
3232 "MakePointerAvailableKHR|NonPrivatePointerKHR",
3233 "MakePointerVisibleKHR|NonPrivatePointerKHR");
3234
3235 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
3236 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3237 }
3238
3239 struct StrideMissingCase {
3240 unsigned layout;
3241 bool useLoadStride;
3242 bool useStoreStride;
3243 };
3244
3245 using ValidateCoopMatrixStrideMissing =
3246 spvtest::ValidateBase<StrideMissingCase>;
3247
3248 INSTANTIATE_TEST_SUITE_P(
3249 CoopMatrixStrideMissing, ValidateCoopMatrixStrideMissing,
3250 Values(
3251 StrideMissingCase{(unsigned)spv::CooperativeMatrixLayout::RowMajorKHR,
3252 false, true},
3253 StrideMissingCase{(unsigned)spv::CooperativeMatrixLayout::RowMajorKHR,
3254 true, false},
3255 StrideMissingCase{
3256 (unsigned)spv::CooperativeMatrixLayout::ColumnMajorKHR, false,
3257 true},
3258 StrideMissingCase{
3259 (unsigned)spv::CooperativeMatrixLayout::ColumnMajorKHR, true,
3260 false},
3261 StrideMissingCase{
3262 (unsigned)spv::CooperativeMatrixLayout::RowBlockedInterleavedARM,
3263 false, true},
3264 StrideMissingCase{
3265 (unsigned)spv::CooperativeMatrixLayout::RowBlockedInterleavedARM,
3266 true, false},
3267 StrideMissingCase{
3268 (unsigned)spv::CooperativeMatrixLayout::ColumnBlockedInterleavedARM,
3269 false, true},
3270 StrideMissingCase{
3271 (unsigned)spv::CooperativeMatrixLayout::ColumnBlockedInterleavedARM,
3272 true, false}));
3273
3274 TEST_P(ValidateCoopMatrixStrideMissing, CoopMatKHRLoadStrideMissingFail) {
3275 const StrideMissingCase& param = GetParam();
3276 std::string spirv = GenCoopMatLoadStoreShaderKHR(
3277 "MakePointerAvailableKHR|NonPrivatePointerKHR",
3278 "MakePointerVisibleKHR|NonPrivatePointerKHR", param.layout,
3279 false /*useSpecConstantLayout*/, param.useStoreStride,
3280 param.useLoadStride);
3281 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3282 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3283 EXPECT_THAT(getDiagnosticString(),
3284 HasSubstr("MemoryLayout " + std::to_string(param.layout) +
3285 " requires a Stride"));
3286 }
3287
3288 TEST_F(ValidateMemory, CoopMatKHRMemoryLayoutFromSpecConstantSuccess) {
3289 std::string spirv = GenCoopMatLoadStoreShaderKHR(
3290 "MakePointerAvailableKHR|NonPrivatePointerKHR",
3291 "MakePointerVisibleKHR|NonPrivatePointerKHR",
3292 (unsigned)spv::CooperativeMatrixLayout::RowMajorKHR,
3293 true /*useSpecConstantLayout*/);
3294
3295 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
3296 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3297 }
3298
3299 TEST_F(ValidateMemory, CoopMatKHRStoreMemoryAccessFail) {
3300 std::string spirv = GenCoopMatLoadStoreShaderKHR(
3301 "MakePointerVisibleKHR|NonPrivatePointerKHR",
3302 "MakePointerVisibleKHR|NonPrivatePointerKHR");
3303
3304 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
3305 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3306 EXPECT_THAT(getDiagnosticString(),
3307 HasSubstr("MakePointerVisibleKHR cannot be used with OpStore"));
3308 }
3309
3310 TEST_F(ValidateMemory, CoopMatKHRLoadMemoryAccessFail) {
3311 std::string spirv = GenCoopMatLoadStoreShaderKHR(
3312 "MakePointerAvailableKHR|NonPrivatePointerKHR",
3313 "MakePointerAvailableKHR|NonPrivatePointerKHR");
3314
3315 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
3316 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3317 EXPECT_THAT(getDiagnosticString(),
3318 HasSubstr("MakePointerAvailableKHR cannot be used with OpLoad"));
3319 }
3320
3321 TEST_F(ValidateMemory,
3322 CoopMatLoadArmRowBlockedInterleavedLayoutNonConstantStrideFail) {
3323 std::string spirv = GenCoopMatLoadStoreShaderKHR(
3324 "MakePointerAvailableKHR|NonPrivatePointerKHR",
3325 "MakePointerAvailableKHR|NonPrivatePointerKHR",
3326 (unsigned)spv::CooperativeMatrixLayout::RowBlockedInterleavedARM,
3327 false, /* useSpecConstantLayout */
3328 true, /* useStoreStride */
3329 true, /* useLoadStride */
3330 false /* useConstantStride */);
3331 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
3332 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3333 EXPECT_THAT(getDiagnosticString(),
3334 HasSubstr("MemoryLayout 4202 requires Stride come from a "
3335 "constant instruction"));
3336 }
3337
3338 TEST_F(ValidateMemory,
3339 CoopMatLoadArmColumnBlockedInterleavedLayoutNonConstantStrideFail) {
3340 std::string spirv = GenCoopMatLoadStoreShaderKHR(
3341 "MakePointerAvailableKHR|NonPrivatePointerKHR",
3342 "MakePointerAvailableKHR|NonPrivatePointerKHR",
3343 (unsigned)spv::CooperativeMatrixLayout::ColumnBlockedInterleavedARM,
3344 false, /* useSpecConstantLayout */
3345 true, /* useStoreStride */
3346 true, /* useLoadStride */
3347 false /* useConstantStride */);
3348 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
3349 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3350 EXPECT_THAT(getDiagnosticString(),
3351 HasSubstr("MemoryLayout 4203 requires Stride come from a "
3352 "constant instruction"));
3353 }
3354
3355 struct StrideValueCase {
3356 unsigned layout;
3357 unsigned stride;
3358 bool valid;
3359 };
3360
3361 using ValidateCoopMatrixStrideValue = spvtest::ValidateBase<StrideValueCase>;
3362
3363 INSTANTIATE_TEST_SUITE_P(
3364 CoopMatrixStrideValue, ValidateCoopMatrixStrideValue,
3365 Values(
3366 StrideValueCase{
3367 (unsigned)spv::CooperativeMatrixLayout::RowBlockedInterleavedARM, 1,
3368 true},
3369 StrideValueCase{
3370 (unsigned)spv::CooperativeMatrixLayout::RowBlockedInterleavedARM, 2,
3371 true},
3372 StrideValueCase{
3373 (unsigned)spv::CooperativeMatrixLayout::RowBlockedInterleavedARM, 4,
3374 true},
3375 StrideValueCase{
3376 (unsigned)spv::CooperativeMatrixLayout::RowBlockedInterleavedARM, 8,
3377 false},
3378 StrideValueCase{
3379 (unsigned)spv::CooperativeMatrixLayout::ColumnBlockedInterleavedARM,
3380 1, true},
3381 StrideValueCase{
3382 (unsigned)spv::CooperativeMatrixLayout::ColumnBlockedInterleavedARM,
3383 2, true},
3384 StrideValueCase{
3385 (unsigned)spv::CooperativeMatrixLayout::ColumnBlockedInterleavedARM,
3386 4, true},
3387 StrideValueCase{
3388 (unsigned)spv::CooperativeMatrixLayout::ColumnBlockedInterleavedARM,
3389 8, false}));
3390
3391 TEST_P(ValidateCoopMatrixStrideValue, CoopMatLoadStoreStrideValue) {
3392 const StrideValueCase& param = GetParam();
3393 std::string spirv = GenCoopMatLoadStoreShaderKHR(
3394 "MakePointerAvailableKHR|NonPrivatePointerKHR",
3395 "MakePointerVisibleKHR|NonPrivatePointerKHR", param.layout,
3396 false /*useSpecConstantLayout*/, true /*useStoreStride*/,
3397 true /*useLoadStride*/, true /*useConstantStride*/, param.stride);
3398 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3399 if (param.valid) {
3400 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3401 } else {
3402 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3403 EXPECT_THAT(getDiagnosticString(),
3404 HasSubstr("MemoryLayout " + std::to_string(param.layout) +
3405 " requires Stride be 1, 2, or 4"));
3406 }
3407 }
3408
3409 struct MatrixSizeCase {
3410 unsigned layout;
3411 unsigned rows;
3412 unsigned cols;
3413 unsigned stride;
3414 bool valid_rows;
3415 bool valid_cols;
3416 };
3417
3418 using ValidateCoopMatrixSize = spvtest::ValidateBase<MatrixSizeCase>;
3419
3420 INSTANTIATE_TEST_SUITE_P(
3421 CoopMatrixMatrixSize, ValidateCoopMatrixSize,
3422 Values(
3423 MatrixSizeCase{
3424 (unsigned)spv::CooperativeMatrixLayout::RowBlockedInterleavedARM, 4,
3425 4, 1, true, true},
3426 MatrixSizeCase{
3427 (unsigned)spv::CooperativeMatrixLayout::RowBlockedInterleavedARM, 4,
3428 8, 2, true, true},
3429 MatrixSizeCase{
3430 (unsigned)spv::CooperativeMatrixLayout::RowBlockedInterleavedARM, 2,
3431 4, 1, false, true},
3432 MatrixSizeCase{
3433 (unsigned)spv::CooperativeMatrixLayout::RowBlockedInterleavedARM, 2,
3434 4, 2, false, false},
3435 MatrixSizeCase{
3436 (unsigned)spv::CooperativeMatrixLayout::RowBlockedInterleavedARM, 4,
3437 2, 1, true, false},
3438 MatrixSizeCase{
3439 (unsigned)spv::CooperativeMatrixLayout::RowBlockedInterleavedARM, 4,
3440 4, 2, true, false},
3441 MatrixSizeCase{
3442 (unsigned)spv::CooperativeMatrixLayout::ColumnBlockedInterleavedARM,
3443 4, 4, 1, true, true},
3444 MatrixSizeCase{
3445 (unsigned)spv::CooperativeMatrixLayout::ColumnBlockedInterleavedARM,
3446 8, 4, 2, true, true},
3447 MatrixSizeCase{
3448 (unsigned)spv::CooperativeMatrixLayout::ColumnBlockedInterleavedARM,
3449 4, 2, 1, true, false},
3450 MatrixSizeCase{
3451 (unsigned)spv::CooperativeMatrixLayout::ColumnBlockedInterleavedARM,
3452 4, 2, 2, false, false},
3453 MatrixSizeCase{
3454 (unsigned)spv::CooperativeMatrixLayout::ColumnBlockedInterleavedARM,
3455 2, 4, 1, false, true},
3456 MatrixSizeCase{
3457 (unsigned)spv::CooperativeMatrixLayout::ColumnBlockedInterleavedARM,
3458 4, 4, 2, false, true}));
3459
3460 TEST_P(ValidateCoopMatrixSize, CoopMatLoadStoreMatrixSize) {
3461 const MatrixSizeCase& param = GetParam();
3462 std::string spirv = GenCoopMatLoadStoreShaderKHR(
3463 "MakePointerAvailableKHR|NonPrivatePointerKHR",
3464 "MakePointerVisibleKHR|NonPrivatePointerKHR", param.layout,
3465 false /*useSpecConstantLayout*/, true /*useStoreStride*/,
3466 true /*useLoadStride*/, true /*useConstantStride*/, param.stride,
3467 param.rows, param.cols);
3468 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3469
3470 uint32_t rows_required_multiple = 4;
3471 uint32_t cols_required_multiple =
3472 16 / 4; // The shader always uses FP32 matrix components
3473
3474 if (param.layout ==
3475 (uint64_t)spv::CooperativeMatrixLayout::RowBlockedInterleavedARM) {
3476 cols_required_multiple *= param.stride;
3477 }
3478 if (param.layout ==
3479 (uint64_t)spv::CooperativeMatrixLayout::ColumnBlockedInterleavedARM) {
3480 rows_required_multiple *= param.stride;
3481 }
3482
3483 if (param.valid_rows && param.valid_cols) {
3484 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3485 } else if (param.valid_rows) {
3486 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3487 EXPECT_THAT(
3488 getDiagnosticString(),
3489 HasSubstr("MemoryLayout " + std::to_string(param.layout) +
3490 " with a Stride of " + std::to_string(param.stride) +
3491 " requires that the number of columns be a multiple of " +
3492 std::to_string(cols_required_multiple)));
3493 } else {
3494 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3495 EXPECT_THAT(
3496 getDiagnosticString(),
3497 HasSubstr("MemoryLayout " + std::to_string(param.layout) +
3498 " with a Stride of " + std::to_string(param.stride) +
3499 " requires that the number of rows be a multiple of " +
3500 std::to_string(rows_required_multiple)));
3501 }
3502 }
3503
3504 TEST_F(ValidateMemory, CoopMatKHRInvalidStorageClassFail) {
3505 const std::string body = R"(
3506 OpCapability Shader
3507 OpCapability Float16
3508 OpCapability CooperativeMatrixKHR
3509 OpCapability VulkanMemoryModelKHR
3510 OpExtension "SPV_KHR_cooperative_matrix"
3511 OpExtension "SPV_KHR_vulkan_memory_model"
3512 OpMemoryModel Logical VulkanKHR
3513 OpEntryPoint GLCompute %main "main"
3514 %void = OpTypeVoid
3515 %func = OpTypeFunction %void
3516 %f16 = OpTypeFloat 16
3517 %u32 = OpTypeInt 32 0
3518
3519 %u32_8 = OpConstant %u32 8
3520 %use_A = OpConstant %u32 0
3521 %subgroup = OpConstant %u32 3
3522
3523 %f16mat = OpTypeCooperativeMatrixKHR %f16 %subgroup %u32_8 %u32_8 %use_A
3524
3525 %str = OpTypeStruct %f16mat
3526 %str_ptr = OpTypePointer Workgroup %str
3527 %sh = OpVariable %str_ptr Workgroup
3528
3529 %main = OpFunction %void None %func
3530 %main_entry = OpLabel
3531
3532 OpReturn
3533 OpFunctionEnd)";
3534
3535 CompileSuccessfully(body.c_str(), SPV_ENV_UNIVERSAL_1_3);
3536 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
3537 EXPECT_THAT(
3538 getDiagnosticString(),
3539 HasSubstr(
3540 "Cooperative matrix types (or types containing them) can only be "
3541 "allocated in Function or Private storage classes or as function "
3542 "parameters"));
3543 }
3544
3545 TEST_F(ValidateMemory, CoopMatMatrixKHRLengthResultTypeBad) {
3546 const std::string body = R"(
3547 OpCapability Shader
3548 OpCapability Float16
3549 OpCapability CooperativeMatrixKHR
3550 OpCapability VulkanMemoryModelKHR
3551 OpExtension "SPV_KHR_cooperative_matrix"
3552 OpExtension "SPV_KHR_vulkan_memory_model"
3553 OpMemoryModel Logical VulkanKHR
3554 OpEntryPoint GLCompute %main "main"
3555 %void = OpTypeVoid
3556 %func = OpTypeFunction %void
3557 %f16 = OpTypeFloat 16
3558 %u32 = OpTypeInt 32 0
3559 %i32 = OpTypeInt 32 1
3560
3561 %u32_8 = OpConstant %u32 8
3562 %use_A = OpConstant %u32 0
3563 %subgroup = OpConstant %u32 3
3564
3565 %f16mat = OpTypeCooperativeMatrixKHR %f16 %subgroup %u32_8 %u32_8 %use_A
3566
3567 %main = OpFunction %void None %func
3568 %main_entry = OpLabel
3569
3570 %1 = OpCooperativeMatrixLengthKHR %i32 %f16mat
3571
3572 OpReturn
3573 OpFunctionEnd)";
3574
3575 CompileSuccessfully(body.c_str(), SPV_ENV_UNIVERSAL_1_3);
3576 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
3577 EXPECT_THAT(
3578 getDiagnosticString(),
3579 HasSubstr("The Result Type of OpCooperativeMatrixLengthKHR <id> "
3580 "'12[%12]' must be OpTypeInt with width 32 and signedness 0"));
3581 }
3582
3583 TEST_F(ValidateMemory, CoopMatMatrixKHRLengthOperandTypeBad) {
3584 const std::string body =
3585 R"(
3586 OpCapability Shader
3587 OpCapability Float16
3588 OpCapability CooperativeMatrixKHR
3589 OpCapability VulkanMemoryModelKHR
3590 OpExtension "SPV_KHR_cooperative_matrix"
3591 OpExtension "SPV_KHR_vulkan_memory_model"
3592 OpMemoryModel Logical VulkanKHR
3593 OpEntryPoint GLCompute %main "main"
3594 %void = OpTypeVoid
3595 %func = OpTypeFunction %void
3596 %f16 = OpTypeFloat 16
3597 %u32 = OpTypeInt 32 0
3598 %i32 = OpTypeInt 32 1
3599
3600 %u32_8 = OpConstant %u32 8
3601 %use_A = OpConstant %u32 0
3602 %subgroup = OpConstant %u32 3
3603
3604 %f16mat = OpTypeCooperativeMatrixKHR %f16 %subgroup %u32_8 %u32_8 %use_A
3605
3606 %main = OpFunction %void None %func
3607 %main_entry = OpLabel
3608
3609 %1 = OpCooperativeMatrixLengthKHR %u32 %u32
3610
3611 OpReturn
3612 OpFunctionEnd)";
3613
3614 CompileSuccessfully(body.c_str(), SPV_ENV_UNIVERSAL_1_3);
3615 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
3616 EXPECT_THAT(
3617 getDiagnosticString(),
3618 HasSubstr("The type in OpCooperativeMatrixLengthKHR <id> '5[%uint]' "
3619 "must be OpTypeCooperativeMatrixKHR"));
3620 }
3621
3622 TEST_F(ValidateMemory, CoopMatMatrixKHRLengthGood) {
3623 const std::string body =
3624 R"(
3625 OpCapability Shader
3626 OpCapability Float16
3627 OpCapability CooperativeMatrixKHR
3628 OpCapability VulkanMemoryModelKHR
3629 OpExtension "SPV_KHR_cooperative_matrix"
3630 OpExtension "SPV_KHR_vulkan_memory_model"
3631 OpMemoryModel Logical VulkanKHR
3632 OpEntryPoint GLCompute %main "main"
3633 %void = OpTypeVoid
3634 %func = OpTypeFunction %void
3635 %f16 = OpTypeFloat 16
3636 %u32 = OpTypeInt 32 0
3637 %i32 = OpTypeInt 32 1
3638
3639 %u32_8 = OpConstant %u32 8
3640 %use_A = OpConstant %u32 0
3641 %subgroup = OpConstant %u32 3
3642
3643 %f16mat = OpTypeCooperativeMatrixKHR %f16 %subgroup %u32_8 %u32_8 %use_A
3644
3645 %main = OpFunction %void None %func
3646 %main_entry = OpLabel
3647
3648 %1 = OpCooperativeMatrixLengthKHR %u32 %f16mat
3649
3650 OpReturn
3651 OpFunctionEnd)";
3652
3653 CompileSuccessfully(body.c_str(), SPV_ENV_UNIVERSAL_1_3);
3654 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
3655 }
3656
3657 TEST_F(ValidateMemory, VulkanRTAOutsideOfStructBad) {
3658 std::string spirv = R"(
3659 OpCapability Shader
3660 OpMemoryModel Logical GLSL450
3661 OpEntryPoint Fragment %func "func"
3662 OpExecutionMode %func OriginUpperLeft
3663 %sampler_t = OpTypeSampler
3664 %array_t = OpTypeRuntimeArray %sampler_t
3665 %array_ptr = OpTypePointer UniformConstant %array_t
3666 %2 = OpVariable %array_ptr UniformConstant
3667 %void = OpTypeVoid
3668 %func_t = OpTypeFunction %void
3669 %func = OpFunction %void None %func_t
3670 %1 = OpLabel
3671 OpReturn
3672 OpFunctionEnd
3673 )";
3674
3675 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
3676 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3677 EXPECT_THAT(getDiagnosticString(),
3678 AnyVUID("VUID-StandaloneSpirv-OpTypeRuntimeArray-04680"));
3679 EXPECT_THAT(
3680 getDiagnosticString(),
3681 HasSubstr(
3682 "OpVariable, <id> '5[%5]', is attempting to create memory for an "
3683 "illegal type, OpTypeRuntimeArray.\nFor Vulkan OpTypeRuntimeArray "
3684 "can only appear as the final member of an OpTypeStruct, thus cannot "
3685 "be instantiated via OpVariable, unless the RuntimeDescriptorArray "
3686 "Capability is declared\n %5 = OpVariable "
3687 "%_ptr_UniformConstant__runtimearr_2 UniformConstant\n"));
3688 }
3689
3690 TEST_F(ValidateMemory, VulkanRTAOutsideOfStructWithRuntimeDescriptorArrayGood) {
3691 std::string spirv = R"(
3692 OpCapability Shader
3693 OpCapability RuntimeDescriptorArrayEXT
3694 OpExtension "SPV_EXT_descriptor_indexing"
3695 OpMemoryModel Logical GLSL450
3696 OpEntryPoint Fragment %func "func"
3697 OpExecutionMode %func OriginUpperLeft
3698 OpDecorate %struct Block
3699 OpMemberDecorate %struct 0 Offset 0
3700 OpDecorate %2 DescriptorSet 0
3701 OpDecorate %2 Binding 0
3702 OpDecorate %3 DescriptorSet 0
3703 OpDecorate %3 Binding 1
3704 %sampler_t = OpTypeSampler
3705 %uint = OpTypeInt 32 0
3706 %array_t = OpTypeRuntimeArray %sampler_t
3707 %struct = OpTypeStruct %uint
3708 %sb_array_t = OpTypeRuntimeArray %struct
3709 %array_sb_ptr = OpTypePointer StorageBuffer %sb_array_t
3710 %2 = OpVariable %array_sb_ptr StorageBuffer
3711 %array_uc_ptr = OpTypePointer UniformConstant %array_t
3712 %3 = OpVariable %array_uc_ptr UniformConstant
3713 %void = OpTypeVoid
3714 %func_t = OpTypeFunction %void
3715 %func = OpFunction %void None %func_t
3716 %1 = OpLabel
3717 OpReturn
3718 OpFunctionEnd
3719 )";
3720
3721 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
3722 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3723 }
3724
3725 TEST_F(
3726 ValidateMemory,
3727 VulkanRTAOutsideOfStructWithRuntimeDescriptorArrayAndWrongStorageClassBad) {
3728 std::string spirv = R"(
3729 OpCapability Shader
3730 OpCapability RuntimeDescriptorArrayEXT
3731 OpExtension "SPV_EXT_descriptor_indexing"
3732 OpMemoryModel Logical GLSL450
3733 OpEntryPoint Fragment %func "func"
3734 OpExecutionMode %func OriginUpperLeft
3735 %uint_t = OpTypeInt 32 0
3736 %array_t = OpTypeRuntimeArray %uint_t
3737 %array_ptr = OpTypePointer Workgroup %array_t
3738 %2 = OpVariable %array_ptr Workgroup
3739 %void = OpTypeVoid
3740 %func_t = OpTypeFunction %void
3741 %func = OpFunction %void None %func_t
3742 %1 = OpLabel
3743 OpReturn
3744 OpFunctionEnd
3745 )";
3746
3747 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
3748 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3749 EXPECT_THAT(getDiagnosticString(),
3750 AnyVUID("VUID-StandaloneSpirv-OpTypeRuntimeArray-04680"));
3751 EXPECT_THAT(
3752 getDiagnosticString(),
3753 HasSubstr("For Vulkan with RuntimeDescriptorArray, a variable "
3754 "containing OpTypeRuntimeArray must have storage class of "
3755 "StorageBuffer, Uniform, or UniformConstant.\n %5 = "
3756 "OpVariable %_ptr_Workgroup__runtimearr_uint Workgroup\n"));
3757 }
3758
3759 TEST_F(ValidateMemory, VulkanRTAInsideStorageBufferStructGood) {
3760 std::string spirv = R"(
3761 OpCapability Shader
3762 OpMemoryModel Logical GLSL450
3763 OpEntryPoint Fragment %func "func"
3764 OpExecutionMode %func OriginUpperLeft
3765 OpDecorate %array_t ArrayStride 4
3766 OpMemberDecorate %struct_t 0 Offset 0
3767 OpDecorate %struct_t Block
3768 OpDecorate %2 DescriptorSet 0
3769 OpDecorate %2 Binding 0
3770 %uint_t = OpTypeInt 32 0
3771 %array_t = OpTypeRuntimeArray %uint_t
3772 %struct_t = OpTypeStruct %array_t
3773 %struct_ptr = OpTypePointer StorageBuffer %struct_t
3774 %2 = OpVariable %struct_ptr StorageBuffer
3775 %void = OpTypeVoid
3776 %func_t = OpTypeFunction %void
3777 %func = OpFunction %void None %func_t
3778 %1 = OpLabel
3779 OpReturn
3780 OpFunctionEnd
3781 )";
3782
3783 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
3784 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3785 }
3786
3787 TEST_F(ValidateMemory, VulkanRTAInsideWrongStorageClassStructBad) {
3788 std::string spirv = R"(
3789 OpCapability Shader
3790 OpMemoryModel Logical GLSL450
3791 OpEntryPoint Fragment %func "func"
3792 OpExecutionMode %func OriginUpperLeft
3793 OpDecorate %struct_t Block
3794 %uint_t = OpTypeInt 32 0
3795 %array_t = OpTypeRuntimeArray %uint_t
3796 %struct_t = OpTypeStruct %array_t
3797 %struct_ptr = OpTypePointer Workgroup %struct_t
3798 %2 = OpVariable %struct_ptr Workgroup
3799 %void = OpTypeVoid
3800 %func_t = OpTypeFunction %void
3801 %func = OpFunction %void None %func_t
3802 %1 = OpLabel
3803 OpReturn
3804 OpFunctionEnd
3805 )";
3806
3807 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
3808 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3809 EXPECT_THAT(getDiagnosticString(),
3810 AnyVUID("VUID-StandaloneSpirv-OpTypeRuntimeArray-04680"));
3811 EXPECT_THAT(
3812 getDiagnosticString(),
3813 HasSubstr(
3814 "For Vulkan, OpTypeStruct variables containing OpTypeRuntimeArray "
3815 "must have storage class of StorageBuffer, PhysicalStorageBuffer, or "
3816 "Uniform.\n %6 = "
3817 "OpVariable %_ptr_Workgroup__struct_2 Workgroup\n"));
3818 }
3819
3820 TEST_F(ValidateMemory, VulkanRTAInsideStorageBufferStructWithoutBlockBad) {
3821 std::string spirv = R"(
3822 OpCapability Shader
3823 OpMemoryModel Logical GLSL450
3824 OpEntryPoint Fragment %func "func"
3825 OpExecutionMode %func OriginUpperLeft
3826 OpDecorate %struct_t BufferBlock
3827 %uint_t = OpTypeInt 32 0
3828 %array_t = OpTypeRuntimeArray %uint_t
3829 %struct_t = OpTypeStruct %array_t
3830 %struct_ptr = OpTypePointer StorageBuffer %struct_t
3831 %2 = OpVariable %struct_ptr StorageBuffer
3832 %void = OpTypeVoid
3833 %func_t = OpTypeFunction %void
3834 %func = OpFunction %void None %func_t
3835 %1 = OpLabel
3836 OpReturn
3837 OpFunctionEnd
3838 )";
3839
3840 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
3841 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3842 EXPECT_THAT(getDiagnosticString(),
3843 AnyVUID("VUID-StandaloneSpirv-OpTypeRuntimeArray-04680"));
3844 EXPECT_THAT(getDiagnosticString(),
3845 HasSubstr("For Vulkan, an OpTypeStruct variable containing an "
3846 "OpTypeRuntimeArray must be decorated with Block if it "
3847 "has storage class StorageBuffer or "
3848 "PhysicalStorageBuffer.\n %6 = OpVariable "
3849 "%_ptr_StorageBuffer__struct_2 StorageBuffer\n"));
3850 }
3851
3852 TEST_F(ValidateMemory, VulkanRTAInsideUniformStructGood) {
3853 std::string spirv = R"(
3854 OpCapability Shader
3855 OpMemoryModel Logical GLSL450
3856 OpEntryPoint Fragment %func "func"
3857 OpExecutionMode %func OriginUpperLeft
3858 OpDecorate %array_t ArrayStride 4
3859 OpMemberDecorate %struct_t 0 Offset 0
3860 OpDecorate %struct_t BufferBlock
3861 OpDecorate %2 DescriptorSet 0
3862 OpDecorate %2 Binding 0
3863 %uint_t = OpTypeInt 32 0
3864 %array_t = OpTypeRuntimeArray %uint_t
3865 %struct_t = OpTypeStruct %array_t
3866 %struct_ptr = OpTypePointer Uniform %struct_t
3867 %2 = OpVariable %struct_ptr Uniform
3868 %void = OpTypeVoid
3869 %func_t = OpTypeFunction %void
3870 %func = OpFunction %void None %func_t
3871 %1 = OpLabel
3872 OpReturn
3873 OpFunctionEnd
3874 )";
3875
3876 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
3877 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3878 }
3879
3880 TEST_F(ValidateMemory, VulkanRTAInsideUniformStructWithoutBufferBlock) {
3881 std::string spirv = R"(
3882 OpCapability Shader
3883 OpMemoryModel Logical GLSL450
3884 OpEntryPoint Fragment %func "func"
3885 OpExecutionMode %func OriginUpperLeft
3886 OpDecorate %array_t ArrayStride 16
3887 OpDecorate %struct_t Block
3888 OpMemberDecorate %struct_t 0 Offset 0
3889 OpDecorate %2 Binding 0
3890 OpDecorate %2 DescriptorSet 0
3891 %uint_t = OpTypeInt 32 0
3892 %array_t = OpTypeRuntimeArray %uint_t
3893 %struct_t = OpTypeStruct %array_t
3894 %struct_ptr = OpTypePointer Uniform %struct_t
3895 %2 = OpVariable %struct_ptr Uniform
3896 %void = OpTypeVoid
3897 %func_t = OpTypeFunction %void
3898 %func = OpFunction %void None %func_t
3899 %1 = OpLabel
3900 OpReturn
3901 OpFunctionEnd
3902 )";
3903
3904 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
3905 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3906 }
3907
3908 TEST_F(ValidateMemory, VulkanArrayLengthUniformRuntimeArray) {
3909 std::string spirv = R"(
3910 OpCapability Shader
3911 OpMemoryModel Logical GLSL450
3912 OpEntryPoint Fragment %func "func"
3913 OpExecutionMode %func OriginUpperLeft
3914 OpDecorate %array_t ArrayStride 16
3915 OpDecorate %struct_t Block
3916 OpMemberDecorate %struct_t 0 Offset 0
3917 OpDecorate %2 Binding 0
3918 OpDecorate %2 DescriptorSet 0
3919 %uint_t = OpTypeInt 32 0
3920 %array_t = OpTypeRuntimeArray %uint_t
3921 %struct_t = OpTypeStruct %array_t
3922 %struct_ptr = OpTypePointer Uniform %struct_t
3923 %2 = OpVariable %struct_ptr Uniform
3924 %void = OpTypeVoid
3925 %func_t = OpTypeFunction %void
3926 %func = OpFunction %void None %func_t
3927 %1 = OpLabel
3928 %length = OpArrayLength %uint_t %2 0
3929 OpReturn
3930 OpFunctionEnd
3931 )";
3932
3933 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
3934 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3935 EXPECT_THAT(getDiagnosticString(),
3936 AnyVUID("VUID-StandaloneSpirv-OpArrayLength-11805"));
3937 EXPECT_THAT(getDiagnosticString(),
3938 HasSubstr("OpArrayLength must not be used on the "
3939 "OpTypeRuntimeArray inside a Uniform block"));
3940 }
3941
3942 TEST_F(ValidateMemory, VulkanRTAInsideRTABad) {
3943 std::string spirv = R"(
3944 OpCapability Shader
3945 OpMemoryModel Logical GLSL450
3946 OpEntryPoint Fragment %func "func"
3947 OpExecutionMode %func OriginUpperLeft
3948 %sampler_t = OpTypeSampler
3949 %inner_array_t = OpTypeRuntimeArray %sampler_t
3950 %array_t = OpTypeRuntimeArray %inner_array_t
3951 %array_ptr = OpTypePointer UniformConstant %array_t
3952 %2 = OpVariable %array_ptr UniformConstant
3953 %void = OpTypeVoid
3954 %func_t = OpTypeFunction %void
3955 %func = OpFunction %void None %func_t
3956 %1 = OpLabel
3957 OpReturn
3958 OpFunctionEnd
3959 )";
3960
3961 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
3962 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3963 EXPECT_THAT(getDiagnosticString(),
3964 AnyVUID("VUID-StandaloneSpirv-OpTypeRuntimeArray-04680"));
3965 EXPECT_THAT(
3966 getDiagnosticString(),
3967 HasSubstr(
3968 "OpTypeRuntimeArray Element Type <id> '3[%_runtimearr_2]' is not "
3969 "valid in Vulkan environments.\n %_runtimearr__runtimearr_2 = "
3970 "OpTypeRuntimeArray %_runtimearr_2\n"));
3971 }
3972
3973 TEST_F(ValidateMemory, VulkanRTAInsideRTAWithRuntimeDescriptorArrayBad) {
3974 std::string spirv = R"(
3975 OpCapability RuntimeDescriptorArrayEXT
3976 OpCapability Shader
3977 OpExtension "SPV_EXT_descriptor_indexing"
3978 OpMemoryModel Logical GLSL450
3979 OpEntryPoint Fragment %func "func"
3980 OpExecutionMode %func OriginUpperLeft
3981 OpDecorate %struct Block
3982 %uint_t = OpTypeInt 32 0
3983 %inner_array_t = OpTypeRuntimeArray %uint_t
3984 %array_t = OpTypeRuntimeArray %inner_array_t
3985 %struct = OpTypeStruct %array_t
3986 %array_ptr = OpTypePointer StorageBuffer %struct
3987 %2 = OpVariable %array_ptr StorageBuffer
3988 %void = OpTypeVoid
3989 %func_t = OpTypeFunction %void
3990 %func = OpFunction %void None %func_t
3991 %1 = OpLabel
3992 OpReturn
3993 OpFunctionEnd
3994 )";
3995
3996 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
3997 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3998 EXPECT_THAT(getDiagnosticString(),
3999 AnyVUID("VUID-StandaloneSpirv-OpTypeRuntimeArray-04680"));
4000 EXPECT_THAT(
4001 getDiagnosticString(),
4002 HasSubstr(
4003 "OpTypeRuntimeArray Element Type <id> '4[%_runtimearr_uint]' is not "
4004 "valid in Vulkan environments.\n %_runtimearr__runtimearr_uint = "
4005 "OpTypeRuntimeArray %_runtimearr_uint\n"));
4006 }
4007
4008 TEST_F(ValidateMemory,
4009 VulkanUniformStructInsideRTAWithRuntimeDescriptorArrayGood) {
4010 std::string spirv = R"(
4011 OpCapability RuntimeDescriptorArrayEXT
4012 OpCapability Shader
4013 OpExtension "SPV_EXT_descriptor_indexing"
4014 OpMemoryModel Logical GLSL450
4015 OpEntryPoint Fragment %func "func"
4016 OpExecutionMode %func OriginUpperLeft
4017 OpMemberDecorate %struct_t 0 Offset 0
4018 OpDecorate %struct_t Block
4019 OpDecorate %2 DescriptorSet 0
4020 OpDecorate %2 Binding 0
4021 %uint_t = OpTypeInt 32 0
4022 %struct_t = OpTypeStruct %uint_t
4023 %array_t = OpTypeRuntimeArray %struct_t
4024 %array_ptr = OpTypePointer Uniform %array_t
4025 %2 = OpVariable %array_ptr Uniform
4026 %void = OpTypeVoid
4027 %func_t = OpTypeFunction %void
4028 %func = OpFunction %void None %func_t
4029 %1 = OpLabel
4030 OpReturn
4031 OpFunctionEnd
4032 )";
4033
4034 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
4035 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
4036 }
4037
4038 TEST_F(ValidateMemory, VulkanRTAInsideRTAInsideStructBad) {
4039 std::string spirv = R"(
4040 OpCapability Shader
4041 OpMemoryModel Logical GLSL450
4042 OpEntryPoint Fragment %func "func"
4043 OpExecutionMode %func OriginUpperLeft
4044 OpDecorate %array_t ArrayStride 4
4045 OpMemberDecorate %struct_t 0 Offset 0
4046 OpDecorate %struct_t Block
4047 %uint_t = OpTypeInt 32 0
4048 %inner_array_t = OpTypeRuntimeArray %uint_t
4049 %array_t = OpTypeRuntimeArray %inner_array_t
4050 %struct_t = OpTypeStruct %array_t
4051 %struct_ptr = OpTypePointer StorageBuffer %struct_t
4052 %2 = OpVariable %struct_ptr StorageBuffer
4053 %void = OpTypeVoid
4054 %func_t = OpTypeFunction %void
4055 %func = OpFunction %void None %func_t
4056 %1 = OpLabel
4057 OpReturn
4058 OpFunctionEnd
4059 )";
4060
4061 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
4062 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
4063 EXPECT_THAT(getDiagnosticString(),
4064 AnyVUID("VUID-StandaloneSpirv-OpTypeRuntimeArray-04680"));
4065 EXPECT_THAT(
4066 getDiagnosticString(),
4067 HasSubstr(
4068 "OpTypeRuntimeArray Element Type <id> '5[%_runtimearr_uint]' is not "
4069 "valid in Vulkan environments.\n %_runtimearr__runtimearr_uint = "
4070 "OpTypeRuntimeArray %_runtimearr_uint\n"));
4071 }
4072
4073 TEST_F(ValidateMemory,
4074 VulkanRTAInsideRTAInsideStructWithRuntimeDescriptorArrayBad) {
4075 std::string spirv = R"(
4076 OpCapability RuntimeDescriptorArrayEXT
4077 OpCapability Shader
4078 OpExtension "SPV_EXT_descriptor_indexing"
4079 OpMemoryModel Logical GLSL450
4080 OpEntryPoint Fragment %func "func"
4081 OpExecutionMode %func OriginUpperLeft
4082 OpDecorate %array_t ArrayStride 4
4083 OpMemberDecorate %struct_t 0 Offset 0
4084 OpDecorate %struct_t Block
4085 %uint_t = OpTypeInt 32 0
4086 %inner_array_t = OpTypeRuntimeArray %uint_t
4087 %array_t = OpTypeRuntimeArray %inner_array_t
4088 %struct_t = OpTypeStruct %array_t
4089 %struct_ptr = OpTypePointer StorageBuffer %struct_t
4090 %2 = OpVariable %struct_ptr StorageBuffer
4091 %void = OpTypeVoid
4092 %func_t = OpTypeFunction %void
4093 %func = OpFunction %void None %func_t
4094 %1 = OpLabel
4095 OpReturn
4096 OpFunctionEnd
4097 )";
4098
4099 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
4100 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
4101 EXPECT_THAT(getDiagnosticString(),
4102 AnyVUID("VUID-StandaloneSpirv-OpTypeRuntimeArray-04680"));
4103 EXPECT_THAT(
4104 getDiagnosticString(),
4105 HasSubstr(
4106 "OpTypeRuntimeArray Element Type <id> '5[%_runtimearr_uint]' is not "
4107 "valid in Vulkan environments.\n %_runtimearr__runtimearr_uint = "
4108 "OpTypeRuntimeArray %_runtimearr_uint\n"));
4109 }
4110
4111 TEST_F(ValidateMemory, VulkanRTAInsideArrayBad) {
4112 std::string spirv = R"(
4113 OpCapability Shader
4114 OpMemoryModel Logical GLSL450
4115 OpEntryPoint Fragment %func "func"
4116 OpExecutionMode %func OriginUpperLeft
4117 %uint_t = OpTypeInt 32 0
4118 %dim = OpConstant %uint_t 1
4119 %sampler_t = OpTypeSampler
4120 %inner_array_t = OpTypeRuntimeArray %sampler_t
4121 %array_t = OpTypeArray %inner_array_t %dim
4122 %array_ptr = OpTypePointer UniformConstant %array_t
4123 %2 = OpVariable %array_ptr UniformConstant
4124 %void = OpTypeVoid
4125 %func_t = OpTypeFunction %void
4126 %func = OpFunction %void None %func_t
4127 %1 = OpLabel
4128 OpReturn
4129 OpFunctionEnd
4130 )";
4131
4132 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
4133 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
4134 EXPECT_THAT(getDiagnosticString(),
4135 AnyVUID("VUID-StandaloneSpirv-OpTypeRuntimeArray-04680"));
4136 EXPECT_THAT(
4137 getDiagnosticString(),
4138 HasSubstr("OpTypeArray Element Type <id> '5[%_runtimearr_4]' is not "
4139 "valid in Vulkan environments.\n %_arr__runtimearr_4_uint_1 = "
4140 "OpTypeArray %_runtimearr_4 %uint_1\n"));
4141 }
4142
4143 TEST_F(ValidateMemory, VulkanRTAInsideArrayWithRuntimeDescriptorArrayBad) {
4144 std::string spirv = R"(
4145 OpCapability RuntimeDescriptorArrayEXT
4146 OpCapability Shader
4147 OpExtension "SPV_EXT_descriptor_indexing"
4148 OpMemoryModel Logical GLSL450
4149 OpEntryPoint Fragment %func "func"
4150 OpExecutionMode %func OriginUpperLeft
4151 OpDecorate %struct Block
4152 %uint_t = OpTypeInt 32 0
4153 %dim = OpConstant %uint_t 1
4154 %sampler_t = OpTypeSampler
4155 %inner_array_t = OpTypeRuntimeArray %uint_t
4156 %array_t = OpTypeRuntimeArray %inner_array_t
4157 %struct = OpTypeStruct %array_t
4158 %array_ptr = OpTypePointer StorageBuffer %struct
4159 %2 = OpVariable %array_ptr StorageBuffer
4160 %void = OpTypeVoid
4161 %func_t = OpTypeFunction %void
4162 %func = OpFunction %void None %func_t
4163 %1 = OpLabel
4164 OpReturn
4165 OpFunctionEnd
4166 )";
4167
4168 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
4169 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
4170 EXPECT_THAT(getDiagnosticString(),
4171 AnyVUID("VUID-StandaloneSpirv-OpTypeRuntimeArray-04680"));
4172 EXPECT_THAT(
4173 getDiagnosticString(),
4174 HasSubstr(
4175 "OpTypeRuntimeArray Element Type <id> '6[%_runtimearr_uint]' is not "
4176 "valid in Vulkan environments.\n %_runtimearr__runtimearr_uint = "
4177 "OpTypeRuntimeArray %_runtimearr_uint\n"));
4178 }
4179
4180 TEST_F(ValidateMemory, VulkanRTAInsideArrayInsideStructBad) {
4181 std::string spirv = R"(
4182 OpCapability Shader
4183 OpMemoryModel Logical GLSL450
4184 OpEntryPoint Fragment %func "func"
4185 OpExecutionMode %func OriginUpperLeft
4186 OpDecorate %array_t ArrayStride 4
4187 OpMemberDecorate %struct_t 0 Offset 0
4188 OpDecorate %struct_t Block
4189 %uint_t = OpTypeInt 32 0
4190 %dim = OpConstant %uint_t 1
4191 %inner_array_t = OpTypeRuntimeArray %uint_t
4192 %array_t = OpTypeArray %inner_array_t %dim
4193 %struct_t = OpTypeStruct %array_t
4194 %struct_ptr = OpTypePointer StorageBuffer %struct_t
4195 %2 = OpVariable %struct_ptr StorageBuffer
4196 %void = OpTypeVoid
4197 %func_t = OpTypeFunction %void
4198 %func = OpFunction %void None %func_t
4199 %1 = OpLabel
4200 OpReturn
4201 OpFunctionEnd
4202 )";
4203
4204 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
4205 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
4206 EXPECT_THAT(getDiagnosticString(),
4207 AnyVUID("VUID-StandaloneSpirv-OpTypeRuntimeArray-04680"));
4208 EXPECT_THAT(
4209 getDiagnosticString(),
4210 HasSubstr(
4211 "OpTypeArray Element Type <id> '6[%_runtimearr_uint]' is not "
4212 "valid in Vulkan environments.\n %_arr__runtimearr_uint_uint_1 "
4213 "= OpTypeArray %_runtimearr_uint %uint_1\n"));
4214 }
4215
4216 TEST_F(ValidateMemory,
4217 VulkanRTAInsideArrayInsideStructWithRuntimeDescriptorArrayBad) {
4218 std::string spirv = R"(
4219 OpCapability RuntimeDescriptorArrayEXT
4220 OpCapability Shader
4221 OpExtension "SPV_EXT_descriptor_indexing"
4222 OpMemoryModel Logical GLSL450
4223 OpEntryPoint Fragment %func "func"
4224 OpExecutionMode %func OriginUpperLeft
4225 OpDecorate %array_t ArrayStride 4
4226 OpMemberDecorate %struct_t 0 Offset 0
4227 OpDecorate %struct_t Block
4228 %uint_t = OpTypeInt 32 0
4229 %dim = OpConstant %uint_t 1
4230 %inner_array_t = OpTypeRuntimeArray %uint_t
4231 %array_t = OpTypeArray %inner_array_t %dim
4232 %struct_t = OpTypeStruct %array_t
4233 %struct_ptr = OpTypePointer StorageBuffer %struct_t
4234 %2 = OpVariable %struct_ptr StorageBuffer
4235 %void = OpTypeVoid
4236 %func_t = OpTypeFunction %void
4237 %func = OpFunction %void None %func_t
4238 %1 = OpLabel
4239 OpReturn
4240 OpFunctionEnd
4241 )";
4242
4243 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
4244 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
4245 EXPECT_THAT(getDiagnosticString(),
4246 AnyVUID("VUID-StandaloneSpirv-OpTypeRuntimeArray-04680"));
4247 EXPECT_THAT(
4248 getDiagnosticString(),
4249 HasSubstr(
4250 "OpTypeArray Element Type <id> '6[%_runtimearr_uint]' is not "
4251 "valid in Vulkan environments.\n %_arr__runtimearr_uint_uint_1 "
4252 "= OpTypeArray %_runtimearr_uint %uint_1\n"));
4253 }
4254
4255 TEST_F(ValidateMemory, VulkanRTAStructInsideRTAWithRuntimeDescriptorArrayGood) {
4256 std::string spirv = R"(
4257 OpCapability RuntimeDescriptorArrayEXT
4258 OpCapability Shader
4259 OpExtension "SPV_EXT_descriptor_indexing"
4260 OpMemoryModel Logical GLSL450
4261 OpEntryPoint Fragment %func "func"
4262 OpExecutionMode %func OriginUpperLeft
4263 OpDecorate %inner_array_t ArrayStride 4
4264 OpMemberDecorate %struct_t 0 Offset 0
4265 OpDecorate %struct_t Block
4266 OpDecorate %2 DescriptorSet 0
4267 OpDecorate %2 Binding 0
4268 %uint_t = OpTypeInt 32 0
4269 %inner_array_t = OpTypeRuntimeArray %uint_t
4270 %struct_t = OpTypeStruct %inner_array_t
4271 %array_t = OpTypeRuntimeArray %struct_t
4272 %array_ptr = OpTypePointer StorageBuffer %array_t
4273 %2 = OpVariable %array_ptr StorageBuffer
4274 %void = OpTypeVoid
4275 %func_t = OpTypeFunction %void
4276 %func = OpFunction %void None %func_t
4277 %1 = OpLabel
4278 OpReturn
4279 OpFunctionEnd
4280 )";
4281
4282 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
4283 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
4284 }
4285
4286 TEST_F(ValidateMemory, VulkanRTAStructInsideArrayGood) {
4287 std::string spirv = R"(
4288 OpCapability RuntimeDescriptorArrayEXT
4289 OpCapability Shader
4290 OpExtension "SPV_EXT_descriptor_indexing"
4291 OpMemoryModel Logical GLSL450
4292 OpEntryPoint Fragment %func "func"
4293 OpExecutionMode %func OriginUpperLeft
4294 OpDecorate %inner_array_t ArrayStride 4
4295 OpMemberDecorate %struct_t 0 Offset 0
4296 OpDecorate %struct_t Block
4297 OpDecorate %2 DescriptorSet 0
4298 OpDecorate %2 Binding 0
4299 %uint_t = OpTypeInt 32 0
4300 %inner_array_t = OpTypeRuntimeArray %uint_t
4301 %struct_t = OpTypeStruct %inner_array_t
4302 %array_size = OpConstant %uint_t 5
4303 %array_t = OpTypeArray %struct_t %array_size
4304 %array_ptr = OpTypePointer StorageBuffer %array_t
4305 %2 = OpVariable %array_ptr StorageBuffer
4306 %void = OpTypeVoid
4307 %func_t = OpTypeFunction %void
4308 %func = OpFunction %void None %func_t
4309 %1 = OpLabel
4310 OpReturn
4311 OpFunctionEnd
4312 )";
4313
4314 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
4315 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
4316 }
4317
4318 TEST_F(ValidateMemory, CopyMemoryNoAccessGood) {
4319 const std::string spirv = R"(
4320 OpCapability Shader
4321 OpCapability Linkage
4322 OpMemoryModel Logical GLSL450
4323 %void = OpTypeVoid
4324 %int = OpTypeInt 32 0
4325 %int_ptr_priv = OpTypePointer Private %int
4326 %var1 = OpVariable %int_ptr_priv Private
4327 %var2 = OpVariable %int_ptr_priv Private
4328 %voidfn = OpTypeFunction %void
4329 %func = OpFunction %void None %voidfn
4330 %entry = OpLabel
4331 OpCopyMemory %var1 %var2
4332 OpReturn
4333 OpFunctionEnd
4334 )";
4335
4336 CompileSuccessfully(spirv);
4337 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4338 EXPECT_THAT(getDiagnosticString(), Eq(""));
4339 }
4340
4341 TEST_F(ValidateMemory, CopyMemorySimpleMixedAccessGood) {
4342 // Test one memory access operand using features that don't require the
4343 // Vulkan memory model.
4344 const std::string spirv = R"(
4345 OpCapability Shader
4346 OpCapability Linkage
4347 OpMemoryModel Logical GLSL450
4348 %void = OpTypeVoid
4349 %int = OpTypeInt 32 0
4350 %int_ptr_priv = OpTypePointer Private %int
4351 %var1 = OpVariable %int_ptr_priv Private
4352 %var2 = OpVariable %int_ptr_priv Private
4353 %voidfn = OpTypeFunction %void
4354 %func = OpFunction %void None %voidfn
4355 %entry = OpLabel
4356 OpCopyMemory %var1 %var2 Volatile|Aligned|Nontemporal 4
4357 OpReturn
4358 OpFunctionEnd
4359 )";
4360
4361 CompileSuccessfully(spirv);
4362 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4363 EXPECT_THAT(getDiagnosticString(), Eq(""));
4364 }
4365
4366 TEST_F(ValidateMemory, CopyMemorySimpleTwoMixedAccessV13Bad) {
4367 // Two memory access operands is invalid up to SPIR-V 1.3
4368 const std::string spirv = R"(
4369 OpCapability Shader
4370 OpCapability Linkage
4371 OpMemoryModel Logical GLSL450
4372 %void = OpTypeVoid
4373 %int = OpTypeInt 32 0
4374 %int_ptr_priv = OpTypePointer Private %int
4375 %var1 = OpVariable %int_ptr_priv Private
4376 %var2 = OpVariable %int_ptr_priv Private
4377 %voidfn = OpTypeFunction %void
4378 %func = OpFunction %void None %voidfn
4379 %entry = OpLabel
4380 OpCopyMemory %var1 %var2 Volatile Volatile
4381 OpReturn
4382 OpFunctionEnd
4383 )";
4384
4385 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
4386 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
4387 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
4388 EXPECT_THAT(getDiagnosticString(),
4389 HasSubstr("CopyMemory with two memory access operands requires "
4390 "SPIR-V 1.4 or later"));
4391 }
4392
4393 TEST_F(ValidateMemory, CopyMemorySimpleTwoMixedAccessV14Good) {
4394 // Two memory access operands is valid in SPIR-V 1.4
4395 const std::string spirv = R"(
4396 OpCapability Shader
4397 OpCapability Linkage
4398 OpMemoryModel Logical GLSL450
4399 %void = OpTypeVoid
4400 %int = OpTypeInt 32 0
4401 %int_ptr_priv = OpTypePointer Private %int
4402 %var1 = OpVariable %int_ptr_priv Private
4403 %var2 = OpVariable %int_ptr_priv Private
4404 %voidfn = OpTypeFunction %void
4405 %func = OpFunction %void None %voidfn
4406 %entry = OpLabel
4407 OpCopyMemory %var1 %var2 Volatile Volatile
4408 OpReturn
4409 OpFunctionEnd
4410 )";
4411
4412 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4413 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4414 EXPECT_THAT(getDiagnosticString(), Eq(""));
4415 }
4416
4417 TEST_F(ValidateMemory, CopyMemorySizedNoAccessGood) {
4418 const std::string spirv = R"(
4419 OpCapability Shader
4420 OpCapability Linkage
4421 OpCapability Addresses
4422 OpMemoryModel Logical GLSL450
4423 %void = OpTypeVoid
4424 %int = OpTypeInt 32 0
4425 %int_16 = OpConstant %int 16
4426 %int_ptr_priv = OpTypePointer Private %int
4427 %var1 = OpVariable %int_ptr_priv Private
4428 %var2 = OpVariable %int_ptr_priv Private
4429 %voidfn = OpTypeFunction %void
4430 %func = OpFunction %void None %voidfn
4431 %entry = OpLabel
4432 OpCopyMemorySized %var1 %var2 %int_16
4433 OpReturn
4434 OpFunctionEnd
4435 )";
4436
4437 CompileSuccessfully(spirv);
4438 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4439 EXPECT_THAT(getDiagnosticString(), Eq(""));
4440 }
4441
4442 TEST_F(ValidateMemory, CopyMemorySizedSimpleMixedAccessGood) {
4443 // Test one memory access operand using features that don't require the
4444 // Vulkan memory model.
4445 const std::string spirv = R"(
4446 OpCapability Shader
4447 OpCapability Linkage
4448 OpCapability Addresses
4449 OpMemoryModel Logical GLSL450
4450 %void = OpTypeVoid
4451 %int = OpTypeInt 32 0
4452 %int_16 = OpConstant %int 16
4453 %int_ptr_priv = OpTypePointer Private %int
4454 %var1 = OpVariable %int_ptr_priv Private
4455 %var2 = OpVariable %int_ptr_priv Private
4456 %voidfn = OpTypeFunction %void
4457 %func = OpFunction %void None %voidfn
4458 %entry = OpLabel
4459 OpCopyMemorySized %var1 %var2 %int_16 Volatile|Aligned|Nontemporal 4
4460 OpReturn
4461 OpFunctionEnd
4462 )";
4463
4464 CompileSuccessfully(spirv);
4465 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
4466 }
4467
4468 TEST_F(ValidateMemory, CopyMemorySizedSimpleTwoMixedAccessV13Bad) {
4469 // Two memory access operands is invalid up to SPIR-V 1.3
4470 const std::string spirv = R"(
4471 OpCapability Shader
4472 OpCapability Linkage
4473 OpCapability Addresses
4474 OpMemoryModel Logical GLSL450
4475 %void = OpTypeVoid
4476 %int = OpTypeInt 32 0
4477 %int_16 = OpConstant %int 16
4478 %int_ptr_priv = OpTypePointer Private %int
4479 %var1 = OpVariable %int_ptr_priv Private
4480 %var2 = OpVariable %int_ptr_priv Private
4481 %voidfn = OpTypeFunction %void
4482 %func = OpFunction %void None %voidfn
4483 %entry = OpLabel
4484 OpCopyMemorySized %var1 %var2 %int_16 Volatile Volatile
4485 OpReturn
4486 OpFunctionEnd
4487 )";
4488
4489 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
4490 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
4491 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
4492 EXPECT_THAT(
4493 getDiagnosticString(),
4494 HasSubstr("CopyMemorySized with two memory access operands requires "
4495 "SPIR-V 1.4 or later"));
4496 }
4497
4498 TEST_F(ValidateMemory, CopyMemorySizedSimpleTwoMixedAccessV14Good) {
4499 // Two memory access operands is valid in SPIR-V 1.4
4500 const std::string spirv = R"(
4501 OpCapability Shader
4502 OpCapability Linkage
4503 OpCapability Addresses
4504 OpMemoryModel Logical GLSL450
4505 %void = OpTypeVoid
4506 %int = OpTypeInt 32 0
4507 %int_16 = OpConstant %int 16
4508 %int_ptr_priv = OpTypePointer Private %int
4509 %var1 = OpVariable %int_ptr_priv Private
4510 %var2 = OpVariable %int_ptr_priv Private
4511 %voidfn = OpTypeFunction %void
4512 %func = OpFunction %void None %voidfn
4513 %entry = OpLabel
4514 OpCopyMemorySized %var1 %var2 %int_16 Volatile Volatile
4515 OpReturn
4516 OpFunctionEnd
4517 )";
4518
4519 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4520 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4521 EXPECT_THAT(getDiagnosticString(), Eq(""));
4522 }
4523
4524 using ValidatePointerComparisons = spvtest::ValidateBase<std::string>;
4525
4526 TEST_P(ValidatePointerComparisons, Good) {
4527 const std::string operation = GetParam();
4528
4529 std::string spirv = R"(
4530 OpCapability Shader
4531 OpCapability Linkage
4532 OpCapability VariablePointersStorageBuffer
4533 OpMemoryModel Logical GLSL450
4534 %void = OpTypeVoid
4535 %bool = OpTypeBool
4536 %int = OpTypeInt 32 0
4537 %ptr_int = OpTypePointer StorageBuffer %int
4538 %var = OpVariable %ptr_int StorageBuffer
4539 %func_ty = OpTypeFunction %void
4540 %func = OpFunction %void None %func_ty
4541 %1 = OpLabel
4542 %equal = )" + operation;
4543
4544 if (operation == "OpPtrDiff") {
4545 spirv += " %int ";
4546 } else {
4547 spirv += " %bool ";
4548 }
4549
4550 spirv += R"(%var %var
4551 OpReturn
4552 OpFunctionEnd
4553 )";
4554
4555 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4556 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4557 }
4558
4559 TEST_P(ValidatePointerComparisons, GoodWorkgroup) {
4560 const std::string operation = GetParam();
4561
4562 std::string spirv = R"(
4563 OpCapability Shader
4564 OpCapability Linkage
4565 OpCapability VariablePointers
4566 OpMemoryModel Logical GLSL450
4567 %void = OpTypeVoid
4568 %bool = OpTypeBool
4569 %int = OpTypeInt 32 0
4570 %ptr_int = OpTypePointer Workgroup %int
4571 %var = OpVariable %ptr_int Workgroup
4572 %func_ty = OpTypeFunction %void
4573 %func = OpFunction %void None %func_ty
4574 %1 = OpLabel
4575 %equal = )" + operation;
4576
4577 if (operation == "OpPtrDiff") {
4578 spirv += " %int ";
4579 } else {
4580 spirv += " %bool ";
4581 }
4582
4583 spirv += R"(%var %var
4584 OpReturn
4585 OpFunctionEnd
4586 )";
4587
4588 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4589 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4590 }
4591
4592 TEST_P(ValidatePointerComparisons, BadResultType) {
4593 const std::string operation = GetParam();
4594
4595 std::string spirv = R"(
4596 OpCapability Shader
4597 OpCapability Linkage
4598 OpCapability VariablePointersStorageBuffer
4599 OpMemoryModel Logical GLSL450
4600 %void = OpTypeVoid
4601 %bool = OpTypeBool
4602 %int = OpTypeInt 32 0
4603 %ptr_int = OpTypePointer StorageBuffer %int
4604 %var = OpVariable %ptr_int StorageBuffer
4605 %func_ty = OpTypeFunction %void
4606 %func = OpFunction %void None %func_ty
4607 %1 = OpLabel
4608 %equal = )" + operation;
4609
4610 if (operation == "OpPtrDiff") {
4611 spirv += " %bool ";
4612 } else {
4613 spirv += " %int ";
4614 }
4615
4616 spirv += R"(%var %var
4617 OpReturn
4618 OpFunctionEnd
4619 )";
4620
4621 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4622 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4623 if (operation == "OpPtrDiff") {
4624 EXPECT_THAT(getDiagnosticString(),
4625 HasSubstr("Result Type must be an integer scalar"));
4626 } else {
4627 EXPECT_THAT(getDiagnosticString(),
4628 HasSubstr("Result Type must be OpTypeBool"));
4629 }
4630 }
4631
4632 TEST_P(ValidatePointerComparisons, BadCapabilities) {
4633 const std::string operation = GetParam();
4634
4635 std::string spirv = R"(
4636 OpCapability Shader
4637 OpCapability Linkage
4638 OpMemoryModel Logical GLSL450
4639 %void = OpTypeVoid
4640 %bool = OpTypeBool
4641 %int = OpTypeInt 32 0
4642 %ptr_int = OpTypePointer StorageBuffer %int
4643 %var = OpVariable %ptr_int StorageBuffer
4644 %func_ty = OpTypeFunction %void
4645 %func = OpFunction %void None %func_ty
4646 %1 = OpLabel
4647 %equal = )" + operation;
4648
4649 if (operation == "OpPtrDiff") {
4650 spirv += " %int ";
4651 } else {
4652 spirv += " %bool ";
4653 }
4654
4655 spirv += R"(%var %var
4656 OpReturn
4657 OpFunctionEnd
4658 )";
4659
4660 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4661 if (operation == "OpPtrDiff") {
4662 // Gets caught by the grammar.
4663 EXPECT_EQ(SPV_ERROR_INVALID_CAPABILITY,
4664 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4665 } else {
4666 EXPECT_EQ(SPV_ERROR_INVALID_ID,
4667 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4668 EXPECT_THAT(getDiagnosticString(),
4669 HasSubstr("Instruction on logical pointers cannot be "
4670 "used without a variable pointers capability"));
4671 }
4672 }
4673
4674 TEST_P(ValidatePointerComparisons, BadOperandType) {
4675 const std::string operation = GetParam();
4676
4677 std::string spirv = R"(
4678 OpCapability Shader
4679 OpCapability Linkage
4680 OpCapability VariablePointersStorageBuffer
4681 OpMemoryModel Logical GLSL450
4682 %void = OpTypeVoid
4683 %bool = OpTypeBool
4684 %int = OpTypeInt 32 0
4685 %ptr_int = OpTypePointer StorageBuffer %int
4686 %var = OpVariable %ptr_int StorageBuffer
4687 %func_ty = OpTypeFunction %void
4688 %func = OpFunction %void None %func_ty
4689 %1 = OpLabel
4690 %ld = OpLoad %int %var
4691 %equal = )" + operation;
4692
4693 if (operation == "OpPtrDiff") {
4694 spirv += " %int ";
4695 } else {
4696 spirv += " %bool ";
4697 }
4698
4699 spirv += R"(%ld %ld
4700 OpReturn
4701 OpFunctionEnd
4702 )";
4703
4704 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4705 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4706 EXPECT_THAT(getDiagnosticString(),
4707 HasSubstr("Operand type must be a pointer"));
4708 }
4709
4710 TEST_P(ValidatePointerComparisons, BadStorageClassWorkgroup) {
4711 const std::string operation = GetParam();
4712
4713 std::string spirv = R"(
4714 OpCapability Shader
4715 OpCapability Linkage
4716 OpCapability VariablePointersStorageBuffer
4717 OpMemoryModel Logical GLSL450
4718 %void = OpTypeVoid
4719 %bool = OpTypeBool
4720 %int = OpTypeInt 32 0
4721 %ptr_int = OpTypePointer Workgroup %int
4722 %var = OpVariable %ptr_int Workgroup
4723 %func_ty = OpTypeFunction %void
4724 %func = OpFunction %void None %func_ty
4725 %1 = OpLabel
4726 %equal = )" + operation;
4727
4728 if (operation == "OpPtrDiff") {
4729 spirv += " %int ";
4730 } else {
4731 spirv += " %bool ";
4732 }
4733
4734 spirv += R"(%var %var
4735 OpReturn
4736 OpFunctionEnd
4737 )";
4738
4739 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4740 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4741 EXPECT_THAT(getDiagnosticString(),
4742 HasSubstr("Workgroup storage class pointer requires "
4743 "VariablePointers capability to be specified"));
4744 }
4745
4746 TEST_P(ValidatePointerComparisons, BadStorageClass) {
4747 const std::string operation = GetParam();
4748
4749 std::string spirv = R"(
4750 OpCapability Shader
4751 OpCapability Linkage
4752 OpCapability VariablePointersStorageBuffer
4753 OpMemoryModel Logical GLSL450
4754 %void = OpTypeVoid
4755 %bool = OpTypeBool
4756 %int = OpTypeInt 32 0
4757 %ptr_int = OpTypePointer Private %int
4758 %var = OpVariable %ptr_int Private
4759 %func_ty = OpTypeFunction %void
4760 %func = OpFunction %void None %func_ty
4761 %1 = OpLabel
4762 %equal = )" + operation;
4763
4764 if (operation == "OpPtrDiff") {
4765 spirv += " %int ";
4766 } else {
4767 spirv += " %bool ";
4768 }
4769
4770 spirv += R"(%var %var
4771 OpReturn
4772 OpFunctionEnd
4773 )";
4774
4775 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4776 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4777 EXPECT_THAT(getDiagnosticString(),
4778 HasSubstr("Invalid pointer storage class"));
4779 }
4780
4781 TEST_P(ValidatePointerComparisons, BadDiffOperandTypes) {
4782 const std::string operation = GetParam();
4783
4784 std::string spirv = R"(
4785 OpCapability Shader
4786 OpCapability Linkage
4787 OpCapability VariablePointersStorageBuffer
4788 OpMemoryModel Logical GLSL450
4789 %void = OpTypeVoid
4790 %bool = OpTypeBool
4791 %int = OpTypeInt 32 0
4792 %float = OpTypeFloat 32
4793 %ptr_int = OpTypePointer Private %int
4794 %var = OpVariable %ptr_int Private
4795 %ptr_float = OpTypePointer Private %float
4796 %var2 = OpVariable %ptr_float Private
4797 %func_ty = OpTypeFunction %void
4798 %func = OpFunction %void None %func_ty
4799 %1 = OpLabel
4800 %ld = OpLoad %int %var
4801 %equal = )" + operation;
4802
4803 if (operation == "OpPtrDiff") {
4804 spirv += " %int ";
4805 } else {
4806 spirv += " %bool ";
4807 }
4808
4809 spirv += R"(%var %var2
4810 OpReturn
4811 OpFunctionEnd
4812 )";
4813
4814 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4815 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4816 EXPECT_THAT(getDiagnosticString(),
4817 HasSubstr("The types of Operand 1 and Operand 2 must match"));
4818 }
4819
4820 TEST_P(ValidatePointerComparisons, GoodUntypedPointerSameType) {
4821 const std::string operation = GetParam();
4822
4823 std::string spirv = R"(
4824 OpCapability Shader
4825 OpCapability Linkage
4826 OpCapability VariablePointersStorageBuffer
4827 OpCapability UntypedPointersKHR
4828 OpExtension "SPV_KHR_untyped_pointers"
4829 OpMemoryModel Logical GLSL450
4830 %void = OpTypeVoid
4831 %bool = OpTypeBool
4832 %int = OpTypeInt 32 0
4833 %ptr = OpTypeUntypedPointerKHR StorageBuffer
4834 %var = OpUntypedVariableKHR %ptr StorageBuffer
4835 %func_ty = OpTypeFunction %void
4836 %func = OpFunction %void None %func_ty
4837 %1 = OpLabel
4838 %equal = )" + operation;
4839
4840 if (operation == "OpPtrDiff") {
4841 spirv += " %int ";
4842 } else {
4843 spirv += " %bool ";
4844 }
4845
4846 spirv += R"(%var %var
4847 OpReturn
4848 OpFunctionEnd
4849 )";
4850
4851 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4852 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4853 }
4854
4855 TEST_P(ValidatePointerComparisons, GoodUntypedPointerSameStorageClass) {
4856 const std::string operation = GetParam();
4857
4858 std::string spirv = R"(
4859 OpCapability Shader
4860 OpCapability Linkage
4861 OpCapability VariablePointersStorageBuffer
4862 OpCapability UntypedPointersKHR
4863 OpExtension "SPV_KHR_untyped_pointers"
4864 OpMemoryModel Logical GLSL450
4865 %void = OpTypeVoid
4866 %bool = OpTypeBool
4867 %int = OpTypeInt 32 0
4868 %ptr1 = OpTypeUntypedPointerKHR StorageBuffer
4869 %var = OpUntypedVariableKHR %ptr1 StorageBuffer
4870 %ptr2 = OpTypeUntypedPointerKHR StorageBuffer
4871 %var2 = OpUntypedVariableKHR %ptr2 StorageBuffer
4872 %func_ty = OpTypeFunction %void
4873 %func = OpFunction %void None %func_ty
4874 %1 = OpLabel
4875 %equal = )" + operation;
4876
4877 if (operation == "OpPtrDiff") {
4878 spirv += " %int ";
4879 } else {
4880 spirv += " %bool ";
4881 }
4882
4883 spirv += R"(%var %var2
4884 OpReturn
4885 OpFunctionEnd
4886 )";
4887
4888 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4889 if (operation == "OpPtrDiff") {
4890 EXPECT_EQ(SPV_ERROR_INVALID_ID,
4891 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4892 EXPECT_THAT(getDiagnosticString(),
4893 HasSubstr("The types of Operand 1 and Operand 2 must match"));
4894 } else {
4895 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4896 }
4897 }
4898
4899 TEST_P(ValidatePointerComparisons, BadUntypedPointerDiffStorageClass) {
4900 const std::string operation = GetParam();
4901
4902 std::string spirv = R"(
4903 OpCapability Shader
4904 OpCapability Linkage
4905 OpCapability VariablePointers
4906 OpCapability UntypedPointersKHR
4907 OpExtension "SPV_KHR_untyped_pointers"
4908 OpMemoryModel Logical GLSL450
4909 %void = OpTypeVoid
4910 %bool = OpTypeBool
4911 %int = OpTypeInt 32 0
4912 %ptr1 = OpTypeUntypedPointerKHR StorageBuffer
4913 %var1 = OpUntypedVariableKHR %ptr1 StorageBuffer
4914 %ptr2 = OpTypeUntypedPointerKHR Workgroup
4915 %var2 = OpUntypedVariableKHR %ptr2 Workgroup %int
4916 %func_ty = OpTypeFunction %void
4917 %func = OpFunction %void None %func_ty
4918 %1 = OpLabel
4919 %equal = )" + operation;
4920
4921 if (operation == "OpPtrDiff") {
4922 spirv += " %int ";
4923 } else {
4924 spirv += " %bool ";
4925 }
4926
4927 spirv += R"(%var1 %var2
4928 OpReturn
4929 OpFunctionEnd
4930 )";
4931
4932 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4933 if (operation == "OpPtrDiff") {
4934 EXPECT_EQ(SPV_ERROR_INVALID_ID,
4935 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4936 EXPECT_THAT(getDiagnosticString(),
4937 HasSubstr("The types of Operand 1 and Operand 2 must match"));
4938 } else {
4939 EXPECT_EQ(SPV_ERROR_INVALID_ID,
4940 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4941 EXPECT_THAT(getDiagnosticString(),
4942 HasSubstr("Pointer storage classes must match"));
4943 }
4944 }
4945
4946 TEST_P(ValidatePointerComparisons, GoodMixedPointerSameStorageClass) {
4947 const std::string operation = GetParam();
4948
4949 std::string spirv = R"(
4950 OpCapability Shader
4951 OpCapability Linkage
4952 OpCapability VariablePointersStorageBuffer
4953 OpCapability UntypedPointersKHR
4954 OpExtension "SPV_KHR_untyped_pointers"
4955 OpMemoryModel Logical GLSL450
4956 %void = OpTypeVoid
4957 %bool = OpTypeBool
4958 %int = OpTypeInt 32 0
4959 %ptr1 = OpTypeUntypedPointerKHR StorageBuffer
4960 %var = OpUntypedVariableKHR %ptr1 StorageBuffer
4961 %ptr2 = OpTypePointer StorageBuffer %int
4962 %var2 = OpVariable %ptr2 StorageBuffer
4963 %func_ty = OpTypeFunction %void
4964 %func = OpFunction %void None %func_ty
4965 %1 = OpLabel
4966 %equal = )" + operation;
4967
4968 if (operation == "OpPtrDiff") {
4969 spirv += " %int ";
4970 } else {
4971 spirv += " %bool ";
4972 }
4973
4974 spirv += R"(%var %var2
4975 OpReturn
4976 OpFunctionEnd
4977 )";
4978
4979 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
4980 if (operation == "OpPtrDiff") {
4981 EXPECT_EQ(SPV_ERROR_INVALID_ID,
4982 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4983 EXPECT_THAT(getDiagnosticString(),
4984 HasSubstr("The types of Operand 1 and Operand 2 must match"));
4985 } else {
4986 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
4987 }
4988 }
4989
4990 TEST_P(ValidatePointerComparisons, BadMixedPointerDiffStorageClass) {
4991 const std::string operation = GetParam();
4992
4993 std::string spirv = R"(
4994 OpCapability Shader
4995 OpCapability Linkage
4996 OpCapability VariablePointers
4997 OpCapability UntypedPointersKHR
4998 OpExtension "SPV_KHR_untyped_pointers"
4999 OpMemoryModel Logical GLSL450
5000 %void = OpTypeVoid
5001 %bool = OpTypeBool
5002 %int = OpTypeInt 32 0
5003 %ptr1 = OpTypeUntypedPointerKHR StorageBuffer
5004 %var1 = OpUntypedVariableKHR %ptr1 StorageBuffer
5005 %ptr2 = OpTypePointer Workgroup %int
5006 %var2 = OpVariable %ptr2 Workgroup
5007 %func_ty = OpTypeFunction %void
5008 %func = OpFunction %void None %func_ty
5009 %1 = OpLabel
5010 %equal = )" + operation;
5011
5012 if (operation == "OpPtrDiff") {
5013 spirv += " %int ";
5014 } else {
5015 spirv += " %bool ";
5016 }
5017
5018 spirv += R"(%var1 %var2
5019 OpReturn
5020 OpFunctionEnd
5021 )";
5022
5023 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
5024 if (operation == "OpPtrDiff") {
5025 EXPECT_EQ(SPV_ERROR_INVALID_ID,
5026 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
5027 EXPECT_THAT(getDiagnosticString(),
5028 HasSubstr("The types of Operand 1 and Operand 2 must match"));
5029 } else {
5030 EXPECT_EQ(SPV_ERROR_INVALID_ID,
5031 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
5032 EXPECT_THAT(getDiagnosticString(),
5033 HasSubstr("Pointer storage classes must match"));
5034 }
5035 }
5036
5037 INSTANTIATE_TEST_SUITE_P(PointerComparisons, ValidatePointerComparisons,
5038 Values("OpPtrEqual", "OpPtrNotEqual", "OpPtrDiff"));
5039
5040 TEST_F(ValidateMemory, VariableInitializerWrongType) {
5041 const std::string spirv = R"(
5042 OpCapability Shader
5043 OpCapability Linkage
5044 OpCapability VariablePointersStorageBuffer
5045 OpMemoryModel Logical GLSL450
5046 %void = OpTypeVoid
5047 %int = OpTypeInt 32 0
5048 %float = OpTypeFloat 32
5049 %ptr_wg_int = OpTypePointer Workgroup %int
5050 %ptr_wg_float = OpTypePointer Workgroup %int
5051 %wg_var = OpVariable %ptr_wg_int Workgroup
5052 %ptr_private_wg_float = OpTypePointer Private %ptr_wg_float
5053 %priv_var = OpVariable %ptr_private_wg_float Private %wg_var
5054 )";
5055
5056 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
5057 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
5058 EXPECT_THAT(getDiagnosticString(),
5059 HasSubstr("Initializer type must match the data type"));
5060 }
5061
5062 TEST_F(ValidateMemory, StoreToImage) {
5063 const std::string spirv = R"(
5064 OpCapability Shader
5065 OpMemoryModel Logical GLSL450
5066 OpEntryPoint GLCompute %main "main"
5067 OpExecutionMode %main LocalSize 1 1 1
5068 %void = OpTypeVoid
5069 %int = OpTypeInt 32 0
5070 %img = OpTypeImage %int 2D 2 0 0 2 R32i
5071 %ptr_img = OpTypePointer Function %img
5072 %void_fn = OpTypeFunction %void
5073 %main = OpFunction %void None %void_fn
5074 %entry = OpLabel
5075 %var = OpVariable %ptr_img Function
5076 %value = OpLoad %img %var
5077 OpStore %var %value
5078 OpReturn
5079 OpFunctionEnd
5080 )";
5081 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
5082 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
5083 EXPECT_THAT(getDiagnosticString(),
5084 AnyVUID("VUID-StandaloneSpirv-OpTypeImage-06924"));
5085 EXPECT_THAT(
5086 getDiagnosticString(),
5087 HasSubstr("Cannot store to OpTypeImage, OpTypeSampler, "
5088 "OpTypeSampledImage, or OpTypeAccelerationStructureKHR"));
5089 }
5090
5091 TEST_F(ValidateMemory, StoreToImageArray) {
5092 const std::string spirv = R"(
5093 OpCapability Shader
5094 OpMemoryModel Logical GLSL450
5095 OpEntryPoint GLCompute %main "main"
5096 OpExecutionMode %main LocalSize 1 1 1
5097 %void = OpTypeVoid
5098 %int = OpTypeInt 32 0
5099 %img = OpTypeImage %int 2D 2 0 0 2 R32i
5100 %arr_size = OpConstant %int 5
5101 %i = OpConstant %int 2
5102 %arr_img = OpTypeArray %img %arr_size
5103 %ptr_img = OpTypePointer Function %img
5104 %ptr_arr_img = OpTypePointer Function %arr_img
5105 %void_fn = OpTypeFunction %void
5106 %main = OpFunction %void None %void_fn
5107 %entry = OpLabel
5108 %var = OpVariable %ptr_arr_img Function
5109 %value = OpLoad %arr_img %var
5110 OpStore %var %value
5111 OpReturn
5112 OpFunctionEnd
5113 )";
5114 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
5115 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
5116 EXPECT_THAT(getDiagnosticString(),
5117 AnyVUID("VUID-StandaloneSpirv-OpTypeImage-06924"));
5118 EXPECT_THAT(
5119 getDiagnosticString(),
5120 HasSubstr("Cannot store to OpTypeImage, OpTypeSampler, "
5121 "OpTypeSampledImage, or OpTypeAccelerationStructureKHR"));
5122 }
5123
5124 TEST_F(ValidateMemory, StoreToSampler) {
5125 const std::string spirv = R"(
5126 OpCapability Shader
5127 OpMemoryModel Logical GLSL450
5128 OpEntryPoint GLCompute %main "main"
5129 OpExecutionMode %main LocalSize 1 1 1
5130 %void = OpTypeVoid
5131 %smp = OpTypeSampler
5132 %ptr_smp = OpTypePointer Function %smp
5133 %void_fn = OpTypeFunction %void
5134 %main = OpFunction %void None %void_fn
5135 %entry = OpLabel
5136 %var = OpVariable %ptr_smp Function
5137 %value = OpLoad %smp %var
5138 OpStore %var %value
5139 OpReturn
5140 OpFunctionEnd
5141 )";
5142 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
5143 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
5144 EXPECT_THAT(getDiagnosticString(),
5145 AnyVUID("VUID-StandaloneSpirv-OpTypeImage-06924"));
5146 EXPECT_THAT(
5147 getDiagnosticString(),
5148 HasSubstr("Cannot store to OpTypeImage, OpTypeSampler, "
5149 "OpTypeSampledImage, or OpTypeAccelerationStructureKHR"));
5150 }
5151
5152 TEST_F(ValidateMemory, StoreToSampledImage) {
5153 const std::string spirv = R"(
5154 OpCapability Shader
5155 OpMemoryModel Logical GLSL450
5156 OpEntryPoint GLCompute %main "main"
5157 OpExecutionMode %main LocalSize 1 1 1
5158 %void = OpTypeVoid
5159 %int = OpTypeInt 32 0
5160 %img = OpTypeImage %int 2D 2 0 0 1 R32i
5161 %samp_img = OpTypeSampledImage %img
5162 %ptr_samp_img = OpTypePointer Function %samp_img
5163 %void_fn = OpTypeFunction %void
5164 %main = OpFunction %void None %void_fn
5165 %entry = OpLabel
5166 %var = OpVariable %ptr_samp_img Function
5167 %value = OpLoad %samp_img %var
5168 OpStore %var %value
5169 OpReturn
5170 OpFunctionEnd
5171 )";
5172
5173 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
5174 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
5175 EXPECT_THAT(getDiagnosticString(),
5176 AnyVUID("VUID-StandaloneSpirv-OpTypeImage-06924"));
5177 EXPECT_THAT(
5178 getDiagnosticString(),
5179 HasSubstr("Cannot store to OpTypeImage, OpTypeSampler, "
5180 "OpTypeSampledImage, or OpTypeAccelerationStructureKHR"));
5181 }
5182
5183 TEST_F(ValidateMemory, StoreToAccelarationStructureKHR) {
5184 const std::string spirv = R"(
5185 OpCapability Shader
5186 OpCapability RayQueryKHR
5187 OpExtension "SPV_KHR_ray_query"
5188 OpMemoryModel Logical GLSL450
5189 OpEntryPoint GLCompute %main "main"
5190 OpExecutionMode %main LocalSize 1 1 1
5191 %void = OpTypeVoid
5192 %as = OpTypeAccelerationStructureKHR
5193 %ptr_as = OpTypePointer Function %as
5194 %void_fn = OpTypeFunction %void
5195 %main = OpFunction %void None %void_fn
5196 %entry = OpLabel
5197 %var = OpVariable %ptr_as Function
5198 %value = OpLoad %as %var
5199 OpStore %var %value
5200 OpReturn
5201 OpFunctionEnd
5202 )";
5203
5204 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
5205 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
5206 EXPECT_THAT(getDiagnosticString(),
5207 AnyVUID("VUID-StandaloneSpirv-OpTypeImage-06924"));
5208 EXPECT_THAT(
5209 getDiagnosticString(),
5210 HasSubstr("Cannot store to OpTypeImage, OpTypeSampler, "
5211 "OpTypeSampledImage, or OpTypeAccelerationStructureKHR"));
5212 }
5213
5214 TEST_F(ValidateMemory, StoreToUniformBlock) {
5215 const std::string spirv = R"(
5216 OpCapability Shader
5217 OpMemoryModel Logical GLSL450
5218 OpEntryPoint GLCompute %main "main"
5219 OpExecutionMode %main LocalSize 1 1 1
5220 OpDecorate %struct Block
5221 OpMemberDecorate %struct 0 Offset 0
5222 OpDecorate %var DescriptorSet 0
5223 OpDecorate %var Binding 0
5224 %void = OpTypeVoid
5225 %int = OpTypeInt 32 0
5226 %int_0 = OpConstant %int 0
5227 %int4 = OpTypeVector %int 4
5228 %struct = OpTypeStruct %int4
5229 %ptr_uniform_struct = OpTypePointer Uniform %struct
5230 %ptr_uniform_int4 = OpTypePointer Uniform %int4
5231 %ptr_uniform_int = OpTypePointer Uniform %int
5232 %var = OpVariable %ptr_uniform_struct Uniform
5233 %void_fn = OpTypeFunction %void
5234 %main = OpFunction %void None %void_fn
5235 %entry = OpLabel
5236 %gep1 = OpAccessChain %ptr_uniform_int4 %var %int_0
5237 %gep2 = OpAccessChain %ptr_uniform_int %gep1 %int_0
5238 OpStore %gep2 %int_0
5239 OpReturn
5240 OpFunctionEnd
5241 )";
5242
5243 CompileSuccessfully(spirv);
5244 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5245 }
5246
5247 TEST_F(ValidateMemory, StoreToUniformBlockVulkan) {
5248 const std::string spirv = R"(
5249 OpCapability Shader
5250 OpMemoryModel Logical GLSL450
5251 OpEntryPoint GLCompute %main "main"
5252 OpExecutionMode %main LocalSize 1 1 1
5253 OpDecorate %struct Block
5254 OpMemberDecorate %struct 0 Offset 0
5255 OpDecorate %var DescriptorSet 0
5256 OpDecorate %var Binding 0
5257 %void = OpTypeVoid
5258 %int = OpTypeInt 32 0
5259 %int_0 = OpConstant %int 0
5260 %int4 = OpTypeVector %int 4
5261 %struct = OpTypeStruct %int4
5262 %ptr_uniform_struct = OpTypePointer Uniform %struct
5263 %ptr_uniform_int4 = OpTypePointer Uniform %int4
5264 %ptr_uniform_int = OpTypePointer Uniform %int
5265 %var = OpVariable %ptr_uniform_struct Uniform
5266 %void_fn = OpTypeFunction %void
5267 %main = OpFunction %void None %void_fn
5268 %entry = OpLabel
5269 %gep1 = OpAccessChain %ptr_uniform_int4 %var %int_0
5270 %gep2 = OpAccessChain %ptr_uniform_int %gep1 %int_0
5271 OpStore %gep2 %int_0
5272 OpReturn
5273 OpFunctionEnd
5274 )";
5275
5276 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
5277 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
5278 EXPECT_THAT(getDiagnosticString(),
5279 AnyVUID("VUID-StandaloneSpirv-Uniform-06925"));
5280 EXPECT_THAT(
5281 getDiagnosticString(),
5282 HasSubstr("In the Vulkan environment, cannot store to Uniform Blocks"));
5283 }
5284
5285 // This test requires that the struct is not id 2.
5286 TEST_F(ValidateMemory, StoreToUniformBlockVulkan2) {
5287 const std::string spirv = R"(
5288 OpCapability Shader
5289 OpMemoryModel Logical GLSL450
5290 OpEntryPoint GLCompute %main "main" %gid_var
5291 OpExecutionMode %main LocalSize 1 1 1
5292 OpDecorate %3 Block
5293 OpMemberDecorate %3 0 Offset 0
5294 OpDecorate %var DescriptorSet 0
5295 OpDecorate %var Binding 0
5296 OpDecorate %gid_var BuiltIn GlobalInvocationId
5297 %void = OpTypeVoid
5298 %int = OpTypeInt 32 0
5299 %int_0 = OpConstant %int 0
5300 %int3 = OpTypeVector %int 3
5301 %int4 = OpTypeVector %int 4
5302 %3 = OpTypeStruct %int4
5303 %ptr_uniform_struct = OpTypePointer Uniform %3
5304 %ptr_uniform_int4 = OpTypePointer Uniform %int4
5305 %ptr_uniform_int = OpTypePointer Uniform %int
5306 %var = OpVariable %ptr_uniform_struct Uniform
5307 %ptr_input_int3 = OpTypePointer Input %int3
5308 %gid_var = OpVariable %ptr_input_int3 Input
5309 %void_fn = OpTypeFunction %void
5310 %main = OpFunction %void None %void_fn
5311 %entry = OpLabel
5312 %gep1 = OpAccessChain %ptr_uniform_int4 %var %int_0
5313 %gep2 = OpAccessChain %ptr_uniform_int %gep1 %int_0
5314 OpStore %gep2 %int_0
5315 OpReturn
5316 OpFunctionEnd
5317 )";
5318
5319 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
5320 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
5321 EXPECT_THAT(getDiagnosticString(),
5322 AnyVUID("VUID-StandaloneSpirv-Uniform-06925"));
5323 EXPECT_THAT(
5324 getDiagnosticString(),
5325 HasSubstr("In the Vulkan environment, cannot store to Uniform Blocks"));
5326 }
5327
5328 TEST_F(ValidateMemory, StoreToUniformBufferBlockVulkan) {
5329 const std::string spirv = R"(
5330 OpCapability Shader
5331 OpMemoryModel Logical GLSL450
5332 OpEntryPoint GLCompute %main "main"
5333 OpExecutionMode %main LocalSize 1 1 1
5334 OpDecorate %struct BufferBlock
5335 OpMemberDecorate %struct 0 Offset 0
5336 OpDecorate %var DescriptorSet 0
5337 OpDecorate %var Binding 0
5338 %void = OpTypeVoid
5339 %int = OpTypeInt 32 0
5340 %int_0 = OpConstant %int 0
5341 %int4 = OpTypeVector %int 4
5342 %struct = OpTypeStruct %int4
5343 %ptr_uniform_struct = OpTypePointer Uniform %struct
5344 %ptr_uniform_int4 = OpTypePointer Uniform %int4
5345 %ptr_uniform_int = OpTypePointer Uniform %int
5346 %var = OpVariable %ptr_uniform_struct Uniform
5347 %void_fn = OpTypeFunction %void
5348 %main = OpFunction %void None %void_fn
5349 %entry = OpLabel
5350 %gep1 = OpAccessChain %ptr_uniform_int4 %var %int_0
5351 %gep2 = OpAccessChain %ptr_uniform_int %gep1 %int_0
5352 OpStore %gep2 %int_0
5353 OpReturn
5354 OpFunctionEnd
5355 )";
5356
5357 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
5358 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
5359 }
5360
5361 TEST_F(ValidateMemory, StoreToUniformBlockVulkanArray) {
5362 const std::string spirv = R"(
5363 OpCapability Shader
5364 OpMemoryModel Logical GLSL450
5365 OpEntryPoint GLCompute %main "main"
5366 OpExecutionMode %main LocalSize 1 1 1
5367 OpDecorate %struct Block
5368 OpMemberDecorate %struct 0 Offset 0
5369 OpDecorate %var DescriptorSet 0
5370 OpDecorate %var Binding 0
5371 %void = OpTypeVoid
5372 %int = OpTypeInt 32 0
5373 %int_0 = OpConstant %int 0
5374 %int_1 = OpConstant %int 1
5375 %int4 = OpTypeVector %int 4
5376 %struct = OpTypeStruct %int4
5377 %array_struct = OpTypeArray %struct %int_1
5378 %ptr_uniform_array = OpTypePointer Uniform %array_struct
5379 %ptr_uniform_struct = OpTypePointer Uniform %struct
5380 %ptr_uniform_int4 = OpTypePointer Uniform %int4
5381 %ptr_uniform_int = OpTypePointer Uniform %int
5382 %var = OpVariable %ptr_uniform_array Uniform
5383 %void_fn = OpTypeFunction %void
5384 %main = OpFunction %void None %void_fn
5385 %entry = OpLabel
5386 %gep1 = OpAccessChain %ptr_uniform_int %var %int_0 %int_0 %int_0
5387 %gep2 = OpCopyObject %ptr_uniform_int %gep1
5388 OpStore %gep2 %int_0
5389 OpReturn
5390 OpFunctionEnd
5391 )";
5392
5393 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
5394 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
5395 EXPECT_THAT(getDiagnosticString(),
5396 AnyVUID("VUID-StandaloneSpirv-Uniform-06925"));
5397 EXPECT_THAT(
5398 getDiagnosticString(),
5399 HasSubstr("In the Vulkan environment, cannot store to Uniform Blocks"));
5400 }
5401
5402 // This test requires that the struct is not id 2.
5403 TEST_F(ValidateMemory, StoreToUniformBlockVulkanArray2) {
5404 const std::string spirv = R"(
5405 OpCapability Shader
5406 OpMemoryModel Logical GLSL450
5407 OpEntryPoint GLCompute %main "main" %gid_var
5408 OpExecutionMode %main LocalSize 1 1 1
5409 OpDecorate %struct Block
5410 OpMemberDecorate %struct 0 Offset 0
5411 OpDecorate %var DescriptorSet 0
5412 OpDecorate %var Binding 0
5413 OpDecorate %gid_var BuiltIn GlobalInvocationId
5414 %void = OpTypeVoid
5415 %int = OpTypeInt 32 0
5416 %int_0 = OpConstant %int 0
5417 %int_1 = OpConstant %int 1
5418 %int3 = OpTypeVector %int 3
5419 %int4 = OpTypeVector %int 4
5420 %struct = OpTypeStruct %int4
5421 %array_struct = OpTypeArray %struct %int_1
5422 %ptr_uniform_array = OpTypePointer Uniform %array_struct
5423 %ptr_uniform_struct = OpTypePointer Uniform %struct
5424 %ptr_uniform_int4 = OpTypePointer Uniform %int4
5425 %ptr_uniform_int = OpTypePointer Uniform %int
5426 %var = OpVariable %ptr_uniform_array Uniform
5427 %ptr_input_int3 = OpTypePointer Input %int3
5428 %gid_var = OpVariable %ptr_input_int3 Input
5429 %void_fn = OpTypeFunction %void
5430 %main = OpFunction %void None %void_fn
5431 %entry = OpLabel
5432 %gep1 = OpAccessChain %ptr_uniform_int %var %int_0 %int_0 %int_0
5433 %gep2 = OpCopyObject %ptr_uniform_int %gep1
5434 OpStore %gep2 %int_0
5435 OpReturn
5436 OpFunctionEnd
5437 )";
5438
5439 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
5440 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
5441 EXPECT_THAT(getDiagnosticString(),
5442 AnyVUID("VUID-StandaloneSpirv-Uniform-06925"));
5443 EXPECT_THAT(
5444 getDiagnosticString(),
5445 HasSubstr("In the Vulkan environment, cannot store to Uniform Blocks"));
5446 }
5447
5448 TEST_F(ValidateMemory, StoreToUniformBlockVulkanRuntimeArray) {
5449 const std::string spirv = R"(
5450 OpCapability Shader
5451 OpCapability RuntimeDescriptorArrayEXT
5452 OpExtension "SPV_EXT_descriptor_indexing"
5453 OpMemoryModel Logical GLSL450
5454 OpEntryPoint GLCompute %main "main"
5455 OpExecutionMode %main LocalSize 1 1 1
5456 OpDecorate %struct Block
5457 OpMemberDecorate %struct 0 Offset 0
5458 OpDecorate %var DescriptorSet 0
5459 OpDecorate %var Binding 0
5460 %void = OpTypeVoid
5461 %int = OpTypeInt 32 0
5462 %int_0 = OpConstant %int 0
5463 %int4 = OpTypeVector %int 4
5464 %struct = OpTypeStruct %int4
5465 %array_struct = OpTypeRuntimeArray %struct
5466 %ptr_uniform_array = OpTypePointer Uniform %array_struct
5467 %ptr_uniform_struct = OpTypePointer Uniform %struct
5468 %ptr_uniform_int4 = OpTypePointer Uniform %int4
5469 %ptr_uniform_int = OpTypePointer Uniform %int
5470 %var = OpVariable %ptr_uniform_array Uniform
5471 %void_fn = OpTypeFunction %void
5472 %main = OpFunction %void None %void_fn
5473 %entry = OpLabel
5474 %gep1 = OpAccessChain %ptr_uniform_int4 %var %int_0 %int_0
5475 %gep2 = OpInBoundsAccessChain %ptr_uniform_int %gep1 %int_0
5476 OpStore %gep2 %int_0
5477 OpReturn
5478 OpFunctionEnd
5479 )";
5480
5481 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
5482 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
5483 EXPECT_THAT(getDiagnosticString(),
5484 AnyVUID("VUID-StandaloneSpirv-Uniform-06925"));
5485 EXPECT_THAT(
5486 getDiagnosticString(),
5487 HasSubstr("In the Vulkan environment, cannot store to Uniform Blocks"));
5488 }
5489
5490 using ValidateSizedVariable = spvtest::ValidateBase<
5491 std::tuple<std::string, std::string, std::string, spv_target_env>>;
5492
5493 CodeGenerator GetSizedVariableCodeGenerator(bool is_8bit, bool buffer_block) {
5494 CodeGenerator generator;
5495 generator.capabilities_ = "OpCapability Shader\nOpCapability Linkage\n";
5496 generator.extensions_ =
5497 "OpExtension \"SPV_KHR_16bit_storage\"\nOpExtension "
5498 "\"SPV_KHR_8bit_storage\"\n";
5499 generator.memory_model_ = "OpMemoryModel Logical GLSL450\n";
5500 if (is_8bit) {
5501 generator.before_types_ =
5502 "OpMemberDecorate %char_buffer_block 0 Offset 0\n";
5503 if (buffer_block)
5504 generator.before_types_ += "OpDecorate %char_buffer_block BufferBlock\n";
5505
5506 generator.types_ = R"(%void = OpTypeVoid
5507 %char = OpTypeInt 8 0
5508 %char4 = OpTypeVector %char 4
5509 %char_buffer_block = OpTypeStruct %char
5510 )";
5511 } else {
5512 generator.before_types_ =
5513 "OpMemberDecorate %half_buffer_block 0 Offset 0\n"
5514 "OpMemberDecorate %short_buffer_block 0 Offset 0\n";
5515 if (buffer_block) {
5516 generator.before_types_ +=
5517 "OpDecorate %half_buffer_block BufferBlock\n"
5518 "OpDecorate %short_buffer_block BufferBlock\n";
5519 }
5520
5521 generator.types_ = R"(%void = OpTypeVoid
5522 %short = OpTypeInt 16 0
5523 %half = OpTypeFloat 16
5524 %short4 = OpTypeVector %short 4
5525 %half4 = OpTypeVector %half 4
5526 %mat4x4 = OpTypeMatrix %half4 4
5527 %short_buffer_block = OpTypeStruct %short
5528 %half_buffer_block = OpTypeStruct %half
5529 )";
5530 }
5531 generator.after_types_ = R"(%void_fn = OpTypeFunction %void
5532 %func = OpFunction %void None %void_fn
5533 %entry = OpLabel
5534 )";
5535 generator.add_at_the_end_ = "OpReturn\nOpFunctionEnd\n";
5536 return generator;
5537 }
5538
5539 TEST_P(ValidateSizedVariable, Capability) {
5540 const std::string storage_class = std::get<0>(GetParam());
5541 const std::string capability = std::get<1>(GetParam());
5542 const std::string var_type = std::get<2>(GetParam());
5543 const spv_target_env target = std::get<3>(GetParam());
5544
5545 ASSERT_TRUE(target == SPV_ENV_UNIVERSAL_1_3 ||
5546 target == SPV_ENV_UNIVERSAL_1_4);
5547
5548 bool type_8bit = false;
5549 if (var_type == "%char" || var_type == "%char4" ||
5550 var_type == "%char_buffer_block") {
5551 type_8bit = true;
5552 }
5553
5554 const bool buffer_block = var_type.find("buffer_block") != std::string::npos;
5555
5556 auto generator = GetSizedVariableCodeGenerator(type_8bit, buffer_block);
5557
5558 if (capability == "WorkgroupMemoryExplicitLayout8BitAccessKHR" ||
5559 capability == "WorkgroupMemoryExplicitLayout16BitAccessKHR") {
5560 generator.extensions_ +=
5561 "OpExtension \"SPV_KHR_workgroup_memory_explicit_layout\"\n";
5562 }
5563
5564 generator.types_ += "%ptr_type = OpTypePointer " + storage_class + " " +
5565 var_type + "\n%var = OpVariable %ptr_type " +
5566 storage_class + "\n";
5567 generator.capabilities_ += "OpCapability " + capability + "\n";
5568
5569 bool capability_ok = false;
5570 bool storage_class_ok = false;
5571 if (storage_class == "Input" || storage_class == "Output") {
5572 if (!type_8bit) {
5573 capability_ok = capability == "StorageInputOutput16";
5574 storage_class_ok = true;
5575 }
5576 } else if (storage_class == "StorageBuffer") {
5577 if (type_8bit) {
5578 capability_ok = capability == "StorageBuffer8BitAccess" ||
5579 capability == "UniformAndStorageBuffer8BitAccess";
5580 } else {
5581 capability_ok = capability == "StorageBuffer16BitAccess" ||
5582 capability == "UniformAndStorageBuffer16BitAccess";
5583 }
5584 storage_class_ok = true;
5585 } else if (storage_class == "PushConstant") {
5586 if (type_8bit) {
5587 capability_ok = capability == "StoragePushConstant8";
5588 } else {
5589 capability_ok = capability == "StoragePushConstant16";
5590 }
5591 storage_class_ok = true;
5592 } else if (storage_class == "Uniform") {
5593 if (type_8bit) {
5594 capability_ok = capability == "UniformAndStorageBuffer8BitAccess" ||
5595 (capability == "StorageBuffer8BitAccess" && buffer_block);
5596 } else {
5597 capability_ok =
5598 capability == "UniformAndStorageBuffer16BitAccess" ||
5599 (capability == "StorageBuffer16BitAccess" && buffer_block);
5600 }
5601 storage_class_ok = true;
5602 } else if (storage_class == "Workgroup") {
5603 if (type_8bit) {
5604 capability_ok =
5605 capability == "WorkgroupMemoryExplicitLayout8BitAccessKHR";
5606 } else {
5607 capability_ok =
5608 capability == "WorkgroupMemoryExplicitLayout16BitAccessKHR";
5609 }
5610 storage_class_ok = true;
5611 }
5612
5613 CompileSuccessfully(generator.Build(), target);
5614 spv_result_t result = ValidateInstructions(target);
5615 if (target < SPV_ENV_UNIVERSAL_1_4 &&
5616 (capability == "WorkgroupMemoryExplicitLayout8BitAccessKHR" ||
5617 capability == "WorkgroupMemoryExplicitLayout16BitAccessKHR")) {
5618 EXPECT_EQ(SPV_ERROR_WRONG_VERSION, result);
5619 EXPECT_THAT(getDiagnosticString(),
5620 HasSubstr("requires SPIR-V version 1.4 or later"));
5621 } else if (buffer_block && target > SPV_ENV_UNIVERSAL_1_3) {
5622 EXPECT_EQ(SPV_ERROR_WRONG_VERSION, result);
5623 EXPECT_THAT(getDiagnosticString(),
5624 HasSubstr("requires SPIR-V version 1.3 or earlier"));
5625 } else if (capability_ok) {
5626 EXPECT_EQ(SPV_SUCCESS, result);
5627 } else {
5628 EXPECT_EQ(SPV_ERROR_INVALID_ID, result);
5629 if (storage_class_ok) {
5630 std::string message = std::string("Allocating a variable containing a ") +
5631 (type_8bit ? "8" : "16") + "-bit element in " +
5632 storage_class +
5633 " storage class requires an additional capability";
5634 EXPECT_THAT(getDiagnosticString(), HasSubstr(message));
5635 } else {
5636 std::string message =
5637 std::string("Cannot allocate a variable containing a ") +
5638 (type_8bit ? "8" : "16") + "-bit type in " + storage_class +
5639 " storage class";
5640 EXPECT_THAT(getDiagnosticString(), HasSubstr(message));
5641 }
5642 }
5643 }
5644
5645 INSTANTIATE_TEST_SUITE_P(
5646 Storage8, ValidateSizedVariable,
5647 Combine(Values("UniformConstant", "Input", "Output", "Workgroup",
5648 "CrossWorkgroup", "Private", "StorageBuffer", "Uniform"),
5649 Values("StorageBuffer8BitAccess",
5650 "UniformAndStorageBuffer8BitAccess", "StoragePushConstant8",
5651 "WorkgroupMemoryExplicitLayout8BitAccessKHR"),
5652 Values("%char", "%char4", "%char_buffer_block"),
5653 Values(SPV_ENV_UNIVERSAL_1_3, SPV_ENV_UNIVERSAL_1_4)));
5654
5655 INSTANTIATE_TEST_SUITE_P(
5656 Storage16, ValidateSizedVariable,
5657 Combine(Values("UniformConstant", "Input", "Output", "Workgroup",
5658 "CrossWorkgroup", "Private", "StorageBuffer", "Uniform"),
5659 Values("StorageBuffer16BitAccess",
5660 "UniformAndStorageBuffer16BitAccess",
5661 "StoragePushConstant16", "StorageInputOutput16",
5662 "WorkgroupMemoryExplicitLayout16BitAccessKHR"),
5663 Values("%short", "%half", "%short4", "%half4", "%mat4x4",
5664 "%short_buffer_block", "%half_buffer_block"),
5665 Values(SPV_ENV_UNIVERSAL_1_3, SPV_ENV_UNIVERSAL_1_4)));
5666
5667 using ValidateSizedLoadStore =
5668 spvtest::ValidateBase<std::tuple<std::string, uint32_t, std::string>>;
5669
5670 CodeGenerator GetSizedLoadStoreCodeGenerator(const std::string& base_type,
5671 uint32_t width) {
5672 CodeGenerator generator;
5673 generator.capabilities_ = "OpCapability Shader\nOpCapability Linkage\n";
5674 if (width == 8) {
5675 generator.capabilities_ +=
5676 "OpCapability UniformAndStorageBuffer8BitAccess\n";
5677 generator.extensions_ = "OpExtension \"SPV_KHR_8bit_storage\"\n";
5678 } else {
5679 generator.capabilities_ +=
5680 "OpCapability UniformAndStorageBuffer16BitAccess\n";
5681 generator.extensions_ = "OpExtension \"SPV_KHR_16bit_storage\"\n";
5682 }
5683 generator.memory_model_ = "OpMemoryModel Logical GLSL450\n";
5684 generator.before_types_ = R"(OpDecorate %block Block
5685 OpMemberDecorate %block 0 Offset 0
5686 OpMemberDecorate %struct 0 Offset 0
5687 )";
5688 generator.types_ = R"(%void = OpTypeVoid
5689 %int = OpTypeInt 32 0
5690 %int_0 = OpConstant %int 0
5691 %int_1 = OpConstant %int 1
5692 %int_2 = OpConstant %int 2
5693 %int_3 = OpConstant %int 3
5694 )";
5695
5696 if (width == 8) {
5697 generator.types_ += R"(%scalar = OpTypeInt 8 0
5698 %vector = OpTypeVector %scalar 4
5699 %struct = OpTypeStruct %vector
5700 )";
5701 } else if (base_type == "int") {
5702 generator.types_ += R"(%scalar = OpTypeInt 16 0
5703 %vector = OpTypeVector %scalar 4
5704 %struct = OpTypeStruct %vector
5705 )";
5706 } else {
5707 generator.types_ += R"(%scalar = OpTypeFloat 16
5708 %vector = OpTypeVector %scalar 4
5709 %matrix = OpTypeMatrix %vector 4
5710 %struct = OpTypeStruct %matrix
5711 %ptr_ssbo_matrix = OpTypePointer StorageBuffer %matrix
5712 )";
5713 generator.before_types_ += R"(OpMemberDecorate %struct 0 RowMajor
5714 OpMemberDecorate %struct 0 MatrixStride 16
5715 )";
5716 }
5717 generator.types_ += R"(%block = OpTypeStruct %struct
5718 %ptr_ssbo_block = OpTypePointer StorageBuffer %block
5719 %ptr_ssbo_struct = OpTypePointer StorageBuffer %struct
5720 %ptr_ssbo_vector = OpTypePointer StorageBuffer %vector
5721 %ptr_ssbo_scalar = OpTypePointer StorageBuffer %scalar
5722 %ld_var = OpVariable %ptr_ssbo_block StorageBuffer
5723 %st_var = OpVariable %ptr_ssbo_block StorageBuffer
5724 )";
5725
5726 generator.after_types_ = R"(%void_fn = OpTypeFunction %void
5727 %func = OpFunction %void None %void_fn
5728 %entry = OpLabel
5729 )";
5730 generator.add_at_the_end_ = "OpReturn\nOpFunctionEnd\n";
5731 return generator;
5732 }
5733
5734 TEST_P(ValidateSizedLoadStore, Load) {
5735 std::string base_type = std::get<0>(GetParam());
5736 uint32_t width = std::get<1>(GetParam());
5737 std::string mem_type = std::get<2>(GetParam());
5738
5739 CodeGenerator generator = GetSizedLoadStoreCodeGenerator(base_type, width);
5740 generator.after_types_ +=
5741 "%ld_gep = OpAccessChain %ptr_ssbo_" + mem_type + " %ld_var %int_0";
5742 if (mem_type != "struct") {
5743 generator.after_types_ += " %int_0";
5744 if (mem_type != "matrix" && base_type == "float") {
5745 generator.after_types_ += " %int_0";
5746 }
5747 if (mem_type == "scalar") {
5748 generator.after_types_ += " %int_0";
5749 }
5750 }
5751 generator.after_types_ += "\n";
5752 generator.after_types_ += "%ld = OpLoad %" + mem_type + " %ld_gep\n";
5753
5754 CompileSuccessfully(generator.Build(), SPV_ENV_UNIVERSAL_1_3);
5755 if (mem_type == "struct") {
5756 EXPECT_EQ(SPV_ERROR_INVALID_ID,
5757 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
5758 EXPECT_THAT(
5759 getDiagnosticString(),
5760 HasSubstr(
5761 "8- or 16-bit loads must be a scalar, vector or matrix type"));
5762 } else {
5763 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
5764 }
5765 }
5766
5767 TEST_P(ValidateSizedLoadStore, Store) {
5768 std::string base_type = std::get<0>(GetParam());
5769 uint32_t width = std::get<1>(GetParam());
5770 std::string mem_type = std::get<2>(GetParam());
5771
5772 CodeGenerator generator = GetSizedLoadStoreCodeGenerator(base_type, width);
5773 generator.after_types_ +=
5774 "%ld_gep = OpAccessChain %ptr_ssbo_" + mem_type + " %ld_var %int_0";
5775 if (mem_type != "struct") {
5776 generator.after_types_ += " %int_0";
5777 if (mem_type != "matrix" && base_type == "float") {
5778 generator.after_types_ += " %int_0";
5779 }
5780 if (mem_type == "scalar") {
5781 generator.after_types_ += " %int_0";
5782 }
5783 }
5784 generator.after_types_ += "\n";
5785 generator.after_types_ += "%ld = OpLoad %" + mem_type + " %ld_gep\n";
5786 generator.after_types_ +=
5787 "%st_gep = OpAccessChain %ptr_ssbo_" + mem_type + " %st_var %int_0";
5788 if (mem_type != "struct") {
5789 generator.after_types_ += " %int_0";
5790 if (mem_type != "matrix" && base_type == "float") {
5791 generator.after_types_ += " %int_0";
5792 }
5793 if (mem_type == "scalar") {
5794 generator.after_types_ += " %int_0";
5795 }
5796 }
5797 generator.after_types_ += "\n";
5798 generator.after_types_ += "OpStore %st_gep %ld\n";
5799
5800 CompileSuccessfully(generator.Build(), SPV_ENV_UNIVERSAL_1_3);
5801 if (mem_type == "struct") {
5802 EXPECT_EQ(SPV_ERROR_INVALID_ID,
5803 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
5804 // Can only catch the load.
5805 EXPECT_THAT(
5806 getDiagnosticString(),
5807 HasSubstr(
5808 "8- or 16-bit loads must be a scalar, vector or matrix type"));
5809 } else {
5810 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
5811 }
5812 }
5813
5814 INSTANTIATE_TEST_SUITE_P(LoadStoreInt8, ValidateSizedLoadStore,
5815 Combine(Values("int"), Values(8u),
5816 Values("scalar", "vector", "struct")));
5817 INSTANTIATE_TEST_SUITE_P(LoadStoreInt16, ValidateSizedLoadStore,
5818 Combine(Values("int"), Values(16u),
5819 Values("scalar", "vector", "struct")));
5820 INSTANTIATE_TEST_SUITE_P(LoadStoreFloat16, ValidateSizedLoadStore,
5821 Combine(Values("float"), Values(16u),
5822 Values("scalar", "vector", "matrix",
5823 "struct")));
5824
5825 TEST_F(ValidateMemory, SmallStorageCopyMemoryChar) {
5826 const std::string spirv = R"(
5827 OpCapability Shader
5828 OpCapability Linkage
5829 OpCapability UniformAndStorageBuffer8BitAccess
5830 OpExtension "SPV_KHR_8bit_storage"
5831 OpMemoryModel Logical GLSL450
5832 OpDecorate %block Block
5833 OpMemberDecorate %block 0 Offset 0
5834 %void = OpTypeVoid
5835 %int = OpTypeInt 32 0
5836 %int_0 = OpConstant %int 0
5837 %char = OpTypeInt 8 0
5838 %block = OpTypeStruct %char
5839 %ptr_ssbo_block = OpTypePointer StorageBuffer %block
5840 %in = OpVariable %ptr_ssbo_block StorageBuffer
5841 %out = OpVariable %ptr_ssbo_block StorageBuffer
5842 %void_fn = OpTypeFunction %void
5843 %func = OpFunction %void None %void_fn
5844 %entry = OpLabel
5845 OpCopyMemory %out %in
5846 OpReturn
5847 OpFunctionEnd
5848 )";
5849
5850 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
5851 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
5852 EXPECT_THAT(
5853 getDiagnosticString(),
5854 HasSubstr("Cannot copy memory of objects containing 8- or 16-bit types"));
5855 }
5856
5857 TEST_F(ValidateMemory, SmallStorageCopyMemoryShort) {
5858 const std::string spirv = R"(
5859 OpCapability Shader
5860 OpCapability Linkage
5861 OpCapability UniformAndStorageBuffer16BitAccess
5862 OpExtension "SPV_KHR_16bit_storage"
5863 OpMemoryModel Logical GLSL450
5864 OpDecorate %block Block
5865 OpMemberDecorate %block 0 Offset 0
5866 %void = OpTypeVoid
5867 %int = OpTypeInt 32 0
5868 %int_0 = OpConstant %int 0
5869 %short = OpTypeInt 16 0
5870 %block = OpTypeStruct %short
5871 %ptr_ssbo_block = OpTypePointer StorageBuffer %block
5872 %in = OpVariable %ptr_ssbo_block StorageBuffer
5873 %out = OpVariable %ptr_ssbo_block StorageBuffer
5874 %void_fn = OpTypeFunction %void
5875 %func = OpFunction %void None %void_fn
5876 %entry = OpLabel
5877 OpCopyMemory %out %in
5878 OpReturn
5879 OpFunctionEnd
5880 )";
5881
5882 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
5883 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
5884 EXPECT_THAT(
5885 getDiagnosticString(),
5886 HasSubstr("Cannot copy memory of objects containing 8- or 16-bit types"));
5887 }
5888
5889 TEST_F(ValidateMemory, SmallStorageCopyMemoryHalf) {
5890 const std::string spirv = R"(
5891 OpCapability Shader
5892 OpCapability Linkage
5893 OpCapability UniformAndStorageBuffer16BitAccess
5894 OpExtension "SPV_KHR_16bit_storage"
5895 OpMemoryModel Logical GLSL450
5896 OpDecorate %block Block
5897 OpMemberDecorate %block 0 Offset 0
5898 %void = OpTypeVoid
5899 %int = OpTypeInt 32 0
5900 %int_0 = OpConstant %int 0
5901 %half = OpTypeFloat 16
5902 %block = OpTypeStruct %half
5903 %ptr_ssbo_block = OpTypePointer StorageBuffer %block
5904 %in = OpVariable %ptr_ssbo_block StorageBuffer
5905 %out = OpVariable %ptr_ssbo_block StorageBuffer
5906 %void_fn = OpTypeFunction %void
5907 %func = OpFunction %void None %void_fn
5908 %entry = OpLabel
5909 OpCopyMemory %out %in
5910 OpReturn
5911 OpFunctionEnd
5912 )";
5913
5914 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
5915 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
5916 EXPECT_THAT(
5917 getDiagnosticString(),
5918 HasSubstr("Cannot copy memory of objects containing 8- or 16-bit types"));
5919 }
5920
5921 TEST_F(ValidateMemory, SmallStorageVariableArrayBufferBlockShort) {
5922 const std::string spirv = R"(
5923 OpCapability Shader
5924 OpCapability Linkage
5925 OpCapability StorageBuffer16BitAccess
5926 OpExtension "SPV_KHR_16bit_storage"
5927 OpMemoryModel Logical GLSL450
5928 OpDecorate %block BufferBlock
5929 OpMemberDecorate %block 0 Offset 0
5930 %void = OpTypeVoid
5931 %short = OpTypeInt 16 0
5932 %int = OpTypeInt 32 0
5933 %int_4 = OpConstant %int 4
5934 %block = OpTypeStruct %short
5935 %block_array = OpTypeArray %block %int_4
5936 %ptr_block_array = OpTypePointer Uniform %block_array
5937 %var = OpVariable %ptr_block_array Uniform
5938 )";
5939
5940 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
5941 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
5942 }
5943
5944 TEST_F(ValidateMemory, SmallStorageVariableArrayBufferBlockChar) {
5945 const std::string spirv = R"(
5946 OpCapability Shader
5947 OpCapability Linkage
5948 OpCapability StorageBuffer8BitAccess
5949 OpExtension "SPV_KHR_8bit_storage"
5950 OpMemoryModel Logical GLSL450
5951 OpDecorate %block BufferBlock
5952 OpMemberDecorate %block 0 Offset 0
5953 %void = OpTypeVoid
5954 %char = OpTypeInt 8 0
5955 %int = OpTypeInt 32 0
5956 %int_4 = OpConstant %int 4
5957 %block = OpTypeStruct %char
5958 %block_array = OpTypeArray %block %int_4
5959 %ptr_block_array = OpTypePointer Uniform %block_array
5960 %var = OpVariable %ptr_block_array Uniform
5961 )";
5962
5963 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
5964 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
5965 }
5966
5967 TEST_F(ValidateMemory, SmallStorageVariableArrayBufferBlockHalf) {
5968 const std::string spirv = R"(
5969 OpCapability Shader
5970 OpCapability Linkage
5971 OpCapability StorageBuffer16BitAccess
5972 OpExtension "SPV_KHR_16bit_storage"
5973 OpMemoryModel Logical GLSL450
5974 OpDecorate %block BufferBlock
5975 OpMemberDecorate %block 0 Offset 0
5976 %void = OpTypeVoid
5977 %half = OpTypeFloat 16
5978 %int = OpTypeInt 32 0
5979 %int_4 = OpConstant %int 4
5980 %block = OpTypeStruct %half
5981 %block_array = OpTypeArray %block %int_4
5982 %ptr_block_array = OpTypePointer Uniform %block_array
5983 %var = OpVariable %ptr_block_array Uniform
5984 )";
5985
5986 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
5987 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
5988 }
5989
5990 TEST_F(ValidateMemory, VulkanStorageBufferNotAStruct) {
5991 const std::string spirv = R"(
5992 OpCapability Shader
5993 OpExtension "SPV_KHR_storage_buffer_storage_class"
5994 OpMemoryModel Logical GLSL450
5995 OpEntryPoint GLCompute %main "main"
5996 OpExecutionMode %main LocalSize 1 1 1
5997 %void = OpTypeVoid
5998 %uint = OpTypeInt 32 0
5999 %ptr_ssbo = OpTypePointer StorageBuffer %uint
6000 %var = OpVariable %ptr_ssbo StorageBuffer
6001 %void_fn = OpTypeFunction %void
6002 %main = OpFunction %void None %void_fn
6003 %entry = OpLabel
6004 OpReturn
6005 OpFunctionEnd
6006 )";
6007
6008 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
6009 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
6010 EXPECT_THAT(getDiagnosticString(),
6011 AnyVUID("VUID-StandaloneSpirv-Uniform-06807"));
6012 EXPECT_THAT(
6013 getDiagnosticString(),
6014 HasSubstr("From Vulkan spec:\nVariables identified with "
6015 "the StorageBuffer storage class are used to access "
6016 "transparent buffer backed resources. Such variables must be "
6017 "typed as OpTypeStruct, or an array of this type"));
6018 }
6019
6020 TEST_F(ValidateMemory, VulkanStorageBufferRuntimeArrayNotAStruct) {
6021 const std::string spirv = R"(
6022 OpCapability Shader
6023 OpCapability RuntimeDescriptorArrayEXT
6024 OpExtension "SPV_KHR_storage_buffer_storage_class"
6025 OpExtension "SPV_EXT_descriptor_indexing"
6026 OpMemoryModel Logical GLSL450
6027 OpEntryPoint GLCompute %main "main"
6028 OpExecutionMode %main LocalSize 1 1 1
6029 %void = OpTypeVoid
6030 %uint = OpTypeInt 32 0
6031 %array = OpTypeRuntimeArray %uint
6032 %ptr_ssbo = OpTypePointer StorageBuffer %array
6033 %var = OpVariable %ptr_ssbo StorageBuffer
6034 %void_fn = OpTypeFunction %void
6035 %main = OpFunction %void None %void_fn
6036 %entry = OpLabel
6037 OpReturn
6038 OpFunctionEnd
6039 )";
6040
6041 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
6042 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
6043 EXPECT_THAT(getDiagnosticString(),
6044 AnyVUID("VUID-StandaloneSpirv-Uniform-06807"));
6045 EXPECT_THAT(
6046 getDiagnosticString(),
6047 HasSubstr("From Vulkan spec:\nVariables identified with "
6048 "the StorageBuffer storage class are used to access "
6049 "transparent buffer backed resources. Such variables must be "
6050 "typed as OpTypeStruct, or an array of this type"));
6051 }
6052
6053 TEST_F(ValidateMemory, VulkanStorageBufferArrayNotAStruct) {
6054 const std::string spirv = R"(
6055 OpCapability Shader
6056 OpExtension "SPV_KHR_storage_buffer_storage_class"
6057 OpMemoryModel Logical GLSL450
6058 OpEntryPoint GLCompute %main "main"
6059 OpExecutionMode %main LocalSize 1 1 1
6060 %void = OpTypeVoid
6061 %uint = OpTypeInt 32 0
6062 %uint_4 = OpConstant %uint 4
6063 %array = OpTypeArray %uint %uint_4
6064 %ptr_ssbo = OpTypePointer StorageBuffer %array
6065 %var = OpVariable %ptr_ssbo StorageBuffer
6066 %void_fn = OpTypeFunction %void
6067 %main = OpFunction %void None %void_fn
6068 %entry = OpLabel
6069 OpReturn
6070 OpFunctionEnd
6071 )";
6072
6073 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
6074 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
6075 EXPECT_THAT(getDiagnosticString(),
6076 AnyVUID("VUID-StandaloneSpirv-Uniform-06807"));
6077 EXPECT_THAT(
6078 getDiagnosticString(),
6079 HasSubstr("From Vulkan spec:\nVariables identified with "
6080 "the StorageBuffer storage class are used to access "
6081 "transparent buffer backed resources. Such variables must be "
6082 "typed as OpTypeStruct, or an array of this type"));
6083 }
6084
6085 TEST_F(ValidateMemory, VulkanInvariantOutputSuccess) {
6086 const std::string spirv = R"(
6087 OpCapability Shader
6088 OpMemoryModel Logical GLSL450
6089 OpEntryPoint Vertex %main "main" %var
6090 OpDecorate %var Location 0
6091 OpDecorate %var Invariant
6092 %void = OpTypeVoid
6093 %f32 = OpTypeFloat 32
6094 %ptr_output = OpTypePointer Output %f32
6095 %var = OpVariable %ptr_output Output
6096 %void_fn = OpTypeFunction %void
6097 %main = OpFunction %void None %void_fn
6098 %entry = OpLabel
6099 OpReturn
6100 OpFunctionEnd
6101 )";
6102
6103 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
6104 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
6105 }
6106
6107 TEST_F(ValidateMemory, VulkanInvariantInputStructSuccess) {
6108 const std::string spirv = R"(
6109 OpCapability Shader
6110 OpMemoryModel Logical GLSL450
6111 OpEntryPoint Fragment %main "main" %var
6112 OpExecutionMode %main OriginUpperLeft
6113 OpDecorate %var Location 0
6114 OpMemberDecorate %struct 1 Invariant
6115 %void = OpTypeVoid
6116 %f32 = OpTypeFloat 32
6117 %struct = OpTypeStruct %f32 %f32
6118 %ptr_input = OpTypePointer Input %struct
6119 %var = OpVariable %ptr_input Input
6120 %void_fn = OpTypeFunction %void
6121 %main = OpFunction %void None %void_fn
6122 %entry = OpLabel
6123 OpReturn
6124 OpFunctionEnd
6125 )";
6126
6127 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
6128 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
6129 }
6130
6131 TEST_F(ValidateMemory, VulkanInvariantWrongStorageClass) {
6132 const std::string spirv = R"(
6133 OpCapability Shader
6134 OpMemoryModel Logical GLSL450
6135 OpEntryPoint Vertex %main "main"
6136 OpDecorate %var Invariant
6137 %void = OpTypeVoid
6138 %f32 = OpTypeFloat 32
6139 %ptr_private = OpTypePointer Private %f32
6140 %var = OpVariable %ptr_private Private
6141 %void_fn = OpTypeFunction %void
6142 %main = OpFunction %void None %void_fn
6143 %entry = OpLabel
6144 OpReturn
6145 OpFunctionEnd
6146 )";
6147
6148 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
6149 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
6150 EXPECT_THAT(getDiagnosticString(),
6151 AnyVUID("VUID-StandaloneSpirv-Invariant-04677"));
6152 EXPECT_THAT(
6153 getDiagnosticString(),
6154 HasSubstr(
6155 "Variable decorated with Invariant must only be identified with the "
6156 "Input or Output storage class in Vulkan environment."));
6157 }
6158
6159 TEST_F(ValidateMemory, VulkanInvariantMemberWrongStorageClass) {
6160 const std::string spirv = R"(
6161 OpCapability Shader
6162 OpMemoryModel Logical GLSL450
6163 OpEntryPoint Fragment %main "main"
6164 OpExecutionMode %main OriginUpperLeft
6165 OpMemberDecorate %struct 1 Invariant
6166 %void = OpTypeVoid
6167 %f32 = OpTypeFloat 32
6168 %struct = OpTypeStruct %f32 %f32
6169 %ptr_private = OpTypePointer Private %struct
6170 %var = OpVariable %ptr_private Private
6171 %void_fn = OpTypeFunction %void
6172 %main = OpFunction %void None %void_fn
6173 %entry = OpLabel
6174 OpReturn
6175 OpFunctionEnd
6176 )";
6177
6178 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
6179 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
6180 EXPECT_THAT(getDiagnosticString(),
6181 AnyVUID("VUID-StandaloneSpirv-Invariant-04677"));
6182 EXPECT_THAT(getDiagnosticString(),
6183 HasSubstr("Variable struct member decorated with Invariant must "
6184 "only be identified with the Input or Output storage "
6185 "class in Vulkan environment."));
6186 }
6187
6188 TEST_F(ValidateMemory, PhysicalStorageBufferPtrEqual) {
6189 const std::string spirv = R"(
6190 OpCapability Shader
6191 OpCapability Int64
6192 OpCapability PhysicalStorageBufferAddresses
6193 OpMemoryModel PhysicalStorageBuffer64 GLSL450
6194 OpEntryPoint GLCompute %main "main"
6195 OpExecutionMode %main LocalSize 1 1 1
6196 %void = OpTypeVoid
6197 %bool = OpTypeBool
6198 %long = OpTypeInt 64 0
6199 %long_0 = OpConstant %long 0
6200 %ptr_pssbo_long = OpTypePointer PhysicalStorageBuffer %long
6201 %void_fn = OpTypeFunction %void
6202 %main = OpFunction %void None %void_fn
6203 %entry = OpLabel
6204 %conv = OpConvertUToPtr %ptr_pssbo_long %long_0
6205 %eq = OpPtrEqual %bool %conv %conv
6206 OpReturn
6207 OpFunctionEnd
6208 )";
6209
6210 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_5);
6211 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_5));
6212 EXPECT_THAT(
6213 getDiagnosticString(),
6214 HasSubstr(
6215 "Cannot use a pointer in the PhysicalStorageBuffer storage class"));
6216 }
6217
6218 TEST_F(ValidateMemory, PhysicalStorageBufferPtrNotEqual) {
6219 const std::string spirv = R"(
6220 OpCapability Shader
6221 OpCapability Int64
6222 OpCapability PhysicalStorageBufferAddresses
6223 OpMemoryModel PhysicalStorageBuffer64 GLSL450
6224 OpEntryPoint GLCompute %main "main"
6225 OpExecutionMode %main LocalSize 1 1 1
6226 %void = OpTypeVoid
6227 %bool = OpTypeBool
6228 %long = OpTypeInt 64 0
6229 %long_0 = OpConstant %long 0
6230 %ptr_pssbo_long = OpTypePointer PhysicalStorageBuffer %long
6231 %void_fn = OpTypeFunction %void
6232 %main = OpFunction %void None %void_fn
6233 %entry = OpLabel
6234 %conv = OpConvertUToPtr %ptr_pssbo_long %long_0
6235 %neq = OpPtrNotEqual %bool %conv %conv
6236 OpReturn
6237 OpFunctionEnd
6238 )";
6239
6240 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_5);
6241 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_5));
6242 EXPECT_THAT(
6243 getDiagnosticString(),
6244 HasSubstr(
6245 "Cannot use a pointer in the PhysicalStorageBuffer storage class"));
6246 }
6247
6248 TEST_F(ValidateMemory, PhysicalStorageBufferPtrDiff) {
6249 const std::string spirv = R"(
6250 OpCapability Shader
6251 OpCapability Int64
6252 OpCapability PhysicalStorageBufferAddresses
6253 OpCapability VariablePointers
6254 OpMemoryModel PhysicalStorageBuffer64 GLSL450
6255 OpEntryPoint GLCompute %main "main"
6256 OpExecutionMode %main LocalSize 1 1 1
6257 %void = OpTypeVoid
6258 %long = OpTypeInt 64 0
6259 %long_0 = OpConstant %long 0
6260 %ptr_pssbo_long = OpTypePointer PhysicalStorageBuffer %long
6261 %void_fn = OpTypeFunction %void
6262 %main = OpFunction %void None %void_fn
6263 %entry = OpLabel
6264 %conv = OpConvertUToPtr %ptr_pssbo_long %long_0
6265 %diff = OpPtrDiff %long %conv %conv
6266 OpReturn
6267 OpFunctionEnd
6268 )";
6269
6270 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_5);
6271 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_5));
6272 EXPECT_THAT(
6273 getDiagnosticString(),
6274 HasSubstr(
6275 "Cannot use a pointer in the PhysicalStorageBuffer storage class"));
6276 }
6277
6278 TEST_F(ValidateMemory, VulkanInitializerWithWorkgroupStorageClassBad) {
6279 std::string spirv = R"(
6280 OpCapability Shader
6281 OpCapability VulkanMemoryModelKHR
6282 OpExtension "SPV_KHR_vulkan_memory_model"
6283 OpMemoryModel Logical VulkanKHR
6284 OpEntryPoint Fragment %func "func"
6285 OpExecutionMode %func OriginUpperLeft
6286 %float = OpTypeFloat 32
6287 %float_ptr = OpTypePointer Workgroup %float
6288 %init_val = OpConstant %float 1.0
6289 %1 = OpVariable %float_ptr Workgroup %init_val
6290 %void = OpTypeVoid
6291 %functy = OpTypeFunction %void
6292 %func = OpFunction %void None %functy
6293 %2 = OpLabel
6294 OpReturn
6295 OpFunctionEnd
6296 )";
6297 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_3);
6298 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3));
6299 EXPECT_THAT(getDiagnosticString(),
6300 AnyVUID(" VUID-StandaloneSpirv-OpVariable-04734"));
6301 EXPECT_THAT(getDiagnosticString(),
6302 HasSubstr("OpVariable, <id> '5[%5]', initializers are limited to "
6303 "OpConstantNull in Workgroup storage class"));
6304 }
6305
6306 TEST_F(ValidateMemory, VulkanInitializerWithWorkgroupStorageClassGood) {
6307 std::string spirv = R"(
6308 OpCapability Shader
6309 OpCapability VulkanMemoryModelKHR
6310 OpExtension "SPV_KHR_vulkan_memory_model"
6311 OpMemoryModel Logical VulkanKHR
6312 OpEntryPoint Fragment %func "func"
6313 OpExecutionMode %func OriginUpperLeft
6314 %float = OpTypeFloat 32
6315 %float_ptr = OpTypePointer Workgroup %float
6316 %init_val = OpConstantNull %float
6317 %1 = OpVariable %float_ptr Workgroup %init_val
6318 %void = OpTypeVoid
6319 %functy = OpTypeFunction %void
6320 %func = OpFunction %void None %functy
6321 %2 = OpLabel
6322 OpReturn
6323 OpFunctionEnd
6324 )";
6325 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_3);
6326 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3));
6327 }
6328
6329 TEST_F(ValidateMemory, LoadRuntimeArray) {
6330 const std::string spirv = R"(
6331 OpCapability Shader
6332 OpExtension "SPV_KHR_storage_buffer_storage_class"
6333 OpMemoryModel Logical GLSL450
6334 OpEntryPoint GLCompute %main "main"
6335 %void = OpTypeVoid
6336 %int = OpTypeInt 32 0
6337 %int_0 = OpConstant %int 0
6338 %rta = OpTypeRuntimeArray %int
6339 %block = OpTypeStruct %rta
6340 %ptr_rta = OpTypePointer StorageBuffer %rta
6341 %ptr_block = OpTypePointer StorageBuffer %block
6342 %var = OpVariable %ptr_block StorageBuffer
6343 %void_fn = OpTypeFunction %void
6344 %main = OpFunction %void None %void_fn
6345 %entry = OpLabel
6346 %gep = OpAccessChain %ptr_rta %var %int_0
6347 %ld = OpLoad %rta %gep
6348 OpReturn
6349 OpFunctionEnd
6350 )";
6351
6352 CompileSuccessfully(spirv);
6353 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6354 EXPECT_THAT(getDiagnosticString(),
6355 HasSubstr("Cannot load a runtime-sized array"));
6356 }
6357
6358 TEST_F(ValidateMemory, LoadRuntimeArrayInStruct) {
6359 const std::string spirv = R"(
6360 OpCapability Shader
6361 OpExtension "SPV_KHR_storage_buffer_storage_class"
6362 OpMemoryModel Logical GLSL450
6363 OpEntryPoint GLCompute %main "main"
6364 %void = OpTypeVoid
6365 %int = OpTypeInt 32 0
6366 %int_0 = OpConstant %int 0
6367 %rta = OpTypeRuntimeArray %int
6368 %block = OpTypeStruct %rta
6369 %ptr_rta = OpTypePointer StorageBuffer %rta
6370 %ptr_block = OpTypePointer StorageBuffer %block
6371 %var = OpVariable %ptr_block StorageBuffer
6372 %void_fn = OpTypeFunction %void
6373 %main = OpFunction %void None %void_fn
6374 %entry = OpLabel
6375 %ld = OpLoad %block %var
6376 OpReturn
6377 OpFunctionEnd
6378 )";
6379
6380 CompileSuccessfully(spirv);
6381 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6382 EXPECT_THAT(getDiagnosticString(),
6383 HasSubstr("Cannot load a runtime-sized array"));
6384 }
6385
6386 TEST_F(ValidateMemory, LoadRuntimeArrayInArray) {
6387 const std::string spirv = R"(
6388 OpCapability Shader
6389 OpExtension "SPV_KHR_storage_buffer_storage_class"
6390 OpMemoryModel Logical GLSL450
6391 OpEntryPoint GLCompute %main "main"
6392 %void = OpTypeVoid
6393 %int = OpTypeInt 32 0
6394 %int_0 = OpConstant %int 0
6395 %int_4 = OpConstant %int 4
6396 %rta = OpTypeRuntimeArray %int
6397 %block = OpTypeStruct %rta
6398 %array = OpTypeArray %block %int_4
6399 %ptr_rta = OpTypePointer StorageBuffer %rta
6400 %ptr_block = OpTypePointer StorageBuffer %block
6401 %ptr_array = OpTypePointer StorageBuffer %array
6402 %var = OpVariable %ptr_array StorageBuffer
6403 %void_fn = OpTypeFunction %void
6404 %main = OpFunction %void None %void_fn
6405 %entry = OpLabel
6406 %ld = OpLoad %array %var
6407 OpReturn
6408 OpFunctionEnd
6409 )";
6410
6411 CompileSuccessfully(spirv);
6412 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6413 EXPECT_THAT(getDiagnosticString(),
6414 HasSubstr("Cannot load a runtime-sized array"));
6415 }
6416
6417 TEST_F(ValidateMemory, Pre1p4WorkgroupMemoryBadLayoutOk) {
6418 const std::string spirv = R"(
6419 OpCapability Shader
6420 OpMemoryModel Logical GLSL450
6421 OpEntryPoint GLCompute %main "main"
6422 OpDecorate %struct Block
6423 OpMemberDecorate %struct 0 Offset 0
6424 %void = OpTypeVoid
6425 %bool = OpTypeBool
6426 %struct = OpTypeStruct %bool
6427 %ptr = OpTypePointer Workgroup %struct
6428 %var = OpVariable %ptr Workgroup
6429 %void_fn = OpTypeFunction %void
6430 %main = OpFunction %void None %void_fn
6431 %entry = OpLabel
6432 OpReturn
6433 OpFunctionEnd
6434 )";
6435
6436 CompileSuccessfully(spirv);
6437 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6438 }
6439
6440 TEST_F(ValidateMemory, UntypedVariableGood) {
6441 const std::string spirv = R"(
6442 OpCapability Shader
6443 OpCapability Linkage
6444 OpCapability UntypedPointersKHR
6445 OpExtension "SPV_KHR_untyped_pointers"
6446 OpMemoryModel Logical GLSL450
6447 %int = OpTypeInt 32 0
6448 %int_0 = OpConstant %int 0
6449 %ptr = OpTypeUntypedPointerKHR Private
6450 %var = OpUntypedVariableKHR %ptr Private %int %int_0
6451 )";
6452
6453 CompileSuccessfully(spirv);
6454 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6455 }
6456
6457 TEST_F(ValidateMemory, UntypedVariableNoDataType) {
6458 const std::string spirv = R"(
6459 OpCapability Shader
6460 OpCapability Linkage
6461 OpCapability UntypedPointersKHR
6462 OpExtension "SPV_KHR_untyped_pointers"
6463 OpExtension "SPV_KHR_storage_buffer_storage_class"
6464 OpMemoryModel Logical GLSL450
6465 %int = OpTypeInt 32 0
6466 %int_0 = OpConstant %int 0
6467 %ptr = OpTypeUntypedPointerKHR StorageBuffer
6468 %var = OpUntypedVariableKHR %ptr StorageBuffer
6469 )";
6470
6471 CompileSuccessfully(spirv);
6472 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6473 }
6474
6475 TEST_F(ValidateMemory, UntypedVariableNoDataTypeFunction) {
6476 const std::string spirv = R"(
6477 OpCapability Shader
6478 OpCapability Linkage
6479 OpCapability UntypedPointersKHR
6480 OpExtension "SPV_KHR_untyped_pointers"
6481 OpMemoryModel Logical GLSL450
6482 %int = OpTypeInt 32 0
6483 %int_0 = OpConstant %int 0
6484 %ptr = OpTypeUntypedPointerKHR Function
6485 %var = OpUntypedVariableKHR %ptr Function
6486 )";
6487
6488 CompileSuccessfully(spirv);
6489 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6490 EXPECT_THAT(getDiagnosticString(),
6491 HasSubstr("Data type must be specified for Function, "
6492 "Private, and Workgroup storage classes"));
6493 }
6494
6495 TEST_F(ValidateMemory, UntypedVariableNoDataTypePrivate) {
6496 const std::string spirv = R"(
6497 OpCapability Shader
6498 OpCapability Linkage
6499 OpCapability UntypedPointersKHR
6500 OpExtension "SPV_KHR_untyped_pointers"
6501 OpMemoryModel Logical GLSL450
6502 %int = OpTypeInt 32 0
6503 %int_0 = OpConstant %int 0
6504 %ptr = OpTypeUntypedPointerKHR Private
6505 %var = OpUntypedVariableKHR %ptr Private
6506 )";
6507
6508 CompileSuccessfully(spirv);
6509 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6510 EXPECT_THAT(getDiagnosticString(),
6511 HasSubstr("Data type must be specified for Function, "
6512 "Private, and Workgroup storage classes"));
6513 }
6514
6515 TEST_F(ValidateMemory, UntypedVariableNoDataTypeWorkgroup) {
6516 const std::string spirv = R"(
6517 OpCapability Shader
6518 OpCapability Linkage
6519 OpCapability UntypedPointersKHR
6520 OpExtension "SPV_KHR_untyped_pointers"
6521 OpMemoryModel Logical GLSL450
6522 %int = OpTypeInt 32 0
6523 %int_0 = OpConstant %int 0
6524 %ptr = OpTypeUntypedPointerKHR Workgroup
6525 %var = OpUntypedVariableKHR %ptr Workgroup
6526 )";
6527
6528 CompileSuccessfully(spirv);
6529 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6530 EXPECT_THAT(getDiagnosticString(),
6531 HasSubstr("Data type must be specified for Function, "
6532 "Private, and Workgroup storage classes"));
6533 }
6534
6535 TEST_F(ValidateMemory, UntypedVariableNoDataTypeVulkan) {
6536 const std::string spirv = R"(
6537 OpCapability Shader
6538 OpCapability UntypedPointersKHR
6539 OpExtension "SPV_KHR_untyped_pointers"
6540 OpExtension "SPV_KHR_storage_buffer_storage_class"
6541 OpMemoryModel Logical GLSL450
6542 OpEntryPoint GLCompute %main "main"
6543 OpExecutionMode %main LocalSize 1 1 1
6544 %int = OpTypeInt 32 0
6545 %int_0 = OpConstant %int 0
6546 %ptr = OpTypeUntypedPointerKHR StorageBuffer
6547 %var = OpUntypedVariableKHR %ptr StorageBuffer
6548 %void = OpTypeVoid
6549 %void_fn = OpTypeFunction %void
6550 %main = OpFunction %void None %void_fn
6551 %entry = OpLabel
6552 OpReturn
6553 OpFunctionEnd
6554 )";
6555
6556 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
6557 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
6558 EXPECT_THAT(getDiagnosticString(),
6559 AnyVUID("VUID-StandaloneSpirv-OpUntypedVariableKHR-11167"));
6560 EXPECT_THAT(
6561 getDiagnosticString(),
6562 HasSubstr(
6563 "Storage class is StorageBuffer, but Vulkan requires that Data Type "
6564 "be specified when not using UniformConstant storage class"));
6565 }
6566
6567 TEST_F(ValidateMemory, UntypedVariableNoDataTypeNonHeapVulkan) {
6568 const std::string spirv = R"(
6569 OpCapability Shader
6570 OpCapability DescriptorHeapEXT
6571 OpCapability UntypedPointersKHR
6572 OpExtension "SPV_EXT_descriptor_heap"
6573 OpExtension "SPV_KHR_untyped_pointers"
6574 OpExtension "SPV_KHR_storage_buffer_storage_class"
6575 OpMemoryModel Logical GLSL450
6576 OpEntryPoint GLCompute %main "main"
6577 OpExecutionMode %main LocalSize 1 1 1
6578 %int = OpTypeInt 32 0
6579 %int_0 = OpConstant %int 0
6580 %ptr = OpTypeUntypedPointerKHR UniformConstant
6581 %var = OpUntypedVariableKHR %ptr UniformConstant
6582 %void = OpTypeVoid
6583 %void_fn = OpTypeFunction %void
6584 %main = OpFunction %void None %void_fn
6585 %entry = OpLabel
6586 OpReturn
6587 OpFunctionEnd
6588 )";
6589
6590 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
6591 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
6592 EXPECT_THAT(getDiagnosticString(),
6593 AnyVUID("VUID-StandaloneSpirv-OpUntypedVariableKHR-11347"));
6594 EXPECT_THAT(getDiagnosticString(),
6595 HasSubstr("Storage class is UniformConstant, but Vulkan requires "
6596 "that Data Type be specified if the variable is not "
6597 "decorated with SamplerHeapEXT or ResourceHeapEXT"));
6598 }
6599
6600 TEST_F(ValidateMemory, PtrAccessChainArrayStrideBad) {
6601 const std::string spirv = R"(
6602 OpCapability Shader
6603 OpCapability VariablePointersStorageBuffer
6604 OpExtension "SPV_KHR_storage_buffer_storage_class"
6605 OpExtension "SPV_KHR_variable_pointers"
6606 OpMemoryModel Logical GLSL450
6607 OpEntryPoint GLCompute %main "foo" %var
6608 OpExecutionMode %main LocalSize 1 1 1
6609 OpDecorate %var DescriptorSet 0
6610 OpDecorate %var Binding 0
6611 %uint = OpTypeInt 32 0
6612 %uint_0 = OpConstant %uint 0
6613 %uint_1 = OpConstant %uint 1
6614 %ptr = OpTypePointer StorageBuffer %uint
6615 %void = OpTypeVoid
6616 %func = OpTypeFunction %void
6617 %var = OpVariable %ptr StorageBuffer
6618 %main = OpFunction %void None %func
6619 %label = OpLabel
6620 %access = OpAccessChain %ptr %var
6621 %ptr_access = OpPtrAccessChain %ptr %access %uint_1
6622 OpReturn
6623 OpFunctionEnd
6624 )";
6625
6626 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_5);
6627 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
6628 ValidateInstructions(SPV_ENV_UNIVERSAL_1_5));
6629 EXPECT_THAT(getDiagnosticString(),
6630 HasSubstr("OpPtrAccessChain must have a Base whose type is "
6631 "decorated with ArrayStride"));
6632 }
6633
6634 TEST_F(ValidateMemory, PtrAccessChainArrayStrideSuccess) {
6635 const std::string spirv = R"(
6636 OpCapability Shader
6637 OpCapability VariablePointersStorageBuffer
6638 OpExtension "SPV_KHR_storage_buffer_storage_class"
6639 OpExtension "SPV_KHR_variable_pointers"
6640 OpMemoryModel Logical GLSL450
6641 OpEntryPoint GLCompute %main "foo" %var
6642 OpExecutionMode %main LocalSize 1 1 1
6643 OpDecorate %var DescriptorSet 0
6644 OpDecorate %var Binding 00
6645 OpDecorate %ptr ArrayStride 4
6646 %uint = OpTypeInt 32 0
6647 %uint_0 = OpConstant %uint 0
6648 %uint_1 = OpConstant %uint 1
6649 %ptr = OpTypePointer StorageBuffer %uint
6650 %void = OpTypeVoid
6651 %func = OpTypeFunction %void
6652 %var = OpVariable %ptr StorageBuffer
6653 %main = OpFunction %void None %func
6654 %label = OpLabel
6655 %access = OpAccessChain %ptr %var
6656 %ptr_access = OpPtrAccessChain %ptr %access %uint_1
6657 OpReturn
6658 OpFunctionEnd
6659 )";
6660
6661 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_5);
6662 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_5));
6663 }
6664
6665 TEST_F(ValidateMemory, VulkanPtrAccessChainStorageBufferSuccess) {
6666 const std::string spirv = R"(
6667 OpCapability Shader
6668 OpCapability VariablePointersStorageBuffer
6669 OpExtension "SPV_KHR_storage_buffer_storage_class"
6670 OpExtension "SPV_KHR_variable_pointers"
6671 OpMemoryModel Logical GLSL450
6672 OpEntryPoint GLCompute %main "foo" %var
6673 OpExecutionMode %main LocalSize 1 1 1
6674 OpDecorate %_runtimearr_uint ArrayStride 4
6675 OpMemberDecorate %_struct_10 0 Offset 0
6676 OpDecorate %_struct_10 Block
6677 OpDecorate %var DescriptorSet 0
6678 OpDecorate %var Binding 0
6679 OpDecorate %_ptr_StorageBuffer_uint ArrayStride 4
6680 %uint = OpTypeInt 32 0
6681 %uint_0 = OpConstant %uint 0
6682 %uint_1 = OpConstant %uint 1
6683 %_runtimearr_uint = OpTypeRuntimeArray %uint
6684 %_struct_10 = OpTypeStruct %_runtimearr_uint
6685 %_ptr_StorageBuffer__struct_10 = OpTypePointer StorageBuffer %_struct_10
6686 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
6687 %void = OpTypeVoid
6688 %func2 = OpTypeFunction %void %_ptr_StorageBuffer_uint
6689 %func1 = OpTypeFunction %void
6690 %var = OpVariable %_ptr_StorageBuffer__struct_10 StorageBuffer
6691 %called = OpFunction %void None %func2
6692 %param = OpFunctionParameter %_ptr_StorageBuffer_uint
6693 %label2 = OpLabel
6694 %ptr_access = OpPtrAccessChain %_ptr_StorageBuffer_uint %param %uint_1
6695 OpReturn
6696 OpFunctionEnd
6697 %main = OpFunction %void None %func1
6698 %label1 = OpLabel
6699 %access = OpAccessChain %_ptr_StorageBuffer_uint %var %uint_0 %uint_0
6700 %call = OpFunctionCall %void %called %access
6701 OpReturn
6702 OpFunctionEnd
6703 )";
6704
6705 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_2);
6706 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
6707 }
6708
6709 TEST_F(ValidateMemory, VulkanPtrAccessChainStorageBufferCapability) {
6710 const std::string spirv = R"(
6711 OpCapability Shader
6712 OpCapability PhysicalStorageBufferAddresses
6713 OpExtension "SPV_KHR_storage_buffer_storage_class"
6714 OpExtension "SPV_KHR_variable_pointers"
6715 OpMemoryModel PhysicalStorageBuffer64 GLSL450
6716 OpEntryPoint GLCompute %main "foo" %var
6717 OpExecutionMode %main LocalSize 1 1 1
6718 OpDecorate %_runtimearr_uint ArrayStride 4
6719 OpMemberDecorate %_struct_10 0 Offset 0
6720 OpDecorate %_struct_10 Block
6721 OpDecorate %var DescriptorSet 0
6722 OpDecorate %var Binding 0
6723 OpDecorate %_ptr_StorageBuffer_uint ArrayStride 4
6724 %uint = OpTypeInt 32 0
6725 %uint_0 = OpConstant %uint 0
6726 %uint_1 = OpConstant %uint 1
6727 %_runtimearr_uint = OpTypeRuntimeArray %uint
6728 %_struct_10 = OpTypeStruct %_runtimearr_uint
6729 %_ptr_StorageBuffer__struct_10 = OpTypePointer StorageBuffer %_struct_10
6730 %_ptr_StorageBuffer_uint = OpTypePointer StorageBuffer %uint
6731 %void = OpTypeVoid
6732 %func = OpTypeFunction %void
6733 %var = OpVariable %_ptr_StorageBuffer__struct_10 StorageBuffer
6734 %main = OpFunction %void None %func
6735 %label = OpLabel
6736 %access = OpAccessChain %_ptr_StorageBuffer_uint %var %uint_0 %uint_0
6737 %ptr_access = OpPtrAccessChain %_ptr_StorageBuffer_uint %access %uint_1
6738 OpReturn
6739 OpFunctionEnd
6740 )";
6741
6742 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_2);
6743 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_2));
6744 EXPECT_THAT(getDiagnosticString(),
6745 AnyVUID("VUID-StandaloneSpirv-Base-07652"));
6746 EXPECT_THAT(getDiagnosticString(),
6747 HasSubstr("OpPtrAccessChain Base operand pointing to "
6748 "StorageBuffer storage class must use VariablePointers "
6749 "or VariablePointersStorageBuffer capability"));
6750 }
6751
6752 TEST_F(ValidateMemory, VulkanPtrAccessChainWorkgroupCapability) {
6753 const std::string spirv = R"(
6754 OpCapability Shader
6755 OpCapability VariablePointersStorageBuffer
6756 OpExtension "SPV_KHR_storage_buffer_storage_class"
6757 OpExtension "SPV_KHR_variable_pointers"
6758 OpMemoryModel Logical GLSL450
6759 OpEntryPoint GLCompute %main "foo" %var
6760 OpExecutionMode %main LocalSize 1 1 1
6761 OpDecorate %_ptr_Workgroup_uint ArrayStride 4
6762 %uint = OpTypeInt 32 0
6763 %uint_0 = OpConstant %uint 0
6764 %uint_1 = OpConstant %uint 1
6765 %_arr_uint = OpTypeArray %uint %uint_1
6766 %_ptr_Workgroup__arr_uint = OpTypePointer Workgroup %_arr_uint
6767 %_ptr_Workgroup_uint = OpTypePointer Workgroup %uint
6768 %void = OpTypeVoid
6769 %func = OpTypeFunction %void
6770 %var = OpVariable %_ptr_Workgroup__arr_uint Workgroup
6771 %main = OpFunction %void None %func
6772 %label = OpLabel
6773 %access = OpAccessChain %_ptr_Workgroup_uint %var %uint_0
6774 %ptr_access = OpPtrAccessChain %_ptr_Workgroup_uint %access %uint_1
6775 OpReturn
6776 OpFunctionEnd
6777 )";
6778
6779 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_2);
6780 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_2));
6781 EXPECT_THAT(getDiagnosticString(),
6782 AnyVUID("VUID-StandaloneSpirv-Base-07651"));
6783 EXPECT_THAT(getDiagnosticString(),
6784 HasSubstr("OpPtrAccessChain Base operand pointing to Workgroup "
6785 "storage class must use VariablePointers capability"));
6786 }
6787
6788 TEST_F(ValidateMemory, VulkanPtrAccessChainWorkgroupNoArrayStrideSuccess) {
6789 const std::string spirv = R"(
6790 OpCapability Shader
6791 OpCapability VariablePointers
6792 OpExtension "SPV_KHR_storage_buffer_storage_class"
6793 OpExtension "SPV_KHR_variable_pointers"
6794 OpMemoryModel Logical GLSL450
6795 OpEntryPoint GLCompute %main "foo" %var
6796 OpExecutionMode %main LocalSize 1 1 1
6797 %uint = OpTypeInt 32 0
6798 %uint_0 = OpConstant %uint 0
6799 %uint_1 = OpConstant %uint 1
6800 %_arr_uint = OpTypeArray %uint %uint_1
6801 %_ptr_Workgroup__arr_uint = OpTypePointer Workgroup %_arr_uint
6802 %_ptr_Workgroup_uint = OpTypePointer Workgroup %uint
6803 %void = OpTypeVoid
6804 %func = OpTypeFunction %void
6805 %var = OpVariable %_ptr_Workgroup__arr_uint Workgroup
6806 %main = OpFunction %void None %func
6807 %label = OpLabel
6808 %access = OpAccessChain %_ptr_Workgroup_uint %var %uint_0
6809 %ptr_access = OpPtrAccessChain %_ptr_Workgroup_uint %access %uint_1
6810 OpReturn
6811 OpFunctionEnd
6812 )";
6813
6814 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_2);
6815 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_2));
6816 }
6817
6818 TEST_F(ValidateMemory, AccessChainNegativeStructIndex32) {
6819 const std::string spirv = R"(
6820 OpCapability Shader
6821 OpCapability Linkage
6822 OpMemoryModel Logical GLSL450
6823 %void = OpTypeVoid
6824 %void_fn = OpTypeFunction %void
6825 %int = OpTypeInt 32 1
6826 %_struct_4 = OpTypeStruct %int %int %int
6827 %_ptr_Function__struct_4 = OpTypePointer Function %_struct_4
6828 %_ptr_Function_int = OpTypePointer Function %int
6829 %int_n224 = OpConstant %int -224
6830 %fn = OpFunction %void Inline %void_fn
6831 %entry = OpLabel
6832 %var = OpVariable %_ptr_Function__struct_4 Function
6833 %gep = OpInBoundsAccessChain %_ptr_Function_int %var %int_n224
6834 OpReturn
6835 OpFunctionEnd
6836 )";
6837
6838 CompileSuccessfully(spirv);
6839 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6840 EXPECT_THAT(getDiagnosticString(),
6841 HasSubstr("Index at word 4 may not have a negative value"));
6842 }
6843
6844 TEST_F(ValidateMemory, AccessChainNegativeStructIndex64) {
6845 const std::string spirv = R"(
6846 OpCapability Shader
6847 OpCapability Linkage
6848 OpCapability Int64
6849 OpMemoryModel Logical GLSL450
6850 %void = OpTypeVoid
6851 %void_fn = OpTypeFunction %void
6852 %int = OpTypeInt 32 1
6853 %long = OpTypeInt 64 1
6854 %_struct_4 = OpTypeStruct %int %int %int
6855 %_ptr_Function__struct_4 = OpTypePointer Function %_struct_4
6856 %_ptr_Function_int = OpTypePointer Function %int
6857 %long_n224 = OpConstant %long -224
6858 %fn = OpFunction %void Inline %void_fn
6859 %entry = OpLabel
6860 %var = OpVariable %_ptr_Function__struct_4 Function
6861 %gep = OpInBoundsAccessChain %_ptr_Function_int %var %long_n224
6862 OpReturn
6863 OpFunctionEnd
6864 )";
6865
6866 CompileSuccessfully(spirv);
6867 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6868 EXPECT_THAT(getDiagnosticString(),
6869 HasSubstr("Index at word 4 may not have a negative value"));
6870 }
6871
6872 TEST_F(ValidateMemory, UntypedVariableFunctionOutsideFunction) {
6873 const std::string spirv = R"(
6874 OpCapability Shader
6875 OpCapability Linkage
6876 OpCapability UntypedPointersKHR
6877 OpExtension "SPV_KHR_untyped_pointers"
6878 OpMemoryModel Logical GLSL450
6879 %int = OpTypeInt 32 0
6880 %int_0 = OpConstant %int 0
6881 %ptr = OpTypeUntypedPointerKHR Function
6882 %var = OpUntypedVariableKHR %ptr Function %int
6883 )";
6884
6885 CompileSuccessfully(spirv);
6886 EXPECT_EQ(SPV_ERROR_INVALID_LAYOUT, ValidateInstructions());
6887 EXPECT_THAT(getDiagnosticString(),
6888 HasSubstr("Variables can not have a function[7] storage class "
6889 "outside of a function"));
6890 }
6891
6892 TEST_F(ValidateMemory, UntypedVariableBadResultType) {
6893 const std::string spirv = R"(
6894 OpCapability Shader
6895 OpCapability Linkage
6896 OpCapability UntypedPointersKHR
6897 OpExtension "SPV_KHR_untyped_pointers"
6898 OpMemoryModel Logical GLSL450
6899 %int = OpTypeInt 32 0
6900 %int_0 = OpConstant %int 0
6901 %ptr = OpTypeUntypedPointerKHR Workgroup
6902 %var = OpUntypedVariableKHR %int Workgroup %int
6903 )";
6904
6905 CompileSuccessfully(spirv);
6906 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6907 EXPECT_THAT(getDiagnosticString(),
6908 HasSubstr("Result type must be an untyped pointer"));
6909 }
6910
6911 TEST_F(ValidateMemory, UntypedVariableBadDataType) {
6912 const std::string spirv = R"(
6913 OpCapability Shader
6914 OpCapability Linkage
6915 OpCapability UntypedPointersKHR
6916 OpExtension "SPV_KHR_untyped_pointers"
6917 OpMemoryModel Logical GLSL450
6918 %int = OpTypeInt 32 0
6919 %int_0 = OpConstant %int 0
6920 %ptr = OpTypeUntypedPointerKHR Workgroup
6921 %var = OpUntypedVariableKHR %ptr Workgroup %int_0
6922 )";
6923
6924 CompileSuccessfully(spirv);
6925 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6926 EXPECT_THAT(getDiagnosticString(),
6927 HasSubstr("Data type must be a type instruction"));
6928 }
6929
6930 TEST_F(ValidateMemory, UntypedVariableBadStorageClass) {
6931 const std::string spirv = R"(
6932 OpCapability Kernel
6933 OpCapability GenericPointer
6934 OpCapability Linkage
6935 OpCapability UntypedPointersKHR
6936 OpExtension "SPV_KHR_untyped_pointers"
6937 OpMemoryModel Logical OpenCL
6938 %int = OpTypeInt 32 0
6939 %int_0 = OpConstant %int 0
6940 %ptr = OpTypeUntypedPointerKHR Generic
6941 %var = OpUntypedVariableKHR %ptr Generic %int
6942 )";
6943
6944 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_2);
6945 EXPECT_EQ(SPV_ERROR_INVALID_BINARY,
6946 ValidateInstructions(SPV_ENV_UNIVERSAL_1_2));
6947 EXPECT_THAT(getDiagnosticString(),
6948 HasSubstr("Variable storage class cannot be Generic"));
6949 }
6950
6951 TEST_F(ValidateMemory, UntypedVariableMismatchedStorageClass) {
6952 const std::string spirv = R"(
6953 OpCapability Shader
6954 OpCapability Linkage
6955 OpCapability UntypedPointersKHR
6956 OpExtension "SPV_KHR_untyped_pointers"
6957 OpMemoryModel Logical GLSL450
6958 %int = OpTypeInt 32 0
6959 %int_0 = OpConstant %int 0
6960 %ptr = OpTypeUntypedPointerKHR Workgroup
6961 %var = OpUntypedVariableKHR %ptr Private %int
6962 )";
6963
6964 CompileSuccessfully(spirv);
6965 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6966 EXPECT_THAT(getDiagnosticString(),
6967 HasSubstr("Storage class must match result type storage class"));
6968 }
6969
6970 TEST_F(ValidateMemory, UntypedVariableBadInitializer) {
6971 const std::string spirv = R"(
6972 OpCapability Shader
6973 OpCapability Linkage
6974 OpCapability UntypedPointersKHR
6975 OpExtension "SPV_KHR_untyped_pointers"
6976 OpMemoryModel Logical GLSL450
6977 %int = OpTypeInt 32 0
6978 %float = OpTypeFloat 32
6979 %float_0 = OpConstant %float 0
6980 %ptr = OpTypeUntypedPointerKHR Private
6981 %var = OpUntypedVariableKHR %ptr Private %int %float_0
6982 )";
6983
6984 CompileSuccessfully(spirv);
6985 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6986 EXPECT_THAT(getDiagnosticString(),
6987 HasSubstr("Initializer type must match the data type"));
6988 }
6989
6990 TEST_F(ValidateMemory, AccessChainBaseUntypedPointer) {
6991 const std::string spirv = R"(
6992 OpCapability Shader
6993 OpCapability UntypedPointersKHR
6994 OpExtension "SPV_KHR_storage_buffer_storage_class"
6995 OpExtension "SPV_KHR_untyped_pointers"
6996 OpMemoryModel Logical GLSL450
6997 OpEntryPoint GLCompute %main "main"
6998 OpName %var "var"
6999 %void = OpTypeVoid
7000 %int = OpTypeInt 32 0
7001 %int_0 = OpConstant %int 0
7002 %block = OpTypeStruct %int
7003 %ptr_ssbo = OpTypePointer StorageBuffer %block
7004 %ptr_ssbo_int = OpTypePointer StorageBuffer %int
7005 %ptr = OpTypeUntypedPointerKHR StorageBuffer
7006 %var = OpUntypedVariableKHR %ptr StorageBuffer %int
7007 %void_fn = OpTypeFunction %void
7008 %main = OpFunction %void None %void_fn
7009 %entry = OpLabel
7010 %gep = OpAccessChain %ptr_ssbo_int %var %int_0
7011 OpReturn
7012 OpFunctionEnd
7013 )";
7014
7015 CompileSuccessfully(spirv);
7016 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
7017 EXPECT_THAT(getDiagnosticString(),
7018 HasSubstr("The Base <id> '2[%var]' in OpAccessChain "
7019 "instruction must be a pointer"));
7020 }
7021
7022 using ValidateMemoryUntypedAccessChain = spvtest::ValidateBase<std::string>;
7023
7024 TEST_P(ValidateMemoryUntypedAccessChain, GoodTypedPointerBase) {
7025 const std::string opcode = GetParam();
7026 const bool ptr = opcode == "OpUntypedPtrAccessChainKHR" ||
7027 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7028 const std::string extra_param = ptr ? "%int_0" : "";
7029 const std::string deco = ptr ? "OpDecorate %ptr_ssbo ArrayStride 4" : "";
7030
7031 const std::string spirv = R"(
7032 OpCapability Shader
7033 OpCapability UntypedPointersKHR
7034 OpCapability VariablePointers
7035 OpExtension "SPV_KHR_variable_pointers"
7036 OpExtension "SPV_KHR_storage_buffer_storage_class"
7037 OpExtension "SPV_KHR_untyped_pointers"
7038 OpMemoryModel Logical GLSL450
7039 OpEntryPoint GLCompute %main "main"
7040 )" + deco + R"(
7041 %void = OpTypeVoid
7042 %int = OpTypeInt 32 0
7043 %int_0 = OpConstant %int 0
7044 %block = OpTypeStruct %int
7045 %ptr_ssbo = OpTypePointer StorageBuffer %block
7046 %var = OpVariable %ptr_ssbo StorageBuffer
7047 %ptr = OpTypeUntypedPointerKHR StorageBuffer
7048 %void_fn = OpTypeFunction %void
7049 %main = OpFunction %void None %void_fn
7050 %entry = OpLabel
7051 %gep = )" + opcode + R"( %ptr %block %var )" +
7052 extra_param + R"( %int_0
7053 OpReturn
7054 OpFunctionEnd
7055 )";
7056
7057 getValidatorOptions()->relax_logical_pointer =
7058 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7059 CompileSuccessfully(spirv);
7060 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
7061 }
7062
7063 TEST_P(ValidateMemoryUntypedAccessChain, GoodUntypedPointerBase) {
7064 const std::string opcode = GetParam();
7065 const bool ptr = opcode == "OpUntypedPtrAccessChainKHR" ||
7066 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7067 const std::string extra_param = ptr ? "%int_0" : "";
7068 const std::string deco = ptr ? "OpDecorate %ptr ArrayStride 4" : "";
7069
7070 const std::string spirv = R"(
7071 OpCapability Shader
7072 OpCapability UntypedPointersKHR
7073 OpCapability VariablePointers
7074 OpExtension "SPV_KHR_variable_pointers"
7075 OpExtension "SPV_KHR_storage_buffer_storage_class"
7076 OpExtension "SPV_KHR_untyped_pointers"
7077 OpMemoryModel Logical GLSL450
7078 OpEntryPoint GLCompute %main "main"
7079 )" + deco + R"(
7080 %void = OpTypeVoid
7081 %int = OpTypeInt 32 0
7082 %int_0 = OpConstant %int 0
7083 %block = OpTypeStruct %int
7084 %ptr_ssbo = OpTypePointer StorageBuffer %block
7085 %ptr = OpTypeUntypedPointerKHR StorageBuffer
7086 %var = OpUntypedVariableKHR %ptr StorageBuffer %int
7087 %void_fn = OpTypeFunction %void
7088 %main = OpFunction %void None %void_fn
7089 %entry = OpLabel
7090 %gep = )" + opcode + R"( %ptr %block %var )" +
7091 extra_param + R"( %int_0
7092 OpReturn
7093 OpFunctionEnd
7094 )";
7095
7096 getValidatorOptions()->relax_logical_pointer =
7097 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7098 CompileSuccessfully(spirv);
7099 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
7100 }
7101
7102 TEST_P(ValidateMemoryUntypedAccessChain, ResultTypedPointer) {
7103 const std::string opcode = GetParam();
7104 const bool ptr = opcode == "OpUntypedPtrAccessChainKHR" ||
7105 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7106 const std::string extra_param = ptr ? "%int_0" : "";
7107
7108 const std::string spirv = R"(
7109 OpCapability Shader
7110 OpCapability UntypedPointersKHR
7111 OpCapability VariablePointers
7112 OpExtension "SPV_KHR_variable_pointers"
7113 OpExtension "SPV_KHR_storage_buffer_storage_class"
7114 OpExtension "SPV_KHR_untyped_pointers"
7115 OpMemoryModel Logical GLSL450
7116 OpEntryPoint GLCompute %main "main"
7117 OpName %gep "gep"
7118 %void = OpTypeVoid
7119 %int = OpTypeInt 32 0
7120 %int_0 = OpConstant %int 0
7121 %block = OpTypeStruct %int
7122 %ptr_ssbo = OpTypePointer StorageBuffer %block
7123 %var = OpVariable %ptr_ssbo StorageBuffer
7124 %ptr = OpTypeUntypedPointerKHR StorageBuffer
7125 %ptr_int = OpTypePointer StorageBuffer %int
7126 %void_fn = OpTypeFunction %void
7127 %main = OpFunction %void None %void_fn
7128 %entry = OpLabel
7129 %gep = )" + opcode + R"( %ptr_int %block %var )" +
7130 extra_param + R"( %int_0
7131 OpReturn
7132 OpFunctionEnd
7133 )";
7134
7135 getValidatorOptions()->relax_logical_pointer =
7136 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7137 CompileSuccessfully(spirv);
7138 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
7139 EXPECT_THAT(getDiagnosticString(),
7140 HasSubstr("The Result Type of " + opcode +
7141 " <id> '2[%gep]' must be OpTypeUntypedPointer"));
7142 }
7143
7144 TEST_P(ValidateMemoryUntypedAccessChain, BaseTypeNotAType) {
7145 const std::string opcode = GetParam();
7146 const bool ptr = opcode == "OpUntypedPtrAccessChainKHR" ||
7147 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7148 const std::string extra_param = ptr ? "%int_0" : "";
7149
7150 const std::string spirv = R"(
7151 OpCapability Shader
7152 OpCapability UntypedPointersKHR
7153 OpCapability VariablePointers
7154 OpExtension "SPV_KHR_variable_pointers"
7155 OpExtension "SPV_KHR_storage_buffer_storage_class"
7156 OpExtension "SPV_KHR_untyped_pointers"
7157 OpMemoryModel Logical GLSL450
7158 OpEntryPoint GLCompute %main "main"
7159 OpName %gep "gep"
7160 %void = OpTypeVoid
7161 %int = OpTypeInt 32 0
7162 %int_0 = OpConstant %int 0
7163 %block = OpTypeStruct %int
7164 %ptr_ssbo = OpTypePointer StorageBuffer %block
7165 %var = OpVariable %ptr_ssbo StorageBuffer
7166 %ptr = OpTypeUntypedPointerKHR StorageBuffer
7167 %void_fn = OpTypeFunction %void
7168 %main = OpFunction %void None %void_fn
7169 %entry = OpLabel
7170 %gep = )" + opcode + R"( %ptr %int_0 %var )" +
7171 extra_param + R"( %int_0
7172 OpReturn
7173 OpFunctionEnd
7174 )";
7175
7176 getValidatorOptions()->relax_logical_pointer =
7177 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7178 CompileSuccessfully(spirv);
7179 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
7180 EXPECT_THAT(getDiagnosticString(),
7181 HasSubstr("Base type must be a non-pointer type"));
7182 }
7183
7184 TEST_P(ValidateMemoryUntypedAccessChain, BaseTypedPointer) {
7185 const std::string opcode = GetParam();
7186 const bool ptr = opcode == "OpUntypedPtrAccessChainKHR" ||
7187 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7188 const std::string extra_param = ptr ? "%int_0" : "";
7189
7190 const std::string spirv = R"(
7191 OpCapability Shader
7192 OpCapability UntypedPointersKHR
7193 OpCapability VariablePointers
7194 OpExtension "SPV_KHR_variable_pointers"
7195 OpExtension "SPV_KHR_storage_buffer_storage_class"
7196 OpExtension "SPV_KHR_untyped_pointers"
7197 OpMemoryModel Logical GLSL450
7198 OpEntryPoint GLCompute %main "main"
7199 OpName %gep "gep"
7200 %void = OpTypeVoid
7201 %int = OpTypeInt 32 0
7202 %int_0 = OpConstant %int 0
7203 %block = OpTypeStruct %int
7204 %ptr_ssbo = OpTypePointer StorageBuffer %block
7205 %var = OpVariable %ptr_ssbo StorageBuffer
7206 %ptr = OpTypeUntypedPointerKHR StorageBuffer
7207 %void_fn = OpTypeFunction %void
7208 %main = OpFunction %void None %void_fn
7209 %entry = OpLabel
7210 %gep = )" + opcode + R"( %ptr %ptr_ssbo %var )" +
7211 extra_param + R"( %int_0
7212 OpReturn
7213 OpFunctionEnd
7214 )";
7215
7216 getValidatorOptions()->relax_logical_pointer =
7217 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7218 CompileSuccessfully(spirv);
7219 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
7220 EXPECT_THAT(getDiagnosticString(),
7221 HasSubstr("Base type must be a non-pointer type"));
7222 }
7223
7224 TEST_P(ValidateMemoryUntypedAccessChain, BaseUntypedPointer) {
7225 const std::string opcode = GetParam();
7226 const bool ptr = opcode == "OpUntypedPtrAccessChainKHR" ||
7227 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7228 const std::string extra_param = ptr ? "%int_0" : "";
7229
7230 const std::string spirv = R"(
7231 OpCapability Shader
7232 OpCapability UntypedPointersKHR
7233 OpCapability VariablePointers
7234 OpExtension "SPV_KHR_variable_pointers"
7235 OpExtension "SPV_KHR_storage_buffer_storage_class"
7236 OpExtension "SPV_KHR_untyped_pointers"
7237 OpMemoryModel Logical GLSL450
7238 OpEntryPoint GLCompute %main "main"
7239 OpName %gep "gep"
7240 %void = OpTypeVoid
7241 %int = OpTypeInt 32 0
7242 %int_0 = OpConstant %int 0
7243 %block = OpTypeStruct %int
7244 %ptr_ssbo = OpTypePointer StorageBuffer %block
7245 %var = OpVariable %ptr_ssbo StorageBuffer
7246 %ptr = OpTypeUntypedPointerKHR StorageBuffer
7247 %void_fn = OpTypeFunction %void
7248 %main = OpFunction %void None %void_fn
7249 %entry = OpLabel
7250 %gep = )" + opcode + R"( %ptr %ptr %var )" +
7251 extra_param + R"( %int_0
7252 OpReturn
7253 OpFunctionEnd
7254 )";
7255
7256 getValidatorOptions()->relax_logical_pointer =
7257 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7258 CompileSuccessfully(spirv);
7259 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
7260 EXPECT_THAT(getDiagnosticString(),
7261 HasSubstr("Base type must be a non-pointer type"));
7262 }
7263
7264 TEST_P(ValidateMemoryUntypedAccessChain, BaseNotAPointer) {
7265 const std::string opcode = GetParam();
7266 const bool ptr = opcode == "OpUntypedPtrAccessChainKHR" ||
7267 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7268 const std::string extra_param = ptr ? "%int_0" : "";
7269
7270 const std::string spirv = R"(
7271 OpCapability Shader
7272 OpCapability UntypedPointersKHR
7273 OpCapability VariablePointers
7274 OpExtension "SPV_KHR_variable_pointers"
7275 OpExtension "SPV_KHR_storage_buffer_storage_class"
7276 OpExtension "SPV_KHR_untyped_pointers"
7277 OpMemoryModel Logical GLSL450
7278 OpEntryPoint GLCompute %main "main"
7279 OpName %int_0 "int_0"
7280 %void = OpTypeVoid
7281 %int = OpTypeInt 32 0
7282 %int_0 = OpConstant %int 0
7283 %block = OpTypeStruct %int
7284 %ptr_ssbo = OpTypePointer StorageBuffer %block
7285 %var = OpVariable %ptr_ssbo StorageBuffer
7286 %ptr = OpTypeUntypedPointerKHR StorageBuffer
7287 %void_fn = OpTypeFunction %void
7288 %main = OpFunction %void None %void_fn
7289 %entry = OpLabel
7290 %gep = )" + opcode + R"( %ptr %int %int_0 )" +
7291 extra_param + R"( %int_0
7292 OpReturn
7293 OpFunctionEnd
7294 )";
7295
7296 getValidatorOptions()->relax_logical_pointer =
7297 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7298 CompileSuccessfully(spirv);
7299 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
7300 EXPECT_THAT(getDiagnosticString(),
7301 HasSubstr("The Base <id> '2[%int_0]' in " + opcode +
7302 " instruction must be a pointer"));
7303 }
7304
7305 TEST_P(ValidateMemoryUntypedAccessChain, StorageClassMismatch) {
7306 const std::string opcode = GetParam();
7307 const bool ptr = opcode == "OpUntypedPtrAccessChainKHR" ||
7308 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7309 const std::string extra_param = ptr ? "%int_0" : "";
7310
7311 const std::string spirv = R"(
7312 OpCapability Shader
7313 OpCapability UntypedPointersKHR
7314 OpCapability VariablePointers
7315 OpExtension "SPV_KHR_variable_pointers"
7316 OpExtension "SPV_KHR_storage_buffer_storage_class"
7317 OpExtension "SPV_KHR_untyped_pointers"
7318 OpMemoryModel Logical GLSL450
7319 OpEntryPoint GLCompute %main "main"
7320 OpName %int_0 "int_0"
7321 %void = OpTypeVoid
7322 %int = OpTypeInt 32 0
7323 %int_0 = OpConstant %int 0
7324 %block = OpTypeStruct %int
7325 %ptr_wg = OpTypePointer Workgroup %block
7326 %var = OpVariable %ptr_wg Workgroup
7327 %ptr = OpTypeUntypedPointerKHR StorageBuffer
7328 %void_fn = OpTypeFunction %void
7329 %main = OpFunction %void None %void_fn
7330 %entry = OpLabel
7331 %gep = )" + opcode + R"( %ptr %block %var )" +
7332 extra_param + R"( %int_0
7333 OpReturn
7334 OpFunctionEnd
7335 )";
7336
7337 getValidatorOptions()->relax_logical_pointer =
7338 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7339 CompileSuccessfully(spirv);
7340 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
7341 EXPECT_THAT(
7342 getDiagnosticString(),
7343 HasSubstr("The result pointer storage class and base pointer storage "
7344 "class in " +
7345 opcode + " do not match"));
7346 }
7347
7348 TEST_P(ValidateMemoryUntypedAccessChain, NonCompositeBase) {
7349 const std::string opcode = GetParam();
7350 const bool ptr = opcode == "OpUntypedPtrAccessChainKHR" ||
7351 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7352 const std::string extra_param = ptr ? "%int_0" : "";
7353
7354 const std::string spirv = R"(
7355 OpCapability Shader
7356 OpCapability UntypedPointersKHR
7357 OpCapability VariablePointers
7358 OpExtension "SPV_KHR_variable_pointers"
7359 OpExtension "SPV_KHR_storage_buffer_storage_class"
7360 OpExtension "SPV_KHR_untyped_pointers"
7361 OpMemoryModel Logical GLSL450
7362 OpEntryPoint GLCompute %main "main"
7363 OpName %int_0 "int_0"
7364 %void = OpTypeVoid
7365 %int = OpTypeInt 32 0
7366 %int_0 = OpConstant %int 0
7367 %block = OpTypeStruct %int
7368 %ptr_wg = OpTypePointer StorageBuffer %block
7369 %var = OpVariable %ptr_wg StorageBuffer
7370 %ptr = OpTypeUntypedPointerKHR StorageBuffer
7371 %void_fn = OpTypeFunction %void
7372 %main = OpFunction %void None %void_fn
7373 %entry = OpLabel
7374 %gep = )" + opcode + R"( %ptr %int %var )" +
7375 extra_param + R"( %int_0
7376 OpReturn
7377 OpFunctionEnd
7378 )";
7379
7380 getValidatorOptions()->relax_logical_pointer =
7381 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7382 CompileSuccessfully(spirv);
7383 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
7384 EXPECT_THAT(getDiagnosticString(),
7385 HasSubstr(opcode + " reached non-composite type while indexes "
7386 "still remain to be traversed"));
7387 }
7388
7389 TEST_P(ValidateMemoryUntypedAccessChain, TooManyIndices) {
7390 const std::string opcode = GetParam();
7391 const bool ptr = opcode == "OpUntypedPtrAccessChainKHR" ||
7392 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7393 const std::string extra_param = ptr ? "%int_0" : "";
7394
7395 const std::string spirv = R"(
7396 OpCapability Shader
7397 OpCapability UntypedPointersKHR
7398 OpCapability VariablePointers
7399 OpExtension "SPV_KHR_variable_pointers"
7400 OpExtension "SPV_KHR_storage_buffer_storage_class"
7401 OpExtension "SPV_KHR_untyped_pointers"
7402 OpMemoryModel Logical GLSL450
7403 OpEntryPoint GLCompute %main "main"
7404 OpName %int_0 "int_0"
7405 %void = OpTypeVoid
7406 %int = OpTypeInt 32 0
7407 %int_0 = OpConstant %int 0
7408 %block = OpTypeStruct %int
7409 %ptr_wg = OpTypePointer StorageBuffer %block
7410 %var = OpVariable %ptr_wg StorageBuffer
7411 %ptr = OpTypeUntypedPointerKHR StorageBuffer
7412 %void_fn = OpTypeFunction %void
7413 %main = OpFunction %void None %void_fn
7414 %entry = OpLabel
7415 %gep = )" + opcode + R"( %ptr %block %var )" +
7416 extra_param + R"( %int_0 %int_0
7417 OpReturn
7418 OpFunctionEnd
7419 )";
7420
7421 getValidatorOptions()->relax_logical_pointer =
7422 opcode == "OpUntypedInBoundsPtrAccessChainKHR";
7423 CompileSuccessfully(spirv);
7424 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
7425 EXPECT_THAT(getDiagnosticString(),
7426 HasSubstr(opcode + " reached non-composite type while indexes "
7427 "still remain to be traversed"));
7428 }
7429
7430 INSTANTIATE_TEST_SUITE_P(
7431 ValidateUntypedAccessChains, ValidateMemoryUntypedAccessChain,
7432 Values("OpUntypedAccessChainKHR", "OpUntypedInBoundsAccessChainKHR",
7433 "OpUntypedPtrAccessChainKHR", "OpUntypedInBoundsPtrAccessChainKHR"));
7434
7435 TEST_F(ValidateMemory, LoadUntypedPointerGood) {
7436 const std::string spirv = R"(
7437 OpCapability Shader
7438 OpCapability UntypedPointersKHR
7439 OpCapability WorkgroupMemoryExplicitLayoutKHR
7440 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
7441 OpExtension "SPV_KHR_untyped_pointers"
7442 OpMemoryModel Logical GLSL450
7443 OpEntryPoint GLCompute %main "main" %var
7444 OpDecorate %struct Block
7445 OpMemberDecorate %struct 0 Offset 0
7446 %void = OpTypeVoid
7447 %int = OpTypeInt 32 0
7448 %float = OpTypeFloat 32
7449 %struct = OpTypeStruct %int
7450 %ptr = OpTypeUntypedPointerKHR Workgroup
7451 %var = OpUntypedVariableKHR %ptr Workgroup %struct
7452 %void_fn = OpTypeFunction %void
7453 %main = OpFunction %void None %void_fn
7454 %entry = OpLabel
7455 %load = OpLoad %float %var
7456 OpReturn
7457 OpFunctionEnd
7458 )";
7459
7460 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
7461 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
7462 }
7463
7464 TEST_F(ValidateMemory, StoreUntypedPointerGood) {
7465 const std::string spirv = R"(
7466 OpCapability Shader
7467 OpCapability UntypedPointersKHR
7468 OpCapability WorkgroupMemoryExplicitLayoutKHR
7469 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
7470 OpExtension "SPV_KHR_untyped_pointers"
7471 OpMemoryModel Logical GLSL450
7472 OpEntryPoint GLCompute %main "main" %var
7473 OpDecorate %struct Block
7474 OpMemberDecorate %struct 0 Offset 0
7475 %void = OpTypeVoid
7476 %int = OpTypeInt 32 0
7477 %float = OpTypeFloat 32
7478 %float_0 = OpConstant %float 0
7479 %struct = OpTypeStruct %int
7480 %ptr = OpTypeUntypedPointerKHR Workgroup
7481 %var = OpUntypedVariableKHR %ptr Workgroup %struct
7482 %void_fn = OpTypeFunction %void
7483 %main = OpFunction %void None %void_fn
7484 %entry = OpLabel
7485 OpStore %var %float_0
7486 OpReturn
7487 OpFunctionEnd
7488 )";
7489
7490 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
7491 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
7492 }
7493
7494 TEST_F(ValidateMemory, CopyMemoryUntypedPointerSourceGood) {
7495 const std::string spirv = R"(
7496 OpCapability Shader
7497 OpCapability UntypedPointersKHR
7498 OpCapability WorkgroupMemoryExplicitLayoutKHR
7499 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
7500 OpExtension "SPV_KHR_untyped_pointers"
7501 OpMemoryModel Logical GLSL450
7502 OpEntryPoint GLCompute %main "main" %var1 %var2
7503 OpName %var1 "var1"
7504 OpDecorate %struct Block
7505 OpMemberDecorate %struct 0 Offset 0
7506 %void = OpTypeVoid
7507 %int = OpTypeInt 32 0
7508 %struct = OpTypeStruct %int
7509 %ptr = OpTypeUntypedPointerKHR Workgroup
7510 %var1 = OpUntypedVariableKHR %ptr Workgroup %struct
7511 %ptr_wg = OpTypePointer Workgroup %int
7512 %var2 = OpVariable %ptr_wg Workgroup
7513 %void_fn = OpTypeFunction %void
7514 %main = OpFunction %void None %void_fn
7515 %entry = OpLabel
7516 OpCopyMemory %var2 %var1
7517 OpReturn
7518 OpFunctionEnd
7519 )";
7520
7521 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
7522 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
7523 }
7524
7525 TEST_F(ValidateMemory, CopyMemoryUntypedPointerTargetGood) {
7526 const std::string spirv = R"(
7527 OpCapability Shader
7528 OpCapability UntypedPointersKHR
7529 OpCapability WorkgroupMemoryExplicitLayoutKHR
7530 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
7531 OpExtension "SPV_KHR_untyped_pointers"
7532 OpMemoryModel Logical GLSL450
7533 OpEntryPoint GLCompute %main "main" %var1 %var2
7534 OpName %var1 "var1"
7535 OpDecorate %struct Block
7536 OpMemberDecorate %struct 0 Offset 0
7537 %void = OpTypeVoid
7538 %int = OpTypeInt 32 0
7539 %struct = OpTypeStruct %int
7540 %ptr = OpTypeUntypedPointerKHR Workgroup
7541 %var1 = OpUntypedVariableKHR %ptr Workgroup %struct
7542 %ptr_wg = OpTypePointer Workgroup %int
7543 %var2 = OpVariable %ptr_wg Workgroup
7544 %void_fn = OpTypeFunction %void
7545 %main = OpFunction %void None %void_fn
7546 %entry = OpLabel
7547 OpCopyMemory %var1 %var2
7548 OpReturn
7549 OpFunctionEnd
7550 )";
7551
7552 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
7553 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
7554 }
7555
7556 TEST_F(ValidateMemory, CopyMemoryUntypedPointerTargetAndSourceBad) {
7557 const std::string spirv = R"(
7558 OpCapability Shader
7559 OpCapability UntypedPointersKHR
7560 OpCapability WorkgroupMemoryExplicitLayoutKHR
7561 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
7562 OpExtension "SPV_KHR_untyped_pointers"
7563 OpMemoryModel Logical GLSL450
7564 OpEntryPoint GLCompute %main "main" %var1 %var2
7565 OpName %var1 "var1"
7566 OpDecorate %struct Block
7567 OpMemberDecorate %struct 0 Offset 0
7568 %void = OpTypeVoid
7569 %int = OpTypeInt 32 0
7570 %struct = OpTypeStruct %int
7571 %ptr = OpTypeUntypedPointerKHR Workgroup
7572 %var1 = OpUntypedVariableKHR %ptr Workgroup %struct
7573 %var2 = OpUntypedVariableKHR %ptr Workgroup %struct
7574 %void_fn = OpTypeFunction %void
7575 %main = OpFunction %void None %void_fn
7576 %entry = OpLabel
7577 OpCopyMemory %var1 %var2
7578 OpReturn
7579 OpFunctionEnd
7580 )";
7581
7582 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
7583 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
7584 EXPECT_THAT(getDiagnosticString(),
7585 HasSubstr("One of Source or Target must be a typed pointer"));
7586 }
7587
7588 TEST_F(ValidateMemory, CopyMemorySizedUntypedPointersGood) {
7589 const std::string spirv = R"(
7590 OpCapability Shader
7591 OpCapability UntypedPointersKHR
7592 OpCapability WorkgroupMemoryExplicitLayoutKHR
7593 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
7594 OpExtension "SPV_KHR_untyped_pointers"
7595 OpMemoryModel Logical GLSL450
7596 OpEntryPoint GLCompute %main "main" %v1 %v2
7597 OpDecorate %struct Block
7598 OpMemberDecorate %struct 0 Offset 0
7599 %void = OpTypeVoid
7600 %int = OpTypeInt 32 0
7601 %int_4 = OpConstant %int 4
7602 %struct = OpTypeStruct %int
7603 %ptr = OpTypeUntypedPointerKHR Workgroup
7604 %v1 = OpUntypedVariableKHR %ptr Workgroup %struct
7605 %v2 = OpUntypedVariableKHR %ptr Workgroup %struct
7606 %void_fn = OpTypeFunction %void
7607 %main = OpFunction %void None %void_fn
7608 %entry = OpLabel
7609 OpCopyMemorySized %v2 %v1 %int_4
7610 OpReturn
7611 OpFunctionEnd
7612 )";
7613
7614 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
7615 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
7616 }
7617
7618 TEST_F(ValidateMemory, CopyMemorySizedUntypedPointersSizeBad1) {
7619 const std::string spirv = R"(
7620 OpCapability Shader
7621 OpCapability UntypedPointersKHR
7622 OpCapability StorageBuffer16BitAccess
7623 OpCapability WorkgroupMemoryExplicitLayoutKHR
7624 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
7625 OpExtension "SPV_KHR_untyped_pointers"
7626 OpMemoryModel Logical GLSL450
7627 OpEntryPoint GLCompute %main "main" %var_wg %var_ssbo
7628 OpDecorate %struct Block
7629 OpMemberDecorate %struct 0 Offset 0
7630 %void = OpTypeVoid
7631 %int = OpTypeInt 32 0
7632 %short = OpTypeInt 16 0
7633 %int_2 = OpConstant %int 2
7634 %struct = OpTypeStruct %int
7635 %ptr_ssbo = OpTypeUntypedPointerKHR StorageBuffer
7636 %ptr_wg = OpTypeUntypedPointerKHR Workgroup
7637 %var_ssbo = OpUntypedVariableKHR %ptr_ssbo StorageBuffer %struct
7638 %var_wg = OpUntypedVariableKHR %ptr_wg Workgroup %struct
7639 %void_fn = OpTypeFunction %void
7640 %main = OpFunction %void None %void_fn
7641 %entry = OpLabel
7642 OpCopyMemorySized %var_ssbo %var_wg %int_2
7643 OpReturn
7644 OpFunctionEnd
7645 )";
7646
7647 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
7648 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
7649 EXPECT_THAT(getDiagnosticString(), HasSubstr("Size must be a multiple of 4"));
7650 }
7651
7652 TEST_F(ValidateMemory, CopyMemorySizedUntypedPointersSizeBad2) {
7653 const std::string spirv = R"(
7654 OpCapability Shader
7655 OpCapability UntypedPointersKHR
7656 OpCapability StorageBuffer16BitAccess
7657 OpCapability WorkgroupMemoryExplicitLayoutKHR
7658 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
7659 OpExtension "SPV_KHR_untyped_pointers"
7660 OpMemoryModel Logical GLSL450
7661 OpEntryPoint GLCompute %main "main" %var_ssbo %var_wg
7662 OpDecorate %struct Block
7663 OpMemberDecorate %struct 0 Offset 0
7664 %void = OpTypeVoid
7665 %int = OpTypeInt 32 0
7666 %short = OpTypeInt 16 0
7667 %int_2 = OpConstant %int 2
7668 %struct = OpTypeStruct %int
7669 %ptr_ssbo = OpTypeUntypedPointerKHR StorageBuffer
7670 %ptr_wg = OpTypeUntypedPointerKHR Workgroup
7671 %var_ssbo = OpUntypedVariableKHR %ptr_ssbo StorageBuffer %struct
7672 %var_wg = OpUntypedVariableKHR %ptr_wg Workgroup %struct
7673 %void_fn = OpTypeFunction %void
7674 %main = OpFunction %void None %void_fn
7675 %entry = OpLabel
7676 OpCopyMemorySized %var_wg %var_ssbo %int_2
7677 OpReturn
7678 OpFunctionEnd
7679 )";
7680
7681 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
7682 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
7683 EXPECT_THAT(getDiagnosticString(), HasSubstr("Size must be a multiple of 4"));
7684 }
7685
7686 TEST_F(ValidateMemory, CopyMemorySizedUntypedPointersSizeBad3) {
7687 const std::string spirv = R"(
7688 OpCapability Shader
7689 OpCapability Int16
7690 OpCapability UntypedPointersKHR
7691 OpCapability StorageBuffer8BitAccess
7692 OpCapability WorkgroupMemoryExplicitLayoutKHR
7693 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
7694 OpExtension "SPV_KHR_8bit_storage"
7695 OpExtension "SPV_KHR_untyped_pointers"
7696 OpMemoryModel Logical GLSL450
7697 OpEntryPoint GLCompute %main "main" %var_ssbo %var_wg
7698 OpDecorate %struct Block
7699 OpMemberDecorate %struct 0 Offset 0
7700 %void = OpTypeVoid
7701 %int = OpTypeInt 32 0
7702 %short = OpTypeInt 16 0
7703 %int_1 = OpConstant %int 1
7704 %struct = OpTypeStruct %int
7705 %ptr_ssbo = OpTypeUntypedPointerKHR StorageBuffer
7706 %ptr_wg = OpTypeUntypedPointerKHR Workgroup
7707 %var_ssbo = OpUntypedVariableKHR %ptr_ssbo StorageBuffer %struct
7708 %var_wg = OpUntypedVariableKHR %ptr_wg Workgroup %struct
7709 %void_fn = OpTypeFunction %void
7710 %main = OpFunction %void None %void_fn
7711 %entry = OpLabel
7712 OpCopyMemorySized %var_ssbo %var_wg %int_1
7713 OpReturn
7714 OpFunctionEnd
7715 )";
7716
7717 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
7718 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
7719 EXPECT_THAT(getDiagnosticString(), HasSubstr("Size must be a multiple of 2"));
7720 }
7721
7722 TEST_F(ValidateMemory, CopyMemorySizedUntypedPointersSizeBad4) {
7723 const std::string spirv = R"(
7724 OpCapability Shader
7725 OpCapability Int16
7726 OpCapability UntypedPointersKHR
7727 OpCapability StorageBuffer8BitAccess
7728 OpCapability WorkgroupMemoryExplicitLayoutKHR
7729 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
7730 OpExtension "SPV_KHR_8bit_storage"
7731 OpExtension "SPV_KHR_untyped_pointers"
7732 OpMemoryModel Logical GLSL450
7733 OpEntryPoint GLCompute %main "main" %var_ssbo %var_wg
7734 OpDecorate %struct Block
7735 OpMemberDecorate %struct 0 Offset 0
7736 %void = OpTypeVoid
7737 %int = OpTypeInt 32 0
7738 %short = OpTypeInt 16 0
7739 %int_1 = OpConstant %int 1
7740 %struct = OpTypeStruct %int
7741 %ptr_ssbo = OpTypeUntypedPointerKHR StorageBuffer
7742 %ptr_wg = OpTypeUntypedPointerKHR Workgroup
7743 %var_ssbo = OpUntypedVariableKHR %ptr_ssbo StorageBuffer %struct
7744 %var_wg = OpUntypedVariableKHR %ptr_wg Workgroup %struct
7745 %void_fn = OpTypeFunction %void
7746 %main = OpFunction %void None %void_fn
7747 %entry = OpLabel
7748 OpCopyMemorySized %var_wg %var_ssbo %int_1
7749 OpReturn
7750 OpFunctionEnd
7751 )";
7752
7753 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
7754 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
7755 EXPECT_THAT(getDiagnosticString(), HasSubstr("Size must be a multiple of 2"));
7756 }
7757
7758 TEST_F(ValidateMemory, PtrEqualUntypedPointersGood) {
7759 const std::string spirv = R"(
7760 OpCapability Shader
7761 OpCapability VariablePointers
7762 OpCapability UntypedPointersKHR
7763 OpCapability WorkgroupMemoryExplicitLayoutKHR
7764 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
7765 OpExtension "SPV_KHR_untyped_pointers"
7766 OpMemoryModel Logical GLSL450
7767 OpEntryPoint GLCompute %main "main" %v1 %v2
7768 OpDecorate %struct Block
7769 OpMemberDecorate %struct 0 Offset 0
7770 %void = OpTypeVoid
7771 %bool = OpTypeBool
7772 %int = OpTypeInt 32 0
7773 %struct = OpTypeStruct %int
7774 %ptr = OpTypeUntypedPointerKHR Workgroup
7775 %v1 = OpUntypedVariableKHR %ptr Workgroup %struct
7776 %v2 = OpUntypedVariableKHR %ptr Workgroup %struct
7777 %void_fn = OpTypeFunction %void
7778 %main = OpFunction %void None %void_fn
7779 %entry = OpLabel
7780 %res = OpPtrEqual %bool %v1 %v2
7781 OpReturn
7782 OpFunctionEnd
7783 )";
7784
7785 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
7786 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
7787 }
7788
7789 TEST_F(ValidateMemory, PtrNotEqualUntypedPointersGood) {
7790 const std::string spirv = R"(
7791 OpCapability Shader
7792 OpCapability VariablePointers
7793 OpCapability UntypedPointersKHR
7794 OpCapability WorkgroupMemoryExplicitLayoutKHR
7795 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
7796 OpExtension "SPV_KHR_untyped_pointers"
7797 OpMemoryModel Logical GLSL450
7798 OpEntryPoint GLCompute %main "main" %v1 %v2
7799 OpDecorate %struct Block
7800 OpMemberDecorate %struct 0 Offset 0
7801 %void = OpTypeVoid
7802 %bool = OpTypeBool
7803 %int = OpTypeInt 32 0
7804 %struct = OpTypeStruct %int
7805 %ptr = OpTypeUntypedPointerKHR Workgroup
7806 %v1 = OpUntypedVariableKHR %ptr Workgroup %struct
7807 %v2 = OpUntypedVariableKHR %ptr Workgroup %struct
7808 %void_fn = OpTypeFunction %void
7809 %main = OpFunction %void None %void_fn
7810 %entry = OpLabel
7811 %res = OpPtrNotEqual %bool %v1 %v2
7812 OpReturn
7813 OpFunctionEnd
7814 )";
7815
7816 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
7817 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
7818 }
7819
7820 TEST_F(ValidateMemory, PtrDiffUntypedPointersGood) {
7821 const std::string spirv = R"(
7822 OpCapability Shader
7823 OpCapability VariablePointers
7824 OpCapability UntypedPointersKHR
7825 OpCapability WorkgroupMemoryExplicitLayoutKHR
7826 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
7827 OpExtension "SPV_KHR_untyped_pointers"
7828 OpMemoryModel Logical GLSL450
7829 OpEntryPoint GLCompute %main "main" %v1
7830 OpDecorate %struct Block
7831 OpMemberDecorate %struct 0 Offset 0
7832 %void = OpTypeVoid
7833 %int = OpTypeInt 32 0
7834 %struct = OpTypeStruct %int
7835 %ptr = OpTypeUntypedPointerKHR Workgroup
7836 %v1 = OpUntypedVariableKHR %ptr Workgroup %struct
7837 %void_fn = OpTypeFunction %void
7838 %main = OpFunction %void None %void_fn
7839 %entry = OpLabel
7840 %res = OpPtrDiff %int %v1 %v1
7841 OpReturn
7842 OpFunctionEnd
7843 )";
7844
7845 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
7846 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
7847 }
7848
7849 TEST_F(ValidateMemory, UntypedVariableVulkanPushConstantGood) {
7850 const std::string spirv = R"(
7851 OpCapability Shader
7852 OpCapability UntypedPointersKHR
7853 OpExtension "SPV_KHR_untyped_pointers"
7854 OpMemoryModel Logical GLSL450
7855 OpEntryPoint GLCompute %main "main"
7856 OpExecutionMode %main LocalSize 1 1 1
7857 OpDecorate %struct Block
7858 OpMemberDecorate %struct 0 Offset 0
7859 %void = OpTypeVoid
7860 %int = OpTypeInt 32 0
7861 %struct = OpTypeStruct %int
7862 %ptr = OpTypeUntypedPointerKHR PushConstant
7863 %var = OpUntypedVariableKHR %ptr PushConstant %struct
7864 %void_fn = OpTypeFunction %void
7865 %main = OpFunction %void None %void_fn
7866 %entry = OpLabel
7867 OpReturn
7868 OpFunctionEnd
7869 )";
7870
7871 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
7872 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
7873 }
7874
7875 TEST_F(ValidateMemory, UntypedVariableVulkanStorageBufferGood) {
7876 const std::string spirv = R"(
7877 OpCapability Shader
7878 OpCapability UntypedPointersKHR
7879 OpExtension "SPV_KHR_untyped_pointers"
7880 OpExtension "SPV_KHR_storage_buffer_storage_class"
7881 OpMemoryModel Logical GLSL450
7882 OpEntryPoint GLCompute %main "main"
7883 OpExecutionMode %main LocalSize 1 1 1
7884 OpDecorate %struct Block
7885 OpMemberDecorate %struct 0 Offset 0
7886 OpDecorate %var DescriptorSet 0
7887 OpDecorate %var Binding 0
7888 %void = OpTypeVoid
7889 %int = OpTypeInt 32 0
7890 %struct = OpTypeStruct %int
7891 %ptr = OpTypeUntypedPointerKHR StorageBuffer
7892 %var = OpUntypedVariableKHR %ptr StorageBuffer %struct
7893 %void_fn = OpTypeFunction %void
7894 %main = OpFunction %void None %void_fn
7895 %entry = OpLabel
7896 OpReturn
7897 OpFunctionEnd
7898 )";
7899
7900 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
7901 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
7902 }
7903
7904 TEST_F(ValidateMemory, UntypedVariableVulkanUniformGood) {
7905 const std::string spirv = R"(
7906 OpCapability Shader
7907 OpCapability UntypedPointersKHR
7908 OpExtension "SPV_KHR_untyped_pointers"
7909 OpExtension "SPV_KHR_storage_buffer_storage_class"
7910 OpMemoryModel Logical GLSL450
7911 OpEntryPoint GLCompute %main "main"
7912 OpExecutionMode %main LocalSize 1 1 1
7913 OpDecorate %struct Block
7914 OpMemberDecorate %struct 0 Offset 0
7915 OpDecorate %var DescriptorSet 0
7916 OpDecorate %var Binding 0
7917 %void = OpTypeVoid
7918 %int = OpTypeInt 32 0
7919 %struct = OpTypeStruct %int
7920 %ptr = OpTypeUntypedPointerKHR Uniform
7921 %var = OpUntypedVariableKHR %ptr Uniform %struct
7922 %void_fn = OpTypeFunction %void
7923 %main = OpFunction %void None %void_fn
7924 %entry = OpLabel
7925 OpReturn
7926 OpFunctionEnd
7927 )";
7928
7929 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
7930 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
7931 }
7932
7933 TEST_F(ValidateMemory, UntypedVariableVulkanWorkgroupGood) {
7934 const std::string spirv = R"(
7935 OpCapability Shader
7936 OpCapability UntypedPointersKHR
7937 OpCapability WorkgroupMemoryExplicitLayoutKHR
7938 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
7939 OpExtension "SPV_KHR_untyped_pointers"
7940 OpMemoryModel Logical GLSL450
7941 OpEntryPoint GLCompute %main "main"
7942 OpExecutionMode %main LocalSize 1 1 1
7943 OpDecorate %struct Block
7944 OpMemberDecorate %struct 0 Offset 0
7945 %void = OpTypeVoid
7946 %int = OpTypeInt 32 0
7947 %struct = OpTypeStruct %int
7948 %ptr = OpTypeUntypedPointerKHR Workgroup
7949 %var = OpUntypedVariableKHR %ptr Workgroup %struct
7950 %void_fn = OpTypeFunction %void
7951 %main = OpFunction %void None %void_fn
7952 %entry = OpLabel
7953 OpReturn
7954 OpFunctionEnd
7955 )";
7956
7957 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1_SPIRV_1_4);
7958 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
7959 }
7960
7961 TEST_F(ValidateMemory, UntypedPointerAsVariableType) {
7962 const std::string spirv = R"(
7963 OpCapability Shader
7964 OpCapability Linkage
7965 OpCapability VariablePointers
7966 OpCapability UntypedPointersKHR
7967 OpExtension "SPV_KHR_untyped_pointers"
7968 OpMemoryModel Logical GLSL450
7969 %void = OpTypeVoid
7970 %float = OpTypeFloat 32
7971 %ptr = OpTypeUntypedPointerKHR StorageBuffer
7972 %priv_ptr = OpTypePointer Private %ptr
7973 %var = OpVariable %priv_ptr Private
7974 )";
7975
7976 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
7977 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
7978 }
7979
7980 TEST_F(ValidateMemory, UntypedArrayLengthGood) {
7981 const std::string spirv = R"(
7982 OpCapability Shader
7983 OpCapability UntypedPointersKHR
7984 OpExtension "SPV_KHR_untyped_pointers"
7985 OpMemoryModel Logical GLSL450
7986 OpEntryPoint GLCompute %main "main"
7987 OpExecutionMode %main LocalSize 1 1 1
7988 OpDecorate %var DescriptorSet 0
7989 OpDecorate %var Binding 0
7990 OpDecorate %block Block
7991 OpMemberDecorate %block 0 Offset 0
7992 OpDecorate %array ArrayStride 4
7993 %void = OpTypeVoid
7994 %int = OpTypeInt 32 0
7995 %array = OpTypeRuntimeArray %int
7996 %block = OpTypeStruct %array
7997 %ptr = OpTypeUntypedPointerKHR StorageBuffer
7998 %var = OpUntypedVariableKHR %ptr StorageBuffer %block
7999 %void_fn = OpTypeFunction %void
8000 %main = OpFunction %void None %void_fn
8001 %entry = OpLabel
8002 %length = OpUntypedArrayLengthKHR %int %block %var 0
8003 OpReturn
8004 OpFunctionEnd
8005 )";
8006
8007 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
8008 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
8009 }
8010
8011 TEST_F(ValidateMemory, UntypedArrayLengthBadResultType) {
8012 const std::string spirv = R"(
8013 OpCapability Shader
8014 OpCapability UntypedPointersKHR
8015 OpExtension "SPV_KHR_untyped_pointers"
8016 OpMemoryModel Logical GLSL450
8017 OpEntryPoint GLCompute %main "main"
8018 OpExecutionMode %main LocalSize 1 1 1
8019 OpDecorate %var DescriptorSet 0
8020 OpDecorate %var Binding 0
8021 OpDecorate %block Block
8022 OpMemberDecorate %block 0 Offset 0
8023 OpDecorate %array ArrayStride 4
8024 %void = OpTypeVoid
8025 %int = OpTypeInt 32 0
8026 %float = OpTypeFloat 32
8027 %array = OpTypeRuntimeArray %int
8028 %block = OpTypeStruct %array
8029 %ptr = OpTypeUntypedPointerKHR StorageBuffer
8030 %var = OpUntypedVariableKHR %ptr StorageBuffer %block
8031 %void_fn = OpTypeFunction %void
8032 %main = OpFunction %void None %void_fn
8033 %entry = OpLabel
8034 %length = OpUntypedArrayLengthKHR %float %block %var 0
8035 OpReturn
8036 OpFunctionEnd
8037 )";
8038
8039 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
8040 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
8041 EXPECT_THAT(
8042 getDiagnosticString(),
8043 HasSubstr("must be OpTypeInt with width 32 or 64 and signedness 0"));
8044 }
8045
8046 TEST_F(ValidateMemory, UntypedArrayLengthBadPointer) {
8047 const std::string spirv = R"(
8048 OpCapability Shader
8049 OpCapability UntypedPointersKHR
8050 OpExtension "SPV_KHR_untyped_pointers"
8051 OpMemoryModel Logical GLSL450
8052 OpEntryPoint GLCompute %main "main"
8053 OpExecutionMode %main LocalSize 1 1 1
8054 OpDecorate %var DescriptorSet 0
8055 OpDecorate %var Binding 0
8056 OpDecorate %block Block
8057 OpMemberDecorate %block 0 Offset 0
8058 OpDecorate %array ArrayStride 4
8059 %void = OpTypeVoid
8060 %int = OpTypeInt 32 0
8061 %array = OpTypeRuntimeArray %int
8062 %block = OpTypeStruct %array
8063 %ptr = OpTypeUntypedPointerKHR StorageBuffer
8064 %typed_ptr = OpTypePointer StorageBuffer %block
8065 %var = OpVariable %typed_ptr StorageBuffer
8066 %void_fn = OpTypeFunction %void
8067 %main = OpFunction %void None %void_fn
8068 %entry = OpLabel
8069 %length = OpUntypedArrayLengthKHR %int %block %var 0
8070 OpReturn
8071 OpFunctionEnd
8072 )";
8073
8074 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
8075 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
8076 EXPECT_THAT(getDiagnosticString(),
8077 HasSubstr("Pointer must be an untyped pointer object"));
8078 }
8079
8080 TEST_F(ValidateMemory, UntypedArrayLengthBadPointer2) {
8081 const std::string spirv = R"(
8082 OpCapability Shader
8083 OpCapability UntypedPointersKHR
8084 OpCapability Int64
8085 OpExtension "SPV_KHR_untyped_pointers"
8086 OpMemoryModel Logical GLSL450
8087 OpEntryPoint GLCompute %main "main" %b
8088 OpExecutionMode %main LocalSize 1 1 1
8089 OpDecorate %_runtimearr_float ArrayStride 4
8090 OpDecorate %B Block
8091 OpMemberDecorate %B 0 NonWritable
8092 OpMemberDecorate %B 0 Offset 0
8093 OpDecorate %b NonWritable
8094 OpDecorate %b Binding 0
8095 OpDecorate %b DescriptorSet 0
8096 %void = OpTypeVoid
8097 %4 = OpTypeFunction %void
8098 %float = OpTypeFloat 32
8099 %_runtimearr_float = OpTypeRuntimeArray %float
8100 %B = OpTypeStruct %_runtimearr_float
8101 %ptr = OpTypeUntypedPointerKHR StorageBuffer
8102 %b = OpUntypedVariableKHR %ptr StorageBuffer %B
8103 %ulong = OpTypeInt 64 0
8104 %long = OpTypeInt 64 1
8105 %main = OpFunction %void None %4
8106 %6 = OpLabel
8107 %13 = OpUntypedArrayLengthKHR %ulong %b %ptr 0
8108 %15 = OpBitcast %long %13
8109 OpReturn
8110 OpFunctionEnd
8111 )";
8112
8113 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
8114 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
8115 EXPECT_THAT(getDiagnosticString(),
8116 HasSubstr("Pointer must be an untyped pointer object"));
8117 }
8118
8119 TEST_F(ValidateMemory, UntypedArrayLengtBadStruct) {
8120 const std::string spirv = R"(
8121 OpCapability Shader
8122 OpCapability UntypedPointersKHR
8123 OpExtension "SPV_KHR_untyped_pointers"
8124 OpMemoryModel Logical GLSL450
8125 OpEntryPoint GLCompute %main "main"
8126 OpExecutionMode %main LocalSize 1 1 1
8127 OpDecorate %var DescriptorSet 0
8128 OpDecorate %var Binding 0
8129 OpDecorate %block Block
8130 OpMemberDecorate %block 0 Offset 0
8131 OpDecorate %array ArrayStride 4
8132 %void = OpTypeVoid
8133 %int = OpTypeInt 32 0
8134 %array = OpTypeRuntimeArray %int
8135 %block = OpTypeStruct %array
8136 %ptr = OpTypeUntypedPointerKHR StorageBuffer
8137 %var = OpUntypedVariableKHR %ptr StorageBuffer %block
8138 %void_fn = OpTypeFunction %void
8139 %main = OpFunction %void None %void_fn
8140 %entry = OpLabel
8141 %length = OpUntypedArrayLengthKHR %int %int %var 0
8142 OpReturn
8143 OpFunctionEnd
8144 )";
8145
8146 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
8147 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
8148 EXPECT_THAT(getDiagnosticString(), HasSubstr("to an OpTypeStruct"));
8149 }
8150
8151 TEST_F(ValidateMemory, UntypedArrayLengthLastMemberNotArray) {
8152 const std::string spirv = R"(
8153 OpCapability Shader
8154 OpCapability UntypedPointersKHR
8155 OpExtension "SPV_KHR_untyped_pointers"
8156 OpMemoryModel Logical GLSL450
8157 OpEntryPoint GLCompute %main "main"
8158 OpExecutionMode %main LocalSize 1 1 1
8159 OpDecorate %var DescriptorSet 0
8160 OpDecorate %var Binding 0
8161 OpDecorate %block Block
8162 OpMemberDecorate %block 0 Offset 0
8163 OpDecorate %array ArrayStride 4
8164 %void = OpTypeVoid
8165 %int = OpTypeInt 32 0
8166 %array = OpTypeRuntimeArray %int
8167 %block = OpTypeStruct %int
8168 %ptr = OpTypeUntypedPointerKHR StorageBuffer
8169 %var = OpUntypedVariableKHR %ptr StorageBuffer %block
8170 %void_fn = OpTypeFunction %void
8171 %main = OpFunction %void None %void_fn
8172 %entry = OpLabel
8173 %length = OpUntypedArrayLengthKHR %int %block %var 0
8174 OpReturn
8175 OpFunctionEnd
8176 )";
8177
8178 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
8179 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
8180 EXPECT_THAT(getDiagnosticString(),
8181 HasSubstr("must be an OpTypeRuntimeArray"));
8182 }
8183
8184 TEST_F(ValidateMemory, UntypedArrayLengthBadIndex) {
8185 const std::string spirv = R"(
8186 OpCapability Shader
8187 OpCapability UntypedPointersKHR
8188 OpExtension "SPV_KHR_untyped_pointers"
8189 OpMemoryModel Logical GLSL450
8190 OpEntryPoint GLCompute %main "main"
8191 OpExecutionMode %main LocalSize 1 1 1
8192 OpDecorate %var DescriptorSet 0
8193 OpDecorate %var Binding 0
8194 OpDecorate %block Block
8195 OpMemberDecorate %block 0 Offset 0
8196 OpDecorate %array ArrayStride 4
8197 %void = OpTypeVoid
8198 %int = OpTypeInt 32 0
8199 %array = OpTypeRuntimeArray %int
8200 %block = OpTypeStruct %array
8201 %ptr = OpTypeUntypedPointerKHR StorageBuffer
8202 %var = OpUntypedVariableKHR %ptr StorageBuffer %block
8203 %void_fn = OpTypeFunction %void
8204 %main = OpFunction %void None %void_fn
8205 %entry = OpLabel
8206 %length = OpUntypedArrayLengthKHR %int %block %var 1
8207 OpReturn
8208 OpFunctionEnd
8209 )";
8210
8211 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
8212 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
8213 EXPECT_THAT(getDiagnosticString(),
8214 HasSubstr("must be the last member of the struct"));
8215 }
8216
8217 TEST_F(ValidateMemory, UntypedCooperativeMatrixLoad) {
8218 const std::string spirv = R"(
8219 OpCapability Shader
8220 OpCapability UntypedPointersKHR
8221 OpCapability CooperativeMatrixKHR
8222 OpCapability VulkanMemoryModel
8223 OpExtension "SPV_KHR_untyped_pointers"
8224 OpExtension "SPV_KHR_cooperative_matrix"
8225 OpMemoryModel Logical Vulkan
8226 OpEntryPoint GLCompute %main "main" %var
8227 OpExecutionMode %main LocalSize 1 1 1
8228 OpDecorate %var DescriptorSet 0
8229 OpDecorate %var Binding 0
8230 OpDecorate %block Block
8231 OpMemberDecorate %block 0 Offset 0
8232 OpDecorate %array ArrayStride 4
8233 %void = OpTypeVoid
8234 %void_fn = OpTypeFunction %void
8235 %untyped = OpTypeUntypedPointerKHR StorageBuffer
8236 %float = OpTypeFloat 32
8237 %array = OpTypeRuntimeArray %float
8238 %block = OpTypeStruct %array
8239 %int = OpTypeInt 32 0
8240 %int_0 = OpConstant %int 0
8241 %subgroup = OpConstant %int 3
8242 %rows = OpSpecConstant %int 1
8243 %cols = OpSpecConstant %int 1
8244 %matrix_a = OpConstant %int 1
8245 %stride = OpConstant %int 42
8246 %matrix = OpTypeCooperativeMatrixKHR %float %subgroup %rows %cols %matrix_a
8247 %var = OpUntypedVariableKHR %untyped StorageBuffer %block
8248 %main = OpFunction %void None %void_fn
8249 %entry = OpLabel
8250 %ld = OpCooperativeMatrixLoadKHR %matrix %var %int_0 %stride
8251 OpReturn
8252 OpFunctionEnd
8253 )";
8254
8255 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_3);
8256 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3));
8257 }
8258
8259 TEST_F(ValidateMemory, UntypedCooperativeMatrixLoad2) {
8260 const std::string spirv = R"(
8261 OpCapability Shader
8262 OpCapability UntypedPointersKHR
8263 OpCapability CooperativeMatrixKHR
8264 OpCapability VulkanMemoryModel
8265 OpExtension "SPV_KHR_untyped_pointers"
8266 OpExtension "SPV_KHR_cooperative_matrix"
8267 OpMemoryModel Logical Vulkan
8268 OpEntryPoint GLCompute %main "main" %var
8269 OpExecutionMode %main LocalSize 1 1 1
8270 OpDecorate %var DescriptorSet 0
8271 OpDecorate %var Binding 0
8272 OpDecorate %block Block
8273 OpMemberDecorate %block 0 Offset 0
8274 OpDecorate %array ArrayStride 4
8275 %void = OpTypeVoid
8276 %void_fn = OpTypeFunction %void
8277 %untyped = OpTypeUntypedPointerKHR StorageBuffer
8278 %float = OpTypeFloat 32
8279 %array = OpTypeRuntimeArray %float
8280 %block = OpTypeStruct %array
8281 %int = OpTypeInt 32 0
8282 %int_0 = OpConstant %int 0
8283 %subgroup = OpConstant %int 3
8284 %rows = OpSpecConstant %int 1
8285 %cols = OpSpecConstant %int 1
8286 %matrix_a = OpConstant %int 1
8287 %stride = OpConstant %int 42
8288 %matrix = OpTypeCooperativeMatrixKHR %float %subgroup %rows %cols %matrix_a
8289 %var = OpUntypedVariableKHR %untyped StorageBuffer %block
8290 %main = OpFunction %void None %void_fn
8291 %entry = OpLabel
8292 %gep = OpUntypedAccessChainKHR %untyped %block %var %int_0 %int_0
8293 %ld = OpCooperativeMatrixLoadKHR %matrix %gep %int_0 %stride
8294 OpReturn
8295 OpFunctionEnd
8296 )";
8297
8298 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_3);
8299 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3));
8300 }
8301
8302 TEST_F(ValidateMemory, UntypedCooperativeMatrixStore) {
8303 const std::string spirv = R"(
8304 OpCapability Shader
8305 OpCapability UntypedPointersKHR
8306 OpCapability CooperativeMatrixKHR
8307 OpCapability VulkanMemoryModel
8308 OpExtension "SPV_KHR_untyped_pointers"
8309 OpExtension "SPV_KHR_cooperative_matrix"
8310 OpMemoryModel Logical Vulkan
8311 OpEntryPoint GLCompute %main "main" %var1 %var2
8312 OpExecutionMode %main LocalSize 1 1 1
8313 OpDecorate %var1 DescriptorSet 0
8314 OpDecorate %var1 Binding 0
8315 OpDecorate %var2 DescriptorSet 0
8316 OpDecorate %var2 Binding 1
8317 OpDecorate %block Block
8318 OpMemberDecorate %block 0 Offset 0
8319 OpDecorate %array ArrayStride 4
8320 %void = OpTypeVoid
8321 %void_fn = OpTypeFunction %void
8322 %untyped = OpTypeUntypedPointerKHR StorageBuffer
8323 %float = OpTypeFloat 32
8324 %array = OpTypeRuntimeArray %float
8325 %block = OpTypeStruct %array
8326 %ptr = OpTypePointer StorageBuffer %block
8327 %ptr_float = OpTypePointer StorageBuffer %float
8328 %int = OpTypeInt 32 0
8329 %int_0 = OpConstant %int 0
8330 %subgroup = OpConstant %int 3
8331 %rows = OpSpecConstant %int 1
8332 %cols = OpSpecConstant %int 1
8333 %matrix_a = OpConstant %int 1
8334 %stride = OpConstant %int 42
8335 %matrix = OpTypeCooperativeMatrixKHR %float %subgroup %rows %cols %matrix_a
8336 %var1 = OpVariable %ptr StorageBuffer
8337 %var2 = OpUntypedVariableKHR %untyped StorageBuffer %block
8338 %main = OpFunction %void None %void_fn
8339 %entry = OpLabel
8340 %gep = OpAccessChain %ptr_float %var1 %int_0 %int_0
8341 %ld = OpCooperativeMatrixLoadKHR %matrix %gep %int_0 %stride
8342 OpCooperativeMatrixStoreKHR %var2 %ld %int_0 %stride
8343 OpReturn
8344 OpFunctionEnd
8345 )";
8346
8347 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_3);
8348 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3));
8349 }
8350
8351 TEST_F(ValidateMemory, UntypedCooperativeMatrixStore2) {
8352 const std::string spirv = R"(
8353 OpCapability Shader
8354 OpCapability UntypedPointersKHR
8355 OpCapability CooperativeMatrixKHR
8356 OpCapability VulkanMemoryModel
8357 OpExtension "SPV_KHR_untyped_pointers"
8358 OpExtension "SPV_KHR_cooperative_matrix"
8359 OpMemoryModel Logical Vulkan
8360 OpEntryPoint GLCompute %main "main" %var1 %var2
8361 OpExecutionMode %main LocalSize 1 1 1
8362 OpDecorate %var1 DescriptorSet 0
8363 OpDecorate %var1 Binding 0
8364 OpDecorate %var2 DescriptorSet 0
8365 OpDecorate %var2 Binding 1
8366 OpDecorate %block Block
8367 OpMemberDecorate %block 0 Offset 0
8368 OpDecorate %array ArrayStride 4
8369 %void = OpTypeVoid
8370 %void_fn = OpTypeFunction %void
8371 %untyped = OpTypeUntypedPointerKHR StorageBuffer
8372 %float = OpTypeFloat 32
8373 %array = OpTypeRuntimeArray %float
8374 %block = OpTypeStruct %array
8375 %ptr = OpTypePointer StorageBuffer %block
8376 %ptr_float = OpTypePointer StorageBuffer %float
8377 %int = OpTypeInt 32 0
8378 %int_0 = OpConstant %int 0
8379 %subgroup = OpConstant %int 3
8380 %rows = OpSpecConstant %int 1
8381 %cols = OpSpecConstant %int 1
8382 %matrix_a = OpConstant %int 1
8383 %stride = OpConstant %int 42
8384 %matrix = OpTypeCooperativeMatrixKHR %float %subgroup %rows %cols %matrix_a
8385 %var1 = OpVariable %ptr StorageBuffer
8386 %var2 = OpUntypedVariableKHR %untyped StorageBuffer %block
8387 %main = OpFunction %void None %void_fn
8388 %entry = OpLabel
8389 %gep = OpAccessChain %ptr_float %var1 %int_0 %int_0
8390 %ld = OpCooperativeMatrixLoadKHR %matrix %gep %int_0 %stride
8391 %gep2 = OpUntypedAccessChainKHR %untyped %block %var2 %int_0 %int_0
8392 OpCooperativeMatrixStoreKHR %gep2 %ld %int_0 %stride
8393 OpReturn
8394 OpFunctionEnd
8395 )";
8396
8397 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_3);
8398 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_3));
8399 }
8400
8401 TEST_F(ValidateMemory, PtrAccessChainElementNotInteger) {
8402 const std::string spirv = R"(
8403 OpCapability Shader
8404 OpCapability VariablePointers
8405 OpMemoryModel Logical GLSL450
8406 OpEntryPoint GLCompute %main "main"
8407 OpExecutionMode %main LocalSize 1 1 1
8408 OpDecorate %ptr_int ArrayStride 4
8409 %void = OpTypeVoid
8410 %int = OpTypeInt 32 0
8411 %int_4 = OpConstant %int 4
8412 %int_0 = OpConstant %int 0
8413 %float = OpTypeFloat 32
8414 %float_0 = OpConstant %float 0
8415 %array = OpTypeArray %int %int_4
8416 %ptr_array = OpTypePointer Workgroup %array
8417 %ptr_int = OpTypePointer Workgroup %int
8418 %var = OpVariable %ptr_array Workgroup
8419 %void_fn = OpTypeFunction %void
8420 %main = OpFunction %void None %void_fn
8421 %entry = OpLabel
8422 %gep = OpAccessChain %ptr_int %var %int_0
8423 %ptr_gep = OpPtrAccessChain %ptr_int %gep %float_0
8424 OpReturn
8425 OpFunctionEnd
8426 )";
8427
8428 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
8429 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_1));
8430 EXPECT_THAT(getDiagnosticString(), HasSubstr("Element must be an integer"));
8431 }
8432
8433 TEST_F(ValidateMemory, PtrAccessChainElementNotIntegerUntyped) {
8434 const std::string spirv = R"(
8435 OpCapability Shader
8436 OpCapability VariablePointers
8437 OpCapability UntypedPointersKHR
8438 OpCapability WorkgroupMemoryExplicitLayoutKHR
8439 OpExtension "SPV_KHR_untyped_pointers"
8440 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
8441 OpMemoryModel Logical GLSL450
8442 OpEntryPoint GLCompute %main "main" %var
8443 OpExecutionMode %main LocalSize 1 1 1
8444 OpDecorate %ptr_int ArrayStride 4
8445 OpDecorate %array ArrayStride 4
8446 OpDecorate %block Block
8447 OpMemberDecorate %block 0 Offset 0
8448 %void = OpTypeVoid
8449 %int = OpTypeInt 32 0
8450 %int_4 = OpConstant %int 4
8451 %int_0 = OpConstant %int 0
8452 %float = OpTypeFloat 32
8453 %float_0 = OpConstant %float 0
8454 %array = OpTypeArray %int %int_4
8455 %block = OpTypeStruct %array
8456 %ptr_block = OpTypeUntypedPointerKHR Workgroup
8457 %ptr_int = OpTypeUntypedPointerKHR Workgroup
8458 %var = OpUntypedVariableKHR %ptr_block Workgroup %block
8459 %void_fn = OpTypeFunction %void
8460 %main = OpFunction %void None %void_fn
8461 %entry = OpLabel
8462 %gep = OpUntypedAccessChainKHR %ptr_int %block %var %int_0 %int_0
8463 %ptr_gep = OpUntypedPtrAccessChainKHR %ptr_int %int %gep %float_0
8464 OpReturn
8465 OpFunctionEnd
8466 )";
8467
8468 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_2);
8469 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_2));
8470 EXPECT_THAT(getDiagnosticString(), HasSubstr("Element must be an integer"));
8471 }
8472
8473 TEST_F(ValidateMemory, PtrAccessChainElementBlockArrayNonZeroConstant) {
8474 const std::string spirv = R"(
8475 OpCapability Shader
8476 OpCapability VariablePointers
8477 OpMemoryModel Logical GLSL450
8478 OpEntryPoint GLCompute %main "main"
8479 OpExecutionMode %main LocalSize 1 1 1
8480 OpDecorate %var DescriptorSet 0
8481 OpDecorate %var Binding 0
8482 OpDecorate %block Block
8483 OpMemberDecorate %block 0 Offset 0
8484 %void = OpTypeVoid
8485 %int = OpTypeInt 32 0
8486 %int_4 = OpConstant %int 4
8487 %int_1 = OpConstant %int 1
8488 %int_0 = OpConstant %int 0
8489 %block = OpTypeStruct %int
8490 %array = OpTypeArray %block %int_4
8491 %ptr_array = OpTypePointer StorageBuffer %array
8492 %ptr_block = OpTypePointer StorageBuffer %block
8493 %var = OpVariable %ptr_array StorageBuffer
8494 %void_fn = OpTypeFunction %void
8495 %main = OpFunction %void None %void_fn
8496 %entry = OpLabel
8497 %gep = OpAccessChain %ptr_block %var %int_0
8498 %ptr_gep = OpPtrAccessChain %ptr_block %gep %int_1
8499 OpReturn
8500 OpFunctionEnd
8501 )";
8502
8503 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
8504 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_1));
8505 EXPECT_THAT(getDiagnosticString(),
8506 HasSubstr("Element must be 0 if the interpretation type is a "
8507 "Block- or BufferBlock-decorated structure"));
8508 }
8509
8510 TEST_F(ValidateMemory, PtrAccessChainElementBlockArrayNonZeroConstantUntyped) {
8511 const std::string spirv = R"(
8512 OpCapability Shader
8513 OpCapability VariablePointers
8514 OpCapability UntypedPointersKHR
8515 OpExtension "SPV_KHR_untyped_pointers"
8516 OpMemoryModel Logical GLSL450
8517 OpEntryPoint GLCompute %main "main"
8518 OpExecutionMode %main LocalSize 1 1 1
8519 OpDecorate %var DescriptorSet 0
8520 OpDecorate %var Binding 0
8521 OpDecorate %block Block
8522 OpMemberDecorate %block 0 Offset 0
8523 %void = OpTypeVoid
8524 %int = OpTypeInt 32 0
8525 %int_4 = OpConstant %int 4
8526 %int_1 = OpConstant %int 1
8527 %int_0 = OpConstant %int 0
8528 %block = OpTypeStruct %int
8529 %array = OpTypeArray %block %int_4
8530 %ptr_array = OpTypeUntypedPointerKHR StorageBuffer
8531 %ptr_block = OpTypeUntypedPointerKHR StorageBuffer
8532 %var = OpUntypedVariableKHR %ptr_array StorageBuffer %array
8533 %void_fn = OpTypeFunction %void
8534 %main = OpFunction %void None %void_fn
8535 %entry = OpLabel
8536 %gep = OpUntypedAccessChainKHR %ptr_block %array %var %int_0
8537 %ptr_gep = OpUntypedPtrAccessChainKHR %ptr_block %block %gep %int_1
8538 OpReturn
8539 OpFunctionEnd
8540 )";
8541
8542 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
8543 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_1));
8544 EXPECT_THAT(getDiagnosticString(),
8545 HasSubstr("Element must be 0 if the interpretation type is a "
8546 "Block- or BufferBlock-decorated structure"));
8547 }
8548
8549 TEST_F(ValidateMemory, UntypedAccessChainBlockArrayMismatch1) {
8550 const std::string spirv = R"(
8551 OpCapability Shader
8552 OpCapability UntypedPointersKHR
8553 OpCapability VariablePointers
8554 OpExtension "SPV_KHR_untyped_pointers"
8555 OpMemoryModel Logical GLSL450
8556 OpEntryPoint GLCompute %main "main"
8557 OpExecutionMode %main LocalSize 1 1 1
8558 OpDecorate %var DescriptorSet 0
8559 OpDecorate %var Binding 0
8560 OpDecorate %block Block
8561 OpMemberDecorate %block 0 Offset 0
8562 %void = OpTypeVoid
8563 %int = OpTypeInt 32 0
8564 %int_1 = OpConstant %int 1
8565 %int_4 = OpConstant %int 4
8566 %int_0 = OpConstant %int 0
8567 %block = OpTypeStruct %int
8568 %array1 = OpTypeArray %block %int_4
8569 %array2 = OpTypeArray %block %int_4
8570 %ptr = OpTypeUntypedPointerKHR StorageBuffer
8571 %var = OpUntypedVariableKHR %ptr StorageBuffer %array1
8572 %void_fn = OpTypeFunction %void
8573 %main = OpFunction %void None %void_fn
8574 %entry = OpLabel
8575 %gep = OpUntypedAccessChainKHR %ptr %array2 %var
8576 OpReturn
8577 OpFunctionEnd
8578 )";
8579
8580 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
8581 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
8582 EXPECT_THAT(getDiagnosticString(),
8583 HasSubstr("If Base or Base Type is a Block or BufferBlock array, "
8584 "the other must also be the same array"));
8585 }
8586
8587 TEST_F(ValidateMemory, UntypedAccessChainBlockArrayMismatch2) {
8588 const std::string spirv = R"(
8589 OpCapability Shader
8590 OpCapability UntypedPointersKHR
8591 OpCapability VariablePointers
8592 OpExtension "SPV_KHR_untyped_pointers"
8593 OpMemoryModel Logical GLSL450
8594 OpEntryPoint GLCompute %main "main"
8595 OpExecutionMode %main LocalSize 1 1 1
8596 OpDecorate %var DescriptorSet 0
8597 OpDecorate %var Binding 0
8598 OpDecorate %block Block
8599 OpMemberDecorate %block 0 Offset 0
8600 %void = OpTypeVoid
8601 %int = OpTypeInt 32 0
8602 %int_1 = OpConstant %int 1
8603 %int_4 = OpConstant %int 4
8604 %int_0 = OpConstant %int 0
8605 %block = OpTypeStruct %int
8606 %array1 = OpTypeArray %block %int_4
8607 %ptr = OpTypeUntypedPointerKHR StorageBuffer
8608 %var = OpUntypedVariableKHR %ptr StorageBuffer %array1
8609 %void_fn = OpTypeFunction %void
8610 %main = OpFunction %void None %void_fn
8611 %entry = OpLabel
8612 %gep = OpUntypedAccessChainKHR %ptr %block %var
8613 OpReturn
8614 OpFunctionEnd
8615 )";
8616
8617 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
8618 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
8619 EXPECT_THAT(getDiagnosticString(),
8620 HasSubstr("Both Base Type and Base must be Block or BufferBlock "
8621 "arrays or neither can be"));
8622 }
8623
8624 TEST_F(ValidateMemory, UntypedAccessChainBlockArrayMismatch3) {
8625 const std::string spirv = R"(
8626 OpCapability Shader
8627 OpCapability UntypedPointersKHR
8628 OpCapability VariablePointers
8629 OpExtension "SPV_KHR_untyped_pointers"
8630 OpMemoryModel Logical GLSL450
8631 OpEntryPoint GLCompute %main "main"
8632 OpExecutionMode %main LocalSize 1 1 1
8633 OpDecorate %var DescriptorSet 0
8634 OpDecorate %var Binding 0
8635 OpDecorate %block Block
8636 OpMemberDecorate %block 0 Offset 0
8637 %void = OpTypeVoid
8638 %int = OpTypeInt 32 0
8639 %int_1 = OpConstant %int 1
8640 %int_4 = OpConstant %int 4
8641 %int_0 = OpConstant %int 0
8642 %block = OpTypeStruct %int
8643 %array1 = OpTypeArray %block %int_4
8644 %array2 = OpTypeArray %block %int_4
8645 %ptr = OpTypeUntypedPointerKHR StorageBuffer
8646 %ptr_block_array = OpTypePointer StorageBuffer %array1
8647 %var = OpVariable %ptr_block_array StorageBuffer
8648 %void_fn = OpTypeFunction %void
8649 %main = OpFunction %void None %void_fn
8650 %entry = OpLabel
8651 %copy1 = OpCopyObject %ptr_block_array %var
8652 %copy2 = OpCopyObject %ptr_block_array %copy1
8653 %gep = OpUntypedAccessChainKHR %ptr %array2 %copy2
8654 OpReturn
8655 OpFunctionEnd
8656 )";
8657
8658 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
8659 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
8660 EXPECT_THAT(getDiagnosticString(),
8661 HasSubstr("If Base or Base Type is a Block or BufferBlock array, "
8662 "the other must also be the same array"));
8663 }
8664
8665 TEST_F(ValidateMemory, UntypedAccessChainBlockArrayMismatch4) {
8666 const std::string spirv = R"(
8667 OpCapability Shader
8668 OpCapability UntypedPointersKHR
8669 OpCapability VariablePointers
8670 OpExtension "SPV_KHR_untyped_pointers"
8671 OpMemoryModel Logical GLSL450
8672 OpEntryPoint GLCompute %main "main"
8673 OpExecutionMode %main LocalSize 1 1 1
8674 OpDecorate %var DescriptorSet 0
8675 OpDecorate %var Binding 0
8676 OpDecorate %block Block
8677 OpMemberDecorate %block 0 Offset 0
8678 %void = OpTypeVoid
8679 %int = OpTypeInt 32 0
8680 %int_1 = OpConstant %int 1
8681 %int_4 = OpConstant %int 4
8682 %int_0 = OpConstant %int 0
8683 %block = OpTypeStruct %int
8684 %array1 = OpTypeArray %block %int_4
8685 %ptr = OpTypeUntypedPointerKHR StorageBuffer
8686 %ptr_block_array = OpTypePointer StorageBuffer %array1
8687 %var = OpVariable %ptr_block_array StorageBuffer
8688 %void_fn = OpTypeFunction %void
8689 %main = OpFunction %void None %void_fn
8690 %entry = OpLabel
8691 %copy = OpCopyObject %ptr_block_array %var
8692 %gep = OpUntypedAccessChainKHR %ptr %block %copy
8693 OpReturn
8694 OpFunctionEnd
8695 )";
8696
8697 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
8698 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
8699 EXPECT_THAT(getDiagnosticString(),
8700 HasSubstr("Both Base Type and Base must be Block or BufferBlock "
8701 "arrays or neither can be"));
8702 }
8703
8704 TEST_F(ValidateMemory, VariableFunctionPointer) {
8705 const std::string spirv = R"(
8706 OpCapability Shader
8707 OpMemoryModel Logical GLSL450
8708 OpEntryPoint GLCompute %main "main"
8709 OpExecutionMode %main LocalSize 1 1 1
8710 %void = OpTypeVoid
8711 %void_fn = OpTypeFunction %void
8712 %int = OpTypeInt 32 0
8713 %ptr = OpTypePointer Function %int
8714 %ptr_ptr = OpTypePointer Function %ptr
8715 %main = OpFunction %void None %void_fn
8716 %entry = OpLabel
8717 %var = OpVariable %ptr_ptr Function
8718 OpReturn
8719 OpFunctionEnd
8720 )";
8721
8722 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
8723 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
8724 EXPECT_THAT(
8725 getDiagnosticString(),
8726 HasSubstr("In Logical addressing, variables can only allocate a pointer "
8727 "to the StorageBuffer or Workgroup storage classes"));
8728 }
8729
8730 TEST_F(ValidateMemory, VariableFunctionPointerUntyped) {
8731 const std::string spirv = R"(
8732 OpCapability Shader
8733 OpCapability UntypedPointersKHR
8734 OpExtension "SPV_KHR_untyped_pointers"
8735 OpMemoryModel Logical GLSL450
8736 OpEntryPoint GLCompute %main "main"
8737 OpExecutionMode %main LocalSize 1 1 1
8738 %void = OpTypeVoid
8739 %void_fn = OpTypeFunction %void
8740 %int = OpTypeInt 32 0
8741 %ptr = OpTypeUntypedPointerKHR Function
8742 %main = OpFunction %void None %void_fn
8743 %entry = OpLabel
8744 %var = OpUntypedVariableKHR %ptr Function %ptr
8745 OpReturn
8746 OpFunctionEnd
8747 )";
8748
8749 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
8750 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
8751 EXPECT_THAT(
8752 getDiagnosticString(),
8753 HasSubstr("In Logical addressing, variables can only allocate a pointer "
8754 "to the StorageBuffer or Workgroup storage classes"));
8755 }
8756
8757 TEST_F(ValidateMemory, VariableStorageBufferPointer) {
8758 const std::string spirv = R"(
8759 OpCapability Shader
8760 OpMemoryModel Logical GLSL450
8761 OpEntryPoint GLCompute %main "main"
8762 OpExecutionMode %main LocalSize 1 1 1
8763 %void = OpTypeVoid
8764 %void_fn = OpTypeFunction %void
8765 %int = OpTypeInt 32 0
8766 %ptr = OpTypePointer StorageBuffer %int
8767 %ptr_ptr = OpTypePointer Function %ptr
8768 %main = OpFunction %void None %void_fn
8769 %entry = OpLabel
8770 %var = OpVariable %ptr_ptr Function
8771 OpReturn
8772 OpFunctionEnd
8773 )";
8774
8775 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
8776 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
8777 EXPECT_THAT(
8778 getDiagnosticString(),
8779 HasSubstr(
8780 "In Logical addressing, variables can only allocate a storage "
8781 "buffer pointer if the VariablePointersStorageBuffer capability "
8782 "is declared"));
8783 }
8784
8785 TEST_F(ValidateMemory, VariableStorageBufferPointerUntyped) {
8786 const std::string spirv = R"(
8787 OpCapability Shader
8788 OpCapability UntypedPointersKHR
8789 OpExtension "SPV_KHR_untyped_pointers"
8790 OpMemoryModel Logical GLSL450
8791 OpEntryPoint GLCompute %main "main"
8792 OpExecutionMode %main LocalSize 1 1 1
8793 %void = OpTypeVoid
8794 %void_fn = OpTypeFunction %void
8795 %int = OpTypeInt 32 0
8796 %ptr_s = OpTypeUntypedPointerKHR StorageBuffer
8797 %ptr_f = OpTypeUntypedPointerKHR Function
8798 %main = OpFunction %void None %void_fn
8799 %entry = OpLabel
8800 %var = OpUntypedVariableKHR %ptr_f Function %ptr_s
8801 OpReturn
8802 OpFunctionEnd
8803 )";
8804
8805 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
8806 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
8807 EXPECT_THAT(
8808 getDiagnosticString(),
8809 HasSubstr(
8810 "In Logical addressing, variables can only allocate a storage "
8811 "buffer pointer if the VariablePointersStorageBuffer capability "
8812 "is declared"));
8813 }
8814
8815 TEST_F(ValidateMemory, VariableWorkgroupPointer) {
8816 const std::string spirv = R"(
8817 OpCapability Shader
8818 OpMemoryModel Logical GLSL450
8819 OpEntryPoint GLCompute %main "main"
8820 OpExecutionMode %main LocalSize 1 1 1
8821 %void = OpTypeVoid
8822 %void_fn = OpTypeFunction %void
8823 %int = OpTypeInt 32 0
8824 %ptr = OpTypePointer Workgroup %int
8825 %ptr_ptr = OpTypePointer Function %ptr
8826 %main = OpFunction %void None %void_fn
8827 %entry = OpLabel
8828 %var = OpVariable %ptr_ptr Function
8829 OpReturn
8830 OpFunctionEnd
8831 )";
8832
8833 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
8834 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
8835 EXPECT_THAT(
8836 getDiagnosticString(),
8837 HasSubstr(
8838 "In Logical addressing, variables can only allocate a workgroup "
8839 "pointer if the VariablePointers capability is declared"));
8840 }
8841
8842 TEST_F(ValidateMemory, VariableWorkgroupPointerUntyped) {
8843 const std::string spirv = R"(
8844 OpCapability Shader
8845 OpCapability UntypedPointersKHR
8846 OpExtension "SPV_KHR_untyped_pointers"
8847 OpMemoryModel Logical GLSL450
8848 OpEntryPoint GLCompute %main "main"
8849 OpExecutionMode %main LocalSize 1 1 1
8850 %void = OpTypeVoid
8851 %void_fn = OpTypeFunction %void
8852 %int = OpTypeInt 32 0
8853 %ptr_w = OpTypeUntypedPointerKHR Workgroup
8854 %ptr_f = OpTypeUntypedPointerKHR Function
8855 %main = OpFunction %void None %void_fn
8856 %entry = OpLabel
8857 %var = OpUntypedVariableKHR %ptr_f Function %ptr_w
8858 OpReturn
8859 OpFunctionEnd
8860 )";
8861
8862 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
8863 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
8864 EXPECT_THAT(
8865 getDiagnosticString(),
8866 HasSubstr(
8867 "In Logical addressing, variables can only allocate a workgroup "
8868 "pointer if the VariablePointers capability is declared"));
8869 }
8870
8871 TEST_F(ValidateMemory, VariablePointerBadStorageClass) {
8872 const std::string spirv = R"(
8873 OpCapability Shader
8874 OpCapability VariablePointers
8875 OpMemoryModel Logical GLSL450
8876 OpEntryPoint GLCompute %main "main"
8877 OpExecutionMode %main LocalSize 1 1 1
8878 %void = OpTypeVoid
8879 %void_fn = OpTypeFunction %void
8880 %int = OpTypeInt 32 0
8881 %ptr = OpTypePointer Workgroup %int
8882 %ptr_ptr = OpTypePointer Workgroup %ptr
8883 %var = OpVariable %ptr_ptr Workgroup
8884 %main = OpFunction %void None %void_fn
8885 %entry = OpLabel
8886 OpReturn
8887 OpFunctionEnd
8888 )";
8889
8890 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
8891 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
8892 EXPECT_THAT(
8893 getDiagnosticString(),
8894 HasSubstr(
8895 "In Logical addressing with variable pointers, variables that "
8896 "allocate pointers must be in Function or Private storage classes"));
8897 }
8898
8899 TEST_F(ValidateMemory, VariablePointerBadStorageClassUntyped) {
8900 const std::string spirv = R"(
8901 OpCapability Shader
8902 OpCapability UntypedPointersKHR
8903 OpCapability VariablePointers
8904 OpExtension "SPV_KHR_untyped_pointers"
8905 OpMemoryModel Logical GLSL450
8906 OpEntryPoint GLCompute %main "main"
8907 OpExecutionMode %main LocalSize 1 1 1
8908 %void = OpTypeVoid
8909 %void_fn = OpTypeFunction %void
8910 %int = OpTypeInt 32 0
8911 %ptr_w = OpTypeUntypedPointerKHR Workgroup
8912 %var = OpUntypedVariableKHR %ptr_w Workgroup %ptr_w
8913 %main = OpFunction %void None %void_fn
8914 %entry = OpLabel
8915 OpReturn
8916 OpFunctionEnd
8917 )";
8918
8919 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
8920 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
8921 EXPECT_THAT(
8922 getDiagnosticString(),
8923 HasSubstr(
8924 "In Logical addressing with variable pointers, variables that "
8925 "allocate pointers must be in Function or Private storage classes"));
8926 }
8927
8928 TEST_F(ValidateMemory, AccessChainNegativeSignedIndex) {
8929 const std::string spirv = R"(
8930 OpCapability Shader
8931 OpMemoryModel Logical GLSL450
8932 OpEntryPoint GLCompute %main "main"
8933 OpExecutionMode %main LocalSize 1 1 1
8934 %void = OpTypeVoid
8935 %void_fn = OpTypeFunction %void
8936 %int = OpTypeInt 32 1
8937 %int_n1 = OpConstant %int -1
8938 %int_4 = OpConstant %int 4
8939 %array = OpTypeArray %int %int_4
8940 %ptr_array = OpTypePointer Workgroup %array
8941 %ptr_int = OpTypePointer Workgroup %int
8942 %var = OpVariable %ptr_array Workgroup
8943 %main = OpFunction %void None %void_fn
8944 %entry = OpLabel
8945 %gep = OpAccessChain %ptr_int %var %int_n1
8946 OpReturn
8947 OpFunctionEnd
8948 )";
8949
8950 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
8951 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
8952 EXPECT_THAT(getDiagnosticString(),
8953 HasSubstr("Index at word 4 may not have a negative value"));
8954 }
8955
8956 std::string GenCoopMat2Shader(const std::string& extra_types,
8957 const std::string& main_body,
8958 const std::string& after_main = "",
8959 const std::string& extra_decorations = "") {
8960 const std::string prefix = R"(
8961 OpCapability Shader
8962 OpCapability Float16
8963 OpCapability PhysicalStorageBufferAddresses
8964 OpCapability VulkanMemoryModel
8965 OpCapability CooperativeMatrixKHR
8966 OpCapability TensorAddressingNV
8967 OpCapability CooperativeMatrixTensorAddressingNV
8968 OpCapability CooperativeMatrixBlockLoadsNV
8969 OpExtension "SPV_KHR_physical_storage_buffer"
8970 OpExtension "SPV_KHR_storage_buffer_storage_class"
8971 OpExtension "SPV_NV_tensor_addressing"
8972 OpExtension "SPV_NV_cooperative_matrix2"
8973 OpExtension "SPV_KHR_cooperative_matrix"
8974 OpExtension "SPV_KHR_vulkan_memory_model"
8975 OpMemoryModel Logical VulkanKHR
8976 OpEntryPoint GLCompute %main "main"
8977 OpExecutionMode %main LocalSize 1 1 1
8978
8979 OpDecorate %f16_arr ArrayStride 2
8980 OpDecorate %46 Block
8981 OpMemberDecorate %46 0 Offset 0
8982 OpDecorate %48 Binding 0
8983 OpDecorate %48 DescriptorSet 0
8984 OpDecorate %psb Restrict
8985 )" + extra_decorations + R"(
8986 %void = OpTypeVoid
8987 %bool = OpTypeBool
8988 %func = OpTypeFunction %void
8989 %f16 = OpTypeFloat 16
8990 %f32 = OpTypeFloat 32
8991 %u32 = OpTypeInt 32 0
8992 %s32 = OpTypeInt 32 1
8993
8994 %s32_0 = OpConstant %s32 0
8995 %f16_0 = OpConstant %f16 0
8996 %u32_2 = OpConstant %u32 2
8997 %u32_8 = OpConstant %u32 8
8998 %use_A = OpConstant %u32 0
8999 %workgroup = OpConstant %u32 2
9000 %subgroup = OpConstant %u32 3
9001
9002 %f16_arr = OpTypeRuntimeArray %f16
9003 %46 = OpTypeStruct %f16_arr
9004 %47 = OpTypePointer StorageBuffer %46
9005 %48 = OpVariable %47 StorageBuffer
9006 %51 = OpTypePointer StorageBuffer %f16_arr
9007 %psbptr = OpTypePointer PhysicalStorageBuffer %f16_arr
9008
9009 %f16mat = OpTypeCooperativeMatrixKHR %f16 %workgroup %u32_8 %u32_8 %use_A
9010 %f32mat = OpTypeCooperativeMatrixKHR %f32 %subgroup %u32_8 %u32_8 %use_A
9011
9012 %arr2 = OpTypeArray %u32 %u32_2
9013 %functy = OpTypeFunction %f16 %psbptr %arr2 %arr2
9014 )";
9015
9016 const std::string decode_func =
9017 R"(
9018 %decodefunc = OpFunction %f16 None %functy
9019 %psb = OpFunctionParameter %psbptr
9020 %c0 = OpFunctionParameter %arr2
9021 %c1 = OpFunctionParameter %arr2
9022 %entry2 = OpLabel
9023 OpReturnValue %f16_0
9024 OpFunctionEnd
9025 )";
9026
9027 const std::string func_begin =
9028 R"(
9029 %main = OpFunction %void None %func
9030 %main_entry = OpLabel
9031
9032 %array_ptr = OpAccessChain %51 %48 %s32_0
9033 )";
9034
9035 const std::string suffix =
9036 R"(
9037 OpReturn
9038 OpFunctionEnd)";
9039
9040 return prefix + extra_types + func_begin + main_body + suffix + decode_func +
9041 after_main;
9042 }
9043
9044 TEST_F(ValidateMemory, CoopMat2TensorLayoutAndViewSuccess) {
9045 std::string spirv = GenCoopMat2Shader(
9046 R"(
9047 %clamp = OpConstant %u32 0
9048 %dim = OpConstant %u32 2
9049 %p0 = OpConstant %u32 0
9050 %p1 = OpConstant %u32 1
9051 %hasdim = OpConstantFalse %bool
9052 %layout = OpTypeTensorLayoutNV %dim %clamp
9053 %view = OpTypeTensorViewNV %dim %hasdim %p0 %p1
9054 )",
9055 R"(
9056 )");
9057
9058 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9059 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9060 }
9061
9062 TEST_F(ValidateMemory, CoopMat2TensorLayoutInvalidDimFail) {
9063 std::string spirv = GenCoopMat2Shader(
9064 R"(
9065 %clamp = OpConstant %u32 0
9066 %dim = OpConstant %u32 6
9067 %layout = OpTypeTensorLayoutNV %dim %clamp
9068 )",
9069 R"(
9070 )");
9071
9072 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9073 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9074 EXPECT_THAT(getDiagnosticString(), HasSubstr("must be between 1 and 5"));
9075 }
9076
9077 TEST_F(ValidateMemory, CoopMat2TensorLayoutInvalidClampFail) {
9078 std::string spirv = GenCoopMat2Shader(
9079 R"(
9080 %clamp = OpConstant %u32 6
9081 %dim = OpConstant %u32 2
9082 %layout = OpTypeTensorLayoutNV %dim %clamp
9083 )",
9084 R"(
9085 )");
9086
9087 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9088 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9089 EXPECT_THAT(getDiagnosticString(),
9090 HasSubstr("must be a valid TensorClampMode"));
9091 }
9092
9093 TEST_F(ValidateMemory, CoopMat2TensorViewInvalidDimFail) {
9094 std::string spirv = GenCoopMat2Shader(
9095 R"(
9096 %dim = OpConstant %u32 6
9097 %p0 = OpConstant %u32 0
9098 %p1 = OpConstant %u32 1
9099 %hasdim = OpConstantFalse %bool
9100 %view = OpTypeTensorViewNV %dim %hasdim %p0 %p1
9101 )",
9102 R"(
9103 )");
9104
9105 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9106 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9107 EXPECT_THAT(getDiagnosticString(), HasSubstr("must be between 1 and 5"));
9108 }
9109
9110 TEST_F(ValidateMemory, CoopMat2TensorViewInvalidPermutationFail) {
9111 std::string spirv = GenCoopMat2Shader(
9112 R"(
9113 %dim = OpConstant %u32 3
9114 %p0 = OpConstant %u32 0
9115 %p1 = OpConstant %u32 1
9116 %hasdim = OpConstantFalse %bool
9117 %view = OpTypeTensorViewNV %dim %hasdim %p0 %p1 %p1
9118 )",
9119 R"(
9120 )");
9121
9122 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9123 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9124 EXPECT_THAT(getDiagnosticString(),
9125 HasSubstr("Permutation values don't form a valid permutation"));
9126 }
9127
9128 TEST_F(ValidateMemory, CoopMat2TensorViewInvalidPermutation2Fail) {
9129 std::string spirv = GenCoopMat2Shader(
9130 R"(
9131 %dim = OpConstant %u32 3
9132 %p0 = OpConstant %u32 0
9133 %p1 = OpConstant %u32 1
9134 %hasdim = OpConstantFalse %bool
9135 %view = OpTypeTensorViewNV %dim %hasdim %p0 %p1
9136 )",
9137 R"(
9138 )");
9139
9140 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9141 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9142 EXPECT_THAT(getDiagnosticString(),
9143 HasSubstr("Incorrect number of permutation values."));
9144 }
9145
9146 TEST_F(ValidateMemory, CoopMat2TensorLayoutBlockSizePass) {
9147 std::string spirv = GenCoopMat2Shader(
9148 R"(
9149 %clamp = OpConstant %u32 0
9150 %dim = OpConstant %u32 3
9151 %b = OpConstant %u32 1
9152 %layout = OpTypeTensorLayoutNV %dim %clamp
9153 )",
9154 R"(
9155 %tl = OpCreateTensorLayoutNV %layout
9156 %tl2 = OpTensorLayoutSetBlockSizeNV %layout %tl %b %b %b
9157 )");
9158
9159 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9160 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9161 }
9162
9163 TEST_F(ValidateMemory, CoopMat2TensorLayoutBlockSizeFail) {
9164 std::string spirv = GenCoopMat2Shader(
9165 R"(
9166 %clamp = OpConstant %u32 0
9167 %dim = OpConstant %u32 3
9168 %b = OpConstant %u32 1
9169 %layout = OpTypeTensorLayoutNV %dim %clamp
9170 )",
9171 R"(
9172 %tl = OpCreateTensorLayoutNV %layout
9173 %tl2 = OpTensorLayoutSetBlockSizeNV %layout %tl %b %b %b %b
9174 )");
9175
9176 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9177 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9178 EXPECT_THAT(getDiagnosticString(),
9179 HasSubstr("unexpected number of operands"));
9180 }
9181
9182 TEST_F(ValidateMemory, CoopMat2TensorLayoutDimensionPass) {
9183 std::string spirv = GenCoopMat2Shader(
9184 R"(
9185 %clamp = OpConstant %u32 0
9186 %dim = OpConstant %u32 3
9187 %b = OpConstant %u32 1
9188 %layout = OpTypeTensorLayoutNV %dim %clamp
9189 )",
9190 R"(
9191 %tl = OpCreateTensorLayoutNV %layout
9192 %tl2 = OpTensorLayoutSetDimensionNV %layout %tl %b %b %b
9193 )");
9194
9195 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9196 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9197 }
9198
9199 TEST_F(ValidateMemory, CoopMat2TensorLayoutDimensionFail) {
9200 std::string spirv = GenCoopMat2Shader(
9201 R"(
9202 %clamp = OpConstant %u32 0
9203 %dim = OpConstant %u32 3
9204 %b = OpConstant %u32 1
9205 %layout = OpTypeTensorLayoutNV %dim %clamp
9206 )",
9207 R"(
9208 %tl = OpCreateTensorLayoutNV %layout
9209 %tl2 = OpTensorLayoutSetDimensionNV %layout %tl %b %b %b %b
9210 )");
9211
9212 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9213 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9214 EXPECT_THAT(getDiagnosticString(),
9215 HasSubstr("unexpected number of operands"));
9216 }
9217
9218 TEST_F(ValidateMemory, CoopMat2TensorLayoutStridePass) {
9219 std::string spirv = GenCoopMat2Shader(
9220 R"(
9221 %clamp = OpConstant %u32 0
9222 %dim = OpConstant %u32 3
9223 %b = OpConstant %u32 1
9224 %layout = OpTypeTensorLayoutNV %dim %clamp
9225 )",
9226 R"(
9227 %tl = OpCreateTensorLayoutNV %layout
9228 %tl2 = OpTensorLayoutSetStrideNV %layout %tl %b %b %b
9229 )");
9230
9231 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9232 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9233 }
9234
9235 TEST_F(ValidateMemory, CoopMat2TensorLayoutStrideFail) {
9236 std::string spirv = GenCoopMat2Shader(
9237 R"(
9238 %clamp = OpConstant %u32 0
9239 %dim = OpConstant %u32 3
9240 %b = OpConstant %u32 1
9241 %layout = OpTypeTensorLayoutNV %dim %clamp
9242 )",
9243 R"(
9244 %tl = OpCreateTensorLayoutNV %layout
9245 %tl2 = OpTensorLayoutSetStrideNV %layout %tl %b %b %b %b
9246 )");
9247
9248 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9249 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9250 EXPECT_THAT(getDiagnosticString(),
9251 HasSubstr("unexpected number of operands"));
9252 }
9253
9254 TEST_F(ValidateMemory, CoopMat2TensorLayoutSlicePass) {
9255 std::string spirv = GenCoopMat2Shader(
9256 R"(
9257 %clamp = OpConstant %u32 0
9258 %dim = OpConstant %u32 3
9259 %b = OpConstant %u32 1
9260 %layout = OpTypeTensorLayoutNV %dim %clamp
9261 )",
9262 R"(
9263 %tl = OpCreateTensorLayoutNV %layout
9264 %tl2 = OpTensorLayoutSliceNV %layout %tl %b %b %b %b %b %b
9265 )");
9266
9267 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9268 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9269 }
9270
9271 TEST_F(ValidateMemory, CoopMat2TensorLayoutSliceFail) {
9272 std::string spirv = GenCoopMat2Shader(
9273 R"(
9274 %clamp = OpConstant %u32 0
9275 %dim = OpConstant %u32 3
9276 %b = OpConstant %u32 1
9277 %layout = OpTypeTensorLayoutNV %dim %clamp
9278 )",
9279 R"(
9280 %tl = OpCreateTensorLayoutNV %layout
9281 %tl2 = OpTensorLayoutSliceNV %layout %tl %b %b %b
9282 )");
9283
9284 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9285 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9286 EXPECT_THAT(getDiagnosticString(),
9287 HasSubstr("unexpected number of operands"));
9288 }
9289
9290 TEST_F(ValidateMemory, CoopMat2TensorLayoutSetClampValuePass) {
9291 std::string spirv = GenCoopMat2Shader(
9292 R"(
9293 %clamp = OpConstant %u32 0
9294 %dim = OpConstant %u32 3
9295 %b = OpConstant %u32 1
9296 %layout = OpTypeTensorLayoutNV %dim %clamp
9297 )",
9298 R"(
9299 %tl = OpCreateTensorLayoutNV %layout
9300 %tl2 = OpTensorLayoutSetClampValueNV %layout %tl %b
9301 )");
9302
9303 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9304 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9305 }
9306
9307 TEST_F(ValidateMemory, CoopMat2TensorViewDimensionPass) {
9308 std::string spirv = GenCoopMat2Shader(
9309 R"(
9310 %dim = OpConstant %u32 3
9311 %hasdim = OpConstantFalse %bool
9312 %p0 = OpConstant %u32 0
9313 %p1 = OpConstant %u32 1
9314 %p2 = OpConstant %u32 2
9315 %view = OpTypeTensorViewNV %dim %hasdim %p0 %p1 %p2
9316 %b = OpConstant %u32 1
9317 )",
9318 R"(
9319 %tv = OpCreateTensorViewNV %view
9320 %tv2 = OpTensorViewSetDimensionNV %view %tv %b %b %b
9321 )");
9322
9323 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9324 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9325 }
9326
9327 TEST_F(ValidateMemory, CoopMat2TensorViewDimensionFail) {
9328 std::string spirv = GenCoopMat2Shader(
9329 R"(
9330 %dim = OpConstant %u32 3
9331 %hasdim = OpConstantFalse %bool
9332 %p0 = OpConstant %u32 0
9333 %p1 = OpConstant %u32 1
9334 %p2 = OpConstant %u32 2
9335 %view = OpTypeTensorViewNV %dim %hasdim %p0 %p1 %p2
9336 %b = OpConstant %u32 1
9337 )",
9338 R"(
9339 %tv = OpCreateTensorViewNV %view
9340 %tv2 = OpTensorViewSetDimensionNV %view %tv %b %b %b %b
9341 )");
9342
9343 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9344 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9345 EXPECT_THAT(getDiagnosticString(),
9346 HasSubstr("unexpected number of operands"));
9347 }
9348
9349 TEST_F(ValidateMemory, CoopMat2TensorViewStridePass) {
9350 std::string spirv = GenCoopMat2Shader(
9351 R"(
9352 %dim = OpConstant %u32 3
9353 %hasdim = OpConstantFalse %bool
9354 %p0 = OpConstant %u32 0
9355 %p1 = OpConstant %u32 1
9356 %p2 = OpConstant %u32 2
9357 %view = OpTypeTensorViewNV %dim %hasdim %p0 %p1 %p2
9358 %b = OpConstant %u32 1
9359 )",
9360 R"(
9361 %tv = OpCreateTensorViewNV %view
9362 %tv2 = OpTensorViewSetStrideNV %view %tv %b %b %b
9363 )");
9364
9365 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9366 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9367 }
9368
9369 TEST_F(ValidateMemory, CoopMat2TensorViewStrideFail) {
9370 std::string spirv = GenCoopMat2Shader(
9371 R"(
9372 %dim = OpConstant %u32 3
9373 %hasdim = OpConstantFalse %bool
9374 %p0 = OpConstant %u32 0
9375 %p1 = OpConstant %u32 1
9376 %p2 = OpConstant %u32 2
9377 %view = OpTypeTensorViewNV %dim %hasdim %p0 %p1 %p2
9378 %b = OpConstant %u32 1
9379 )",
9380 R"(
9381 %tv = OpCreateTensorViewNV %view
9382 %tv2 = OpTensorViewSetStrideNV %view %tv %b %b %b %b
9383 )");
9384
9385 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9386 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9387 EXPECT_THAT(getDiagnosticString(),
9388 HasSubstr("unexpected number of operands"));
9389 }
9390
9391 TEST_F(ValidateMemory, CoopMat2TensorViewClipPass) {
9392 std::string spirv = GenCoopMat2Shader(
9393 R"(
9394 %dim = OpConstant %u32 3
9395 %hasdim = OpConstantFalse %bool
9396 %p0 = OpConstant %u32 0
9397 %p1 = OpConstant %u32 1
9398 %p2 = OpConstant %u32 2
9399 %view = OpTypeTensorViewNV %dim %hasdim %p0 %p1 %p2
9400 %b = OpConstant %u32 1
9401 )",
9402 R"(
9403 %tv = OpCreateTensorViewNV %view
9404 %tv2 = OpTensorViewSetClipNV %view %tv %b %b %b %b
9405 )");
9406
9407 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9408 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9409 }
9410
9411 TEST_F(ValidateMemory, CoopMat2LoadStoreTensorPass) {
9412 std::string spirv = GenCoopMat2Shader(
9413 R"(
9414 %clamp = OpConstant %u32 0
9415 %dim = OpConstant %u32 2
9416 %p0 = OpConstant %u32 0
9417 %p1 = OpConstant %u32 1
9418 %hasdim = OpConstantFalse %bool
9419 %layout = OpTypeTensorLayoutNV %dim %clamp
9420 %view = OpTypeTensorViewNV %dim %hasdim %p0 %p1
9421 )",
9422 R"(
9423 %mat = OpUndef %f16mat
9424 %tl = OpCreateTensorLayoutNV %layout
9425 %tv = OpCreateTensorViewNV %view
9426 %mat2 = OpCooperativeMatrixLoadTensorNV %f16mat %array_ptr %mat %tl None None
9427 %mat3 = OpCooperativeMatrixLoadTensorNV %f16mat %array_ptr %mat %tl Aligned 4 None
9428 %mat4 = OpCooperativeMatrixLoadTensorNV %f16mat %array_ptr %mat %tl None TensorView %tv
9429 %mat5 = OpCooperativeMatrixLoadTensorNV %f16mat %array_ptr %mat %tl None DecodeFunc %decodefunc
9430 %mat6 = OpCooperativeMatrixLoadTensorNV %f16mat %array_ptr %mat %tl None TensorView|DecodeFunc %tv %decodefunc
9431 %mat7 = OpCooperativeMatrixLoadTensorNV %f16mat %array_ptr %mat %tl Aligned 4 TensorView|DecodeFunc %tv %decodefunc
9432 OpCooperativeMatrixStoreTensorNV %array_ptr %mat %tl None None
9433 OpCooperativeMatrixStoreTensorNV %array_ptr %mat %tl Aligned 4 None
9434 OpCooperativeMatrixStoreTensorNV %array_ptr %mat %tl None TensorView %tv
9435 OpCooperativeMatrixStoreTensorNV %array_ptr %mat %tl Aligned 4 TensorView %tv
9436 )");
9437
9438 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9439 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9440 }
9441
9442 TEST_F(ValidateMemory, CoopMat2LoadTensorWrongLayoutTypeFail) {
9443 std::string spirv = GenCoopMat2Shader(
9444 R"(
9445 %clamp = OpConstant %u32 0
9446 %dim = OpConstant %u32 2
9447 %p0 = OpConstant %u32 0
9448 %p1 = OpConstant %u32 1
9449 %hasdim = OpConstantFalse %bool
9450 %layout = OpTypeTensorLayoutNV %dim %clamp
9451 %view = OpTypeTensorViewNV %dim %hasdim %p0 %p1
9452 )",
9453 R"(
9454 %mat = OpUndef %f16mat
9455 %tl = OpCreateTensorLayoutNV %layout
9456 %tv = OpCreateTensorViewNV %view
9457 %mat2 = OpCooperativeMatrixLoadTensorNV %f16mat %array_ptr %mat %tv None None
9458 )");
9459
9460 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9461 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9462 EXPECT_THAT(getDiagnosticString(),
9463 HasSubstr("does not have a tensor layout type"));
9464 }
9465
9466 TEST_F(ValidateMemory, CoopMat2LoadTensorWrongObjectTypeFail) {
9467 std::string spirv = GenCoopMat2Shader(
9468 R"(
9469 %clamp = OpConstant %u32 0
9470 %dim = OpConstant %u32 2
9471 %p0 = OpConstant %u32 0
9472 %p1 = OpConstant %u32 1
9473 %hasdim = OpConstantFalse %bool
9474 %layout = OpTypeTensorLayoutNV %dim %clamp
9475 %view = OpTypeTensorViewNV %dim %hasdim %p0 %p1
9476 )",
9477 R"(
9478 %mat = OpUndef %f32mat
9479 %tl = OpCreateTensorLayoutNV %layout
9480 %tv = OpCreateTensorViewNV %view
9481 %mat2 = OpCooperativeMatrixLoadTensorNV %f16mat %array_ptr %mat %tl None None
9482 )");
9483
9484 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9485 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9486 EXPECT_THAT(getDiagnosticString(),
9487 HasSubstr("type does not match Result Type"));
9488 }
9489
9490 TEST_F(ValidateMemory, CoopMat2LoadTensorDecodeFuncTypeFail) {
9491 std::string spirv = GenCoopMat2Shader(
9492 R"(
9493 %clamp = OpConstant %u32 0
9494 %dim = OpConstant %u32 2
9495 %p0 = OpConstant %u32 0
9496 %p1 = OpConstant %u32 1
9497 %hasdim = OpConstantFalse %bool
9498 %layout = OpTypeTensorLayoutNV %dim %clamp
9499 %view = OpTypeTensorViewNV %dim %hasdim %p0 %p1
9500 )",
9501 R"(
9502 %mat = OpUndef %f32mat
9503 %tl = OpCreateTensorLayoutNV %layout
9504 %tv = OpCreateTensorViewNV %view
9505 %mat2 = OpCooperativeMatrixLoadTensorNV %f32mat %array_ptr %mat %tl None DecodeFunc %decodefunc
9506 )");
9507
9508 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9509 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9510 EXPECT_THAT(getDiagnosticString(),
9511 HasSubstr("return type must match matrix component type"));
9512 }
9513
9514 TEST_F(ValidateMemory, CoopMat2LoadTensorDecodeFuncArrayTypeFail) {
9515 std::string spirv = GenCoopMat2Shader(
9516 R"(
9517 %clamp = OpConstant %u32 0
9518 %dim = OpConstant %u32 2
9519 %u32_3 = OpConstant %u32 3
9520 %p0 = OpConstant %u32 0
9521 %p1 = OpConstant %u32 1
9522 %hasdim = OpConstantFalse %bool
9523 %layout = OpTypeTensorLayoutNV %dim %clamp
9524 %view = OpTypeTensorViewNV %dim %hasdim %p0 %p1
9525 %arr3 = OpTypeArray %u32 %u32_3
9526 %functy2 = OpTypeFunction %f16 %psbptr %arr3 %arr3
9527 )",
9528 R"(
9529 %mat = OpUndef %f16mat
9530 %tl = OpCreateTensorLayoutNV %layout
9531 %tv = OpCreateTensorViewNV %view
9532 %mat2 = OpCooperativeMatrixLoadTensorNV %f16mat %array_ptr %mat %tl None DecodeFunc %decodefunc2
9533 )",
9534 R"(
9535 %decodefunc2 = OpFunction %f16 None %functy2
9536 %psb2 = OpFunctionParameter %psbptr
9537 %c02 = OpFunctionParameter %arr3
9538 %c12 = OpFunctionParameter %arr3
9539 %entry3 = OpLabel
9540 OpReturnValue %f16_0
9541 OpFunctionEnd
9542 )",
9543 R"(
9544 OpDecorate %psb2 Restrict
9545 )");
9546
9547 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9548 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9549 EXPECT_THAT(getDiagnosticString(),
9550 HasSubstr("dimension equal to the tensor dimension"));
9551 }
9552
9553 TEST_F(ValidateMemory, CoopMat2LoadTensorDecodeFuncPointerTypeFail) {
9554 std::string spirv = GenCoopMat2Shader(
9555 R"(
9556 %clamp = OpConstant %u32 0
9557 %dim = OpConstant %u32 2
9558 %p0 = OpConstant %u32 0
9559 %p1 = OpConstant %u32 1
9560 %hasdim = OpConstantFalse %bool
9561 %layout = OpTypeTensorLayoutNV %dim %clamp
9562 %view = OpTypeTensorViewNV %dim %hasdim %p0 %p1
9563 %sbptr = OpTypePointer StorageBuffer %f16_arr
9564 %functy2 = OpTypeFunction %f16 %sbptr %arr2 %arr2
9565 )",
9566 R"(
9567 %mat = OpUndef %f16mat
9568 %tl = OpCreateTensorLayoutNV %layout
9569 %tv = OpCreateTensorViewNV %view
9570 %mat2 = OpCooperativeMatrixLoadTensorNV %f16mat %array_ptr %mat %tl None DecodeFunc %decodefunc2
9571 )",
9572 R"(
9573 %decodefunc2 = OpFunction %f16 None %functy2
9574 %sb = OpFunctionParameter %sbptr
9575 %c02 = OpFunctionParameter %arr2
9576 %c12 = OpFunctionParameter %arr2
9577 %entry3 = OpLabel
9578 OpReturnValue %f16_0
9579 OpFunctionEnd
9580 )");
9581
9582 CompileSuccessfully(spirv.c_str(), SPV_ENV_UNIVERSAL_1_3);
9583 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
9584 EXPECT_THAT(
9585 getDiagnosticString(),
9586 HasSubstr("first parameter must be pointer to PhysicalStorageBuffer"));
9587 }
9588
9589 TEST_F(ValidateMemory, PtrAccessChainNodePayloadArray) {
9590 const std::string spirv = R"(
9591 OpCapability Shader
9592 OpCapability ShaderEnqueueAMDX
9593 OpExtension "SPV_AMDX_shader_enqueue"
9594 OpMemoryModel Logical GLSL450
9595 OpEntryPoint GLCompute %main "main" %input
9596 %uint = OpTypeInt 32 0
9597 %uint_0 = OpConstant %uint 0
9598 %uint_1 = OpConstant %uint 1
9599 %node0 = OpConstantStringAMDX "node0"
9600 %node1 = OpConstantStringAMDX "node1"
9601 %node2 = OpConstantStringAMDX "node2"
9602 %S = OpTypeStruct %uint
9603 %_payloadarr_S = OpTypeNodePayloadArrayAMDX %S
9604 %_ptr_NodePayloadAMDX__payloadarr_S = OpTypePointer NodePayloadAMDX %_payloadarr_S
9605 %_ptr_NodePayloadAMDX_uint = OpTypePointer NodePayloadAMDX %uint
9606 %void = OpTypeVoid
9607 %void_fn = OpTypeFunction %void
9608 %input = OpVariable %_ptr_NodePayloadAMDX__payloadarr_S NodePayloadAMDX
9609 %main = OpFunction %void None %void_fn
9610 %entry = OpLabel
9611 %x = OpAccessChain %_ptr_NodePayloadAMDX_uint %input %uint_0 %uint_0
9612 OpReturn
9613 OpFunctionEnd
9614 )";
9615
9616 spv_target_env env = SPV_ENV_UNIVERSAL_1_4;
9617 CompileSuccessfully(spirv, env);
9618 EXPECT_THAT(SPV_SUCCESS, ValidateInstructions(env));
9619 }
9620
9621 std::string GenCoopVecLoadStoreShader(const std::string& storeMemoryAccess,
9622 const std::string& loadMemoryAccess) {
9623 std::string s = R"(
9624 OpCapability Shader
9625 OpCapability Float16
9626 OpCapability StorageBuffer16BitAccess
9627 OpCapability VulkanMemoryModel
9628 OpCapability CooperativeVectorNV
9629 OpCapability ReplicatedCompositesEXT
9630 OpExtension "SPV_EXT_replicated_composites"
9631 OpExtension "SPV_KHR_vulkan_memory_model"
9632 OpExtension "SPV_NV_cooperative_vector"
9633 %1 = OpExtInstImport "GLSL.std.450"
9634 OpMemoryModel Logical Vulkan
9635 OpEntryPoint GLCompute %4 "main" %48 %73
9636 OpExecutionMode %4 LocalSize 1 1 1
9637
9638 OpDecorate %45 ArrayStride 2
9639 OpDecorate %46 Block
9640 OpMemberDecorate %46 0 Offset 0
9641 OpDecorate %48 Binding 0
9642 OpDecorate %48 DescriptorSet 0
9643
9644 %2 = OpTypeVoid
9645 %3 = OpTypeFunction %2
9646 %6 = OpTypeInt 32 0
9647 %49 = OpTypeInt 32 1
9648 %41 = OpTypeFloat 16
9649
9650 %14 = OpConstant %6 1
9651 %50 = OpConstant %49 0
9652 %82 = OpConstant %6 5
9653
9654 %42 = OpTypeCooperativeVectorNV %41 %14
9655 %43 = OpTypePointer Function %42
9656
9657 %45 = OpTypeRuntimeArray %41
9658 %46 = OpTypeStruct %45
9659 %47 = OpTypePointer StorageBuffer %46
9660 %48 = OpVariable %47 StorageBuffer
9661 %51 = OpTypePointer StorageBuffer %45
9662
9663 %57 = OpTypePointer Private %42
9664 %73 = OpVariable %57 Private
9665
9666 %4 = OpFunction %2 None %3
9667 %5 = OpLabel
9668 %52 = OpAccessChain %51 %48 %50
9669 %56 = OpCooperativeVectorLoadNV %42 %52 %50 )" +
9670 loadMemoryAccess + R"( %82
9671 %77 = OpLoad %42 %73
9672 OpCooperativeVectorStoreNV %52 %50 %77 )" +
9673 storeMemoryAccess + R"( %82
9674 OpReturn
9675 OpFunctionEnd
9676 )";
9677
9678 return s;
9679 }
9680
9681 TEST_F(ValidateMemory, CoopVecLoadStoreSuccess) {
9682 std::string spirv =
9683 GenCoopVecLoadStoreShader("MakePointerAvailableKHR|NonPrivatePointerKHR",
9684 "MakePointerVisibleKHR|NonPrivatePointerKHR");
9685
9686 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
9687 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
9688 }
9689
9690 TEST_F(ValidateMemory, CoopVecStoreMemoryAccessFail) {
9691 std::string spirv =
9692 GenCoopVecLoadStoreShader("MakePointerVisibleKHR|NonPrivatePointerKHR",
9693 "MakePointerVisibleKHR|NonPrivatePointerKHR");
9694
9695 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
9696 ASSERT_EQ(SPV_ERROR_INVALID_ID,
9697 ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
9698 EXPECT_THAT(getDiagnosticString(),
9699 HasSubstr("MakePointerVisibleKHR cannot be used with OpStore"));
9700 }
9701
9702 TEST_F(ValidateMemory, CoopVecLoadMemoryAccessFail) {
9703 std::string spirv =
9704 GenCoopVecLoadStoreShader("MakePointerAvailableKHR|NonPrivatePointerKHR",
9705 "MakePointerAvailableKHR|NonPrivatePointerKHR");
9706
9707 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
9708 ASSERT_EQ(SPV_ERROR_INVALID_ID,
9709 ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
9710 EXPECT_THAT(getDiagnosticString(),
9711 HasSubstr("MakePointerAvailableKHR cannot be used with OpLoad"));
9712 }
9713
9714 TEST_F(ValidateMemory, CoopVecInvalidStorageClassFail) {
9715 const std::string body = R"(
9716 OpCapability Shader
9717 OpCapability Float16
9718 OpCapability CooperativeVectorNV
9719 OpCapability ReplicatedCompositesEXT
9720 OpExtension "SPV_NV_cooperative_vector"
9721 OpExtension "SPV_EXT_replicated_composites"
9722 OpExtension "SPV_KHR_vulkan_memory_model"
9723 OpMemoryModel Logical GLSL450
9724 OpEntryPoint GLCompute %main "main"
9725 OpExecutionMode %main LocalSize 1 1 1
9726 %void = OpTypeVoid
9727 %func = OpTypeFunction %void
9728 %f16 = OpTypeFloat 16
9729 %u32 = OpTypeInt 32 0
9730
9731 %u32_8 = OpConstant %u32 8
9732 %use_A = OpConstant %u32 0
9733 %subgroup = OpConstant %u32 3
9734
9735 %f16vec = OpTypeCooperativeVectorNV %f16 %u32_8
9736
9737 %str = OpTypeStruct %f16vec
9738 %str_ptr = OpTypePointer Workgroup %str
9739 %sh = OpVariable %str_ptr Workgroup
9740
9741 %main = OpFunction %void None %func
9742 %main_entry = OpLabel
9743
9744 OpReturn
9745 OpFunctionEnd)";
9746
9747 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
9748 ASSERT_EQ(SPV_ERROR_INVALID_ID,
9749 ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
9750 EXPECT_THAT(
9751 getDiagnosticString(),
9752 HasSubstr(
9753 "Cooperative vector types (or types containing them) can only be "
9754 "allocated in Function or Private storage classes or as function "
9755 "parameters"));
9756 }
9757
9758 std::string GenCoopVecShader(const std::string& extra_types,
9759 const std::string& main_body,
9760 const std::string& execution_modes = "") {
9761 const std::string prefix =
9762 R"(
9763 OpCapability Shader
9764 OpCapability Float16
9765 OpCapability Int64
9766 OpCapability Int16
9767 OpCapability StorageBuffer16BitAccess
9768 OpCapability VulkanMemoryModel
9769 OpCapability CooperativeVectorNV
9770 OpCapability CooperativeVectorTrainingNV
9771 OpCapability ReplicatedCompositesEXT
9772 OpCapability Shader64BitIndexingEXT
9773 OpExtension "SPV_EXT_replicated_composites"
9774 OpExtension "SPV_KHR_vulkan_memory_model"
9775 OpExtension "SPV_NV_cooperative_vector"
9776 OpExtension "SPV_EXT_shader_64bit_indexing"
9777 %1 = OpExtInstImport "GLSL.std.450"
9778 OpMemoryModel Logical Vulkan
9779 OpEntryPoint GLCompute %main "main" %48 %73
9780 OpExecutionMode %main LocalSize 1 1 1
9781
9782 )" + execution_modes +
9783 R"(
9784
9785 OpDecorate %f16_arr ArrayStride 2
9786 OpDecorate %46 Block
9787 OpMemberDecorate %46 0 Offset 0
9788 OpDecorate %48 Binding 0
9789 OpDecorate %48 DescriptorSet 0
9790
9791 %void = OpTypeVoid
9792 %func = OpTypeFunction %void
9793 %u32 = OpTypeInt 32 0
9794 %s32 = OpTypeInt 32 1
9795 %f16 = OpTypeFloat 16
9796 %bool = OpTypeBool
9797
9798 %false = OpConstantFalse %bool
9799 %u32_4 = OpConstant %u32 4
9800 %u32_8 = OpConstant %u32 8
9801 %s32_0 = OpConstant %s32 0
9802 %f16_0 = OpConstant %f16 0
9803
9804 %f16vec4 = OpTypeCooperativeVectorNV %f16 %u32_4
9805 %f16vec8 = OpTypeCooperativeVectorNV %f16 %u32_8
9806
9807 %f16_arr = OpTypeRuntimeArray %f16
9808 %46 = OpTypeStruct %f16_arr
9809 %47 = OpTypePointer StorageBuffer %46
9810 %48 = OpVariable %47 StorageBuffer
9811 %51 = OpTypePointer StorageBuffer %f16_arr
9812
9813 %57 = OpTypePointer Private %f16vec4
9814 %73 = OpVariable %57 Private
9815 %u32ptr = OpTypePointer Function %u32
9816
9817 %input4 = OpConstantCompositeReplicateEXT %f16vec4 %f16_0
9818 %input8 = OpConstantCompositeReplicateEXT %f16vec8 %f16_0
9819 %interp = OpConstant %u32 0
9820 %offset = OpConstant %u32 0
9821
9822 )";
9823
9824 const std::string func_begin =
9825 R"(
9826 %main = OpFunction %void None %func
9827 %main_entry = OpLabel
9828 %u32var = OpVariable %u32ptr Function
9829 %array_ptr = OpAccessChain %51 %48 %s32_0
9830 )";
9831
9832 const std::string suffix =
9833 R"(
9834 OpReturn
9835 OpFunctionEnd)";
9836
9837 return prefix + extra_types + func_begin + main_body + suffix;
9838 }
9839
9840 TEST_F(ValidateMemory, CoopVecMatMulSuccess) {
9841 std::string spirv = GenCoopVecShader("",
9842 R"(
9843 %result0 = OpCooperativeVectorMatrixMulAddNV %f16vec4 %input4 %interp %array_ptr %offset %interp %array_ptr %offset %interp %u32_4 %u32_4 %s32_0 %false
9844 %result1 = OpCooperativeVectorMatrixMulAddNV %f16vec4 %input8 %interp %array_ptr %offset %interp %array_ptr %offset %interp %u32_4 %u32_8 %s32_0 %false
9845 %result2 = OpCooperativeVectorMatrixMulAddNV %f16vec8 %input4 %interp %array_ptr %offset %interp %array_ptr %offset %interp %u32_8 %u32_4 %s32_0 %false
9846 %result3 = OpCooperativeVectorMatrixMulNV %f16vec4 %input4 %interp %array_ptr %offset %interp %u32_4 %u32_4 %s32_0 %false
9847 %result4 = OpCooperativeVectorMatrixMulNV %f16vec4 %input8 %interp %array_ptr %offset %interp %u32_4 %u32_8 %s32_0 %false
9848 %result5 = OpCooperativeVectorMatrixMulNV %f16vec8 %input4 %interp %array_ptr %offset %interp %u32_8 %u32_4 %s32_0 %false
9849
9850 OpCooperativeVectorReduceSumAccumulateNV %array_ptr %offset %input4
9851 OpCooperativeVectorOuterProductAccumulateNV %array_ptr %offset %input4 %input8 %interp %interp
9852 )");
9853
9854 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
9855 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
9856 }
9857
9858 TEST_F(ValidateMemory, CoopVecMatMulKMismatchFail) {
9859 std::string spirv = GenCoopVecShader(R"()",
9860 R"(
9861 %result1 = OpCooperativeVectorMatrixMulAddNV %f16vec4 %input8 %interp %array_ptr %offset %interp %array_ptr %offset %interp %u32_4 %u32_4 %s32_0 %false
9862 )");
9863
9864 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
9865 ASSERT_EQ(SPV_ERROR_INVALID_ID,
9866 ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
9867 EXPECT_THAT(getDiagnosticString(),
9868 HasSubstr("OpCooperativeVectorMatrixMulAddNV input number of "
9869 "components 8 does not match K 4"));
9870 }
9871
9872 TEST_F(ValidateMemory, CoopVecMatMulPackedKMismatchPass) {
9873 std::string spirv = GenCoopVecShader(
9874 R"(
9875 %packed = OpConstant %u32 1000491001
9876 )",
9877 R"(
9878 %result1 = OpCooperativeVectorMatrixMulAddNV %f16vec4 %input8 %packed %array_ptr %offset %interp %array_ptr %offset %interp %u32_4 %u32_4 %s32_0 %false
9879 )");
9880
9881 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
9882 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
9883 }
9884
9885 TEST_F(ValidateMemory, CoopVecMatMulMMismatchFail) {
9886 std::string spirv = GenCoopVecShader(R"()",
9887 R"(
9888 %result1 = OpCooperativeVectorMatrixMulAddNV %f16vec8 %input8 %interp %array_ptr %offset %interp %array_ptr %offset %interp %u32_4 %u32_8 %s32_0 %false
9889 )");
9890
9891 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
9892 ASSERT_EQ(SPV_ERROR_INVALID_ID,
9893 ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
9894 EXPECT_THAT(getDiagnosticString(),
9895 HasSubstr("OpCooperativeVectorMatrixMulAddNV result type number "
9896 "of components 8 does not match M 4"));
9897 }
9898
9899 TEST_F(ValidateMemory, CoopVecMatMulTransposeTypeFail) {
9900 std::string spirv = GenCoopVecShader(R"()",
9901 R"(
9902 %result0 = OpCooperativeVectorMatrixMulAddNV %f16vec4 %input4 %interp %array_ptr %offset %interp %array_ptr %offset %interp %u32_4 %u32_4 %s32_0 %s32_0
9903 )");
9904
9905 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
9906 ASSERT_EQ(SPV_ERROR_INVALID_ID,
9907 ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
9908 EXPECT_THAT(getDiagnosticString(),
9909 HasSubstr("OpCooperativeVectorMatrixMulAddNV Transpose <id> "
9910 "'16[%int_0]' is not a scalar boolean"));
9911 }
9912
9913 TEST_F(ValidateMemory, CoopVecMatMulInputInterpretationNotConstantFail) {
9914 std::string spirv = GenCoopVecShader(
9915 R"(
9916 )",
9917 R"(
9918 %u32val = OpLoad %u32 %u32var
9919 %result0 = OpCooperativeVectorMatrixMulAddNV %f16vec4 %input4 %u32val %array_ptr %offset %interp %array_ptr %offset %interp %u32_4 %u32_4 %s32_0 %false
9920 )");
9921
9922 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
9923 ASSERT_EQ(SPV_ERROR_INVALID_ID,
9924 ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
9925 EXPECT_THAT(getDiagnosticString(),
9926 HasSubstr("OpCooperativeVectorMatrixMulAddNV InputInterpretation "
9927 "<id> '31[%31]' is not a constant instruction"));
9928 }
9929
9930 TEST_F(ValidateMemory, CoopVecMatMulMatrixInterpretationNotConstantFail) {
9931 std::string spirv = GenCoopVecShader(
9932 R"(
9933 )",
9934 R"(
9935 %u32val = OpLoad %u32 %u32var
9936 %result0 = OpCooperativeVectorMatrixMulAddNV %f16vec4 %input4 %interp %array_ptr %offset %u32val %array_ptr %offset %interp %u32_4 %u32_4 %s32_0 %false
9937 )");
9938
9939 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
9940 ASSERT_EQ(SPV_ERROR_INVALID_ID,
9941 ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
9942 EXPECT_THAT(
9943 getDiagnosticString(),
9944 HasSubstr("OpCooperativeVectorMatrixMulAddNV MatrixInterpretation <id> "
9945 "'31[%31]' is not a constant instruction"));
9946 }
9947
9948 TEST_F(ValidateMemory, CoopVecMatMulBiasInterpretationNotConstantFail) {
9949 std::string spirv = GenCoopVecShader(
9950 R"(
9951 )",
9952 R"(
9953 %u32val = OpLoad %u32 %u32var
9954 %result0 = OpCooperativeVectorMatrixMulAddNV %f16vec4 %input4 %interp %array_ptr %offset %interp %array_ptr %offset %u32val %u32_4 %u32_4 %s32_0 %false
9955 )");
9956
9957 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
9958 ASSERT_EQ(SPV_ERROR_INVALID_ID,
9959 ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
9960 EXPECT_THAT(getDiagnosticString(),
9961 HasSubstr("OpCooperativeVectorMatrixMulAddNV BiasInterpretation "
9962 "<id> '31[%31]' is not a constant instruction"));
9963 }
9964
9965 TEST_F(ValidateMemory, CoopVecMatMulInputInterpretationNotInt32Fail) {
9966 std::string spirv = GenCoopVecShader(
9967 R"(
9968 )",
9969 R"(
9970 %result0 = OpCooperativeVectorMatrixMulAddNV %f16vec4 %input4 %false %array_ptr %offset %interp %array_ptr %offset %interp %u32_4 %u32_4 %s32_0 %false
9971 )");
9972
9973 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
9974 ASSERT_EQ(SPV_ERROR_INVALID_ID,
9975 ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
9976 EXPECT_THAT(getDiagnosticString(),
9977 HasSubstr("OpCooperativeVectorMatrixMulAddNV InputInterpretation "
9978 "type <id> '12[%bool]' is not a 32 bit integer"));
9979 }
9980
9981 TEST_F(ValidateMemory, CoopVecOuterProductABMismatchFail) {
9982 std::string spirv = GenCoopVecShader(
9983 R"(
9984 %f32 = OpTypeFloat 32
9985 %f32vec8 = OpTypeCooperativeVectorNV %f32 %u32_8
9986 %f32_0 = OpConstant %f32 0
9987 %input8f32 = OpConstantCompositeReplicateEXT %f32vec8 %f32_0
9988 )",
9989 R"(
9990 OpCooperativeVectorOuterProductAccumulateNV %array_ptr %offset %input4 %input8f32 %interp %interp
9991 )");
9992
9993 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
9994 ASSERT_EQ(SPV_ERROR_INVALID_ID,
9995 ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
9996 EXPECT_THAT(
9997 getDiagnosticString(),
9998 HasSubstr("OpCooperativeVectorOuterProductAccumulateNV A and B component "
9999 "types '11[%half]' and '28[%float]' do not match"));
10000 }
10001
10002 TEST_F(ValidateMemory, CoopVecOuterProductIntOffsetFail) {
10003 std::string spirv = GenCoopVecShader(
10004 R"(
10005 %u16 = OpTypeInt 16 0
10006 %u16_0 = OpConstant %u16 0
10007 )",
10008 R"(
10009 OpCooperativeVectorOuterProductAccumulateNV %array_ptr %u16_0 %input4 %input8 %interp %interp
10010 )");
10011
10012 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
10013 ASSERT_EQ(SPV_ERROR_INVALID_ID,
10014 ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
10015 EXPECT_THAT(
10016 getDiagnosticString(),
10017 HasSubstr("OpCooperativeVectorOuterProductAccumulateNV Offset "
10018 "type <id> '28[%ushort]' is not a 32 or 64 bit integer"));
10019 }
10020
10021 TEST_F(ValidateMemory, CoopVecOuterProductInt32MatrixStrideFail) {
10022 std::string spirv = GenCoopVecShader(
10023 R"(
10024 %u64 = OpTypeInt 64 0
10025 %u64_0 = OpConstant %u64 0
10026 )",
10027 R"(
10028 OpCooperativeVectorOuterProductAccumulateNV %array_ptr %offset %input4 %input8 %interp %interp %u64_0
10029 )");
10030
10031 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
10032 ASSERT_EQ(SPV_ERROR_INVALID_ID,
10033 ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
10034 EXPECT_THAT(
10035 getDiagnosticString(),
10036 HasSubstr("OpCooperativeVectorOuterProductAccumulateNV MatrixStride type "
10037 "<id> '28[%ulong]' is not a 32 bit integer"));
10038 }
10039
10040 TEST_F(ValidateMemory, CoopVecOuterProductVectorTypeFail) {
10041 std::string spirv = GenCoopVecShader(
10042 R"(
10043 %f16v4 = OpTypeVector %f16 4
10044 %f16c = OpConstantCompositeReplicateEXT %f16v4 %f16_0
10045 )",
10046 R"(
10047 OpCooperativeVectorOuterProductAccumulateNV %array_ptr %offset %f16c %input8 %interp %interp
10048 )");
10049
10050 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
10051 ASSERT_EQ(SPV_ERROR_INVALID_ID,
10052 ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
10053 EXPECT_THAT(getDiagnosticString(),
10054 HasSubstr("OpCooperativeVectorOuterProductAccumulateNV A type "
10055 "<id> '28[%v4half]' is not a cooperative vector type"));
10056 }
10057
10058 TEST_F(ValidateMemory, CoopVecReduceSumIntOffsetFail) {
10059 std::string spirv = GenCoopVecShader(
10060 R"(
10061 %u16 = OpTypeInt 16 0
10062 %u16_0 = OpConstant %u16 0
10063 )",
10064 R"(
10065 OpCooperativeVectorReduceSumAccumulateNV %array_ptr %u16_0 %input4
10066 )");
10067
10068 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
10069 ASSERT_EQ(SPV_ERROR_INVALID_ID,
10070 ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
10071 EXPECT_THAT(getDiagnosticString(),
10072 HasSubstr("OpCooperativeVectorReduceSumAccumulateNV Offset type "
10073 "<id> '28[%ushort]' is not a 32 or 64 bit integer"));
10074 }
10075
10076 TEST_F(ValidateMemory, CoopVecOuterProductInt64OffsetPass) {
10077 std::string spirv = GenCoopVecShader(
10078 R"(
10079 %u64 = OpTypeInt 64 0
10080 %u64_0 = OpConstant %u64 0
10081 )",
10082 R"(
10083 OpCooperativeVectorOuterProductAccumulateNV %array_ptr %u64_0 %input4 %input8 %interp %interp
10084 )");
10085
10086 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
10087 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
10088 }
10089
10090 TEST_F(ValidateMemory, CoopVecReduceSumVectorTypeFail) {
10091 std::string spirv = GenCoopVecShader(
10092 R"(
10093 %f16v4 = OpTypeVector %f16 4
10094 %f16c = OpConstantCompositeReplicateEXT %f16v4 %f16_0
10095 )",
10096 R"(
10097 OpCooperativeVectorReduceSumAccumulateNV %array_ptr %offset %f16c
10098 )");
10099
10100 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1_SPIRV_1_4);
10101 ASSERT_EQ(SPV_ERROR_INVALID_ID,
10102 ValidateInstructions(SPV_ENV_VULKAN_1_1_SPIRV_1_4));
10103 EXPECT_THAT(getDiagnosticString(),
10104 HasSubstr("OpCooperativeVectorReduceSumAccumulateNV V type <id> "
10105 "'28[%v4half]' is not a cooperative vector type."));
10106 }
10107
10108 TEST_F(ValidateMemory, CoopMatMatrixBFloatFAdd) {
10109 const std::string body =
10110 R"(
10111 OpCapability Shader
10112 OpCapability Float16
10113 OpCapability BFloat16TypeKHR
10114 OpCapability BFloat16CooperativeMatrixKHR
10115 OpCapability VulkanMemoryModel
10116 OpCapability CooperativeMatrixKHR
10117 OpExtension "SPV_KHR_bfloat16"
10118 OpExtension "SPV_KHR_vulkan_memory_model"
10119 OpExtension "SPV_KHR_cooperative_matrix"
10120 OpMemoryModel Logical Vulkan
10121 OpEntryPoint GLCompute %main "main" %_ %__0 %__1
10122 OpExecutionMode %main LocalSize 32 1 1
10123 OpDecorate %_arr_bfloat16_uint_64 ArrayStride 2
10124 OpDecorate %A Block
10125 OpMemberDecorate %A 0 Offset 0
10126 OpDecorate %_ Binding 0
10127 OpDecorate %_ DescriptorSet 0
10128 OpDecorate %_arr_bfloat16_uint_64_0 ArrayStride 2
10129 OpDecorate %B Block
10130 OpMemberDecorate %B 0 Offset 0
10131 OpDecorate %__0 Binding 1
10132 OpDecorate %__0 DescriptorSet 0
10133 OpDecorate %_arr_bfloat16_uint_64_1 ArrayStride 2
10134 OpDecorate %R Block
10135 OpMemberDecorate %R 0 Offset 0
10136 OpDecorate %__1 Binding 2
10137 OpDecorate %__1 DescriptorSet 0
10138 %void = OpTypeVoid
10139 %4 = OpTypeFunction %void
10140 %bfloat16 = OpTypeFloat 16 BFloat16KHR
10141 %uint = OpTypeInt 32 0
10142 %uint_3 = OpConstant %uint 3
10143 %uint_8 = OpConstant %uint 8
10144 %uint_0 = OpConstant %uint 0
10145 %12 = OpTypeCooperativeMatrixKHR %bfloat16 %uint_3 %uint_8 %uint_8 %uint_0
10146 %_ptr_Function_12 = OpTypePointer Function %12
10147 %uint_64 = OpConstant %uint 64
10148 %_arr_bfloat16_uint_64 = OpTypeArray %bfloat16 %uint_64
10149 %A = OpTypeStruct %_arr_bfloat16_uint_64
10150 %_ptr_StorageBuffer_A = OpTypePointer StorageBuffer %A
10151 %_ = OpVariable %_ptr_StorageBuffer_A StorageBuffer
10152 %int = OpTypeInt 32 1
10153 %int_0 = OpConstant %int 0
10154 %_ptr_StorageBuffer_bfloat16 = OpTypePointer StorageBuffer %bfloat16
10155 %_arr_bfloat16_uint_64_0 = OpTypeArray %bfloat16 %uint_64
10156 %B = OpTypeStruct %_arr_bfloat16_uint_64_0
10157 %_ptr_StorageBuffer_B = OpTypePointer StorageBuffer %B
10158 %__0 = OpVariable %_ptr_StorageBuffer_B StorageBuffer
10159 %v3uint = OpTypeVector %uint 3
10160 %uint_32 = OpConstant %uint 32
10161 %uint_1 = OpConstant %uint 1
10162 %35 = OpConstantComposite %v3uint %uint_32 %uint_1 %uint_1
10163 %_arr_bfloat16_uint_64_1 = OpTypeArray %bfloat16 %uint_64
10164 %R = OpTypeStruct %_arr_bfloat16_uint_64_1
10165 %_ptr_StorageBuffer_R = OpTypePointer StorageBuffer %R
10166 %__1 = OpVariable %_ptr_StorageBuffer_R StorageBuffer
10167 %main = OpFunction %void None %4
10168 %6 = OpLabel
10169 %matX = OpVariable %_ptr_Function_12 Function
10170 %matY = OpVariable %_ptr_Function_12 Function
10171 %23 = OpAccessChain %_ptr_StorageBuffer_bfloat16 %_ %int_0 %uint_0
10172 %24 = OpCooperativeMatrixLoadKHR %12 %23 %int_0 %uint_8 None
10173 OpStore %matX %24
10174 %30 = OpAccessChain %_ptr_StorageBuffer_bfloat16 %__0 %int_0 %uint_0
10175 %31 = OpCooperativeMatrixLoadKHR %12 %30 %int_0 %uint_8 None
10176 OpStore %matY %31
10177 %32 = OpLoad %12 %matX
10178 %33 = OpLoad %12 %matY
10179 %34 = OpFAdd %12 %32 %33
10180 OpReturn
10181 OpFunctionEnd
10182 )";
10183
10184 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_3);
10185 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_3));
10186 EXPECT_THAT(getDiagnosticString(),
10187 HasSubstr("FAdd doesn't support BFloat16 type"));
10188 }
10189
10190 TEST_F(ValidateMemory, CoopMatMatrixFloat8FAdd) {
10191 const std::string body =
10192 R"(
10193 OpCapability Shader
10194 OpCapability Float8EXT
10195 OpCapability Float8CooperativeMatrixEXT
10196 OpCapability VulkanMemoryModel
10197 OpCapability CooperativeMatrixKHR
10198 OpExtension "SPV_EXT_float8"
10199 OpExtension "SPV_KHR_cooperative_matrix"
10200 OpExtension "SPV_KHR_vulkan_memory_model"
10201 OpMemoryModel Logical Vulkan
10202 OpEntryPoint GLCompute %main "main"
10203 OpExecutionMode %main LocalSize 32 1 1
10204 OpDecorate %gl_WorkGroupSize BuiltIn WorkgroupSize
10205 %void = OpTypeVoid
10206 %4 = OpTypeFunction %void
10207 %fp8e4m3 = OpTypeFloat 8 Float8E4M3EXT
10208 %uint = OpTypeInt 32 0
10209 %uint_3 = OpConstant %uint 3
10210 %uint_16 = OpConstant %uint 16
10211 %uint_0 = OpConstant %uint 0
10212 %12 = OpTypeCooperativeMatrixKHR %fp8e4m3 %uint_3 %uint_16 %uint_16 %uint_0
10213 %_ptr_Function_12 = OpTypePointer Function %12
10214 %v3uint = OpTypeVector %uint 3
10215 %uint_32 = OpConstant %uint 32
10216 %uint_1 = OpConstant %uint 1
10217 %gl_WorkGroupSize = OpConstantComposite %v3uint %uint_32 %uint_1 %uint_1
10218 %main = OpFunction %void None %4
10219 %6 = OpLabel
10220 %matR = OpVariable %_ptr_Function_12 Function
10221 %matX = OpVariable %_ptr_Function_12 Function
10222 %matY = OpVariable %_ptr_Function_12 Function
10223 %16 = OpLoad %12 %matX
10224 %18 = OpLoad %12 %matY
10225 %19 = OpFAdd %12 %16 %18
10226 OpStore %matR %19
10227 OpReturn
10228 OpFunctionEnd
10229 )";
10230
10231 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_3);
10232 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_3));
10233 EXPECT_THAT(getDiagnosticString(),
10234 HasSubstr("FAdd doesn't support FP8 E4M3/E5M2 types"));
10235 }
10236
10237 TEST_F(ValidateMemory, PhysicalStorageBufferArray) {
10238 const std::string body =
10239 R"(
10240 OpCapability Shader
10241 OpCapability Int64
10242 OpCapability PhysicalStorageBufferAddresses
10243 OpExtension "SPV_KHR_storage_buffer_storage_class"
10244 OpExtension "SPV_KHR_physical_storage_buffer"
10245 OpMemoryModel PhysicalStorageBuffer64 GLSL450
10246 OpEntryPoint GLCompute %18 "main"
10247 OpExecutionMode %18 LocalSize 1 1 1
10248 OpSource OpenCL_C 120
10249 OpMemberDecorate %S 0 Offset 0
10250 OpMemberDecorate %S 1 Offset 256
10251 OpDecorate %array ArrayStride 16
10252 %uint = OpTypeInt 32 0
10253 %void = OpTypeVoid
10254 %ulong = OpTypeInt 64 0
10255 %float = OpTypeFloat 32
10256 %v4float = OpTypeVector %float 4
10257 %17 = OpTypeFunction %void
10258 %ulong_0 = OpConstant %ulong 0
10259 %uint_3 = OpConstant %uint 3
10260 %array = OpTypeArray %v4float %uint_3
10261 %S = OpTypeStruct %array %uint
10262 %ptr_S = OpTypePointer PhysicalStorageBuffer %S
10263 %float_0 = OpConstant %float 0
10264 %v4float_0 = OpConstantComposite %v4float %float_0 %float_0 %float_0 %float_0
10265 %23 = OpConstantComposite %array %v4float_0 %v4float_0 %v4float_0
10266 %24 = OpConstantComposite %S %23 %uint_3
10267 %18 = OpFunction %void None %17
10268 %19 = OpLabel
10269 %58 = OpConvertUToPtr %ptr_S %ulong_0
10270 OpStore %58 %24 Aligned 4
10271 OpReturn
10272 OpFunctionEnd
10273 )";
10274
10275 CompileSuccessfully(body.c_str(), SPV_ENV_VULKAN_1_0);
10276 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
10277 }
10278
10279 TEST_F(ValidateMemory, LongVectorPrivateStorageClassGood) {
10280 std::string spirv = R"(
10281 OpCapability Shader
10282 OpCapability LongVectorEXT
10283 OpExtension "SPV_EXT_long_vector"
10284 OpMemoryModel Logical GLSL450
10285 OpEntryPoint Vertex %func "func"
10286 %float = OpTypeFloat 32
10287 %vec5 = OpTypeVector %float 5
10288 %vec5ptr = OpTypePointer Private %vec5
10289 %1 = OpVariable %vec5ptr Private
10290 %void = OpTypeVoid
10291 %functy = OpTypeFunction %void
10292 %func = OpFunction %void None %functy
10293 %2 = OpLabel
10294 OpReturn
10295 OpFunctionEnd
10296 )";
10297 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
10298 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
10299 }
10300
10301 TEST_F(ValidateMemory, LongVectorInputStorageClassBad) {
10302 std::string spirv = R"(
10303 OpCapability Shader
10304 OpCapability LongVectorEXT
10305 OpExtension "SPV_EXT_long_vector"
10306 OpMemoryModel Logical GLSL450
10307 OpEntryPoint Vertex %func "func"
10308 %float = OpTypeFloat 32
10309 %vec5 = OpTypeVector %float 5
10310 %vec5ptr = OpTypePointer Input %vec5
10311 %1 = OpVariable %vec5ptr Input
10312 %void = OpTypeVoid
10313 %functy = OpTypeFunction %void
10314 %func = OpFunction %void None %functy
10315 %2 = OpLabel
10316 OpReturn
10317 OpFunctionEnd
10318 )";
10319 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
10320 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
10321 EXPECT_THAT(
10322 getDiagnosticString(),
10323 HasSubstr("Long vector types with more than 4 components (or types "
10324 "containing them) not supported in storage class Input"));
10325 }
10326
10327 TEST_F(ValidateMemory, LongVectorMissingCapabilityBad) {
10328 std::string spirv = R"(
10329 OpCapability Shader
10330 OpMemoryModel Logical GLSL450
10331 OpEntryPoint Vertex %func "func"
10332 %float = OpTypeFloat 32
10333 %vec5 = OpTypeVector %float 5
10334 %vec5ptr = OpTypePointer Private %vec5
10335 %1 = OpVariable %vec5ptr Private
10336 %void = OpTypeVoid
10337 %functy = OpTypeFunction %void
10338 %func = OpFunction %void None %functy
10339 %2 = OpLabel
10340 OpReturn
10341 OpFunctionEnd
10342 )";
10343 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_1);
10344 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_1));
10345 EXPECT_THAT(getDiagnosticString(),
10346 HasSubstr("Illegal number of components (5) for TypeVector"));
10347 }
10348
10349 } // namespace
10350 } // namespace val
10351 } // namespace spvtools