Debian Codegraph @

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

raw
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_add_loop_preheaders.h"
16
17 #include "source/fuzz/fuzzer_util.h"
18 #include "source/fuzz/transformation_add_loop_preheader.h"
19
20 namespace spvtools {
21 namespace fuzz {
22
23 FuzzerPassAddLoopPreheaders::FuzzerPassAddLoopPreheaders(
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 FuzzerPassAddLoopPreheaders::~FuzzerPassAddLoopPreheaders() = default;
31
32 void FuzzerPassAddLoopPreheaders::Apply() {
33 for (auto& function : *GetIRContext()->module()) {
34 // Keep track of all the loop headers we want to add a preheader to.
35 std::vector<uint32_t> loop_header_ids_to_consider;
36 for (auto& block : function) {
37 // We only care about loop headers.
38 if (!block.IsLoopHeader()) {
39 continue;
40 }
41
42 // Randomly decide whether to consider this header.
43 if (!GetFuzzerContext()->ChoosePercentage(
44 GetFuzzerContext()->GetChanceOfAddingLoopPreheader())) {
45 continue;
46 }
47
48 // We exclude loop headers with just one predecessor (the back-edge block)
49 // because they are unreachable.
50 if (GetIRContext()->cfg()->preds(block.id()).size() < 2) {
51 continue;
52 }
53
54 loop_header_ids_to_consider.push_back(block.id());
55 }
56
57 for (uint32_t header_id : loop_header_ids_to_consider) {
58 // If not already present, add a preheader which is not also a loop
59 // header.
60 GetOrCreateSimpleLoopPreheader(header_id);
61 }
62 }
63 }
64
65 } // namespace fuzz
66 } // namespace spvtools