Debian Codegraph @

test/fuzz/transformation_record_synonymous_constants_test.cpp

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