instruction_descriptor.cpp 4.7 KB

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