transformation_split_block.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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* 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() == SpvOpSelectionMerge) {
  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() == SpvOpVariable) {
  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() == SpvOpPhi &&
  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. std::set<uint32_t> sampled_image_result_ids;
  76. bool before_split = true;
  77. for (auto& instruction : *block_to_split) {
  78. if (&instruction == &*split_before) {
  79. before_split = false;
  80. }
  81. if (before_split) {
  82. if (instruction.opcode() == SpvOpSampledImage) {
  83. sampled_image_result_ids.insert(instruction.result_id());
  84. }
  85. } else {
  86. if (!instruction.WhileEachInId(
  87. [&sampled_image_result_ids](uint32_t* id) -> bool {
  88. return !sampled_image_result_ids.count(*id);
  89. })) {
  90. return false;
  91. }
  92. }
  93. }
  94. return true;
  95. }
  96. void TransformationSplitBlock::Apply(
  97. opt::IRContext* ir_context,
  98. TransformationContext* transformation_context) const {
  99. opt::Instruction* instruction_to_split_before =
  100. FindInstruction(message_.instruction_to_split_before(), ir_context);
  101. opt::BasicBlock* block_to_split =
  102. ir_context->get_instr_block(instruction_to_split_before);
  103. auto split_before = fuzzerutil::GetIteratorForInstruction(
  104. block_to_split, instruction_to_split_before);
  105. assert(split_before != block_to_split->end() &&
  106. "If the transformation is applicable, we should have an "
  107. "instruction to split on.");
  108. // We need to make sure the module's id bound is large enough to add the
  109. // fresh id.
  110. fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
  111. // Split the block.
  112. auto new_bb = block_to_split->SplitBasicBlock(ir_context, message_.fresh_id(),
  113. split_before);
  114. // The split does not automatically add a branch between the two parts of
  115. // the original block, so we add one.
  116. block_to_split->AddInstruction(MakeUnique<opt::Instruction>(
  117. ir_context, SpvOpBranch, 0, 0,
  118. std::initializer_list<opt::Operand>{opt::Operand(
  119. spv_operand_type_t::SPV_OPERAND_TYPE_ID, {message_.fresh_id()})}));
  120. // If we split before OpPhi instructions, we need to update their
  121. // predecessor operand so that the block they used to be inside is now the
  122. // predecessor.
  123. new_bb->ForEachPhiInst([block_to_split](opt::Instruction* phi_inst) {
  124. // The following assertion is a sanity check. It is guaranteed to hold
  125. // if IsApplicable holds.
  126. assert(phi_inst->NumInOperands() == 2 &&
  127. "We can only split a block before an OpPhi if block has exactly "
  128. "one predecessor.");
  129. phi_inst->SetInOperand(1, {block_to_split->id()});
  130. });
  131. // If the block being split was dead, the new block arising from the split is
  132. // also dead.
  133. if (transformation_context->GetFactManager()->BlockIsDead(
  134. block_to_split->id())) {
  135. transformation_context->GetFactManager()->AddFactBlockIsDead(
  136. message_.fresh_id());
  137. }
  138. // Invalidate all analyses
  139. ir_context->InvalidateAnalysesExceptFor(
  140. opt::IRContext::Analysis::kAnalysisNone);
  141. }
  142. protobufs::Transformation TransformationSplitBlock::ToMessage() const {
  143. protobufs::Transformation result;
  144. *result.mutable_split_block() = message_;
  145. return result;
  146. }
  147. } // namespace fuzz
  148. } // namespace spvtools