Debian Codegraph @

test/val/val_extension_spv_nv_raw_access_chains.cpp

raw
1 // Copyright (c) 2024 The Khronos Group 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 <string>
16
17 #include "gmock/gmock.h"
18 #include "source/spirv_target_env.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
28 using ValidateSpvNVRawAccessChains = spvtest::ValidateBase<bool>;
29
30 TEST_F(ValidateSpvNVRawAccessChains, Valid) {
31 const std::string str = R"(
32 OpCapability Shader
33 OpCapability RawAccessChainsNV
34 OpExtension "SPV_KHR_storage_buffer_storage_class"
35 OpExtension "SPV_NV_raw_access_chains"
36 OpMemoryModel Logical GLSL450
37
38 OpEntryPoint GLCompute %main "main"
39 OpExecutionMode %main LocalSize 1 1 1
40
41 OpDecorate %intStruct Block
42 OpMemberDecorate %intStruct 0 Offset 0
43 OpDecorate %ssbo DescriptorSet 0
44 OpDecorate %ssbo Binding 0
45
46 %int = OpTypeInt 32 1
47 %void = OpTypeVoid
48 %mainFunctionType = OpTypeFunction %void
49 %intStruct = OpTypeStruct %int
50 %intStructPtr = OpTypePointer StorageBuffer %intStruct
51 %ssbo = OpVariable %intStructPtr StorageBuffer
52 %intPtr = OpTypePointer StorageBuffer %int
53
54 %int_0 = OpConstant %int 0
55 %int_16 = OpConstant %int 16
56
57 %main = OpFunction %void None %mainFunctionType
58 %label = OpLabel
59 %rawChain = OpRawAccessChainNV %intPtr %ssbo %int_16 %int_0 %int_0 RobustnessPerComponentNV
60 %unused = OpLoad %int %rawChain
61 OpReturn
62 OpFunctionEnd
63 )";
64 CompileSuccessfully(str.c_str());
65 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
66 }
67
68 TEST_F(ValidateSpvNVRawAccessChains, NoCapability) {
69 const std::string str = R"(
70 OpCapability Shader
71 OpExtension "SPV_KHR_storage_buffer_storage_class"
72 OpExtension "SPV_NV_raw_access_chains"
73 OpMemoryModel Logical GLSL450
74
75 OpEntryPoint GLCompute %main "main"
76 OpExecutionMode %main LocalSize 1 1 1
77
78 OpDecorate %intStruct Block
79 OpMemberDecorate %intStruct 0 Offset 0
80 OpDecorate %ssbo DescriptorSet 0
81 OpDecorate %ssbo Binding 0
82
83 %int = OpTypeInt 32 1
84 %void = OpTypeVoid
85 %mainFunctionType = OpTypeFunction %void
86 %intStruct = OpTypeStruct %int
87 %intStructPtr = OpTypePointer StorageBuffer %intStruct
88 %ssbo = OpVariable %intStructPtr StorageBuffer
89 %intPtr = OpTypePointer StorageBuffer %int
90
91 %int_0 = OpConstant %int 0
92 %int_16 = OpConstant %int 16
93
94 %main = OpFunction %void None %mainFunctionType
95 %label = OpLabel
96 %rawChain = OpRawAccessChainNV %intPtr %ssbo %int_16 %int_0 %int_0 RobustnessPerComponentNV
97 %unused = OpLoad %int %rawChain
98 OpReturn
99 OpFunctionEnd
100 )";
101 CompileSuccessfully(str.c_str());
102 EXPECT_EQ(SPV_ERROR_INVALID_CAPABILITY, ValidateInstructions());
103 EXPECT_THAT(
104 getDiagnosticString(),
105 HasSubstr("requires one of these capabilities: RawAccessChainsNV"));
106 }
107
108 TEST_F(ValidateSpvNVRawAccessChains, NoExtension) {
109 const std::string str = R"(
110 OpCapability Shader
111 OpCapability RawAccessChainsNV
112 OpExtension "SPV_KHR_storage_buffer_storage_class"
113 OpMemoryModel Logical GLSL450
114
115 OpEntryPoint GLCompute %main "main"
116 OpExecutionMode %main LocalSize 1 1 1
117
118 OpDecorate %intStruct Block
119 OpMemberDecorate %intStruct 0 Offset 0
120 OpDecorate %ssbo DescriptorSet 0
121 OpDecorate %ssbo Binding 0
122
123 %int = OpTypeInt 32 1
124 %void = OpTypeVoid
125 %mainFunctionType = OpTypeFunction %void
126 %intStruct = OpTypeStruct %int
127 %intStructPtr = OpTypePointer StorageBuffer %intStruct
128 %ssbo = OpVariable %intStructPtr StorageBuffer
129 %intPtr = OpTypePointer StorageBuffer %int
130
131 %int_0 = OpConstant %int 0
132 %int_16 = OpConstant %int 16
133
134 %main = OpFunction %void None %mainFunctionType
135 %label = OpLabel
136 %rawChain = OpRawAccessChainNV %intPtr %ssbo %int_16 %int_0 %int_0 RobustnessPerComponentNV
137 %unused = OpLoad %int %rawChain
138 OpReturn
139 OpFunctionEnd
140 )";
141 CompileSuccessfully(str.c_str());
142 EXPECT_EQ(SPV_ERROR_MISSING_EXTENSION, ValidateInstructions());
143 EXPECT_THAT(
144 getDiagnosticString(),
145 HasSubstr("requires one of these extensions: SPV_NV_raw_access_chains"));
146 }
147
148 TEST_F(ValidateSpvNVRawAccessChains, ReturnTypeNotPointer) {
149 const std::string str = R"(
150 OpCapability Shader
151 OpCapability RawAccessChainsNV
152 OpExtension "SPV_KHR_storage_buffer_storage_class"
153 OpExtension "SPV_NV_raw_access_chains"
154 OpMemoryModel Logical GLSL450
155
156 OpEntryPoint GLCompute %main "main"
157 OpExecutionMode %main LocalSize 1 1 1
158
159 OpDecorate %intStruct Block
160 OpMemberDecorate %intStruct 0 Offset 0
161 OpDecorate %ssbo DescriptorSet 0
162 OpDecorate %ssbo Binding 0
163
164 %int = OpTypeInt 32 1
165 %void = OpTypeVoid
166 %mainFunctionType = OpTypeFunction %void
167 %intStruct = OpTypeStruct %int
168 %intStructPtr = OpTypePointer StorageBuffer %intStruct
169 %ssbo = OpVariable %intStructPtr StorageBuffer
170
171 %int_0 = OpConstant %int 0
172 %int_16 = OpConstant %int 16
173
174 %main = OpFunction %void None %mainFunctionType
175 %label = OpLabel
176 %rawChain = OpRawAccessChainNV %int %ssbo %int_16 %int_0 %int_0 RobustnessPerComponentNV
177 OpReturn
178 OpFunctionEnd
179 )";
180 CompileSuccessfully(str.c_str());
181 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
182 EXPECT_THAT(getDiagnosticString(),
183 HasSubstr("must be OpTypePointer. Found OpTypeInt"));
184 }
185
186 TEST_F(ValidateSpvNVRawAccessChains, Workgroup) {
187 const std::string str = R"(
188 OpCapability Shader
189 OpCapability RawAccessChainsNV
190 OpExtension "SPV_KHR_storage_buffer_storage_class"
191 OpExtension "SPV_NV_raw_access_chains"
192 OpMemoryModel Logical GLSL450
193
194 OpEntryPoint GLCompute %main "main"
195 OpExecutionMode %main LocalSize 1 1 1
196
197 OpDecorate %intStruct Block
198 OpMemberDecorate %intStruct 0 Offset 0
199
200 %int = OpTypeInt 32 1
201 %void = OpTypeVoid
202 %mainFunctionType = OpTypeFunction %void
203 %intStruct = OpTypeStruct %int
204 %intStructPtr = OpTypePointer Workgroup %intStruct
205 %ssbo = OpVariable %intStructPtr Workgroup
206 %intPtr = OpTypePointer Workgroup %int
207
208 %int_0 = OpConstant %int 0
209 %int_16 = OpConstant %int 16
210
211 %main = OpFunction %void None %mainFunctionType
212 %label = OpLabel
213 %rawChain = OpRawAccessChainNV %intPtr %ssbo %int_16 %int_0 %int_0 RobustnessPerComponentNV
214 %unused = OpLoad %int %rawChain
215 OpReturn
216 OpFunctionEnd
217 )";
218 CompileSuccessfully(str.c_str());
219 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
220 EXPECT_THAT(getDiagnosticString(),
221 HasSubstr("must point to a storage class of"));
222 }
223
224 TEST_F(ValidateSpvNVRawAccessChains, ReturnTypeArray) {
225 const std::string str = R"(
226 OpCapability Shader
227 OpCapability RawAccessChainsNV
228 OpExtension "SPV_KHR_storage_buffer_storage_class"
229 OpExtension "SPV_NV_raw_access_chains"
230 OpMemoryModel Logical GLSL450
231
232 OpEntryPoint GLCompute %main "main"
233 OpExecutionMode %main LocalSize 1 1 1
234
235 OpDecorate %intStruct Block
236 OpMemberDecorate %intStruct 0 Offset 0
237 OpDecorate %ssbo DescriptorSet 0
238 OpDecorate %ssbo Binding 0
239
240 %int = OpTypeInt 32 1
241 %void = OpTypeVoid
242 %mainFunctionType = OpTypeFunction %void
243 %intStruct = OpTypeStruct %int
244 %intStructPtr = OpTypePointer StorageBuffer %intStruct
245 %ssbo = OpVariable %intStructPtr StorageBuffer
246 %int_1 = OpConstant %int 1
247 %intArray = OpTypeArray %int %int_1
248 %intArrayPtr = OpTypePointer StorageBuffer %intArray
249
250 %int_0 = OpConstant %int 0
251 %int_16 = OpConstant %int 16
252
253 %main = OpFunction %void None %mainFunctionType
254 %label = OpLabel
255 %rawChain = OpRawAccessChainNV %intArrayPtr %ssbo %int_16 %int_0 %int_0 RobustnessPerComponentNV
256 OpReturn
257 OpFunctionEnd
258 )";
259 CompileSuccessfully(str.c_str());
260 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
261 EXPECT_THAT(getDiagnosticString(), HasSubstr("must not point to"));
262 }
263
264 TEST_F(ValidateSpvNVRawAccessChains, VariableStride) {
265 const std::string str = R"(
266 OpCapability Shader
267 OpCapability RawAccessChainsNV
268 OpExtension "SPV_KHR_storage_buffer_storage_class"
269 OpExtension "SPV_NV_raw_access_chains"
270 OpMemoryModel Logical GLSL450
271
272 OpEntryPoint GLCompute %main "main"
273 OpExecutionMode %main LocalSize 1 1 1
274
275 OpDecorate %intStruct Block
276 OpMemberDecorate %intStruct 0 Offset 0
277 OpDecorate %ssbo DescriptorSet 0
278 OpDecorate %ssbo Binding 0
279
280 %int = OpTypeInt 32 1
281 %void = OpTypeVoid
282 %mainFunctionType = OpTypeFunction %void
283 %intStruct = OpTypeStruct %int
284 %intStructPtr = OpTypePointer StorageBuffer %intStruct
285 %ssbo = OpVariable %intStructPtr StorageBuffer
286 %intPtr = OpTypePointer StorageBuffer %int
287
288 %int_0 = OpConstant %int 0
289
290 %main = OpFunction %void None %mainFunctionType
291 %label = OpLabel
292 %stride = OpIAdd %int %int_0 %int_0
293 %rawChain = OpRawAccessChainNV %intPtr %ssbo %stride %int_0 %int_0 RobustnessPerComponentNV
294 %unused = OpLoad %int %rawChain
295 OpReturn
296 OpFunctionEnd
297 )";
298 CompileSuccessfully(str.c_str());
299 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
300 EXPECT_THAT(getDiagnosticString(), HasSubstr("must be OpConstant"));
301 }
302
303 TEST_F(ValidateSpvNVRawAccessChains, RobustnessPerElementZeroStride) {
304 const std::string str = R"(
305 OpCapability Shader
306 OpCapability RawAccessChainsNV
307 OpExtension "SPV_KHR_storage_buffer_storage_class"
308 OpExtension "SPV_NV_raw_access_chains"
309 OpMemoryModel Logical GLSL450
310
311 OpEntryPoint GLCompute %main "main"
312 OpExecutionMode %main LocalSize 1 1 1
313
314 OpDecorate %intStruct Block
315 OpMemberDecorate %intStruct 0 Offset 0
316 OpDecorate %ssbo DescriptorSet 0
317 OpDecorate %ssbo Binding 0
318
319 %int = OpTypeInt 32 1
320 %void = OpTypeVoid
321 %mainFunctionType = OpTypeFunction %void
322 %intStruct = OpTypeStruct %int
323 %intStructPtr = OpTypePointer StorageBuffer %intStruct
324 %ssbo = OpVariable %intStructPtr StorageBuffer
325 %intPtr = OpTypePointer StorageBuffer %int
326
327 %int_0 = OpConstant %int 0
328
329 %main = OpFunction %void None %mainFunctionType
330 %label = OpLabel
331 %rawChain = OpRawAccessChainNV %intPtr %ssbo %int_0 %int_0 %int_0 RobustnessPerElementNV
332 %unused = OpLoad %int %rawChain
333 OpReturn
334 OpFunctionEnd
335 )";
336 CompileSuccessfully(str.c_str());
337 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
338 EXPECT_THAT(
339 getDiagnosticString(),
340 HasSubstr("Stride must not be zero when per-element robustness is used"));
341 }
342
343 TEST_F(ValidateSpvNVRawAccessChains, BothRobustness) {
344 const std::string str = R"(
345 OpCapability Shader
346 OpCapability RawAccessChainsNV
347 OpExtension "SPV_KHR_storage_buffer_storage_class"
348 OpExtension "SPV_NV_raw_access_chains"
349 OpMemoryModel Logical GLSL450
350
351 OpEntryPoint GLCompute %main "main"
352 OpExecutionMode %main LocalSize 1 1 1
353
354 OpDecorate %intStruct Block
355 OpMemberDecorate %intStruct 0 Offset 0
356 OpDecorate %ssbo DescriptorSet 0
357 OpDecorate %ssbo Binding 0
358
359 %int = OpTypeInt 32 1
360 %void = OpTypeVoid
361 %mainFunctionType = OpTypeFunction %void
362 %intStruct = OpTypeStruct %int
363 %intStructPtr = OpTypePointer StorageBuffer %intStruct
364 %ssbo = OpVariable %intStructPtr StorageBuffer
365 %intPtr = OpTypePointer StorageBuffer %int
366
367 %int_0 = OpConstant %int 0
368 %int_16 = OpConstant %int 16
369
370 %main = OpFunction %void None %mainFunctionType
371 %label = OpLabel
372 %rawChain = OpRawAccessChainNV %intPtr %ssbo %int_16 %int_0 %int_0 RobustnessPerElementNV|RobustnessPerComponentNV
373 %unused = OpLoad %int %rawChain
374 OpReturn
375 OpFunctionEnd
376 )";
377 CompileSuccessfully(str.c_str());
378 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
379 EXPECT_THAT(getDiagnosticString(),
380 HasSubstr("Per-component robustness and per-element robustness "
381 "are mutually exclusive"));
382 }
383
384 TEST_F(ValidateSpvNVRawAccessChains, StrideFloat) {
385 const std::string str = R"(
386 OpCapability Shader
387 OpCapability RawAccessChainsNV
388 OpExtension "SPV_KHR_storage_buffer_storage_class"
389 OpExtension "SPV_NV_raw_access_chains"
390 OpMemoryModel Logical GLSL450
391
392 OpEntryPoint GLCompute %main "main"
393 OpExecutionMode %main LocalSize 1 1 1
394
395 OpDecorate %intStruct Block
396 OpMemberDecorate %intStruct 0 Offset 0
397 OpDecorate %ssbo DescriptorSet 0
398 OpDecorate %ssbo Binding 0
399
400 %int = OpTypeInt 32 1
401 %float = OpTypeFloat 32
402 %void = OpTypeVoid
403 %mainFunctionType = OpTypeFunction %void
404 %intStruct = OpTypeStruct %int
405 %intStructPtr = OpTypePointer StorageBuffer %intStruct
406 %ssbo = OpVariable %intStructPtr StorageBuffer
407 %intPtr = OpTypePointer StorageBuffer %int
408
409 %int_0 = OpConstant %int 0
410 %float_16 = OpConstant %float 16
411
412 %main = OpFunction %void None %mainFunctionType
413 %label = OpLabel
414 %rawChain = OpRawAccessChainNV %intPtr %ssbo %float_16 %int_0 %int_0 RobustnessPerComponentNV
415 %unused = OpLoad %int %rawChain
416 OpReturn
417 OpFunctionEnd
418 )";
419 CompileSuccessfully(str.c_str());
420 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
421 EXPECT_THAT(getDiagnosticString(), HasSubstr("must be OpTypeInt"));
422 }
423
424 TEST_F(ValidateSpvNVRawAccessChains, IndexType) {
425 const std::string str = R"(
426 OpCapability Shader
427 OpCapability RawAccessChainsNV
428 OpCapability Int64
429 OpExtension "SPV_KHR_storage_buffer_storage_class"
430 OpExtension "SPV_NV_raw_access_chains"
431 OpMemoryModel Logical GLSL450
432
433 OpEntryPoint GLCompute %main "main"
434 OpExecutionMode %main LocalSize 1 1 1
435
436 OpDecorate %intStruct Block
437 OpMemberDecorate %intStruct 0 Offset 0
438 OpDecorate %ssbo DescriptorSet 0
439 OpDecorate %ssbo Binding 0
440
441 %int = OpTypeInt 32 1
442 %long = OpTypeInt 64 1
443 %void = OpTypeVoid
444 %mainFunctionType = OpTypeFunction %void
445 %intStruct = OpTypeStruct %int
446 %intStructPtr = OpTypePointer StorageBuffer %intStruct
447 %ssbo = OpVariable %intStructPtr StorageBuffer
448 %intPtr = OpTypePointer StorageBuffer %int
449
450 %int_0 = OpConstant %int 0
451 %int_16 = OpConstant %int 16
452 %long_0 = OpConstant %long 0
453
454 %main = OpFunction %void None %mainFunctionType
455 %label = OpLabel
456 %rawChain = OpRawAccessChainNV %intPtr %ssbo %int_16 %long_0 %int_0 RobustnessPerComponentNV
457 %unused = OpLoad %int %rawChain
458 OpReturn
459 OpFunctionEnd
460 )";
461 CompileSuccessfully(str.c_str());
462 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
463 EXPECT_THAT(getDiagnosticString(), HasSubstr("The integer width of Index"));
464 }
465
466 TEST_F(ValidateSpvNVRawAccessChains, OffsetType) {
467 const std::string str = R"(
468 OpCapability Shader
469 OpCapability RawAccessChainsNV
470 OpCapability Int64
471 OpExtension "SPV_KHR_storage_buffer_storage_class"
472 OpExtension "SPV_NV_raw_access_chains"
473 OpMemoryModel Logical GLSL450
474
475 OpEntryPoint GLCompute %main "main"
476 OpExecutionMode %main LocalSize 1 1 1
477
478 OpDecorate %intStruct Block
479 OpMemberDecorate %intStruct 0 Offset 0
480 OpDecorate %ssbo DescriptorSet 0
481 OpDecorate %ssbo Binding 0
482
483 %int = OpTypeInt 32 1
484 %long = OpTypeInt 64 1
485 %void = OpTypeVoid
486 %mainFunctionType = OpTypeFunction %void
487 %intStruct = OpTypeStruct %int
488 %intStructPtr = OpTypePointer StorageBuffer %intStruct
489 %ssbo = OpVariable %intStructPtr StorageBuffer
490 %intPtr = OpTypePointer StorageBuffer %int
491
492 %int_0 = OpConstant %int 0
493 %int_16 = OpConstant %int 16
494 %long_0 = OpConstant %long 0
495
496 %main = OpFunction %void None %mainFunctionType
497 %label = OpLabel
498 %rawChain = OpRawAccessChainNV %intPtr %ssbo %int_16 %int_0 %long_0 RobustnessPerComponentNV
499 %unused = OpLoad %int %rawChain
500 OpReturn
501 OpFunctionEnd
502 )";
503 CompileSuccessfully(str.c_str());
504 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
505 EXPECT_THAT(getDiagnosticString(), HasSubstr("The integer width of Offset"));
506 }
507
508 } // namespace
509 } // namespace val
510 } // namespace spvtools