construct.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. // Copyright (c) 2015-2016 The Khronos Group Inc.
  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/val/construct.h"
  15. #include <cassert>
  16. #include <cstddef>
  17. #include <unordered_set>
  18. #include "source/val/function.h"
  19. #include "source/val/validation_state.h"
  20. namespace spvtools {
  21. namespace val {
  22. Construct::Construct(ConstructType construct_type, BasicBlock* entry,
  23. BasicBlock* exit, std::vector<Construct*> constructs)
  24. : type_(construct_type),
  25. corresponding_constructs_(constructs),
  26. entry_block_(entry),
  27. exit_block_(exit) {}
  28. ConstructType Construct::type() const { return type_; }
  29. const std::vector<Construct*>& Construct::corresponding_constructs() const {
  30. return corresponding_constructs_;
  31. }
  32. std::vector<Construct*>& Construct::corresponding_constructs() {
  33. return corresponding_constructs_;
  34. }
  35. bool ValidateConstructSize(ConstructType type, size_t size) {
  36. switch (type) {
  37. case ConstructType::kSelection:
  38. return size == 0;
  39. case ConstructType::kContinue:
  40. return size == 1;
  41. case ConstructType::kLoop:
  42. return size == 1;
  43. case ConstructType::kCase:
  44. return size >= 1;
  45. default:
  46. assert(1 == 0 && "Type not defined");
  47. }
  48. return false;
  49. }
  50. void Construct::set_corresponding_constructs(
  51. std::vector<Construct*> constructs) {
  52. assert(ValidateConstructSize(type_, constructs.size()));
  53. corresponding_constructs_ = constructs;
  54. }
  55. const BasicBlock* Construct::entry_block() const { return entry_block_; }
  56. BasicBlock* Construct::entry_block() { return entry_block_; }
  57. const BasicBlock* Construct::exit_block() const { return exit_block_; }
  58. BasicBlock* Construct::exit_block() { return exit_block_; }
  59. void Construct::set_exit(BasicBlock* block) { exit_block_ = block; }
  60. Construct::ConstructBlockSet Construct::blocks(Function* function) const {
  61. auto header = entry_block();
  62. auto merge = exit_block();
  63. assert(header);
  64. int header_depth = function->GetBlockDepth(const_cast<BasicBlock*>(header));
  65. ConstructBlockSet construct_blocks;
  66. std::unordered_set<BasicBlock*> corresponding_headers;
  67. for (auto& other : corresponding_constructs()) {
  68. // The corresponding header can be the same block as this construct's
  69. // header for loops with no loop construct. In those cases, don't add the
  70. // loop header as it prevents finding any blocks in the construct.
  71. if (type() != ConstructType::kContinue || other->entry_block() != header) {
  72. corresponding_headers.insert(other->entry_block());
  73. }
  74. }
  75. std::vector<BasicBlock*> stack;
  76. stack.push_back(const_cast<BasicBlock*>(header));
  77. while (!stack.empty()) {
  78. BasicBlock* block = stack.back();
  79. stack.pop_back();
  80. if (merge == block && ExitBlockIsMergeBlock()) {
  81. // Merge block is not part of the construct.
  82. continue;
  83. }
  84. if (corresponding_headers.count(block)) {
  85. // Entered a corresponding construct.
  86. continue;
  87. }
  88. int block_depth = function->GetBlockDepth(block);
  89. if (block_depth < header_depth) {
  90. // Broke to outer construct.
  91. continue;
  92. }
  93. // In a loop, the continue target is at a depth of the loop construct + 1.
  94. // A selection construct nested directly within the loop construct is also
  95. // at the same depth. It is valid, however, to branch directly to the
  96. // continue target from within the selection construct.
  97. if (block != header && block_depth == header_depth &&
  98. type() == ConstructType::kSelection &&
  99. block->is_type(kBlockTypeContinue)) {
  100. // Continued to outer construct.
  101. continue;
  102. }
  103. if (!construct_blocks.insert(block).second) continue;
  104. if (merge != block) {
  105. for (auto succ : *block->successors()) {
  106. // All blocks in the construct must be dominated by the header.
  107. if (header->dominates(*succ)) {
  108. stack.push_back(succ);
  109. }
  110. }
  111. }
  112. }
  113. return construct_blocks;
  114. }
  115. bool Construct::IsStructuredExit(ValidationState_t& _, BasicBlock* dest) const {
  116. // Structured Exits:
  117. // - Selection:
  118. // - branch to its merge
  119. // - branch to nearest enclosing loop merge or continue
  120. // - branch to nearest enclosing switch selection merge
  121. // - Loop:
  122. // - branch to its merge
  123. // - branch to its continue
  124. // - Continue:
  125. // - branch to loop header
  126. // - branch to loop merge
  127. //
  128. // Note: we will never see a case construct here.
  129. assert(type() != ConstructType::kCase);
  130. if (type() == ConstructType::kLoop) {
  131. auto header = entry_block();
  132. auto terminator = header->terminator();
  133. auto index = terminator - &_.ordered_instructions()[0];
  134. auto merge_inst = &_.ordered_instructions()[index - 1];
  135. auto merge_block_id = merge_inst->GetOperandAs<uint32_t>(0u);
  136. auto continue_block_id = merge_inst->GetOperandAs<uint32_t>(1u);
  137. if (dest->id() == merge_block_id || dest->id() == continue_block_id) {
  138. return true;
  139. }
  140. } else if (type() == ConstructType::kContinue) {
  141. auto loop_construct = corresponding_constructs()[0];
  142. auto header = loop_construct->entry_block();
  143. auto terminator = header->terminator();
  144. auto index = terminator - &_.ordered_instructions()[0];
  145. auto merge_inst = &_.ordered_instructions()[index - 1];
  146. auto merge_block_id = merge_inst->GetOperandAs<uint32_t>(0u);
  147. if (dest == header || dest->id() == merge_block_id) {
  148. return true;
  149. }
  150. } else {
  151. assert(type() == ConstructType::kSelection);
  152. if (dest == exit_block()) {
  153. return true;
  154. }
  155. // The next block in the traversal is either:
  156. // i. The header block that declares |block| as its merge block.
  157. // ii. The immediate dominator of |block|.
  158. auto NextBlock = [](const BasicBlock* block) -> const BasicBlock* {
  159. for (auto& use : block->label()->uses()) {
  160. if ((use.first->opcode() == SpvOpLoopMerge ||
  161. use.first->opcode() == SpvOpSelectionMerge) &&
  162. use.second == 1)
  163. return use.first->block();
  164. }
  165. return block->immediate_dominator();
  166. };
  167. bool seen_switch = false;
  168. auto header = entry_block();
  169. auto block = NextBlock(header);
  170. while (block) {
  171. auto terminator = block->terminator();
  172. auto index = terminator - &_.ordered_instructions()[0];
  173. auto merge_inst = &_.ordered_instructions()[index - 1];
  174. if (merge_inst->opcode() == SpvOpLoopMerge ||
  175. (header->terminator()->opcode() != SpvOpSwitch &&
  176. merge_inst->opcode() == SpvOpSelectionMerge &&
  177. terminator->opcode() == SpvOpSwitch)) {
  178. auto merge_target = merge_inst->GetOperandAs<uint32_t>(0u);
  179. auto merge_block = merge_inst->function()->GetBlock(merge_target).first;
  180. if (merge_block->dominates(*header)) {
  181. block = NextBlock(block);
  182. continue;
  183. }
  184. if ((!seen_switch || merge_inst->opcode() == SpvOpLoopMerge) &&
  185. dest->id() == merge_target) {
  186. return true;
  187. } else if (merge_inst->opcode() == SpvOpLoopMerge) {
  188. auto continue_target = merge_inst->GetOperandAs<uint32_t>(1u);
  189. if (dest->id() == continue_target) {
  190. return true;
  191. }
  192. }
  193. if (terminator->opcode() == SpvOpSwitch) {
  194. seen_switch = true;
  195. }
  196. // Hit an enclosing loop and didn't break or continue.
  197. if (merge_inst->opcode() == SpvOpLoopMerge) return false;
  198. }
  199. block = NextBlock(block);
  200. }
  201. }
  202. return false;
  203. }
  204. } // namespace val
  205. } // namespace spvtools