2
0

construct.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. corresponding_headers.insert(other->entry_block());
  69. }
  70. std::vector<BasicBlock*> stack;
  71. stack.push_back(const_cast<BasicBlock*>(header));
  72. while (!stack.empty()) {
  73. BasicBlock* block = stack.back();
  74. stack.pop_back();
  75. if (merge == block && ExitBlockIsMergeBlock()) {
  76. // Merge block is not part of the construct.
  77. continue;
  78. }
  79. if (corresponding_headers.count(block)) {
  80. // Entered a corresponding construct.
  81. continue;
  82. }
  83. int block_depth = function->GetBlockDepth(block);
  84. if (block_depth < header_depth) {
  85. // Broke to outer construct.
  86. continue;
  87. }
  88. // In a loop, the continue target is at a depth of the loop construct + 1.
  89. // A selection construct nested directly within the loop construct is also
  90. // at the same depth. It is valid, however, to branch directly to the
  91. // continue target from within the selection construct.
  92. if (block != header && block_depth == header_depth &&
  93. type() == ConstructType::kSelection &&
  94. block->is_type(kBlockTypeContinue)) {
  95. // Continued to outer construct.
  96. continue;
  97. }
  98. if (!construct_blocks.insert(block).second) continue;
  99. if (merge != block) {
  100. for (auto succ : *block->successors()) {
  101. // All blocks in the construct must be dominated by the header.
  102. if (header->dominates(*succ)) {
  103. stack.push_back(succ);
  104. }
  105. }
  106. }
  107. }
  108. return construct_blocks;
  109. }
  110. bool Construct::IsStructuredExit(ValidationState_t& _, BasicBlock* dest) const {
  111. // Structured Exits:
  112. // - Selection:
  113. // - branch to its merge
  114. // - branch to nearest enclosing loop merge or continue
  115. // - Loop:
  116. // - branch to its merge
  117. // - branch to its continue
  118. // - Continue:
  119. // - branch to loop header
  120. // - branch to loop merge
  121. //
  122. // Note: we will never see a case construct here.
  123. assert(type() != ConstructType::kCase);
  124. if (type() == ConstructType::kLoop) {
  125. auto header = entry_block();
  126. auto terminator = header->terminator();
  127. auto index = terminator - &_.ordered_instructions()[0];
  128. auto merge_inst = &_.ordered_instructions()[index - 1];
  129. auto merge_block_id = merge_inst->GetOperandAs<uint32_t>(0u);
  130. auto continue_block_id = merge_inst->GetOperandAs<uint32_t>(1u);
  131. if (dest->id() == merge_block_id || dest->id() == continue_block_id) {
  132. return true;
  133. }
  134. } else if (type() == ConstructType::kContinue) {
  135. auto loop_construct = corresponding_constructs()[0];
  136. auto header = loop_construct->entry_block();
  137. auto terminator = header->terminator();
  138. auto index = terminator - &_.ordered_instructions()[0];
  139. auto merge_inst = &_.ordered_instructions()[index - 1];
  140. auto merge_block_id = merge_inst->GetOperandAs<uint32_t>(0u);
  141. if (dest == header || dest->id() == merge_block_id) {
  142. return true;
  143. }
  144. } else {
  145. assert(type() == ConstructType::kSelection);
  146. if (dest == exit_block()) {
  147. return true;
  148. }
  149. auto header = entry_block();
  150. auto block = header;
  151. while (block) {
  152. auto terminator = block->terminator();
  153. auto index = terminator - &_.ordered_instructions()[0];
  154. auto merge_inst = &_.ordered_instructions()[index - 1];
  155. if (merge_inst->opcode() == SpvOpLoopMerge) {
  156. auto merge_target = merge_inst->GetOperandAs<uint32_t>(0u);
  157. auto merge_block = merge_inst->function()->GetBlock(merge_target).first;
  158. if (merge_block->dominates(*header)) {
  159. block = block->immediate_dominator();
  160. continue;
  161. }
  162. auto continue_target = merge_inst->GetOperandAs<uint32_t>(1u);
  163. if (dest->id() == merge_target || dest->id() == continue_target) {
  164. return true;
  165. }
  166. }
  167. block = block->immediate_dominator();
  168. }
  169. }
  170. return false;
  171. }
  172. } // namespace val
  173. } // namespace spvtools