transformation_move_block_down.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_move_block_down.h"
  15. #include "source/opt/basic_block.h"
  16. namespace spvtools {
  17. namespace fuzz {
  18. TransformationMoveBlockDown::TransformationMoveBlockDown(
  19. protobufs::TransformationMoveBlockDown message)
  20. : message_(std::move(message)) {}
  21. TransformationMoveBlockDown::TransformationMoveBlockDown(uint32_t id) {
  22. message_.set_block_id(id);
  23. }
  24. bool TransformationMoveBlockDown::IsApplicable(
  25. opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
  26. // Go through every block in every function, looking for a block whose id
  27. // matches that of the block we want to consider moving down.
  28. for (auto& function : *ir_context->module()) {
  29. for (auto block_it = function.begin(); block_it != function.end();
  30. ++block_it) {
  31. if (block_it->id() == message_.block_id()) {
  32. // We have found a match.
  33. if (block_it == function.begin()) {
  34. // The block is the first one appearing in the function. We are not
  35. // allowed to move this block down.
  36. return false;
  37. }
  38. // Record the block we would like to consider moving down.
  39. opt::BasicBlock* block_matching_id = &*block_it;
  40. if (!ir_context->GetDominatorAnalysis(&function)->IsReachable(
  41. block_matching_id)) {
  42. // The block is not reachable. We are not allowed to move it down.
  43. return false;
  44. }
  45. // Now see whether there is some block following that block in program
  46. // order.
  47. ++block_it;
  48. if (block_it == function.end()) {
  49. // There is no such block; i.e., the block we are considering moving
  50. // is the last one in the function. The transformation thus does not
  51. // apply.
  52. return false;
  53. }
  54. opt::BasicBlock* next_block_in_program_order = &*block_it;
  55. // We can move the block of interest down if and only if it does not
  56. // dominate the block that comes next.
  57. return !ir_context->GetDominatorAnalysis(&function)->Dominates(
  58. block_matching_id, next_block_in_program_order);
  59. }
  60. }
  61. }
  62. // We did not find a matching block, so the transformation is not applicable:
  63. // there is no relevant block to move.
  64. return false;
  65. }
  66. void TransformationMoveBlockDown::Apply(
  67. opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
  68. // Go through every block in every function, looking for a block whose id
  69. // matches that of the block we want to move down.
  70. for (auto& function : *ir_context->module()) {
  71. for (auto block_it = function.begin(); block_it != function.end();
  72. ++block_it) {
  73. if (block_it->id() == message_.block_id()) {
  74. ++block_it;
  75. assert(block_it != function.end() &&
  76. "To be able to move a block down, it needs to have a "
  77. "program-order successor.");
  78. function.MoveBasicBlockToAfter(message_.block_id(), &*block_it);
  79. // For performance, it is vital to keep the dominator analysis valid
  80. // (which due to https://github.com/KhronosGroup/SPIRV-Tools/issues/2889
  81. // requires keeping the CFG analysis valid).
  82. ir_context->InvalidateAnalysesExceptFor(
  83. opt::IRContext::Analysis::kAnalysisDefUse |
  84. opt::IRContext::Analysis::kAnalysisCFG |
  85. opt::IRContext::Analysis::kAnalysisDominatorAnalysis);
  86. return;
  87. }
  88. }
  89. }
  90. assert(false && "No block was found to move down.");
  91. }
  92. protobufs::Transformation TransformationMoveBlockDown::ToMessage() const {
  93. protobufs::Transformation result;
  94. *result.mutable_move_block_down() = message_;
  95. return result;
  96. }
  97. std::unordered_set<uint32_t> TransformationMoveBlockDown::GetFreshIds() const {
  98. return std::unordered_set<uint32_t>();
  99. }
  100. } // namespace fuzz
  101. } // namespace spvtools