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 return opt::blockmergeutil::CanMergeWithSuccessor(ir_context, first_block);
47 }
48
Apply(opt::IRContext * ir_context,TransformationContext *) const49 void TransformationMergeBlocks::Apply(opt::IRContext* ir_context,
50 TransformationContext* /*unused*/) const {
51 auto second_block =
52 fuzzerutil::MaybeFindBlock(ir_context, message_.block_id());
53 auto first_block = ir_context->cfg()->block(
54 ir_context->cfg()->preds(second_block->id()).at(0));
55
56 auto function = first_block->GetParent();
57 // We need an iterator pointing to the predecessor, hence the loop.
58 for (auto bi = function->begin(); bi != function->end(); ++bi) {
59 if (bi->id() == first_block->id()) {
60 assert(opt::blockmergeutil::CanMergeWithSuccessor(ir_context, &*bi) &&
61 "Because 'Apply' should only be invoked if 'IsApplicable' holds, "
62 "it must be possible to merge |bi| with its successor.");
63 opt::blockmergeutil::MergeWithSuccessor(ir_context, function, bi);
64 // Invalidate all analyses, since we have changed the module
65 // significantly.
66 ir_context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone);
67 return;
68 }
69 }
70 assert(false &&
71 "Control should not reach here - we should always find the desired "
72 "block");
73 }
74
ToMessage() const75 protobufs::Transformation TransformationMergeBlocks::ToMessage() const {
76 protobufs::Transformation result;
77 *result.mutable_merge_blocks() = message_;
78 return result;
79 }
80
GetFreshIds() const81 std::unordered_set<uint32_t> TransformationMergeBlocks::GetFreshIds() const {
82 return std::unordered_set<uint32_t>();
83 }
84
85 } // namespace fuzz
86 } // namespace spvtools
87