• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/fuzzer_pass_adjust_loop_controls.h"
16 
17 #include "source/fuzz/transformation_set_loop_control.h"
18 
19 namespace spvtools {
20 namespace fuzz {
21 
FuzzerPassAdjustLoopControls(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations)22 FuzzerPassAdjustLoopControls::FuzzerPassAdjustLoopControls(
23     opt::IRContext* ir_context, TransformationContext* transformation_context,
24     FuzzerContext* fuzzer_context,
25     protobufs::TransformationSequence* transformations)
26     : FuzzerPass(ir_context, transformation_context, fuzzer_context,
27                  transformations) {}
28 
Apply()29 void FuzzerPassAdjustLoopControls::Apply() {
30   // Consider every merge instruction in the module (via looking through all
31   // functions and blocks).
32   for (auto& function : *GetIRContext()->module()) {
33     for (auto& block : function) {
34       if (auto merge_inst = block.GetMergeInst()) {
35         // Ignore the instruction if it is not a loop merge.
36         if (merge_inst->opcode() != SpvOpLoopMerge) {
37           continue;
38         }
39 
40         // Decide randomly whether to adjust this loop merge.
41         if (!GetFuzzerContext()->ChoosePercentage(
42                 GetFuzzerContext()->GetChanceOfAdjustingLoopControl())) {
43           continue;
44         }
45 
46         uint32_t existing_mask = merge_inst->GetSingleWordOperand(
47             TransformationSetLoopControl::kLoopControlMaskInOperandIndex);
48 
49         // First, set the new mask to one of None, Unroll or DontUnroll.
50         std::vector<uint32_t> basic_masks = {SpvLoopControlMaskNone,
51                                              SpvLoopControlUnrollMask,
52                                              SpvLoopControlDontUnrollMask};
53         uint32_t new_mask =
54             basic_masks[GetFuzzerContext()->RandomIndex(basic_masks)];
55 
56         // For the loop controls that depend on guarantees about what the loop
57         // does, check which of these were present in the existing mask and
58         // randomly decide whether to keep them.  They are just hints, so
59         // removing them should not change the semantics of the module.
60         for (auto mask_bit :
61              {SpvLoopControlDependencyInfiniteMask,
62               SpvLoopControlDependencyLengthMask,
63               SpvLoopControlMinIterationsMask, SpvLoopControlMaxIterationsMask,
64               SpvLoopControlIterationMultipleMask}) {
65           if ((existing_mask & mask_bit) && GetFuzzerContext()->ChooseEven()) {
66             // The mask bits we are considering are not available in all SPIR-V
67             // versions.  However, we only include a mask bit if it was present
68             // in the original loop control mask, and we work under the
69             // assumption that we are transforming a valid module, thus we don't
70             // need to actually check whether the SPIR-V version being used
71             // supports these loop control mask bits.
72             new_mask |= mask_bit;
73           }
74         }
75 
76         // We use 0 for peel count and partial count in the case that we choose
77         // not to set these controls.
78         uint32_t peel_count = 0;
79         uint32_t partial_count = 0;
80 
81         // PeelCount and PartialCount are not compatible with DontUnroll, so
82         // we check whether DontUnroll is set.
83         if (!(new_mask & SpvLoopControlDontUnrollMask)) {
84           // If PeelCount is supported by this SPIR-V version, randomly choose
85           // whether to set it.  If it was set in the original mask and is not
86           // selected for setting here, that amounts to dropping it.
87           if (TransformationSetLoopControl::PeelCountIsSupported(
88                   GetIRContext()) &&
89               GetFuzzerContext()->ChooseEven()) {
90             new_mask |= SpvLoopControlPeelCountMask;
91             // The peel count is chosen randomly - if PeelCount was already set
92             // this will overwrite whatever peel count was previously used.
93             peel_count = GetFuzzerContext()->GetRandomLoopControlPeelCount();
94           }
95           // Similar, but for PartialCount.
96           if (TransformationSetLoopControl::PartialCountIsSupported(
97                   GetIRContext()) &&
98               GetFuzzerContext()->ChooseEven()) {
99             new_mask |= SpvLoopControlPartialCountMask;
100             partial_count =
101                 GetFuzzerContext()->GetRandomLoopControlPartialCount();
102           }
103         }
104 
105         // Apply the transformation and add it to the output transformation
106         // sequence.
107         TransformationSetLoopControl transformation(block.id(), new_mask,
108                                                     peel_count, partial_count);
109         ApplyTransformation(transformation);
110       }
111     }
112   }
113 }
114 
115 }  // namespace fuzz
116 }  // namespace spvtools
117