transformation_wrap_region_in_selection.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright (c) 2020 Vasyl Teliman
  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_wrap_region_in_selection.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. namespace spvtools {
  17. namespace fuzz {
  18. TransformationWrapRegionInSelection::TransformationWrapRegionInSelection(
  19. protobufs::TransformationWrapRegionInSelection message)
  20. : message_(std::move(message)) {}
  21. TransformationWrapRegionInSelection::TransformationWrapRegionInSelection(
  22. uint32_t region_entry_block_id, uint32_t region_exit_block_id,
  23. bool branch_condition) {
  24. message_.set_region_entry_block_id(region_entry_block_id);
  25. message_.set_region_exit_block_id(region_exit_block_id);
  26. message_.set_branch_condition(branch_condition);
  27. }
  28. bool TransformationWrapRegionInSelection::IsApplicable(
  29. opt::IRContext* ir_context,
  30. const TransformationContext& transformation_context) const {
  31. // Check that it is possible to outline a region of blocks without breaking
  32. // domination and structured control flow rules.
  33. if (!IsApplicableToBlockRange(ir_context, message_.region_entry_block_id(),
  34. message_.region_exit_block_id())) {
  35. return false;
  36. }
  37. // There must exist an irrelevant boolean constant to be used as a condition
  38. // in the OpBranchConditional instruction.
  39. return fuzzerutil::MaybeGetBoolConstant(ir_context, transformation_context,
  40. message_.branch_condition(),
  41. true) != 0;
  42. }
  43. void TransformationWrapRegionInSelection::Apply(
  44. opt::IRContext* ir_context,
  45. TransformationContext* transformation_context) const {
  46. auto* new_header_block =
  47. ir_context->cfg()->block(message_.region_entry_block_id());
  48. assert(new_header_block->terminator()->opcode() == spv::Op::OpBranch &&
  49. "This condition should have been checked in the IsApplicable");
  50. const auto successor_id =
  51. new_header_block->terminator()->GetSingleWordInOperand(0);
  52. // Change |entry_block|'s terminator to |OpBranchConditional|.
  53. new_header_block->terminator()->SetOpcode(spv::Op::OpBranchConditional);
  54. new_header_block->terminator()->SetInOperands(
  55. {{SPV_OPERAND_TYPE_ID,
  56. {fuzzerutil::MaybeGetBoolConstant(ir_context, *transformation_context,
  57. message_.branch_condition(), true)}},
  58. {SPV_OPERAND_TYPE_ID, {successor_id}},
  59. {SPV_OPERAND_TYPE_ID, {successor_id}}});
  60. // Insert OpSelectionMerge before the terminator.
  61. new_header_block->terminator()->InsertBefore(MakeUnique<opt::Instruction>(
  62. ir_context, spv::Op::OpSelectionMerge, 0, 0,
  63. opt::Instruction::OperandList{
  64. {SPV_OPERAND_TYPE_ID, {message_.region_exit_block_id()}},
  65. {SPV_OPERAND_TYPE_SELECTION_CONTROL,
  66. {uint32_t(spv::SelectionControlMask::MaskNone)}}}));
  67. // We've change the module so we must invalidate analyses.
  68. ir_context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone);
  69. }
  70. protobufs::Transformation TransformationWrapRegionInSelection::ToMessage()
  71. const {
  72. protobufs::Transformation result;
  73. *result.mutable_wrap_region_in_selection() = message_;
  74. return result;
  75. }
  76. bool TransformationWrapRegionInSelection::IsApplicableToBlockRange(
  77. opt::IRContext* ir_context, uint32_t header_block_candidate_id,
  78. uint32_t merge_block_candidate_id) {
  79. // Check that |header_block_candidate_id| and |merge_block_candidate_id| are
  80. // valid.
  81. const auto* header_block_candidate =
  82. fuzzerutil::MaybeFindBlock(ir_context, header_block_candidate_id);
  83. if (!header_block_candidate) {
  84. return false;
  85. }
  86. const auto* merge_block_candidate =
  87. fuzzerutil::MaybeFindBlock(ir_context, merge_block_candidate_id);
  88. if (!merge_block_candidate) {
  89. return false;
  90. }
  91. // |header_block_candidate| and |merge_block_candidate| must be from the same
  92. // function.
  93. if (header_block_candidate->GetParent() !=
  94. merge_block_candidate->GetParent()) {
  95. return false;
  96. }
  97. const auto* dominator_analysis =
  98. ir_context->GetDominatorAnalysis(header_block_candidate->GetParent());
  99. const auto* postdominator_analysis =
  100. ir_context->GetPostDominatorAnalysis(header_block_candidate->GetParent());
  101. if (!dominator_analysis->StrictlyDominates(header_block_candidate,
  102. merge_block_candidate) ||
  103. !postdominator_analysis->StrictlyDominates(merge_block_candidate,
  104. header_block_candidate)) {
  105. return false;
  106. }
  107. // |header_block_candidate| can't be a header since we are about to make it
  108. // one.
  109. if (header_block_candidate->GetMergeInst()) {
  110. return false;
  111. }
  112. // |header_block_candidate| must have an OpBranch terminator.
  113. if (header_block_candidate->terminator()->opcode() != spv::Op::OpBranch) {
  114. return false;
  115. }
  116. // Every header block must have a unique merge block. Thus,
  117. // |merge_block_candidate| can't be a merge block of some other header.
  118. auto* structured_cfg = ir_context->GetStructuredCFGAnalysis();
  119. if (structured_cfg->IsMergeBlock(merge_block_candidate_id)) {
  120. return false;
  121. }
  122. // |header_block_candidate|'s containing construct must also contain
  123. // |merge_block_candidate|.
  124. //
  125. // ContainingConstruct will return the id of a loop header for a block in the
  126. // loop's continue construct. Thus, we must also check the case when one of
  127. // the candidates is in continue construct and the other one is not.
  128. if (structured_cfg->ContainingConstruct(header_block_candidate_id) !=
  129. structured_cfg->ContainingConstruct(merge_block_candidate_id) ||
  130. structured_cfg->IsInContinueConstruct(header_block_candidate_id) !=
  131. structured_cfg->IsInContinueConstruct(merge_block_candidate_id)) {
  132. return false;
  133. }
  134. return true;
  135. }
  136. std::unordered_set<uint32_t> TransformationWrapRegionInSelection::GetFreshIds()
  137. const {
  138. return std::unordered_set<uint32_t>();
  139. }
  140. } // namespace fuzz
  141. } // namespace spvtools