Debian Codegraph @

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

raw
1 // Copyright (c) 2015-2016 The Khronos Group Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // Validation tests for Control Flow Graph
16
17 #include <array>
18 #include <functional>
19 #include <iterator>
20 #include <sstream>
21 #include <string>
22 #include <utility>
23 #include <vector>
24
25 #include "gmock/gmock.h"
26 #include "source/diagnostic.h"
27 #include "source/spirv_target_env.h"
28 #include "source/val/validate.h"
29 #include "test/test_fixture.h"
30 #include "test/unit_spirv.h"
31 #include "test/val/val_fixtures.h"
32
33 namespace spvtools {
34 namespace val {
35 namespace {
36
37 using ::testing::HasSubstr;
38 using ::testing::MatchesRegex;
39
40 using ValidateCFG = spvtest::ValidateBase<SpvCapability>;
41 using spvtest::ScopedContext;
42
43 std::string nameOps() { return ""; }
44
45 template <typename... Args>
46 std::string nameOps(std::pair<std::string, std::string> head, Args... names) {
47 return "OpName %" + head.first + " \"" + head.second + "\"\n" +
48 nameOps(names...);
49 }
50
51 template <typename... Args>
52 std::string nameOps(std::string head, Args... names) {
53 return "OpName %" + head + " \"" + head + "\"\n" + nameOps(names...);
54 }
55
56 /// This class allows the easy creation of complex control flow without writing
57 /// SPIR-V. This class is used in the test cases below.
58 class Block {
59 std::string label_;
60 std::string body_;
61 SpvOp type_;
62 std::vector<Block> successors_;
63
64 public:
65 /// Creates a Block with a given label
66 ///
67 /// @param[in]: label the label id of the block
68 /// @param[in]: type the branch instruciton that ends the block
69 explicit Block(std::string label, SpvOp type = SpvOpBranch)
70 : label_(label), body_(), type_(type), successors_() {}
71
72 /// Sets the instructions which will appear in the body of the block
73 Block& SetBody(std::string body) {
74 body_ = body;
75 return *this;
76 }
77
78 Block& AppendBody(std::string body) {
79 body_ += body;
80 return *this;
81 }
82
83 /// Converts the block into a SPIR-V string
84 operator std::string() {
85 std::stringstream out;
86 out << std::setw(8) << "%" + label_ + " = OpLabel \n";
87 if (!body_.empty()) {
88 out << body_;
89 }
90
91 switch (type_) {
92 case SpvOpBranchConditional:
93 out << "OpBranchConditional %cond ";
94 for (Block& b : successors_) {
95 out << "%" + b.label_ + " ";
96 }
97 break;
98 case SpvOpSwitch: {
99 out << "OpSwitch %one %" + successors_.front().label_;
100 std::stringstream ss;
101 for (size_t i = 1; i < successors_.size(); i++) {
102 ss << " " << i << " %" << successors_[i].label_;
103 }
104 out << ss.str();
105 } break;
106 case SpvOpLoopMerge: {
107 assert(successors_.size() == 2);
108 out << "OpLoopMerge %" + successors_[0].label_ + " %" +
109 successors_[0].label_ + "None";
110 } break;
111
112 case SpvOpReturn:
113 assert(successors_.size() == 0);
114 out << "OpReturn\n";
115 break;
116 case SpvOpUnreachable:
117 assert(successors_.size() == 0);
118 out << "OpUnreachable\n";
119 break;
120 case SpvOpBranch:
121 assert(successors_.size() == 1);
122 out << "OpBranch %" + successors_.front().label_;
123 break;
124 case SpvOpKill:
125 assert(successors_.size() == 0);
126 out << "OpKill\n";
127 break;
128 default:
129 assert(1 == 0 && "Unhandled");
130 }
131 out << "\n";
132
133 return out.str();
134 }
135 friend Block& operator>>(Block& curr, std::vector<Block> successors);
136 friend Block& operator>>(Block& lhs, Block& successor);
137 };
138
139 /// Assigns the successors for the Block on the lhs
140 Block& operator>>(Block& lhs, std::vector<Block> successors) {
141 if (lhs.type_ == SpvOpBranchConditional) {
142 assert(successors.size() == 2);
143 } else if (lhs.type_ == SpvOpSwitch) {
144 assert(successors.size() > 1);
145 }
146 lhs.successors_ = successors;
147 return lhs;
148 }
149
150 /// Assigns the successor for the Block on the lhs
151 Block& operator>>(Block& lhs, Block& successor) {
152 assert(lhs.type_ == SpvOpBranch);
153 lhs.successors_.push_back(successor);
154 return lhs;
155 }
156
157 const std::string& GetDefaultHeader(SpvCapability cap) {
158 static const std::string shader_header =
159 "OpCapability Shader\n"
160 "OpCapability Linkage\n"
161 "OpMemoryModel Logical GLSL450\n";
162
163 static const std::string kernel_header =
164 "OpCapability Kernel\n"
165 "OpCapability Linkage\n"
166 "OpMemoryModel Logical OpenCL\n";
167
168 return (cap == SpvCapabilityShader) ? shader_header : kernel_header;
169 }
170
171 const std::string& GetWebGPUHeader() {
172 static const std::string header =
173 "OpCapability Shader\n"
174 "OpCapability VulkanMemoryModelKHR\n"
175 "OpExtension \"SPV_KHR_vulkan_memory_model\"\n"
176 "OpMemoryModel Logical VulkanKHR\n";
177 return header;
178 }
179
180 const std::string& types_consts() {
181 static const std::string types =
182 "%voidt = OpTypeVoid\n"
183 "%boolt = OpTypeBool\n"
184 "%intt = OpTypeInt 32 0\n"
185 "%one = OpConstant %intt 1\n"
186 "%two = OpConstant %intt 2\n"
187 "%ptrt = OpTypePointer Function %intt\n"
188 "%funct = OpTypeFunction %voidt\n";
189 return types;
190 }
191
192 INSTANTIATE_TEST_SUITE_P(StructuredControlFlow, ValidateCFG,
193 ::testing::Values(SpvCapabilityShader,
194 SpvCapabilityKernel));
195
196 TEST_P(ValidateCFG, LoopReachableFromEntryButNeverLeadingToReturn) {
197 // In this case, the loop is reachable from a node without a predecessor,
198 // but never reaches a node with a return.
199 //
200 // This motivates the need for the pseudo-exit node to have a node
201 // from a cycle in its predecessors list. Otherwise the validator's
202 // post-dominance calculation will go into an infinite loop.
203 //
204 // For more motivation, see
205 // https://github.com/KhronosGroup/SPIRV-Tools/issues/279
206 std::string str = R"(
207 OpCapability Shader
208 OpCapability Linkage
209 OpMemoryModel Logical GLSL450
210
211 OpName %entry "entry"
212 OpName %loop "loop"
213 OpName %exit "exit"
214
215 %voidt = OpTypeVoid
216 %funct = OpTypeFunction %voidt
217
218 %main = OpFunction %voidt None %funct
219 %entry = OpLabel
220 OpBranch %loop
221 %loop = OpLabel
222 OpLoopMerge %exit %loop None
223 OpBranch %loop
224 %exit = OpLabel
225 OpReturn
226 OpFunctionEnd
227 )";
228 CompileSuccessfully(str);
229 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions()) << str;
230 }
231
232 TEST_P(ValidateCFG, LoopUnreachableFromEntryButLeadingToReturn) {
233 // In this case, the loop is not reachable from a node without a
234 // predecessor, but eventually reaches a node with a return.
235 //
236 // This motivates the need for the pseudo-entry node to have a node
237 // from a cycle in its successors list. Otherwise the validator's
238 // dominance calculation will go into an infinite loop.
239 //
240 // For more motivation, see
241 // https://github.com/KhronosGroup/SPIRV-Tools/issues/279
242 // Before that fix, we'd have an infinite loop when calculating
243 // post-dominators.
244 std::string str = R"(
245 OpCapability Shader
246 OpCapability Linkage
247 OpMemoryModel Logical GLSL450
248
249 OpName %entry "entry"
250 OpName %loop "loop"
251 OpName %cont "cont"
252 OpName %exit "exit"
253
254 %voidt = OpTypeVoid
255 %funct = OpTypeFunction %voidt
256 %boolt = OpTypeBool
257 %false = OpConstantFalse %boolt
258
259 %main = OpFunction %voidt None %funct
260 %entry = OpLabel
261 OpReturn
262
263 %loop = OpLabel
264 OpLoopMerge %exit %cont None
265 OpBranch %cont
266
267 %cont = OpLabel
268 OpBranchConditional %false %loop %exit
269
270 %exit = OpLabel
271 OpReturn
272 OpFunctionEnd
273 )";
274 CompileSuccessfully(str);
275 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions())
276 << str << getDiagnosticString();
277 }
278
279 TEST_P(ValidateCFG, Simple) {
280 bool is_shader = GetParam() == SpvCapabilityShader;
281 Block entry("entry");
282 Block loop("loop", SpvOpBranchConditional);
283 Block cont("cont");
284 Block merge("merge", SpvOpReturn);
285
286 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
287 if (is_shader) {
288 loop.SetBody("OpLoopMerge %merge %cont None\n");
289 }
290
291 std::string str = GetDefaultHeader(GetParam()) +
292 nameOps("loop", "entry", "cont", "merge",
293 std::make_pair("func", "Main")) +
294 types_consts() +
295 "%func = OpFunction %voidt None %funct\n";
296
297 str += entry >> loop;
298 str += loop >> std::vector<Block>({cont, merge});
299 str += cont >> loop;
300 str += merge;
301 str += "OpFunctionEnd\n";
302
303 CompileSuccessfully(str);
304 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
305 }
306
307 TEST_P(ValidateCFG, Variable) {
308 Block entry("entry");
309 Block cont("cont");
310 Block exit("exit", SpvOpReturn);
311
312 entry.SetBody("%var = OpVariable %ptrt Function\n");
313
314 std::string str = GetDefaultHeader(GetParam()) +
315 nameOps(std::make_pair("func", "Main")) + types_consts() +
316 " %func = OpFunction %voidt None %funct\n";
317 str += entry >> cont;
318 str += cont >> exit;
319 str += exit;
320 str += "OpFunctionEnd\n";
321
322 CompileSuccessfully(str);
323 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
324 }
325
326 TEST_P(ValidateCFG, VariableNotInFirstBlockBad) {
327 Block entry("entry");
328 Block cont("cont");
329 Block exit("exit", SpvOpReturn);
330
331 // This operation should only be performed in the entry block
332 cont.SetBody("%var = OpVariable %ptrt Function\n");
333
334 std::string str = GetDefaultHeader(GetParam()) +
335 nameOps(std::make_pair("func", "Main")) + types_consts() +
336 " %func = OpFunction %voidt None %funct\n";
337
338 str += entry >> cont;
339 str += cont >> exit;
340 str += exit;
341 str += "OpFunctionEnd\n";
342
343 CompileSuccessfully(str);
344 ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
345 EXPECT_THAT(getDiagnosticString(),
346 HasSubstr("All OpVariable instructions in a function must be the "
347 "first instructions in the first block"));
348 }
349
350 TEST_P(ValidateCFG, BlockSelfLoopIsOk) {
351 bool is_shader = GetParam() == SpvCapabilityShader;
352 Block entry("entry");
353 Block loop("loop", SpvOpBranchConditional);
354 Block merge("merge", SpvOpReturn);
355
356 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
357 if (is_shader) loop.SetBody("OpLoopMerge %merge %loop None\n");
358
359 std::string str = GetDefaultHeader(GetParam()) +
360 nameOps("loop", "merge", std::make_pair("func", "Main")) +
361 types_consts() +
362 "%func = OpFunction %voidt None %funct\n";
363
364 str += entry >> loop;
365 // loop branches to itself, but does not trigger an error.
366 str += loop >> std::vector<Block>({merge, loop});
367 str += merge;
368 str += "OpFunctionEnd\n";
369
370 CompileSuccessfully(str);
371 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions()) << getDiagnosticString();
372 }
373
374 TEST_P(ValidateCFG, BlockAppearsBeforeDominatorBad) {
375 bool is_shader = GetParam() == SpvCapabilityShader;
376 Block entry("entry");
377 Block cont("cont");
378 Block branch("branch", SpvOpBranchConditional);
379 Block merge("merge", SpvOpReturn);
380
381 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
382 if (is_shader) branch.SetBody("OpSelectionMerge %merge None\n");
383
384 std::string str = GetDefaultHeader(GetParam()) +
385 nameOps("cont", "branch", std::make_pair("func", "Main")) +
386 types_consts() +
387 "%func = OpFunction %voidt None %funct\n";
388
389 str += entry >> branch;
390 str += cont >> merge; // cont appears before its dominator
391 str += branch >> std::vector<Block>({cont, merge});
392 str += merge;
393 str += "OpFunctionEnd\n";
394
395 CompileSuccessfully(str);
396 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
397 EXPECT_THAT(getDiagnosticString(),
398 MatchesRegex("Block .\\[%cont\\] appears in the binary "
399 "before its dominator .\\[%branch\\]\n"
400 " %branch = OpLabel\n"));
401 }
402
403 TEST_P(ValidateCFG, MergeBlockTargetedByMultipleHeaderBlocksBad) {
404 bool is_shader = GetParam() == SpvCapabilityShader;
405 Block entry("entry");
406 Block loop("loop");
407 Block selection("selection", SpvOpBranchConditional);
408 Block merge("merge", SpvOpReturn);
409
410 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
411 if (is_shader) loop.SetBody(" OpLoopMerge %merge %loop None\n");
412
413 // cannot share the same merge
414 if (is_shader) selection.SetBody("OpSelectionMerge %merge None\n");
415
416 std::string str = GetDefaultHeader(GetParam()) +
417 nameOps("merge", std::make_pair("func", "Main")) +
418 types_consts() +
419 "%func = OpFunction %voidt None %funct\n";
420
421 str += entry >> loop;
422 str += loop >> selection;
423 str += selection >> std::vector<Block>({loop, merge});
424 str += merge;
425 str += "OpFunctionEnd\n";
426
427 CompileSuccessfully(str);
428 if (is_shader) {
429 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
430 EXPECT_THAT(getDiagnosticString(),
431 MatchesRegex("Block .\\[%merge\\] is already a merge block "
432 "for another header\n"
433 " %Main = OpFunction %void None %9\n"));
434 } else {
435 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
436 }
437 }
438
439 TEST_P(ValidateCFG, MergeBlockTargetedByMultipleHeaderBlocksSelectionBad) {
440 bool is_shader = GetParam() == SpvCapabilityShader;
441 Block entry("entry");
442 Block loop("loop", SpvOpBranchConditional);
443 Block selection("selection", SpvOpBranchConditional);
444 Block merge("merge", SpvOpReturn);
445
446 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
447 if (is_shader) selection.SetBody(" OpSelectionMerge %merge None\n");
448
449 // cannot share the same merge
450 if (is_shader) loop.SetBody(" OpLoopMerge %merge %loop None\n");
451
452 std::string str = GetDefaultHeader(GetParam()) +
453 nameOps("merge", std::make_pair("func", "Main")) +
454 types_consts() +
455 "%func = OpFunction %voidt None %funct\n";
456
457 str += entry >> selection;
458 str += selection >> std::vector<Block>({merge, loop});
459 str += loop >> std::vector<Block>({loop, merge});
460 str += merge;
461 str += "OpFunctionEnd\n";
462
463 CompileSuccessfully(str);
464 if (is_shader) {
465 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
466 EXPECT_THAT(getDiagnosticString(),
467 MatchesRegex("Block .\\[%merge\\] is already a merge block "
468 "for another header\n"
469 " %Main = OpFunction %void None %9\n"));
470 } else {
471 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
472 }
473 }
474
475 TEST_P(ValidateCFG, BranchTargetFirstBlockBadSinceEntryBlock) {
476 Block entry("entry");
477 Block bad("bad");
478 Block end("end", SpvOpReturn);
479 std::string str = GetDefaultHeader(GetParam()) +
480 nameOps("entry", "bad", std::make_pair("func", "Main")) +
481 types_consts() +
482 "%func = OpFunction %voidt None %funct\n";
483
484 str += entry >> bad;
485 str += bad >> entry; // Cannot target entry block
486 str += end;
487 str += "OpFunctionEnd\n";
488
489 CompileSuccessfully(str);
490 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
491 EXPECT_THAT(getDiagnosticString(),
492 MatchesRegex("First block .\\[%entry\\] of function "
493 ".\\[%Main\\] is targeted by block .\\[%bad\\]\n"
494 " %Main = OpFunction %void None %10\n"));
495 }
496
497 TEST_P(ValidateCFG, BranchTargetFirstBlockBadSinceValue) {
498 Block entry("entry");
499 entry.SetBody("%undef = OpUndef %voidt\n");
500 Block bad("bad");
501 Block end("end", SpvOpReturn);
502 Block badvalue("undef"); // This referenes the OpUndef.
503 std::string str = GetDefaultHeader(GetParam()) +
504 nameOps("entry", "bad", std::make_pair("func", "Main")) +
505 types_consts() +
506 "%func = OpFunction %voidt None %funct\n";
507
508 str += entry >> bad;
509 str +=
510 bad >> badvalue; // Check branch to a function value (it's not a block!)
511 str += end;
512 str += "OpFunctionEnd\n";
513
514 CompileSuccessfully(str);
515 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
516 EXPECT_THAT(getDiagnosticString(),
517 HasSubstr("'Target Label' operands for OpBranch must "
518 "be the ID of an OpLabel instruction"));
519 }
520
521 TEST_P(ValidateCFG, BranchConditionalTrueTargetFirstBlockBad) {
522 Block entry("entry");
523 Block bad("bad", SpvOpBranchConditional);
524 Block exit("exit", SpvOpReturn);
525
526 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
527 bad.SetBody(" OpLoopMerge %entry %exit None\n");
528
529 std::string str = GetDefaultHeader(GetParam()) +
530 nameOps("entry", "bad", std::make_pair("func", "Main")) +
531 types_consts() +
532 "%func = OpFunction %voidt None %funct\n";
533
534 str += entry >> bad;
535 str += bad >> std::vector<Block>({entry, exit}); // cannot target entry block
536 str += exit;
537 str += "OpFunctionEnd\n";
538
539 CompileSuccessfully(str);
540 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
541 EXPECT_THAT(getDiagnosticString(),
542 MatchesRegex("First block .\\[%entry\\] of function .\\[%Main\\] "
543 "is targeted by block .\\[%bad\\]\n"
544 " %Main = OpFunction %void None %10\n"));
545 }
546
547 TEST_P(ValidateCFG, BranchConditionalFalseTargetFirstBlockBad) {
548 Block entry("entry");
549 Block bad("bad", SpvOpBranchConditional);
550 Block t("t");
551 Block merge("merge");
552 Block end("end", SpvOpReturn);
553
554 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
555 bad.SetBody("OpLoopMerge %merge %cont None\n");
556
557 std::string str = GetDefaultHeader(GetParam()) +
558 nameOps("entry", "bad", std::make_pair("func", "Main")) +
559 types_consts() +
560 "%func = OpFunction %voidt None %funct\n";
561
562 str += entry >> bad;
563 str += bad >> std::vector<Block>({t, entry});
564 str += merge >> end;
565 str += end;
566 str += "OpFunctionEnd\n";
567
568 CompileSuccessfully(str);
569 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
570 EXPECT_THAT(getDiagnosticString(),
571 MatchesRegex("First block .\\[%entry\\] of function .\\[%Main\\] "
572 "is targeted by block .\\[%bad\\]\n"
573 " %Main = OpFunction %void None %10\n"));
574 }
575
576 TEST_P(ValidateCFG, SwitchTargetFirstBlockBad) {
577 Block entry("entry");
578 Block bad("bad", SpvOpSwitch);
579 Block block1("block1");
580 Block block2("block2");
581 Block block3("block3");
582 Block def("def"); // default block
583 Block merge("merge");
584 Block end("end", SpvOpReturn);
585
586 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
587 bad.SetBody("OpSelectionMerge %merge None\n");
588
589 std::string str = GetDefaultHeader(GetParam()) +
590 nameOps("entry", "bad", std::make_pair("func", "Main")) +
591 types_consts() +
592 "%func = OpFunction %voidt None %funct\n";
593
594 str += entry >> bad;
595 str += bad >> std::vector<Block>({def, block1, block2, block3, entry});
596 str += def >> merge;
597 str += block1 >> merge;
598 str += block2 >> merge;
599 str += block3 >> merge;
600 str += merge >> end;
601 str += end;
602 str += "OpFunctionEnd\n";
603
604 CompileSuccessfully(str);
605 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
606 EXPECT_THAT(getDiagnosticString(),
607 MatchesRegex("First block .\\[%entry\\] of function .\\[%Main\\] "
608 "is targeted by block .\\[%bad\\]\n"
609 " %Main = OpFunction %void None %10\n"));
610 }
611
612 TEST_P(ValidateCFG, BranchToBlockInOtherFunctionBad) {
613 Block entry("entry");
614 Block middle("middle", SpvOpBranchConditional);
615 Block end("end", SpvOpReturn);
616
617 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
618 middle.SetBody("OpSelectionMerge %end None\n");
619
620 Block entry2("entry2");
621 Block middle2("middle2");
622 Block end2("end2", SpvOpReturn);
623
624 std::string str = GetDefaultHeader(GetParam()) +
625 nameOps("middle2", std::make_pair("func", "Main")) +
626 types_consts() +
627 "%func = OpFunction %voidt None %funct\n";
628
629 str += entry >> middle;
630 str += middle >> std::vector<Block>({end, middle2});
631 str += end;
632 str += "OpFunctionEnd\n";
633
634 str += "%func2 = OpFunction %voidt None %funct\n";
635 str += entry2 >> middle2;
636 str += middle2 >> end2;
637 str += end2;
638 str += "OpFunctionEnd\n";
639
640 CompileSuccessfully(str);
641 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
642 EXPECT_THAT(
643 getDiagnosticString(),
644 MatchesRegex("Block\\(s\\) \\{.\\[%middle2\\]\\} are referenced but not "
645 "defined in function .\\[%Main\\]\n"
646 " %Main = OpFunction %void None %9\n"));
647 }
648
649 TEST_P(ValidateCFG, HeaderDoesntDominatesMergeBad) {
650 bool is_shader = GetParam() == SpvCapabilityShader;
651 Block entry("entry");
652 Block head("head", SpvOpBranchConditional);
653 Block f("f");
654 Block merge("merge", SpvOpReturn);
655
656 head.SetBody("%cond = OpSLessThan %boolt %one %two\n");
657
658 if (is_shader) head.AppendBody("OpSelectionMerge %merge None\n");
659
660 std::string str = GetDefaultHeader(GetParam()) +
661 nameOps("head", "merge", std::make_pair("func", "Main")) +
662 types_consts() +
663 "%func = OpFunction %voidt None %funct\n";
664
665 str += entry >> merge;
666 str += head >> std::vector<Block>({merge, f});
667 str += f >> merge;
668 str += merge;
669 str += "OpFunctionEnd\n";
670
671 CompileSuccessfully(str);
672 if (is_shader) {
673 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
674 EXPECT_THAT(
675 getDiagnosticString(),
676 MatchesRegex("The selection construct with the selection header "
677 ".\\[%head\\] does not dominate the merge block "
678 ".\\[%merge\\]\n %merge = OpLabel\n"));
679 } else {
680 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
681 }
682 }
683
684 TEST_P(ValidateCFG, HeaderDoesntStrictlyDominateMergeBad) {
685 // If a merge block is reachable, then it must be strictly dominated by
686 // its header block.
687 bool is_shader = GetParam() == SpvCapabilityShader;
688 Block head("head", SpvOpBranchConditional);
689 Block exit("exit", SpvOpReturn);
690
691 head.SetBody("%cond = OpSLessThan %boolt %one %two\n");
692
693 if (is_shader) head.AppendBody("OpSelectionMerge %head None\n");
694
695 std::string str = GetDefaultHeader(GetParam()) +
696 nameOps("head", "exit", std::make_pair("func", "Main")) +
697 types_consts() +
698 "%func = OpFunction %voidt None %funct\n";
699
700 str += head >> std::vector<Block>({exit, exit});
701 str += exit;
702 str += "OpFunctionEnd\n";
703
704 CompileSuccessfully(str);
705 if (is_shader) {
706 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
707 EXPECT_THAT(
708 getDiagnosticString(),
709 MatchesRegex("The selection construct with the selection header "
710 ".\\[%head\\] does not strictly dominate the merge block "
711 ".\\[%head\\]\n %head = OpLabel\n"));
712 } else {
713 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions()) << str;
714 }
715 }
716
717 std::string GetUnreachableMergeNoMergeInst(SpvCapability cap,
718 spv_target_env env) {
719 std::string header =
720 spvIsWebGPUEnv(env) ? GetWebGPUHeader() : GetDefaultHeader(cap);
721 Block entry("entry");
722 Block branch("branch", SpvOpBranchConditional);
723 Block t("t", SpvOpReturn);
724 Block f("f", SpvOpReturn);
725 Block merge("merge", SpvOpReturn);
726
727 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
728 if (!spvIsWebGPUEnv(env) && cap == SpvCapabilityShader)
729 branch.AppendBody("OpSelectionMerge %merge None\n");
730
731 std::string str = header;
732 if (spvIsWebGPUEnv(env)) {
733 str +=
734 "OpEntryPoint Fragment %func \"func\"\n"
735 "OpExecutionMode %func OriginUpperLeft\n";
736 }
737 if (!spvIsWebGPUEnv(env))
738 str += nameOps("branch", "merge", std::make_pair("func", "Main"));
739 str += types_consts() + "%func = OpFunction %voidt None %funct\n";
740 str += entry >> branch;
741 str += branch >> std::vector<Block>({t, f});
742 str += t;
743 str += f;
744 str += merge;
745 str += "OpFunctionEnd\n";
746
747 return str;
748 }
749
750 TEST_P(ValidateCFG, UnreachableMergeNoMergeInst) {
751 CompileSuccessfully(
752 GetUnreachableMergeNoMergeInst(GetParam(), SPV_ENV_UNIVERSAL_1_0));
753 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
754 }
755
756 TEST_F(ValidateCFG, WebGPUUnreachableMergeNoMergeInst) {
757 CompileSuccessfully(
758 GetUnreachableMergeNoMergeInst(SpvCapabilityShader, SPV_ENV_WEBGPU_0));
759 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions(SPV_ENV_WEBGPU_0));
760 EXPECT_THAT(getDiagnosticString(),
761 HasSubstr("For WebGPU, all blocks must be reachable"));
762 }
763
764 std::string GetUnreachableMergeTerminatedBy(SpvCapability cap,
765 spv_target_env env, SpvOp op) {
766 std::string header =
767 spvIsWebGPUEnv(env) ? GetWebGPUHeader() : GetDefaultHeader(cap);
768
769 Block entry("entry");
770 Block branch("branch", SpvOpBranchConditional);
771 Block t("t", SpvOpReturn);
772 Block f("f", SpvOpReturn);
773 Block merge("merge", op);
774
775 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
776 std::string str = header;
777 if (spvIsWebGPUEnv(env)) {
778 str +=
779 "OpEntryPoint Fragment %func \"func\"\n"
780 "OpExecutionMode %func OriginUpperLeft\n";
781 }
782 if (cap == SpvCapabilityShader)
783 branch.AppendBody("OpSelectionMerge %merge None\n");
784 if (!spvIsWebGPUEnv(env))
785 str += nameOps("branch", "merge", std::make_pair("func", "Main"));
786
787 str += types_consts();
788 str += "%func = OpFunction %voidt None %funct\n";
789 str += entry >> branch;
790 str += branch >> std::vector<Block>({t, f});
791 str += t;
792 str += f;
793 str += merge;
794 str += "OpFunctionEnd\n";
795
796 return str;
797 }
798
799 TEST_P(ValidateCFG, UnreachableMergeTerminatedByOpUnreachable) {
800 CompileSuccessfully(GetUnreachableMergeTerminatedBy(
801 GetParam(), SPV_ENV_UNIVERSAL_1_0, SpvOpUnreachable));
802 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
803 }
804
805 TEST_F(ValidateCFG, UnreachableMergeTerminatedByOpKill) {
806 CompileSuccessfully(GetUnreachableMergeTerminatedBy(
807 SpvCapabilityShader, SPV_ENV_UNIVERSAL_1_0, SpvOpKill));
808 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
809 }
810
811 TEST_P(ValidateCFG, UnreachableMergeTerminatedByOpReturn) {
812 CompileSuccessfully(GetUnreachableMergeTerminatedBy(
813 GetParam(), SPV_ENV_UNIVERSAL_1_0, SpvOpReturn));
814 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
815 }
816
817 TEST_F(ValidateCFG, WebGPUUnreachableMergeTerminatedByOpUnreachable) {
818 CompileSuccessfully(GetUnreachableMergeTerminatedBy(
819 SpvCapabilityShader, SPV_ENV_WEBGPU_0, SpvOpUnreachable));
820 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
821 }
822
823 TEST_F(ValidateCFG, WebGPUUnreachableMergeTerminatedByOpKill) {
824 CompileSuccessfully(GetUnreachableMergeTerminatedBy(
825 SpvCapabilityShader, SPV_ENV_WEBGPU_0, SpvOpKill));
826 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions(SPV_ENV_WEBGPU_0));
827 EXPECT_THAT(getDiagnosticString(),
828 HasSubstr("must terminate with OpUnreachable"));
829 }
830
831 TEST_P(ValidateCFG, WebGPUUnreachableMergeTerminatedByOpReturn) {
832 CompileSuccessfully(GetUnreachableMergeTerminatedBy(
833 SpvCapabilityShader, SPV_ENV_WEBGPU_0, SpvOpReturn));
834 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions(SPV_ENV_WEBGPU_0));
835 EXPECT_THAT(getDiagnosticString(),
836 HasSubstr("must terminate with OpUnreachable"));
837 }
838
839 std::string GetUnreachableContinueTerminatedBy(SpvCapability cap,
840 spv_target_env env, SpvOp op) {
841 std::string header =
842 spvIsWebGPUEnv(env) ? GetWebGPUHeader() : GetDefaultHeader(cap);
843
844 Block entry("entry");
845 Block branch("branch", SpvOpBranch);
846 Block merge("merge", SpvOpReturn);
847 Block target("target", op);
848
849 if (op == SpvOpBranch) target >> branch;
850
851 std::string str = header;
852 if (spvIsWebGPUEnv(env)) {
853 str +=
854 "OpEntryPoint Fragment %func \"func\"\n"
855 "OpExecutionMode %func OriginUpperLeft\n";
856 }
857 if (cap == SpvCapabilityShader)
858 branch.AppendBody("OpLoopMerge %merge %target None\n");
859 if (!spvIsWebGPUEnv(env))
860 str += nameOps("branch", "merge", "target", std::make_pair("func", "Main"));
861
862 str += types_consts();
863 str += "%func = OpFunction %voidt None %funct\n";
864 str += entry >> branch;
865 str += branch >> std::vector<Block>({merge});
866 str += merge;
867 str += target;
868 str += "OpFunctionEnd\n";
869
870 return str;
871 }
872
873 TEST_P(ValidateCFG, UnreachableContinueTerminatedBySpvOpUnreachable) {
874 CompileSuccessfully(GetUnreachableContinueTerminatedBy(
875 GetParam(), SPV_ENV_UNIVERSAL_1_0, SpvOpUnreachable));
876 if (GetParam() == SpvCapabilityShader) {
877 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
878 EXPECT_THAT(getDiagnosticString(),
879 HasSubstr("targeted by 0 back-edge blocks"));
880 } else {
881 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
882 }
883 }
884
885 TEST_F(ValidateCFG, UnreachableContinueTerminatedBySpvOpKill) {
886 CompileSuccessfully(GetUnreachableContinueTerminatedBy(
887 SpvCapabilityShader, SPV_ENV_UNIVERSAL_1_0, SpvOpKill));
888 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
889 EXPECT_THAT(getDiagnosticString(),
890 HasSubstr("targeted by 0 back-edge blocks"));
891 }
892
893 TEST_P(ValidateCFG, UnreachableContinueTerminatedBySpvOpReturn) {
894 CompileSuccessfully(GetUnreachableContinueTerminatedBy(
895 GetParam(), SPV_ENV_UNIVERSAL_1_0, SpvOpReturn));
896 if (GetParam() == SpvCapabilityShader) {
897 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
898 EXPECT_THAT(getDiagnosticString(),
899 HasSubstr("targeted by 0 back-edge blocks"));
900 } else {
901 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
902 }
903 }
904
905 TEST_P(ValidateCFG, UnreachableContinueTerminatedBySpvOpBranch) {
906 CompileSuccessfully(GetUnreachableContinueTerminatedBy(
907 GetParam(), SPV_ENV_UNIVERSAL_1_0, SpvOpBranch));
908 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
909 }
910
911 TEST_F(ValidateCFG, WebGPUUnreachableContinueTerminatedBySpvOpUnreachable) {
912 CompileSuccessfully(GetUnreachableContinueTerminatedBy(
913 SpvCapabilityShader, SPV_ENV_WEBGPU_0, SpvOpUnreachable));
914 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions(SPV_ENV_WEBGPU_0));
915 EXPECT_THAT(getDiagnosticString(),
916 HasSubstr("For WebGPU, unreachable continue-target must "
917 "terminate with OpBranch.\n %12 = OpLabel\n"));
918 }
919
920 TEST_F(ValidateCFG, WebGPUUnreachableContinueTerminatedBySpvOpKill) {
921 CompileSuccessfully(GetUnreachableContinueTerminatedBy(
922 SpvCapabilityShader, SPV_ENV_WEBGPU_0, SpvOpKill));
923 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions(SPV_ENV_WEBGPU_0));
924 EXPECT_THAT(getDiagnosticString(),
925 HasSubstr("For WebGPU, unreachable continue-target must "
926 "terminate with OpBranch.\n %12 = OpLabel\n"));
927 }
928
929 TEST_F(ValidateCFG, WebGPUUnreachableContinueTerminatedBySpvOpReturn) {
930 CompileSuccessfully(GetUnreachableContinueTerminatedBy(
931 SpvCapabilityShader, SPV_ENV_WEBGPU_0, SpvOpReturn));
932 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions(SPV_ENV_WEBGPU_0));
933 EXPECT_THAT(getDiagnosticString(),
934 HasSubstr("For WebGPU, unreachable continue-target must "
935 "terminate with OpBranch.\n %12 = OpLabel\n"));
936 }
937
938 TEST_F(ValidateCFG, WebGPUUnreachableContinueTerminatedBySpvOpBranch) {
939 CompileSuccessfully(GetUnreachableContinueTerminatedBy(
940 SpvCapabilityShader, SPV_ENV_WEBGPU_0, SpvOpBranch));
941 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
942 }
943
944 std::string GetUnreachableMergeUnreachableMergeInst(SpvCapability cap,
945 spv_target_env env) {
946 std::string header =
947 spvIsWebGPUEnv(env) ? GetWebGPUHeader() : GetDefaultHeader(cap);
948
949 Block body("body", SpvOpReturn);
950 Block entry("entry");
951 Block branch("branch", SpvOpBranchConditional);
952 Block t("t", SpvOpReturn);
953 Block f("f", SpvOpReturn);
954 Block merge("merge", SpvOpUnreachable);
955
956 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
957 std::string str = header;
958 if (spvIsWebGPUEnv(env)) {
959 str +=
960 "OpEntryPoint Fragment %func \"func\"\n"
961 "OpExecutionMode %func OriginUpperLeft\n";
962 }
963 if (cap == SpvCapabilityShader)
964 branch.AppendBody("OpSelectionMerge %merge None\n");
965 if (!spvIsWebGPUEnv(env))
966 str += nameOps("branch", "merge", std::make_pair("func", "Main"));
967
968 str += types_consts();
969 str += "%func = OpFunction %voidt None %funct\n";
970 str += body;
971 str += merge;
972 str += entry >> branch;
973 str += branch >> std::vector<Block>({t, f});
974 str += t;
975 str += f;
976 str += "OpFunctionEnd\n";
977
978 return str;
979 }
980
981 TEST_P(ValidateCFG, UnreachableMergeUnreachableMergeInst) {
982 CompileSuccessfully(GetUnreachableMergeUnreachableMergeInst(
983 GetParam(), SPV_ENV_UNIVERSAL_1_0));
984 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
985 }
986
987 TEST_F(ValidateCFG, WebGPUUnreachableMergeUnreachableMergeInst) {
988 CompileSuccessfully(GetUnreachableMergeUnreachableMergeInst(
989 SpvCapabilityShader, SPV_ENV_WEBGPU_0));
990 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions(SPV_ENV_WEBGPU_0));
991 EXPECT_THAT(getDiagnosticString(),
992 HasSubstr("must be referenced by a reachable merge instruction"));
993 }
994
995 std::string GetUnreachableContinueUnreachableLoopInst(SpvCapability cap,
996 spv_target_env env) {
997 std::string header =
998 spvIsWebGPUEnv(env) ? GetWebGPUHeader() : GetDefaultHeader(cap);
999
1000 Block body("body", SpvOpReturn);
1001 Block entry("entry");
1002 Block branch("branch", SpvOpBranch);
1003 Block merge("merge", SpvOpReturn);
1004 Block target("target", SpvOpBranch);
1005
1006 target >> branch;
1007
1008 std::string str = header;
1009 if (spvIsWebGPUEnv(env)) {
1010 str +=
1011 "OpEntryPoint Fragment %func \"func\"\n"
1012 "OpExecutionMode %func OriginUpperLeft\n";
1013 }
1014 if (cap == SpvCapabilityShader)
1015 branch.AppendBody("OpLoopMerge %merge %target None\n");
1016 if (!spvIsWebGPUEnv(env))
1017 str += nameOps("branch", "merge", "target", std::make_pair("func", "Main"));
1018
1019 str += types_consts();
1020 str += "%func = OpFunction %voidt None %funct\n";
1021 str += body;
1022 str += target;
1023 str += merge;
1024 str += entry >> branch;
1025 str += branch >> std::vector<Block>({merge});
1026 str += "OpFunctionEnd\n";
1027
1028 return str;
1029 }
1030
1031 TEST_P(ValidateCFG, UnreachableContinueUnreachableLoopInst) {
1032 CompileSuccessfully(GetUnreachableContinueUnreachableLoopInst(
1033 GetParam(), SPV_ENV_UNIVERSAL_1_0));
1034 if (GetParam() == SpvCapabilityShader) {
1035 // Shader causes additional structured CFG checks that cause a failure.
1036 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
1037 EXPECT_THAT(getDiagnosticString(),
1038 HasSubstr("Back-edges (1[%branch] -> 3[%target]) can only be "
1039 "formed between a block and a loop header."));
1040
1041 } else {
1042 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1043 }
1044 }
1045
1046 TEST_F(ValidateCFG, WebGPUUnreachableContinueUnreachableLoopInst) {
1047 CompileSuccessfully(GetUnreachableContinueUnreachableLoopInst(
1048 SpvCapabilityShader, SPV_ENV_WEBGPU_0));
1049 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions(SPV_ENV_WEBGPU_0));
1050 EXPECT_THAT(getDiagnosticString(),
1051 HasSubstr("must be referenced by a reachable loop instruction"));
1052 }
1053
1054 std::string GetUnreachableMergeWithComplexBody(SpvCapability cap,
1055 spv_target_env env) {
1056 std::string header =
1057 spvIsWebGPUEnv(env) ? GetWebGPUHeader() : GetDefaultHeader(cap);
1058
1059 Block entry("entry");
1060 Block branch("branch", SpvOpBranchConditional);
1061 Block t("t", SpvOpReturn);
1062 Block f("f", SpvOpReturn);
1063 Block merge("merge", SpvOpUnreachable);
1064
1065 entry.AppendBody(spvIsWebGPUEnv(env)
1066 ? "%placeholder = OpVariable %intptrt Function %two\n"
1067 : "%placeholder = OpVariable %intptrt Function\n");
1068 entry.AppendBody("%cond = OpSLessThan %boolt %one %two\n");
1069 merge.AppendBody("OpStore %placeholder %one\n");
1070
1071 std::string str = header;
1072 if (spvIsWebGPUEnv(env)) {
1073 str +=
1074 "OpEntryPoint Fragment %func \"func\"\n"
1075 "OpExecutionMode %func OriginUpperLeft\n";
1076 }
1077 if (cap == SpvCapabilityShader)
1078 branch.AppendBody("OpSelectionMerge %merge None\n");
1079 if (!spvIsWebGPUEnv(env))
1080 str += nameOps("branch", "merge", std::make_pair("func", "Main"));
1081
1082 str += types_consts();
1083 str += "%intptrt = OpTypePointer Function %intt\n";
1084 str += "%func = OpFunction %voidt None %funct\n";
1085 str += entry >> branch;
1086 str += branch >> std::vector<Block>({t, f});
1087 str += t;
1088 str += f;
1089 str += merge;
1090 str += "OpFunctionEnd\n";
1091
1092 return str;
1093 }
1094
1095 TEST_P(ValidateCFG, UnreachableMergeWithComplexBody) {
1096 CompileSuccessfully(
1097 GetUnreachableMergeWithComplexBody(GetParam(), SPV_ENV_UNIVERSAL_1_0));
1098 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1099 }
1100
1101 TEST_F(ValidateCFG, WebGPUUnreachableMergeWithComplexBody) {
1102 CompileSuccessfully(GetUnreachableMergeWithComplexBody(SpvCapabilityShader,
1103 SPV_ENV_WEBGPU_0));
1104 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions(SPV_ENV_WEBGPU_0));
1105 EXPECT_THAT(
1106 getDiagnosticString(),
1107 HasSubstr("must only contain an OpLabel and OpUnreachable instruction"));
1108 }
1109
1110 std::string GetUnreachableContinueWithComplexBody(SpvCapability cap,
1111 spv_target_env env) {
1112 std::string header =
1113 spvIsWebGPUEnv(env) ? GetWebGPUHeader() : GetDefaultHeader(cap);
1114
1115 Block entry("entry");
1116 Block branch("branch", SpvOpBranch);
1117 Block merge("merge", SpvOpReturn);
1118 Block target("target", SpvOpBranch);
1119
1120 target >> branch;
1121
1122 entry.AppendBody(spvIsWebGPUEnv(env)
1123 ? "%placeholder = OpVariable %intptrt Function %two\n"
1124 : "%placeholder = OpVariable %intptrt Function\n");
1125 target.AppendBody("OpStore %placeholder %one\n");
1126
1127 std::string str = header;
1128 if (spvIsWebGPUEnv(env)) {
1129 str +=
1130 "OpEntryPoint Fragment %func \"func\"\n"
1131 "OpExecutionMode %func OriginUpperLeft\n";
1132 }
1133 if (cap == SpvCapabilityShader)
1134 branch.AppendBody("OpLoopMerge %merge %target None\n");
1135 if (!spvIsWebGPUEnv(env))
1136 str += nameOps("branch", "merge", "target", std::make_pair("func", "Main"));
1137
1138 str += types_consts();
1139 str += "%intptrt = OpTypePointer Function %intt\n";
1140 str += "%func = OpFunction %voidt None %funct\n";
1141 str += entry >> branch;
1142 str += branch >> std::vector<Block>({merge});
1143 str += merge;
1144 str += target;
1145 str += "OpFunctionEnd\n";
1146
1147 return str;
1148 }
1149
1150 TEST_P(ValidateCFG, UnreachableContinueWithComplexBody) {
1151 CompileSuccessfully(
1152 GetUnreachableContinueWithComplexBody(GetParam(), SPV_ENV_UNIVERSAL_1_0));
1153 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1154 }
1155
1156 TEST_F(ValidateCFG, WebGPUUnreachableContinueWithComplexBody) {
1157 CompileSuccessfully(GetUnreachableContinueWithComplexBody(SpvCapabilityShader,
1158 SPV_ENV_WEBGPU_0));
1159 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions(SPV_ENV_WEBGPU_0));
1160 EXPECT_THAT(
1161 getDiagnosticString(),
1162 HasSubstr("must only contain an OpLabel and an OpBranch instruction"));
1163 }
1164
1165 std::string GetUnreachableMergeWithBranchUse(SpvCapability cap,
1166 spv_target_env env) {
1167 std::string header =
1168 spvIsWebGPUEnv(env) ? GetWebGPUHeader() : GetDefaultHeader(cap);
1169
1170 Block entry("entry");
1171 Block branch("branch", SpvOpBranchConditional);
1172 Block t("t", SpvOpBranch);
1173 Block f("f", SpvOpReturn);
1174 Block merge("merge", SpvOpUnreachable);
1175
1176 entry.AppendBody("%cond = OpSLessThan %boolt %one %two\n");
1177
1178 std::string str = header;
1179 if (spvIsWebGPUEnv(env)) {
1180 str +=
1181 "OpEntryPoint Fragment %func \"func\"\n"
1182 "OpExecutionMode %func OriginUpperLeft\n";
1183 }
1184 if (cap == SpvCapabilityShader)
1185 branch.AppendBody("OpSelectionMerge %merge None\n");
1186 if (!spvIsWebGPUEnv(env))
1187 str += nameOps("branch", "merge", std::make_pair("func", "Main"));
1188
1189 str += types_consts();
1190 str += "%func = OpFunction %voidt None %funct\n";
1191 str += entry >> branch;
1192 str += branch >> std::vector<Block>({t, f});
1193 str += t >> merge;
1194 str += f;
1195 str += merge;
1196 str += "OpFunctionEnd\n";
1197
1198 return str;
1199 }
1200
1201 TEST_P(ValidateCFG, UnreachableMergeWithBranchUse) {
1202 CompileSuccessfully(
1203 GetUnreachableMergeWithBranchUse(GetParam(), SPV_ENV_UNIVERSAL_1_0));
1204 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1205 }
1206
1207 std::string GetUnreachableMergeWithMultipleUses(SpvCapability cap,
1208 spv_target_env env) {
1209 std::string header =
1210 spvIsWebGPUEnv(env) ? GetWebGPUHeader() : GetDefaultHeader(cap);
1211
1212 Block entry("entry");
1213 Block branch("branch", SpvOpBranchConditional);
1214 Block t("t", SpvOpReturn);
1215 Block f("f", SpvOpReturn);
1216 Block merge("merge", SpvOpUnreachable);
1217 Block duplicate("duplicate", SpvOpBranchConditional);
1218
1219 entry.AppendBody("%cond = OpSLessThan %boolt %one %two\n");
1220
1221 std::string str = header;
1222 if (spvIsWebGPUEnv(env)) {
1223 str +=
1224 "OpEntryPoint Fragment %func \"func\"\n"
1225 "OpExecutionMode %func OriginUpperLeft\n";
1226 }
1227 if (cap == SpvCapabilityShader) {
1228 branch.AppendBody("OpSelectionMerge %merge None\n");
1229 duplicate.AppendBody("OpSelectionMerge %merge None\n");
1230 }
1231 if (!spvIsWebGPUEnv(env))
1232 str += nameOps("branch", "merge", std::make_pair("func", "Main"));
1233
1234 str += types_consts();
1235 str += "%func = OpFunction %voidt None %funct\n";
1236 str += entry >> branch;
1237 str += branch >> std::vector<Block>({t, f});
1238 str += duplicate >> std::vector<Block>({t, f});
1239 str += t;
1240 str += f;
1241 str += merge;
1242 str += "OpFunctionEnd\n";
1243
1244 return str;
1245 }
1246
1247 TEST_P(ValidateCFG, UnreachableMergeWithMultipleUses) {
1248 CompileSuccessfully(
1249 GetUnreachableMergeWithMultipleUses(GetParam(), SPV_ENV_UNIVERSAL_1_0));
1250 if (GetParam() == SpvCapabilityShader) {
1251 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
1252 EXPECT_THAT(getDiagnosticString(),
1253 HasSubstr("is already a merge block for another header"));
1254 } else {
1255 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1256 }
1257 }
1258
1259 TEST_F(ValidateCFG, WebGPUUnreachableMergeWithMultipleUses) {
1260 CompileSuccessfully(GetUnreachableMergeWithMultipleUses(SpvCapabilityShader,
1261 SPV_ENV_WEBGPU_0));
1262 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions(SPV_ENV_WEBGPU_0));
1263 EXPECT_THAT(getDiagnosticString(),
1264 HasSubstr("is already a merge block for another header"));
1265 }
1266
1267 std::string GetUnreachableContinueWithBranchUse(SpvCapability cap,
1268 spv_target_env env) {
1269 std::string header =
1270 spvIsWebGPUEnv(env) ? GetWebGPUHeader() : GetDefaultHeader(cap);
1271
1272 Block entry("entry");
1273 Block foo("foo", SpvOpBranch);
1274 Block branch("branch", SpvOpBranch);
1275 Block merge("merge", SpvOpReturn);
1276 Block target("target", SpvOpBranch);
1277
1278 foo >> target;
1279 target >> branch;
1280
1281 entry.AppendBody(spvIsWebGPUEnv(env)
1282 ? "%placeholder = OpVariable %intptrt Function %two\n"
1283 : "%placeholder = OpVariable %intptrt Function\n");
1284
1285 std::string str = header;
1286 if (spvIsWebGPUEnv(env)) {
1287 str +=
1288 "OpEntryPoint Fragment %func \"func\"\n"
1289 "OpExecutionMode %func OriginUpperLeft\n";
1290 }
1291 if (cap == SpvCapabilityShader)
1292 branch.AppendBody("OpLoopMerge %merge %target None\n");
1293 if (!spvIsWebGPUEnv(env))
1294 str += nameOps("branch", "merge", "target", std::make_pair("func", "Main"));
1295
1296 str += types_consts();
1297 str += "%intptrt = OpTypePointer Function %intt\n";
1298 str += "%func = OpFunction %voidt None %funct\n";
1299 str += entry >> branch;
1300 str += branch >> std::vector<Block>({merge});
1301 str += merge;
1302 str += target;
1303 str += foo;
1304 str += "OpFunctionEnd\n";
1305
1306 return str;
1307 }
1308
1309 TEST_P(ValidateCFG, UnreachableContinueWithBranchUse) {
1310 CompileSuccessfully(
1311 GetUnreachableContinueWithBranchUse(GetParam(), SPV_ENV_UNIVERSAL_1_0));
1312 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1313 }
1314
1315 TEST_F(ValidateCFG, WebGPUUnreachableContinueWithBranchUse) {
1316 CompileSuccessfully(GetUnreachableContinueWithBranchUse(SpvCapabilityShader,
1317 SPV_ENV_WEBGPU_0));
1318 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions(SPV_ENV_WEBGPU_0));
1319 EXPECT_THAT(getDiagnosticString(),
1320 HasSubstr("cannot be the target of a branch."));
1321 }
1322
1323 std::string GetReachableMergeAndContinue(SpvCapability cap,
1324 spv_target_env env) {
1325 std::string header =
1326 spvIsWebGPUEnv(env) ? GetWebGPUHeader() : GetDefaultHeader(cap);
1327
1328 Block entry("entry");
1329 Block branch("branch", SpvOpBranch);
1330 Block merge("merge", SpvOpReturn);
1331 Block target("target", SpvOpBranch);
1332 Block body("body", SpvOpBranchConditional);
1333 Block t("t", SpvOpBranch);
1334 Block f("f", SpvOpBranch);
1335
1336 target >> branch;
1337 body.SetBody("%cond = OpSLessThan %boolt %one %two\n");
1338 t >> merge;
1339 f >> target;
1340
1341 std::string str = header;
1342 if (spvIsWebGPUEnv(env)) {
1343 str +=
1344 "OpEntryPoint Fragment %func \"func\"\n"
1345 "OpExecutionMode %func OriginUpperLeft\n";
1346 }
1347 if (cap == SpvCapabilityShader) {
1348 branch.AppendBody("OpLoopMerge %merge %target None\n");
1349 body.AppendBody("OpSelectionMerge %f None\n");
1350 }
1351
1352 if (!spvIsWebGPUEnv(env))
1353 str += nameOps("branch", "merge", "target", "body", "t", "f",
1354 std::make_pair("func", "Main"));
1355
1356 str += types_consts();
1357 str += "%func = OpFunction %voidt None %funct\n";
1358 str += entry >> branch;
1359 str += branch >> std::vector<Block>({body});
1360 str += body >> std::vector<Block>({t, f});
1361 str += t;
1362 str += f;
1363 str += merge;
1364 str += target;
1365 str += "OpFunctionEnd\n";
1366
1367 return str;
1368 }
1369
1370 TEST_P(ValidateCFG, ReachableMergeAndContinue) {
1371 CompileSuccessfully(
1372 GetReachableMergeAndContinue(GetParam(), SPV_ENV_UNIVERSAL_1_0));
1373 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1374 }
1375
1376 TEST_F(ValidateCFG, WebGPUReachableMergeAndContinue) {
1377 CompileSuccessfully(
1378 GetReachableMergeAndContinue(SpvCapabilityShader, SPV_ENV_WEBGPU_0));
1379 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions(SPV_ENV_WEBGPU_0));
1380 }
1381
1382 std::string GetUnreachableMergeAndContinue(SpvCapability cap,
1383 spv_target_env env) {
1384 std::string header =
1385 spvIsWebGPUEnv(env) ? GetWebGPUHeader() : GetDefaultHeader(cap);
1386
1387 Block entry("entry");
1388 Block branch("branch", SpvOpBranch);
1389 Block merge("merge", SpvOpReturn);
1390 Block target("target", SpvOpBranch);
1391 Block body("body", SpvOpBranchConditional);
1392 Block t("t", SpvOpReturn);
1393 Block f("f", SpvOpReturn);
1394
1395 target >> branch;
1396 body.SetBody("%cond = OpSLessThan %boolt %one %two\n");
1397
1398 std::string str = header;
1399 if (spvIsWebGPUEnv(env)) {
1400 str +=
1401 "OpEntryPoint Fragment %func \"func\"\n"
1402 "OpExecutionMode %func OriginUpperLeft\n";
1403 }
1404 if (cap == SpvCapabilityShader) {
1405 branch.AppendBody("OpLoopMerge %merge %target None\n");
1406 body.AppendBody("OpSelectionMerge %target None\n");
1407 }
1408
1409 if (!spvIsWebGPUEnv(env))
1410 str += nameOps("branch", "merge", "target", "body", "t", "f",
1411 std::make_pair("func", "Main"));
1412
1413 str += types_consts();
1414 str += "%func = OpFunction %voidt None %funct\n";
1415 str += entry >> branch;
1416 str += branch >> std::vector<Block>({body});
1417 str += body >> std::vector<Block>({t, f});
1418 str += t;
1419 str += f;
1420 str += merge;
1421 str += target;
1422 str += "OpFunctionEnd\n";
1423
1424 return str;
1425 }
1426
1427 TEST_P(ValidateCFG, UnreachableMergeAndContinue) {
1428 CompileSuccessfully(
1429 GetUnreachableMergeAndContinue(GetParam(), SPV_ENV_UNIVERSAL_1_0));
1430 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1431 }
1432
1433 TEST_F(ValidateCFG, WebGPUUnreachableMergeAndContinue) {
1434 CompileSuccessfully(
1435 GetUnreachableMergeAndContinue(SpvCapabilityShader, SPV_ENV_WEBGPU_0));
1436 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions(SPV_ENV_WEBGPU_0));
1437 EXPECT_THAT(
1438 getDiagnosticString(),
1439 HasSubstr("unreachable merge-blocks must terminate with OpUnreachable"));
1440 }
1441
1442 std::string GetUnreachableBlock(SpvCapability cap, spv_target_env env) {
1443 std::string header =
1444 spvIsWebGPUEnv(env) ? GetWebGPUHeader() : GetDefaultHeader(cap);
1445
1446 Block entry("entry");
1447 Block unreachable("unreachable");
1448 Block exit("exit", SpvOpReturn);
1449
1450 std::string str = header;
1451 if (spvIsWebGPUEnv(env)) {
1452 str +=
1453 "OpEntryPoint Fragment %func \"func\"\n"
1454 "OpExecutionMode %func OriginUpperLeft\n";
1455 }
1456 if (!spvIsWebGPUEnv(env))
1457 str += nameOps("unreachable", "exit", std::make_pair("func", "Main"));
1458 str += types_consts();
1459 str += "%func = OpFunction %voidt None %funct\n";
1460 str += entry >> exit;
1461 str += unreachable >> exit;
1462 str += exit;
1463 str += "OpFunctionEnd\n";
1464
1465 return str;
1466 }
1467
1468 TEST_P(ValidateCFG, UnreachableBlock) {
1469 CompileSuccessfully(GetUnreachableBlock(GetParam(), SPV_ENV_UNIVERSAL_1_0));
1470 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1471 }
1472
1473 TEST_F(ValidateCFG, WebGPUUnreachableBlock) {
1474 CompileSuccessfully(
1475 GetUnreachableBlock(SpvCapabilityShader, SPV_ENV_WEBGPU_0));
1476 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions(SPV_ENV_WEBGPU_0));
1477 EXPECT_THAT(getDiagnosticString(), HasSubstr("all blocks must be reachable"));
1478 }
1479
1480 std::string GetUnreachableBranch(SpvCapability cap, spv_target_env env) {
1481 std::string header =
1482 spvIsWebGPUEnv(env) ? GetWebGPUHeader() : GetDefaultHeader(cap);
1483
1484 Block entry("entry");
1485 Block unreachable("unreachable", SpvOpBranchConditional);
1486 Block unreachablechildt("unreachablechildt");
1487 Block unreachablechildf("unreachablechildf");
1488 Block merge("merge");
1489 Block exit("exit", SpvOpReturn);
1490
1491 unreachable.SetBody("%cond = OpSLessThan %boolt %one %two\n");
1492 if (cap == SpvCapabilityShader)
1493 unreachable.AppendBody("OpSelectionMerge %merge None\n");
1494
1495 std::string str = header;
1496 if (spvIsWebGPUEnv(env)) {
1497 str +=
1498 "OpEntryPoint Fragment %func \"func\"\n"
1499 "OpExecutionMode %func OriginUpperLeft\n";
1500 }
1501 if (!spvIsWebGPUEnv(env))
1502 str += nameOps("unreachable", "exit", std::make_pair("func", "Main"));
1503 str += types_consts();
1504 str += "%func = OpFunction %voidt None %funct\n";
1505
1506 str += entry >> exit;
1507 str +=
1508 unreachable >> std::vector<Block>({unreachablechildt, unreachablechildf});
1509 str += unreachablechildt >> merge;
1510 str += unreachablechildf >> merge;
1511 str += merge >> exit;
1512 str += exit;
1513 str += "OpFunctionEnd\n";
1514
1515 return str;
1516 }
1517
1518 TEST_P(ValidateCFG, UnreachableBranch) {
1519 CompileSuccessfully(GetUnreachableBranch(GetParam(), SPV_ENV_UNIVERSAL_1_0));
1520 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1521 }
1522
1523 TEST_F(ValidateCFG, WebGPUUnreachableBranch) {
1524 CompileSuccessfully(
1525 GetUnreachableBranch(SpvCapabilityShader, SPV_ENV_WEBGPU_0));
1526 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions(SPV_ENV_WEBGPU_0));
1527 EXPECT_THAT(getDiagnosticString(), HasSubstr("all blocks must be reachable"));
1528 }
1529
1530 TEST_P(ValidateCFG, EmptyFunction) {
1531 std::string str = GetDefaultHeader(GetParam()) + std::string(types_consts()) +
1532 R"(%func = OpFunction %voidt None %funct
1533 %l = OpLabel
1534 OpReturn
1535 OpFunctionEnd)";
1536
1537 CompileSuccessfully(str);
1538 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1539 }
1540
1541 TEST_P(ValidateCFG, SingleBlockLoop) {
1542 bool is_shader = GetParam() == SpvCapabilityShader;
1543 Block entry("entry");
1544 Block loop("loop", SpvOpBranchConditional);
1545 Block exit("exit", SpvOpReturn);
1546
1547 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
1548 if (is_shader) loop.AppendBody("OpLoopMerge %exit %loop None\n");
1549
1550 std::string str = GetDefaultHeader(GetParam()) + std::string(types_consts()) +
1551 "%func = OpFunction %voidt None %funct\n";
1552
1553 str += entry >> loop;
1554 str += loop >> std::vector<Block>({loop, exit});
1555 str += exit;
1556 str += "OpFunctionEnd";
1557
1558 CompileSuccessfully(str);
1559 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1560 }
1561
1562 TEST_P(ValidateCFG, NestedLoops) {
1563 bool is_shader = GetParam() == SpvCapabilityShader;
1564 Block entry("entry");
1565 Block loop1("loop1");
1566 Block loop1_cont_break_block("loop1_cont_break_block",
1567 SpvOpBranchConditional);
1568 Block loop2("loop2", SpvOpBranchConditional);
1569 Block loop2_merge("loop2_merge");
1570 Block loop1_merge("loop1_merge");
1571 Block exit("exit", SpvOpReturn);
1572
1573 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
1574 if (is_shader) {
1575 loop1.SetBody("OpLoopMerge %loop1_merge %loop2 None\n");
1576 loop2.SetBody("OpLoopMerge %loop2_merge %loop2 None\n");
1577 }
1578
1579 std::string str = GetDefaultHeader(GetParam()) +
1580 nameOps("loop2", "loop2_merge") + types_consts() +
1581 "%func = OpFunction %voidt None %funct\n";
1582
1583 str += entry >> loop1;
1584 str += loop1 >> loop1_cont_break_block;
1585 str += loop1_cont_break_block >> std::vector<Block>({loop1_merge, loop2});
1586 str += loop2 >> std::vector<Block>({loop2, loop2_merge});
1587 str += loop2_merge >> loop1;
1588 str += loop1_merge >> exit;
1589 str += exit;
1590 str += "OpFunctionEnd";
1591
1592 CompileSuccessfully(str);
1593 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1594 }
1595
1596 TEST_P(ValidateCFG, NestedSelection) {
1597 bool is_shader = GetParam() == SpvCapabilityShader;
1598 Block entry("entry");
1599 const int N = 256;
1600 std::vector<Block> if_blocks;
1601 std::vector<Block> merge_blocks;
1602 Block inner("inner");
1603
1604 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
1605
1606 if_blocks.emplace_back("if0", SpvOpBranchConditional);
1607
1608 if (is_shader) if_blocks[0].SetBody("OpSelectionMerge %if_merge0 None\n");
1609 merge_blocks.emplace_back("if_merge0", SpvOpReturn);
1610
1611 for (int i = 1; i < N; i++) {
1612 std::stringstream ss;
1613 ss << i;
1614 if_blocks.emplace_back("if" + ss.str(), SpvOpBranchConditional);
1615 if (is_shader)
1616 if_blocks[i].SetBody("OpSelectionMerge %if_merge" + ss.str() + " None\n");
1617 merge_blocks.emplace_back("if_merge" + ss.str(), SpvOpBranch);
1618 }
1619 std::string str = GetDefaultHeader(GetParam()) + std::string(types_consts()) +
1620 "%func = OpFunction %voidt None %funct\n";
1621
1622 str += entry >> if_blocks[0];
1623 for (int i = 0; i < N - 1; i++) {
1624 str +=
1625 if_blocks[i] >> std::vector<Block>({if_blocks[i + 1], merge_blocks[i]});
1626 }
1627 str += if_blocks.back() >> std::vector<Block>({inner, merge_blocks.back()});
1628 str += inner >> merge_blocks.back();
1629 for (int i = N - 1; i > 0; i--) {
1630 str += merge_blocks[i] >> merge_blocks[i - 1];
1631 }
1632 str += merge_blocks[0];
1633 str += "OpFunctionEnd";
1634
1635 CompileSuccessfully(str);
1636 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1637 }
1638
1639 TEST_P(ValidateCFG, BackEdgeBlockDoesntPostDominateContinueTargetBad) {
1640 bool is_shader = GetParam() == SpvCapabilityShader;
1641 Block entry("entry");
1642 Block loop1("loop1", SpvOpBranchConditional);
1643 Block loop2("loop2", SpvOpBranchConditional);
1644 Block loop2_merge("loop2_merge");
1645 Block loop1_cont("loop1_cont", SpvOpBranchConditional);
1646 Block be_block("be_block");
1647 Block exit("exit", SpvOpReturn);
1648
1649 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
1650 if (is_shader) {
1651 loop1.SetBody("OpLoopMerge %exit %loop1_cont None\n");
1652 loop2.SetBody("OpLoopMerge %loop2_merge %loop2 None\n");
1653 }
1654
1655 std::string str =
1656 GetDefaultHeader(GetParam()) +
1657 nameOps("loop1", "loop2", "be_block", "loop1_cont", "loop2_merge") +
1658 types_consts() + "%func = OpFunction %voidt None %funct\n";
1659
1660 str += entry >> loop1;
1661 str += loop1 >> std::vector<Block>({loop2, exit});
1662 str += loop2 >> std::vector<Block>({loop2, loop2_merge});
1663 str += loop2_merge >> loop1_cont;
1664 str += loop1_cont >> std::vector<Block>({be_block, exit});
1665 str += be_block >> loop1;
1666 str += exit;
1667 str += "OpFunctionEnd";
1668
1669 CompileSuccessfully(str);
1670 if (GetParam() == SpvCapabilityShader) {
1671 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
1672 EXPECT_THAT(getDiagnosticString(),
1673 MatchesRegex("The continue construct with the continue target "
1674 ".\\[%loop1_cont\\] is not post dominated by the "
1675 "back-edge block .\\[%be_block\\]\n"
1676 " %be_block = OpLabel\n"));
1677 } else {
1678 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1679 }
1680 }
1681
1682 TEST_P(ValidateCFG, BranchingToNonLoopHeaderBlockBad) {
1683 bool is_shader = GetParam() == SpvCapabilityShader;
1684 Block entry("entry");
1685 Block split("split", SpvOpBranchConditional);
1686 Block t("t");
1687 Block f("f");
1688 Block exit("exit", SpvOpReturn);
1689
1690 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
1691 if (is_shader) split.SetBody("OpSelectionMerge %exit None\n");
1692
1693 std::string str = GetDefaultHeader(GetParam()) + nameOps("split", "f") +
1694 types_consts() +
1695 "%func = OpFunction %voidt None %funct\n";
1696
1697 str += entry >> split;
1698 str += split >> std::vector<Block>({t, f});
1699 str += t >> exit;
1700 str += f >> split;
1701 str += exit;
1702 str += "OpFunctionEnd";
1703
1704 CompileSuccessfully(str);
1705 if (is_shader) {
1706 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
1707 EXPECT_THAT(
1708 getDiagnosticString(),
1709 MatchesRegex("Back-edges \\(.\\[%f\\] -> .\\[%split\\]\\) can only "
1710 "be formed between a block and a loop header.\n"
1711 " %f = OpLabel\n"));
1712 } else {
1713 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1714 }
1715 }
1716
1717 TEST_P(ValidateCFG, BranchingToSameNonLoopHeaderBlockBad) {
1718 bool is_shader = GetParam() == SpvCapabilityShader;
1719 Block entry("entry");
1720 Block split("split", SpvOpBranchConditional);
1721 Block exit("exit", SpvOpReturn);
1722
1723 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
1724 if (is_shader) split.SetBody("OpSelectionMerge %exit None\n");
1725
1726 std::string str = GetDefaultHeader(GetParam()) + nameOps("split") +
1727 types_consts() +
1728 "%func = OpFunction %voidt None %funct\n";
1729
1730 str += entry >> split;
1731 str += split >> std::vector<Block>({split, exit});
1732 str += exit;
1733 str += "OpFunctionEnd";
1734
1735 CompileSuccessfully(str);
1736 if (is_shader) {
1737 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
1738 EXPECT_THAT(
1739 getDiagnosticString(),
1740 MatchesRegex(
1741 "Back-edges \\(.\\[%split\\] -> .\\[%split\\]\\) can only be "
1742 "formed between a block and a loop header.\n %split = OpLabel\n"));
1743 } else {
1744 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1745 }
1746 }
1747
1748 TEST_P(ValidateCFG, MultipleBackEdgeBlocksToLoopHeaderBad) {
1749 bool is_shader = GetParam() == SpvCapabilityShader;
1750 Block entry("entry");
1751 Block loop("loop", SpvOpBranchConditional);
1752 Block back0("back0");
1753 Block back1("back1");
1754 Block merge("merge", SpvOpReturn);
1755
1756 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
1757 if (is_shader) loop.SetBody("OpLoopMerge %merge %back0 None\n");
1758
1759 std::string str = GetDefaultHeader(GetParam()) +
1760 nameOps("loop", "back0", "back1") + types_consts() +
1761 "%func = OpFunction %voidt None %funct\n";
1762
1763 str += entry >> loop;
1764 str += loop >> std::vector<Block>({back0, back1});
1765 str += back0 >> loop;
1766 str += back1 >> loop;
1767 str += merge;
1768 str += "OpFunctionEnd";
1769
1770 CompileSuccessfully(str);
1771 if (is_shader) {
1772 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
1773 EXPECT_THAT(
1774 getDiagnosticString(),
1775 MatchesRegex(
1776 "Loop header .\\[%loop\\] is targeted by 2 back-edge blocks but "
1777 "the standard requires exactly one\n %loop = OpLabel\n"))
1778 << str;
1779 } else {
1780 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1781 }
1782 }
1783
1784 TEST_P(ValidateCFG, ContinueTargetMustBePostDominatedByBackEdge) {
1785 bool is_shader = GetParam() == SpvCapabilityShader;
1786 Block entry("entry");
1787 Block loop("loop", SpvOpBranchConditional);
1788 Block cheader("cheader", SpvOpBranchConditional);
1789 Block be_block("be_block");
1790 Block merge("merge", SpvOpReturn);
1791 Block exit("exit", SpvOpReturn);
1792
1793 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
1794 if (is_shader) loop.SetBody("OpLoopMerge %merge %cheader None\n");
1795
1796 std::string str = GetDefaultHeader(GetParam()) +
1797 nameOps("cheader", "be_block") + types_consts() +
1798 "%func = OpFunction %voidt None %funct\n";
1799
1800 str += entry >> loop;
1801 str += loop >> std::vector<Block>({cheader, merge});
1802 str += cheader >> std::vector<Block>({exit, be_block});
1803 str += exit; // Branches out of a continue construct
1804 str += be_block >> loop;
1805 str += merge;
1806 str += "OpFunctionEnd";
1807
1808 CompileSuccessfully(str);
1809 if (is_shader) {
1810 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
1811 EXPECT_THAT(getDiagnosticString(),
1812 MatchesRegex("The continue construct with the continue target "
1813 ".\\[%cheader\\] is not post dominated by the "
1814 "back-edge block .\\[%be_block\\]\n"
1815 " %be_block = OpLabel\n"));
1816 } else {
1817 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1818 }
1819 }
1820
1821 TEST_P(ValidateCFG, BranchOutOfConstructToMergeBad) {
1822 bool is_shader = GetParam() == SpvCapabilityShader;
1823 Block entry("entry");
1824 Block loop("loop", SpvOpBranchConditional);
1825 Block cont("cont", SpvOpBranchConditional);
1826 Block merge("merge", SpvOpReturn);
1827
1828 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
1829 if (is_shader) loop.SetBody("OpLoopMerge %merge %loop None\n");
1830
1831 std::string str = GetDefaultHeader(GetParam()) + nameOps("cont", "loop") +
1832 types_consts() +
1833 "%func = OpFunction %voidt None %funct\n";
1834
1835 str += entry >> loop;
1836 str += loop >> std::vector<Block>({cont, merge});
1837 str += cont >> std::vector<Block>({loop, merge});
1838 str += merge;
1839 str += "OpFunctionEnd";
1840
1841 CompileSuccessfully(str);
1842 if (is_shader) {
1843 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
1844 EXPECT_THAT(getDiagnosticString(),
1845 MatchesRegex("The continue construct with the continue target "
1846 ".\\[%loop\\] is not post dominated by the "
1847 "back-edge block .\\[%cont\\]\n"
1848 " %cont = OpLabel\n"))
1849 << str;
1850 } else {
1851 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1852 }
1853 }
1854
1855 TEST_P(ValidateCFG, BranchOutOfConstructBad) {
1856 bool is_shader = GetParam() == SpvCapabilityShader;
1857 Block entry("entry");
1858 Block loop("loop", SpvOpBranchConditional);
1859 Block cont("cont", SpvOpBranchConditional);
1860 Block merge("merge");
1861 Block exit("exit", SpvOpReturn);
1862
1863 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
1864 if (is_shader) loop.SetBody("OpLoopMerge %merge %loop None\n");
1865
1866 std::string str = GetDefaultHeader(GetParam()) + nameOps("cont", "loop") +
1867 types_consts() +
1868 "%func = OpFunction %voidt None %funct\n";
1869
1870 str += entry >> loop;
1871 str += loop >> std::vector<Block>({cont, merge});
1872 str += cont >> std::vector<Block>({loop, exit});
1873 str += merge >> exit;
1874 str += exit;
1875 str += "OpFunctionEnd";
1876
1877 CompileSuccessfully(str);
1878 if (is_shader) {
1879 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
1880 EXPECT_THAT(getDiagnosticString(),
1881 MatchesRegex("The continue construct with the continue target "
1882 ".\\[%loop\\] is not post dominated by the "
1883 "back-edge block .\\[%cont\\]\n"
1884 " %cont = OpLabel\n"));
1885 } else {
1886 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
1887 }
1888 }
1889
1890 TEST_F(ValidateCFG, OpSwitchToUnreachableBlock) {
1891 Block entry("entry", SpvOpSwitch);
1892 Block case0("case0");
1893 Block case1("case1");
1894 Block case2("case2");
1895 Block def("default", SpvOpUnreachable);
1896 Block phi("phi", SpvOpReturn);
1897
1898 std::string str = R"(
1899 OpCapability Shader
1900 OpMemoryModel Logical GLSL450
1901 OpEntryPoint GLCompute %main "main" %id
1902 OpExecutionMode %main LocalSize 1 1 1
1903 OpSource GLSL 430
1904 OpName %main "main"
1905 OpDecorate %id BuiltIn GlobalInvocationId
1906 %void = OpTypeVoid
1907 %voidf = OpTypeFunction %void
1908 %u32 = OpTypeInt 32 0
1909 %f32 = OpTypeFloat 32
1910 %uvec3 = OpTypeVector %u32 3
1911 %fvec3 = OpTypeVector %f32 3
1912 %uvec3ptr = OpTypePointer Input %uvec3
1913 %id = OpVariable %uvec3ptr Input
1914 %one = OpConstant %u32 1
1915 %three = OpConstant %u32 3
1916 %main = OpFunction %void None %voidf
1917 )";
1918
1919 entry.SetBody(
1920 "%idval = OpLoad %uvec3 %id\n"
1921 "%x = OpCompositeExtract %u32 %idval 0\n"
1922 "%selector = OpUMod %u32 %x %three\n"
1923 "OpSelectionMerge %phi None\n");
1924 str += entry >> std::vector<Block>({def, case0, case1, case2});
1925 str += case1 >> phi;
1926 str += def;
1927 str += phi;
1928 str += case0 >> phi;
1929 str += case2 >> phi;
1930 str += "OpFunctionEnd";
1931
1932 CompileSuccessfully(str);
1933 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
1934 }
1935
1936 TEST_F(ValidateCFG, LoopWithZeroBackEdgesBad) {
1937 std::string str = R"(
1938 OpCapability Shader
1939 OpMemoryModel Logical GLSL450
1940 OpEntryPoint Fragment %main "main"
1941 OpExecutionMode %main OriginUpperLeft
1942 OpName %loop "loop"
1943 %voidt = OpTypeVoid
1944 %funct = OpTypeFunction %voidt
1945 %main = OpFunction %voidt None %funct
1946 %loop = OpLabel
1947 OpLoopMerge %exit %loop None
1948 OpBranch %exit
1949 %exit = OpLabel
1950 OpReturn
1951 OpFunctionEnd
1952 )";
1953 CompileSuccessfully(str);
1954 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
1955 EXPECT_THAT(
1956 getDiagnosticString(),
1957 MatchesRegex("Loop header .\\[%loop\\] is targeted by "
1958 "0 back-edge blocks but the standard requires exactly "
1959 "one\n %loop = OpLabel\n"));
1960 }
1961
1962 TEST_F(ValidateCFG, LoopWithBackEdgeFromUnreachableContinueConstructGood) {
1963 std::string str = R"(
1964 OpCapability Shader
1965 OpMemoryModel Logical GLSL450
1966 OpEntryPoint Fragment %main "main"
1967 OpExecutionMode %main OriginUpperLeft
1968 OpName %loop "loop"
1969 %voidt = OpTypeVoid
1970 %funct = OpTypeFunction %voidt
1971 %floatt = OpTypeFloat 32
1972 %boolt = OpTypeBool
1973 %one = OpConstant %floatt 1
1974 %two = OpConstant %floatt 2
1975 %main = OpFunction %voidt None %funct
1976 %entry = OpLabel
1977 OpBranch %loop
1978 %loop = OpLabel
1979 OpLoopMerge %exit %cont None
1980 OpBranch %16
1981 %16 = OpLabel
1982 %cond = OpFOrdLessThan %boolt %one %two
1983 OpBranchConditional %cond %body %exit
1984 %body = OpLabel
1985 OpReturn
1986 %cont = OpLabel ; Reachable only from OpLoopMerge ContinueTarget parameter
1987 OpBranch %loop ; Should be considered a back-edge
1988 %exit = OpLabel
1989 OpReturn
1990 OpFunctionEnd
1991 )";
1992
1993 CompileSuccessfully(str);
1994 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions()) << getDiagnosticString();
1995 }
1996
1997 TEST_P(ValidateCFG,
1998 NestedConstructWithUnreachableMergeBlockBranchingToOuterMergeBlock) {
1999 // Test for https://github.com/KhronosGroup/SPIRV-Tools/issues/297
2000 // The nested construct has an unreachable merge block. In the
2001 // augmented CFG that merge block
2002 // we still determine that the
2003 bool is_shader = GetParam() == SpvCapabilityShader;
2004 Block entry("entry", SpvOpBranchConditional);
2005 Block inner_head("inner_head", SpvOpBranchConditional);
2006 Block inner_true("inner_true", SpvOpReturn);
2007 Block inner_false("inner_false", SpvOpReturn);
2008 Block inner_merge("inner_merge");
2009 Block exit("exit", SpvOpReturn);
2010
2011 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
2012 if (is_shader) {
2013 entry.AppendBody("OpSelectionMerge %exit None\n");
2014 inner_head.SetBody("OpSelectionMerge %inner_merge None\n");
2015 }
2016
2017 std::string str = GetDefaultHeader(GetParam()) +
2018 nameOps("entry", "inner_merge", "exit") + types_consts() +
2019 "%func = OpFunction %voidt None %funct\n";
2020
2021 str += entry >> std::vector<Block>({inner_head, exit});
2022 str += inner_head >> std::vector<Block>({inner_true, inner_false});
2023 str += inner_true;
2024 str += inner_false;
2025 str += inner_merge >> exit;
2026 str += exit;
2027 str += "OpFunctionEnd";
2028
2029 CompileSuccessfully(str);
2030 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions()) << getDiagnosticString();
2031 }
2032
2033 TEST_P(ValidateCFG, ContinueTargetCanBeMergeBlockForNestedStructure) {
2034 // The continue construct cannot be the merge target of a nested selection
2035 // because the loop construct must contain "if_merge" because it contains
2036 // "if_head".
2037 bool is_shader = GetParam() == SpvCapabilityShader;
2038 Block entry("entry");
2039 Block loop("loop");
2040 Block if_head("if_head", SpvOpBranchConditional);
2041 Block if_true("if_true");
2042 Block if_merge("if_merge", SpvOpBranchConditional);
2043 Block merge("merge", SpvOpReturn);
2044
2045 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
2046 if (is_shader) {
2047 loop.SetBody("OpLoopMerge %merge %if_merge None\n");
2048 if_head.SetBody("OpSelectionMerge %if_merge None\n");
2049 }
2050
2051 std::string str =
2052 GetDefaultHeader(GetParam()) +
2053 nameOps("entry", "loop", "if_head", "if_true", "if_merge", "merge") +
2054 types_consts() + "%func = OpFunction %voidt None %funct\n";
2055
2056 str += entry >> loop;
2057 str += loop >> if_head;
2058 str += if_head >> std::vector<Block>({if_true, if_merge});
2059 str += if_true >> if_merge;
2060 str += if_merge >> std::vector<Block>({loop, merge});
2061 str += merge;
2062 str += "OpFunctionEnd";
2063
2064 CompileSuccessfully(str);
2065 if (is_shader) {
2066 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
2067 EXPECT_THAT(
2068 getDiagnosticString(),
2069 HasSubstr("Header block 3[%if_head] is contained in the loop construct "
2070 "headed "
2071 "by 2[%loop], but its merge block 5[%if_merge] is not"));
2072 } else {
2073 EXPECT_THAT(SPV_SUCCESS, ValidateInstructions());
2074 }
2075 }
2076
2077 TEST_P(ValidateCFG, SingleLatchBlockMultipleBranchesToLoopHeader) {
2078 // This test case ensures we allow both branches of a loop latch block
2079 // to go back to the loop header. It still counts as a single back edge.
2080 bool is_shader = GetParam() == SpvCapabilityShader;
2081 Block entry("entry");
2082 Block loop("loop", SpvOpBranchConditional);
2083 Block latch("latch", SpvOpBranchConditional);
2084 Block merge("merge", SpvOpReturn);
2085
2086 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
2087 if (is_shader) {
2088 loop.SetBody("OpLoopMerge %merge %latch None\n");
2089 }
2090
2091 std::string str = GetDefaultHeader(GetParam()) +
2092 nameOps("entry", "loop", "latch", "merge") +
2093 types_consts() +
2094 "%func = OpFunction %voidt None %funct\n";
2095
2096 str += entry >> loop;
2097 str += loop >> std::vector<Block>({latch, merge});
2098 str += latch >> std::vector<Block>({loop, loop}); // This is the key
2099 str += merge;
2100 str += "OpFunctionEnd";
2101
2102 CompileSuccessfully(str);
2103 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions())
2104 << str << getDiagnosticString();
2105 }
2106
2107 TEST_P(ValidateCFG, SingleLatchBlockHeaderContinueTargetIsItselfGood) {
2108 // This test case ensures we don't count a Continue Target from a loop
2109 // header to itself as a self-loop when computing back edges.
2110 // Also, it detects that there is an edge from %latch to the pseudo-exit
2111 // node, rather than from %loop. In particular, it detects that we
2112 // have used the *reverse* textual order of blocks when computing
2113 // predecessor traversal roots.
2114 bool is_shader = GetParam() == SpvCapabilityShader;
2115 Block entry("entry");
2116 Block loop("loop");
2117 Block latch("latch");
2118 Block merge("merge", SpvOpReturn);
2119
2120 entry.SetBody("%cond = OpSLessThan %boolt %one %two\n");
2121 if (is_shader) {
2122 loop.SetBody("OpLoopMerge %merge %loop None\n");
2123 }
2124
2125 std::string str = GetDefaultHeader(GetParam()) +
2126 nameOps("entry", "loop", "latch", "merge") +
2127 types_consts() +
2128 "%func = OpFunction %voidt None %funct\n";
2129
2130 str += entry >> loop;
2131 str += loop >> latch;
2132 str += latch >> loop;
2133 str += merge;
2134 str += "OpFunctionEnd";
2135
2136 CompileSuccessfully(str);
2137 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions())
2138 << str << getDiagnosticString();
2139 }
2140
2141 // Unit test to check the case where a basic block is the entry block of 2
2142 // different constructs. In this case, the basic block is the entry block of a
2143 // continue construct as well as a selection construct. See issue# 517 for more
2144 // details.
2145 TEST_F(ValidateCFG, BasicBlockIsEntryBlockOfTwoConstructsGood) {
2146 std::string spirv = R"(
2147 OpCapability Shader
2148 OpCapability Linkage
2149 OpMemoryModel Logical GLSL450
2150 %void = OpTypeVoid
2151 %bool = OpTypeBool
2152 %int = OpTypeInt 32 1
2153 %void_func = OpTypeFunction %void
2154 %int_0 = OpConstant %int 0
2155 %testfun = OpFunction %void None %void_func
2156 %label_1 = OpLabel
2157 OpBranch %start
2158 %start = OpLabel
2159 %cond = OpSLessThan %bool %int_0 %int_0
2160 ;
2161 ; Note: In this case, the "target" block is both the entry block of
2162 ; the continue construct of the loop as well as the entry block of
2163 ; the selection construct.
2164 ;
2165 OpLoopMerge %loop_merge %target None
2166 OpBranchConditional %cond %target %loop_merge
2167 %loop_merge = OpLabel
2168 OpReturn
2169 %target = OpLabel
2170 OpSelectionMerge %selection_merge None
2171 OpBranchConditional %cond %do_stuff %do_other_stuff
2172 %do_other_stuff = OpLabel
2173 OpBranch %selection_merge
2174 %selection_merge = OpLabel
2175 OpBranch %start
2176 %do_stuff = OpLabel
2177 OpBranch %selection_merge
2178 OpFunctionEnd
2179 )";
2180 CompileSuccessfully(spirv);
2181 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2182 }
2183
2184 TEST_F(ValidateCFG, OpReturnInNonVoidFunc) {
2185 std::string spirv = R"(
2186 OpCapability Shader
2187 OpCapability Linkage
2188 OpMemoryModel Logical GLSL450
2189 %int = OpTypeInt 32 1
2190 %int_func = OpTypeFunction %int
2191 %testfun = OpFunction %int None %int_func
2192 %label_1 = OpLabel
2193 OpReturn
2194 OpFunctionEnd
2195 )";
2196 CompileSuccessfully(spirv);
2197 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
2198 EXPECT_THAT(
2199 getDiagnosticString(),
2200 HasSubstr(
2201 "OpReturn can only be called from a function with void return type.\n"
2202 " OpReturn"));
2203 }
2204
2205 TEST_F(ValidateCFG, StructuredCFGBranchIntoSelectionBody) {
2206 std::string spirv = R"(
2207 OpCapability Shader
2208 OpMemoryModel Logical GLSL450
2209 OpEntryPoint Fragment %func "func"
2210 OpExecutionMode %func OriginUpperLeft
2211 %void = OpTypeVoid
2212 %bool = OpTypeBool
2213 %true = OpConstantTrue %bool
2214 %functy = OpTypeFunction %void
2215 %func = OpFunction %void None %functy
2216 %entry = OpLabel
2217 OpSelectionMerge %merge None
2218 OpBranchConditional %true %then %merge
2219 %merge = OpLabel
2220 OpBranch %then
2221 %then = OpLabel
2222 OpReturn
2223 OpFunctionEnd
2224 )";
2225
2226 CompileSuccessfully(spirv);
2227 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
2228 EXPECT_THAT(getDiagnosticString(),
2229 HasSubstr("branches to the selection construct, but not to the "
2230 "selection header <ID> 6\n %7 = OpLabel"));
2231 }
2232
2233 TEST_F(ValidateCFG, SwitchDefaultOnly) {
2234 std::string text = R"(
2235 OpCapability Shader
2236 OpCapability Linkage
2237 OpMemoryModel Logical GLSL450
2238 %1 = OpTypeVoid
2239 %2 = OpTypeInt 32 0
2240 %3 = OpConstant %2 0
2241 %4 = OpTypeFunction %1
2242 %5 = OpFunction %1 None %4
2243 %6 = OpLabel
2244 OpSelectionMerge %7 None
2245 OpSwitch %3 %7
2246 %7 = OpLabel
2247 OpReturn
2248 OpFunctionEnd
2249 )";
2250
2251 CompileSuccessfully(text);
2252 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
2253 }
2254
2255 TEST_F(ValidateCFG, SwitchSingleCase) {
2256 std::string text = R"(
2257 OpCapability Shader
2258 OpCapability Linkage
2259 OpMemoryModel Logical GLSL450
2260 %1 = OpTypeVoid
2261 %2 = OpTypeInt 32 0
2262 %3 = OpConstant %2 0
2263 %4 = OpTypeFunction %1
2264 %5 = OpFunction %1 None %4
2265 %6 = OpLabel
2266 OpSelectionMerge %7 None
2267 OpSwitch %3 %7 0 %8
2268 %8 = OpLabel
2269 OpBranch %7
2270 %7 = OpLabel
2271 OpReturn
2272 OpFunctionEnd
2273 )";
2274
2275 CompileSuccessfully(text);
2276 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
2277 }
2278
2279 TEST_F(ValidateCFG, MultipleFallThroughBlocks) {
2280 std::string text = R"(
2281 OpCapability Shader
2282 OpCapability Linkage
2283 OpMemoryModel Logical GLSL450
2284 %1 = OpTypeVoid
2285 %2 = OpTypeInt 32 0
2286 %3 = OpConstant %2 0
2287 %4 = OpTypeFunction %1
2288 %5 = OpTypeBool
2289 %6 = OpConstantTrue %5
2290 %7 = OpFunction %1 None %4
2291 %8 = OpLabel
2292 OpSelectionMerge %9 None
2293 OpSwitch %3 %10 0 %11 1 %12
2294 %10 = OpLabel
2295 OpBranchConditional %6 %11 %12
2296 %11 = OpLabel
2297 OpBranch %9
2298 %12 = OpLabel
2299 OpBranch %9
2300 %9 = OpLabel
2301 OpReturn
2302 OpFunctionEnd
2303 )";
2304
2305 CompileSuccessfully(text);
2306 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
2307 EXPECT_THAT(
2308 getDiagnosticString(),
2309 HasSubstr(
2310 "Case construct that targets 10[%10] has branches to multiple other "
2311 "case construct targets 12[%12] and 11[%11]\n %10 = OpLabel"));
2312 }
2313
2314 TEST_F(ValidateCFG, MultipleFallThroughToDefault) {
2315 std::string text = R"(
2316 OpCapability Shader
2317 OpCapability Linkage
2318 OpMemoryModel Logical GLSL450
2319 %1 = OpTypeVoid
2320 %2 = OpTypeInt 32 0
2321 %3 = OpConstant %2 0
2322 %4 = OpTypeFunction %1
2323 %5 = OpTypeBool
2324 %6 = OpConstantTrue %5
2325 %7 = OpFunction %1 None %4
2326 %8 = OpLabel
2327 OpSelectionMerge %9 None
2328 OpSwitch %3 %10 0 %11 1 %12
2329 %10 = OpLabel
2330 OpBranch %9
2331 %11 = OpLabel
2332 OpBranch %10
2333 %12 = OpLabel
2334 OpBranch %10
2335 %9 = OpLabel
2336 OpReturn
2337 OpFunctionEnd
2338 )";
2339
2340 CompileSuccessfully(text);
2341 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
2342 EXPECT_THAT(
2343 getDiagnosticString(),
2344 HasSubstr("Multiple case constructs have branches to the case construct "
2345 "that targets 10[%10]\n %10 = OpLabel"));
2346 }
2347
2348 TEST_F(ValidateCFG, MultipleFallThroughToNonDefault) {
2349 std::string text = R"(
2350 OpCapability Shader
2351 OpCapability Linkage
2352 OpMemoryModel Logical GLSL450
2353 %1 = OpTypeVoid
2354 %2 = OpTypeInt 32 0
2355 %3 = OpConstant %2 0
2356 %4 = OpTypeFunction %1
2357 %5 = OpTypeBool
2358 %6 = OpConstantTrue %5
2359 %7 = OpFunction %1 None %4
2360 %8 = OpLabel
2361 OpSelectionMerge %9 None
2362 OpSwitch %3 %10 0 %11 1 %12
2363 %10 = OpLabel
2364 OpBranch %12
2365 %11 = OpLabel
2366 OpBranch %12
2367 %12 = OpLabel
2368 OpBranch %9
2369 %9 = OpLabel
2370 OpReturn
2371 OpFunctionEnd
2372 )";
2373
2374 CompileSuccessfully(text);
2375 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
2376 EXPECT_THAT(
2377 getDiagnosticString(),
2378 HasSubstr("Multiple case constructs have branches to the case construct "
2379 "that targets 12[%12]\n %12 = OpLabel"));
2380 }
2381
2382 TEST_F(ValidateCFG, DuplicateTargetWithFallThrough) {
2383 std::string text = R"(
2384 OpCapability Shader
2385 OpCapability Linkage
2386 OpMemoryModel Logical GLSL450
2387 %1 = OpTypeVoid
2388 %2 = OpTypeInt 32 0
2389 %3 = OpConstant %2 0
2390 %4 = OpTypeFunction %1
2391 %5 = OpTypeBool
2392 %6 = OpConstantTrue %5
2393 %7 = OpFunction %1 None %4
2394 %8 = OpLabel
2395 OpSelectionMerge %9 None
2396 OpSwitch %3 %10 0 %10 1 %11
2397 %10 = OpLabel
2398 OpBranch %11
2399 %11 = OpLabel
2400 OpBranch %9
2401 %9 = OpLabel
2402 OpReturn
2403 OpFunctionEnd
2404 )";
2405
2406 CompileSuccessfully(text);
2407 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
2408 }
2409
2410 TEST_F(ValidateCFG, WrongOperandList) {
2411 std::string text = R"(
2412 OpCapability Shader
2413 OpCapability Linkage
2414 OpMemoryModel Logical GLSL450
2415 %1 = OpTypeVoid
2416 %2 = OpTypeInt 32 0
2417 %3 = OpConstant %2 0
2418 %4 = OpTypeFunction %1
2419 %5 = OpTypeBool
2420 %6 = OpConstantTrue %5
2421 %7 = OpFunction %1 None %4
2422 %8 = OpLabel
2423 OpSelectionMerge %9 None
2424 OpSwitch %3 %10 0 %11 1 %12
2425 %10 = OpLabel
2426 OpBranch %9
2427 %12 = OpLabel
2428 OpBranch %11
2429 %11 = OpLabel
2430 OpBranch %9
2431 %9 = OpLabel
2432 OpReturn
2433 OpFunctionEnd
2434 )";
2435
2436 CompileSuccessfully(text);
2437 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
2438 EXPECT_THAT(
2439 getDiagnosticString(),
2440 HasSubstr("Case construct that targets 12[%12] has branches to the case "
2441 "construct that targets 11[%11], but does not immediately "
2442 "precede it in the OpSwitch's target list\n"
2443 " OpSwitch %uint_0 %10 0 %11 1 %12"));
2444 }
2445
2446 TEST_F(ValidateCFG, WrongOperandListThroughDefault) {
2447 std::string text = R"(
2448 OpCapability Shader
2449 OpCapability Linkage
2450 OpMemoryModel Logical GLSL450
2451 %1 = OpTypeVoid
2452 %2 = OpTypeInt 32 0
2453 %3 = OpConstant %2 0
2454 %4 = OpTypeFunction %1
2455 %5 = OpTypeBool
2456 %6 = OpConstantTrue %5
2457 %7 = OpFunction %1 None %4
2458 %8 = OpLabel
2459 OpSelectionMerge %9 None
2460 OpSwitch %3 %10 0 %11 1 %12
2461 %10 = OpLabel
2462 OpBranch %11
2463 %12 = OpLabel
2464 OpBranch %10
2465 %11 = OpLabel
2466 OpBranch %9
2467 %9 = OpLabel
2468 OpReturn
2469 OpFunctionEnd
2470 )";
2471
2472 CompileSuccessfully(text);
2473 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
2474 EXPECT_THAT(
2475 getDiagnosticString(),
2476 HasSubstr("Case construct that targets 12[%12] has branches to the case "
2477 "construct that targets 11[%11], but does not immediately "
2478 "precede it in the OpSwitch's target list\n"
2479 " OpSwitch %uint_0 %10 0 %11 1 %12"));
2480 }
2481
2482 TEST_F(ValidateCFG, WrongOperandListNotLast) {
2483 std::string text = R"(
2484 OpCapability Shader
2485 OpCapability Linkage
2486 OpMemoryModel Logical GLSL450
2487 %1 = OpTypeVoid
2488 %2 = OpTypeInt 32 0
2489 %3 = OpConstant %2 0
2490 %4 = OpTypeFunction %1
2491 %5 = OpTypeBool
2492 %6 = OpConstantTrue %5
2493 %7 = OpFunction %1 None %4
2494 %8 = OpLabel
2495 OpSelectionMerge %9 None
2496 OpSwitch %3 %10 0 %11 1 %12 2 %13
2497 %10 = OpLabel
2498 OpBranch %9
2499 %12 = OpLabel
2500 OpBranch %11
2501 %11 = OpLabel
2502 OpBranch %9
2503 %13 = OpLabel
2504 OpBranch %9
2505 %9 = OpLabel
2506 OpReturn
2507 OpFunctionEnd
2508 )";
2509
2510 CompileSuccessfully(text);
2511 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
2512 EXPECT_THAT(
2513 getDiagnosticString(),
2514 HasSubstr("Case construct that targets 12[%12] has branches to the case "
2515 "construct that targets 11[%11], but does not immediately "
2516 "precede it in the OpSwitch's target list\n"
2517 " OpSwitch %uint_0 %10 0 %11 1 %12 2 %13"));
2518 }
2519
2520 TEST_F(ValidateCFG, GoodUnreachableSwitch) {
2521 const std::string text = R"(
2522 OpCapability Shader
2523 OpMemoryModel Logical GLSL450
2524 OpEntryPoint Fragment %2 "main"
2525 OpExecutionMode %2 OriginUpperLeft
2526 %3 = OpTypeVoid
2527 %4 = OpTypeFunction %3
2528 %5 = OpTypeBool
2529 %6 = OpConstantTrue %5
2530 %7 = OpTypeInt 32 1
2531 %9 = OpConstant %7 0
2532 %2 = OpFunction %3 None %4
2533 %10 = OpLabel
2534 OpSelectionMerge %11 None
2535 OpBranchConditional %6 %12 %13
2536 %12 = OpLabel
2537 OpReturn
2538 %13 = OpLabel
2539 OpReturn
2540 %11 = OpLabel
2541 OpSelectionMerge %14 None
2542 OpSwitch %9 %14 0 %15
2543 %15 = OpLabel
2544 OpBranch %14
2545 %14 = OpLabel
2546 OpReturn
2547 OpFunctionEnd
2548 )";
2549
2550 CompileSuccessfully(text);
2551 EXPECT_THAT(SPV_SUCCESS, ValidateInstructions());
2552 }
2553
2554 TEST_F(ValidateCFG, InvalidCaseExit) {
2555 const std::string text = R"(
2556 OpCapability Shader
2557 OpMemoryModel Logical GLSL450
2558 OpEntryPoint Fragment %1 "func"
2559 OpExecutionMode %1 OriginUpperLeft
2560 %2 = OpTypeVoid
2561 %3 = OpTypeInt 32 0
2562 %4 = OpTypeFunction %2
2563 %5 = OpConstant %3 0
2564 %1 = OpFunction %2 None %4
2565 %6 = OpLabel
2566 OpSelectionMerge %7 None
2567 OpSwitch %5 %7 0 %8 1 %9
2568 %8 = OpLabel
2569 OpBranch %10
2570 %9 = OpLabel
2571 OpBranch %10
2572 %10 = OpLabel
2573 OpReturn
2574 %7 = OpLabel
2575 OpReturn
2576 OpFunctionEnd
2577 )";
2578
2579 CompileSuccessfully(text);
2580 ASSERT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
2581 EXPECT_THAT(getDiagnosticString(),
2582 HasSubstr("Case construct that targets 8[%8] has invalid branch "
2583 "to block 10[%10] (not another case construct, "
2584 "corresponding merge, outer loop merge or outer loop "
2585 "continue)"));
2586 }
2587
2588 TEST_F(ValidateCFG, GoodCaseExitsToOuterConstructs) {
2589 const std::string text = R"(
2590 OpCapability Shader
2591 OpMemoryModel Logical GLSL450
2592 OpEntryPoint Fragment %func "func"
2593 OpExecutionMode %func OriginUpperLeft
2594 %void = OpTypeVoid
2595 %bool = OpTypeBool
2596 %true = OpConstantTrue %bool
2597 %int = OpTypeInt 32 0
2598 %int0 = OpConstant %int 0
2599 %func_ty = OpTypeFunction %void
2600 %func = OpFunction %void None %func_ty
2601 %1 = OpLabel
2602 OpBranch %2
2603 %2 = OpLabel
2604 OpLoopMerge %7 %6 None
2605 OpBranch %3
2606 %3 = OpLabel
2607 OpSelectionMerge %5 None
2608 OpSwitch %int0 %5 0 %4
2609 %4 = OpLabel
2610 OpBranchConditional %true %6 %7
2611 %5 = OpLabel
2612 OpBranchConditional %true %6 %7
2613 %6 = OpLabel
2614 OpBranch %2
2615 %7 = OpLabel
2616 OpReturn
2617 OpFunctionEnd
2618 )";
2619
2620 CompileSuccessfully(text);
2621 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
2622 }
2623
2624 TEST_F(ValidateCFG, SwitchCaseOrderingBad1) {
2625 const std::string text = R"(
2626 OpCapability Shader
2627 OpCapability Linkage
2628 OpMemoryModel Logical GLSL450
2629 OpName %default "default"
2630 OpName %other "other"
2631 %void = OpTypeVoid
2632 %int = OpTypeInt 32 0
2633 %undef = OpUndef %int
2634 %void_fn = OpTypeFunction %void
2635 %func = OpFunction %void None %void_fn
2636 %entry = OpLabel
2637 OpSelectionMerge %merge None
2638 OpSwitch %undef %default 0 %other 1 %default
2639 %default = OpLabel
2640 OpBranch %other
2641 %other = OpLabel
2642 OpBranch %merge
2643 %merge = OpLabel
2644 OpReturn
2645 OpFunctionEnd
2646 )";
2647
2648 CompileSuccessfully(text);
2649 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
2650 EXPECT_THAT(
2651 getDiagnosticString(),
2652 HasSubstr("Case construct that targets 1[%default] has branches to the "
2653 "case construct that targets 2[%other], but does not "
2654 "immediately precede it in the OpSwitch's target list"));
2655 }
2656
2657 TEST_F(ValidateCFG, SwitchCaseOrderingBad2) {
2658 const std::string text = R"(
2659 OpCapability Shader
2660 OpCapability Linkage
2661 OpMemoryModel Logical GLSL450
2662 OpName %default "default"
2663 OpName %other "other"
2664 %void = OpTypeVoid
2665 %int = OpTypeInt 32 0
2666 %undef = OpUndef %int
2667 %void_fn = OpTypeFunction %void
2668 %func = OpFunction %void None %void_fn
2669 %entry = OpLabel
2670 OpSelectionMerge %merge None
2671 OpSwitch %undef %default 0 %default 1 %other
2672 %other = OpLabel
2673 OpBranch %default
2674 %default = OpLabel
2675 OpBranch %merge
2676 %merge = OpLabel
2677 OpReturn
2678 OpFunctionEnd
2679 )";
2680
2681 CompileSuccessfully(text);
2682 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
2683 EXPECT_THAT(
2684 getDiagnosticString(),
2685 HasSubstr("Case construct that targets 2[%other] has branches to the "
2686 "case construct that targets 1[%default], but does not "
2687 "immediately precede it in the OpSwitch's target list"));
2688 }
2689
2690 TEST_F(ValidateCFG, SwitchMultipleDefaultWithFallThroughGood) {
2691 const std::string text = R"(
2692 OpCapability Shader
2693 OpCapability Linkage
2694 OpMemoryModel Logical GLSL450
2695 OpName %first "first"
2696 OpName %second "second"
2697 OpName %third "third"
2698 %void = OpTypeVoid
2699 %int = OpTypeInt 32 0
2700 %undef = OpUndef %int
2701 %void_fn = OpTypeFunction %void
2702 %func = OpFunction %void None %void_fn
2703 %entry = OpLabel
2704 OpSelectionMerge %merge None
2705 OpSwitch %undef %second 0 %first 1 %second 2 %third
2706 %first = OpLabel
2707 OpBranch %second
2708 %second = OpLabel
2709 OpBranch %third
2710 %third = OpLabel
2711 OpBranch %merge
2712 %merge = OpLabel
2713 OpReturn
2714 OpFunctionEnd
2715 )";
2716
2717 CompileSuccessfully(text);
2718 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2719 }
2720
2721 TEST_F(ValidateCFG, SwitchMultipleDefaultWithFallThroughBad) {
2722 const std::string text = R"(
2723 OpCapability Shader
2724 OpCapability Linkage
2725 OpMemoryModel Logical GLSL450
2726 OpName %first "first"
2727 OpName %second "second"
2728 OpName %third "third"
2729 %void = OpTypeVoid
2730 %int = OpTypeInt 32 0
2731 %undef = OpUndef %int
2732 %void_fn = OpTypeFunction %void
2733 %func = OpFunction %void None %void_fn
2734 %entry = OpLabel
2735 OpSelectionMerge %merge None
2736 OpSwitch %undef %second 0 %second 1 %first 2 %third
2737 %first = OpLabel
2738 OpBranch %second
2739 %second = OpLabel
2740 OpBranch %third
2741 %third = OpLabel
2742 OpBranch %merge
2743 %merge = OpLabel
2744 OpReturn
2745 OpFunctionEnd
2746 )";
2747
2748 CompileSuccessfully(text);
2749 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
2750 }
2751
2752 TEST_F(ValidateCFG, GoodUnreachableSelection) {
2753 const std::string text = R"(
2754 OpCapability Shader
2755 %1 = OpExtInstImport "GLSL.std.450"
2756 OpMemoryModel Logical GLSL450
2757 OpEntryPoint Fragment %main "main"
2758 OpExecutionMode %main OriginUpperLeft
2759 %void = OpTypeVoid
2760 %8 = OpTypeFunction %void
2761 %bool = OpTypeBool
2762 %false = OpConstantFalse %bool
2763 %main = OpFunction %void None %8
2764 %15 = OpLabel
2765 OpBranch %16
2766 %16 = OpLabel
2767 OpLoopMerge %17 %18 None
2768 OpBranch %19
2769 %19 = OpLabel
2770 OpBranchConditional %false %21 %17
2771 %21 = OpLabel
2772 OpSelectionMerge %22 None
2773 OpBranchConditional %false %23 %22
2774 %23 = OpLabel
2775 OpBranch %24
2776 %24 = OpLabel
2777 OpLoopMerge %25 %26 None
2778 OpBranch %27
2779 %27 = OpLabel
2780 OpReturn
2781 %26 = OpLabel
2782 OpBranchConditional %false %24 %25
2783 %25 = OpLabel
2784 OpSelectionMerge %28 None
2785 OpBranchConditional %false %18 %28
2786 %28 = OpLabel
2787 OpBranch %22
2788 %22 = OpLabel
2789 OpBranch %18
2790 %18 = OpLabel
2791 OpBranch %16
2792 %17 = OpLabel
2793 OpReturn
2794 OpFunctionEnd
2795 )";
2796
2797 CompileSuccessfully(text);
2798 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
2799 }
2800
2801 TEST_F(ValidateCFG, ShaderWithPhiPtr) {
2802 const std::string text = R"(
2803 OpCapability Shader
2804 OpMemoryModel Logical GLSL450
2805 OpEntryPoint GLCompute %1 "main"
2806 OpExecutionMode %1 LocalSize 1 1 1
2807 OpSource HLSL 600
2808 %bool = OpTypeBool
2809 %_ptr_Function_bool = OpTypePointer Function %bool
2810 %void = OpTypeVoid
2811 %5 = OpTypeFunction %void
2812 %1 = OpFunction %void None %5
2813 %6 = OpLabel
2814 %7 = OpVariable %_ptr_Function_bool Function
2815 %8 = OpVariable %_ptr_Function_bool Function
2816 %9 = OpUndef %bool
2817 OpSelectionMerge %10 None
2818 OpBranchConditional %9 %11 %10
2819 %11 = OpLabel
2820 OpBranch %10
2821 %10 = OpLabel
2822 %12 = OpPhi %_ptr_Function_bool %7 %6 %8 %11
2823 OpReturn
2824 OpFunctionEnd
2825 )";
2826
2827 CompileSuccessfully(text);
2828 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
2829 EXPECT_THAT(getDiagnosticString(),
2830 HasSubstr("Using pointers with OpPhi requires capability "
2831 "VariablePointers or VariablePointersStorageBuffer"));
2832 }
2833
2834 TEST_F(ValidateCFG, VarPtrShaderWithPhiPtr) {
2835 const std::string text = R"(
2836 OpCapability Shader
2837 OpCapability VariablePointers
2838 OpExtension "SPV_KHR_variable_pointers"
2839 OpMemoryModel Logical GLSL450
2840 OpEntryPoint GLCompute %1 "main"
2841 OpExecutionMode %1 LocalSize 1 1 1
2842 OpSource HLSL 600
2843 %bool = OpTypeBool
2844 %_ptr_Function_bool = OpTypePointer Function %bool
2845 %void = OpTypeVoid
2846 %5 = OpTypeFunction %void
2847 %1 = OpFunction %void None %5
2848 %6 = OpLabel
2849 %7 = OpVariable %_ptr_Function_bool Function
2850 %8 = OpVariable %_ptr_Function_bool Function
2851 %9 = OpUndef %bool
2852 OpSelectionMerge %10 None
2853 OpBranchConditional %9 %11 %10
2854 %11 = OpLabel
2855 OpBranch %10
2856 %10 = OpLabel
2857 %12 = OpPhi %_ptr_Function_bool %7 %6 %8 %11
2858 OpReturn
2859 OpFunctionEnd
2860 )";
2861
2862 CompileSuccessfully(text);
2863 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2864 }
2865
2866 TEST_F(ValidateCFG, VarPtrStgBufShaderWithPhiStgBufPtr) {
2867 const std::string text = R"(
2868 OpCapability Shader
2869 OpCapability VariablePointersStorageBuffer
2870 OpExtension "SPV_KHR_variable_pointers"
2871 OpMemoryModel Logical GLSL450
2872 OpEntryPoint GLCompute %1 "main"
2873 OpExecutionMode %1 LocalSize 1 1 1
2874 OpSource HLSL 600
2875 %bool = OpTypeBool
2876 %float = OpTypeFloat 32
2877 %_ptr_StorageBuffer_float = OpTypePointer StorageBuffer %float
2878 %7 = OpVariable %_ptr_StorageBuffer_float StorageBuffer
2879 %8 = OpVariable %_ptr_StorageBuffer_float StorageBuffer
2880 %void = OpTypeVoid
2881 %5 = OpTypeFunction %void
2882 %1 = OpFunction %void None %5
2883 %6 = OpLabel
2884 %9 = OpUndef %bool
2885 OpSelectionMerge %10 None
2886 OpBranchConditional %9 %11 %10
2887 %11 = OpLabel
2888 OpBranch %10
2889 %10 = OpLabel
2890 %12 = OpPhi %_ptr_StorageBuffer_float %7 %6 %8 %11
2891 OpReturn
2892 OpFunctionEnd
2893 )";
2894
2895 CompileSuccessfully(text);
2896 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2897 }
2898
2899 TEST_F(ValidateCFG, KernelWithPhiPtr) {
2900 const std::string text = R"(
2901 OpCapability Kernel
2902 OpCapability Addresses
2903 OpMemoryModel Physical32 OpenCL
2904 OpEntryPoint Kernel %1 "main"
2905 OpExecutionMode %1 LocalSize 1 1 1
2906 OpSource HLSL 600
2907 %bool = OpTypeBool
2908 %_ptr_Function_bool = OpTypePointer Function %bool
2909 %void = OpTypeVoid
2910 %5 = OpTypeFunction %void
2911 %1 = OpFunction %void None %5
2912 %6 = OpLabel
2913 %7 = OpVariable %_ptr_Function_bool Function
2914 %8 = OpVariable %_ptr_Function_bool Function
2915 %9 = OpUndef %bool
2916 OpSelectionMerge %10 None
2917 OpBranchConditional %9 %11 %10
2918 %11 = OpLabel
2919 OpBranch %10
2920 %10 = OpLabel
2921 %12 = OpPhi %_ptr_Function_bool %7 %6 %8 %11
2922 OpReturn
2923 OpFunctionEnd
2924 )";
2925
2926 CompileSuccessfully(text);
2927 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2928 }
2929
2930 TEST_F(ValidateCFG, SwitchTargetMustBeLabel) {
2931 const std::string text = R"(
2932 OpCapability Shader
2933 OpMemoryModel Logical GLSL450
2934 OpEntryPoint GLCompute %1 "foo"
2935 %uint = OpTypeInt 32 0
2936 %uint_0 = OpConstant %uint 0
2937 %void = OpTypeVoid
2938 %5 = OpTypeFunction %void
2939 %1 = OpFunction %void None %5
2940 %6 = OpLabel
2941 %7 = OpCopyObject %uint %uint_0
2942 OpSelectionMerge %8 None
2943 OpSwitch %uint_0 %8 0 %7
2944 %8 = OpLabel
2945 OpReturn
2946 OpFunctionEnd
2947 )";
2948
2949 CompileSuccessfully(text);
2950 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2951 EXPECT_THAT(getDiagnosticString(),
2952 HasSubstr("'Target Label' operands for OpSwitch must "
2953 "be IDs of an OpLabel instruction"));
2954 }
2955
2956 TEST_F(ValidateCFG, BranchTargetMustBeLabel) {
2957 const std::string text = R"(
2958 OpCapability Shader
2959 OpMemoryModel Logical GLSL450
2960 OpEntryPoint GLCompute %1 "foo"
2961 %uint = OpTypeInt 32 0
2962 %uint_0 = OpConstant %uint 0
2963 %void = OpTypeVoid
2964 %5 = OpTypeFunction %void
2965 %1 = OpFunction %void None %5
2966 %2 = OpLabel
2967 %7 = OpCopyObject %uint %uint_0
2968 OpBranch %7
2969 %8 = OpLabel
2970 OpReturn
2971 OpFunctionEnd
2972 )";
2973
2974 CompileSuccessfully(text);
2975 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
2976 EXPECT_THAT(getDiagnosticString(),
2977 HasSubstr("'Target Label' operands for OpBranch must "
2978 "be the ID of an OpLabel instruction"));
2979 }
2980
2981 TEST_F(ValidateCFG, ReachableOpUnreachableOneBlock) {
2982 const std::string text = R"(
2983 OpCapability Shader
2984 OpCapability Linkage
2985 OpMemoryModel Logical GLSL450
2986 %void = OpTypeVoid
2987 %void_fn = OpTypeFunction %void
2988 %func = OpFunction %void None %void_fn
2989 %entry = OpLabel
2990 OpUnreachable
2991 OpFunctionEnd
2992 )";
2993
2994 CompileSuccessfully(text);
2995 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
2996 }
2997
2998 TEST_F(ValidateCFG, ReachableOpUnreachableOpBranch) {
2999 const std::string text = R"(
3000 OpCapability Shader
3001 OpCapability Linkage
3002 OpMemoryModel Logical GLSL450
3003 %void = OpTypeVoid
3004 %void_fn = OpTypeFunction %void
3005 %func = OpFunction %void None %void_fn
3006 %entry = OpLabel
3007 OpBranch %block
3008 %block = OpLabel
3009 OpUnreachable
3010 OpFunctionEnd
3011 )";
3012
3013 CompileSuccessfully(text);
3014 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3015 }
3016
3017 TEST_F(ValidateCFG, ReachableOpUnreachableOpBranchConditional) {
3018 const std::string text = R"(
3019 OpCapability Shader
3020 OpCapability Linkage
3021 OpMemoryModel Logical GLSL450
3022 %void = OpTypeVoid
3023 %void_fn = OpTypeFunction %void
3024 %bool = OpTypeBool
3025 %undef = OpUndef %bool
3026 %func = OpFunction %void None %void_fn
3027 %entry = OpLabel
3028 OpSelectionMerge %block None
3029 OpBranchConditional %undef %block %unreachable
3030 %block = OpLabel
3031 OpReturn
3032 %unreachable = OpLabel
3033 OpUnreachable
3034 OpFunctionEnd
3035 )";
3036
3037 CompileSuccessfully(text);
3038 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3039 }
3040
3041 TEST_F(ValidateCFG, ReachableOpUnreachableOpSwitch) {
3042 const std::string text = R"(
3043 OpCapability Shader
3044 OpCapability Linkage
3045 OpMemoryModel Logical GLSL450
3046 %void = OpTypeVoid
3047 %void_fn = OpTypeFunction %void
3048 %int = OpTypeInt 32 0
3049 %undef = OpUndef %int
3050 %func = OpFunction %void None %void_fn
3051 %entry = OpLabel
3052 OpSelectionMerge %block1 None
3053 OpSwitch %undef %block1 0 %unreachable 1 %block2
3054 %block1 = OpLabel
3055 OpReturn
3056 %unreachable = OpLabel
3057 OpUnreachable
3058 %block2 = OpLabel
3059 OpReturn
3060 OpFunctionEnd
3061 )";
3062
3063 CompileSuccessfully(text);
3064 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3065 }
3066
3067 TEST_F(ValidateCFG, ReachableOpUnreachableLoop) {
3068 const std::string text = R"(
3069 OpCapability Shader
3070 OpCapability Linkage
3071 OpMemoryModel Logical GLSL450
3072 %void = OpTypeVoid
3073 %void_fn = OpTypeFunction %void
3074 %bool = OpTypeBool
3075 %undef = OpUndef %bool
3076 %func = OpFunction %void None %void_fn
3077 %entry = OpLabel
3078 OpBranch %loop
3079 %loop = OpLabel
3080 OpLoopMerge %unreachable %loop None
3081 OpBranchConditional %undef %loop %unreachable
3082 %unreachable = OpLabel
3083 OpUnreachable
3084 OpFunctionEnd
3085 )";
3086
3087 CompileSuccessfully(text);
3088 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3089 }
3090
3091 TEST_F(ValidateCFG, UnreachableLoopBadBackedge) {
3092 const std::string text = R"(
3093 OpCapability Shader
3094 OpMemoryModel Logical GLSL450
3095 OpEntryPoint Fragment %2 "main"
3096 OpExecutionMode %2 OriginUpperLeft
3097 %4 = OpTypeVoid
3098 %5 = OpTypeFunction %4
3099 %8 = OpTypeBool
3100 %13 = OpConstantTrue %8
3101 %2 = OpFunction %4 None %5
3102 %14 = OpLabel
3103 OpSelectionMerge %15 None
3104 OpBranchConditional %13 %15 %15
3105 %16 = OpLabel
3106 OpLoopMerge %17 %18 None
3107 OpBranch %17
3108 %18 = OpLabel
3109 OpBranch %17
3110 %17 = OpLabel
3111 OpBranch %15
3112 %15 = OpLabel
3113 OpReturn
3114 OpFunctionEnd
3115 )";
3116
3117 // The back-edge in this test is bad, but the validator fails to identify it
3118 // because it is in an entirely unreachable section of code. Prior to #2488
3119 // this code failed an assert in Construct::blocks().
3120 CompileSuccessfully(text);
3121 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3122 }
3123
3124 TEST_F(ValidateCFG, OneContinueTwoBackedges) {
3125 const std::string text = R"(
3126 OpCapability Shader
3127 OpMemoryModel Logical GLSL450
3128 OpEntryPoint GLCompute %1 "main"
3129 OpExecutionMode %1 LocalSize 1 1 1
3130 %void = OpTypeVoid
3131 %bool = OpTypeBool
3132 %true = OpConstantTrue %bool
3133 %5 = OpTypeFunction %void
3134 %1 = OpFunction %void None %5
3135 %6 = OpLabel
3136 OpBranch %7
3137 %7 = OpLabel
3138 OpLoopMerge %8 %9 None
3139 OpBranch %10
3140 %10 = OpLabel
3141 OpLoopMerge %11 %9 None
3142 OpBranchConditional %true %11 %9
3143 %9 = OpLabel
3144 OpBranchConditional %true %10 %7
3145 %11 = OpLabel
3146 OpBranch %8
3147 %8 = OpLabel
3148 OpReturn
3149 OpFunctionEnd
3150 )";
3151
3152 CompileSuccessfully(text);
3153 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
3154 EXPECT_THAT(getDiagnosticString(),
3155 HasSubstr("block <ID> 9 branches to the loop construct, but not "
3156 "to the loop header <ID> 7"));
3157 }
3158
3159 TEST_F(ValidateCFG, LoopMergeMergeBlockNotLabel) {
3160 const std::string text = R"(
3161 OpCapability Shader
3162 OpCapability Linkage
3163 OpMemoryModel Logical GLSL450
3164 OpName %undef "undef"
3165 %void = OpTypeVoid
3166 %bool = OpTypeBool
3167 %undef = OpUndef %bool
3168 %void_fn = OpTypeFunction %void
3169 %func = OpFunction %void None %void_fn
3170 %1 = OpLabel
3171 OpLoopMerge %undef %2 None
3172 OpBranchConditional %undef %2 %2
3173 %2 = OpLabel
3174 OpReturn
3175 OpFunctionEnd
3176 )";
3177
3178 CompileSuccessfully(text);
3179 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3180 EXPECT_THAT(getDiagnosticString(),
3181 HasSubstr("Merge Block 1[%undef] must be an OpLabel"));
3182 }
3183
3184 TEST_F(ValidateCFG, LoopMergeContinueTargetNotLabel) {
3185 const std::string text = R"(
3186 OpCapability Shader
3187 OpCapability Linkage
3188 OpMemoryModel Logical GLSL450
3189 OpName %undef "undef"
3190 %void = OpTypeVoid
3191 %bool = OpTypeBool
3192 %undef = OpUndef %bool
3193 %void_fn = OpTypeFunction %void
3194 %func = OpFunction %void None %void_fn
3195 %1 = OpLabel
3196 OpLoopMerge %2 %undef None
3197 OpBranchConditional %undef %2 %2
3198 %2 = OpLabel
3199 OpReturn
3200 OpFunctionEnd
3201 )";
3202
3203 CompileSuccessfully(text);
3204 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3205 EXPECT_THAT(getDiagnosticString(),
3206 HasSubstr("Continue Target 1[%undef] must be an OpLabel"));
3207 }
3208
3209 TEST_F(ValidateCFG, LoopMergeMergeBlockContinueTargetSameLabel) {
3210 const std::string text = R"(
3211 OpCapability Shader
3212 OpCapability Linkage
3213 OpMemoryModel Logical GLSL450
3214 OpName %undef "undef"
3215 %void = OpTypeVoid
3216 %bool = OpTypeBool
3217 %undef = OpUndef %bool
3218 %void_fn = OpTypeFunction %void
3219 %func = OpFunction %void None %void_fn
3220 %1 = OpLabel
3221 OpLoopMerge %2 %2 None
3222 OpBranchConditional %undef %2 %2
3223 %2 = OpLabel
3224 OpReturn
3225 OpFunctionEnd
3226 )";
3227
3228 CompileSuccessfully(text);
3229 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3230 EXPECT_THAT(
3231 getDiagnosticString(),
3232 HasSubstr("Merge Block and Continue Target must be different ids"));
3233 }
3234
3235 TEST_F(ValidateCFG, LoopMergeUnrollAndDontUnroll) {
3236 const std::string text = R"(
3237 OpCapability Shader
3238 OpCapability Linkage
3239 OpMemoryModel Logical GLSL450
3240 OpName %undef "undef"
3241 %void = OpTypeVoid
3242 %bool = OpTypeBool
3243 %undef = OpUndef %bool
3244 %void_fn = OpTypeFunction %void
3245 %func = OpFunction %void None %void_fn
3246 %5 = OpLabel
3247 OpBranch %1
3248 %1 = OpLabel
3249 OpLoopMerge %2 %3 Unroll|DontUnroll
3250 OpBranchConditional %undef %2 %3
3251 %3 = OpLabel
3252 OpBranch %1
3253 %2 = OpLabel
3254 OpReturn
3255 OpFunctionEnd
3256 )";
3257
3258 CompileSuccessfully(text);
3259 EXPECT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
3260 EXPECT_THAT(
3261 getDiagnosticString(),
3262 HasSubstr(
3263 "Unroll and DontUnroll loop controls must not both be specified"));
3264 }
3265
3266 TEST_F(ValidateCFG, LoopMergePeelCountAndDontUnroll) {
3267 const std::string text = R"(
3268 OpCapability Shader
3269 OpCapability Linkage
3270 OpMemoryModel Logical GLSL450
3271 OpName %undef "undef"
3272 %void = OpTypeVoid
3273 %bool = OpTypeBool
3274 %undef = OpUndef %bool
3275 %void_fn = OpTypeFunction %void
3276 %func = OpFunction %void None %void_fn
3277 %5 = OpLabel
3278 OpBranch %1
3279 %1 = OpLabel
3280 OpLoopMerge %2 %3 DontUnroll|PeelCount 1
3281 OpBranchConditional %undef %2 %3
3282 %3 = OpLabel
3283 OpBranch %1
3284 %2 = OpLabel
3285 OpReturn
3286 OpFunctionEnd
3287 )";
3288
3289 CompileSuccessfully(text, SPV_ENV_UNIVERSAL_1_4);
3290 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
3291 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
3292 EXPECT_THAT(
3293 getDiagnosticString(),
3294 HasSubstr(
3295 "PeelCount and DontUnroll loop controls must not both be specified"));
3296 }
3297
3298 TEST_F(ValidateCFG, LoopMergePartialCountAndDontUnroll) {
3299 const std::string text = R"(
3300 OpCapability Shader
3301 OpCapability Linkage
3302 OpMemoryModel Logical GLSL450
3303 OpName %undef "undef"
3304 %void = OpTypeVoid
3305 %bool = OpTypeBool
3306 %undef = OpUndef %bool
3307 %void_fn = OpTypeFunction %void
3308 %func = OpFunction %void None %void_fn
3309 %5 = OpLabel
3310 OpBranch %1
3311 %1 = OpLabel
3312 OpLoopMerge %2 %3 DontUnroll|PartialCount 1
3313 OpBranchConditional %undef %2 %3
3314 %3 = OpLabel
3315 OpBranch %1
3316 %2 = OpLabel
3317 OpReturn
3318 OpFunctionEnd
3319 )";
3320
3321 CompileSuccessfully(text, SPV_ENV_UNIVERSAL_1_4);
3322 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
3323 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
3324 EXPECT_THAT(getDiagnosticString(),
3325 HasSubstr("PartialCount and DontUnroll loop controls must not "
3326 "both be specified"));
3327 }
3328
3329 TEST_F(ValidateCFG, LoopMergeIterationMultipleZero) {
3330 const std::string text = R"(
3331 OpCapability Shader
3332 OpCapability Linkage
3333 OpMemoryModel Logical GLSL450
3334 OpName %undef "undef"
3335 %void = OpTypeVoid
3336 %bool = OpTypeBool
3337 %undef = OpUndef %bool
3338 %void_fn = OpTypeFunction %void
3339 %func = OpFunction %void None %void_fn
3340 %5 = OpLabel
3341 OpBranch %1
3342 %1 = OpLabel
3343 OpLoopMerge %2 %3 IterationMultiple 0
3344 OpBranchConditional %undef %2 %3
3345 %3 = OpLabel
3346 OpBranch %1
3347 %2 = OpLabel
3348 OpReturn
3349 OpFunctionEnd
3350 )";
3351
3352 CompileSuccessfully(text, SPV_ENV_UNIVERSAL_1_4);
3353 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
3354 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
3355 EXPECT_THAT(
3356 getDiagnosticString(),
3357 HasSubstr(
3358 "IterationMultiple loop control operand must be greater than zero"));
3359 }
3360
3361 TEST_F(ValidateCFG, LoopMergeIterationMultipleZeroMoreOperands) {
3362 const std::string text = R"(
3363 OpCapability Shader
3364 OpCapability Linkage
3365 OpMemoryModel Logical GLSL450
3366 OpName %undef "undef"
3367 %void = OpTypeVoid
3368 %bool = OpTypeBool
3369 %undef = OpUndef %bool
3370 %void_fn = OpTypeFunction %void
3371 %func = OpFunction %void None %void_fn
3372 %5 = OpLabel
3373 OpBranch %1
3374 %1 = OpLabel
3375 OpLoopMerge %2 %3 MaxIterations|IterationMultiple 4 0
3376 OpBranchConditional %undef %2 %3
3377 %3 = OpLabel
3378 OpBranch %1
3379 %2 = OpLabel
3380 OpReturn
3381 OpFunctionEnd
3382 )";
3383
3384 CompileSuccessfully(text, SPV_ENV_UNIVERSAL_1_4);
3385 EXPECT_EQ(SPV_ERROR_INVALID_DATA,
3386 ValidateInstructions(SPV_ENV_UNIVERSAL_1_4));
3387 EXPECT_THAT(
3388 getDiagnosticString(),
3389 HasSubstr(
3390 "IterationMultiple loop control operand must be greater than zero"));
3391 }
3392
3393 TEST_F(ValidateCFG, LoopMergeTargetsHeader) {
3394 const std::string text = R"(
3395 OpCapability Shader
3396 OpCapability Linkage
3397 OpMemoryModel Logical GLSL450
3398 %void = OpTypeVoid
3399 %bool = OpTypeBool
3400 %undef = OpUndef %bool
3401 %void_fn = OpTypeFunction %void
3402 %fn = OpFunction %void None %void_fn
3403 %entry = OpLabel
3404 OpBranch %loop
3405 %loop = OpLabel
3406 OpLoopMerge %loop %continue None
3407 OpBranch %body
3408 %continue = OpLabel
3409 OpBranch %loop
3410 %body = OpLabel
3411 OpReturn
3412 OpFunctionEnd
3413 )";
3414
3415 CompileSuccessfully(text);
3416 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
3417 EXPECT_THAT(
3418 getDiagnosticString(),
3419 HasSubstr("Merge Block may not be the block containing the OpLoopMerge"));
3420 }
3421
3422 TEST_F(ValidateCFG, InvalidSelectionExit) {
3423 const std::string text = R"(
3424 OpCapability Shader
3425 OpMemoryModel Logical GLSL450
3426 OpEntryPoint Fragment %1 "main"
3427 OpExecutionMode %1 OriginUpperLeft
3428 %2 = OpTypeVoid
3429 %3 = OpTypeBool
3430 %4 = OpConstantTrue %3
3431 %5 = OpTypeFunction %2
3432 %1 = OpFunction %2 None %5
3433 %6 = OpLabel
3434 OpSelectionMerge %7 None
3435 OpBranchConditional %4 %7 %8
3436 %8 = OpLabel
3437 OpSelectionMerge %9 None
3438 OpBranchConditional %4 %10 %9
3439 %10 = OpLabel
3440 OpBranch %7
3441 %9 = OpLabel
3442 OpBranch %7
3443 %7 = OpLabel
3444 OpReturn
3445 OpFunctionEnd
3446 )";
3447
3448 CompileSuccessfully(text);
3449 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
3450 EXPECT_THAT(getDiagnosticString(),
3451 HasSubstr("block <ID> 10[%10] exits the selection headed by <ID> "
3452 "8[%8], but not via a structured exit"));
3453 }
3454
3455 TEST_F(ValidateCFG, InvalidLoopExit) {
3456 const std::string text = R"(
3457 OpCapability Shader
3458 OpMemoryModel Logical GLSL450
3459 OpEntryPoint Fragment %1 "main"
3460 OpExecutionMode %1 OriginUpperLeft
3461 %2 = OpTypeVoid
3462 %3 = OpTypeBool
3463 %4 = OpConstantTrue %3
3464 %5 = OpTypeFunction %2
3465 %1 = OpFunction %2 None %5
3466 %6 = OpLabel
3467 OpSelectionMerge %7 None
3468 OpBranchConditional %4 %7 %8
3469 %8 = OpLabel
3470 OpLoopMerge %9 %10 None
3471 OpBranchConditional %4 %9 %11
3472 %11 = OpLabel
3473 OpBranchConditional %4 %7 %10
3474 %10 = OpLabel
3475 OpBranch %8
3476 %9 = OpLabel
3477 OpBranch %7
3478 %7 = OpLabel
3479 OpReturn
3480 OpFunctionEnd
3481 )";
3482
3483 CompileSuccessfully(text);
3484 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
3485 EXPECT_THAT(getDiagnosticString(),
3486 HasSubstr("block <ID> 11[%11] exits the loop headed by <ID> "
3487 "8[%8], but not via a structured exit"));
3488 }
3489
3490 TEST_F(ValidateCFG, InvalidContinueExit) {
3491 const std::string text = R"(
3492 OpCapability Shader
3493 OpMemoryModel Logical GLSL450
3494 OpEntryPoint Fragment %1 "main"
3495 OpExecutionMode %1 OriginUpperLeft
3496 %2 = OpTypeVoid
3497 %3 = OpTypeBool
3498 %4 = OpConstantTrue %3
3499 %5 = OpTypeFunction %2
3500 %1 = OpFunction %2 None %5
3501 %6 = OpLabel
3502 OpSelectionMerge %7 None
3503 OpBranchConditional %4 %7 %8
3504 %8 = OpLabel
3505 OpLoopMerge %9 %10 None
3506 OpBranchConditional %4 %9 %10
3507 %10 = OpLabel
3508 OpBranch %11
3509 %11 = OpLabel
3510 OpBranchConditional %4 %8 %7
3511 %9 = OpLabel
3512 OpBranch %7
3513 %7 = OpLabel
3514 OpReturn
3515 OpFunctionEnd
3516 )";
3517
3518 CompileSuccessfully(text);
3519 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
3520 EXPECT_THAT(getDiagnosticString(),
3521 HasSubstr("block <ID> 11[%11] exits the continue headed by <ID> "
3522 "10[%10], but not via a structured exit"));
3523 }
3524
3525 TEST_F(ValidateCFG, InvalidSelectionExitBackedge) {
3526 const std::string text = R"(
3527 OpCapability Shader
3528 OpCapability Linkage
3529 OpMemoryModel Logical GLSL450
3530 %1 = OpTypeVoid
3531 %2 = OpTypeBool
3532 %3 = OpUndef %2
3533 %4 = OpTypeFunction %1
3534 %5 = OpFunction %1 None %4
3535 %6 = OpLabel
3536 OpBranch %7
3537 %7 = OpLabel
3538 OpLoopMerge %8 %9 None
3539 OpBranchConditional %3 %8 %9
3540 %9 = OpLabel
3541 OpSelectionMerge %10 None
3542 OpBranchConditional %3 %11 %12
3543 %11 = OpLabel
3544 OpBranch %13
3545 %12 = OpLabel
3546 OpBranch %13
3547 %13 = OpLabel
3548 OpBranch %7
3549 %10 = OpLabel
3550 OpUnreachable
3551 %8 = OpLabel
3552 OpReturn
3553 OpFunctionEnd
3554 )";
3555
3556 CompileSuccessfully(text);
3557 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
3558 EXPECT_THAT(getDiagnosticString(),
3559 HasSubstr("block <ID> 13[%13] exits the selection headed by <ID> "
3560 "9[%9], but not via a structured exit"));
3561 }
3562
3563 TEST_F(ValidateCFG, BreakFromSwitch) {
3564 const std::string text = R"(
3565 OpCapability Shader
3566 OpCapability Linkage
3567 OpMemoryModel Logical GLSL450
3568 %1 = OpTypeVoid
3569 %2 = OpTypeBool
3570 %3 = OpTypeInt 32 0
3571 %4 = OpUndef %2
3572 %5 = OpUndef %3
3573 %6 = OpTypeFunction %1
3574 %7 = OpFunction %1 None %6
3575 %8 = OpLabel
3576 OpSelectionMerge %9 None
3577 OpSwitch %5 %9 0 %10
3578 %10 = OpLabel
3579 OpSelectionMerge %11 None
3580 OpBranchConditional %4 %11 %12
3581 %12 = OpLabel
3582 OpBranch %9
3583 %11 = OpLabel
3584 OpBranch %9
3585 %9 = OpLabel
3586 OpReturn
3587 OpFunctionEnd
3588 )";
3589
3590 CompileSuccessfully(text);
3591 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3592 }
3593
3594 TEST_F(ValidateCFG, InvalidBreakFromSwitch) {
3595 const std::string text = R"(
3596 OpCapability Shader
3597 OpCapability Linkage
3598 OpMemoryModel Logical GLSL450
3599 %1 = OpTypeVoid
3600 %2 = OpTypeBool
3601 %3 = OpTypeInt 32 0
3602 %4 = OpUndef %2
3603 %5 = OpUndef %3
3604 %6 = OpTypeFunction %1
3605 %7 = OpFunction %1 None %6
3606 %8 = OpLabel
3607 OpSelectionMerge %9 None
3608 OpSwitch %5 %9 0 %10
3609 %10 = OpLabel
3610 OpSelectionMerge %11 None
3611 OpSwitch %5 %11 0 %12
3612 %12 = OpLabel
3613 OpBranch %9
3614 %11 = OpLabel
3615 OpBranch %9
3616 %9 = OpLabel
3617 OpReturn
3618 OpFunctionEnd
3619 )";
3620
3621 CompileSuccessfully(text);
3622 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
3623 EXPECT_THAT(getDiagnosticString(),
3624 HasSubstr("block <ID> 12[%12] exits the selection headed by <ID> "
3625 "10[%10], but not via a structured exit"));
3626 }
3627
3628 TEST_F(ValidateCFG, BreakToOuterSwitch) {
3629 const std::string text = R"(
3630 OpCapability Shader
3631 OpCapability Linkage
3632 OpMemoryModel Logical GLSL450
3633 %1 = OpTypeVoid
3634 %2 = OpTypeBool
3635 %3 = OpTypeInt 32 0
3636 %4 = OpUndef %2
3637 %5 = OpUndef %3
3638 %6 = OpTypeFunction %1
3639 %7 = OpFunction %1 None %6
3640 %8 = OpLabel
3641 OpSelectionMerge %9 None
3642 OpSwitch %5 %9 0 %10
3643 %10 = OpLabel
3644 OpSelectionMerge %11 None
3645 OpSwitch %5 %11 0 %12
3646 %12 = OpLabel
3647 OpSelectionMerge %13 None
3648 OpBranchConditional %4 %13 %14
3649 %14 = OpLabel
3650 OpBranch %9
3651 %13 = OpLabel
3652 OpBranch %11
3653 %11 = OpLabel
3654 OpBranch %9
3655 %9 = OpLabel
3656 OpReturn
3657 OpFunctionEnd
3658 )";
3659
3660 CompileSuccessfully(text);
3661 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
3662 EXPECT_THAT(getDiagnosticString(),
3663 HasSubstr("block <ID> 14[%14] exits the selection headed by <ID> "
3664 "10[%10], but not via a structured exit"));
3665 }
3666
3667 TEST_F(ValidateCFG, BreakToOuterLoop) {
3668 const std::string text = R"(
3669 OpCapability Shader
3670 OpCapability Linkage
3671 OpMemoryModel Logical GLSL450
3672 %1 = OpTypeVoid
3673 %2 = OpTypeBool
3674 %3 = OpUndef %2
3675 %4 = OpTypeFunction %1
3676 %5 = OpFunction %1 None %4
3677 %6 = OpLabel
3678 OpBranch %7
3679 %7 = OpLabel
3680 OpLoopMerge %8 %9 None
3681 OpBranch %10
3682 %10 = OpLabel
3683 OpLoopMerge %11 %12 None
3684 OpBranch %13
3685 %13 = OpLabel
3686 OpSelectionMerge %14 None
3687 OpBranchConditional %3 %14 %15
3688 %15 = OpLabel
3689 OpBranch %8
3690 %14 = OpLabel
3691 OpBranch %12
3692 %12 = OpLabel
3693 OpBranchConditional %3 %10 %11
3694 %11 = OpLabel
3695 OpBranch %9
3696 %9 = OpLabel
3697 OpBranchConditional %3 %7 %8
3698 %8 = OpLabel
3699 OpReturn
3700 OpFunctionEnd
3701 )";
3702
3703 CompileSuccessfully(text);
3704 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
3705 EXPECT_THAT(getDiagnosticString(),
3706 HasSubstr("block <ID> 15[%15] exits the loop headed by <ID> "
3707 "10[%10], but not via a structured exit"));
3708 }
3709
3710 TEST_F(ValidateCFG, ContinueFromNestedSelection) {
3711 const std::string text = R"(
3712 OpCapability Shader
3713 OpCapability Linkage
3714 OpMemoryModel Logical GLSL450
3715 %void = OpTypeVoid
3716 %void_fn = OpTypeFunction %void
3717 %bool = OpTypeBool
3718 %undef = OpUndef %bool
3719 %4 = OpFunction %void None %void_fn
3720 %5 = OpLabel
3721 OpBranch %48
3722 %48 = OpLabel
3723 OpLoopMerge %47 %50 None
3724 OpBranch %10
3725 %10 = OpLabel
3726 OpLoopMerge %12 %37 None
3727 OpBranchConditional %undef %11 %12
3728 %11 = OpLabel
3729 OpSelectionMerge %31 None
3730 OpBranchConditional %undef %30 %31
3731 %30 = OpLabel
3732 OpSelectionMerge %38 None
3733 OpBranchConditional %undef %36 %38
3734 %36 = OpLabel
3735 OpBranch %38
3736 %38 = OpLabel
3737 OpBranch %37
3738 %37 = OpLabel
3739 OpBranch %10
3740 %31 = OpLabel
3741 OpBranch %12
3742 %12 = OpLabel
3743 OpSelectionMerge %55 None
3744 OpBranchConditional %undef %47 %55
3745 %55 = OpLabel
3746 OpBranch %47
3747 %50 = OpLabel
3748 OpBranch %48
3749 %47 = OpLabel
3750 OpReturn
3751 OpFunctionEnd
3752 )";
3753
3754 CompileSuccessfully(text);
3755 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3756 }
3757
3758 TEST_F(ValidateCFG, MissingMergeConditionalBranchBad) {
3759 const std::string text = R"(
3760 OpCapability Shader
3761 OpCapability Linkage
3762 OpMemoryModel Logical GLSL450
3763 %void = OpTypeVoid
3764 %void_fn = OpTypeFunction %void
3765 %bool = OpTypeBool
3766 %undef = OpUndef %bool
3767 %func = OpFunction %void None %void_fn
3768 %entry = OpLabel
3769 OpBranchConditional %undef %then %else
3770 %then = OpLabel
3771 OpReturn
3772 %else = OpLabel
3773 OpReturn
3774 OpFunctionEnd
3775 )";
3776
3777 CompileSuccessfully(text);
3778 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
3779 EXPECT_THAT(getDiagnosticString(), HasSubstr("Selection must be structured"));
3780 }
3781
3782 TEST_F(ValidateCFG, MissingMergeSwitchBad) {
3783 const std::string text = R"(
3784 OpCapability Shader
3785 OpCapability Linkage
3786 OpMemoryModel Logical GLSL450
3787 %void = OpTypeVoid
3788 %void_fn = OpTypeFunction %void
3789 %int = OpTypeInt 32 0
3790 %undef = OpUndef %int
3791 %func = OpFunction %void None %void_fn
3792 %entry = OpLabel
3793 OpSwitch %undef %then 0 %else
3794 %then = OpLabel
3795 OpReturn
3796 %else = OpLabel
3797 OpReturn
3798 OpFunctionEnd
3799 )";
3800
3801 CompileSuccessfully(text);
3802 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
3803 EXPECT_THAT(getDiagnosticString(), HasSubstr("Selection must be structured"));
3804 }
3805
3806 TEST_F(ValidateCFG, MissingMergeSwitchBad2) {
3807 const std::string text = R"(
3808 OpCapability Shader
3809 OpCapability Linkage
3810 OpMemoryModel Logical GLSL450
3811 %void = OpTypeVoid
3812 %void_fn = OpTypeFunction %void
3813 %int = OpTypeInt 32 0
3814 %undef = OpUndef %int
3815 %func = OpFunction %void None %void_fn
3816 %entry = OpLabel
3817 OpSwitch %undef %then 0 %then 1 %then 2 %else
3818 %then = OpLabel
3819 OpReturn
3820 %else = OpLabel
3821 OpReturn
3822 OpFunctionEnd
3823 )";
3824
3825 CompileSuccessfully(text);
3826 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
3827 EXPECT_THAT(getDiagnosticString(), HasSubstr("Selection must be structured"));
3828 }
3829
3830 TEST_F(ValidateCFG, MissingMergeOneBranchToMergeGood) {
3831 const std::string text = R"(
3832 OpCapability Shader
3833 OpCapability Linkage
3834 OpMemoryModel Logical GLSL450
3835 %void = OpTypeVoid
3836 %void_fn = OpTypeFunction %void
3837 %bool = OpTypeBool
3838 %undef = OpUndef %bool
3839 %func = OpFunction %void None %void_fn
3840 %entry = OpLabel
3841 OpSelectionMerge %b3 None
3842 OpBranchConditional %undef %b1 %b2
3843 %b1 = OpLabel
3844 OpBranchConditional %undef %b2 %b3
3845 %b2 = OpLabel
3846 OpBranch %b3
3847 %b3 = OpLabel
3848 OpReturn
3849 OpFunctionEnd
3850 )";
3851
3852 CompileSuccessfully(text);
3853 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3854 }
3855
3856 TEST_F(ValidateCFG, MissingMergeSameTargetConditionalBranchGood) {
3857 const std::string text = R"(
3858 OpCapability Shader
3859 OpCapability Linkage
3860 OpMemoryModel Logical GLSL450
3861 %void = OpTypeVoid
3862 %void_fn = OpTypeFunction %void
3863 %bool = OpTypeBool
3864 %undef = OpUndef %bool
3865 %func = OpFunction %void None %void_fn
3866 %entry = OpLabel
3867 OpBranchConditional %undef %then %then
3868 %then = OpLabel
3869 OpReturn
3870 OpFunctionEnd
3871 )";
3872
3873 CompileSuccessfully(text);
3874 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3875 }
3876
3877 TEST_F(ValidateCFG, MissingMergeOneTargetSwitchGood) {
3878 const std::string text = R"(
3879 OpCapability Shader
3880 OpCapability Linkage
3881 OpMemoryModel Logical GLSL450
3882 %void = OpTypeVoid
3883 %void_fn = OpTypeFunction %void
3884 %int = OpTypeInt 32 0
3885 %undef = OpUndef %int
3886 %func = OpFunction %void None %void_fn
3887 %entry = OpLabel
3888 OpSwitch %undef %then 0 %then 1 %then
3889 %then = OpLabel
3890 OpReturn
3891 OpFunctionEnd
3892 )";
3893
3894 CompileSuccessfully(text);
3895 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3896 }
3897
3898 TEST_F(ValidateCFG, MissingMergeOneUnseenTargetSwitchGood) {
3899 const std::string text = R"(
3900 OpCapability Shader
3901 OpCapability Linkage
3902 OpMemoryModel Logical GLSL450
3903 %void = OpTypeVoid
3904 %void_fn = OpTypeFunction %void
3905 %int = OpTypeInt 32 0
3906 %undef_int = OpUndef %int
3907 %bool = OpTypeBool
3908 %undef_bool = OpUndef %bool
3909 %func = OpFunction %void None %void_fn
3910 %entry = OpLabel
3911 OpSelectionMerge %merge None
3912 OpBranchConditional %undef_bool %merge %b1
3913 %b1 = OpLabel
3914 OpSwitch %undef_int %b2 0 %b2 1 %merge 2 %b2
3915 %b2 = OpLabel
3916 OpBranch %merge
3917 %merge = OpLabel
3918 OpReturn
3919 OpFunctionEnd
3920 )";
3921
3922 CompileSuccessfully(text);
3923 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3924 }
3925
3926 TEST_F(ValidateCFG, MissingMergeLoopBreakGood) {
3927 const std::string text = R"(
3928 OpCapability Shader
3929 OpCapability Linkage
3930 OpMemoryModel Logical GLSL450
3931 %void = OpTypeVoid
3932 %void_fn = OpTypeFunction %void
3933 %bool = OpTypeBool
3934 %undef = OpUndef %bool
3935 %func = OpFunction %void None %void_fn
3936 %entry = OpLabel
3937 OpBranch %loop
3938 %loop = OpLabel
3939 OpLoopMerge %exit %continue None
3940 OpBranch %body
3941 %body = OpLabel
3942 OpBranchConditional %undef %body2 %exit
3943 %body2 = OpLabel
3944 OpBranch %continue
3945 %continue = OpLabel
3946 OpBranch %loop
3947 %exit = OpLabel
3948 OpReturn
3949 OpFunctionEnd
3950 )";
3951
3952 CompileSuccessfully(text);
3953 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3954 }
3955
3956 TEST_F(ValidateCFG, MissingMergeLoopContinueGood) {
3957 const std::string text = R"(
3958 OpCapability Shader
3959 OpCapability Linkage
3960 OpMemoryModel Logical GLSL450
3961 %void = OpTypeVoid
3962 %void_fn = OpTypeFunction %void
3963 %bool = OpTypeBool
3964 %undef = OpUndef %bool
3965 %func = OpFunction %void None %void_fn
3966 %entry = OpLabel
3967 OpBranch %loop
3968 %loop = OpLabel
3969 OpLoopMerge %exit %continue None
3970 OpBranch %body
3971 %body = OpLabel
3972 OpBranchConditional %undef %body2 %continue
3973 %body2 = OpLabel
3974 OpBranch %continue
3975 %continue = OpLabel
3976 OpBranch %loop
3977 %exit = OpLabel
3978 OpReturn
3979 OpFunctionEnd
3980 )";
3981
3982 CompileSuccessfully(text);
3983 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
3984 }
3985
3986 TEST_F(ValidateCFG, MissingMergeSwitchBreakGood) {
3987 const std::string text = R"(
3988 OpCapability Shader
3989 OpCapability Linkage
3990 OpMemoryModel Logical GLSL450
3991 %void = OpTypeVoid
3992 %void_fn = OpTypeFunction %void
3993 %bool = OpTypeBool
3994 %undef = OpUndef %bool
3995 %int = OpTypeInt 32 0
3996 %int_0 = OpConstant %int 0
3997 %func = OpFunction %void None %void_fn
3998 %entry = OpLabel
3999 OpSelectionMerge %merge None
4000 OpSwitch %int_0 %merge 1 %b1
4001 %b1 = OpLabel
4002 OpBranchConditional %undef %merge %b2
4003 %b2 = OpLabel
4004 OpBranch %merge
4005 %merge = OpLabel
4006 OpReturn
4007 OpFunctionEnd
4008 )";
4009
4010 CompileSuccessfully(text);
4011 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4012 }
4013
4014 TEST_F(ValidateCFG, MissingMergeSwitchFallThroughGood) {
4015 const std::string text = R"(
4016 OpCapability Shader
4017 OpCapability Linkage
4018 OpMemoryModel Logical GLSL450
4019 %void = OpTypeVoid
4020 %void_fn = OpTypeFunction %void
4021 %bool = OpTypeBool
4022 %undef = OpUndef %bool
4023 %int = OpTypeInt 32 0
4024 %int_0 = OpConstant %int 0
4025 %func = OpFunction %void None %void_fn
4026 %entry = OpLabel
4027 OpSelectionMerge %merge None
4028 OpSwitch %int_0 %b1 1 %b2
4029 %b1 = OpLabel
4030 OpBranchConditional %undef %b3 %b2
4031 %b2 = OpLabel
4032 OpBranch %merge
4033 %b3 = OpLabel
4034 OpBranch %merge
4035 %merge = OpLabel
4036 OpReturn
4037 OpFunctionEnd
4038 )";
4039
4040 CompileSuccessfully(text);
4041 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4042 }
4043
4044 TEST_F(ValidateCFG, MissingMergeInALoopBad) {
4045 const std::string text = R"(
4046 OpCapability Shader
4047 OpCapability Linkage
4048 OpMemoryModel Logical GLSL450
4049 %void = OpTypeVoid
4050 %void_fn = OpTypeFunction %void
4051 %bool = OpTypeBool
4052 %undef = OpUndef %bool
4053 %func = OpFunction %void None %void_fn
4054 %entry = OpLabel
4055 OpBranch %loop
4056 %loop = OpLabel
4057 OpLoopMerge %exit %continue None
4058 OpBranch %body
4059 %body = OpLabel
4060 OpBranchConditional %undef %b1 %b2
4061 %b1 = OpLabel
4062 OpBranch %exit
4063 %b2 = OpLabel
4064 OpBranch %continue
4065 %continue = OpLabel
4066 OpBranch %loop
4067 %exit = OpLabel
4068 OpReturn
4069 OpFunctionEnd
4070 )";
4071
4072 CompileSuccessfully(text);
4073 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
4074 EXPECT_THAT(getDiagnosticString(), HasSubstr("Selection must be structured"));
4075 }
4076
4077 TEST_F(ValidateCFG, MissingMergeCrissCrossBad) {
4078 const std::string text = R"(
4079 OpCapability Shader
4080 OpCapability Linkage
4081 OpMemoryModel Logical GLSL450
4082 %void = OpTypeVoid
4083 %void_fn = OpTypeFunction %void
4084 %bool = OpTypeBool
4085 %undef = OpUndef %bool
4086 %func = OpFunction %void None %void_fn
4087 %entry = OpLabel
4088 OpSelectionMerge %merge None
4089 OpBranchConditional %undef %b1 %b2
4090 %b1 = OpLabel
4091 OpBranchConditional %undef %b3 %b4
4092 %b2 = OpLabel
4093 OpBranchConditional %undef %b3 %b4
4094 %b3 = OpLabel
4095 OpBranch %merge
4096 %b4 = OpLabel
4097 OpBranch %merge
4098 %merge = OpLabel
4099 OpReturn
4100 OpFunctionEnd
4101 )";
4102
4103 CompileSuccessfully(text);
4104 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
4105 EXPECT_THAT(getDiagnosticString(), HasSubstr("Selection must be structured"));
4106 }
4107
4108 TEST_F(ValidateCFG, ContinueCannotBeSelectionMergeTarget) {
4109 const std::string text = R"(
4110 OpCapability Shader
4111 OpCapability Linkage
4112 OpMemoryModel Logical GLSL450
4113 OpName %loop "loop"
4114 OpName %continue "continue"
4115 OpName %body "body"
4116 %void = OpTypeVoid
4117 %void_fn = OpTypeFunction %void
4118 %bool = OpTypeBool
4119 %undef = OpUndef %bool
4120 %func = OpFunction %void None %void_fn
4121 %entry = OpLabel
4122 OpBranch %loop
4123 %loop = OpLabel
4124 OpLoopMerge %exit %continue None
4125 OpBranch %body
4126 %body = OpLabel
4127 OpSelectionMerge %continue None
4128 OpBranchConditional %undef %exit %continue
4129 %continue = OpLabel
4130 OpBranch %loop
4131 %exit = OpLabel
4132 OpReturn
4133 OpFunctionEnd
4134 )";
4135
4136 CompileSuccessfully(text);
4137 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
4138 EXPECT_THAT(
4139 getDiagnosticString(),
4140 HasSubstr(
4141 "Header block 3[%body] is contained in the loop construct headed by "
4142 "1[%loop], but its merge block 2[%continue] is not"));
4143 }
4144
4145 TEST_F(ValidateCFG, ContinueCannotBeLoopMergeTarget) {
4146 const std::string text = R"(
4147 OpCapability Shader
4148 OpCapability Linkage
4149 OpMemoryModel Logical GLSL450
4150 OpName %loop "loop"
4151 OpName %continue "continue"
4152 OpName %inner "inner"
4153 %void = OpTypeVoid
4154 %void_fn = OpTypeFunction %void
4155 %bool = OpTypeBool
4156 %undef = OpUndef %bool
4157 %func = OpFunction %void None %void_fn
4158 %entry = OpLabel
4159 OpBranch %loop
4160 %loop = OpLabel
4161 OpLoopMerge %exit %continue None
4162 OpBranchConditional %undef %exit %inner
4163 %inner = OpLabel
4164 OpLoopMerge %continue %inner None
4165 OpBranchConditional %undef %inner %continue
4166 %continue = OpLabel
4167 OpBranch %loop
4168 %exit = OpLabel
4169 OpReturn
4170 OpFunctionEnd
4171 )";
4172
4173 CompileSuccessfully(text);
4174 EXPECT_EQ(SPV_ERROR_INVALID_CFG, ValidateInstructions());
4175 EXPECT_THAT(
4176 getDiagnosticString(),
4177 HasSubstr(
4178 "Header block 3[%inner] is contained in the loop construct headed by "
4179 "1[%loop], but its merge block 2[%continue] is not"));
4180 }
4181
4182 TEST_F(ValidateCFG, ExitFromConstructWhoseHeaderIsAMerge) {
4183 const std::string text = R"(
4184 OpCapability Shader
4185 OpCapability Linkage
4186 OpMemoryModel Logical GLSL450
4187 %void = OpTypeVoid
4188 %2 = OpTypeFunction %void
4189 %int = OpTypeInt 32 1
4190 %4 = OpUndef %int
4191 %bool = OpTypeBool
4192 %6 = OpUndef %bool
4193 %7 = OpFunction %void None %2
4194 %8 = OpLabel
4195 OpSelectionMerge %9 None
4196 OpSwitch %4 %10 0 %11
4197 %10 = OpLabel
4198 OpBranch %9
4199 %11 = OpLabel
4200 OpBranch %12
4201 %12 = OpLabel
4202 OpLoopMerge %13 %14 None
4203 OpBranch %15
4204 %15 = OpLabel
4205 OpSelectionMerge %16 None
4206 OpSwitch %4 %17 1 %18 2 %19
4207 %17 = OpLabel
4208 OpBranch %16
4209 %18 = OpLabel
4210 OpBranch %14
4211 %19 = OpLabel
4212 OpBranch %16
4213 %16 = OpLabel
4214 OpBranch %14
4215 %14 = OpLabel
4216 OpBranchConditional %6 %12 %13
4217 %13 = OpLabel
4218 OpSelectionMerge %20 None
4219 OpBranchConditional %6 %21 %20
4220 %21 = OpLabel
4221 OpBranch %9
4222 %20 = OpLabel
4223 OpBranch %10
4224 %9 = OpLabel
4225 OpReturn
4226 OpFunctionEnd
4227 )";
4228
4229 CompileSuccessfully(text);
4230 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4231 }
4232
4233 TEST_F(ValidateCFG, ExitFromConstructWhoseHeaderIsAMerge2) {
4234 const std::string text = R"(
4235 OpCapability Shader
4236 %1 = OpExtInstImport "GLSL.std.450"
4237 OpMemoryModel Logical GLSL450
4238 OpEntryPoint Fragment %2 "main"
4239 OpExecutionMode %2 OriginUpperLeft
4240 %void = OpTypeVoid
4241 %4 = OpTypeFunction %void
4242 %int = OpTypeInt 32 1
4243 %6 = OpUndef %int
4244 %bool = OpTypeBool
4245 %8 = OpUndef %bool
4246 %2 = OpFunction %void None %4
4247 %9 = OpLabel
4248 OpSelectionMerge %10 None
4249 OpSwitch %6 %11 0 %12
4250 %11 = OpLabel
4251 OpBranch %10
4252 %12 = OpLabel
4253 OpBranch %13
4254 %13 = OpLabel
4255 OpLoopMerge %14 %15 None
4256 OpBranch %16
4257 %16 = OpLabel
4258 OpSelectionMerge %17 None
4259 OpSwitch %6 %18 1 %19 2 %20
4260 %18 = OpLabel
4261 OpBranch %17
4262 %19 = OpLabel
4263 OpBranch %15
4264 %20 = OpLabel
4265 OpBranch %17
4266 %17 = OpLabel
4267 OpBranch %15
4268 %15 = OpLabel
4269 OpBranchConditional %8 %13 %14
4270 %14 = OpLabel
4271 OpSelectionMerge %21 None
4272 OpBranchConditional %8 %22 %21
4273 %22 = OpLabel
4274 OpSelectionMerge %23 None
4275 OpBranchConditional %8 %24 %23
4276 %24 = OpLabel
4277 OpBranch %10
4278 %23 = OpLabel
4279 OpBranch %21
4280 %21 = OpLabel
4281 OpBranch %11
4282 %10 = OpLabel
4283 OpReturn
4284 OpFunctionEnd
4285 )";
4286
4287 CompileSuccessfully(text);
4288 EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
4289 }
4290
4291 TEST_F(ValidateCFG, PhiResultInvalidSampler) {
4292 const std::string text = R"(
4293 OpCapability Shader
4294 OpCapability Linkage
4295 OpMemoryModel Logical GLSL450
4296 %void = OpTypeVoid
4297 %bool = OpTypeBool
4298 %f32 = OpTypeFloat 32
4299 %sampler = OpTypeSampler
4300 %ptr_uc_sampler = OpTypePointer UniformConstant %sampler
4301 %sampler_var = OpVariable %ptr_uc_sampler UniformConstant
4302 %undef_bool = OpUndef %bool
4303 %undef_sampler = OpUndef %sampler
4304 %void_fn = OpTypeFunction %void
4305 %fn = OpFunction %void None %void_fn
4306 %entry = OpLabel
4307 %ld_sampler = OpLoad %sampler %sampler_var
4308 OpBranch %loop
4309 %loop = OpLabel
4310 %phi = OpPhi %sampler %undef_sampler %entry %ld_sampler %loop
4311 OpLoopMerge %exit %loop None
4312 OpBranchConditional %undef_bool %exit %loop
4313 %exit = OpLabel
4314 OpReturn
4315 OpFunctionEnd
4316 )";
4317
4318 CompileSuccessfully(text);
4319 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4320 EXPECT_THAT(getDiagnosticString(),
4321 HasSubstr("Result type cannot be OpTypeSampler"));
4322 }
4323
4324 TEST_F(ValidateCFG, PhiResultInvalidImage) {
4325 const std::string text = R"(
4326 OpCapability Shader
4327 OpCapability Linkage
4328 OpMemoryModel Logical GLSL450
4329 %void = OpTypeVoid
4330 %bool = OpTypeBool
4331 %f32 = OpTypeFloat 32
4332 %image = OpTypeImage %f32 2D 0 0 0 1 Rgba32f
4333 %ptr_uc_image = OpTypePointer UniformConstant %image
4334 %image_var = OpVariable %ptr_uc_image UniformConstant
4335 %undef_bool = OpUndef %bool
4336 %undef_image = OpUndef %image
4337 %void_fn = OpTypeFunction %void
4338 %fn = OpFunction %void None %void_fn
4339 %entry = OpLabel
4340 %ld_image = OpLoad %image %image_var
4341 OpBranch %loop
4342 %loop = OpLabel
4343 %phi = OpPhi %image %undef_image %entry %ld_image %loop
4344 OpLoopMerge %exit %loop None
4345 OpBranchConditional %undef_bool %exit %loop
4346 %exit = OpLabel
4347 OpReturn
4348 OpFunctionEnd
4349 )";
4350
4351 CompileSuccessfully(text);
4352 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4353 EXPECT_THAT(getDiagnosticString(),
4354 HasSubstr("Result type cannot be OpTypeImage"));
4355 }
4356
4357 TEST_F(ValidateCFG, PhiResultInvalidSampledImage) {
4358 const std::string text = R"(
4359 OpCapability Shader
4360 OpCapability Linkage
4361 OpMemoryModel Logical GLSL450
4362 %void = OpTypeVoid
4363 %bool = OpTypeBool
4364 %f32 = OpTypeFloat 32
4365 %sampler = OpTypeSampler
4366 %ptr_uc_sampler = OpTypePointer UniformConstant %sampler
4367 %sampler_var = OpVariable %ptr_uc_sampler UniformConstant
4368 %image = OpTypeImage %f32 2D 0 0 0 1 Rgba32f
4369 %ptr_uc_image = OpTypePointer UniformConstant %image
4370 %image_var = OpVariable %ptr_uc_image UniformConstant
4371 %sampled_image = OpTypeSampledImage %image
4372 %undef_bool = OpUndef %bool
4373 %undef_sampled_image = OpUndef %sampled_image
4374 %void_fn = OpTypeFunction %void
4375 %fn = OpFunction %void None %void_fn
4376 %entry = OpLabel
4377 %ld_image = OpLoad %image %image_var
4378 %ld_sampler = OpLoad %sampler %sampler_var
4379 OpBranch %loop
4380 %loop = OpLabel
4381 %phi = OpPhi %sampled_image %undef_sampled_image %entry %sample %loop
4382 %sample = OpSampledImage %sampled_image %ld_image %ld_sampler
4383 OpLoopMerge %exit %loop None
4384 OpBranchConditional %undef_bool %exit %loop
4385 %exit = OpLabel
4386 OpReturn
4387 OpFunctionEnd
4388 )";
4389
4390 CompileSuccessfully(text);
4391 ASSERT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4392 EXPECT_THAT(getDiagnosticString(),
4393 HasSubstr("Result type cannot be OpTypeSampledImage"));
4394 }
4395
4396 TEST_F(ValidateCFG, PhiResultValidPreLegalizationSampler) {
4397 const std::string text = R"(
4398 OpCapability Shader
4399 OpCapability Linkage
4400 OpMemoryModel Logical GLSL450
4401 %void = OpTypeVoid
4402 %bool = OpTypeBool
4403 %f32 = OpTypeFloat 32
4404 %sampler = OpTypeSampler
4405 %ptr_uc_sampler = OpTypePointer UniformConstant %sampler
4406 %sampler_var = OpVariable %ptr_uc_sampler UniformConstant
4407 %undef_bool = OpUndef %bool
4408 %undef_sampler = OpUndef %sampler
4409 %void_fn = OpTypeFunction %void
4410 %fn = OpFunction %void None %void_fn
4411 %entry = OpLabel
4412 %ld_sampler = OpLoad %sampler %sampler_var
4413 OpBranch %loop
4414 %loop = OpLabel
4415 %phi = OpPhi %sampler %undef_sampler %entry %ld_sampler %loop
4416 OpLoopMerge %exit %loop None
4417 OpBranchConditional %undef_bool %exit %loop
4418 %exit = OpLabel
4419 OpReturn
4420 OpFunctionEnd
4421 )";
4422
4423 options_->before_hlsl_legalization = true;
4424 CompileSuccessfully(text);
4425 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
4426 }
4427
4428 TEST_F(ValidateCFG, PhiResultValidPreLegalizationImage) {
4429 const std::string text = R"(
4430 OpCapability Shader
4431 OpCapability Linkage
4432 OpMemoryModel Logical GLSL450
4433 %void = OpTypeVoid
4434 %bool = OpTypeBool
4435 %f32 = OpTypeFloat 32
4436 %image = OpTypeImage %f32 2D 0 0 0 1 Rgba32f
4437 %ptr_uc_image = OpTypePointer UniformConstant %image
4438 %image_var = OpVariable %ptr_uc_image UniformConstant
4439 %undef_bool = OpUndef %bool
4440 %undef_image = OpUndef %image
4441 %void_fn = OpTypeFunction %void
4442 %fn = OpFunction %void None %void_fn
4443 %entry = OpLabel
4444 %ld_image = OpLoad %image %image_var
4445 OpBranch %loop
4446 %loop = OpLabel
4447 %phi = OpPhi %image %undef_image %entry %ld_image %loop
4448 OpLoopMerge %exit %loop None
4449 OpBranchConditional %undef_bool %exit %loop
4450 %exit = OpLabel
4451 OpReturn
4452 OpFunctionEnd
4453 )";
4454
4455 options_->before_hlsl_legalization = true;
4456 CompileSuccessfully(text);
4457 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
4458 }
4459
4460 TEST_F(ValidateCFG, PhiResultValidPreLegalizationSampledImage) {
4461 const std::string text = R"(
4462 OpCapability Shader
4463 OpCapability Linkage
4464 OpMemoryModel Logical GLSL450
4465 %void = OpTypeVoid
4466 %bool = OpTypeBool
4467 %f32 = OpTypeFloat 32
4468 %sampler = OpTypeSampler
4469 %ptr_uc_sampler = OpTypePointer UniformConstant %sampler
4470 %sampler_var = OpVariable %ptr_uc_sampler UniformConstant
4471 %image = OpTypeImage %f32 2D 0 0 0 1 Rgba32f
4472 %ptr_uc_image = OpTypePointer UniformConstant %image
4473 %image_var = OpVariable %ptr_uc_image UniformConstant
4474 %sampled_image = OpTypeSampledImage %image
4475 %undef_bool = OpUndef %bool
4476 %undef_sampled_image = OpUndef %sampled_image
4477 %void_fn = OpTypeFunction %void
4478 %fn = OpFunction %void None %void_fn
4479 %entry = OpLabel
4480 %ld_image = OpLoad %image %image_var
4481 %ld_sampler = OpLoad %sampler %sampler_var
4482 OpBranch %loop
4483 %loop = OpLabel
4484 %phi = OpPhi %sampled_image %undef_sampled_image %entry %sample %loop
4485 %sample = OpSampledImage %sampled_image %ld_image %ld_sampler
4486 OpLoopMerge %exit %loop None
4487 OpBranchConditional %undef_bool %exit %loop
4488 %exit = OpLabel
4489 OpReturn
4490 OpFunctionEnd
4491 )";
4492
4493 options_->before_hlsl_legalization = true;
4494 CompileSuccessfully(text);
4495 ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
4496 }
4497
4498 TEST_F(ValidateCFG, UnreachableIsStaticallyReachable) {
4499 const std::string text = R"(
4500 OpCapability Shader
4501 OpCapability Linkage
4502 OpMemoryModel Logical GLSL450
4503 %1 = OpTypeVoid
4504 %2 = OpTypeFunction %1
4505 %3 = OpFunction %1 None %2
4506 %4 = OpLabel
4507 OpBranch %5
4508 %5 = OpLabel
4509 OpUnreachable
4510 OpFunctionEnd
4511 )";
4512
4513 CompileSuccessfully(text);
4514 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
4515
4516 auto f = vstate_->function(3);
4517 auto entry = f->GetBlock(4).first;
4518 ASSERT_TRUE(entry->reachable());
4519 auto end = f->GetBlock(5).first;
4520 ASSERT_TRUE(end->reachable());
4521 }
4522
4523 TEST_F(ValidateCFG, BlockOrderDoesNotAffectReachability) {
4524 const std::string text = R"(
4525 OpCapability Shader
4526 OpCapability Linkage
4527 OpMemoryModel Logical GLSL450
4528 %1 = OpTypeVoid
4529 %2 = OpTypeFunction %1
4530 %3 = OpTypeBool
4531 %4 = OpUndef %3
4532 %5 = OpFunction %1 None %2
4533 %6 = OpLabel
4534 OpBranch %7
4535 %7 = OpLabel
4536 OpSelectionMerge %8 None
4537 OpBranchConditional %4 %9 %10
4538 %8 = OpLabel
4539 OpReturn
4540 %9 = OpLabel
4541 OpBranch %8
4542 %10 = OpLabel
4543 OpBranch %8
4544 %11 = OpLabel
4545 OpUnreachable
4546 OpFunctionEnd
4547 )";
4548
4549 CompileSuccessfully(text);
4550 EXPECT_EQ(SPV_SUCCESS, ValidateAndRetrieveValidationState());
4551
4552 auto f = vstate_->function(5);
4553 auto b6 = f->GetBlock(6).first;
4554 auto b7 = f->GetBlock(7).first;
4555 auto b8 = f->GetBlock(8).first;
4556 auto b9 = f->GetBlock(9).first;
4557 auto b10 = f->GetBlock(10).first;
4558 auto b11 = f->GetBlock(11).first;
4559
4560 ASSERT_TRUE(b6->reachable());
4561 ASSERT_TRUE(b7->reachable());
4562 ASSERT_TRUE(b8->reachable());
4563 ASSERT_TRUE(b9->reachable());
4564 ASSERT_TRUE(b10->reachable());
4565 ASSERT_FALSE(b11->reachable());
4566 }
4567
4568 TEST_F(ValidateCFG, PhiInstructionWithDuplicateIncomingEdges) {
4569 const std::string text = R"(
4570 OpCapability Shader
4571 %1 = OpExtInstImport "GLSL.std.450"
4572 OpMemoryModel Logical GLSL450
4573 OpEntryPoint Fragment %4 "main"
4574 OpExecutionMode %4 OriginUpperLeft
4575 OpSource ESSL 320
4576 %2 = OpTypeVoid
4577 %3 = OpTypeFunction %2
4578 %6 = OpTypeBool
4579 %7 = OpConstantTrue %6
4580 %4 = OpFunction %2 None %3
4581 %5 = OpLabel
4582 OpSelectionMerge %10 None
4583 OpBranchConditional %7 %8 %9
4584 %8 = OpLabel
4585 OpBranch %10
4586 %9 = OpLabel
4587 OpBranch %10
4588 %10 = OpLabel
4589 %11 = OpPhi %6 %7 %8 %7 %8
4590 OpReturn
4591 OpFunctionEnd
4592 )";
4593
4594 CompileSuccessfully(text);
4595 EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
4596 EXPECT_THAT(getDiagnosticString(),
4597 HasSubstr("OpPhi references incoming basic block <id> "));
4598 EXPECT_THAT(getDiagnosticString(), HasSubstr("multiple times."));
4599 }
4600
4601 } // namespace
4602 } // namespace val
4603 } // namespace spvtools