• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(protobufs::TransformationToggleAccessChainInstruction message)24     TransformationToggleAccessChainInstruction(
25         protobufs::TransformationToggleAccessChainInstruction message)
26     : message_(std::move(message)) {}
27 
28 TransformationToggleAccessChainInstruction::
TransformationToggleAccessChainInstruction(const protobufs::InstructionDescriptor & instruction_descriptor)29     TransformationToggleAccessChainInstruction(
30         const protobufs::InstructionDescriptor& instruction_descriptor) {
31   *message_.mutable_instruction_descriptor() = instruction_descriptor;
32 }
33 
IsApplicable(opt::IRContext * ir_context,const TransformationContext &) const34 bool TransformationToggleAccessChainInstruction::IsApplicable(
35     opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
36   auto instruction =
37       FindInstruction(message_.instruction_descriptor(), ir_context);
38   if (instruction == nullptr) {
39     return false;
40   }
41 
42   SpvOp opcode = static_cast<SpvOp>(
43       message_.instruction_descriptor().target_instruction_opcode());
44 
45   assert(instruction->opcode() == opcode &&
46          "The located instruction must have the same opcode as in the "
47          "descriptor.");
48 
49   if (opcode == SpvOpAccessChain || opcode == SpvOpInBoundsAccessChain) {
50     return true;
51   }
52 
53   return false;
54 }
55 
Apply(opt::IRContext * ir_context,TransformationContext *) const56 void TransformationToggleAccessChainInstruction::Apply(
57     opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
58   auto instruction =
59       FindInstruction(message_.instruction_descriptor(), ir_context);
60   SpvOp opcode = instruction->opcode();
61 
62   if (opcode == SpvOpAccessChain) {
63     instruction->SetOpcode(SpvOpInBoundsAccessChain);
64   } else {
65     assert(opcode == SpvOpInBoundsAccessChain &&
66            "The located instruction must be an OpInBoundsAccessChain "
67            "instruction.");
68     instruction->SetOpcode(SpvOpAccessChain);
69   }
70 }
71 
72 protobufs::Transformation
ToMessage() const73 TransformationToggleAccessChainInstruction::ToMessage() const {
74   protobufs::Transformation result;
75   *result.mutable_toggle_access_chain_instruction() = message_;
76   return result;
77 }
78 
79 std::unordered_set<uint32_t>
GetFreshIds() const80 TransformationToggleAccessChainInstruction::GetFreshIds() const {
81   return std::unordered_set<uint32_t>();
82 }
83 
84 }  // namespace fuzz
85 }  // namespace spvtools
86