Debian Codegraph @

test/val/val_id_test.cpp

raw
1 // Copyright (c) 2015-2016 The Khronos Group Inc.
2 // Modifications Copyright (C) 2024 Advanced Micro Devices, Inc. All rights
3 // reserved.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16
17 #include <sstream>
18 #include <string>
19 #include <vector>
20
21 #include "gmock/gmock.h"
22 #include "test/test_fixture.h"
23 #include "test/unit_spirv.h"
24 #include "test/val/val_fixtures.h"
25
26 // NOTE: The tests in this file are ONLY testing ID usage, there for the input
27 // SPIR-V does not follow the logical layout rules from the spec in all cases in
28 // order to makes the tests smaller. Validation of the whole module is handled
29 // in stages, ID validation is only one of these stages. All validation stages
30 // are stand alone.
31
32 namespace spvtools {
33 namespace val {
34 namespace {
35
36 using spvtest::ScopedContext;
37 using ::testing::HasSubstr;
38 using ::testing::ValuesIn;
39
40 class ValidateIdWithMessage : public spvtest::ValidateBase<bool> {
41 public:
42 ValidateIdWithMessage() {
43 const bool use_friendly_names = GetParam();
44 spvValidatorOptionsSetFriendlyNames(options_, use_friendly_names);
45 }
46
47 std::string make_message(const char* msg);
48 };
49
50 std::string kOpCapabilitySetupWithoutVector16 = R"(
51 OpCapability Shader
52 OpCapability Linkage
53 OpCapability Addresses
54 OpCapability Int8
55 OpCapability Int16
56 OpCapability Int64
57 OpCapability Float64
58 OpCapability LiteralSampler
59 OpCapability Pipes
60 OpCapability DeviceEnqueue
61 )";
62
63 std::string kOpCapabilitySetup = R"(
64 OpCapability Shader
65 OpCapability Linkage
66 OpCapability Addresses
67 OpCapability Int8
68 OpCapability Int16
69 OpCapability Int64
70 OpCapability Float64
71 OpCapability LiteralSampler
72 OpCapability Pipes
73 OpCapability DeviceEnqueue
74 OpCapability Vector16
75 )";
76
77 std::string kOpVariablePtrSetUp = R"(
78 OpCapability VariablePointers
79 OpExtension "SPV_KHR_variable_pointers"
80 )";
81
82 std::string kGLSL450MemoryModel = kOpCapabilitySetup + kOpVariablePtrSetUp + R"(
83 OpMemoryModel Logical GLSL450
84 )";
85
86 std::string kGLSL450MemoryModelWithoutVector16 =
87 kOpCapabilitySetupWithoutVector16 + kOpVariablePtrSetUp + R"(
88 OpMemoryModel Logical GLSL450
89 )";
90
91 std::string kNoKernelGLSL450MemoryModel = R"(
92 OpCapability Shader
93 OpCapability Linkage
94 OpCapability Addresses
95 OpCapability Int8
96 OpCapability Int16
97 OpCapability Int64
98 OpCapability Float64
99 OpMemoryModel Logical GLSL450
100 )";
101
102 std::string kOpenCLMemoryModel32 = R"(
103 OpCapability Addresses
104 OpCapability Linkage
105 OpCapability Kernel
106 %1 = OpExtInstImport "OpenCL.std"
107 OpMemoryModel Physical32 OpenCL
108 )";
109
110 std::string kOpenCLMemoryModel64 = R"(
111 OpCapability Addresses
112 OpCapability Linkage
113 OpCapability Kernel
114 OpCapability Int64
115 %1 = OpExtInstImport "OpenCL.std"
116 OpMemoryModel Physical64 OpenCL
117 )";
118
119 std::string sampledImageSetup = R"(
120 %void = OpTypeVoid
121 %typeFuncVoid = OpTypeFunction %void
122 %float = OpTypeFloat 32
123 %v4float = OpTypeVector %float 4
124 %image_type = OpTypeImage %float 2D 0 0 0 1 Unknown
125 %_ptr_UniformConstant_img = OpTypePointer UniformConstant %image_type
126 %tex = OpVariable %_ptr_UniformConstant_img UniformConstant
127 %sampler_type = OpTypeSampler
128 %_ptr_UniformConstant_sam = OpTypePointer UniformConstant %sampler_type
129 %s = OpVariable %_ptr_UniformConstant_sam UniformConstant
130 %sampled_image_type = OpTypeSampledImage %image_type
131 %v2float = OpTypeVector %float 2
132 %float_1 = OpConstant %float 1
133 %float_2 = OpConstant %float 2
134 %const_vec_1_1 = OpConstantComposite %v2float %float_1 %float_1
135 %const_vec_2_2 = OpConstantComposite %v2float %float_2 %float_2
136 %bool_type = OpTypeBool
137 %spec_true = OpSpecConstantTrue %bool_type
138 %main = OpFunction %void None %typeFuncVoid
139 %label_1 = OpLabel
140 %image_inst = OpLoad %image_type %tex
141 %sampler_inst = OpLoad %sampler_type %s
142 )";
143
144 std::string BranchConditionalSetup = R"(
145 OpCapability Shader
146 %1 = OpExtInstImport "GLSL.std.450"
147 OpMemoryModel Logical GLSL450
148 OpEntryPoint Fragment %main "main"
149 OpExecutionMode %main OriginUpperLeft
150 OpSource GLSL 140
151 OpName %main "main"
152
153 ; type definitions
154 %bool = OpTypeBool
155 %uint = OpTypeInt 32 0
156 %int = OpTypeInt 32 1
157 %float = OpTypeFloat 32
158 %v4float = OpTypeVector %float 4
159
160 ; constants
161 %true = OpConstantTrue %bool
162 %i0 = OpConstant %int 0
163 %i1 = OpConstant %int 1
164 %f0 = OpConstant %float 0
165 %f1 = OpConstant %float 1
166
167
168 ; main function header
169 %void = OpTypeVoid
170 %voidfunc = OpTypeFunction %void
171 %main = OpFunction %void None %voidfunc
172 %lmain = OpLabel
173 )";
174
175 std::string BranchConditionalTail = R"(
176 %target_t = OpLabel
177 OpNop
178 OpBranch %end
179 %target_f = OpLabel
180 OpNop
181 OpBranch %end
182
183 %end = OpLabel
184
185 OpReturn
186 OpFunctionEnd
187 )";
188
189 // Transform an expected validation message to either use friendly names (as
190 // provided in the message) or replace the friendly names by the corresponding
191 // id. The same flag used to configure the validator to output friendly names
192 // or not is used here.
193 std::string ValidateIdWithMessage::make_message(const char* msg) {
194 const bool use_friendly_names = GetParam();
195 if (use_friendly_names) {
196 return msg;
197 }
198
199 std::string message(msg);
200 std::ostringstream result;
201
202 size_t next = 0;
203 while (next < message.size()) {
204 // Parse 'num[%name]'
205 size_t open_quote = message.find('\'', next);
206
207 if (open_quote == std::string::npos) {
208 break;
209 }
210
211 // Copy up to the first quote
212 result.write(msg + next, open_quote - next);
213 // Handle apostrophes
214 if (!isdigit(message[open_quote + 1])) {
215 result << '\'';
216 next = open_quote + 1;
217 continue;
218 }
219
220 size_t open_bracket = message.find('[', open_quote + 1);
221 assert(open_bracket != std::string::npos);
222
223 size_t close_bracket = message.find(']', open_bracket + 1);
224 assert(close_bracket != std::string::npos);
225
226 size_t close_quote = close_bracket + 1;
227 assert(close_quote < message.size() && message[close_quote] == '\'');
228
229 // Change to 'num[%num]' because friendly names are not being used.
230 result.write(msg + open_quote, open_bracket - open_quote + 1);
231 result << '%';
232 result.write(msg + open_quote + 1, open_bracket - open_quote - 1);
233 result << "]'";
234
235 // Continue to the next id, or end of string.
236 next = close_quote + 1;
237 }
238
239 return result.str();
240 }
241
242 // TODO: OpUndef
243
244 TEST_P(ValidateIdWithMessage, OpName) {
245 std::string spirv = kGLSL450MemoryModel + R"(
246 OpName %2 "name"
247 %1 = OpTypeInt 32 0
248 %2 = OpTypePointer UniformConstant %1
249 %3 = OpVariable %2 UniformConstant)";
250 CompileSuccessfully(spirv.c_str());
251 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
252 }
253
254 TEST_P(ValidateIdWithMessage, OpMemberNameGood) {
255 std::string spirv = kGLSL450MemoryModel + R"(
256 OpMemberName %2 0 "foo"
257 %1 = OpTypeInt 32 0
258 %2 = OpTypeStruct %1)";
259 CompileSuccessfully(spirv.c_str());
260 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
261 }
262 TEST_P(ValidateIdWithMessage, OpMemberNameTypeBad) {
263 std::string spirv = kGLSL450MemoryModel + R"(
264 OpMemberName %1 0 "foo"
265 %1 = OpTypeInt 32 0)";
266 CompileSuccessfully(spirv.c_str());
267 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
268 EXPECT_THAT(getDiagnosticString(),
269 HasSubstr(make_message(
270 "OpMemberName Type <id> '1[%uint]' is not a struct type.")));
271 }
272 TEST_P(ValidateIdWithMessage, OpMemberNameMemberBad) {
273 std::string spirv = kGLSL450MemoryModel + R"(
274 OpMemberName %1 1 "foo"
275 %2 = OpTypeInt 32 0
276 %1 = OpTypeStruct %2)";
277 CompileSuccessfully(spirv.c_str());
278 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
279 EXPECT_THAT(getDiagnosticString(),
280 HasSubstr(make_message(
281 "OpMemberName Member <id> '1[%_struct_1]' index is larger "
282 "than Type <id> '1[%_struct_1]'s member count.")));
283 }
284
285 TEST_P(ValidateIdWithMessage, OpLineGood) {
286 std::string spirv = kGLSL450MemoryModel + R"(
287 %1 = OpString "/path/to/source.file"
288 OpLine %1 0 0
289 %2 = OpTypeInt 32 0
290 %3 = OpTypePointer Input %2
291 %4 = OpVariable %3 Input)";
292 CompileSuccessfully(spirv.c_str());
293 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
294 }
295
296 TEST_P(ValidateIdWithMessage, OpLineFileBad) {
297 std::string spirv = kGLSL450MemoryModel + R"(
298 %1 = OpTypeInt 32 0
299 OpLine %1 0 0
300 )";
301 CompileSuccessfully(spirv.c_str());
302 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
303 EXPECT_THAT(getDiagnosticString(),
304 HasSubstr(make_message(
305 "OpLine Target <id> '1[%uint]' is not an OpString.")));
306 }
307
308 TEST_P(ValidateIdWithMessage, OpDecorateGood) {
309 std::string spirv = kGLSL450MemoryModel + R"(
310 OpDecorate %2 GLSLShared
311 %1 = OpTypeInt 64 0
312 %2 = OpTypeStruct %1 %1)";
313 CompileSuccessfully(spirv.c_str());
314 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
315 }
316 TEST_P(ValidateIdWithMessage, OpDecorateBad) {
317 std::string spirv = kGLSL450MemoryModel + R"(
318 OpDecorate %1 GLSLShared)";
319 CompileSuccessfully(spirv.c_str());
320 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
321 EXPECT_THAT(
322 getDiagnosticString(),
323 HasSubstr(make_message("forward referenced IDs have not been defined")));
324 }
325
326 TEST_P(ValidateIdWithMessage, OpMemberDecorateGood) {
327 std::string spirv = kGLSL450MemoryModel + R"(
328 OpMemberDecorate %2 0 RelaxedPrecision
329 %1 = OpTypeInt 32 0
330 %2 = OpTypeStruct %1 %1)";
331 CompileSuccessfully(spirv.c_str());
332 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
333 }
334 TEST_P(ValidateIdWithMessage, OpMemberDecorateBad) {
335 std::string spirv = kGLSL450MemoryModel + R"(
336 OpMemberDecorate %1 0 RelaxedPrecision
337 %1 = OpTypeInt 32 0)";
338 CompileSuccessfully(spirv.c_str());
339 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
340 EXPECT_THAT(getDiagnosticString(),
341 HasSubstr(make_message(
342 "OpMemberDecorate Structure type <id> '1[%uint]' is "
343 "not a struct type.")));
344 }
345 TEST_P(ValidateIdWithMessage, OpMemberDecorateMemberBad) {
346 std::string spirv = kGLSL450MemoryModel + R"(
347 OpMemberDecorate %1 3 RelaxedPrecision
348 %int = OpTypeInt 32 0
349 %1 = OpTypeStruct %int %int)";
350 CompileSuccessfully(spirv.c_str());
351 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
352 EXPECT_THAT(getDiagnosticString(),
353 HasSubstr(make_message(
354 "Index 3 provided in OpMemberDecorate for struct <id> "
355 "'1[%_struct_1]' is out of bounds. The structure has 2 "
356 "members. Largest valid index is 1.")));
357 }
358
359 TEST_P(ValidateIdWithMessage, OpGroupDecorateGood) {
360 std::string spirv = kGLSL450MemoryModel + R"(
361 %1 = OpDecorationGroup
362 OpDecorate %1 RelaxedPrecision
363 OpDecorate %1 GLSLShared
364 OpGroupDecorate %1 %3 %4
365 %2 = OpTypeInt 32 0
366 %3 = OpConstant %2 42
367 %4 = OpConstant %2 23)";
368 CompileSuccessfully(spirv.c_str());
369 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
370 }
371 TEST_P(ValidateIdWithMessage, OpDecorationGroupBad) {
372 std::string spirv = kGLSL450MemoryModel + R"(
373 %1 = OpDecorationGroup
374 OpDecorate %1 RelaxedPrecision
375 OpDecorate %1 GLSLShared
376 OpMemberDecorate %1 0 Constant
377 )";
378 CompileSuccessfully(spirv.c_str());
379 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
380 EXPECT_THAT(getDiagnosticString(),
381 HasSubstr(make_message(
382 "Result id of OpDecorationGroup can only "
383 "be targeted by OpName, OpGroupDecorate, "
384 "OpDecorate, OpDecorateId, and OpGroupMemberDecorate")));
385 }
386 TEST_P(ValidateIdWithMessage, OpGroupDecorateDecorationGroupBad) {
387 std::string spirv = R"(
388 OpCapability Shader
389 OpCapability Linkage
390 %1 = OpExtInstImport "GLSL.std.450"
391 OpMemoryModel Logical GLSL450
392 OpGroupDecorate %1 %2 %3
393 %2 = OpTypeInt 32 0
394 %3 = OpConstant %2 42)";
395 CompileSuccessfully(spirv.c_str());
396 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
397 EXPECT_THAT(getDiagnosticString(),
398 HasSubstr(make_message(
399 "OpGroupDecorate Decoration group <id> '1[%1]' is not "
400 "a decoration group.")));
401 }
402 TEST_P(ValidateIdWithMessage, OpGroupDecorateTargetBad) {
403 std::string spirv = kGLSL450MemoryModel + R"(
404 %1 = OpDecorationGroup
405 OpDecorate %1 RelaxedPrecision
406 OpDecorate %1 GLSLShared
407 OpGroupDecorate %1 %3
408 %2 = OpTypeInt 32 0)";
409 CompileSuccessfully(spirv.c_str());
410 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
411 EXPECT_THAT(
412 getDiagnosticString(),
413 HasSubstr(make_message("forward referenced IDs have not been defined")));
414 }
415 TEST_P(ValidateIdWithMessage, OpGroupMemberDecorateDecorationGroupBad) {
416 std::string spirv = R"(
417 OpCapability Shader
418 OpCapability Linkage
419 %1 = OpExtInstImport "GLSL.std.450"
420 OpMemoryModel Logical GLSL450
421 OpGroupMemberDecorate %1 %2 0
422 %2 = OpTypeInt 32 0)";
423 CompileSuccessfully(spirv.c_str());
424 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
425 EXPECT_THAT(getDiagnosticString(),
426 HasSubstr(make_message(
427 "OpGroupMemberDecorate Decoration group <id> '1[%1]' "
428 "is not a decoration group.")));
429 }
430 TEST_P(ValidateIdWithMessage, OpGroupMemberDecorateIdNotStructBad) {
431 std::string spirv = kGLSL450MemoryModel + R"(
432 %1 = OpDecorationGroup
433 OpGroupMemberDecorate %1 %2 0
434 %2 = OpTypeInt 32 0)";
435 CompileSuccessfully(spirv.c_str());
436 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
437 EXPECT_THAT(getDiagnosticString(),
438 HasSubstr(make_message(
439 "OpGroupMemberDecorate Structure type <id> '2[%uint]' "
440 "is not a struct type.")));
441 }
442 TEST_P(ValidateIdWithMessage, OpGroupMemberDecorateIndexOutOfBoundBad) {
443 std::string spirv = kGLSL450MemoryModel + R"(
444 OpDecorate %1 Offset 0
445 %1 = OpDecorationGroup
446 OpGroupMemberDecorate %1 %struct 3
447 %float = OpTypeFloat 32
448 %struct = OpTypeStruct %float %float %float
449 )";
450 CompileSuccessfully(spirv.c_str());
451 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
452 EXPECT_THAT(getDiagnosticString(),
453 HasSubstr(make_message(
454 "Index 3 provided in OpGroupMemberDecorate for struct "
455 "<id> '2[%_struct_2]' is out of bounds. The structure "
456 "has 3 members. Largest valid index is 2.")));
457 }
458
459 // TODO: OpExtInst
460
461 TEST_P(ValidateIdWithMessage, OpEntryPointGood) {
462 std::string spirv = kGLSL450MemoryModel + R"(
463 OpEntryPoint GLCompute %3 ""
464 %1 = OpTypeVoid
465 %2 = OpTypeFunction %1
466 %3 = OpFunction %1 None %2
467 %4 = OpLabel
468 OpReturn
469 OpFunctionEnd
470 )";
471 CompileSuccessfully(spirv.c_str());
472 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
473 }
474 TEST_P(ValidateIdWithMessage, OpEntryPointFunctionBad) {
475 std::string spirv = kGLSL450MemoryModel + R"(
476 OpEntryPoint GLCompute %1 ""
477 %1 = OpTypeVoid)";
478 CompileSuccessfully(spirv.c_str());
479 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
480 EXPECT_THAT(getDiagnosticString(),
481 HasSubstr(make_message(
482 "OpEntryPoint Entry Point <id> '1[%void]' is not a "
483 "function.")));
484 }
485 TEST_P(ValidateIdWithMessage, OpEntryPointParameterCountBad) {
486 std::string spirv = kGLSL450MemoryModel + R"(
487 OpEntryPoint GLCompute %1 ""
488 %2 = OpTypeVoid
489 %3 = OpTypeFunction %2 %2
490 %1 = OpFunction %2 None %3
491 %4 = OpLabel
492 OpReturn
493 OpFunctionEnd)";
494 CompileSuccessfully(spirv.c_str());
495 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
496 EXPECT_THAT(
497 getDiagnosticString(),
498 HasSubstr(make_message("OpEntryPoint Entry Point <id> '1[%1]'s function "
499 "parameter count is not zero")));
500 }
501 TEST_P(ValidateIdWithMessage, OpEntryPointReturnTypeBad) {
502 std::string spirv = kGLSL450MemoryModel + R"(
503 OpEntryPoint GLCompute %1 ""
504 %2 = OpTypeInt 32 0
505 %ret = OpConstant %2 0
506 %3 = OpTypeFunction %2
507 %1 = OpFunction %2 None %3
508 %4 = OpLabel
509 OpReturnValue %ret
510 OpFunctionEnd)";
511 CompileSuccessfully(spirv.c_str());
512 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
513 EXPECT_THAT(
514 getDiagnosticString(),
515 HasSubstr(make_message("OpEntryPoint Entry Point <id> '1[%1]'s function "
516 "return type is not void.")));
517 }
518 TEST_P(ValidateIdWithMessage, OpEntryPointParameterCountBadInVulkan) {
519 std::string spirv = R"(
520 OpCapability Shader
521 OpMemoryModel Logical GLSL450
522 OpEntryPoint GLCompute %1 ""
523 %2 = OpTypeVoid
524 %3 = OpTypeFunction %2 %2
525 %1 = OpFunction %2 None %3
526 %4 = OpLabel
527 OpReturn
528 OpFunctionEnd)";
529 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_0);
530 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
531 EXPECT_THAT(getDiagnosticString(),
532 AnyVUID("VUID-StandaloneSpirv-None-04633"));
533 EXPECT_THAT(
534 getDiagnosticString(),
535 HasSubstr(make_message("OpEntryPoint Entry Point <id> '1[%1]'s function "
536 "parameter count is not zero")));
537 }
538 TEST_P(ValidateIdWithMessage, OpEntryPointReturnTypeBadInVulkan) {
539 std::string spirv = R"(
540 OpCapability Shader
541 OpMemoryModel Logical GLSL450
542 OpEntryPoint GLCompute %1 ""
543 %2 = OpTypeInt 32 0
544 %ret = OpConstant %2 0
545 %3 = OpTypeFunction %2
546 %1 = OpFunction %2 None %3
547 %4 = OpLabel
548 OpReturnValue %ret
549 OpFunctionEnd)";
550 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_0);
551 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
552 EXPECT_THAT(getDiagnosticString(),
553 AnyVUID("VUID-StandaloneSpirv-None-04633"));
554 EXPECT_THAT(
555 getDiagnosticString(),
556 HasSubstr(make_message("OpEntryPoint Entry Point <id> '1[%1]'s function "
557 "return type is not void.")));
558 }
559
560 TEST_P(ValidateIdWithMessage, OpEntryPointInterfaceIsNotVariableTypeBad) {
561 std::string spirv = R"(
562 OpCapability Shader
563 OpCapability Geometry
564 OpMemoryModel Logical GLSL450
565 OpEntryPoint Geometry %main "main" %ptr_builtin_1
566 OpExecutionMode %main InputPoints
567 OpExecutionMode %main OutputPoints
568 OpMemberDecorate %struct_1 0 BuiltIn InvocationId
569 %int = OpTypeInt 32 1
570 %void = OpTypeVoid
571 %func = OpTypeFunction %void
572 %struct_1 = OpTypeStruct %int
573 %ptr_builtin_1 = OpTypePointer Input %struct_1
574 %main = OpFunction %void None %func
575 %5 = OpLabel
576 OpReturn
577 OpFunctionEnd
578 )";
579 CompileSuccessfully(spirv);
580 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
581 EXPECT_THAT(getDiagnosticString(),
582 HasSubstr("Interfaces passed to OpEntryPoint must be variables. "
583 "Found OpTypePointer."));
584 }
585
586 TEST_P(ValidateIdWithMessage, OpEntryPointInterfaceStorageClassBad) {
587 std::string spirv = R"(
588 OpCapability Shader
589 OpCapability Geometry
590 OpMemoryModel Logical GLSL450
591 OpEntryPoint Geometry %main "main" %in_1
592 OpExecutionMode %main InputPoints
593 OpExecutionMode %main OutputPoints
594 OpMemberDecorate %struct_1 0 BuiltIn InvocationId
595 %int = OpTypeInt 32 1
596 %void = OpTypeVoid
597 %func = OpTypeFunction %void
598 %struct_1 = OpTypeStruct %int
599 %ptr_builtin_1 = OpTypePointer Uniform %struct_1
600 %in_1 = OpVariable %ptr_builtin_1 Uniform
601 %main = OpFunction %void None %func
602 %5 = OpLabel
603 OpReturn
604 OpFunctionEnd
605 )";
606 CompileSuccessfully(spirv);
607 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
608 EXPECT_THAT(getDiagnosticString(),
609 HasSubstr(make_message(
610 "OpEntryPoint interfaces must be OpVariables with "
611 "Storage Class of Input(1) or Output(3). Found Storage "
612 "Class 2 for Entry Point id 1.")));
613 }
614
615 TEST_P(ValidateIdWithMessage, OpExecutionModeGood) {
616 std::string spirv = kGLSL450MemoryModel + R"(
617 OpEntryPoint GLCompute %3 ""
618 OpExecutionMode %3 LocalSize 1 1 1
619 %1 = OpTypeVoid
620 %2 = OpTypeFunction %1
621 %3 = OpFunction %1 None %2
622 %4 = OpLabel
623 OpReturn
624 OpFunctionEnd)";
625 CompileSuccessfully(spirv.c_str());
626 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
627 }
628
629 TEST_P(ValidateIdWithMessage, OpExecutionModeEntryPointMissing) {
630 std::string spirv = kGLSL450MemoryModel + R"(
631 OpExecutionMode %3 LocalSize 1 1 1
632 %1 = OpTypeVoid
633 %2 = OpTypeFunction %1
634 %3 = OpFunction %1 None %2
635 %4 = OpLabel
636 OpReturn
637 OpFunctionEnd)";
638 CompileSuccessfully(spirv.c_str());
639 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
640 EXPECT_THAT(getDiagnosticString(),
641 HasSubstr(make_message(
642 "OpExecutionMode Entry Point <id> '1[%1]' is not the "
643 "Entry Point operand of an OpEntryPoint.")));
644 }
645
646 TEST_P(ValidateIdWithMessage, OpExecutionModeEntryPointBad) {
647 std::string spirv = kGLSL450MemoryModel + R"(
648 OpEntryPoint GLCompute %3 "" %a
649 OpExecutionMode %a LocalSize 1 1 1
650 %void = OpTypeVoid
651 %ptr = OpTypePointer Input %void
652 %a = OpVariable %ptr Input
653 %2 = OpTypeFunction %void
654 %3 = OpFunction %void None %2
655 %4 = OpLabel
656 OpReturn
657 OpFunctionEnd)";
658 CompileSuccessfully(spirv.c_str());
659 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
660 EXPECT_THAT(getDiagnosticString(),
661 HasSubstr(make_message(
662 "OpExecutionMode Entry Point <id> '2[%2]' is not the "
663 "Entry Point operand of an OpEntryPoint.")));
664 }
665
666 TEST_P(ValidateIdWithMessage, OpTypeVectorFloat) {
667 std::string spirv = kGLSL450MemoryModel + R"(
668 %1 = OpTypeFloat 32
669 %2 = OpTypeVector %1 4)";
670 CompileSuccessfully(spirv.c_str());
671 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
672 }
673
674 TEST_P(ValidateIdWithMessage, OpTypeVectorInt) {
675 std::string spirv = kGLSL450MemoryModel + R"(
676 %1 = OpTypeInt 32 0
677 %2 = OpTypeVector %1 4)";
678 CompileSuccessfully(spirv.c_str());
679 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
680 }
681
682 TEST_P(ValidateIdWithMessage, OpTypeVectorUInt) {
683 std::string spirv = kGLSL450MemoryModel + R"(
684 %1 = OpTypeInt 64 0
685 %2 = OpTypeVector %1 4)";
686 CompileSuccessfully(spirv.c_str());
687 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
688 }
689
690 TEST_P(ValidateIdWithMessage, OpTypeVectorBool) {
691 std::string spirv = kGLSL450MemoryModel + R"(
692 %1 = OpTypeBool
693 %2 = OpTypeVector %1 4)";
694 CompileSuccessfully(spirv.c_str());
695 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
696 }
697
698 TEST_P(ValidateIdWithMessage, OpTypeVectorComponentTypeBad) {
699 std::string spirv = kGLSL450MemoryModel + R"(
700 %1 = OpTypeFloat 32
701 %2 = OpTypePointer UniformConstant %1
702 %3 = OpTypeVector %2 4)";
703 CompileSuccessfully(spirv.c_str());
704 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
705 EXPECT_THAT(getDiagnosticString(),
706 HasSubstr(make_message(
707 "OpTypeVector Component Type <id> "
708 "'2[%_ptr_UniformConstant_float]' is not a scalar type.")));
709 }
710
711 TEST_P(ValidateIdWithMessage, OpTypeVectorComponentTypeCanBePointerType) {
712 std::string spirv = R"(
713 OpCapability Addresses
714 OpCapability Linkage
715 OpCapability Kernel
716 OpCapability Int64
717 OpCapability GenericPointer
718 OpCapability MaskedGatherScatterINTEL
719 OpExtension "SPV_INTEL_masked_gather_scatter"
720 OpMemoryModel Physical64 OpenCL
721
722 %2 = OpTypeInt 32 0
723 %3 = OpTypePointer Generic %2
724 %4 = OpTypeVector %3 4)";
725 CompileSuccessfully(spirv.c_str());
726 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
727 }
728
729 TEST_P(ValidateIdWithMessage, OpTypeVectorColumnCountLessThanTwoBad) {
730 std::string spirv = kGLSL450MemoryModel + R"(
731 %1 = OpTypeFloat 32
732 %2 = OpTypeVector %1 1)";
733 CompileSuccessfully(spirv.c_str());
734 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
735 EXPECT_THAT(
736 getDiagnosticString(),
737 HasSubstr(make_message(
738 "Illegal number of components (1) for TypeVector\n %v1float = "
739 "OpTypeVector %float 1\n")));
740 }
741
742 TEST_P(ValidateIdWithMessage, OpTypeVectorColumnCountGreaterThanFourBad) {
743 std::string spirv = kGLSL450MemoryModel + R"(
744 %1 = OpTypeFloat 32
745 %2 = OpTypeVector %1 5)";
746 CompileSuccessfully(spirv.c_str());
747 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
748 EXPECT_THAT(
749 getDiagnosticString(),
750 HasSubstr(make_message(
751 "Illegal number of components (5) for TypeVector\n %v5float = "
752 "OpTypeVector %float 5\n")));
753 }
754
755 TEST_P(ValidateIdWithMessage, OpTypeVectorColumnCountEightWithoutVector16Bad) {
756 std::string spirv = kGLSL450MemoryModelWithoutVector16 + R"(
757 %1 = OpTypeFloat 32
758 %2 = OpTypeVector %1 8)";
759
760 CompileSuccessfully(spirv.c_str());
761 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
762 EXPECT_THAT(getDiagnosticString(),
763 HasSubstr(make_message(
764 "Having 8 components for TypeVector requires the Vector16 "
765 "capability\n %v8float = OpTypeVector %float 8\n")));
766 }
767
768 TEST_P(ValidateIdWithMessage,
769 OpTypeVectorColumnCountSixteenWithoutVector16Bad) {
770 std::string spirv = kGLSL450MemoryModelWithoutVector16 + R"(
771 %1 = OpTypeFloat 32
772 %2 = OpTypeVector %1 16)";
773
774 CompileSuccessfully(spirv.c_str());
775 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
776 EXPECT_THAT(getDiagnosticString(),
777 HasSubstr(make_message(
778 "Having 16 components for TypeVector requires the Vector16 "
779 "capability\n %v16float = OpTypeVector %float 16\n")));
780 }
781
782 TEST_P(ValidateIdWithMessage, OpTypeVectorColumnCountOfEightWithVector16Good) {
783 std::string spirv = kGLSL450MemoryModel + R"(
784 %1 = OpTypeFloat 32
785 %2 = OpTypeVector %1 8)";
786 CompileSuccessfully(spirv.c_str());
787 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
788 }
789
790 TEST_P(ValidateIdWithMessage,
791 OpTypeVectorColumnCountOfSixteenWithVector16Good) {
792 std::string spirv = kGLSL450MemoryModel + R"(
793 %1 = OpTypeFloat 32
794 %2 = OpTypeVector %1 16)";
795 CompileSuccessfully(spirv.c_str());
796 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
797 }
798
799 TEST_P(ValidateIdWithMessage, OpTypeMatrixGood) {
800 std::string spirv = kGLSL450MemoryModel + R"(
801 %1 = OpTypeFloat 32
802 %2 = OpTypeVector %1 2
803 %3 = OpTypeMatrix %2 3)";
804 CompileSuccessfully(spirv.c_str());
805 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
806 }
807
808 TEST_P(ValidateIdWithMessage, OpTypeMatrixColumnTypeNonVectorBad) {
809 std::string spirv = kGLSL450MemoryModel + R"(
810 %1 = OpTypeFloat 32
811 %2 = OpTypeMatrix %1 3)";
812 CompileSuccessfully(spirv.c_str());
813 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
814 EXPECT_THAT(getDiagnosticString(),
815 HasSubstr(make_message(
816 "olumns in a matrix must be of type vector.\n %mat3float = "
817 "OpTypeMatrix %float 3\n")));
818 }
819
820 TEST_P(ValidateIdWithMessage, OpTypeMatrixVectorTypeNonFloatBad) {
821 std::string spirv = kGLSL450MemoryModel + R"(
822 %1 = OpTypeInt 16 0
823 %2 = OpTypeVector %1 2
824 %3 = OpTypeMatrix %2 2)";
825 CompileSuccessfully(spirv.c_str());
826 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
827 EXPECT_THAT(getDiagnosticString(),
828 HasSubstr(make_message(
829 "Matrix types can only be parameterized with floating-point "
830 "types.\n %mat2v2ushort = OpTypeMatrix %v2ushort 2\n")));
831 }
832
833 TEST_P(ValidateIdWithMessage, OpTypeMatrixColumnCountLessThanTwoBad) {
834 std::string spirv = kGLSL450MemoryModel + R"(
835 %1 = OpTypeFloat 32
836 %2 = OpTypeVector %1 2
837 %3 = OpTypeMatrix %2 1)";
838 CompileSuccessfully(spirv.c_str());
839 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
840 EXPECT_THAT(
841 getDiagnosticString(),
842 HasSubstr(make_message(
843 "Matrix types can only be parameterized as having only 2, 3, "
844 "or 4 columns.\n %mat1v2float = OpTypeMatrix %v2float 1\n")));
845 }
846
847 TEST_P(ValidateIdWithMessage, OpTypeMatrixColumnCountGreaterThanFourBad) {
848 std::string spirv = kGLSL450MemoryModel + R"(
849 %1 = OpTypeFloat 32
850 %2 = OpTypeVector %1 2
851 %3 = OpTypeMatrix %2 8)";
852 CompileSuccessfully(spirv.c_str());
853 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
854 EXPECT_THAT(
855 getDiagnosticString(),
856 HasSubstr(make_message(
857 "Matrix types can only be parameterized as having only 2, 3, "
858 "or 4 columns.\n %mat8v2float = OpTypeMatrix %v2float 8\n")));
859 }
860
861 TEST_P(ValidateIdWithMessage, OpTypeSamplerGood) {
862 // In Rev31, OpTypeSampler takes no arguments.
863 std::string spirv = kGLSL450MemoryModel + R"(
864 %s = OpTypeSampler)";
865 CompileSuccessfully(spirv.c_str());
866 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
867 }
868
869 TEST_P(ValidateIdWithMessage, OpTypeArrayGood) {
870 std::string spirv = kGLSL450MemoryModel + R"(
871 %1 = OpTypeInt 32 0
872 %2 = OpConstant %1 1
873 %3 = OpTypeArray %1 %2)";
874 CompileSuccessfully(spirv.c_str());
875 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
876 }
877
878 TEST_P(ValidateIdWithMessage, OpTypeArrayElementTypeBad) {
879 std::string spirv = kGLSL450MemoryModel + R"(
880 %1 = OpTypeInt 32 0
881 %2 = OpConstant %1 1
882 %3 = OpTypeArray %2 %2)";
883 CompileSuccessfully(spirv.c_str());
884 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
885 EXPECT_THAT(getDiagnosticString(),
886 HasSubstr(make_message(
887 "OpTypeArray Element Type <id> '2[%uint_1]' is not a "
888 "type.")));
889 }
890
891 // Signed or unsigned.
892 enum Signed { kSigned, kUnsigned };
893
894 // Creates an assembly module declaring OpTypeArray with the given length.
895 std::string MakeArrayLength(const std::string& len, Signed isSigned, int width,
896 int max_int_width = 64,
897 bool use_vulkan_memory_model = false) {
898 std::ostringstream ss;
899 ss << R"(
900 OpCapability Shader
901 )";
902 if (use_vulkan_memory_model) {
903 ss << " OpCapability VulkanMemoryModel\n";
904 }
905 if (width == 16) {
906 ss << " OpCapability Int16\n";
907 }
908 if (max_int_width > 32) {
909 ss << "\n OpCapability Int64\n";
910 }
911 if (use_vulkan_memory_model) {
912 ss << " OpExtension \"SPV_KHR_vulkan_memory_model\"\n";
913 ss << "OpMemoryModel Logical Vulkan\n";
914 } else {
915 ss << "OpMemoryModel Logical GLSL450\n";
916 }
917 ss << "OpEntryPoint GLCompute %main \"main\"\n";
918 ss << "OpExecutionMode %main LocalSize 1 1 1\n";
919 ss << " %t = OpTypeInt " << width << (isSigned == kSigned ? " 1" : " 0");
920 ss << " %l = OpConstant %t " << len;
921 ss << " %a = OpTypeArray %t %l";
922 ss << " %void = OpTypeVoid \n"
923 " %voidfn = OpTypeFunction %void \n"
924 " %main = OpFunction %void None %voidfn \n"
925 " %entry = OpLabel\n"
926 " OpReturn\n"
927 " OpFunctionEnd\n";
928 return ss.str();
929 }
930
931 // Tests OpTypeArray. Parameter is the width (in bits) of the array-length's
932 // type.
933 class OpTypeArrayLengthTest
934 : public spvtest::TextToBinaryTestBase<::testing::TestWithParam<int>> {
935 protected:
936 OpTypeArrayLengthTest()
937 : env_(SPV_ENV_UNIVERSAL_1_0),
938 position_(spv_position_t{0, 0, 0}),
939 diagnostic_(spvDiagnosticCreate(&position_, "")) {}
940
941 ~OpTypeArrayLengthTest() override { spvDiagnosticDestroy(diagnostic_); }
942
943 // Runs spvValidate() on v, printing any errors via spvDiagnosticPrint().
944 spv_result_t Val(const SpirvVector& v, const std::string& expected_err = "") {
945 spv_const_binary_t cbinary{v.data(), v.size()};
946 spvDiagnosticDestroy(diagnostic_);
947 diagnostic_ = nullptr;
948 const auto status =
949 spvValidate(ScopedContext(env_).context, &cbinary, &diagnostic_);
950 if (status != SPV_SUCCESS) {
951 spvDiagnosticPrint(diagnostic_);
952 EXPECT_THAT(std::string(diagnostic_->error),
953 testing::ContainsRegex(expected_err));
954 }
955 return status;
956 }
957
958 protected:
959 spv_target_env env_;
960
961 private:
962 spv_position_t position_; // For creating diagnostic_.
963 spv_diagnostic diagnostic_;
964 };
965
966 TEST_P(OpTypeArrayLengthTest, LengthPositiveSmall) {
967 const int width = GetParam();
968 EXPECT_EQ(SPV_SUCCESS,
969 Val(CompileSuccessfully(MakeArrayLength("1", kSigned, width))));
970 EXPECT_EQ(SPV_SUCCESS,
971 Val(CompileSuccessfully(MakeArrayLength("1", kUnsigned, width))));
972 EXPECT_EQ(SPV_SUCCESS,
973 Val(CompileSuccessfully(MakeArrayLength("2", kSigned, width))));
974 EXPECT_EQ(SPV_SUCCESS,
975 Val(CompileSuccessfully(MakeArrayLength("2", kUnsigned, width))));
976 EXPECT_EQ(SPV_SUCCESS,
977 Val(CompileSuccessfully(MakeArrayLength("55", kSigned, width))));
978 EXPECT_EQ(SPV_SUCCESS,
979 Val(CompileSuccessfully(MakeArrayLength("55", kUnsigned, width))));
980 const std::string fpad(width / 4 - 1, 'F');
981 EXPECT_EQ(
982 SPV_SUCCESS,
983 Val(CompileSuccessfully(MakeArrayLength("0x7" + fpad, kSigned, width))))
984 << MakeArrayLength("0x7" + fpad, kSigned, width);
985 }
986
987 TEST_P(OpTypeArrayLengthTest, LengthZero) {
988 const int width = GetParam();
989 EXPECT_EQ(SPV_ERROR_INVALID_ID,
990 Val(CompileSuccessfully(MakeArrayLength("0", kSigned, width)),
991 "OpTypeArray Length <id> '3\\[%.*\\]' default value must be at "
992 "least 1."));
993 EXPECT_EQ(SPV_ERROR_INVALID_ID,
994 Val(CompileSuccessfully(MakeArrayLength("0", kUnsigned, width)),
995 "OpTypeArray Length <id> '3\\[%.*\\]' default value must be at "
996 "least 1."));
997 }
998
999 TEST_P(OpTypeArrayLengthTest, LengthNegative) {
1000 const int width = GetParam();
1001 EXPECT_EQ(SPV_ERROR_INVALID_ID,
1002 Val(CompileSuccessfully(MakeArrayLength("-1", kSigned, width)),
1003 "OpTypeArray Length <id> '3\\[%.*\\]' default value must be at "
1004 "least 1."));
1005 EXPECT_EQ(SPV_ERROR_INVALID_ID,
1006 Val(CompileSuccessfully(MakeArrayLength("-2", kSigned, width)),
1007 "OpTypeArray Length <id> '3\\[%.*\\]' default value must be at "
1008 "least 1."));
1009 EXPECT_EQ(SPV_ERROR_INVALID_ID,
1010 Val(CompileSuccessfully(MakeArrayLength("-123", kSigned, width)),
1011 "OpTypeArray Length <id> '3\\[%.*\\]' default value must be at "
1012 "least 1."));
1013 const std::string neg_max = "0x8" + std::string(width / 4 - 1, '0');
1014 EXPECT_EQ(SPV_ERROR_INVALID_ID,
1015 Val(CompileSuccessfully(MakeArrayLength(neg_max, kSigned, width)),
1016 "OpTypeArray Length <id> '3\\[%.*\\]' default value must be at "
1017 "least 1."));
1018 }
1019
1020 // Returns the string form of an integer of the form 0x80....0 of the
1021 // given bit width.
1022 std::string big_num_ending_0(int bit_width) {
1023 return "0x8" + std::string(bit_width / 4 - 1, '0');
1024 }
1025
1026 // Returns the string form of an integer of the form 0x80..001 of the
1027 // given bit width.
1028 std::string big_num_ending_1(int bit_width) {
1029 return "0x8" + std::string(bit_width / 4 - 2, '0') + "1";
1030 }
1031
1032 TEST_P(OpTypeArrayLengthTest, LengthPositiveHugeEnding0InVulkan) {
1033 env_ = SPV_ENV_VULKAN_1_0;
1034 const int width = GetParam();
1035 for (int max_int_width : {32, 64}) {
1036 if (width > max_int_width) {
1037 // Not valid to even make the OpConstant in this case.
1038 continue;
1039 }
1040 const auto module = CompileSuccessfully(MakeArrayLength(
1041 big_num_ending_0(width), kUnsigned, width, max_int_width));
1042 EXPECT_EQ(SPV_SUCCESS, Val(module));
1043 }
1044 }
1045
1046 TEST_P(OpTypeArrayLengthTest, LengthPositiveHugeEnding1InVulkan) {
1047 env_ = SPV_ENV_VULKAN_1_0;
1048 const int width = GetParam();
1049 for (int max_int_width : {32, 64}) {
1050 if (width > max_int_width) {
1051 // Not valid to even make the OpConstant in this case.
1052 continue;
1053 }
1054 const auto module = CompileSuccessfully(MakeArrayLength(
1055 big_num_ending_1(width), kUnsigned, width, max_int_width));
1056 EXPECT_EQ(SPV_SUCCESS, Val(module));
1057 }
1058 }
1059
1060 // The only valid widths for integers are 8, 16, 32, and 64.
1061 // Since the Int8 capability requires the Kernel capability, and the Kernel
1062 // capability prohibits usage of signed integers, we can skip 8-bit integers
1063 // here since the purpose of these tests is to check the validity of
1064 // OpTypeArray, not OpTypeInt.
1065 INSTANTIATE_TEST_SUITE_P(Widths, OpTypeArrayLengthTest,
1066 ValuesIn(std::vector<int>{16, 32, 64}));
1067
1068 TEST_P(ValidateIdWithMessage, OpTypeArrayLengthNull) {
1069 std::string spirv = kGLSL450MemoryModel + R"(
1070 %i32 = OpTypeInt 32 0
1071 %len = OpConstantNull %i32
1072 %ary = OpTypeArray %i32 %len)";
1073 CompileSuccessfully(spirv.c_str());
1074 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1075 EXPECT_THAT(getDiagnosticString(),
1076 HasSubstr(make_message("OpTypeArray Length <id> '2[%2]' default "
1077 "value must be at least 1: found 0")));
1078 }
1079
1080 TEST_P(ValidateIdWithMessage, OpTypeArrayLengthSpecConst) {
1081 std::string spirv = kGLSL450MemoryModel + R"(
1082 %i32 = OpTypeInt 32 0
1083 %len = OpSpecConstant %i32 2
1084 %ary = OpTypeArray %i32 %len)";
1085 CompileSuccessfully(spirv.c_str());
1086 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1087 }
1088
1089 TEST_P(ValidateIdWithMessage, OpTypeArrayLengthSpecConstOp) {
1090 std::string spirv = kGLSL450MemoryModel + R"(
1091 %i32 = OpTypeInt 32 0
1092 %c1 = OpConstant %i32 1
1093 %c2 = OpConstant %i32 2
1094 %len = OpSpecConstantOp %i32 IAdd %c1 %c2
1095 %ary = OpTypeArray %i32 %len)";
1096 CompileSuccessfully(spirv.c_str());
1097 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1098 }
1099
1100 TEST_P(ValidateIdWithMessage, OpTypeRuntimeArrayGood) {
1101 std::string spirv = kGLSL450MemoryModel + R"(
1102 %1 = OpTypeInt 32 0
1103 %2 = OpTypeRuntimeArray %1)";
1104 CompileSuccessfully(spirv.c_str());
1105 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1106 }
1107 TEST_P(ValidateIdWithMessage, OpTypeRuntimeArrayBad) {
1108 std::string spirv = kGLSL450MemoryModel + R"(
1109 %1 = OpTypeInt 32 0
1110 %2 = OpConstant %1 0
1111 %3 = OpTypeRuntimeArray %2)";
1112 CompileSuccessfully(spirv.c_str());
1113 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1114 EXPECT_THAT(getDiagnosticString(),
1115 HasSubstr(make_message(
1116 "OpTypeRuntimeArray Element Type <id> '2[%uint_0]' is not a "
1117 "type.")));
1118 }
1119 // TODO: Object of this type can only be created with OpVariable using the
1120 // Uniform Storage Class
1121
1122 TEST_P(ValidateIdWithMessage, OpTypeStructGood) {
1123 std::string spirv = kGLSL450MemoryModel + R"(
1124 %1 = OpTypeInt 32 0
1125 %2 = OpTypeFloat 64
1126 %3 = OpTypePointer Input %1
1127 %4 = OpTypeStruct %1 %2 %3)";
1128 CompileSuccessfully(spirv.c_str());
1129 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1130 }
1131 TEST_P(ValidateIdWithMessage, OpTypeStructMemberTypeBad) {
1132 std::string spirv = kGLSL450MemoryModel + R"(
1133 %1 = OpTypeInt 32 0
1134 %2 = OpTypeFloat 64
1135 %3 = OpConstant %2 0.0
1136 %4 = OpTypeStruct %1 %2 %3)";
1137 CompileSuccessfully(spirv.c_str());
1138 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1139 EXPECT_THAT(getDiagnosticString(),
1140 HasSubstr(make_message(
1141 "OpTypeStruct Member Type <id> '3[%double_0]' is not "
1142 "a type.")));
1143 }
1144
1145 TEST_P(ValidateIdWithMessage, OpTypeStructOpaqueTypeBad) {
1146 std::string spirv = R"(
1147 OpCapability Shader
1148 OpMemoryModel Logical GLSL450
1149 OpEntryPoint Vertex %main "main"
1150 %1 = OpTypeSampler
1151 %2 = OpTypeStruct %1
1152 %void = OpTypeVoid
1153 %3 = OpTypeFunction %void
1154 %main = OpFunction %void None %3
1155 %5 = OpLabel
1156 OpReturn
1157 OpFunctionEnd
1158 )";
1159 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_0);
1160 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
1161 EXPECT_THAT(getDiagnosticString(),
1162 AnyVUID("VUID-StandaloneSpirv-None-04667"));
1163 EXPECT_THAT(getDiagnosticString(),
1164 HasSubstr(make_message(
1165 "OpTypeStruct must not contain an invalid opaque type")));
1166 }
1167
1168 TEST_P(ValidateIdWithMessage, OpTypePointerGood) {
1169 std::string spirv = kGLSL450MemoryModel + R"(
1170 %1 = OpTypeInt 32 0
1171 %2 = OpTypePointer Input %1)";
1172 CompileSuccessfully(spirv.c_str());
1173 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1174 }
1175 TEST_P(ValidateIdWithMessage, OpTypePointerBad) {
1176 std::string spirv = kGLSL450MemoryModel + R"(
1177 %1 = OpTypeInt 32 0
1178 %2 = OpConstant %1 0
1179 %3 = OpTypePointer Input %2)";
1180 CompileSuccessfully(spirv.c_str());
1181 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1182 EXPECT_THAT(
1183 getDiagnosticString(),
1184 HasSubstr(make_message("OpTypePointer Type <id> '2[%uint_0]' is not a "
1185 "type.")));
1186 }
1187
1188 TEST_P(ValidateIdWithMessage, OpTypePointerCanHaveUntypedPointer) {
1189 const std::string spirv = R"(
1190 OpCapability Kernel
1191 OpCapability Linkage
1192 OpCapability UntypedPointersKHR
1193 OpCapability WorkgroupMemoryExplicitLayoutKHR
1194 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
1195 OpExtension "SPV_KHR_untyped_pointers"
1196 OpMemoryModel Logical OpenCL
1197 %ptr = OpTypeUntypedPointerKHR Workgroup
1198 %ptr2 = OpTypePointer Private %ptr
1199 )";
1200
1201 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
1202 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
1203 }
1204
1205 TEST_P(ValidateIdWithMessage, OpTypeUntypedPointerWorkgroupGood) {
1206 const std::string spirv = R"(
1207 OpCapability Shader
1208 OpCapability Linkage
1209 OpCapability UntypedPointersKHR
1210 OpCapability WorkgroupMemoryExplicitLayoutKHR
1211 OpExtension "SPV_KHR_workgroup_memory_explicit_layout"
1212 OpExtension "SPV_KHR_untyped_pointers"
1213 OpMemoryModel Logical GLSL450
1214 %ptr = OpTypeUntypedPointerKHR Workgroup
1215 )";
1216
1217 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_4);
1218 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
1219 }
1220
1221 TEST_P(ValidateIdWithMessage,
1222 OpTypeUntypedPointerWorkgroupMissingExplicitLayout) {
1223 const std::string spirv = R"(
1224 OpCapability Shader
1225 OpCapability UntypedPointersKHR
1226 OpExtension "SPV_KHR_untyped_pointers"
1227 OpMemoryModel Logical GLSL450
1228 OpEntryPoint GLCompute %main "main"
1229 OpExecutionMode %main LocalSize 1 1 1
1230 %ptr = OpTypeUntypedPointerKHR Workgroup
1231 %void = OpTypeVoid
1232 %void_fn = OpTypeFunction %void
1233 %main = OpFunction %void None %void_fn
1234 %entry = OpLabel
1235 OpReturn
1236 OpFunctionEnd
1237 )";
1238
1239 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
1240 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
1241 EXPECT_THAT(
1242 getDiagnosticString(),
1243 HasSubstr("Workgroup storage class untyped pointers in Vulkan require "
1244 "WorkgroupMemoryExplicitLayoutKHR be declared"));
1245 }
1246
1247 TEST_P(ValidateIdWithMessage, OpTypeUntypedPointerWorkgroupGoodAll) {
1248 const std::string spirv = R"(
1249 OpCapability Shader
1250 OpCapability Linkage
1251 OpCapability UntypedPointersKHR
1252 OpExtension "SPV_KHR_untyped_pointers"
1253 OpMemoryModel Logical GLSL450
1254 %ptr = OpTypeUntypedPointerKHR Workgroup
1255 )";
1256
1257 CompileSuccessfully(spirv);
1258 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1259 }
1260
1261 TEST_P(ValidateIdWithMessage, OpTypeUntypedPointerStorageBufferGood) {
1262 const std::string spirv = R"(
1263 OpCapability Shader
1264 OpCapability Linkage
1265 OpCapability UntypedPointersKHR
1266 OpExtension "SPV_KHR_storage_buffer_storage_class"
1267 OpExtension "SPV_KHR_untyped_pointers"
1268 OpMemoryModel Logical GLSL450
1269 %ptr = OpTypeUntypedPointerKHR StorageBuffer
1270 )";
1271
1272 CompileSuccessfully(spirv);
1273 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1274 }
1275
1276 TEST_P(ValidateIdWithMessage, OpTypeUntypedPointerUniformGood) {
1277 const std::string spirv = R"(
1278 OpCapability Shader
1279 OpCapability Linkage
1280 OpCapability UntypedPointersKHR
1281 OpExtension "SPV_KHR_untyped_pointers"
1282 OpMemoryModel Logical GLSL450
1283 %ptr = OpTypeUntypedPointerKHR Uniform
1284 )";
1285
1286 CompileSuccessfully(spirv);
1287 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1288 }
1289
1290 TEST_P(ValidateIdWithMessage, OpTypeUntypedPointerPushConstantGood) {
1291 const std::string spirv = R"(
1292 OpCapability Shader
1293 OpCapability Linkage
1294 OpCapability UntypedPointersKHR
1295 OpExtension "SPV_KHR_untyped_pointers"
1296 OpMemoryModel Logical GLSL450
1297 %ptr = OpTypeUntypedPointerKHR PushConstant
1298 )";
1299
1300 CompileSuccessfully(spirv);
1301 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1302 }
1303
1304 TEST_P(ValidateIdWithMessage, OpTypeUntypedPointerCrossWorkgroupGood) {
1305 const std::string spirv = R"(
1306 OpCapability Kernel
1307 OpCapability Linkage
1308 OpCapability UntypedPointersKHR
1309 OpExtension "SPV_KHR_untyped_pointers"
1310 OpMemoryModel Logical OpenCL
1311 %ptr = OpTypeUntypedPointerKHR CrossWorkgroup
1312 )";
1313
1314 CompileSuccessfully(spirv);
1315 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1316 }
1317
1318 TEST_P(ValidateIdWithMessage, OpTypeUntypedPointerVulkanInvalidStorageClass) {
1319 const std::string spirv = R"(
1320 OpCapability Shader
1321 OpCapability UntypedPointersKHR
1322 OpExtension "SPV_KHR_untyped_pointers"
1323 OpMemoryModel Logical GLSL450
1324 OpEntryPoint GLCompute %main "main"
1325 OpExecutionMode %main LocalSize 1 1 1
1326 %void = OpTypeVoid
1327 %void_fn = OpTypeFunction %void
1328 %ptr = OpTypeUntypedPointerKHR Private
1329 %main = OpFunction %void None %void_fn
1330 %entry = OpLabel
1331 OpReturn
1332 OpFunctionEnd
1333 )";
1334
1335 CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_0);
1336 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
1337 EXPECT_THAT(getDiagnosticString(),
1338 HasSubstr("In Vulkan, untyped pointers can only be used in an "
1339 "explicitly laid out storage class"));
1340 }
1341
1342 TEST_P(ValidateIdWithMessage, OpTypeFunctionGood) {
1343 std::string spirv = kGLSL450MemoryModel + R"(
1344 %1 = OpTypeVoid
1345 %2 = OpTypeFunction %1)";
1346 CompileSuccessfully(spirv.c_str());
1347 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1348 }
1349 TEST_P(ValidateIdWithMessage, OpTypeFunctionReturnTypeBad) {
1350 std::string spirv = kGLSL450MemoryModel + R"(
1351 %1 = OpTypeInt 32 0
1352 %2 = OpConstant %1 0
1353 %3 = OpTypeFunction %2)";
1354 CompileSuccessfully(spirv.c_str());
1355 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1356 EXPECT_THAT(getDiagnosticString(),
1357 HasSubstr(make_message(
1358 "OpTypeFunction Return Type <id> '2[%uint_0]' is not "
1359 "a type.")));
1360 }
1361 TEST_P(ValidateIdWithMessage, OpTypeFunctionParameterBad) {
1362 std::string spirv = kGLSL450MemoryModel + R"(
1363 %1 = OpTypeVoid
1364 %2 = OpTypeInt 32 0
1365 %3 = OpConstant %2 0
1366 %4 = OpTypeFunction %1 %2 %3)";
1367 CompileSuccessfully(spirv.c_str());
1368 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1369 EXPECT_THAT(getDiagnosticString(),
1370 HasSubstr(make_message(
1371 "OpTypeFunction Parameter Type <id> '3[%uint_0]' is not a "
1372 "type.")));
1373 }
1374
1375 TEST_P(ValidateIdWithMessage, OpTypeFunctionParameterTypeVoidBad) {
1376 std::string spirv = kGLSL450MemoryModel + R"(
1377 %1 = OpTypeVoid
1378 %2 = OpTypeInt 32 0
1379 %4 = OpTypeFunction %1 %2 %1)";
1380 CompileSuccessfully(spirv.c_str());
1381 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1382 EXPECT_THAT(getDiagnosticString(),
1383 HasSubstr(make_message(
1384 "OpTypeFunction Parameter Type <id> '1[%void]' cannot "
1385 "be OpTypeVoid.")));
1386 }
1387
1388 TEST_P(ValidateIdWithMessage, OpTypePipeGood) {
1389 std::string spirv = kGLSL450MemoryModel + R"(
1390 %1 = OpTypeFloat 32
1391 %2 = OpTypeVector %1 16
1392 %3 = OpTypePipe ReadOnly)";
1393 CompileSuccessfully(spirv.c_str());
1394 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1395 }
1396
1397 TEST_P(ValidateIdWithMessage, OpConstantTrueGood) {
1398 std::string spirv = kGLSL450MemoryModel + R"(
1399 %1 = OpTypeBool
1400 %2 = OpConstantTrue %1)";
1401 CompileSuccessfully(spirv.c_str());
1402 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1403 }
1404 TEST_P(ValidateIdWithMessage, OpConstantTrueBad) {
1405 std::string spirv = kGLSL450MemoryModel + R"(
1406 %1 = OpTypeVoid
1407 %2 = OpConstantTrue %1)";
1408 CompileSuccessfully(spirv.c_str());
1409 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1410 EXPECT_THAT(getDiagnosticString(),
1411 HasSubstr(make_message(
1412 "OpConstantTrue Result Type <id> '1[%void]' is not a boolean "
1413 "type.")));
1414 }
1415
1416 TEST_P(ValidateIdWithMessage, OpConstantFalseGood) {
1417 std::string spirv = kGLSL450MemoryModel + R"(
1418 %1 = OpTypeBool
1419 %2 = OpConstantTrue %1)";
1420 CompileSuccessfully(spirv.c_str());
1421 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1422 }
1423 TEST_P(ValidateIdWithMessage, OpConstantFalseBad) {
1424 std::string spirv = kGLSL450MemoryModel + R"(
1425 %1 = OpTypeVoid
1426 %2 = OpConstantFalse %1)";
1427 CompileSuccessfully(spirv.c_str());
1428 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1429 EXPECT_THAT(
1430 getDiagnosticString(),
1431 HasSubstr(make_message(
1432 "OpConstantFalse Result Type <id> '1[%void]' is not a boolean "
1433 "type.")));
1434 }
1435
1436 TEST_P(ValidateIdWithMessage, OpConstantGood) {
1437 std::string spirv = kGLSL450MemoryModel + R"(
1438 %1 = OpTypeInt 32 0
1439 %2 = OpConstant %1 1)";
1440 CompileSuccessfully(spirv.c_str());
1441 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1442 }
1443 TEST_P(ValidateIdWithMessage, OpConstantBad) {
1444 std::string spirv = kGLSL450MemoryModel + R"(
1445 %1 = OpTypeVoid
1446 %2 = OpConstant !1 !0)";
1447 // The expected failure code is implementation dependent (currently
1448 // INVALID_BINARY because the binary parser catches these cases) and may
1449 // change over time, but this must always fail.
1450 CompileSuccessfully(spirv.c_str());
1451 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions());
1452 }
1453
1454 TEST_P(ValidateIdWithMessage, OpConstantCompositeVectorGood) {
1455 std::string spirv = kGLSL450MemoryModel + R"(
1456 %1 = OpTypeFloat 32
1457 %2 = OpTypeVector %1 4
1458 %3 = OpConstant %1 3.14
1459 %4 = OpConstantComposite %2 %3 %3 %3 %3)";
1460 CompileSuccessfully(spirv.c_str());
1461 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1462 }
1463 TEST_P(ValidateIdWithMessage, OpConstantCompositeVectorWithUndefGood) {
1464 std::string spirv = kGLSL450MemoryModel + R"(
1465 %1 = OpTypeFloat 32
1466 %2 = OpTypeVector %1 4
1467 %3 = OpConstant %1 3.14
1468 %9 = OpUndef %1
1469 %4 = OpConstantComposite %2 %3 %3 %3 %9)";
1470 CompileSuccessfully(spirv.c_str());
1471 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1472 }
1473 TEST_P(ValidateIdWithMessage, OpConstantCompositeVectorResultTypeBad) {
1474 std::string spirv = kGLSL450MemoryModel + R"(
1475 %1 = OpTypeFloat 32
1476 %2 = OpTypeVector %1 4
1477 %3 = OpConstant %1 3.14
1478 %4 = OpConstantComposite %1 %3 %3 %3 %3)";
1479 CompileSuccessfully(spirv.c_str());
1480 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1481 EXPECT_THAT(getDiagnosticString(),
1482 HasSubstr(make_message(
1483 "OpConstantComposite Result Type <id> '1[%float]' is not a "
1484 "composite type.")));
1485 }
1486 TEST_P(ValidateIdWithMessage, OpConstantCompositeVectorConstituentTypeBad) {
1487 std::string spirv = kGLSL450MemoryModel + R"(
1488 %1 = OpTypeFloat 32
1489 %2 = OpTypeVector %1 4
1490 %4 = OpTypeInt 32 0
1491 %3 = OpConstant %1 3.14
1492 %5 = OpConstant %4 42 ; bad type for constant value
1493 %6 = OpConstantComposite %2 %3 %5 %3 %3)";
1494 CompileSuccessfully(spirv.c_str());
1495 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1496 EXPECT_THAT(getDiagnosticString(),
1497 HasSubstr(make_message(
1498 "OpConstantComposite Constituent <id> '5[%uint_42]'s type "
1499 "does not match Result Type <id> '2[%v4float]'s vector "
1500 "element type.")));
1501 }
1502 TEST_P(ValidateIdWithMessage,
1503 OpConstantCompositeVectorConstituentUndefTypeBad) {
1504 std::string spirv = kGLSL450MemoryModel + R"(
1505 %1 = OpTypeFloat 32
1506 %2 = OpTypeVector %1 4
1507 %4 = OpTypeInt 32 0
1508 %3 = OpConstant %1 3.14
1509 %5 = OpUndef %4 ; bad type for undef value
1510 %6 = OpConstantComposite %2 %3 %5 %3 %3)";
1511 CompileSuccessfully(spirv.c_str());
1512 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1513 EXPECT_THAT(
1514 getDiagnosticString(),
1515 HasSubstr(make_message(
1516 "OpConstantComposite Constituent <id> '5[%5]'s type does not "
1517 "match Result Type <id> '2[%v4float]'s vector element type.")));
1518 }
1519 TEST_P(ValidateIdWithMessage, OpConstantCompositeMatrixGood) {
1520 std::string spirv = kGLSL450MemoryModel + R"(
1521 %1 = OpTypeFloat 32
1522 %2 = OpTypeVector %1 4
1523 %3 = OpTypeMatrix %2 4
1524 %4 = OpConstant %1 1.0
1525 %5 = OpConstant %1 0.0
1526 %6 = OpConstantComposite %2 %4 %5 %5 %5
1527 %7 = OpConstantComposite %2 %5 %4 %5 %5
1528 %8 = OpConstantComposite %2 %5 %5 %4 %5
1529 %9 = OpConstantComposite %2 %5 %5 %5 %4
1530 %10 = OpConstantComposite %3 %6 %7 %8 %9)";
1531 CompileSuccessfully(spirv.c_str());
1532 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1533 }
1534 TEST_P(ValidateIdWithMessage, OpConstantCompositeMatrixUndefGood) {
1535 std::string spirv = kGLSL450MemoryModel + R"(
1536 %1 = OpTypeFloat 32
1537 %2 = OpTypeVector %1 4
1538 %3 = OpTypeMatrix %2 4
1539 %4 = OpConstant %1 1.0
1540 %5 = OpConstant %1 0.0
1541 %6 = OpConstantComposite %2 %4 %5 %5 %5
1542 %7 = OpConstantComposite %2 %5 %4 %5 %5
1543 %8 = OpConstantComposite %2 %5 %5 %4 %5
1544 %9 = OpUndef %2
1545 %10 = OpConstantComposite %3 %6 %7 %8 %9)";
1546 CompileSuccessfully(spirv.c_str());
1547 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1548 }
1549 TEST_P(ValidateIdWithMessage, OpConstantCompositeMatrixConstituentTypeBad) {
1550 std::string spirv = kGLSL450MemoryModel + R"(
1551 %1 = OpTypeFloat 32
1552 %2 = OpTypeVector %1 4
1553 %11 = OpTypeVector %1 3
1554 %3 = OpTypeMatrix %2 4
1555 %4 = OpConstant %1 1.0
1556 %5 = OpConstant %1 0.0
1557 %6 = OpConstantComposite %2 %4 %5 %5 %5
1558 %7 = OpConstantComposite %2 %5 %4 %5 %5
1559 %8 = OpConstantComposite %2 %5 %5 %4 %5
1560 %9 = OpConstantComposite %11 %5 %5 %5
1561 %10 = OpConstantComposite %3 %6 %7 %8 %9)";
1562 CompileSuccessfully(spirv.c_str());
1563 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1564 EXPECT_THAT(getDiagnosticString(),
1565 HasSubstr(make_message(
1566 "OpConstantComposite Constituent <id> '10[%10]' vector "
1567 "component count does not match Result Type <id> "
1568 "'4[%mat4v4float]'s vector component count.")));
1569 }
1570 TEST_P(ValidateIdWithMessage,
1571 OpConstantCompositeMatrixConstituentUndefTypeBad) {
1572 std::string spirv = kGLSL450MemoryModel + R"(
1573 %1 = OpTypeFloat 32
1574 %2 = OpTypeVector %1 4
1575 %11 = OpTypeVector %1 3
1576 %3 = OpTypeMatrix %2 4
1577 %4 = OpConstant %1 1.0
1578 %5 = OpConstant %1 0.0
1579 %6 = OpConstantComposite %2 %4 %5 %5 %5
1580 %7 = OpConstantComposite %2 %5 %4 %5 %5
1581 %8 = OpConstantComposite %2 %5 %5 %4 %5
1582 %9 = OpUndef %11
1583 %10 = OpConstantComposite %3 %6 %7 %8 %9)";
1584 CompileSuccessfully(spirv.c_str());
1585 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1586 EXPECT_THAT(getDiagnosticString(),
1587 HasSubstr(make_message(
1588 "OpConstantComposite Constituent <id> '10[%10]' vector "
1589 "component count does not match Result Type <id> "
1590 "'4[%mat4v4float]'s vector component count.")));
1591 }
1592 TEST_P(ValidateIdWithMessage, OpConstantCompositeArrayGood) {
1593 std::string spirv = kGLSL450MemoryModel + R"(
1594 %1 = OpTypeInt 32 0
1595 %2 = OpConstant %1 4
1596 %3 = OpTypeArray %1 %2
1597 %4 = OpConstantComposite %3 %2 %2 %2 %2)";
1598 CompileSuccessfully(spirv.c_str());
1599 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1600 }
1601 TEST_P(ValidateIdWithMessage, OpConstantCompositeArrayWithUndefGood) {
1602 std::string spirv = kGLSL450MemoryModel + R"(
1603 %1 = OpTypeInt 32 0
1604 %2 = OpConstant %1 4
1605 %9 = OpUndef %1
1606 %3 = OpTypeArray %1 %2
1607 %4 = OpConstantComposite %3 %2 %2 %2 %9)";
1608 CompileSuccessfully(spirv.c_str());
1609 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1610 }
1611
1612 TEST_P(ValidateIdWithMessage, OpConstantCompositeArrayConstConstituentTypeBad) {
1613 std::string spirv = kGLSL450MemoryModel + R"(
1614 %1 = OpTypeInt 32 0
1615 %2 = OpConstant %1 4
1616 %3 = OpTypeArray %1 %2
1617 %4 = OpConstantComposite %3 %2 %2 %2 %1)"; // Uses a type as operand
1618 CompileSuccessfully(spirv.c_str());
1619 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1620 EXPECT_THAT(getDiagnosticString(),
1621 HasSubstr(make_message("Operand '1[%uint]' cannot be a "
1622 "type")));
1623 }
1624 TEST_P(ValidateIdWithMessage, OpConstantCompositeArrayConstConstituentBad) {
1625 std::string spirv = kGLSL450MemoryModel + R"(
1626 %1 = OpTypeInt 32 0
1627 %2 = OpConstant %1 4
1628 %3 = OpTypeArray %1 %2
1629 %4 = OpTypePointer Uniform %1
1630 %5 = OpVariable %4 Uniform
1631 %6 = OpConstantComposite %3 %2 %2 %2 %5)";
1632 CompileSuccessfully(spirv.c_str());
1633 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1634 EXPECT_THAT(getDiagnosticString(),
1635 HasSubstr(make_message(
1636 "OpConstantComposite Constituent <id> '5[%5]' is not a "
1637 "constant or undef.")));
1638 }
1639 TEST_P(ValidateIdWithMessage, OpConstantCompositeArrayConstituentTypeBad) {
1640 std::string spirv = kGLSL450MemoryModel + R"(
1641 %1 = OpTypeInt 32 0
1642 %2 = OpConstant %1 4
1643 %3 = OpTypeArray %1 %2
1644 %5 = OpTypeFloat 32
1645 %6 = OpConstant %5 3.14 ; bad type for const value
1646 %4 = OpConstantComposite %3 %2 %2 %2 %6)";
1647 CompileSuccessfully(spirv.c_str());
1648 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1649 EXPECT_THAT(getDiagnosticString(),
1650 HasSubstr(make_message(
1651 "OpConstantComposite Constituent <id> "
1652 "'5[%float_3_1400001]'s type does not match Result "
1653 "Type <id> '3[%_arr_uint_uint_4]'s array element "
1654 "type.")));
1655 }
1656 TEST_P(ValidateIdWithMessage, OpConstantCompositeArrayConstituentUndefTypeBad) {
1657 std::string spirv = kGLSL450MemoryModel + R"(
1658 %1 = OpTypeInt 32 0
1659 %2 = OpConstant %1 4
1660 %3 = OpTypeArray %1 %2
1661 %5 = OpTypeFloat 32
1662 %6 = OpUndef %5 ; bad type for undef
1663 %4 = OpConstantComposite %3 %2 %2 %2 %6)";
1664 CompileSuccessfully(spirv.c_str());
1665 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1666 EXPECT_THAT(
1667 getDiagnosticString(),
1668 HasSubstr(make_message("OpConstantComposite Constituent <id> "
1669 "'5[%5]'s type does not match Result "
1670 "Type <id> '3[%_arr_uint_uint_4]'s array element "
1671 "type.")));
1672 }
1673 TEST_P(ValidateIdWithMessage, OpConstantCompositeStructGood) {
1674 std::string spirv = kGLSL450MemoryModel + R"(
1675 %1 = OpTypeInt 32 0
1676 %2 = OpTypeInt 64 0
1677 %3 = OpTypeStruct %1 %1 %2
1678 %4 = OpConstant %1 42
1679 %5 = OpConstant %2 4300000000
1680 %6 = OpConstantComposite %3 %4 %4 %5)";
1681 CompileSuccessfully(spirv.c_str());
1682 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1683 }
1684 TEST_P(ValidateIdWithMessage, OpConstantCompositeStructUndefGood) {
1685 std::string spirv = kGLSL450MemoryModel + R"(
1686 %1 = OpTypeInt 32 0
1687 %2 = OpTypeInt 64 0
1688 %3 = OpTypeStruct %1 %1 %2
1689 %4 = OpConstant %1 42
1690 %5 = OpUndef %2
1691 %6 = OpConstantComposite %3 %4 %4 %5)";
1692 CompileSuccessfully(spirv.c_str());
1693 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1694 }
1695 TEST_P(ValidateIdWithMessage, OpConstantCompositeStructMemberTypeBad) {
1696 std::string spirv = kGLSL450MemoryModel + R"(
1697 %1 = OpTypeInt 32 0
1698 %2 = OpTypeInt 64 0
1699 %3 = OpTypeStruct %1 %1 %2
1700 %4 = OpConstant %1 42
1701 %5 = OpConstant %2 4300000000
1702 %6 = OpConstantComposite %3 %4 %5 %4)";
1703 CompileSuccessfully(spirv.c_str());
1704 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1705 EXPECT_THAT(getDiagnosticString(),
1706 HasSubstr(make_message(
1707 "OpConstantComposite Constituent <id> "
1708 "'5[%ulong_4300000000]' type does not match the "
1709 "Result Type <id> '3[%_struct_3]'s member type.")));
1710 }
1711
1712 TEST_P(ValidateIdWithMessage, OpConstantCompositeStructMemberUndefTypeBad) {
1713 std::string spirv = kGLSL450MemoryModel + R"(
1714 %1 = OpTypeInt 32 0
1715 %2 = OpTypeInt 64 0
1716 %3 = OpTypeStruct %1 %1 %2
1717 %4 = OpConstant %1 42
1718 %5 = OpUndef %2
1719 %6 = OpConstantComposite %3 %4 %5 %4)";
1720 CompileSuccessfully(spirv.c_str());
1721 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1722 EXPECT_THAT(getDiagnosticString(),
1723 HasSubstr(make_message(
1724 "OpConstantComposite Constituent <id> '5[%5]' type "
1725 "does not match the Result Type <id> '3[%_struct_3]'s "
1726 "member type.")));
1727 }
1728
1729 TEST_P(ValidateIdWithMessage, OpConstantSamplerGood) {
1730 std::string spirv = kGLSL450MemoryModel + R"(
1731 %float = OpTypeFloat 32
1732 %samplerType = OpTypeSampler
1733 %3 = OpConstantSampler %samplerType ClampToEdge 0 Nearest)";
1734 CompileSuccessfully(spirv.c_str());
1735 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1736 }
1737 TEST_P(ValidateIdWithMessage, OpConstantSamplerResultTypeBad) {
1738 std::string spirv = kGLSL450MemoryModel + R"(
1739 %1 = OpTypeFloat 32
1740 %2 = OpConstantSampler %1 Clamp 0 Nearest)";
1741 CompileSuccessfully(spirv.c_str());
1742 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1743 EXPECT_THAT(
1744 getDiagnosticString(),
1745 HasSubstr(make_message(
1746 "OpConstantSampler Result Type <id> '1[%float]' is not a sampler "
1747 "type.")));
1748 }
1749
1750 TEST_P(ValidateIdWithMessage, OpConstantNullGood) {
1751 std::string spirv = kGLSL450MemoryModel + R"(
1752 %1 = OpTypeBool
1753 %2 = OpConstantNull %1
1754 %3 = OpTypeInt 32 0
1755 %4 = OpConstantNull %3
1756 %5 = OpTypeFloat 32
1757 %6 = OpConstantNull %5
1758 %7 = OpTypePointer Workgroup %3
1759 %8 = OpConstantNull %7
1760 %9 = OpTypeEvent
1761 %10 = OpConstantNull %9
1762 %11 = OpTypeDeviceEvent
1763 %12 = OpConstantNull %11
1764 %13 = OpTypeReserveId
1765 %14 = OpConstantNull %13
1766 %15 = OpTypeQueue
1767 %16 = OpConstantNull %15
1768 %17 = OpTypeVector %5 2
1769 %18 = OpConstantNull %17
1770 %19 = OpTypeMatrix %17 2
1771 %20 = OpConstantNull %19
1772 %25 = OpConstant %3 8
1773 %21 = OpTypeArray %3 %25
1774 %22 = OpConstantNull %21
1775 %23 = OpTypeStruct %3 %5 %1
1776 %24 = OpConstantNull %23
1777 %26 = OpTypeArray %17 %25
1778 %27 = OpConstantNull %26
1779 %28 = OpTypeStruct %7 %26 %26 %1
1780 %29 = OpConstantNull %28
1781 )";
1782 CompileSuccessfully(spirv.c_str());
1783 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1784 }
1785
1786 TEST_P(ValidateIdWithMessage, OpConstantNullBasicBad) {
1787 std::string spirv = kGLSL450MemoryModel + R"(
1788 %1 = OpTypeVoid
1789 %2 = OpConstantNull %1)";
1790 CompileSuccessfully(spirv.c_str());
1791 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1792 EXPECT_THAT(
1793 getDiagnosticString(),
1794 HasSubstr(make_message(
1795 "OpConstantNull Result Type <id> '1[%void]' cannot have a null "
1796 "value.")));
1797 }
1798
1799 TEST_P(ValidateIdWithMessage, OpConstantNullArrayBad) {
1800 std::string spirv = kGLSL450MemoryModel + R"(
1801 %2 = OpTypeInt 32 0
1802 %3 = OpTypeSampler
1803 %4 = OpConstant %2 4
1804 %5 = OpTypeArray %3 %4
1805 %6 = OpConstantNull %5)";
1806 CompileSuccessfully(spirv.c_str());
1807 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1808 EXPECT_THAT(
1809 getDiagnosticString(),
1810 HasSubstr(make_message(
1811 "OpConstantNull Result Type <id> '4[%_arr_2_uint_4]' cannot have a "
1812 "null value.")));
1813 }
1814
1815 TEST_P(ValidateIdWithMessage, OpConstantNullStructBad) {
1816 std::string spirv = kGLSL450MemoryModel + R"(
1817 %2 = OpTypeSampler
1818 %3 = OpTypeStruct %2 %2
1819 %4 = OpConstantNull %3)";
1820 CompileSuccessfully(spirv.c_str());
1821 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1822 EXPECT_THAT(
1823 getDiagnosticString(),
1824 HasSubstr(make_message("OpConstantNull Result Type <id> '2[%_struct_2]' "
1825 "cannot have a null value.")));
1826 }
1827
1828 TEST_P(ValidateIdWithMessage, OpConstantNullRuntimeArrayBad) {
1829 std::string spirv = kGLSL450MemoryModel + R"(
1830 %bool = OpTypeBool
1831 %array = OpTypeRuntimeArray %bool
1832 %null = OpConstantNull %array)";
1833 CompileSuccessfully(spirv.c_str());
1834 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1835 EXPECT_THAT(
1836 getDiagnosticString(),
1837 HasSubstr(make_message(
1838 "OpConstantNull Result Type <id> '2[%_runtimearr_bool]' cannot have "
1839 "a null value.")));
1840 }
1841
1842 TEST_P(ValidateIdWithMessage, OpSpecConstantTrueGood) {
1843 std::string spirv = kGLSL450MemoryModel + R"(
1844 %1 = OpTypeBool
1845 %2 = OpSpecConstantTrue %1)";
1846 CompileSuccessfully(spirv.c_str());
1847 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1848 }
1849 TEST_P(ValidateIdWithMessage, OpSpecConstantTrueBad) {
1850 std::string spirv = kGLSL450MemoryModel + R"(
1851 %1 = OpTypeVoid
1852 %2 = OpSpecConstantTrue %1)";
1853 CompileSuccessfully(spirv.c_str());
1854 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1855 EXPECT_THAT(getDiagnosticString(),
1856 HasSubstr(make_message(
1857 "OpSpecConstantTrue Result Type <id> '1[%void]' is not "
1858 "a boolean type")));
1859 }
1860
1861 TEST_P(ValidateIdWithMessage, OpSpecConstantFalseGood) {
1862 std::string spirv = kGLSL450MemoryModel + R"(
1863 %1 = OpTypeBool
1864 %2 = OpSpecConstantFalse %1)";
1865 CompileSuccessfully(spirv.c_str());
1866 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1867 }
1868 TEST_P(ValidateIdWithMessage, OpSpecConstantFalseBad) {
1869 std::string spirv = kGLSL450MemoryModel + R"(
1870 %1 = OpTypeVoid
1871 %2 = OpSpecConstantFalse %1)";
1872 CompileSuccessfully(spirv.c_str());
1873 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1874 EXPECT_THAT(getDiagnosticString(),
1875 HasSubstr(make_message(
1876 "OpSpecConstantFalse Result Type <id> '1[%void]' is not "
1877 "a boolean type")));
1878 }
1879
1880 TEST_P(ValidateIdWithMessage, OpSpecConstantGood) {
1881 std::string spirv = kGLSL450MemoryModel + R"(
1882 %1 = OpTypeFloat 32
1883 %2 = OpSpecConstant %1 42)";
1884 CompileSuccessfully(spirv.c_str());
1885 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1886 }
1887 TEST_P(ValidateIdWithMessage, OpSpecConstantBad) {
1888 std::string spirv = kGLSL450MemoryModel + R"(
1889 %1 = OpTypeVoid
1890 %2 = OpSpecConstant !1 !4)";
1891 // The expected failure code is implementation dependent (currently
1892 // INVALID_BINARY because the binary parser catches these cases) and may
1893 // change over time, but this must always fail.
1894 CompileSuccessfully(spirv.c_str());
1895 EXPECT_EQ(SPV_ERROR_INVALID_BINARY, ValidateInstructions());
1896 EXPECT_THAT(
1897 getDiagnosticString(),
1898 HasSubstr(make_message("Type Id 1 is not a scalar numeric type")));
1899 }
1900
1901 // Valid: SpecConstantComposite specializes to a vector.
1902 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeVectorGood) {
1903 std::string spirv = kGLSL450MemoryModel + R"(
1904 %1 = OpTypeFloat 32
1905 %2 = OpTypeVector %1 4
1906 %3 = OpSpecConstant %1 3.14
1907 %4 = OpConstant %1 3.14
1908 %5 = OpSpecConstantComposite %2 %3 %3 %4 %4)";
1909 CompileSuccessfully(spirv.c_str());
1910 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1911 }
1912
1913 // Valid: Vector of floats and Undefs.
1914 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeVectorWithUndefGood) {
1915 std::string spirv = kGLSL450MemoryModel + R"(
1916 %1 = OpTypeFloat 32
1917 %2 = OpTypeVector %1 4
1918 %3 = OpSpecConstant %1 3.14
1919 %5 = OpConstant %1 3.14
1920 %9 = OpUndef %1
1921 %4 = OpSpecConstantComposite %2 %3 %5 %3 %9)";
1922 CompileSuccessfully(spirv.c_str());
1923 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1924 }
1925
1926 // Invalid: result type is float.
1927 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeVectorResultTypeBad) {
1928 std::string spirv = kGLSL450MemoryModel + R"(
1929 %1 = OpTypeFloat 32
1930 %2 = OpTypeVector %1 4
1931 %3 = OpSpecConstant %1 3.14
1932 %4 = OpSpecConstantComposite %1 %3 %3 %3 %3)";
1933 CompileSuccessfully(spirv.c_str());
1934 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1935 EXPECT_THAT(getDiagnosticString(),
1936 HasSubstr(make_message("is not a composite type")));
1937 }
1938
1939 // Invalid: Vector contains a mix of Int and Float.
1940 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeVectorConstituentTypeBad) {
1941 std::string spirv = kGLSL450MemoryModel + R"(
1942 %1 = OpTypeFloat 32
1943 %2 = OpTypeVector %1 4
1944 %4 = OpTypeInt 32 0
1945 %3 = OpSpecConstant %1 3.14
1946 %5 = OpConstant %4 42 ; bad type for constant value
1947 %6 = OpSpecConstantComposite %2 %3 %5 %3 %3)";
1948 CompileSuccessfully(spirv.c_str());
1949 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1950 EXPECT_THAT(getDiagnosticString(),
1951 HasSubstr(make_message(
1952 "OpSpecConstantComposite Constituent <id> "
1953 "'5[%uint_42]'s type does not match Result Type <id> "
1954 "'2[%v4float]'s vector element type.")));
1955 }
1956
1957 // Invalid: Constituent is not a constant
1958 TEST_P(ValidateIdWithMessage,
1959 OpSpecConstantCompositeVectorConstituentNotConstantBad) {
1960 std::string spirv = kGLSL450MemoryModel + R"(
1961 %1 = OpTypeFloat 32
1962 %2 = OpTypeVector %1 4
1963 %3 = OpTypeInt 32 0
1964 %4 = OpSpecConstant %1 3.14
1965 %5 = OpTypePointer Uniform %1
1966 %6 = OpVariable %5 Uniform
1967 %7 = OpSpecConstantComposite %2 %6 %4 %4 %4)";
1968 CompileSuccessfully(spirv.c_str());
1969 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1970 EXPECT_THAT(getDiagnosticString(),
1971 HasSubstr(make_message(
1972 "OpSpecConstantComposite Constituent <id> '6[%6]' is "
1973 "not a constant or undef.")));
1974 }
1975
1976 // Invalid: Vector contains a mix of Undef-int and Float.
1977 TEST_P(ValidateIdWithMessage,
1978 OpSpecConstantCompositeVectorConstituentUndefTypeBad) {
1979 std::string spirv = kGLSL450MemoryModel + R"(
1980 %1 = OpTypeFloat 32
1981 %2 = OpTypeVector %1 4
1982 %4 = OpTypeInt 32 0
1983 %3 = OpSpecConstant %1 3.14
1984 %5 = OpUndef %4 ; bad type for undef value
1985 %6 = OpSpecConstantComposite %2 %3 %5 %3 %3)";
1986 CompileSuccessfully(spirv.c_str());
1987 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
1988 EXPECT_THAT(getDiagnosticString(),
1989 HasSubstr(make_message(
1990 "OpSpecConstantComposite Constituent <id> '5[%5]'s "
1991 "type does not match Result Type <id> '2[%v4float]'s "
1992 "vector element type.")));
1993 }
1994
1995 // Invalid: Vector expects 3 components, but 4 specified.
1996 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeVectorNumComponentsBad) {
1997 std::string spirv = kGLSL450MemoryModel + R"(
1998 %1 = OpTypeFloat 32
1999 %2 = OpTypeVector %1 3
2000 %3 = OpConstant %1 3.14
2001 %5 = OpSpecConstant %1 4.0
2002 %6 = OpSpecConstantComposite %2 %3 %5 %3 %3)";
2003 CompileSuccessfully(spirv.c_str());
2004 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2005 EXPECT_THAT(getDiagnosticString(),
2006 HasSubstr(make_message(
2007 "OpSpecConstantComposite Constituent <id> count does "
2008 "not match Result Type <id> '2[%v3float]'s vector "
2009 "component count.")));
2010 }
2011
2012 // Valid: 4x4 matrix of floats
2013 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeMatrixGood) {
2014 std::string spirv = kGLSL450MemoryModel + R"(
2015 %1 = OpTypeFloat 32
2016 %2 = OpTypeVector %1 4
2017 %3 = OpTypeMatrix %2 4
2018 %4 = OpConstant %1 1.0
2019 %5 = OpSpecConstant %1 0.0
2020 %6 = OpSpecConstantComposite %2 %4 %5 %5 %5
2021 %7 = OpSpecConstantComposite %2 %5 %4 %5 %5
2022 %8 = OpSpecConstantComposite %2 %5 %5 %4 %5
2023 %9 = OpSpecConstantComposite %2 %5 %5 %5 %4
2024 %10 = OpSpecConstantComposite %3 %6 %7 %8 %9)";
2025 CompileSuccessfully(spirv.c_str());
2026 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2027 }
2028
2029 // Valid: Matrix in which one column is Undef
2030 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeMatrixUndefGood) {
2031 std::string spirv = kGLSL450MemoryModel + R"(
2032 %1 = OpTypeFloat 32
2033 %2 = OpTypeVector %1 4
2034 %3 = OpTypeMatrix %2 4
2035 %4 = OpConstant %1 1.0
2036 %5 = OpSpecConstant %1 0.0
2037 %6 = OpSpecConstantComposite %2 %4 %5 %5 %5
2038 %7 = OpSpecConstantComposite %2 %5 %4 %5 %5
2039 %8 = OpSpecConstantComposite %2 %5 %5 %4 %5
2040 %9 = OpUndef %2
2041 %10 = OpSpecConstantComposite %3 %6 %7 %8 %9)";
2042 CompileSuccessfully(spirv.c_str());
2043 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2044 }
2045
2046 // Invalid: Matrix in which the sizes of column vectors are not equal.
2047 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeMatrixConstituentTypeBad) {
2048 std::string spirv = kGLSL450MemoryModel + R"(
2049 %1 = OpTypeFloat 32
2050 %2 = OpTypeVector %1 4
2051 %3 = OpTypeVector %1 3
2052 %4 = OpTypeMatrix %2 4
2053 %5 = OpSpecConstant %1 1.0
2054 %6 = OpConstant %1 0.0
2055 %7 = OpSpecConstantComposite %2 %5 %6 %6 %6
2056 %8 = OpSpecConstantComposite %2 %6 %5 %6 %6
2057 %9 = OpSpecConstantComposite %2 %6 %6 %5 %6
2058 %10 = OpSpecConstantComposite %3 %6 %6 %6
2059 %11 = OpSpecConstantComposite %4 %7 %8 %9 %10)";
2060 CompileSuccessfully(spirv.c_str());
2061 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2062 EXPECT_THAT(getDiagnosticString(),
2063 HasSubstr(make_message(
2064 "OpSpecConstantComposite Constituent <id> '10[%10]' "
2065 "vector component count does not match Result Type "
2066 "<id> '4[%mat4v4float]'s vector component count.")));
2067 }
2068
2069 // Invalid: Matrix type expects 4 columns but only 3 specified.
2070 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeMatrixNumColsBad) {
2071 std::string spirv = kGLSL450MemoryModel + R"(
2072 %1 = OpTypeFloat 32
2073 %2 = OpTypeVector %1 4
2074 %3 = OpTypeMatrix %2 4
2075 %4 = OpSpecConstant %1 1.0
2076 %5 = OpConstant %1 0.0
2077 %6 = OpSpecConstantComposite %2 %4 %5 %5 %5
2078 %7 = OpSpecConstantComposite %2 %5 %4 %5 %5
2079 %8 = OpSpecConstantComposite %2 %5 %5 %4 %5
2080 %10 = OpSpecConstantComposite %3 %6 %7 %8)";
2081 CompileSuccessfully(spirv.c_str());
2082 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2083 EXPECT_THAT(getDiagnosticString(),
2084 HasSubstr(make_message(
2085 "OpSpecConstantComposite Constituent <id> count does "
2086 "not match Result Type <id> '3[%mat4v4float]'s matrix column "
2087 "count.")));
2088 }
2089
2090 // Invalid: Composite contains a non-const/undef component
2091 TEST_P(ValidateIdWithMessage,
2092 OpSpecConstantCompositeMatrixConstituentNotConstBad) {
2093 std::string spirv = kGLSL450MemoryModel + R"(
2094 %1 = OpTypeFloat 32
2095 %2 = OpConstant %1 0.0
2096 %3 = OpTypeVector %1 4
2097 %4 = OpTypeMatrix %3 4
2098 %5 = OpSpecConstantComposite %3 %2 %2 %2 %2
2099 %6 = OpTypePointer Uniform %1
2100 %7 = OpVariable %6 Uniform
2101 %8 = OpSpecConstantComposite %4 %5 %5 %5 %7)";
2102 CompileSuccessfully(spirv.c_str());
2103 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2104 EXPECT_THAT(getDiagnosticString(),
2105 HasSubstr(make_message(
2106 "OpSpecConstantComposite Constituent <id> '7[%7]' is "
2107 "not a constant or undef.")));
2108 }
2109
2110 // Invalid: Composite contains a column that is *not* a vector (it's an array)
2111 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeMatrixColTypeBad) {
2112 std::string spirv = kGLSL450MemoryModel + R"(
2113 %1 = OpTypeFloat 32
2114 %2 = OpTypeInt 32 0
2115 %3 = OpSpecConstant %2 4
2116 %4 = OpConstant %1 0.0
2117 %5 = OpTypeVector %1 4
2118 %6 = OpTypeArray %2 %3
2119 %7 = OpTypeMatrix %5 4
2120 %8 = OpSpecConstantComposite %6 %3 %3 %3 %3
2121 %9 = OpSpecConstantComposite %5 %4 %4 %4 %4
2122 %10 = OpSpecConstantComposite %7 %9 %9 %9 %8)";
2123 CompileSuccessfully(spirv.c_str());
2124 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2125 EXPECT_THAT(getDiagnosticString(),
2126 HasSubstr(make_message(
2127 "OpSpecConstantComposite Constituent <id> '8[%8]' type "
2128 "does not match Result Type <id> '7[%mat4v4float]'s "
2129 "matrix column type.")));
2130 }
2131
2132 // Invalid: Matrix with an Undef column of the wrong size.
2133 TEST_P(ValidateIdWithMessage,
2134 OpSpecConstantCompositeMatrixConstituentUndefTypeBad) {
2135 std::string spirv = kGLSL450MemoryModel + R"(
2136 %1 = OpTypeFloat 32
2137 %2 = OpTypeVector %1 4
2138 %3 = OpTypeVector %1 3
2139 %4 = OpTypeMatrix %2 4
2140 %5 = OpSpecConstant %1 1.0
2141 %6 = OpSpecConstant %1 0.0
2142 %7 = OpSpecConstantComposite %2 %5 %6 %6 %6
2143 %8 = OpSpecConstantComposite %2 %6 %5 %6 %6
2144 %9 = OpSpecConstantComposite %2 %6 %6 %5 %6
2145 %10 = OpUndef %3
2146 %11 = OpSpecConstantComposite %4 %7 %8 %9 %10)";
2147 CompileSuccessfully(spirv.c_str());
2148 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2149 EXPECT_THAT(getDiagnosticString(),
2150 HasSubstr(make_message(
2151 "OpSpecConstantComposite Constituent <id> '10[%10]' "
2152 "vector component count does not match Result Type "
2153 "<id> '4[%mat4v4float]'s vector component count.")));
2154 }
2155
2156 // Invalid: Matrix in which some columns are Int and some are Float.
2157 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeMatrixColumnTypeBad) {
2158 std::string spirv = kGLSL450MemoryModel + R"(
2159 %1 = OpTypeInt 32 0
2160 %2 = OpTypeFloat 32
2161 %3 = OpTypeVector %1 2
2162 %4 = OpTypeVector %2 2
2163 %5 = OpTypeMatrix %4 2
2164 %6 = OpSpecConstant %1 42
2165 %7 = OpConstant %2 3.14
2166 %8 = OpSpecConstantComposite %3 %6 %6
2167 %9 = OpSpecConstantComposite %4 %7 %7
2168 %10 = OpSpecConstantComposite %5 %8 %9)";
2169 CompileSuccessfully(spirv.c_str());
2170 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2171 EXPECT_THAT(getDiagnosticString(),
2172 HasSubstr(make_message(
2173 "OpSpecConstantComposite Constituent <id> '8[%8]' "
2174 "component type does not match Result Type <id> "
2175 "'5[%mat2v2float]'s matrix column component type.")));
2176 }
2177
2178 // Valid: Array of integers
2179 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeArrayGood) {
2180 std::string spirv = kGLSL450MemoryModel + R"(
2181 %1 = OpTypeInt 32 0
2182 %2 = OpSpecConstant %1 4
2183 %5 = OpConstant %1 5
2184 %3 = OpTypeArray %1 %2
2185 %6 = OpTypeArray %1 %5
2186 %4 = OpSpecConstantComposite %3 %2 %2 %2 %2
2187 %7 = OpSpecConstantComposite %3 %5 %5 %5 %5)";
2188 CompileSuccessfully(spirv.c_str());
2189 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2190 }
2191
2192 // Invalid: Expecting an array of 4 components, but 3 specified.
2193 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeArrayNumComponentsBad) {
2194 std::string spirv = kGLSL450MemoryModel + R"(
2195 %1 = OpTypeInt 32 0
2196 %2 = OpConstant %1 4
2197 %3 = OpTypeArray %1 %2
2198 %4 = OpSpecConstantComposite %3 %2 %2 %2)";
2199 CompileSuccessfully(spirv.c_str());
2200 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2201 EXPECT_THAT(getDiagnosticString(),
2202 HasSubstr(make_message(
2203 "OpSpecConstantComposite Constituent count does not "
2204 "match Result Type <id> '3[%_arr_uint_uint_4]'s array "
2205 "length.")));
2206 }
2207
2208 // Valid: Array of Integers and Undef-int
2209 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeArrayWithUndefGood) {
2210 std::string spirv = kGLSL450MemoryModel + R"(
2211 %1 = OpTypeInt 32 0
2212 %2 = OpSpecConstant %1 4
2213 %9 = OpUndef %1
2214 %3 = OpTypeArray %1 %2
2215 %4 = OpSpecConstantComposite %3 %2 %2 %2 %9)";
2216 CompileSuccessfully(spirv.c_str());
2217 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2218 }
2219
2220 // Invalid: Array uses a type as operand.
2221 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeArrayConstConstituentBad) {
2222 std::string spirv = kGLSL450MemoryModel + R"(
2223 %1 = OpTypeInt 32 0
2224 %2 = OpConstant %1 4
2225 %3 = OpTypeArray %1 %2
2226 %4 = OpTypePointer Uniform %1
2227 %5 = OpVariable %4 Uniform
2228 %6 = OpSpecConstantComposite %3 %2 %2 %2 %5)";
2229 CompileSuccessfully(spirv.c_str());
2230 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2231 EXPECT_THAT(getDiagnosticString(),
2232 HasSubstr(make_message(
2233 "OpSpecConstantComposite Constituent <id> '5[%5]' is "
2234 "not a constant or undef.")));
2235 }
2236
2237 // Invalid: Array has a mix of Int and Float components.
2238 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeArrayConstituentTypeBad) {
2239 std::string spirv = kGLSL450MemoryModel + R"(
2240 %1 = OpTypeInt 32 0
2241 %2 = OpConstant %1 4
2242 %3 = OpTypeArray %1 %2
2243 %4 = OpTypeFloat 32
2244 %5 = OpSpecConstant %4 3.14 ; bad type for const value
2245 %6 = OpSpecConstantComposite %3 %2 %2 %2 %5)";
2246 CompileSuccessfully(spirv.c_str());
2247 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2248 EXPECT_THAT(getDiagnosticString(),
2249 HasSubstr(make_message(
2250 "OpSpecConstantComposite Constituent <id> '5[%5]'s "
2251 "type does not match Result Type <id> "
2252 "'3[%_arr_uint_uint_4]'s array element type.")));
2253 }
2254
2255 // Invalid: Array has a mix of Int and Undef-float.
2256 TEST_P(ValidateIdWithMessage,
2257 OpSpecConstantCompositeArrayConstituentUndefTypeBad) {
2258 std::string spirv = kGLSL450MemoryModel + R"(
2259 %1 = OpTypeInt 32 0
2260 %2 = OpSpecConstant %1 4
2261 %3 = OpTypeArray %1 %2
2262 %5 = OpTypeFloat 32
2263 %6 = OpUndef %5 ; bad type for undef
2264 %4 = OpSpecConstantComposite %3 %2 %2 %2 %6)";
2265 CompileSuccessfully(spirv.c_str());
2266 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2267 EXPECT_THAT(getDiagnosticString(),
2268 HasSubstr(make_message(
2269 "OpSpecConstantComposite Constituent <id> '5[%5]'s "
2270 "type does not match Result Type <id> "
2271 "'3[%_arr_uint_2]'s array element type.")));
2272 }
2273
2274 // Valid: Struct of {Int32,Int32,Int64}.
2275 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeStructGood) {
2276 std::string spirv = kGLSL450MemoryModel + R"(
2277 %1 = OpTypeInt 32 0
2278 %2 = OpTypeInt 64 0
2279 %3 = OpTypeStruct %1 %1 %2
2280 %4 = OpConstant %1 42
2281 %5 = OpSpecConstant %2 4300000000
2282 %6 = OpSpecConstantComposite %3 %4 %4 %5)";
2283 CompileSuccessfully(spirv.c_str());
2284 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2285 }
2286
2287 // Invalid: missing one int32 struct member.
2288 TEST_P(ValidateIdWithMessage,
2289 OpSpecConstantCompositeStructMissingComponentBad) {
2290 std::string spirv = kGLSL450MemoryModel + R"(
2291 %1 = OpTypeInt 32 0
2292 %3 = OpTypeStruct %1 %1 %1
2293 %4 = OpConstant %1 42
2294 %5 = OpSpecConstant %1 430
2295 %6 = OpSpecConstantComposite %3 %4 %5)";
2296 CompileSuccessfully(spirv.c_str());
2297 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2298 EXPECT_THAT(
2299 getDiagnosticString(),
2300 HasSubstr(make_message("OpSpecConstantComposite Constituent <id> "
2301 "'2[%_struct_2]' count does not match Result Type "
2302 "<id> '2[%_struct_2]'s struct member count.")));
2303 }
2304
2305 // Valid: Struct uses Undef-int64.
2306 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeStructUndefGood) {
2307 std::string spirv = kGLSL450MemoryModel + R"(
2308 %1 = OpTypeInt 32 0
2309 %2 = OpTypeInt 64 0
2310 %3 = OpTypeStruct %1 %1 %2
2311 %4 = OpSpecConstant %1 42
2312 %5 = OpUndef %2
2313 %6 = OpSpecConstantComposite %3 %4 %4 %5)";
2314 CompileSuccessfully(spirv.c_str());
2315 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2316 }
2317
2318 // Invalid: Composite contains non-const/undef component.
2319 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeStructNonConstBad) {
2320 std::string spirv = kGLSL450MemoryModel + R"(
2321 %1 = OpTypeInt 32 0
2322 %2 = OpTypeInt 64 0
2323 %3 = OpTypeStruct %1 %1 %2
2324 %4 = OpSpecConstant %1 42
2325 %5 = OpUndef %2
2326 %6 = OpTypePointer Uniform %1
2327 %7 = OpVariable %6 Uniform
2328 %8 = OpSpecConstantComposite %3 %4 %7 %5)";
2329 CompileSuccessfully(spirv.c_str());
2330 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2331 EXPECT_THAT(getDiagnosticString(),
2332 HasSubstr(make_message(
2333 "OpSpecConstantComposite Constituent <id> '7[%7]' is "
2334 "not a constant or undef.")));
2335 }
2336
2337 // Invalid: Struct component type does not match expected specialization type.
2338 // Second component was expected to be Int32, but got Int64.
2339 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeStructMemberTypeBad) {
2340 std::string spirv = kGLSL450MemoryModel + R"(
2341 %1 = OpTypeInt 32 0
2342 %2 = OpTypeInt 64 0
2343 %3 = OpTypeStruct %1 %1 %2
2344 %4 = OpConstant %1 42
2345 %5 = OpSpecConstant %2 4300000000
2346 %6 = OpSpecConstantComposite %3 %4 %5 %4)";
2347 CompileSuccessfully(spirv.c_str());
2348 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2349 EXPECT_THAT(getDiagnosticString(),
2350 HasSubstr(make_message(
2351 "OpSpecConstantComposite Constituent <id> '5[%5]' type "
2352 "does not match the Result Type <id> '3[%_struct_3]'s "
2353 "member type.")));
2354 }
2355
2356 // Invalid: Undef-int64 used when Int32 was expected.
2357 TEST_P(ValidateIdWithMessage, OpSpecConstantCompositeStructMemberUndefTypeBad) {
2358 std::string spirv = kGLSL450MemoryModel + R"(
2359 %1 = OpTypeInt 32 0
2360 %2 = OpTypeInt 64 0
2361 %3 = OpTypeStruct %1 %1 %2
2362 %4 = OpSpecConstant %1 42
2363 %5 = OpUndef %2
2364 %6 = OpSpecConstantComposite %3 %4 %5 %4)";
2365 CompileSuccessfully(spirv.c_str());
2366 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2367 EXPECT_THAT(getDiagnosticString(),
2368 HasSubstr(make_message(
2369 "OpSpecConstantComposite Constituent <id> '5[%5]' type "
2370 "does not match the Result Type <id> '3[%_struct_3]'s "
2371 "member type.")));
2372 }
2373
2374 // TODO: OpSpecConstantOp
2375
2376 TEST_P(ValidateIdWithMessage, OpVariableGood) {
2377 std::string spirv = kGLSL450MemoryModel + R"(
2378 %1 = OpTypeInt 32 0
2379 %2 = OpTypePointer Input %1
2380 %3 = OpVariable %2 Input)";
2381 CompileSuccessfully(spirv.c_str());
2382 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2383 }
2384 TEST_P(ValidateIdWithMessage, OpVariableInitializerConstantGood) {
2385 std::string spirv = kGLSL450MemoryModel + R"(
2386 %1 = OpTypeInt 32 0
2387 %2 = OpTypePointer Output %1
2388 %3 = OpConstant %1 42
2389 %4 = OpVariable %2 Output %3)";
2390 CompileSuccessfully(spirv.c_str());
2391 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2392 }
2393 TEST_P(ValidateIdWithMessage, OpVariableInitializerGlobalVariableGood) {
2394 std::string spirv = kOpenCLMemoryModel64 + R"(
2395 %2 = OpTypeInt 32 0
2396 %3 = OpTypePointer CrossWorkgroup %2
2397 %4 = OpVariable %3 CrossWorkgroup
2398 %5 = OpTypePointer Function %3 ; pointer to pointer
2399 %void = OpTypeVoid
2400 %void_fn = OpTypeFunction %void
2401 %func = OpFunction %void None %void_fn
2402 %entry = OpLabel
2403 %6 = OpVariable %5 Function %4
2404 OpReturn
2405 OpFunctionEnd
2406 )";
2407 CompileSuccessfully(spirv.c_str());
2408 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2409 }
2410 // TODO: Positive test OpVariable with OpConstantNull of OpTypePointer
2411 TEST_P(ValidateIdWithMessage, OpVariableResultTypeBad) {
2412 std::string spirv = kGLSL450MemoryModel + R"(
2413 %1 = OpTypeInt 32 0
2414 %2 = OpVariable %1 Input)";
2415 CompileSuccessfully(spirv.c_str());
2416 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2417 EXPECT_THAT(getDiagnosticString(),
2418 HasSubstr(make_message(
2419 "OpVariable Result Type <id> '1[%uint]' is not a pointer "
2420 "type.")));
2421 }
2422 TEST_P(ValidateIdWithMessage, OpVariableInitializerIsTypeBad) {
2423 std::string spirv = kGLSL450MemoryModel + R"(
2424 %1 = OpTypeInt 32 0
2425 %2 = OpTypePointer Input %1
2426 %3 = OpVariable %2 Input %2)";
2427 CompileSuccessfully(spirv.c_str());
2428 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2429 EXPECT_THAT(getDiagnosticString(),
2430 HasSubstr(make_message("Operand '2[%_ptr_Input_uint]' "
2431 "cannot be a type")));
2432 }
2433
2434 TEST_P(ValidateIdWithMessage, OpVariableInitializerIsFunctionVarBad) {
2435 std::string spirv = kGLSL450MemoryModel + R"(
2436 %int = OpTypeInt 32 0
2437 %ptrint = OpTypePointer Function %int
2438 %ptrptrint = OpTypePointer Function %ptrint
2439 %void = OpTypeVoid
2440 %fnty = OpTypeFunction %void
2441 %main = OpFunction %void None %fnty
2442 %entry = OpLabel
2443 %var = OpVariable %ptrint Function
2444 %varinit = OpVariable %ptrptrint Function %var ; Can't initialize function variable.
2445 OpReturn
2446 OpFunctionEnd
2447 )";
2448 CompileSuccessfully(spirv.c_str());
2449 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2450 EXPECT_THAT(getDiagnosticString(),
2451 HasSubstr("Variable Initializer <id> '8[%8]' is not a constant "
2452 "or module-scope variable"));
2453 }
2454
2455 TEST_P(ValidateIdWithMessage, OpVariableInitializerIsModuleVarGood) {
2456 std::string spirv = kOpenCLMemoryModel64 + R"(
2457 %int = OpTypeInt 32 0
2458 %ptrint = OpTypePointer CrossWorkgroup %int
2459 %mvar = OpVariable %ptrint CrossWorkgroup
2460 %ptrptrint = OpTypePointer Function %ptrint
2461 %void = OpTypeVoid
2462 %fnty = OpTypeFunction %void
2463 %main = OpFunction %void None %fnty
2464 %entry = OpLabel
2465 %goodvar = OpVariable %ptrptrint Function %mvar ; This is ok
2466 OpReturn
2467 OpFunctionEnd
2468 )";
2469 CompileSuccessfully(spirv.c_str());
2470 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2471 }
2472
2473 TEST_P(ValidateIdWithMessage, OpVariableContainsBoolBad) {
2474 std::string spirv = kGLSL450MemoryModel + R"(
2475 %bool = OpTypeBool
2476 %int = OpTypeInt 32 0
2477 %block = OpTypeStruct %bool %int
2478 %_ptr_Uniform_block = OpTypePointer Uniform %block
2479 %var = OpVariable %_ptr_Uniform_block Uniform
2480 %void = OpTypeVoid
2481 %fnty = OpTypeFunction %void
2482 %main = OpFunction %void None %fnty
2483 %entry = OpLabel
2484 %load = OpLoad %block %var
2485 OpReturn
2486 OpFunctionEnd
2487 )";
2488 CompileSuccessfully(spirv.c_str());
2489 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2490 EXPECT_THAT(
2491 getDiagnosticString(),
2492 HasSubstr(make_message(
2493 "If OpTypeBool is stored in conjunction with OpVariable, it can only "
2494 "be used with non-externally visible shader Storage Classes: "
2495 "Workgroup, CrossWorkgroup, Private, Function, Input, Output, "
2496 "RayPayloadKHR, IncomingRayPayloadKHR, HitAttributeKHR, "
2497 "CallableDataKHR, IncomingCallableDataKHR, NodePayloadAMDX, or "
2498 "UniformConstant")));
2499 }
2500
2501 TEST_P(ValidateIdWithMessage, OpVariableContainsBoolPrivateGood) {
2502 std::string spirv = kGLSL450MemoryModel + R"(
2503 %bool = OpTypeBool
2504 %int = OpTypeInt 32 0
2505 %block = OpTypeStruct %bool %int
2506 %_ptr_Private_block = OpTypePointer Private %block
2507 %var = OpVariable %_ptr_Private_block Private
2508 %void = OpTypeVoid
2509 %fnty = OpTypeFunction %void
2510 %main = OpFunction %void None %fnty
2511 %entry = OpLabel
2512 %load = OpLoad %block %var
2513 OpReturn
2514 OpFunctionEnd
2515 )";
2516 CompileSuccessfully(spirv.c_str());
2517 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2518 }
2519
2520 TEST_P(ValidateIdWithMessage, OpVariableContainsBoolUniformConstantGood) {
2521 std::string spirv = kGLSL450MemoryModel + R"(
2522 %bool = OpTypeBool
2523 %int = OpTypeInt 32 0
2524 %block = OpTypeStruct %bool %int
2525 %_ptr_UniformConstant_block = OpTypePointer UniformConstant %block
2526 %var = OpVariable %_ptr_UniformConstant_block UniformConstant
2527 %void = OpTypeVoid
2528 %fnty = OpTypeFunction %void
2529 %main = OpFunction %void None %fnty
2530 %entry = OpLabel
2531 %load = OpLoad %block %var
2532 OpReturn
2533 OpFunctionEnd
2534 )";
2535 CompileSuccessfully(spirv.c_str());
2536 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2537 }
2538
2539 TEST_P(ValidateIdWithMessage, OpVariableContainsBoolPointerGood) {
2540 std::string spirv = kGLSL450MemoryModel + R"(
2541 %bool = OpTypeBool
2542 %boolptr = OpTypePointer Uniform %bool
2543 %int = OpTypeInt 32 0
2544 %block = OpTypeStruct %boolptr %int
2545 %_ptr_Uniform_block = OpTypePointer Uniform %block
2546 %var = OpVariable %_ptr_Uniform_block Uniform
2547 %void = OpTypeVoid
2548 %fnty = OpTypeFunction %void
2549 %main = OpFunction %void None %fnty
2550 %entry = OpLabel
2551 %load = OpLoad %block %var
2552 OpReturn
2553 OpFunctionEnd
2554 )";
2555 CompileSuccessfully(spirv.c_str());
2556 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2557 }
2558
2559 TEST_P(ValidateIdWithMessage, OpVariableContainsBuiltinBoolGood) {
2560 std::string spirv = kGLSL450MemoryModel + R"(
2561 OpMemberDecorate %input 0 BuiltIn FrontFacing
2562 %bool = OpTypeBool
2563 %input = OpTypeStruct %bool
2564 %_ptr_input = OpTypePointer Input %input
2565 %var = OpVariable %_ptr_input Input
2566 %void = OpTypeVoid
2567 %fnty = OpTypeFunction %void
2568 %main = OpFunction %void None %fnty
2569 %entry = OpLabel
2570 %load = OpLoad %input %var
2571 OpReturn
2572 OpFunctionEnd
2573 )";
2574 CompileSuccessfully(spirv.c_str());
2575 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2576 }
2577
2578 TEST_P(ValidateIdWithMessage, OpVariableContainsNoBuiltinBoolBad) {
2579 std::string spirv = kGLSL450MemoryModel + R"(
2580 %bool = OpTypeBool
2581 %input = OpTypeStruct %bool
2582 %_ptr_input = OpTypePointer Input %input
2583 %var = OpVariable %_ptr_input Input
2584 %void = OpTypeVoid
2585 %fnty = OpTypeFunction %void
2586 %main = OpFunction %void None %fnty
2587 %entry = OpLabel
2588 %load = OpLoad %input %var
2589 OpReturn
2590 OpFunctionEnd
2591 )";
2592 CompileSuccessfully(spirv.c_str());
2593 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2594 EXPECT_THAT(
2595 getDiagnosticString(),
2596 HasSubstr(make_message(
2597 "If OpTypeBool is stored in conjunction with OpVariable using Input "
2598 "or Output Storage Classes it requires a BuiltIn decoration")));
2599 }
2600
2601 TEST_P(ValidateIdWithMessage, OpVariableContainsNoBuiltinBoolBadVulkan) {
2602 std::string spirv = R"(
2603 OpCapability Shader
2604 OpMemoryModel Logical GLSL450
2605 OpEntryPoint Fragment %main "main" %var
2606 OpExecutionMode %main OriginUpperLeft
2607 %bool = OpTypeBool
2608 %input = OpTypeStruct %bool
2609 %_ptr_input = OpTypePointer Input %input
2610 %var = OpVariable %_ptr_input Input
2611 %void = OpTypeVoid
2612 %fnty = OpTypeFunction %void
2613 %main = OpFunction %void None %fnty
2614 %entry = OpLabel
2615 %load = OpLoad %input %var
2616 OpReturn
2617 OpFunctionEnd
2618 )";
2619 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_0);
2620 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_0));
2621 EXPECT_THAT(getDiagnosticString(),
2622 AnyVUID("VUID-StandaloneSpirv-Input-07290"));
2623 EXPECT_THAT(
2624 getDiagnosticString(),
2625 HasSubstr(make_message(
2626 "If OpTypeBool is stored in conjunction with OpVariable using Input "
2627 "or Output Storage Classes it requires a BuiltIn decoration")));
2628 }
2629
2630 TEST_P(ValidateIdWithMessage, OpVariableContainsRayPayloadBoolGood) {
2631 std::string spirv = R"(
2632 OpCapability RayTracingNV
2633 OpCapability Shader
2634 OpCapability Linkage
2635 OpExtension "SPV_NV_ray_tracing"
2636 OpMemoryModel Logical GLSL450
2637 %bool = OpTypeBool
2638 %PerRayData = OpTypeStruct %bool
2639 %_ptr_PerRayData = OpTypePointer RayPayloadNV %PerRayData
2640 %var = OpVariable %_ptr_PerRayData RayPayloadNV
2641 %void = OpTypeVoid
2642 %fnty = OpTypeFunction %void
2643 %main = OpFunction %void None %fnty
2644 %entry = OpLabel
2645 %load = OpLoad %PerRayData %var
2646 OpReturn
2647 OpFunctionEnd
2648 )";
2649 CompileSuccessfully(spirv.c_str());
2650 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2651 }
2652
2653 TEST_P(ValidateIdWithMessage, OpVariablePointerNoVariablePointersBad) {
2654 const std::string spirv = R"(
2655 OpCapability Shader
2656 OpCapability Linkage
2657 OpMemoryModel Logical GLSL450
2658 %void = OpTypeVoid
2659 %int = OpTypeInt 32 0
2660 %_ptr_workgroup_int = OpTypePointer Workgroup %int
2661 %_ptr_function_ptr = OpTypePointer Function %_ptr_workgroup_int
2662 %voidfn = OpTypeFunction %void
2663 %func = OpFunction %void None %voidfn
2664 %entry = OpLabel
2665 %var = OpVariable %_ptr_function_ptr Function
2666 OpReturn
2667 OpFunctionEnd
2668 )";
2669
2670 CompileSuccessfully(spirv);
2671 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2672 EXPECT_THAT(
2673 getDiagnosticString(),
2674 HasSubstr(make_message(
2675 "In Logical addressing, variables can only allocate a workgroup "
2676 "pointer if the VariablePointers capability is declared")));
2677 }
2678
2679 TEST_P(ValidateIdWithMessage,
2680 OpVariablePointerNoVariablePointersRelaxedLogicalGood) {
2681 const std::string spirv = R"(
2682 OpCapability Shader
2683 OpCapability Linkage
2684 OpMemoryModel Logical GLSL450
2685 %void = OpTypeVoid
2686 %int = OpTypeInt 32 0
2687 %_ptr_workgroup_int = OpTypePointer Workgroup %int
2688 %_ptr_function_ptr = OpTypePointer Function %_ptr_workgroup_int
2689 %voidfn = OpTypeFunction %void
2690 %func = OpFunction %void None %voidfn
2691 %entry = OpLabel
2692 %var = OpVariable %_ptr_function_ptr Function
2693 OpReturn
2694 OpFunctionEnd
2695 )";
2696
2697 auto options = getValidatorOptions();
2698 options->relax_logical_pointer = true;
2699 CompileSuccessfully(spirv);
2700 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2701 }
2702
2703 TEST_P(ValidateIdWithMessage, OpFunctionWithNonMemoryObject) {
2704 // DXC generates code that looks like when given something like:
2705 // T t;
2706 // t.s.fn_1();
2707 // This needs to be accepted before legalization takes place, so we
2708 // will include it with the relaxed logical pointer.
2709
2710 const std::string spirv = R"(
2711 OpCapability Shader
2712 OpMemoryModel Logical GLSL450
2713 OpEntryPoint Vertex %1 "main"
2714 OpSource HLSL 600
2715 %int = OpTypeInt 32 1
2716 %int_0 = OpConstant %int 0
2717 %void = OpTypeVoid
2718 %9 = OpTypeFunction %void
2719 %_struct_5 = OpTypeStruct
2720 %_struct_6 = OpTypeStruct %_struct_5
2721 %_ptr_Function__struct_6 = OpTypePointer Function %_struct_6
2722 %_ptr_Function__struct_5 = OpTypePointer Function %_struct_5
2723 %23 = OpTypeFunction %void %_ptr_Function__struct_5
2724 %1 = OpFunction %void None %9
2725 %10 = OpLabel
2726 %11 = OpVariable %_ptr_Function__struct_6 Function
2727 %20 = OpAccessChain %_ptr_Function__struct_5 %11 %int_0
2728 %21 = OpFunctionCall %void %12 %20
2729 OpReturn
2730 OpFunctionEnd
2731 %12 = OpFunction %void None %23
2732 %13 = OpFunctionParameter %_ptr_Function__struct_5
2733 %14 = OpLabel
2734 OpReturn
2735 OpFunctionEnd
2736 )";
2737
2738 auto options = getValidatorOptions();
2739 options->relax_logical_pointer = true;
2740 CompileSuccessfully(spirv);
2741 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2742 }
2743
2744 TEST_P(ValidateIdWithMessage,
2745 OpVariablePointerVariablePointersStorageBufferGood) {
2746 const std::string spirv = R"(
2747 OpCapability Shader
2748 OpCapability Linkage
2749 OpCapability VariablePointersStorageBuffer
2750 OpExtension "SPV_KHR_variable_pointers"
2751 OpMemoryModel Logical GLSL450
2752 %void = OpTypeVoid
2753 %int = OpTypeInt 32 0
2754 %_ptr_storagebuffer_int = OpTypePointer StorageBuffer %int
2755 %_ptr_function_ptr = OpTypePointer Function %_ptr_storagebuffer_int
2756 %voidfn = OpTypeFunction %void
2757 %func = OpFunction %void None %voidfn
2758 %entry = OpLabel
2759 %var = OpVariable %_ptr_function_ptr Function
2760 OpReturn
2761 OpFunctionEnd
2762 )";
2763
2764 CompileSuccessfully(spirv);
2765 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2766 }
2767
2768 TEST_P(ValidateIdWithMessage, OpVariablePointerVariablePointersGood) {
2769 const std::string spirv = R"(
2770 OpCapability Shader
2771 OpCapability Linkage
2772 OpCapability VariablePointers
2773 OpExtension "SPV_KHR_variable_pointers"
2774 OpMemoryModel Logical GLSL450
2775 %void = OpTypeVoid
2776 %int = OpTypeInt 32 0
2777 %_ptr_workgroup_int = OpTypePointer Workgroup %int
2778 %_ptr_function_ptr = OpTypePointer Function %_ptr_workgroup_int
2779 %voidfn = OpTypeFunction %void
2780 %func = OpFunction %void None %voidfn
2781 %entry = OpLabel
2782 %var = OpVariable %_ptr_function_ptr Function
2783 OpReturn
2784 OpFunctionEnd
2785 )";
2786
2787 CompileSuccessfully(spirv);
2788 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2789 }
2790
2791 TEST_P(ValidateIdWithMessage, OpVariablePointerVariablePointersBad) {
2792 const std::string spirv = R"(
2793 OpCapability Shader
2794 OpCapability VariablePointers
2795 OpExtension "SPV_KHR_variable_pointers"
2796 OpMemoryModel Logical GLSL450
2797 %void = OpTypeVoid
2798 %int = OpTypeInt 32 0
2799 %_ptr_workgroup_int = OpTypePointer Workgroup %int
2800 %_ptr_uniform_ptr = OpTypePointer Uniform %_ptr_workgroup_int
2801 %var = OpVariable %_ptr_uniform_ptr Uniform
2802 )";
2803
2804 CompileSuccessfully(spirv);
2805 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2806 EXPECT_THAT(getDiagnosticString(),
2807 HasSubstr(make_message(
2808 "In Logical addressing with variable pointers, "
2809 "variables that allocate pointers must be in Function "
2810 "or Private storage classes")));
2811 }
2812
2813 TEST_P(ValidateIdWithMessage, OpLoadGood) {
2814 std::string spirv = kGLSL450MemoryModel + R"(
2815 %1 = OpTypeVoid
2816 %2 = OpTypeInt 32 0
2817 %3 = OpTypePointer UniformConstant %2
2818 %4 = OpTypeFunction %1
2819 %5 = OpVariable %3 UniformConstant
2820 %6 = OpFunction %1 None %4
2821 %7 = OpLabel
2822 %8 = OpLoad %2 %5
2823 OpReturn
2824 OpFunctionEnd
2825 )";
2826 CompileSuccessfully(spirv.c_str());
2827 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2828 }
2829
2830 // TODO: Add tests that exercise VariablePointersStorageBuffer instead of
2831 // VariablePointers.
2832 void createVariablePointerSpirvProgram(std::ostringstream* spirv,
2833 std::string result_strategy,
2834 bool use_varptr_cap,
2835 bool add_helper_function) {
2836 *spirv << "OpCapability Shader ";
2837 if (use_varptr_cap) {
2838 *spirv << "OpCapability VariablePointers ";
2839 *spirv << "OpExtension \"SPV_KHR_variable_pointers\" ";
2840 }
2841 *spirv << "OpExtension \"SPV_KHR_storage_buffer_storage_class\" ";
2842 *spirv << R"(
2843 OpMemoryModel Logical GLSL450
2844 OpEntryPoint GLCompute %main "main"
2845 %void = OpTypeVoid
2846 %voidf = OpTypeFunction %void
2847 %bool = OpTypeBool
2848 %i32 = OpTypeInt 32 1
2849 %f32 = OpTypeFloat 32
2850 %f32ptr = OpTypePointer StorageBuffer %f32
2851 %i = OpConstant %i32 1
2852 %zero = OpConstant %i32 0
2853 %float_1 = OpConstant %f32 1.0
2854 %ptr1 = OpVariable %f32ptr StorageBuffer
2855 %ptr2 = OpVariable %f32ptr StorageBuffer
2856 )";
2857 if (add_helper_function) {
2858 *spirv << R"(
2859 ; ////////////////////////////////////////////////////////////
2860 ;;;; Function that returns a pointer
2861 ; ////////////////////////////////////////////////////////////
2862 %selector_func_type = OpTypeFunction %f32ptr %bool %f32ptr %f32ptr
2863 %choose_input_func = OpFunction %f32ptr None %selector_func_type
2864 %is_neg_param = OpFunctionParameter %bool
2865 %first_ptr_param = OpFunctionParameter %f32ptr
2866 %second_ptr_param = OpFunctionParameter %f32ptr
2867 %selector_func_begin = OpLabel
2868 %result_ptr = OpSelect %f32ptr %is_neg_param %first_ptr_param %second_ptr_param
2869 OpReturnValue %result_ptr
2870 OpFunctionEnd
2871 )";
2872 }
2873 *spirv << R"(
2874 %main = OpFunction %void None %voidf
2875 %label = OpLabel
2876 )";
2877 *spirv << result_strategy;
2878 *spirv << R"(
2879 OpReturn
2880 OpFunctionEnd
2881 )";
2882 }
2883
2884 // With the VariablePointer Capability, OpLoad should allow loading a
2885 // VaiablePointer. In this test the variable pointer is obtained by an OpSelect
2886 TEST_P(ValidateIdWithMessage, OpLoadVarPtrOpSelectGood) {
2887 std::string result_strategy = R"(
2888 %isneg = OpSLessThan %bool %i %zero
2889 %varptr = OpSelect %f32ptr %isneg %ptr1 %ptr2
2890 %result = OpLoad %f32 %varptr
2891 )";
2892
2893 std::ostringstream spirv;
2894 createVariablePointerSpirvProgram(&spirv, result_strategy,
2895 true /* Add VariablePointers Capability? */,
2896 false /* Use Helper Function? */);
2897 CompileSuccessfully(spirv.str());
2898 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2899 }
2900
2901 // Without the VariablePointers Capability, OpLoad will not allow loading
2902 // through a variable pointer.
2903 // Disabled since using OpSelect with pointers without VariablePointers will
2904 // fail LogicalsPass.
2905 TEST_P(ValidateIdWithMessage, DISABLED_OpLoadVarPtrOpSelectBad) {
2906 std::string result_strategy = R"(
2907 %isneg = OpSLessThan %bool %i %zero
2908 %varptr = OpSelect %f32ptr %isneg %ptr1 %ptr2
2909 %result = OpLoad %f32 %varptr
2910 )";
2911
2912 std::ostringstream spirv;
2913 createVariablePointerSpirvProgram(&spirv, result_strategy,
2914 false /* Add VariablePointers Capability?*/,
2915 false /* Use Helper Function? */);
2916 CompileSuccessfully(spirv.str());
2917 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2918 EXPECT_THAT(getDiagnosticString(),
2919 HasSubstr(make_message("is not a logical pointer.")));
2920 }
2921
2922 // With the VariablePointer Capability, OpLoad should allow loading a
2923 // VaiablePointer. In this test the variable pointer is obtained by an OpPhi
2924 TEST_P(ValidateIdWithMessage, OpLoadVarPtrOpPhiGood) {
2925 std::string result_strategy = R"(
2926 %is_neg = OpSLessThan %bool %i %zero
2927 OpSelectionMerge %end_label None
2928 OpBranchConditional %is_neg %take_ptr_1 %take_ptr_2
2929 %take_ptr_1 = OpLabel
2930 OpBranch %end_label
2931 %take_ptr_2 = OpLabel
2932 OpBranch %end_label
2933 %end_label = OpLabel
2934 %varptr = OpPhi %f32ptr %ptr1 %take_ptr_1 %ptr2 %take_ptr_2
2935 %result = OpLoad %f32 %varptr
2936 )";
2937
2938 std::ostringstream spirv;
2939 createVariablePointerSpirvProgram(&spirv, result_strategy,
2940 true /* Add VariablePointers Capability?*/,
2941 false /* Use Helper Function? */);
2942 CompileSuccessfully(spirv.str());
2943 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2944 }
2945
2946 // Without the VariablePointers Capability, OpPhi can have a pointer result
2947 // type.
2948 TEST_P(ValidateIdWithMessage, OpPhiBad) {
2949 std::string result_strategy = R"(
2950 %is_neg = OpSLessThan %bool %i %zero
2951 OpSelectionMerge %end_label None
2952 OpBranchConditional %is_neg %take_ptr_1 %take_ptr_2
2953 %take_ptr_1 = OpLabel
2954 OpBranch %end_label
2955 %take_ptr_2 = OpLabel
2956 OpBranch %end_label
2957 %end_label = OpLabel
2958 %varptr = OpPhi %f32ptr %ptr1 %take_ptr_1 %ptr2 %take_ptr_2
2959 %result = OpLoad %f32 %varptr
2960 )";
2961
2962 std::ostringstream spirv;
2963 createVariablePointerSpirvProgram(&spirv, result_strategy,
2964 false /* Add VariablePointers Capability?*/,
2965 false /* Use Helper Function? */);
2966 CompileSuccessfully(spirv.str());
2967 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
2968 EXPECT_THAT(getDiagnosticString(),
2969 HasSubstr(make_message(
2970 "Using pointers with OpPhi requires capability "
2971 "VariablePointers or VariablePointersStorageBuffer")));
2972 }
2973
2974 // With the VariablePointer Capability, OpLoad should allow loading through a
2975 // VaiablePointer. In this test the variable pointer is obtained from an
2976 // OpFunctionCall (return value from a function)
2977 TEST_P(ValidateIdWithMessage, OpLoadVarPtrOpFunctionCallGood) {
2978 std::ostringstream spirv;
2979 std::string result_strategy = R"(
2980 %isneg = OpSLessThan %bool %i %zero
2981 %varptr = OpFunctionCall %f32ptr %choose_input_func %isneg %ptr1 %ptr2
2982 %result = OpLoad %f32 %varptr
2983 )";
2984
2985 createVariablePointerSpirvProgram(&spirv, result_strategy,
2986 true /* Add VariablePointers Capability?*/,
2987 true /* Use Helper Function? */);
2988 CompileSuccessfully(spirv.str());
2989 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2990 }
2991
2992 TEST_P(ValidateIdWithMessage, OpLoadResultTypeBad) {
2993 std::string spirv = kGLSL450MemoryModel + R"(
2994 %1 = OpTypeVoid
2995 %2 = OpTypeInt 32 0
2996 %3 = OpTypePointer UniformConstant %2
2997 %4 = OpTypeFunction %1
2998 %5 = OpVariable %3 UniformConstant
2999 %6 = OpFunction %1 None %4
3000 %7 = OpLabel
3001 %8 = OpLoad %3 %5
3002 OpReturn
3003 OpFunctionEnd
3004 )";
3005 CompileSuccessfully(spirv.c_str());
3006 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3007 EXPECT_THAT(
3008 getDiagnosticString(),
3009 HasSubstr(make_message("OpLoad Result Type <id> "
3010 "'3[%_ptr_UniformConstant_uint]' does not match "
3011 "Pointer <id> '5[%5]'s type.")));
3012 }
3013
3014 TEST_P(ValidateIdWithMessage, OpLoadPointerBad) {
3015 std::string spirv = kGLSL450MemoryModel + R"(
3016 %1 = OpTypeVoid
3017 %2 = OpTypeInt 32 0
3018 %3 = OpTypePointer UniformConstant %2
3019 %4 = OpTypeFunction %1
3020 %5 = OpFunction %1 None %4
3021 %6 = OpLabel
3022 %7 = OpLoad %2 %8
3023 OpReturn
3024 OpFunctionEnd
3025 )";
3026 CompileSuccessfully(spirv.c_str());
3027 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3028 // Prove that SSA checks trigger for a bad Id value.
3029 // The next test case show the not-a-logical-pointer case.
3030 EXPECT_THAT(getDiagnosticString(),
3031 HasSubstr(make_message("ID '8[%8]' has not been "
3032 "defined")));
3033 }
3034
3035 // Disabled as bitcasting type to object is now not valid.
3036 TEST_P(ValidateIdWithMessage, DISABLED_OpLoadLogicalPointerBad) {
3037 std::string spirv = kGLSL450MemoryModel + R"(
3038 %1 = OpTypeVoid
3039 %2 = OpTypeInt 32 0
3040 %3 = OpTypeFloat 32
3041 %4 = OpTypePointer UniformConstant %2
3042 %5 = OpTypePointer UniformConstant %3
3043 %6 = OpTypeFunction %1
3044 %7 = OpFunction %1 None %6
3045 %8 = OpLabel
3046 %9 = OpBitcast %5 %4 ; Not valid in logical addressing
3047 %10 = OpLoad %3 %9 ; Should trigger message
3048 OpReturn
3049 OpFunctionEnd
3050 )";
3051 CompileSuccessfully(spirv.c_str());
3052 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3053 // Once we start checking bitcasts, we might catch that
3054 // as the error first, instead of catching it here.
3055 // I don't know if it's possible to generate a bad case
3056 // if/when the validator is complete.
3057 EXPECT_THAT(getDiagnosticString(),
3058 HasSubstr(make_message(
3059 "OpLoad Pointer <id> '9' is not a logical pointer.")));
3060 }
3061
3062 TEST_P(ValidateIdWithMessage, OpStoreGood) {
3063 std::string spirv = kGLSL450MemoryModel + R"(
3064 %1 = OpTypeVoid
3065 %2 = OpTypeInt 32 0
3066 %3 = OpTypePointer Uniform %2
3067 %4 = OpTypeFunction %1
3068 %5 = OpConstant %2 42
3069 %6 = OpVariable %3 Uniform
3070 %7 = OpFunction %1 None %4
3071 %8 = OpLabel
3072 OpStore %6 %5
3073 OpReturn
3074 OpFunctionEnd)";
3075 CompileSuccessfully(spirv.c_str());
3076 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3077 }
3078 TEST_P(ValidateIdWithMessage, OpStorePointerBad) {
3079 std::string spirv = kGLSL450MemoryModel + R"(
3080 %1 = OpTypeVoid
3081 %2 = OpTypeInt 32 0
3082 %3 = OpTypePointer UniformConstant %2
3083 %4 = OpTypeFunction %1
3084 %5 = OpConstant %2 42
3085 %6 = OpVariable %3 UniformConstant
3086 %7 = OpConstant %2 0
3087 %8 = OpFunction %1 None %4
3088 %9 = OpLabel
3089 OpStore %7 %5
3090 OpReturn
3091 OpFunctionEnd)";
3092 CompileSuccessfully(spirv.c_str());
3093 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3094 EXPECT_THAT(getDiagnosticString(),
3095 HasSubstr(make_message(
3096 "OpStore Pointer <id> '7[%uint_0]' is not a logical "
3097 "pointer.")));
3098 }
3099
3100 // Disabled as bitcasting type to object is now not valid.
3101 TEST_P(ValidateIdWithMessage, DISABLED_OpStoreLogicalPointerBad) {
3102 std::string spirv = kGLSL450MemoryModel + R"(
3103 %1 = OpTypeVoid
3104 %2 = OpTypeInt 32 0
3105 %3 = OpTypeFloat 32
3106 %4 = OpTypePointer UniformConstant %2
3107 %5 = OpTypePointer UniformConstant %3
3108 %6 = OpTypeFunction %1
3109 %7 = OpConstantNull %5
3110 %8 = OpFunction %1 None %6
3111 %9 = OpLabel
3112 %10 = OpBitcast %5 %4 ; Not valid in logical addressing
3113 %11 = OpStore %10 %7 ; Should trigger message
3114 OpReturn
3115 OpFunctionEnd
3116 )";
3117 CompileSuccessfully(spirv.c_str());
3118 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3119 EXPECT_THAT(getDiagnosticString(),
3120 HasSubstr(make_message(
3121 "OpStore Pointer <id> '10' is not a logical pointer.")));
3122 }
3123
3124 // Without the VariablePointer Capability, OpStore should may not store
3125 // through a variable pointer.
3126 // Disabled since using OpSelect with pointers without VariablePointers will
3127 // fail LogicalsPass.
3128 TEST_P(ValidateIdWithMessage, DISABLED_OpStoreVarPtrBad) {
3129 std::string result_strategy = R"(
3130 %isneg = OpSLessThan %bool %i %zero
3131 %varptr = OpSelect %f32ptr %isneg %ptr1 %ptr2
3132 OpStore %varptr %float_1
3133 )";
3134
3135 std::ostringstream spirv;
3136 createVariablePointerSpirvProgram(
3137 &spirv, result_strategy, false /* Add VariablePointers Capability? */,
3138 false /* Use Helper Function? */);
3139 CompileSuccessfully(spirv.str());
3140 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3141 EXPECT_THAT(getDiagnosticString(),
3142 HasSubstr(make_message("is not a logical pointer.")));
3143 }
3144
3145 // With the VariablePointer Capability, OpStore should allow storing through a
3146 // variable pointer.
3147 TEST_P(ValidateIdWithMessage, OpStoreVarPtrGood) {
3148 std::string result_strategy = R"(
3149 %isneg = OpSLessThan %bool %i %zero
3150 %varptr = OpSelect %f32ptr %isneg %ptr1 %ptr2
3151 OpStore %varptr %float_1
3152 )";
3153
3154 std::ostringstream spirv;
3155 createVariablePointerSpirvProgram(&spirv, result_strategy,
3156 true /* Add VariablePointers Capability? */,
3157 false /* Use Helper Function? */);
3158 CompileSuccessfully(spirv.str());
3159 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3160 }
3161
3162 TEST_P(ValidateIdWithMessage, OpStoreObjectGood) {
3163 std::string spirv = kGLSL450MemoryModel + R"(
3164 %1 = OpTypeVoid
3165 %2 = OpTypeInt 32 0
3166 %3 = OpTypePointer Uniform %2
3167 %4 = OpTypeFunction %1
3168 %5 = OpConstant %2 42
3169 %6 = OpVariable %3 Uniform
3170 %7 = OpFunction %1 None %4
3171 %8 = OpLabel
3172 %9 = OpFunctionCall %1 %10
3173 OpStore %6 %9
3174 OpReturn
3175 OpFunctionEnd
3176 %10 = OpFunction %1 None %4
3177 %11 = OpLabel
3178 OpReturn
3179 OpFunctionEnd)";
3180 CompileSuccessfully(spirv.c_str());
3181 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3182 EXPECT_THAT(
3183 getDiagnosticString(),
3184 HasSubstr(make_message("OpStore Object <id> '9[%9]'s type is void.")));
3185 }
3186 TEST_P(ValidateIdWithMessage, OpStoreTypeBad) {
3187 std::string spirv = kGLSL450MemoryModel + R"(
3188 %1 = OpTypeVoid
3189 %2 = OpTypeInt 32 0
3190 %9 = OpTypeFloat 32
3191 %3 = OpTypePointer Uniform %2
3192 %4 = OpTypeFunction %1
3193 %5 = OpConstant %9 3.14
3194 %6 = OpVariable %3 Uniform
3195 %7 = OpFunction %1 None %4
3196 %8 = OpLabel
3197 OpStore %6 %5
3198 OpReturn
3199 OpFunctionEnd)";
3200 CompileSuccessfully(spirv.c_str());
3201 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3202 EXPECT_THAT(getDiagnosticString(),
3203 HasSubstr(make_message(
3204 "OpStore Pointer <id> '7[%7]'s type does not match "
3205 "Object <id> '6[%float_3_1400001]'s type.")));
3206 }
3207
3208 // The next series of test check test a relaxation of the rules for stores to
3209 // structs. The first test checks that we get a failure when the option is not
3210 // set to relax the rule.
3211 // TODO: Add tests for layout compatible arrays and matricies when the validator
3212 // relaxes the rules for them as well. Also need test to check for layout
3213 // decorations specific to those types.
3214 TEST_P(ValidateIdWithMessage, OpStoreTypeBadStruct) {
3215 std::string spirv = kGLSL450MemoryModel + R"(
3216 OpMemberDecorate %1 0 Offset 0
3217 OpMemberDecorate %1 1 Offset 4
3218 OpMemberDecorate %2 0 Offset 0
3219 OpMemberDecorate %2 1 Offset 4
3220 %3 = OpTypeVoid
3221 %4 = OpTypeFloat 32
3222 %1 = OpTypeStruct %4 %4
3223 %5 = OpTypePointer Uniform %1
3224 %2 = OpTypeStruct %4 %4
3225 %6 = OpTypeFunction %3
3226 %7 = OpConstant %4 3.14
3227 %8 = OpVariable %5 Uniform
3228 %9 = OpFunction %3 None %6
3229 %10 = OpLabel
3230 %11 = OpCompositeConstruct %2 %7 %7
3231 OpStore %8 %11
3232 OpReturn
3233 OpFunctionEnd)";
3234 CompileSuccessfully(spirv.c_str());
3235 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3236 EXPECT_THAT(getDiagnosticString(),
3237 HasSubstr(make_message(
3238 "OpStore Pointer <id> '8[%8]'s type does not match "
3239 "Object <id> '11[%11]'s type.")));
3240 }
3241
3242 // Same code as the last test. The difference is that we relax the rule.
3243 // Because the structs %3 and %5 are defined the same way.
3244 TEST_P(ValidateIdWithMessage, OpStoreTypeRelaxedStruct) {
3245 std::string spirv = kGLSL450MemoryModel + R"(
3246 OpMemberDecorate %1 0 Offset 0
3247 OpMemberDecorate %1 1 Offset 4
3248 OpMemberDecorate %2 0 Offset 0
3249 OpMemberDecorate %2 1 Offset 4
3250 %3 = OpTypeVoid
3251 %4 = OpTypeFloat 32
3252 %1 = OpTypeStruct %4 %4
3253 %5 = OpTypePointer Uniform %1
3254 %2 = OpTypeStruct %4 %4
3255 %6 = OpTypeFunction %3
3256 %7 = OpConstant %4 3.14
3257 %8 = OpVariable %5 Uniform
3258 %9 = OpFunction %3 None %6
3259 %10 = OpLabel
3260 %11 = OpCompositeConstruct %2 %7 %7
3261 OpStore %8 %11
3262 OpReturn
3263 OpFunctionEnd)";
3264 spvValidatorOptionsSetRelaxStoreStruct(options_, true);
3265 CompileSuccessfully(spirv.c_str());
3266 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3267 }
3268
3269 // Same code as the last test except for an extra decoration on one of the
3270 // members. With the relaxed rules, the code is still valid.
3271 TEST_P(ValidateIdWithMessage, OpStoreTypeRelaxedStructWithExtraDecoration) {
3272 std::string spirv = kGLSL450MemoryModel + R"(
3273 OpMemberDecorate %1 0 Offset 0
3274 OpMemberDecorate %1 1 Offset 4
3275 OpMemberDecorate %1 0 RelaxedPrecision
3276 OpMemberDecorate %2 0 Offset 0
3277 OpMemberDecorate %2 1 Offset 4
3278 %3 = OpTypeVoid
3279 %4 = OpTypeFloat 32
3280 %1 = OpTypeStruct %4 %4
3281 %5 = OpTypePointer Uniform %1
3282 %2 = OpTypeStruct %4 %4
3283 %6 = OpTypeFunction %3
3284 %7 = OpConstant %4 3.14
3285 %8 = OpVariable %5 Uniform
3286 %9 = OpFunction %3 None %6
3287 %10 = OpLabel
3288 %11 = OpCompositeConstruct %2 %7 %7
3289 OpStore %8 %11
3290 OpReturn
3291 OpFunctionEnd)";
3292 spvValidatorOptionsSetRelaxStoreStruct(options_, true);
3293 CompileSuccessfully(spirv.c_str());
3294 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3295 }
3296
3297 // This test check that we recursively traverse the struct to check if they are
3298 // interchangable.
3299 TEST_P(ValidateIdWithMessage, OpStoreTypeRelaxedNestedStruct) {
3300 std::string spirv = kGLSL450MemoryModel + R"(
3301 OpMemberDecorate %1 0 Offset 0
3302 OpMemberDecorate %1 1 Offset 4
3303 OpMemberDecorate %2 0 Offset 0
3304 OpMemberDecorate %2 1 Offset 8
3305 OpMemberDecorate %3 0 Offset 0
3306 OpMemberDecorate %3 1 Offset 4
3307 OpMemberDecorate %4 0 Offset 0
3308 OpMemberDecorate %4 1 Offset 8
3309 %5 = OpTypeVoid
3310 %6 = OpTypeInt 32 0
3311 %7 = OpTypeFloat 32
3312 %1 = OpTypeStruct %7 %6
3313 %2 = OpTypeStruct %1 %1
3314 %8 = OpTypePointer Uniform %2
3315 %3 = OpTypeStruct %7 %6
3316 %4 = OpTypeStruct %3 %3
3317 %9 = OpTypeFunction %5
3318 %10 = OpConstant %6 7
3319 %11 = OpConstant %7 3.14
3320 %12 = OpConstantComposite %3 %11 %10
3321 %13 = OpVariable %8 Uniform
3322 %14 = OpFunction %5 None %9
3323 %15 = OpLabel
3324 %16 = OpCompositeConstruct %4 %12 %12
3325 OpStore %13 %16
3326 OpReturn
3327 OpFunctionEnd)";
3328 spvValidatorOptionsSetRelaxStoreStruct(options_, true);
3329 CompileSuccessfully(spirv.c_str());
3330 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3331 }
3332
3333 // This test check that the even with the relaxed rules an error is identified
3334 // if the members of the struct are in a different order.
3335 TEST_P(ValidateIdWithMessage, OpStoreTypeBadRelaxedStruct1) {
3336 std::string spirv = kGLSL450MemoryModel + R"(
3337 OpMemberDecorate %1 0 Offset 0
3338 OpMemberDecorate %1 1 Offset 4
3339 OpMemberDecorate %2 0 Offset 0
3340 OpMemberDecorate %2 1 Offset 8
3341 OpMemberDecorate %3 0 Offset 0
3342 OpMemberDecorate %3 1 Offset 4
3343 OpMemberDecorate %4 0 Offset 0
3344 OpMemberDecorate %4 1 Offset 8
3345 %5 = OpTypeVoid
3346 %6 = OpTypeInt 32 0
3347 %7 = OpTypeFloat 32
3348 %1 = OpTypeStruct %6 %7
3349 %2 = OpTypeStruct %1 %1
3350 %8 = OpTypePointer Uniform %2
3351 %3 = OpTypeStruct %7 %6
3352 %4 = OpTypeStruct %3 %3
3353 %9 = OpTypeFunction %5
3354 %10 = OpConstant %6 7
3355 %11 = OpConstant %7 3.14
3356 %12 = OpConstantComposite %3 %11 %10
3357 %13 = OpVariable %8 Uniform
3358 %14 = OpFunction %5 None %9
3359 %15 = OpLabel
3360 %16 = OpCompositeConstruct %4 %12 %12
3361 OpStore %13 %16
3362 OpReturn
3363 OpFunctionEnd)";
3364 spvValidatorOptionsSetRelaxStoreStruct(options_, true);
3365 CompileSuccessfully(spirv.c_str());
3366 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3367 EXPECT_THAT(
3368 getDiagnosticString(),
3369 HasSubstr(make_message(
3370 "OpStore Pointer <id> '13[%13]'s layout does not match Object "
3371 "<id> '16[%16]'s layout.")));
3372 }
3373
3374 // This test check that the even with the relaxed rules an error is identified
3375 // if the members of the struct are at different offsets.
3376 TEST_P(ValidateIdWithMessage, OpStoreTypeBadRelaxedStruct2) {
3377 std::string spirv = kGLSL450MemoryModel + R"(
3378 OpMemberDecorate %1 0 Offset 4
3379 OpMemberDecorate %1 1 Offset 0
3380 OpMemberDecorate %2 0 Offset 0
3381 OpMemberDecorate %2 1 Offset 8
3382 OpMemberDecorate %3 0 Offset 0
3383 OpMemberDecorate %3 1 Offset 4
3384 OpMemberDecorate %4 0 Offset 0
3385 OpMemberDecorate %4 1 Offset 8
3386 %5 = OpTypeVoid
3387 %6 = OpTypeInt 32 0
3388 %7 = OpTypeFloat 32
3389 %1 = OpTypeStruct %7 %6
3390 %2 = OpTypeStruct %1 %1
3391 %8 = OpTypePointer Uniform %2
3392 %3 = OpTypeStruct %7 %6
3393 %4 = OpTypeStruct %3 %3
3394 %9 = OpTypeFunction %5
3395 %10 = OpConstant %6 7
3396 %11 = OpConstant %7 3.14
3397 %12 = OpConstantComposite %3 %11 %10
3398 %13 = OpVariable %8 Uniform
3399 %14 = OpFunction %5 None %9
3400 %15 = OpLabel
3401 %16 = OpCompositeConstruct %4 %12 %12
3402 OpStore %13 %16
3403 OpReturn
3404 OpFunctionEnd)";
3405 spvValidatorOptionsSetRelaxStoreStruct(options_, true);
3406 CompileSuccessfully(spirv.c_str());
3407 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3408 EXPECT_THAT(
3409 getDiagnosticString(),
3410 HasSubstr(make_message(
3411 "OpStore Pointer <id> '13[%13]'s layout does not match Object "
3412 "<id> '16[%16]'s layout.")));
3413 }
3414
3415 TEST_P(ValidateIdWithMessage, OpStoreTypeRelaxedLogicalPointerReturnPointer) {
3416 const std::string spirv = R"(
3417 OpCapability Shader
3418 OpCapability Linkage
3419 OpMemoryModel Logical GLSL450
3420 %1 = OpTypeInt 32 1
3421 %2 = OpTypePointer Function %1
3422 %3 = OpTypeFunction %2 %2
3423 %4 = OpFunction %2 None %3
3424 %5 = OpFunctionParameter %2
3425 %6 = OpLabel
3426 OpReturnValue %5
3427 OpFunctionEnd)";
3428
3429 spvValidatorOptionsSetRelaxLogicalPointer(options_, true);
3430 CompileSuccessfully(spirv.c_str());
3431 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3432 }
3433
3434 TEST_P(ValidateIdWithMessage, OpStoreTypeRelaxedLogicalPointerAllocPointer) {
3435 const std::string spirv = R"(
3436 OpCapability Shader
3437 OpCapability Linkage
3438 OpMemoryModel Logical GLSL450
3439 %1 = OpTypeVoid
3440 %2 = OpTypeInt 32 1
3441 %3 = OpTypeFunction %1 ; void(void)
3442 %4 = OpTypePointer Uniform %2 ; int*
3443 %5 = OpTypePointer Private %4 ; int** (Private)
3444 %6 = OpTypePointer Function %4 ; int** (Function)
3445 %7 = OpVariable %5 Private
3446 %8 = OpFunction %1 None %3
3447 %9 = OpLabel
3448 %10 = OpVariable %6 Function
3449 OpReturn
3450 OpFunctionEnd)";
3451
3452 spvValidatorOptionsSetRelaxLogicalPointer(options_, true);
3453 CompileSuccessfully(spirv.c_str());
3454 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3455 }
3456
3457 TEST_P(ValidateIdWithMessage, OpStoreVoid) {
3458 std::string spirv = kGLSL450MemoryModel + R"(
3459 %1 = OpTypeVoid
3460 %2 = OpTypeInt 32 0
3461 %3 = OpTypePointer Uniform %2
3462 %4 = OpTypeFunction %1
3463 %6 = OpVariable %3 Uniform
3464 %7 = OpFunction %1 None %4
3465 %8 = OpLabel
3466 %9 = OpFunctionCall %1 %7
3467 OpStore %6 %9
3468 OpReturn
3469 OpFunctionEnd)";
3470 CompileSuccessfully(spirv.c_str());
3471 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3472 EXPECT_THAT(
3473 getDiagnosticString(),
3474 HasSubstr(make_message("OpStore Object <id> '8[%8]'s type is void.")));
3475 }
3476
3477 TEST_P(ValidateIdWithMessage, OpStoreLabel) {
3478 std::string spirv = kGLSL450MemoryModel + R"(
3479 %1 = OpTypeVoid
3480 %2 = OpTypeInt 32 0
3481 %3 = OpTypePointer Uniform %2
3482 %4 = OpTypeFunction %1
3483 %6 = OpVariable %3 Uniform
3484 %7 = OpFunction %1 None %4
3485 %8 = OpLabel
3486 OpStore %6 %8
3487 OpReturn
3488 OpFunctionEnd)";
3489 CompileSuccessfully(spirv.c_str());
3490 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3491 EXPECT_THAT(getDiagnosticString(),
3492 HasSubstr(make_message("Operand '7[%7]' requires a type")));
3493 }
3494
3495 // TODO: enable when this bug is fixed:
3496 // https://cvs.khronos.org/bugzilla/show_bug.cgi?id=15404
3497 TEST_P(ValidateIdWithMessage, DISABLED_OpStoreFunction) {
3498 std::string spirv = kGLSL450MemoryModel + R"(
3499 %2 = OpTypeInt 32 0
3500 %3 = OpTypePointer UniformConstant %2
3501 %4 = OpTypeFunction %2
3502 %5 = OpConstant %2 123
3503 %6 = OpVariable %3 UniformConstant
3504 %7 = OpFunction %2 None %4
3505 %8 = OpLabel
3506 OpStore %6 %7
3507 OpReturnValue %5
3508 OpFunctionEnd)";
3509 CompileSuccessfully(spirv.c_str());
3510 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3511 }
3512
3513 TEST_P(ValidateIdWithMessage, OpStoreBuiltin) {
3514 std::string spirv = R"(
3515 OpCapability Shader
3516 %1 = OpExtInstImport "GLSL.std.450"
3517 OpMemoryModel Logical GLSL450
3518 OpEntryPoint GLCompute %main "main" %gl_GlobalInvocationID
3519 OpExecutionMode %main LocalSize 1 1 1
3520 OpSource GLSL 450
3521 OpName %main "main"
3522
3523 OpName %gl_GlobalInvocationID "gl_GlobalInvocationID"
3524 OpDecorate %gl_GlobalInvocationID BuiltIn GlobalInvocationId
3525
3526 %int = OpTypeInt 32 1
3527 %uint = OpTypeInt 32 0
3528 %v3uint = OpTypeVector %uint 3
3529 %_ptr_Input_v3uint = OpTypePointer Input %v3uint
3530 %gl_GlobalInvocationID = OpVariable %_ptr_Input_v3uint Input
3531
3532 %zero = OpConstant %uint 0
3533 %v3uint_000 = OpConstantComposite %v3uint %zero %zero %zero
3534
3535 %void = OpTypeVoid
3536 %voidfunc = OpTypeFunction %void
3537 %main = OpFunction %void None %voidfunc
3538 %lmain = OpLabel
3539
3540 OpStore %gl_GlobalInvocationID %v3uint_000
3541
3542 OpReturn
3543 OpFunctionEnd
3544 )";
3545
3546 CompileSuccessfully(spirv.c_str());
3547 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3548 EXPECT_THAT(getDiagnosticString(),
3549 HasSubstr(make_message("storage class is read-only")));
3550 }
3551
3552 TEST_P(ValidateIdWithMessage, OpCopyMemoryGood) {
3553 std::string spirv = kGLSL450MemoryModel + R"(
3554 %1 = OpTypeVoid
3555 %2 = OpTypeInt 32 0
3556 %3 = OpTypePointer UniformConstant %2
3557 %4 = OpConstant %2 42
3558 %5 = OpVariable %3 UniformConstant %4
3559 %6 = OpTypePointer Function %2
3560 %7 = OpTypeFunction %1
3561 %8 = OpFunction %1 None %7
3562 %9 = OpLabel
3563 %10 = OpVariable %6 Function
3564 OpCopyMemory %10 %5 None
3565 OpReturn
3566 OpFunctionEnd
3567 )";
3568 CompileSuccessfully(spirv.c_str());
3569 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3570 }
3571
3572 TEST_P(ValidateIdWithMessage, OpCopyMemoryNonPointerTarget) {
3573 const std::string spirv = kGLSL450MemoryModel + R"(
3574 %1 = OpTypeVoid
3575 %2 = OpTypeInt 32 0
3576 %3 = OpTypePointer Uniform %2
3577 %4 = OpTypeFunction %1 %2 %3
3578 %5 = OpFunction %1 None %4
3579 %6 = OpFunctionParameter %2
3580 %7 = OpFunctionParameter %3
3581 %8 = OpLabel
3582 OpCopyMemory %6 %7
3583 OpReturn
3584 OpFunctionEnd
3585 )";
3586
3587 CompileSuccessfully(spirv);
3588 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3589 EXPECT_THAT(
3590 getDiagnosticString(),
3591 HasSubstr(make_message("Target operand <id> '6[%6]' is not a pointer.")));
3592 }
3593
3594 TEST_P(ValidateIdWithMessage, OpCopyMemoryNonPointerSource) {
3595 const std::string spirv = kGLSL450MemoryModel + R"(
3596 %1 = OpTypeVoid
3597 %2 = OpTypeInt 32 0
3598 %3 = OpTypePointer Uniform %2
3599 %4 = OpTypeFunction %1 %2 %3
3600 %5 = OpFunction %1 None %4
3601 %6 = OpFunctionParameter %2
3602 %7 = OpFunctionParameter %3
3603 %8 = OpLabel
3604 OpCopyMemory %7 %6
3605 OpReturn
3606 OpFunctionEnd
3607 )";
3608
3609 CompileSuccessfully(spirv);
3610 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3611 EXPECT_THAT(
3612 getDiagnosticString(),
3613 HasSubstr(make_message("Source operand <id> '6[%6]' is not a pointer.")));
3614 }
3615
3616 TEST_P(ValidateIdWithMessage, OpCopyMemoryBad) {
3617 std::string spirv = kGLSL450MemoryModel + R"(
3618 %1 = OpTypeVoid
3619 %2 = OpTypeInt 32 0
3620 %3 = OpTypePointer UniformConstant %2
3621 %4 = OpConstant %2 42
3622 %5 = OpVariable %3 UniformConstant %4
3623 %11 = OpTypeFloat 32
3624 %6 = OpTypePointer Function %11
3625 %7 = OpTypeFunction %1
3626 %8 = OpFunction %1 None %7
3627 %9 = OpLabel
3628 %10 = OpVariable %6 Function
3629 OpCopyMemory %10 %5 None
3630 OpReturn
3631 OpFunctionEnd
3632 )";
3633 CompileSuccessfully(spirv.c_str());
3634 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3635 EXPECT_THAT(getDiagnosticString(),
3636 HasSubstr(make_message("Target <id> '5[%5]'s type does not match "
3637 "Source <id> '2[%uint]'s type.")));
3638 }
3639
3640 TEST_P(ValidateIdWithMessage, OpCopyMemoryVoidTarget) {
3641 const std::string spirv = kGLSL450MemoryModel + R"(
3642 %1 = OpTypeVoid
3643 %2 = OpTypeInt 32 0
3644 %3 = OpTypePointer Uniform %1
3645 %4 = OpTypePointer Uniform %2
3646 %5 = OpTypeFunction %1 %3 %4
3647 %6 = OpFunction %1 None %5
3648 %7 = OpFunctionParameter %3
3649 %8 = OpFunctionParameter %4
3650 %9 = OpLabel
3651 OpCopyMemory %7 %8
3652 OpReturn
3653 OpFunctionEnd
3654 )";
3655
3656 CompileSuccessfully(spirv);
3657 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3658 EXPECT_THAT(
3659 getDiagnosticString(),
3660 HasSubstr(make_message("Target operand <id> '7[%7]' cannot be a void "
3661 "pointer.")));
3662 }
3663
3664 TEST_P(ValidateIdWithMessage, OpCopyMemoryVoidSource) {
3665 const std::string spirv = kGLSL450MemoryModel + R"(
3666 %1 = OpTypeVoid
3667 %2 = OpTypeInt 32 0
3668 %3 = OpTypePointer Uniform %1
3669 %4 = OpTypePointer Uniform %2
3670 %5 = OpTypeFunction %1 %3 %4
3671 %6 = OpFunction %1 None %5
3672 %7 = OpFunctionParameter %3
3673 %8 = OpFunctionParameter %4
3674 %9 = OpLabel
3675 OpCopyMemory %8 %7
3676 OpReturn
3677 OpFunctionEnd
3678 )";
3679
3680 CompileSuccessfully(spirv);
3681 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3682 EXPECT_THAT(
3683 getDiagnosticString(),
3684 HasSubstr(make_message("Source operand <id> '7[%7]' cannot be a void "
3685 "pointer.")));
3686 }
3687
3688 TEST_P(ValidateIdWithMessage, OpCopyMemorySizedGood) {
3689 std::string spirv = kGLSL450MemoryModel + R"(
3690 %1 = OpTypeVoid
3691 %2 = OpTypeInt 32 0
3692 %3 = OpTypePointer UniformConstant %2
3693 %4 = OpTypePointer Function %2
3694 %5 = OpConstant %2 4
3695 %6 = OpVariable %3 UniformConstant %5
3696 %7 = OpTypeFunction %1
3697 %8 = OpFunction %1 None %7
3698 %9 = OpLabel
3699 %10 = OpVariable %4 Function
3700 OpCopyMemorySized %10 %6 %5 None
3701 OpReturn
3702 OpFunctionEnd)";
3703 CompileSuccessfully(spirv.c_str());
3704 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3705 }
3706 TEST_P(ValidateIdWithMessage, OpCopyMemorySizedTargetBad) {
3707 std::string spirv = kGLSL450MemoryModel + R"(
3708 %1 = OpTypeVoid
3709 %2 = OpTypeInt 32 0
3710 %3 = OpTypePointer UniformConstant %2
3711 %4 = OpTypePointer Function %2
3712 %5 = OpConstant %2 4
3713 %6 = OpVariable %3 UniformConstant %5
3714 %7 = OpTypeFunction %1
3715 %8 = OpFunction %1 None %7
3716 %9 = OpLabel
3717 OpCopyMemorySized %5 %5 %5 None
3718 OpReturn
3719 OpFunctionEnd)";
3720 CompileSuccessfully(spirv.c_str());
3721 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3722 EXPECT_THAT(getDiagnosticString(),
3723 HasSubstr(make_message(
3724 "Target operand <id> '5[%uint_4]' is not a pointer.")));
3725 }
3726 TEST_P(ValidateIdWithMessage, OpCopyMemorySizedSourceBad) {
3727 std::string spirv = kGLSL450MemoryModel + R"(
3728 %1 = OpTypeVoid
3729 %2 = OpTypeInt 32 0
3730 %3 = OpTypePointer UniformConstant %2
3731 %4 = OpTypePointer Function %2
3732 %5 = OpConstant %2 4
3733 %6 = OpTypeFunction %1
3734 %7 = OpFunction %1 None %6
3735 %8 = OpLabel
3736 %9 = OpVariable %4 Function
3737 OpCopyMemorySized %9 %5 %5 None
3738 OpReturn
3739 OpFunctionEnd)";
3740 CompileSuccessfully(spirv.c_str());
3741 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3742 EXPECT_THAT(getDiagnosticString(),
3743 HasSubstr(make_message(
3744 "Source operand <id> '5[%uint_4]' is not a pointer.")));
3745 }
3746 TEST_P(ValidateIdWithMessage, OpCopyMemorySizedSizeBad) {
3747 std::string spirv = kGLSL450MemoryModel + R"(
3748 %1 = OpTypeVoid
3749 %2 = OpTypeInt 32 0
3750 %3 = OpTypePointer UniformConstant %2
3751 %4 = OpTypePointer Function %2
3752 %5 = OpConstant %2 4
3753 %6 = OpVariable %3 UniformConstant %5
3754 %7 = OpTypeFunction %1
3755 %8 = OpFunction %1 None %7
3756 %9 = OpLabel
3757 %10 = OpVariable %4 Function
3758 OpCopyMemorySized %10 %6 %6 None
3759 OpReturn
3760 OpFunctionEnd)";
3761 CompileSuccessfully(spirv.c_str());
3762 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3763 EXPECT_THAT(getDiagnosticString(),
3764 HasSubstr(make_message(
3765 "Size operand <id> '6[%6]' must be a scalar integer type.")));
3766 }
3767 TEST_P(ValidateIdWithMessage, OpCopyMemorySizedSizeTypeBad) {
3768 std::string spirv = kGLSL450MemoryModel + R"(
3769 %1 = OpTypeVoid
3770 %2 = OpTypeInt 32 0
3771 %3 = OpTypePointer UniformConstant %2
3772 %4 = OpTypePointer Function %2
3773 %5 = OpConstant %2 4
3774 %6 = OpVariable %3 UniformConstant %5
3775 %7 = OpTypeFunction %1
3776 %11 = OpTypeFloat 32
3777 %12 = OpConstant %11 1.0
3778 %8 = OpFunction %1 None %7
3779 %9 = OpLabel
3780 %10 = OpVariable %4 Function
3781 OpCopyMemorySized %10 %6 %12 None
3782 OpReturn
3783 OpFunctionEnd)";
3784 CompileSuccessfully(spirv.c_str());
3785 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3786 EXPECT_THAT(getDiagnosticString(),
3787 HasSubstr(make_message(
3788 "Size operand <id> '9[%float_1]' must be a scalar integer "
3789 "type.")));
3790 }
3791
3792 TEST_P(ValidateIdWithMessage, OpCopyMemorySizedSizeConstantNull) {
3793 const std::string spirv = kGLSL450MemoryModel + R"(
3794 %1 = OpTypeVoid
3795 %2 = OpTypeInt 32 0
3796 %3 = OpConstantNull %2
3797 %4 = OpTypePointer Uniform %2
3798 %5 = OpTypeFloat 32
3799 %6 = OpTypePointer UniformConstant %5
3800 %7 = OpTypeFunction %1 %4 %6
3801 %8 = OpFunction %1 None %7
3802 %9 = OpFunctionParameter %4
3803 %10 = OpFunctionParameter %6
3804 %11 = OpLabel
3805 OpCopyMemorySized %9 %10 %3
3806 OpReturn
3807 OpFunctionEnd
3808 )";
3809
3810 CompileSuccessfully(spirv);
3811 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3812 EXPECT_THAT(
3813 getDiagnosticString(),
3814 HasSubstr(make_message("Size operand <id> '3[%3]' cannot be a constant "
3815 "zero.")));
3816 }
3817
3818 TEST_P(ValidateIdWithMessage, OpCopyMemorySizedSizeConstantZero) {
3819 const std::string spirv = kGLSL450MemoryModel + R"(
3820 %1 = OpTypeVoid
3821 %2 = OpTypeInt 32 0
3822 %3 = OpConstant %2 0
3823 %4 = OpTypePointer Uniform %2
3824 %5 = OpTypeFloat 32
3825 %6 = OpTypePointer UniformConstant %5
3826 %7 = OpTypeFunction %1 %4 %6
3827 %8 = OpFunction %1 None %7
3828 %9 = OpFunctionParameter %4
3829 %10 = OpFunctionParameter %6
3830 %11 = OpLabel
3831 OpCopyMemorySized %9 %10 %3
3832 OpReturn
3833 OpFunctionEnd
3834 )";
3835
3836 CompileSuccessfully(spirv);
3837 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3838 EXPECT_THAT(getDiagnosticString(),
3839 HasSubstr(make_message(
3840 "Size operand <id> '3[%uint_0]' cannot be a constant "
3841 "zero.")));
3842 }
3843
3844 TEST_P(ValidateIdWithMessage, OpCopyMemorySizedSizeConstantZero64) {
3845 const std::string spirv = kGLSL450MemoryModel + R"(
3846 %1 = OpTypeVoid
3847 %2 = OpTypeInt 64 0
3848 %3 = OpConstant %2 0
3849 %4 = OpTypePointer Uniform %2
3850 %5 = OpTypeFloat 32
3851 %6 = OpTypePointer UniformConstant %5
3852 %7 = OpTypeFunction %1 %4 %6
3853 %8 = OpFunction %1 None %7
3854 %9 = OpFunctionParameter %4
3855 %10 = OpFunctionParameter %6
3856 %11 = OpLabel
3857 OpCopyMemorySized %9 %10 %3
3858 OpReturn
3859 OpFunctionEnd
3860 )";
3861
3862 CompileSuccessfully(spirv);
3863 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3864 EXPECT_THAT(getDiagnosticString(),
3865 HasSubstr(make_message(
3866 "Size operand <id> '3[%ulong_0]' cannot be a constant "
3867 "zero.")));
3868 }
3869
3870 TEST_P(ValidateIdWithMessage, OpCopyMemorySizedSizeConstantNegative) {
3871 const std::string spirv = kNoKernelGLSL450MemoryModel + R"(
3872 %1 = OpTypeVoid
3873 %2 = OpTypeInt 32 1
3874 %3 = OpConstant %2 -1
3875 %4 = OpTypePointer Uniform %2
3876 %5 = OpTypeFloat 32
3877 %6 = OpTypePointer UniformConstant %5
3878 %7 = OpTypeFunction %1 %4 %6
3879 %8 = OpFunction %1 None %7
3880 %9 = OpFunctionParameter %4
3881 %10 = OpFunctionParameter %6
3882 %11 = OpLabel
3883 OpCopyMemorySized %9 %10 %3
3884 OpReturn
3885 OpFunctionEnd
3886 )";
3887
3888 CompileSuccessfully(spirv);
3889 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3890 EXPECT_THAT(getDiagnosticString(),
3891 HasSubstr(make_message(
3892 "Size operand <id> '3[%int_n1]' cannot have the sign bit set "
3893 "to 1.")));
3894 }
3895
3896 TEST_P(ValidateIdWithMessage, OpCopyMemorySizedSizeConstantNegative64) {
3897 const std::string spirv = kNoKernelGLSL450MemoryModel + R"(
3898 %1 = OpTypeVoid
3899 %2 = OpTypeInt 64 1
3900 %3 = OpConstant %2 -1
3901 %4 = OpTypePointer Uniform %2
3902 %5 = OpTypeFloat 32
3903 %6 = OpTypePointer UniformConstant %5
3904 %7 = OpTypeFunction %1 %4 %6
3905 %8 = OpFunction %1 None %7
3906 %9 = OpFunctionParameter %4
3907 %10 = OpFunctionParameter %6
3908 %11 = OpLabel
3909 OpCopyMemorySized %9 %10 %3
3910 OpReturn
3911 OpFunctionEnd
3912 )";
3913
3914 CompileSuccessfully(spirv);
3915 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3916 EXPECT_THAT(
3917 getDiagnosticString(),
3918 HasSubstr(make_message(
3919 "Size operand <id> '3[%long_n1]' cannot have the sign bit set "
3920 "to 1.")));
3921 }
3922
3923 TEST_P(ValidateIdWithMessage, OpCopyMemorySizedSizeUnsignedNegative) {
3924 const std::string spirv = kGLSL450MemoryModel + R"(
3925 %1 = OpTypeVoid
3926 %2 = OpTypeInt 32 0
3927 %3 = OpConstant %2 2147483648
3928 %4 = OpTypePointer Uniform %2
3929 %5 = OpTypeFloat 32
3930 %6 = OpTypePointer UniformConstant %5
3931 %7 = OpTypeFunction %1 %4 %6
3932 %8 = OpFunction %1 None %7
3933 %9 = OpFunctionParameter %4
3934 %10 = OpFunctionParameter %6
3935 %11 = OpLabel
3936 OpCopyMemorySized %9 %10 %3
3937 OpReturn
3938 OpFunctionEnd
3939 )";
3940
3941 CompileSuccessfully(spirv);
3942 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3943 }
3944
3945 TEST_P(ValidateIdWithMessage, OpCopyMemorySizedSizeUnsignedNegative64) {
3946 const std::string spirv = kGLSL450MemoryModel + R"(
3947 %1 = OpTypeVoid
3948 %2 = OpTypeInt 64 0
3949 %3 = OpConstant %2 9223372036854775808
3950 %4 = OpTypePointer Uniform %2
3951 %5 = OpTypeFloat 32
3952 %6 = OpTypePointer UniformConstant %5
3953 %7 = OpTypeFunction %1 %4 %6
3954 %8 = OpFunction %1 None %7
3955 %9 = OpFunctionParameter %4
3956 %10 = OpFunctionParameter %6
3957 %11 = OpLabel
3958 OpCopyMemorySized %9 %10 %3
3959 OpReturn
3960 OpFunctionEnd
3961 )";
3962
3963 CompileSuccessfully(spirv);
3964 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3965 }
3966
3967 const char kDeeplyNestedStructureSetup[] = R"(
3968 %void = OpTypeVoid
3969 %void_f = OpTypeFunction %void
3970 %int = OpTypeInt 32 0
3971 %float = OpTypeFloat 32
3972 %v3float = OpTypeVector %float 3
3973 %mat4x3 = OpTypeMatrix %v3float 4
3974 %_ptr_Private_mat4x3 = OpTypePointer Private %mat4x3
3975 %_ptr_Private_float = OpTypePointer Private %float
3976 %my_matrix = OpVariable %_ptr_Private_mat4x3 Private
3977 %my_float_var = OpVariable %_ptr_Private_float Private
3978 %_ptr_Function_float = OpTypePointer Function %float
3979 %int_0 = OpConstant %int 0
3980 %int_1 = OpConstant %int 1
3981 %int_2 = OpConstant %int 2
3982 %int_3 = OpConstant %int 3
3983 %int_5 = OpConstant %int 5
3984
3985 ; Making the following nested structures.
3986 ;
3987 ; struct S {
3988 ; bool b;
3989 ; vec4 v[5];
3990 ; int i;
3991 ; mat4x3 m[5];
3992 ; }
3993 ; uniform blockName {
3994 ; S s;
3995 ; bool cond;
3996 ; RunTimeArray arr;
3997 ; }
3998
3999 %f32arr = OpTypeRuntimeArray %float
4000 %v4float = OpTypeVector %float 4
4001 %array5_mat4x3 = OpTypeArray %mat4x3 %int_5
4002 %array5_vec4 = OpTypeArray %v4float %int_5
4003 %_ptr_Uniform_float = OpTypePointer Uniform %float
4004 %_ptr_Function_vec4 = OpTypePointer Function %v4float
4005 %_ptr_Uniform_vec4 = OpTypePointer Uniform %v4float
4006 %struct_s = OpTypeStruct %int %array5_vec4 %int %array5_mat4x3
4007 %struct_blockName = OpTypeStruct %struct_s %int %f32arr
4008 %_ptr_Uniform_blockName = OpTypePointer Uniform %struct_blockName
4009 %_ptr_Uniform_struct_s = OpTypePointer Uniform %struct_s
4010 %_ptr_Uniform_array5_mat4x3 = OpTypePointer Uniform %array5_mat4x3
4011 %_ptr_Uniform_mat4x3 = OpTypePointer Uniform %mat4x3
4012 %_ptr_Uniform_v3float = OpTypePointer Uniform %v3float
4013 %blockName_var = OpVariable %_ptr_Uniform_blockName Uniform
4014 %spec_int = OpSpecConstant %int 2
4015 %float_0 = OpConstant %float 0
4016 %func = OpFunction %void None %void_f
4017 %my_label = OpLabel
4018 )";
4019
4020 // In what follows, Access Chain Instruction refers to one of the following:
4021 // OpAccessChain, OpInBoundsAccessChain, OpPtrAccessChain, and
4022 // OpInBoundsPtrAccessChain
4023 using AccessChainInstructionTest = spvtest::ValidateBase<std::string>;
4024
4025 // Determines whether the access chain instruction requires the 'element id'
4026 // argument.
4027 bool AccessChainRequiresElemId(const std::string& instr) {
4028 return (instr == "OpPtrAccessChain" || instr == "OpInBoundsPtrAccessChain");
4029 }
4030
4031 // Valid: Access a float in a matrix using an access chain instruction.
4032 TEST_P(AccessChainInstructionTest, AccessChainGood) {
4033 const std::string instr = GetParam();
4034 const std::string elem = AccessChainRequiresElemId(instr) ? "%int_0 " : "";
4035 std::string spirv = kGLSL450MemoryModel + kDeeplyNestedStructureSetup +
4036 "%float_entry = " + instr +
4037 R"( %_ptr_Private_float %my_matrix )" + elem +
4038 R"(%int_0 %int_1
4039 OpReturn
4040 OpFunctionEnd
4041 )";
4042 getValidatorOptions()->relax_logical_pointer = true;
4043 CompileSuccessfully(spirv);
4044 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4045 }
4046
4047 // Invalid. The result type of an access chain instruction must be a pointer.
4048 TEST_P(AccessChainInstructionTest, AccessChainResultTypeBad) {
4049 const std::string instr = GetParam();
4050 const std::string elem = AccessChainRequiresElemId(instr) ? "%int_0 " : "";
4051 std::string spirv = kGLSL450MemoryModel + kDeeplyNestedStructureSetup + R"(
4052 %float_entry = )" + instr +
4053 R"( %float %my_matrix )" + elem +
4054 R"(%int_0 %int_1
4055 OpReturn
4056 OpFunctionEnd
4057 )";
4058
4059 const std::string expected_err = "The Result Type of " + instr +
4060 " <id> '36[%36]' must be "
4061 "OpTypePointer. Found OpTypeFloat.";
4062 getValidatorOptions()->relax_logical_pointer = true;
4063 CompileSuccessfully(spirv);
4064 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4065 EXPECT_THAT(getDiagnosticString(), HasSubstr(expected_err));
4066 }
4067
4068 // Invalid. The base type of an access chain instruction must be a pointer.
4069 TEST_P(AccessChainInstructionTest, AccessChainBaseTypeVoidBad) {
4070 const std::string instr = GetParam();
4071 const std::string elem = AccessChainRequiresElemId(instr) ? "%int_0 " : "";
4072 std::string spirv = kGLSL450MemoryModel + kDeeplyNestedStructureSetup + R"(
4073 %float_entry = )" + instr +
4074 " %_ptr_Private_float %void " + elem +
4075 R"(%int_0 %int_1
4076 OpReturn
4077 OpFunctionEnd
4078 )";
4079 getValidatorOptions()->relax_logical_pointer = true;
4080 CompileSuccessfully(spirv);
4081 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4082 EXPECT_THAT(getDiagnosticString(), HasSubstr("Operand '1[%void]' cannot be a "
4083 "type"));
4084 }
4085
4086 // Invalid. The base type of an access chain instruction must be a pointer.
4087 TEST_P(AccessChainInstructionTest, AccessChainBaseTypeNonPtrVariableBad) {
4088 const std::string instr = GetParam();
4089 const std::string elem = AccessChainRequiresElemId(instr) ? "%int_0 " : "";
4090 std::string spirv = kGLSL450MemoryModel + kDeeplyNestedStructureSetup + R"(
4091 %entry = )" + instr + R"( %_ptr_Private_float %_ptr_Private_float )" +
4092 elem +
4093 R"(%int_0 %int_1
4094 OpReturn
4095 OpFunctionEnd
4096 )";
4097 getValidatorOptions()->relax_logical_pointer = true;
4098 CompileSuccessfully(spirv);
4099 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4100 EXPECT_THAT(getDiagnosticString(),
4101 HasSubstr("Operand '8[%_ptr_Private_float]' cannot be a type"));
4102 }
4103
4104 // Invalid: The storage class of Base and Result do not match.
4105 TEST_P(AccessChainInstructionTest,
4106 AccessChainResultAndBaseStorageClassDoesntMatchBad) {
4107 const std::string instr = GetParam();
4108 const std::string elem = AccessChainRequiresElemId(instr) ? "%int_0 " : "";
4109 std::string spirv = kGLSL450MemoryModel + kDeeplyNestedStructureSetup + R"(
4110 %entry = )" + instr + R"( %_ptr_Function_float %my_matrix )" +
4111 elem +
4112 R"(%int_0 %int_1
4113 OpReturn
4114 OpFunctionEnd
4115 )";
4116 const std::string expected_err =
4117 "The result pointer storage class and base pointer storage class in " +
4118 instr + " do not match.";
4119 getValidatorOptions()->relax_logical_pointer = true;
4120 CompileSuccessfully(spirv);
4121 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4122 EXPECT_THAT(getDiagnosticString(), HasSubstr(expected_err));
4123 }
4124
4125 // Invalid. The base type of an access chain instruction must point to a
4126 // composite object.
4127 TEST_P(AccessChainInstructionTest,
4128 AccessChainBasePtrNotPointingToCompositeBad) {
4129 const std::string instr = GetParam();
4130 const std::string elem = AccessChainRequiresElemId(instr) ? "%int_0 " : "";
4131 std::string spirv = kGLSL450MemoryModel + kDeeplyNestedStructureSetup + R"(
4132 %entry = )" + instr + R"( %_ptr_Private_float %my_float_var )" +
4133 elem +
4134 R"(%int_0
4135 OpReturn
4136 OpFunctionEnd
4137 )";
4138 const std::string expected_err = instr +
4139 " reached non-composite type while "
4140 "indexes still remain to be traversed.";
4141 getValidatorOptions()->relax_logical_pointer = true;
4142 CompileSuccessfully(spirv);
4143 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4144 EXPECT_THAT(getDiagnosticString(), HasSubstr(expected_err));
4145 }
4146
4147 // Valid. No Indexes were passed to the access chain instruction. The Result
4148 // Type is the same as the Base type.
4149 TEST_P(AccessChainInstructionTest, AccessChainNoIndexesGood) {
4150 const std::string instr = GetParam();
4151 const std::string elem = AccessChainRequiresElemId(instr) ? "%int_0 " : "";
4152 std::string spirv = kGLSL450MemoryModel + kDeeplyNestedStructureSetup + R"(
4153 %entry = )" + instr + R"( %_ptr_Private_float %my_float_var )" +
4154 elem +
4155 R"(
4156 OpReturn
4157 OpFunctionEnd
4158 )";
4159 getValidatorOptions()->relax_logical_pointer = true;
4160 CompileSuccessfully(spirv);
4161 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4162 }
4163
4164 // Invalid. No Indexes were passed to the access chain instruction, but the
4165 // Result Type is different from the Base type.
4166 TEST_P(AccessChainInstructionTest, AccessChainNoIndexesBad) {
4167 const std::string instr = GetParam();
4168 const std::string elem = AccessChainRequiresElemId(instr) ? "%int_0 " : "";
4169 std::string spirv = kGLSL450MemoryModel + kDeeplyNestedStructureSetup + R"(
4170 %entry = )" + instr + R"( %_ptr_Private_mat4x3 %my_float_var )" +
4171 elem +
4172 R"(
4173 OpReturn
4174 OpFunctionEnd
4175 )";
4176 getValidatorOptions()->relax_logical_pointer = true;
4177 CompileSuccessfully(spirv);
4178 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4179 EXPECT_THAT(getDiagnosticString(),
4180 HasSubstr("result type <id> '6[%mat4v3float]' (OpTypeMatrix) "
4181 "does not match the type that results from indexing "
4182 "into the base <id> '4[%float]' (OpTypeFloat)"));
4183 }
4184
4185 TEST_P(AccessChainInstructionTest, AccessChainDifferentIntTypes) {
4186 std::string spirv = R"(
4187 OpCapability Shader
4188 OpMemoryModel Logical GLSL450
4189 OpEntryPoint GLCompute %main "main" %_
4190 OpExecutionMode %main LocalSize 1 1 1
4191 OpDecorate %_arr_uint_uint_32 ArrayStride 4
4192 OpDecorate %SSBO Block
4193 OpMemberDecorate %SSBO 0 Offset 0
4194 OpDecorate %_ Binding 0
4195 OpDecorate %_ DescriptorSet 0
4196 %void = OpTypeVoid
4197 %4 = OpTypeFunction %void
4198 %uint = OpTypeInt 32 0
4199 %uint_32 = OpConstant %uint 32
4200 %_arr_uint_uint_32 = OpTypeArray %uint %uint_32
4201 %SSBO = OpTypeStruct %_arr_uint_uint_32
4202 %_ptr_StorageBuffer_SSBO = OpTypePointer StorageBuffer %SSBO
4203 %_ = OpVariable %_ptr_StorageBuffer_SSBO StorageBuffer
4204 %int = OpTypeInt 32 1
4205 %int_0 = OpConstant %int 0
4206 %uint_1 = OpConstant %uint 1
4207 %ptr_ssbo_int = OpTypePointer StorageBuffer %int
4208 %main = OpFunction %void None %4
4209 %6 = OpLabel
4210 %18 = OpAccessChain %ptr_ssbo_int %_ %int_0 %int_0
4211 OpReturn
4212 OpFunctionEnd
4213 )";
4214 CompileSuccessfully(spirv.c_str(), SPV_ENV_VULKAN_1_3);
4215 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_3));
4216 EXPECT_THAT(getDiagnosticString(),
4217 HasSubstr("The types must be the exact same Id, so the two types "
4218 "referenced are slighlty different"));
4219 }
4220
4221 // Valid: 255 indexes passed to the access chain instruction. Limit is 255.
4222 TEST_P(AccessChainInstructionTest, AccessChainTooManyIndexesGood) {
4223 const std::string instr = GetParam();
4224 const std::string elem = AccessChainRequiresElemId(instr) ? " %int_0 " : "";
4225 const std::string arrayStride =
4226 " OpDecorate %_ptr_Uniform_deep_struct ArrayStride 8 ";
4227 int depth = 255;
4228 std::string header =
4229 kGLSL450MemoryModel + arrayStride + kDeeplyNestedStructureSetup;
4230 header.erase(header.find("%func"));
4231 std::ostringstream spirv;
4232 spirv << header << "\n";
4233
4234 // Build nested structures. Struct 'i' contains struct 'i-1'
4235 spirv << "%s_depth_1 = OpTypeStruct %float\n";
4236 for (int i = 2; i <= depth; ++i) {
4237 spirv << "%s_depth_" << i << " = OpTypeStruct %s_depth_" << i - 1 << "\n";
4238 }
4239
4240 // Define Pointer and Variable to use for the AccessChain instruction.
4241 spirv << "%_ptr_Uniform_deep_struct = OpTypePointer Uniform %s_depth_"
4242 << depth << "\n";
4243 spirv << "%deep_var = OpVariable %_ptr_Uniform_deep_struct Uniform\n";
4244
4245 // Function Start
4246 spirv << R"(
4247 %func = OpFunction %void None %void_f
4248 %my_label = OpLabel
4249 )";
4250
4251 // AccessChain with 'n' indexes (n = depth)
4252 spirv << "%entry = " << instr << " %_ptr_Uniform_float %deep_var" << elem;
4253 for (int i = 0; i < depth; ++i) {
4254 spirv << " %int_0";
4255 }
4256
4257 // Function end
4258 spirv << R"(
4259 OpReturn
4260 OpFunctionEnd
4261 )";
4262 getValidatorOptions()->relax_logical_pointer = true;
4263 CompileSuccessfully(spirv.str());
4264 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4265 }
4266
4267 // Invalid: 256 indexes passed to the access chain instruction. Limit is 255.
4268 TEST_P(AccessChainInstructionTest, AccessChainTooManyIndexesBad) {
4269 const std::string instr = GetParam();
4270 const std::string elem = AccessChainRequiresElemId(instr) ? " %int_0 " : "";
4271 std::ostringstream spirv;
4272 spirv << kGLSL450MemoryModel << kDeeplyNestedStructureSetup;
4273 spirv << "%entry = " << instr << " %_ptr_Private_float %my_matrix" << elem;
4274 for (int i = 0; i < 256; ++i) {
4275 spirv << " %int_0";
4276 }
4277 spirv << R"(
4278 OpReturn
4279 OpFunctionEnd
4280 )";
4281 const std::string expected_err = "The number of indexes in " + instr +
4282 " may not exceed 255. Found 256 indexes.";
4283 getValidatorOptions()->relax_logical_pointer = true;
4284 CompileSuccessfully(spirv.str());
4285 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4286 EXPECT_THAT(getDiagnosticString(), HasSubstr(expected_err));
4287 }
4288
4289 // Valid: 10 indexes passed to the access chain instruction. (Custom limit: 10)
4290 TEST_P(AccessChainInstructionTest, CustomizedAccessChainTooManyIndexesGood) {
4291 const std::string instr = GetParam();
4292 const std::string elem = AccessChainRequiresElemId(instr) ? " %int_0 " : "";
4293 const std::string arrayStride =
4294 " OpDecorate %_ptr_Uniform_deep_struct ArrayStride 8 ";
4295 int depth = 10;
4296 std::string header =
4297 kGLSL450MemoryModel + arrayStride + kDeeplyNestedStructureSetup;
4298 header.erase(header.find("%func"));
4299 std::ostringstream spirv;
4300 spirv << header << "\n";
4301
4302 // Build nested structures. Struct 'i' contains struct 'i-1'
4303 spirv << "%s_depth_1 = OpTypeStruct %float\n";
4304 for (int i = 2; i <= depth; ++i) {
4305 spirv << "%s_depth_" << i << " = OpTypeStruct %s_depth_" << i - 1 << "\n";
4306 }
4307
4308 // Define Pointer and Variable to use for the AccessChain instruction.
4309 spirv << "%_ptr_Uniform_deep_struct = OpTypePointer Uniform %s_depth_"
4310 << depth << "\n";
4311 spirv << "%deep_var = OpVariable %_ptr_Uniform_deep_struct Uniform\n";
4312
4313 // Function Start
4314 spirv << R"(
4315 %func = OpFunction %void None %void_f
4316 %my_label = OpLabel
4317 )";
4318
4319 // AccessChain with 'n' indexes (n = depth)
4320 spirv << "%entry = " << instr << " %_ptr_Uniform_float %deep_var" << elem;
4321 for (int i = 0; i < depth; ++i) {
4322 spirv << " %int_0";
4323 }
4324
4325 // Function end
4326 spirv << R"(
4327 OpReturn
4328 OpFunctionEnd
4329 )";
4330
4331 getValidatorOptions()->relax_logical_pointer = true;
4332 spvValidatorOptionsSetUniversalLimit(
4333 options_, spv_validator_limit_max_access_chain_indexes, 10u);
4334 CompileSuccessfully(spirv.str());
4335 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4336 }
4337
4338 // Invalid: 11 indexes passed to the access chain instruction. Custom Limit:10
4339 TEST_P(AccessChainInstructionTest, CustomizedAccessChainTooManyIndexesBad) {
4340 const std::string instr = GetParam();
4341 const std::string elem = AccessChainRequiresElemId(instr) ? " %int_0 " : "";
4342 std::ostringstream spirv;
4343 spirv << kGLSL450MemoryModel << kDeeplyNestedStructureSetup;
4344 spirv << "%entry = " << instr << " %_ptr_Private_float %my_matrix" << elem;
4345 for (int i = 0; i < 11; ++i) {
4346 spirv << " %int_0";
4347 }
4348 spirv << R"(
4349 OpReturn
4350 OpFunctionEnd
4351 )";
4352 const std::string expected_err = "The number of indexes in " + instr +
4353 " may not exceed 10. Found 11 indexes.";
4354 getValidatorOptions()->relax_logical_pointer = true;
4355 spvValidatorOptionsSetUniversalLimit(
4356 options_, spv_validator_limit_max_access_chain_indexes, 10u);
4357 CompileSuccessfully(spirv.str());
4358 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4359 EXPECT_THAT(getDiagnosticString(), HasSubstr(expected_err));
4360 }
4361
4362 // Invalid: Index passed to the access chain instruction is float (must be
4363 // integer).
4364 TEST_P(AccessChainInstructionTest, AccessChainUndefinedIndexBad) {
4365 const std::string instr = GetParam();
4366 const std::string elem = AccessChainRequiresElemId(instr) ? "%int_0 " : "";
4367 std::string spirv = kGLSL450MemoryModel + kDeeplyNestedStructureSetup + R"(
4368 %entry = )" + instr + R"( %_ptr_Private_float %my_matrix )" +
4369 elem +
4370 R"(%float_0 %int_1
4371 OpReturn
4372 OpFunctionEnd
4373 )";
4374 const std::string expected_err =
4375 "Indexes passed to " + instr + " must be of type integer.";
4376 getValidatorOptions()->relax_logical_pointer = true;
4377 CompileSuccessfully(spirv);
4378 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4379 EXPECT_THAT(getDiagnosticString(), HasSubstr(expected_err));
4380 }
4381
4382 // Invalid: The index argument that indexes into a struct must be of type
4383 // OpConstant.
4384 TEST_P(AccessChainInstructionTest, AccessChainStructIndexNotConstantBad) {
4385 const std::string instr = GetParam();
4386 const std::string elem = AccessChainRequiresElemId(instr) ? "%int_0 " : "";
4387 std::string spirv = kGLSL450MemoryModel + kDeeplyNestedStructureSetup + R"(
4388 %f = )" + instr + R"( %_ptr_Uniform_float %blockName_var )" +
4389 elem +
4390 R"(%int_0 %spec_int %int_2
4391 OpReturn
4392 OpFunctionEnd
4393 )";
4394 getValidatorOptions()->relax_logical_pointer = true;
4395 CompileSuccessfully(spirv);
4396 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4397 EXPECT_THAT(getDiagnosticString(), HasSubstr("The <id> passed to " + instr));
4398 EXPECT_THAT(getDiagnosticString(),
4399 HasSubstr("into a structure must be an OpConstant"));
4400 }
4401
4402 // Invalid: Indexing up to a vec4 granularity, but result type expected float.
4403 TEST_P(AccessChainInstructionTest,
4404 AccessChainStructResultTypeDoesntMatchIndexedTypeBad) {
4405 const std::string instr = GetParam();
4406 const std::string elem = AccessChainRequiresElemId(instr) ? "%int_0 " : "";
4407 std::string spirv = kGLSL450MemoryModel + kDeeplyNestedStructureSetup + R"(
4408 %entry = )" + instr + R"( %_ptr_Uniform_float %blockName_var )" +
4409 elem +
4410 R"(%int_0 %int_1 %int_2
4411 OpReturn
4412 OpFunctionEnd
4413 )";
4414 const std::string expected_err =
4415 instr +
4416 " result type <id> '4[%float]' (OpTypeFloat) does not match the type "
4417 "that results from indexing into the base <id> '18[%v4float]' "
4418 "(OpTypeVector).";
4419 getValidatorOptions()->relax_logical_pointer = true;
4420 CompileSuccessfully(spirv);
4421 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4422 EXPECT_THAT(getDiagnosticString(), HasSubstr(expected_err));
4423 }
4424
4425 // Invalid: Reach non-composite type (bool) when unused indexes remain.
4426 TEST_P(AccessChainInstructionTest, AccessChainStructTooManyIndexesBad) {
4427 const std::string instr = GetParam();
4428 const std::string elem = AccessChainRequiresElemId(instr) ? "%int_0 " : "";
4429 std::string spirv = kGLSL450MemoryModel + kDeeplyNestedStructureSetup + R"(
4430 %entry = )" + instr + R"( %_ptr_Uniform_float %blockName_var )" +
4431 elem +
4432 R"(%int_0 %int_2 %int_2
4433 OpReturn
4434 OpFunctionEnd
4435 )";
4436 const std::string expected_err = instr +
4437 " reached non-composite type while "
4438 "indexes still remain to be traversed.";
4439 getValidatorOptions()->relax_logical_pointer = true;
4440 CompileSuccessfully(spirv);
4441 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4442 EXPECT_THAT(getDiagnosticString(), HasSubstr(expected_err));
4443 }
4444
4445 // Invalid: Trying to find index 3 of the struct that has only 3 members.
4446 TEST_P(AccessChainInstructionTest, AccessChainStructIndexOutOfBoundBad) {
4447 const std::string instr = GetParam();
4448 const std::string elem = AccessChainRequiresElemId(instr) ? "%int_0 " : "";
4449 std::string spirv = kGLSL450MemoryModel + kDeeplyNestedStructureSetup + R"(
4450 %entry = )" + instr + R"( %_ptr_Uniform_float %blockName_var )" +
4451 elem +
4452 R"(%int_3 %int_2 %int_2
4453 OpReturn
4454 OpFunctionEnd
4455 )";
4456 const std::string expected_err = "is out of bounds: " + instr +
4457 " cannot find index 3 into the structure "
4458 "<id> '25[%_struct_25]'. This structure "
4459 "has 3 members. Largest valid index is 2.";
4460 getValidatorOptions()->relax_logical_pointer = true;
4461 CompileSuccessfully(spirv);
4462 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4463 EXPECT_THAT(getDiagnosticString(), HasSubstr(expected_err));
4464 }
4465
4466 // Valid: Tests that we can index into Struct, Array, Matrix, and Vector!
4467 TEST_P(AccessChainInstructionTest, AccessChainIndexIntoAllTypesGood) {
4468 // indexes that we are passing are: 0, 3, 1, 2, 0
4469 // 0 will select the struct_s within the base struct (blockName)
4470 // 3 will select the Array that contains 5 matrices
4471 // 1 will select the Matrix that is at index 1 of the array
4472 // 2 will select the column (which is a vector) within the matrix at index 2
4473 // 0 will select the element at the index 0 of the vector. (which is a float).
4474 const std::string instr = GetParam();
4475 const std::string elem = AccessChainRequiresElemId(instr) ? "%int_0 " : "";
4476 const std::string arrayStride =
4477 " OpDecorate %_ptr_Uniform_blockName ArrayStride 8 ";
4478 std::ostringstream spirv;
4479 spirv << kGLSL450MemoryModel << arrayStride << kDeeplyNestedStructureSetup
4480 << std::endl;
4481 spirv << "%ss = " << instr << " %_ptr_Uniform_struct_s %blockName_var "
4482 << elem << "%int_0" << std::endl;
4483 spirv << "%sa = " << instr << " %_ptr_Uniform_array5_mat4x3 %blockName_var "
4484 << elem << "%int_0 %int_3" << std::endl;
4485 spirv << "%sm = " << instr << " %_ptr_Uniform_mat4x3 %blockName_var " << elem
4486 << "%int_0 %int_3 %int_1" << std::endl;
4487 spirv << "%sc = " << instr << " %_ptr_Uniform_v3float %blockName_var " << elem
4488 << "%int_0 %int_3 %int_1 %int_2" << std::endl;
4489 spirv << "%entry = " << instr << " %_ptr_Uniform_float %blockName_var "
4490 << elem << "%int_0 %int_3 %int_1 %int_2 %int_0" << std::endl;
4491 spirv << R"(
4492 OpReturn
4493 OpFunctionEnd
4494 )";
4495 getValidatorOptions()->relax_logical_pointer = true;
4496 CompileSuccessfully(spirv.str());
4497 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4498 }
4499
4500 // Valid: Access an element of OpTypeRuntimeArray.
4501 TEST_P(AccessChainInstructionTest, AccessChainIndexIntoRuntimeArrayGood) {
4502 const std::string instr = GetParam();
4503 const std::string elem = AccessChainRequiresElemId(instr) ? "%int_0 " : "";
4504 const std::string arrayStride =
4505 " OpDecorate %_ptr_Uniform_blockName ArrayStride 8 ";
4506 std::string spirv = kGLSL450MemoryModel + arrayStride +
4507 kDeeplyNestedStructureSetup + R"(
4508 %runtime_arr_entry = )" +
4509 instr + R"( %_ptr_Uniform_float %blockName_var )" + elem +
4510 R"(%int_2 %int_0
4511 OpReturn
4512 OpFunctionEnd
4513 )";
4514 getValidatorOptions()->relax_logical_pointer = true;
4515 CompileSuccessfully(spirv);
4516 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4517 }
4518
4519 // Invalid: Unused index when accessing OpTypeRuntimeArray.
4520 TEST_P(AccessChainInstructionTest, AccessChainIndexIntoRuntimeArrayBad) {
4521 const std::string instr = GetParam();
4522 const std::string elem = AccessChainRequiresElemId(instr) ? "%int_0 " : "";
4523 std::string spirv = kGLSL450MemoryModel + kDeeplyNestedStructureSetup + R"(
4524 %runtime_arr_entry = )" +
4525 instr + R"( %_ptr_Uniform_float %blockName_var )" + elem +
4526 R"(%int_2 %int_0 %int_1
4527 OpReturn
4528 OpFunctionEnd
4529 )";
4530 const std::string expected_err =
4531 instr +
4532 " reached non-composite type while indexes still remain to be traversed.";
4533 getValidatorOptions()->relax_logical_pointer = true;
4534 CompileSuccessfully(spirv);
4535 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4536 EXPECT_THAT(getDiagnosticString(), HasSubstr(expected_err));
4537 }
4538
4539 // Invalid: Reached scalar type before arguments to the access chain instruction
4540 // finished.
4541 TEST_P(AccessChainInstructionTest, AccessChainMatrixMoreArgsThanNeededBad) {
4542 const std::string instr = GetParam();
4543 const std::string elem = AccessChainRequiresElemId(instr) ? "%int_0 " : "";
4544 std::string spirv = kGLSL450MemoryModel + kDeeplyNestedStructureSetup + R"(
4545 %entry = )" + instr + R"( %_ptr_Private_float %my_matrix )" +
4546 elem +
4547 R"(%int_0 %int_1 %int_0
4548 OpReturn
4549 OpFunctionEnd
4550 )";
4551 const std::string expected_err = instr +
4552 " reached non-composite type while "
4553 "indexes still remain to be traversed.";
4554 getValidatorOptions()->relax_logical_pointer = true;
4555 CompileSuccessfully(spirv);
4556 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4557 EXPECT_THAT(getDiagnosticString(), HasSubstr(expected_err));
4558 }
4559
4560 // Invalid: The result type and the type indexed into do not match.
4561 TEST_P(AccessChainInstructionTest,
4562 AccessChainResultTypeDoesntMatchIndexedTypeBad) {
4563 const std::string instr = GetParam();
4564 const std::string elem = AccessChainRequiresElemId(instr) ? "%int_0 " : "";
4565 std::string spirv = kGLSL450MemoryModel + kDeeplyNestedStructureSetup + R"(
4566 %entry = )" + instr + R"( %_ptr_Private_mat4x3 %my_matrix )" +
4567 elem +
4568 R"(%int_0 %int_1
4569 OpReturn
4570 OpFunctionEnd
4571 )";
4572 const std::string expected_err =
4573 instr +
4574 " result type <id> '6[%mat4v3float]' (OpTypeMatrix) does not match the "
4575 "type that results from indexing into the base <id> '4[%float]' "
4576 "(OpTypeFloat).";
4577 getValidatorOptions()->relax_logical_pointer = true;
4578 CompileSuccessfully(spirv);
4579 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4580 EXPECT_THAT(getDiagnosticString(), HasSubstr(expected_err));
4581 }
4582
4583 // Run tests for Access Chain Instructions.
4584 INSTANTIATE_TEST_SUITE_P(
4585 CheckAccessChainInstructions, AccessChainInstructionTest,
4586 ::testing::Values("OpAccessChain", "OpInBoundsAccessChain",
4587 "OpPtrAccessChain", "OpInBoundsPtrAccessChain"));
4588
4589 // TODO: OpArrayLength
4590 // TODO: OpImagePointer
4591 // TODO: OpGenericPtrMemSemantics
4592
4593 TEST_P(ValidateIdWithMessage, OpFunctionGood) {
4594 std::string spirv = kGLSL450MemoryModel + R"(
4595 %1 = OpTypeVoid
4596 %2 = OpTypeInt 32 0
4597 %3 = OpTypeFunction %1 %2 %2
4598 %4 = OpFunction %1 None %3
4599 %5 = OpLabel
4600 OpReturn
4601 OpFunctionEnd)";
4602 CompileSuccessfully(spirv.c_str());
4603 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4604 }
4605 TEST_P(ValidateIdWithMessage, OpFunctionResultTypeBad) {
4606 std::string spirv = kGLSL450MemoryModel + R"(
4607 %1 = OpTypeVoid
4608 %2 = OpTypeInt 32 0
4609 %3 = OpConstant %2 42
4610 %4 = OpTypeFunction %1 %2 %2
4611 %5 = OpFunction %2 None %4
4612 %6 = OpLabel
4613 OpReturnValue %3
4614 OpFunctionEnd)";
4615 CompileSuccessfully(spirv.c_str());
4616 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4617 EXPECT_THAT(
4618 getDiagnosticString(),
4619 HasSubstr(make_message("OpFunction Result Type <id> '2[%uint]' does not "
4620 "match the Function Type's return type <id> "
4621 "'1[%void]'.")));
4622 }
4623 TEST_P(ValidateIdWithMessage, OpReturnValueTypeBad) {
4624 std::string spirv = kGLSL450MemoryModel + R"(
4625 %1 = OpTypeInt 32 0
4626 %2 = OpTypeFloat 32
4627 %3 = OpConstant %2 0
4628 %4 = OpTypeFunction %1
4629 %5 = OpFunction %1 None %4
4630 %6 = OpLabel
4631 OpReturnValue %3
4632 OpFunctionEnd)";
4633 CompileSuccessfully(spirv.c_str());
4634 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4635 EXPECT_THAT(getDiagnosticString(),
4636 HasSubstr(make_message(
4637 "OpReturnValue Value <id> '3[%float_0]'s type does "
4638 "not match OpFunction's return type.")));
4639 }
4640 TEST_P(ValidateIdWithMessage, OpFunctionFunctionTypeBad) {
4641 std::string spirv = kGLSL450MemoryModel + R"(
4642 %1 = OpTypeVoid
4643 %2 = OpTypeInt 32 0
4644 %4 = OpFunction %1 None %2
4645 %5 = OpLabel
4646 OpReturn
4647 OpFunctionEnd)";
4648 CompileSuccessfully(spirv.c_str());
4649 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4650 EXPECT_THAT(getDiagnosticString(),
4651 HasSubstr(make_message(
4652 "OpFunction Function Type <id> '2[%uint]' is not a function "
4653 "type.")));
4654 }
4655
4656 TEST_P(ValidateIdWithMessage, OpFunctionUseBad) {
4657 const std::string spirv = kGLSL450MemoryModel + R"(
4658 %1 = OpTypeFloat 32
4659 %2 = OpTypeFunction %1
4660 %3 = OpFunction %1 None %2
4661 %4 = OpLabel
4662 OpReturnValue %3
4663 OpFunctionEnd
4664 )";
4665
4666 CompileSuccessfully(spirv);
4667 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4668 EXPECT_THAT(
4669 getDiagnosticString(),
4670 HasSubstr(make_message("Invalid use of function result id '3[%3]'.")));
4671 }
4672
4673 TEST_P(ValidateIdWithMessage, OpFunctionParameterGood) {
4674 std::string spirv = kGLSL450MemoryModel + R"(
4675 %1 = OpTypeVoid
4676 %2 = OpTypeInt 32 0
4677 %3 = OpTypeFunction %1 %2
4678 %4 = OpFunction %1 None %3
4679 %5 = OpFunctionParameter %2
4680 %6 = OpLabel
4681 OpReturn
4682 OpFunctionEnd)";
4683 CompileSuccessfully(spirv.c_str());
4684 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4685 }
4686 TEST_P(ValidateIdWithMessage, OpFunctionParameterMultipleGood) {
4687 std::string spirv = kGLSL450MemoryModel + R"(
4688 %1 = OpTypeVoid
4689 %2 = OpTypeInt 32 0
4690 %3 = OpTypeFunction %1 %2 %2
4691 %4 = OpFunction %1 None %3
4692 %5 = OpFunctionParameter %2
4693 %6 = OpFunctionParameter %2
4694 %7 = OpLabel
4695 OpReturn
4696 OpFunctionEnd)";
4697 CompileSuccessfully(spirv.c_str());
4698 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4699 }
4700 TEST_P(ValidateIdWithMessage, OpFunctionParameterResultTypeBad) {
4701 std::string spirv = kGLSL450MemoryModel + R"(
4702 %1 = OpTypeVoid
4703 %2 = OpTypeInt 32 0
4704 %3 = OpTypeFunction %1 %2
4705 %4 = OpFunction %1 None %3
4706 %5 = OpFunctionParameter %1
4707 %6 = OpLabel
4708 OpReturn
4709 OpFunctionEnd)";
4710 CompileSuccessfully(spirv.c_str());
4711 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4712 EXPECT_THAT(
4713 getDiagnosticString(),
4714 HasSubstr(make_message(
4715 "OpFunctionParameter Result Type <id> '1[%void]' does not "
4716 "match the OpTypeFunction parameter type of the same index.")));
4717 }
4718
4719 TEST_P(ValidateIdWithMessage, OpFunctionCallGood) {
4720 std::string spirv = kGLSL450MemoryModel + R"(
4721 %1 = OpTypeVoid
4722 %2 = OpTypeInt 32 0
4723 %3 = OpTypeFunction %2 %2
4724 %4 = OpTypeFunction %1
4725 %5 = OpConstant %2 42 ;21
4726
4727 %6 = OpFunction %2 None %3
4728 %7 = OpFunctionParameter %2
4729 %8 = OpLabel
4730 OpReturnValue %7
4731 OpFunctionEnd
4732
4733 %10 = OpFunction %1 None %4
4734 %11 = OpLabel
4735 %12 = OpFunctionCall %2 %6 %5
4736 OpReturn
4737 OpFunctionEnd)";
4738 CompileSuccessfully(spirv.c_str());
4739 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4740 }
4741 TEST_P(ValidateIdWithMessage, OpFunctionCallResultTypeBad) {
4742 std::string spirv = kGLSL450MemoryModel + R"(
4743 %1 = OpTypeVoid
4744 %2 = OpTypeInt 32 0
4745 %3 = OpTypeFunction %2 %2
4746 %4 = OpTypeFunction %1
4747 %5 = OpConstant %2 42 ;21
4748
4749 %6 = OpFunction %2 None %3
4750 %7 = OpFunctionParameter %2
4751 %8 = OpLabel
4752 %9 = OpIAdd %2 %7 %7
4753 OpReturnValue %9
4754 OpFunctionEnd
4755
4756 %10 = OpFunction %1 None %4
4757 %11 = OpLabel
4758 %12 = OpFunctionCall %1 %6 %5
4759 OpReturn
4760 OpFunctionEnd)";
4761 CompileSuccessfully(spirv.c_str());
4762 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4763 EXPECT_THAT(
4764 getDiagnosticString(),
4765 HasSubstr(make_message("OpFunctionCall Result Type <id> '1[%void]'s type "
4766 "does not match Function <id> '2[%uint]'s return "
4767 "type.")));
4768 }
4769 TEST_P(ValidateIdWithMessage, OpFunctionCallFunctionBad) {
4770 std::string spirv = kGLSL450MemoryModel + R"(
4771 %1 = OpTypeVoid
4772 %2 = OpTypeInt 32 0
4773 %3 = OpTypeFunction %2 %2
4774 %4 = OpTypeFunction %1
4775 %5 = OpConstant %2 42 ;21
4776
4777 %10 = OpFunction %1 None %4
4778 %11 = OpLabel
4779 %12 = OpFunctionCall %2 %5 %5
4780 OpReturn
4781 OpFunctionEnd)";
4782 CompileSuccessfully(spirv.c_str());
4783 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4784 EXPECT_THAT(getDiagnosticString(),
4785 HasSubstr(make_message(
4786 "OpFunctionCall Function <id> '5[%uint_42]' is not a "
4787 "function.")));
4788 }
4789 TEST_P(ValidateIdWithMessage, OpFunctionCallArgumentTypeBad) {
4790 std::string spirv = kGLSL450MemoryModel + R"(
4791 %1 = OpTypeVoid
4792 %2 = OpTypeInt 32 0
4793 %3 = OpTypeFunction %2 %2
4794 %4 = OpTypeFunction %1
4795 %5 = OpConstant %2 42
4796
4797 %13 = OpTypeFloat 32
4798 %14 = OpConstant %13 3.14
4799
4800 %6 = OpFunction %2 None %3
4801 %7 = OpFunctionParameter %2
4802 %8 = OpLabel
4803 %9 = OpIAdd %2 %7 %7
4804 OpReturnValue %9
4805 OpFunctionEnd
4806
4807 %10 = OpFunction %1 None %4
4808 %11 = OpLabel
4809 %12 = OpFunctionCall %2 %6 %14
4810 OpReturn
4811 OpFunctionEnd)";
4812 CompileSuccessfully(spirv.c_str());
4813 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4814 EXPECT_THAT(getDiagnosticString(),
4815 HasSubstr(make_message(
4816 "OpFunctionCall Argument <id> '7[%float_3_1400001]'s "
4817 "type does not match Function <id> '2[%uint]'s "
4818 "parameter type.")));
4819 }
4820
4821 // Valid: OpSampledImage result <id> is used in the same block by
4822 // OpImageSampleImplictLod
4823 TEST_P(ValidateIdWithMessage, OpSampledImageGood) {
4824 std::string spirv = kGLSL450MemoryModel + sampledImageSetup + R"(
4825 %smpld_img = OpSampledImage %sampled_image_type %image_inst %sampler_inst
4826 %si_lod = OpImageSampleImplicitLod %v4float %smpld_img %const_vec_1_1
4827 OpReturn
4828 OpFunctionEnd)";
4829 CompileSuccessfully(spirv.c_str());
4830 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4831 }
4832
4833 // Invalid: OpSampledImage result <id> is defined in one block and used in a
4834 // different block.
4835 TEST_P(ValidateIdWithMessage, OpSampledImageUsedInDifferentBlockBad) {
4836 std::string spirv = kGLSL450MemoryModel + sampledImageSetup + R"(
4837 %smpld_img = OpSampledImage %sampled_image_type %image_inst %sampler_inst
4838 OpBranch %label_2
4839 %label_2 = OpLabel
4840 %si_lod = OpImageSampleImplicitLod %v4float %smpld_img %const_vec_1_1
4841 OpReturn
4842 OpFunctionEnd)";
4843 CompileSuccessfully(spirv.c_str());
4844 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4845 EXPECT_THAT(
4846 getDiagnosticString(),
4847 HasSubstr(make_message(
4848 "All OpSampledImage instructions must be in the same block in "
4849 "which their Result <id> are consumed. OpSampledImage Result "
4850 "Type <id> '23[%23]' has a consumer in a different basic "
4851 "block. The consumer instruction <id> is '25[%25]'.")));
4852 }
4853
4854 // Invalid: OpSampledImage result <id> is used by OpSelect
4855 // Note: According to the Spec, OpSelect parameters must be either a scalar or a
4856 // vector. Therefore, OpTypeSampledImage is an illegal parameter for OpSelect.
4857 // However, the OpSelect validation does not catch this today. Therefore, it is
4858 // caught by the OpSampledImage validation. If the OpSelect validation code is
4859 // updated, the error message for this test may change.
4860 //
4861 // Disabled since OpSelect catches this now.
4862 TEST_P(ValidateIdWithMessage, DISABLED_OpSampledImageUsedInOpSelectBad) {
4863 std::string spirv = kGLSL450MemoryModel + sampledImageSetup + R"(
4864 %smpld_img = OpSampledImage %sampled_image_type %image_inst %sampler_inst
4865 %select_img = OpSelect %sampled_image_type %spec_true %smpld_img %smpld_img
4866 OpReturn
4867 OpFunctionEnd)";
4868 CompileSuccessfully(spirv.c_str());
4869 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4870 EXPECT_THAT(getDiagnosticString(),
4871 HasSubstr(make_message(
4872 "Result <id> from OpSampledImage instruction must not "
4873 "appear as operands of OpSelect. Found result <id> "
4874 "'23' as an operand of <id> '24'.")));
4875 }
4876
4877 TEST_P(ValidateIdWithMessage, OpCopyObjectSampledImageGood) {
4878 std::string spirv = kGLSL450MemoryModel + sampledImageSetup + R"(
4879 %smpld_img = OpSampledImage %sampled_image_type %image_inst %sampler_inst
4880 %smpld_img2 = OpCopyObject %sampled_image_type %smpld_img
4881 %image_inst2 = OpCopyObject %image_type %image_inst
4882 OpReturn
4883 OpFunctionEnd)";
4884 CompileSuccessfully(spirv.c_str());
4885 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4886 }
4887
4888 // Valid: Get a float in a matrix using CompositeExtract.
4889 // Valid: Insert float into a matrix using CompositeInsert.
4890 TEST_P(ValidateIdWithMessage, CompositeExtractInsertGood) {
4891 std::ostringstream spirv;
4892 spirv << kGLSL450MemoryModel << kDeeplyNestedStructureSetup << std::endl;
4893 spirv << "%matrix = OpLoad %mat4x3 %my_matrix" << std::endl;
4894 spirv << "%float_entry = OpCompositeExtract %float %matrix 0 1" << std::endl;
4895
4896 // To test CompositeInsert, insert the object back in after extraction.
4897 spirv << "%new_composite = OpCompositeInsert %mat4x3 %float_entry %matrix 0 1"
4898 << std::endl;
4899 spirv << R"(OpReturn
4900 OpFunctionEnd)";
4901 CompileSuccessfully(spirv.str());
4902 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4903 }
4904
4905 #if 0
4906 TEST_P(ValidateIdWithMessage, OpFunctionCallArgumentCountBar) {
4907 const char *spirv = R"(
4908 %1 = OpTypeVoid
4909 %2 = OpTypeInt 32 0
4910 %3 = OpTypeFunction %2 %2
4911 %4 = OpTypeFunction %1
4912 %5 = OpConstant %2 42 ;21
4913
4914 %6 = OpFunction %2 None %3
4915 %7 = OpFunctionParameter %2
4916 %8 = OpLabel
4917 %9 = OpLoad %2 %7
4918 OpReturnValue %9
4919 OpFunctionEnd
4920
4921 %10 = OpFunction %1 None %4
4922 %11 = OpLabel
4923 OpReturn
4924 %12 = OpFunctionCall %2 %6 %5
4925 OpFunctionEnd)";
4926 CHECK(spirv, SPV_ERROR_INVALID_ID);
4927 }
4928 #endif
4929
4930 // TODO: The many things that changed with how images are used.
4931 // TODO: OpTextureSample
4932 // TODO: OpTextureSampleDref
4933 // TODO: OpTextureSampleLod
4934 // TODO: OpTextureSampleProj
4935 // TODO: OpTextureSampleGrad
4936 // TODO: OpTextureSampleOffset
4937 // TODO: OpTextureSampleProjLod
4938 // TODO: OpTextureSampleProjGrad
4939 // TODO: OpTextureSampleLodOffset
4940 // TODO: OpTextureSampleProjOffset
4941 // TODO: OpTextureSampleGradOffset
4942 // TODO: OpTextureSampleProjLodOffset
4943 // TODO: OpTextureSampleProjGradOffset
4944 // TODO: OpTextureFetchTexelLod
4945 // TODO: OpTextureFetchTexelOffset
4946 // TODO: OpTextureFetchSample
4947 // TODO: OpTextureFetchTexel
4948 // TODO: OpTextureGather
4949 // TODO: OpTextureGatherOffset
4950 // TODO: OpTextureGatherOffsets
4951 // TODO: OpTextureQuerySizeLod
4952 // TODO: OpTextureQuerySize
4953 // TODO: OpTextureQueryLevels
4954 // TODO: OpTextureQuerySamples
4955 // TODO: OpConvertUToF
4956 // TODO: OpConvertFToS
4957 // TODO: OpConvertSToF
4958 // TODO: OpConvertUToF
4959 // TODO: OpUConvert
4960 // TODO: OpSConvert
4961 // TODO: OpFConvert
4962 // TODO: OpConvertPtrToU
4963 // TODO: OpConvertUToPtr
4964 // TODO: OpPtrCastToGeneric
4965 // TODO: OpGenericCastToPtr
4966 // TODO: OpBitcast
4967 // TODO: OpGenericCastToPtrExplicit
4968 // TODO: OpSatConvertSToU
4969 // TODO: OpSatConvertUToS
4970 // TODO: OpVectorExtractDynamic
4971 // TODO: OpVectorInsertDynamic
4972
4973 TEST_P(ValidateIdWithMessage, OpVectorShuffleIntGood) {
4974 std::string spirv = kGLSL450MemoryModel + R"(
4975 %int = OpTypeInt 32 0
4976 %ivec3 = OpTypeVector %int 3
4977 %ivec4 = OpTypeVector %int 4
4978 %ptr_ivec3 = OpTypePointer Function %ivec3
4979 %undef = OpUndef %ivec4
4980 %int_42 = OpConstant %int 42
4981 %int_0 = OpConstant %int 0
4982 %int_2 = OpConstant %int 2
4983 %1 = OpConstantComposite %ivec3 %int_42 %int_0 %int_2
4984 %2 = OpTypeFunction %ivec3
4985 %3 = OpFunction %ivec3 None %2
4986 %4 = OpLabel
4987 %var = OpVariable %ptr_ivec3 Function %1
4988 %5 = OpLoad %ivec3 %var
4989 %6 = OpVectorShuffle %ivec3 %5 %undef 2 1 0
4990 OpReturnValue %6
4991 OpFunctionEnd)";
4992 CompileSuccessfully(spirv.c_str());
4993 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4994 }
4995
4996 TEST_P(ValidateIdWithMessage, OpVectorShuffleFloatGood) {
4997 std::string spirv = kGLSL450MemoryModel + R"(
4998 %float = OpTypeFloat 32
4999 %vec2 = OpTypeVector %float 2
5000 %vec3 = OpTypeVector %float 3
5001 %vec4 = OpTypeVector %float 4
5002 %ptr_vec2 = OpTypePointer Function %vec2
5003 %ptr_vec3 = OpTypePointer Function %vec3
5004 %float_1 = OpConstant %float 1
5005 %float_2 = OpConstant %float 2
5006 %1 = OpConstantComposite %vec2 %float_2 %float_1
5007 %2 = OpConstantComposite %vec3 %float_1 %float_2 %float_2
5008 %3 = OpTypeFunction %vec4
5009 %4 = OpFunction %vec4 None %3
5010 %5 = OpLabel
5011 %var = OpVariable %ptr_vec2 Function %1
5012 %var2 = OpVariable %ptr_vec3 Function %2
5013 %6 = OpLoad %vec2 %var
5014 %7 = OpLoad %vec3 %var2
5015 %8 = OpVectorShuffle %vec4 %6 %7 4 3 1 0xffffffff
5016 OpReturnValue %8
5017 OpFunctionEnd)";
5018 CompileSuccessfully(spirv.c_str());
5019 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5020 }
5021
5022 TEST_P(ValidateIdWithMessage, OpVectorIdShuffleFloatGood) {
5023 std::string spirv = "OpCapability LongVectorEXT\n" + kOpCapabilitySetup +
5024 kOpVariablePtrSetUp +
5025 "OpExtension \"SPV_EXT_long_vector\"" + R"(
5026 OpMemoryModel Logical GLSL450
5027 %float = OpTypeFloat 32
5028 %uint = OpTypeInt 32 0
5029 %u2 = OpConstant %uint 2
5030 %u3 = OpConstant %uint 3
5031 %u4 = OpConstant %uint 4
5032 %vec2 = OpTypeVectorIdEXT %float %u2
5033 %vec3 = OpTypeVectorIdEXT %float %u3
5034 %vec4 = OpTypeVectorIdEXT %float %u4
5035 %ptr_vec2 = OpTypePointer Function %vec2
5036 %ptr_vec3 = OpTypePointer Function %vec3
5037 %float_1 = OpConstant %float 1
5038 %float_2 = OpConstant %float 2
5039 %1 = OpConstantComposite %vec2 %float_2 %float_1
5040 %2 = OpConstantComposite %vec3 %float_1 %float_2 %float_2
5041 %3 = OpTypeFunction %vec4
5042 %4 = OpFunction %vec4 None %3
5043 %5 = OpLabel
5044 %var = OpVariable %ptr_vec2 Function %1
5045 %var2 = OpVariable %ptr_vec3 Function %2
5046 %6 = OpLoad %vec2 %var
5047 %7 = OpLoad %vec3 %var2
5048 %8 = OpVectorShuffle %vec4 %6 %7 4 3 1 0xffffffff
5049 OpReturnValue %8
5050 OpFunctionEnd)";
5051
5052 CompileSuccessfully(spirv.c_str());
5053 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5054 }
5055
5056 TEST_P(ValidateIdWithMessage, OpVectorShuffleScalarResultType) {
5057 std::string spirv = kGLSL450MemoryModel + R"(
5058 %float = OpTypeFloat 32
5059 %vec2 = OpTypeVector %float 2
5060 %ptr_vec2 = OpTypePointer Function %vec2
5061 %float_1 = OpConstant %float 1
5062 %float_2 = OpConstant %float 2
5063 %1 = OpConstantComposite %vec2 %float_2 %float_1
5064 %2 = OpTypeFunction %float
5065 %3 = OpFunction %float None %2
5066 %4 = OpLabel
5067 %var = OpVariable %ptr_vec2 Function %1
5068 %5 = OpLoad %vec2 %var
5069 %6 = OpVectorShuffle %float %5 %5 0
5070 OpReturnValue %6
5071 OpFunctionEnd)";
5072 CompileSuccessfully(spirv.c_str());
5073 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5074 EXPECT_THAT(getDiagnosticString(),
5075 HasSubstr(make_message(
5076 "Result Type of OpVectorShuffle must be a vector type.")));
5077 }
5078
5079 TEST_P(ValidateIdWithMessage, OpVectorShuffleComponentCount) {
5080 std::string spirv = kGLSL450MemoryModel + R"(
5081 %int = OpTypeInt 32 0
5082 %ivec3 = OpTypeVector %int 3
5083 %ptr_ivec3 = OpTypePointer Function %ivec3
5084 %int_42 = OpConstant %int 42
5085 %int_0 = OpConstant %int 0
5086 %int_2 = OpConstant %int 2
5087 %1 = OpConstantComposite %ivec3 %int_42 %int_0 %int_2
5088 %2 = OpTypeFunction %ivec3
5089 %3 = OpFunction %ivec3 None %2
5090 %4 = OpLabel
5091 %var = OpVariable %ptr_ivec3 Function %1
5092 %5 = OpLoad %ivec3 %var
5093 %6 = OpVectorShuffle %ivec3 %5 %5 0 1
5094 OpReturnValue %6
5095 OpFunctionEnd)";
5096 CompileSuccessfully(spirv.c_str());
5097 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5098 EXPECT_THAT(getDiagnosticString(),
5099 HasSubstr(make_message(
5100 "OpVectorShuffle component literals count does not match "
5101 "Result Type <id> '2[%v3uint]'s vector component count.")));
5102 }
5103
5104 TEST_P(ValidateIdWithMessage, OpVectorShuffleVector1Type) {
5105 std::string spirv = kGLSL450MemoryModel + R"(
5106 %int = OpTypeInt 32 0
5107 %ivec2 = OpTypeVector %int 2
5108 %ptr_int = OpTypePointer Function %int
5109 %undef = OpUndef %ivec2
5110 %int_42 = OpConstant %int 42
5111 %2 = OpTypeFunction %ivec2
5112 %3 = OpFunction %ivec2 None %2
5113 %4 = OpLabel
5114 %var = OpVariable %ptr_int Function %int_42
5115 %5 = OpLoad %int %var
5116 %6 = OpVectorShuffle %ivec2 %5 %undef 0 0
5117 OpReturnValue %6
5118 OpFunctionEnd)";
5119 CompileSuccessfully(spirv.c_str());
5120 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5121 EXPECT_THAT(
5122 getDiagnosticString(),
5123 HasSubstr(make_message("The type of Vector 1 must be a vector type.")));
5124 }
5125
5126 TEST_P(ValidateIdWithMessage, OpVectorShuffleVector2Type) {
5127 std::string spirv = kGLSL450MemoryModel + R"(
5128 %int = OpTypeInt 32 0
5129 %ivec2 = OpTypeVector %int 2
5130 %ptr_ivec2 = OpTypePointer Function %ivec2
5131 %undef = OpUndef %int
5132 %int_42 = OpConstant %int 42
5133 %1 = OpConstantComposite %ivec2 %int_42 %int_42
5134 %2 = OpTypeFunction %ivec2
5135 %3 = OpFunction %ivec2 None %2
5136 %4 = OpLabel
5137 %var = OpVariable %ptr_ivec2 Function %1
5138 %5 = OpLoad %ivec2 %var
5139 %6 = OpVectorShuffle %ivec2 %5 %undef 0 1
5140 OpReturnValue %6
5141 OpFunctionEnd)";
5142 CompileSuccessfully(spirv.c_str());
5143 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5144 EXPECT_THAT(
5145 getDiagnosticString(),
5146 HasSubstr(make_message("The type of Vector 2 must be a vector type.")));
5147 }
5148
5149 TEST_P(ValidateIdWithMessage, OpVectorShuffleVector1ComponentType) {
5150 std::string spirv = kGLSL450MemoryModel + R"(
5151 %int = OpTypeInt 32 0
5152 %ivec3 = OpTypeVector %int 3
5153 %ptr_ivec3 = OpTypePointer Function %ivec3
5154 %int_42 = OpConstant %int 42
5155 %int_0 = OpConstant %int 0
5156 %int_2 = OpConstant %int 2
5157 %float = OpTypeFloat 32
5158 %vec3 = OpTypeVector %float 3
5159 %vec4 = OpTypeVector %float 4
5160 %ptr_vec3 = OpTypePointer Function %vec3
5161 %float_1 = OpConstant %float 1
5162 %float_2 = OpConstant %float 2
5163 %1 = OpConstantComposite %ivec3 %int_42 %int_0 %int_2
5164 %2 = OpConstantComposite %vec3 %float_1 %float_2 %float_2
5165 %3 = OpTypeFunction %vec4
5166 %4 = OpFunction %vec4 None %3
5167 %5 = OpLabel
5168 %var = OpVariable %ptr_ivec3 Function %1
5169 %var2 = OpVariable %ptr_vec3 Function %2
5170 %6 = OpLoad %ivec3 %var
5171 %7 = OpLoad %vec3 %var2
5172 %8 = OpVectorShuffle %vec4 %6 %7 4 3 1 0
5173 OpReturnValue %8
5174 OpFunctionEnd)";
5175 CompileSuccessfully(spirv.c_str());
5176 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5177 EXPECT_THAT(getDiagnosticString(),
5178 HasSubstr(make_message(
5179 "The Component Type of Vector 1 must be the same as "
5180 "ResultType.")));
5181 }
5182
5183 TEST_P(ValidateIdWithMessage, OpVectorShuffleVector2ComponentType) {
5184 std::string spirv = kGLSL450MemoryModel + R"(
5185 %int = OpTypeInt 32 0
5186 %ivec3 = OpTypeVector %int 3
5187 %ptr_ivec3 = OpTypePointer Function %ivec3
5188 %int_42 = OpConstant %int 42
5189 %int_0 = OpConstant %int 0
5190 %int_2 = OpConstant %int 2
5191 %float = OpTypeFloat 32
5192 %vec3 = OpTypeVector %float 3
5193 %vec4 = OpTypeVector %float 4
5194 %ptr_vec3 = OpTypePointer Function %vec3
5195 %float_1 = OpConstant %float 1
5196 %float_2 = OpConstant %float 2
5197 %1 = OpConstantComposite %ivec3 %int_42 %int_0 %int_2
5198 %2 = OpConstantComposite %vec3 %float_1 %float_2 %float_2
5199 %3 = OpTypeFunction %vec4
5200 %4 = OpFunction %vec4 None %3
5201 %5 = OpLabel
5202 %var = OpVariable %ptr_ivec3 Function %1
5203 %var2 = OpVariable %ptr_vec3 Function %2
5204 %6 = OpLoad %vec3 %var2
5205 %7 = OpLoad %ivec3 %var
5206 %8 = OpVectorShuffle %vec4 %6 %7 4 3 1 0
5207 OpReturnValue %8
5208 OpFunctionEnd)";
5209 CompileSuccessfully(spirv.c_str());
5210 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5211 EXPECT_THAT(getDiagnosticString(),
5212 HasSubstr(make_message(
5213 "The Component Type of Vector 2 must be the same as "
5214 "ResultType.")));
5215 }
5216
5217 TEST_P(ValidateIdWithMessage, OpVectorShuffleLiterals) {
5218 std::string spirv = kGLSL450MemoryModel + R"(
5219 %float = OpTypeFloat 32
5220 %vec2 = OpTypeVector %float 2
5221 %vec3 = OpTypeVector %float 3
5222 %vec4 = OpTypeVector %float 4
5223 %ptr_vec2 = OpTypePointer Function %vec2
5224 %ptr_vec3 = OpTypePointer Function %vec3
5225 %float_1 = OpConstant %float 1
5226 %float_2 = OpConstant %float 2
5227 %1 = OpConstantComposite %vec2 %float_2 %float_1
5228 %2 = OpConstantComposite %vec3 %float_1 %float_2 %float_2
5229 %3 = OpTypeFunction %vec4
5230 %4 = OpFunction %vec4 None %3
5231 %5 = OpLabel
5232 %var = OpVariable %ptr_vec2 Function %1
5233 %var2 = OpVariable %ptr_vec3 Function %2
5234 %6 = OpLoad %vec2 %var
5235 %7 = OpLoad %vec3 %var2
5236 %8 = OpVectorShuffle %vec4 %6 %7 0 8 2 6
5237 OpReturnValue %8
5238 OpFunctionEnd)";
5239 CompileSuccessfully(spirv.c_str());
5240 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5241 EXPECT_THAT(
5242 getDiagnosticString(),
5243 HasSubstr(make_message(
5244 "Component index 8 is out of bounds for combined (Vector1 + Vector2) "
5245 "size of 5.")));
5246 }
5247
5248 // TODO: OpCompositeConstruct
5249 // TODO: OpCompositeExtract
5250 // TODO: OpCompositeInsert
5251 // TODO: OpCopyObject
5252 // TODO: OpTranspose
5253 // TODO: OpSNegate
5254 // TODO: OpFNegate
5255 // TODO: OpNot
5256 // TODO: OpIAdd
5257 // TODO: OpFAdd
5258 // TODO: OpISub
5259 // TODO: OpFSub
5260 // TODO: OpIMul
5261 // TODO: OpFMul
5262 // TODO: OpUDiv
5263 // TODO: OpSDiv
5264 // TODO: OpFDiv
5265 // TODO: OpUMod
5266 // TODO: OpSRem
5267 // TODO: OpSMod
5268 // TODO: OpFRem
5269 // TODO: OpFMod
5270 // TODO: OpVectorTimesScalar
5271 // TODO: OpMatrixTimesScalar
5272 // TODO: OpVectorTimesMatrix
5273 // TODO: OpMatrixTimesVector
5274 // TODO: OpMatrixTimesMatrix
5275 // TODO: OpOuterProduct
5276 // TODO: OpDot
5277 // TODO: OpShiftRightLogical
5278 // TODO: OpShiftRightArithmetic
5279 // TODO: OpShiftLeftLogical
5280 // TODO: OpBitwiseOr
5281 // TODO: OpBitwiseXor
5282 // TODO: OpBitwiseAnd
5283 // TODO: OpAny
5284 // TODO: OpAll
5285 // TODO: OpIsNan
5286 // TODO: OpIsInf
5287 // TODO: OpIsFinite
5288 // TODO: OpIsNormal
5289 // TODO: OpSignBitSet
5290 // TODO: OpLessOrGreater
5291 // TODO: OpOrdered
5292 // TODO: OpUnordered
5293 // TODO: OpLogicalOr
5294 // TODO: OpLogicalXor
5295 // TODO: OpLogicalAnd
5296 // TODO: OpSelect
5297 // TODO: OpIEqual
5298 // TODO: OpFOrdEqual
5299 // TODO: OpFUnordEqual
5300 // TODO: OpINotEqual
5301 // TODO: OpFOrdNotEqual
5302 // TODO: OpFUnordNotEqual
5303 // TODO: OpULessThan
5304 // TODO: OpSLessThan
5305 // TODO: OpFOrdLessThan
5306 // TODO: OpFUnordLessThan
5307 // TODO: OpUGreaterThan
5308 // TODO: OpSGreaterThan
5309 // TODO: OpFOrdGreaterThan
5310 // TODO: OpFUnordGreaterThan
5311 // TODO: OpULessThanEqual
5312 // TODO: OpSLessThanEqual
5313 // TODO: OpFOrdLessThanEqual
5314 // TODO: OpFUnordLessThanEqual
5315 // TODO: OpUGreaterThanEqual
5316 // TODO: OpSGreaterThanEqual
5317 // TODO: OpFOrdGreaterThanEqual
5318 // TODO: OpFUnordGreaterThanEqual
5319 // TODO: OpDPdx
5320 // TODO: OpDPdy
5321 // TODO: OpFWidth
5322 // TODO: OpDPdxFine
5323 // TODO: OpDPdyFine
5324 // TODO: OpFwidthFine
5325 // TODO: OpDPdxCoarse
5326 // TODO: OpDPdyCoarse
5327 // TODO: OpFwidthCoarse
5328 // TODO: OpLoopMerge
5329 // TODO: OpSelectionMerge
5330 // TODO: OpBranch
5331
5332 TEST_P(ValidateIdWithMessage, OpPhiNotAType) {
5333 std::string spirv = kOpenCLMemoryModel32 + R"(
5334 %2 = OpTypeBool
5335 %3 = OpConstantTrue %2
5336 %4 = OpTypeVoid
5337 %5 = OpTypeFunction %4
5338 %6 = OpFunction %4 None %5
5339 %7 = OpLabel
5340 OpBranch %8
5341 %8 = OpLabel
5342 %9 = OpPhi %3 %3 %7
5343 OpReturn
5344 OpFunctionEnd
5345 )";
5346
5347 CompileSuccessfully(spirv.c_str());
5348 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5349 EXPECT_THAT(getDiagnosticString(),
5350 HasSubstr(make_message("ID '3[%true]' is not a type "
5351 "id")));
5352 }
5353
5354 TEST_P(ValidateIdWithMessage, OpPhiSamePredecessor) {
5355 std::string spirv = kOpenCLMemoryModel32 + R"(
5356 %2 = OpTypeBool
5357 %3 = OpConstantTrue %2
5358 %4 = OpTypeVoid
5359 %5 = OpTypeFunction %4
5360 %6 = OpFunction %4 None %5
5361 %7 = OpLabel
5362 OpBranchConditional %3 %8 %8
5363 %8 = OpLabel
5364 %9 = OpPhi %2 %3 %7
5365 OpReturn
5366 OpFunctionEnd
5367 )";
5368
5369 CompileSuccessfully(spirv.c_str());
5370 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5371 }
5372
5373 TEST_P(ValidateIdWithMessage, OpPhiOddArgumentNumber) {
5374 std::string spirv = kOpenCLMemoryModel32 + R"(
5375 %2 = OpTypeBool
5376 %3 = OpConstantTrue %2
5377 %4 = OpTypeVoid
5378 %5 = OpTypeFunction %4
5379 %6 = OpFunction %4 None %5
5380 %7 = OpLabel
5381 OpBranch %8
5382 %8 = OpLabel
5383 %9 = OpPhi %2 %3
5384 OpReturn
5385 OpFunctionEnd
5386 )";
5387
5388 CompileSuccessfully(spirv.c_str());
5389 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5390 EXPECT_THAT(
5391 getDiagnosticString(),
5392 HasSubstr(make_message("OpPhi does not have an equal number of incoming "
5393 "values and basic blocks.")));
5394 }
5395
5396 TEST_P(ValidateIdWithMessage, OpPhiTooFewPredecessors) {
5397 std::string spirv = kOpenCLMemoryModel32 + R"(
5398 %2 = OpTypeBool
5399 %3 = OpConstantTrue %2
5400 %4 = OpTypeVoid
5401 %5 = OpTypeFunction %4
5402 %6 = OpFunction %4 None %5
5403 %7 = OpLabel
5404 OpBranch %8
5405 %8 = OpLabel
5406 %9 = OpPhi %2
5407 OpReturn
5408 OpFunctionEnd
5409 )";
5410
5411 CompileSuccessfully(spirv.c_str());
5412 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5413 EXPECT_THAT(getDiagnosticString(),
5414 HasSubstr(make_message(
5415 "OpPhi's number of incoming blocks (0) does not match "
5416 "block's predecessor count (1).")));
5417 }
5418
5419 TEST_P(ValidateIdWithMessage, OpPhiTooManyPredecessors) {
5420 std::string spirv = kOpenCLMemoryModel32 + R"(
5421 %2 = OpTypeBool
5422 %3 = OpConstantTrue %2
5423 %4 = OpTypeVoid
5424 %5 = OpTypeFunction %4
5425 %6 = OpFunction %4 None %5
5426 %7 = OpLabel
5427 OpBranch %8
5428 %9 = OpLabel
5429 OpReturn
5430 %8 = OpLabel
5431 %10 = OpPhi %2 %3 %7 %3 %9
5432 OpReturn
5433 OpFunctionEnd
5434 )";
5435
5436 CompileSuccessfully(spirv.c_str());
5437 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5438 EXPECT_THAT(getDiagnosticString(),
5439 HasSubstr(make_message(
5440 "OpPhi's number of incoming blocks (2) does not match "
5441 "block's predecessor count (1).")));
5442 }
5443
5444 TEST_P(ValidateIdWithMessage, OpPhiMismatchedTypes) {
5445 std::string spirv = kOpenCLMemoryModel32 + R"(
5446 %2 = OpTypeBool
5447 %3 = OpConstantTrue %2
5448 %4 = OpTypeVoid
5449 %5 = OpTypeInt 32 0
5450 %6 = OpConstant %5 0
5451 %7 = OpTypeFunction %4
5452 %8 = OpFunction %4 None %7
5453 %9 = OpLabel
5454 OpBranchConditional %3 %10 %11
5455 %11 = OpLabel
5456 OpBranch %10
5457 %10 = OpLabel
5458 %12 = OpPhi %2 %3 %9 %6 %11
5459 OpReturn
5460 OpFunctionEnd
5461 )";
5462
5463 CompileSuccessfully(spirv.c_str());
5464 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5465 EXPECT_THAT(getDiagnosticString(),
5466 HasSubstr(make_message(
5467 "OpPhi's result type <id> '2[%bool]' does not match "
5468 "incoming value <id> '6[%uint_0]' type <id> "
5469 "'5[%uint]'.")));
5470 }
5471
5472 TEST_P(ValidateIdWithMessage, OpPhiPredecessorNotABlock) {
5473 std::string spirv = kOpenCLMemoryModel32 + R"(
5474 %2 = OpTypeBool
5475 %3 = OpConstantTrue %2
5476 %4 = OpTypeVoid
5477 %5 = OpTypeFunction %4
5478 %6 = OpFunction %4 None %5
5479 %7 = OpLabel
5480 OpBranchConditional %3 %8 %9
5481 %9 = OpLabel
5482 OpBranch %11
5483 %11 = OpLabel
5484 OpBranch %8
5485 %8 = OpLabel
5486 %10 = OpPhi %2 %3 %7 %3 %3
5487 OpReturn
5488 OpFunctionEnd
5489 )";
5490
5491 CompileSuccessfully(spirv.c_str());
5492 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5493 EXPECT_THAT(getDiagnosticString(),
5494 HasSubstr(make_message(
5495 "OpPhi's incoming basic block <id> '3[%true]' is not an "
5496 "OpLabel.")));
5497 }
5498
5499 TEST_P(ValidateIdWithMessage, OpPhiNotAPredecessor) {
5500 std::string spirv = kOpenCLMemoryModel32 + R"(
5501 %2 = OpTypeBool
5502 %3 = OpConstantTrue %2
5503 %4 = OpTypeVoid
5504 %5 = OpTypeFunction %4
5505 %6 = OpFunction %4 None %5
5506 %7 = OpLabel
5507 OpBranchConditional %3 %8 %9
5508 %9 = OpLabel
5509 OpBranch %11
5510 %11 = OpLabel
5511 OpBranch %8
5512 %8 = OpLabel
5513 %10 = OpPhi %2 %3 %7 %3 %9
5514 OpReturn
5515 OpFunctionEnd
5516 )";
5517
5518 CompileSuccessfully(spirv.c_str());
5519 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5520 EXPECT_THAT(getDiagnosticString(),
5521 HasSubstr(make_message(
5522 "OpPhi's incoming basic block <id> '9[%9]' is not a "
5523 "predecessor of <id> '8[%8]'.")));
5524 }
5525
5526 TEST_P(ValidateIdWithMessage, OpBranchConditionalGood) {
5527 std::string spirv = BranchConditionalSetup + R"(
5528 %branch_cond = OpINotEqual %bool %i0 %i1
5529 OpSelectionMerge %end None
5530 OpBranchConditional %branch_cond %target_t %target_f
5531 )" + BranchConditionalTail;
5532
5533 CompileSuccessfully(spirv.c_str());
5534 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
5535 }
5536
5537 TEST_P(ValidateIdWithMessage, OpBranchConditionalWithWeightsGood) {
5538 std::string spirv = BranchConditionalSetup + R"(
5539 %branch_cond = OpINotEqual %bool %i0 %i1
5540 OpSelectionMerge %end None
5541 OpBranchConditional %branch_cond %target_t %target_f 1 1
5542 )" + BranchConditionalTail;
5543
5544 CompileSuccessfully(spirv.c_str());
5545 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
5546 }
5547
5548 TEST_P(ValidateIdWithMessage, OpBranchConditional_CondIsScalarInt) {
5549 std::string spirv = BranchConditionalSetup + R"(
5550 OpSelectionMerge %end None
5551 OpBranchConditional %i0 %target_t %target_f
5552 )" + BranchConditionalTail;
5553
5554 CompileSuccessfully(spirv.c_str());
5555 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5556 EXPECT_THAT(
5557 getDiagnosticString(),
5558 HasSubstr(make_message("Condition operand for OpBranchConditional must "
5559 "be of boolean type")));
5560 }
5561
5562 TEST_P(ValidateIdWithMessage, OpBranchConditional_TrueTargetIsNotLabel) {
5563 std::string spirv = BranchConditionalSetup + R"(
5564 OpSelectionMerge %end None
5565 OpBranchConditional %true %i0 %target_f
5566 )" + BranchConditionalTail;
5567
5568 CompileSuccessfully(spirv.c_str());
5569 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5570 EXPECT_THAT(getDiagnosticString(),
5571 HasSubstr(make_message(
5572 "The 'True Label' operand for OpBranchConditional must "
5573 "be the ID of an OpLabel instruction")));
5574 }
5575
5576 TEST_P(ValidateIdWithMessage, OpBranchConditional_FalseTargetIsNotLabel) {
5577 std::string spirv = BranchConditionalSetup + R"(
5578 OpSelectionMerge %end None
5579 OpBranchConditional %true %target_t %i0
5580 )" + BranchConditionalTail;
5581
5582 CompileSuccessfully(spirv.c_str());
5583 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5584 EXPECT_THAT(getDiagnosticString(),
5585 HasSubstr(make_message(
5586 "The 'False Label' operand for OpBranchConditional "
5587 "must be the ID of an OpLabel instruction")));
5588 }
5589
5590 TEST_P(ValidateIdWithMessage, OpBranchConditional_NotEnoughWeights) {
5591 std::string spirv = BranchConditionalSetup + R"(
5592 %branch_cond = OpINotEqual %bool %i0 %i1
5593 OpSelectionMerge %end None
5594 OpBranchConditional %branch_cond %target_t %target_f 1
5595 )" + BranchConditionalTail;
5596
5597 CompileSuccessfully(spirv.c_str());
5598 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5599 EXPECT_THAT(getDiagnosticString(),
5600 HasSubstr(make_message(
5601 "OpBranchConditional requires either 3 or 5 parameters")));
5602 }
5603
5604 TEST_P(ValidateIdWithMessage, OpBranchConditional_TooManyWeights) {
5605 std::string spirv = BranchConditionalSetup + R"(
5606 %branch_cond = OpINotEqual %bool %i0 %i1
5607 OpSelectionMerge %end None
5608 OpBranchConditional %branch_cond %target_t %target_f 1 2 3
5609 )" + BranchConditionalTail;
5610
5611 CompileSuccessfully(spirv.c_str());
5612 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5613 EXPECT_THAT(getDiagnosticString(),
5614 HasSubstr(make_message(
5615 "OpBranchConditional requires either 3 or 5 parameters")));
5616 }
5617
5618 TEST_P(ValidateIdWithMessage, OpBranchConditional_ConditionIsAType) {
5619 std::string spirv = BranchConditionalSetup + R"(
5620 OpBranchConditional %bool %target_t %target_f
5621 )" + BranchConditionalTail;
5622
5623 CompileSuccessfully(spirv.c_str());
5624 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5625 EXPECT_THAT(getDiagnosticString(),
5626 HasSubstr(make_message("Operand '3[%bool]' cannot be a "
5627 "type")));
5628 }
5629
5630 // TODO: OpSwitch
5631
5632 TEST_P(ValidateIdWithMessage, OpReturnValueConstantGood) {
5633 std::string spirv = kGLSL450MemoryModel + R"(
5634 %1 = OpTypeVoid
5635 %2 = OpTypeInt 32 0
5636 %3 = OpTypeFunction %2
5637 %4 = OpConstant %2 42
5638 %5 = OpFunction %2 None %3
5639 %6 = OpLabel
5640 OpReturnValue %4
5641 OpFunctionEnd)";
5642 CompileSuccessfully(spirv.c_str());
5643 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5644 }
5645
5646 TEST_P(ValidateIdWithMessage, OpReturnValueVariableGood) {
5647 std::string spirv = kGLSL450MemoryModel + R"(
5648 %1 = OpTypeVoid
5649 %2 = OpTypeInt 32 0 ;10
5650 %3 = OpTypeFunction %2
5651 %8 = OpTypePointer Function %2 ;18
5652 %4 = OpConstant %2 42 ;22
5653 %5 = OpFunction %2 None %3 ;27
5654 %6 = OpLabel ;29
5655 %7 = OpVariable %8 Function %4 ;34
5656 %9 = OpLoad %2 %7
5657 OpReturnValue %9 ;36
5658 OpFunctionEnd)";
5659 CompileSuccessfully(spirv.c_str());
5660 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5661 }
5662
5663 TEST_P(ValidateIdWithMessage, OpReturnValueExpressionGood) {
5664 std::string spirv = kGLSL450MemoryModel + R"(
5665 %1 = OpTypeVoid
5666 %2 = OpTypeInt 32 0
5667 %3 = OpTypeFunction %2
5668 %4 = OpConstant %2 42
5669 %5 = OpFunction %2 None %3
5670 %6 = OpLabel
5671 %7 = OpIAdd %2 %4 %4
5672 OpReturnValue %7
5673 OpFunctionEnd)";
5674 CompileSuccessfully(spirv.c_str());
5675 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5676 }
5677
5678 TEST_P(ValidateIdWithMessage, OpReturnValueIsType) {
5679 std::string spirv = kGLSL450MemoryModel + R"(
5680 %1 = OpTypeVoid
5681 %2 = OpTypeInt 32 0
5682 %3 = OpTypeFunction %2
5683 %5 = OpFunction %2 None %3
5684 %6 = OpLabel
5685 OpReturnValue %1
5686 OpFunctionEnd)";
5687 CompileSuccessfully(spirv.c_str());
5688 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5689 EXPECT_THAT(getDiagnosticString(),
5690 HasSubstr(make_message("Operand '1[%void]' cannot be a "
5691 "type")));
5692 }
5693
5694 TEST_P(ValidateIdWithMessage, OpReturnValueIsLabel) {
5695 std::string spirv = kGLSL450MemoryModel + R"(
5696 %1 = OpTypeVoid
5697 %2 = OpTypeInt 32 0
5698 %3 = OpTypeFunction %2
5699 %5 = OpFunction %2 None %3
5700 %6 = OpLabel
5701 OpReturnValue %6
5702 OpFunctionEnd)";
5703 CompileSuccessfully(spirv.c_str());
5704 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5705 EXPECT_THAT(getDiagnosticString(),
5706 HasSubstr(make_message("Operand '5[%5]' requires a type")));
5707 }
5708
5709 TEST_P(ValidateIdWithMessage, OpReturnValueIsVoid) {
5710 std::string spirv = kGLSL450MemoryModel + R"(
5711 %1 = OpTypeVoid
5712 %2 = OpTypeInt 32 0
5713 %3 = OpTypeFunction %1
5714 %5 = OpFunction %1 None %3
5715 %6 = OpLabel
5716 %7 = OpFunctionCall %1 %5
5717 OpReturnValue %7
5718 OpFunctionEnd)";
5719 CompileSuccessfully(spirv.c_str());
5720 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5721 EXPECT_THAT(getDiagnosticString(),
5722 HasSubstr(make_message(
5723 "OpReturnValue value's type <id> '1[%void]' is missing or "
5724 "void.")));
5725 }
5726
5727 TEST_P(ValidateIdWithMessage, OpReturnValueIsVariableInPhysical) {
5728 // It's valid to return a pointer in a physical addressing model.
5729 std::string spirv = kOpCapabilitySetup + R"(
5730 OpMemoryModel Physical32 OpenCL
5731 %1 = OpTypeVoid
5732 %2 = OpTypeInt 32 0
5733 %3 = OpTypePointer Function %2
5734 %4 = OpTypeFunction %3
5735 %5 = OpFunction %3 None %4
5736 %6 = OpLabel
5737 %7 = OpVariable %3 Function
5738 OpReturnValue %7
5739 OpFunctionEnd)";
5740 CompileSuccessfully(spirv.c_str());
5741 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5742 }
5743
5744 TEST_P(ValidateIdWithMessage, OpReturnValueIsVariableInLogical) {
5745 // It's invalid to return a pointer in a physical addressing model.
5746 std::string spirv = kOpCapabilitySetup + R"(
5747 OpMemoryModel Logical GLSL450
5748 %1 = OpTypeVoid
5749 %2 = OpTypeInt 32 0
5750 %3 = OpTypePointer Function %2
5751 %4 = OpTypeFunction %3
5752 %5 = OpFunction %3 None %4
5753 %6 = OpLabel
5754 %7 = OpVariable %3 Function
5755 OpReturnValue %7
5756 OpFunctionEnd)";
5757 CompileSuccessfully(spirv.c_str());
5758 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5759 EXPECT_THAT(
5760 getDiagnosticString(),
5761 HasSubstr(make_message("OpReturnValue value's type <id> "
5762 "'3[%_ptr_Function_uint]' is a pointer, which is "
5763 "invalid in the Logical addressing model.")));
5764 }
5765
5766 // With the VariablePointer Capability, the return value of a function is
5767 // allowed to be a pointer.
5768 TEST_P(ValidateIdWithMessage, OpReturnValueVarPtrGood) {
5769 std::ostringstream spirv;
5770 createVariablePointerSpirvProgram(&spirv,
5771 "" /* Instructions to add to "main" */,
5772 true /* Add VariablePointers Capability?*/,
5773 true /* Use Helper Function? */);
5774 CompileSuccessfully(spirv.str());
5775 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5776 }
5777
5778 // Without the VariablePointer Capability, the return value of a function is
5779 // *not* allowed to be a pointer.
5780 // Disabled since using OpSelect with pointers without VariablePointers will
5781 // fail LogicalsPass.
5782 TEST_P(ValidateIdWithMessage, DISABLED_OpReturnValueVarPtrBad) {
5783 std::ostringstream spirv;
5784 createVariablePointerSpirvProgram(&spirv,
5785 "" /* Instructions to add to "main" */,
5786 false /* Add VariablePointers Capability?*/,
5787 true /* Use Helper Function? */);
5788 CompileSuccessfully(spirv.str());
5789 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5790 EXPECT_THAT(getDiagnosticString(),
5791 HasSubstr(make_message(
5792 "OpReturnValue value's type <id> '7' is a pointer, "
5793 "which is invalid in the Logical addressing model.")));
5794 }
5795
5796 // TODO: enable when this bug is fixed:
5797 // https://cvs.khronos.org/bugzilla/show_bug.cgi?id=15404
5798 TEST_P(ValidateIdWithMessage, DISABLED_OpReturnValueIsFunction) {
5799 std::string spirv = kGLSL450MemoryModel + R"(
5800 %1 = OpTypeVoid
5801 %2 = OpTypeInt 32 0
5802 %3 = OpTypeFunction %2
5803 %5 = OpFunction %2 None %3
5804 %6 = OpLabel
5805 OpReturnValue %5
5806 OpFunctionEnd)";
5807 CompileSuccessfully(spirv.c_str());
5808 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5809 }
5810
5811 TEST_P(ValidateIdWithMessage, UndefinedTypeId) {
5812 std::string spirv = kGLSL450MemoryModel + R"(
5813 %s = OpTypeStruct %i32
5814 )";
5815 CompileSuccessfully(spirv.c_str());
5816 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5817 EXPECT_THAT(getDiagnosticString(),
5818 HasSubstr(make_message(
5819 "Operand '2[%2]' requires a previous definition")));
5820 }
5821
5822 TEST_P(ValidateIdWithMessage, UndefinedIdScope) {
5823 std::string spirv = kGLSL450MemoryModel + R"(
5824 %u32 = OpTypeInt 32 0
5825 %memsem = OpConstant %u32 0
5826 %void = OpTypeVoid
5827 %void_f = OpTypeFunction %void
5828 %f = OpFunction %void None %void_f
5829 %l = OpLabel
5830 OpMemoryBarrier %undef %memsem
5831 OpReturn
5832 OpFunctionEnd
5833 )";
5834 CompileSuccessfully(spirv.c_str());
5835 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5836 EXPECT_THAT(getDiagnosticString(),
5837 HasSubstr(make_message("ID '7[%7]' has not been "
5838 "defined")));
5839 }
5840
5841 TEST_P(ValidateIdWithMessage, UndefinedIdMemSem) {
5842 std::string spirv = kGLSL450MemoryModel + R"(
5843 %u32 = OpTypeInt 32 0
5844 %scope = OpConstant %u32 0
5845 %void = OpTypeVoid
5846 %void_f = OpTypeFunction %void
5847 %f = OpFunction %void None %void_f
5848 %l = OpLabel
5849 OpMemoryBarrier %scope %undef
5850 OpReturn
5851 OpFunctionEnd
5852 )";
5853 CompileSuccessfully(spirv.c_str());
5854 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5855 EXPECT_THAT(getDiagnosticString(),
5856 HasSubstr(make_message("ID '7[%7]' has not been "
5857 "defined")));
5858 }
5859
5860 TEST_P(ValidateIdWithMessage,
5861 KernelOpEntryPointAndOpInBoundsPtrAccessChainGood) {
5862 std::string spirv = kOpenCLMemoryModel32 + R"(
5863 OpEntryPoint Kernel %2 "simple_kernel"
5864 OpSource OpenCL_C 200000
5865 OpDecorate %3 BuiltIn GlobalInvocationId
5866 OpDecorate %3 Constant
5867 OpDecorate %4 FuncParamAttr NoCapture
5868 OpDecorate %3 LinkageAttributes "__spirv_GlobalInvocationId" Import
5869 %5 = OpTypeInt 32 0
5870 %6 = OpTypeVector %5 3
5871 %7 = OpTypePointer UniformConstant %6
5872 %3 = OpVariable %7 UniformConstant
5873 %8 = OpTypeVoid
5874 %9 = OpTypeStruct %5
5875 %10 = OpTypePointer CrossWorkgroup %9
5876 %11 = OpTypeFunction %8 %10
5877 %12 = OpConstant %5 0
5878 %13 = OpTypePointer CrossWorkgroup %5
5879 %14 = OpConstant %5 42
5880 %2 = OpFunction %8 None %11
5881 %4 = OpFunctionParameter %10
5882 %15 = OpLabel
5883 %16 = OpLoad %6 %3 Aligned 1
5884 %17 = OpCompositeExtract %5 %16 0
5885 %18 = OpInBoundsPtrAccessChain %13 %4 %17 %12
5886 OpStore %18 %14 Aligned 4
5887 OpReturn
5888 OpFunctionEnd)";
5889 CompileSuccessfully(spirv.c_str());
5890 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5891 }
5892
5893 TEST_P(ValidateIdWithMessage, OpPtrAccessChainGood) {
5894 std::string spirv = kOpenCLMemoryModel64 + R"(
5895 OpEntryPoint Kernel %2 "another_kernel"
5896 OpSource OpenCL_C 200000
5897 OpDecorate %3 BuiltIn GlobalInvocationId
5898 OpDecorate %3 Constant
5899 OpDecorate %4 FuncParamAttr NoCapture
5900 OpDecorate %3 LinkageAttributes "__spirv_GlobalInvocationId" Import
5901 %5 = OpTypeInt 64 0
5902 %6 = OpTypeVector %5 3
5903 %7 = OpTypePointer UniformConstant %6
5904 %3 = OpVariable %7 UniformConstant
5905 %8 = OpTypeVoid
5906 %9 = OpTypeInt 32 0
5907 %10 = OpTypeStruct %9
5908 %11 = OpTypePointer CrossWorkgroup %10
5909 %12 = OpTypeFunction %8 %11
5910 %13 = OpConstant %5 4294967295
5911 %14 = OpConstant %9 0
5912 %15 = OpTypePointer CrossWorkgroup %9
5913 %16 = OpConstant %9 42
5914 %2 = OpFunction %8 None %12
5915 %4 = OpFunctionParameter %11
5916 %17 = OpLabel
5917 %18 = OpLoad %6 %3 Aligned 1
5918 %19 = OpCompositeExtract %5 %18 0
5919 %20 = OpBitwiseAnd %5 %19 %13
5920 %21 = OpPtrAccessChain %15 %4 %20 %14
5921 OpStore %21 %16 Aligned 4
5922 OpReturn
5923 OpFunctionEnd)";
5924 CompileSuccessfully(spirv.c_str());
5925 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5926 }
5927
5928 TEST_P(ValidateIdWithMessage, StgBufOpPtrAccessChainGood) {
5929 std::string spirv = R"(
5930 OpCapability Shader
5931 OpCapability Linkage
5932 OpCapability VariablePointersStorageBuffer
5933 OpExtension "SPV_KHR_variable_pointers"
5934 OpMemoryModel Logical GLSL450
5935 OpEntryPoint GLCompute %3 ""
5936 OpDecorate %ptr ArrayStride 8
5937 %int = OpTypeInt 32 0
5938 %int_2 = OpConstant %int 2
5939 %int_4 = OpConstant %int 4
5940 %struct = OpTypeStruct %int
5941 %array = OpTypeArray %struct %int_4
5942 %ptr = OpTypePointer StorageBuffer %array
5943 %var = OpVariable %ptr StorageBuffer
5944 %1 = OpTypeVoid
5945 %2 = OpTypeFunction %1
5946 %3 = OpFunction %1 None %2
5947 %4 = OpLabel
5948 %5 = OpPtrAccessChain %ptr %var %int_2
5949 OpReturn
5950 OpFunctionEnd
5951 )";
5952 CompileSuccessfully(spirv.c_str());
5953 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5954 }
5955
5956 TEST_P(ValidateIdWithMessage, OpLoadBitcastPointerGood) {
5957 std::string spirv = kOpenCLMemoryModel64 + R"(
5958 %2 = OpTypeVoid
5959 %3 = OpTypeInt 32 0
5960 %4 = OpTypeFloat 32
5961 %5 = OpTypePointer UniformConstant %3
5962 %6 = OpTypePointer UniformConstant %4
5963 %7 = OpVariable %5 UniformConstant
5964 %8 = OpTypeFunction %2
5965 %9 = OpFunction %2 None %8
5966 %10 = OpLabel
5967 %11 = OpBitcast %6 %7
5968 %12 = OpLoad %4 %11
5969 OpReturn
5970 OpFunctionEnd)";
5971 CompileSuccessfully(spirv.c_str());
5972 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
5973 }
5974 TEST_P(ValidateIdWithMessage, OpLoadBitcastNonPointerBad) {
5975 std::string spirv = kOpenCLMemoryModel64 + R"(
5976 %2 = OpTypeVoid
5977 %3 = OpTypeInt 32 0
5978 %4 = OpTypeFloat 32
5979 %5 = OpTypePointer UniformConstant %3
5980 %6 = OpTypeFunction %2
5981 %7 = OpVariable %5 UniformConstant
5982 %8 = OpFunction %2 None %6
5983 %9 = OpLabel
5984 %10 = OpLoad %3 %7
5985 %11 = OpBitcast %4 %10
5986 %12 = OpLoad %3 %11
5987 OpReturn
5988 OpFunctionEnd)";
5989 CompileSuccessfully(spirv.c_str());
5990 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
5991 EXPECT_THAT(getDiagnosticString(),
5992 HasSubstr(make_message(
5993 "OpLoad type for pointer <id> '11[%11]' is not a pointer "
5994 "type.")));
5995 }
5996 TEST_P(ValidateIdWithMessage, OpStoreBitcastPointerGood) {
5997 std::string spirv = kOpenCLMemoryModel64 + R"(
5998 %2 = OpTypeVoid
5999 %3 = OpTypeInt 32 0
6000 %4 = OpTypeFloat 32
6001 %5 = OpTypePointer Function %3
6002 %6 = OpTypePointer Function %4
6003 %7 = OpTypeFunction %2
6004 %8 = OpConstant %3 42
6005 %9 = OpFunction %2 None %7
6006 %10 = OpLabel
6007 %11 = OpVariable %6 Function
6008 %12 = OpBitcast %5 %11
6009 OpStore %12 %8
6010 OpReturn
6011 OpFunctionEnd)";
6012 CompileSuccessfully(spirv.c_str());
6013 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
6014 }
6015 TEST_P(ValidateIdWithMessage, OpStoreBitcastNonPointerBad) {
6016 std::string spirv = kOpenCLMemoryModel64 + R"(
6017 %2 = OpTypeVoid
6018 %3 = OpTypeInt 32 0
6019 %4 = OpTypeFloat 32
6020 %5 = OpTypePointer Function %4
6021 %6 = OpTypeFunction %2
6022 %7 = OpConstant %4 42
6023 %8 = OpFunction %2 None %6
6024 %9 = OpLabel
6025 %10 = OpVariable %5 Function
6026 %11 = OpBitcast %3 %7
6027 OpStore %11 %7
6028 OpReturn
6029 OpFunctionEnd)";
6030 CompileSuccessfully(spirv.c_str());
6031 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6032 EXPECT_THAT(getDiagnosticString(),
6033 HasSubstr(make_message(
6034 "OpStore type for pointer <id> '11[%11]' is not a pointer "
6035 "type.")));
6036 }
6037
6038 // Result <id> resulting from an instruction within a function may not be used
6039 // outside that function.
6040 TEST_P(ValidateIdWithMessage, ResultIdUsedOutsideOfFunctionBad) {
6041 std::string spirv = kGLSL450MemoryModel + R"(
6042 %1 = OpTypeVoid
6043 %2 = OpTypeFunction %1
6044 %3 = OpTypeInt 32 0
6045 %4 = OpTypePointer Function %3
6046 %5 = OpFunction %1 None %2
6047 %6 = OpLabel
6048 %7 = OpVariable %4 Function
6049 OpReturn
6050 OpFunctionEnd
6051 %8 = OpFunction %1 None %2
6052 %9 = OpLabel
6053 %10 = OpLoad %3 %7
6054 OpReturn
6055 OpFunctionEnd
6056 )";
6057 CompileSuccessfully(spirv.c_str());
6058 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6059 EXPECT_THAT(getDiagnosticString(),
6060 HasSubstr(make_message("ID '7[%7]' defined in block '6[%6]' does "
6061 "not dominate its use in block "
6062 "'9[%9]'")));
6063 }
6064
6065 TEST_P(ValidateIdWithMessage, SpecIdTargetNotSpecializationConstant) {
6066 std::string spirv = kGLSL450MemoryModel + R"(
6067 OpDecorate %1 SpecId 200
6068 %void = OpTypeVoid
6069 %2 = OpTypeFunction %void
6070 %int = OpTypeInt 32 0
6071 %1 = OpConstant %int 3
6072 %main = OpFunction %void None %2
6073 %4 = OpLabel
6074 OpReturnValue %1
6075 OpFunctionEnd
6076 )";
6077 CompileSuccessfully(spirv.c_str());
6078 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6079 EXPECT_THAT(
6080 getDiagnosticString(),
6081 HasSubstr(make_message("SpecId decoration on target <id> "
6082 "'1[%uint_3]' must be a scalar specialization "
6083 "constant")));
6084 }
6085
6086 TEST_P(ValidateIdWithMessage, SpecIdTargetOpSpecConstantOpBad) {
6087 std::string spirv = kGLSL450MemoryModel + R"(
6088 OpDecorate %1 SpecId 200
6089 %void = OpTypeVoid
6090 %2 = OpTypeFunction %void
6091 %int = OpTypeInt 32 0
6092 %3 = OpConstant %int 1
6093 %4 = OpConstant %int 2
6094 %1 = OpSpecConstantOp %int IAdd %3 %4
6095 %main = OpFunction %void None %2
6096 %6 = OpLabel
6097 OpReturnValue %3
6098 OpFunctionEnd
6099 )";
6100 CompileSuccessfully(spirv.c_str());
6101 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6102 EXPECT_THAT(
6103 getDiagnosticString(),
6104 HasSubstr(make_message("SpecId decoration on target <id> '1[%1]' "
6105 "must be a scalar specialization constant")));
6106 }
6107
6108 TEST_P(ValidateIdWithMessage, SpecIdTargetOpSpecConstantCompositeBad) {
6109 std::string spirv = kGLSL450MemoryModel + R"(
6110 OpDecorate %1 SpecId 200
6111 %void = OpTypeVoid
6112 %2 = OpTypeFunction %void
6113 %int = OpTypeInt 32 0
6114 %3 = OpConstant %int 1
6115 %1 = OpSpecConstantComposite %int
6116 %main = OpFunction %void None %2
6117 %4 = OpLabel
6118 OpReturnValue %3
6119 OpFunctionEnd
6120 )";
6121 CompileSuccessfully(spirv.c_str());
6122 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6123 EXPECT_THAT(
6124 getDiagnosticString(),
6125 HasSubstr(make_message("SpecId decoration on target <id> '1[%1]' "
6126 "must be a scalar specialization constant")));
6127 }
6128
6129 TEST_P(ValidateIdWithMessage, SpecIdTargetGood) {
6130 std::string spirv = kGLSL450MemoryModel + R"(
6131 OpDecorate %3 SpecId 200
6132 OpDecorate %4 SpecId 201
6133 OpDecorate %5 SpecId 202
6134 %1 = OpTypeVoid
6135 %2 = OpTypeFunction %1
6136 %int = OpTypeInt 32 0
6137 %bool = OpTypeBool
6138 %3 = OpSpecConstant %int 3
6139 %4 = OpSpecConstantTrue %bool
6140 %5 = OpSpecConstantFalse %bool
6141 %main = OpFunction %1 None %2
6142 %6 = OpLabel
6143 OpReturn
6144 OpFunctionEnd
6145 )";
6146 CompileSuccessfully(spirv.c_str());
6147 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
6148 }
6149
6150 TEST_P(ValidateIdWithMessage, CorrectErrorForShuffle) {
6151 std::string spirv = kGLSL450MemoryModel + R"(
6152 %uint = OpTypeInt 32 0
6153 %float = OpTypeFloat 32
6154 %v4float = OpTypeVector %float 4
6155 %v2float = OpTypeVector %float 2
6156 %void = OpTypeVoid
6157 %548 = OpTypeFunction %void
6158 %CS = OpFunction %void None %548
6159 %550 = OpLabel
6160 %6275 = OpUndef %v2float
6161 %6280 = OpUndef %v2float
6162 %6282 = OpVectorShuffle %v4float %6275 %6280 0 1 4 5
6163 OpReturn
6164 OpFunctionEnd
6165 )";
6166
6167 CompileSuccessfully(spirv.c_str());
6168 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6169 EXPECT_THAT(
6170 getDiagnosticString(),
6171 HasSubstr(make_message(
6172 "Component index 4 is out of bounds for combined (Vector1 + Vector2) "
6173 "size of 4.")));
6174 EXPECT_EQ(25, getErrorPosition().index);
6175 }
6176
6177 TEST_P(ValidateIdWithMessage, VoidStructMember) {
6178 const std::string spirv = kGLSL450MemoryModel + R"(
6179 %void = OpTypeVoid
6180 %struct = OpTypeStruct %void
6181 )";
6182
6183 CompileSuccessfully(spirv);
6184 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6185 EXPECT_THAT(
6186 getDiagnosticString(),
6187 HasSubstr(make_message("Structures cannot contain a void type.")));
6188 }
6189
6190 TEST_P(ValidateIdWithMessage, TypeFunctionBadUse) {
6191 std::string spirv = kGLSL450MemoryModel + R"(
6192 %1 = OpTypeVoid
6193 %2 = OpTypeFunction %1
6194 %3 = OpTypePointer Function %2
6195 %4 = OpFunction %1 None %2
6196 %5 = OpLabel
6197 OpReturn
6198 OpFunctionEnd)";
6199
6200 CompileSuccessfully(spirv);
6201 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6202 EXPECT_THAT(getDiagnosticString(),
6203 HasSubstr(make_message(
6204 "Invalid use of function type result id '2[%2]'.")));
6205 }
6206
6207 TEST_P(ValidateIdWithMessage, BadTypeId) {
6208 std::string spirv = kGLSL450MemoryModel + R"(
6209 %1 = OpTypeVoid
6210 %2 = OpTypeFunction %1
6211 %3 = OpTypeFloat 32
6212 %4 = OpConstant %3 0
6213 %5 = OpFunction %1 None %2
6214 %6 = OpLabel
6215 %7 = OpUndef %4
6216 OpReturn
6217 OpFunctionEnd
6218 )";
6219
6220 CompileSuccessfully(spirv);
6221 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
6222 EXPECT_THAT(getDiagnosticString(),
6223 HasSubstr(make_message("ID '4[%float_0]' is not a type "
6224 "id")));
6225 }
6226
6227 TEST_P(ValidateIdWithMessage, VulkanMemoryModelLoadMakePointerVisibleGood) {
6228 std::string spirv = R"(
6229 OpCapability Shader
6230 OpCapability VulkanMemoryModelKHR
6231 OpCapability Linkage
6232 OpExtension "SPV_KHR_vulkan_memory_model"
6233 OpMemoryModel Logical VulkanKHR
6234 %1 = OpTypeVoid
6235 %2 = OpTypeInt 32 0
6236 %3 = OpTypePointer Workgroup %2
6237 %4 = OpVariable %3 Workgroup
6238 %5 = OpTypeFunction %1
6239 %6 = OpConstant %2 2
6240 %7 = OpFunction %1 None %5
6241 %8 = OpLabel
6242 %9 = OpLoad %2 %4 NonPrivatePointerKHR|MakePointerVisibleKHR %6
6243 OpReturn
6244 OpFunctionEnd
6245 )";
6246
6247 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6248 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6249 }
6250
6251 TEST_P(ValidateIdWithMessage,
6252 VulkanMemoryModelLoadMakePointerVisibleMissingNonPrivatePointer) {
6253 std::string spirv = R"(
6254 OpCapability Shader
6255 OpCapability VulkanMemoryModelKHR
6256 OpCapability Linkage
6257 OpExtension "SPV_KHR_vulkan_memory_model"
6258 OpMemoryModel Logical VulkanKHR
6259 %1 = OpTypeVoid
6260 %2 = OpTypeInt 32 0
6261 %3 = OpTypePointer Workgroup %2
6262 %4 = OpVariable %3 Workgroup
6263 %5 = OpTypeFunction %1
6264 %6 = OpConstant %2 2
6265 %7 = OpFunction %1 None %5
6266 %8 = OpLabel
6267 %9 = OpLoad %2 %4 MakePointerVisibleKHR %6
6268 OpReturn
6269 OpFunctionEnd
6270 )";
6271
6272 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6273 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6274 EXPECT_THAT(
6275 getDiagnosticString(),
6276 HasSubstr(make_message("NonPrivatePointerKHR must be specified if "
6277 "MakePointerVisibleKHR is specified.")));
6278 }
6279
6280 TEST_P(ValidateIdWithMessage,
6281 VulkanMemoryModelLoadNonPrivatePointerBadStorageClass) {
6282 std::string spirv = R"(
6283 OpCapability Shader
6284 OpCapability VulkanMemoryModelKHR
6285 OpCapability Linkage
6286 OpExtension "SPV_KHR_vulkan_memory_model"
6287 OpMemoryModel Logical VulkanKHR
6288 %1 = OpTypeVoid
6289 %2 = OpTypeInt 32 0
6290 %3 = OpTypePointer Private %2
6291 %4 = OpVariable %3 Private
6292 %5 = OpTypeFunction %1
6293 %6 = OpConstant %2 2
6294 %7 = OpFunction %1 None %5
6295 %8 = OpLabel
6296 %9 = OpLoad %2 %4 NonPrivatePointerKHR
6297 OpReturn
6298 OpFunctionEnd
6299 )";
6300
6301 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6302 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6303 EXPECT_THAT(getDiagnosticString(),
6304 HasSubstr(make_message(
6305 "NonPrivatePointerKHR requires a pointer in Uniform, "
6306 "Workgroup, CrossWorkgroup, Generic, Image or "
6307 "StorageBuffer storage classes.")));
6308 }
6309
6310 TEST_P(ValidateIdWithMessage,
6311 VulkanMemoryModelLoadMakePointerAvailableCannotBeUsed) {
6312 std::string spirv = R"(
6313 OpCapability Shader
6314 OpCapability VulkanMemoryModelKHR
6315 OpCapability Linkage
6316 OpExtension "SPV_KHR_vulkan_memory_model"
6317 OpMemoryModel Logical VulkanKHR
6318 %1 = OpTypeVoid
6319 %2 = OpTypeInt 32 0
6320 %3 = OpTypePointer Workgroup %2
6321 %4 = OpVariable %3 Workgroup
6322 %5 = OpTypeFunction %1
6323 %6 = OpConstant %2 2
6324 %7 = OpFunction %1 None %5
6325 %8 = OpLabel
6326 %9 = OpLoad %2 %4 NonPrivatePointerKHR|MakePointerAvailableKHR %6
6327 OpReturn
6328 OpFunctionEnd
6329 )";
6330
6331 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6332 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6333 EXPECT_THAT(getDiagnosticString(),
6334 HasSubstr(make_message(
6335 "MakePointerAvailableKHR cannot be used with OpLoad")));
6336 }
6337
6338 TEST_P(ValidateIdWithMessage, VulkanMemoryModelStoreMakePointerAvailableGood) {
6339 std::string spirv = R"(
6340 OpCapability Shader
6341 OpCapability VulkanMemoryModelKHR
6342 OpCapability Linkage
6343 OpExtension "SPV_KHR_vulkan_memory_model"
6344 OpMemoryModel Logical VulkanKHR
6345 %1 = OpTypeVoid
6346 %2 = OpTypeInt 32 0
6347 %3 = OpTypePointer Uniform %2
6348 %4 = OpVariable %3 Uniform
6349 %5 = OpTypeFunction %1
6350 %6 = OpConstant %2 5
6351 %7 = OpFunction %1 None %5
6352 %8 = OpLabel
6353 OpStore %4 %6 NonPrivatePointerKHR|MakePointerAvailableKHR %6
6354 OpReturn
6355 OpFunctionEnd
6356 )";
6357
6358 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6359 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6360 }
6361
6362 TEST_P(ValidateIdWithMessage,
6363 VulkanMemoryModelStoreMakePointerAvailableMissingNonPrivatePointer) {
6364 std::string spirv = R"(
6365 OpCapability Shader
6366 OpCapability VulkanMemoryModelKHR
6367 OpCapability Linkage
6368 OpExtension "SPV_KHR_vulkan_memory_model"
6369 OpMemoryModel Logical VulkanKHR
6370 %1 = OpTypeVoid
6371 %2 = OpTypeInt 32 0
6372 %3 = OpTypePointer Uniform %2
6373 %4 = OpVariable %3 Uniform
6374 %5 = OpTypeFunction %1
6375 %6 = OpConstant %2 5
6376 %7 = OpFunction %1 None %5
6377 %8 = OpLabel
6378 OpStore %4 %6 MakePointerAvailableKHR %6
6379 OpReturn
6380 OpFunctionEnd
6381 )";
6382
6383 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6384 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6385 EXPECT_THAT(
6386 getDiagnosticString(),
6387 HasSubstr(make_message("NonPrivatePointerKHR must be specified if "
6388 "MakePointerAvailableKHR is specified.")));
6389 }
6390
6391 TEST_P(ValidateIdWithMessage,
6392 VulkanMemoryModelStoreNonPrivatePointerBadStorageClass) {
6393 std::string spirv = R"(
6394 OpCapability Shader
6395 OpCapability VulkanMemoryModelKHR
6396 OpCapability Linkage
6397 OpExtension "SPV_KHR_vulkan_memory_model"
6398 OpMemoryModel Logical VulkanKHR
6399 %1 = OpTypeVoid
6400 %2 = OpTypeInt 32 0
6401 %3 = OpTypePointer Output %2
6402 %4 = OpVariable %3 Output
6403 %5 = OpTypeFunction %1
6404 %6 = OpConstant %2 5
6405 %7 = OpFunction %1 None %5
6406 %8 = OpLabel
6407 OpStore %4 %6 NonPrivatePointerKHR
6408 OpReturn
6409 OpFunctionEnd
6410 )";
6411
6412 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6413 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6414 EXPECT_THAT(getDiagnosticString(),
6415 HasSubstr(make_message(
6416 "NonPrivatePointerKHR requires a pointer in Uniform, "
6417 "Workgroup, CrossWorkgroup, Generic, Image or "
6418 "StorageBuffer storage classes.")));
6419 }
6420
6421 TEST_P(ValidateIdWithMessage,
6422 VulkanMemoryModelStoreMakePointerVisibleCannotBeUsed) {
6423 std::string spirv = R"(
6424 OpCapability Shader
6425 OpCapability VulkanMemoryModelKHR
6426 OpCapability Linkage
6427 OpExtension "SPV_KHR_vulkan_memory_model"
6428 OpMemoryModel Logical VulkanKHR
6429 %1 = OpTypeVoid
6430 %2 = OpTypeInt 32 0
6431 %3 = OpTypePointer Uniform %2
6432 %4 = OpVariable %3 Uniform
6433 %5 = OpTypeFunction %1
6434 %6 = OpConstant %2 5
6435 %7 = OpFunction %1 None %5
6436 %8 = OpLabel
6437 OpStore %4 %6 NonPrivatePointerKHR|MakePointerVisibleKHR %6
6438 OpReturn
6439 OpFunctionEnd
6440 )";
6441
6442 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6443 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6444 EXPECT_THAT(getDiagnosticString(),
6445 HasSubstr(make_message(
6446 "MakePointerVisibleKHR cannot be used with OpStore.")));
6447 }
6448
6449 TEST_P(ValidateIdWithMessage, VulkanMemoryModelCopyMemoryAvailable) {
6450 std::string spirv = R"(
6451 OpCapability Shader
6452 OpCapability Linkage
6453 OpCapability VulkanMemoryModelKHR
6454 OpExtension "SPV_KHR_vulkan_memory_model"
6455 OpMemoryModel Logical VulkanKHR
6456 %1 = OpTypeVoid
6457 %2 = OpTypeInt 32 0
6458 %3 = OpTypePointer Workgroup %2
6459 %4 = OpVariable %3 Workgroup
6460 %5 = OpTypePointer Uniform %2
6461 %6 = OpVariable %5 Uniform
6462 %7 = OpConstant %2 2
6463 %8 = OpConstant %2 5
6464 %9 = OpTypeFunction %1
6465 %10 = OpFunction %1 None %9
6466 %11 = OpLabel
6467 OpCopyMemory %4 %6 NonPrivatePointerKHR|MakePointerAvailableKHR %7
6468 OpReturn
6469 OpFunctionEnd
6470 )";
6471
6472 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6473 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6474 }
6475
6476 TEST_P(ValidateIdWithMessage, VulkanMemoryModelCopyMemoryVisible) {
6477 std::string spirv = R"(
6478 OpCapability Shader
6479 OpCapability Linkage
6480 OpCapability VulkanMemoryModelKHR
6481 OpExtension "SPV_KHR_vulkan_memory_model"
6482 OpMemoryModel Logical VulkanKHR
6483 %1 = OpTypeVoid
6484 %2 = OpTypeInt 32 0
6485 %3 = OpTypePointer Workgroup %2
6486 %4 = OpVariable %3 Workgroup
6487 %5 = OpTypePointer Uniform %2
6488 %6 = OpVariable %5 Uniform
6489 %7 = OpConstant %2 2
6490 %8 = OpConstant %2 5
6491 %9 = OpTypeFunction %1
6492 %10 = OpFunction %1 None %9
6493 %11 = OpLabel
6494 OpCopyMemory %4 %6 NonPrivatePointerKHR|MakePointerVisibleKHR %8
6495 OpReturn
6496 OpFunctionEnd
6497 )";
6498
6499 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6500 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6501 }
6502
6503 TEST_P(ValidateIdWithMessage, VulkanMemoryModelCopyMemoryAvailableAndVisible) {
6504 std::string spirv = R"(
6505 OpCapability Shader
6506 OpCapability Linkage
6507 OpCapability VulkanMemoryModelKHR
6508 OpExtension "SPV_KHR_vulkan_memory_model"
6509 OpMemoryModel Logical VulkanKHR
6510 %1 = OpTypeVoid
6511 %2 = OpTypeInt 32 0
6512 %3 = OpTypePointer Workgroup %2
6513 %4 = OpVariable %3 Workgroup
6514 %5 = OpTypePointer Uniform %2
6515 %6 = OpVariable %5 Uniform
6516 %7 = OpConstant %2 2
6517 %8 = OpConstant %2 5
6518 %9 = OpTypeFunction %1
6519 %10 = OpFunction %1 None %9
6520 %11 = OpLabel
6521 OpCopyMemory %4 %6 NonPrivatePointerKHR|MakePointerAvailableKHR|MakePointerVisibleKHR %7 %8
6522 OpReturn
6523 OpFunctionEnd
6524 )";
6525
6526 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6527 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6528 }
6529
6530 TEST_P(ValidateIdWithMessage,
6531 VulkanMemoryModelCopyMemoryAvailableMissingNonPrivatePointer) {
6532 std::string spirv = R"(
6533 OpCapability Shader
6534 OpCapability Linkage
6535 OpCapability VulkanMemoryModelKHR
6536 OpExtension "SPV_KHR_vulkan_memory_model"
6537 OpMemoryModel Logical VulkanKHR
6538 %1 = OpTypeVoid
6539 %2 = OpTypeInt 32 0
6540 %3 = OpTypePointer Workgroup %2
6541 %4 = OpVariable %3 Workgroup
6542 %5 = OpTypePointer Uniform %2
6543 %6 = OpVariable %5 Uniform
6544 %7 = OpConstant %2 2
6545 %8 = OpConstant %2 5
6546 %9 = OpTypeFunction %1
6547 %10 = OpFunction %1 None %9
6548 %11 = OpLabel
6549 OpCopyMemory %4 %6 MakePointerAvailableKHR %7
6550 OpReturn
6551 OpFunctionEnd
6552 )";
6553
6554 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6555 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6556 EXPECT_THAT(
6557 getDiagnosticString(),
6558 HasSubstr(make_message("NonPrivatePointerKHR must be specified if "
6559 "MakePointerAvailableKHR is specified.")));
6560 }
6561
6562 TEST_P(ValidateIdWithMessage,
6563 VulkanMemoryModelCopyMemoryVisibleMissingNonPrivatePointer) {
6564 std::string spirv = R"(
6565 OpCapability Shader
6566 OpCapability Linkage
6567 OpCapability VulkanMemoryModelKHR
6568 OpExtension "SPV_KHR_vulkan_memory_model"
6569 OpMemoryModel Logical VulkanKHR
6570 %1 = OpTypeVoid
6571 %2 = OpTypeInt 32 0
6572 %3 = OpTypePointer Workgroup %2
6573 %4 = OpVariable %3 Workgroup
6574 %5 = OpTypePointer Uniform %2
6575 %6 = OpVariable %5 Uniform
6576 %7 = OpConstant %2 2
6577 %8 = OpConstant %2 5
6578 %9 = OpTypeFunction %1
6579 %10 = OpFunction %1 None %9
6580 %11 = OpLabel
6581 OpCopyMemory %4 %6 MakePointerVisibleKHR %8
6582 OpReturn
6583 OpFunctionEnd
6584 )";
6585
6586 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6587 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6588 EXPECT_THAT(
6589 getDiagnosticString(),
6590 HasSubstr(make_message("NonPrivatePointerKHR must be specified if "
6591 "MakePointerVisibleKHR is specified.")));
6592 }
6593
6594 TEST_P(ValidateIdWithMessage,
6595 VulkanMemoryModelCopyMemoryAvailableBadStorageClass) {
6596 std::string spirv = R"(
6597 OpCapability Shader
6598 OpCapability Linkage
6599 OpCapability VulkanMemoryModelKHR
6600 OpExtension "SPV_KHR_vulkan_memory_model"
6601 OpMemoryModel Logical VulkanKHR
6602 %1 = OpTypeVoid
6603 %2 = OpTypeInt 32 0
6604 %3 = OpTypePointer Output %2
6605 %4 = OpVariable %3 Output
6606 %5 = OpTypePointer Uniform %2
6607 %6 = OpVariable %5 Uniform
6608 %7 = OpConstant %2 2
6609 %8 = OpConstant %2 5
6610 %9 = OpTypeFunction %1
6611 %10 = OpFunction %1 None %9
6612 %11 = OpLabel
6613 OpCopyMemory %4 %6 NonPrivatePointerKHR
6614 OpReturn
6615 OpFunctionEnd
6616 )";
6617
6618 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6619 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6620 EXPECT_THAT(getDiagnosticString(),
6621 HasSubstr(make_message(
6622 "NonPrivatePointerKHR requires a pointer in Uniform, "
6623 "Workgroup, CrossWorkgroup, Generic, Image or "
6624 "StorageBuffer storage classes.")));
6625 }
6626
6627 TEST_P(ValidateIdWithMessage,
6628 VulkanMemoryModelCopyMemoryVisibleBadStorageClass) {
6629 std::string spirv = R"(
6630 OpCapability Shader
6631 OpCapability Linkage
6632 OpCapability VulkanMemoryModelKHR
6633 OpExtension "SPV_KHR_vulkan_memory_model"
6634 OpMemoryModel Logical VulkanKHR
6635 %1 = OpTypeVoid
6636 %2 = OpTypeInt 32 0
6637 %3 = OpTypePointer Workgroup %2
6638 %4 = OpVariable %3 Workgroup
6639 %5 = OpTypePointer Input %2
6640 %6 = OpVariable %5 Input
6641 %7 = OpConstant %2 2
6642 %8 = OpConstant %2 5
6643 %9 = OpTypeFunction %1
6644 %10 = OpFunction %1 None %9
6645 %11 = OpLabel
6646 OpCopyMemory %4 %6 NonPrivatePointerKHR
6647 OpReturn
6648 OpFunctionEnd
6649 )";
6650
6651 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6652 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6653 EXPECT_THAT(getDiagnosticString(),
6654 HasSubstr(make_message(
6655 "NonPrivatePointerKHR requires a pointer in Uniform, "
6656 "Workgroup, CrossWorkgroup, Generic, Image or "
6657 "StorageBuffer storage classes.")));
6658 }
6659
6660 TEST_P(ValidateIdWithMessage, VulkanMemoryModelCopyMemorySizedAvailable) {
6661 std::string spirv = R"(
6662 OpCapability Shader
6663 OpCapability Linkage
6664 OpCapability Addresses
6665 OpCapability VulkanMemoryModelKHR
6666 OpExtension "SPV_KHR_vulkan_memory_model"
6667 OpMemoryModel Logical VulkanKHR
6668 %1 = OpTypeVoid
6669 %2 = OpTypeInt 32 0
6670 %3 = OpTypePointer Workgroup %2
6671 %4 = OpVariable %3 Workgroup
6672 %5 = OpTypePointer Uniform %2
6673 %6 = OpVariable %5 Uniform
6674 %7 = OpConstant %2 2
6675 %8 = OpConstant %2 5
6676 %9 = OpTypeFunction %1
6677 %12 = OpConstant %2 4
6678 %10 = OpFunction %1 None %9
6679 %11 = OpLabel
6680 OpCopyMemorySized %4 %6 %12 NonPrivatePointerKHR|MakePointerAvailableKHR %7
6681 OpReturn
6682 OpFunctionEnd
6683 )";
6684
6685 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6686 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6687 }
6688
6689 TEST_P(ValidateIdWithMessage, VulkanMemoryModelCopyMemorySizedVisible) {
6690 std::string spirv = R"(
6691 OpCapability Shader
6692 OpCapability Linkage
6693 OpCapability Addresses
6694 OpCapability VulkanMemoryModelKHR
6695 OpExtension "SPV_KHR_vulkan_memory_model"
6696 OpMemoryModel Logical VulkanKHR
6697 %1 = OpTypeVoid
6698 %2 = OpTypeInt 32 0
6699 %3 = OpTypePointer Workgroup %2
6700 %4 = OpVariable %3 Workgroup
6701 %5 = OpTypePointer Uniform %2
6702 %6 = OpVariable %5 Uniform
6703 %7 = OpConstant %2 2
6704 %8 = OpConstant %2 5
6705 %12 = OpConstant %2 4
6706 %9 = OpTypeFunction %1
6707 %10 = OpFunction %1 None %9
6708 %11 = OpLabel
6709 OpCopyMemorySized %4 %6 %12 NonPrivatePointerKHR|MakePointerVisibleKHR %8
6710 OpReturn
6711 OpFunctionEnd
6712 )";
6713
6714 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6715 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6716 }
6717
6718 TEST_P(ValidateIdWithMessage,
6719 VulkanMemoryModelCopyMemorySizedAvailableAndVisible) {
6720 std::string spirv = R"(
6721 OpCapability Shader
6722 OpCapability Linkage
6723 OpCapability Addresses
6724 OpCapability VulkanMemoryModelKHR
6725 OpExtension "SPV_KHR_vulkan_memory_model"
6726 OpMemoryModel Logical VulkanKHR
6727 %1 = OpTypeVoid
6728 %2 = OpTypeInt 32 0
6729 %3 = OpTypePointer Workgroup %2
6730 %4 = OpVariable %3 Workgroup
6731 %5 = OpTypePointer Uniform %2
6732 %6 = OpVariable %5 Uniform
6733 %7 = OpConstant %2 2
6734 %8 = OpConstant %2 5
6735 %12 = OpConstant %2 4
6736 %9 = OpTypeFunction %1
6737 %10 = OpFunction %1 None %9
6738 %11 = OpLabel
6739 OpCopyMemorySized %4 %6 %12 NonPrivatePointerKHR|MakePointerAvailableKHR|MakePointerVisibleKHR %7 %8
6740 OpReturn
6741 OpFunctionEnd
6742 )";
6743
6744 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6745 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6746 }
6747
6748 TEST_P(ValidateIdWithMessage,
6749 VulkanMemoryModelCopyMemorySizedAvailableMissingNonPrivatePointer) {
6750 std::string spirv = R"(
6751 OpCapability Shader
6752 OpCapability Linkage
6753 OpCapability Addresses
6754 OpCapability VulkanMemoryModelKHR
6755 OpExtension "SPV_KHR_vulkan_memory_model"
6756 OpMemoryModel Logical VulkanKHR
6757 %1 = OpTypeVoid
6758 %2 = OpTypeInt 32 0
6759 %3 = OpTypePointer Workgroup %2
6760 %4 = OpVariable %3 Workgroup
6761 %5 = OpTypePointer Uniform %2
6762 %6 = OpVariable %5 Uniform
6763 %7 = OpConstant %2 2
6764 %8 = OpConstant %2 5
6765 %12 = OpConstant %2 4
6766 %9 = OpTypeFunction %1
6767 %10 = OpFunction %1 None %9
6768 %11 = OpLabel
6769 OpCopyMemorySized %4 %6 %12 MakePointerAvailableKHR %7
6770 OpReturn
6771 OpFunctionEnd
6772 )";
6773
6774 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6775 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6776 EXPECT_THAT(
6777 getDiagnosticString(),
6778 HasSubstr(make_message("NonPrivatePointerKHR must be specified if "
6779 "MakePointerAvailableKHR is specified.")));
6780 }
6781
6782 TEST_P(ValidateIdWithMessage,
6783 VulkanMemoryModelCopyMemorySizedVisibleMissingNonPrivatePointer) {
6784 std::string spirv = R"(
6785 OpCapability Shader
6786 OpCapability Linkage
6787 OpCapability Addresses
6788 OpCapability VulkanMemoryModelKHR
6789 OpExtension "SPV_KHR_vulkan_memory_model"
6790 OpMemoryModel Logical VulkanKHR
6791 %1 = OpTypeVoid
6792 %2 = OpTypeInt 32 0
6793 %3 = OpTypePointer Workgroup %2
6794 %4 = OpVariable %3 Workgroup
6795 %5 = OpTypePointer Uniform %2
6796 %6 = OpVariable %5 Uniform
6797 %7 = OpConstant %2 2
6798 %8 = OpConstant %2 5
6799 %12 = OpConstant %2 4
6800 %9 = OpTypeFunction %1
6801 %10 = OpFunction %1 None %9
6802 %11 = OpLabel
6803 OpCopyMemorySized %4 %6 %12 MakePointerVisibleKHR %8
6804 OpReturn
6805 OpFunctionEnd
6806 )";
6807
6808 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6809 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6810 EXPECT_THAT(
6811 getDiagnosticString(),
6812 HasSubstr(make_message("NonPrivatePointerKHR must be specified if "
6813 "MakePointerVisibleKHR is specified.")));
6814 }
6815
6816 TEST_P(ValidateIdWithMessage,
6817 VulkanMemoryModelCopyMemorySizedAvailableBadStorageClass) {
6818 std::string spirv = R"(
6819 OpCapability Shader
6820 OpCapability Linkage
6821 OpCapability Addresses
6822 OpCapability VulkanMemoryModelKHR
6823 OpExtension "SPV_KHR_vulkan_memory_model"
6824 OpMemoryModel Logical VulkanKHR
6825 %1 = OpTypeVoid
6826 %2 = OpTypeInt 32 0
6827 %3 = OpTypePointer Output %2
6828 %4 = OpVariable %3 Output
6829 %5 = OpTypePointer Uniform %2
6830 %6 = OpVariable %5 Uniform
6831 %7 = OpConstant %2 2
6832 %8 = OpConstant %2 5
6833 %12 = OpConstant %2 4
6834 %9 = OpTypeFunction %1
6835 %10 = OpFunction %1 None %9
6836 %11 = OpLabel
6837 OpCopyMemorySized %4 %6 %12 NonPrivatePointerKHR
6838 OpReturn
6839 OpFunctionEnd
6840 )";
6841
6842 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6843 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6844 EXPECT_THAT(getDiagnosticString(),
6845 HasSubstr(make_message(
6846 "NonPrivatePointerKHR requires a pointer in Uniform, "
6847 "Workgroup, CrossWorkgroup, Generic, Image or "
6848 "StorageBuffer storage classes.")));
6849 }
6850
6851 TEST_P(ValidateIdWithMessage,
6852 VulkanMemoryModelCopyMemorySizedVisibleBadStorageClass) {
6853 std::string spirv = R"(
6854 OpCapability Shader
6855 OpCapability Linkage
6856 OpCapability Addresses
6857 OpCapability VulkanMemoryModelKHR
6858 OpExtension "SPV_KHR_vulkan_memory_model"
6859 OpMemoryModel Logical VulkanKHR
6860 %1 = OpTypeVoid
6861 %2 = OpTypeInt 32 0
6862 %3 = OpTypePointer Workgroup %2
6863 %4 = OpVariable %3 Workgroup
6864 %5 = OpTypePointer Input %2
6865 %6 = OpVariable %5 Input
6866 %7 = OpConstant %2 2
6867 %8 = OpConstant %2 5
6868 %12 = OpConstant %2 4
6869 %9 = OpTypeFunction %1
6870 %10 = OpFunction %1 None %9
6871 %11 = OpLabel
6872 OpCopyMemorySized %4 %6 %12 NonPrivatePointerKHR
6873 OpReturn
6874 OpFunctionEnd
6875 )";
6876
6877 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6878 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6879 EXPECT_THAT(getDiagnosticString(),
6880 HasSubstr(make_message(
6881 "NonPrivatePointerKHR requires a pointer in Uniform, "
6882 "Workgroup, CrossWorkgroup, Generic, Image or "
6883 "StorageBuffer storage classes.")));
6884 }
6885
6886 TEST_P(ValidateIdWithMessage, IdDefInUnreachableBlock1) {
6887 const std::string spirv = kNoKernelGLSL450MemoryModel + R"(
6888 %1 = OpTypeVoid
6889 %2 = OpTypeFunction %1
6890 %3 = OpTypeFloat 32
6891 %4 = OpTypeFunction %3
6892 %5 = OpFunction %1 None %2
6893 %6 = OpLabel
6894 OpReturn
6895 %7 = OpLabel
6896 %8 = OpFunctionCall %3 %9
6897 OpUnreachable
6898 OpFunctionEnd
6899 %9 = OpFunction %3 None %4
6900 %10 = OpLabel
6901 OpReturnValue %8
6902 OpFunctionEnd
6903 )";
6904
6905 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6906 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6907 EXPECT_THAT(getDiagnosticString(),
6908 HasSubstr(make_message(
6909 "ID '8[%8]' defined in block '7[%7]' does not dominate its "
6910 "use in block '10[%10]'\n %10 = OpLabel")));
6911 }
6912
6913 TEST_P(ValidateIdWithMessage, IdDefInUnreachableBlock2) {
6914 const std::string spirv = kNoKernelGLSL450MemoryModel + R"(
6915 %1 = OpTypeVoid
6916 %2 = OpTypeFunction %1
6917 %3 = OpTypeFloat 32
6918 %4 = OpTypeFunction %3
6919 %5 = OpFunction %1 None %2
6920 %6 = OpLabel
6921 OpReturn
6922 %7 = OpLabel
6923 %8 = OpFunctionCall %3 %9
6924 OpUnreachable
6925 OpFunctionEnd
6926 %9 = OpFunction %3 None %4
6927 %10 = OpLabel
6928 OpReturnValue %8
6929 OpFunctionEnd
6930 )";
6931
6932 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6933 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6934 EXPECT_THAT(getDiagnosticString(),
6935 HasSubstr(make_message(
6936 "ID '8[%8]' defined in block '7[%7]' does not dominate its "
6937 "use in block '10[%10]'\n %10 = OpLabel")));
6938 }
6939
6940 TEST_P(ValidateIdWithMessage, IdDefInUnreachableBlock3) {
6941 const std::string spirv = kNoKernelGLSL450MemoryModel + R"(
6942 %1 = OpTypeVoid
6943 %2 = OpTypeFunction %1
6944 %3 = OpTypeFloat 32
6945 %4 = OpTypeFunction %3
6946 %5 = OpFunction %1 None %2
6947 %6 = OpLabel
6948 OpReturn
6949 %7 = OpLabel
6950 %8 = OpFunctionCall %3 %9
6951 OpReturn
6952 OpFunctionEnd
6953 %9 = OpFunction %3 None %4
6954 %10 = OpLabel
6955 OpReturnValue %8
6956 OpFunctionEnd
6957 )";
6958
6959 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6960 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6961 EXPECT_THAT(getDiagnosticString(),
6962 HasSubstr(make_message(
6963 "ID '8[%8]' defined in block '7[%7]' does not dominate its "
6964 "use in block '10[%10]'\n %10 = OpLabel")));
6965 }
6966
6967 TEST_P(ValidateIdWithMessage, IdDefInUnreachableBlock4) {
6968 const std::string spirv = kNoKernelGLSL450MemoryModel + R"(
6969 %1 = OpTypeVoid
6970 %2 = OpTypeFunction %1
6971 %3 = OpTypeFloat 32
6972 %4 = OpTypeFunction %3
6973 %5 = OpFunction %1 None %2
6974 %6 = OpLabel
6975 OpReturn
6976 %7 = OpLabel
6977 %8 = OpUndef %3
6978 %9 = OpCopyObject %3 %8
6979 OpReturn
6980 OpFunctionEnd
6981 )";
6982
6983 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
6984 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
6985 }
6986
6987 TEST_P(ValidateIdWithMessage, IdDefInUnreachableBlock5) {
6988 const std::string spirv = kNoKernelGLSL450MemoryModel + R"(
6989 %1 = OpTypeVoid
6990 %2 = OpTypeFunction %1
6991 %3 = OpTypeFloat 32
6992 %4 = OpTypeFunction %3
6993 %5 = OpFunction %1 None %2
6994 %6 = OpLabel
6995 OpReturn
6996 %7 = OpLabel
6997 %8 = OpUndef %3
6998 OpBranch %9
6999 %9 = OpLabel
7000 %10 = OpCopyObject %3 %8
7001 OpReturn
7002 OpFunctionEnd
7003 )";
7004
7005 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
7006 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
7007 }
7008
7009 TEST_P(ValidateIdWithMessage, IdDefInUnreachableBlock6) {
7010 const std::string spirv = kNoKernelGLSL450MemoryModel + R"(
7011 %1 = OpTypeVoid
7012 %2 = OpTypeFunction %1
7013 %3 = OpTypeFloat 32
7014 %4 = OpTypeFunction %3
7015 %5 = OpFunction %1 None %2
7016 %6 = OpLabel
7017 OpBranch %7
7018 %8 = OpLabel
7019 %9 = OpUndef %3
7020 OpBranch %7
7021 %7 = OpLabel
7022 %10 = OpCopyObject %3 %9
7023 OpReturn
7024 OpFunctionEnd
7025 )";
7026
7027 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
7028 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
7029 EXPECT_THAT(getDiagnosticString(),
7030 HasSubstr(make_message(
7031 "ID '9[%9]' defined in block '8[%8]' does not dominate its "
7032 "use in block '7[%7]'\n %7 = OpLabel")));
7033 }
7034
7035 TEST_P(ValidateIdWithMessage, ReachableDefUnreachableUse) {
7036 const std::string spirv = kNoKernelGLSL450MemoryModel + R"(
7037 %1 = OpTypeVoid
7038 %2 = OpTypeFunction %1
7039 %3 = OpTypeFloat 32
7040 %4 = OpTypeFunction %3
7041 %5 = OpFunction %1 None %2
7042 %6 = OpLabel
7043 %7 = OpUndef %3
7044 OpReturn
7045 %8 = OpLabel
7046 %9 = OpCopyObject %3 %7
7047 OpReturn
7048 OpFunctionEnd
7049 )";
7050
7051 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
7052 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
7053 }
7054
7055 TEST_P(ValidateIdWithMessage, UnreachableDefUsedInPhi) {
7056 const std::string spirv = kNoKernelGLSL450MemoryModel + R"(
7057 %void = OpTypeVoid
7058 %3 = OpTypeFunction %void
7059 %float = OpTypeFloat 32
7060 %bool = OpTypeBool
7061 %6 = OpTypeFunction %float
7062 %1 = OpFunction %void None %3
7063 %7 = OpLabel
7064 %8 = OpUndef %bool
7065 OpSelectionMerge %9 None
7066 OpBranchConditional %8 %10 %9
7067 %10 = OpLabel
7068 %11 = OpUndef %float
7069 OpBranch %9
7070 %12 = OpLabel
7071 %13 = OpUndef %float
7072 OpUnreachable
7073 %9 = OpLabel
7074 %14 = OpPhi %float %11 %10 %13 %7
7075 OpReturn
7076 OpFunctionEnd
7077 )";
7078
7079 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
7080 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
7081 EXPECT_THAT(
7082 getDiagnosticString(),
7083 HasSubstr(make_message(
7084 "In OpPhi instruction '14[%14]', ID '13[%13]' definition does not "
7085 "dominate its parent '7[%7]'\n %14 = OpPhi %float %11 %10 %13 "
7086 "%7")));
7087 }
7088
7089 TEST_P(ValidateIdWithMessage, OpTypeForwardPointerNotAPointerType) {
7090 std::string spirv = R"(
7091 OpCapability GenericPointer
7092 OpCapability VariablePointersStorageBuffer
7093 OpMemoryModel Logical GLSL450
7094 OpEntryPoint Fragment %1 "main"
7095 OpExecutionMode %1 OriginLowerLeft
7096 OpTypeForwardPointer %2 CrossWorkgroup
7097 %2 = OpTypeVoid
7098 %3 = OpTypeFunction %2
7099 %1 = OpFunction %2 DontInline %3
7100 %4 = OpLabel
7101 OpReturn
7102 OpFunctionEnd
7103 )";
7104
7105 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
7106 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
7107 EXPECT_THAT(getDiagnosticString(),
7108 HasSubstr(make_message(
7109 "Pointer type in OpTypeForwardPointer is not a pointer "
7110 "type.\n OpTypeForwardPointer %void CrossWorkgroup")));
7111 }
7112
7113 TEST_P(ValidateIdWithMessage, OpTypeForwardPointerWrongStorageClass) {
7114 std::string spirv = R"(
7115 OpCapability GenericPointer
7116 OpCapability VariablePointersStorageBuffer
7117 OpMemoryModel Logical GLSL450
7118 OpEntryPoint Fragment %1 "main"
7119 OpExecutionMode %1 OriginLowerLeft
7120 OpTypeForwardPointer %2 CrossWorkgroup
7121 %int = OpTypeInt 32 1
7122 %2 = OpTypePointer Function %int
7123 %void = OpTypeVoid
7124 %3 = OpTypeFunction %void
7125 %1 = OpFunction %void None %3
7126 %4 = OpLabel
7127 OpReturn
7128 OpFunctionEnd
7129 )";
7130
7131 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
7132 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
7133 EXPECT_THAT(getDiagnosticString(),
7134 HasSubstr(make_message(
7135 "Storage class in OpTypeForwardPointer does not match the "
7136 "pointer definition.\n OpTypeForwardPointer "
7137 "%_ptr_Function_int CrossWorkgroup")));
7138 }
7139
7140 TEST_P(ValidateIdWithMessage, MissingForwardPointer) {
7141 const std::string spirv = R"(
7142 OpCapability Linkage
7143 OpCapability Shader
7144 OpMemoryModel Logical Simple
7145 %float = OpTypeFloat 32
7146 %_struct_9 = OpTypeStruct %float %_ptr_Uniform__struct_9
7147 %_ptr_Uniform__struct_9 = OpTypePointer Uniform %_struct_9
7148 %1278 = OpVariable %_ptr_Uniform__struct_9 Uniform
7149 )";
7150
7151 CompileSuccessfully(spirv);
7152 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
7153 EXPECT_THAT(getDiagnosticString(),
7154 HasSubstr(make_message("Operand '3[%_ptr_Uniform__struct_2]' "
7155 "requires a previous definition")));
7156 }
7157
7158 TEST_P(ValidateIdWithMessage, NVBindlessSamplerInStruct) {
7159 std::string spirv = R"(
7160 OpCapability Shader
7161 OpCapability BindlessTextureNV
7162 OpExtension "SPV_NV_bindless_texture"
7163 OpMemoryModel Logical GLSL450
7164 OpSamplerImageAddressingModeNV 64
7165 OpEntryPoint Fragment %main "main"
7166 OpExecutionMode %main OriginUpperLeft
7167 %void = OpTypeVoid
7168 %3 = OpTypeFunction %void
7169 %float = OpTypeFloat 32
7170 %7 = OpTypeImage %float 2D 0 0 0 1 Unknown
7171 %8 = OpTypeSampledImage %7
7172 %9 = OpTypeImage %float 2D 0 0 0 2 Rgba32f
7173 %10 = OpTypeSampler
7174 %UBO = OpTypeStruct %8 %9 %10
7175 %_ptr_Uniform_UBO = OpTypePointer Uniform %UBO
7176 %_ = OpVariable %_ptr_Uniform_UBO Uniform
7177 %main = OpFunction %void None %3
7178 %5 = OpLabel
7179 OpReturn
7180 OpFunctionEnd
7181 )";
7182
7183 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
7184 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
7185 }
7186
7187 TEST_P(ValidateIdWithMessage,
7188 OpExtInstWithForwardRefsKHRDisallowedNoForwardRef) {
7189 std::string spirv = R"(
7190 OpCapability Shader
7191 OpExtension "SPV_KHR_non_semantic_info"
7192 OpExtension "SPV_KHR_relaxed_extended_instruction"
7193 %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
7194 OpMemoryModel Logical GLSL450
7195 OpEntryPoint GLCompute %main "main"
7196 OpExecutionMode %main LocalSize 1 1 1
7197 %void = OpTypeVoid
7198 %main_type = OpTypeFunction %void
7199 %4 = OpExtInstWithForwardRefsKHR %void %1 DebugInfoNone
7200 %main = OpFunction %void None %main_type
7201 %5 = OpLabel
7202 OpReturn
7203 OpFunctionEnd
7204 )";
7205
7206 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_6);
7207 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_6));
7208 EXPECT_THAT(
7209 getDiagnosticString(),
7210 HasSubstr(make_message("Opcode OpExtInstWithForwardRefsKHR must have at "
7211 "least one forward declared ID.")));
7212 }
7213
7214 TEST_P(ValidateIdWithMessage, OpExtInstNoForwardRef) {
7215 std::string spirv = R"(
7216 OpCapability Shader
7217 OpExtension "SPV_KHR_non_semantic_info"
7218 OpExtension "SPV_KHR_relaxed_extended_instruction"
7219 %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
7220 OpMemoryModel Logical GLSL450
7221 OpEntryPoint GLCompute %main "main"
7222 OpExecutionMode %main LocalSize 1 1 1
7223 %void = OpTypeVoid
7224 %main_type = OpTypeFunction %void
7225 %4 = OpExtInst %void %1 DebugInfoNone
7226 %main = OpFunction %void None %main_type
7227 %5 = OpLabel
7228 OpReturn
7229 OpFunctionEnd
7230 )";
7231
7232 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_6);
7233 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_6));
7234 }
7235
7236 TEST_P(ValidateIdWithMessage,
7237 OpExtInstWithForwardRefsKHRAllowedForwardReferenceInNonSemantic) {
7238 std::string spirv = R"(
7239 OpCapability Shader
7240 OpExtension "SPV_KHR_non_semantic_info"
7241 OpExtension "SPV_KHR_relaxed_extended_instruction"
7242 %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
7243 OpMemoryModel Logical GLSL450
7244 OpEntryPoint GLCompute %2 "main"
7245 OpExecutionMode %2 LocalSize 1 1 1
7246 %3 = OpString "sample"
7247 %void = OpTypeVoid
7248 %uint = OpTypeInt 32 0
7249 %uint_0 = OpConstant %uint 0
7250 %7 = OpTypeFunction %void
7251 %8 = OpExtInst %void %1 DebugSource %3 %3
7252 %9 = OpExtInst %void %1 DebugCompilationUnit %uint_0 %uint_0 %8 %uint_0
7253 %10 = OpExtInstWithForwardRefsKHR %void %1 DebugTypeFunction %uint_0 %11
7254 %12 = OpExtInstWithForwardRefsKHR %void %1 DebugFunction %3 %10 %8 %uint_0 %uint_0 %11 %3 %uint_0 %uint_0
7255 %11 = OpExtInst %void %1 DebugTypeComposite %3 %uint_0 %8 %uint_0 %uint_0 %9 %3 %uint_0 %uint_0 %12
7256 %2 = OpFunction %void None %7
7257 %13 = OpLabel
7258 OpReturn
7259 OpFunctionEnd
7260 )";
7261
7262 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_6);
7263 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_UNIVERSAL_1_6));
7264 }
7265
7266 TEST_P(ValidateIdWithMessage, OpExtInstNoForwardDeclAllowed) {
7267 std::string spirv = R"(
7268 OpCapability Shader
7269 %1 = OpExtInstImport "NonSemantic.Shader.DebugInfo.100"
7270 OpMemoryModel Logical GLSL450
7271 OpEntryPoint GLCompute %2 "main"
7272 OpExecutionMode %2 LocalSize 1 1 1
7273 %3 = OpString "sample"
7274 %void = OpTypeVoid
7275 %uint = OpTypeInt 32 0
7276 %uint_0 = OpConstant %uint 0
7277 %7 = OpTypeFunction %void
7278 %8 = OpExtInst %void %1 DebugSource %3 %3
7279 %9 = OpExtInst %void %1 DebugCompilationUnit %uint_0 %uint_0 %8 %uint_0
7280 %10 = OpExtInst %void %1 DebugTypeFunction %uint_0 %11
7281 %12 = OpExtInst %void %1 DebugFunction %3 %10 %8 %uint_0 %uint_0 %11 %3 %uint_0 %uint_0
7282 %11 = OpExtInst %void %1 DebugTypeComposite %3 %uint_0 %8 %uint_0 %uint_0 %9 %3 %uint_0 %uint_0 %12
7283 %2 = OpFunction %void None %7
7284 %13 = OpLabel
7285 OpReturn
7286 OpFunctionEnd
7287 )";
7288
7289 CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_6);
7290 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_6));
7291 EXPECT_THAT(getDiagnosticString(),
7292 HasSubstr(make_message("ID '11[%11]' has not been defined")));
7293 }
7294
7295 INSTANTIATE_TEST_SUITE_P(, ValidateIdWithMessage, ::testing::Bool());
7296
7297 } // namespace
7298 } // namespace val
7299 } // namespace spvtools