transformation_add_dead_continue.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. protobufs::TransformationAddDeadContinue message)
  20. : message_(std::move(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* ir_context,
  32. const TransformationContext& transformation_context) const {
  33. // First, we check that a constant with the same value as
  34. // |message_.continue_condition_value| is present.
  35. const auto bool_id = fuzzerutil::MaybeGetBoolConstant(
  36. ir_context, transformation_context, message_.continue_condition_value(),
  37. false);
  38. if (!bool_id) {
  39. // The required constant is not present, so the transformation cannot be
  40. // applied.
  41. return false;
  42. }
  43. // Check that |message_.from_block| really is a block id.
  44. opt::BasicBlock* bb_from =
  45. fuzzerutil::MaybeFindBlock(ir_context, message_.from_block());
  46. if (bb_from == nullptr) {
  47. return false;
  48. }
  49. // Check that |message_.from_block| ends with an unconditional branch.
  50. if (bb_from->terminator()->opcode() != spv::Op::OpBranch) {
  51. // The block associated with the id does not end with an unconditional
  52. // branch.
  53. return false;
  54. }
  55. assert(bb_from != nullptr &&
  56. "We should have found a block if this line of code is reached.");
  57. assert(
  58. bb_from->id() == message_.from_block() &&
  59. "The id of the block we found should match the source id for the break.");
  60. // Get the header for the innermost loop containing |message_.from_block|.
  61. // Because the structured CFG analysis does not regard a loop header as part
  62. // of the loop it heads, we check first whether bb_from is a loop header
  63. // before using the structured CFG analysis.
  64. auto loop_header =
  65. bb_from->IsLoopHeader()
  66. ? message_.from_block()
  67. : ir_context->GetStructuredCFGAnalysis()->ContainingLoop(
  68. message_.from_block());
  69. if (!loop_header) {
  70. return false;
  71. }
  72. auto continue_block =
  73. ir_context->cfg()->block(loop_header)->ContinueBlockId();
  74. if (!ir_context->IsReachable(*ir_context->cfg()->block(continue_block))) {
  75. // If the loop's continue block is unreachable, we conservatively do not
  76. // allow adding a dead continue, to avoid the compilations that arise due to
  77. // the lack of sensible dominance information for unreachable blocks.
  78. return false;
  79. }
  80. if (fuzzerutil::BlockIsInLoopContinueConstruct(
  81. ir_context, message_.from_block(), loop_header)) {
  82. // We cannot jump to the continue target from the continue construct.
  83. return false;
  84. }
  85. if (ir_context->GetStructuredCFGAnalysis()->IsMergeBlock(continue_block)) {
  86. // A branch straight to the continue target that is also a merge block might
  87. // break the property that a construct header must dominate its merge block
  88. // (if the merge block is reachable).
  89. return false;
  90. }
  91. // Check whether the data passed to extend OpPhi instructions is appropriate.
  92. if (!fuzzerutil::PhiIdsOkForNewEdge(ir_context, bb_from,
  93. ir_context->cfg()->block(continue_block),
  94. message_.phi_id())) {
  95. return false;
  96. }
  97. // Adding the dead break is only valid if SPIR-V rules related to dominance
  98. // hold.
  99. return fuzzerutil::NewTerminatorPreservesDominationRules(
  100. ir_context, message_.from_block(),
  101. fuzzerutil::CreateUnreachableEdgeInstruction(
  102. ir_context, message_.from_block(), continue_block, bool_id));
  103. }
  104. void TransformationAddDeadContinue::Apply(
  105. opt::IRContext* ir_context,
  106. TransformationContext* transformation_context) const {
  107. auto bb_from = ir_context->cfg()->block(message_.from_block());
  108. auto continue_block =
  109. bb_from->IsLoopHeader()
  110. ? bb_from->ContinueBlockId()
  111. : ir_context->GetStructuredCFGAnalysis()->LoopContinueBlock(
  112. message_.from_block());
  113. assert(continue_block && "message_.from_block must be in a loop.");
  114. fuzzerutil::AddUnreachableEdgeAndUpdateOpPhis(
  115. ir_context, bb_from, ir_context->cfg()->block(continue_block),
  116. fuzzerutil::MaybeGetBoolConstant(ir_context, *transformation_context,
  117. message_.continue_condition_value(),
  118. false),
  119. message_.phi_id());
  120. // Invalidate all analyses
  121. ir_context->InvalidateAnalysesExceptFor(
  122. opt::IRContext::Analysis::kAnalysisNone);
  123. }
  124. protobufs::Transformation TransformationAddDeadContinue::ToMessage() const {
  125. protobufs::Transformation result;
  126. *result.mutable_add_dead_continue() = message_;
  127. return result;
  128. }
  129. std::unordered_set<uint32_t> TransformationAddDeadContinue::GetFreshIds()
  130. const {
  131. return std::unordered_set<uint32_t>();
  132. }
  133. } // namespace fuzz
  134. } // namespace spvtools