Debian Codegraph @

test/fuzz/fact_manager/data_synonym_and_id_equation_facts_test.cpp

raw
1 // Copyright (c) 2019 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/fact_manager/fact_manager.h"
16
17 #include "gtest/gtest.h"
18 #include "source/fuzz/fuzzer_util.h"
19 #include "source/fuzz/transformation_merge_blocks.h"
20 #include "test/fuzz/fuzz_test_util.h"
21
22 namespace spvtools {
23 namespace fuzz {
24 namespace {
25
26 void CheckConsistencyOfSynonymFacts(
27 opt::IRContext* ir_context,
28 const TransformationContext& transformation_context) {
29 for (uint32_t id : transformation_context.GetFactManager()
30 ->GetIdsForWhichSynonymsAreKnown()) {
31 // Every id reported by the fact manager should exist in the module.
32 ASSERT_NE(ir_context->get_def_use_mgr()->GetDef(id), nullptr);
33 auto synonyms =
34 transformation_context.GetFactManager()->GetSynonymsForId(id);
35 for (auto& dd : synonyms) {
36 // Every reported synonym should have a base object that exists in the
37 // module.
38 ASSERT_NE(ir_context->get_def_use_mgr()->GetDef(dd->object()), nullptr);
39 }
40 }
41 }
42
43 TEST(DataSynonymAndIdEquationFactsTest, RecursiveAdditionOfFacts) {
44 std::string shader = R"(
45 OpCapability Shader
46 %1 = OpExtInstImport "GLSL.std.450"
47 OpMemoryModel Logical GLSL450
48 OpEntryPoint Fragment %12 "main"
49 OpExecutionMode %12 OriginUpperLeft
50 OpSource ESSL 310
51 %2 = OpTypeVoid
52 %3 = OpTypeFunction %2
53 %6 = OpTypeFloat 32
54 %7 = OpTypeVector %6 4
55 %8 = OpTypeMatrix %7 4
56 %9 = OpConstant %6 0
57 %10 = OpConstantComposite %7 %9 %9 %9 %9
58 %11 = OpConstantComposite %8 %10 %10 %10 %10
59 %12 = OpFunction %2 None %3
60 %13 = OpLabel
61 OpReturn
62 OpFunctionEnd
63 )";
64
65 const auto env = SPV_ENV_UNIVERSAL_1_3;
66 const auto consumer = nullptr;
67 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
68 spvtools::ValidatorOptions validator_options;
69 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
70 kConsoleMessageConsumer));
71
72 FactManager fact_manager(context.get());
73
74 fact_manager.AddFactDataSynonym(MakeDataDescriptor(10, {}),
75 MakeDataDescriptor(11, {2}));
76
77 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(10, {}),
78 MakeDataDescriptor(11, {2})));
79 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(10, {0}),
80 MakeDataDescriptor(11, {2, 0})));
81 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(10, {1}),
82 MakeDataDescriptor(11, {2, 1})));
83 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(10, {2}),
84 MakeDataDescriptor(11, {2, 2})));
85 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(10, {3}),
86 MakeDataDescriptor(11, {2, 3})));
87 }
88
89 TEST(DataSynonymAndIdEquationFactsTest, CorollaryConversionFacts) {
90 std::string shader = R"(
91 OpCapability Shader
92 %1 = OpExtInstImport "GLSL.std.450"
93 OpMemoryModel Logical GLSL450
94 OpEntryPoint Fragment %12 "main"
95 OpExecutionMode %12 OriginUpperLeft
96 OpSource ESSL 310
97 %2 = OpTypeVoid
98 %3 = OpTypeFunction %2
99 %6 = OpTypeInt 32 1
100 %7 = OpTypeInt 32 0
101 %8 = OpTypeVector %6 2
102 %9 = OpTypeVector %7 2
103 %10 = OpTypeFloat 32
104 %11 = OpTypeVector %10 2
105 %15 = OpConstant %6 24 ; synonym of %16
106 %16 = OpConstant %6 24
107 %17 = OpConstant %7 24 ; synonym of %18
108 %18 = OpConstant %7 24
109 %19 = OpConstantComposite %8 %15 %15 ; synonym of %20
110 %20 = OpConstantComposite %8 %16 %16
111 %21 = OpConstantComposite %9 %17 %17 ; synonym of %22
112 %22 = OpConstantComposite %9 %18 %18
113 %23 = OpConstantComposite %8 %15 %15 ; not a synonym of %19
114 %12 = OpFunction %2 None %3
115 %13 = OpLabel
116 %24 = OpConvertSToF %10 %15 ; synonym of %25
117 %25 = OpConvertSToF %10 %16
118 %26 = OpConvertUToF %10 %17 ; not a synonym of %27 (different opcode)
119 %27 = OpConvertSToF %10 %18
120 %28 = OpConvertUToF %11 %19 ; synonym of %29
121 %29 = OpConvertUToF %11 %20
122 %30 = OpConvertSToF %11 %21 ; not a synonym of %31 (different opcode)
123 %31 = OpConvertUToF %11 %22
124 %32 = OpConvertUToF %11 %23 ; not a synonym of %28 (operand is not synonymous)
125 OpReturn
126 OpFunctionEnd
127 )";
128
129 const auto env = SPV_ENV_UNIVERSAL_1_3;
130 const auto consumer = nullptr;
131 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
132 spvtools::ValidatorOptions validator_options;
133 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
134 kConsoleMessageConsumer));
135
136 FactManager fact_manager(context.get());
137
138 // Add equation facts
139 fact_manager.AddFactIdEquation(24, spv::Op::OpConvertSToF, {15});
140 fact_manager.AddFactIdEquation(25, spv::Op::OpConvertSToF, {16});
141 fact_manager.AddFactIdEquation(26, spv::Op::OpConvertUToF, {17});
142 fact_manager.AddFactIdEquation(27, spv::Op::OpConvertSToF, {18});
143 fact_manager.AddFactIdEquation(28, spv::Op::OpConvertUToF, {19});
144 fact_manager.AddFactIdEquation(29, spv::Op::OpConvertUToF, {20});
145 fact_manager.AddFactIdEquation(30, spv::Op::OpConvertSToF, {21});
146 fact_manager.AddFactIdEquation(31, spv::Op::OpConvertUToF, {22});
147 fact_manager.AddFactIdEquation(32, spv::Op::OpConvertUToF, {23});
148
149 fact_manager.AddFactDataSynonym(MakeDataDescriptor(15, {}),
150 MakeDataDescriptor(16, {}));
151 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(24, {}),
152 MakeDataDescriptor(25, {})));
153
154 fact_manager.AddFactDataSynonym(MakeDataDescriptor(17, {}),
155 MakeDataDescriptor(18, {}));
156 ASSERT_FALSE(fact_manager.IsSynonymous(MakeDataDescriptor(26, {}),
157 MakeDataDescriptor(27, {})));
158
159 fact_manager.AddFactDataSynonym(MakeDataDescriptor(19, {}),
160 MakeDataDescriptor(20, {}));
161 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(28, {}),
162 MakeDataDescriptor(29, {})));
163
164 fact_manager.AddFactDataSynonym(MakeDataDescriptor(21, {}),
165 MakeDataDescriptor(22, {}));
166 ASSERT_FALSE(fact_manager.IsSynonymous(MakeDataDescriptor(30, {}),
167 MakeDataDescriptor(31, {})));
168
169 ASSERT_FALSE(fact_manager.IsSynonymous(MakeDataDescriptor(32, {}),
170 MakeDataDescriptor(28, {})));
171 fact_manager.AddFactDataSynonym(MakeDataDescriptor(23, {}),
172 MakeDataDescriptor(19, {}));
173 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(32, {}),
174 MakeDataDescriptor(28, {})));
175 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(32, {}),
176 MakeDataDescriptor(29, {})));
177 }
178
179 TEST(DataSynonymAndIdEquationFactsTest, HandlesCorollariesWithInvalidIds) {
180 std::string shader = R"(
181 OpCapability Shader
182 %1 = OpExtInstImport "GLSL.std.450"
183 OpMemoryModel Logical GLSL450
184 OpEntryPoint Fragment %12 "main"
185 OpExecutionMode %12 OriginUpperLeft
186 OpSource ESSL 310
187 %2 = OpTypeVoid
188 %3 = OpTypeFunction %2
189 %6 = OpTypeFloat 32
190 %8 = OpTypeInt 32 1
191 %9 = OpConstant %8 3
192 %12 = OpFunction %2 None %3
193 %13 = OpLabel
194 %14 = OpConvertSToF %6 %9
195 OpBranch %16
196 %16 = OpLabel
197 %17 = OpPhi %6 %14 %13
198 %15 = OpConvertSToF %6 %9
199 %18 = OpConvertSToF %6 %9
200 OpReturn
201 OpFunctionEnd
202 )";
203
204 const auto env = SPV_ENV_UNIVERSAL_1_3;
205 const auto consumer = nullptr;
206 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
207 spvtools::ValidatorOptions validator_options;
208 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
209 kConsoleMessageConsumer));
210 TransformationContext transformation_context(
211 MakeUnique<FactManager>(context.get()), validator_options);
212
213 // Add required facts.
214 transformation_context.GetFactManager()->AddFactIdEquation(
215 14, spv::Op::OpConvertSToF, {9});
216 transformation_context.GetFactManager()->AddFactDataSynonym(
217 MakeDataDescriptor(14, {}), MakeDataDescriptor(17, {}));
218
219 CheckConsistencyOfSynonymFacts(context.get(), transformation_context);
220
221 // Apply TransformationMergeBlocks which will remove %17 from the module.
222 TransformationMergeBlocks transformation(16);
223 ASSERT_TRUE(
224 transformation.IsApplicable(context.get(), transformation_context));
225 transformation.Apply(context.get(), &transformation_context);
226 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
227 kConsoleMessageConsumer));
228
229 CheckConsistencyOfSynonymFacts(context.get(), transformation_context);
230
231 ASSERT_EQ(context->get_def_use_mgr()->GetDef(17), nullptr);
232
233 // Add another equation.
234 transformation_context.GetFactManager()->AddFactIdEquation(
235 15, spv::Op::OpConvertSToF, {9});
236
237 // Check that two ids are synonymous even though one of them doesn't exist in
238 // the module (%17).
239 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
240 MakeDataDescriptor(15, {}), MakeDataDescriptor(17, {})));
241 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
242 MakeDataDescriptor(15, {}), MakeDataDescriptor(14, {})));
243
244 CheckConsistencyOfSynonymFacts(context.get(), transformation_context);
245
246 // Remove some instructions from the module. At this point, the equivalence
247 // class of %14 has no valid members.
248 ASSERT_TRUE(context->KillDef(14));
249 ASSERT_TRUE(context->KillDef(15));
250
251 transformation_context.GetFactManager()->AddFactIdEquation(
252 18, spv::Op::OpConvertSToF, {9});
253
254 CheckConsistencyOfSynonymFacts(context.get(), transformation_context);
255
256 // We don't create synonyms if at least one of the equivalence classes has no
257 // valid members.
258 ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
259 MakeDataDescriptor(14, {}), MakeDataDescriptor(18, {})));
260 }
261
262 TEST(DataSynonymAndIdEquationFactsTest, LogicalNotEquationFacts) {
263 std::string shader = R"(
264 OpCapability Shader
265 %1 = OpExtInstImport "GLSL.std.450"
266 OpMemoryModel Logical GLSL450
267 OpEntryPoint Fragment %12 "main"
268 OpExecutionMode %12 OriginUpperLeft
269 OpSource ESSL 310
270 %2 = OpTypeVoid
271 %3 = OpTypeFunction %2
272 %6 = OpTypeBool
273 %7 = OpConstantTrue %6
274 %12 = OpFunction %2 None %3
275 %13 = OpLabel
276 %14 = OpLogicalNot %6 %7
277 %15 = OpCopyObject %6 %7
278 %16 = OpCopyObject %6 %14
279 %17 = OpLogicalNot %6 %16
280 OpReturn
281 OpFunctionEnd
282 )";
283
284 const auto env = SPV_ENV_UNIVERSAL_1_3;
285 const auto consumer = nullptr;
286 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
287 spvtools::ValidatorOptions validator_options;
288 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
289 kConsoleMessageConsumer));
290
291 FactManager fact_manager(context.get());
292
293 fact_manager.AddFactDataSynonym(MakeDataDescriptor(15, {}),
294 MakeDataDescriptor(7, {}));
295 fact_manager.AddFactDataSynonym(MakeDataDescriptor(16, {}),
296 MakeDataDescriptor(14, {}));
297 fact_manager.AddFactIdEquation(14, spv::Op::OpLogicalNot, {7});
298 fact_manager.AddFactIdEquation(17, spv::Op::OpLogicalNot, {16});
299
300 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(15, {}),
301 MakeDataDescriptor(7, {})));
302 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(17, {}),
303 MakeDataDescriptor(7, {})));
304 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(15, {}),
305 MakeDataDescriptor(17, {})));
306 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(16, {}),
307 MakeDataDescriptor(14, {})));
308 }
309
310 TEST(DataSynonymAndIdEquationFactsTest, SignedNegateEquationFacts) {
311 std::string shader = R"(
312 OpCapability Shader
313 %1 = OpExtInstImport "GLSL.std.450"
314 OpMemoryModel Logical GLSL450
315 OpEntryPoint Fragment %12 "main"
316 OpExecutionMode %12 OriginUpperLeft
317 OpSource ESSL 310
318 %2 = OpTypeVoid
319 %3 = OpTypeFunction %2
320 %6 = OpTypeInt 32 1
321 %7 = OpConstant %6 24
322 %12 = OpFunction %2 None %3
323 %13 = OpLabel
324 %14 = OpSNegate %6 %7
325 %15 = OpSNegate %6 %14
326 OpReturn
327 OpFunctionEnd
328 )";
329
330 const auto env = SPV_ENV_UNIVERSAL_1_3;
331 const auto consumer = nullptr;
332 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
333 spvtools::ValidatorOptions validator_options;
334 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
335 kConsoleMessageConsumer));
336
337 FactManager fact_manager(context.get());
338
339 fact_manager.AddFactIdEquation(14, spv::Op::OpSNegate, {7});
340 fact_manager.AddFactIdEquation(15, spv::Op::OpSNegate, {14});
341
342 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(7, {}),
343 MakeDataDescriptor(15, {})));
344 }
345
346 TEST(DataSynonymAndIdEquationFactsTest, AddSubNegateFacts1) {
347 std::string shader = R"(
348 OpCapability Shader
349 %1 = OpExtInstImport "GLSL.std.450"
350 OpMemoryModel Logical GLSL450
351 OpEntryPoint Fragment %12 "main"
352 OpExecutionMode %12 OriginUpperLeft
353 OpSource ESSL 310
354 %2 = OpTypeVoid
355 %3 = OpTypeFunction %2
356 %6 = OpTypeInt 32 1
357 %15 = OpConstant %6 24
358 %16 = OpConstant %6 37
359 %12 = OpFunction %2 None %3
360 %13 = OpLabel
361 %14 = OpIAdd %6 %15 %16
362 %17 = OpCopyObject %6 %15
363 %18 = OpCopyObject %6 %16
364 %19 = OpISub %6 %14 %18 ; ==> synonymous(%19, %15)
365 %20 = OpISub %6 %14 %17 ; ==> synonymous(%20, %16)
366 %21 = OpCopyObject %6 %14
367 %22 = OpISub %6 %16 %21
368 %23 = OpCopyObject %6 %22
369 %24 = OpSNegate %6 %23 ; ==> synonymous(%24, %15)
370 OpReturn
371 OpFunctionEnd
372 )";
373
374 const auto env = SPV_ENV_UNIVERSAL_1_3;
375 const auto consumer = nullptr;
376 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
377 spvtools::ValidatorOptions validator_options;
378 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
379 kConsoleMessageConsumer));
380
381 FactManager fact_manager(context.get());
382
383 fact_manager.AddFactIdEquation(14, spv::Op::OpIAdd, {15, 16});
384 fact_manager.AddFactDataSynonym(MakeDataDescriptor(17, {}),
385 MakeDataDescriptor(15, {}));
386 fact_manager.AddFactDataSynonym(MakeDataDescriptor(18, {}),
387 MakeDataDescriptor(16, {}));
388 fact_manager.AddFactIdEquation(19, spv::Op::OpISub, {14, 18});
389 fact_manager.AddFactIdEquation(20, spv::Op::OpISub, {14, 17});
390 fact_manager.AddFactDataSynonym(MakeDataDescriptor(21, {}),
391 MakeDataDescriptor(14, {}));
392 fact_manager.AddFactIdEquation(22, spv::Op::OpISub, {16, 21});
393 fact_manager.AddFactDataSynonym(MakeDataDescriptor(23, {}),
394 MakeDataDescriptor(22, {}));
395 fact_manager.AddFactIdEquation(24, spv::Op::OpSNegate, {23});
396
397 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(19, {}),
398 MakeDataDescriptor(15, {})));
399 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(20, {}),
400 MakeDataDescriptor(16, {})));
401 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(24, {}),
402 MakeDataDescriptor(15, {})));
403 }
404
405 TEST(DataSynonymAndIdEquationFactsTest, AddSubNegateFacts2) {
406 std::string shader = R"(
407 OpCapability Shader
408 %1 = OpExtInstImport "GLSL.std.450"
409 OpMemoryModel Logical GLSL450
410 OpEntryPoint Fragment %12 "main"
411 OpExecutionMode %12 OriginUpperLeft
412 OpSource ESSL 310
413 %2 = OpTypeVoid
414 %3 = OpTypeFunction %2
415 %6 = OpTypeInt 32 1
416 %15 = OpConstant %6 24
417 %16 = OpConstant %6 37
418 %12 = OpFunction %2 None %3
419 %13 = OpLabel
420 %14 = OpISub %6 %15 %16
421 %17 = OpIAdd %6 %14 %16 ; ==> synonymous(%17, %15)
422 %18 = OpIAdd %6 %16 %14 ; ==> synonymous(%17, %18, %15)
423 %19 = OpISub %6 %14 %15
424 %20 = OpSNegate %6 %19 ; ==> synonymous(%20, %16)
425 %21 = OpISub %6 %14 %19 ; ==> synonymous(%21, %15)
426 %22 = OpISub %6 %14 %18
427 %23 = OpSNegate %6 %22 ; ==> synonymous(%23, %16)
428 OpReturn
429 OpFunctionEnd
430 )";
431
432 const auto env = SPV_ENV_UNIVERSAL_1_3;
433 const auto consumer = nullptr;
434 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
435 spvtools::ValidatorOptions validator_options;
436 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
437 kConsoleMessageConsumer));
438
439 FactManager fact_manager(context.get());
440
441 fact_manager.AddFactIdEquation(14, spv::Op::OpISub, {15, 16});
442 fact_manager.AddFactIdEquation(17, spv::Op::OpIAdd, {14, 16});
443
444 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(17, {}),
445 MakeDataDescriptor(15, {})));
446
447 fact_manager.AddFactIdEquation(18, spv::Op::OpIAdd, {16, 14});
448
449 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(18, {}),
450 MakeDataDescriptor(15, {})));
451 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(17, {}),
452 MakeDataDescriptor(18, {})));
453
454 fact_manager.AddFactIdEquation(19, spv::Op::OpISub, {14, 15});
455 fact_manager.AddFactIdEquation(20, spv::Op::OpSNegate, {19});
456
457 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(20, {}),
458 MakeDataDescriptor(16, {})));
459
460 fact_manager.AddFactIdEquation(21, spv::Op::OpISub, {14, 19});
461 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(21, {}),
462 MakeDataDescriptor(15, {})));
463
464 fact_manager.AddFactIdEquation(22, spv::Op::OpISub, {14, 18});
465 fact_manager.AddFactIdEquation(23, spv::Op::OpSNegate, {22});
466 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(23, {}),
467 MakeDataDescriptor(16, {})));
468 }
469
470 TEST(DataSynonymAndIdEquationFactsTest, ConversionEquations) {
471 std::string shader = R"(
472 OpCapability Shader
473 %1 = OpExtInstImport "GLSL.std.450"
474 OpMemoryModel Logical GLSL450
475 OpEntryPoint Fragment %12 "main"
476 OpExecutionMode %12 OriginUpperLeft
477 OpSource ESSL 310
478 %2 = OpTypeVoid
479 %3 = OpTypeFunction %2
480 %4 = OpTypeInt 32 1
481 %5 = OpTypeInt 32 0
482 %6 = OpTypeFloat 32
483 %14 = OpTypeVector %4 2
484 %15 = OpTypeVector %5 2
485 %24 = OpTypeVector %6 2
486 %16 = OpConstant %4 32 ; synonym of %17
487 %17 = OpConstant %4 32
488 %18 = OpConstant %5 32 ; synonym of %19
489 %19 = OpConstant %5 32
490 %20 = OpConstantComposite %14 %16 %16 ; synonym of %21
491 %21 = OpConstantComposite %14 %17 %17
492 %22 = OpConstantComposite %15 %18 %18 ; synonym of %23
493 %23 = OpConstantComposite %15 %19 %19
494 %12 = OpFunction %2 None %3
495 %13 = OpLabel
496 %25 = OpConvertUToF %6 %16 ; synonym of %26
497 %26 = OpConvertUToF %6 %17
498 %27 = OpConvertSToF %24 %20 ; not a synonym of %28 (wrong opcode)
499 %28 = OpConvertUToF %24 %21
500 %29 = OpConvertSToF %6 %18 ; not a synonym of %30 (wrong opcode)
501 %30 = OpConvertUToF %6 %19
502 %31 = OpConvertSToF %24 %22 ; synonym of %32
503 %32 = OpConvertSToF %24 %23
504 %33 = OpConvertUToF %6 %17 ; synonym of %26
505 %34 = OpConvertSToF %24 %23 ; synonym of %32
506 OpReturn
507 OpFunctionEnd
508 )";
509
510 const auto env = SPV_ENV_UNIVERSAL_1_3;
511 const auto consumer = nullptr;
512 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
513 spvtools::ValidatorOptions validator_options;
514 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
515 kConsoleMessageConsumer));
516
517 FactManager fact_manager(context.get());
518
519 fact_manager.AddFactDataSynonym(MakeDataDescriptor(16, {}),
520 MakeDataDescriptor(17, {}));
521 fact_manager.AddFactDataSynonym(MakeDataDescriptor(18, {}),
522 MakeDataDescriptor(19, {}));
523 fact_manager.AddFactDataSynonym(MakeDataDescriptor(20, {}),
524 MakeDataDescriptor(21, {}));
525 fact_manager.AddFactDataSynonym(MakeDataDescriptor(22, {}),
526 MakeDataDescriptor(23, {}));
527
528 fact_manager.AddFactIdEquation(25, spv::Op::OpConvertUToF, {16});
529 fact_manager.AddFactIdEquation(26, spv::Op::OpConvertUToF, {17});
530 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(25, {}),
531 MakeDataDescriptor(26, {})));
532
533 fact_manager.AddFactIdEquation(27, spv::Op::OpConvertSToF, {20});
534 fact_manager.AddFactIdEquation(28, spv::Op::OpConvertUToF, {21});
535 ASSERT_FALSE(fact_manager.IsSynonymous(MakeDataDescriptor(27, {}),
536 MakeDataDescriptor(28, {})));
537
538 fact_manager.AddFactIdEquation(29, spv::Op::OpConvertSToF, {18});
539 fact_manager.AddFactIdEquation(30, spv::Op::OpConvertUToF, {19});
540 ASSERT_FALSE(fact_manager.IsSynonymous(MakeDataDescriptor(29, {}),
541 MakeDataDescriptor(30, {})));
542
543 fact_manager.AddFactIdEquation(31, spv::Op::OpConvertSToF, {22});
544 fact_manager.AddFactIdEquation(32, spv::Op::OpConvertSToF, {23});
545 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(31, {}),
546 MakeDataDescriptor(32, {})));
547
548 fact_manager.AddFactIdEquation(33, spv::Op::OpConvertUToF, {17});
549 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(33, {}),
550 MakeDataDescriptor(26, {})));
551
552 fact_manager.AddFactIdEquation(34, spv::Op::OpConvertSToF, {23});
553 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(32, {}),
554 MakeDataDescriptor(34, {})));
555 }
556
557 TEST(DataSynonymAndIdEquationFactsTest, BitcastEquationFacts) {
558 std::string shader = R"(
559 OpCapability Shader
560 %1 = OpExtInstImport "GLSL.std.450"
561 OpMemoryModel Logical GLSL450
562 OpEntryPoint Fragment %12 "main"
563 OpExecutionMode %12 OriginUpperLeft
564 OpSource ESSL 310
565 %2 = OpTypeVoid
566 %3 = OpTypeFunction %2
567 %4 = OpTypeInt 32 1
568 %5 = OpTypeInt 32 0
569 %8 = OpTypeFloat 32
570 %9 = OpTypeVector %4 2
571 %10 = OpTypeVector %5 2
572 %11 = OpTypeVector %8 2
573 %6 = OpConstant %4 23
574 %7 = OpConstant %5 23
575 %19 = OpConstant %8 23
576 %20 = OpConstantComposite %9 %6 %6
577 %21 = OpConstantComposite %10 %7 %7
578 %22 = OpConstantComposite %11 %19 %19
579 %12 = OpFunction %2 None %3
580 %13 = OpLabel
581 %30 = OpBitcast %8 %6
582 %31 = OpBitcast %5 %6
583 %32 = OpBitcast %8 %7
584 %33 = OpBitcast %4 %7
585 %34 = OpBitcast %4 %19
586 %35 = OpBitcast %5 %19
587 %36 = OpBitcast %10 %20
588 %37 = OpBitcast %11 %20
589 %38 = OpBitcast %9 %21
590 %39 = OpBitcast %11 %21
591 %40 = OpBitcast %9 %22
592 %41 = OpBitcast %10 %22
593 OpReturn
594 OpFunctionEnd
595 )";
596
597 const auto env = SPV_ENV_UNIVERSAL_1_3;
598 const auto consumer = nullptr;
599 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
600 spvtools::ValidatorOptions validator_options;
601 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
602 kConsoleMessageConsumer));
603
604 FactManager fact_manager(context.get());
605
606 uint32_t lhs_id = 30;
607 for (uint32_t rhs_id : {6, 6, 7, 7, 19, 19, 20, 20, 21, 21, 22, 22}) {
608 fact_manager.AddFactIdEquation(lhs_id, spv::Op::OpBitcast, {rhs_id});
609 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(lhs_id, {}),
610 MakeDataDescriptor(rhs_id, {})));
611 ++lhs_id;
612 }
613 }
614
615 TEST(DataSynonymAndIdEquationFactsTest, EquationAndEquivalenceFacts) {
616 std::string shader = R"(
617 OpCapability Shader
618 %1 = OpExtInstImport "GLSL.std.450"
619 OpMemoryModel Logical GLSL450
620 OpEntryPoint Fragment %12 "main"
621 OpExecutionMode %12 OriginUpperLeft
622 OpSource ESSL 310
623 %2 = OpTypeVoid
624 %3 = OpTypeFunction %2
625 %6 = OpTypeInt 32 1
626 %15 = OpConstant %6 24
627 %16 = OpConstant %6 37
628 %12 = OpFunction %2 None %3
629 %13 = OpLabel
630 %14 = OpISub %6 %15 %16
631 %114 = OpCopyObject %6 %14
632 %17 = OpIAdd %6 %114 %16 ; ==> synonymous(%17, %15)
633 %18 = OpIAdd %6 %16 %114 ; ==> synonymous(%17, %18, %15)
634 %19 = OpISub %6 %114 %15
635 %119 = OpCopyObject %6 %19
636 %20 = OpSNegate %6 %119 ; ==> synonymous(%20, %16)
637 %21 = OpISub %6 %14 %19 ; ==> synonymous(%21, %15)
638 %22 = OpISub %6 %14 %18
639 %220 = OpCopyObject %6 %22
640 %23 = OpSNegate %6 %220 ; ==> synonymous(%23, %16)
641 OpReturn
642 OpFunctionEnd
643 )";
644
645 const auto env = SPV_ENV_UNIVERSAL_1_3;
646 const auto consumer = nullptr;
647 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
648 spvtools::ValidatorOptions validator_options;
649 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
650 kConsoleMessageConsumer));
651
652 FactManager fact_manager(context.get());
653
654 fact_manager.AddFactIdEquation(14, spv::Op::OpISub, {15, 16});
655 fact_manager.AddFactDataSynonym(MakeDataDescriptor(114, {}),
656 MakeDataDescriptor(14, {}));
657 fact_manager.AddFactIdEquation(17, spv::Op::OpIAdd, {114, 16});
658
659 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(17, {}),
660 MakeDataDescriptor(15, {})));
661
662 fact_manager.AddFactIdEquation(18, spv::Op::OpIAdd, {16, 114});
663
664 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(18, {}),
665 MakeDataDescriptor(15, {})));
666 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(17, {}),
667 MakeDataDescriptor(18, {})));
668
669 fact_manager.AddFactIdEquation(19, spv::Op::OpISub, {14, 15});
670 fact_manager.AddFactDataSynonym(MakeDataDescriptor(119, {}),
671 MakeDataDescriptor(19, {}));
672 fact_manager.AddFactIdEquation(20, spv::Op::OpSNegate, {119});
673
674 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(20, {}),
675 MakeDataDescriptor(16, {})));
676
677 fact_manager.AddFactIdEquation(21, spv::Op::OpISub, {14, 19});
678 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(21, {}),
679 MakeDataDescriptor(15, {})));
680
681 fact_manager.AddFactIdEquation(22, spv::Op::OpISub, {14, 18});
682 fact_manager.AddFactDataSynonym(MakeDataDescriptor(22, {}),
683 MakeDataDescriptor(220, {}));
684 fact_manager.AddFactIdEquation(23, spv::Op::OpSNegate, {220});
685 ASSERT_TRUE(fact_manager.IsSynonymous(MakeDataDescriptor(23, {}),
686 MakeDataDescriptor(16, {})));
687 }
688
689 } // namespace
690 } // namespace fuzz
691 } // namespace spvtools