1 // Copyright (c) 2020 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_replace_adds_subs_muls_with_carrying_extended.h"
16
17 #include "source/fuzz/fuzzer_util.h"
18 #include "source/fuzz/transformation_replace_add_sub_mul_with_carrying_extended.h"
19
20 namespace spvtools {
21 namespace fuzz {
22
23 namespace {
24 const uint32_t kArithmeticInstructionIndexLeftInOperand = 0;
25 } // namespace
26
27 FuzzerPassReplaceAddsSubsMulsWithCarryingExtended::
FuzzerPassReplaceAddsSubsMulsWithCarryingExtended(opt::IRContext * ir_context,TransformationContext * transformation_context,FuzzerContext * fuzzer_context,protobufs::TransformationSequence * transformations,bool ignore_inapplicable_transformations)28 FuzzerPassReplaceAddsSubsMulsWithCarryingExtended(
29 opt::IRContext* ir_context,
30 TransformationContext* transformation_context,
31 FuzzerContext* fuzzer_context,
32 protobufs::TransformationSequence* transformations,
33 bool ignore_inapplicable_transformations)
34 : FuzzerPass(ir_context, transformation_context, fuzzer_context,
35 transformations, ignore_inapplicable_transformations) {}
36
Apply()37 void FuzzerPassReplaceAddsSubsMulsWithCarryingExtended::Apply() {
38 std::vector<opt::Instruction> instructions_for_transformation;
39 for (auto& function : *GetIRContext()->module()) {
40 for (auto& block : function) {
41 for (auto& instruction : block) {
42 // Randomly decide whether to apply the transformation.
43 if (!GetFuzzerContext()->ChoosePercentage(
44 GetFuzzerContext()
45 ->GetChanceOfReplacingAddSubMulWithCarryingExtended())) {
46 continue;
47 }
48
49 // Check if the transformation can be applied to this instruction.
50 if (!TransformationReplaceAddSubMulWithCarryingExtended::
51 IsInstructionSuitable(GetIRContext(), instruction)) {
52 continue;
53 }
54 instructions_for_transformation.push_back(instruction);
55 }
56 }
57 }
58 for (auto& instruction : instructions_for_transformation) {
59 // Get the operand type id. We know that both operands have the same
60 // type.
61 uint32_t operand_type_id =
62 GetIRContext()
63 ->get_def_use_mgr()
64 ->GetDef(instruction.GetSingleWordInOperand(
65 kArithmeticInstructionIndexLeftInOperand))
66 ->type_id();
67
68 // Ensure the required struct type exists. The struct type is based on
69 // the operand type.
70 FindOrCreateStructType({operand_type_id, operand_type_id});
71
72 ApplyTransformation(TransformationReplaceAddSubMulWithCarryingExtended(
73 GetFuzzerContext()->GetFreshId(), instruction.result_id()));
74 }
75 }
76 } // namespace fuzz
77 } // namespace spvtools
78