transformation_add_dead_continue.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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_add_dead_continue.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. namespace spvtools {
  17. namespace fuzz {
  18. TransformationAddDeadContinue::TransformationAddDeadContinue(
  19. const spvtools::fuzz::protobufs::TransformationAddDeadContinue& message)
  20. : message_(message) {}
  21. TransformationAddDeadContinue::TransformationAddDeadContinue(
  22. uint32_t from_block, bool continue_condition_value,
  23. std::vector<uint32_t> phi_id) {
  24. message_.set_from_block(from_block);
  25. message_.set_continue_condition_value(continue_condition_value);
  26. for (auto id : phi_id) {
  27. message_.add_phi_id(id);
  28. }
  29. }
  30. bool TransformationAddDeadContinue::IsApplicable(
  31. opt::IRContext* context, const FactManager& /*unused*/) const {
  32. // First, we check that a constant with the same value as
  33. // |message_.continue_condition_value| is present.
  34. opt::analysis::Bool bool_type;
  35. auto registered_bool_type =
  36. context->get_type_mgr()->GetRegisteredType(&bool_type);
  37. if (!registered_bool_type) {
  38. return false;
  39. }
  40. opt::analysis::BoolConstant bool_constant(
  41. registered_bool_type->AsBool(), message_.continue_condition_value());
  42. if (!context->get_constant_mgr()->FindConstant(&bool_constant)) {
  43. // The required constant is not present, so the transformation cannot be
  44. // applied.
  45. return false;
  46. }
  47. // Check that |message_.from_block| really is a block id.
  48. opt::BasicBlock* bb_from =
  49. fuzzerutil::MaybeFindBlock(context, message_.from_block());
  50. if (bb_from == nullptr) {
  51. return false;
  52. }
  53. // Check that |message_.from_block| ends with an unconditional branch.
  54. if (bb_from->terminator()->opcode() != SpvOpBranch) {
  55. // The block associated with the id does not end with an unconditional
  56. // branch.
  57. return false;
  58. }
  59. assert(bb_from != nullptr &&
  60. "We should have found a block if this line of code is reached.");
  61. assert(
  62. bb_from->id() == message_.from_block() &&
  63. "The id of the block we found should match the source id for the break.");
  64. // Get the header for the innermost loop containing |message_.from_block|.
  65. // Because the structured CFG analysis does not regard a loop header as part
  66. // of the loop it heads, we check first whether bb_from is a loop header
  67. // before using the structured CFG analysis.
  68. auto loop_header = bb_from->IsLoopHeader()
  69. ? message_.from_block()
  70. : context->GetStructuredCFGAnalysis()->ContainingLoop(
  71. message_.from_block());
  72. if (!loop_header) {
  73. return false;
  74. }
  75. auto continue_block = context->cfg()->block(loop_header)->ContinueBlockId();
  76. if (!fuzzerutil::BlockIsReachableInItsFunction(
  77. context, context->cfg()->block(continue_block))) {
  78. // If the loop's continue block is unreachable, we conservatively do not
  79. // allow adding a dead continue, to avoid the compilations that arise due to
  80. // the lack of sensible dominance information for unreachable blocks.
  81. return false;
  82. }
  83. if (fuzzerutil::BlockIsInLoopContinueConstruct(context, message_.from_block(),
  84. loop_header)) {
  85. // We cannot jump to the continue target from the continue construct.
  86. return false;
  87. }
  88. if (context->GetStructuredCFGAnalysis()->IsMergeBlock(continue_block)) {
  89. // A branch straight to the continue target that is also a merge block might
  90. // break the property that a construct header must dominate its merge block
  91. // (if the merge block is reachable).
  92. return false;
  93. }
  94. // Check that adding the continue would not violate the property that a
  95. // definition must dominate all of its uses.
  96. if (!fuzzerutil::NewEdgeRespectsUseDefDominance(
  97. context, bb_from, context->cfg()->block(continue_block))) {
  98. return false;
  99. }
  100. // The transformation is good if and only if the given phi ids are sufficient
  101. // to extend relevant OpPhi instructions in the continue block.
  102. return fuzzerutil::PhiIdsOkForNewEdge(context, bb_from,
  103. context->cfg()->block(continue_block),
  104. message_.phi_id());
  105. }
  106. void TransformationAddDeadContinue::Apply(opt::IRContext* context,
  107. FactManager* /*unused*/) const {
  108. auto bb_from = context->cfg()->block(message_.from_block());
  109. auto continue_block =
  110. bb_from->IsLoopHeader()
  111. ? bb_from->ContinueBlockId()
  112. : context->GetStructuredCFGAnalysis()->LoopContinueBlock(
  113. message_.from_block());
  114. assert(continue_block &&
  115. "Precondition for this transformation requires that "
  116. "message_.from_block must be in a loop.");
  117. fuzzerutil::AddUnreachableEdgeAndUpdateOpPhis(
  118. context, bb_from, context->cfg()->block(continue_block),
  119. message_.continue_condition_value(), message_.phi_id());
  120. // Invalidate all analyses
  121. context->InvalidateAnalysesExceptFor(opt::IRContext::Analysis::kAnalysisNone);
  122. }
  123. protobufs::Transformation TransformationAddDeadContinue::ToMessage() const {
  124. protobufs::Transformation result;
  125. *result.mutable_add_dead_continue() = message_;
  126. return result;
  127. }
  128. } // namespace fuzz
  129. } // namespace spvtools