transformation_swap_commutable_operands.cpp 2.5 KB

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