block_merge_util.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // Copyright (c) 2017 The Khronos Group Inc.
  2. // Copyright (c) 2017 Valve Corporation
  3. // Copyright (c) 2017 LunarG Inc.
  4. // Copyright (c) 2019 Google LLC
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. #include "block_merge_util.h"
  18. namespace spvtools {
  19. namespace opt {
  20. namespace blockmergeutil {
  21. namespace {
  22. // Returns true if |block| contains a merge instruction.
  23. bool IsHeader(BasicBlock* block) { return block->GetMergeInst() != nullptr; }
  24. // Returns true if |id| contains a merge instruction.
  25. bool IsHeader(IRContext* context, uint32_t id) {
  26. return IsHeader(
  27. context->get_instr_block(context->get_def_use_mgr()->GetDef(id)));
  28. }
  29. // Returns true if |id| is the merge target of a merge instruction.
  30. bool IsMerge(IRContext* context, uint32_t id) {
  31. return !context->get_def_use_mgr()->WhileEachUse(
  32. id, [](Instruction* user, uint32_t index) {
  33. spv::Op op = user->opcode();
  34. if ((op == spv::Op::OpLoopMerge || op == spv::Op::OpSelectionMerge) &&
  35. index == 0u) {
  36. return false;
  37. }
  38. return true;
  39. });
  40. }
  41. // Returns true if |block| is the merge target of a merge instruction.
  42. bool IsMerge(IRContext* context, BasicBlock* block) {
  43. return IsMerge(context, block->id());
  44. }
  45. // Returns true if |id| is the continue target of a merge instruction.
  46. bool IsContinue(IRContext* context, uint32_t id) {
  47. return !context->get_def_use_mgr()->WhileEachUse(
  48. id, [](Instruction* user, uint32_t index) {
  49. spv::Op op = user->opcode();
  50. if (op == spv::Op::OpLoopMerge && index == 1u) {
  51. return false;
  52. }
  53. return true;
  54. });
  55. }
  56. // Removes any OpPhi instructions in |block|, which should have exactly one
  57. // predecessor, replacing uses of OpPhi ids with the ids associated with the
  58. // predecessor.
  59. void EliminateOpPhiInstructions(IRContext* context, BasicBlock* block) {
  60. block->ForEachPhiInst([context](Instruction* phi) {
  61. assert(2 == phi->NumInOperands() &&
  62. "A block can only have one predecessor for block merging to make "
  63. "sense.");
  64. context->ReplaceAllUsesWith(phi->result_id(),
  65. phi->GetSingleWordInOperand(0));
  66. context->KillInst(phi);
  67. });
  68. }
  69. } // Anonymous namespace
  70. bool CanMergeWithSuccessor(IRContext* context, BasicBlock* block) {
  71. // Find block with single successor which has no other predecessors.
  72. auto ii = block->end();
  73. --ii;
  74. Instruction* br = &*ii;
  75. if (br->opcode() != spv::Op::OpBranch) {
  76. return false;
  77. }
  78. const uint32_t lab_id = br->GetSingleWordInOperand(0);
  79. if (context->cfg()->preds(lab_id).size() != 1) {
  80. return false;
  81. }
  82. bool pred_is_merge = IsMerge(context, block);
  83. bool succ_is_merge = IsMerge(context, lab_id);
  84. if (pred_is_merge && succ_is_merge) {
  85. // Cannot merge two merges together.
  86. return false;
  87. }
  88. // Note: This means that the instructions in a break block will execute as if
  89. // they were still diverged according to the loop iteration. This restricts
  90. // potential transformations an implementation may perform on the IR to match
  91. // shader author expectations. Similarly, instructions in the loop construct
  92. // cannot be moved into the continue construct unless it can be proven that
  93. // invocations are always converged.
  94. if (succ_is_merge && context->get_feature_mgr()->HasExtension(
  95. kSPV_KHR_maximal_reconvergence)) {
  96. return false;
  97. }
  98. if (pred_is_merge && IsContinue(context, lab_id)) {
  99. // Cannot merge a continue target with a merge block.
  100. return false;
  101. }
  102. Instruction* merge_inst = block->GetMergeInst();
  103. const bool pred_is_header = IsHeader(block);
  104. if (pred_is_header && lab_id != merge_inst->GetSingleWordInOperand(0u)) {
  105. bool succ_is_header = IsHeader(context, lab_id);
  106. if (pred_is_header && succ_is_header) {
  107. // Cannot merge two headers together when the successor is not the merge
  108. // block of the predecessor.
  109. return false;
  110. }
  111. // If this is a header block and the successor is not its merge, we must
  112. // be careful about which blocks we are willing to merge together.
  113. // OpLoopMerge must be followed by a conditional or unconditional branch.
  114. // The merge must be a loop merge because a selection merge cannot be
  115. // followed by an unconditional branch.
  116. BasicBlock* succ_block = context->get_instr_block(lab_id);
  117. spv::Op succ_term_op = succ_block->terminator()->opcode();
  118. assert(merge_inst->opcode() == spv::Op::OpLoopMerge);
  119. if (succ_term_op != spv::Op::OpBranch &&
  120. succ_term_op != spv::Op::OpBranchConditional) {
  121. return false;
  122. }
  123. }
  124. if (succ_is_merge || IsContinue(context, lab_id)) {
  125. auto* struct_cfg = context->GetStructuredCFGAnalysis();
  126. auto switch_block_id = struct_cfg->ContainingSwitch(block->id());
  127. if (switch_block_id) {
  128. auto switch_merge_id = struct_cfg->SwitchMergeBlock(switch_block_id);
  129. const auto* switch_inst =
  130. &*block->GetParent()->FindBlock(switch_block_id)->tail();
  131. for (uint32_t i = 1; i < switch_inst->NumInOperands(); i += 2) {
  132. auto target_id = switch_inst->GetSingleWordInOperand(i);
  133. if (target_id == block->id() && target_id != switch_merge_id) {
  134. // Case constructs must be structurally dominated by the OpSwitch.
  135. // Since the successor is the merge/continue for another construct,
  136. // merging the blocks would break that requirement.
  137. return false;
  138. }
  139. }
  140. }
  141. }
  142. return true;
  143. }
  144. void MergeWithSuccessor(IRContext* context, Function* func,
  145. Function::iterator bi) {
  146. assert(CanMergeWithSuccessor(context, &*bi) &&
  147. "Precondition failure for MergeWithSuccessor: it must be legal to "
  148. "merge the block and its successor.");
  149. auto ii = bi->end();
  150. --ii;
  151. Instruction* br = &*ii;
  152. const uint32_t lab_id = br->GetSingleWordInOperand(0);
  153. Instruction* merge_inst = bi->GetMergeInst();
  154. bool pred_is_header = IsHeader(&*bi);
  155. // Merge blocks.
  156. context->KillInst(br);
  157. auto sbi = bi;
  158. for (; sbi != func->end(); ++sbi)
  159. if (sbi->id() == lab_id) break;
  160. // If bi is sbi's only predecessor, it dominates sbi and thus
  161. // sbi must follow bi in func's ordering.
  162. assert(sbi != func->end());
  163. if (sbi->tail()->opcode() == spv::Op::OpSwitch &&
  164. sbi->MergeBlockIdIfAny() != 0) {
  165. context->InvalidateAnalyses(IRContext::Analysis::kAnalysisStructuredCFG);
  166. }
  167. // Update the inst-to-block mapping for the instructions in sbi.
  168. for (auto& inst : *sbi) {
  169. context->set_instr_block(&inst, &*bi);
  170. }
  171. EliminateOpPhiInstructions(context, &*sbi);
  172. // Now actually move the instructions.
  173. bi->AddInstructions(&*sbi);
  174. if (merge_inst) {
  175. if (pred_is_header && lab_id == merge_inst->GetSingleWordInOperand(0u)) {
  176. // Merging the header and merge blocks, so remove the structured control
  177. // flow declaration.
  178. context->KillInst(merge_inst);
  179. } else {
  180. // Move OpLine/OpNoLine information to merge_inst. This solves
  181. // the validation error that OpLine is placed between OpLoopMerge
  182. // and OpBranchConditional.
  183. auto terminator = bi->terminator();
  184. auto& vec = terminator->dbg_line_insts();
  185. if (vec.size() > 0) {
  186. merge_inst->ClearDbgLineInsts();
  187. auto& new_vec = merge_inst->dbg_line_insts();
  188. new_vec.insert(new_vec.end(), vec.begin(), vec.end());
  189. terminator->ClearDbgLineInsts();
  190. for (auto& l_inst : new_vec)
  191. context->get_def_use_mgr()->AnalyzeInstDefUse(&l_inst);
  192. }
  193. // Clear debug scope of terminator to avoid DebugScope
  194. // emitted between terminator and merge.
  195. terminator->SetDebugScope(DebugScope(kNoDebugScope, kNoInlinedAt));
  196. // Move the merge instruction to just before the terminator.
  197. merge_inst->InsertBefore(terminator);
  198. }
  199. }
  200. context->ReplaceAllUsesWith(lab_id, bi->id());
  201. context->KillInst(sbi->GetLabelInst());
  202. (void)sbi.Erase();
  203. }
  204. } // namespace blockmergeutil
  205. } // namespace opt
  206. } // namespace spvtools