Debian Codegraph @

test/fuzz/transformation_propagate_instruction_down_test.cpp

raw
1 // Copyright (c) 2020 Vasyl Teliman
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_propagate_instruction_down.h"
16
17 #include "gtest/gtest.h"
18 #include "source/fuzz/counter_overflow_id_source.h"
19 #include "source/fuzz/fuzzer_util.h"
20 #include "test/fuzz/fuzz_test_util.h"
21
22 namespace spvtools {
23 namespace fuzz {
24 namespace {
25
26 TEST(TransformationPropagateInstructionDownTest, BasicTest) {
27 std::string shader = R"(
28 OpCapability Shader
29 %1 = OpExtInstImport "GLSL.std.450"
30 OpMemoryModel Logical GLSL450
31 OpEntryPoint Fragment %4 "main"
32 OpExecutionMode %4 OriginUpperLeft
33 OpSource ESSL 310
34 %2 = OpTypeVoid
35 %3 = OpTypeFunction %2
36 %6 = OpTypeInt 32 1
37 %7 = OpConstant %6 1
38 %12 = OpTypeBool
39 %13 = OpConstantTrue %12
40 %9 = OpTypePointer Function %6
41 %4 = OpFunction %2 None %3
42
43 ; Has no instruction to propagate
44 %5 = OpLabel
45 %10 = OpVariable %9 Function
46 %8 = OpCopyObject %6 %7
47 OpStore %10 %8
48 OpBranch %11
49
50 ; Unreachable block
51 %100 = OpLabel
52 %101 = OpCopyObject %6 %7
53 OpBranch %11
54
55 ; Selection header
56 ;
57 ; One of acceptable successors has an OpPhi that uses propagated
58 ; instruction's id
59 %11 = OpLabel
60 %19 = OpCopyObject %6 %7
61 OpSelectionMerge %18 None
62 OpBranchConditional %13 %14 %18
63
64 ; %16 has no acceptable successors
65 %14 = OpLabel
66 %20 = OpPhi %6 %19 %11
67 %15 = OpCopyObject %6 %7 ; dependency
68 OpBranch %16
69 %16 = OpLabel
70 %17 = OpCopyObject %6 %15
71 OpBranch %18
72
73 ; Can be applied
74 %18 = OpLabel
75 %21 = OpCopyObject %6 %7
76 OpSelectionMerge %24 None
77 OpBranchConditional %13 %22 %23
78 %22 = OpLabel
79 %29 = OpPhi %6 %7 %18
80 OpStore %10 %21
81 OpBranch %24
82 %23 = OpLabel
83 OpStore %10 %21
84 OpBranch %24
85 %24 = OpLabel
86 OpStore %10 %21
87 OpBranch %32
88
89 ; Can't replace all uses of the propagated instruction: %30 is
90 ; propagated into %27.
91 %32 = OpLabel
92 OpLoopMerge %28 %27 None
93 OpBranchConditional %13 %26 %28
94 %26 = OpLabel
95 %25 = OpCopyObject %6 %7
96 %30 = OpCopyObject %6 %25
97 OpBranchConditional %13 %27 %28
98 %27 = OpLabel
99 OpBranch %32
100 %28 = OpLabel
101 %31 = OpPhi %6 %30 %26 %7 %32 ; Can't replace this use
102 OpReturn
103
104 OpFunctionEnd
105 )";
106
107 const auto env = SPV_ENV_UNIVERSAL_1_3;
108 const auto consumer = nullptr;
109 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
110 spvtools::ValidatorOptions validator_options;
111 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
112 kConsoleMessageConsumer));
113 TransformationContext transformation_context(
114 MakeUnique<FactManager>(context.get()), validator_options);
115
116 // Invalid block id.
117 ASSERT_FALSE(TransformationPropagateInstructionDown(200, 200, {{}})
118 .IsApplicable(context.get(), transformation_context));
119 ASSERT_FALSE(TransformationPropagateInstructionDown(101, 200, {{}})
120 .IsApplicable(context.get(), transformation_context));
121
122 // The block is unreachable.
123 ASSERT_FALSE(TransformationPropagateInstructionDown(100, 200, {{}})
124 .IsApplicable(context.get(), transformation_context));
125
126 // The block has no instruction to propagate.
127 ASSERT_FALSE(TransformationPropagateInstructionDown(5, 200, {{{11, 201}}})
128 .IsApplicable(context.get(), transformation_context));
129
130 // The block has no acceptable successors.
131 ASSERT_FALSE(TransformationPropagateInstructionDown(16, 200, {{{18, 201}}})
132 .IsApplicable(context.get(), transformation_context));
133
134 // One of acceptable successors has an OpPhi that uses propagated
135 // instruction's id.
136 ASSERT_FALSE(
137 TransformationPropagateInstructionDown(11, 200, {{{14, 201}, {18, 202}}})
138 .IsApplicable(context.get(), transformation_context));
139
140 #ifndef NDEBUG
141 // Not all fresh ids are provided.
142 ASSERT_DEATH(
143 TransformationPropagateInstructionDown(18, 200, {{{22, 201}, {202, 203}}})
144 .IsApplicable(context.get(), transformation_context),
145 "Bad attempt to query whether overflow ids are available.");
146 #endif
147
148 // Not all fresh ids are fresh.
149 ASSERT_FALSE(TransformationPropagateInstructionDown(
150 18, 18, {{{22, 201}, {23, 202}, {202, 203}}})
151 .IsApplicable(context.get(), transformation_context));
152 ASSERT_FALSE(TransformationPropagateInstructionDown(
153 18, 200, {{{22, 22}, {23, 202}, {202, 203}}})
154 .IsApplicable(context.get(), transformation_context));
155 ASSERT_FALSE(TransformationPropagateInstructionDown(
156 18, 18, {{{22, 22}, {23, 202}, {202, 203}}})
157 .IsApplicable(context.get(), transformation_context));
158
159 // Not all fresh ids are unique.
160 ASSERT_FALSE(TransformationPropagateInstructionDown(
161 18, 200, {{{22, 200}, {23, 202}, {202, 200}}})
162 .IsApplicable(context.get(), transformation_context));
163 ASSERT_FALSE(TransformationPropagateInstructionDown(
164 18, 200, {{{22, 201}, {23, 202}, {202, 200}}})
165 .IsApplicable(context.get(), transformation_context));
166 ASSERT_FALSE(TransformationPropagateInstructionDown(
167 18, 200, {{{22, 201}, {23, 201}, {202, 203}}})
168 .IsApplicable(context.get(), transformation_context));
169
170 // Can't replace all uses of the propagated instruction: %30 is propagated
171 // into %27.
172 ASSERT_FALSE(TransformationPropagateInstructionDown(26, 200, {{{27, 201}}})
173 .IsApplicable(context.get(), transformation_context));
174
175 {
176 TransformationPropagateInstructionDown transformation(
177 18, 200, {{{22, 201}, {23, 202}, {202, 203}}});
178 ASSERT_TRUE(
179 transformation.IsApplicable(context.get(), transformation_context));
180 ApplyAndCheckFreshIds(transformation, context.get(),
181 &transformation_context);
182 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
183 context.get(), validator_options, kConsoleMessageConsumer));
184
185 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
186 MakeDataDescriptor(201, {}), MakeDataDescriptor(202, {})));
187 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
188 MakeDataDescriptor(201, {}), MakeDataDescriptor(200, {})));
189 }
190
191 std::string after_transformation = R"(
192 OpCapability Shader
193 %1 = OpExtInstImport "GLSL.std.450"
194 OpMemoryModel Logical GLSL450
195 OpEntryPoint Fragment %4 "main"
196 OpExecutionMode %4 OriginUpperLeft
197 OpSource ESSL 310
198 %2 = OpTypeVoid
199 %3 = OpTypeFunction %2
200 %6 = OpTypeInt 32 1
201 %7 = OpConstant %6 1
202 %12 = OpTypeBool
203 %13 = OpConstantTrue %12
204 %9 = OpTypePointer Function %6
205 %4 = OpFunction %2 None %3
206
207 ; Has no instruction to propagate
208 %5 = OpLabel
209 %10 = OpVariable %9 Function
210 %8 = OpCopyObject %6 %7
211 OpStore %10 %8
212 OpBranch %11
213
214 ; Unreachable block
215 %100 = OpLabel
216 %101 = OpCopyObject %6 %7
217 OpBranch %11
218
219 ; Selection header
220 ;
221 ; One of acceptable successors has an OpPhi that uses propagated
222 ; instruction's id
223 %11 = OpLabel
224 %19 = OpCopyObject %6 %7
225 OpSelectionMerge %18 None
226 OpBranchConditional %13 %14 %18
227
228 ; %16 has no acceptable successors
229 %14 = OpLabel
230 %20 = OpPhi %6 %19 %11
231 %15 = OpCopyObject %6 %7 ; dependency
232 OpBranch %16
233 %16 = OpLabel
234 %17 = OpCopyObject %6 %15
235 OpBranch %18
236
237 ; Can be applied
238 %18 = OpLabel
239 OpSelectionMerge %24 None
240 OpBranchConditional %13 %22 %23
241 %22 = OpLabel
242 %29 = OpPhi %6 %7 %18
243 %201 = OpCopyObject %6 %7
244 OpStore %10 %201
245 OpBranch %24
246 %23 = OpLabel
247 %202 = OpCopyObject %6 %7
248 OpStore %10 %202
249 OpBranch %24
250 %24 = OpLabel
251 %200 = OpPhi %6 %201 %22 %202 %23
252 OpStore %10 %200
253 OpBranch %32
254
255 ; Can't replace all uses of the propagated instruction: %30 is
256 ; propagated into %27.
257 %32 = OpLabel
258 OpLoopMerge %28 %27 None
259 OpBranchConditional %13 %26 %28
260 %26 = OpLabel
261 %25 = OpCopyObject %6 %7
262 %30 = OpCopyObject %6 %25
263 OpBranchConditional %13 %27 %28
264 %27 = OpLabel
265 OpBranch %32
266 %28 = OpLabel
267 %31 = OpPhi %6 %30 %26 %7 %32 ; Can't replace this use
268 OpReturn
269
270 OpFunctionEnd
271 )";
272
273 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
274 }
275
276 TEST(TransformationPropagateInstructionDownTest, CantCreateOpPhiTest) {
277 std::string shader = R"(
278 OpCapability Shader
279 %1 = OpExtInstImport "GLSL.std.450"
280 OpMemoryModel Logical GLSL450
281 OpEntryPoint Fragment %4 "main"
282 OpExecutionMode %4 OriginUpperLeft
283 OpSource ESSL 310
284 %2 = OpTypeVoid
285 %3 = OpTypeFunction %2
286 %6 = OpTypeInt 32 1
287 %7 = OpConstant %6 1
288 %12 = OpTypeBool
289 %13 = OpConstantTrue %12
290 %4 = OpFunction %2 None %3
291
292 ; %5 doesn't belong to any construct
293 %5 = OpLabel
294 %15 = OpCopyObject %6 %7
295 OpBranch %16
296
297 ; The merge block (%19) is unreachable
298 %16 = OpLabel
299 %17 = OpCopyObject %6 %7
300 OpSelectionMerge %19 None
301 OpBranchConditional %13 %18 %18
302
303 ; %21 doesn't dominate the merge block - %20
304 %18 = OpLabel
305 OpSelectionMerge %20 None
306 OpBranchConditional %13 %20 %21
307 %21 = OpLabel
308 %22 = OpCopyObject %6 %7
309 OpBranch %20
310
311 ; The merge block (%24) is an acceptable successor of the propagated
312 ; instruction's block
313 %20 = OpLabel
314 %23 = OpCopyObject %6 %7
315 OpSelectionMerge %24 None
316 OpBranchConditional %13 %24 %30
317 %30 = OpLabel
318 OpBranch %24
319
320 ; One of the predecessors of the merge block is not dominated by any
321 ; successor of the propagated instruction's block
322 %24 = OpLabel
323 %26 = OpCopyObject %6 %7
324 OpLoopMerge %29 %25 None
325 OpBranch %25
326 %25 = OpLabel
327 OpBranchConditional %13 %24 %29
328 %28 = OpLabel ; unreachable predecessor of %29
329 OpBranch %29
330 %29 = OpLabel
331 OpReturn
332
333 %19 = OpLabel
334 OpReturn
335 OpFunctionEnd
336 )";
337
338 const auto env = SPV_ENV_UNIVERSAL_1_3;
339 const auto consumer = nullptr;
340 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
341 spvtools::ValidatorOptions validator_options;
342 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
343 kConsoleMessageConsumer));
344 TransformationContext transformation_context(
345 MakeUnique<FactManager>(context.get()), validator_options);
346
347 TransformationPropagateInstructionDown transformations[] = {
348 // %5 doesn't belong to any construct.
349 {5, 200, {{{16, 201}}}},
350
351 // The merge block (%19) is unreachable.
352 {16, 200, {{{18, 202}}}},
353
354 // %21 doesn't dominate the merge block - %20.
355 {21, 200, {{{20, 203}}}},
356
357 // The merge block (%24) is an acceptable successor of the propagated
358 // instruction's block.
359 {20, 200, {{{24, 204}, {30, 205}}}},
360
361 // One of the predecessors of the merge block is not dominated by any
362 // successor of the propagated instruction's block.
363 {24, 200, {{{25, 206}}}},
364 };
365
366 for (const auto& transformation : transformations) {
367 ASSERT_TRUE(
368 transformation.IsApplicable(context.get(), transformation_context));
369 ApplyAndCheckFreshIds(transformation, context.get(),
370 &transformation_context);
371 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
372 context.get(), validator_options, kConsoleMessageConsumer));
373 }
374
375 // No transformation has introduced an OpPhi instruction.
376 ASSERT_FALSE(context->get_def_use_mgr()->GetDef(200));
377
378 std::string after_transformation = R"(
379 OpCapability Shader
380 %1 = OpExtInstImport "GLSL.std.450"
381 OpMemoryModel Logical GLSL450
382 OpEntryPoint Fragment %4 "main"
383 OpExecutionMode %4 OriginUpperLeft
384 OpSource ESSL 310
385 %2 = OpTypeVoid
386 %3 = OpTypeFunction %2
387 %6 = OpTypeInt 32 1
388 %7 = OpConstant %6 1
389 %12 = OpTypeBool
390 %13 = OpConstantTrue %12
391 %4 = OpFunction %2 None %3
392
393 ; %5 doesn't belong to any construct
394 %5 = OpLabel
395 OpBranch %16
396
397 ; The merge block (%19) is unreachable
398 %16 = OpLabel
399 %201 = OpCopyObject %6 %7
400 OpSelectionMerge %19 None
401 OpBranchConditional %13 %18 %18
402
403 ; %21 doesn't dominate the merge block - %20
404 %18 = OpLabel
405 %202 = OpCopyObject %6 %7
406 OpSelectionMerge %20 None
407 OpBranchConditional %13 %20 %21
408 %21 = OpLabel
409 OpBranch %20
410
411 ; The merge block (%24) is an acceptable successor of the propagated
412 ; instruction's block
413 %20 = OpLabel
414 %203 = OpCopyObject %6 %7
415 OpSelectionMerge %24 None
416 OpBranchConditional %13 %24 %30
417 %30 = OpLabel
418 %205 = OpCopyObject %6 %7
419 OpBranch %24
420
421 ; One of the predecessors of the merge block is not dominated by any
422 ; successor of the propagated instruction's block
423 %24 = OpLabel
424 %204 = OpCopyObject %6 %7
425 OpLoopMerge %29 %25 None
426 OpBranch %25
427 %25 = OpLabel
428 %206 = OpCopyObject %6 %7
429 OpBranchConditional %13 %24 %29
430 %28 = OpLabel ; unreachable predecessor of %29
431 OpBranch %29
432 %29 = OpLabel
433 OpReturn
434
435 %19 = OpLabel
436 OpReturn
437 OpFunctionEnd
438 )";
439
440 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
441 }
442
443 TEST(TransformationPropagateInstructionDownTest, VariablePointersCapability) {
444 std::string shader = R"(
445 OpCapability Shader
446 %1 = OpExtInstImport "GLSL.std.450"
447 OpMemoryModel Logical GLSL450
448 OpEntryPoint Fragment %4 "main"
449 OpExecutionMode %4 OriginUpperLeft
450 OpSource ESSL 310
451 %2 = OpTypeVoid
452 %3 = OpTypeFunction %2
453 %6 = OpTypeInt 32 1
454 %7 = OpConstant %6 1
455 %12 = OpTypeBool
456 %13 = OpConstantTrue %12
457 %10 = OpTypePointer Workgroup %6
458 %11 = OpVariable %10 Workgroup
459 %4 = OpFunction %2 None %3
460 %5 = OpLabel
461 %18 = OpCopyObject %10 %11
462 %14 = OpCopyObject %10 %11
463 OpSelectionMerge %17 None
464 OpBranchConditional %13 %15 %16
465 %15 = OpLabel
466 OpBranch %17
467 %16 = OpLabel
468 OpBranch %17
469 %17 = OpLabel
470 OpStore %18 %7
471 OpReturn
472 OpFunctionEnd
473 )";
474
475 const auto env = SPV_ENV_UNIVERSAL_1_3;
476 const auto consumer = nullptr;
477 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
478 spvtools::ValidatorOptions validator_options;
479 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
480 kConsoleMessageConsumer));
481 TransformationContext transformation_context(
482 MakeUnique<FactManager>(context.get()), validator_options);
483
484 {
485 // Can propagate a pointer only if we don't have to create an OpPhi.
486 TransformationPropagateInstructionDown transformation(
487 5, 200, {{{15, 201}, {16, 202}}});
488 ASSERT_TRUE(
489 transformation.IsApplicable(context.get(), transformation_context));
490 ApplyAndCheckFreshIds(transformation, context.get(),
491 &transformation_context);
492 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
493 context.get(), validator_options, kConsoleMessageConsumer));
494 ASSERT_FALSE(context->get_def_use_mgr()->GetDef(200));
495 }
496 {
497 // Can't propagate a pointer if there is no VariablePointersStorageBuffer
498 // capability and we need to create an OpPhi.
499 TransformationPropagateInstructionDown transformation(
500 5, 200, {{{15, 203}, {16, 204}}});
501 ASSERT_FALSE(context->get_feature_mgr()->HasCapability(
502 spv::Capability::VariablePointersStorageBuffer));
503 ASSERT_FALSE(
504 transformation.IsApplicable(context.get(), transformation_context));
505
506 context->AddCapability(spv::Capability::VariablePointers);
507 ASSERT_TRUE(context->get_feature_mgr()->HasCapability(
508 spv::Capability::VariablePointersStorageBuffer));
509 ASSERT_TRUE(
510 transformation.IsApplicable(context.get(), transformation_context));
511 ApplyAndCheckFreshIds(transformation, context.get(),
512 &transformation_context);
513 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
514 context.get(), validator_options, kConsoleMessageConsumer));
515 }
516
517 std::string after_transformation = R"(
518 OpCapability Shader
519 OpCapability VariablePointers
520 %1 = OpExtInstImport "GLSL.std.450"
521 OpMemoryModel Logical GLSL450
522 OpEntryPoint Fragment %4 "main"
523 OpExecutionMode %4 OriginUpperLeft
524 OpSource ESSL 310
525 %2 = OpTypeVoid
526 %3 = OpTypeFunction %2
527 %6 = OpTypeInt 32 1
528 %7 = OpConstant %6 1
529 %12 = OpTypeBool
530 %13 = OpConstantTrue %12
531 %10 = OpTypePointer Workgroup %6
532 %11 = OpVariable %10 Workgroup
533 %4 = OpFunction %2 None %3
534 %5 = OpLabel
535 OpSelectionMerge %17 None
536 OpBranchConditional %13 %15 %16
537 %15 = OpLabel
538 %203 = OpCopyObject %10 %11
539 %201 = OpCopyObject %10 %11
540 OpBranch %17
541 %16 = OpLabel
542 %204 = OpCopyObject %10 %11
543 %202 = OpCopyObject %10 %11
544 OpBranch %17
545 %17 = OpLabel
546 %200 = OpPhi %10 %203 %15 %204 %16
547 OpStore %200 %7
548 OpReturn
549 OpFunctionEnd
550 )";
551
552 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
553 }
554
555 TEST(TransformationPropagateInstructionDownTest, UseOverflowIdsTest) {
556 std::string shader = R"(
557 OpCapability Shader
558 %1 = OpExtInstImport "GLSL.std.450"
559 OpMemoryModel Logical GLSL450
560 OpEntryPoint Fragment %4 "main"
561 OpExecutionMode %4 OriginUpperLeft
562 OpSource ESSL 310
563 %2 = OpTypeVoid
564 %3 = OpTypeFunction %2
565 %6 = OpTypeInt 32 1
566 %7 = OpConstant %6 1
567 %12 = OpTypeBool
568 %13 = OpConstantTrue %12
569 %10 = OpTypePointer Private %6
570 %11 = OpVariable %10 Private
571 %4 = OpFunction %2 None %3
572 %5 = OpLabel
573 %20 = OpCopyObject %6 %7
574 OpSelectionMerge %23 None
575 OpBranchConditional %13 %21 %22
576 %21 = OpLabel
577 OpStore %11 %20
578 OpBranch %23
579 %22 = OpLabel
580 OpStore %11 %20
581 OpBranch %23
582 %23 = OpLabel
583 OpStore %11 %20
584 OpReturn
585 OpFunctionEnd
586 )";
587
588 const auto env = SPV_ENV_UNIVERSAL_1_3;
589 const auto consumer = nullptr;
590 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
591 spvtools::ValidatorOptions validator_options;
592 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
593 kConsoleMessageConsumer));
594 TransformationContext transformation_context(
595 MakeUnique<FactManager>(context.get()), validator_options,
596 MakeUnique<CounterOverflowIdSource>(300));
597
598 TransformationPropagateInstructionDown transformation(5, 200, {{{21, 201}}});
599 ASSERT_TRUE(
600 transformation.IsApplicable(context.get(), transformation_context));
601 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context,
602 {300});
603 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
604 kConsoleMessageConsumer));
605
606 std::string after_transformation = R"(
607 OpCapability Shader
608 %1 = OpExtInstImport "GLSL.std.450"
609 OpMemoryModel Logical GLSL450
610 OpEntryPoint Fragment %4 "main"
611 OpExecutionMode %4 OriginUpperLeft
612 OpSource ESSL 310
613 %2 = OpTypeVoid
614 %3 = OpTypeFunction %2
615 %6 = OpTypeInt 32 1
616 %7 = OpConstant %6 1
617 %12 = OpTypeBool
618 %13 = OpConstantTrue %12
619 %10 = OpTypePointer Private %6
620 %11 = OpVariable %10 Private
621 %4 = OpFunction %2 None %3
622 %5 = OpLabel
623 OpSelectionMerge %23 None
624 OpBranchConditional %13 %21 %22
625 %21 = OpLabel
626 %201 = OpCopyObject %6 %7
627 OpStore %11 %201
628 OpBranch %23
629 %22 = OpLabel
630 %300 = OpCopyObject %6 %7
631 OpStore %11 %300
632 OpBranch %23
633 %23 = OpLabel
634 %200 = OpPhi %6 %201 %21 %300 %22
635 OpStore %11 %200
636 OpReturn
637 OpFunctionEnd
638 )";
639
640 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
641 }
642
643 TEST(TransformationPropagateInstructionDownTest, TestCreatedFacts) {
644 std::string shader = R"(
645 OpCapability VariablePointers
646 %1 = OpExtInstImport "GLSL.std.450"
647 OpMemoryModel Logical GLSL450
648 OpEntryPoint Fragment %4 "main"
649 OpExecutionMode %4 OriginUpperLeft
650 OpSource ESSL 310
651 %2 = OpTypeVoid
652 %3 = OpTypeFunction %2
653 %6 = OpTypeInt 32 1
654 %7 = OpConstant %6 1
655 %12 = OpTypeBool
656 %13 = OpConstantTrue %12
657 %10 = OpTypePointer Private %6
658 %11 = OpVariable %10 Private
659 %4 = OpFunction %2 None %3
660 %5 = OpLabel
661 %20 = OpCopyObject %6 %7
662 %24 = OpCopyObject %6 %7 ; Irrelevant id
663 %25 = OpCopyObject %10 %11 ; Pointee is irrelevant
664 OpSelectionMerge %23 None
665 OpBranchConditional %13 %21 %22
666 %21 = OpLabel
667 OpStore %25 %20
668 OpBranch %23
669 %22 = OpLabel ; Dead block
670 OpStore %25 %20
671 OpBranch %23
672 %23 = OpLabel
673 OpStore %25 %20
674 OpReturn
675 OpFunctionEnd
676 )";
677
678 const auto env = SPV_ENV_UNIVERSAL_1_3;
679 const auto consumer = nullptr;
680 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
681 spvtools::ValidatorOptions validator_options;
682 validator_options.SetRelaxLogicalPointer(true);
683 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
684 kConsoleMessageConsumer));
685 TransformationContext transformation_context(
686 MakeUnique<FactManager>(context.get()), validator_options);
687
688 transformation_context.GetFactManager()->AddFactBlockIsDead(22);
689 transformation_context.GetFactManager()->AddFactIdIsIrrelevant(24);
690 transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
691 25);
692
693 {
694 // Propagate pointer with PointeeIsIrrelevant fact.
695 TransformationPropagateInstructionDown transformation(
696 5, 200, {{{21, 201}, {22, 202}}});
697 ASSERT_TRUE(
698 transformation.IsApplicable(context.get(), transformation_context));
699 ApplyAndCheckFreshIds(transformation, context.get(),
700 &transformation_context);
701 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
702 context.get(), validator_options, kConsoleMessageConsumer));
703
704 ASSERT_FALSE(transformation_context.GetFactManager()->IdIsIrrelevant(201));
705 ASSERT_FALSE(transformation_context.GetFactManager()->IdIsIrrelevant(202));
706 ASSERT_FALSE(transformation_context.GetFactManager()->IdIsIrrelevant(200));
707
708 ASSERT_TRUE(
709 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(201));
710 ASSERT_TRUE(
711 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(202));
712 ASSERT_TRUE(
713 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(200));
714
715 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
716 MakeDataDescriptor(201, {}), MakeDataDescriptor(202, {})));
717 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
718 MakeDataDescriptor(201, {}), MakeDataDescriptor(200, {})));
719 }
720 {
721 // Propagate an irrelevant id.
722 TransformationPropagateInstructionDown transformation(
723 5, 203, {{{21, 204}, {22, 205}}});
724 ASSERT_TRUE(
725 transformation.IsApplicable(context.get(), transformation_context));
726 ApplyAndCheckFreshIds(transformation, context.get(),
727 &transformation_context);
728 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
729 context.get(), validator_options, kConsoleMessageConsumer));
730
731 ASSERT_TRUE(transformation_context.GetFactManager()->IdIsIrrelevant(203));
732 ASSERT_TRUE(transformation_context.GetFactManager()->IdIsIrrelevant(204));
733 ASSERT_TRUE(transformation_context.GetFactManager()->IdIsIrrelevant(205));
734
735 ASSERT_FALSE(
736 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(203));
737 ASSERT_FALSE(
738 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(204));
739 ASSERT_FALSE(
740 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(205));
741
742 ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
743 MakeDataDescriptor(204, {}), MakeDataDescriptor(205, {})));
744 ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
745 MakeDataDescriptor(204, {}), MakeDataDescriptor(203, {})));
746 }
747 {
748 // Propagate a regular id.
749 TransformationPropagateInstructionDown transformation(
750 5, 206, {{{21, 207}, {22, 208}}});
751 ASSERT_TRUE(
752 transformation.IsApplicable(context.get(), transformation_context));
753 ApplyAndCheckFreshIds(transformation, context.get(),
754 &transformation_context);
755 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
756 context.get(), validator_options, kConsoleMessageConsumer));
757
758 ASSERT_FALSE(transformation_context.GetFactManager()->IdIsIrrelevant(206));
759 ASSERT_FALSE(transformation_context.GetFactManager()->IdIsIrrelevant(207));
760 ASSERT_TRUE(transformation_context.GetFactManager()->IdIsIrrelevant(208));
761
762 ASSERT_FALSE(
763 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(206));
764 ASSERT_FALSE(
765 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(207));
766 ASSERT_FALSE(
767 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(208));
768
769 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
770 MakeDataDescriptor(206, {}), MakeDataDescriptor(207, {})));
771 ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
772 MakeDataDescriptor(206, {}), MakeDataDescriptor(208, {})));
773 }
774
775 std::string after_transformation = R"(
776 OpCapability VariablePointers
777 %1 = OpExtInstImport "GLSL.std.450"
778 OpMemoryModel Logical GLSL450
779 OpEntryPoint Fragment %4 "main"
780 OpExecutionMode %4 OriginUpperLeft
781 OpSource ESSL 310
782 %2 = OpTypeVoid
783 %3 = OpTypeFunction %2
784 %6 = OpTypeInt 32 1
785 %7 = OpConstant %6 1
786 %12 = OpTypeBool
787 %13 = OpConstantTrue %12
788 %10 = OpTypePointer Private %6
789 %11 = OpVariable %10 Private
790 %4 = OpFunction %2 None %3
791 %5 = OpLabel
792 OpSelectionMerge %23 None
793 OpBranchConditional %13 %21 %22
794 %21 = OpLabel
795 %207 = OpCopyObject %6 %7
796 %204 = OpCopyObject %6 %7 ; Irrelevant id
797 %201 = OpCopyObject %10 %11 ; Pointee is irrelevant
798 OpStore %201 %207
799 OpBranch %23
800 %22 = OpLabel ; Dead block
801 %208 = OpCopyObject %6 %7
802 %205 = OpCopyObject %6 %7 ; Irrelevant id
803 %202 = OpCopyObject %10 %11 ; Pointee is irrelevant
804 OpStore %202 %208
805 OpBranch %23
806 %23 = OpLabel
807 %206 = OpPhi %6 %207 %21 %208 %22
808 %203 = OpPhi %6 %204 %21 %205 %22 ; Irrelevant id
809 %200 = OpPhi %10 %201 %21 %202 %22 ; Pointee is irrelevant
810 OpStore %200 %206
811 OpReturn
812 OpFunctionEnd
813 )";
814
815 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
816 }
817
818 TEST(TransformationPropagateInstructionDownTest, TestLoops1) {
819 std::string shader = R"(
820 OpCapability Shader
821 %1 = OpExtInstImport "GLSL.std.450"
822 OpMemoryModel Logical GLSL450
823 OpEntryPoint Fragment %4 "main"
824 OpExecutionMode %4 OriginUpperLeft
825 OpSource ESSL 310
826 %2 = OpTypeVoid
827 %3 = OpTypeFunction %2
828 %6 = OpTypeInt 32 1
829 %7 = OpConstant %6 1
830 %12 = OpTypeBool
831 %13 = OpConstantTrue %12
832 %4 = OpFunction %2 None %3
833 %5 = OpLabel
834 OpBranch %20
835
836 %20 = OpLabel
837 OpLoopMerge %26 %25 None
838 OpBranch %21
839
840 %21 = OpLabel
841 %22 = OpCopyObject %6 %7
842 %31 = OpCopyObject %6 %7
843 OpSelectionMerge %35 None
844 OpBranchConditional %13 %23 %24
845
846 %23 = OpLabel
847 %27 = OpCopyObject %6 %22
848 %32 = OpCopyObject %6 %31
849 OpBranch %26
850 %24 = OpLabel
851 %28 = OpCopyObject %6 %22
852 %33 = OpCopyObject %6 %31
853 OpBranchConditional %13 %26 %25
854
855 %35 = OpLabel
856 OpBranch %25
857
858 %25 = OpLabel
859 %29 = OpCopyObject %6 %22
860 %34 = OpCopyObject %6 %31
861 OpBranch %20
862 %26 = OpLabel
863 %30 = OpCopyObject %6 %22
864 OpReturn
865 OpFunctionEnd
866 )";
867
868 const auto env = SPV_ENV_UNIVERSAL_1_3;
869 const auto consumer = nullptr;
870 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
871 spvtools::ValidatorOptions validator_options;
872 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
873 kConsoleMessageConsumer));
874 TransformationContext transformation_context(
875 MakeUnique<FactManager>(context.get()), validator_options);
876
877 {
878 TransformationPropagateInstructionDown transformation(
879 21, 200, {{{23, 201}, {24, 202}}});
880 ASSERT_TRUE(
881 transformation.IsApplicable(context.get(), transformation_context));
882 ApplyAndCheckFreshIds(transformation, context.get(),
883 &transformation_context);
884 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
885 context.get(), validator_options, kConsoleMessageConsumer));
886 }
887
888 // Can't replace usage of %22 in %26.
889 ASSERT_FALSE(
890 TransformationPropagateInstructionDown(21, 200, {{{23, 201}, {24, 202}}})
891 .IsApplicable(context.get(), transformation_context));
892
893 std::string after_transformation = R"(
894 OpCapability Shader
895 %1 = OpExtInstImport "GLSL.std.450"
896 OpMemoryModel Logical GLSL450
897 OpEntryPoint Fragment %4 "main"
898 OpExecutionMode %4 OriginUpperLeft
899 OpSource ESSL 310
900 %2 = OpTypeVoid
901 %3 = OpTypeFunction %2
902 %6 = OpTypeInt 32 1
903 %7 = OpConstant %6 1
904 %12 = OpTypeBool
905 %13 = OpConstantTrue %12
906 %4 = OpFunction %2 None %3
907 %5 = OpLabel
908 OpBranch %20
909
910 %20 = OpLabel
911 OpLoopMerge %26 %25 None
912 OpBranch %21
913
914 %21 = OpLabel
915 %22 = OpCopyObject %6 %7
916 OpSelectionMerge %35 None
917 OpBranchConditional %13 %23 %24
918 %23 = OpLabel
919 %201 = OpCopyObject %6 %7
920 %27 = OpCopyObject %6 %22
921 %32 = OpCopyObject %6 %201
922 OpBranch %26
923 %24 = OpLabel
924 %202 = OpCopyObject %6 %7
925 %28 = OpCopyObject %6 %22
926 %33 = OpCopyObject %6 %202
927 OpBranchConditional %13 %26 %25
928
929 %35 = OpLabel
930 OpBranch %25
931
932 %25 = OpLabel
933 %29 = OpCopyObject %6 %22
934 %34 = OpCopyObject %6 %202
935 OpBranch %20
936 %26 = OpLabel
937 %30 = OpCopyObject %6 %22
938 OpReturn
939 OpFunctionEnd
940 )";
941
942 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
943 }
944
945 TEST(TransformationPropagateInstructionDownTest, TestLoops2) {
946 std::string shader = R"(
947 OpCapability Shader
948 %1 = OpExtInstImport "GLSL.std.450"
949 OpMemoryModel Logical GLSL450
950 OpEntryPoint Fragment %4 "main"
951 OpExecutionMode %4 OriginUpperLeft
952 OpSource ESSL 310
953 %2 = OpTypeVoid
954 %3 = OpTypeFunction %2
955 %6 = OpTypeInt 32 1
956 %7 = OpConstant %6 1
957 %12 = OpTypeBool
958 %13 = OpConstantTrue %12
959 %4 = OpFunction %2 None %3
960 %5 = OpLabel
961 OpBranch %20
962
963 %20 = OpLabel
964 %23 = OpPhi %6 %7 %5 %24 %21
965 OpLoopMerge %22 %21 None
966 OpBranch %21
967
968 %21 = OpLabel
969 %24 = OpCopyObject %6 %23
970 %25 = OpCopyObject %6 %7
971 OpBranchConditional %13 %22 %20
972
973 %22 = OpLabel
974 OpReturn
975 OpFunctionEnd
976 )";
977
978 const auto env = SPV_ENV_UNIVERSAL_1_3;
979 const auto consumer = nullptr;
980 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
981 spvtools::ValidatorOptions validator_options;
982 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
983 kConsoleMessageConsumer));
984 TransformationContext transformation_context(
985 MakeUnique<FactManager>(context.get()), validator_options);
986
987 {
988 // Can propagate %25 from %21 into %20.
989 TransformationPropagateInstructionDown transformation(
990 21, 200, {{{20, 201}, {22, 202}}});
991 ASSERT_TRUE(
992 transformation.IsApplicable(context.get(), transformation_context));
993 ApplyAndCheckFreshIds(transformation, context.get(),
994 &transformation_context);
995 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
996 context.get(), validator_options, kConsoleMessageConsumer));
997 }
998 {
999 // Can propagate %201 from %20 into %21.
1000 TransformationPropagateInstructionDown transformation(20, 200,
1001 {{{21, 203}}});
1002 ASSERT_TRUE(
1003 transformation.IsApplicable(context.get(), transformation_context));
1004 ApplyAndCheckFreshIds(transformation, context.get(),
1005 &transformation_context);
1006 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1007 context.get(), validator_options, kConsoleMessageConsumer));
1008 }
1009
1010 // Can't propagate %24 from %21 into %20.
1011 ASSERT_FALSE(
1012 TransformationPropagateInstructionDown(21, 200, {{{20, 204}, {22, 205}}})
1013 .IsApplicable(context.get(), transformation_context));
1014
1015 std::string after_transformation = R"(
1016 OpCapability Shader
1017 %1 = OpExtInstImport "GLSL.std.450"
1018 OpMemoryModel Logical GLSL450
1019 OpEntryPoint Fragment %4 "main"
1020 OpExecutionMode %4 OriginUpperLeft
1021 OpSource ESSL 310
1022 %2 = OpTypeVoid
1023 %3 = OpTypeFunction %2
1024 %6 = OpTypeInt 32 1
1025 %7 = OpConstant %6 1
1026 %12 = OpTypeBool
1027 %13 = OpConstantTrue %12
1028 %4 = OpFunction %2 None %3
1029 %5 = OpLabel
1030 OpBranch %20
1031
1032 %20 = OpLabel
1033 %23 = OpPhi %6 %7 %5 %24 %21
1034 OpLoopMerge %22 %21 None
1035 OpBranch %21
1036
1037 %21 = OpLabel
1038 %203 = OpCopyObject %6 %7
1039 %24 = OpCopyObject %6 %23
1040 OpBranchConditional %13 %22 %20
1041
1042 %22 = OpLabel
1043 %200 = OpPhi %6 %203 %21
1044 %202 = OpCopyObject %6 %7
1045 OpReturn
1046 OpFunctionEnd
1047 )";
1048
1049 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
1050 }
1051
1052 TEST(TransformationPropagateInstructionDownTest, TestLoops3) {
1053 std::string shader = R"(
1054 OpCapability Shader
1055 %1 = OpExtInstImport "GLSL.std.450"
1056 OpMemoryModel Logical GLSL450
1057 OpEntryPoint Fragment %4 "main"
1058 OpExecutionMode %4 OriginUpperLeft
1059 OpSource ESSL 310
1060 %2 = OpTypeVoid
1061 %3 = OpTypeFunction %2
1062 %6 = OpTypeInt 32 1
1063 %7 = OpConstant %6 1
1064 %12 = OpTypeBool
1065 %13 = OpConstantTrue %12
1066 %4 = OpFunction %2 None %3
1067 %5 = OpLabel
1068 OpBranch %20
1069
1070 %20 = OpLabel
1071 %27 = OpPhi %6 %7 %5 %26 %20
1072 %25 = OpCopyObject %6 %7
1073 %26 = OpCopyObject %6 %7
1074 OpLoopMerge %22 %20 None
1075 OpBranchConditional %13 %20 %22
1076
1077 %22 = OpLabel
1078 OpReturn
1079 OpFunctionEnd
1080 )";
1081
1082 const auto env = SPV_ENV_UNIVERSAL_1_3;
1083 const auto consumer = nullptr;
1084 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1085 spvtools::ValidatorOptions validator_options;
1086 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1087 kConsoleMessageConsumer));
1088 TransformationContext transformation_context(
1089 MakeUnique<FactManager>(context.get()), validator_options);
1090
1091 {
1092 // Propagate %25 into %20 and %22. Not that we are skipping %26 since not
1093 // all of its users are in different blocks (%27).h
1094 TransformationPropagateInstructionDown transformation(
1095 20, 200, {{{20, 201}, {22, 202}}});
1096 ASSERT_TRUE(
1097 transformation.IsApplicable(context.get(), transformation_context));
1098 ApplyAndCheckFreshIds(transformation, context.get(),
1099 &transformation_context);
1100 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(
1101 context.get(), validator_options, kConsoleMessageConsumer));
1102 }
1103
1104 std::string after_transformation = R"(
1105 OpCapability Shader
1106 %1 = OpExtInstImport "GLSL.std.450"
1107 OpMemoryModel Logical GLSL450
1108 OpEntryPoint Fragment %4 "main"
1109 OpExecutionMode %4 OriginUpperLeft
1110 OpSource ESSL 310
1111 %2 = OpTypeVoid
1112 %3 = OpTypeFunction %2
1113 %6 = OpTypeInt 32 1
1114 %7 = OpConstant %6 1
1115 %12 = OpTypeBool
1116 %13 = OpConstantTrue %12
1117 %4 = OpFunction %2 None %3
1118 %5 = OpLabel
1119 OpBranch %20
1120
1121 %20 = OpLabel
1122 %27 = OpPhi %6 %7 %5 %26 %20
1123 %201 = OpCopyObject %6 %7
1124 %26 = OpCopyObject %6 %7
1125 OpLoopMerge %22 %20 None
1126 OpBranchConditional %13 %20 %22
1127
1128 %22 = OpLabel
1129 %202 = OpCopyObject %6 %7
1130 OpReturn
1131 OpFunctionEnd
1132 )";
1133
1134 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
1135 }
1136
1137 } // namespace
1138 } // namespace fuzz
1139 } // namespace spvtools