remove_selection_reduction_opportunity_finder.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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/reduce/remove_selection_reduction_opportunity_finder.h"
  15. #include "source/reduce/remove_selection_reduction_opportunity.h"
  16. namespace spvtools {
  17. namespace reduce {
  18. using opt::BasicBlock;
  19. using opt::IRContext;
  20. using opt::Instruction;
  21. namespace {
  22. const uint32_t kMergeNodeIndex = 0;
  23. const uint32_t kContinueNodeIndex = 1;
  24. } // namespace
  25. std::string RemoveSelectionReductionOpportunityFinder::GetName() const {
  26. return "RemoveSelectionReductionOpportunityFinder";
  27. }
  28. std::vector<std::unique_ptr<ReductionOpportunity>>
  29. RemoveSelectionReductionOpportunityFinder::GetAvailableOpportunities(
  30. IRContext* context) const {
  31. // Get all loop merge and continue blocks so we can check for these later.
  32. std::unordered_set<uint32_t> merge_and_continue_blocks_from_loops;
  33. for (auto& function : *context->module()) {
  34. for (auto& block : function) {
  35. if (auto merge_instruction = block.GetMergeInst()) {
  36. if (merge_instruction->opcode() == SpvOpLoopMerge) {
  37. uint32_t merge_block_id =
  38. merge_instruction->GetSingleWordOperand(kMergeNodeIndex);
  39. uint32_t continue_block_id =
  40. merge_instruction->GetSingleWordOperand(kContinueNodeIndex);
  41. merge_and_continue_blocks_from_loops.insert(merge_block_id);
  42. merge_and_continue_blocks_from_loops.insert(continue_block_id);
  43. }
  44. }
  45. }
  46. }
  47. // Return all selection headers where the OpSelectionMergeInstruction can be
  48. // removed.
  49. std::vector<std::unique_ptr<ReductionOpportunity>> result;
  50. for (auto& function : *context->module()) {
  51. for (auto& block : function) {
  52. if (auto merge_instruction = block.GetMergeInst()) {
  53. if (merge_instruction->opcode() == SpvOpSelectionMerge) {
  54. if (CanOpSelectionMergeBeRemoved(
  55. context, block, merge_instruction,
  56. merge_and_continue_blocks_from_loops)) {
  57. result.push_back(
  58. MakeUnique<RemoveSelectionReductionOpportunity>(&block));
  59. }
  60. }
  61. }
  62. }
  63. }
  64. return result;
  65. }
  66. bool RemoveSelectionReductionOpportunityFinder::CanOpSelectionMergeBeRemoved(
  67. IRContext* context, const BasicBlock& header_block,
  68. Instruction* merge_instruction,
  69. std::unordered_set<uint32_t> merge_and_continue_blocks_from_loops) {
  70. assert(header_block.GetMergeInst() == merge_instruction &&
  71. "CanOpSelectionMergeBeRemoved(...): header block and merge "
  72. "instruction mismatch");
  73. // The OpSelectionMerge instruction is needed if either of the following are
  74. // true.
  75. //
  76. // 1. The header block has at least two (unique) successors that are not
  77. // merge or continue blocks of a loop.
  78. //
  79. // 2. The predecessors of the merge block are "using" the merge block to avoid
  80. // divergence. In other words, there exists a predecessor of the merge block
  81. // that has a successor that is not the merge block of this construct and not
  82. // a merge or continue block of a loop.
  83. // 1.
  84. {
  85. uint32_t divergent_successor_count = 0;
  86. std::unordered_set<uint32_t> seen_successors;
  87. header_block.ForEachSuccessorLabel(
  88. [&seen_successors, &merge_and_continue_blocks_from_loops,
  89. &divergent_successor_count](uint32_t successor) {
  90. // Not already seen.
  91. if (seen_successors.find(successor) == seen_successors.end()) {
  92. seen_successors.insert(successor);
  93. // Not a loop continue or merge.
  94. if (merge_and_continue_blocks_from_loops.find(successor) ==
  95. merge_and_continue_blocks_from_loops.end()) {
  96. ++divergent_successor_count;
  97. }
  98. }
  99. });
  100. if (divergent_successor_count > 1) {
  101. return false;
  102. }
  103. }
  104. // 2.
  105. {
  106. uint32_t merge_block_id =
  107. merge_instruction->GetSingleWordOperand(kMergeNodeIndex);
  108. for (uint32_t predecessor_block_id :
  109. context->cfg()->preds(merge_block_id)) {
  110. const BasicBlock* predecessor_block =
  111. context->cfg()->block(predecessor_block_id);
  112. assert(predecessor_block);
  113. bool found_divergent_successor = false;
  114. predecessor_block->ForEachSuccessorLabel(
  115. [&found_divergent_successor, merge_block_id,
  116. &merge_and_continue_blocks_from_loops](uint32_t successor_id) {
  117. // The successor is not the merge block, nor a loop merge or
  118. // continue.
  119. if (successor_id != merge_block_id &&
  120. merge_and_continue_blocks_from_loops.find(successor_id) ==
  121. merge_and_continue_blocks_from_loops.end()) {
  122. found_divergent_successor = true;
  123. }
  124. });
  125. if (found_divergent_successor) {
  126. return false;
  127. }
  128. }
  129. }
  130. return true;
  131. }
  132. } // namespace reduce
  133. } // namespace spvtools