1 // Copyright (c) 2019 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/transformation_merge_blocks.h"
16
17 #include "source/fuzz/fuzzer_util.h"
18 #include "source/opt/block_merge_util.h"
19
20 namespace spvtools {
21 namespace fuzz {
22
TransformationMergeBlocks(protobufs::TransformationMergeBlocks message)23 TransformationMergeBlocks::TransformationMergeBlocks(
24 protobufs::TransformationMergeBlocks message)
25 : message_(std::move(message)) {}
26
TransformationMergeBlocks(uint32_t block_id)27 TransformationMergeBlocks::TransformationMergeBlocks(uint32_t block_id) {
28 message_.set_block_id(block_id);
29 }
30
IsApplicable(opt::IRContext * ir_context,const TransformationContext &) const31 bool TransformationMergeBlocks::IsApplicable(
32 opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
33 auto second_block =
34 fuzzerutil::MaybeFindBlock(ir_context, message_.block_id());
35 // The given block must exist.
36 if (!second_block) {
37 return false;
38 }
39 // The block must have just one predecessor.
40 auto predecessors = ir_context->cfg()->preds(second_block->id());
41 if (predecessors.size() != 1) {
42 return false;
43 }
44 auto first_block = ir_context->cfg()->block(predecessors.at(0));
45
46 if (!ir_context->IsReachable(*first_block)) {
47 return false;
48 }
49 return opt::blockmergeutil::CanMergeWithSuccessor(ir_context, first_block);
50 }
51
Apply(opt::IRContext * ir_context,TransformationContext *) const52 void TransformationMergeBlocks::Apply(opt::IRContext* ir_context,
53 TransformationContext* /*unused*/) const {
54 auto second_block =
55 fuzzerutil::MaybeFindBlock(ir_context, message_.block_id());
56 auto first_block = ir_context->cfg()->block(
57 ir_context->cfg()->preds(second_block->id()).at(0));
58
59 auto function = first_block->GetParent();
60 // We need an iterator pointing to the predecessor, hence the loop.
61 for (auto bi = function->begin(); bi != function->end(); ++bi) {
62 if (bi->id() == first_block->id()) {
63 assert(opt::blockmergeutil::CanMergeWithSuccessor(ir_context, &*bi) &&
64 "Because 'Apply' should only be invoked if 'IsApplicable' holds, "
65 "it must be possible to merge |bi| with its successor.");
66 opt::blockmergeutil::MergeWithSuccessor(ir_context, function, bi);
67 // Invalidate all analyses, since we have changed the module
68 // significantly.
69 ir_context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone);
70 return;
71 }
72 }
73 assert(false &&
74 "Control should not reach here - we should always find the desired "
75 "block");
76 }
77
ToMessage() const78 protobufs::Transformation TransformationMergeBlocks::ToMessage() const {
79 protobufs::Transformation result;
80 *result.mutable_merge_blocks() = message_;
81 return result;
82 }
83
GetFreshIds() const84 std::unordered_set<uint32_t> TransformationMergeBlocks::GetFreshIds() const {
85 return std::unordered_set<uint32_t>();
86 }
87
88 } // namespace fuzz
89 } // namespace spvtools
90