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