Debian Codegraph @

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

raw
1 // Copyright (c) 2018 Google Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Validation tests for WebGPU env specific checks
16
17 #include <string>
18
19 #include "gmock/gmock.h"
20 #include "test/val/val_fixtures.h"
21
22 namespace spvtools {
23 namespace val {
24 namespace {
25
26 using testing::HasSubstr;
27
28 using ValidateWebGPU = spvtest::ValidateBase<bool>;
29
30 TEST_F(ValidateWebGPU, OpUndefIsDisallowed) {
31 std::string spirv = R"(
32 OpCapability Shader
33 OpCapability VulkanMemoryModelKHR
34 OpExtension "SPV_KHR_vulkan_memory_model"
35 OpMemoryModel Logical VulkanKHR
36 OpEntryPoint Vertex %func "shader"
37 %float = OpTypeFloat 32
38 %1 = OpUndef %float
39 %void = OpTypeVoid
40 %void_f = OpTypeFunction %void
41 %func = OpFunction %void None %void_f
42 %label = OpLabel
43 OpReturn
44 OpFunctionEnd
45 )";
46
47 CompileSuccessfully(spirv);
48
49 // Control case: OpUndef is allowed in SPIR-V 1.3
50 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
51
52 // Control case: OpUndef is disallowed in the WebGPU env
53 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_WEBGPU_0));
54 EXPECT_THAT(getDiagnosticString(), HasSubstr("OpUndef is disallowed"));
55 }
56
57 TEST_F(ValidateWebGPU, OpNameIsAllowed) {
58 std::string spirv = R"(
59 OpCapability Shader
60 OpCapability VulkanMemoryModelKHR
61 OpExtension "SPV_KHR_vulkan_memory_model"
62 OpMemoryModel Logical VulkanKHR
63 OpEntryPoint Vertex %func "shader"
64 OpName %1 "foo"
65 %1 = OpTypeFloat 32
66 %void = OpTypeVoid
67 %void_f = OpTypeFunction %void
68 %func = OpFunction %void None %void_f
69 %label = OpLabel
70 OpReturn
71 OpFunctionEnd
72 )";
73
74 CompileSuccessfully(spirv);
75 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
76 }
77
78 TEST_F(ValidateWebGPU, OpMemberNameIsAllowed) {
79 std::string spirv = R"(
80 OpCapability Shader
81 OpCapability VulkanMemoryModelKHR
82 OpExtension "SPV_KHR_vulkan_memory_model"
83 OpMemoryModel Logical VulkanKHR
84 OpEntryPoint Vertex %func "shader"
85 OpMemberName %2 0 "foo"
86 %1 = OpTypeFloat 32
87 %2 = OpTypeStruct %1
88 %void = OpTypeVoid
89 %void_f = OpTypeFunction %void
90 %func = OpFunction %void None %void_f
91 %label = OpLabel
92 OpReturn
93 OpFunctionEnd
94
95 )";
96
97 CompileSuccessfully(spirv);
98 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
99 }
100
101 TEST_F(ValidateWebGPU, OpSourceIsAllowed) {
102 std::string spirv = R"(
103 OpCapability Shader
104 OpCapability VulkanMemoryModelKHR
105 OpExtension "SPV_KHR_vulkan_memory_model"
106 OpMemoryModel Logical VulkanKHR
107 OpEntryPoint Vertex %func "shader"
108 OpSource GLSL 450
109 %void = OpTypeVoid
110 %void_f = OpTypeFunction %void
111 %func = OpFunction %void None %void_f
112 %label = OpLabel
113 OpReturn
114 OpFunctionEnd
115 )";
116
117 CompileSuccessfully(spirv);
118 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
119 }
120
121 TEST_F(ValidateWebGPU, OpSourceContinuedIsAllowed) {
122 std::string spirv = R"(
123 OpCapability Shader
124 OpCapability VulkanMemoryModelKHR
125 OpExtension "SPV_KHR_vulkan_memory_model"
126 OpMemoryModel Logical VulkanKHR
127 OpEntryPoint Vertex %func "shader"
128 OpSource GLSL 450
129 OpSourceContinued "I am a happy shader! Yay! ;"
130 %void = OpTypeVoid
131 %void_f = OpTypeFunction %void
132 %func = OpFunction %void None %void_f
133 %label = OpLabel
134 OpReturn
135 OpFunctionEnd
136 )";
137
138 CompileSuccessfully(spirv);
139 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
140 }
141
142 TEST_F(ValidateWebGPU, OpSourceExtensionIsAllowed) {
143 std::string spirv = R"(
144 OpCapability Shader
145 OpCapability VulkanMemoryModelKHR
146 OpExtension "SPV_KHR_vulkan_memory_model"
147 OpMemoryModel Logical VulkanKHR
148 OpEntryPoint Vertex %func "shader"
149 OpSourceExtension "bar"
150 %void = OpTypeVoid
151 %void_f = OpTypeFunction %void
152 %func = OpFunction %void None %void_f
153 %label = OpLabel
154 OpReturn
155 OpFunctionEnd
156 )";
157
158 CompileSuccessfully(spirv);
159 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
160 }
161
162 TEST_F(ValidateWebGPU, OpStringIsAllowed) {
163 std::string spirv = R"(
164 OpCapability Shader
165 OpCapability VulkanMemoryModelKHR
166 OpExtension "SPV_KHR_vulkan_memory_model"
167 OpMemoryModel Logical VulkanKHR
168 OpEntryPoint Vertex %func "shader"
169 %1 = OpString "foo"
170 %void = OpTypeVoid
171 %void_f = OpTypeFunction %void
172 %func = OpFunction %void None %void_f
173 %label = OpLabel
174 OpReturn
175 OpFunctionEnd
176 )";
177
178 CompileSuccessfully(spirv);
179 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
180 }
181
182 TEST_F(ValidateWebGPU, OpLineIsAllowed) {
183 std::string spirv = R"(
184 OpCapability Shader
185 OpCapability VulkanMemoryModelKHR
186 OpExtension "SPV_KHR_vulkan_memory_model"
187 OpMemoryModel Logical VulkanKHR
188 OpEntryPoint Vertex %func "shader"
189 %1 = OpString "minimal.vert"
190 OpLine %1 1 1
191 %void = OpTypeVoid
192 %void_f = OpTypeFunction %void
193 %func = OpFunction %void None %void_f
194 %label = OpLabel
195 OpReturn
196 OpFunctionEnd
197 )";
198
199 CompileSuccessfully(spirv);
200 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
201 }
202
203 TEST_F(ValidateWebGPU, OpNoLineIsAllowed) {
204 std::string spirv = R"(
205 OpCapability Shader
206 OpCapability VulkanMemoryModelKHR
207 OpExtension "SPV_KHR_vulkan_memory_model"
208 OpMemoryModel Logical VulkanKHR
209 OpEntryPoint Vertex %func "shader"
210 OpNoLine
211 %void = OpTypeVoid
212 %void_f = OpTypeFunction %void
213 %func = OpFunction %void None %void_f
214 %label = OpLabel
215 OpReturn
216 OpFunctionEnd
217 )";
218
219 CompileSuccessfully(spirv);
220 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
221 }
222
223 TEST_F(ValidateWebGPU, LogicalAddressingVulkanKHRMemoryGood) {
224 std::string spirv = R"(
225 OpCapability Shader
226 OpCapability VulkanMemoryModelKHR
227 OpExtension "SPV_KHR_vulkan_memory_model"
228 OpMemoryModel Logical VulkanKHR
229 OpEntryPoint Vertex %func "shader"
230 %void = OpTypeVoid
231 %void_f = OpTypeFunction %void
232 %func = OpFunction %void None %void_f
233 %label = OpLabel
234 OpReturn
235 OpFunctionEnd
236 )";
237
238 CompileSuccessfully(spirv);
239 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
240 }
241
242 TEST_F(ValidateWebGPU, LogicalAddressingGLSL450MemoryGood) {
243 std::string spirv = R"(
244 OpCapability Shader
245 OpMemoryModel Logical GLSL450
246 OpEntryPoint Vertex %func "shader"
247 %void = OpTypeVoid
248 %void_f = OpTypeFunction %void
249 %func = OpFunction %void None %void_f
250 %label = OpLabel
251 OpReturn
252 OpFunctionEnd
253 )";
254
255 CompileSuccessfully(spirv);
256 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
257 }
258
259 TEST_F(ValidateWebGPU, LogicalAddressingSimpleMemoryGood) {
260 std::string spirv = R"(
261 OpCapability Shader
262 OpMemoryModel Logical Simple
263 OpEntryPoint Vertex %func "shader"
264 %void = OpTypeVoid
265 %void_f = OpTypeFunction %void
266 %func = OpFunction %void None %void_f
267 %label = OpLabel
268 OpReturn
269 OpFunctionEnd
270 )";
271
272 CompileSuccessfully(spirv);
273 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
274 }
275
276 TEST_F(ValidateWebGPU, KernelIsBad) {
277 std::string spirv = R"(
278 OpCapability Kernel
279 OpMemoryModel Logical Simple
280 OpNoLine
281 )";
282
283 CompileSuccessfully(spirv);
284
285 EXPECT_EQ(SPV_ERROR_INVALID_CAPABILITY,
286 ValidateInstructions(SPV_ENV_WEBGPU_0));
287 EXPECT_THAT(getDiagnosticString(),
288 HasSubstr("Capability Kernel is not allowed by WebGPU "
289 "specification (or requires extension)"));
290 }
291
292 TEST_F(ValidateWebGPU, OpenCLMemoryModelBad) {
293 std::string spirv = R"(
294 OpCapability Shader
295 OpMemoryModel Logical OpenCL
296 OpNoLine
297 )";
298
299 CompileSuccessfully(spirv);
300
301 EXPECT_EQ(SPV_ERROR_INVALID_CAPABILITY,
302 ValidateInstructions(SPV_ENV_WEBGPU_0));
303 EXPECT_THAT(getDiagnosticString(),
304 HasSubstr("Operand 2 of MemoryModel requires one of these "
305 "capabilities: Kernel"));
306 }
307
308 TEST_F(ValidateWebGPU, AllowListedExtendedInstructionsImportGood) {
309 std::string spirv = R"(
310 OpCapability Shader
311 OpCapability VulkanMemoryModelKHR
312 OpExtension "SPV_KHR_vulkan_memory_model"
313 %1 = OpExtInstImport "GLSL.std.450"
314 OpMemoryModel Logical VulkanKHR
315 OpEntryPoint Vertex %func "shader"
316 %void = OpTypeVoid
317 %void_f = OpTypeFunction %void
318 %func = OpFunction %void None %void_f
319 %label = OpLabel
320 OpReturn
321 OpFunctionEnd
322 )";
323
324 CompileSuccessfully(spirv);
325
326 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
327 }
328
329 TEST_F(ValidateWebGPU, NonAllowListedExtendedInstructionsImportBad) {
330 std::string spirv = R"(
331 OpCapability Shader
332 OpCapability VulkanMemoryModelKHR
333 OpExtension "SPV_KHR_vulkan_memory_model"
334 %1 = OpExtInstImport "OpenCL.std"
335 OpMemoryModel Logical VulkanKHR
336 )";
337
338 CompileSuccessfully(spirv);
339
340 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_WEBGPU_0));
341 EXPECT_THAT(getDiagnosticString(),
342 HasSubstr("For WebGPU, the only valid parameter to "
343 "OpExtInstImport is \"GLSL.std.450\".\n %1 = "
344 "OpExtInstImport \"OpenCL.std\"\n"));
345 }
346
347 TEST_F(ValidateWebGPU, NonVulkanKHRMemoryModelExtensionBad) {
348 std::string spirv = R"(
349 OpCapability Shader
350 OpCapability VulkanMemoryModelKHR
351 OpExtension "SPV_KHR_8bit_storage"
352 OpExtension "SPV_KHR_vulkan_memory_model"
353 OpMemoryModel Logical VulkanKHR
354 )";
355
356 CompileSuccessfully(spirv);
357
358 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions(SPV_ENV_WEBGPU_0));
359 EXPECT_THAT(getDiagnosticString(),
360 HasSubstr("For WebGPU, the only valid parameter to OpExtension "
361 "is \"SPV_KHR_vulkan_memory_model\".\n OpExtension "
362 "\"SPV_KHR_8bit_storage\"\n"));
363 }
364
365 spv_binary GenerateTrivialBinary(bool need_little_endian) {
366 // Smallest possible valid WebGPU SPIR-V binary in little endian. Contains all
367 // the required boilerplate and a trivial entry point function.
368 static const uint8_t binary_bytes[] = {
369 // clang-format off
370 0x03, 0x02, 0x23, 0x07, 0x00, 0x03, 0x01, 0x00, 0x00, 0x00, 0x07, 0x00,
371 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00,
372 0x01, 0x00, 0x00, 0x00, 0x11, 0x00, 0x02, 0x00, 0xE1, 0x14, 0x00, 0x00,
373 0x0A, 0x00, 0x08, 0x00, 0x53, 0x50, 0x56, 0x5F, 0x4B, 0x48, 0x52, 0x5F,
374 0x76, 0x75, 0x6C, 0x6B, 0x61, 0x6E, 0x5F, 0x6D, 0x65, 0x6D, 0x6F, 0x72,
375 0x79, 0x5F, 0x6D, 0x6F, 0x64, 0x65, 0x6C, 0x00, 0x0E, 0x00, 0x03, 0x00,
376 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x05, 0x00,
377 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x73, 0x68, 0x61, 0x64,
378 0x65, 0x72, 0x00, 0x00, 0x13, 0x00, 0x02, 0x00, 0x02, 0x00, 0x00, 0x00,
379 0x21, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
380 0x36, 0x00, 0x05, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
381 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xF8, 0x00, 0x02, 0x00,
382 0x04, 0x00, 0x00, 0x00, 0xFD, 0x00, 0x01, 0x00, 0x38, 0x00, 0x01, 0x00
383 // clang-format on
384 };
385 static const size_t word_count = sizeof(binary_bytes) / sizeof(uint32_t);
386 std::unique_ptr<spv_binary_t> result(new spv_binary_t);
387 if (!result) return nullptr;
388
389 result->wordCount = word_count;
390 result->code = new uint32_t[word_count];
391 if (!result->code) return nullptr;
392
393 if (need_little_endian) {
394 memcpy(result->code, binary_bytes, sizeof(binary_bytes));
395 } else {
396 uint8_t* code_bytes = reinterpret_cast<uint8_t*>(result->code);
397 for (size_t word = 0; word < word_count; ++word) {
398 code_bytes[4 * word] = binary_bytes[4 * word + 3];
399 code_bytes[4 * word + 1] = binary_bytes[4 * word + 2];
400 code_bytes[4 * word + 2] = binary_bytes[4 * word + 1];
401 code_bytes[4 * word + 3] = binary_bytes[4 * word];
402 }
403 }
404
405 return result.release();
406 }
407
408 TEST_F(ValidateWebGPU, LittleEndianGood) {
409 DestroyBinary();
410 binary_ = GenerateTrivialBinary(true);
411 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
412 }
413
414 TEST_F(ValidateWebGPU, BigEndianBad) {
415 DestroyBinary();
416 binary_ = GenerateTrivialBinary(false);
417 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions(SPV_ENV_WEBGPU_0));
418 EXPECT_THAT(getDiagnosticString(),
419 HasSubstr("WebGPU requires SPIR-V to be little endian."));
420 }
421
422 } // namespace
423 } // namespace val
424 } // namespace spvtools