Debian Codegraph @

test/val/val_constants_test.cpp

raw
1 // Copyright (c) 2019 Google LLC
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 // Test validation of constants.
16 //
17 // This file contains newer tests. Older tests may be in other files such as
18 // val_id_test.cpp.
19
20 #include <string>
21 #include <vector>
22
23 #include "gmock/gmock.h"
24 #include "test/unit_spirv.h"
25 #include "test/val/val_code_generator.h"
26 #include "test/val/val_fixtures.h"
27
28 namespace spvtools {
29 namespace val {
30 namespace {
31
32 using ::testing::Combine;
33 using ::testing::Eq;
34 using ::testing::HasSubstr;
35 using ::testing::Values;
36 using ::testing::ValuesIn;
37
38 using ValidateConstant = spvtest::ValidateBase<bool>;
39
40 #define kBasicTypes \
41 "%bool = OpTypeBool " \
42 "%uint = OpTypeInt 32 0 " \
43 "%uint2 = OpTypeVector %uint 2 " \
44 "%float = OpTypeFloat 32 " \
45 "%_ptr_uint = OpTypePointer Workgroup %uint " \
46 "%uint_0 = OpConstantNull %uint " \
47 "%uint2_0 = OpConstantNull %uint " \
48 "%float_0 = OpConstantNull %float " \
49 "%false = OpConstantFalse %bool " \
50 "%true = OpConstantTrue %bool " \
51 "%null = OpConstantNull %_ptr_uint "
52
53 #define kShaderPreamble \
54 "OpCapability Shader\n" \
55 "OpCapability Linkage\n" \
56 "OpCapability VariablePointers\n" \
57 "OpExtension \"SPV_KHR_variable_pointers\"\n" \
58 "OpMemoryModel Logical Simple\n"
59
60 #define kKernelPreamble \
61 "OpCapability Kernel\n" \
62 "OpCapability Linkage\n" \
63 "OpCapability Addresses\n" \
64 "OpMemoryModel Physical32 OpenCL\n"
65
66 struct ConstantOpCase {
67 spv_target_env env;
68 std::string assembly;
69 bool expect_success;
70 std::string expect_err;
71 };
72
73 using ValidateConstantOp = spvtest::ValidateBase<ConstantOpCase>;
74
75 TEST_P(ValidateConstantOp, Samples) {
76 const auto env = GetParam().env;
77 CompileSuccessfully(GetParam().assembly, env);
78 const auto result = ValidateInstructions(env);
79 if (GetParam().expect_success) {
80 EXPECT_EQ(SPV_SUCCESS, result);
81 EXPECT_THAT(getDiagnosticString(), Eq(""));
82 } else {
83 EXPECT_EQ(SPV_ERROR_INVALID_ID, result);
84 EXPECT_THAT(getDiagnosticString(), HasSubstr(GetParam().expect_err));
85 }
86 }
87
88 #define GOOD_SHADER_10(STR) \
89 { SPV_ENV_UNIVERSAL_1_0, kShaderPreamble kBasicTypes STR, true, "" }
90 #define GOOD_KERNEL_10(STR) \
91 { SPV_ENV_UNIVERSAL_1_0, kKernelPreamble kBasicTypes STR, true, "" }
92 INSTANTIATE_TEST_SUITE_P(
93 UniversalInShader, ValidateConstantOp,
94 ValuesIn(std::vector<ConstantOpCase>{
95 // TODO(dneto): Conversions must change width.
96 GOOD_SHADER_10("%v = OpSpecConstantOp %uint SConvert %uint_0"),
97 GOOD_SHADER_10("%v = OpSpecConstantOp %float FConvert %float_0"),
98 GOOD_SHADER_10("%v = OpSpecConstantOp %uint SNegate %uint_0"),
99 GOOD_SHADER_10("%v = OpSpecConstantOp %uint Not %uint_0"),
100 GOOD_SHADER_10("%v = OpSpecConstantOp %uint IAdd %uint_0 %uint_0"),
101 GOOD_SHADER_10("%v = OpSpecConstantOp %uint ISub %uint_0 %uint_0"),
102 GOOD_SHADER_10("%v = OpSpecConstantOp %uint IMul %uint_0 %uint_0"),
103 GOOD_SHADER_10("%v = OpSpecConstantOp %uint UDiv %uint_0 %uint_0"),
104 GOOD_SHADER_10("%v = OpSpecConstantOp %uint SDiv %uint_0 %uint_0"),
105 GOOD_SHADER_10("%v = OpSpecConstantOp %uint UMod %uint_0 %uint_0"),
106 GOOD_SHADER_10("%v = OpSpecConstantOp %uint SRem %uint_0 %uint_0"),
107 GOOD_SHADER_10("%v = OpSpecConstantOp %uint SMod %uint_0 %uint_0"),
108 GOOD_SHADER_10(
109 "%v = OpSpecConstantOp %uint ShiftRightLogical %uint_0 %uint_0"),
110 GOOD_SHADER_10(
111 "%v = OpSpecConstantOp %uint ShiftRightArithmetic %uint_0 %uint_0"),
112 GOOD_SHADER_10(
113 "%v = OpSpecConstantOp %uint ShiftLeftLogical %uint_0 %uint_0"),
114 GOOD_SHADER_10("%v = OpSpecConstantOp %uint BitwiseOr %uint_0 %uint_0"),
115 GOOD_SHADER_10(
116 "%v = OpSpecConstantOp %uint BitwiseXor %uint_0 %uint_0"),
117 GOOD_SHADER_10(
118 "%v = OpSpecConstantOp %uint2 VectorShuffle %uint2_0 %uint2_0 1 3"),
119 GOOD_SHADER_10(
120 "%v = OpSpecConstantOp %uint CompositeExtract %uint2_0 1"),
121 GOOD_SHADER_10(
122 "%v = OpSpecConstantOp %uint2 CompositeInsert %uint_0 %uint2_0 1"),
123 GOOD_SHADER_10("%v = OpSpecConstantOp %bool LogicalOr %true %false"),
124 GOOD_SHADER_10("%v = OpSpecConstantOp %bool LogicalNot %true"),
125 GOOD_SHADER_10("%v = OpSpecConstantOp %bool LogicalAnd %true %false"),
126 GOOD_SHADER_10("%v = OpSpecConstantOp %bool LogicalEqual %true %false"),
127 GOOD_SHADER_10(
128 "%v = OpSpecConstantOp %bool LogicalNotEqual %true %false"),
129 GOOD_SHADER_10(
130 "%v = OpSpecConstantOp %uint Select %true %uint_0 %uint_0"),
131 GOOD_SHADER_10("%v = OpSpecConstantOp %bool IEqual %uint_0 %uint_0"),
132 GOOD_SHADER_10("%v = OpSpecConstantOp %bool INotEqual %uint_0 %uint_0"),
133 GOOD_SHADER_10("%v = OpSpecConstantOp %bool ULessThan %uint_0 %uint_0"),
134 GOOD_SHADER_10("%v = OpSpecConstantOp %bool SLessThan %uint_0 %uint_0"),
135 GOOD_SHADER_10(
136 "%v = OpSpecConstantOp %bool ULessThanEqual %uint_0 %uint_0"),
137 GOOD_SHADER_10(
138 "%v = OpSpecConstantOp %bool SLessThanEqual %uint_0 %uint_0"),
139 GOOD_SHADER_10(
140 "%v = OpSpecConstantOp %bool UGreaterThan %uint_0 %uint_0"),
141 GOOD_SHADER_10(
142 "%v = OpSpecConstantOp %bool UGreaterThanEqual %uint_0 %uint_0"),
143 GOOD_SHADER_10(
144 "%v = OpSpecConstantOp %bool SGreaterThan %uint_0 %uint_0"),
145 GOOD_SHADER_10(
146 "%v = OpSpecConstantOp %bool SGreaterThanEqual %uint_0 %uint_0"),
147 }));
148
149 INSTANTIATE_TEST_SUITE_P(
150 UniversalInKernel, ValidateConstantOp,
151 ValuesIn(std::vector<ConstantOpCase>{
152 // TODO(dneto): Conversions must change width.
153 GOOD_KERNEL_10("%v = OpSpecConstantOp %uint SConvert %uint_0"),
154 GOOD_KERNEL_10("%v = OpSpecConstantOp %float FConvert %float_0"),
155 GOOD_KERNEL_10("%v = OpSpecConstantOp %uint SNegate %uint_0"),
156 GOOD_KERNEL_10("%v = OpSpecConstantOp %uint Not %uint_0"),
157 GOOD_KERNEL_10("%v = OpSpecConstantOp %uint IAdd %uint_0 %uint_0"),
158 GOOD_KERNEL_10("%v = OpSpecConstantOp %uint ISub %uint_0 %uint_0"),
159 GOOD_KERNEL_10("%v = OpSpecConstantOp %uint IMul %uint_0 %uint_0"),
160 GOOD_KERNEL_10("%v = OpSpecConstantOp %uint UDiv %uint_0 %uint_0"),
161 GOOD_KERNEL_10("%v = OpSpecConstantOp %uint SDiv %uint_0 %uint_0"),
162 GOOD_KERNEL_10("%v = OpSpecConstantOp %uint UMod %uint_0 %uint_0"),
163 GOOD_KERNEL_10("%v = OpSpecConstantOp %uint SRem %uint_0 %uint_0"),
164 GOOD_KERNEL_10("%v = OpSpecConstantOp %uint SMod %uint_0 %uint_0"),
165 GOOD_KERNEL_10(
166 "%v = OpSpecConstantOp %uint ShiftRightLogical %uint_0 %uint_0"),
167 GOOD_KERNEL_10(
168 "%v = OpSpecConstantOp %uint ShiftRightArithmetic %uint_0 %uint_0"),
169 GOOD_KERNEL_10(
170 "%v = OpSpecConstantOp %uint ShiftLeftLogical %uint_0 %uint_0"),
171 GOOD_KERNEL_10("%v = OpSpecConstantOp %uint BitwiseOr %uint_0 %uint_0"),
172 GOOD_KERNEL_10(
173 "%v = OpSpecConstantOp %uint BitwiseXor %uint_0 %uint_0"),
174 GOOD_KERNEL_10(
175 "%v = OpSpecConstantOp %uint2 VectorShuffle %uint2_0 %uint2_0 1 3"),
176 GOOD_KERNEL_10(
177 "%v = OpSpecConstantOp %uint CompositeExtract %uint2_0 1"),
178 GOOD_KERNEL_10(
179 "%v = OpSpecConstantOp %uint2 CompositeInsert %uint_0 %uint2_0 1"),
180 GOOD_KERNEL_10("%v = OpSpecConstantOp %bool LogicalOr %true %false"),
181 GOOD_KERNEL_10("%v = OpSpecConstantOp %bool LogicalNot %true"),
182 GOOD_KERNEL_10("%v = OpSpecConstantOp %bool LogicalAnd %true %false"),
183 GOOD_KERNEL_10("%v = OpSpecConstantOp %bool LogicalEqual %true %false"),
184 GOOD_KERNEL_10(
185 "%v = OpSpecConstantOp %bool LogicalNotEqual %true %false"),
186 GOOD_KERNEL_10(
187 "%v = OpSpecConstantOp %uint Select %true %uint_0 %uint_0"),
188 GOOD_KERNEL_10("%v = OpSpecConstantOp %bool IEqual %uint_0 %uint_0"),
189 GOOD_KERNEL_10("%v = OpSpecConstantOp %bool INotEqual %uint_0 %uint_0"),
190 GOOD_KERNEL_10("%v = OpSpecConstantOp %bool ULessThan %uint_0 %uint_0"),
191 GOOD_KERNEL_10("%v = OpSpecConstantOp %bool SLessThan %uint_0 %uint_0"),
192 GOOD_KERNEL_10(
193 "%v = OpSpecConstantOp %bool ULessThanEqual %uint_0 %uint_0"),
194 GOOD_KERNEL_10(
195 "%v = OpSpecConstantOp %bool SLessThanEqual %uint_0 %uint_0"),
196 GOOD_KERNEL_10(
197 "%v = OpSpecConstantOp %bool UGreaterThan %uint_0 %uint_0"),
198 GOOD_KERNEL_10(
199 "%v = OpSpecConstantOp %bool UGreaterThanEqual %uint_0 %uint_0"),
200 GOOD_KERNEL_10(
201 "%v = OpSpecConstantOp %bool SGreaterThan %uint_0 %uint_0"),
202 GOOD_KERNEL_10(
203 "%v = OpSpecConstantOp %bool SGreaterThanEqual %uint_0 %uint_0"),
204 }));
205
206 INSTANTIATE_TEST_SUITE_P(
207 UConvert, ValidateConstantOp,
208 ValuesIn(std::vector<ConstantOpCase>{
209 // TODO(dneto): Conversions must change width.
210 {SPV_ENV_UNIVERSAL_1_0,
211 kKernelPreamble kBasicTypes
212 "%v = OpSpecConstantOp %uint UConvert %uint_0",
213 true, ""},
214 {SPV_ENV_UNIVERSAL_1_1,
215 kKernelPreamble kBasicTypes
216 "%v = OpSpecConstantOp %uint UConvert %uint_0",
217 true, ""},
218 {SPV_ENV_UNIVERSAL_1_3,
219 kKernelPreamble kBasicTypes
220 "%v = OpSpecConstantOp %uint UConvert %uint_0",
221 true, ""},
222 {SPV_ENV_UNIVERSAL_1_3,
223 kKernelPreamble kBasicTypes
224 "%v = OpSpecConstantOp %uint UConvert %uint_0",
225 true, ""},
226 {SPV_ENV_UNIVERSAL_1_4,
227 kKernelPreamble kBasicTypes
228 "%v = OpSpecConstantOp %uint UConvert %uint_0",
229 true, ""},
230 {SPV_ENV_UNIVERSAL_1_0,
231 kShaderPreamble kBasicTypes
232 "%v = OpSpecConstantOp %uint UConvert %uint_0",
233 false,
234 "Prior to SPIR-V 1.4, specialization constant operation "
235 "UConvert requires Kernel capability"},
236 {SPV_ENV_UNIVERSAL_1_1,
237 kShaderPreamble kBasicTypes
238 "%v = OpSpecConstantOp %uint UConvert %uint_0",
239 false,
240 "Prior to SPIR-V 1.4, specialization constant operation "
241 "UConvert requires Kernel capability"},
242 {SPV_ENV_UNIVERSAL_1_3,
243 kShaderPreamble kBasicTypes
244 "%v = OpSpecConstantOp %uint UConvert %uint_0",
245 false,
246 "Prior to SPIR-V 1.4, specialization constant operation "
247 "UConvert requires Kernel capability"},
248 {SPV_ENV_UNIVERSAL_1_3,
249 kShaderPreamble kBasicTypes
250 "%v = OpSpecConstantOp %uint UConvert %uint_0",
251 false,
252 "Prior to SPIR-V 1.4, specialization constant operation "
253 "UConvert requires Kernel capability"},
254 {SPV_ENV_UNIVERSAL_1_4,
255 kShaderPreamble kBasicTypes
256 "%v = OpSpecConstantOp %uint UConvert %uint_0",
257 true, ""},
258 }));
259
260 INSTANTIATE_TEST_SUITE_P(
261 KernelInKernel, ValidateConstantOp,
262 ValuesIn(std::vector<ConstantOpCase>{
263 // TODO(dneto): Conversions must change width.
264 GOOD_KERNEL_10("%v = OpSpecConstantOp %uint ConvertFToS %float_0"),
265 GOOD_KERNEL_10("%v = OpSpecConstantOp %float ConvertSToF %uint_0"),
266 GOOD_KERNEL_10("%v = OpSpecConstantOp %uint ConvertFToU %float_0"),
267 GOOD_KERNEL_10("%v = OpSpecConstantOp %float ConvertUToF %uint_0"),
268 GOOD_KERNEL_10("%v = OpSpecConstantOp %uint UConvert %uint_0"),
269 GOOD_KERNEL_10(
270 "%v = OpSpecConstantOp %_ptr_uint GenericCastToPtr %null"),
271 GOOD_KERNEL_10(
272 "%v = OpSpecConstantOp %_ptr_uint PtrCastToGeneric %null"),
273 GOOD_KERNEL_10("%v = OpSpecConstantOp %uint Bitcast %uint_0"),
274 GOOD_KERNEL_10("%v = OpSpecConstantOp %float FNegate %float_0"),
275 GOOD_KERNEL_10("%v = OpSpecConstantOp %float FAdd %float_0 %float_0"),
276 GOOD_KERNEL_10("%v = OpSpecConstantOp %float FSub %float_0 %float_0"),
277 GOOD_KERNEL_10("%v = OpSpecConstantOp %float FMul %float_0 %float_0"),
278 GOOD_KERNEL_10("%v = OpSpecConstantOp %float FDiv %float_0 %float_0"),
279 GOOD_KERNEL_10("%v = OpSpecConstantOp %float FRem %float_0 %float_0"),
280 GOOD_KERNEL_10("%v = OpSpecConstantOp %float FMod %float_0 %float_0"),
281 GOOD_KERNEL_10(
282 "%v = OpSpecConstantOp %_ptr_uint AccessChain %null %uint_0"),
283 GOOD_KERNEL_10("%v = OpSpecConstantOp %_ptr_uint InBoundsAccessChain "
284 "%null %uint_0"),
285 GOOD_KERNEL_10(
286 "%v = OpSpecConstantOp %_ptr_uint PtrAccessChain %null %uint_0"),
287 GOOD_KERNEL_10("%v = OpSpecConstantOp %_ptr_uint "
288 "InBoundsPtrAccessChain %null %uint_0"),
289 }));
290
291 #define BAD_SHADER_10(STR, NAME) \
292 { \
293 SPV_ENV_UNIVERSAL_1_0, kShaderPreamble kBasicTypes STR, false, \
294 "Specialization constant operation " NAME \
295 " requires Kernel capability" \
296 }
297 INSTANTIATE_TEST_SUITE_P(
298 KernelInShader, ValidateConstantOp,
299 ValuesIn(std::vector<ConstantOpCase>{
300 // TODO(dneto): Conversions must change width.
301 BAD_SHADER_10("%v = OpSpecConstantOp %uint ConvertFToS %float_0",
302 "ConvertFToS"),
303 BAD_SHADER_10("%v = OpSpecConstantOp %float ConvertSToF %uint_0",
304 "ConvertSToF"),
305 BAD_SHADER_10("%v = OpSpecConstantOp %uint ConvertFToU %float_0",
306 "ConvertFToU"),
307 BAD_SHADER_10("%v = OpSpecConstantOp %float ConvertUToF %uint_0",
308 "ConvertUToF"),
309 BAD_SHADER_10("%v = OpSpecConstantOp %_ptr_uint GenericCastToPtr %null",
310 "GenericCastToPtr"),
311 BAD_SHADER_10("%v = OpSpecConstantOp %_ptr_uint PtrCastToGeneric %null",
312 "PtrCastToGeneric"),
313 BAD_SHADER_10("%v = OpSpecConstantOp %uint Bitcast %uint_0", "Bitcast"),
314 BAD_SHADER_10("%v = OpSpecConstantOp %float FNegate %float_0",
315 "FNegate"),
316 BAD_SHADER_10("%v = OpSpecConstantOp %float FAdd %float_0 %float_0",
317 "FAdd"),
318 BAD_SHADER_10("%v = OpSpecConstantOp %float FSub %float_0 %float_0",
319 "FSub"),
320 BAD_SHADER_10("%v = OpSpecConstantOp %float FMul %float_0 %float_0",
321 "FMul"),
322 BAD_SHADER_10("%v = OpSpecConstantOp %float FDiv %float_0 %float_0",
323 "FDiv"),
324 BAD_SHADER_10("%v = OpSpecConstantOp %float FRem %float_0 %float_0",
325 "FRem"),
326 BAD_SHADER_10("%v = OpSpecConstantOp %float FMod %float_0 %float_0",
327 "FMod"),
328 BAD_SHADER_10(
329 "%v = OpSpecConstantOp %_ptr_uint AccessChain %null %uint_0",
330 "AccessChain"),
331 BAD_SHADER_10("%v = OpSpecConstantOp %_ptr_uint InBoundsAccessChain "
332 "%null %uint_0",
333 "InBoundsAccessChain"),
334 BAD_SHADER_10(
335 "%v = OpSpecConstantOp %_ptr_uint PtrAccessChain %null %uint_0",
336 "PtrAccessChain"),
337 BAD_SHADER_10("%v = OpSpecConstantOp %_ptr_uint "
338 "InBoundsPtrAccessChain %null %uint_0",
339 "InBoundsPtrAccessChain"),
340 }));
341
342 INSTANTIATE_TEST_SUITE_P(
343 UConvertInAMD_gpu_shader_int16, ValidateConstantOp,
344 ValuesIn(std::vector<ConstantOpCase>{
345 // SPV_AMD_gpu_shader_int16 should enable UConvert for OpSpecConstantOp
346 // https://github.com/KhronosGroup/glslang/issues/848
347 {SPV_ENV_UNIVERSAL_1_0,
348 "OpCapability Shader\n"
349 "OpCapability VariablePointers\n"
350 "OpCapability Linkage ; So we don't need to define a function\n"
351 "OpExtension \"SPV_AMD_gpu_shader_int16\"\n"
352 "OpExtension \"SPV_KHR_variable_pointers\"\n"
353 "OpMemoryModel Logical Simple " kBasicTypes
354 "%v = OpSpecConstantOp %uint UConvert %uint_0",
355 true, ""},
356 }));
357
358 TEST_F(ValidateConstant, SpecConstantUConvert1p3Binary1p4EnvBad) {
359 const std::string spirv = R"(
360 OpCapability Shader
361 OpCapability Linkage
362 OpMemoryModel Logical GLSL450
363 %int = OpTypeInt 32 0
364 %int0 = OpConstant %int 0
365 %const = OpSpecConstantOp %int UConvert %int0
366 )";
367
368 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
369 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
370 EXPECT_THAT(
371 getDiagnosticString(),
372 HasSubstr(
373 "Prior to SPIR-V 1.4, specialization constant operation UConvert "
374 "requires Kernel capability or extension SPV_AMD_gpu_shader_int16"));
375 }
376
377 using SmallStorageConstants = spvtest::ValidateBase<std::string>;
378
379 CodeGenerator GetSmallStorageCodeGenerator() {
380 CodeGenerator generator;
381 generator.capabilities_ = R"(
382 OpCapability Shader
383 OpCapability Linkage
384 OpCapability UniformAndStorageBuffer16BitAccess
385 OpCapability StoragePushConstant16
386 OpCapability StorageInputOutput16
387 OpCapability UniformAndStorageBuffer8BitAccess
388 OpCapability StoragePushConstant8
389 )";
390 generator.extensions_ = R"(
391 OpExtension "SPV_KHR_16bit_storage"
392 OpExtension "SPV_KHR_8bit_storage"
393 )";
394 generator.memory_model_ = "OpMemoryModel Logical GLSL450\n";
395 generator.types_ = R"(
396 %short = OpTypeInt 16 0
397 %short2 = OpTypeVector %short 2
398 %char = OpTypeInt 8 0
399 %char2 = OpTypeVector %char 2
400 %half = OpTypeFloat 16
401 %half2 = OpTypeVector %half 2
402 %int = OpTypeInt 32 0
403 %int_0 = OpConstant %int 0
404 %float = OpTypeFloat 32
405 %float_0 = OpConstant %float 0
406 )";
407 return generator;
408 }
409
410 TEST_P(SmallStorageConstants, SmallConstant) {
411 std::string constant = GetParam();
412 CodeGenerator generator = GetSmallStorageCodeGenerator();
413 generator.after_types_ += constant + "\n";
414 CompileSuccessfully(generator.Build(), SPV_ENV_UNIVERSAL_1_3);
415 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
416 EXPECT_THAT(getDiagnosticString(),
417 HasSubstr("Cannot form constants of 8- or 16-bit types"));
418 }
419
420 // Constant composites would be caught through scalar constants.
421 INSTANTIATE_TEST_SUITE_P(
422 SmallConstants, SmallStorageConstants,
423 Values("%c = OpConstant %char 0", "%c = OpConstantNull %char2",
424 "%c = OpConstant %short 0", "%c = OpConstantNull %short",
425 "%c = OpConstant %half 0", "%c = OpConstantNull %half",
426 "%c = OpSpecConstant %char 0", "%c = OpSpecConstant %short 0",
427 "%c = OpSpecConstant %half 0",
428 "%c = OpSpecConstantOp %char SConvert %int_0",
429 "%c = OpSpecConstantOp %short SConvert %int_0",
430 "%c = OpSpecConstantOp %half FConvert %float_0"));
431
432 TEST_F(ValidateConstant, NullPointerTo16BitStorageOk) {
433 std::string spirv = R"(
434 OpCapability Shader
435 OpCapability VariablePointersStorageBuffer
436 OpCapability UniformAndStorageBuffer16BitAccess
437 OpCapability Linkage
438 OpExtension "SPV_KHR_16bit_storage"
439 OpMemoryModel Logical GLSL450
440 %half = OpTypeFloat 16
441 %ptr_ssbo_half = OpTypePointer StorageBuffer %half
442 %null_ptr = OpConstantNull %ptr_ssbo_half
443 )";
444
445 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
446 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
447 }
448
449 TEST_F(ValidateConstant, NullMatrix) {
450 std::string spirv = R"(
451 OpCapability Shader
452 OpCapability Linkage
453 OpMemoryModel Logical GLSL450
454 %float = OpTypeFloat 32
455 %v2float = OpTypeVector %float 2
456 %mat2x2 = OpTypeMatrix %v2float 2
457 %null_vector = OpConstantNull %v2float
458 %null_matrix = OpConstantComposite %mat2x2 %null_vector %null_vector
459 )";
460
461 CompileSuccessfully(spirv);
462 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
463 }
464
465 TEST_F(ValidateConstant, NullPhysicalStorageBuffer) {
466 std::string spirv = R"(
467 OpCapability Shader
468 OpCapability PhysicalStorageBufferAddresses
469 OpCapability Linkage
470 OpExtension "SPV_KHR_physical_storage_buffer"
471 OpMemoryModel PhysicalStorageBuffer64 GLSL450
472 OpName %ptr "ptr"
473 %int = OpTypeInt 32 0
474 %ptr = OpTypePointer PhysicalStorageBuffer %int
475 %null = OpConstantNull %ptr
476 )";
477
478 CompileSuccessfully(spirv);
479 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
480 EXPECT_THAT(getDiagnosticString(),
481 HasSubstr("OpConstantNull Result Type <id> '1[%ptr]' cannot have "
482 "a null value"));
483 }
484
485 TEST_F(ValidateConstant, VectorMismatchedConstituents) {
486 std::string spirv = kShaderPreamble kBasicTypes R"(
487 %int = OpTypeInt 32 1
488 %int_0 = OpConstantNull %int
489 %const_vector = OpConstantComposite %uint2 %uint_0 %int_0
490 )";
491
492 CompileSuccessfully(spirv);
493 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
494 EXPECT_THAT(
495 getDiagnosticString(),
496 HasSubstr(
497 "OpConstantComposite Constituent <id> '13[%13]'s type "
498 "does not match Result Type <id> '3[%v2uint]'s vector element type"));
499 }
500
501 } // namespace
502 } // namespace val
503 } // namespace spvtools