Debian Codegraph @

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