transformation_swap_commutable_operands.cpp 2.4 KB

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