transformation_toggle_access_chain_instruction.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_toggle_access_chain_instruction.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/instruction_descriptor.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. TransformationToggleAccessChainInstruction::
  20. TransformationToggleAccessChainInstruction(
  21. const spvtools::fuzz::protobufs::
  22. TransformationToggleAccessChainInstruction& message)
  23. : message_(message) {}
  24. TransformationToggleAccessChainInstruction::
  25. TransformationToggleAccessChainInstruction(
  26. const protobufs::InstructionDescriptor& instruction_descriptor) {
  27. *message_.mutable_instruction_descriptor() = instruction_descriptor;
  28. }
  29. bool TransformationToggleAccessChainInstruction::IsApplicable(
  30. opt::IRContext* ir_context, const TransformationContext& /*unused*/
  31. ) const {
  32. auto instruction =
  33. FindInstruction(message_.instruction_descriptor(), ir_context);
  34. if (instruction == nullptr) {
  35. return false;
  36. }
  37. SpvOp opcode = static_cast<SpvOp>(
  38. message_.instruction_descriptor().target_instruction_opcode());
  39. assert(instruction->opcode() == opcode &&
  40. "The located instruction must have the same opcode as in the "
  41. "descriptor.");
  42. if (opcode == SpvOpAccessChain || opcode == SpvOpInBoundsAccessChain) {
  43. return true;
  44. }
  45. return false;
  46. }
  47. void TransformationToggleAccessChainInstruction::Apply(
  48. opt::IRContext* ir_context, TransformationContext* /*unused*/
  49. ) const {
  50. auto instruction =
  51. FindInstruction(message_.instruction_descriptor(), ir_context);
  52. SpvOp opcode = instruction->opcode();
  53. if (opcode == SpvOpAccessChain) {
  54. instruction->SetOpcode(SpvOpInBoundsAccessChain);
  55. } else {
  56. assert(opcode == SpvOpInBoundsAccessChain &&
  57. "The located instruction must be an OpInBoundsAccessChain "
  58. "instruction.");
  59. instruction->SetOpcode(SpvOpAccessChain);
  60. }
  61. }
  62. protobufs::Transformation
  63. TransformationToggleAccessChainInstruction::ToMessage() const {
  64. protobufs::Transformation result;
  65. *result.mutable_toggle_access_chain_instruction() = message_;
  66. return result;
  67. }
  68. } // namespace fuzz
  69. } // namespace spvtools