Debian Codegraph @

test/fuzz/transformation_replace_branch_from_dead_block_with_exit_test.cpp

raw
1 // Copyright (c) 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "source/fuzz/transformation_replace_branch_from_dead_block_with_exit.h"
16
17 #include "gtest/gtest.h"
18 #include "source/fuzz/fuzzer_util.h"
19 #include "test/fuzz/fuzz_test_util.h"
20
21 namespace spvtools {
22 namespace fuzz {
23 namespace {
24
25 TEST(TransformationReplaceBranchFromDeadBlockWithExitTest, BasicTest) {
26 std::string shader = R"(
27 OpCapability Shader
28 %1 = OpExtInstImport "GLSL.std.450"
29 OpMemoryModel Logical GLSL450
30 OpEntryPoint Fragment %4 "main"
31 OpExecutionMode %4 OriginUpperLeft
32 OpSource ESSL 320
33 %2 = OpTypeVoid
34 %3 = OpTypeFunction %2
35 %6 = OpTypeBool
36 %7 = OpConstantFalse %6
37 %12 = OpTypeInt 32 1
38 %13 = OpTypePointer Function %12
39 %15 = OpConstant %12 1
40 %17 = OpConstant %12 2
41 %4 = OpFunction %2 None %3
42 %5 = OpLabel
43 %14 = OpVariable %13 Function
44 OpSelectionMerge %9 None
45 OpBranchConditional %7 %8 %21
46 %8 = OpLabel
47 OpSelectionMerge %11 None
48 OpBranchConditional %7 %10 %16
49 %10 = OpLabel
50 OpStore %14 %15
51 OpBranch %20
52 %20 = OpLabel
53 OpBranch %11
54 %16 = OpLabel
55 OpStore %14 %17
56 OpBranch %11
57 %11 = OpLabel
58 OpBranch %9
59 %21 = OpLabel
60 OpBranch %9
61 %9 = OpLabel
62 OpReturn
63 OpFunctionEnd
64 )";
65
66 const auto env = SPV_ENV_UNIVERSAL_1_4;
67 const auto consumer = nullptr;
68 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
69 spvtools::ValidatorOptions validator_options;
70 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
71 kConsoleMessageConsumer));
72 TransformationContext transformation_context(
73 MakeUnique<FactManager>(context.get()), validator_options);
74
75 transformation_context.GetFactManager()->AddFactBlockIsDead(8);
76 transformation_context.GetFactManager()->AddFactBlockIsDead(10);
77 transformation_context.GetFactManager()->AddFactBlockIsDead(11);
78 transformation_context.GetFactManager()->AddFactBlockIsDead(16);
79 transformation_context.GetFactManager()->AddFactBlockIsDead(20);
80
81 // Bad: 4 is not a block
82 ASSERT_FALSE(
83 TransformationReplaceBranchFromDeadBlockWithExit(4, spv::Op::OpKill, 0)
84 .IsApplicable(context.get(), transformation_context));
85 // Bad: 200 does not exist
86 ASSERT_FALSE(
87 TransformationReplaceBranchFromDeadBlockWithExit(200, spv::Op::OpKill, 0)
88 .IsApplicable(context.get(), transformation_context));
89 // Bad: 21 is not a dead block
90 ASSERT_FALSE(
91 TransformationReplaceBranchFromDeadBlockWithExit(21, spv::Op::OpKill, 0)
92 .IsApplicable(context.get(), transformation_context));
93 // Bad: terminator of 8 is not OpBranch
94 ASSERT_FALSE(
95 TransformationReplaceBranchFromDeadBlockWithExit(8, spv::Op::OpKill, 0)
96 .IsApplicable(context.get(), transformation_context));
97 // Bad: 10's successor only has 10 as a predecessor
98 ASSERT_FALSE(
99 TransformationReplaceBranchFromDeadBlockWithExit(10, spv::Op::OpKill, 0)
100 .IsApplicable(context.get(), transformation_context));
101
102 #ifndef NDEBUG
103 ASSERT_DEATH(
104 TransformationReplaceBranchFromDeadBlockWithExit(20, spv::Op::OpSwitch, 0)
105 .IsApplicable(context.get(), transformation_context),
106 "Invalid early exit opcode.");
107 #endif
108
109 auto transformation1 =
110 TransformationReplaceBranchFromDeadBlockWithExit(20, spv::Op::OpKill, 0);
111 auto transformation2 =
112 TransformationReplaceBranchFromDeadBlockWithExit(16, spv::Op::OpKill, 0);
113 ASSERT_TRUE(
114 transformation1.IsApplicable(context.get(), transformation_context));
115 ASSERT_TRUE(
116 transformation2.IsApplicable(context.get(), transformation_context));
117
118 ApplyAndCheckFreshIds(transformation1, context.get(),
119 &transformation_context);
120 // Applying transformation 1 should disable transformation 2
121 ASSERT_FALSE(
122 transformation2.IsApplicable(context.get(), transformation_context));
123
124 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
125 kConsoleMessageConsumer));
126
127 std::string after_transformation = R"(
128 OpCapability Shader
129 %1 = OpExtInstImport "GLSL.std.450"
130 OpMemoryModel Logical GLSL450
131 OpEntryPoint Fragment %4 "main"
132 OpExecutionMode %4 OriginUpperLeft
133 OpSource ESSL 320
134 %2 = OpTypeVoid
135 %3 = OpTypeFunction %2
136 %6 = OpTypeBool
137 %7 = OpConstantFalse %6
138 %12 = OpTypeInt 32 1
139 %13 = OpTypePointer Function %12
140 %15 = OpConstant %12 1
141 %17 = OpConstant %12 2
142 %4 = OpFunction %2 None %3
143 %5 = OpLabel
144 %14 = OpVariable %13 Function
145 OpSelectionMerge %9 None
146 OpBranchConditional %7 %8 %21
147 %8 = OpLabel
148 OpSelectionMerge %11 None
149 OpBranchConditional %7 %10 %16
150 %10 = OpLabel
151 OpStore %14 %15
152 OpBranch %20
153 %20 = OpLabel
154 OpKill
155 %16 = OpLabel
156 OpStore %14 %17
157 OpBranch %11
158 %11 = OpLabel
159 OpBranch %9
160 %21 = OpLabel
161 OpBranch %9
162 %9 = OpLabel
163 OpReturn
164 OpFunctionEnd
165 )";
166 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
167 }
168
169 TEST(TransformationReplaceBranchFromDeadBlockWithExitTest,
170 VertexShaderWithLoopInContinueConstruct) {
171 std::string shader = R"(
172 OpCapability Shader
173 %1 = OpExtInstImport "GLSL.std.450"
174 OpMemoryModel Logical GLSL450
175 OpEntryPoint Vertex %4 "main"
176 OpSource ESSL 320
177 %2 = OpTypeVoid
178 %3 = OpTypeFunction %2
179 %6 = OpTypeInt 32 1
180 %7 = OpTypeFunction %6
181 %12 = OpTypePointer Function %6
182 %14 = OpConstant %6 0
183 %22 = OpConstant %6 10
184 %23 = OpTypeBool
185 %26 = OpConstant %6 1
186 %40 = OpConstant %6 100
187 %48 = OpConstantFalse %23
188 %4 = OpFunction %2 None %3
189 %5 = OpLabel
190 OpSelectionMerge %50 None
191 OpBranchConditional %48 %49 %50
192 %49 = OpLabel
193 %51 = OpFunctionCall %6 %10
194 OpBranch %50
195 %50 = OpLabel
196 OpReturn
197 OpFunctionEnd
198 %10 = OpFunction %6 None %7
199 %11 = OpLabel
200 %13 = OpVariable %12 Function
201 %15 = OpVariable %12 Function
202 %33 = OpVariable %12 Function
203 OpStore %33 %14
204 OpBranch %34
205 %34 = OpLabel
206 OpLoopMerge %36 %37 None
207 OpBranch %38
208 %38 = OpLabel
209 %39 = OpLoad %6 %33
210 %41 = OpSLessThan %23 %39 %40
211 OpBranchConditional %41 %35 %36
212 %35 = OpLabel
213 OpSelectionMerge %202 None
214 OpBranchConditional %48 %200 %201
215 %200 = OpLabel
216 OpBranch %202
217 %201 = OpLabel
218 %400 = OpCopyObject %6 %14
219 OpBranch %202
220 %202 = OpLabel
221 OpBranch %37
222 %37 = OpLabel
223 OpStore %13 %14
224 OpStore %15 %14
225 OpBranch %16
226 %16 = OpLabel
227 OpLoopMerge %18 %19 None
228 OpBranch %20
229 %20 = OpLabel
230 OpSelectionMerge %102 None
231 OpBranchConditional %48 %100 %101
232 %100 = OpLabel
233 OpBranch %102
234 %101 = OpLabel
235 OpBranch %102
236 %102 = OpLabel
237 %21 = OpLoad %6 %15
238 %24 = OpSLessThan %23 %21 %22
239 OpBranchConditional %24 %17 %18
240 %17 = OpLabel
241 OpSelectionMerge %302 None
242 OpBranchConditional %48 %300 %301
243 %300 = OpLabel
244 OpBranch %302
245 %301 = OpLabel
246 OpBranch %302
247 %302 = OpLabel
248 %25 = OpLoad %6 %13
249 %27 = OpIAdd %6 %25 %26
250 OpStore %13 %27
251 OpBranch %19
252 %19 = OpLabel
253 %28 = OpLoad %6 %15
254 %29 = OpIAdd %6 %28 %26
255 OpStore %15 %29
256 OpBranch %16
257 %18 = OpLabel
258 %30 = OpLoad %6 %13
259 %42 = OpCopyObject %6 %30
260 %43 = OpLoad %6 %33
261 %44 = OpIAdd %6 %43 %42
262 OpStore %33 %44
263 OpBranch %34
264 %36 = OpLabel
265 %45 = OpLoad %6 %33
266 OpReturnValue %45
267 OpFunctionEnd
268 )";
269
270 const auto env = SPV_ENV_UNIVERSAL_1_4;
271 const auto consumer = nullptr;
272 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
273 spvtools::ValidatorOptions validator_options;
274 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
275 kConsoleMessageConsumer));
276 TransformationContext transformation_context(
277 MakeUnique<FactManager>(context.get()), validator_options);
278
279 for (auto block : {16, 17, 18, 19, 20, 34, 35, 36, 37, 38,
280 49, 100, 101, 102, 200, 201, 202, 300, 301, 302}) {
281 transformation_context.GetFactManager()->AddFactBlockIsDead(block);
282 }
283
284 // Bad: OpKill not allowed in vertex shader
285 ASSERT_FALSE(
286 TransformationReplaceBranchFromDeadBlockWithExit(201, spv::Op::OpKill, 0)
287 .IsApplicable(context.get(), transformation_context));
288
289 // Bad: OpReturn is not allowed in function that expects a returned value.
290 ASSERT_FALSE(TransformationReplaceBranchFromDeadBlockWithExit(
291 200, spv::Op::OpReturn, 0)
292 .IsApplicable(context.get(), transformation_context));
293
294 // Bad: Return value id does not exist
295 ASSERT_FALSE(TransformationReplaceBranchFromDeadBlockWithExit(
296 201, spv::Op::OpReturnValue, 1000)
297 .IsApplicable(context.get(), transformation_context));
298
299 // Bad: Return value id does not have a type
300 ASSERT_FALSE(TransformationReplaceBranchFromDeadBlockWithExit(
301 200, spv::Op::OpReturnValue, 6)
302 .IsApplicable(context.get(), transformation_context));
303
304 // Bad: Return value id does not have the right type
305 ASSERT_FALSE(TransformationReplaceBranchFromDeadBlockWithExit(
306 201, spv::Op::OpReturnValue, 48)
307 .IsApplicable(context.get(), transformation_context));
308
309 // Bad: Return value id is not available
310 ASSERT_FALSE(TransformationReplaceBranchFromDeadBlockWithExit(
311 200, spv::Op::OpReturnValue, 400)
312 .IsApplicable(context.get(), transformation_context));
313
314 // Bad: Early exit now allowed in continue construct
315 ASSERT_FALSE(TransformationReplaceBranchFromDeadBlockWithExit(
316 101, spv::Op::OpUnreachable, 0)
317 .IsApplicable(context.get(), transformation_context));
318
319 // Bad: Early exit now allowed in continue construct (again)
320 ASSERT_FALSE(TransformationReplaceBranchFromDeadBlockWithExit(
321 300, spv::Op::OpReturnValue, 14)
322 .IsApplicable(context.get(), transformation_context));
323
324 auto transformation1 = TransformationReplaceBranchFromDeadBlockWithExit(
325 200, spv::Op::OpUnreachable, 0);
326 auto transformation2 = TransformationReplaceBranchFromDeadBlockWithExit(
327 201, spv::Op::OpReturnValue, 400);
328
329 ASSERT_TRUE(
330 transformation1.IsApplicable(context.get(), transformation_context));
331 ASSERT_TRUE(
332 transformation2.IsApplicable(context.get(), transformation_context));
333
334 ApplyAndCheckFreshIds(transformation2, context.get(),
335 &transformation_context);
336 ASSERT_FALSE(
337 transformation1.IsApplicable(context.get(), transformation_context));
338
339 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
340 kConsoleMessageConsumer));
341
342 opt::Instruction* return_value_inst =
343 context->get_instr_block(201)->terminator();
344 ASSERT_EQ(spv::Op::OpReturnValue, return_value_inst->opcode());
345 ASSERT_EQ(SPV_OPERAND_TYPE_ID, return_value_inst->GetInOperand(0).type);
346 ASSERT_EQ(400, return_value_inst->GetSingleWordInOperand(0));
347
348 std::string after_transformation = R"(
349 OpCapability Shader
350 %1 = OpExtInstImport "GLSL.std.450"
351 OpMemoryModel Logical GLSL450
352 OpEntryPoint Vertex %4 "main"
353 OpSource ESSL 320
354 %2 = OpTypeVoid
355 %3 = OpTypeFunction %2
356 %6 = OpTypeInt 32 1
357 %7 = OpTypeFunction %6
358 %12 = OpTypePointer Function %6
359 %14 = OpConstant %6 0
360 %22 = OpConstant %6 10
361 %23 = OpTypeBool
362 %26 = OpConstant %6 1
363 %40 = OpConstant %6 100
364 %48 = OpConstantFalse %23
365 %4 = OpFunction %2 None %3
366 %5 = OpLabel
367 OpSelectionMerge %50 None
368 OpBranchConditional %48 %49 %50
369 %49 = OpLabel
370 %51 = OpFunctionCall %6 %10
371 OpBranch %50
372 %50 = OpLabel
373 OpReturn
374 OpFunctionEnd
375 %10 = OpFunction %6 None %7
376 %11 = OpLabel
377 %13 = OpVariable %12 Function
378 %15 = OpVariable %12 Function
379 %33 = OpVariable %12 Function
380 OpStore %33 %14
381 OpBranch %34
382 %34 = OpLabel
383 OpLoopMerge %36 %37 None
384 OpBranch %38
385 %38 = OpLabel
386 %39 = OpLoad %6 %33
387 %41 = OpSLessThan %23 %39 %40
388 OpBranchConditional %41 %35 %36
389 %35 = OpLabel
390 OpSelectionMerge %202 None
391 OpBranchConditional %48 %200 %201
392 %200 = OpLabel
393 OpBranch %202
394 %201 = OpLabel
395 %400 = OpCopyObject %6 %14
396 OpReturnValue %400
397 %202 = OpLabel
398 OpBranch %37
399 %37 = OpLabel
400 OpStore %13 %14
401 OpStore %15 %14
402 OpBranch %16
403 %16 = OpLabel
404 OpLoopMerge %18 %19 None
405 OpBranch %20
406 %20 = OpLabel
407 OpSelectionMerge %102 None
408 OpBranchConditional %48 %100 %101
409 %100 = OpLabel
410 OpBranch %102
411 %101 = OpLabel
412 OpBranch %102
413 %102 = OpLabel
414 %21 = OpLoad %6 %15
415 %24 = OpSLessThan %23 %21 %22
416 OpBranchConditional %24 %17 %18
417 %17 = OpLabel
418 OpSelectionMerge %302 None
419 OpBranchConditional %48 %300 %301
420 %300 = OpLabel
421 OpBranch %302
422 %301 = OpLabel
423 OpBranch %302
424 %302 = OpLabel
425 %25 = OpLoad %6 %13
426 %27 = OpIAdd %6 %25 %26
427 OpStore %13 %27
428 OpBranch %19
429 %19 = OpLabel
430 %28 = OpLoad %6 %15
431 %29 = OpIAdd %6 %28 %26
432 OpStore %15 %29
433 OpBranch %16
434 %18 = OpLabel
435 %30 = OpLoad %6 %13
436 %42 = OpCopyObject %6 %30
437 %43 = OpLoad %6 %33
438 %44 = OpIAdd %6 %43 %42
439 OpStore %33 %44
440 OpBranch %34
441 %36 = OpLabel
442 %45 = OpLoad %6 %33
443 OpReturnValue %45
444 OpFunctionEnd
445 )";
446 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
447 }
448
449 TEST(TransformationReplaceBranchFromDeadBlockWithExitTest, OpPhi) {
450 std::string shader = R"(
451 OpCapability Shader
452 %1 = OpExtInstImport "GLSL.std.450"
453 OpMemoryModel Logical GLSL450
454 OpEntryPoint Fragment %4 "main"
455 OpExecutionMode %4 OriginUpperLeft
456 OpSource ESSL 320
457 %2 = OpTypeVoid
458 %3 = OpTypeFunction %2
459 %6 = OpTypeBool
460 %7 = OpConstantFalse %6
461 %12 = OpTypeInt 32 1
462 %13 = OpTypePointer Function %12
463 %15 = OpConstant %12 1
464 %17 = OpConstant %12 2
465 %4 = OpFunction %2 None %3
466 %5 = OpLabel
467 %14 = OpVariable %13 Function
468 OpSelectionMerge %9 None
469 OpBranchConditional %7 %8 %21
470 %8 = OpLabel
471 OpSelectionMerge %11 None
472 OpBranchConditional %7 %10 %16
473 %10 = OpLabel
474 OpStore %14 %15
475 OpBranch %20
476 %20 = OpLabel
477 %48 = OpCopyObject %12 %15
478 OpBranch %11
479 %16 = OpLabel
480 OpStore %14 %17
481 %49 = OpCopyObject %12 %17
482 OpBranch %11
483 %11 = OpLabel
484 %50 = OpPhi %12 %48 %20 %49 %16
485 OpBranch %9
486 %21 = OpLabel
487 OpBranch %9
488 %9 = OpLabel
489 OpReturn
490 OpFunctionEnd
491 )";
492
493 const auto env = SPV_ENV_UNIVERSAL_1_4;
494 const auto consumer = nullptr;
495 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
496 spvtools::ValidatorOptions validator_options;
497 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
498 kConsoleMessageConsumer));
499 TransformationContext transformation_context(
500 MakeUnique<FactManager>(context.get()), validator_options);
501
502 transformation_context.GetFactManager()->AddFactBlockIsDead(8);
503 transformation_context.GetFactManager()->AddFactBlockIsDead(10);
504 transformation_context.GetFactManager()->AddFactBlockIsDead(11);
505 transformation_context.GetFactManager()->AddFactBlockIsDead(16);
506 transformation_context.GetFactManager()->AddFactBlockIsDead(20);
507
508 auto transformation1 =
509 TransformationReplaceBranchFromDeadBlockWithExit(20, spv::Op::OpKill, 0);
510 auto transformation2 =
511 TransformationReplaceBranchFromDeadBlockWithExit(16, spv::Op::OpKill, 0);
512 ASSERT_TRUE(
513 transformation1.IsApplicable(context.get(), transformation_context));
514 ASSERT_TRUE(
515 transformation2.IsApplicable(context.get(), transformation_context));
516
517 ApplyAndCheckFreshIds(transformation1, context.get(),
518 &transformation_context);
519 // Applying transformation 1 should disable transformation 2
520 ASSERT_FALSE(
521 transformation2.IsApplicable(context.get(), transformation_context));
522
523 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
524 kConsoleMessageConsumer));
525
526 std::string after_transformation = R"(
527 OpCapability Shader
528 %1 = OpExtInstImport "GLSL.std.450"
529 OpMemoryModel Logical GLSL450
530 OpEntryPoint Fragment %4 "main"
531 OpExecutionMode %4 OriginUpperLeft
532 OpSource ESSL 320
533 %2 = OpTypeVoid
534 %3 = OpTypeFunction %2
535 %6 = OpTypeBool
536 %7 = OpConstantFalse %6
537 %12 = OpTypeInt 32 1
538 %13 = OpTypePointer Function %12
539 %15 = OpConstant %12 1
540 %17 = OpConstant %12 2
541 %4 = OpFunction %2 None %3
542 %5 = OpLabel
543 %14 = OpVariable %13 Function
544 OpSelectionMerge %9 None
545 OpBranchConditional %7 %8 %21
546 %8 = OpLabel
547 OpSelectionMerge %11 None
548 OpBranchConditional %7 %10 %16
549 %10 = OpLabel
550 OpStore %14 %15
551 OpBranch %20
552 %20 = OpLabel
553 %48 = OpCopyObject %12 %15
554 OpKill
555 %16 = OpLabel
556 OpStore %14 %17
557 %49 = OpCopyObject %12 %17
558 OpBranch %11
559 %11 = OpLabel
560 %50 = OpPhi %12 %49 %16
561 OpBranch %9
562 %21 = OpLabel
563 OpBranch %9
564 %9 = OpLabel
565 OpReturn
566 OpFunctionEnd
567 )";
568 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
569 }
570
571 TEST(TransformationReplaceBranchFromDeadBlockWithExitTest,
572 DominatorAfterDeadBlockSuccessor) {
573 std::string shader = R"(
574 OpCapability Shader
575 %1 = OpExtInstImport "GLSL.std.450"
576 OpMemoryModel Logical GLSL450
577 OpEntryPoint Fragment %4 "main"
578 OpExecutionMode %4 OriginUpperLeft
579 OpSource ESSL 320
580 %2 = OpTypeVoid
581 %3 = OpTypeFunction %2
582 %6 = OpTypeBool
583 %7 = OpConstantFalse %6
584 %4 = OpFunction %2 None %3
585 %5 = OpLabel
586 OpSelectionMerge %8 None
587 OpBranchConditional %7 %9 %10
588 %9 = OpLabel
589 OpBranch %11
590 %11 = OpLabel
591 %12 = OpCopyObject %6 %7
592 OpBranch %8
593 %10 = OpLabel
594 OpBranch %13
595 %8 = OpLabel
596 OpReturn
597 %13 = OpLabel
598 OpBranch %8
599 OpFunctionEnd
600 )";
601
602 const auto env = SPV_ENV_UNIVERSAL_1_4;
603 const auto consumer = nullptr;
604 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
605 spvtools::ValidatorOptions validator_options;
606 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
607 kConsoleMessageConsumer));
608 TransformationContext transformation_context(
609 MakeUnique<FactManager>(context.get()), validator_options);
610
611 transformation_context.GetFactManager()->AddFactBlockIsDead(9);
612 transformation_context.GetFactManager()->AddFactBlockIsDead(11);
613
614 ASSERT_FALSE(TransformationReplaceBranchFromDeadBlockWithExit(
615 11, spv::Op::OpUnreachable, 0)
616 .IsApplicable(context.get(), transformation_context));
617 }
618
619 TEST(TransformationReplaceBranchFromDeadBlockWithExitTest,
620 UnreachableSuccessor) {
621 std::string shader = R"(
622 OpCapability Shader
623 %1 = OpExtInstImport "GLSL.std.450"
624 OpMemoryModel Logical GLSL450
625 OpEntryPoint Fragment %4 "main"
626 OpExecutionMode %4 OriginUpperLeft
627 OpSource ESSL 320
628 %2 = OpTypeVoid
629 %3 = OpTypeFunction %2
630 %6 = OpTypeBool
631 %7 = OpConstantFalse %6
632 %4 = OpFunction %2 None %3
633 %5 = OpLabel
634 OpSelectionMerge %8 None
635 OpBranchConditional %7 %9 %10
636 %9 = OpLabel
637 OpBranch %11
638 %11 = OpLabel
639 %12 = OpCopyObject %6 %7
640 OpBranch %8
641 %10 = OpLabel
642 OpReturn
643 %8 = OpLabel
644 OpReturn
645 %13 = OpLabel
646 OpBranch %8
647 OpFunctionEnd
648 )";
649
650 const auto env = SPV_ENV_UNIVERSAL_1_4;
651 const auto consumer = nullptr;
652 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
653 spvtools::ValidatorOptions validator_options;
654 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
655 kConsoleMessageConsumer));
656 TransformationContext transformation_context(
657 MakeUnique<FactManager>(context.get()), validator_options);
658
659 transformation_context.GetFactManager()->AddFactBlockIsDead(9);
660 transformation_context.GetFactManager()->AddFactBlockIsDead(11);
661
662 TransformationReplaceBranchFromDeadBlockWithExit transformation(
663 11, spv::Op::OpUnreachable, 0);
664 ASSERT_TRUE(
665 transformation.IsApplicable(context.get(), transformation_context));
666 transformation.Apply(context.get(), &transformation_context);
667 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
668 kConsoleMessageConsumer));
669
670 std::string after_transformation = R"(
671 OpCapability Shader
672 %1 = OpExtInstImport "GLSL.std.450"
673 OpMemoryModel Logical GLSL450
674 OpEntryPoint Fragment %4 "main"
675 OpExecutionMode %4 OriginUpperLeft
676 OpSource ESSL 320
677 %2 = OpTypeVoid
678 %3 = OpTypeFunction %2
679 %6 = OpTypeBool
680 %7 = OpConstantFalse %6
681 %4 = OpFunction %2 None %3
682 %5 = OpLabel
683 OpSelectionMerge %8 None
684 OpBranchConditional %7 %9 %10
685 %9 = OpLabel
686 OpBranch %11
687 %11 = OpLabel
688 %12 = OpCopyObject %6 %7
689 OpUnreachable
690 %10 = OpLabel
691 OpReturn
692 %8 = OpLabel
693 OpReturn
694 %13 = OpLabel
695 OpBranch %8
696 OpFunctionEnd
697 )";
698
699 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
700 }
701
702 TEST(TransformationReplaceBranchFromDeadBlockWithExitTest,
703 DeadBlockAfterItsSuccessor) {
704 std::string shader = R"(
705 OpCapability Shader
706 %1 = OpExtInstImport "GLSL.std.450"
707 OpMemoryModel Logical GLSL450
708 OpEntryPoint Fragment %4 "main"
709 OpExecutionMode %4 OriginUpperLeft
710 OpSource ESSL 320
711 %2 = OpTypeVoid
712 %3 = OpTypeFunction %2
713 %6 = OpTypeBool
714 %7 = OpConstantTrue %6
715 %4 = OpFunction %2 None %3
716 %5 = OpLabel
717 OpSelectionMerge %8 None
718 OpBranchConditional %7 %9 %10
719 %9 = OpLabel
720 OpBranch %11
721 %11 = OpLabel
722 %12 = OpCopyObject %6 %7
723 OpBranch %8
724 %10 = OpLabel
725 OpBranch %13
726 %8 = OpLabel
727 OpReturn
728 %13 = OpLabel
729 OpBranch %8
730 OpFunctionEnd
731 )";
732
733 const auto env = SPV_ENV_UNIVERSAL_1_4;
734 const auto consumer = nullptr;
735 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
736 spvtools::ValidatorOptions validator_options;
737 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
738 kConsoleMessageConsumer));
739 TransformationContext transformation_context(
740 MakeUnique<FactManager>(context.get()), validator_options);
741
742 transformation_context.GetFactManager()->AddFactBlockIsDead(10);
743 transformation_context.GetFactManager()->AddFactBlockIsDead(13);
744
745 TransformationReplaceBranchFromDeadBlockWithExit transformation(
746 13, spv::Op::OpUnreachable, 0);
747 ASSERT_TRUE(
748 transformation.IsApplicable(context.get(), transformation_context));
749 transformation.Apply(context.get(), &transformation_context);
750 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
751 kConsoleMessageConsumer));
752
753 std::string after_transformation = R"(
754 OpCapability Shader
755 %1 = OpExtInstImport "GLSL.std.450"
756 OpMemoryModel Logical GLSL450
757 OpEntryPoint Fragment %4 "main"
758 OpExecutionMode %4 OriginUpperLeft
759 OpSource ESSL 320
760 %2 = OpTypeVoid
761 %3 = OpTypeFunction %2
762 %6 = OpTypeBool
763 %7 = OpConstantTrue %6
764 %4 = OpFunction %2 None %3
765 %5 = OpLabel
766 OpSelectionMerge %8 None
767 OpBranchConditional %7 %9 %10
768 %9 = OpLabel
769 OpBranch %11
770 %11 = OpLabel
771 %12 = OpCopyObject %6 %7
772 OpBranch %8
773 %10 = OpLabel
774 OpBranch %13
775 %8 = OpLabel
776 OpReturn
777 %13 = OpLabel
778 OpUnreachable
779 OpFunctionEnd
780 )";
781
782 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
783 }
784
785 TEST(TransformationReplaceBranchFromDeadBlockWithExitTest,
786 BranchToOuterMergeBlock) {
787 std::string shader = R"(
788 OpCapability Shader
789 %1 = OpExtInstImport "GLSL.std.450"
790 OpMemoryModel Logical GLSL450
791 OpEntryPoint Fragment %4 "main"
792 OpExecutionMode %4 OriginUpperLeft
793 OpSource ESSL 320
794 %2 = OpTypeVoid
795 %3 = OpTypeFunction %2
796 %6 = OpTypeBool
797 %7 = OpConstantTrue %6
798 %15 = OpTypeInt 32 0
799 %14 = OpUndef %15
800 %4 = OpFunction %2 None %3
801 %5 = OpLabel
802 OpSelectionMerge %8 None
803 OpSwitch %14 %9 1 %8
804 %9 = OpLabel
805 OpSelectionMerge %10 None
806 OpBranchConditional %7 %11 %10
807 %8 = OpLabel
808 OpReturn
809 %11 = OpLabel
810 OpBranch %8
811 %10 = OpLabel
812 OpBranch %8
813 OpFunctionEnd
814 )";
815
816 const auto env = SPV_ENV_UNIVERSAL_1_4;
817 const auto consumer = nullptr;
818 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
819 spvtools::ValidatorOptions validator_options;
820 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
821 kConsoleMessageConsumer));
822 TransformationContext transformation_context(
823 MakeUnique<FactManager>(context.get()), validator_options);
824
825 transformation_context.GetFactManager()->AddFactBlockIsDead(10);
826
827 TransformationReplaceBranchFromDeadBlockWithExit transformation(
828 10, spv::Op::OpUnreachable, 0);
829 ASSERT_TRUE(
830 transformation.IsApplicable(context.get(), transformation_context));
831 transformation.Apply(context.get(), &transformation_context);
832 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
833 kConsoleMessageConsumer));
834
835 std::string after_transformation = R"(
836 OpCapability Shader
837 %1 = OpExtInstImport "GLSL.std.450"
838 OpMemoryModel Logical GLSL450
839 OpEntryPoint Fragment %4 "main"
840 OpExecutionMode %4 OriginUpperLeft
841 OpSource ESSL 320
842 %2 = OpTypeVoid
843 %3 = OpTypeFunction %2
844 %6 = OpTypeBool
845 %7 = OpConstantTrue %6
846 %15 = OpTypeInt 32 0
847 %14 = OpUndef %15
848 %4 = OpFunction %2 None %3
849 %5 = OpLabel
850 OpSelectionMerge %8 None
851 OpSwitch %14 %9 1 %8
852 %9 = OpLabel
853 OpSelectionMerge %10 None
854 OpBranchConditional %7 %11 %10
855 %8 = OpLabel
856 OpReturn
857 %11 = OpLabel
858 OpBranch %8
859 %10 = OpLabel
860 OpUnreachable
861 OpFunctionEnd
862 )";
863
864 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
865 }
866
867 } // namespace
868 } // namespace fuzz
869 } // namespace spvtools