transformation_split_block.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/transformation_split_block.h"
  15. #include <utility>
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/fuzz/instruction_descriptor.h"
  18. #include "source/util/make_unique.h"
  19. namespace spvtools {
  20. namespace fuzz {
  21. TransformationSplitBlock::TransformationSplitBlock(
  22. const spvtools::fuzz::protobufs::TransformationSplitBlock& message)
  23. : message_(message) {}
  24. TransformationSplitBlock::TransformationSplitBlock(
  25. const protobufs::InstructionDescriptor& instruction_to_split_before,
  26. uint32_t fresh_id) {
  27. *message_.mutable_instruction_to_split_before() = instruction_to_split_before;
  28. message_.set_fresh_id(fresh_id);
  29. }
  30. bool TransformationSplitBlock::IsApplicable(
  31. opt::IRContext* context, const FactManager& /*unused*/) const {
  32. if (!fuzzerutil::IsFreshId(context, message_.fresh_id())) {
  33. // We require the id for the new block to be unused.
  34. return false;
  35. }
  36. auto instruction_to_split_before =
  37. FindInstruction(message_.instruction_to_split_before(), context);
  38. if (!instruction_to_split_before) {
  39. // The instruction describing the block we should split does not exist.
  40. return false;
  41. }
  42. auto block_to_split = context->get_instr_block(instruction_to_split_before);
  43. assert(block_to_split &&
  44. "We should not have managed to find the "
  45. "instruction if it was not contained in a block.");
  46. if (block_to_split->IsLoopHeader()) {
  47. // We cannot split a loop header block: back-edges would become invalid.
  48. return false;
  49. }
  50. auto split_before = fuzzerutil::GetIteratorForInstruction(
  51. block_to_split, instruction_to_split_before);
  52. assert(split_before != block_to_split->end() &&
  53. "At this point we know the"
  54. " block split point exists.");
  55. if (split_before->PreviousNode() &&
  56. split_before->PreviousNode()->opcode() == SpvOpSelectionMerge) {
  57. // We cannot split directly after a selection merge: this would separate
  58. // the merge from its associated branch or switch operation.
  59. return false;
  60. }
  61. if (split_before->opcode() == SpvOpVariable) {
  62. // We cannot split directly after a variable; variables in a function
  63. // must be contiguous in the entry block.
  64. return false;
  65. }
  66. // We cannot split before an OpPhi unless the OpPhi has exactly one
  67. // associated incoming edge.
  68. return !(split_before->opcode() == SpvOpPhi &&
  69. split_before->NumInOperands() != 2);
  70. }
  71. void TransformationSplitBlock::Apply(opt::IRContext* context,
  72. FactManager* /*unused*/) const {
  73. opt::Instruction* instruction_to_split_before =
  74. FindInstruction(message_.instruction_to_split_before(), context);
  75. opt::BasicBlock* block_to_split =
  76. context->get_instr_block(instruction_to_split_before);
  77. auto split_before = fuzzerutil::GetIteratorForInstruction(
  78. block_to_split, instruction_to_split_before);
  79. assert(split_before != block_to_split->end() &&
  80. "If the transformation is applicable, we should have an "
  81. "instruction to split on.");
  82. // We need to make sure the module's id bound is large enough to add the
  83. // fresh id.
  84. fuzzerutil::UpdateModuleIdBound(context, message_.fresh_id());
  85. // Split the block.
  86. auto new_bb = block_to_split->SplitBasicBlock(context, message_.fresh_id(),
  87. split_before);
  88. // The split does not automatically add a branch between the two parts of
  89. // the original block, so we add one.
  90. block_to_split->AddInstruction(MakeUnique<opt::Instruction>(
  91. context, SpvOpBranch, 0, 0,
  92. std::initializer_list<opt::Operand>{opt::Operand(
  93. spv_operand_type_t::SPV_OPERAND_TYPE_ID, {message_.fresh_id()})}));
  94. // If we split before OpPhi instructions, we need to update their
  95. // predecessor operand so that the block they used to be inside is now the
  96. // predecessor.
  97. new_bb->ForEachPhiInst([block_to_split](opt::Instruction* phi_inst) {
  98. // The following assertion is a sanity check. It is guaranteed to hold
  99. // if IsApplicable holds.
  100. assert(phi_inst->NumInOperands() == 2 &&
  101. "We can only split a block before an OpPhi if block has exactly "
  102. "one predecessor.");
  103. phi_inst->SetInOperand(1, {block_to_split->id()});
  104. });
  105. // Invalidate all analyses
  106. context->InvalidateAnalysesExceptFor(opt::IRContext::Analysis::kAnalysisNone);
  107. }
  108. protobufs::Transformation TransformationSplitBlock::ToMessage() const {
  109. protobufs::Transformation result;
  110. *result.mutable_split_block() = message_;
  111. return result;
  112. }
  113. } // namespace fuzz
  114. } // namespace spvtools