Debian Codegraph @

test/fuzz/transformation_push_id_through_variable_test.cpp

raw
1 // Copyright (c) 2020 André Perez Maselco
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_push_id_through_variable.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(TransformationPushIdThroughVariableTest, IsApplicable) {
27 std::string reference_shader = R"(
28 OpCapability Shader
29 OpCapability VariablePointers
30 %1 = OpExtInstImport "GLSL.std.450"
31 OpMemoryModel Logical GLSL450
32 OpEntryPoint Fragment %4 "main" %92 %52 %53
33 OpExecutionMode %4 OriginUpperLeft
34 OpSource ESSL 310
35 OpDecorate %92 BuiltIn FragCoord
36 %2 = OpTypeVoid
37 %3 = OpTypeFunction %2
38 %6 = OpTypeInt 32 1
39 %7 = OpTypeFloat 32
40 %8 = OpTypeStruct %6 %7
41 %9 = OpTypePointer Function %8
42 %10 = OpTypeFunction %6 %9
43 %14 = OpConstant %6 0
44 %15 = OpTypePointer Function %6
45 %51 = OpTypePointer Private %6
46 %21 = OpConstant %6 2
47 %23 = OpConstant %6 1
48 %24 = OpConstant %7 1
49 %25 = OpTypePointer Function %7
50 %50 = OpTypePointer Private %7
51 %34 = OpTypeBool
52 %35 = OpConstantFalse %34
53 %60 = OpConstantNull %50
54 %52 = OpVariable %50 Private
55 %53 = OpVariable %51 Private
56 %80 = OpConstantComposite %8 %21 %24
57 %90 = OpTypeVector %7 4
58 %91 = OpTypePointer Input %90
59 %92 = OpVariable %91 Input
60 %93 = OpConstantComposite %90 %24 %24 %24 %24
61 %4 = OpFunction %2 None %3
62 %5 = OpLabel
63 %20 = OpVariable %9 Function
64 %27 = OpVariable %9 Function
65 %22 = OpAccessChain %15 %20 %14
66 %44 = OpCopyObject %9 %20
67 %26 = OpAccessChain %25 %20 %23
68 %29 = OpFunctionCall %6 %12 %27
69 %30 = OpAccessChain %15 %20 %14
70 %45 = OpCopyObject %15 %30
71 %81 = OpCopyObject %9 %27
72 %33 = OpAccessChain %15 %20 %14
73 OpSelectionMerge %37 None
74 OpBranchConditional %35 %36 %37
75 %36 = OpLabel
76 %38 = OpAccessChain %15 %20 %14
77 %40 = OpAccessChain %15 %20 %14
78 %43 = OpAccessChain %15 %20 %14
79 %82 = OpCopyObject %9 %27
80 OpBranch %37
81 %37 = OpLabel
82 OpReturn
83 OpFunctionEnd
84 %12 = OpFunction %6 None %10
85 %11 = OpFunctionParameter %9
86 %13 = OpLabel
87 %46 = OpCopyObject %9 %11
88 %16 = OpAccessChain %15 %11 %14
89 %95 = OpCopyObject %8 %80
90 OpReturnValue %21
91 %100 = OpLabel
92 OpUnreachable
93 OpFunctionEnd
94 )";
95
96 const auto env = SPV_ENV_UNIVERSAL_1_4;
97 const auto consumer = nullptr;
98 const auto context =
99 BuildModule(env, consumer, reference_shader, kFuzzAssembleOption);
100
101 spvtools::ValidatorOptions validator_options;
102 validator_options.SetRelaxLogicalPointer(true);
103 TransformationContext transformation_context(
104 MakeUnique<FactManager>(context.get()), validator_options);
105 // Tests the reference shader validity.
106 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
107 kConsoleMessageConsumer));
108
109 // Tests |value_synonym_id| and |variable_id| are fresh ids.
110 uint32_t value_id = 21;
111 uint32_t value_synonym_id = 62;
112 uint32_t variable_id = 63;
113 uint32_t initializer_id = 23;
114 uint32_t variable_storage_class = (uint32_t)spv::StorageClass::Private;
115 auto instruction_descriptor =
116 MakeInstructionDescriptor(95, spv::Op::OpReturnValue, 0);
117 auto transformation = TransformationPushIdThroughVariable(
118 value_id, value_synonym_id, variable_id, variable_storage_class,
119 initializer_id, instruction_descriptor);
120 ASSERT_TRUE(
121 transformation.IsApplicable(context.get(), transformation_context));
122
123 // Tests |value_synonym_id| and |variable_id| are non-fresh ids.
124 value_id = 80;
125 value_synonym_id = 60;
126 variable_id = 61;
127 initializer_id = 80;
128 variable_storage_class = (uint32_t)spv::StorageClass::Function;
129 instruction_descriptor =
130 MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0);
131 transformation = TransformationPushIdThroughVariable(
132 value_id, value_synonym_id, variable_id, variable_storage_class,
133 initializer_id, instruction_descriptor);
134 ASSERT_FALSE(
135 transformation.IsApplicable(context.get(), transformation_context));
136
137 // The instruction to insert before is not defined.
138 value_id = 80;
139 value_synonym_id = 62;
140 variable_id = 63;
141 initializer_id = 80;
142 variable_storage_class = (uint32_t)spv::StorageClass::Function;
143 instruction_descriptor =
144 MakeInstructionDescriptor(64, spv::Op::OpAccessChain, 0);
145 transformation = TransformationPushIdThroughVariable(
146 value_id, value_synonym_id, variable_id, variable_storage_class,
147 initializer_id, instruction_descriptor);
148 ASSERT_FALSE(
149 transformation.IsApplicable(context.get(), transformation_context));
150
151 // Attempting to insert the store and load instructions
152 // before an OpVariable instruction.
153 value_id = 24;
154 value_synonym_id = 62;
155 variable_id = 63;
156 initializer_id = 24;
157 variable_storage_class = (uint32_t)spv::StorageClass::Function;
158 instruction_descriptor =
159 MakeInstructionDescriptor(27, spv::Op::OpVariable, 0);
160 transformation = TransformationPushIdThroughVariable(
161 value_id, value_synonym_id, variable_id, variable_storage_class,
162 initializer_id, instruction_descriptor);
163 ASSERT_FALSE(
164 transformation.IsApplicable(context.get(), transformation_context));
165
166 // The block containing instruction descriptor must be reachable.
167 value_id = 80;
168 value_synonym_id = 62;
169 variable_id = 63;
170 initializer_id = 80;
171 variable_storage_class = (uint32_t)spv::StorageClass::Function;
172 instruction_descriptor =
173 MakeInstructionDescriptor(100, spv::Op::OpUnreachable, 0);
174 transformation = TransformationPushIdThroughVariable(
175 value_id, value_synonym_id, variable_id, variable_storage_class,
176 initializer_id, instruction_descriptor);
177 ASSERT_FALSE(
178 transformation.IsApplicable(context.get(), transformation_context));
179
180 // Tests value instruction not available.
181 value_id = 64;
182 value_synonym_id = 62;
183 variable_id = 63;
184 initializer_id = 23;
185 variable_storage_class = (uint32_t)spv::StorageClass::Function;
186 instruction_descriptor =
187 MakeInstructionDescriptor(95, spv::Op::OpReturnValue, 0);
188 transformation = TransformationPushIdThroughVariable(
189 value_id, value_synonym_id, variable_id, variable_storage_class,
190 initializer_id, instruction_descriptor);
191 ASSERT_FALSE(
192 transformation.IsApplicable(context.get(), transformation_context));
193
194 // Tests pointer type not available.
195 value_id = 80;
196 value_synonym_id = 62;
197 variable_id = 63;
198 initializer_id = 80;
199 variable_storage_class = (uint32_t)spv::StorageClass::Private;
200 instruction_descriptor =
201 MakeInstructionDescriptor(95, spv::Op::OpReturnValue, 0);
202 transformation = TransformationPushIdThroughVariable(
203 value_id, value_synonym_id, variable_id, variable_storage_class,
204 initializer_id, instruction_descriptor);
205 ASSERT_FALSE(
206 transformation.IsApplicable(context.get(), transformation_context));
207
208 // Tests not a private nor function storage class.
209 value_id = 93;
210 value_synonym_id = 62;
211 variable_id = 63;
212 initializer_id = 93;
213 variable_storage_class = (uint32_t)spv::StorageClass::Input;
214 instruction_descriptor =
215 MakeInstructionDescriptor(95, spv::Op::OpReturnValue, 0);
216 transformation = TransformationPushIdThroughVariable(
217 value_id, value_synonym_id, variable_id, variable_storage_class,
218 initializer_id, instruction_descriptor);
219 #ifndef NDEBUG
220 ASSERT_DEATH(
221 transformation.IsApplicable(context.get(), transformation_context),
222 "The variable storage class must be private or function");
223 #endif
224
225 // Tests value instruction not available before instruction.
226 value_id = 95;
227 value_synonym_id = 62;
228 variable_id = 63;
229 initializer_id = 80;
230 variable_storage_class = (uint32_t)spv::StorageClass::Function;
231 instruction_descriptor =
232 MakeInstructionDescriptor(40, spv::Op::OpAccessChain, 0);
233 transformation = TransformationPushIdThroughVariable(
234 value_id, value_synonym_id, variable_id, variable_storage_class,
235 initializer_id, instruction_descriptor);
236 ASSERT_FALSE(
237 transformation.IsApplicable(context.get(), transformation_context));
238
239 // Variable initializer is not constant.
240 value_id = 95;
241 value_synonym_id = 62;
242 variable_id = 63;
243 initializer_id = 95;
244 variable_storage_class = (uint32_t)spv::StorageClass::Function;
245 instruction_descriptor =
246 MakeInstructionDescriptor(40, spv::Op::OpAccessChain, 0);
247 transformation = TransformationPushIdThroughVariable(
248 value_id, value_synonym_id, variable_id, variable_storage_class,
249 initializer_id, instruction_descriptor);
250 ASSERT_FALSE(
251 transformation.IsApplicable(context.get(), transformation_context));
252
253 // Variable initializer has wrong type.
254 value_id = 95;
255 value_synonym_id = 62;
256 variable_id = 63;
257 initializer_id = 93;
258 variable_storage_class = (uint32_t)spv::StorageClass::Function;
259 instruction_descriptor =
260 MakeInstructionDescriptor(40, spv::Op::OpAccessChain, 0);
261 transformation = TransformationPushIdThroughVariable(
262 value_id, value_synonym_id, variable_id, variable_storage_class,
263 initializer_id, instruction_descriptor);
264 ASSERT_FALSE(
265 transformation.IsApplicable(context.get(), transformation_context));
266 }
267
268 TEST(TransformationPushIdThroughVariableTest, Apply) {
269 std::string reference_shader = R"(
270 OpCapability Shader
271 OpCapability VariablePointers
272 %1 = OpExtInstImport "GLSL.std.450"
273 OpMemoryModel Logical GLSL450
274 OpEntryPoint Fragment %4 "main" %92 %52 %53
275 OpExecutionMode %4 OriginUpperLeft
276 OpSource ESSL 310
277 OpDecorate %92 BuiltIn FragCoord
278 %2 = OpTypeVoid
279 %3 = OpTypeFunction %2
280 %6 = OpTypeInt 32 1
281 %7 = OpTypeFloat 32
282 %8 = OpTypeStruct %6 %7
283 %9 = OpTypePointer Function %8
284 %10 = OpTypeFunction %6 %9
285 %14 = OpConstant %6 0
286 %15 = OpTypePointer Function %6
287 %51 = OpTypePointer Private %6
288 %21 = OpConstant %6 2
289 %23 = OpConstant %6 1
290 %24 = OpConstant %7 1
291 %25 = OpTypePointer Function %7
292 %50 = OpTypePointer Private %7
293 %34 = OpTypeBool
294 %35 = OpConstantFalse %34
295 %60 = OpConstantNull %50
296 %52 = OpVariable %50 Private
297 %53 = OpVariable %51 Private
298 %80 = OpConstantComposite %8 %21 %24
299 %90 = OpTypeVector %7 4
300 %91 = OpTypePointer Input %90
301 %92 = OpVariable %91 Input
302 %93 = OpConstantComposite %90 %24 %24 %24 %24
303 %4 = OpFunction %2 None %3
304 %5 = OpLabel
305 %20 = OpVariable %9 Function
306 %27 = OpVariable %9 Function
307 OpStore %53 %21
308 %22 = OpAccessChain %15 %20 %14
309 %44 = OpCopyObject %9 %20
310 %26 = OpAccessChain %25 %20 %23
311 %29 = OpFunctionCall %6 %12 %27
312 %30 = OpAccessChain %15 %20 %14
313 %45 = OpCopyObject %15 %30
314 %81 = OpCopyObject %9 %27
315 %33 = OpAccessChain %15 %20 %14
316 OpSelectionMerge %37 None
317 OpBranchConditional %35 %36 %37
318 %36 = OpLabel
319 %38 = OpAccessChain %15 %20 %14
320 %40 = OpAccessChain %15 %20 %14
321 %43 = OpAccessChain %15 %20 %14
322 %82 = OpCopyObject %9 %27
323 OpBranch %37
324 %37 = OpLabel
325 OpReturn
326 OpFunctionEnd
327 %12 = OpFunction %6 None %10
328 %11 = OpFunctionParameter %9
329 %13 = OpLabel
330 %46 = OpCopyObject %9 %11
331 %16 = OpAccessChain %15 %11 %14
332 %95 = OpCopyObject %8 %80
333 OpReturnValue %21
334 OpFunctionEnd
335 )";
336
337 const auto env = SPV_ENV_UNIVERSAL_1_4;
338 const auto consumer = nullptr;
339 const auto context =
340 BuildModule(env, consumer, reference_shader, kFuzzAssembleOption);
341
342 spvtools::ValidatorOptions validator_options;
343 validator_options.SetRelaxLogicalPointer(true);
344 TransformationContext transformation_context(
345 MakeUnique<FactManager>(context.get()), validator_options);
346 uint32_t value_id = 80;
347 uint32_t value_synonym_id = 100;
348 uint32_t variable_id = 101;
349 uint32_t initializer_id = 80;
350 uint32_t variable_storage_class = (uint32_t)spv::StorageClass::Function;
351 auto instruction_descriptor =
352 MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0);
353 auto transformation = TransformationPushIdThroughVariable(
354 value_id, value_synonym_id, variable_id, variable_storage_class,
355 initializer_id, instruction_descriptor);
356 ASSERT_EQ(nullptr, context->get_def_use_mgr()->GetDef(value_synonym_id));
357 ASSERT_EQ(nullptr, context->get_instr_block(value_synonym_id));
358 ASSERT_EQ(nullptr, context->get_def_use_mgr()->GetDef(variable_id));
359 ASSERT_EQ(nullptr, context->get_instr_block(variable_id));
360 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
361 ASSERT_EQ(spv::Op::OpLoad,
362 context->get_def_use_mgr()->GetDef(value_synonym_id)->opcode());
363 ASSERT_EQ(36, context->get_instr_block(value_synonym_id)->id());
364 ASSERT_EQ(spv::Op::OpVariable,
365 context->get_def_use_mgr()->GetDef(variable_id)->opcode());
366 ASSERT_EQ(5, context->get_instr_block(variable_id)->id());
367 uint32_t variable_use_count = 0;
368 context->get_def_use_mgr()->ForEachUse(
369 variable_id,
370 [&variable_use_count](opt::Instruction* inst, uint32_t /*unused*/) {
371 ASSERT_TRUE(inst->opcode() == spv::Op::OpLoad ||
372 inst->opcode() == spv::Op::OpStore);
373 variable_use_count++;
374 });
375 ASSERT_EQ(2, variable_use_count);
376
377 value_id = 21;
378 value_synonym_id = 102;
379 variable_id = 103;
380 initializer_id = 21;
381 variable_storage_class = (uint32_t)spv::StorageClass::Function;
382 instruction_descriptor =
383 MakeInstructionDescriptor(38, spv::Op::OpAccessChain, 0);
384 transformation = TransformationPushIdThroughVariable(
385 value_id, value_synonym_id, variable_id, variable_storage_class,
386 initializer_id, instruction_descriptor);
387 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
388
389 value_id = 95;
390 value_synonym_id = 104;
391 variable_id = 105;
392 initializer_id = 80;
393 variable_storage_class = (uint32_t)spv::StorageClass::Function;
394 instruction_descriptor =
395 MakeInstructionDescriptor(95, spv::Op::OpReturnValue, 0);
396 transformation = TransformationPushIdThroughVariable(
397 value_id, value_synonym_id, variable_id, variable_storage_class,
398 initializer_id, instruction_descriptor);
399 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
400
401 value_id = 80;
402 value_synonym_id = 106;
403 variable_id = 107;
404 initializer_id = 80;
405 variable_storage_class = (uint32_t)spv::StorageClass::Function;
406 instruction_descriptor =
407 MakeInstructionDescriptor(95, spv::Op::OpReturnValue, 0);
408 transformation = TransformationPushIdThroughVariable(
409 value_id, value_synonym_id, variable_id, variable_storage_class,
410 initializer_id, instruction_descriptor);
411 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
412
413 value_id = 21;
414 value_synonym_id = 108;
415 variable_id = 109;
416 initializer_id = 21;
417 variable_storage_class = (uint32_t)spv::StorageClass::Private;
418 instruction_descriptor =
419 MakeInstructionDescriptor(95, spv::Op::OpReturnValue, 0);
420 transformation = TransformationPushIdThroughVariable(
421 value_id, value_synonym_id, variable_id, variable_storage_class,
422 initializer_id, instruction_descriptor);
423 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
424
425 value_id = 23;
426 value_synonym_id = 110;
427 variable_id = 111;
428 initializer_id = 21;
429 variable_storage_class = (uint32_t)spv::StorageClass::Private;
430 instruction_descriptor = MakeInstructionDescriptor(27, spv::Op::OpStore, 0);
431 transformation = TransformationPushIdThroughVariable(
432 value_id, value_synonym_id, variable_id, variable_storage_class,
433 initializer_id, instruction_descriptor);
434 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
435
436 std::string variant_shader = R"(
437 OpCapability Shader
438 OpCapability VariablePointers
439 %1 = OpExtInstImport "GLSL.std.450"
440 OpMemoryModel Logical GLSL450
441 OpEntryPoint Fragment %4 "main" %92 %52 %53 %109 %111
442 OpExecutionMode %4 OriginUpperLeft
443 OpSource ESSL 310
444 OpDecorate %92 BuiltIn FragCoord
445 %2 = OpTypeVoid
446 %3 = OpTypeFunction %2
447 %6 = OpTypeInt 32 1
448 %7 = OpTypeFloat 32
449 %8 = OpTypeStruct %6 %7
450 %9 = OpTypePointer Function %8
451 %10 = OpTypeFunction %6 %9
452 %14 = OpConstant %6 0
453 %15 = OpTypePointer Function %6
454 %51 = OpTypePointer Private %6
455 %21 = OpConstant %6 2
456 %23 = OpConstant %6 1
457 %24 = OpConstant %7 1
458 %25 = OpTypePointer Function %7
459 %50 = OpTypePointer Private %7
460 %34 = OpTypeBool
461 %35 = OpConstantFalse %34
462 %60 = OpConstantNull %50
463 %52 = OpVariable %50 Private
464 %53 = OpVariable %51 Private
465 %80 = OpConstantComposite %8 %21 %24
466 %90 = OpTypeVector %7 4
467 %91 = OpTypePointer Input %90
468 %92 = OpVariable %91 Input
469 %93 = OpConstantComposite %90 %24 %24 %24 %24
470 %109 = OpVariable %51 Private %21
471 %111 = OpVariable %51 Private %21
472 %4 = OpFunction %2 None %3
473 %5 = OpLabel
474 %103 = OpVariable %15 Function %21
475 %101 = OpVariable %9 Function %80
476 %20 = OpVariable %9 Function
477 %27 = OpVariable %9 Function
478 OpStore %111 %23
479 %110 = OpLoad %6 %111
480 OpStore %53 %21
481 %22 = OpAccessChain %15 %20 %14
482 %44 = OpCopyObject %9 %20
483 %26 = OpAccessChain %25 %20 %23
484 %29 = OpFunctionCall %6 %12 %27
485 %30 = OpAccessChain %15 %20 %14
486 %45 = OpCopyObject %15 %30
487 %81 = OpCopyObject %9 %27
488 %33 = OpAccessChain %15 %20 %14
489 OpSelectionMerge %37 None
490 OpBranchConditional %35 %36 %37
491 %36 = OpLabel
492 OpStore %101 %80
493 %100 = OpLoad %8 %101
494 OpStore %103 %21
495 %102 = OpLoad %6 %103
496 %38 = OpAccessChain %15 %20 %14
497 %40 = OpAccessChain %15 %20 %14
498 %43 = OpAccessChain %15 %20 %14
499 %82 = OpCopyObject %9 %27
500 OpBranch %37
501 %37 = OpLabel
502 OpReturn
503 OpFunctionEnd
504 %12 = OpFunction %6 None %10
505 %11 = OpFunctionParameter %9
506 %13 = OpLabel
507 %107 = OpVariable %9 Function %80
508 %105 = OpVariable %9 Function %80
509 %46 = OpCopyObject %9 %11
510 %16 = OpAccessChain %15 %11 %14
511 %95 = OpCopyObject %8 %80
512 OpStore %105 %95
513 %104 = OpLoad %8 %105
514 OpStore %107 %80
515 %106 = OpLoad %8 %107
516 OpStore %109 %21
517 %108 = OpLoad %6 %109
518 OpReturnValue %21
519 OpFunctionEnd
520 )";
521
522 ASSERT_TRUE(IsEqual(env, variant_shader, context.get()));
523 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
524 MakeDataDescriptor(80, {}), MakeDataDescriptor(100, {})));
525 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
526 MakeDataDescriptor(21, {}), MakeDataDescriptor(102, {})));
527 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
528 MakeDataDescriptor(95, {}), MakeDataDescriptor(104, {})));
529 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
530 MakeDataDescriptor(80, {}), MakeDataDescriptor(106, {})));
531 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
532 MakeDataDescriptor(21, {}), MakeDataDescriptor(108, {})));
533 }
534
535 TEST(TransformationPushIdThroughVariableTest, AddSynonymsForRelevantIds) {
536 std::string reference_shader = R"(
537 OpCapability Shader
538 OpCapability VariablePointers
539 %1 = OpExtInstImport "GLSL.std.450"
540 OpMemoryModel Logical GLSL450
541 OpEntryPoint Fragment %4 "main" %92 %52 %53
542 OpExecutionMode %4 OriginUpperLeft
543 OpSource ESSL 310
544 OpDecorate %92 BuiltIn FragCoord
545 %2 = OpTypeVoid
546 %3 = OpTypeFunction %2
547 %6 = OpTypeInt 32 1
548 %7 = OpTypeFloat 32
549 %8 = OpTypeStruct %6 %7
550 %9 = OpTypePointer Function %8
551 %10 = OpTypeFunction %6 %9
552 %14 = OpConstant %6 0
553 %15 = OpTypePointer Function %6
554 %51 = OpTypePointer Private %6
555 %21 = OpConstant %6 2
556 %23 = OpConstant %6 1
557 %24 = OpConstant %7 1
558 %25 = OpTypePointer Function %7
559 %50 = OpTypePointer Private %7
560 %34 = OpTypeBool
561 %35 = OpConstantFalse %34
562 %60 = OpConstantNull %50
563 %52 = OpVariable %50 Private
564 %53 = OpVariable %51 Private
565 %80 = OpConstantComposite %8 %21 %24
566 %90 = OpTypeVector %7 4
567 %91 = OpTypePointer Input %90
568 %92 = OpVariable %91 Input
569 %93 = OpConstantComposite %90 %24 %24 %24 %24
570 %4 = OpFunction %2 None %3
571 %5 = OpLabel
572 %20 = OpVariable %9 Function
573 %27 = OpVariable %9 Function
574 %22 = OpAccessChain %15 %20 %14
575 %44 = OpCopyObject %9 %20
576 %26 = OpAccessChain %25 %20 %23
577 %29 = OpFunctionCall %6 %12 %27
578 %30 = OpAccessChain %15 %20 %14
579 %45 = OpCopyObject %15 %30
580 %81 = OpCopyObject %9 %27
581 %33 = OpAccessChain %15 %20 %14
582 OpSelectionMerge %37 None
583 OpBranchConditional %35 %36 %37
584 %36 = OpLabel
585 %38 = OpAccessChain %15 %20 %14
586 %40 = OpAccessChain %15 %20 %14
587 %43 = OpAccessChain %15 %20 %14
588 %82 = OpCopyObject %9 %27
589 OpBranch %37
590 %37 = OpLabel
591 OpReturn
592 OpFunctionEnd
593 %12 = OpFunction %6 None %10
594 %11 = OpFunctionParameter %9
595 %13 = OpLabel
596 %46 = OpCopyObject %9 %11
597 %16 = OpAccessChain %15 %11 %14
598 %95 = OpCopyObject %8 %80
599 OpReturnValue %21
600 %100 = OpLabel
601 OpUnreachable
602 OpFunctionEnd
603 )";
604
605 const auto env = SPV_ENV_UNIVERSAL_1_4;
606 const auto consumer = nullptr;
607 const auto context =
608 BuildModule(env, consumer, reference_shader, kFuzzAssembleOption);
609
610 spvtools::ValidatorOptions validator_options;
611 validator_options.SetRelaxLogicalPointer(true);
612 TransformationContext transformation_context(
613 MakeUnique<FactManager>(context.get()), validator_options);
614 // Tests the reference shader validity.
615 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
616 kConsoleMessageConsumer));
617
618 uint32_t value_id = 21;
619 uint32_t value_synonym_id = 62;
620 uint32_t variable_id = 63;
621 uint32_t initializer_id = 23;
622 uint32_t variable_storage_class = (uint32_t)spv::StorageClass::Private;
623 auto instruction_descriptor =
624 MakeInstructionDescriptor(95, spv::Op::OpReturnValue, 0);
625 auto transformation = TransformationPushIdThroughVariable(
626 value_id, value_synonym_id, variable_id, variable_storage_class,
627 initializer_id, instruction_descriptor);
628 ASSERT_TRUE(
629 transformation.IsApplicable(context.get(), transformation_context));
630 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
631 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
632 kConsoleMessageConsumer));
633 ASSERT_TRUE(transformation_context.GetFactManager()->IsSynonymous(
634 MakeDataDescriptor(21, {}), MakeDataDescriptor(62, {})));
635 }
636
637 TEST(TransformationPushIdThroughVariableTest, DontAddSynonymsForIrrelevantIds) {
638 std::string reference_shader = R"(
639 OpCapability Shader
640 OpCapability VariablePointers
641 %1 = OpExtInstImport "GLSL.std.450"
642 OpMemoryModel Logical GLSL450
643 OpEntryPoint Fragment %4 "main" %92 %52 %53
644 OpExecutionMode %4 OriginUpperLeft
645 OpSource ESSL 310
646 OpDecorate %92 BuiltIn FragCoord
647 %2 = OpTypeVoid
648 %3 = OpTypeFunction %2
649 %6 = OpTypeInt 32 1
650 %7 = OpTypeFloat 32
651 %8 = OpTypeStruct %6 %7
652 %9 = OpTypePointer Function %8
653 %10 = OpTypeFunction %6 %9
654 %14 = OpConstant %6 0
655 %15 = OpTypePointer Function %6
656 %51 = OpTypePointer Private %6
657 %21 = OpConstant %6 2
658 %23 = OpConstant %6 1
659 %24 = OpConstant %7 1
660 %25 = OpTypePointer Function %7
661 %50 = OpTypePointer Private %7
662 %34 = OpTypeBool
663 %35 = OpConstantFalse %34
664 %60 = OpConstantNull %50
665 %52 = OpVariable %50 Private
666 %53 = OpVariable %51 Private
667 %80 = OpConstantComposite %8 %21 %24
668 %90 = OpTypeVector %7 4
669 %91 = OpTypePointer Input %90
670 %92 = OpVariable %91 Input
671 %93 = OpConstantComposite %90 %24 %24 %24 %24
672 %4 = OpFunction %2 None %3
673 %5 = OpLabel
674 %20 = OpVariable %9 Function
675 %27 = OpVariable %9 Function
676 %22 = OpAccessChain %15 %20 %14
677 %44 = OpCopyObject %9 %20
678 %26 = OpAccessChain %25 %20 %23
679 %29 = OpFunctionCall %6 %12 %27
680 %30 = OpAccessChain %15 %20 %14
681 %45 = OpCopyObject %15 %30
682 %81 = OpCopyObject %9 %27
683 %33 = OpAccessChain %15 %20 %14
684 OpSelectionMerge %37 None
685 OpBranchConditional %35 %36 %37
686 %36 = OpLabel
687 %38 = OpAccessChain %15 %20 %14
688 %40 = OpAccessChain %15 %20 %14
689 %43 = OpAccessChain %15 %20 %14
690 %82 = OpCopyObject %9 %27
691 OpBranch %37
692 %37 = OpLabel
693 OpReturn
694 OpFunctionEnd
695 %12 = OpFunction %6 None %10
696 %11 = OpFunctionParameter %9
697 %13 = OpLabel
698 %46 = OpCopyObject %9 %11
699 %16 = OpAccessChain %15 %11 %14
700 %95 = OpCopyObject %8 %80
701 OpReturnValue %21
702 %100 = OpLabel
703 OpUnreachable
704 OpFunctionEnd
705 )";
706
707 const auto env = SPV_ENV_UNIVERSAL_1_4;
708 const auto consumer = nullptr;
709 const auto context =
710 BuildModule(env, consumer, reference_shader, kFuzzAssembleOption);
711
712 spvtools::ValidatorOptions validator_options;
713 validator_options.SetRelaxLogicalPointer(true);
714 TransformationContext transformation_context(
715 MakeUnique<FactManager>(context.get()), validator_options);
716 // Tests the reference shader validity.
717 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
718 kConsoleMessageConsumer));
719
720 transformation_context.GetFactManager()->AddFactIdIsIrrelevant(21);
721
722 uint32_t value_id = 21;
723 uint32_t value_synonym_id = 62;
724 uint32_t variable_id = 63;
725 uint32_t initializer_id = 23;
726 uint32_t variable_storage_class = (uint32_t)spv::StorageClass::Private;
727 auto instruction_descriptor =
728 MakeInstructionDescriptor(95, spv::Op::OpReturnValue, 0);
729 auto transformation = TransformationPushIdThroughVariable(
730 value_id, value_synonym_id, variable_id, variable_storage_class,
731 initializer_id, instruction_descriptor);
732 ASSERT_TRUE(
733 transformation.IsApplicable(context.get(), transformation_context));
734 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
735 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
736 kConsoleMessageConsumer));
737 ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
738 MakeDataDescriptor(21, {}), MakeDataDescriptor(62, {})));
739 }
740
741 TEST(TransformationPushIdThroughVariableTest, DontAddSynonymsInDeadBlocks) {
742 std::string reference_shader = R"(
743 OpCapability Shader
744 %1 = OpExtInstImport "GLSL.std.450"
745 OpMemoryModel Logical GLSL450
746 OpEntryPoint Fragment %4 "main"
747 OpExecutionMode %4 OriginUpperLeft
748 OpSource ESSL 320
749 %2 = OpTypeVoid
750 %3 = OpTypeFunction %2
751 %6 = OpTypeInt 32 1
752 %7 = OpTypeVector %6 2
753 %8 = OpTypePointer Function %7
754 %10 = OpConstant %6 0
755 %11 = OpConstant %6 1
756 %12 = OpConstantComposite %7 %10 %11
757 %13 = OpTypeBool
758 %50 = OpTypePointer Function %13
759 %14 = OpConstantFalse %13
760 %4 = OpFunction %2 None %3
761 %5 = OpLabel
762 %9 = OpVariable %8 Function
763 OpStore %9 %12
764 OpSelectionMerge %16 None
765 OpBranchConditional %14 %15 %16
766 %15 = OpLabel
767 OpBranch %16
768 %16 = OpLabel
769 OpReturn
770 OpFunctionEnd
771 )";
772
773 const auto env = SPV_ENV_UNIVERSAL_1_4;
774 const auto consumer = nullptr;
775 const auto context =
776 BuildModule(env, consumer, reference_shader, kFuzzAssembleOption);
777
778 spvtools::ValidatorOptions validator_options;
779 validator_options.SetRelaxLogicalPointer(true);
780 TransformationContext transformation_context(
781 MakeUnique<FactManager>(context.get()), validator_options);
782 // Tests the reference shader validity.
783 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
784 kConsoleMessageConsumer));
785
786 transformation_context.GetFactManager()->AddFactBlockIsDead(15);
787 auto transformation = TransformationPushIdThroughVariable(
788 14, 100, 101, uint32_t(spv::StorageClass::Function), 14,
789 MakeInstructionDescriptor(15, spv::Op::OpBranch, 0));
790 ASSERT_TRUE(
791 transformation.IsApplicable(context.get(), transformation_context));
792 ApplyAndCheckFreshIds(transformation, context.get(), &transformation_context);
793 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
794 kConsoleMessageConsumer));
795 ASSERT_FALSE(transformation_context.GetFactManager()->IsSynonymous(
796 MakeDataDescriptor(14, {}), MakeDataDescriptor(100, {})));
797 }
798
799 } // namespace
800 } // namespace fuzz
801 } // namespace spvtools