Debian Codegraph @

third_party/spirv-tools/test/val/val_atomics_test.cpp

raw
1 // Copyright (c) 2017 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include <sstream>
16 #include <string>
17
18 #include "gmock/gmock.h"
19 #include "test/unit_spirv.h"
20 #include "test/val/val_fixtures.h"
21
22 namespace spvtools {
23 namespace val {
24 namespace {
25
26 using ::testing::HasSubstr;
27 using ::testing::Not;
28
29 using ValidateAtomics = spvtest::ValidateBase<bool>;
30
31 std::string GenerateShaderCodeImpl(
32 const std::string& body, const std::string& capabilities_and_extensions,
33 const std::string& definitions, const std::string& memory_model) {
34 std::ostringstream ss;
35 ss << R"(
36 OpCapability Shader
37 )";
38 ss << capabilities_and_extensions;
39 ss << "OpMemoryModel Logical " << memory_model << "\n";
40 ss << R"(
41 OpEntryPoint Fragment %main "main"
42 OpExecutionMode %main OriginUpperLeft
43 %void = OpTypeVoid
44 %func = OpTypeFunction %void
45 %bool = OpTypeBool
46 %f32 = OpTypeFloat 32
47 %u32 = OpTypeInt 32 0
48 %f32vec4 = OpTypeVector %f32 4
49
50 %f32_0 = OpConstant %f32 0
51 %f32_1 = OpConstant %f32 1
52 %u32_0 = OpConstant %u32 0
53 %u32_1 = OpConstant %u32 1
54 %f32vec4_0000 = OpConstantComposite %f32vec4 %f32_0 %f32_0 %f32_0 %f32_0
55
56 %cross_device = OpConstant %u32 0
57 %device = OpConstant %u32 1
58 %workgroup = OpConstant %u32 2
59 %subgroup = OpConstant %u32 3
60 %invocation = OpConstant %u32 4
61 %queuefamily = OpConstant %u32 5
62
63 %relaxed = OpConstant %u32 0
64 %acquire = OpConstant %u32 2
65 %release = OpConstant %u32 4
66 %acquire_release = OpConstant %u32 8
67 %acquire_and_release = OpConstant %u32 6
68 %sequentially_consistent = OpConstant %u32 16
69 %acquire_release_uniform_workgroup = OpConstant %u32 328
70
71 %f32_ptr = OpTypePointer Workgroup %f32
72 %f32_var = OpVariable %f32_ptr Workgroup
73
74 %u32_ptr = OpTypePointer Workgroup %u32
75 %u32_var = OpVariable %u32_ptr Workgroup
76
77 %f32vec4_ptr = OpTypePointer Workgroup %f32vec4
78 %f32vec4_var = OpVariable %f32vec4_ptr Workgroup
79
80 %f32_ptr_function = OpTypePointer Function %f32
81 )";
82 ss << definitions;
83 ss << R"(
84 %main = OpFunction %void None %func
85 %main_entry = OpLabel
86 )";
87 ss << body;
88 ss << R"(
89 OpReturn
90 OpFunctionEnd)";
91
92 return ss.str();
93 }
94
95 std::string GenerateShaderCode(
96 const std::string& body,
97 const std::string& capabilities_and_extensions = "",
98 const std::string& memory_model = "GLSL450") {
99 const std::string defintions = R"(
100 %u64 = OpTypeInt 64 0
101 %s64 = OpTypeInt 64 1
102
103 %u64_1 = OpConstant %u64 1
104 %s64_1 = OpConstant %s64 1
105
106 %u64_ptr = OpTypePointer Workgroup %u64
107 %s64_ptr = OpTypePointer Workgroup %s64
108 %u64_var = OpVariable %u64_ptr Workgroup
109 %s64_var = OpVariable %s64_ptr Workgroup
110 )";
111 return GenerateShaderCodeImpl(
112 body, "OpCapability Int64\n" + capabilities_and_extensions, defintions,
113 memory_model);
114 }
115
116 std::string GenerateWebGPUShaderCode(
117 const std::string& body,
118 const std::string& capabilities_and_extensions = "") {
119 const std::string vulkan_memory_capability = R"(
120 OpCapability VulkanMemoryModelDeviceScopeKHR
121 OpCapability VulkanMemoryModelKHR
122 )";
123 const std::string vulkan_memory_extension = R"(
124 OpExtension "SPV_KHR_vulkan_memory_model"
125 )";
126 return GenerateShaderCodeImpl(body,
127 vulkan_memory_capability +
128 capabilities_and_extensions +
129 vulkan_memory_extension,
130 "", "VulkanKHR");
131 }
132
133 std::string GenerateKernelCode(
134 const std::string& body,
135 const std::string& capabilities_and_extensions = "") {
136 std::ostringstream ss;
137 ss << R"(
138 OpCapability Addresses
139 OpCapability Kernel
140 OpCapability Linkage
141 OpCapability Int64
142 )";
143
144 ss << capabilities_and_extensions;
145 ss << R"(
146 OpMemoryModel Physical32 OpenCL
147 %void = OpTypeVoid
148 %func = OpTypeFunction %void
149 %bool = OpTypeBool
150 %f32 = OpTypeFloat 32
151 %u32 = OpTypeInt 32 0
152 %u64 = OpTypeInt 64 0
153 %f32vec4 = OpTypeVector %f32 4
154
155 %f32_0 = OpConstant %f32 0
156 %f32_1 = OpConstant %f32 1
157 %u32_0 = OpConstant %u32 0
158 %u32_1 = OpConstant %u32 1
159 %u64_1 = OpConstant %u64 1
160 %f32vec4_0000 = OpConstantComposite %f32vec4 %f32_0 %f32_0 %f32_0 %f32_0
161
162 %cross_device = OpConstant %u32 0
163 %device = OpConstant %u32 1
164 %workgroup = OpConstant %u32 2
165 %subgroup = OpConstant %u32 3
166 %invocation = OpConstant %u32 4
167
168 %relaxed = OpConstant %u32 0
169 %acquire = OpConstant %u32 2
170 %release = OpConstant %u32 4
171 %acquire_release = OpConstant %u32 8
172 %acquire_and_release = OpConstant %u32 6
173 %sequentially_consistent = OpConstant %u32 16
174 %acquire_release_uniform_workgroup = OpConstant %u32 328
175 %acquire_release_atomic_counter_workgroup = OpConstant %u32 1288
176
177 %f32_ptr = OpTypePointer Workgroup %f32
178 %f32_var = OpVariable %f32_ptr Workgroup
179
180 %u32_ptr = OpTypePointer Workgroup %u32
181 %u32_var = OpVariable %u32_ptr Workgroup
182
183 %u64_ptr = OpTypePointer Workgroup %u64
184 %u64_var = OpVariable %u64_ptr Workgroup
185
186 %f32vec4_ptr = OpTypePointer Workgroup %f32vec4
187 %f32vec4_var = OpVariable %f32vec4_ptr Workgroup
188
189 %f32_ptr_function = OpTypePointer Function %f32
190 %f32_ptr_uniformconstant = OpTypePointer UniformConstant %f32
191 %f32_uc_var = OpVariable %f32_ptr_uniformconstant UniformConstant
192
193 %f32_ptr_image = OpTypePointer Image %f32
194 %f32_im_var = OpVariable %f32_ptr_image Image
195
196 %main = OpFunction %void None %func
197 %main_entry = OpLabel
198 )";
199
200 ss << body;
201
202 ss << R"(
203 OpReturn
204 OpFunctionEnd)";
205
206 return ss.str();
207 }
208
209 TEST_F(ValidateAtomics, AtomicLoadShaderSuccess) {
210 const std::string body = R"(
211 %val1 = OpAtomicLoad %u32 %u32_var %device %relaxed
212 %val2 = OpAtomicLoad %u32 %u32_var %workgroup %acquire
213 %val3 = OpAtomicLoad %u64 %u64_var %subgroup %sequentially_consistent
214 )";
215
216 CompileSuccessfully(GenerateShaderCode(body));
217 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
218 }
219
220 TEST_F(ValidateAtomics, AtomicLoadKernelSuccess) {
221 const std::string body = R"(
222 %val1 = OpAtomicLoad %f32 %f32_var %device %relaxed
223 %val2 = OpAtomicLoad %u32 %u32_var %workgroup %sequentially_consistent
224 %val3 = OpAtomicLoad %u64 %u64_var %subgroup %acquire
225 )";
226
227 CompileSuccessfully(GenerateKernelCode(body));
228 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
229 }
230
231 TEST_F(ValidateAtomics, AtomicLoadInt32VulkanSuccess) {
232 const std::string body = R"(
233 %val1 = OpAtomicLoad %u32 %u32_var %device %relaxed
234 %val2 = OpAtomicLoad %u32 %u32_var %workgroup %acquire
235 )";
236
237 CompileSuccessfully(GenerateShaderCode(body), SPV_ENV_VULKAN_1_0);
238 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
239 }
240
241 TEST_F(ValidateAtomics, AtomicAddIntVulkanWrongType1) {
242 const std::string body = R"(
243 %val1 = OpAtomicIAdd %f32 %f32_var %device %relaxed %f32_1
244 )";
245
246 CompileSuccessfully(GenerateShaderCode(body));
247 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
248 EXPECT_THAT(getDiagnosticString(),
249 HasSubstr("AtomicIAdd: "
250 "expected Result Type to be int scalar type"));
251 }
252
253 TEST_F(ValidateAtomics, AtomicAddIntVulkanWrongType2) {
254 const std::string body = R"(
255 %val1 = OpAtomicIAdd %f32vec4 %f32vec4_var %device %relaxed %f32_1
256 )";
257
258 CompileSuccessfully(GenerateShaderCode(body));
259 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
260 EXPECT_THAT(getDiagnosticString(),
261 HasSubstr("AtomicIAdd: "
262 "expected Result Type to be integer scalar type"));
263 }
264
265 TEST_F(ValidateAtomics, AtomicAddFloatVulkan) {
266 const std::string body = R"(
267 %val1 = OpAtomicFAddEXT %f32 %f32_var %device %relaxed %f32_1
268 )";
269
270 CompileSuccessfully(GenerateShaderCode(body));
271 ASSERT_EQ(SPV_ERROR_INVALID_CAPABILITY, ValidateInstructions());
272 EXPECT_THAT(
273 getDiagnosticString(),
274 HasSubstr("Opcode AtomicFAddEXT requires one of these capabilities: "
275 "AtomicFloat32AddEXT AtomicFloat64AddEXT"));
276 }
277
278 TEST_F(ValidateAtomics, AtomicAddFloatVulkanWrongType1) {
279 const std::string body = R"(
280 %val1 = OpAtomicFAddEXT %f32vec4 %f32vec4_var %device %relaxed %f32_1
281 )";
282 const std::string extra = R"(
283 OpCapability AtomicFloat32AddEXT
284 OpExtension "SPV_EXT_shader_atomic_float_add"
285 )";
286
287 CompileSuccessfully(GenerateShaderCode(body, extra), SPV_ENV_VULKAN_1_0);
288 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
289 EXPECT_THAT(getDiagnosticString(),
290 HasSubstr("AtomicFAddEXT: "
291 "expected Result Type to be float scalar type"));
292 }
293
294 TEST_F(ValidateAtomics, AtomicAddFloatVulkanWrongType2) {
295 const std::string body = R"(
296 %val1 = OpAtomicFAddEXT %u32 %u32_var %device %relaxed %u32_1
297 )";
298 const std::string extra = R"(
299 OpCapability AtomicFloat32AddEXT
300 OpExtension "SPV_EXT_shader_atomic_float_add"
301 )";
302
303 CompileSuccessfully(GenerateShaderCode(body, extra), SPV_ENV_VULKAN_1_0);
304 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
305 EXPECT_THAT(getDiagnosticString(),
306 HasSubstr("AtomicFAddEXT: "
307 "expected Result Type to be float scalar type"));
308 }
309
310 TEST_F(ValidateAtomics, AtomicAddFloatVulkanWrongType3) {
311 const std::string body = R"(
312 %val1 = OpAtomicFAddEXT %u64 %u64_var %device %relaxed %u64_1
313 )";
314 const std::string extra = R"(
315 OpCapability AtomicFloat32AddEXT
316 OpExtension "SPV_EXT_shader_atomic_float_add"
317 )";
318
319 CompileSuccessfully(GenerateShaderCode(body, extra), SPV_ENV_VULKAN_1_0);
320 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
321 EXPECT_THAT(getDiagnosticString(),
322 HasSubstr("AtomicFAddEXT: "
323 "expected Result Type to be float scalar type"));
324 }
325
326 TEST_F(ValidateAtomics, AtomicAddFloatVulkanWrongCapability) {
327 const std::string body = R"(
328 %val1 = OpAtomicFAddEXT %f32 %f32_var %device %relaxed %f32_1
329 )";
330 const std::string extra = R"(
331 OpCapability AtomicFloat64AddEXT
332 OpExtension "SPV_EXT_shader_atomic_float_add"
333 )";
334
335 CompileSuccessfully(GenerateShaderCode(body, extra), SPV_ENV_VULKAN_1_0);
336 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
337 EXPECT_THAT(getDiagnosticString(),
338 HasSubstr("AtomicFAddEXT: float add atomics "
339 "require the AtomicFloat32AddEXT capability"));
340 }
341
342 TEST_F(ValidateAtomics, AtomicAddFloatVulkanSuccess) {
343 const std::string body = R"(
344 %val1 = OpAtomicFAddEXT %f32 %f32_var %device %relaxed %f32_1
345 )";
346 const std::string extra = R"(
347 OpCapability AtomicFloat32AddEXT
348 OpExtension "SPV_EXT_shader_atomic_float_add"
349 )";
350
351 CompileSuccessfully(GenerateShaderCode(body, extra), SPV_ENV_VULKAN_1_0);
352 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
353 }
354
355 TEST_F(ValidateAtomics, AtomicLoadFloatVulkan) {
356 const std::string body = R"(
357 %val1 = OpAtomicLoad %f32 %f32_var %device %relaxed
358 %val2 = OpAtomicLoad %f32 %f32_var %workgroup %acquire
359 )";
360
361 CompileSuccessfully(GenerateShaderCode(body), SPV_ENV_VULKAN_1_0);
362 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
363 }
364
365 TEST_F(ValidateAtomics, AtomicStoreFloatVulkan) {
366 const std::string body = R"(
367 OpAtomicStore %f32_var %device %relaxed %f32_1
368 )";
369
370 CompileSuccessfully(GenerateShaderCode(body), SPV_ENV_VULKAN_1_0);
371 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
372 }
373
374 TEST_F(ValidateAtomics, AtomicExchangeFloatVulkan) {
375 const std::string body = R"(
376 %val2 = OpAtomicExchange %f32 %f32_var %device %relaxed %f32_0
377 )";
378
379 CompileSuccessfully(GenerateShaderCode(body), SPV_ENV_VULKAN_1_0);
380 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
381 }
382
383 TEST_F(ValidateAtomics, AtomicLoadInt64WithCapabilityVulkanSuccess) {
384 const std::string body = R"(
385 %val1 = OpAtomicLoad %u64 %u64_var %device %relaxed
386 %val2 = OpAtomicLoad %u64 %u64_var %workgroup %acquire
387 )";
388
389 CompileSuccessfully(GenerateShaderCode(body, "OpCapability Int64Atomics\n"),
390 SPV_ENV_VULKAN_1_0);
391 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
392 }
393
394 TEST_F(ValidateAtomics, AtomicLoadInt64WithoutCapabilityVulkan) {
395 const std::string body = R"(
396 %val1 = OpAtomicLoad %u64 %u64_var %device %relaxed
397 %val2 = OpAtomicLoad %u64 %u64_var %workgroup %acquire
398 )";
399
400 CompileSuccessfully(GenerateShaderCode(body), SPV_ENV_VULKAN_1_0);
401 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
402 EXPECT_THAT(getDiagnosticString(),
403 HasSubstr("64-bit atomics require the Int64Atomics capability"));
404 }
405
406 TEST_F(ValidateAtomics, AtomicStoreOpenCLFunctionPointerStorageTypeSuccess) {
407 const std::string body = R"(
408 %f32_var_function = OpVariable %f32_ptr_function Function
409 OpAtomicStore %f32_var_function %device %relaxed %f32_1
410 )";
411
412 CompileSuccessfully(GenerateKernelCode(body), SPV_ENV_OPENCL_1_2);
413 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_OPENCL_1_2));
414 }
415
416 TEST_F(ValidateAtomics, AtomicStoreVulkanFunctionPointerStorageType) {
417 const std::string body = R"(
418 %f32_var_function = OpVariable %f32_ptr_function Function
419 OpAtomicStore %f32_var_function %device %relaxed %f32_1
420 )";
421
422 CompileSuccessfully(GenerateShaderCode(body), SPV_ENV_VULKAN_1_0);
423 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
424 EXPECT_THAT(getDiagnosticString(),
425 HasSubstr("AtomicStore: Function storage class forbidden when "
426 "the Shader capability is declared."));
427 }
428
429 // TODO([email protected]): the corresponding check fails Vulkan CTS,
430 // reenable once fixed.
431 TEST_F(ValidateAtomics, DISABLED_AtomicLoadVulkanSubgroup) {
432 const std::string body = R"(
433 %val1 = OpAtomicLoad %u32 %u32_var %subgroup %acquire
434 )";
435
436 CompileSuccessfully(GenerateShaderCode(body), SPV_ENV_VULKAN_1_0);
437 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
438 EXPECT_THAT(getDiagnosticString(),
439 HasSubstr("AtomicLoad: in Vulkan environment memory scope is "
440 "limited to Device, Workgroup and Invocation"));
441 }
442
443 TEST_F(ValidateAtomics, AtomicLoadVulkanRelease) {
444 const std::string body = R"(
445 %val1 = OpAtomicLoad %u32 %u32_var %workgroup %release
446 )";
447
448 CompileSuccessfully(GenerateShaderCode(body), SPV_ENV_VULKAN_1_0);
449 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
450 EXPECT_THAT(
451 getDiagnosticString(),
452 HasSubstr("Vulkan spec disallows OpAtomicLoad with Memory Semantics "
453 "Release, AcquireRelease and SequentiallyConsistent"));
454 }
455
456 TEST_F(ValidateAtomics, AtomicLoadVulkanAcquireRelease) {
457 const std::string body = R"(
458 %val1 = OpAtomicLoad %u32 %u32_var %workgroup %acquire_release
459 )";
460
461 CompileSuccessfully(GenerateShaderCode(body), SPV_ENV_VULKAN_1_0);
462 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
463 EXPECT_THAT(
464 getDiagnosticString(),
465 HasSubstr("Vulkan spec disallows OpAtomicLoad with Memory Semantics "
466 "Release, AcquireRelease and SequentiallyConsistent"));
467 }
468
469 TEST_F(ValidateAtomics, AtomicLoadVulkanSequentiallyConsistent) {
470 const std::string body = R"(
471 %val1 = OpAtomicLoad %u32 %u32_var %workgroup %sequentially_consistent
472 )";
473
474 CompileSuccessfully(GenerateShaderCode(body), SPV_ENV_VULKAN_1_0);
475 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
476 EXPECT_THAT(
477 getDiagnosticString(),
478 HasSubstr("Vulkan spec disallows OpAtomicLoad with Memory Semantics "
479 "Release, AcquireRelease and SequentiallyConsistent"));
480 }
481
482 TEST_F(ValidateAtomics, AtomicLoadShaderFloat) {
483 const std::string body = R"(
484 %val1 = OpAtomicLoad %f32 %f32_var %device %relaxed
485 )";
486
487 CompileSuccessfully(GenerateShaderCode(body));
488 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
489 }
490
491 TEST_F(ValidateAtomics, AtomicLoadVulkanInt64) {
492 const std::string body = R"(
493 %val1 = OpAtomicLoad %u64 %u64_var %device %relaxed
494 )";
495
496 CompileSuccessfully(GenerateShaderCode(body), SPV_ENV_VULKAN_1_0);
497 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
498 EXPECT_THAT(
499 getDiagnosticString(),
500 HasSubstr(
501 "AtomicLoad: 64-bit atomics require the Int64Atomics capability"));
502 }
503
504 TEST_F(ValidateAtomics, AtomicLoadWebGPUSuccess) {
505 const std::string body = R"(
506 %val1 = OpAtomicLoad %u32 %u32_var %queuefamily %relaxed
507 )";
508
509 CompileSuccessfully(GenerateWebGPUShaderCode(body), SPV_ENV_WEBGPU_0);
510 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
511 }
512
513 TEST_F(ValidateAtomics, AtomicLoadWebGPUNonQueueFamilyFailure) {
514 const std::string body = R"(
515 %val3 = OpAtomicLoad %u32 %u32_var %invocation %relaxed
516 )";
517
518 CompileSuccessfully(GenerateWebGPUShaderCode(body), SPV_ENV_WEBGPU_0);
519 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_WEBGPU_0));
520 EXPECT_THAT(getDiagnosticString(),
521 HasSubstr("Memory Scope is limited to QueueFamilyKHR for "
522 "OpAtomic* operations"));
523 }
524
525 TEST_F(ValidateAtomics, AtomicLoadWebGPUNonRelaxedFailure) {
526 const std::string body = R"(
527 %val1 = OpAtomicLoad %u32 %u32_var %queuefamily %acquire
528 )";
529
530 CompileSuccessfully(GenerateWebGPUShaderCode(body), SPV_ENV_WEBGPU_0);
531 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_WEBGPU_0));
532 EXPECT_THAT(getDiagnosticString(),
533 HasSubstr("no bits may be set for Memory Semantics of OpAtomic* "
534 "instructions"));
535 }
536
537 TEST_F(ValidateAtomics, VK_KHR_shader_atomic_int64Success) {
538 const std::string body = R"(
539 %val1 = OpAtomicUMin %u64 %u64_var %device %relaxed %u64_1
540 %val2 = OpAtomicUMax %u64 %u64_var %device %relaxed %u64_1
541 %val3 = OpAtomicSMin %u64 %u64_var %device %relaxed %u64_1
542 %val4 = OpAtomicSMax %u64 %u64_var %device %relaxed %u64_1
543 %val5 = OpAtomicAnd %u64 %u64_var %device %relaxed %u64_1
544 %val6 = OpAtomicOr %u64 %u64_var %device %relaxed %u64_1
545 %val7 = OpAtomicXor %u64 %u64_var %device %relaxed %u64_1
546 %val8 = OpAtomicIAdd %u64 %u64_var %device %relaxed %u64_1
547 %val9 = OpAtomicExchange %u64 %u64_var %device %relaxed %u64_1
548 %val10 = OpAtomicCompareExchange %u64 %u64_var %device %relaxed %relaxed %u64_1 %u64_1
549
550 %val11 = OpAtomicUMin %s64 %s64_var %device %relaxed %s64_1
551 %val12 = OpAtomicUMax %s64 %s64_var %device %relaxed %s64_1
552 %val13 = OpAtomicSMin %s64 %s64_var %device %relaxed %s64_1
553 %val14 = OpAtomicSMax %s64 %s64_var %device %relaxed %s64_1
554 %val15 = OpAtomicAnd %s64 %s64_var %device %relaxed %s64_1
555 %val16 = OpAtomicOr %s64 %s64_var %device %relaxed %s64_1
556 %val17 = OpAtomicXor %s64 %s64_var %device %relaxed %s64_1
557 %val18 = OpAtomicIAdd %s64 %s64_var %device %relaxed %s64_1
558 %val19 = OpAtomicExchange %s64 %s64_var %device %relaxed %s64_1
559 %val20 = OpAtomicCompareExchange %s64 %s64_var %device %relaxed %relaxed %s64_1 %s64_1
560
561 %val21 = OpAtomicLoad %u64 %u64_var %device %relaxed
562 %val22 = OpAtomicLoad %s64 %s64_var %device %relaxed
563
564 OpAtomicStore %u64_var %device %relaxed %u64_1
565 OpAtomicStore %s64_var %device %relaxed %s64_1
566 )";
567
568 CompileSuccessfully(GenerateShaderCode(body, "OpCapability Int64Atomics\n"),
569 SPV_ENV_VULKAN_1_0);
570 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
571 }
572
573 TEST_F(ValidateAtomics, VK_KHR_shader_atomic_int64MissingCapability) {
574 const std::string body = R"(
575 %val1 = OpAtomicUMin %u64 %u64_var %device %relaxed %u64_1
576 )";
577
578 CompileSuccessfully(GenerateShaderCode(body), SPV_ENV_VULKAN_1_0);
579 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
580 EXPECT_THAT(
581 getDiagnosticString(),
582 HasSubstr(
583 "AtomicUMin: 64-bit atomics require the Int64Atomics capability"));
584 }
585
586 TEST_F(ValidateAtomics, AtomicLoadWrongResultType) {
587 const std::string body = R"(
588 %val1 = OpAtomicLoad %f32vec4 %f32vec4_var %device %relaxed
589 )";
590
591 CompileSuccessfully(GenerateKernelCode(body));
592 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
593 EXPECT_THAT(getDiagnosticString(),
594 HasSubstr("AtomicLoad: "
595 "expected Result Type to be int or float scalar type"));
596 }
597
598 TEST_F(ValidateAtomics, AtomicLoadWrongPointerType) {
599 const std::string body = R"(
600 %val1 = OpAtomicLoad %f32 %f32_ptr %device %relaxed
601 )";
602
603 CompileSuccessfully(GenerateKernelCode(body));
604 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
605 EXPECT_THAT(getDiagnosticString(),
606 HasSubstr("Operand 27[%_ptr_Workgroup_float] cannot be a type"));
607 }
608
609 TEST_F(ValidateAtomics, AtomicLoadWrongPointerDataType) {
610 const std::string body = R"(
611 %val1 = OpAtomicLoad %u32 %f32_var %device %relaxed
612 )";
613
614 CompileSuccessfully(GenerateKernelCode(body));
615 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
616 EXPECT_THAT(
617 getDiagnosticString(),
618 HasSubstr("AtomicLoad: "
619 "expected Pointer to point to a value of type Result Type"));
620 }
621
622 TEST_F(ValidateAtomics, AtomicLoadWrongScopeType) {
623 const std::string body = R"(
624 %val1 = OpAtomicLoad %f32 %f32_var %f32_1 %relaxed
625 )";
626
627 CompileSuccessfully(GenerateKernelCode(body));
628 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
629 EXPECT_THAT(getDiagnosticString(),
630 HasSubstr("AtomicLoad: expected scope to be a 32-bit int"));
631 }
632
633 TEST_F(ValidateAtomics, AtomicLoadWrongMemorySemanticsType) {
634 const std::string body = R"(
635 %val1 = OpAtomicLoad %f32 %f32_var %device %u64_1
636 )";
637
638 CompileSuccessfully(GenerateKernelCode(body));
639 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
640 EXPECT_THAT(
641 getDiagnosticString(),
642 HasSubstr("AtomicLoad: expected Memory Semantics to be a 32-bit int"));
643 }
644
645 TEST_F(ValidateAtomics, AtomicStoreKernelSuccess) {
646 const std::string body = R"(
647 OpAtomicStore %f32_var %device %relaxed %f32_1
648 OpAtomicStore %u32_var %subgroup %release %u32_1
649 )";
650
651 CompileSuccessfully(GenerateKernelCode(body));
652 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
653 }
654
655 TEST_F(ValidateAtomics, AtomicStoreShaderSuccess) {
656 const std::string body = R"(
657 OpAtomicStore %u32_var %device %release %u32_1
658 OpAtomicStore %u32_var %subgroup %sequentially_consistent %u32_1
659 )";
660
661 CompileSuccessfully(GenerateShaderCode(body));
662 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
663 }
664
665 TEST_F(ValidateAtomics, AtomicStoreVulkanSuccess) {
666 const std::string body = R"(
667 OpAtomicStore %u32_var %device %release %u32_1
668 )";
669
670 CompileSuccessfully(GenerateShaderCode(body), SPV_ENV_VULKAN_1_0);
671 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_0));
672 }
673
674 TEST_F(ValidateAtomics, AtomicStoreVulkanAcquire) {
675 const std::string body = R"(
676 OpAtomicStore %u32_var %device %acquire %u32_1
677 )";
678
679 CompileSuccessfully(GenerateShaderCode(body), SPV_ENV_VULKAN_1_0);
680 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
681 EXPECT_THAT(
682 getDiagnosticString(),
683 HasSubstr("Vulkan spec disallows OpAtomicStore with Memory Semantics "
684 "Acquire, AcquireRelease and SequentiallyConsistent"));
685 }
686
687 TEST_F(ValidateAtomics, AtomicStoreVulkanAcquireRelease) {
688 const std::string body = R"(
689 OpAtomicStore %u32_var %device %acquire_release %u32_1
690 )";
691
692 CompileSuccessfully(GenerateShaderCode(body), SPV_ENV_VULKAN_1_0);
693 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
694 EXPECT_THAT(
695 getDiagnosticString(),
696 HasSubstr("Vulkan spec disallows OpAtomicStore with Memory Semantics "
697 "Acquire, AcquireRelease and SequentiallyConsistent"));
698 }
699
700 TEST_F(ValidateAtomics, AtomicStoreVulkanSequentiallyConsistent) {
701 const std::string body = R"(
702 OpAtomicStore %u32_var %device %sequentially_consistent %u32_1
703 )";
704
705 CompileSuccessfully(GenerateShaderCode(body), SPV_ENV_VULKAN_1_0);
706 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_0));
707 EXPECT_THAT(
708 getDiagnosticString(),
709 HasSubstr("Vulkan spec disallows OpAtomicStore with Memory Semantics "
710 "Acquire, AcquireRelease and SequentiallyConsistent"));
711 }
712
713 TEST_F(ValidateAtomics, AtomicStoreWebGPUSuccess) {
714 const std::string body = R"(
715 OpAtomicStore %u32_var %queuefamily %relaxed %u32_1
716 )";
717
718 CompileSuccessfully(GenerateWebGPUShaderCode(body), SPV_ENV_WEBGPU_0);
719 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
720 }
721 TEST_F(ValidateAtomics, AtomicStoreWebGPUNonQueueFamilyFailure) {
722 const std::string body = R"(
723 OpAtomicStore %u32_var %workgroup %relaxed %u32_1
724 )";
725
726 CompileSuccessfully(GenerateWebGPUShaderCode(body), SPV_ENV_WEBGPU_0);
727 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_WEBGPU_0));
728 EXPECT_THAT(getDiagnosticString(),
729 HasSubstr("Memory Scope is limited to QueueFamilyKHR for "
730 "OpAtomic* operations"));
731 }
732
733 TEST_F(ValidateAtomics, AtomicStoreWebGPUNonRelaxedFailure) {
734 const std::string body = R"(
735 OpAtomicStore %u32_var %queuefamily %release %u32_1
736 )";
737
738 CompileSuccessfully(GenerateWebGPUShaderCode(body), SPV_ENV_WEBGPU_0);
739 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_WEBGPU_0));
740 EXPECT_THAT(getDiagnosticString(),
741 HasSubstr("no bits may be set for Memory Semantics of OpAtomic* "
742 "instructions"));
743 }
744
745 TEST_F(ValidateAtomics, AtomicStoreWrongPointerType) {
746 const std::string body = R"(
747 OpAtomicStore %f32_1 %device %relaxed %f32_1
748 )";
749
750 CompileSuccessfully(GenerateKernelCode(body));
751 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
752 EXPECT_THAT(
753 getDiagnosticString(),
754 HasSubstr("AtomicStore: expected Pointer to be of type OpTypePointer"));
755 }
756
757 TEST_F(ValidateAtomics, AtomicStoreWrongPointerDataType) {
758 const std::string body = R"(
759 OpAtomicStore %f32vec4_var %device %relaxed %f32_1
760 )";
761
762 CompileSuccessfully(GenerateKernelCode(body));
763 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
764 EXPECT_THAT(
765 getDiagnosticString(),
766 HasSubstr("AtomicStore: "
767 "expected Pointer to be a pointer to int or float scalar "
768 "type"));
769 }
770
771 TEST_F(ValidateAtomics, AtomicStoreWrongPointerStorageTypeForOpenCL) {
772 const std::string body = R"(
773 OpAtomicStore %f32_im_var %device %relaxed %f32_1
774 )";
775
776 CompileSuccessfully(GenerateKernelCode(body));
777 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_OPENCL_1_2));
778 EXPECT_THAT(
779 getDiagnosticString(),
780 HasSubstr("AtomicStore: storage class must be Function, Workgroup, "
781 "CrossWorkGroup or Generic in the OpenCL environment."));
782 }
783
784 TEST_F(ValidateAtomics, AtomicStoreWrongPointerStorageType) {
785 const std::string body = R"(
786 OpAtomicStore %f32_uc_var %device %relaxed %f32_1
787 )";
788
789 CompileSuccessfully(GenerateKernelCode(body));
790 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
791 EXPECT_THAT(getDiagnosticString(),
792 HasSubstr("AtomicStore: storage class forbidden by universal "
793 "validation rules."));
794 }
795
796 TEST_F(ValidateAtomics, AtomicStoreWrongScopeType) {
797 const std::string body = R"(
798 OpAtomicStore %f32_var %f32_1 %relaxed %f32_1
799 )";
800
801 CompileSuccessfully(GenerateKernelCode(body));
802 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
803 EXPECT_THAT(getDiagnosticString(),
804 HasSubstr("AtomicStore: expected scope to be a 32-bit int\n "
805 "OpAtomicStore %28 %float_1 %uint_0_1 %float_1\n"));
806 }
807
808 TEST_F(ValidateAtomics, AtomicStoreWrongMemorySemanticsType) {
809 const std::string body = R"(
810 OpAtomicStore %f32_var %device %f32_1 %f32_1
811 )";
812
813 CompileSuccessfully(GenerateKernelCode(body));
814 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
815 EXPECT_THAT(
816 getDiagnosticString(),
817 HasSubstr("AtomicStore: expected Memory Semantics to be a 32-bit int"));
818 }
819
820 TEST_F(ValidateAtomics, AtomicStoreWrongValueType) {
821 const std::string body = R"(
822 OpAtomicStore %f32_var %device %relaxed %u32_1
823 )";
824
825 CompileSuccessfully(GenerateKernelCode(body));
826 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
827 EXPECT_THAT(
828 getDiagnosticString(),
829 HasSubstr("AtomicStore: "
830 "expected Value type and the type pointed to by Pointer to "
831 "be the same"));
832 }
833
834 TEST_F(ValidateAtomics, AtomicExchangeShaderSuccess) {
835 const std::string body = R"(
836 OpAtomicStore %u32_var %device %relaxed %u32_1
837 %val2 = OpAtomicExchange %u32 %u32_var %device %relaxed %u32_0
838 )";
839
840 CompileSuccessfully(GenerateShaderCode(body));
841 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
842 }
843
844 TEST_F(ValidateAtomics, AtomicExchangeKernelSuccess) {
845 const std::string body = R"(
846 OpAtomicStore %f32_var %device %relaxed %f32_1
847 %val2 = OpAtomicExchange %f32 %f32_var %device %relaxed %f32_0
848 OpAtomicStore %u32_var %device %relaxed %u32_1
849 %val4 = OpAtomicExchange %u32 %u32_var %device %relaxed %u32_0
850 )";
851
852 CompileSuccessfully(GenerateKernelCode(body));
853 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
854 }
855
856 TEST_F(ValidateAtomics, AtomicExchangeShaderFloat) {
857 const std::string body = R"(
858 OpAtomicStore %f32_var %device %relaxed %f32_1
859 %val2 = OpAtomicExchange %f32 %f32_var %device %relaxed %f32_0
860 )";
861
862 CompileSuccessfully(GenerateShaderCode(body));
863 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
864 }
865
866 TEST_F(ValidateAtomics, AtomicExchangeWrongResultType) {
867 const std::string body = R"(
868 OpStore %f32vec4_var %f32vec4_0000
869 %val2 = OpAtomicExchange %f32vec4 %f32vec4_var %device %relaxed %f32vec4_0000
870 )";
871
872 CompileSuccessfully(GenerateKernelCode(body));
873 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
874 EXPECT_THAT(getDiagnosticString(),
875 HasSubstr("AtomicExchange: "
876 "expected Result Type to be int or float scalar type"));
877 }
878
879 TEST_F(ValidateAtomics, AtomicExchangeWrongPointerType) {
880 const std::string body = R"(
881 %val2 = OpAtomicExchange %f32 %f32vec4_ptr %device %relaxed %f32vec4_0000
882 )";
883
884 CompileSuccessfully(GenerateKernelCode(body));
885 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
886 EXPECT_THAT(getDiagnosticString(),
887 HasSubstr("Operand 33[%_ptr_Workgroup_v4float] cannot be a "
888 "type"));
889 }
890
891 TEST_F(ValidateAtomics, AtomicExchangeWrongPointerDataType) {
892 const std::string body = R"(
893 OpStore %f32vec4_var %f32vec4_0000
894 %val2 = OpAtomicExchange %f32 %f32vec4_var %device %relaxed %f32vec4_0000
895 )";
896
897 CompileSuccessfully(GenerateKernelCode(body));
898 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
899 EXPECT_THAT(
900 getDiagnosticString(),
901 HasSubstr("AtomicExchange: "
902 "expected Pointer to point to a value of type Result Type"));
903 }
904
905 TEST_F(ValidateAtomics, AtomicExchangeWrongScopeType) {
906 const std::string body = R"(
907 OpAtomicStore %f32_var %device %relaxed %f32_1
908 %val2 = OpAtomicExchange %f32 %f32_var %f32_1 %relaxed %f32_0
909 )";
910
911 CompileSuccessfully(GenerateKernelCode(body));
912 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
913 EXPECT_THAT(getDiagnosticString(),
914 HasSubstr("AtomicExchange: expected scope to be a 32-bit int"));
915 }
916
917 TEST_F(ValidateAtomics, AtomicExchangeWrongMemorySemanticsType) {
918 const std::string body = R"(
919 OpAtomicStore %f32_var %device %relaxed %f32_1
920 %val2 = OpAtomicExchange %f32 %f32_var %device %f32_1 %f32_0
921 )";
922
923 CompileSuccessfully(GenerateKernelCode(body));
924 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
925 EXPECT_THAT(
926 getDiagnosticString(),
927 HasSubstr(
928 "AtomicExchange: expected Memory Semantics to be a 32-bit int"));
929 }
930
931 TEST_F(ValidateAtomics, AtomicExchangeWrongValueType) {
932 const std::string body = R"(
933 OpAtomicStore %f32_var %device %relaxed %f32_1
934 %val2 = OpAtomicExchange %f32 %f32_var %device %relaxed %u32_0
935 )";
936
937 CompileSuccessfully(GenerateKernelCode(body));
938 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
939 EXPECT_THAT(getDiagnosticString(),
940 HasSubstr("AtomicExchange: "
941 "expected Value to be of type Result Type"));
942 }
943
944 TEST_F(ValidateAtomics, AtomicCompareExchangeShaderSuccess) {
945 const std::string body = R"(
946 OpAtomicStore %u32_var %device %relaxed %u32_1
947 %val2 = OpAtomicCompareExchange %u32 %u32_var %device %relaxed %relaxed %u32_0 %u32_0
948 )";
949
950 CompileSuccessfully(GenerateShaderCode(body));
951 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
952 }
953
954 TEST_F(ValidateAtomics, AtomicCompareExchangeKernelSuccess) {
955 const std::string body = R"(
956 OpAtomicStore %f32_var %device %relaxed %f32_1
957 %val2 = OpAtomicCompareExchange %f32 %f32_var %device %relaxed %relaxed %f32_0 %f32_1
958 OpAtomicStore %u32_var %device %relaxed %u32_1
959 %val4 = OpAtomicCompareExchange %u32 %u32_var %device %relaxed %relaxed %u32_0 %u32_0
960 )";
961
962 CompileSuccessfully(GenerateKernelCode(body));
963 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
964 }
965
966 TEST_F(ValidateAtomics, AtomicCompareExchangeShaderFloat) {
967 const std::string body = R"(
968 OpAtomicStore %f32_var %device %relaxed %f32_1
969 %val1 = OpAtomicCompareExchange %f32 %f32_var %device %relaxed %relaxed %f32_0 %f32_1
970 )";
971
972 CompileSuccessfully(GenerateShaderCode(body));
973 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
974 EXPECT_THAT(getDiagnosticString(),
975 HasSubstr("AtomicCompareExchange: "
976 "expected Result Type to be int scalar type"));
977 }
978
979 TEST_F(ValidateAtomics, AtomicCompareExchangeWrongResultType) {
980 const std::string body = R"(
981 OpStore %f32vec4_var %f32vec4_0000
982 %val2 = OpAtomicCompareExchange %f32vec4 %f32vec4_var %device %relaxed %relaxed %f32vec4_0000 %f32vec4_0000
983 )";
984
985 CompileSuccessfully(GenerateKernelCode(body));
986 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
987 EXPECT_THAT(getDiagnosticString(),
988 HasSubstr("AtomicCompareExchange: "
989 "expected Result Type to be int or float scalar type"));
990 }
991
992 TEST_F(ValidateAtomics, AtomicCompareExchangeWrongPointerType) {
993 const std::string body = R"(
994 %val2 = OpAtomicCompareExchange %f32 %f32vec4_ptr %device %relaxed %relaxed %f32vec4_0000 %f32vec4_0000
995 )";
996
997 CompileSuccessfully(GenerateKernelCode(body));
998 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
999 EXPECT_THAT(getDiagnosticString(),
1000 HasSubstr("Operand 33[%_ptr_Workgroup_v4float] cannot be a "
1001 "type"));
1002 }
1003
1004 TEST_F(ValidateAtomics, AtomicCompareExchangeWrongPointerDataType) {
1005 const std::string body = R"(
1006 OpStore %f32vec4_var %f32vec4_0000
1007 %val2 = OpAtomicCompareExchange %f32 %f32vec4_var %device %relaxed %relaxed %f32_0 %f32_1
1008 )";
1009
1010 CompileSuccessfully(GenerateKernelCode(body));
1011 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1012 EXPECT_THAT(
1013 getDiagnosticString(),
1014 HasSubstr("AtomicCompareExchange: "
1015 "expected Pointer to point to a value of type Result Type"));
1016 }
1017
1018 TEST_F(ValidateAtomics, AtomicCompareExchangeWrongScopeType) {
1019 const std::string body = R"(
1020 OpAtomicStore %f32_var %device %relaxed %f32_1
1021 %val2 = OpAtomicCompareExchange %f32 %f32_var %f32_1 %relaxed %relaxed %f32_0 %f32_0
1022 )";
1023
1024 CompileSuccessfully(GenerateKernelCode(body));
1025 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1026 EXPECT_THAT(getDiagnosticString(),
1027 HasSubstr("AtomicCompareExchange: expected scope to be a 32-bit "
1028 "int"));
1029 }
1030
1031 TEST_F(ValidateAtomics, AtomicCompareExchangeWrongMemorySemanticsType1) {
1032 const std::string body = R"(
1033 OpAtomicStore %f32_var %device %relaxed %f32_1
1034 %val2 = OpAtomicCompareExchange %f32 %f32_var %device %f32_1 %relaxed %f32_0 %f32_0
1035 )";
1036
1037 CompileSuccessfully(GenerateKernelCode(body));
1038 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1039 EXPECT_THAT(getDiagnosticString(),
1040 HasSubstr("AtomicCompareExchange: expected Memory Semantics to "
1041 "be a 32-bit int"));
1042 }
1043
1044 TEST_F(ValidateAtomics, AtomicCompareExchangeWrongMemorySemanticsType2) {
1045 const std::string body = R"(
1046 OpAtomicStore %f32_var %device %relaxed %f32_1
1047 %val2 = OpAtomicCompareExchange %f32 %f32_var %device %relaxed %f32_1 %f32_0 %f32_0
1048 )";
1049
1050 CompileSuccessfully(GenerateKernelCode(body));
1051 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1052 EXPECT_THAT(getDiagnosticString(),
1053 HasSubstr("AtomicCompareExchange: expected Memory Semantics to "
1054 "be a 32-bit int"));
1055 }
1056
1057 TEST_F(ValidateAtomics, AtomicCompareExchangeUnequalRelease) {
1058 const std::string body = R"(
1059 OpAtomicStore %f32_var %device %relaxed %f32_1
1060 %val2 = OpAtomicCompareExchange %f32 %f32_var %device %relaxed %release %f32_0 %f32_0
1061 )";
1062
1063 CompileSuccessfully(GenerateKernelCode(body));
1064 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1065 EXPECT_THAT(getDiagnosticString(),
1066 HasSubstr("AtomicCompareExchange: Memory Semantics Release and "
1067 "AcquireRelease cannot be used for operand Unequal"));
1068 }
1069
1070 TEST_F(ValidateAtomics, AtomicCompareExchangeWrongValueType) {
1071 const std::string body = R"(
1072 OpAtomicStore %f32_var %device %relaxed %f32_1
1073 %val2 = OpAtomicCompareExchange %f32 %f32_var %device %relaxed %relaxed %u32_0 %f32_1
1074 )";
1075
1076 CompileSuccessfully(GenerateKernelCode(body));
1077 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1078 EXPECT_THAT(getDiagnosticString(),
1079 HasSubstr("AtomicCompareExchange: "
1080 "expected Value to be of type Result Type"));
1081 }
1082
1083 TEST_F(ValidateAtomics, AtomicCompareExchangeWrongComparatorType) {
1084 const std::string body = R"(
1085 OpAtomicStore %f32_var %device %relaxed %f32_1
1086 %val2 = OpAtomicCompareExchange %f32 %f32_var %device %relaxed %relaxed %f32_0 %u32_1
1087 )";
1088
1089 CompileSuccessfully(GenerateKernelCode(body));
1090 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1091 EXPECT_THAT(getDiagnosticString(),
1092 HasSubstr("AtomicCompareExchange: "
1093 "expected Comparator to be of type Result Type"));
1094 }
1095
1096 TEST_F(ValidateAtomics, AtomicCompareExchangeWeakSuccess) {
1097 const std::string body = R"(
1098 OpAtomicStore %u32_var %device %relaxed %u32_1
1099 %val4 = OpAtomicCompareExchangeWeak %u32 %u32_var %device %relaxed %relaxed %u32_0 %u32_0
1100 )";
1101
1102 CompileSuccessfully(GenerateKernelCode(body));
1103 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1104 }
1105
1106 TEST_F(ValidateAtomics, AtomicCompareExchangeWeakWrongResultType) {
1107 const std::string body = R"(
1108 OpAtomicStore %f32_var %device %relaxed %f32_1
1109 %val2 = OpAtomicCompareExchangeWeak %f32 %f32_var %device %relaxed %relaxed %f32_0 %f32_1
1110 )";
1111
1112 CompileSuccessfully(GenerateKernelCode(body));
1113 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1114 EXPECT_THAT(getDiagnosticString(),
1115 HasSubstr("AtomicCompareExchangeWeak: "
1116 "expected Result Type to be int scalar type"));
1117 }
1118
1119 TEST_F(ValidateAtomics, AtomicArithmeticsSuccess) {
1120 const std::string body = R"(
1121 OpAtomicStore %u32_var %device %relaxed %u32_1
1122 %val1 = OpAtomicIIncrement %u32 %u32_var %device %acquire_release
1123 %val2 = OpAtomicIDecrement %u32 %u32_var %device %acquire_release
1124 %val3 = OpAtomicIAdd %u32 %u32_var %device %acquire_release %u32_1
1125 %val4 = OpAtomicISub %u32 %u32_var %device %acquire_release %u32_1
1126 %val5 = OpAtomicUMin %u32 %u32_var %device %acquire_release %u32_1
1127 %val6 = OpAtomicUMax %u32 %u32_var %device %acquire_release %u32_1
1128 %val7 = OpAtomicSMin %u32 %u32_var %device %sequentially_consistent %u32_1
1129 %val8 = OpAtomicSMax %u32 %u32_var %device %sequentially_consistent %u32_1
1130 %val9 = OpAtomicAnd %u32 %u32_var %device %sequentially_consistent %u32_1
1131 %val10 = OpAtomicOr %u32 %u32_var %device %sequentially_consistent %u32_1
1132 %val11 = OpAtomicXor %u32 %u32_var %device %sequentially_consistent %u32_1
1133 )";
1134
1135 CompileSuccessfully(GenerateKernelCode(body));
1136 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1137 }
1138
1139 TEST_F(ValidateAtomics, AtomicFlagsSuccess) {
1140 const std::string body = R"(
1141 OpAtomicFlagClear %u32_var %device %release
1142 %val1 = OpAtomicFlagTestAndSet %bool %u32_var %device %relaxed
1143 )";
1144
1145 CompileSuccessfully(GenerateKernelCode(body));
1146 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1147 }
1148
1149 TEST_F(ValidateAtomics, AtomicFlagTestAndSetWrongResultType) {
1150 const std::string body = R"(
1151 %val1 = OpAtomicFlagTestAndSet %u32 %u32_var %device %relaxed
1152 )";
1153
1154 CompileSuccessfully(GenerateKernelCode(body));
1155 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1156 EXPECT_THAT(getDiagnosticString(),
1157 HasSubstr("AtomicFlagTestAndSet: "
1158 "expected Result Type to be bool scalar type"));
1159 }
1160
1161 TEST_F(ValidateAtomics, AtomicFlagTestAndSetNotPointer) {
1162 const std::string body = R"(
1163 %val1 = OpAtomicFlagTestAndSet %bool %u32_1 %device %relaxed
1164 )";
1165
1166 CompileSuccessfully(GenerateKernelCode(body));
1167 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1168 EXPECT_THAT(getDiagnosticString(),
1169 HasSubstr("AtomicFlagTestAndSet: "
1170 "expected Pointer to be of type OpTypePointer"));
1171 }
1172
1173 TEST_F(ValidateAtomics, AtomicFlagTestAndSetNotIntPointer) {
1174 const std::string body = R"(
1175 %val1 = OpAtomicFlagTestAndSet %bool %f32_var %device %relaxed
1176 )";
1177
1178 CompileSuccessfully(GenerateKernelCode(body));
1179 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1180 EXPECT_THAT(
1181 getDiagnosticString(),
1182 HasSubstr("AtomicFlagTestAndSet: "
1183 "expected Pointer to point to a value of 32-bit int type"));
1184 }
1185
1186 TEST_F(ValidateAtomics, AtomicFlagTestAndSetNotInt32Pointer) {
1187 const std::string body = R"(
1188 %val1 = OpAtomicFlagTestAndSet %bool %u64_var %device %relaxed
1189 )";
1190
1191 CompileSuccessfully(GenerateKernelCode(body));
1192 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1193 EXPECT_THAT(
1194 getDiagnosticString(),
1195 HasSubstr("AtomicFlagTestAndSet: "
1196 "expected Pointer to point to a value of 32-bit int type"));
1197 }
1198
1199 TEST_F(ValidateAtomics, AtomicFlagTestAndSetWrongScopeType) {
1200 const std::string body = R"(
1201 %val1 = OpAtomicFlagTestAndSet %bool %u32_var %u64_1 %relaxed
1202 )";
1203
1204 CompileSuccessfully(GenerateKernelCode(body));
1205 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1206 EXPECT_THAT(
1207 getDiagnosticString(),
1208 HasSubstr("AtomicFlagTestAndSet: expected scope to be a 32-bit int"));
1209 }
1210
1211 TEST_F(ValidateAtomics, AtomicFlagTestAndSetWrongMemorySemanticsType) {
1212 const std::string body = R"(
1213 %val1 = OpAtomicFlagTestAndSet %bool %u32_var %device %u64_1
1214 )";
1215
1216 CompileSuccessfully(GenerateKernelCode(body));
1217 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1218 EXPECT_THAT(getDiagnosticString(),
1219 HasSubstr("AtomicFlagTestAndSet: "
1220 "expected Memory Semantics to be a 32-bit int"));
1221 }
1222
1223 TEST_F(ValidateAtomics, AtomicFlagClearAcquire) {
1224 const std::string body = R"(
1225 OpAtomicFlagClear %u32_var %device %acquire
1226 )";
1227
1228 CompileSuccessfully(GenerateKernelCode(body));
1229 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1230 EXPECT_THAT(getDiagnosticString(),
1231 HasSubstr("Memory Semantics Acquire and AcquireRelease cannot be "
1232 "used with AtomicFlagClear"));
1233 }
1234
1235 TEST_F(ValidateAtomics, AtomicFlagClearNotPointer) {
1236 const std::string body = R"(
1237 OpAtomicFlagClear %u32_1 %device %relaxed
1238 )";
1239
1240 CompileSuccessfully(GenerateKernelCode(body));
1241 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1242 EXPECT_THAT(getDiagnosticString(),
1243 HasSubstr("AtomicFlagClear: "
1244 "expected Pointer to be of type OpTypePointer"));
1245 }
1246
1247 TEST_F(ValidateAtomics, AtomicFlagClearNotIntPointer) {
1248 const std::string body = R"(
1249 OpAtomicFlagClear %f32_var %device %relaxed
1250 )";
1251
1252 CompileSuccessfully(GenerateKernelCode(body));
1253 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1254 EXPECT_THAT(
1255 getDiagnosticString(),
1256 HasSubstr("AtomicFlagClear: "
1257 "expected Pointer to point to a value of 32-bit int type"));
1258 }
1259
1260 TEST_F(ValidateAtomics, AtomicFlagClearNotInt32Pointer) {
1261 const std::string body = R"(
1262 OpAtomicFlagClear %u64_var %device %relaxed
1263 )";
1264
1265 CompileSuccessfully(GenerateKernelCode(body));
1266 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1267 EXPECT_THAT(
1268 getDiagnosticString(),
1269 HasSubstr("AtomicFlagClear: "
1270 "expected Pointer to point to a value of 32-bit int type"));
1271 }
1272
1273 TEST_F(ValidateAtomics, AtomicFlagClearWrongScopeType) {
1274 const std::string body = R"(
1275 OpAtomicFlagClear %u32_var %u64_1 %relaxed
1276 )";
1277
1278 CompileSuccessfully(GenerateKernelCode(body));
1279 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1280 EXPECT_THAT(getDiagnosticString(),
1281 HasSubstr("AtomicFlagClear: expected scope to be a 32-bit "
1282 "int\n OpAtomicFlagClear %30 %ulong_1 %uint_0_1\n"));
1283 }
1284
1285 TEST_F(ValidateAtomics, AtomicFlagClearWrongMemorySemanticsType) {
1286 const std::string body = R"(
1287 OpAtomicFlagClear %u32_var %device %u64_1
1288 )";
1289
1290 CompileSuccessfully(GenerateKernelCode(body));
1291 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1292 EXPECT_THAT(
1293 getDiagnosticString(),
1294 HasSubstr(
1295 "AtomicFlagClear: expected Memory Semantics to be a 32-bit int"));
1296 }
1297
1298 TEST_F(ValidateAtomics, AtomicIIncrementAcquireAndRelease) {
1299 const std::string body = R"(
1300 OpAtomicStore %u32_var %device %relaxed %u32_1
1301 %val1 = OpAtomicIIncrement %u32 %u32_var %device %acquire_and_release
1302 )";
1303
1304 CompileSuccessfully(GenerateKernelCode(body));
1305 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1306 EXPECT_THAT(getDiagnosticString(),
1307 HasSubstr("AtomicIIncrement: Memory Semantics can have at most "
1308 "one of the following bits set: Acquire, Release, "
1309 "AcquireRelease or SequentiallyConsistent"));
1310 }
1311
1312 TEST_F(ValidateAtomics, AtomicUniformMemorySemanticsShader) {
1313 const std::string body = R"(
1314 OpAtomicStore %u32_var %device %relaxed %u32_1
1315 %val1 = OpAtomicIIncrement %u32 %u32_var %device %acquire_release_uniform_workgroup
1316 )";
1317
1318 CompileSuccessfully(GenerateShaderCode(body));
1319 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1320 }
1321
1322 TEST_F(ValidateAtomics, AtomicUniformMemorySemanticsKernel) {
1323 const std::string body = R"(
1324 OpAtomicStore %u32_var %device %relaxed %u32_1
1325 %val1 = OpAtomicIIncrement %u32 %u32_var %device %acquire_release_uniform_workgroup
1326 )";
1327
1328 CompileSuccessfully(GenerateKernelCode(body));
1329 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1330 EXPECT_THAT(getDiagnosticString(),
1331 HasSubstr("AtomicIIncrement: Memory Semantics UniformMemory "
1332 "requires capability Shader"));
1333 }
1334
1335 // Lack of the AtomicStorage capability is intentionally ignored, see
1336 // https://github.com/KhronosGroup/glslang/issues/1618 for the reasoning why.
1337 TEST_F(ValidateAtomics, AtomicCounterMemorySemanticsNoCapability) {
1338 const std::string body = R"(
1339 OpAtomicStore %u32_var %device %relaxed %u32_1
1340 %val1 = OpAtomicIIncrement %u32 %u32_var %device
1341 %acquire_release_atomic_counter_workgroup
1342 )";
1343
1344 CompileSuccessfully(GenerateKernelCode(body));
1345 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1346 }
1347
1348 TEST_F(ValidateAtomics, AtomicCounterMemorySemanticsWithCapability) {
1349 const std::string body = R"(
1350 OpAtomicStore %u32_var %device %relaxed %u32_1
1351 %val1 = OpAtomicIIncrement %u32 %u32_var %device %acquire_release_atomic_counter_workgroup
1352 )";
1353
1354 CompileSuccessfully(GenerateKernelCode(body, "OpCapability AtomicStorage\n"));
1355 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1356 }
1357
1358 TEST_F(ValidateAtomics, VulkanMemoryModelBanSequentiallyConsistentAtomicLoad) {
1359 const std::string body = R"(
1360 %ld = OpAtomicLoad %u32 %u32_var %workgroup %sequentially_consistent
1361 )";
1362
1363 const std::string extra = R"(
1364 OpCapability VulkanMemoryModelKHR
1365 OpExtension "SPV_KHR_vulkan_memory_model"
1366 )";
1367
1368 CompileSuccessfully(GenerateShaderCode(body, extra, "VulkanKHR"),
1369 SPV_ENV_UNIVERSAL_1_3);
1370 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1371 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1372 EXPECT_THAT(getDiagnosticString(),
1373 HasSubstr("SequentiallyConsistent memory semantics cannot be "
1374 "used with the VulkanKHR memory model."));
1375 }
1376
1377 TEST_F(ValidateAtomics, VulkanMemoryModelBanSequentiallyConsistentAtomicStore) {
1378 const std::string body = R"(
1379 OpAtomicStore %u32_var %workgroup %sequentially_consistent %u32_0
1380 )";
1381
1382 const std::string extra = R"(
1383 OpCapability VulkanMemoryModelKHR
1384 OpExtension "SPV_KHR_vulkan_memory_model"
1385 )";
1386
1387 CompileSuccessfully(GenerateShaderCode(body, extra, "VulkanKHR"),
1388 SPV_ENV_UNIVERSAL_1_3);
1389 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1390 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1391 EXPECT_THAT(getDiagnosticString(),
1392 HasSubstr("SequentiallyConsistent memory semantics cannot be "
1393 "used with the VulkanKHR memory model."));
1394 }
1395
1396 TEST_F(ValidateAtomics,
1397 VulkanMemoryModelBanSequentiallyConsistentAtomicExchange) {
1398 const std::string body = R"(
1399 %ex = OpAtomicExchange %u32 %u32_var %workgroup %sequentially_consistent %u32_0
1400 )";
1401
1402 const std::string extra = R"(
1403 OpCapability VulkanMemoryModelKHR
1404 OpExtension "SPV_KHR_vulkan_memory_model"
1405 )";
1406
1407 CompileSuccessfully(GenerateShaderCode(body, extra, "VulkanKHR"),
1408 SPV_ENV_UNIVERSAL_1_3);
1409 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1410 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1411 EXPECT_THAT(getDiagnosticString(),
1412 HasSubstr("SequentiallyConsistent memory semantics cannot be "
1413 "used with the VulkanKHR memory model."));
1414 }
1415
1416 TEST_F(ValidateAtomics,
1417 VulkanMemoryModelBanSequentiallyConsistentAtomicCompareExchangeEqual) {
1418 const std::string body = R"(
1419 %ex = OpAtomicCompareExchange %u32 %u32_var %workgroup %sequentially_consistent %relaxed %u32_0 %u32_0
1420 )";
1421
1422 const std::string extra = R"(
1423 OpCapability VulkanMemoryModelKHR
1424 OpExtension "SPV_KHR_vulkan_memory_model"
1425 )";
1426
1427 CompileSuccessfully(GenerateShaderCode(body, extra, "VulkanKHR"),
1428 SPV_ENV_UNIVERSAL_1_3);
1429 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1430 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1431 EXPECT_THAT(getDiagnosticString(),
1432 HasSubstr("SequentiallyConsistent memory semantics cannot be "
1433 "used with the VulkanKHR memory model."));
1434 }
1435
1436 TEST_F(ValidateAtomics,
1437 VulkanMemoryModelBanSequentiallyConsistentAtomicCompareExchangeUnequal) {
1438 const std::string body = R"(
1439 %ex = OpAtomicCompareExchange %u32 %u32_var %workgroup %relaxed %sequentially_consistent %u32_0 %u32_0
1440 )";
1441
1442 const std::string extra = R"(
1443 OpCapability VulkanMemoryModelKHR
1444 OpExtension "SPV_KHR_vulkan_memory_model"
1445 )";
1446
1447 CompileSuccessfully(GenerateShaderCode(body, extra, "VulkanKHR"),
1448 SPV_ENV_UNIVERSAL_1_3);
1449 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1450 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1451 EXPECT_THAT(getDiagnosticString(),
1452 HasSubstr("SequentiallyConsistent memory semantics cannot be "
1453 "used with the VulkanKHR memory model."));
1454 }
1455
1456 TEST_F(ValidateAtomics,
1457 VulkanMemoryModelBanSequentiallyConsistentAtomicIIncrement) {
1458 const std::string body = R"(
1459 %inc = OpAtomicIIncrement %u32 %u32_var %workgroup %sequentially_consistent
1460 )";
1461
1462 const std::string extra = R"(
1463 OpCapability VulkanMemoryModelKHR
1464 OpExtension "SPV_KHR_vulkan_memory_model"
1465 )";
1466
1467 CompileSuccessfully(GenerateShaderCode(body, extra, "VulkanKHR"),
1468 SPV_ENV_UNIVERSAL_1_3);
1469 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1470 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1471 EXPECT_THAT(getDiagnosticString(),
1472 HasSubstr("SequentiallyConsistent memory semantics cannot be "
1473 "used with the VulkanKHR memory model."));
1474 }
1475
1476 TEST_F(ValidateAtomics,
1477 VulkanMemoryModelBanSequentiallyConsistentAtomicIDecrement) {
1478 const std::string body = R"(
1479 %dec = OpAtomicIDecrement %u32 %u32_var %workgroup %sequentially_consistent
1480 )";
1481
1482 const std::string extra = R"(
1483 OpCapability VulkanMemoryModelKHR
1484 OpExtension "SPV_KHR_vulkan_memory_model"
1485 )";
1486
1487 CompileSuccessfully(GenerateShaderCode(body, extra, "VulkanKHR"),
1488 SPV_ENV_UNIVERSAL_1_3);
1489 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1490 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1491 EXPECT_THAT(getDiagnosticString(),
1492 HasSubstr("SequentiallyConsistent memory semantics cannot be "
1493 "used with the VulkanKHR memory model."));
1494 }
1495
1496 TEST_F(ValidateAtomics, VulkanMemoryModelBanSequentiallyConsistentAtomicIAdd) {
1497 const std::string body = R"(
1498 %add = OpAtomicIAdd %u32 %u32_var %workgroup %sequentially_consistent %u32_0
1499 )";
1500
1501 const std::string extra = R"(
1502 OpCapability VulkanMemoryModelKHR
1503 OpExtension "SPV_KHR_vulkan_memory_model"
1504 )";
1505
1506 CompileSuccessfully(GenerateShaderCode(body, extra, "VulkanKHR"),
1507 SPV_ENV_UNIVERSAL_1_3);
1508 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1509 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1510 EXPECT_THAT(getDiagnosticString(),
1511 HasSubstr("SequentiallyConsistent memory semantics cannot be "
1512 "used with the VulkanKHR memory model."));
1513 }
1514
1515 TEST_F(ValidateAtomics, VulkanMemoryModelBanSequentiallyConsistentAtomicISub) {
1516 const std::string body = R"(
1517 %sub = OpAtomicISub %u32 %u32_var %workgroup %sequentially_consistent %u32_0
1518 )";
1519
1520 const std::string extra = R"(
1521 OpCapability VulkanMemoryModelKHR
1522 OpExtension "SPV_KHR_vulkan_memory_model"
1523 )";
1524
1525 CompileSuccessfully(GenerateShaderCode(body, extra, "VulkanKHR"),
1526 SPV_ENV_UNIVERSAL_1_3);
1527 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1528 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1529 EXPECT_THAT(getDiagnosticString(),
1530 HasSubstr("SequentiallyConsistent memory semantics cannot be "
1531 "used with the VulkanKHR memory model."));
1532 }
1533
1534 TEST_F(ValidateAtomics, VulkanMemoryModelBanSequentiallyConsistentAtomicSMin) {
1535 const std::string body = R"(
1536 %min = OpAtomicSMin %u32 %u32_var %workgroup %sequentially_consistent %u32_0
1537 )";
1538
1539 const std::string extra = R"(
1540 OpCapability VulkanMemoryModelKHR
1541 OpExtension "SPV_KHR_vulkan_memory_model"
1542 )";
1543
1544 CompileSuccessfully(GenerateShaderCode(body, extra, "VulkanKHR"),
1545 SPV_ENV_UNIVERSAL_1_3);
1546 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1547 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1548 EXPECT_THAT(getDiagnosticString(),
1549 HasSubstr("SequentiallyConsistent memory semantics cannot be "
1550 "used with the VulkanKHR memory model."));
1551 }
1552
1553 TEST_F(ValidateAtomics, VulkanMemoryModelBanSequentiallyConsistentAtomicUMin) {
1554 const std::string body = R"(
1555 %min = OpAtomicUMin %u32 %u32_var %workgroup %sequentially_consistent %u32_0
1556 )";
1557
1558 const std::string extra = R"(
1559 OpCapability VulkanMemoryModelKHR
1560 OpExtension "SPV_KHR_vulkan_memory_model"
1561 )";
1562
1563 CompileSuccessfully(GenerateShaderCode(body, extra, "VulkanKHR"),
1564 SPV_ENV_UNIVERSAL_1_3);
1565 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1566 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1567 EXPECT_THAT(getDiagnosticString(),
1568 HasSubstr("SequentiallyConsistent memory semantics cannot be "
1569 "used with the VulkanKHR memory model."));
1570 }
1571
1572 TEST_F(ValidateAtomics, VulkanMemoryModelBanSequentiallyConsistentAtomicSMax) {
1573 const std::string body = R"(
1574 %max = OpAtomicSMax %u32 %u32_var %workgroup %sequentially_consistent %u32_0
1575 )";
1576
1577 const std::string extra = R"(
1578 OpCapability VulkanMemoryModelKHR
1579 OpExtension "SPV_KHR_vulkan_memory_model"
1580 )";
1581
1582 CompileSuccessfully(GenerateShaderCode(body, extra, "VulkanKHR"),
1583 SPV_ENV_UNIVERSAL_1_3);
1584 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1585 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1586 EXPECT_THAT(getDiagnosticString(),
1587 HasSubstr("SequentiallyConsistent memory semantics cannot be "
1588 "used with the VulkanKHR memory model."));
1589 }
1590
1591 TEST_F(ValidateAtomics, VulkanMemoryModelBanSequentiallyConsistentAtomicUMax) {
1592 const std::string body = R"(
1593 %max = OpAtomicUMax %u32 %u32_var %workgroup %sequentially_consistent %u32_0
1594 )";
1595
1596 const std::string extra = R"(
1597 OpCapability VulkanMemoryModelKHR
1598 OpExtension "SPV_KHR_vulkan_memory_model"
1599 )";
1600
1601 CompileSuccessfully(GenerateShaderCode(body, extra, "VulkanKHR"),
1602 SPV_ENV_UNIVERSAL_1_3);
1603 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1604 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1605 EXPECT_THAT(getDiagnosticString(),
1606 HasSubstr("SequentiallyConsistent memory semantics cannot be "
1607 "used with the VulkanKHR memory model."));
1608 }
1609
1610 TEST_F(ValidateAtomics, VulkanMemoryModelBanSequentiallyConsistentAtomicAnd) {
1611 const std::string body = R"(
1612 %and = OpAtomicAnd %u32 %u32_var %workgroup %sequentially_consistent %u32_0
1613 )";
1614
1615 const std::string extra = R"(
1616 OpCapability VulkanMemoryModelKHR
1617 OpExtension "SPV_KHR_vulkan_memory_model"
1618 )";
1619
1620 CompileSuccessfully(GenerateShaderCode(body, extra, "VulkanKHR"),
1621 SPV_ENV_UNIVERSAL_1_3);
1622 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1623 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1624 EXPECT_THAT(getDiagnosticString(),
1625 HasSubstr("SequentiallyConsistent memory semantics cannot be "
1626 "used with the VulkanKHR memory model."));
1627 }
1628
1629 TEST_F(ValidateAtomics, VulkanMemoryModelBanSequentiallyConsistentAtomicOr) {
1630 const std::string body = R"(
1631 %or = OpAtomicOr %u32 %u32_var %workgroup %sequentially_consistent %u32_0
1632 )";
1633
1634 const std::string extra = R"(
1635 OpCapability VulkanMemoryModelKHR
1636 OpExtension "SPV_KHR_vulkan_memory_model"
1637 )";
1638
1639 CompileSuccessfully(GenerateShaderCode(body, extra, "VulkanKHR"),
1640 SPV_ENV_UNIVERSAL_1_3);
1641 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1642 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1643 EXPECT_THAT(getDiagnosticString(),
1644 HasSubstr("SequentiallyConsistent memory semantics cannot be "
1645 "used with the VulkanKHR memory model."));
1646 }
1647
1648 TEST_F(ValidateAtomics, VulkanMemoryModelBanSequentiallyConsistentAtomicXor) {
1649 const std::string body = R"(
1650 %xor = OpAtomicXor %u32 %u32_var %workgroup %sequentially_consistent %u32_0
1651 )";
1652
1653 const std::string extra = R"(
1654 OpCapability VulkanMemoryModelKHR
1655 OpExtension "SPV_KHR_vulkan_memory_model"
1656 )";
1657
1658 CompileSuccessfully(GenerateShaderCode(body, extra, "VulkanKHR"),
1659 SPV_ENV_UNIVERSAL_1_3);
1660 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1661 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1662 EXPECT_THAT(getDiagnosticString(),
1663 HasSubstr("SequentiallyConsistent memory semantics cannot be "
1664 "used with the VulkanKHR memory model."));
1665 }
1666
1667 TEST_F(ValidateAtomics, OutputMemoryKHRRequiresVulkanMemoryModelKHR) {
1668 const std::string text = R"(
1669 OpCapability Shader
1670 OpMemoryModel Logical GLSL450
1671 OpEntryPoint Fragment %1 "func"
1672 OpExecutionMode %1 OriginUpperLeft
1673 %2 = OpTypeVoid
1674 %3 = OpTypeInt 32 0
1675 %semantics = OpConstant %3 4100
1676 %5 = OpTypeFunction %2
1677 %workgroup = OpConstant %3 2
1678 %ptr = OpTypePointer Workgroup %3
1679 %var = OpVariable %ptr Workgroup
1680 %1 = OpFunction %2 None %5
1681 %7 = OpLabel
1682 OpAtomicStore %var %workgroup %semantics %workgroup
1683 OpReturn
1684 OpFunctionEnd
1685 )";
1686
1687 CompileSuccessfully(text);
1688 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1689 EXPECT_THAT(getDiagnosticString(),
1690 HasSubstr("AtomicStore: Memory Semantics OutputMemoryKHR "
1691 "requires capability VulkanMemoryModelKHR"));
1692 }
1693
1694 TEST_F(ValidateAtomics, MakeAvailableKHRRequiresVulkanMemoryModelKHR) {
1695 const std::string text = R"(
1696 OpCapability Shader
1697 OpMemoryModel Logical GLSL450
1698 OpEntryPoint Fragment %1 "func"
1699 OpExecutionMode %1 OriginUpperLeft
1700 %2 = OpTypeVoid
1701 %3 = OpTypeInt 32 0
1702 %semantics = OpConstant %3 8196
1703 %5 = OpTypeFunction %2
1704 %workgroup = OpConstant %3 2
1705 %ptr = OpTypePointer Workgroup %3
1706 %var = OpVariable %ptr Workgroup
1707 %1 = OpFunction %2 None %5
1708 %7 = OpLabel
1709 OpAtomicStore %var %workgroup %semantics %workgroup
1710 OpReturn
1711 OpFunctionEnd
1712 )";
1713
1714 CompileSuccessfully(text);
1715 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1716 EXPECT_THAT(getDiagnosticString(),
1717 HasSubstr("AtomicStore: Memory Semantics MakeAvailableKHR "
1718 "requires capability VulkanMemoryModelKHR"));
1719 }
1720
1721 TEST_F(ValidateAtomics, MakeVisibleKHRRequiresVulkanMemoryModelKHR) {
1722 const std::string text = R"(
1723 OpCapability Shader
1724 OpMemoryModel Logical GLSL450
1725 OpEntryPoint Fragment %1 "func"
1726 OpExecutionMode %1 OriginUpperLeft
1727 %2 = OpTypeVoid
1728 %3 = OpTypeInt 32 0
1729 %semantics = OpConstant %3 16386
1730 %5 = OpTypeFunction %2
1731 %workgroup = OpConstant %3 2
1732 %ptr = OpTypePointer Workgroup %3
1733 %var = OpVariable %ptr Workgroup
1734 %1 = OpFunction %2 None %5
1735 %7 = OpLabel
1736 %ld = OpAtomicLoad %3 %var %workgroup %semantics
1737 OpReturn
1738 OpFunctionEnd
1739 )";
1740
1741 CompileSuccessfully(text);
1742 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1743 EXPECT_THAT(getDiagnosticString(),
1744 HasSubstr("AtomicLoad: Memory Semantics MakeVisibleKHR requires "
1745 "capability VulkanMemoryModelKHR"));
1746 }
1747
1748 TEST_F(ValidateAtomics, MakeAvailableKHRRequiresReleaseSemantics) {
1749 const std::string text = R"(
1750 OpCapability Shader
1751 OpCapability VulkanMemoryModelKHR
1752 OpExtension "SPV_KHR_vulkan_memory_model"
1753 OpMemoryModel Logical VulkanKHR
1754 OpEntryPoint Fragment %1 "func"
1755 OpExecutionMode %1 OriginUpperLeft
1756 %2 = OpTypeVoid
1757 %3 = OpTypeInt 32 0
1758 %semantics = OpConstant %3 8448
1759 %5 = OpTypeFunction %2
1760 %workgroup = OpConstant %3 2
1761 %ptr = OpTypePointer Workgroup %3
1762 %var = OpVariable %ptr Workgroup
1763 %1 = OpFunction %2 None %5
1764 %7 = OpLabel
1765 OpAtomicStore %var %workgroup %semantics %workgroup
1766 OpReturn
1767 OpFunctionEnd
1768 )";
1769
1770 CompileSuccessfully(text, SPV_ENV_UNIVERSAL_1_3);
1771 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1772 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1773 EXPECT_THAT(
1774 getDiagnosticString(),
1775 HasSubstr("AtomicStore: MakeAvailableKHR Memory Semantics also requires "
1776 "either Release or AcquireRelease Memory Semantics"));
1777 }
1778
1779 TEST_F(ValidateAtomics, MakeVisibleKHRRequiresAcquireSemantics) {
1780 const std::string text = R"(
1781 OpCapability Shader
1782 OpCapability VulkanMemoryModelKHR
1783 OpExtension "SPV_KHR_vulkan_memory_model"
1784 OpMemoryModel Logical VulkanKHR
1785 OpEntryPoint Fragment %1 "func"
1786 OpExecutionMode %1 OriginUpperLeft
1787 %2 = OpTypeVoid
1788 %3 = OpTypeInt 32 0
1789 %semantics = OpConstant %3 16640
1790 %5 = OpTypeFunction %2
1791 %workgroup = OpConstant %3 2
1792 %ptr = OpTypePointer Workgroup %3
1793 %var = OpVariable %ptr Workgroup
1794 %1 = OpFunction %2 None %5
1795 %7 = OpLabel
1796 %ld = OpAtomicLoad %3 %var %workgroup %semantics
1797 OpReturn
1798 OpFunctionEnd
1799 )";
1800
1801 CompileSuccessfully(text, SPV_ENV_UNIVERSAL_1_3);
1802 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1803 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1804 EXPECT_THAT(
1805 getDiagnosticString(),
1806 HasSubstr("AtomicLoad: MakeVisibleKHR Memory Semantics also requires "
1807 "either Acquire or AcquireRelease Memory Semantics"));
1808 }
1809
1810 TEST_F(ValidateAtomics, MakeAvailableKHRRequiresStorageSemantics) {
1811 const std::string text = R"(
1812 OpCapability Shader
1813 OpCapability VulkanMemoryModelKHR
1814 OpExtension "SPV_KHR_vulkan_memory_model"
1815 OpMemoryModel Logical VulkanKHR
1816 OpEntryPoint Fragment %1 "func"
1817 OpExecutionMode %1 OriginUpperLeft
1818 %2 = OpTypeVoid
1819 %3 = OpTypeInt 32 0
1820 %semantics = OpConstant %3 8196
1821 %5 = OpTypeFunction %2
1822 %workgroup = OpConstant %3 2
1823 %ptr = OpTypePointer Workgroup %3
1824 %var = OpVariable %ptr Workgroup
1825 %1 = OpFunction %2 None %5
1826 %7 = OpLabel
1827 OpAtomicStore %var %workgroup %semantics %workgroup
1828 OpReturn
1829 OpFunctionEnd
1830 )";
1831
1832 CompileSuccessfully(text, SPV_ENV_UNIVERSAL_1_3);
1833 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1834 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1835 EXPECT_THAT(
1836 getDiagnosticString(),
1837 HasSubstr(
1838 "AtomicStore: expected Memory Semantics to include a storage class"));
1839 }
1840
1841 TEST_F(ValidateAtomics, MakeVisibleKHRRequiresStorageSemantics) {
1842 const std::string text = R"(
1843 OpCapability Shader
1844 OpCapability VulkanMemoryModelKHR
1845 OpExtension "SPV_KHR_vulkan_memory_model"
1846 OpMemoryModel Logical VulkanKHR
1847 OpEntryPoint Fragment %1 "func"
1848 OpExecutionMode %1 OriginUpperLeft
1849 %2 = OpTypeVoid
1850 %3 = OpTypeInt 32 0
1851 %semantics = OpConstant %3 16386
1852 %5 = OpTypeFunction %2
1853 %workgroup = OpConstant %3 2
1854 %ptr = OpTypePointer Workgroup %3
1855 %var = OpVariable %ptr Workgroup
1856 %1 = OpFunction %2 None %5
1857 %7 = OpLabel
1858 %ld = OpAtomicLoad %3 %var %workgroup %semantics
1859 OpReturn
1860 OpFunctionEnd
1861 )";
1862
1863 CompileSuccessfully(text, SPV_ENV_UNIVERSAL_1_3);
1864 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
1865 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
1866 EXPECT_THAT(
1867 getDiagnosticString(),
1868 HasSubstr(
1869 "AtomicLoad: expected Memory Semantics to include a storage class"));
1870 }
1871
1872 TEST_F(ValidateAtomics, VulkanMemoryModelAllowsQueueFamilyKHR) {
1873 const std::string body = R"(
1874 %val = OpAtomicAnd %u32 %u32_var %queuefamily %relaxed %u32_1
1875 )";
1876
1877 const std::string extra = R"(
1878 OpCapability VulkanMemoryModelKHR
1879 OpExtension "SPV_KHR_vulkan_memory_model"
1880 )";
1881
1882 CompileSuccessfully(GenerateShaderCode(body, extra, "VulkanKHR"),
1883 SPV_ENV_VULKAN_1_1);
1884 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_VULKAN_1_1));
1885 }
1886
1887 TEST_F(ValidateAtomics, NonVulkanMemoryModelDisallowsQueueFamilyKHR) {
1888 const std::string body = R"(
1889 %val = OpAtomicAnd %u32 %u32_var %queuefamily %relaxed %u32_1
1890 )";
1891
1892 CompileSuccessfully(GenerateShaderCode(body), SPV_ENV_VULKAN_1_1);
1893 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_VULKAN_1_1));
1894 EXPECT_THAT(getDiagnosticString(),
1895 HasSubstr("AtomicAnd: Memory Scope QueueFamilyKHR requires "
1896 "capability VulkanMemoryModelKHR\n %42 = OpAtomicAnd "
1897 "%uint %29 %uint_5 %uint_0_1 %uint_1\n"));
1898 }
1899
1900 TEST_F(ValidateAtomics, SemanticsSpecConstantShader) {
1901 const std::string spirv = R"(
1902 OpCapability Shader
1903 OpMemoryModel Logical GLSL450
1904 OpEntryPoint Fragment %func "func"
1905 OpExecutionMode %func OriginUpperLeft
1906 %void = OpTypeVoid
1907 %int = OpTypeInt 32 0
1908 %spec_const = OpSpecConstant %int 0
1909 %workgroup = OpConstant %int 2
1910 %ptr_int_workgroup = OpTypePointer Workgroup %int
1911 %var = OpVariable %ptr_int_workgroup Workgroup
1912 %voidfn = OpTypeFunction %void
1913 %func = OpFunction %void None %voidfn
1914 %entry = OpLabel
1915 %ld = OpAtomicLoad %int %var %workgroup %spec_const
1916 OpReturn
1917 OpFunctionEnd
1918 )";
1919
1920 CompileSuccessfully(spirv);
1921 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1922 EXPECT_THAT(getDiagnosticString(),
1923 HasSubstr("Memory Semantics ids must be OpConstant when Shader "
1924 "capability is present"));
1925 }
1926
1927 TEST_F(ValidateAtomics, SemanticsSpecConstantKernel) {
1928 const std::string spirv = R"(
1929 OpCapability Kernel
1930 OpCapability Linkage
1931 OpMemoryModel Logical OpenCL
1932 %void = OpTypeVoid
1933 %int = OpTypeInt 32 0
1934 %spec_const = OpSpecConstant %int 0
1935 %workgroup = OpConstant %int 2
1936 %ptr_int_workgroup = OpTypePointer Workgroup %int
1937 %var = OpVariable %ptr_int_workgroup Workgroup
1938 %voidfn = OpTypeFunction %void
1939 %func = OpFunction %void None %voidfn
1940 %entry = OpLabel
1941 %ld = OpAtomicLoad %int %var %workgroup %spec_const
1942 OpReturn
1943 OpFunctionEnd
1944 )";
1945
1946 CompileSuccessfully(spirv);
1947 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1948 }
1949
1950 TEST_F(ValidateAtomics, ScopeSpecConstantShader) {
1951 const std::string spirv = R"(
1952 OpCapability Shader
1953 OpMemoryModel Logical GLSL450
1954 OpEntryPoint Fragment %func "func"
1955 OpExecutionMode %func OriginUpperLeft
1956 %void = OpTypeVoid
1957 %int = OpTypeInt 32 0
1958 %spec_const = OpSpecConstant %int 0
1959 %relaxed = OpConstant %int 0
1960 %ptr_int_workgroup = OpTypePointer Workgroup %int
1961 %var = OpVariable %ptr_int_workgroup Workgroup
1962 %voidfn = OpTypeFunction %void
1963 %func = OpFunction %void None %voidfn
1964 %entry = OpLabel
1965 %ld = OpAtomicLoad %int %var %spec_const %relaxed
1966 OpReturn
1967 OpFunctionEnd
1968 )";
1969
1970 CompileSuccessfully(spirv);
1971 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
1972 EXPECT_THAT(
1973 getDiagnosticString(),
1974 HasSubstr(
1975 "Scope ids must be OpConstant when Shader capability is present"));
1976 }
1977
1978 TEST_F(ValidateAtomics, ScopeSpecConstantKernel) {
1979 const std::string spirv = R"(
1980 OpCapability Kernel
1981 OpCapability Linkage
1982 OpMemoryModel Logical OpenCL
1983 %void = OpTypeVoid
1984 %int = OpTypeInt 32 0
1985 %spec_const = OpSpecConstant %int 0
1986 %relaxed = OpConstant %int 0
1987 %ptr_int_workgroup = OpTypePointer Workgroup %int
1988 %var = OpVariable %ptr_int_workgroup Workgroup
1989 %voidfn = OpTypeFunction %void
1990 %func = OpFunction %void None %voidfn
1991 %entry = OpLabel
1992 %ld = OpAtomicLoad %int %var %spec_const %relaxed
1993 OpReturn
1994 OpFunctionEnd
1995 )";
1996
1997 CompileSuccessfully(spirv);
1998 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1999 }
2000
2001 TEST_F(ValidateAtomics, VulkanMemoryModelDeviceScopeBad) {
2002 const std::string body = R"(
2003 %val = OpAtomicAnd %u32 %u32_var %device %relaxed %u32_1
2004 )";
2005
2006 const std::string extra = R"(OpCapability VulkanMemoryModelKHR
2007 OpExtension "SPV_KHR_vulkan_memory_model"
2008 )";
2009
2010 CompileSuccessfully(GenerateShaderCode(body, extra, "VulkanKHR"),
2011 SPV_ENV_UNIVERSAL_1_3);
2012 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
2013 ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
2014 EXPECT_THAT(
2015 getDiagnosticString(),
2016 HasSubstr("Use of device scope with VulkanKHR memory model requires the "
2017 "VulkanMemoryModelDeviceScopeKHR capability"));
2018 }
2019
2020 TEST_F(ValidateAtomics, VulkanMemoryModelDeviceScopeGood) {
2021 const std::string body = R"(
2022 %val = OpAtomicAnd %u32 %u32_var %device %relaxed %u32_1
2023 )";
2024
2025 const std::string extra = R"(OpCapability VulkanMemoryModelKHR
2026 OpCapability VulkanMemoryModelDeviceScopeKHR
2027 OpExtension "SPV_KHR_vulkan_memory_model"
2028 )";
2029
2030 CompileSuccessfully(GenerateShaderCode(body, extra, "VulkanKHR"),
2031 SPV_ENV_UNIVERSAL_1_3);
2032 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
2033 }
2034
2035 TEST_F(ValidateAtomics, WebGPUCrossDeviceMemoryScopeBad) {
2036 const std::string body = R"(
2037 %val1 = OpAtomicLoad %u32 %u32_var %cross_device %relaxed
2038 )";
2039
2040 CompileSuccessfully(GenerateWebGPUShaderCode(body), SPV_ENV_WEBGPU_0);
2041 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_WEBGPU_0));
2042 EXPECT_THAT(getDiagnosticString(),
2043 HasSubstr("in WebGPU environment Memory Scope is limited to "
2044 "QueueFamilyKHR for OpAtomic* operations"));
2045 }
2046
2047 TEST_F(ValidateAtomics, WebGPUDeviceMemoryScopeBad) {
2048 const std::string body = R"(
2049 %val1 = OpAtomicLoad %u32 %u32_var %device %relaxed
2050 )";
2051
2052 CompileSuccessfully(GenerateWebGPUShaderCode(body), SPV_ENV_WEBGPU_0);
2053 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_WEBGPU_0));
2054 EXPECT_THAT(getDiagnosticString(),
2055 HasSubstr("in WebGPU environment Memory Scope is limited to "
2056 "QueueFamilyKHR for OpAtomic* operations"));
2057 }
2058
2059 TEST_F(ValidateAtomics, WebGPUWorkgroupMemoryScopeBad) {
2060 const std::string body = R"(
2061 %val1 = OpAtomicLoad %u32 %u32_var %workgroup %relaxed
2062 )";
2063
2064 CompileSuccessfully(GenerateWebGPUShaderCode(body), SPV_ENV_WEBGPU_0);
2065 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_WEBGPU_0));
2066 EXPECT_THAT(getDiagnosticString(),
2067 HasSubstr("in WebGPU environment Memory Scope is limited to "
2068 "QueueFamilyKHR for OpAtomic* operations"));
2069 }
2070
2071 TEST_F(ValidateAtomics, WebGPUSubgroupMemoryScopeBad) {
2072 const std::string body = R"(
2073 %val1 = OpAtomicLoad %u32 %u32_var %subgroup %relaxed
2074 )";
2075
2076 CompileSuccessfully(GenerateWebGPUShaderCode(body), SPV_ENV_WEBGPU_0);
2077 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_WEBGPU_0));
2078 EXPECT_THAT(getDiagnosticString(),
2079 HasSubstr("in WebGPU environment Memory Scope is limited to "
2080 "QueueFamilyKHR for OpAtomic* operations"));
2081 }
2082
2083 TEST_F(ValidateAtomics, WebGPUInvocationMemoryScopeBad) {
2084 const std::string body = R"(
2085 %val1 = OpAtomicLoad %u32 %u32_var %invocation %relaxed
2086 )";
2087
2088 CompileSuccessfully(GenerateWebGPUShaderCode(body), SPV_ENV_WEBGPU_0);
2089 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_WEBGPU_0));
2090 EXPECT_THAT(getDiagnosticString(),
2091 HasSubstr("in WebGPU environment Memory Scope is limited to "
2092 "QueueFamilyKHR for OpAtomic* operations"));
2093 }
2094
2095 TEST_F(ValidateAtomics, WebGPUQueueFamilyMemoryScopeGood) {
2096 const std::string body = R"(
2097 %val1 = OpAtomicLoad %u32 %u32_var %queuefamily %relaxed
2098 )";
2099
2100 CompileSuccessfully(GenerateWebGPUShaderCode(body), SPV_ENV_WEBGPU_0);
2101 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
2102 }
2103
2104 TEST_F(ValidateAtomics, CompareExchangeWeakV13ValV14Good) {
2105 const std::string body = R"(
2106 %val1 = OpAtomicCompareExchangeWeak %u32 %u32_var %device %relaxed %relaxed %u32_0 %u32_0
2107 )";
2108
2109 CompileSuccessfully(GenerateKernelCode(body), SPV_ENV_UNIVERSAL_1_3);
2110 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
2111 }
2112
2113 TEST_F(ValidateAtomics, CompareExchangeWeakV14Bad) {
2114 const std::string body = R"(
2115 %val1 = OpAtomicCompareExchangeWeak %u32 %u32_var %device %relaxed %relaxed %u32_0 %u32_0
2116 )";
2117
2118 CompileSuccessfully(GenerateKernelCode(body), SPV_ENV_UNIVERSAL_1_4);
2119 EXPECT_EQ(SPV_ERROR_WRONG_VERSION,
2120 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
2121 EXPECT_THAT(
2122 getDiagnosticString(),
2123 HasSubstr(
2124 "AtomicCompareExchangeWeak requires SPIR-V version 1.3 or earlier"));
2125 }
2126
2127 TEST_F(ValidateAtomics, CompareExchangeVolatileMatch) {
2128 const std::string spirv = R"(
2129 OpCapability Shader
2130 OpCapability VulkanMemoryModelKHR
2131 OpCapability Linkage
2132 OpExtension "SPV_KHR_vulkan_memory_model"
2133 OpMemoryModel Logical VulkanKHR
2134 %void = OpTypeVoid
2135 %int = OpTypeInt 32 0
2136 %int_0 = OpConstant %int 0
2137 %int_1 = OpConstant %int 1
2138 %workgroup = OpConstant %int 2
2139 %volatile = OpConstant %int 32768
2140 %ptr_wg_int = OpTypePointer Workgroup %int
2141 %wg_var = OpVariable %ptr_wg_int Workgroup
2142 %void_fn = OpTypeFunction %void
2143 %func = OpFunction %void None %void_fn
2144 %entry = OpLabel
2145 %cmp_ex = OpAtomicCompareExchange %int %wg_var %workgroup %volatile %volatile %int_0 %int_1
2146 OpReturn
2147 OpFunctionEnd
2148 )";
2149
2150 CompileSuccessfully(spirv);
2151 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2152 }
2153
2154 TEST_F(ValidateAtomics, CompareExchangeVolatileMismatch) {
2155 const std::string spirv = R"(
2156 OpCapability Shader
2157 OpCapability VulkanMemoryModelKHR
2158 OpCapability Linkage
2159 OpExtension "SPV_KHR_vulkan_memory_model"
2160 OpMemoryModel Logical VulkanKHR
2161 %void = OpTypeVoid
2162 %int = OpTypeInt 32 0
2163 %int_0 = OpConstant %int 0
2164 %int_1 = OpConstant %int 1
2165 %workgroup = OpConstant %int 2
2166 %volatile = OpConstant %int 32768
2167 %non_volatile = OpConstant %int 0
2168 %ptr_wg_int = OpTypePointer Workgroup %int
2169 %wg_var = OpVariable %ptr_wg_int Workgroup
2170 %void_fn = OpTypeFunction %void
2171 %func = OpFunction %void None %void_fn
2172 %entry = OpLabel
2173 %cmp_ex = OpAtomicCompareExchange %int %wg_var %workgroup %non_volatile %volatile %int_0 %int_1
2174 OpReturn
2175 OpFunctionEnd
2176 )";
2177
2178 CompileSuccessfully(spirv);
2179 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2180 EXPECT_THAT(getDiagnosticString(),
2181 HasSubstr("Volatile mask setting must match for Equal and "
2182 "Unequal memory semantics"));
2183 }
2184
2185 TEST_F(ValidateAtomics, CompareExchangeVolatileMismatchCooperativeMatrix) {
2186 const std::string spirv = R"(
2187 OpCapability Shader
2188 OpCapability VulkanMemoryModelKHR
2189 OpCapability Linkage
2190 OpCapability CooperativeMatrixNV
2191 OpExtension "SPV_KHR_vulkan_memory_model"
2192 OpExtension "SPV_NV_cooperative_matrix"
2193 OpMemoryModel Logical VulkanKHR
2194 %void = OpTypeVoid
2195 %int = OpTypeInt 32 0
2196 %int_0 = OpConstant %int 0
2197 %int_1 = OpConstant %int 1
2198 %workgroup = OpConstant %int 2
2199 %volatile = OpSpecConstant %int 32768
2200 %non_volatile = OpSpecConstant %int 32768
2201 %ptr_wg_int = OpTypePointer Workgroup %int
2202 %wg_var = OpVariable %ptr_wg_int Workgroup
2203 %void_fn = OpTypeFunction %void
2204 %func = OpFunction %void None %void_fn
2205 %entry = OpLabel
2206 %cmp_ex = OpAtomicCompareExchange %int %wg_var %workgroup %volatile %non_volatile %int_0 %int_1
2207 OpReturn
2208 OpFunctionEnd
2209 )";
2210
2211 // This is ok because we cannot evaluate the spec constant defaults.
2212 CompileSuccessfully(spirv);
2213 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2214 }
2215
2216 TEST_F(ValidateAtomics, VolatileRequiresVulkanMemoryModel) {
2217 const std::string spirv = R"(
2218 OpCapability Shader
2219 OpCapability Linkage
2220 OpMemoryModel Logical GLSL450
2221 %void = OpTypeVoid
2222 %int = OpTypeInt 32 0
2223 %int_0 = OpConstant %int 0
2224 %int_1 = OpConstant %int 1
2225 %workgroup = OpConstant %int 2
2226 %volatile = OpConstant %int 32768
2227 %ptr_wg_int = OpTypePointer Workgroup %int
2228 %wg_var = OpVariable %ptr_wg_int Workgroup
2229 %void_fn = OpTypeFunction %void
2230 %func = OpFunction %void None %void_fn
2231 %entry = OpLabel
2232 %ld = OpAtomicLoad %int %wg_var %workgroup %volatile
2233 OpReturn
2234 OpFunctionEnd
2235 )";
2236
2237 CompileSuccessfully(spirv);
2238 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
2239 EXPECT_THAT(getDiagnosticString(),
2240 HasSubstr("Memory Semantics Volatile requires capability "
2241 "VulkanMemoryModelKHR"));
2242 }
2243
2244 TEST_F(ValidateAtomics, CooperativeMatrixSemanticsMustBeConstant) {
2245 const std::string spirv = R"(
2246 OpCapability Shader
2247 OpCapability Linkage
2248 OpCapability CooperativeMatrixNV
2249 OpExtension "SPV_NV_cooperative_matrix"
2250 OpMemoryModel Logical GLSL450
2251 %void = OpTypeVoid
2252 %int = OpTypeInt 32 0
2253 %int_0 = OpConstant %int 0
2254 %int_1 = OpConstant %int 1
2255 %workgroup = OpConstant %int 2
2256 %undef = OpUndef %int
2257 %ptr_wg_int = OpTypePointer Workgroup %int
2258 %wg_var = OpVariable %ptr_wg_int Workgroup
2259 %void_fn = OpTypeFunction %void
2260 %func = OpFunction %void None %void_fn
2261 %entry = OpLabel
2262 %ld = OpAtomicLoad %int %wg_var %workgroup %undef
2263 OpReturn
2264 OpFunctionEnd
2265 )";
2266
2267 CompileSuccessfully(spirv);
2268 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
2269 EXPECT_THAT(getDiagnosticString(),
2270 HasSubstr("Memory Semantics must be a constant instruction when "
2271 "CooperativeMatrixNV capability is present"));
2272 }
2273
2274 } // namespace
2275 } // namespace val
2276 } // namespace spvtools