• 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_swap_commutable_operands.h"
16 
17 #include "source/fuzz/fuzzer_util.h"
18 #include "source/fuzz/instruction_descriptor.h"
19 
20 namespace spvtools {
21 namespace fuzz {
22 
TransformationSwapCommutableOperands(protobufs::TransformationSwapCommutableOperands message)23 TransformationSwapCommutableOperands::TransformationSwapCommutableOperands(
24     protobufs::TransformationSwapCommutableOperands message)
25     : message_(std::move(message)) {}
26 
TransformationSwapCommutableOperands(const protobufs::InstructionDescriptor & instruction_descriptor)27 TransformationSwapCommutableOperands::TransformationSwapCommutableOperands(
28     const protobufs::InstructionDescriptor& instruction_descriptor) {
29   *message_.mutable_instruction_descriptor() = instruction_descriptor;
30 }
31 
IsApplicable(opt::IRContext * ir_context,const TransformationContext &) const32 bool TransformationSwapCommutableOperands::IsApplicable(
33     opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
34   auto instruction =
35       FindInstruction(message_.instruction_descriptor(), ir_context);
36   if (instruction == nullptr) return false;
37 
38   SpvOp opcode = static_cast<SpvOp>(
39       message_.instruction_descriptor().target_instruction_opcode());
40   assert(instruction->opcode() == opcode &&
41          "The located instruction must have the same opcode as in the "
42          "descriptor.");
43   return spvOpcodeIsCommutativeBinaryOperator(opcode);
44 }
45 
Apply(opt::IRContext * ir_context,TransformationContext *) const46 void TransformationSwapCommutableOperands::Apply(
47     opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
48   auto instruction =
49       FindInstruction(message_.instruction_descriptor(), ir_context);
50   // By design, the instructions defined to be commutative have exactly two
51   // input parameters.
52   std::swap(instruction->GetInOperand(0), instruction->GetInOperand(1));
53 }
54 
ToMessage() const55 protobufs::Transformation TransformationSwapCommutableOperands::ToMessage()
56     const {
57   protobufs::Transformation result;
58   *result.mutable_swap_commutable_operands() = message_;
59   return result;
60 }
61 
GetFreshIds() const62 std::unordered_set<uint32_t> TransformationSwapCommutableOperands::GetFreshIds()
63     const {
64   return std::unordered_set<uint32_t>();
65 }
66 
67 }  // namespace fuzz
68 }  // namespace spvtools
69