1 // Copyright (c) 2020 André Perez Maselco
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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"
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 {
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 "
43 return spvOpcodeIsCommutativeBinaryOperator(opcode
);
46 void TransformationSwapCommutableOperands::Apply(
47 opt::IRContext
* ir_context
, TransformationContext
* /*unused*/) const {
49 FindInstruction(message_
.instruction_descriptor(), ir_context
);
50 // By design, the instructions defined to be commutative have exactly two
52 std::swap(instruction
->GetInOperand(0), instruction
->GetInOperand(1));
55 protobufs::Transformation
TransformationSwapCommutableOperands::ToMessage()
57 protobufs::Transformation result
;
58 *result
.mutable_swap_commutable_operands() = message_
;
62 std::unordered_set
<uint32_t> TransformationSwapCommutableOperands::GetFreshIds()
64 return std::unordered_set
<uint32_t>();
68 } // namespace spvtools