Debian Codegraph @

third_party/spirv-tools/source/fuzz/fuzzer_pass_propagate_instructions_down.cpp

raw
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_down.h"
16
17 #include "source/fuzz/fuzzer_context.h"
18 #include "source/fuzz/transformation_propagate_instruction_down.h"
19
20 namespace spvtools {
21 namespace fuzz {
22
23 FuzzerPassPropagateInstructionsDown::FuzzerPassPropagateInstructionsDown(
24 opt::IRContext* ir_context, TransformationContext* transformation_context,
25 FuzzerContext* fuzzer_context,
26 protobufs::TransformationSequence* transformations)
27 : FuzzerPass(ir_context, transformation_context, fuzzer_context,
28 transformations) {}
29
30 FuzzerPassPropagateInstructionsDown::~FuzzerPassPropagateInstructionsDown() =
31 default;
32
33 void FuzzerPassPropagateInstructionsDown::Apply() {
34 for (const auto& function : *GetIRContext()->module()) {
35 std::vector<const opt::BasicBlock*> reachable_blocks;
36 for (const auto& block : function) {
37 if (GetIRContext()->GetDominatorAnalysis(&function)->IsReachable(
38 &block)) {
39 reachable_blocks.push_back(&block);
40 }
41 }
42
43 for (const auto* block : reachable_blocks) {
44 if (!GetFuzzerContext()->ChoosePercentage(
45 GetFuzzerContext()->GetChanceOfPropagatingInstructionsDown())) {
46 continue;
47 }
48
49 if (TransformationPropagateInstructionDown::IsApplicableToBlock(
50 GetIRContext(), block->id())) {
51 // Record fresh ids for every successor of the |block| that we can
52 // propagate an instruction into.
53 std::map<uint32_t, uint32_t> fresh_ids;
54 for (auto id :
55 TransformationPropagateInstructionDown::GetAcceptableSuccessors(
56 GetIRContext(), block->id())) {
57 fresh_ids[id] = GetFuzzerContext()->GetFreshId();
58 }
59
60 ApplyTransformation(TransformationPropagateInstructionDown(
61 block->id(), GetFuzzerContext()->GetFreshId(), fresh_ids));
62 }
63 }
64 }
65 }
66
67 } // namespace fuzz
68 } // namespace spvtools