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 spv::Op opcode = static_cast<spv::Op>(
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 == spv::Op::OpAccessChain ||
50 opcode == spv::Op::OpInBoundsAccessChain) {
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 spv::Op opcode = instruction->opcode();
62
63 if (opcode == spv::Op::OpAccessChain) {
64 instruction->SetOpcode(spv::Op::OpInBoundsAccessChain);
65 } else {
66 assert(opcode == spv::Op::OpInBoundsAccessChain &&
67 "The located instruction must be an OpInBoundsAccessChain "
68 "instruction.");
69 instruction->SetOpcode(spv::Op::OpAccessChain);
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