Roll external/abseil_cpp/ 8f739d18b..917bfee46 (2 commits) (#5887)
[KhronosGroup/SPIRV-Tools.git] / source / fuzz / transformation_swap_commutable_operands.cpp
blob15121443fe9eaec7eba9fa32a73dc18a8049da21
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.
15 #include "source/fuzz/transformation_swap_commutable_operands.h"
17 #include "source/fuzz/fuzzer_util.h"
18 #include "source/fuzz/instruction_descriptor.h"
20 namespace spvtools {
21 namespace fuzz {
23 TransformationSwapCommutableOperands::TransformationSwapCommutableOperands(
24 protobufs::TransformationSwapCommutableOperands message)
25 : message_(std::move(message)) {}
27 TransformationSwapCommutableOperands::TransformationSwapCommutableOperands(
28 const protobufs::InstructionDescriptor& instruction_descriptor) {
29 *message_.mutable_instruction_descriptor() = instruction_descriptor;
32 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;
38 spv::Op opcode = static_cast<spv::Op>(
39 message_.instruction_descriptor().target_instruction_opcode());
40 assert(spv::Op(instruction->opcode()) == opcode &&
41 "The located instruction must have the same opcode as in the "
42 "descriptor.");
43 return spvOpcodeIsCommutativeBinaryOperator(opcode);
46 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));
55 protobufs::Transformation TransformationSwapCommutableOperands::ToMessage()
56 const {
57 protobufs::Transformation result;
58 *result.mutable_swap_commutable_operands() = message_;
59 return result;
62 std::unordered_set<uint32_t> TransformationSwapCommutableOperands::GetFreshIds()
63 const {
64 return std::unordered_set<uint32_t>();
67 } // namespace fuzz
68 } // namespace spvtools