instruction_descriptor.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (c) 2019 Google LLC
  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/instruction_descriptor.h"
  15. namespace spvtools {
  16. namespace fuzz {
  17. opt::Instruction* FindInstruction(
  18. const protobufs::InstructionDescriptor& instruction_descriptor,
  19. spvtools::opt::IRContext* context) {
  20. auto block = context->get_instr_block(
  21. instruction_descriptor.base_instruction_result_id());
  22. if (block == nullptr) {
  23. return nullptr;
  24. }
  25. bool found_base =
  26. block->id() == instruction_descriptor.base_instruction_result_id();
  27. uint32_t num_ignored = 0;
  28. for (auto& instruction : *block) {
  29. if (instruction.HasResultId() &&
  30. instruction.result_id() ==
  31. instruction_descriptor.base_instruction_result_id()) {
  32. assert(!found_base &&
  33. "It should not be possible to find the base instruction "
  34. "multiple times.");
  35. found_base = true;
  36. assert(num_ignored == 0 &&
  37. "The skipped instruction count should only be incremented "
  38. "after the instruction base has been found.");
  39. }
  40. if (found_base &&
  41. instruction.opcode() ==
  42. spv::Op(instruction_descriptor.target_instruction_opcode())) {
  43. if (num_ignored == instruction_descriptor.num_opcodes_to_ignore()) {
  44. return &instruction;
  45. }
  46. num_ignored++;
  47. }
  48. }
  49. return nullptr;
  50. }
  51. protobufs::InstructionDescriptor MakeInstructionDescriptor(
  52. uint32_t base_instruction_result_id, spv::Op target_instruction_opcode,
  53. uint32_t num_opcodes_to_ignore) {
  54. protobufs::InstructionDescriptor result;
  55. result.set_base_instruction_result_id(base_instruction_result_id);
  56. result.set_target_instruction_opcode(uint32_t(target_instruction_opcode));
  57. result.set_num_opcodes_to_ignore(num_opcodes_to_ignore);
  58. return result;
  59. }
  60. protobufs::InstructionDescriptor MakeInstructionDescriptor(
  61. const opt::BasicBlock& block,
  62. const opt::BasicBlock::const_iterator& inst_it) {
  63. const spv::Op opcode =
  64. inst_it->opcode(); // The opcode of the instruction being described.
  65. uint32_t skip_count = 0; // The number of these opcodes we have skipped when
  66. // searching backwards.
  67. // Consider instructions in the block in reverse order, starting from
  68. // |inst_it|.
  69. for (opt::BasicBlock::const_iterator backwards_iterator = inst_it;;
  70. --backwards_iterator) {
  71. if (backwards_iterator->HasResultId()) {
  72. // As soon as we find an instruction with a result id, we can return a
  73. // descriptor for |inst_it|.
  74. return MakeInstructionDescriptor(backwards_iterator->result_id(), opcode,
  75. skip_count);
  76. }
  77. if (backwards_iterator != inst_it &&
  78. backwards_iterator->opcode() == opcode) {
  79. // We are skipping over an instruction with the same opcode as |inst_it|;
  80. // we increase our skip count to reflect this.
  81. skip_count++;
  82. }
  83. if (backwards_iterator == block.begin()) {
  84. // We exit the loop when we reach the start of the block, but only after
  85. // we have processed the first instruction in the block.
  86. break;
  87. }
  88. }
  89. // We did not find an instruction inside the block with a result id, so we use
  90. // the block's label's id.
  91. return MakeInstructionDescriptor(block.id(), opcode, skip_count);
  92. }
  93. protobufs::InstructionDescriptor MakeInstructionDescriptor(
  94. opt::IRContext* context, opt::Instruction* inst) {
  95. auto block = context->get_instr_block(inst);
  96. uint32_t base_instruction_result_id = block->id();
  97. uint32_t num_opcodes_to_ignore = 0;
  98. for (auto& inst_in_block : *block) {
  99. if (inst_in_block.HasResultId()) {
  100. base_instruction_result_id = inst_in_block.result_id();
  101. num_opcodes_to_ignore = 0;
  102. }
  103. if (&inst_in_block == inst) {
  104. return MakeInstructionDescriptor(base_instruction_result_id,
  105. inst->opcode(), num_opcodes_to_ignore);
  106. }
  107. if (inst_in_block.opcode() == inst->opcode()) {
  108. num_opcodes_to_ignore++;
  109. }
  110. }
  111. assert(false && "No matching instruction was found.");
  112. return protobufs::InstructionDescriptor();
  113. }
  114. } // namespace fuzz
  115. } // namespace spvtools