| 1 |
// Copyright (c) 2020 André Perez Maselco |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include "source/fuzz/fuzzer_pass_inline_functions.h" |
| 16 |
|
| 17 |
#include "source/fuzz/fuzzer_util.h" |
| 18 |
#include "source/fuzz/instruction_descriptor.h" |
| 19 |
#include "source/fuzz/transformation_inline_function.h" |
| 20 |
#include "source/fuzz/transformation_split_block.h" |
| 21 |
|
| 22 |
namespace spvtools { |
| 23 |
namespace fuzz { |
| 24 |
|
| 25 |
FuzzerPassInlineFunctions::FuzzerPassInlineFunctions( |
| 26 |
opt::IRContext* ir_context, TransformationContext* transformation_context, |
| 27 |
FuzzerContext* fuzzer_context, |
| 28 |
protobufs::TransformationSequence* transformations) |
| 29 |
: FuzzerPass(ir_context, transformation_context, fuzzer_context, |
| 30 |
transformations) {} |
| 31 |
|
| 32 |
FuzzerPassInlineFunctions::~FuzzerPassInlineFunctions() = default; |
| 33 |
|
| 34 |
void FuzzerPassInlineFunctions::Apply() { |
| 35 |
// |function_call_instructions| are the instructions that will be inlined. |
| 36 |
// First, they will be collected and then do the inlining in another loop. |
| 37 |
// This avoids changing the module while it is being inspected. |
| 38 |
std::vector<opt::Instruction*> function_call_instructions; |
| 39 |
|
| 40 |
for (auto& function : *GetIRContext()->module()) { |
| 41 |
for (auto& block : function) { |
| 42 |
for (auto& instruction : block) { |
| 43 |
if (!GetFuzzerContext()->ChoosePercentage( |
| 44 |
GetFuzzerContext()->GetChanceOfInliningFunction())) { |
| 45 |
continue; |
| 46 |
} |
| 47 |
|
| 48 |
// |instruction| must be suitable for inlining. |
| 49 |
if (!TransformationInlineFunction::IsSuitableForInlining( |
| 50 |
GetIRContext(), &instruction)) { |
| 51 |
continue; |
| 52 |
} |
| 53 |
|
| 54 |
function_call_instructions.push_back(&instruction); |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
// Once the function calls have been collected, it's time to actually create |
| 60 |
// and apply the inlining transformations. |
| 61 |
for (auto& function_call_instruction : function_call_instructions) { |
| 62 |
// If |function_call_instruction| is not the penultimate instruction in its |
| 63 |
// block or its block termination instruction is not OpBranch, then try to |
| 64 |
// split |function_call_block| such that the conditions are met. |
| 65 |
auto* function_call_block = |
| 66 |
GetIRContext()->get_instr_block(function_call_instruction); |
| 67 |
if ((function_call_instruction != &*--function_call_block->tail() || |
| 68 |
function_call_block->terminator()->opcode() != SpvOpBranch) && |
| 69 |
!MaybeApplyTransformation(TransformationSplitBlock( |
| 70 |
MakeInstructionDescriptor(GetIRContext(), |
| 71 |
function_call_instruction->NextNode()), |
| 72 |
GetFuzzerContext()->GetFreshId()))) { |
| 73 |
continue; |
| 74 |
} |
| 75 |
|
| 76 |
auto* called_function = fuzzerutil::FindFunction( |
| 77 |
GetIRContext(), function_call_instruction->GetSingleWordInOperand(0)); |
| 78 |
|
| 79 |
// Mapping the called function instructions. |
| 80 |
std::map<uint32_t, uint32_t> result_id_map; |
| 81 |
for (auto& called_function_block : *called_function) { |
| 82 |
// The called function entry block label will not be inlined. |
| 83 |
if (&called_function_block != &*called_function->entry()) { |
| 84 |
result_id_map[called_function_block.GetLabelInst()->result_id()] = |
| 85 |
GetFuzzerContext()->GetFreshId(); |
| 86 |
} |
| 87 |
|
| 88 |
for (auto& instruction_to_inline : called_function_block) { |
| 89 |
// The instructions are mapped to fresh ids. |
| 90 |
if (instruction_to_inline.HasResultId()) { |
| 91 |
result_id_map[instruction_to_inline.result_id()] = |
| 92 |
GetFuzzerContext()->GetFreshId(); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
// Applies the inline function transformation. |
| 98 |
ApplyTransformation(TransformationInlineFunction( |
| 99 |
function_call_instruction->result_id(), result_id_map)); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
} // namespace fuzz |
| 104 |
} // namespace spvtools |