transformation_toggle_access_chain_instruction.cpp 2.8 KB

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