transformation_split_block.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. protobufs::TransformationSplitBlock message)
  23. : message_(std::move(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* ir_context, const TransformationContext& /*unused*/) const {
  32. if (!fuzzerutil::IsFreshId(ir_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(), ir_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 =
  43. ir_context->get_instr_block(instruction_to_split_before);
  44. assert(block_to_split &&
  45. "We should not have managed to find the "
  46. "instruction if it was not contained in a block.");
  47. if (block_to_split->IsLoopHeader()) {
  48. // We cannot split a loop header block: back-edges would become invalid.
  49. return false;
  50. }
  51. auto split_before = fuzzerutil::GetIteratorForInstruction(
  52. block_to_split, instruction_to_split_before);
  53. assert(split_before != block_to_split->end() &&
  54. "At this point we know the"
  55. " block split point exists.");
  56. if (split_before->PreviousNode() &&
  57. split_before->PreviousNode()->opcode() == spv::Op::OpSelectionMerge) {
  58. // We cannot split directly after a selection merge: this would separate
  59. // the merge from its associated branch or switch operation.
  60. return false;
  61. }
  62. if (split_before->opcode() == spv::Op::OpVariable) {
  63. // We cannot split directly after a variable; variables in a function
  64. // must be contiguous in the entry block.
  65. return false;
  66. }
  67. // We cannot split before an OpPhi unless the OpPhi has exactly one
  68. // associated incoming edge.
  69. if (split_before->opcode() == spv::Op::OpPhi &&
  70. split_before->NumInOperands() != 2) {
  71. return false;
  72. }
  73. // Splitting the block must not separate the definition of an OpSampledImage
  74. // from its use: the SPIR-V data rules require them to be in the same block.
  75. return !fuzzerutil::
  76. SplittingBeforeInstructionSeparatesOpSampledImageDefinitionFromUse(
  77. block_to_split, instruction_to_split_before);
  78. }
  79. void TransformationSplitBlock::Apply(
  80. opt::IRContext* ir_context,
  81. TransformationContext* transformation_context) const {
  82. opt::Instruction* instruction_to_split_before =
  83. FindInstruction(message_.instruction_to_split_before(), ir_context);
  84. opt::BasicBlock* block_to_split =
  85. ir_context->get_instr_block(instruction_to_split_before);
  86. auto split_before = fuzzerutil::GetIteratorForInstruction(
  87. block_to_split, instruction_to_split_before);
  88. assert(split_before != block_to_split->end() &&
  89. "If the transformation is applicable, we should have an "
  90. "instruction to split on.");
  91. // We need to make sure the module's id bound is large enough to add the
  92. // fresh id.
  93. fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
  94. // Split the block.
  95. auto new_bb = block_to_split->SplitBasicBlock(ir_context, message_.fresh_id(),
  96. split_before);
  97. // The split does not automatically add a branch between the two parts of
  98. // the original block, so we add one.
  99. auto branch_instruction = MakeUnique<opt::Instruction>(
  100. ir_context, spv::Op::OpBranch, 0, 0,
  101. std::initializer_list<opt::Operand>{opt::Operand(
  102. spv_operand_type_t::SPV_OPERAND_TYPE_ID, {message_.fresh_id()})});
  103. auto branch_instruction_ptr = branch_instruction.get();
  104. block_to_split->AddInstruction(std::move(branch_instruction));
  105. // Inform the def-use manager about the branch instruction, and record its
  106. // block.
  107. ir_context->get_def_use_mgr()->AnalyzeInstDefUse(branch_instruction_ptr);
  108. ir_context->set_instr_block(branch_instruction_ptr, block_to_split);
  109. // If we split before OpPhi instructions, we need to update their
  110. // predecessor operand so that the block they used to be inside is now the
  111. // predecessor.
  112. new_bb->ForEachPhiInst([block_to_split,
  113. ir_context](opt::Instruction* phi_inst) {
  114. assert(
  115. phi_inst->NumInOperands() == 2 &&
  116. "Precondition: a block can only be split before an OpPhi if the block"
  117. "has exactly one predecessor.");
  118. phi_inst->SetInOperand(1, {block_to_split->id()});
  119. ir_context->UpdateDefUse(phi_inst);
  120. });
  121. // We have updated the def-use manager and the instruction to block mapping,
  122. // but other analyses (especially control flow-related ones) need to be
  123. // recomputed.
  124. ir_context->InvalidateAnalysesExceptFor(
  125. opt::IRContext::Analysis::kAnalysisDefUse |
  126. opt::IRContext::Analysis::kAnalysisInstrToBlockMapping);
  127. // If the block being split was dead, the new block arising from the split is
  128. // also dead.
  129. if (transformation_context->GetFactManager()->BlockIsDead(
  130. block_to_split->id())) {
  131. transformation_context->GetFactManager()->AddFactBlockIsDead(
  132. message_.fresh_id());
  133. }
  134. }
  135. protobufs::Transformation TransformationSplitBlock::ToMessage() const {
  136. protobufs::Transformation result;
  137. *result.mutable_split_block() = message_;
  138. return result;
  139. }
  140. std::unordered_set<uint32_t> TransformationSplitBlock::GetFreshIds() const {
  141. return {message_.fresh_id()};
  142. }
  143. } // namespace fuzz
  144. } // namespace spvtools