Debian Codegraph @

third_party/spirv-tools/test/fuzz/transformation_duplicate_region_with_selection_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_duplicate_region_with_selection.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(TransformationDuplicateRegionWithSelectionTest, BasicUseTest) {
27 // This test handles a case where the ids from the original region are used in
28 // subsequent block.
29
30 std::string shader = R"(
31 OpCapability Shader
32 OpCapability VariablePointers
33 %1 = OpExtInstImport "GLSL.std.450"
34 OpMemoryModel Logical GLSL450
35 OpEntryPoint Fragment %4 "main"
36 OpExecutionMode %4 OriginUpperLeft
37 OpSource ESSL 310
38 OpName %4 "main"
39 OpName %10 "fun(i1;"
40 OpName %9 "a"
41 OpName %12 "b"
42 OpName %18 "c"
43 OpName %20 "param"
44 %2 = OpTypeVoid
45 %3 = OpTypeFunction %2
46 %6 = OpTypeInt 32 1
47 %7 = OpTypePointer Function %6
48 %8 = OpTypeFunction %2 %7
49 %14 = OpConstant %6 2
50 %16 = OpTypeBool
51 %17 = OpTypePointer Function %16
52 %19 = OpConstantTrue %16
53 %4 = OpFunction %2 None %3
54 %5 = OpLabel
55 %18 = OpVariable %17 Function
56 %20 = OpVariable %7 Function
57 OpStore %18 %19
58 OpStore %20 %14
59 %21 = OpFunctionCall %2 %10 %20
60 OpReturn
61 OpFunctionEnd
62 %10 = OpFunction %2 None %8
63 %9 = OpFunctionParameter %7
64 %11 = OpLabel
65 %12 = OpVariable %7 Function
66 OpBranch %800
67 %800 = OpLabel
68 %13 = OpLoad %6 %9
69 %15 = OpIAdd %6 %13 %14
70 OpStore %12 %15
71 OpBranch %900
72 %900 = OpLabel
73 %901 = OpIAdd %6 %15 %13
74 %902 = OpISub %6 %13 %15
75 OpReturn
76 OpFunctionEnd
77 )";
78
79 const auto env = SPV_ENV_UNIVERSAL_1_4;
80 const auto consumer = nullptr;
81 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
82 spvtools::ValidatorOptions validator_options;
83 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
84 kConsoleMessageConsumer));
85 TransformationContext transformation_context(
86 MakeUnique<FactManager>(context.get()), validator_options);
87 TransformationDuplicateRegionWithSelection transformation_good_1 =
88 TransformationDuplicateRegionWithSelection(
89 500, 19, 501, 800, 800, {{800, 100}}, {{13, 201}, {15, 202}},
90 {{13, 301}, {15, 302}});
91
92 ASSERT_TRUE(transformation_good_1.IsApplicable(context.get(),
93 transformation_context));
94 ApplyAndCheckFreshIds(transformation_good_1, context.get(),
95 &transformation_context);
96
97 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
98 kConsoleMessageConsumer));
99 std::string expected_shader = R"(
100 OpCapability Shader
101 OpCapability VariablePointers
102 %1 = OpExtInstImport "GLSL.std.450"
103 OpMemoryModel Logical GLSL450
104 OpEntryPoint Fragment %4 "main"
105 OpExecutionMode %4 OriginUpperLeft
106 OpSource ESSL 310
107 OpName %4 "main"
108 OpName %10 "fun(i1;"
109 OpName %9 "a"
110 OpName %12 "b"
111 OpName %18 "c"
112 OpName %20 "param"
113 %2 = OpTypeVoid
114 %3 = OpTypeFunction %2
115 %6 = OpTypeInt 32 1
116 %7 = OpTypePointer Function %6
117 %8 = OpTypeFunction %2 %7
118 %14 = OpConstant %6 2
119 %16 = OpTypeBool
120 %17 = OpTypePointer Function %16
121 %19 = OpConstantTrue %16
122 %4 = OpFunction %2 None %3
123 %5 = OpLabel
124 %18 = OpVariable %17 Function
125 %20 = OpVariable %7 Function
126 OpStore %18 %19
127 OpStore %20 %14
128 %21 = OpFunctionCall %2 %10 %20
129 OpReturn
130 OpFunctionEnd
131 %10 = OpFunction %2 None %8
132 %9 = OpFunctionParameter %7
133 %11 = OpLabel
134 %12 = OpVariable %7 Function
135 OpBranch %500
136 %500 = OpLabel
137 OpSelectionMerge %501 None
138 OpBranchConditional %19 %800 %100
139 %800 = OpLabel
140 %13 = OpLoad %6 %9
141 %15 = OpIAdd %6 %13 %14
142 OpStore %12 %15
143 OpBranch %501
144 %100 = OpLabel
145 %201 = OpLoad %6 %9
146 %202 = OpIAdd %6 %201 %14
147 OpStore %12 %202
148 OpBranch %501
149 %501 = OpLabel
150 %301 = OpPhi %6 %13 %800 %201 %100
151 %302 = OpPhi %6 %15 %800 %202 %100
152 OpBranch %900
153 %900 = OpLabel
154 %901 = OpIAdd %6 %302 %301
155 %902 = OpISub %6 %301 %302
156 OpReturn
157 OpFunctionEnd
158 )";
159 ASSERT_TRUE(IsEqual(env, expected_shader, context.get()));
160 }
161
162 TEST(TransformationDuplicateRegionWithSelectionTest, BasicExitBlockTest) {
163 // This test handles a case where the exit block of the region is the exit
164 // block of the containing function.
165
166 std::string shader = R"(
167 OpCapability Shader
168 OpCapability VariablePointers
169 %1 = OpExtInstImport "GLSL.std.450"
170 OpMemoryModel Logical GLSL450
171 OpEntryPoint Fragment %4 "main"
172 OpExecutionMode %4 OriginUpperLeft
173 OpSource ESSL 310
174 OpName %4 "main"
175 OpName %10 "fun(i1;"
176 OpName %9 "a"
177 OpName %12 "b"
178 OpName %18 "c"
179 OpName %20 "param"
180 %2 = OpTypeVoid
181 %3 = OpTypeFunction %2
182 %6 = OpTypeInt 32 1
183 %7 = OpTypePointer Function %6
184 %8 = OpTypeFunction %2 %7
185 %14 = OpConstant %6 2
186 %16 = OpTypeBool
187 %17 = OpTypePointer Function %16
188 %19 = OpConstantTrue %16
189 %4 = OpFunction %2 None %3
190 %5 = OpLabel
191 %18 = OpVariable %17 Function
192 %20 = OpVariable %7 Function
193 OpStore %18 %19
194 OpStore %20 %14
195 %21 = OpFunctionCall %2 %10 %20
196 OpReturn
197 OpFunctionEnd
198 %10 = OpFunction %2 None %8
199 %9 = OpFunctionParameter %7
200 %11 = OpLabel
201 %12 = OpVariable %7 Function
202 OpBranch %800
203 %800 = OpLabel
204 %13 = OpLoad %6 %9
205 %15 = OpIAdd %6 %13 %14
206 OpStore %12 %15
207 OpReturn
208 OpFunctionEnd
209 )";
210
211 const auto env = SPV_ENV_UNIVERSAL_1_4;
212 const auto consumer = nullptr;
213 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
214 spvtools::ValidatorOptions validator_options;
215 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
216 kConsoleMessageConsumer));
217 TransformationContext transformation_context(
218 MakeUnique<FactManager>(context.get()), validator_options);
219 TransformationDuplicateRegionWithSelection transformation_good_1 =
220 TransformationDuplicateRegionWithSelection(
221 500, 19, 501, 800, 800, {{800, 100}}, {{13, 201}, {15, 202}},
222 {{13, 301}, {15, 302}});
223
224 ASSERT_TRUE(transformation_good_1.IsApplicable(context.get(),
225 transformation_context));
226 ApplyAndCheckFreshIds(transformation_good_1, context.get(),
227 &transformation_context);
228
229 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
230 kConsoleMessageConsumer));
231
232 std::string expected_shader = R"(
233 OpCapability Shader
234 OpCapability VariablePointers
235 %1 = OpExtInstImport "GLSL.std.450"
236 OpMemoryModel Logical GLSL450
237 OpEntryPoint Fragment %4 "main"
238 OpExecutionMode %4 OriginUpperLeft
239 OpSource ESSL 310
240 OpName %4 "main"
241 OpName %10 "fun(i1;"
242 OpName %9 "a"
243 OpName %12 "b"
244 OpName %18 "c"
245 OpName %20 "param"
246 %2 = OpTypeVoid
247 %3 = OpTypeFunction %2
248 %6 = OpTypeInt 32 1
249 %7 = OpTypePointer Function %6
250 %8 = OpTypeFunction %2 %7
251 %14 = OpConstant %6 2
252 %16 = OpTypeBool
253 %17 = OpTypePointer Function %16
254 %19 = OpConstantTrue %16
255 %4 = OpFunction %2 None %3
256 %5 = OpLabel
257 %18 = OpVariable %17 Function
258 %20 = OpVariable %7 Function
259 OpStore %18 %19
260 OpStore %20 %14
261 %21 = OpFunctionCall %2 %10 %20
262 OpReturn
263 OpFunctionEnd
264 %10 = OpFunction %2 None %8
265 %9 = OpFunctionParameter %7
266 %11 = OpLabel
267 %12 = OpVariable %7 Function
268 OpBranch %500
269 %500 = OpLabel
270 OpSelectionMerge %501 None
271 OpBranchConditional %19 %800 %100
272 %800 = OpLabel
273 %13 = OpLoad %6 %9
274 %15 = OpIAdd %6 %13 %14
275 OpStore %12 %15
276 OpBranch %501
277 %100 = OpLabel
278 %201 = OpLoad %6 %9
279 %202 = OpIAdd %6 %201 %14
280 OpStore %12 %202
281 OpBranch %501
282 %501 = OpLabel
283 %301 = OpPhi %6 %13 %800 %201 %100
284 %302 = OpPhi %6 %15 %800 %202 %100
285 OpReturn
286 OpFunctionEnd
287
288 )";
289 ASSERT_TRUE(IsEqual(env, expected_shader, context.get()));
290 }
291
292 TEST(TransformationDuplicateRegionWithSelectionTest, NotApplicableCFGTest) {
293 // This test handles few cases where the transformation is not applicable
294 // because of the control flow graph or layout of the blocks.
295
296 std::string shader = R"(
297 OpCapability Shader
298 %1 = OpExtInstImport "GLSL.std.450"
299 OpMemoryModel Logical GLSL450
300 OpEntryPoint Fragment %4 "main"
301 OpExecutionMode %4 OriginUpperLeft
302 OpSource ESSL 310
303 OpName %4 "main"
304 OpName %10 "fun(i1;"
305 OpName %9 "a"
306 OpName %18 "b"
307 OpName %25 "c"
308 OpName %27 "param"
309 %2 = OpTypeVoid
310 %3 = OpTypeFunction %2
311 %6 = OpTypeInt 32 1
312 %7 = OpTypePointer Function %6
313 %8 = OpTypeFunction %2 %7
314 %13 = OpConstant %6 2
315 %14 = OpTypeBool
316 %24 = OpTypePointer Function %14
317 %26 = OpConstantTrue %14
318 %4 = OpFunction %2 None %3
319 %5 = OpLabel
320 %25 = OpVariable %24 Function
321 %27 = OpVariable %7 Function
322 OpStore %25 %26
323 OpStore %27 %13
324 %28 = OpFunctionCall %2 %10 %27
325 OpReturn
326 OpFunctionEnd
327 %10 = OpFunction %2 None %8
328 %9 = OpFunctionParameter %7
329 %11 = OpLabel
330 %18 = OpVariable %7 Function
331 %12 = OpLoad %6 %9
332 %15 = OpSLessThan %14 %12 %13
333 OpSelectionMerge %17 None
334 OpBranchConditional %15 %16 %21
335 %16 = OpLabel
336 %19 = OpLoad %6 %9
337 %20 = OpIAdd %6 %19 %13
338 OpStore %18 %20
339 OpBranch %17
340 %21 = OpLabel
341 %22 = OpLoad %6 %9
342 %23 = OpISub %6 %22 %13
343 OpStore %18 %23
344 OpBranch %17
345 %17 = OpLabel
346 OpReturn
347 OpFunctionEnd
348 )";
349
350 const auto env = SPV_ENV_UNIVERSAL_1_4;
351 const auto consumer = nullptr;
352 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
353 spvtools::ValidatorOptions validator_options;
354 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
355 kConsoleMessageConsumer));
356 TransformationContext transformation_context(
357 MakeUnique<FactManager>(context.get()), validator_options);
358 // Bad: |entry_block_id| refers to the entry block of the function (this
359 // transformation currently avoids such cases).
360 TransformationDuplicateRegionWithSelection transformation_bad_1 =
361 TransformationDuplicateRegionWithSelection(
362 500, 26, 501, 11, 11, {{11, 100}}, {{18, 201}, {12, 202}, {15, 203}},
363 {{18, 301}, {12, 302}, {15, 303}});
364 ASSERT_FALSE(
365 transformation_bad_1.IsApplicable(context.get(), transformation_context));
366
367 // Bad: The block with id 16 does not dominate the block with id 21.
368 TransformationDuplicateRegionWithSelection transformation_bad_2 =
369 TransformationDuplicateRegionWithSelection(
370 500, 26, 501, 16, 21, {{16, 100}, {21, 101}},
371 {{19, 201}, {20, 202}, {22, 203}, {23, 204}},
372 {{19, 301}, {20, 302}, {22, 303}, {23, 304}});
373 ASSERT_FALSE(
374 transformation_bad_2.IsApplicable(context.get(), transformation_context));
375
376 // Bad: The block with id 21 does not post-dominate the block with id 11.
377 TransformationDuplicateRegionWithSelection transformation_bad_3 =
378 TransformationDuplicateRegionWithSelection(
379 500, 26, 501, 11, 21, {{11, 100}, {21, 101}},
380 {{18, 201}, {12, 202}, {15, 203}, {22, 204}, {23, 205}},
381 {{18, 301}, {12, 302}, {15, 303}, {22, 304}, {23, 305}});
382 ASSERT_FALSE(
383 transformation_bad_3.IsApplicable(context.get(), transformation_context));
384
385 // Bad: The block with id 5 is contained in a different function than the
386 // block with id 11.
387 TransformationDuplicateRegionWithSelection transformation_bad_4 =
388 TransformationDuplicateRegionWithSelection(
389 500, 26, 501, 5, 11, {{5, 100}, {11, 101}},
390 {{25, 201}, {27, 202}, {28, 203}, {18, 204}, {12, 205}, {15, 206}},
391 {{25, 301}, {27, 302}, {28, 303}, {18, 304}, {12, 305}, {15, 306}});
392 ASSERT_FALSE(
393 transformation_bad_4.IsApplicable(context.get(), transformation_context));
394 }
395
396 TEST(TransformationDuplicateRegionWithSelectionTest, NotApplicableIdTest) {
397 // This test handles a case where the supplied ids are either not fresh, not
398 // distinct, not valid in their context or do not refer to the existing
399 // instructions.
400
401 std::string shader = R"(
402 OpCapability Shader
403 OpCapability VariablePointers
404 %1 = OpExtInstImport "GLSL.std.450"
405 OpMemoryModel Logical GLSL450
406 OpEntryPoint Fragment %4 "main"
407 OpExecutionMode %4 OriginUpperLeft
408 OpSource ESSL 310
409 OpName %4 "main"
410 OpName %10 "fun(i1;"
411 OpName %9 "a"
412 OpName %12 "b"
413 OpName %18 "c"
414 OpName %20 "param"
415 %2 = OpTypeVoid
416 %3 = OpTypeFunction %2
417 %6 = OpTypeInt 32 1
418 %7 = OpTypePointer Function %6
419 %8 = OpTypeFunction %2 %7
420 %14 = OpConstant %6 2
421 %16 = OpTypeBool
422 %17 = OpTypePointer Function %16
423 %19 = OpConstantTrue %16
424 %4 = OpFunction %2 None %3
425 %5 = OpLabel
426 %18 = OpVariable %17 Function
427 %20 = OpVariable %7 Function
428 OpStore %18 %19
429 OpStore %20 %14
430 %21 = OpFunctionCall %2 %10 %20
431 OpReturn
432 OpFunctionEnd
433 %10 = OpFunction %2 None %8
434 %9 = OpFunctionParameter %7
435 %11 = OpLabel
436 %12 = OpVariable %7 Function
437 OpBranch %800
438 %800 = OpLabel
439 %13 = OpLoad %6 %9
440 %15 = OpIAdd %6 %13 %14
441 OpStore %12 %15
442 OpReturn
443 OpFunctionEnd
444 )";
445
446 const auto env = SPV_ENV_UNIVERSAL_1_4;
447 const auto consumer = nullptr;
448 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
449 spvtools::ValidatorOptions validator_options;
450 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
451 kConsoleMessageConsumer));
452 TransformationContext transformation_context(
453 MakeUnique<FactManager>(context.get()), validator_options);
454 // Bad: A value in the |original_label_to_duplicate_label| is not a fresh id.
455 TransformationDuplicateRegionWithSelection transformation_bad_1 =
456 TransformationDuplicateRegionWithSelection(
457 500, 19, 501, 800, 800, {{800, 21}}, {{13, 201}, {15, 202}},
458 {{13, 301}, {15, 302}});
459
460 ASSERT_FALSE(
461 transformation_bad_1.IsApplicable(context.get(), transformation_context));
462
463 // Bad: Values in the |original_id_to_duplicate_id| are not distinct.
464 TransformationDuplicateRegionWithSelection transformation_bad_2 =
465 TransformationDuplicateRegionWithSelection(
466 500, 19, 501, 800, 800, {{800, 100}}, {{13, 201}, {15, 201}},
467 {{13, 301}, {15, 302}});
468 ASSERT_FALSE(
469 transformation_bad_2.IsApplicable(context.get(), transformation_context));
470
471 // Bad: Values in the |original_id_to_phi_id| are not fresh and are not
472 // distinct with previous values.
473 TransformationDuplicateRegionWithSelection transformation_bad_3 =
474 TransformationDuplicateRegionWithSelection(
475 500, 19, 501, 800, 800, {{800, 100}}, {{13, 201}, {15, 202}},
476 {{13, 18}, {15, 202}});
477 ASSERT_FALSE(
478 transformation_bad_3.IsApplicable(context.get(), transformation_context));
479
480 // Bad: |entry_block_id| does not refer to an existing instruction.
481 TransformationDuplicateRegionWithSelection transformation_bad_4 =
482 TransformationDuplicateRegionWithSelection(
483 500, 19, 501, 802, 800, {{800, 100}}, {{13, 201}, {15, 202}},
484 {{13, 301}, {15, 302}});
485 ASSERT_FALSE(
486 transformation_bad_4.IsApplicable(context.get(), transformation_context));
487
488 // Bad: |exit_block_id| does not refer to a block.
489 TransformationDuplicateRegionWithSelection transformation_bad_5 =
490 TransformationDuplicateRegionWithSelection(
491 500, 19, 501, 800, 9, {{800, 100}}, {{13, 201}, {15, 202}},
492 {{13, 301}, {15, 302}});
493 ASSERT_FALSE(
494 transformation_bad_5.IsApplicable(context.get(), transformation_context));
495
496 // Bad: |new_entry_fresh_id| is not fresh.
497 TransformationDuplicateRegionWithSelection transformation_bad_6 =
498 TransformationDuplicateRegionWithSelection(
499 20, 19, 501, 800, 800, {{800, 100}}, {{13, 201}, {15, 202}},
500 {{13, 301}, {15, 302}});
501 ASSERT_FALSE(
502 transformation_bad_6.IsApplicable(context.get(), transformation_context));
503
504 // Bad: |merge_label_fresh_id| is not fresh.
505 TransformationDuplicateRegionWithSelection transformation_bad_7 =
506 TransformationDuplicateRegionWithSelection(
507 500, 19, 20, 800, 800, {{800, 100}}, {{13, 201}, {15, 202}},
508 {{13, 301}, {15, 302}});
509 ASSERT_FALSE(
510 transformation_bad_7.IsApplicable(context.get(), transformation_context));
511
512 #ifndef NDEBUG
513 // Bad: Instruction with id 15 is from the original region and is available
514 // at the end of the region but it is not present in the
515 // |original_id_to_phi_id|.
516 TransformationDuplicateRegionWithSelection transformation_bad_8 =
517 TransformationDuplicateRegionWithSelection(
518 500, 19, 501, 800, 800, {{800, 100}}, {{13, 201}, {15, 202}},
519 {{13, 301}});
520 ASSERT_DEATH(
521 transformation_bad_8.IsApplicable(context.get(), transformation_context),
522 "Bad attempt to query whether overflow ids are available.");
523
524 // Bad: Instruction with id 15 is from the original region but it is
525 // not present in the |original_id_to_duplicate_id|.
526 TransformationDuplicateRegionWithSelection transformation_bad_9 =
527 TransformationDuplicateRegionWithSelection(500, 19, 501, 800, 800,
528 {{800, 100}}, {{13, 201}},
529 {{13, 301}, {15, 302}});
530 ASSERT_DEATH(
531 transformation_bad_9.IsApplicable(context.get(), transformation_context),
532 "Bad attempt to query whether overflow ids are available.");
533 #endif
534
535 // Bad: |condition_id| does not refer to the valid instruction.
536 TransformationDuplicateRegionWithSelection transformation_bad_10 =
537 TransformationDuplicateRegionWithSelection(
538 500, 200, 501, 800, 800, {{800, 100}}, {{13, 201}, {15, 202}},
539 {{13, 301}, {15, 302}});
540
541 ASSERT_FALSE(transformation_bad_10.IsApplicable(context.get(),
542 transformation_context));
543
544 // Bad: |condition_id| does not refer to the instruction of type OpTypeBool
545 TransformationDuplicateRegionWithSelection transformation_bad_11 =
546 TransformationDuplicateRegionWithSelection(
547 500, 14, 501, 800, 800, {{800, 100}}, {{13, 201}, {15, 202}},
548 {{13, 301}, {15, 302}});
549
550 ASSERT_FALSE(transformation_bad_11.IsApplicable(context.get(),
551 transformation_context));
552 }
553
554 TEST(TransformationDuplicateRegionWithSelectionTest, NotApplicableCFGTest2) {
555 // This test handles few cases where the transformation is not applicable
556 // because of the control flow graph or the layout of the blocks.
557
558 std::string shader = R"(
559 OpCapability Shader
560 %1 = OpExtInstImport "GLSL.std.450"
561 OpMemoryModel Logical GLSL450
562 OpEntryPoint Fragment %4 "main"
563 OpExecutionMode %4 OriginUpperLeft
564 OpSource ESSL 310
565 OpName %4 "main"
566 OpName %6 "fun("
567 OpName %10 "s"
568 OpName %12 "i"
569 OpName %29 "b"
570 %2 = OpTypeVoid
571 %3 = OpTypeFunction %2
572 %8 = OpTypeInt 32 1
573 %9 = OpTypePointer Function %8
574 %11 = OpConstant %8 0
575 %19 = OpConstant %8 10
576 %20 = OpTypeBool
577 %26 = OpConstant %8 1
578 %28 = OpTypePointer Function %20
579 %30 = OpConstantTrue %20
580 %4 = OpFunction %2 None %3
581 %5 = OpLabel
582 %29 = OpVariable %28 Function
583 OpStore %29 %30
584 %31 = OpFunctionCall %2 %6
585 OpReturn
586 OpFunctionEnd
587 %6 = OpFunction %2 None %3
588 %7 = OpLabel
589 %10 = OpVariable %9 Function
590 %12 = OpVariable %9 Function
591 OpStore %10 %11
592 OpStore %12 %11
593 OpBranch %13
594 %13 = OpLabel
595 OpLoopMerge %15 %16 None
596 OpBranch %17
597 %17 = OpLabel
598 %18 = OpLoad %8 %12
599 %21 = OpSLessThan %20 %18 %19
600 OpBranchConditional %21 %14 %15
601 %14 = OpLabel
602 %22 = OpLoad %8 %10
603 %23 = OpLoad %8 %12
604 %24 = OpIAdd %8 %22 %23
605 OpStore %10 %24
606 OpBranch %16
607 %16 = OpLabel
608 OpBranch %50
609 %50 = OpLabel
610 %25 = OpLoad %8 %12
611 %27 = OpIAdd %8 %25 %26
612 OpStore %12 %27
613 OpBranch %13
614 %15 = OpLabel
615 OpReturn
616 OpFunctionEnd
617 )";
618
619 const auto env = SPV_ENV_UNIVERSAL_1_4;
620 const auto consumer = nullptr;
621 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
622 spvtools::ValidatorOptions validator_options;
623 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
624 kConsoleMessageConsumer));
625 TransformationContext transformation_context(
626 MakeUnique<FactManager>(context.get()), validator_options);
627 // Bad: The exit block cannot be a header of a loop, because the region won't
628 // be a single-entry, single-exit region.
629 TransformationDuplicateRegionWithSelection transformation_bad_1 =
630 TransformationDuplicateRegionWithSelection(500, 30, 501, 13, 13,
631 {{13, 100}}, {{}}, {{}});
632 ASSERT_FALSE(
633 transformation_bad_1.IsApplicable(context.get(), transformation_context));
634
635 // Bad: The block with id 13, the loop header, is in the region. The block
636 // with id 15, the loop merge block, is not in the region.
637 TransformationDuplicateRegionWithSelection transformation_bad_2 =
638 TransformationDuplicateRegionWithSelection(
639 500, 30, 501, 13, 17, {{13, 100}, {17, 101}}, {{18, 201}, {21, 202}},
640 {{18, 301}, {21, 302}});
641 ASSERT_FALSE(
642 transformation_bad_2.IsApplicable(context.get(), transformation_context));
643
644 // Bad: The block with id 13, the loop header, is not in the region. The block
645 // with id 16, the loop continue target, is in the region.
646 TransformationDuplicateRegionWithSelection transformation_bad_3 =
647 TransformationDuplicateRegionWithSelection(
648 500, 30, 501, 16, 50, {{16, 100}, {50, 101}}, {{25, 201}, {27, 202}},
649 {{25, 301}, {27, 302}});
650 ASSERT_FALSE(
651 transformation_bad_3.IsApplicable(context.get(), transformation_context));
652 }
653
654 TEST(TransformationDuplicateRegionWithSelectionTest, NotApplicableCFGTest3) {
655 // This test handles a case where for the block which is not the exit block,
656 // not all successors are in the region.
657
658 std::string shader = R"(
659 OpCapability Shader
660 %1 = OpExtInstImport "GLSL.std.450"
661 OpMemoryModel Logical GLSL450
662 OpEntryPoint Fragment %4 "main"
663 OpExecutionMode %4 OriginUpperLeft
664 OpSource ESSL 310
665 OpName %4 "main"
666 OpName %6 "fun("
667 OpName %14 "a"
668 OpName %19 "b"
669 %2 = OpTypeVoid
670 %3 = OpTypeFunction %2
671 %8 = OpTypeBool
672 %9 = OpConstantTrue %8
673 %12 = OpTypeInt 32 1
674 %13 = OpTypePointer Function %12
675 %15 = OpConstant %12 2
676 %17 = OpConstant %12 3
677 %18 = OpTypePointer Function %8
678 %4 = OpFunction %2 None %3
679 %5 = OpLabel
680 %19 = OpVariable %18 Function
681 OpStore %19 %9
682 %20 = OpFunctionCall %2 %6
683 OpReturn
684 OpFunctionEnd
685 %6 = OpFunction %2 None %3
686 %7 = OpLabel
687 %14 = OpVariable %13 Function
688 OpSelectionMerge %11 None
689 OpBranchConditional %9 %10 %16
690 %10 = OpLabel
691 OpStore %14 %15
692 OpBranch %11
693 %16 = OpLabel
694 OpStore %14 %17
695 OpBranch %11
696 %11 = OpLabel
697 OpReturn
698 OpFunctionEnd
699 )";
700
701 const auto env = SPV_ENV_UNIVERSAL_1_4;
702 const auto consumer = nullptr;
703 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
704 spvtools::ValidatorOptions validator_options;
705 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
706 kConsoleMessageConsumer));
707 TransformationContext transformation_context(
708 MakeUnique<FactManager>(context.get()), validator_options);
709 // Bad: The block with id 7, which is not an exit block, has two successors:
710 // the block with id 10 and the block with id 16. The block with id 16 is not
711 // in the region.
712 TransformationDuplicateRegionWithSelection transformation_bad_1 =
713 TransformationDuplicateRegionWithSelection(
714 500, 30, 501, 7, 10, {{13, 100}}, {{14, 201}}, {{14, 301}});
715 ASSERT_FALSE(
716 transformation_bad_1.IsApplicable(context.get(), transformation_context));
717 }
718
719 TEST(TransformationDuplicateRegionWithSelectionTest, MultipleBlocksLoopTest) {
720 // This test handles a case where the region consists of multiple blocks
721 // (they form a loop). The transformation is applicable and the region is
722 // duplicated.
723
724 std::string shader = R"(
725 OpCapability Shader
726 %1 = OpExtInstImport "GLSL.std.450"
727 OpMemoryModel Logical GLSL450
728 OpEntryPoint Fragment %4 "main"
729 OpExecutionMode %4 OriginUpperLeft
730 OpSource ESSL 310
731 OpName %4 "main"
732 OpName %6 "fun("
733 OpName %10 "s"
734 OpName %12 "i"
735 OpName %29 "b"
736 %2 = OpTypeVoid
737 %3 = OpTypeFunction %2
738 %8 = OpTypeInt 32 1
739 %9 = OpTypePointer Function %8
740 %11 = OpConstant %8 0
741 %19 = OpConstant %8 10
742 %20 = OpTypeBool
743 %26 = OpConstant %8 1
744 %28 = OpTypePointer Function %20
745 %30 = OpConstantTrue %20
746 %4 = OpFunction %2 None %3
747 %5 = OpLabel
748 %29 = OpVariable %28 Function
749 OpStore %29 %30
750 %31 = OpFunctionCall %2 %6
751 OpReturn
752 OpFunctionEnd
753 %6 = OpFunction %2 None %3
754 %7 = OpLabel
755 %10 = OpVariable %9 Function
756 %12 = OpVariable %9 Function
757 OpStore %10 %11
758 OpStore %12 %11
759 OpBranch %50
760 %50 = OpLabel
761 OpBranch %13
762 %13 = OpLabel
763 OpLoopMerge %15 %16 None
764 OpBranch %17
765 %17 = OpLabel
766 %18 = OpLoad %8 %12
767 %21 = OpSLessThan %20 %18 %19
768 OpBranchConditional %21 %14 %15
769 %14 = OpLabel
770 %22 = OpLoad %8 %10
771 %23 = OpLoad %8 %12
772 %24 = OpIAdd %8 %22 %23
773 OpStore %10 %24
774 OpBranch %16
775 %16 = OpLabel
776 %25 = OpLoad %8 %12
777 %27 = OpIAdd %8 %25 %26
778 OpStore %12 %27
779 OpBranch %13
780 %15 = OpLabel
781 OpReturn
782 OpFunctionEnd
783 )";
784
785 const auto env = SPV_ENV_UNIVERSAL_1_4;
786 const auto consumer = nullptr;
787 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
788 spvtools::ValidatorOptions validator_options;
789 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
790 kConsoleMessageConsumer));
791 TransformationContext transformation_context(
792 MakeUnique<FactManager>(context.get()), validator_options);
793 TransformationDuplicateRegionWithSelection transformation_good_1 =
794 TransformationDuplicateRegionWithSelection(
795 500, 30, 501, 50, 15,
796 {{50, 100}, {13, 101}, {14, 102}, {15, 103}, {16, 104}, {17, 105}},
797 {{22, 201},
798 {23, 202},
799 {24, 203},
800 {25, 204},
801 {27, 205},
802 {18, 206},
803 {21, 207}},
804 {{22, 301},
805 {23, 302},
806 {24, 303},
807 {25, 304},
808 {27, 305},
809 {18, 306},
810 {21, 307}});
811 ASSERT_TRUE(transformation_good_1.IsApplicable(context.get(),
812 transformation_context));
813 ApplyAndCheckFreshIds(transformation_good_1, context.get(),
814 &transformation_context);
815 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
816 kConsoleMessageConsumer));
817
818 std::string expected_shader = R"(
819 OpCapability Shader
820 %1 = OpExtInstImport "GLSL.std.450"
821 OpMemoryModel Logical GLSL450
822 OpEntryPoint Fragment %4 "main"
823 OpExecutionMode %4 OriginUpperLeft
824 OpSource ESSL 310
825 OpName %4 "main"
826 OpName %6 "fun("
827 OpName %10 "s"
828 OpName %12 "i"
829 OpName %29 "b"
830 %2 = OpTypeVoid
831 %3 = OpTypeFunction %2
832 %8 = OpTypeInt 32 1
833 %9 = OpTypePointer Function %8
834 %11 = OpConstant %8 0
835 %19 = OpConstant %8 10
836 %20 = OpTypeBool
837 %26 = OpConstant %8 1
838 %28 = OpTypePointer Function %20
839 %30 = OpConstantTrue %20
840 %4 = OpFunction %2 None %3
841 %5 = OpLabel
842 %29 = OpVariable %28 Function
843 OpStore %29 %30
844 %31 = OpFunctionCall %2 %6
845 OpReturn
846 OpFunctionEnd
847 %6 = OpFunction %2 None %3
848 %7 = OpLabel
849 %10 = OpVariable %9 Function
850 %12 = OpVariable %9 Function
851 OpStore %10 %11
852 OpStore %12 %11
853 OpBranch %500
854 %500 = OpLabel
855 OpSelectionMerge %501 None
856 OpBranchConditional %30 %50 %100
857 %50 = OpLabel
858 OpBranch %13
859 %13 = OpLabel
860 OpLoopMerge %15 %16 None
861 OpBranch %17
862 %17 = OpLabel
863 %18 = OpLoad %8 %12
864 %21 = OpSLessThan %20 %18 %19
865 OpBranchConditional %21 %14 %15
866 %14 = OpLabel
867 %22 = OpLoad %8 %10
868 %23 = OpLoad %8 %12
869 %24 = OpIAdd %8 %22 %23
870 OpStore %10 %24
871 OpBranch %16
872 %16 = OpLabel
873 %25 = OpLoad %8 %12
874 %27 = OpIAdd %8 %25 %26
875 OpStore %12 %27
876 OpBranch %13
877 %15 = OpLabel
878 OpBranch %501
879 %100 = OpLabel
880 OpBranch %101
881 %101 = OpLabel
882 OpLoopMerge %103 %104 None
883 OpBranch %105
884 %105 = OpLabel
885 %206 = OpLoad %8 %12
886 %207 = OpSLessThan %20 %206 %19
887 OpBranchConditional %207 %102 %103
888 %102 = OpLabel
889 %201 = OpLoad %8 %10
890 %202 = OpLoad %8 %12
891 %203 = OpIAdd %8 %201 %202
892 OpStore %10 %203
893 OpBranch %104
894 %104 = OpLabel
895 %204 = OpLoad %8 %12
896 %205 = OpIAdd %8 %204 %26
897 OpStore %12 %205
898 OpBranch %101
899 %103 = OpLabel
900 OpBranch %501
901 %501 = OpLabel
902 %306 = OpPhi %8 %18 %15 %206 %103
903 %307 = OpPhi %20 %21 %15 %207 %103
904 OpReturn
905 OpFunctionEnd
906 )";
907 ASSERT_TRUE(IsEqual(env, expected_shader, context.get()));
908 }
909
910 TEST(TransformationDuplicateRegionWithSelectionTest,
911 ResolvingOpPhiExitBlockTest) {
912 // This test handles a case where the region under the transformation is
913 // referenced in OpPhi instructions. Since the new merge block becomes the
914 // exit of the region, these OpPhi instructions need to be updated.
915
916 std::string shader = R"(
917 OpCapability Shader
918 %1 = OpExtInstImport "GLSL.std.450"
919 OpMemoryModel Logical GLSL450
920 OpEntryPoint Fragment %4 "main"
921 OpExecutionMode %4 OriginUpperLeft
922 OpSource ESSL 310
923 OpName %4 "main"
924 OpName %10 "fun(i1;"
925 OpName %9 "a"
926 OpName %12 "s"
927 OpName %26 "b"
928 OpName %29 "param"
929 %2 = OpTypeVoid
930 %3 = OpTypeFunction %2
931 %6 = OpTypeInt 32 1
932 %7 = OpTypePointer Function %6
933 %8 = OpTypeFunction %2 %7
934 %13 = OpConstant %6 0
935 %15 = OpConstant %6 2
936 %16 = OpTypeBool
937 %25 = OpTypePointer Function %16
938 %27 = OpConstantTrue %16
939 %28 = OpConstant %6 3
940 %4 = OpFunction %2 None %3
941 %5 = OpLabel
942 %26 = OpVariable %25 Function
943 %29 = OpVariable %7 Function
944 OpStore %26 %27
945 OpStore %29 %28
946 %30 = OpFunctionCall %2 %10 %29
947 OpReturn
948 OpFunctionEnd
949 %10 = OpFunction %2 None %8
950 %9 = OpFunctionParameter %7
951 %11 = OpLabel
952 %12 = OpVariable %7 Function
953 OpStore %12 %13
954 %14 = OpLoad %6 %9
955 %17 = OpSLessThan %16 %14 %15
956 OpSelectionMerge %19 None
957 OpBranchConditional %17 %18 %22
958 %18 = OpLabel
959 %20 = OpLoad %6 %9
960 %21 = OpIAdd %6 %20 %15
961 OpStore %12 %21
962 OpBranch %19
963 %22 = OpLabel
964 %23 = OpLoad %6 %9
965 %24 = OpIMul %6 %23 %15
966 OpStore %12 %24
967 OpBranch %19
968 %19 = OpLabel
969 %40 = OpPhi %6 %21 %18 %24 %22
970 OpReturn
971 OpFunctionEnd
972 )";
973
974 const auto env = SPV_ENV_UNIVERSAL_1_4;
975 const auto consumer = nullptr;
976 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
977 spvtools::ValidatorOptions validator_options;
978 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
979 kConsoleMessageConsumer));
980 TransformationContext transformation_context(
981 MakeUnique<FactManager>(context.get()), validator_options);
982
983 TransformationDuplicateRegionWithSelection transformation_good_1 =
984 TransformationDuplicateRegionWithSelection(
985 500, 27, 501, 22, 22, {{22, 100}}, {{23, 201}, {24, 202}},
986 {{23, 301}, {24, 302}});
987
988 ASSERT_TRUE(transformation_good_1.IsApplicable(context.get(),
989 transformation_context));
990 ApplyAndCheckFreshIds(transformation_good_1, context.get(),
991 &transformation_context);
992 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
993 kConsoleMessageConsumer));
994
995 std::string expected_shader = R"(
996 OpCapability Shader
997 %1 = OpExtInstImport "GLSL.std.450"
998 OpMemoryModel Logical GLSL450
999 OpEntryPoint Fragment %4 "main"
1000 OpExecutionMode %4 OriginUpperLeft
1001 OpSource ESSL 310
1002 OpName %4 "main"
1003 OpName %10 "fun(i1;"
1004 OpName %9 "a"
1005 OpName %12 "s"
1006 OpName %26 "b"
1007 OpName %29 "param"
1008 %2 = OpTypeVoid
1009 %3 = OpTypeFunction %2
1010 %6 = OpTypeInt 32 1
1011 %7 = OpTypePointer Function %6
1012 %8 = OpTypeFunction %2 %7
1013 %13 = OpConstant %6 0
1014 %15 = OpConstant %6 2
1015 %16 = OpTypeBool
1016 %25 = OpTypePointer Function %16
1017 %27 = OpConstantTrue %16
1018 %28 = OpConstant %6 3
1019 %4 = OpFunction %2 None %3
1020 %5 = OpLabel
1021 %26 = OpVariable %25 Function
1022 %29 = OpVariable %7 Function
1023 OpStore %26 %27
1024 OpStore %29 %28
1025 %30 = OpFunctionCall %2 %10 %29
1026 OpReturn
1027 OpFunctionEnd
1028 %10 = OpFunction %2 None %8
1029 %9 = OpFunctionParameter %7
1030 %11 = OpLabel
1031 %12 = OpVariable %7 Function
1032 OpStore %12 %13
1033 %14 = OpLoad %6 %9
1034 %17 = OpSLessThan %16 %14 %15
1035 OpSelectionMerge %19 None
1036 OpBranchConditional %17 %18 %500
1037 %18 = OpLabel
1038 %20 = OpLoad %6 %9
1039 %21 = OpIAdd %6 %20 %15
1040 OpStore %12 %21
1041 OpBranch %19
1042 %500 = OpLabel
1043 OpSelectionMerge %501 None
1044 OpBranchConditional %27 %22 %100
1045 %22 = OpLabel
1046 %23 = OpLoad %6 %9
1047 %24 = OpIMul %6 %23 %15
1048 OpStore %12 %24
1049 OpBranch %501
1050 %100 = OpLabel
1051 %201 = OpLoad %6 %9
1052 %202 = OpIMul %6 %201 %15
1053 OpStore %12 %202
1054 OpBranch %501
1055 %501 = OpLabel
1056 %301 = OpPhi %6 %23 %22 %201 %100
1057 %302 = OpPhi %6 %24 %22 %202 %100
1058 OpBranch %19
1059 %19 = OpLabel
1060 %40 = OpPhi %6 %21 %18 %302 %501
1061 OpReturn
1062 OpFunctionEnd
1063 )";
1064 ASSERT_TRUE(IsEqual(env, expected_shader, context.get()));
1065 }
1066
1067 TEST(TransformationDuplicateRegionWithSelectionTest, NotApplicableEarlyReturn) {
1068 // This test handles a case where one of the blocks has successor outside of
1069 // the region, which has an early return from the function, so that the
1070 // transformation is not applicable.
1071
1072 std::string shader = R"(
1073 OpCapability Shader
1074 %1 = OpExtInstImport "GLSL.std.450"
1075 OpMemoryModel Logical GLSL450
1076 OpEntryPoint Fragment %4 "main"
1077 OpExecutionMode %4 OriginUpperLeft
1078 OpSource ESSL 310
1079 OpName %4 "main"
1080 OpName %10 "fun(i1;"
1081 OpName %9 "a"
1082 OpName %12 "s"
1083 OpName %27 "b"
1084 OpName %30 "param"
1085 %2 = OpTypeVoid
1086 %3 = OpTypeFunction %2
1087 %6 = OpTypeInt 32 1
1088 %7 = OpTypePointer Function %6
1089 %8 = OpTypeFunction %2 %7
1090 %13 = OpConstant %6 0
1091 %15 = OpConstant %6 2
1092 %16 = OpTypeBool
1093 %26 = OpTypePointer Function %16
1094 %28 = OpConstantTrue %16
1095 %29 = OpConstant %6 3
1096 %4 = OpFunction %2 None %3
1097 %5 = OpLabel
1098 %27 = OpVariable %26 Function
1099 %30 = OpVariable %7 Function
1100 OpStore %27 %28
1101 OpStore %30 %29
1102 %31 = OpFunctionCall %2 %10 %30
1103 OpReturn
1104 OpFunctionEnd
1105 %10 = OpFunction %2 None %8
1106 %9 = OpFunctionParameter %7
1107 %11 = OpLabel
1108 %12 = OpVariable %7 Function
1109 OpBranch %50
1110 %50 = OpLabel
1111 OpStore %12 %13
1112 %14 = OpLoad %6 %9
1113 %17 = OpSLessThan %16 %14 %15
1114 OpSelectionMerge %19 None
1115 OpBranchConditional %17 %18 %22
1116 %18 = OpLabel
1117 %20 = OpLoad %6 %9
1118 %21 = OpIAdd %6 %20 %15
1119 OpStore %12 %21
1120 OpBranch %19
1121 %22 = OpLabel
1122 OpReturn
1123 %19 = OpLabel
1124 OpReturn
1125 OpFunctionEnd
1126 )";
1127
1128 const auto env = SPV_ENV_UNIVERSAL_1_4;
1129 const auto consumer = nullptr;
1130 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1131 spvtools::ValidatorOptions validator_options;
1132 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1133 kConsoleMessageConsumer));
1134 TransformationContext transformation_context(
1135 MakeUnique<FactManager>(context.get()), validator_options);
1136 // Bad: The block with id 50, which is the entry block, has two successors:
1137 // the block with id 18 and the block with id 22. The block 22 has an early
1138 // return from the function, so that the entry block is not post-dominated by
1139 // the exit block.
1140 TransformationDuplicateRegionWithSelection transformation_bad_1 =
1141 TransformationDuplicateRegionWithSelection(
1142 500, 28, 501, 50, 19, {{50, 100}, {18, 101}, {22, 102}, {19, 103}},
1143 {{14, 202}, {17, 203}, {20, 204}, {21, 205}},
1144 {{14, 302}, {17, 303}, {20, 304}, {21, 305}});
1145 ASSERT_FALSE(
1146 transformation_bad_1.IsApplicable(context.get(), transformation_context));
1147 }
1148
1149 TEST(TransformationDuplicateRegionWithSelectionTest,
1150 ResolvingOpPhiEntryBlockOnePredecessor) {
1151 // This test handles a case where the entry block has an OpPhi instruction
1152 // referring to its predecessor. After transformation, this instruction needs
1153 // to be updated.
1154
1155 std::string shader = R"(
1156 OpCapability Shader
1157 %1 = OpExtInstImport "GLSL.std.450"
1158 OpMemoryModel Logical GLSL450
1159 OpEntryPoint Fragment %4 "main"
1160 OpExecutionMode %4 OriginUpperLeft
1161 OpSource ESSL 310
1162 OpName %4 "main"
1163 OpName %10 "fun(i1;"
1164 OpName %9 "a"
1165 OpName %12 "s"
1166 OpName %14 "t"
1167 OpName %20 "b"
1168 OpName %23 "param"
1169 %2 = OpTypeVoid
1170 %3 = OpTypeFunction %2
1171 %6 = OpTypeInt 32 1
1172 %7 = OpTypePointer Function %6
1173 %8 = OpTypeFunction %2 %7
1174 %13 = OpConstant %6 0
1175 %15 = OpConstant %6 2
1176 %18 = OpTypeBool
1177 %19 = OpTypePointer Function %18
1178 %21 = OpConstantTrue %18
1179 %22 = OpConstant %6 3
1180 %4 = OpFunction %2 None %3
1181 %5 = OpLabel
1182 %20 = OpVariable %19 Function
1183 %23 = OpVariable %7 Function
1184 OpStore %20 %21
1185 OpStore %23 %22
1186 %24 = OpFunctionCall %2 %10 %23
1187 OpReturn
1188 OpFunctionEnd
1189 %10 = OpFunction %2 None %8
1190 %9 = OpFunctionParameter %7
1191 %11 = OpLabel
1192 %12 = OpVariable %7 Function
1193 %14 = OpVariable %7 Function
1194 OpStore %12 %13
1195 %16 = OpLoad %6 %12
1196 %17 = OpIMul %6 %15 %16
1197 OpStore %14 %17
1198 OpBranch %50
1199 %50 = OpLabel
1200 %51 = OpPhi %6 %17 %11
1201 OpReturn
1202 OpFunctionEnd
1203 )";
1204
1205 const auto env = SPV_ENV_UNIVERSAL_1_4;
1206 const auto consumer = nullptr;
1207 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1208 spvtools::ValidatorOptions validator_options;
1209 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1210 kConsoleMessageConsumer));
1211 TransformationContext transformation_context(
1212 MakeUnique<FactManager>(context.get()), validator_options);
1213
1214 TransformationDuplicateRegionWithSelection transformation_good_1 =
1215 TransformationDuplicateRegionWithSelection(
1216 500, 21, 501, 50, 50, {{50, 100}}, {{51, 201}}, {{51, 301}});
1217 ASSERT_TRUE(transformation_good_1.IsApplicable(context.get(),
1218 transformation_context));
1219 ApplyAndCheckFreshIds(transformation_good_1, context.get(),
1220 &transformation_context);
1221 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1222 kConsoleMessageConsumer));
1223
1224 std::string expected_shader = R"(
1225 OpCapability Shader
1226 %1 = OpExtInstImport "GLSL.std.450"
1227 OpMemoryModel Logical GLSL450
1228 OpEntryPoint Fragment %4 "main"
1229 OpExecutionMode %4 OriginUpperLeft
1230 OpSource ESSL 310
1231 OpName %4 "main"
1232 OpName %10 "fun(i1;"
1233 OpName %9 "a"
1234 OpName %12 "s"
1235 OpName %14 "t"
1236 OpName %20 "b"
1237 OpName %23 "param"
1238 %2 = OpTypeVoid
1239 %3 = OpTypeFunction %2
1240 %6 = OpTypeInt 32 1
1241 %7 = OpTypePointer Function %6
1242 %8 = OpTypeFunction %2 %7
1243 %13 = OpConstant %6 0
1244 %15 = OpConstant %6 2
1245 %18 = OpTypeBool
1246 %19 = OpTypePointer Function %18
1247 %21 = OpConstantTrue %18
1248 %22 = OpConstant %6 3
1249 %4 = OpFunction %2 None %3
1250 %5 = OpLabel
1251 %20 = OpVariable %19 Function
1252 %23 = OpVariable %7 Function
1253 OpStore %20 %21
1254 OpStore %23 %22
1255 %24 = OpFunctionCall %2 %10 %23
1256 OpReturn
1257 OpFunctionEnd
1258 %10 = OpFunction %2 None %8
1259 %9 = OpFunctionParameter %7
1260 %11 = OpLabel
1261 %12 = OpVariable %7 Function
1262 %14 = OpVariable %7 Function
1263 OpStore %12 %13
1264 %16 = OpLoad %6 %12
1265 %17 = OpIMul %6 %15 %16
1266 OpStore %14 %17
1267 OpBranch %500
1268 %500 = OpLabel
1269 OpSelectionMerge %501 None
1270 OpBranchConditional %21 %50 %100
1271 %50 = OpLabel
1272 %51 = OpPhi %6 %17 %500
1273 OpBranch %501
1274 %100 = OpLabel
1275 %201 = OpPhi %6 %17 %500
1276 OpBranch %501
1277 %501 = OpLabel
1278 %301 = OpPhi %6 %51 %50 %201 %100
1279 OpReturn
1280 OpFunctionEnd
1281 )";
1282 ASSERT_TRUE(IsEqual(env, expected_shader, context.get()));
1283 }
1284
1285 TEST(TransformationDuplicateRegionWithSelectionTest,
1286 NotApplicableNoVariablePointerCapability) {
1287 // This test handles a case where the transformation would create an OpPhi
1288 // instruction with pointer operands, however there is no cab
1289 // CapabilityVariablePointers. Hence, the transformation is not applicable.
1290
1291 std::string shader = R"(
1292 OpCapability Shader
1293 %1 = OpExtInstImport "GLSL.std.450"
1294 OpMemoryModel Logical GLSL450
1295 OpEntryPoint Fragment %4 "main"
1296 OpExecutionMode %4 OriginUpperLeft
1297 OpSource ESSL 310
1298 OpName %4 "main"
1299 OpName %10 "fun(i1;"
1300 OpName %9 "a"
1301 OpName %12 "s"
1302 OpName %14 "t"
1303 OpName %20 "b"
1304 OpName %23 "param"
1305 %2 = OpTypeVoid
1306 %3 = OpTypeFunction %2
1307 %6 = OpTypeInt 32 1
1308 %7 = OpTypePointer Function %6
1309 %8 = OpTypeFunction %2 %7
1310 %13 = OpConstant %6 0
1311 %15 = OpConstant %6 2
1312 %18 = OpTypeBool
1313 %19 = OpTypePointer Function %18
1314 %21 = OpConstantTrue %18
1315 %22 = OpConstant %6 3
1316 %4 = OpFunction %2 None %3
1317 %5 = OpLabel
1318 %20 = OpVariable %19 Function
1319 %23 = OpVariable %7 Function
1320 OpStore %20 %21
1321 OpStore %23 %22
1322 %24 = OpFunctionCall %2 %10 %23
1323 OpReturn
1324 OpFunctionEnd
1325 %10 = OpFunction %2 None %8
1326 %9 = OpFunctionParameter %7
1327 %11 = OpLabel
1328 %12 = OpVariable %7 Function
1329 %14 = OpVariable %7 Function
1330 OpStore %12 %13
1331 %16 = OpLoad %6 %12
1332 %17 = OpIMul %6 %15 %16
1333 OpStore %14 %17
1334 OpBranch %50
1335 %50 = OpLabel
1336 %51 = OpCopyObject %7 %12
1337 OpReturn
1338 OpFunctionEnd
1339 )";
1340
1341 const auto env = SPV_ENV_UNIVERSAL_1_4;
1342 const auto consumer = nullptr;
1343 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1344 spvtools::ValidatorOptions validator_options;
1345 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1346 kConsoleMessageConsumer));
1347 TransformationContext transformation_context(
1348 MakeUnique<FactManager>(context.get()), validator_options);
1349 // Bad: There is no required capability CapabilityVariablePointers
1350 TransformationDuplicateRegionWithSelection transformation_bad_1 =
1351 TransformationDuplicateRegionWithSelection(
1352 500, 21, 501, 50, 50, {{50, 100}}, {{51, 201}}, {{51, 301}});
1353 ASSERT_FALSE(
1354 transformation_bad_1.IsApplicable(context.get(), transformation_context));
1355 }
1356
1357 TEST(TransformationDuplicateRegionWithSelectionTest,
1358 ExitBlockTerminatorOpUnreachable) {
1359 // This test handles a case where the exit block ends with OpUnreachable.
1360
1361 std::string shader = R"(
1362 OpCapability Shader
1363 %1 = OpExtInstImport "GLSL.std.450"
1364 OpMemoryModel Logical GLSL450
1365 OpEntryPoint Fragment %4 "main"
1366 OpExecutionMode %4 OriginUpperLeft
1367 OpSource ESSL 310
1368 OpName %4 "main"
1369 OpName %6 "fun("
1370 OpName %10 "s"
1371 OpName %17 "b"
1372 %2 = OpTypeVoid
1373 %3 = OpTypeFunction %2
1374 %8 = OpTypeInt 32 1
1375 %9 = OpTypePointer Function %8
1376 %11 = OpConstant %8 0
1377 %13 = OpConstant %8 2
1378 %15 = OpTypeBool
1379 %16 = OpTypePointer Function %15
1380 %18 = OpConstantTrue %15
1381 %4 = OpFunction %2 None %3
1382 %5 = OpLabel
1383 %17 = OpVariable %16 Function
1384 OpStore %17 %18
1385 %19 = OpFunctionCall %2 %6
1386 OpReturn
1387 OpFunctionEnd
1388 %6 = OpFunction %2 None %3
1389 %7 = OpLabel
1390 %10 = OpVariable %9 Function
1391 OpBranch %50
1392 %50 = OpLabel
1393 OpStore %10 %11
1394 %12 = OpLoad %8 %10
1395 %14 = OpIAdd %8 %12 %13
1396 OpStore %10 %14
1397 OpUnreachable
1398 OpFunctionEnd
1399 )";
1400
1401 const auto env = SPV_ENV_UNIVERSAL_1_4;
1402 const auto consumer = nullptr;
1403 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1404 spvtools::ValidatorOptions validator_options;
1405 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1406 kConsoleMessageConsumer));
1407 TransformationContext transformation_context(
1408 MakeUnique<FactManager>(context.get()), validator_options);
1409
1410 TransformationDuplicateRegionWithSelection transformation_good_1 =
1411 TransformationDuplicateRegionWithSelection(
1412 500, 18, 501, 50, 50, {{50, 100}}, {{12, 201}, {14, 202}},
1413 {{12, 301}, {14, 302}});
1414 ASSERT_TRUE(transformation_good_1.IsApplicable(context.get(),
1415 transformation_context));
1416 ApplyAndCheckFreshIds(transformation_good_1, context.get(),
1417 &transformation_context);
1418 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1419 kConsoleMessageConsumer));
1420
1421 std::string expected_shader = R"(
1422 OpCapability Shader
1423 %1 = OpExtInstImport "GLSL.std.450"
1424 OpMemoryModel Logical GLSL450
1425 OpEntryPoint Fragment %4 "main"
1426 OpExecutionMode %4 OriginUpperLeft
1427 OpSource ESSL 310
1428 OpName %4 "main"
1429 OpName %6 "fun("
1430 OpName %10 "s"
1431 OpName %17 "b"
1432 %2 = OpTypeVoid
1433 %3 = OpTypeFunction %2
1434 %8 = OpTypeInt 32 1
1435 %9 = OpTypePointer Function %8
1436 %11 = OpConstant %8 0
1437 %13 = OpConstant %8 2
1438 %15 = OpTypeBool
1439 %16 = OpTypePointer Function %15
1440 %18 = OpConstantTrue %15
1441 %4 = OpFunction %2 None %3
1442 %5 = OpLabel
1443 %17 = OpVariable %16 Function
1444 OpStore %17 %18
1445 %19 = OpFunctionCall %2 %6
1446 OpReturn
1447 OpFunctionEnd
1448 %6 = OpFunction %2 None %3
1449 %7 = OpLabel
1450 %10 = OpVariable %9 Function
1451 OpBranch %500
1452 %500 = OpLabel
1453 OpSelectionMerge %501 None
1454 OpBranchConditional %18 %50 %100
1455 %50 = OpLabel
1456 OpStore %10 %11
1457 %12 = OpLoad %8 %10
1458 %14 = OpIAdd %8 %12 %13
1459 OpStore %10 %14
1460 OpBranch %501
1461 %100 = OpLabel
1462 OpStore %10 %11
1463 %201 = OpLoad %8 %10
1464 %202 = OpIAdd %8 %201 %13
1465 OpStore %10 %202
1466 OpBranch %501
1467 %501 = OpLabel
1468 %301 = OpPhi %8 %12 %50 %201 %100
1469 %302 = OpPhi %8 %14 %50 %202 %100
1470 OpUnreachable
1471 OpFunctionEnd
1472 )";
1473 ASSERT_TRUE(IsEqual(env, expected_shader, context.get()));
1474 }
1475
1476 TEST(TransformationDuplicateRegionWithSelectionTest,
1477 ExitBlockTerminatorOpKill) {
1478 // This test handles a case where the exit block ends with OpKill.
1479
1480 std::string shader = R"(
1481 OpCapability Shader
1482 %1 = OpExtInstImport "GLSL.std.450"
1483 OpMemoryModel Logical GLSL450
1484 OpEntryPoint Fragment %4 "main"
1485 OpExecutionMode %4 OriginUpperLeft
1486 OpSource ESSL 310
1487 OpName %4 "main"
1488 OpName %6 "fun("
1489 OpName %10 "s"
1490 OpName %17 "b"
1491 %2 = OpTypeVoid
1492 %3 = OpTypeFunction %2
1493 %8 = OpTypeInt 32 1
1494 %9 = OpTypePointer Function %8
1495 %11 = OpConstant %8 0
1496 %13 = OpConstant %8 2
1497 %15 = OpTypeBool
1498 %16 = OpTypePointer Function %15
1499 %18 = OpConstantTrue %15
1500 %4 = OpFunction %2 None %3
1501 %5 = OpLabel
1502 %17 = OpVariable %16 Function
1503 OpStore %17 %18
1504 %19 = OpFunctionCall %2 %6
1505 OpReturn
1506 OpFunctionEnd
1507 %6 = OpFunction %2 None %3
1508 %7 = OpLabel
1509 %10 = OpVariable %9 Function
1510 OpBranch %50
1511 %50 = OpLabel
1512 OpStore %10 %11
1513 %12 = OpLoad %8 %10
1514 %14 = OpIAdd %8 %12 %13
1515 OpStore %10 %14
1516 OpKill
1517 OpFunctionEnd
1518 )";
1519
1520 const auto env = SPV_ENV_UNIVERSAL_1_4;
1521 const auto consumer = nullptr;
1522 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1523 spvtools::ValidatorOptions validator_options;
1524 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1525 kConsoleMessageConsumer));
1526 TransformationContext transformation_context(
1527 MakeUnique<FactManager>(context.get()), validator_options);
1528
1529 TransformationDuplicateRegionWithSelection transformation_good_1 =
1530 TransformationDuplicateRegionWithSelection(
1531 500, 18, 501, 50, 50, {{50, 100}}, {{12, 201}, {14, 202}},
1532 {{12, 301}, {14, 302}});
1533 ASSERT_TRUE(transformation_good_1.IsApplicable(context.get(),
1534 transformation_context));
1535 ApplyAndCheckFreshIds(transformation_good_1, context.get(),
1536 &transformation_context);
1537 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1538 kConsoleMessageConsumer));
1539
1540 std::string expected_shader = R"(
1541 OpCapability Shader
1542 %1 = OpExtInstImport "GLSL.std.450"
1543 OpMemoryModel Logical GLSL450
1544 OpEntryPoint Fragment %4 "main"
1545 OpExecutionMode %4 OriginUpperLeft
1546 OpSource ESSL 310
1547 OpName %4 "main"
1548 OpName %6 "fun("
1549 OpName %10 "s"
1550 OpName %17 "b"
1551 %2 = OpTypeVoid
1552 %3 = OpTypeFunction %2
1553 %8 = OpTypeInt 32 1
1554 %9 = OpTypePointer Function %8
1555 %11 = OpConstant %8 0
1556 %13 = OpConstant %8 2
1557 %15 = OpTypeBool
1558 %16 = OpTypePointer Function %15
1559 %18 = OpConstantTrue %15
1560 %4 = OpFunction %2 None %3
1561 %5 = OpLabel
1562 %17 = OpVariable %16 Function
1563 OpStore %17 %18
1564 %19 = OpFunctionCall %2 %6
1565 OpReturn
1566 OpFunctionEnd
1567 %6 = OpFunction %2 None %3
1568 %7 = OpLabel
1569 %10 = OpVariable %9 Function
1570 OpBranch %500
1571 %500 = OpLabel
1572 OpSelectionMerge %501 None
1573 OpBranchConditional %18 %50 %100
1574 %50 = OpLabel
1575 OpStore %10 %11
1576 %12 = OpLoad %8 %10
1577 %14 = OpIAdd %8 %12 %13
1578 OpStore %10 %14
1579 OpBranch %501
1580 %100 = OpLabel
1581 OpStore %10 %11
1582 %201 = OpLoad %8 %10
1583 %202 = OpIAdd %8 %201 %13
1584 OpStore %10 %202
1585 OpBranch %501
1586 %501 = OpLabel
1587 %301 = OpPhi %8 %12 %50 %201 %100
1588 %302 = OpPhi %8 %14 %50 %202 %100
1589 OpKill
1590 OpFunctionEnd
1591 )";
1592
1593 ASSERT_TRUE(IsEqual(env, expected_shader, context.get()));
1594 }
1595
1596 TEST(TransformationDuplicateRegionWithSelectionTest,
1597 ContinueExitBlockNotApplicable) {
1598 // This test handles a case where the exit block is the continue target and
1599 // the transformation is not applicable.
1600
1601 std::string shader = R"(
1602 OpCapability Shader
1603 %1 = OpExtInstImport "GLSL.std.450"
1604 OpMemoryModel Logical GLSL450
1605 OpEntryPoint Fragment %4 "main"
1606 OpExecutionMode %4 OriginUpperLeft
1607 OpSource ESSL 310
1608 OpName %4 "main"
1609 OpName %8 "s"
1610 OpName %10 "i"
1611 %2 = OpTypeVoid
1612 %3 = OpTypeFunction %2
1613 %6 = OpTypeInt 32 1
1614 %7 = OpTypePointer Function %6
1615 %9 = OpConstant %6 0
1616 %17 = OpConstant %6 10
1617 %18 = OpTypeBool
1618 %24 = OpConstant %6 5
1619 %30 = OpConstant %6 1
1620 %50 = OpConstantTrue %18
1621 %4 = OpFunction %2 None %3
1622 %5 = OpLabel
1623 %8 = OpVariable %7 Function
1624 %10 = OpVariable %7 Function
1625 OpStore %8 %9
1626 OpStore %10 %9
1627 OpBranch %11
1628 %11 = OpLabel
1629 OpLoopMerge %13 %14 None
1630 OpBranch %15
1631 %15 = OpLabel
1632 %16 = OpLoad %6 %10
1633 %19 = OpSLessThan %18 %16 %17
1634 OpBranchConditional %19 %12 %13
1635 %12 = OpLabel
1636 %20 = OpLoad %6 %10
1637 %21 = OpLoad %6 %8
1638 %22 = OpIAdd %6 %21 %20
1639 OpStore %8 %22
1640 %23 = OpLoad %6 %10
1641 %25 = OpIEqual %18 %23 %24
1642 OpSelectionMerge %27 None
1643 OpBranchConditional %25 %26 %27
1644 %26 = OpLabel
1645 OpBranch %13
1646 %27 = OpLabel
1647 OpBranch %14
1648 %14 = OpLabel
1649 %29 = OpLoad %6 %10
1650 %31 = OpIAdd %6 %29 %30
1651 OpStore %10 %31
1652 OpBranch %11
1653 %13 = OpLabel
1654 OpReturn
1655 OpFunctionEnd
1656 )";
1657
1658 const auto env = SPV_ENV_UNIVERSAL_1_4;
1659 const auto consumer = nullptr;
1660 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1661 spvtools::ValidatorOptions validator_options;
1662 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1663 kConsoleMessageConsumer));
1664 TransformationContext transformation_context(
1665 MakeUnique<FactManager>(context.get()), validator_options);
1666 TransformationDuplicateRegionWithSelection transformation_bad =
1667 TransformationDuplicateRegionWithSelection(
1668 500, 50, 501, 27, 14, {{27, 101}, {14, 102}}, {{29, 201}, {31, 202}},
1669 {{29, 301}, {31, 302}});
1670
1671 ASSERT_FALSE(
1672 transformation_bad.IsApplicable(context.get(), transformation_context));
1673 }
1674
1675 TEST(TransformationDuplicateRegionWithSelectionTest,
1676 MultiplePredecessorsNotApplicableTest) {
1677 // This test handles a case where the entry block has multiple predecessors
1678 // and the transformation is not applicable.
1679
1680 std::string shader = R"(
1681 OpCapability Shader
1682 %1 = OpExtInstImport "GLSL.std.450"
1683 OpMemoryModel Logical GLSL450
1684 OpEntryPoint Fragment %4 "main"
1685 OpExecutionMode %4 OriginUpperLeft
1686 OpSource ESSL 310
1687 OpName %4 "main"
1688 OpName %10 "fun1(i1;"
1689 OpName %9 "a"
1690 OpName %18 "b"
1691 OpName %24 "b"
1692 OpName %27 "param"
1693 %2 = OpTypeVoid
1694 %3 = OpTypeFunction %2
1695 %6 = OpTypeInt 32 1
1696 %7 = OpTypePointer Function %6
1697 %8 = OpTypeFunction %2 %7
1698 %13 = OpConstant %6 2
1699 %14 = OpTypeBool
1700 %23 = OpTypePointer Function %14
1701 %25 = OpConstantTrue %14
1702 %26 = OpConstant %6 1
1703 %4 = OpFunction %2 None %3
1704 %5 = OpLabel
1705 %24 = OpVariable %23 Function
1706 %27 = OpVariable %7 Function
1707 OpStore %24 %25
1708 OpStore %27 %26
1709 %28 = OpFunctionCall %2 %10 %27
1710 OpReturn
1711 OpFunctionEnd
1712 %10 = OpFunction %2 None %8
1713 %9 = OpFunctionParameter %7
1714 %11 = OpLabel
1715 %18 = OpVariable %7 Function
1716 %12 = OpLoad %6 %9
1717 %15 = OpSLessThan %14 %12 %13
1718 OpSelectionMerge %17 None
1719 OpBranchConditional %15 %16 %20
1720 %16 = OpLabel
1721 %19 = OpLoad %6 %9
1722 OpStore %18 %19
1723 OpBranch %60
1724 %20 = OpLabel
1725 %21 = OpLoad %6 %9
1726 %22 = OpIAdd %6 %21 %13
1727 OpStore %18 %22
1728 OpBranch %60
1729 %60 = OpLabel
1730 OpBranch %17
1731 %17 = OpLabel
1732 OpReturn
1733 OpFunctionEnd
1734 )";
1735
1736 const auto env = SPV_ENV_UNIVERSAL_1_4;
1737 const auto consumer = nullptr;
1738 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1739 spvtools::ValidatorOptions validator_options;
1740 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1741 kConsoleMessageConsumer));
1742 TransformationContext transformation_context(
1743 MakeUnique<FactManager>(context.get()), validator_options);
1744
1745 TransformationDuplicateRegionWithSelection transformation_bad =
1746 TransformationDuplicateRegionWithSelection(500, 25, 501, 60, 60,
1747 {{60, 101}}, {{}}, {{}});
1748
1749 ASSERT_FALSE(
1750 transformation_bad.IsApplicable(context.get(), transformation_context));
1751 }
1752
1753 TEST(TransformationDuplicateRegionWithSelectionTest, OverflowIds) {
1754 // This test checks that the transformation correctly uses overflow ids, when
1755 // they are both needed and provided.
1756
1757 std::string shader = R"(
1758 OpCapability Shader
1759 %1 = OpExtInstImport "GLSL.std.450"
1760 OpMemoryModel Logical GLSL450
1761 OpEntryPoint Fragment %4 "main"
1762 OpExecutionMode %4 OriginUpperLeft
1763 OpSource ESSL 310
1764 OpName %4 "main"
1765 OpName %6 "fun("
1766 OpName %10 "s"
1767 OpName %17 "b"
1768 %2 = OpTypeVoid
1769 %3 = OpTypeFunction %2
1770 %8 = OpTypeInt 32 1
1771 %9 = OpTypePointer Function %8
1772 %11 = OpConstant %8 0
1773 %13 = OpConstant %8 2
1774 %15 = OpTypeBool
1775 %16 = OpTypePointer Function %15
1776 %18 = OpConstantTrue %15
1777 %4 = OpFunction %2 None %3
1778 %5 = OpLabel
1779 %17 = OpVariable %16 Function
1780 OpStore %17 %18
1781 %19 = OpFunctionCall %2 %6
1782 OpReturn
1783 OpFunctionEnd
1784 %6 = OpFunction %2 None %3
1785 %7 = OpLabel
1786 %10 = OpVariable %9 Function
1787 OpBranch %50
1788 %50 = OpLabel
1789 OpStore %10 %11
1790 %12 = OpLoad %8 %10
1791 %14 = OpIAdd %8 %12 %13
1792 OpStore %10 %14
1793 OpKill
1794 OpFunctionEnd
1795 )";
1796
1797 const auto env = SPV_ENV_UNIVERSAL_1_4;
1798 const auto consumer = nullptr;
1799 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1800 spvtools::ValidatorOptions validator_options;
1801 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1802 kConsoleMessageConsumer));
1803 auto overflow_ids_unique_ptr = MakeUnique<CounterOverflowIdSource>(1000);
1804 auto overflow_ids_ptr = overflow_ids_unique_ptr.get();
1805 TransformationContext transformation_context(
1806 MakeUnique<FactManager>(context.get()), validator_options,
1807 std::move(overflow_ids_unique_ptr));
1808
1809 // The mappings do not provide sufficient ids, thus overflow ids are required.
1810 TransformationDuplicateRegionWithSelection transformation_good_1 =
1811 TransformationDuplicateRegionWithSelection(500, 18, 501, 50, 50, {},
1812 {{12, 201}}, {{14, 302}});
1813 ASSERT_TRUE(transformation_good_1.IsApplicable(context.get(),
1814 transformation_context));
1815 ApplyAndCheckFreshIds(transformation_good_1, context.get(),
1816 &transformation_context,
1817 overflow_ids_ptr->GetIssuedOverflowIds());
1818 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1819 kConsoleMessageConsumer));
1820
1821 std::string expected_shader = R"(
1822 OpCapability Shader
1823 %1 = OpExtInstImport "GLSL.std.450"
1824 OpMemoryModel Logical GLSL450
1825 OpEntryPoint Fragment %4 "main"
1826 OpExecutionMode %4 OriginUpperLeft
1827 OpSource ESSL 310
1828 OpName %4 "main"
1829 OpName %6 "fun("
1830 OpName %10 "s"
1831 OpName %17 "b"
1832 %2 = OpTypeVoid
1833 %3 = OpTypeFunction %2
1834 %8 = OpTypeInt 32 1
1835 %9 = OpTypePointer Function %8
1836 %11 = OpConstant %8 0
1837 %13 = OpConstant %8 2
1838 %15 = OpTypeBool
1839 %16 = OpTypePointer Function %15
1840 %18 = OpConstantTrue %15
1841 %4 = OpFunction %2 None %3
1842 %5 = OpLabel
1843 %17 = OpVariable %16 Function
1844 OpStore %17 %18
1845 %19 = OpFunctionCall %2 %6
1846 OpReturn
1847 OpFunctionEnd
1848 %6 = OpFunction %2 None %3
1849 %7 = OpLabel
1850 %10 = OpVariable %9 Function
1851 OpBranch %500
1852 %500 = OpLabel
1853 OpSelectionMerge %501 None
1854 OpBranchConditional %18 %50 %1000
1855 %50 = OpLabel
1856 OpStore %10 %11
1857 %12 = OpLoad %8 %10
1858 %14 = OpIAdd %8 %12 %13
1859 OpStore %10 %14
1860 OpBranch %501
1861 %1000 = OpLabel
1862 OpStore %10 %11
1863 %201 = OpLoad %8 %10
1864 %1002 = OpIAdd %8 %201 %13
1865 OpStore %10 %1002
1866 OpBranch %501
1867 %501 = OpLabel
1868 %1001 = OpPhi %8 %12 %50 %201 %1000
1869 %302 = OpPhi %8 %14 %50 %1002 %1000
1870 OpKill
1871 OpFunctionEnd
1872 )";
1873
1874 ASSERT_TRUE(IsEqual(env, expected_shader, context.get()));
1875 }
1876
1877 TEST(TransformationDuplicateRegionWithSelectionTest,
1878 RegionExitIsOpBranchConditional) {
1879 // Checks the case where the exit block of a region ends with
1880 // OpBranchConditional (but is not a header).
1881 std::string shader = R"(
1882 OpCapability Shader
1883 %1 = OpExtInstImport "GLSL.std.450"
1884 OpMemoryModel Logical GLSL450
1885 OpEntryPoint Fragment %4 "main"
1886 OpExecutionMode %4 OriginUpperLeft
1887 OpSource ESSL 320
1888 OpName %4 "main"
1889 OpName %8 "i"
1890 %2 = OpTypeVoid
1891 %3 = OpTypeFunction %2
1892 %6 = OpTypeInt 32 1
1893 %7 = OpTypePointer Function %6
1894 %9 = OpConstant %6 0
1895 %16 = OpConstant %6 100
1896 %17 = OpTypeBool
1897 %50 = OpConstantTrue %17
1898 %20 = OpConstant %6 1
1899 %4 = OpFunction %2 None %3
1900 %5 = OpLabel
1901 %8 = OpVariable %7 Function
1902 OpStore %8 %9
1903 OpBranch %10
1904 %10 = OpLabel
1905 OpLoopMerge %12 %13 None
1906 OpBranch %14
1907 %14 = OpLabel
1908 %15 = OpLoad %6 %8
1909 %18 = OpSLessThan %17 %15 %16
1910 OpBranchConditional %18 %11 %12
1911 %11 = OpLabel
1912 %19 = OpLoad %6 %8
1913 %21 = OpIAdd %6 %19 %20
1914 OpStore %8 %21
1915 OpBranchConditional %50 %13 %12
1916 %13 = OpLabel
1917 %22 = OpLoad %6 %8
1918 %23 = OpIAdd %6 %22 %20
1919 OpStore %8 %23
1920 OpBranch %10
1921 %12 = OpLabel
1922 OpReturn
1923 OpFunctionEnd
1924 )";
1925
1926 const auto env = SPV_ENV_UNIVERSAL_1_4;
1927 const auto consumer = nullptr;
1928 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1929 spvtools::ValidatorOptions validator_options;
1930 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1931 kConsoleMessageConsumer));
1932 TransformationContext transformation_context(
1933 MakeUnique<FactManager>(context.get()), validator_options);
1934
1935 TransformationDuplicateRegionWithSelection transformation_good_1 =
1936 TransformationDuplicateRegionWithSelection(
1937 600, 50, 601, 11, 11, {{11, 602}}, {{19, 603}, {21, 604}},
1938 {{19, 605}, {21, 606}});
1939 ASSERT_TRUE(transformation_good_1.IsApplicable(context.get(),
1940 transformation_context));
1941 ApplyAndCheckFreshIds(transformation_good_1, context.get(),
1942 &transformation_context);
1943 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1944 kConsoleMessageConsumer));
1945
1946 std::string expected_shader = R"(
1947 OpCapability Shader
1948 %1 = OpExtInstImport "GLSL.std.450"
1949 OpMemoryModel Logical GLSL450
1950 OpEntryPoint Fragment %4 "main"
1951 OpExecutionMode %4 OriginUpperLeft
1952 OpSource ESSL 320
1953 OpName %4 "main"
1954 OpName %8 "i"
1955 %2 = OpTypeVoid
1956 %3 = OpTypeFunction %2
1957 %6 = OpTypeInt 32 1
1958 %7 = OpTypePointer Function %6
1959 %9 = OpConstant %6 0
1960 %16 = OpConstant %6 100
1961 %17 = OpTypeBool
1962 %50 = OpConstantTrue %17
1963 %20 = OpConstant %6 1
1964 %4 = OpFunction %2 None %3
1965 %5 = OpLabel
1966 %8 = OpVariable %7 Function
1967 OpStore %8 %9
1968 OpBranch %10
1969 %10 = OpLabel
1970 OpLoopMerge %12 %13 None
1971 OpBranch %14
1972 %14 = OpLabel
1973 %15 = OpLoad %6 %8
1974 %18 = OpSLessThan %17 %15 %16
1975 OpBranchConditional %18 %600 %12
1976 %600 = OpLabel
1977 OpSelectionMerge %601 None
1978 OpBranchConditional %50 %11 %602
1979 %11 = OpLabel
1980 %19 = OpLoad %6 %8
1981 %21 = OpIAdd %6 %19 %20
1982 OpStore %8 %21
1983 OpBranch %601
1984 %602 = OpLabel
1985 %603 = OpLoad %6 %8
1986 %604 = OpIAdd %6 %603 %20
1987 OpStore %8 %604
1988 OpBranch %601
1989 %601 = OpLabel
1990 %605 = OpPhi %6 %19 %11 %603 %602
1991 %606 = OpPhi %6 %21 %11 %604 %602
1992 OpBranchConditional %50 %13 %12
1993 %13 = OpLabel
1994 %22 = OpLoad %6 %8
1995 %23 = OpIAdd %6 %22 %20
1996 OpStore %8 %23
1997 OpBranch %10
1998 %12 = OpLabel
1999 OpReturn
2000 OpFunctionEnd
2001 )";
2002
2003 ASSERT_TRUE(IsEqual(env, expected_shader, context.get()));
2004 }
2005
2006 TEST(TransformationDuplicateRegionWithSelectionTest,
2007 RegionExitIsOpBranchConditionalUsingBooleanDefinedInBlock) {
2008 std::string shader = R"(
2009 OpCapability Shader
2010 %1 = OpExtInstImport "GLSL.std.450"
2011 OpMemoryModel Logical GLSL450
2012 OpEntryPoint Fragment %4 "main"
2013 OpExecutionMode %4 OriginUpperLeft
2014 OpSource ESSL 320
2015 OpName %4 "main"
2016 OpName %8 "i"
2017 %2 = OpTypeVoid
2018 %3 = OpTypeFunction %2
2019 %6 = OpTypeInt 32 1
2020 %7 = OpTypePointer Function %6
2021 %9 = OpConstant %6 0
2022 %16 = OpConstant %6 100
2023 %17 = OpTypeBool
2024 %50 = OpConstantTrue %17
2025 %20 = OpConstant %6 1
2026 %4 = OpFunction %2 None %3
2027 %5 = OpLabel
2028 %8 = OpVariable %7 Function
2029 OpStore %8 %9
2030 OpBranch %10
2031 %10 = OpLabel
2032 OpLoopMerge %12 %13 None
2033 OpBranch %14
2034 %14 = OpLabel
2035 %15 = OpLoad %6 %8
2036 %18 = OpSLessThan %17 %15 %16
2037 OpBranchConditional %18 %11 %12
2038 %11 = OpLabel
2039 %19 = OpLoad %6 %8
2040 %21 = OpIAdd %6 %19 %20
2041 %70 = OpCopyObject %17 %50
2042 OpStore %8 %21
2043 OpBranchConditional %70 %13 %12
2044 %13 = OpLabel
2045 %22 = OpLoad %6 %8
2046 %23 = OpIAdd %6 %22 %20
2047 OpStore %8 %23
2048 OpBranch %10
2049 %12 = OpLabel
2050 OpReturn
2051 OpFunctionEnd
2052 )";
2053
2054 const auto env = SPV_ENV_UNIVERSAL_1_4;
2055 const auto consumer = nullptr;
2056 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
2057 spvtools::ValidatorOptions validator_options;
2058 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
2059 kConsoleMessageConsumer));
2060 TransformationContext transformation_context(
2061 MakeUnique<FactManager>(context.get()), validator_options);
2062
2063 TransformationDuplicateRegionWithSelection transformation_good_1 =
2064 TransformationDuplicateRegionWithSelection(
2065 600, 50, 601, 11, 11, {{11, 602}}, {{19, 603}, {21, 604}, {70, 608}},
2066 {{19, 605}, {21, 606}, {70, 607}});
2067 ASSERT_TRUE(transformation_good_1.IsApplicable(context.get(),
2068 transformation_context));
2069 ApplyAndCheckFreshIds(transformation_good_1, context.get(),
2070 &transformation_context);
2071 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
2072 kConsoleMessageConsumer));
2073
2074 std::string expected_shader = R"(
2075 OpCapability Shader
2076 %1 = OpExtInstImport "GLSL.std.450"
2077 OpMemoryModel Logical GLSL450
2078 OpEntryPoint Fragment %4 "main"
2079 OpExecutionMode %4 OriginUpperLeft
2080 OpSource ESSL 320
2081 OpName %4 "main"
2082 OpName %8 "i"
2083 %2 = OpTypeVoid
2084 %3 = OpTypeFunction %2
2085 %6 = OpTypeInt 32 1
2086 %7 = OpTypePointer Function %6
2087 %9 = OpConstant %6 0
2088 %16 = OpConstant %6 100
2089 %17 = OpTypeBool
2090 %50 = OpConstantTrue %17
2091 %20 = OpConstant %6 1
2092 %4 = OpFunction %2 None %3
2093 %5 = OpLabel
2094 %8 = OpVariable %7 Function
2095 OpStore %8 %9
2096 OpBranch %10
2097 %10 = OpLabel
2098 OpLoopMerge %12 %13 None
2099 OpBranch %14
2100 %14 = OpLabel
2101 %15 = OpLoad %6 %8
2102 %18 = OpSLessThan %17 %15 %16
2103 OpBranchConditional %18 %600 %12
2104 %600 = OpLabel
2105 OpSelectionMerge %601 None
2106 OpBranchConditional %50 %11 %602
2107 %11 = OpLabel
2108 %19 = OpLoad %6 %8
2109 %21 = OpIAdd %6 %19 %20
2110 %70 = OpCopyObject %17 %50
2111 OpStore %8 %21
2112 OpBranch %601
2113 %602 = OpLabel
2114 %603 = OpLoad %6 %8
2115 %604 = OpIAdd %6 %603 %20
2116 %608 = OpCopyObject %17 %50
2117 OpStore %8 %604
2118 OpBranch %601
2119 %601 = OpLabel
2120 %605 = OpPhi %6 %19 %11 %603 %602
2121 %606 = OpPhi %6 %21 %11 %604 %602
2122 %607 = OpPhi %17 %70 %11 %608 %602
2123 OpBranchConditional %607 %13 %12
2124 %13 = OpLabel
2125 %22 = OpLoad %6 %8
2126 %23 = OpIAdd %6 %22 %20
2127 OpStore %8 %23
2128 OpBranch %10
2129 %12 = OpLabel
2130 OpReturn
2131 OpFunctionEnd
2132 )";
2133
2134 ASSERT_TRUE(IsEqual(env, expected_shader, context.get()));
2135 }
2136
2137 TEST(TransformationDuplicateRegionWithSelectionTest,
2138 RegionExitUsesOpReturnValueWithIdDefinedInRegion) {
2139 std::string shader = R"(
2140 OpCapability Shader
2141 %1 = OpExtInstImport "GLSL.std.450"
2142 OpMemoryModel Logical GLSL450
2143 OpEntryPoint Fragment %4 "main"
2144 OpExecutionMode %4 OriginUpperLeft
2145 OpSource ESSL 320
2146 %2 = OpTypeVoid
2147 %3 = OpTypeFunction %2
2148 %6 = OpTypeInt 32 1
2149 %7 = OpTypeFunction %6
2150 %10 = OpConstant %6 2
2151 %30 = OpTypeBool
2152 %31 = OpConstantTrue %30
2153 %4 = OpFunction %2 None %3
2154 %5 = OpLabel
2155 %13 = OpFunctionCall %6 %8
2156 OpReturn
2157 OpFunctionEnd
2158 %8 = OpFunction %6 None %7
2159 %9 = OpLabel
2160 OpBranch %20
2161 %20 = OpLabel
2162 %21 = OpCopyObject %6 %10
2163 OpReturnValue %21
2164 OpFunctionEnd
2165 )";
2166
2167 const auto env = SPV_ENV_UNIVERSAL_1_4;
2168 const auto consumer = nullptr;
2169 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
2170 spvtools::ValidatorOptions validator_options;
2171 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
2172 kConsoleMessageConsumer));
2173 TransformationContext transformation_context(
2174 MakeUnique<FactManager>(context.get()), validator_options);
2175
2176 TransformationDuplicateRegionWithSelection transformation_good_1 =
2177 TransformationDuplicateRegionWithSelection(
2178 600, 31, 601, 20, 20, {{20, 602}}, {{21, 603}}, {{21, 605}});
2179 ASSERT_TRUE(transformation_good_1.IsApplicable(context.get(),
2180 transformation_context));
2181 ApplyAndCheckFreshIds(transformation_good_1, context.get(),
2182 &transformation_context);
2183 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
2184 kConsoleMessageConsumer));
2185
2186 std::string expected_shader = R"(
2187 OpCapability Shader
2188 %1 = OpExtInstImport "GLSL.std.450"
2189 OpMemoryModel Logical GLSL450
2190 OpEntryPoint Fragment %4 "main"
2191 OpExecutionMode %4 OriginUpperLeft
2192 OpSource ESSL 320
2193 %2 = OpTypeVoid
2194 %3 = OpTypeFunction %2
2195 %6 = OpTypeInt 32 1
2196 %7 = OpTypeFunction %6
2197 %10 = OpConstant %6 2
2198 %30 = OpTypeBool
2199 %31 = OpConstantTrue %30
2200 %4 = OpFunction %2 None %3
2201 %5 = OpLabel
2202 %13 = OpFunctionCall %6 %8
2203 OpReturn
2204 OpFunctionEnd
2205 %8 = OpFunction %6 None %7
2206 %9 = OpLabel
2207 OpBranch %600
2208 %600 = OpLabel
2209 OpSelectionMerge %601 None
2210 OpBranchConditional %31 %20 %602
2211 %20 = OpLabel
2212 %21 = OpCopyObject %6 %10
2213 OpBranch %601
2214 %602 = OpLabel
2215 %603 = OpCopyObject %6 %10
2216 OpBranch %601
2217 %601 = OpLabel
2218 %605 = OpPhi %6 %21 %20 %603 %602
2219 OpReturnValue %605
2220 OpFunctionEnd
2221 )";
2222
2223 ASSERT_TRUE(IsEqual(env, expected_shader, context.get()));
2224 }
2225
2226 TEST(TransformationDuplicateRegionWithSelectionTest,
2227 InapplicableDueToOpTypeSampledImage) {
2228 std::string reference_shader = R"(
2229 OpCapability Shader
2230 %1 = OpExtInstImport "GLSL.std.450"
2231 OpMemoryModel Logical GLSL450
2232 OpEntryPoint Fragment %4 "main" %10
2233 OpExecutionMode %4 OriginUpperLeft
2234 OpSource ESSL 320
2235 OpDecorate %10 RelaxedPrecision
2236 OpDecorate %10 DescriptorSet 0
2237 OpDecorate %10 Binding 0
2238 %2 = OpTypeVoid
2239 %3 = OpTypeFunction %2
2240 %6 = OpTypeFloat 32
2241 %7 = OpTypeImage %6 2D 0 0 0 1 Unknown
2242 %8 = OpTypeSampledImage %7
2243 %9 = OpTypePointer UniformConstant %8
2244 %10 = OpVariable %9 UniformConstant
2245 %12 = OpTypeVector %6 2
2246 %13 = OpConstant %6 0
2247 %14 = OpConstantComposite %12 %13 %13
2248 %15 = OpTypeVector %6 4
2249 %30 = OpTypeBool
2250 %31 = OpConstantTrue %30
2251 %4 = OpFunction %2 None %3
2252 %5 = OpLabel
2253 OpBranch %20
2254 %20 = OpLabel
2255 %11 = OpLoad %8 %10
2256 OpBranch %21
2257 %21 = OpLabel
2258 %16 = OpImageSampleImplicitLod %15 %11 %14
2259 OpReturn
2260 OpFunctionEnd
2261 )";
2262
2263 const auto env = SPV_ENV_UNIVERSAL_1_4;
2264 const auto consumer = nullptr;
2265 const auto context =
2266 BuildModule(env, consumer, reference_shader, kFuzzAssembleOption);
2267 spvtools::ValidatorOptions validator_options;
2268 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
2269 kConsoleMessageConsumer));
2270 TransformationContext transformation_context(
2271 MakeUnique<FactManager>(context.get()), validator_options);
2272
2273 ASSERT_FALSE(TransformationDuplicateRegionWithSelection(
2274 600, 31, 601, 20, 20, {{20, 602}}, {{11, 603}}, {{11, 605}})
2275 .IsApplicable(context.get(), transformation_context));
2276 }
2277
2278 } // namespace
2279 } // namespace fuzz
2280 } // namespace spvtools