InReadableOrder.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // Copyright (C) 2016 Google, Inc.
  3. //
  4. // All rights reserved.
  5. //
  6. // Redistribution and use in source and binary forms, with or without
  7. // modification, are permitted provided that the following conditions
  8. // are met:
  9. //
  10. // Redistributions of source code must retain the above copyright
  11. // notice, this list of conditions and the following disclaimer.
  12. //
  13. // Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following
  15. // disclaimer in the documentation and/or other materials provided
  16. // with the distribution.
  17. //
  18. // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
  19. // contributors may be used to endorse or promote products derived
  20. // from this software without specific prior written permission.
  21. //
  22. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. // COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. // ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. // POSSIBILITY OF SUCH DAMAGE.
  34. // The SPIR-V spec requires code blocks to appear in an order satisfying the
  35. // dominator-tree direction (ie, dominator before the dominated). This is,
  36. // actually, easy to achieve: any pre-order CFG traversal algorithm will do it.
  37. // Because such algorithms visit a block only after traversing some path to it
  38. // from the root, they necessarily visit the block's idom first.
  39. //
  40. // But not every graph-traversal algorithm outputs blocks in an order that
  41. // appears logical to human readers. The problem is that unrelated branches may
  42. // be interspersed with each other, and merge blocks may come before some of the
  43. // branches being merged.
  44. //
  45. // A good, human-readable order of blocks may be achieved by performing
  46. // depth-first search but delaying merge nodes until after all their branches
  47. // have been visited. This is implemented below by the inReadableOrder()
  48. // function.
  49. #include "spvIR.h"
  50. #include <cassert>
  51. #include <unordered_set>
  52. using spv::Block;
  53. using spv::Id;
  54. namespace {
  55. // Traverses CFG in a readable order, invoking a pre-set callback on each block.
  56. // Use by calling visit() on the root block.
  57. class ReadableOrderTraverser {
  58. public:
  59. ReadableOrderTraverser(std::function<void(Block*, spv::ReachReason, Block*)> callback)
  60. : callback_(callback) {}
  61. // Visits the block if it hasn't been visited already and isn't currently
  62. // being delayed. Invokes callback(block, why, header), then descends into its
  63. // successors. Delays merge-block and continue-block processing until all
  64. // the branches have been completed. If |block| is an unreachable merge block or
  65. // an unreachable continue target, then |header| is the corresponding header block.
  66. void visit(Block* block, spv::ReachReason why, Block* header)
  67. {
  68. assert(block);
  69. if (why == spv::ReachViaControlFlow) {
  70. reachableViaControlFlow_.insert(block);
  71. }
  72. if (visited_.count(block) || delayed_.count(block))
  73. return;
  74. callback_(block, why, header);
  75. visited_.insert(block);
  76. Block* mergeBlock = nullptr;
  77. Block* continueBlock = nullptr;
  78. auto mergeInst = block->getMergeInstruction();
  79. if (mergeInst) {
  80. Id mergeId = mergeInst->getIdOperand(0);
  81. mergeBlock = block->getParent().getParent().getInstruction(mergeId)->getBlock();
  82. delayed_.insert(mergeBlock);
  83. if (mergeInst->getOpCode() == spv::OpLoopMerge) {
  84. Id continueId = mergeInst->getIdOperand(1);
  85. continueBlock =
  86. block->getParent().getParent().getInstruction(continueId)->getBlock();
  87. delayed_.insert(continueBlock);
  88. }
  89. }
  90. if (why == spv::ReachViaControlFlow) {
  91. const auto& successors = block->getSuccessors();
  92. for (auto it = successors.cbegin(); it != successors.cend(); ++it)
  93. visit(*it, why, nullptr);
  94. }
  95. if (continueBlock) {
  96. const spv::ReachReason continueWhy =
  97. (reachableViaControlFlow_.count(continueBlock) > 0)
  98. ? spv::ReachViaControlFlow
  99. : spv::ReachDeadContinue;
  100. delayed_.erase(continueBlock);
  101. visit(continueBlock, continueWhy, block);
  102. }
  103. if (mergeBlock) {
  104. const spv::ReachReason mergeWhy =
  105. (reachableViaControlFlow_.count(mergeBlock) > 0)
  106. ? spv::ReachViaControlFlow
  107. : spv::ReachDeadMerge;
  108. delayed_.erase(mergeBlock);
  109. visit(mergeBlock, mergeWhy, block);
  110. }
  111. }
  112. private:
  113. std::function<void(Block*, spv::ReachReason, Block*)> callback_;
  114. // Whether a block has already been visited or is being delayed.
  115. std::unordered_set<Block *> visited_, delayed_;
  116. // The set of blocks that actually are reached via control flow.
  117. std::unordered_set<Block *> reachableViaControlFlow_;
  118. };
  119. }
  120. void spv::inReadableOrder(Block* root, std::function<void(Block*, spv::ReachReason, Block*)> callback)
  121. {
  122. ReadableOrderTraverser(callback).visit(root, spv::ReachViaControlFlow, nullptr);
  123. }