Debian Codegraph @

third_party/spirv-tools/test/fuzz/transformation_replace_id_with_synonym_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/transformation_replace_id_with_synonym.h"
16
17 #include "gtest/gtest.h"
18 #include "source/fuzz/data_descriptor.h"
19 #include "source/fuzz/fuzzer_util.h"
20 #include "source/fuzz/id_use_descriptor.h"
21 #include "source/fuzz/instruction_descriptor.h"
22 #include "test/fuzz/fuzz_test_util.h"
23
24 namespace spvtools {
25 namespace fuzz {
26 namespace {
27
28 // The following shader was obtained from this GLSL, which was then optimized
29 // with spirv-opt -O and manually edited to include some uses of OpCopyObject
30 // (to introduce id synonyms).
31 //
32 // #version 310 es
33 //
34 // precision highp int;
35 // precision highp float;
36 //
37 // layout(set = 0, binding = 0) uniform buf {
38 // int a;
39 // int b;
40 // int c;
41 // };
42 //
43 // layout(location = 0) out vec4 color;
44 //
45 // void main() {
46 // int x = a;
47 // float f = 0.0;
48 // while (x < b) {
49 // switch(x % 4) {
50 // case 0:
51 // color[0] = f;
52 // break;
53 // case 1:
54 // color[1] = f;
55 // break;
56 // case 2:
57 // color[2] = f;
58 // break;
59 // case 3:
60 // color[3] = f;
61 // break;
62 // default:
63 // break;
64 // }
65 // if (x > c) {
66 // x++;
67 // } else {
68 // x += 2;
69 // }
70 // }
71 // color[0] += color[1] + float(x);
72 // }
73 const std::string kComplexShader = R"(
74 OpCapability Shader
75 %1 = OpExtInstImport "GLSL.std.450"
76 OpMemoryModel Logical GLSL450
77 OpEntryPoint Fragment %4 "main" %42
78 OpExecutionMode %4 OriginUpperLeft
79 OpSource ESSL 310
80 OpName %4 "main"
81 OpName %9 "buf"
82 OpMemberName %9 0 "a"
83 OpMemberName %9 1 "b"
84 OpMemberName %9 2 "c"
85 OpName %11 ""
86 OpName %42 "color"
87 OpMemberDecorate %9 0 Offset 0
88 OpMemberDecorate %9 1 Offset 4
89 OpMemberDecorate %9 2 Offset 8
90 OpDecorate %9 Block
91 OpDecorate %11 DescriptorSet 0
92 OpDecorate %11 Binding 0
93 OpDecorate %42 Location 0
94 %2 = OpTypeVoid
95 %3 = OpTypeFunction %2
96 %6 = OpTypeInt 32 1
97 %9 = OpTypeStruct %6 %6 %6
98 %10 = OpTypePointer Uniform %9
99 %11 = OpVariable %10 Uniform
100 %12 = OpConstant %6 0
101 %13 = OpTypePointer Uniform %6
102 %16 = OpTypeFloat 32
103 %19 = OpConstant %16 0
104 %26 = OpConstant %6 1
105 %29 = OpTypeBool
106 %32 = OpConstant %6 4
107 %40 = OpTypeVector %16 4
108 %41 = OpTypePointer Output %40
109 %42 = OpVariable %41 Output
110 %44 = OpTypeInt 32 0
111 %45 = OpConstant %44 0
112 %46 = OpTypePointer Output %16
113 %50 = OpConstant %44 1
114 %54 = OpConstant %44 2
115 %58 = OpConstant %44 3
116 %64 = OpConstant %6 2
117 %4 = OpFunction %2 None %3
118 %5 = OpLabel
119 %209 = OpCopyObject %6 %12
120 %14 = OpAccessChain %13 %11 %12
121 %15 = OpLoad %6 %14
122 %200 = OpCopyObject %6 %15
123 OpBranch %20
124 %20 = OpLabel
125 %84 = OpPhi %6 %15 %5 %86 %69
126 %27 = OpAccessChain %13 %11 %26
127 %28 = OpLoad %6 %27
128 %207 = OpCopyObject %6 %84
129 %201 = OpCopyObject %6 %15
130 %30 = OpSLessThan %29 %84 %28
131 OpLoopMerge %22 %69 None
132 OpBranchConditional %30 %21 %22
133 %21 = OpLabel
134 %33 = OpSMod %6 %84 %32
135 %208 = OpCopyObject %6 %33
136 OpSelectionMerge %39 None
137 OpSwitch %33 %38 0 %34 1 %35 2 %36 3 %37
138 %38 = OpLabel
139 %202 = OpCopyObject %6 %15
140 OpBranch %39
141 %34 = OpLabel
142 %210 = OpCopyObject %16 %19
143 %47 = OpAccessChain %46 %42 %45
144 OpStore %47 %19
145 OpBranch %39
146 %35 = OpLabel
147 %51 = OpAccessChain %46 %42 %50
148 OpStore %51 %19
149 OpBranch %39
150 %36 = OpLabel
151 %204 = OpCopyObject %44 %54
152 %55 = OpAccessChain %46 %42 %54
153 %203 = OpCopyObject %46 %55
154 OpStore %55 %19
155 OpBranch %39
156 %37 = OpLabel
157 %59 = OpAccessChain %46 %42 %58
158 OpStore %59 %19
159 OpBranch %39
160 %39 = OpLabel
161 %300 = OpIAdd %6 %15 %15
162 %65 = OpAccessChain %13 %11 %64
163 %66 = OpLoad %6 %65
164 %67 = OpSGreaterThan %29 %84 %66
165 OpSelectionMerge %1000 None
166 OpBranchConditional %67 %68 %72
167 %68 = OpLabel
168 %71 = OpIAdd %6 %84 %26
169 OpBranch %1000
170 %72 = OpLabel
171 %74 = OpIAdd %6 %84 %64
172 %205 = OpCopyObject %6 %74
173 OpBranch %1000
174 %1000 = OpLabel
175 %86 = OpPhi %6 %71 %68 %74 %72
176 %301 = OpPhi %6 %71 %68 %15 %72
177 OpBranch %69
178 %69 = OpLabel
179 OpBranch %20
180 %22 = OpLabel
181 %75 = OpAccessChain %46 %42 %50
182 %76 = OpLoad %16 %75
183 %78 = OpConvertSToF %16 %84
184 %80 = OpAccessChain %46 %42 %45
185 %206 = OpCopyObject %16 %78
186 %81 = OpLoad %16 %80
187 %79 = OpFAdd %16 %76 %78
188 %82 = OpFAdd %16 %81 %79
189 OpStore %80 %82
190 OpReturn
191 OpFunctionEnd
192 )";
193
194 protobufs::Fact MakeSynonymFact(uint32_t first, uint32_t second) {
195 protobufs::FactDataSynonym data_synonym_fact;
196 *data_synonym_fact.mutable_data1() = MakeDataDescriptor(first, {});
197 *data_synonym_fact.mutable_data2() = MakeDataDescriptor(second, {});
198 protobufs::Fact result;
199 *result.mutable_data_synonym_fact() = data_synonym_fact;
200 return result;
201 }
202
203 // Equips the fact manager with synonym facts for the above shader.
204 void SetUpIdSynonyms(FactManager* fact_manager) {
205 fact_manager->MaybeAddFact(MakeSynonymFact(15, 200));
206 fact_manager->MaybeAddFact(MakeSynonymFact(15, 201));
207 fact_manager->MaybeAddFact(MakeSynonymFact(15, 202));
208 fact_manager->MaybeAddFact(MakeSynonymFact(55, 203));
209 fact_manager->MaybeAddFact(MakeSynonymFact(54, 204));
210 fact_manager->MaybeAddFact(MakeSynonymFact(74, 205));
211 fact_manager->MaybeAddFact(MakeSynonymFact(78, 206));
212 fact_manager->MaybeAddFact(MakeSynonymFact(84, 207));
213 fact_manager->MaybeAddFact(MakeSynonymFact(33, 208));
214 fact_manager->MaybeAddFact(MakeSynonymFact(12, 209));
215 fact_manager->MaybeAddFact(MakeSynonymFact(19, 210));
216 }
217
218 TEST(TransformationReplaceIdWithSynonymTest, IllegalTransformations) {
219 const auto env = SPV_ENV_UNIVERSAL_1_3;
220 const auto consumer = nullptr;
221 const auto context =
222 BuildModule(env, consumer, kComplexShader, kFuzzAssembleOption);
223 spvtools::ValidatorOptions validator_options;
224 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
225 kConsoleMessageConsumer));
226 TransformationContext transformation_context(
227 MakeUnique<FactManager>(context.get()), validator_options);
228 SetUpIdSynonyms(transformation_context.GetFactManager());
229
230 // %202 cannot replace %15 as in-operand 0 of %300, since %202 does not
231 // dominate %300.
232 auto synonym_does_not_dominate_use = TransformationReplaceIdWithSynonym(
233 MakeIdUseDescriptor(15, MakeInstructionDescriptor(300, SpvOpIAdd, 0), 0),
234 202);
235 ASSERT_FALSE(synonym_does_not_dominate_use.IsApplicable(
236 context.get(), transformation_context));
237
238 // %202 cannot replace %15 as in-operand 2 of %301, since this is the OpPhi's
239 // incoming value for block %72, and %202 does not dominate %72.
240 auto synonym_does_not_dominate_use_op_phi =
241 TransformationReplaceIdWithSynonym(
242 MakeIdUseDescriptor(15, MakeInstructionDescriptor(301, SpvOpPhi, 0),
243 2),
244 202);
245 ASSERT_FALSE(synonym_does_not_dominate_use_op_phi.IsApplicable(
246 context.get(), transformation_context));
247
248 // %200 is not a synonym for %84
249 auto id_in_use_is_not_synonymous = TransformationReplaceIdWithSynonym(
250 MakeIdUseDescriptor(
251 84, MakeInstructionDescriptor(67, SpvOpSGreaterThan, 0), 0),
252 200);
253 ASSERT_FALSE(id_in_use_is_not_synonymous.IsApplicable(
254 context.get(), transformation_context));
255
256 // %86 is not a synonym for anything (and in particular not for %74)
257 auto id_has_no_synonyms = TransformationReplaceIdWithSynonym(
258 MakeIdUseDescriptor(86, MakeInstructionDescriptor(84, SpvOpPhi, 0), 2),
259 74);
260 ASSERT_FALSE(
261 id_has_no_synonyms.IsApplicable(context.get(), transformation_context));
262
263 // This would lead to %207 = 'OpCopyObject %type %207' if it were allowed
264 auto synonym_use_is_in_synonym_definition =
265 TransformationReplaceIdWithSynonym(
266 MakeIdUseDescriptor(
267 84, MakeInstructionDescriptor(207, SpvOpCopyObject, 0), 0),
268 207);
269 ASSERT_FALSE(synonym_use_is_in_synonym_definition.IsApplicable(
270 context.get(), transformation_context));
271
272 // The id use descriptor does not lead to a use (%84 is not used in the
273 // definition of %207)
274 auto bad_id_use_descriptor = TransformationReplaceIdWithSynonym(
275 MakeIdUseDescriptor(
276 84, MakeInstructionDescriptor(200, SpvOpCopyObject, 0), 0),
277 207);
278 ASSERT_FALSE(bad_id_use_descriptor.IsApplicable(context.get(),
279 transformation_context));
280
281 // This replacement would lead to an access chain into a struct using a
282 // non-constant index.
283 auto bad_access_chain = TransformationReplaceIdWithSynonym(
284 MakeIdUseDescriptor(
285 12, MakeInstructionDescriptor(14, SpvOpAccessChain, 0), 1),
286 209);
287 ASSERT_FALSE(
288 bad_access_chain.IsApplicable(context.get(), transformation_context));
289 }
290
291 TEST(TransformationReplaceIdWithSynonymTest, LegalTransformations) {
292 const auto env = SPV_ENV_UNIVERSAL_1_3;
293 const auto consumer = nullptr;
294 const auto context =
295 BuildModule(env, consumer, kComplexShader, kFuzzAssembleOption);
296 spvtools::ValidatorOptions validator_options;
297 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
298 kConsoleMessageConsumer));
299 TransformationContext transformation_context(
300 MakeUnique<FactManager>(context.get()), validator_options);
301 SetUpIdSynonyms(transformation_context.GetFactManager());
302
303 auto global_constant_synonym = TransformationReplaceIdWithSynonym(
304 MakeIdUseDescriptor(19, MakeInstructionDescriptor(47, SpvOpStore, 0), 1),
305 210);
306 ASSERT_TRUE(global_constant_synonym.IsApplicable(context.get(),
307 transformation_context));
308 ApplyAndCheckFreshIds(global_constant_synonym, context.get(),
309 &transformation_context);
310 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
311 kConsoleMessageConsumer));
312
313 auto replace_vector_access_chain_index = TransformationReplaceIdWithSynonym(
314 MakeIdUseDescriptor(
315 54, MakeInstructionDescriptor(55, SpvOpAccessChain, 0), 1),
316 204);
317 ASSERT_TRUE(replace_vector_access_chain_index.IsApplicable(
318 context.get(), transformation_context));
319 ApplyAndCheckFreshIds(replace_vector_access_chain_index, context.get(),
320 &transformation_context);
321 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
322 kConsoleMessageConsumer));
323
324 // This is an interesting case because it replaces something that is being
325 // copied with something that is already a synonym.
326 auto regular_replacement = TransformationReplaceIdWithSynonym(
327 MakeIdUseDescriptor(
328 15, MakeInstructionDescriptor(202, SpvOpCopyObject, 0), 0),
329 201);
330 ASSERT_TRUE(
331 regular_replacement.IsApplicable(context.get(), transformation_context));
332 ApplyAndCheckFreshIds(regular_replacement, context.get(),
333 &transformation_context);
334 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
335 kConsoleMessageConsumer));
336
337 auto regular_replacement2 = TransformationReplaceIdWithSynonym(
338 MakeIdUseDescriptor(55, MakeInstructionDescriptor(203, SpvOpStore, 0), 0),
339 203);
340 ASSERT_TRUE(
341 regular_replacement2.IsApplicable(context.get(), transformation_context));
342 ApplyAndCheckFreshIds(regular_replacement2, context.get(),
343 &transformation_context);
344 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
345 kConsoleMessageConsumer));
346
347 auto good_op_phi = TransformationReplaceIdWithSynonym(
348 MakeIdUseDescriptor(74, MakeInstructionDescriptor(86, SpvOpPhi, 0), 2),
349 205);
350 ASSERT_TRUE(good_op_phi.IsApplicable(context.get(), transformation_context));
351 ApplyAndCheckFreshIds(good_op_phi, context.get(), &transformation_context);
352 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
353 kConsoleMessageConsumer));
354
355 const std::string after_transformation = R"(
356 OpCapability Shader
357 %1 = OpExtInstImport "GLSL.std.450"
358 OpMemoryModel Logical GLSL450
359 OpEntryPoint Fragment %4 "main" %42
360 OpExecutionMode %4 OriginUpperLeft
361 OpSource ESSL 310
362 OpName %4 "main"
363 OpName %9 "buf"
364 OpMemberName %9 0 "a"
365 OpMemberName %9 1 "b"
366 OpMemberName %9 2 "c"
367 OpName %11 ""
368 OpName %42 "color"
369 OpMemberDecorate %9 0 Offset 0
370 OpMemberDecorate %9 1 Offset 4
371 OpMemberDecorate %9 2 Offset 8
372 OpDecorate %9 Block
373 OpDecorate %11 DescriptorSet 0
374 OpDecorate %11 Binding 0
375 OpDecorate %42 Location 0
376 %2 = OpTypeVoid
377 %3 = OpTypeFunction %2
378 %6 = OpTypeInt 32 1
379 %9 = OpTypeStruct %6 %6 %6
380 %10 = OpTypePointer Uniform %9
381 %11 = OpVariable %10 Uniform
382 %12 = OpConstant %6 0
383 %13 = OpTypePointer Uniform %6
384 %16 = OpTypeFloat 32
385 %19 = OpConstant %16 0
386 %26 = OpConstant %6 1
387 %29 = OpTypeBool
388 %32 = OpConstant %6 4
389 %40 = OpTypeVector %16 4
390 %41 = OpTypePointer Output %40
391 %42 = OpVariable %41 Output
392 %44 = OpTypeInt 32 0
393 %45 = OpConstant %44 0
394 %46 = OpTypePointer Output %16
395 %50 = OpConstant %44 1
396 %54 = OpConstant %44 2
397 %58 = OpConstant %44 3
398 %64 = OpConstant %6 2
399 %4 = OpFunction %2 None %3
400 %5 = OpLabel
401 %209 = OpCopyObject %6 %12
402 %14 = OpAccessChain %13 %11 %12
403 %15 = OpLoad %6 %14
404 %200 = OpCopyObject %6 %15
405 OpBranch %20
406 %20 = OpLabel
407 %84 = OpPhi %6 %15 %5 %86 %69
408 %27 = OpAccessChain %13 %11 %26
409 %28 = OpLoad %6 %27
410 %207 = OpCopyObject %6 %84
411 %201 = OpCopyObject %6 %15
412 %30 = OpSLessThan %29 %84 %28
413 OpLoopMerge %22 %69 None
414 OpBranchConditional %30 %21 %22
415 %21 = OpLabel
416 %33 = OpSMod %6 %84 %32
417 %208 = OpCopyObject %6 %33
418 OpSelectionMerge %39 None
419 OpSwitch %33 %38 0 %34 1 %35 2 %36 3 %37
420 %38 = OpLabel
421 %202 = OpCopyObject %6 %201
422 OpBranch %39
423 %34 = OpLabel
424 %210 = OpCopyObject %16 %19
425 %47 = OpAccessChain %46 %42 %45
426 OpStore %47 %210
427 OpBranch %39
428 %35 = OpLabel
429 %51 = OpAccessChain %46 %42 %50
430 OpStore %51 %19
431 OpBranch %39
432 %36 = OpLabel
433 %204 = OpCopyObject %44 %54
434 %55 = OpAccessChain %46 %42 %204
435 %203 = OpCopyObject %46 %55
436 OpStore %203 %19
437 OpBranch %39
438 %37 = OpLabel
439 %59 = OpAccessChain %46 %42 %58
440 OpStore %59 %19
441 OpBranch %39
442 %39 = OpLabel
443 %300 = OpIAdd %6 %15 %15
444 %65 = OpAccessChain %13 %11 %64
445 %66 = OpLoad %6 %65
446 %67 = OpSGreaterThan %29 %84 %66
447 OpSelectionMerge %1000 None
448 OpBranchConditional %67 %68 %72
449 %68 = OpLabel
450 %71 = OpIAdd %6 %84 %26
451 OpBranch %1000
452 %72 = OpLabel
453 %74 = OpIAdd %6 %84 %64
454 %205 = OpCopyObject %6 %74
455 OpBranch %1000
456 %1000 = OpLabel
457 %86 = OpPhi %6 %71 %68 %205 %72
458 %301 = OpPhi %6 %71 %68 %15 %72
459 OpBranch %69
460 %69 = OpLabel
461 OpBranch %20
462 %22 = OpLabel
463 %75 = OpAccessChain %46 %42 %50
464 %76 = OpLoad %16 %75
465 %78 = OpConvertSToF %16 %84
466 %80 = OpAccessChain %46 %42 %45
467 %206 = OpCopyObject %16 %78
468 %81 = OpLoad %16 %80
469 %79 = OpFAdd %16 %76 %78
470 %82 = OpFAdd %16 %81 %79
471 OpStore %80 %82
472 OpReturn
473 OpFunctionEnd
474 )";
475
476 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
477 }
478
479 TEST(TransformationReplaceIdWithSynonymTest, SynonymsOfVariables) {
480 // The following SPIR-V comes from this GLSL, with object copies added:
481 //
482 // #version 310 es
483 //
484 // precision highp int;
485 //
486 // int g;
487 //
488 // void main() {
489 // int l;
490 // l = g;
491 // g = l;
492 // }
493 const std::string shader = R"(
494 OpCapability Shader
495 %1 = OpExtInstImport "GLSL.std.450"
496 OpMemoryModel Logical GLSL450
497 OpEntryPoint Fragment %4 "main"
498 OpExecutionMode %4 OriginUpperLeft
499 OpSource ESSL 310
500 OpName %4 "main"
501 OpName %8 "l"
502 OpName %10 "g"
503 %2 = OpTypeVoid
504 %3 = OpTypeFunction %2
505 %6 = OpTypeInt 32 1
506 %7 = OpTypePointer Function %6
507 %9 = OpTypePointer Private %6
508 %10 = OpVariable %9 Private
509 %4 = OpFunction %2 None %3
510 %5 = OpLabel
511 %8 = OpVariable %7 Function
512 %100 = OpCopyObject %9 %10
513 %101 = OpCopyObject %7 %8
514 %11 = OpLoad %6 %10
515 OpStore %8 %11
516 %12 = OpLoad %6 %8
517 OpStore %10 %12
518 OpReturn
519 OpFunctionEnd
520 )";
521
522 const auto env = SPV_ENV_UNIVERSAL_1_3;
523 const auto consumer = nullptr;
524 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
525 spvtools::ValidatorOptions validator_options;
526 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
527 kConsoleMessageConsumer));
528 TransformationContext transformation_context(
529 MakeUnique<FactManager>(context.get()), validator_options);
530 transformation_context.GetFactManager()->MaybeAddFact(
531 MakeSynonymFact(10, 100));
532 transformation_context.GetFactManager()->MaybeAddFact(
533 MakeSynonymFact(8, 101));
534
535 // Replace %10 with %100 in:
536 // %11 = OpLoad %6 %10
537 auto replacement1 = TransformationReplaceIdWithSynonym(
538 MakeIdUseDescriptor(10, MakeInstructionDescriptor(11, SpvOpLoad, 0), 0),
539 100);
540 ASSERT_TRUE(replacement1.IsApplicable(context.get(), transformation_context));
541 ApplyAndCheckFreshIds(replacement1, context.get(), &transformation_context);
542 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
543 kConsoleMessageConsumer));
544
545 // Replace %8 with %101 in:
546 // OpStore %8 %11
547 auto replacement2 = TransformationReplaceIdWithSynonym(
548 MakeIdUseDescriptor(8, MakeInstructionDescriptor(11, SpvOpStore, 0), 0),
549 101);
550 ASSERT_TRUE(replacement2.IsApplicable(context.get(), transformation_context));
551 ApplyAndCheckFreshIds(replacement2, context.get(), &transformation_context);
552 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
553 kConsoleMessageConsumer));
554
555 // Replace %8 with %101 in:
556 // %12 = OpLoad %6 %8
557 auto replacement3 = TransformationReplaceIdWithSynonym(
558 MakeIdUseDescriptor(8, MakeInstructionDescriptor(12, SpvOpLoad, 0), 0),
559 101);
560 ASSERT_TRUE(replacement3.IsApplicable(context.get(), transformation_context));
561 ApplyAndCheckFreshIds(replacement3, context.get(), &transformation_context);
562 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
563 kConsoleMessageConsumer));
564
565 // Replace %10 with %100 in:
566 // OpStore %10 %12
567 auto replacement4 = TransformationReplaceIdWithSynonym(
568 MakeIdUseDescriptor(10, MakeInstructionDescriptor(12, SpvOpStore, 0), 0),
569 100);
570 ASSERT_TRUE(replacement4.IsApplicable(context.get(), transformation_context));
571 ApplyAndCheckFreshIds(replacement4, context.get(), &transformation_context);
572 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
573 kConsoleMessageConsumer));
574
575 const std::string after_transformation = R"(
576 OpCapability Shader
577 %1 = OpExtInstImport "GLSL.std.450"
578 OpMemoryModel Logical GLSL450
579 OpEntryPoint Fragment %4 "main"
580 OpExecutionMode %4 OriginUpperLeft
581 OpSource ESSL 310
582 OpName %4 "main"
583 OpName %8 "l"
584 OpName %10 "g"
585 %2 = OpTypeVoid
586 %3 = OpTypeFunction %2
587 %6 = OpTypeInt 32 1
588 %7 = OpTypePointer Function %6
589 %9 = OpTypePointer Private %6
590 %10 = OpVariable %9 Private
591 %4 = OpFunction %2 None %3
592 %5 = OpLabel
593 %8 = OpVariable %7 Function
594 %100 = OpCopyObject %9 %10
595 %101 = OpCopyObject %7 %8
596 %11 = OpLoad %6 %100
597 OpStore %101 %11
598 %12 = OpLoad %6 %101
599 OpStore %100 %12
600 OpReturn
601 OpFunctionEnd
602 )";
603
604 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
605 }
606
607 TEST(TransformationReplaceIdWithSynonymTest,
608 SynonymOfVariableNoGoodInFunctionCall) {
609 // The following SPIR-V comes from this GLSL, with an object copy added:
610 //
611 // #version 310 es
612 //
613 // precision highp int;
614 //
615 // void foo(int x) { }
616 //
617 // void main() {
618 // int a;
619 // a = 2;
620 // foo(a);
621 // }
622 const std::string shader = R"(
623 OpCapability Shader
624 %1 = OpExtInstImport "GLSL.std.450"
625 OpMemoryModel Logical GLSL450
626 OpEntryPoint Fragment %4 "main"
627 OpExecutionMode %4 OriginUpperLeft
628 OpSource ESSL 310
629 OpName %4 "main"
630 OpName %10 "foo(i1;"
631 OpName %9 "x"
632 OpName %12 "a"
633 OpName %14 "param"
634 %2 = OpTypeVoid
635 %3 = OpTypeFunction %2
636 %6 = OpTypeInt 32 1
637 %7 = OpTypePointer Function %6
638 %8 = OpTypeFunction %2 %7
639 %13 = OpConstant %6 2
640 %4 = OpFunction %2 None %3
641 %5 = OpLabel
642 %12 = OpVariable %7 Function
643 %14 = OpVariable %7 Function
644 OpStore %12 %13
645 %15 = OpLoad %6 %12
646 OpStore %14 %15
647 %100 = OpCopyObject %7 %14
648 %16 = OpFunctionCall %2 %10 %14
649 OpReturn
650 OpFunctionEnd
651 %10 = OpFunction %2 None %8
652 %9 = OpFunctionParameter %7
653 %11 = OpLabel
654 OpReturn
655 OpFunctionEnd
656 )";
657
658 const auto env = SPV_ENV_UNIVERSAL_1_3;
659 const auto consumer = nullptr;
660 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
661 spvtools::ValidatorOptions validator_options;
662 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
663 kConsoleMessageConsumer));
664 TransformationContext transformation_context(
665 MakeUnique<FactManager>(context.get()), validator_options);
666 transformation_context.GetFactManager()->MaybeAddFact(
667 MakeSynonymFact(14, 100));
668
669 // Replace %14 with %100 in:
670 // %16 = OpFunctionCall %2 %10 %14
671 auto replacement = TransformationReplaceIdWithSynonym(
672 MakeIdUseDescriptor(
673 14, MakeInstructionDescriptor(16, SpvOpFunctionCall, 0), 1),
674 100);
675 ASSERT_FALSE(replacement.IsApplicable(context.get(), transformation_context));
676 }
677
678 TEST(TransformationReplaceIdWithSynonymTest, SynonymsOfAccessChainIndices) {
679 // The following SPIR-V comes from this GLSL, with object copies added:
680 //
681 // #version 310 es
682 //
683 // precision highp float;
684 // precision highp int;
685 //
686 // struct S {
687 // int[3] a;
688 // vec4 b;
689 // bool c;
690 // } d;
691 //
692 // float[20] e;
693 //
694 // struct T {
695 // float f;
696 // S g;
697 // } h;
698 //
699 // T[4] i;
700 //
701 // void main() {
702 // d.a[2] = 10;
703 // d.b[3] = 11.0;
704 // d.c = false;
705 // e[17] = 12.0;
706 // h.f = 13.0;
707 // h.g.a[1] = 14;
708 // h.g.b[0] = 15.0;
709 // h.g.c = true;
710 // i[0].f = 16.0;
711 // i[1].g.a[0] = 17;
712 // i[2].g.b[1] = 18.0;
713 // i[3].g.c = true;
714 // }
715 const std::string shader = R"(
716 OpCapability Shader
717 %1 = OpExtInstImport "GLSL.std.450"
718 OpMemoryModel Logical GLSL450
719 OpEntryPoint Fragment %4 "main"
720 OpExecutionMode %4 OriginUpperLeft
721 OpSource ESSL 310
722 OpName %4 "main"
723 OpName %13 "S"
724 OpMemberName %13 0 "a"
725 OpMemberName %13 1 "b"
726 OpMemberName %13 2 "c"
727 OpName %15 "d"
728 OpName %31 "e"
729 OpName %35 "T"
730 OpMemberName %35 0 "f"
731 OpMemberName %35 1 "g"
732 OpName %37 "h"
733 OpName %50 "i"
734 %2 = OpTypeVoid
735 %3 = OpTypeFunction %2
736 %6 = OpTypeInt 32 1
737 %7 = OpTypeInt 32 0
738 %8 = OpConstant %7 3
739 %9 = OpTypeArray %6 %8
740 %10 = OpTypeFloat 32
741 %11 = OpTypeVector %10 4
742 %12 = OpTypeBool
743 %13 = OpTypeStruct %9 %11 %12
744 %14 = OpTypePointer Private %13
745 %15 = OpVariable %14 Private
746 %16 = OpConstant %6 0
747 %17 = OpConstant %6 2
748 %18 = OpConstant %6 10
749 %19 = OpTypePointer Private %6
750 %21 = OpConstant %6 1
751 %22 = OpConstant %10 11
752 %23 = OpTypePointer Private %10
753 %25 = OpConstantFalse %12
754 %26 = OpTypePointer Private %12
755 %28 = OpConstant %7 20
756 %29 = OpTypeArray %10 %28
757 %30 = OpTypePointer Private %29
758 %31 = OpVariable %30 Private
759 %32 = OpConstant %6 17
760 %33 = OpConstant %10 12
761 %35 = OpTypeStruct %10 %13
762 %36 = OpTypePointer Private %35
763 %37 = OpVariable %36 Private
764 %38 = OpConstant %10 13
765 %40 = OpConstant %6 14
766 %42 = OpConstant %10 15
767 %43 = OpConstant %7 0
768 %45 = OpConstantTrue %12
769 %47 = OpConstant %7 4
770 %48 = OpTypeArray %35 %47
771 %49 = OpTypePointer Private %48
772 %50 = OpVariable %49 Private
773 %51 = OpConstant %10 16
774 %54 = OpConstant %10 18
775 %55 = OpConstant %7 1
776 %57 = OpConstant %6 3
777 %4 = OpFunction %2 None %3
778 %5 = OpLabel
779
780 %100 = OpCopyObject %6 %16 ; 0
781 %101 = OpCopyObject %6 %21 ; 1
782 %102 = OpCopyObject %6 %17 ; 2
783 %103 = OpCopyObject %6 %57 ; 3
784 %104 = OpCopyObject %6 %18 ; 10
785 %105 = OpCopyObject %6 %40 ; 14
786 %106 = OpCopyObject %6 %32 ; 17
787 %107 = OpCopyObject %7 %43 ; 0
788 %108 = OpCopyObject %7 %55 ; 1
789 %109 = OpCopyObject %7 %8 ; 3
790 %110 = OpCopyObject %7 %47 ; 4
791 %111 = OpCopyObject %7 %28 ; 20
792 %112 = OpCopyObject %12 %45 ; true
793
794 %20 = OpAccessChain %19 %15 %16 %17
795 OpStore %20 %18
796 %24 = OpAccessChain %23 %15 %21 %8
797 OpStore %24 %22
798 %27 = OpAccessChain %26 %15 %17
799 OpStore %27 %25
800 %34 = OpAccessChain %23 %31 %32
801 OpStore %34 %33
802 %39 = OpAccessChain %23 %37 %16
803 OpStore %39 %38
804 %41 = OpAccessChain %19 %37 %21 %16 %21
805 OpStore %41 %40
806 %44 = OpAccessChain %23 %37 %21 %21 %43
807 OpStore %44 %42
808 %46 = OpAccessChain %26 %37 %21 %17
809 OpStore %46 %45
810 %52 = OpAccessChain %23 %50 %16 %16
811 OpStore %52 %51
812 %53 = OpAccessChain %19 %50 %21 %21 %16 %16
813 OpStore %53 %32
814 %56 = OpAccessChain %23 %50 %17 %21 %21 %55
815 OpStore %56 %54
816 %58 = OpInBoundsAccessChain %26 %50 %57 %21 %17
817 OpStore %58 %45
818 OpReturn
819 OpFunctionEnd
820 )";
821
822 const auto env = SPV_ENV_UNIVERSAL_1_3;
823 const auto consumer = nullptr;
824 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
825 spvtools::ValidatorOptions validator_options;
826 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
827 kConsoleMessageConsumer));
828 TransformationContext transformation_context(
829 MakeUnique<FactManager>(context.get()), validator_options);
830 // Add synonym facts corresponding to the OpCopyObject operations that have
831 // been applied to all constants in the module.
832 transformation_context.GetFactManager()->MaybeAddFact(
833 MakeSynonymFact(16, 100));
834 transformation_context.GetFactManager()->MaybeAddFact(
835 MakeSynonymFact(21, 101));
836 transformation_context.GetFactManager()->MaybeAddFact(
837 MakeSynonymFact(17, 102));
838 transformation_context.GetFactManager()->MaybeAddFact(
839 MakeSynonymFact(57, 103));
840 transformation_context.GetFactManager()->MaybeAddFact(
841 MakeSynonymFact(18, 104));
842 transformation_context.GetFactManager()->MaybeAddFact(
843 MakeSynonymFact(40, 105));
844 transformation_context.GetFactManager()->MaybeAddFact(
845 MakeSynonymFact(32, 106));
846 transformation_context.GetFactManager()->MaybeAddFact(
847 MakeSynonymFact(43, 107));
848 transformation_context.GetFactManager()->MaybeAddFact(
849 MakeSynonymFact(55, 108));
850 transformation_context.GetFactManager()->MaybeAddFact(
851 MakeSynonymFact(8, 109));
852 transformation_context.GetFactManager()->MaybeAddFact(
853 MakeSynonymFact(47, 110));
854 transformation_context.GetFactManager()->MaybeAddFact(
855 MakeSynonymFact(28, 111));
856 transformation_context.GetFactManager()->MaybeAddFact(
857 MakeSynonymFact(45, 112));
858
859 // Replacements of the form %16 -> %100
860
861 // %20 = OpAccessChain %19 %15 *%16* %17
862 // Corresponds to d.*a*[2]
863 // The index %16 used for a cannot be replaced
864 auto replacement1 = TransformationReplaceIdWithSynonym(
865 MakeIdUseDescriptor(
866 16, MakeInstructionDescriptor(20, SpvOpAccessChain, 0), 1),
867 100);
868 ASSERT_FALSE(
869 replacement1.IsApplicable(context.get(), transformation_context));
870
871 // %39 = OpAccessChain %23 %37 *%16*
872 // Corresponds to h.*f*
873 // The index %16 used for f cannot be replaced
874 auto replacement2 = TransformationReplaceIdWithSynonym(
875 MakeIdUseDescriptor(
876 16, MakeInstructionDescriptor(39, SpvOpAccessChain, 0), 1),
877 100);
878 ASSERT_FALSE(
879 replacement2.IsApplicable(context.get(), transformation_context));
880
881 // %41 = OpAccessChain %19 %37 %21 *%16* %21
882 // Corresponds to h.g.*a*[1]
883 // The index %16 used for a cannot be replaced
884 auto replacement3 = TransformationReplaceIdWithSynonym(
885 MakeIdUseDescriptor(
886 16, MakeInstructionDescriptor(41, SpvOpAccessChain, 0), 2),
887 100);
888 ASSERT_FALSE(
889 replacement3.IsApplicable(context.get(), transformation_context));
890
891 // %52 = OpAccessChain %23 %50 *%16* %16
892 // Corresponds to i[*0*].f
893 // The index %16 used for 0 *can* be replaced
894 auto replacement4 = TransformationReplaceIdWithSynonym(
895 MakeIdUseDescriptor(
896 16, MakeInstructionDescriptor(52, SpvOpAccessChain, 0), 1),
897 100);
898 ASSERT_TRUE(replacement4.IsApplicable(context.get(), transformation_context));
899 ApplyAndCheckFreshIds(replacement4, context.get(), &transformation_context);
900 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
901 kConsoleMessageConsumer));
902
903 // %52 = OpAccessChain %23 %50 %16 *%16*
904 // Corresponds to i[0].*f*
905 // The index %16 used for f cannot be replaced
906 auto replacement5 = TransformationReplaceIdWithSynonym(
907 MakeIdUseDescriptor(
908 16, MakeInstructionDescriptor(52, SpvOpAccessChain, 0), 2),
909 100);
910 ASSERT_FALSE(
911 replacement5.IsApplicable(context.get(), transformation_context));
912
913 // %53 = OpAccessChain %19 %50 %21 %21 *%16* %16
914 // Corresponds to i[1].g.*a*[0]
915 // The index %16 used for a cannot be replaced
916 auto replacement6 = TransformationReplaceIdWithSynonym(
917 MakeIdUseDescriptor(
918 16, MakeInstructionDescriptor(53, SpvOpAccessChain, 0), 3),
919 100);
920 ASSERT_FALSE(
921 replacement6.IsApplicable(context.get(), transformation_context));
922
923 // %53 = OpAccessChain %19 %50 %21 %21 %16 *%16*
924 // Corresponds to i[1].g.a[*0*]
925 // The index %16 used for 0 *can* be replaced
926 auto replacement7 = TransformationReplaceIdWithSynonym(
927 MakeIdUseDescriptor(
928 16, MakeInstructionDescriptor(53, SpvOpAccessChain, 0), 4),
929 100);
930 ASSERT_TRUE(replacement7.IsApplicable(context.get(), transformation_context));
931 ApplyAndCheckFreshIds(replacement7, context.get(), &transformation_context);
932 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
933 kConsoleMessageConsumer));
934
935 // Replacements of the form %21 -> %101
936
937 // %24 = OpAccessChain %23 %15 *%21* %8
938 // Corresponds to d.*b*[3]
939 // The index %24 used for b cannot be replaced
940 auto replacement8 = TransformationReplaceIdWithSynonym(
941 MakeIdUseDescriptor(
942 21, MakeInstructionDescriptor(24, SpvOpAccessChain, 0), 1),
943 101);
944 ASSERT_FALSE(
945 replacement8.IsApplicable(context.get(), transformation_context));
946
947 // %41 = OpAccessChain %19 %37 *%21* %16 %21
948 // Corresponds to h.*g*.a[1]
949 // The index %24 used for g cannot be replaced
950 auto replacement9 = TransformationReplaceIdWithSynonym(
951 MakeIdUseDescriptor(
952 21, MakeInstructionDescriptor(41, SpvOpAccessChain, 0), 1),
953 101);
954 ASSERT_FALSE(
955 replacement9.IsApplicable(context.get(), transformation_context));
956
957 // %41 = OpAccessChain %19 %37 %21 %16 *%21*
958 // Corresponds to h.g.a[*1*]
959 // The index %24 used for 1 *can* be replaced
960 auto replacement10 = TransformationReplaceIdWithSynonym(
961 MakeIdUseDescriptor(
962 21, MakeInstructionDescriptor(41, SpvOpAccessChain, 0), 3),
963 101);
964 ASSERT_TRUE(
965 replacement10.IsApplicable(context.get(), transformation_context));
966 ApplyAndCheckFreshIds(replacement10, context.get(), &transformation_context);
967 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
968 kConsoleMessageConsumer));
969
970 // %44 = OpAccessChain %23 %37 *%21* %21 %43
971 // Corresponds to h.*g*.b[0]
972 // The index %24 used for g cannot be replaced
973 auto replacement11 = TransformationReplaceIdWithSynonym(
974 MakeIdUseDescriptor(
975 21, MakeInstructionDescriptor(44, SpvOpAccessChain, 0), 1),
976 101);
977 ASSERT_FALSE(
978 replacement11.IsApplicable(context.get(), transformation_context));
979
980 // %44 = OpAccessChain %23 %37 %21 *%21* %43
981 // Corresponds to h.g.*b*[0]
982 // The index %24 used for b cannot be replaced
983 auto replacement12 = TransformationReplaceIdWithSynonym(
984 MakeIdUseDescriptor(
985 21, MakeInstructionDescriptor(44, SpvOpAccessChain, 0), 2),
986 101);
987 ASSERT_FALSE(
988 replacement12.IsApplicable(context.get(), transformation_context));
989
990 // %46 = OpAccessChain %26 %37 *%21* %17
991 // Corresponds to h.*g*.c
992 // The index %24 used for g cannot be replaced
993 auto replacement13 = TransformationReplaceIdWithSynonym(
994 MakeIdUseDescriptor(
995 21, MakeInstructionDescriptor(46, SpvOpAccessChain, 0), 1),
996 101);
997 ASSERT_FALSE(
998 replacement13.IsApplicable(context.get(), transformation_context));
999
1000 // %53 = OpAccessChain %19 %50 *%21* %21 %16 %16
1001 // Corresponds to i[*1*].g.a[0]
1002 // The index %24 used for 1 *can* be replaced
1003 auto replacement14 = TransformationReplaceIdWithSynonym(
1004 MakeIdUseDescriptor(
1005 21, MakeInstructionDescriptor(53, SpvOpAccessChain, 0), 1),
1006 101);
1007 ASSERT_TRUE(
1008 replacement14.IsApplicable(context.get(), transformation_context));
1009 ApplyAndCheckFreshIds(replacement14, context.get(), &transformation_context);
1010 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1011 kConsoleMessageConsumer));
1012
1013 // %53 = OpAccessChain %19 %50 %21 *%21* %16 %16
1014 // Corresponds to i[1].*g*.a[0]
1015 // The index %24 used for g cannot be replaced
1016 auto replacement15 = TransformationReplaceIdWithSynonym(
1017 MakeIdUseDescriptor(
1018 21, MakeInstructionDescriptor(53, SpvOpAccessChain, 0), 2),
1019 101);
1020 ASSERT_FALSE(
1021 replacement15.IsApplicable(context.get(), transformation_context));
1022
1023 // %56 = OpAccessChain %23 %50 %17 *%21* %21 %55
1024 // Corresponds to i[2].*g*.b[1]
1025 // The index %24 used for g cannot be replaced
1026 auto replacement16 = TransformationReplaceIdWithSynonym(
1027 MakeIdUseDescriptor(
1028 21, MakeInstructionDescriptor(56, SpvOpAccessChain, 0), 2),
1029 101);
1030 ASSERT_FALSE(
1031 replacement16.IsApplicable(context.get(), transformation_context));
1032
1033 // %56 = OpAccessChain %23 %50 %17 %21 *%21* %55
1034 // Corresponds to i[2].g.*b*[1]
1035 // The index %24 used for b cannot be replaced
1036 auto replacement17 = TransformationReplaceIdWithSynonym(
1037 MakeIdUseDescriptor(
1038 21, MakeInstructionDescriptor(56, SpvOpAccessChain, 0), 3),
1039 101);
1040 ASSERT_FALSE(
1041 replacement17.IsApplicable(context.get(), transformation_context));
1042
1043 // %58 = OpInBoundsAccessChain %26 %50 %57 *%21* %17
1044 // Corresponds to i[3].*g*.c
1045 // The index %24 used for g cannot be replaced
1046 auto replacement18 = TransformationReplaceIdWithSynonym(
1047 MakeIdUseDescriptor(
1048 21, MakeInstructionDescriptor(58, SpvOpInBoundsAccessChain, 0), 2),
1049 101);
1050 ASSERT_FALSE(
1051 replacement18.IsApplicable(context.get(), transformation_context));
1052
1053 // Replacements of the form %17 -> %102
1054
1055 // %20 = OpAccessChain %19 %15 %16 %17
1056 // Corresponds to d.a[*2*]
1057 // The index %17 used for 2 *can* be replaced
1058 auto replacement19 = TransformationReplaceIdWithSynonym(
1059 MakeIdUseDescriptor(
1060 17, MakeInstructionDescriptor(20, SpvOpAccessChain, 0), 2),
1061 102);
1062 ASSERT_TRUE(
1063 replacement19.IsApplicable(context.get(), transformation_context));
1064 ApplyAndCheckFreshIds(replacement19, context.get(), &transformation_context);
1065 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1066 kConsoleMessageConsumer));
1067
1068 // %27 = OpAccessChain %26 %15 %17
1069 // Corresponds to d.c
1070 // The index %17 used for c cannot be replaced
1071 auto replacement20 = TransformationReplaceIdWithSynonym(
1072 MakeIdUseDescriptor(
1073 17, MakeInstructionDescriptor(27, SpvOpAccessChain, 0), 1),
1074 102);
1075 ASSERT_FALSE(
1076 replacement20.IsApplicable(context.get(), transformation_context));
1077
1078 // %46 = OpAccessChain %26 %37 %21 %17
1079 // Corresponds to h.g.*c*
1080 // The index %17 used for c cannot be replaced
1081 auto replacement21 = TransformationReplaceIdWithSynonym(
1082 MakeIdUseDescriptor(
1083 17, MakeInstructionDescriptor(46, SpvOpAccessChain, 0), 2),
1084 102);
1085 ASSERT_FALSE(
1086 replacement21.IsApplicable(context.get(), transformation_context));
1087
1088 // %56 = OpAccessChain %23 %50 %17 %21 %21 %55
1089 // Corresponds to i[*2*].g.b[1]
1090 // The index %17 used for 2 *can* be replaced
1091 auto replacement22 = TransformationReplaceIdWithSynonym(
1092 MakeIdUseDescriptor(
1093 17, MakeInstructionDescriptor(56, SpvOpAccessChain, 0), 1),
1094 102);
1095 ASSERT_TRUE(
1096 replacement22.IsApplicable(context.get(), transformation_context));
1097 ApplyAndCheckFreshIds(replacement22, context.get(), &transformation_context);
1098 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1099 kConsoleMessageConsumer));
1100
1101 // %58 = OpInBoundsAccessChain %26 %50 %57 %21 %17
1102 // Corresponds to i[3].g.*c*
1103 // The index %17 used for c cannot be replaced
1104 auto replacement23 = TransformationReplaceIdWithSynonym(
1105 MakeIdUseDescriptor(
1106 17, MakeInstructionDescriptor(58, SpvOpInBoundsAccessChain, 0), 3),
1107 102);
1108 ASSERT_FALSE(
1109 replacement23.IsApplicable(context.get(), transformation_context));
1110
1111 // Replacements of the form %57 -> %103
1112
1113 // %58 = OpInBoundsAccessChain %26 %50 *%57* %21 %17
1114 // Corresponds to i[*3*].g.c
1115 // The index %57 used for 3 *can* be replaced
1116 auto replacement24 = TransformationReplaceIdWithSynonym(
1117 MakeIdUseDescriptor(
1118 57, MakeInstructionDescriptor(58, SpvOpInBoundsAccessChain, 0), 1),
1119 103);
1120 ASSERT_TRUE(
1121 replacement24.IsApplicable(context.get(), transformation_context));
1122 ApplyAndCheckFreshIds(replacement24, context.get(), &transformation_context);
1123 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1124 kConsoleMessageConsumer));
1125
1126 // Replacements of the form %32 -> %106
1127
1128 // %34 = OpAccessChain %23 %31 *%32*
1129 // Corresponds to e[*17*]
1130 // The index %32 used for 17 *can* be replaced
1131 auto replacement25 = TransformationReplaceIdWithSynonym(
1132 MakeIdUseDescriptor(
1133 32, MakeInstructionDescriptor(34, SpvOpAccessChain, 0), 1),
1134 106);
1135 ASSERT_TRUE(
1136 replacement25.IsApplicable(context.get(), transformation_context));
1137 ApplyAndCheckFreshIds(replacement25, context.get(), &transformation_context);
1138 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1139 kConsoleMessageConsumer));
1140
1141 // Replacements of the form %43 -> %107
1142
1143 // %44 = OpAccessChain %23 %37 %21 %21 *%43*
1144 // Corresponds to h.g.b[*0*]
1145 // The index %43 used for 0 *can* be replaced
1146 auto replacement26 = TransformationReplaceIdWithSynonym(
1147 MakeIdUseDescriptor(
1148 43, MakeInstructionDescriptor(44, SpvOpAccessChain, 0), 3),
1149 107);
1150 ASSERT_TRUE(
1151 replacement26.IsApplicable(context.get(), transformation_context));
1152 ApplyAndCheckFreshIds(replacement26, context.get(), &transformation_context);
1153 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1154 kConsoleMessageConsumer));
1155
1156 // Replacements of the form %55 -> %108
1157
1158 // %56 = OpAccessChain %23 %50 %17 %21 %21 *%55*
1159 // Corresponds to i[2].g.b[*1*]
1160 // The index %55 used for 1 *can* be replaced
1161 auto replacement27 = TransformationReplaceIdWithSynonym(
1162 MakeIdUseDescriptor(
1163 55, MakeInstructionDescriptor(56, SpvOpAccessChain, 0), 4),
1164 108);
1165 ASSERT_TRUE(
1166 replacement27.IsApplicable(context.get(), transformation_context));
1167 ApplyAndCheckFreshIds(replacement27, context.get(), &transformation_context);
1168 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1169 kConsoleMessageConsumer));
1170
1171 // Replacements of the form %8 -> %109
1172
1173 // %24 = OpAccessChain %23 %15 %21 *%8*
1174 // Corresponds to d.b[*3*]
1175 // The index %8 used for 3 *can* be replaced
1176 auto replacement28 = TransformationReplaceIdWithSynonym(
1177 MakeIdUseDescriptor(8, MakeInstructionDescriptor(24, SpvOpAccessChain, 0),
1178 2),
1179 109);
1180 ASSERT_TRUE(
1181 replacement28.IsApplicable(context.get(), transformation_context));
1182 ApplyAndCheckFreshIds(replacement28, context.get(), &transformation_context);
1183 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1184 kConsoleMessageConsumer));
1185
1186 const std::string after_transformation = R"(
1187 OpCapability Shader
1188 %1 = OpExtInstImport "GLSL.std.450"
1189 OpMemoryModel Logical GLSL450
1190 OpEntryPoint Fragment %4 "main"
1191 OpExecutionMode %4 OriginUpperLeft
1192 OpSource ESSL 310
1193 OpName %4 "main"
1194 OpName %13 "S"
1195 OpMemberName %13 0 "a"
1196 OpMemberName %13 1 "b"
1197 OpMemberName %13 2 "c"
1198 OpName %15 "d"
1199 OpName %31 "e"
1200 OpName %35 "T"
1201 OpMemberName %35 0 "f"
1202 OpMemberName %35 1 "g"
1203 OpName %37 "h"
1204 OpName %50 "i"
1205 %2 = OpTypeVoid
1206 %3 = OpTypeFunction %2
1207 %6 = OpTypeInt 32 1
1208 %7 = OpTypeInt 32 0
1209 %8 = OpConstant %7 3
1210 %9 = OpTypeArray %6 %8
1211 %10 = OpTypeFloat 32
1212 %11 = OpTypeVector %10 4
1213 %12 = OpTypeBool
1214 %13 = OpTypeStruct %9 %11 %12
1215 %14 = OpTypePointer Private %13
1216 %15 = OpVariable %14 Private
1217 %16 = OpConstant %6 0
1218 %17 = OpConstant %6 2
1219 %18 = OpConstant %6 10
1220 %19 = OpTypePointer Private %6
1221 %21 = OpConstant %6 1
1222 %22 = OpConstant %10 11
1223 %23 = OpTypePointer Private %10
1224 %25 = OpConstantFalse %12
1225 %26 = OpTypePointer Private %12
1226 %28 = OpConstant %7 20
1227 %29 = OpTypeArray %10 %28
1228 %30 = OpTypePointer Private %29
1229 %31 = OpVariable %30 Private
1230 %32 = OpConstant %6 17
1231 %33 = OpConstant %10 12
1232 %35 = OpTypeStruct %10 %13
1233 %36 = OpTypePointer Private %35
1234 %37 = OpVariable %36 Private
1235 %38 = OpConstant %10 13
1236 %40 = OpConstant %6 14
1237 %42 = OpConstant %10 15
1238 %43 = OpConstant %7 0
1239 %45 = OpConstantTrue %12
1240 %47 = OpConstant %7 4
1241 %48 = OpTypeArray %35 %47
1242 %49 = OpTypePointer Private %48
1243 %50 = OpVariable %49 Private
1244 %51 = OpConstant %10 16
1245 %54 = OpConstant %10 18
1246 %55 = OpConstant %7 1
1247 %57 = OpConstant %6 3
1248 %4 = OpFunction %2 None %3
1249 %5 = OpLabel
1250
1251 %100 = OpCopyObject %6 %16 ; 0
1252 %101 = OpCopyObject %6 %21 ; 1
1253 %102 = OpCopyObject %6 %17 ; 2
1254 %103 = OpCopyObject %6 %57 ; 3
1255 %104 = OpCopyObject %6 %18 ; 10
1256 %105 = OpCopyObject %6 %40 ; 14
1257 %106 = OpCopyObject %6 %32 ; 17
1258 %107 = OpCopyObject %7 %43 ; 0
1259 %108 = OpCopyObject %7 %55 ; 1
1260 %109 = OpCopyObject %7 %8 ; 3
1261 %110 = OpCopyObject %7 %47 ; 4
1262 %111 = OpCopyObject %7 %28 ; 20
1263 %112 = OpCopyObject %12 %45 ; true
1264
1265 %20 = OpAccessChain %19 %15 %16 %102
1266 OpStore %20 %18
1267 %24 = OpAccessChain %23 %15 %21 %109
1268 OpStore %24 %22
1269 %27 = OpAccessChain %26 %15 %17
1270 OpStore %27 %25
1271 %34 = OpAccessChain %23 %31 %106
1272 OpStore %34 %33
1273 %39 = OpAccessChain %23 %37 %16
1274 OpStore %39 %38
1275 %41 = OpAccessChain %19 %37 %21 %16 %101
1276 OpStore %41 %40
1277 %44 = OpAccessChain %23 %37 %21 %21 %107
1278 OpStore %44 %42
1279 %46 = OpAccessChain %26 %37 %21 %17
1280 OpStore %46 %45
1281 %52 = OpAccessChain %23 %50 %100 %16
1282 OpStore %52 %51
1283 %53 = OpAccessChain %19 %50 %101 %21 %16 %100
1284 OpStore %53 %32
1285 %56 = OpAccessChain %23 %50 %102 %21 %21 %108
1286 OpStore %56 %54
1287 %58 = OpInBoundsAccessChain %26 %50 %103 %21 %17
1288 OpStore %58 %45
1289 OpReturn
1290 OpFunctionEnd
1291 )";
1292
1293 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
1294 }
1295
1296 TEST(TransformationReplaceIdWithSynonymTest, RuntimeArrayTest) {
1297 // This checks that OpRuntimeArray is correctly handled.
1298 const std::string shader = R"(
1299 OpCapability Shader
1300 %1 = OpExtInstImport "GLSL.std.450"
1301 OpMemoryModel Logical GLSL450
1302 OpEntryPoint Fragment %4 "main"
1303 OpExecutionMode %4 OriginUpperLeft
1304 OpSource ESSL 310
1305 OpDecorate %8 ArrayStride 8
1306 OpMemberDecorate %9 0 Offset 0
1307 OpDecorate %9 BufferBlock
1308 OpDecorate %11 DescriptorSet 0
1309 OpDecorate %11 Binding 0
1310 %2 = OpTypeVoid
1311 %3 = OpTypeFunction %2
1312 %6 = OpTypeInt 32 1
1313 %7 = OpTypeVector %6 2
1314 %8 = OpTypeRuntimeArray %7
1315 %9 = OpTypeStruct %8
1316 %10 = OpTypePointer Uniform %9
1317 %11 = OpVariable %10 Uniform
1318 %12 = OpConstant %6 0
1319 %13 = OpTypeInt 32 0
1320 %14 = OpConstant %13 0
1321 %15 = OpTypePointer Uniform %6
1322 %4 = OpFunction %2 None %3
1323 %5 = OpLabel
1324 %50 = OpCopyObject %6 %12
1325 %51 = OpCopyObject %13 %14
1326 %16 = OpAccessChain %15 %11 %12 %12 %14
1327 OpStore %16 %12
1328 OpReturn
1329 OpFunctionEnd
1330 )";
1331
1332 const auto env = SPV_ENV_UNIVERSAL_1_3;
1333 const auto consumer = nullptr;
1334 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1335 spvtools::ValidatorOptions validator_options;
1336 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1337 kConsoleMessageConsumer));
1338 TransformationContext transformation_context(
1339 MakeUnique<FactManager>(context.get()), validator_options);
1340 // Add synonym fact relating %50 and %12.
1341 transformation_context.GetFactManager()->MaybeAddFact(
1342 MakeSynonymFact(50, 12));
1343 // Add synonym fact relating %51 and %14.
1344 transformation_context.GetFactManager()->MaybeAddFact(
1345 MakeSynonymFact(51, 14));
1346
1347 // Not legal because the index being replaced is a struct index.
1348 ASSERT_FALSE(
1349 TransformationReplaceIdWithSynonym(
1350 MakeIdUseDescriptor(
1351 12, MakeInstructionDescriptor(16, SpvOpAccessChain, 0), 1),
1352 50)
1353 .IsApplicable(context.get(), transformation_context));
1354
1355 // Fine to replace an index into a runtime array.
1356 auto replacement1 = TransformationReplaceIdWithSynonym(
1357 MakeIdUseDescriptor(
1358 12, MakeInstructionDescriptor(16, SpvOpAccessChain, 0), 2),
1359 50);
1360 ASSERT_TRUE(replacement1.IsApplicable(context.get(), transformation_context));
1361 ApplyAndCheckFreshIds(replacement1, context.get(), &transformation_context);
1362
1363 // Fine to replace an index into a vector inside the runtime array.
1364 auto replacement2 = TransformationReplaceIdWithSynonym(
1365 MakeIdUseDescriptor(
1366 14, MakeInstructionDescriptor(16, SpvOpAccessChain, 0), 3),
1367 51);
1368 ASSERT_TRUE(replacement2.IsApplicable(context.get(), transformation_context));
1369 ApplyAndCheckFreshIds(replacement2, context.get(), &transformation_context);
1370
1371 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1372 kConsoleMessageConsumer));
1373
1374 const std::string after_transformation = R"(
1375 OpCapability Shader
1376 %1 = OpExtInstImport "GLSL.std.450"
1377 OpMemoryModel Logical GLSL450
1378 OpEntryPoint Fragment %4 "main"
1379 OpExecutionMode %4 OriginUpperLeft
1380 OpSource ESSL 310
1381 OpDecorate %8 ArrayStride 8
1382 OpMemberDecorate %9 0 Offset 0
1383 OpDecorate %9 BufferBlock
1384 OpDecorate %11 DescriptorSet 0
1385 OpDecorate %11 Binding 0
1386 %2 = OpTypeVoid
1387 %3 = OpTypeFunction %2
1388 %6 = OpTypeInt 32 1
1389 %7 = OpTypeVector %6 2
1390 %8 = OpTypeRuntimeArray %7
1391 %9 = OpTypeStruct %8
1392 %10 = OpTypePointer Uniform %9
1393 %11 = OpVariable %10 Uniform
1394 %12 = OpConstant %6 0
1395 %13 = OpTypeInt 32 0
1396 %14 = OpConstant %13 0
1397 %15 = OpTypePointer Uniform %6
1398 %4 = OpFunction %2 None %3
1399 %5 = OpLabel
1400 %50 = OpCopyObject %6 %12
1401 %51 = OpCopyObject %13 %14
1402 %16 = OpAccessChain %15 %11 %12 %50 %51
1403 OpStore %16 %12
1404 OpReturn
1405 OpFunctionEnd
1406 )";
1407
1408 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
1409 }
1410
1411 TEST(TransformationReplaceIdWithSynonymTest,
1412 DoNotReplaceSampleParameterOfOpImageTexelPointer) {
1413 const std::string shader = R"(
1414 OpCapability Shader
1415 %1 = OpExtInstImport "GLSL.std.450"
1416 OpMemoryModel Logical GLSL450
1417 OpEntryPoint Fragment %2 "main" %3
1418 OpExecutionMode %2 OriginUpperLeft
1419 OpSource ESSL 310
1420 %4 = OpTypeVoid
1421 %5 = OpTypeFunction %4
1422 %6 = OpTypeInt 32 1
1423 %7 = OpTypePointer Function %6
1424 %8 = OpConstant %6 2
1425 %9 = OpConstant %6 0
1426 %10 = OpConstant %6 10
1427 %11 = OpTypeBool
1428 %12 = OpConstant %6 1
1429 %13 = OpTypeFloat 32
1430 %14 = OpTypePointer Image %13
1431 %15 = OpTypeImage %13 2D 0 0 0 0 Rgba8
1432 %16 = OpTypePointer Private %15
1433 %3 = OpVariable %16 Private
1434 %17 = OpTypeVector %6 2
1435 %18 = OpConstantComposite %17 %9 %9
1436 %2 = OpFunction %4 None %5
1437 %19 = OpLabel
1438 %100 = OpCopyObject %6 %9
1439 %20 = OpImageTexelPointer %14 %3 %18 %9
1440 OpReturn
1441 OpFunctionEnd
1442 )";
1443
1444 const auto env = SPV_ENV_UNIVERSAL_1_5;
1445 const auto consumer = nullptr;
1446 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1447 spvtools::ValidatorOptions validator_options;
1448 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1449 kConsoleMessageConsumer));
1450 TransformationContext transformation_context(
1451 MakeUnique<FactManager>(context.get()), validator_options);
1452 // Add synonym fact relating %100 and %9.
1453 transformation_context.GetFactManager()->MaybeAddFact(
1454 MakeSynonymFact(100, 9));
1455
1456 // Not legal the Sample argument of OpImageTexelPointer needs to be a zero
1457 // constant.
1458 ASSERT_FALSE(
1459 TransformationReplaceIdWithSynonym(
1460 MakeIdUseDescriptor(
1461 9, MakeInstructionDescriptor(20, SpvOpImageTexelPointer, 0), 2),
1462 100)
1463 .IsApplicable(context.get(), transformation_context));
1464 }
1465
1466 TEST(TransformationReplaceIdWithSynonymTest, EquivalentIntegerConstants) {
1467 // This checks that replacing an integer constant with an equivalent one with
1468 // different signedness is allowed only when valid.
1469 const std::string shader = R"(
1470 OpCapability Shader
1471 %1 = OpExtInstImport "GLSL.std.450"
1472 OpMemoryModel Logical GLSL450
1473 OpEntryPoint Fragment %2 "main"
1474 OpExecutionMode %2 OriginUpperLeft
1475 OpSource ESSL 310
1476 OpName %2 "main"
1477 OpName %3 "a"
1478 OpDecorate %3 RelaxedPrecision
1479 %4 = OpTypeVoid
1480 %5 = OpTypeBool
1481 %6 = OpConstantTrue %5
1482 %7 = OpTypeFunction %4
1483 %8 = OpTypeInt 32 1
1484 %9 = OpTypePointer Function %8
1485 %10 = OpConstant %8 1
1486 %11 = OpTypeInt 32 0
1487 %12 = OpTypePointer Function %11
1488 %13 = OpConstant %11 1
1489 %2 = OpFunction %4 None %7
1490 %14 = OpLabel
1491 %3 = OpVariable %9 Function
1492 %15 = OpSNegate %8 %10
1493 %16 = OpIAdd %8 %10 %10
1494 %17 = OpSDiv %8 %10 %10
1495 %18 = OpUDiv %11 %13 %13
1496 %19 = OpBitwiseAnd %8 %10 %10
1497 %20 = OpSelect %8 %6 %10 %17
1498 %21 = OpIEqual %5 %10 %10
1499 OpStore %3 %10
1500 OpReturn
1501 OpFunctionEnd
1502 )";
1503
1504 const auto env = SPV_ENV_UNIVERSAL_1_3;
1505 const auto consumer = nullptr;
1506 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1507 spvtools::ValidatorOptions validator_options;
1508 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1509 kConsoleMessageConsumer));
1510 TransformationContext transformation_context(
1511 MakeUnique<FactManager>(context.get()), validator_options);
1512 // Add synonym fact relating %10 and %13 (equivalent integer constant with
1513 // different signedness).
1514 transformation_context.GetFactManager()->MaybeAddFact(
1515 MakeSynonymFact(10, 13));
1516
1517 // Legal because OpSNegate always considers the integer as signed
1518 auto replacement1 = TransformationReplaceIdWithSynonym(
1519 MakeIdUseDescriptor(10, MakeInstructionDescriptor(15, SpvOpSNegate, 0),
1520 0),
1521 13);
1522 ASSERT_TRUE(replacement1.IsApplicable(context.get(), transformation_context));
1523 ApplyAndCheckFreshIds(replacement1, context.get(), &transformation_context);
1524
1525 // Legal because OpIAdd does not care about the signedness of the operands
1526 auto replacement2 = TransformationReplaceIdWithSynonym(
1527 MakeIdUseDescriptor(10, MakeInstructionDescriptor(16, SpvOpIAdd, 0), 0),
1528 13);
1529 ASSERT_TRUE(replacement2.IsApplicable(context.get(), transformation_context));
1530 ApplyAndCheckFreshIds(replacement2, context.get(), &transformation_context);
1531
1532 // Legal because OpSDiv does not care about the signedness of the operands
1533 auto replacement3 = TransformationReplaceIdWithSynonym(
1534 MakeIdUseDescriptor(10, MakeInstructionDescriptor(17, SpvOpSDiv, 0), 0),
1535 13);
1536 ASSERT_TRUE(replacement3.IsApplicable(context.get(), transformation_context));
1537 ApplyAndCheckFreshIds(replacement3, context.get(), &transformation_context);
1538
1539 // Not legal because OpUDiv requires unsigned integers
1540 ASSERT_FALSE(TransformationReplaceIdWithSynonym(
1541 MakeIdUseDescriptor(
1542 13, MakeInstructionDescriptor(18, SpvOpUDiv, 0), 0),
1543 10)
1544 .IsApplicable(context.get(), transformation_context));
1545
1546 // Legal because OpSDiv does not care about the signedness of the operands
1547 auto replacement4 = TransformationReplaceIdWithSynonym(
1548 MakeIdUseDescriptor(10, MakeInstructionDescriptor(19, SpvOpBitwiseAnd, 0),
1549 0),
1550 13);
1551 ASSERT_TRUE(replacement4.IsApplicable(context.get(), transformation_context));
1552 ApplyAndCheckFreshIds(replacement4, context.get(), &transformation_context);
1553
1554 // Not legal because OpSelect requires both operands to have the same type as
1555 // the result type
1556 ASSERT_FALSE(TransformationReplaceIdWithSynonym(
1557 MakeIdUseDescriptor(
1558 10, MakeInstructionDescriptor(20, SpvOpUDiv, 0), 1),
1559 13)
1560 .IsApplicable(context.get(), transformation_context));
1561
1562 // Not legal because OpStore requires the object to match the type pointed
1563 // to by the pointer.
1564 ASSERT_FALSE(TransformationReplaceIdWithSynonym(
1565 MakeIdUseDescriptor(
1566 10, MakeInstructionDescriptor(21, SpvOpStore, 0), 1),
1567 13)
1568 .IsApplicable(context.get(), transformation_context));
1569
1570 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1571 kConsoleMessageConsumer));
1572
1573 const std::string after_transformation = R"(
1574 OpCapability Shader
1575 %1 = OpExtInstImport "GLSL.std.450"
1576 OpMemoryModel Logical GLSL450
1577 OpEntryPoint Fragment %2 "main"
1578 OpExecutionMode %2 OriginUpperLeft
1579 OpSource ESSL 310
1580 OpName %2 "main"
1581 OpName %3 "a"
1582 OpDecorate %3 RelaxedPrecision
1583 %4 = OpTypeVoid
1584 %5 = OpTypeBool
1585 %6 = OpConstantTrue %5
1586 %7 = OpTypeFunction %4
1587 %8 = OpTypeInt 32 1
1588 %9 = OpTypePointer Function %8
1589 %10 = OpConstant %8 1
1590 %11 = OpTypeInt 32 0
1591 %12 = OpTypePointer Function %11
1592 %13 = OpConstant %11 1
1593 %2 = OpFunction %4 None %7
1594 %14 = OpLabel
1595 %3 = OpVariable %9 Function
1596 %15 = OpSNegate %8 %13
1597 %16 = OpIAdd %8 %13 %10
1598 %17 = OpSDiv %8 %13 %10
1599 %18 = OpUDiv %11 %13 %13
1600 %19 = OpBitwiseAnd %8 %13 %10
1601 %20 = OpSelect %8 %6 %10 %17
1602 %21 = OpIEqual %5 %10 %10
1603 OpStore %3 %10
1604 OpReturn
1605 OpFunctionEnd
1606 )";
1607
1608 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
1609 }
1610
1611 TEST(TransformationReplaceIdWithSynonymTest, EquivalentIntegerVectorConstants) {
1612 // This checks that replacing an integer constant with an equivalent one with
1613 // different signedness is allowed only when valid.
1614 const std::string shader = R"(
1615 OpCapability Shader
1616 %1 = OpExtInstImport "GLSL.std.450"
1617 OpMemoryModel Logical GLSL450
1618 OpEntryPoint Fragment %2 "main"
1619 OpExecutionMode %2 OriginUpperLeft
1620 OpSource ESSL 310
1621 OpName %2 "main"
1622 OpName %3 "a"
1623 OpDecorate %3 RelaxedPrecision
1624 OpDecorate %4 RelaxedPrecision
1625 %5 = OpTypeVoid
1626 %6 = OpTypeFunction %5
1627 %7 = OpTypeInt 32 1
1628 %8 = OpTypeInt 32 0
1629 %9 = OpTypeVector %7 4
1630 %10 = OpTypeVector %8 4
1631 %11 = OpTypePointer Function %9
1632 %12 = OpConstant %7 1
1633 %13 = OpConstant %8 1
1634 %14 = OpConstantComposite %9 %12 %12 %12 %12
1635 %15 = OpConstantComposite %10 %13 %13 %13 %13
1636 %16 = OpTypePointer Function %7
1637 %2 = OpFunction %5 None %6
1638 %17 = OpLabel
1639 %3 = OpVariable %11 Function
1640 %18 = OpIAdd %9 %14 %14
1641 OpStore %3 %14
1642 %19 = OpAccessChain %16 %3 %13
1643 %4 = OpLoad %7 %19
1644 OpReturn
1645 OpFunctionEnd
1646 )";
1647
1648 const auto env = SPV_ENV_UNIVERSAL_1_3;
1649 const auto consumer = nullptr;
1650 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1651 spvtools::ValidatorOptions validator_options;
1652 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1653 kConsoleMessageConsumer));
1654 TransformationContext transformation_context(
1655 MakeUnique<FactManager>(context.get()), validator_options);
1656 // Add synonym fact relating %10 and %13 (equivalent integer vectors with
1657 // different signedness).
1658 transformation_context.GetFactManager()->MaybeAddFact(
1659 MakeSynonymFact(14, 15));
1660
1661 // Legal because OpIAdd does not consider the signedness of the operands
1662 auto replacement1 = TransformationReplaceIdWithSynonym(
1663 MakeIdUseDescriptor(14, MakeInstructionDescriptor(18, SpvOpIAdd, 0), 0),
1664 15);
1665 ASSERT_TRUE(replacement1.IsApplicable(context.get(), transformation_context));
1666 ApplyAndCheckFreshIds(replacement1, context.get(), &transformation_context);
1667
1668 // Not legal because OpStore requires the object to match the type pointed
1669 // to by the pointer.
1670 ASSERT_FALSE(TransformationReplaceIdWithSynonym(
1671 MakeIdUseDescriptor(
1672 14, MakeInstructionDescriptor(18, SpvOpStore, 0), 1),
1673 15)
1674 .IsApplicable(context.get(), transformation_context));
1675
1676 // Add synonym fact relating %12 and %13 (equivalent integer constants with
1677 // different signedness).
1678 transformation_context.GetFactManager()->MaybeAddFact(
1679 MakeSynonymFact(12, 13));
1680
1681 // Legal because the indices of OpAccessChain are always treated as signed
1682 auto replacement2 = TransformationReplaceIdWithSynonym(
1683 MakeIdUseDescriptor(
1684 13, MakeInstructionDescriptor(19, SpvOpAccessChain, 0), 1),
1685 12);
1686 ASSERT_TRUE(replacement2.IsApplicable(context.get(), transformation_context));
1687 ApplyAndCheckFreshIds(replacement2, context.get(), &transformation_context);
1688
1689 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1690 kConsoleMessageConsumer));
1691
1692 const std::string after_transformation = R"(
1693 OpCapability Shader
1694 %1 = OpExtInstImport "GLSL.std.450"
1695 OpMemoryModel Logical GLSL450
1696 OpEntryPoint Fragment %2 "main"
1697 OpExecutionMode %2 OriginUpperLeft
1698 OpSource ESSL 310
1699 OpName %2 "main"
1700 OpName %3 "a"
1701 OpDecorate %3 RelaxedPrecision
1702 OpDecorate %4 RelaxedPrecision
1703 %5 = OpTypeVoid
1704 %6 = OpTypeFunction %5
1705 %7 = OpTypeInt 32 1
1706 %8 = OpTypeInt 32 0
1707 %9 = OpTypeVector %7 4
1708 %10 = OpTypeVector %8 4
1709 %11 = OpTypePointer Function %9
1710 %12 = OpConstant %7 1
1711 %13 = OpConstant %8 1
1712 %14 = OpConstantComposite %9 %12 %12 %12 %12
1713 %15 = OpConstantComposite %10 %13 %13 %13 %13
1714 %16 = OpTypePointer Function %7
1715 %2 = OpFunction %5 None %6
1716 %17 = OpLabel
1717 %3 = OpVariable %11 Function
1718 %18 = OpIAdd %9 %15 %14
1719 OpStore %3 %14
1720 %19 = OpAccessChain %16 %3 %12
1721 %4 = OpLoad %7 %19
1722 OpReturn
1723 OpFunctionEnd
1724 )";
1725
1726 ASSERT_TRUE(IsEqual(env, after_transformation, context.get()));
1727 }
1728
1729 TEST(TransformationReplaceIdWithSynonymTest, IncompatibleTypes) {
1730 const std::string shader = R"(
1731 OpCapability Shader
1732 %1 = OpExtInstImport "GLSL.std.450"
1733 OpMemoryModel Logical GLSL450
1734 OpEntryPoint Fragment %2 "main"
1735 OpExecutionMode %2 OriginUpperLeft
1736 OpSource ESSL 310
1737 %5 = OpTypeVoid
1738 %6 = OpTypeFunction %5
1739 %7 = OpTypeInt 32 1
1740 %8 = OpTypeInt 32 0
1741 %9 = OpTypeFloat 32
1742 %12 = OpConstant %7 1
1743 %13 = OpConstant %8 1
1744 %10 = OpConstant %9 1
1745 %2 = OpFunction %5 None %6
1746 %17 = OpLabel
1747 %18 = OpIAdd %7 %12 %13
1748 %19 = OpFAdd %9 %10 %10
1749 OpReturn
1750 OpFunctionEnd
1751 )";
1752
1753 const auto env = SPV_ENV_UNIVERSAL_1_3;
1754 const auto consumer = nullptr;
1755 const auto context = BuildModule(env, consumer, shader, kFuzzAssembleOption);
1756 spvtools::ValidatorOptions validator_options;
1757 ASSERT_TRUE(fuzzerutil::IsValidAndWellFormed(context.get(), validator_options,
1758 kConsoleMessageConsumer));
1759 TransformationContext transformation_context(
1760 MakeUnique<FactManager>(context.get()), validator_options);
1761 auto* op_i_add = context->get_def_use_mgr()->GetDef(18);
1762 ASSERT_TRUE(op_i_add);
1763
1764 auto* op_f_add = context->get_def_use_mgr()->GetDef(19);
1765 ASSERT_TRUE(op_f_add);
1766
1767 transformation_context.GetFactManager()->AddFactDataSynonym(
1768 MakeDataDescriptor(12, {}), MakeDataDescriptor(13, {}));
1769 transformation_context.GetFactManager()->AddFactDataSynonym(
1770 MakeDataDescriptor(12, {}), MakeDataDescriptor(10, {}));
1771
1772 // Synonym differs only in signedness for OpIAdd.
1773 ASSERT_TRUE(TransformationReplaceIdWithSynonym(
1774 MakeIdUseDescriptorFromUse(context.get(), op_i_add, 0), 13)
1775 .IsApplicable(context.get(), transformation_context));
1776
1777 // Synonym has wrong type for OpIAdd.
1778 ASSERT_FALSE(TransformationReplaceIdWithSynonym(
1779 MakeIdUseDescriptorFromUse(context.get(), op_i_add, 0), 10)
1780 .IsApplicable(context.get(), transformation_context));
1781
1782 // Synonym has wrong type for OpFAdd.
1783 ASSERT_FALSE(TransformationReplaceIdWithSynonym(
1784 MakeIdUseDescriptorFromUse(context.get(), op_f_add, 0), 12)
1785 .IsApplicable(context.get(), transformation_context));
1786 ASSERT_FALSE(TransformationReplaceIdWithSynonym(
1787 MakeIdUseDescriptorFromUse(context.get(), op_f_add, 0), 13)
1788 .IsApplicable(context.get(), transformation_context));
1789 }
1790
1791 } // namespace
1792 } // namespace fuzz
1793 } // namespace spvtools