• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2021 Mostafa Ashraf
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_swap_function_variables.h"
16 
17 #include "source/fuzz/fuzzer_util.h"
18 
19 namespace spvtools {
20 namespace fuzz {
21 
TransformationSwapFunctionVariables(protobufs::TransformationSwapFunctionVariables message)22 TransformationSwapFunctionVariables::TransformationSwapFunctionVariables(
23     protobufs::TransformationSwapFunctionVariables message)
24     : message_(std::move(message)) {}
25 
TransformationSwapFunctionVariables(uint32_t result_id1,uint32_t result_id2)26 TransformationSwapFunctionVariables::TransformationSwapFunctionVariables(
27     uint32_t result_id1, uint32_t result_id2) {
28   message_.set_result_id1(result_id1);
29   message_.set_result_id2(result_id2);
30 }
31 
IsApplicable(opt::IRContext * ir_context,const TransformationContext &) const32 bool TransformationSwapFunctionVariables::IsApplicable(
33     opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
34   uint32_t result_id1 = message_.result_id1();
35   uint32_t result_id2 = message_.result_id2();
36 
37   assert((result_id1 != result_id2) && "Two results ids are equal");
38 
39   // The result ids used in the message must refer to instructions.
40   auto instruction1 = ir_context->get_def_use_mgr()->GetDef(result_id1);
41   auto instruction2 = ir_context->get_def_use_mgr()->GetDef(result_id2);
42   if (instruction1 == nullptr || instruction2 == nullptr) {
43     return false;
44   }
45   // Both instructions must be variables.
46   if (instruction1->opcode() != SpvOpVariable ||
47       instruction2->opcode() != SpvOpVariable) {
48     return false;
49   }
50 
51   // Both variable instructions must be in some basic block (as they are
52   // function-local variables), and they must be in the same block (as they need
53   // to be variables of the same function).
54   auto* block_1 = ir_context->get_instr_block(result_id1);
55   auto* block_2 = ir_context->get_instr_block(result_id2);
56   if (block_1 == nullptr || block_2 == nullptr) {
57     return false;
58   }
59 
60   return block_1 == block_2;
61 }
62 
Apply(opt::IRContext * ir_context,TransformationContext *) const63 void TransformationSwapFunctionVariables::Apply(
64     opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
65   // The result ids used in the message must refer to instructions.
66   auto instruction1 =
67       ir_context->get_def_use_mgr()->GetDef(message_.result_id1());
68   auto instruction2 =
69       ir_context->get_def_use_mgr()->GetDef(message_.result_id2());
70 
71   std::unique_ptr<opt::Instruction> temp_instruction =
72       MakeUnique<opt::Instruction>();
73 
74   temp_instruction->InsertBefore(instruction1);
75   instruction1->InsertAfter(instruction2);
76   instruction2->InsertAfter(temp_instruction.get());
77   temp_instruction->RemoveFromList();
78 }
79 
ToMessage() const80 protobufs::Transformation TransformationSwapFunctionVariables::ToMessage()
81     const {
82   protobufs::Transformation result;
83   *result.mutable_swap_function_variables() = message_;
84   return result;
85 }
86 
GetFreshIds() const87 std::unordered_set<uint32_t> TransformationSwapFunctionVariables::GetFreshIds()
88     const {
89   return std::unordered_set<uint32_t>();
90 }
91 
92 }  // namespace fuzz
93 }  // namespace spvtools
94