| 1 |
// Copyright (c) 2020 Vasyl Teliman |
| 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_propagate_instructions_up.h" |
| 16 |
|
| 17 |
#include "source/fuzz/fuzzer_context.h" |
| 18 |
#include "source/fuzz/fuzzer_util.h" |
| 19 |
#include "source/fuzz/instruction_descriptor.h" |
| 20 |
#include "source/fuzz/transformation_propagate_instruction_up.h" |
| 21 |
|
| 22 |
namespace spvtools { |
| 23 |
namespace fuzz { |
| 24 |
|
| 25 |
FuzzerPassPropagateInstructionsUp::FuzzerPassPropagateInstructionsUp( |
| 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 |
FuzzerPassPropagateInstructionsUp::~FuzzerPassPropagateInstructionsUp() = |
| 33 |
default; |
| 34 |
|
| 35 |
void FuzzerPassPropagateInstructionsUp::Apply() { |
| 36 |
for (const auto& function : *GetIRContext()->module()) { |
| 37 |
for (const auto& block : function) { |
| 38 |
if (!GetFuzzerContext()->ChoosePercentage( |
| 39 |
GetFuzzerContext()->GetChanceOfPropagatingInstructionsUp())) { |
| 40 |
continue; |
| 41 |
} |
| 42 |
|
| 43 |
if (TransformationPropagateInstructionUp::IsApplicableToBlock( |
| 44 |
GetIRContext(), block.id())) { |
| 45 |
std::map<uint32_t, uint32_t> fresh_ids; |
| 46 |
for (auto id : GetIRContext()->cfg()->preds(block.id())) { |
| 47 |
auto& fresh_id = fresh_ids[id]; |
| 48 |
|
| 49 |
if (!fresh_id) { |
| 50 |
// Create a fresh id if it hasn't been created yet. |fresh_id| will |
| 51 |
// be default-initialized to 0 in this case. |
| 52 |
fresh_id = GetFuzzerContext()->GetFreshId(); |
| 53 |
} |
| 54 |
} |
| 55 |
ApplyTransformation( |
| 56 |
TransformationPropagateInstructionUp(block.id(), fresh_ids)); |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
} // namespace fuzz |
| 63 |
} // namespace spvtools |