Debian Codegraph @

test/fuzz/transformation_add_synonym_test.cpp

raw
1 // Copyright (c) 2020 Vasyl Teliman
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "source/fuzz/transformation_add_synonym.h"
16
17 #include "gtest/gtest.h"
18 #include "source/fuzz/fuzzer_util.h"
19 #include "source/fuzz/instruction_descriptor.h"
20 #include "test/fuzz/fuzz_test_util.h"
21
22 namespace spvtools {
23 namespace fuzz {
24 namespace {
25
26 TEST(TransformationAddSynonymTest, NotApplicable) {
27 std::string shader = R"(
28 OpCapability Shader
29 %1 = OpExtInstImport "GLSL.std.450"
30 OpMemoryModel Logical GLSL450
31 OpEntryPoint Fragment %4 "main"
32 OpExecutionMode %4 OriginUpperLeft
33 OpSource ESSL 310
34 OpDecorate %8 RelaxedPrecision
35 OpDecorate %22 RelaxedPrecision
36 %2 = OpTypeVoid
37 %3 = OpTypeFunction %2
38 %6 = OpTypeInt 32 1
39 %7 = OpTypePointer Function %6
40 %9 = OpConstant %6 3
41 %10 = OpTypeFloat 32
42 %11 = OpTypePointer Function %10
43 %13 = OpConstant %10 4.5
44 %14 = OpTypeVector %10 2
45 %15 = OpTypePointer Function %14
46 %17 = OpConstant %10 3
47 %18 = OpConstant %10 4
48 %19 = OpConstantComposite %14 %17 %18
49 %20 = OpTypeVector %6 2
50 %21 = OpTypePointer Function %20
51 %23 = OpConstant %6 4
52 %24 = OpConstantComposite %20 %9 %23
53 %26 = OpConstantNull %6
54 %4 = OpFunction %2 None %3
55 %5 = OpLabel
56 %8 = OpVariable %7 Function
57 %12 = OpVariable %11 Function
58 %16 = OpVariable %15 Function
59 %22 = OpVariable %21 Function
60 OpStore %8 %9
61 OpStore %12 %13
62 OpStore %16 %19
63 OpStore %22 %24
64 %25 = OpUndef %6
65 %27 = OpLoad %6 %8
66 OpReturn
67 OpFunctionEnd
68 )";
69
70 const auto env = SPV_ENV_UNIVERSAL_1_3;
71 const auto consumer = nullptr;
72 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
73 spvtools::ValidatorOptions validator_options;
74 validator_options.SetRelaxLogicalPointer(true);
75 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
76 kConsoleMessageConsumer));
77 TransformationContext transformation_context(
78 MakeUnique<FactManager>(context.get()), validator_options);
79 transformation_context.GetFactManager()->AddFactIdIsIrrelevant(24);
80
81 auto insert_before = MakeInstructionDescriptor(22, spv::Op::OpReturn, 0);
82
83 #ifndef NDEBUG
84 ASSERT_DEATH(
85 TransformationAddSynonym(
86 9, static_cast<protobufs::TransformationAddSynonym::SynonymType>(-1),
87 40, insert_before)
88 .IsApplicable(context.get(), transformation_context),
89 "Synonym type is invalid");
90 #endif
91
92 // These tests should succeed regardless of the synonym type.
93 for (int i = 0;
94 i < protobufs::TransformationAddSynonym::SynonymType_descriptor()
95 ->value_count();
96 ++i) {
97 const auto* synonym_value =
98 protobufs::TransformationAddSynonym::SynonymType_descriptor()->value(i);
99 ASSERT_TRUE(protobufs::TransformationAddSynonym::SynonymType_IsValid(
100 synonym_value->number()));
101 auto synonym_type =
102 static_cast<protobufs::TransformationAddSynonym::SynonymType>(
103 synonym_value->number());
104
105 // |synonym_fresh_id| is not fresh.
106 ASSERT_FALSE(TransformationAddSynonym(9, synonym_type, 9, insert_before)
107 .IsApplicable(context.get(), transformation_context));
108
109 // |result_id| is invalid.
110 ASSERT_FALSE(TransformationAddSynonym(40, synonym_type, 40, insert_before)
111 .IsApplicable(context.get(), transformation_context));
112
113 // Instruction with |result_id| has no type id.
114 ASSERT_FALSE(TransformationAddSynonym(5, synonym_type, 40, insert_before)
115 .IsApplicable(context.get(), transformation_context));
116
117 // Instruction with |result_id| is an OpUndef.
118 ASSERT_FALSE(TransformationAddSynonym(25, synonym_type, 40, insert_before)
119 .IsApplicable(context.get(), transformation_context));
120
121 // Instruction with |result_id| is an OpConstantNull.
122 ASSERT_FALSE(TransformationAddSynonym(26, synonym_type, 40, insert_before)
123 .IsApplicable(context.get(), transformation_context));
124
125 // |result_id| is irrelevant.
126 ASSERT_FALSE(TransformationAddSynonym(24, synonym_type, 40, insert_before)
127 .IsApplicable(context.get(), transformation_context));
128
129 // |insert_before| is invalid.
130 ASSERT_FALSE(TransformationAddSynonym(
131 9, synonym_type, 40,
132 MakeInstructionDescriptor(25, spv::Op::OpStore, 0))
133 .IsApplicable(context.get(), transformation_context));
134
135 // Can't insert before |insert_before|.
136 ASSERT_FALSE(TransformationAddSynonym(
137 9, synonym_type, 40,
138 MakeInstructionDescriptor(5, spv::Op::OpLabel, 0))
139 .IsApplicable(context.get(), transformation_context));
140 ASSERT_FALSE(TransformationAddSynonym(
141 9, synonym_type, 40,
142 MakeInstructionDescriptor(22, spv::Op::OpVariable, 0))
143 .IsApplicable(context.get(), transformation_context));
144 ASSERT_FALSE(TransformationAddSynonym(
145 9, synonym_type, 40,
146 MakeInstructionDescriptor(25, spv::Op::OpFunctionEnd, 0))
147 .IsApplicable(context.get(), transformation_context));
148
149 // Domination rules are not satisfied.
150 ASSERT_FALSE(TransformationAddSynonym(
151 27, synonym_type, 40,
152 MakeInstructionDescriptor(27, spv::Op::OpLoad, 0))
153 .IsApplicable(context.get(), transformation_context));
154 ASSERT_FALSE(TransformationAddSynonym(
155 27, synonym_type, 40,
156 MakeInstructionDescriptor(22, spv::Op::OpStore, 1))
157 .IsApplicable(context.get(), transformation_context));
158 }
159 }
160
161 TEST(TransformationAddSynonymTest, AddZeroSubZeroMulOne) {
162 std::string shader = R"(
163 OpCapability Shader
164 %1 = OpExtInstImport "GLSL.std.450"
165 OpMemoryModel Logical GLSL450
166 OpEntryPoint Fragment %4 "main"
167 OpExecutionMode %4 OriginUpperLeft
168 OpSource ESSL 310
169 %2 = OpTypeVoid
170 %3 = OpTypeFunction %2
171 %6 = OpTypeInt 32 1
172 %7 = OpConstant %6 0
173 %8 = OpConstant %6 1
174 %9 = OpConstant %6 34
175 %10 = OpTypeInt 32 0
176 %13 = OpConstant %10 34
177 %14 = OpTypeFloat 32
178 %15 = OpConstant %14 0
179 %16 = OpConstant %14 1
180 %17 = OpConstant %14 34
181 %18 = OpTypeVector %14 2
182 %19 = OpConstantComposite %18 %15 %15
183 %20 = OpConstantComposite %18 %16 %16
184 %21 = OpConstant %14 3
185 %22 = OpConstant %14 4
186 %23 = OpConstantComposite %18 %21 %22
187 %24 = OpTypeVector %6 2
188 %25 = OpConstantComposite %24 %7 %7
189 %26 = OpConstantComposite %24 %8 %8
190 %27 = OpConstant %6 3
191 %28 = OpConstant %6 4
192 %29 = OpConstantComposite %24 %27 %28
193 %30 = OpTypeVector %10 2
194 %33 = OpConstant %10 3
195 %34 = OpConstant %10 4
196 %35 = OpConstantComposite %30 %33 %34
197 %36 = OpTypeBool
198 %37 = OpTypeVector %36 2
199 %38 = OpConstantTrue %36
200 %39 = OpConstantComposite %37 %38 %38
201 %40 = OpConstant %6 37
202 %4 = OpFunction %2 None %3
203 %5 = OpLabel
204 OpReturn
205 OpFunctionEnd
206 )";
207
208 const auto env = SPV_ENV_UNIVERSAL_1_3;
209 const auto consumer = nullptr;
210 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
211 spvtools::ValidatorOptions validator_options;
212 validator_options.SetRelaxLogicalPointer(true);
213 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
214 kConsoleMessageConsumer));
215 TransformationContext transformation_context(
216 MakeUnique<FactManager>(context.get()), validator_options);
217 auto insert_before = MakeInstructionDescriptor(5, spv::Op::OpReturn, 0);
218
219 uint32_t fresh_id = 50;
220 for (auto synonym_type : {protobufs::TransformationAddSynonym::ADD_ZERO,
221 protobufs::TransformationAddSynonym::SUB_ZERO,
222 protobufs::TransformationAddSynonym::MUL_ONE}) {
223 ASSERT_TRUE(
224 TransformationAddSynonym::IsAdditionalConstantRequired(synonym_type));
225
226 // Can't create a synonym of a scalar or a vector of a wrong (in this case -
227 // boolean) type.
228 ASSERT_FALSE(
229 TransformationAddSynonym(38, synonym_type, fresh_id, insert_before)
230 .IsApplicable(context.get(), transformation_context));
231 ASSERT_FALSE(
232 TransformationAddSynonym(39, synonym_type, fresh_id, insert_before)
233 .IsApplicable(context.get(), transformation_context));
234
235 // Required constant is not present in the module.
236 ASSERT_FALSE(
237 TransformationAddSynonym(13, synonym_type, fresh_id, insert_before)
238 .IsApplicable(context.get(), transformation_context));
239 ASSERT_FALSE(
240 TransformationAddSynonym(35, synonym_type, fresh_id, insert_before)
241 .IsApplicable(context.get(), transformation_context));
242
243 for (auto result_id : {9, 17, 23, 29}) {
244 TransformationAddSynonym transformation(result_id, synonym_type, fresh_id,
245 insert_before);
246 ASSERT_TRUE(
247 transformation.IsApplicable(context.get(), transformation_context));
248 ApplyAndCheckFreshIds(transformation, context.get(),
249 &transformation_context);
250 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
251 MakeDataDescriptor(result_id, {}), MakeDataDescriptor(fresh_id, {})));
252 ++fresh_id;
253 }
254 }
255 {
256 TransformationAddSynonym transformation(
257 40, protobufs::TransformationAddSynonym::BITWISE_OR, fresh_id,
258 insert_before);
259 ASSERT_TRUE(
260 transformation.IsApplicable(context.get(), transformation_context));
261 ApplyAndCheckFreshIds(transformation, context.get(),
262 &transformation_context);
263 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
264 MakeDataDescriptor(40, {}), MakeDataDescriptor(fresh_id, {})));
265 ++fresh_id;
266 }
267 {
268 TransformationAddSynonym transformation(
269 40, protobufs::TransformationAddSynonym::BITWISE_XOR, fresh_id,
270 insert_before);
271 ASSERT_TRUE(
272 transformation.IsApplicable(context.get(), transformation_context));
273 ApplyAndCheckFreshIds(transformation, context.get(),
274 &transformation_context);
275 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
276 MakeDataDescriptor(40, {}), MakeDataDescriptor(fresh_id, {})));
277 }
278
279 std::string expected_shader = R"(
280 OpCapability Shader
281 %1 = OpExtInstImport "GLSL.std.450"
282 OpMemoryModel Logical GLSL450
283 OpEntryPoint Fragment %4 "main"
284 OpExecutionMode %4 OriginUpperLeft
285 OpSource ESSL 310
286 %2 = OpTypeVoid
287 %3 = OpTypeFunction %2
288 %6 = OpTypeInt 32 1
289 %7 = OpConstant %6 0
290 %8 = OpConstant %6 1
291 %9 = OpConstant %6 34
292 %10 = OpTypeInt 32 0
293 %13 = OpConstant %10 34
294 %14 = OpTypeFloat 32
295 %15 = OpConstant %14 0
296 %16 = OpConstant %14 1
297 %17 = OpConstant %14 34
298 %18 = OpTypeVector %14 2
299 %19 = OpConstantComposite %18 %15 %15
300 %20 = OpConstantComposite %18 %16 %16
301 %21 = OpConstant %14 3
302 %22 = OpConstant %14 4
303 %23 = OpConstantComposite %18 %21 %22
304 %24 = OpTypeVector %6 2
305 %25 = OpConstantComposite %24 %7 %7
306 %26 = OpConstantComposite %24 %8 %8
307 %27 = OpConstant %6 3
308 %28 = OpConstant %6 4
309 %29 = OpConstantComposite %24 %27 %28
310 %30 = OpTypeVector %10 2
311 %33 = OpConstant %10 3
312 %34 = OpConstant %10 4
313 %35 = OpConstantComposite %30 %33 %34
314 %36 = OpTypeBool
315 %37 = OpTypeVector %36 2
316 %38 = OpConstantTrue %36
317 %39 = OpConstantComposite %37 %38 %38
318 %40 = OpConstant %6 37
319 %4 = OpFunction %2 None %3
320 %5 = OpLabel
321 %50 = OpIAdd %6 %9 %7
322 %51 = OpFAdd %14 %17 %15
323 %52 = OpFAdd %18 %23 %19
324 %53 = OpIAdd %24 %29 %25
325 %54 = OpISub %6 %9 %7
326 %55 = OpFSub %14 %17 %15
327 %56 = OpFSub %18 %23 %19
328 %57 = OpISub %24 %29 %25
329 %58 = OpIMul %6 %9 %8
330 %59 = OpFMul %14 %17 %16
331 %60 = OpFMul %18 %23 %20
332 %61 = OpIMul %24 %29 %26
333 %62 = OpBitwiseOr %6 %40 %7
334 %63 = OpBitwiseXor %6 %40 %7
335 OpReturn
336 OpFunctionEnd
337 )";
338
339 ASSERT_TRUE(IsEqual(env, expected_shader, context.get()));
340 }
341
342 TEST(TransformationAddSynonymTest, LogicalAndLogicalOr) {
343 std::string shader = R"(
344 OpCapability Shader
345 %1 = OpExtInstImport "GLSL.std.450"
346 OpMemoryModel Logical GLSL450
347 OpEntryPoint Fragment %4 "main"
348 OpExecutionMode %4 OriginUpperLeft
349 OpSource ESSL 310
350 %2 = OpTypeVoid
351 %3 = OpTypeFunction %2
352 %6 = OpTypeBool
353 %7 = OpConstantFalse %6
354 %9 = OpConstantTrue %6
355 %10 = OpTypeVector %6 2
356 %11 = OpConstantComposite %10 %7 %9
357 %12 = OpConstantComposite %10 %7 %7
358 %13 = OpConstantComposite %10 %9 %9
359 %14 = OpTypeFloat 32
360 %17 = OpConstant %14 35
361 %18 = OpTypeVector %14 2
362 %21 = OpConstant %14 3
363 %22 = OpConstant %14 4
364 %23 = OpConstantComposite %18 %21 %22
365 %4 = OpFunction %2 None %3
366 %5 = OpLabel
367 OpReturn
368 OpFunctionEnd
369 )";
370
371 const auto env = SPV_ENV_UNIVERSAL_1_3;
372 const auto consumer = nullptr;
373 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
374 spvtools::ValidatorOptions validator_options;
375 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
376 kConsoleMessageConsumer));
377 TransformationContext transformation_context(
378 MakeUnique<FactManager>(context.get()), validator_options);
379 auto insert_before = MakeInstructionDescriptor(5, spv::Op::OpReturn, 0);
380
381 uint32_t fresh_id = 50;
382 for (auto synonym_type : {protobufs::TransformationAddSynonym::LOGICAL_AND,
383 protobufs::TransformationAddSynonym::LOGICAL_OR}) {
384 ASSERT_TRUE(
385 TransformationAddSynonym::IsAdditionalConstantRequired(synonym_type));
386
387 // Can't create a synonym of a scalar or a vector of a wrong (in this case -
388 // float) type.
389 ASSERT_FALSE(
390 TransformationAddSynonym(17, synonym_type, fresh_id, insert_before)
391 .IsApplicable(context.get(), transformation_context));
392 ASSERT_FALSE(
393 TransformationAddSynonym(23, synonym_type, fresh_id, insert_before)
394 .IsApplicable(context.get(), transformation_context));
395
396 for (auto result_id : {9, 11}) {
397 TransformationAddSynonym transformation(result_id, synonym_type, fresh_id,
398 insert_before);
399 ASSERT_TRUE(
400 transformation.IsApplicable(context.get(), transformation_context));
401 ApplyAndCheckFreshIds(transformation, context.get(),
402 &transformation_context);
403 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
404 MakeDataDescriptor(result_id, {}), MakeDataDescriptor(fresh_id, {})));
405 ++fresh_id;
406 }
407 }
408
409 std::string expected_shader = R"(
410 OpCapability Shader
411 %1 = OpExtInstImport "GLSL.std.450"
412 OpMemoryModel Logical GLSL450
413 OpEntryPoint Fragment %4 "main"
414 OpExecutionMode %4 OriginUpperLeft
415 OpSource ESSL 310
416 %2 = OpTypeVoid
417 %3 = OpTypeFunction %2
418 %6 = OpTypeBool
419 %7 = OpConstantFalse %6
420 %9 = OpConstantTrue %6
421 %10 = OpTypeVector %6 2
422 %11 = OpConstantComposite %10 %7 %9
423 %12 = OpConstantComposite %10 %7 %7
424 %13 = OpConstantComposite %10 %9 %9
425 %14 = OpTypeFloat 32
426 %17 = OpConstant %14 35
427 %18 = OpTypeVector %14 2
428 %21 = OpConstant %14 3
429 %22 = OpConstant %14 4
430 %23 = OpConstantComposite %18 %21 %22
431 %4 = OpFunction %2 None %3
432 %5 = OpLabel
433 %50 = OpLogicalAnd %6 %9 %9
434 %51 = OpLogicalAnd %10 %11 %13
435 %52 = OpLogicalOr %6 %9 %7
436 %53 = OpLogicalOr %10 %11 %12
437 OpReturn
438 OpFunctionEnd
439 )";
440
441 ASSERT_TRUE(IsEqual(env, expected_shader, context.get()));
442 }
443
444 TEST(TransformationAddSynonymTest, LogicalAndConstantIsNotPresent) {
445 std::string shader = R"(
446 OpCapability Shader
447 %1 = OpExtInstImport "GLSL.std.450"
448 OpMemoryModel Logical GLSL450
449 OpEntryPoint Fragment %4 "main"
450 OpExecutionMode %4 OriginUpperLeft
451 OpSource ESSL 310
452 %2 = OpTypeVoid
453 %3 = OpTypeFunction %2
454 %6 = OpTypeBool
455 %7 = OpConstantFalse %6
456 %10 = OpTypeVector %6 2
457 %12 = OpConstantComposite %10 %7 %7
458 %4 = OpFunction %2 None %3
459 %5 = OpLabel
460 OpReturn
461 OpFunctionEnd
462 )";
463
464 const auto env = SPV_ENV_UNIVERSAL_1_3;
465 const auto consumer = nullptr;
466 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
467 spvtools::ValidatorOptions validator_options;
468 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
469 kConsoleMessageConsumer));
470 TransformationContext transformation_context(
471 MakeUnique<FactManager>(context.get()), validator_options);
472 auto insert_before = MakeInstructionDescriptor(5, spv::Op::OpReturn, 0);
473 const auto synonym_type = protobufs::TransformationAddSynonym::LOGICAL_AND;
474
475 // Required constant is not present in the module.
476 ASSERT_FALSE(TransformationAddSynonym(7, synonym_type, 50, insert_before)
477 .IsApplicable(context.get(), transformation_context));
478 ASSERT_FALSE(TransformationAddSynonym(12, synonym_type, 50, insert_before)
479 .IsApplicable(context.get(), transformation_context));
480 }
481
482 TEST(TransformationAddSynonymTest, LogicalOrConstantIsNotPresent) {
483 std::string shader = R"(
484 OpCapability Shader
485 %1 = OpExtInstImport "GLSL.std.450"
486 OpMemoryModel Logical GLSL450
487 OpEntryPoint Fragment %4 "main"
488 OpExecutionMode %4 OriginUpperLeft
489 OpSource ESSL 310
490 %2 = OpTypeVoid
491 %3 = OpTypeFunction %2
492 %6 = OpTypeBool
493 %7 = OpConstantTrue %6
494 %10 = OpTypeVector %6 2
495 %12 = OpConstantComposite %10 %7 %7
496 %4 = OpFunction %2 None %3
497 %5 = OpLabel
498 OpReturn
499 OpFunctionEnd
500 )";
501
502 const auto env = SPV_ENV_UNIVERSAL_1_3;
503 const auto consumer = nullptr;
504 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
505 spvtools::ValidatorOptions validator_options;
506 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
507 kConsoleMessageConsumer));
508 TransformationContext transformation_context(
509 MakeUnique<FactManager>(context.get()), validator_options);
510 auto insert_before = MakeInstructionDescriptor(5, spv::Op::OpReturn, 0);
511 const auto synonym_type = protobufs::TransformationAddSynonym::LOGICAL_OR;
512
513 // Required constant is not present in the module.
514 ASSERT_FALSE(TransformationAddSynonym(7, synonym_type, 50, insert_before)
515 .IsApplicable(context.get(), transformation_context));
516 ASSERT_FALSE(TransformationAddSynonym(12, synonym_type, 50, insert_before)
517 .IsApplicable(context.get(), transformation_context));
518 }
519
520 TEST(TransformationAddSynonymTest, CopyObject) {
521 std::string shader = R"(
522 OpCapability Shader
523 %1 = OpExtInstImport "GLSL.std.450"
524 OpMemoryModel Logical GLSL450
525 OpEntryPoint Fragment %4 "main"
526 OpExecutionMode %4 OriginUpperLeft
527 OpSource ESSL 310
528 OpDecorate %8 RelaxedPrecision
529 %2 = OpTypeVoid
530 %3 = OpTypeFunction %2
531 %6 = OpTypeInt 32 1
532 %7 = OpTypePointer Function %6
533 %9 = OpConstant %6 4
534 %10 = OpTypeFloat 32
535 %11 = OpTypePointer Function %10
536 %13 = OpConstant %10 4
537 %14 = OpTypeVector %10 2
538 %15 = OpTypePointer Function %14
539 %17 = OpConstant %10 3.4000001
540 %18 = OpConstantComposite %14 %17 %17
541 %19 = OpTypeBool
542 %20 = OpTypeStruct %19
543 %21 = OpTypePointer Function %20
544 %23 = OpConstantTrue %19
545 %24 = OpConstantComposite %20 %23
546 %4 = OpFunction %2 None %3
547 %5 = OpLabel
548 %8 = OpVariable %7 Function
549 %12 = OpVariable %11 Function
550 %16 = OpVariable %15 Function
551 %22 = OpVariable %21 Function
552 OpStore %8 %9
553 OpStore %12 %13
554 OpStore %16 %18
555 OpStore %22 %24
556 OpReturn
557 OpFunctionEnd
558 )";
559
560 const auto env = SPV_ENV_UNIVERSAL_1_3;
561 const auto consumer = nullptr;
562 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
563 spvtools::ValidatorOptions validator_options;
564 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
565 kConsoleMessageConsumer));
566 TransformationContext transformation_context(
567 MakeUnique<FactManager>(context.get()), validator_options);
568 auto insert_before = MakeInstructionDescriptor(5, spv::Op::OpReturn, 0);
569 const auto synonym_type = protobufs::TransformationAddSynonym::COPY_OBJECT;
570
571 ASSERT_FALSE(
572 TransformationAddSynonym::IsAdditionalConstantRequired(synonym_type));
573
574 uint32_t fresh_id = 50;
575 for (auto result_id : {9, 13, 17, 18, 23, 24, 22}) {
576 TransformationAddSynonym transformation(result_id, synonym_type, fresh_id,
577 insert_before);
578 ASSERT_TRUE(
579 transformation.IsApplicable(context.get(), transformation_context));
580 ApplyAndCheckFreshIds(transformation, context.get(),
581 &transformation_context);
582 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
583 MakeDataDescriptor(result_id, {}), MakeDataDescriptor(fresh_id, {})));
584 ++fresh_id;
585 }
586
587 std::string expected_shader = R"(
588 OpCapability Shader
589 %1 = OpExtInstImport "GLSL.std.450"
590 OpMemoryModel Logical GLSL450
591 OpEntryPoint Fragment %4 "main"
592 OpExecutionMode %4 OriginUpperLeft
593 OpSource ESSL 310
594 OpDecorate %8 RelaxedPrecision
595 %2 = OpTypeVoid
596 %3 = OpTypeFunction %2
597 %6 = OpTypeInt 32 1
598 %7 = OpTypePointer Function %6
599 %9 = OpConstant %6 4
600 %10 = OpTypeFloat 32
601 %11 = OpTypePointer Function %10
602 %13 = OpConstant %10 4
603 %14 = OpTypeVector %10 2
604 %15 = OpTypePointer Function %14
605 %17 = OpConstant %10 3.4000001
606 %18 = OpConstantComposite %14 %17 %17
607 %19 = OpTypeBool
608 %20 = OpTypeStruct %19
609 %21 = OpTypePointer Function %20
610 %23 = OpConstantTrue %19
611 %24 = OpConstantComposite %20 %23
612 %4 = OpFunction %2 None %3
613 %5 = OpLabel
614 %8 = OpVariable %7 Function
615 %12 = OpVariable %11 Function
616 %16 = OpVariable %15 Function
617 %22 = OpVariable %21 Function
618 OpStore %8 %9
619 OpStore %12 %13
620 OpStore %16 %18
621 OpStore %22 %24
622 %50 = OpCopyObject %6 %9
623 %51 = OpCopyObject %10 %13
624 %52 = OpCopyObject %10 %17
625 %53 = OpCopyObject %14 %18
626 %54 = OpCopyObject %19 %23
627 %55 = OpCopyObject %20 %24
628 %56 = OpCopyObject %21 %22
629 OpReturn
630 OpFunctionEnd
631 )";
632
633 ASSERT_TRUE(IsEqual(env, expected_shader, context.get()));
634 }
635
636 TEST(TransformationAddSynonymTest, CopyBooleanConstants) {
637 std::string shader = R"(
638 OpCapability Shader
639 %1 = OpExtInstImport "GLSL.std.450"
640 OpMemoryModel Logical GLSL450
641 OpEntryPoint Fragment %4 "main"
642 OpExecutionMode %4 OriginUpperLeft
643 OpSource ESSL 310
644 OpName %4 "main"
645 %2 = OpTypeVoid
646 %6 = OpTypeBool
647 %7 = OpConstantTrue %6
648 %8 = OpConstantFalse %6
649 %3 = OpTypeFunction %2
650 %4 = OpFunction %2 None %3
651 %5 = OpLabel
652 OpReturn
653 OpFunctionEnd
654 )";
655
656 const auto env = SPV_ENV_UNIVERSAL_1_3;
657 const auto consumer = nullptr;
658 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
659 spvtools::ValidatorOptions validator_options;
660 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
661 kConsoleMessageConsumer));
662 TransformationContext transformation_context(
663 MakeUnique<FactManager>(context.get()), validator_options);
664 ASSERT_EQ(0, transformation_context.GetFactManager()
665 ->GetIdsForWhichSynonymsAreKnown()
666 .size());
667
668 {
669 TransformationAddSynonym copy_true(
670 7, protobufs::TransformationAddSynonym::COPY_OBJECT, 100,
671 MakeInstructionDescriptor(5, spv::Op::OpReturn, 0));
672 ASSERT_TRUE(copy_true.IsApplicable(context.get(), transformation_context));
673 ApplyAndCheckFreshIds(copy_true, context.get(), &transformation_context);
674
675 std::vector<uint32_t> ids_for_which_synonyms_are_known =
676 transformation_context.GetFactManager()
677 ->GetIdsForWhichSynonymsAreKnown();
678 ASSERT_EQ(2, ids_for_which_synonyms_are_known.size());
679 ASSERT_TRUE(std::find(ids_for_which_synonyms_are_known.begin(),
680 ids_for_which_synonyms_are_known.end(),
681 7) != ids_for_which_synonyms_are_known.end());
682 ASSERT_EQ(
683 2, transformation_context.GetFactManager()->GetSynonymsForId(7).size());
684 protobufs::DataDescriptor descriptor_100 = MakeDataDescriptor(100, {});
685 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
686 MakeDataDescriptor(7, {}), descriptor_100));
687 }
688
689 {
690 TransformationAddSynonym copy_false(
691 8, protobufs::TransformationAddSynonym::COPY_OBJECT, 101,
692 MakeInstructionDescriptor(100, spv::Op::OpReturn, 0));
693 ASSERT_TRUE(copy_false.IsApplicable(context.get(), transformation_context));
694 ApplyAndCheckFreshIds(copy_false, context.get(), &transformation_context);
695 std::vector<uint32_t> ids_for_which_synonyms_are_known =
696 transformation_context.GetFactManager()
697 ->GetIdsForWhichSynonymsAreKnown();
698 ASSERT_EQ(4, ids_for_which_synonyms_are_known.size());
699 ASSERT_TRUE(std::find(ids_for_which_synonyms_are_known.begin(),
700 ids_for_which_synonyms_are_known.end(),
701 8) != ids_for_which_synonyms_are_known.end());
702 ASSERT_EQ(
703 2, transformation_context.GetFactManager()->GetSynonymsForId(8).size());
704 protobufs::DataDescriptor descriptor_101 = MakeDataDescriptor(101, {});
705 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
706 MakeDataDescriptor(8, {}), descriptor_101));
707 }
708
709 {
710 TransformationAddSynonym copy_false_again(
711 101, protobufs::TransformationAddSynonym::COPY_OBJECT, 102,
712 MakeInstructionDescriptor(5, spv::Op::OpReturn, 0));
713 ASSERT_TRUE(
714 copy_false_again.IsApplicable(context.get(), transformation_context));
715 ApplyAndCheckFreshIds(copy_false_again, context.get(),
716 &transformation_context);
717 std::vector<uint32_t> ids_for_which_synonyms_are_known =
718 transformation_context.GetFactManager()
719 ->GetIdsForWhichSynonymsAreKnown();
720 ASSERT_EQ(5, ids_for_which_synonyms_are_known.size());
721 ASSERT_TRUE(std::find(ids_for_which_synonyms_are_known.begin(),
722 ids_for_which_synonyms_are_known.end(),
723 101) != ids_for_which_synonyms_are_known.end());
724 ASSERT_EQ(
725 3,
726 transformation_context.GetFactManager()->GetSynonymsForId(101).size());
727 protobufs::DataDescriptor descriptor_102 = MakeDataDescriptor(102, {});
728 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
729 MakeDataDescriptor(101, {}), descriptor_102));
730 }
731
732 {
733 TransformationAddSynonym copy_true_again(
734 7, protobufs::TransformationAddSynonym::COPY_OBJECT, 103,
735 MakeInstructionDescriptor(102, spv::Op::OpReturn, 0));
736 ASSERT_TRUE(
737 copy_true_again.IsApplicable(context.get(), transformation_context));
738 ApplyAndCheckFreshIds(copy_true_again, context.get(),
739 &transformation_context);
740 std::vector<uint32_t> ids_for_which_synonyms_are_known =
741 transformation_context.GetFactManager()
742 ->GetIdsForWhichSynonymsAreKnown();
743 ASSERT_EQ(6, ids_for_which_synonyms_are_known.size());
744 ASSERT_TRUE(std::find(ids_for_which_synonyms_are_known.begin(),
745 ids_for_which_synonyms_are_known.end(),
746 7) != ids_for_which_synonyms_are_known.end());
747 ASSERT_EQ(
748 3, transformation_context.GetFactManager()->GetSynonymsForId(7).size());
749 protobufs::DataDescriptor descriptor_103 = MakeDataDescriptor(103, {});
750 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
751 MakeDataDescriptor(7, {}), descriptor_103));
752 }
753
754 std::string after_transformation = R"(
755 OpCapability Shader
756 %1 = OpExtInstImport "GLSL.std.450"
757 OpMemoryModel Logical GLSL450
758 OpEntryPoint Fragment %4 "main"
759 OpExecutionMode %4 OriginUpperLeft
760 OpSource ESSL 310
761 OpName %4 "main"
762 %2 = OpTypeVoid
763 %6 = OpTypeBool
764 %7 = OpConstantTrue %6
765 %8 = OpConstantFalse %6
766 %3 = OpTypeFunction %2
767 %4 = OpFunction %2 None %3
768 %5 = OpLabel
769 %100 = OpCopyObject %6 %7
770 %101 = OpCopyObject %6 %8
771 %102 = OpCopyObject %6 %101
772 %103 = OpCopyObject %6 %7
773 OpReturn
774 OpFunctionEnd
775 )";
776
777 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
778 }
779
780 TEST(TransformationAddSynonymTest, CheckIllegalCases) {
781 // The following SPIR-V comes from this GLSL, pushed through spirv-opt
782 // and then doctored a bit.
783 //
784 // #version 310 es
785 //
786 // precision highp float;
787 //
788 // struct S {
789 // int a;
790 // float b;
791 // };
792 //
793 // layout(set = 0, binding = 2) uniform block {
794 // S s;
795 // lowp float f;
796 // int ii;
797 // } ubuf;
798 //
799 // layout(location = 0) out vec4 color;
800 //
801 // void main() {
802 // float c = 0.0;
803 // lowp float d = 0.0;
804 // S localS = ubuf.s;
805 // for (int i = 0; i < ubuf.s.a; i++) {
806 // switch (ubuf.ii) {
807 // case 0:
808 // c += 0.1;
809 // d += 0.2;
810 // case 1:
811 // c += 0.1;
812 // if (c > d) {
813 // d += 0.2;
814 // } else {
815 // d += c;
816 // }
817 // break;
818 // default:
819 // i += 1;
820 // localS.b += d;
821 // }
822 // }
823 // color = vec4(c, d, localS.b, 1.0);
824 // }
825
826 std::string shader = R"(
827 OpCapability Shader
828 %1 = OpExtInstImport "GLSL.std.450"
829 OpMemoryModel Logical GLSL450
830 OpEntryPoint Fragment %4 "main" %80
831 OpExecutionMode %4 OriginUpperLeft
832 OpSource ESSL 310
833 OpName %4 "main"
834 OpName %12 "S"
835 OpMemberName %12 0 "a"
836 OpMemberName %12 1 "b"
837 OpName %15 "S"
838 OpMemberName %15 0 "a"
839 OpMemberName %15 1 "b"
840 OpName %16 "block"
841 OpMemberName %16 0 "s"
842 OpMemberName %16 1 "f"
843 OpMemberName %16 2 "ii"
844 OpName %18 "ubuf"
845 OpName %80 "color"
846 OpMemberDecorate %12 0 RelaxedPrecision
847 OpMemberDecorate %15 0 RelaxedPrecision
848 OpMemberDecorate %15 0 Offset 0
849 OpMemberDecorate %15 1 Offset 4
850 OpMemberDecorate %16 0 Offset 0
851 OpMemberDecorate %16 1 RelaxedPrecision
852 OpMemberDecorate %16 1 Offset 16
853 OpMemberDecorate %16 2 RelaxedPrecision
854 OpMemberDecorate %16 2 Offset 20
855 OpDecorate %16 Block
856 OpDecorate %18 DescriptorSet 0
857 OpDecorate %18 Binding 2
858 OpDecorate %38 RelaxedPrecision
859 OpDecorate %43 RelaxedPrecision
860 OpDecorate %53 RelaxedPrecision
861 OpDecorate %62 RelaxedPrecision
862 OpDecorate %69 RelaxedPrecision
863 OpDecorate %77 RelaxedPrecision
864 OpDecorate %80 Location 0
865 OpDecorate %101 RelaxedPrecision
866 OpDecorate %102 RelaxedPrecision
867 OpDecorate %96 RelaxedPrecision
868 OpDecorate %108 RelaxedPrecision
869 OpDecorate %107 RelaxedPrecision
870 OpDecorate %98 RelaxedPrecision
871 %2 = OpTypeVoid
872 %3 = OpTypeFunction %2
873 %6 = OpTypeFloat 32
874 %9 = OpConstant %6 0
875 %11 = OpTypeInt 32 1
876 %12 = OpTypeStruct %11 %6
877 %15 = OpTypeStruct %11 %6
878 %16 = OpTypeStruct %15 %6 %11
879 %17 = OpTypePointer Uniform %16
880 %18 = OpVariable %17 Uniform
881 %19 = OpConstant %11 0
882 %20 = OpTypePointer Uniform %15
883 %27 = OpConstant %11 1
884 %36 = OpTypePointer Uniform %11
885 %39 = OpTypeBool
886 %41 = OpConstant %11 2
887 %48 = OpConstant %6 0.100000001
888 %51 = OpConstant %6 0.200000003
889 %78 = OpTypeVector %6 4
890 %79 = OpTypePointer Output %78
891 %80 = OpVariable %79 Output
892 %85 = OpConstant %6 1
893 %95 = OpUndef %12
894 %112 = OpTypePointer Uniform %6
895 %113 = OpTypeInt 32 0
896 %114 = OpConstant %113 1
897 %179 = OpTypePointer Function %39
898 %4 = OpFunction %2 None %3
899 %5 = OpLabel
900 %180 = OpVariable %179 Function
901 %181 = OpVariable %179 Function
902 %182 = OpVariable %179 Function
903 %21 = OpAccessChain %20 %18 %19
904 %115 = OpAccessChain %112 %21 %114
905 %116 = OpLoad %6 %115
906 %90 = OpCompositeInsert %12 %116 %95 1
907 OpBranch %30
908 %30 = OpLabel
909 %99 = OpPhi %12 %90 %5 %109 %47
910 %98 = OpPhi %6 %9 %5 %107 %47
911 %97 = OpPhi %6 %9 %5 %105 %47
912 %96 = OpPhi %11 %19 %5 %77 %47
913 %37 = OpAccessChain %36 %18 %19 %19
914 %38 = OpLoad %11 %37
915 %40 = OpSLessThan %39 %96 %38
916 OpLoopMerge %32 %47 None
917 OpBranchConditional %40 %31 %32
918 %31 = OpLabel
919 %42 = OpAccessChain %36 %18 %41
920 %43 = OpLoad %11 %42
921 OpSelectionMerge %45 None
922 OpSwitch %43 %46 0 %44 1 %45
923 %46 = OpLabel
924 %69 = OpIAdd %11 %96 %27
925 %72 = OpCompositeExtract %6 %99 1
926 %73 = OpFAdd %6 %72 %98
927 %93 = OpCompositeInsert %12 %73 %99 1
928 OpBranch %47
929 %44 = OpLabel
930 %50 = OpFAdd %6 %97 %48
931 %53 = OpFAdd %6 %98 %51
932 OpBranch %45
933 %45 = OpLabel
934 %101 = OpPhi %6 %98 %31 %53 %44
935 %100 = OpPhi %6 %97 %31 %50 %44
936 %55 = OpFAdd %6 %100 %48
937 %58 = OpFOrdGreaterThan %39 %55 %101
938 OpSelectionMerge %60 None
939 OpBranchConditional %58 %59 %63
940 %59 = OpLabel
941 %62 = OpFAdd %6 %101 %51
942 OpBranch %60
943 %63 = OpLabel
944 %66 = OpFAdd %6 %101 %55
945 OpBranch %60
946 %60 = OpLabel
947 %108 = OpPhi %6 %62 %59 %66 %63
948 OpBranch %47
949 %47 = OpLabel
950 %109 = OpPhi %12 %93 %46 %99 %60
951 %107 = OpPhi %6 %98 %46 %108 %60
952 %105 = OpPhi %6 %97 %46 %55 %60
953 %102 = OpPhi %11 %69 %46 %96 %60
954 %77 = OpIAdd %11 %102 %27
955 OpBranch %30
956 %32 = OpLabel
957 %84 = OpCompositeExtract %6 %99 1
958 %86 = OpCompositeConstruct %78 %97 %98 %84 %85
959 OpStore %80 %86
960 OpReturn
961 OpFunctionEnd
962 )";
963
964 const auto env = SPV_ENV_UNIVERSAL_1_3;
965 const auto consumer = nullptr;
966 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
967 spvtools::ValidatorOptions validator_options;
968 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
969 kConsoleMessageConsumer));
970 TransformationContext transformation_context(
971 MakeUnique<FactManager>(context.get()), validator_options);
972 // Inapplicable because %18 is decorated.
973 ASSERT_FALSE(TransformationAddSynonym(
974 18, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
975 MakeInstructionDescriptor(21, spv::Op::OpAccessChain, 0))
976 .IsApplicable(context.get(), transformation_context));
977
978 // Inapplicable because %77 is decorated.
979 ASSERT_FALSE(TransformationAddSynonym(
980 77, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
981 MakeInstructionDescriptor(77, spv::Op::OpBranch, 0))
982 .IsApplicable(context.get(), transformation_context));
983
984 // Inapplicable because %80 is decorated.
985 ASSERT_FALSE(TransformationAddSynonym(
986 80, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
987 MakeInstructionDescriptor(77, spv::Op::OpIAdd, 0))
988 .IsApplicable(context.get(), transformation_context));
989
990 // Inapplicable because %84 is not available at the requested point
991 ASSERT_FALSE(
992 TransformationAddSynonym(
993 84, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
994 MakeInstructionDescriptor(32, spv::Op::OpCompositeExtract, 0))
995 .IsApplicable(context.get(), transformation_context));
996
997 // Fine because %84 is available at the requested point
998 ASSERT_TRUE(
999 TransformationAddSynonym(
1000 84, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
1001 MakeInstructionDescriptor(32, spv::Op::OpCompositeConstruct, 0))
1002 .IsApplicable(context.get(), transformation_context));
1003
1004 // Inapplicable because id %9 is already in use
1005 ASSERT_FALSE(
1006 TransformationAddSynonym(
1007 84, protobufs::TransformationAddSynonym::COPY_OBJECT, 9,
1008 MakeInstructionDescriptor(32, spv::Op::OpCompositeConstruct, 0))
1009 .IsApplicable(context.get(), transformation_context));
1010
1011 // Inapplicable because the requested point does not exist
1012 ASSERT_FALSE(TransformationAddSynonym(
1013 84, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
1014 MakeInstructionDescriptor(86, spv::Op::OpReturn, 2))
1015 .IsApplicable(context.get(), transformation_context));
1016
1017 // Inapplicable because %9 is not in a function
1018 ASSERT_FALSE(TransformationAddSynonym(
1019 9, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
1020 MakeInstructionDescriptor(9, spv::Op::OpTypeInt, 0))
1021 .IsApplicable(context.get(), transformation_context));
1022
1023 // Inapplicable because the insert point is right before, or inside, a chunk
1024 // of OpPhis
1025 ASSERT_FALSE(TransformationAddSynonym(
1026 9, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
1027 MakeInstructionDescriptor(30, spv::Op::OpPhi, 0))
1028 .IsApplicable(context.get(), transformation_context));
1029 ASSERT_FALSE(TransformationAddSynonym(
1030 9, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
1031 MakeInstructionDescriptor(99, spv::Op::OpPhi, 1))
1032 .IsApplicable(context.get(), transformation_context));
1033
1034 // OK, because the insert point is just after a chunk of OpPhis.
1035 ASSERT_TRUE(TransformationAddSynonym(
1036 9, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
1037 MakeInstructionDescriptor(96, spv::Op::OpAccessChain, 0))
1038 .IsApplicable(context.get(), transformation_context));
1039
1040 // Inapplicable because the insert point is right after an OpSelectionMerge
1041 ASSERT_FALSE(
1042 TransformationAddSynonym(
1043 9, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
1044 MakeInstructionDescriptor(58, spv::Op::OpBranchConditional, 0))
1045 .IsApplicable(context.get(), transformation_context));
1046
1047 // OK, because the insert point is right before the OpSelectionMerge
1048 ASSERT_TRUE(TransformationAddSynonym(
1049 9, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
1050 MakeInstructionDescriptor(58, spv::Op::OpSelectionMerge, 0))
1051 .IsApplicable(context.get(), transformation_context));
1052
1053 // Inapplicable because the insert point is right after an OpSelectionMerge
1054 ASSERT_FALSE(TransformationAddSynonym(
1055 9, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
1056 MakeInstructionDescriptor(43, spv::Op::OpSwitch, 0))
1057 .IsApplicable(context.get(), transformation_context));
1058
1059 // OK, because the insert point is right before the OpSelectionMerge
1060 ASSERT_TRUE(TransformationAddSynonym(
1061 9, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
1062 MakeInstructionDescriptor(43, spv::Op::OpSelectionMerge, 0))
1063 .IsApplicable(context.get(), transformation_context));
1064
1065 // Inapplicable because the insert point is right after an OpLoopMerge
1066 ASSERT_FALSE(
1067 TransformationAddSynonym(
1068 9, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
1069 MakeInstructionDescriptor(40, spv::Op::OpBranchConditional, 0))
1070 .IsApplicable(context.get(), transformation_context));
1071
1072 // OK, because the insert point is right before the OpLoopMerge
1073 ASSERT_TRUE(TransformationAddSynonym(
1074 9, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
1075 MakeInstructionDescriptor(40, spv::Op::OpLoopMerge, 0))
1076 .IsApplicable(context.get(), transformation_context));
1077
1078 // Inapplicable because id %300 does not exist
1079 ASSERT_FALSE(TransformationAddSynonym(
1080 300, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
1081 MakeInstructionDescriptor(40, spv::Op::OpLoopMerge, 0))
1082 .IsApplicable(context.get(), transformation_context));
1083
1084 // Inapplicable because the following instruction is OpVariable
1085 ASSERT_FALSE(TransformationAddSynonym(
1086 9, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
1087 MakeInstructionDescriptor(180, spv::Op::OpVariable, 0))
1088 .IsApplicable(context.get(), transformation_context));
1089 ASSERT_FALSE(TransformationAddSynonym(
1090 9, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
1091 MakeInstructionDescriptor(181, spv::Op::OpVariable, 0))
1092 .IsApplicable(context.get(), transformation_context));
1093 ASSERT_FALSE(TransformationAddSynonym(
1094 9, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
1095 MakeInstructionDescriptor(182, spv::Op::OpVariable, 0))
1096 .IsApplicable(context.get(), transformation_context));
1097
1098 // OK, because this is just past the group of OpVariable instructions.
1099 ASSERT_TRUE(TransformationAddSynonym(
1100 9, protobufs::TransformationAddSynonym::COPY_OBJECT, 200,
1101 MakeInstructionDescriptor(182, spv::Op::OpAccessChain, 0))
1102 .IsApplicable(context.get(), transformation_context));
1103 }
1104
1105 TEST(TransformationAddSynonymTest, MiscellaneousCopies) {
1106 // The following SPIR-V comes from this GLSL:
1107 //
1108 // #version 310 es
1109 //
1110 // precision highp float;
1111 //
1112 // float g;
1113 //
1114 // vec4 h;
1115 //
1116 // void main() {
1117 // int a;
1118 // int b;
1119 // b = int(g);
1120 // h.x = float(a);
1121 // }
1122
1123 std::string shader = R"(
1124 OpCapability Shader
1125 %1 = OpExtInstImport "GLSL.std.450"
1126 OpMemoryModel Logical GLSL450
1127 OpEntryPoint Fragment %4 "main"
1128 OpExecutionMode %4 OriginUpperLeft
1129 OpSource ESSL 310
1130 OpName %4 "main"
1131 OpName %8 "b"
1132 OpName %11 "g"
1133 OpName %16 "h"
1134 OpName %17 "a"
1135 %2 = OpTypeVoid
1136 %3 = OpTypeFunction %2
1137 %6 = OpTypeInt 32 1
1138 %7 = OpTypePointer Function %6
1139 %9 = OpTypeFloat 32
1140 %10 = OpTypePointer Private %9
1141 %11 = OpVariable %10 Private
1142 %14 = OpTypeVector %9 4
1143 %15 = OpTypePointer Private %14
1144 %16 = OpVariable %15 Private
1145 %20 = OpTypeInt 32 0
1146 %21 = OpConstant %20 0
1147 %4 = OpFunction %2 None %3
1148 %5 = OpLabel
1149 %8 = OpVariable %7 Function
1150 %17 = OpVariable %7 Function
1151 %12 = OpLoad %9 %11
1152 %13 = OpConvertFToS %6 %12
1153 OpStore %8 %13
1154 %18 = OpLoad %6 %17
1155 %19 = OpConvertSToF %9 %18
1156 %22 = OpAccessChain %10 %16 %21
1157 OpStore %22 %19
1158 OpReturn
1159 OpFunctionEnd
1160 )";
1161
1162 const auto env = SPV_ENV_UNIVERSAL_1_3;
1163 const auto consumer = nullptr;
1164 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1165 spvtools::ValidatorOptions validator_options;
1166 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1167 kConsoleMessageConsumer));
1168 TransformationContext transformation_context(
1169 MakeUnique<FactManager>(context.get()), validator_options);
1170 std::vector<TransformationAddSynonym> transformations = {
1171 TransformationAddSynonym(
1172 19, protobufs::TransformationAddSynonym::COPY_OBJECT, 100,
1173 MakeInstructionDescriptor(22, spv::Op::OpStore, 0)),
1174 TransformationAddSynonym(
1175 22, protobufs::TransformationAddSynonym::COPY_OBJECT, 101,
1176 MakeInstructionDescriptor(22, spv::Op::OpCopyObject, 0)),
1177 TransformationAddSynonym(
1178 12, protobufs::TransformationAddSynonym::COPY_OBJECT, 102,
1179 MakeInstructionDescriptor(22, spv::Op::OpCopyObject, 0)),
1180 TransformationAddSynonym(
1181 11, protobufs::TransformationAddSynonym::COPY_OBJECT, 103,
1182 MakeInstructionDescriptor(22, spv::Op::OpCopyObject, 0)),
1183 TransformationAddSynonym(
1184 16, protobufs::TransformationAddSynonym::COPY_OBJECT, 104,
1185 MakeInstructionDescriptor(22, spv::Op::OpCopyObject, 0)),
1186 TransformationAddSynonym(
1187 8, protobufs::TransformationAddSynonym::COPY_OBJECT, 105,
1188 MakeInstructionDescriptor(22, spv::Op::OpCopyObject, 0)),
1189 TransformationAddSynonym(
1190 17, protobufs::TransformationAddSynonym::COPY_OBJECT, 106,
1191 MakeInstructionDescriptor(22, spv::Op::OpCopyObject, 0))};
1192
1193 for (auto& transformation : transformations) {
1194 ASSERT_TRUE(
1195 transformation.IsApplicable(context.get(), transformation_context));
1196 ApplyAndCheckFreshIds(transformation, context.get(),
1197 &transformation_context);
1198 }
1199
1200 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1201 kConsoleMessageConsumer));
1202
1203 std::string after_transformation = R"(
1204 OpCapability Shader
1205 %1 = OpExtInstImport "GLSL.std.450"
1206 OpMemoryModel Logical GLSL450
1207 OpEntryPoint Fragment %4 "main"
1208 OpExecutionMode %4 OriginUpperLeft
1209 OpSource ESSL 310
1210 OpName %4 "main"
1211 OpName %8 "b"
1212 OpName %11 "g"
1213 OpName %16 "h"
1214 OpName %17 "a"
1215 %2 = OpTypeVoid
1216 %3 = OpTypeFunction %2
1217 %6 = OpTypeInt 32 1
1218 %7 = OpTypePointer Function %6
1219 %9 = OpTypeFloat 32
1220 %10 = OpTypePointer Private %9
1221 %11 = OpVariable %10 Private
1222 %14 = OpTypeVector %9 4
1223 %15 = OpTypePointer Private %14
1224 %16 = OpVariable %15 Private
1225 %20 = OpTypeInt 32 0
1226 %21 = OpConstant %20 0
1227 %4 = OpFunction %2 None %3
1228 %5 = OpLabel
1229 %8 = OpVariable %7 Function
1230 %17 = OpVariable %7 Function
1231 %12 = OpLoad %9 %11
1232 %13 = OpConvertFToS %6 %12
1233 OpStore %8 %13
1234 %18 = OpLoad %6 %17
1235 %19 = OpConvertSToF %9 %18
1236 %22 = OpAccessChain %10 %16 %21
1237 %106 = OpCopyObject %7 %17
1238 %105 = OpCopyObject %7 %8
1239 %104 = OpCopyObject %15 %16
1240 %103 = OpCopyObject %10 %11
1241 %102 = OpCopyObject %9 %12
1242 %101 = OpCopyObject %10 %22
1243 %100 = OpCopyObject %9 %19
1244 OpStore %22 %19
1245 OpReturn
1246 OpFunctionEnd
1247 )";
1248
1249 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
1250 }
1251
1252 TEST(TransformationAddSynonymTest, DoNotCopyNullPointers) {
1253 std::string shader = R"(
1254 OpCapability Shader
1255 OpCapability VariablePointers
1256 %1 = OpExtInstImport "GLSL.std.450"
1257 OpMemoryModel Logical GLSL450
1258 OpEntryPoint Fragment %4 "main"
1259 OpExecutionMode %4 OriginUpperLeft
1260 OpSource ESSL 310
1261 %2 = OpTypeVoid
1262 %3 = OpTypeFunction %2
1263 %6 = OpTypeInt 32 1
1264 %7 = OpTypePointer Function %6
1265 %8 = OpConstantNull %7
1266 %4 = OpFunction %2 None %3
1267 %5 = OpLabel
1268 OpReturn
1269 OpFunctionEnd
1270 )";
1271
1272 const auto env = SPV_ENV_UNIVERSAL_1_3;
1273 const auto consumer = nullptr;
1274 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1275 spvtools::ValidatorOptions validator_options;
1276 validator_options.SetRelaxLogicalPointer(true);
1277 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1278 kConsoleMessageConsumer));
1279 TransformationContext transformation_context(
1280 MakeUnique<FactManager>(context.get()), validator_options);
1281 // Illegal to copy null.
1282 ASSERT_FALSE(TransformationAddSynonym(
1283 8, protobufs::TransformationAddSynonym::COPY_OBJECT, 100,
1284 MakeInstructionDescriptor(5, spv::Op::OpReturn, 0))
1285 .IsApplicable(context.get(), transformation_context));
1286 }
1287
1288 TEST(TransformationAddSynonymTest, PropagateIrrelevantPointeeFact) {
1289 // Checks that if a pointer is known to have an irrelevant value, the same
1290 // holds after the pointer is copied.
1291
1292 std::string shader = R"(
1293 OpCapability Shader
1294 %1 = OpExtInstImport "GLSL.std.450"
1295 OpMemoryModel Logical GLSL450
1296 OpEntryPoint Fragment %4 "main"
1297 OpExecutionMode %4 OriginUpperLeft
1298 OpSource ESSL 310
1299 %2 = OpTypeVoid
1300 %3 = OpTypeFunction %2
1301 %6 = OpTypeInt 32 1
1302 %7 = OpTypePointer Function %6
1303 %4 = OpFunction %2 None %3
1304 %5 = OpLabel
1305 %8 = OpVariable %7 Function
1306 %9 = OpVariable %7 Function
1307 OpReturn
1308 OpFunctionEnd
1309 )";
1310
1311 const auto env = SPV_ENV_UNIVERSAL_1_3;
1312 const auto consumer = nullptr;
1313 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1314 spvtools::ValidatorOptions validator_options;
1315 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1316 kConsoleMessageConsumer));
1317 TransformationContext transformation_context(
1318 MakeUnique<FactManager>(context.get()), validator_options);
1319 transformation_context.GetFactManager()->AddFactValueOfPointeeIsIrrelevant(8);
1320
1321 TransformationAddSynonym transformation1(
1322 8, protobufs::TransformationAddSynonym::COPY_OBJECT, 100,
1323 MakeInstructionDescriptor(9, spv::Op::OpReturn, 0));
1324 TransformationAddSynonym transformation2(
1325 9, protobufs::TransformationAddSynonym::COPY_OBJECT, 101,
1326 MakeInstructionDescriptor(9, spv::Op::OpReturn, 0));
1327 TransformationAddSynonym transformation3(
1328 100, protobufs::TransformationAddSynonym::COPY_OBJECT, 102,
1329 MakeInstructionDescriptor(9, spv::Op::OpReturn, 0));
1330
1331 ASSERT_TRUE(
1332 transformation1.IsApplicable(context.get(), transformation_context));
1333 ApplyAndCheckFreshIds(transformation1, context.get(),
1334 &transformation_context);
1335 ASSERT_TRUE(
1336 transformation2.IsApplicable(context.get(), transformation_context));
1337 ApplyAndCheckFreshIds(transformation2, context.get(),
1338 &transformation_context);
1339 ASSERT_TRUE(
1340 transformation3.IsApplicable(context.get(), transformation_context));
1341 ApplyAndCheckFreshIds(transformation3, context.get(),
1342 &transformation_context);
1343
1344 ASSERT_TRUE(
1345 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(8));
1346 ASSERT_TRUE(
1347 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(100));
1348 ASSERT_TRUE(
1349 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(102));
1350 ASSERT_FALSE(
1351 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(9));
1352 ASSERT_FALSE(
1353 transformation_context.GetFactManager()->PointeeValueIsIrrelevant(101));
1354 }
1355
1356 TEST(TransformationAddSynonymTest, DoNotCopyOpSampledImage) {
1357 // This checks that we do not try to copy the result id of an OpSampledImage
1358 // instruction.
1359 std::string shader = R"(
1360 OpCapability Shader
1361 OpCapability SampledBuffer
1362 OpCapability ImageBuffer
1363 %1 = OpExtInstImport "GLSL.std.450"
1364 OpMemoryModel Logical GLSL450
1365 OpEntryPoint Fragment %2 "main" %40 %41
1366 OpExecutionMode %2 OriginUpperLeft
1367 OpSource GLSL 450
1368 OpDecorate %40 DescriptorSet 0
1369 OpDecorate %40 Binding 69
1370 OpDecorate %41 DescriptorSet 0
1371 OpDecorate %41 Binding 1
1372 %54 = OpTypeFloat 32
1373 %76 = OpTypeVector %54 4
1374 %55 = OpConstant %54 0
1375 %56 = OpTypeVector %54 3
1376 %94 = OpTypeVector %54 2
1377 %112 = OpConstantComposite %94 %55 %55
1378 %57 = OpConstantComposite %56 %55 %55 %55
1379 %15 = OpTypeImage %54 2D 2 0 0 1 Unknown
1380 %114 = OpTypePointer UniformConstant %15
1381 %38 = OpTypeSampler
1382 %125 = OpTypePointer UniformConstant %38
1383 %132 = OpTypeVoid
1384 %133 = OpTypeFunction %132
1385 %45 = OpTypeSampledImage %15
1386 %40 = OpVariable %114 UniformConstant
1387 %41 = OpVariable %125 UniformConstant
1388 %2 = OpFunction %132 None %133
1389 %164 = OpLabel
1390 %184 = OpLoad %15 %40
1391 %213 = OpLoad %38 %41
1392 %216 = OpSampledImage %45 %184 %213
1393 %217 = OpImageSampleImplicitLod %76 %216 %112 Bias %55
1394 OpReturn
1395 OpFunctionEnd
1396 )";
1397
1398 const auto env = SPV_ENV_UNIVERSAL_1_3;
1399 const auto consumer = nullptr;
1400 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1401
1402 spvtools::ValidatorOptions validator_options;
1403 TransformationContext transformation_context(
1404 MakeUnique<FactManager>(context.get()), validator_options);
1405 ASSERT_FALSE(
1406 TransformationAddSynonym(
1407 216, protobufs::TransformationAddSynonym::COPY_OBJECT, 500,
1408 MakeInstructionDescriptor(217, spv::Op::OpImageSampleImplicitLod, 0))
1409 .IsApplicable(context.get(), transformation_context));
1410 }
1411
1412 TEST(TransformationAddSynonymTest, DoNotCopyVoidRunctionResult) {
1413 // This checks that we do not try to copy the result of a void function.
1414 std::string shader = R"(
1415 OpCapability Shader
1416 %1 = OpExtInstImport "GLSL.std.450"
1417 OpMemoryModel Logical GLSL450
1418 OpEntryPoint Fragment %4 "main"
1419 OpExecutionMode %4 OriginUpperLeft
1420 OpSource ESSL 320
1421 OpName %4 "main"
1422 OpName %6 "foo("
1423 %2 = OpTypeVoid
1424 %3 = OpTypeFunction %2
1425 %4 = OpFunction %2 None %3
1426 %5 = OpLabel
1427 %8 = OpFunctionCall %2 %6
1428 OpReturn
1429 OpFunctionEnd
1430 %6 = OpFunction %2 None %3
1431 %7 = OpLabel
1432 OpReturn
1433 OpFunctionEnd
1434 )";
1435
1436 const auto env = SPV_ENV_UNIVERSAL_1_3;
1437 const auto consumer = nullptr;
1438 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1439
1440 spvtools::ValidatorOptions validator_options;
1441 TransformationContext transformation_context(
1442 MakeUnique<FactManager>(context.get()), validator_options);
1443 ASSERT_FALSE(TransformationAddSynonym(
1444 8, protobufs::TransformationAddSynonym::COPY_OBJECT, 500,
1445 MakeInstructionDescriptor(8, spv::Op::OpReturn, 0))
1446 .IsApplicable(context.get(), transformation_context));
1447 }
1448
1449 TEST(TransformationAddSynonymTest, HandlesDeadBlocks) {
1450 std::string shader = R"(
1451 OpCapability Shader
1452 %1 = OpExtInstImport "GLSL.std.450"
1453 OpMemoryModel Logical GLSL450
1454 OpEntryPoint Fragment %4 "main"
1455 OpExecutionMode %4 OriginUpperLeft
1456 OpSource ESSL 320
1457 %2 = OpTypeVoid
1458 %3 = OpTypeFunction %2
1459 %6 = OpTypeBool
1460 %7 = OpConstantTrue %6
1461 %11 = OpTypePointer Function %6
1462 %4 = OpFunction %2 None %3
1463 %5 = OpLabel
1464 %12 = OpVariable %11 Function
1465 OpSelectionMerge %10 None
1466 OpBranchConditional %7 %8 %9
1467 %8 = OpLabel
1468 OpBranch %10
1469 %9 = OpLabel
1470 OpBranch %10
1471 %10 = OpLabel
1472 OpReturn
1473 OpFunctionEnd
1474 )";
1475
1476 const auto env = SPV_ENV_UNIVERSAL_1_3;
1477 const auto consumer = nullptr;
1478 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1479
1480 spvtools::ValidatorOptions validator_options;
1481 TransformationContext transformation_context(
1482 MakeUnique<FactManager>(context.get()), validator_options);
1483
1484 transformation_context.GetFactManager()->AddFactBlockIsDead(9);
1485
1486 auto insert_before = MakeInstructionDescriptor(9, spv::Op::OpBranch, 0);
1487
1488 ASSERT_FALSE(TransformationAddSynonym(
1489 7, protobufs::TransformationAddSynonym::COPY_OBJECT, 100,
1490 insert_before)
1491 .IsApplicable(context.get(), transformation_context));
1492
1493 ASSERT_FALSE(TransformationAddSynonym(
1494 12, protobufs::TransformationAddSynonym::COPY_OBJECT, 100,
1495 insert_before)
1496 .IsApplicable(context.get(), transformation_context));
1497 }
1498
1499 } // namespace
1500 } // namespace fuzz
1501 } // namespace spvtools