1 // Copyright (c) 2020 André Perez Maselco
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_toggle_access_chain_instruction.h"
16 
17 #include "source/fuzz/fuzzer_util.h"
18 #include "source/fuzz/instruction_descriptor.h"
19 
20 namespace spvtools {
21 namespace fuzz {
22 
23 TransformationToggleAccessChainInstruction::
TransformationToggleAccessChainInstruction(const spvtools::fuzz::protobufs::TransformationToggleAccessChainInstruction & message)24     TransformationToggleAccessChainInstruction(
25         const spvtools::fuzz::protobufs::
26             TransformationToggleAccessChainInstruction& message)
27     : message_(message) {}
28 
29 TransformationToggleAccessChainInstruction::
TransformationToggleAccessChainInstruction(const protobufs::InstructionDescriptor & instruction_descriptor)30     TransformationToggleAccessChainInstruction(
31         const protobufs::InstructionDescriptor& instruction_descriptor) {
32   *message_.mutable_instruction_descriptor() = instruction_descriptor;
33 }
34 
IsApplicable(opt::IRContext * ir_context,const TransformationContext &) const35 bool TransformationToggleAccessChainInstruction::IsApplicable(
36     opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
37   auto instruction =
38       FindInstruction(message_.instruction_descriptor(), ir_context);
39   if (instruction == nullptr) {
40     return false;
41   }
42 
43   SpvOp opcode = static_cast<SpvOp>(
44       message_.instruction_descriptor().target_instruction_opcode());
45 
46   assert(instruction->opcode() == opcode &&
47          "The located instruction must have the same opcode as in the "
48          "descriptor.");
49 
50   if (opcode == SpvOpAccessChain || opcode == SpvOpInBoundsAccessChain) {
51     return true;
52   }
53 
54   return false;
55 }
56 
Apply(opt::IRContext * ir_context,TransformationContext *) const57 void TransformationToggleAccessChainInstruction::Apply(
58     opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
59   auto instruction =
60       FindInstruction(message_.instruction_descriptor(), ir_context);
61   SpvOp opcode = instruction->opcode();
62 
63   if (opcode == SpvOpAccessChain) {
64     instruction->SetOpcode(SpvOpInBoundsAccessChain);
65   } else {
66     assert(opcode == SpvOpInBoundsAccessChain &&
67            "The located instruction must be an OpInBoundsAccessChain "
68            "instruction.");
69     instruction->SetOpcode(SpvOpAccessChain);
70   }
71 }
72 
73 protobufs::Transformation
ToMessage() const74 TransformationToggleAccessChainInstruction::ToMessage() const {
75   protobufs::Transformation result;
76   *result.mutable_toggle_access_chain_instruction() = message_;
77   return result;
78 }
79 
80 std::unordered_set<uint32_t>
GetFreshIds() const81 TransformationToggleAccessChainInstruction::GetFreshIds() const {
82   return std::unordered_set<uint32_t>();
83 }
84 
85 }  // namespace fuzz
86 }  // namespace spvtools
87