function.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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/function.h"
  15. #include <algorithm>
  16. #include <cassert>
  17. #include <sstream>
  18. #include <unordered_map>
  19. #include <unordered_set>
  20. #include <utility>
  21. #include "source/cfa.h"
  22. #include "source/val/basic_block.h"
  23. #include "source/val/construct.h"
  24. #include "source/val/validate.h"
  25. namespace spvtools {
  26. namespace val {
  27. // Universal Limit of ResultID + 1
  28. static const uint32_t kInvalidId = 0x400000;
  29. Function::Function(uint32_t function_id, uint32_t result_type_id,
  30. SpvFunctionControlMask function_control,
  31. uint32_t function_type_id)
  32. : id_(function_id),
  33. function_type_id_(function_type_id),
  34. result_type_id_(result_type_id),
  35. function_control_(function_control),
  36. declaration_type_(FunctionDecl::kFunctionDeclUnknown),
  37. end_has_been_registered_(false),
  38. blocks_(),
  39. current_block_(nullptr),
  40. pseudo_entry_block_(0),
  41. pseudo_exit_block_(kInvalidId),
  42. cfg_constructs_(),
  43. variable_ids_(),
  44. parameter_ids_() {}
  45. bool Function::IsFirstBlock(uint32_t block_id) const {
  46. return !ordered_blocks_.empty() && *first_block() == block_id;
  47. }
  48. spv_result_t Function::RegisterFunctionParameter(uint32_t parameter_id,
  49. uint32_t type_id) {
  50. assert(current_block_ == nullptr &&
  51. "RegisterFunctionParameter can only be called when parsing the binary "
  52. "outside of a block");
  53. // TODO(umar): Validate function parameter type order and count
  54. // TODO(umar): Use these variables to validate parameter type
  55. (void)parameter_id;
  56. (void)type_id;
  57. return SPV_SUCCESS;
  58. }
  59. spv_result_t Function::RegisterLoopMerge(uint32_t merge_id,
  60. uint32_t continue_id) {
  61. RegisterBlock(merge_id, false);
  62. RegisterBlock(continue_id, false);
  63. BasicBlock& merge_block = blocks_.at(merge_id);
  64. BasicBlock& continue_target_block = blocks_.at(continue_id);
  65. assert(current_block_ &&
  66. "RegisterLoopMerge must be called when called within a block");
  67. current_block_->set_type(kBlockTypeLoop);
  68. merge_block.set_type(kBlockTypeMerge);
  69. continue_target_block.set_type(kBlockTypeContinue);
  70. Construct& loop_construct =
  71. AddConstruct({ConstructType::kLoop, current_block_, &merge_block});
  72. Construct& continue_construct =
  73. AddConstruct({ConstructType::kContinue, &continue_target_block});
  74. continue_construct.set_corresponding_constructs({&loop_construct});
  75. loop_construct.set_corresponding_constructs({&continue_construct});
  76. merge_block_header_[&merge_block] = current_block_;
  77. if (continue_target_headers_.find(&continue_target_block) ==
  78. continue_target_headers_.end()) {
  79. continue_target_headers_[&continue_target_block] = {current_block_};
  80. } else {
  81. continue_target_headers_[&continue_target_block].push_back(current_block_);
  82. }
  83. return SPV_SUCCESS;
  84. }
  85. spv_result_t Function::RegisterSelectionMerge(uint32_t merge_id) {
  86. RegisterBlock(merge_id, false);
  87. BasicBlock& merge_block = blocks_.at(merge_id);
  88. current_block_->set_type(kBlockTypeSelection);
  89. merge_block.set_type(kBlockTypeMerge);
  90. merge_block_header_[&merge_block] = current_block_;
  91. AddConstruct({ConstructType::kSelection, current_block(), &merge_block});
  92. return SPV_SUCCESS;
  93. }
  94. spv_result_t Function::RegisterSetFunctionDeclType(FunctionDecl type) {
  95. assert(declaration_type_ == FunctionDecl::kFunctionDeclUnknown);
  96. declaration_type_ = type;
  97. return SPV_SUCCESS;
  98. }
  99. spv_result_t Function::RegisterBlock(uint32_t block_id, bool is_definition) {
  100. assert(
  101. declaration_type_ == FunctionDecl::kFunctionDeclDefinition &&
  102. "RegisterBlocks can only be called after declaration_type_ is defined");
  103. std::unordered_map<uint32_t, BasicBlock>::iterator inserted_block;
  104. bool success = false;
  105. tie(inserted_block, success) =
  106. blocks_.insert({block_id, BasicBlock(block_id)});
  107. if (is_definition) { // new block definition
  108. assert(current_block_ == nullptr &&
  109. "Register Block can only be called when parsing a binary outside of "
  110. "a BasicBlock");
  111. undefined_blocks_.erase(block_id);
  112. current_block_ = &inserted_block->second;
  113. ordered_blocks_.push_back(current_block_);
  114. } else if (success) { // Block doesn't exist but this is not a definition
  115. undefined_blocks_.insert(block_id);
  116. }
  117. return SPV_SUCCESS;
  118. }
  119. void Function::RegisterBlockEnd(std::vector<uint32_t> next_list) {
  120. assert(
  121. current_block_ &&
  122. "RegisterBlockEnd can only be called when parsing a binary in a block");
  123. std::vector<BasicBlock*> next_blocks;
  124. next_blocks.reserve(next_list.size());
  125. std::unordered_map<uint32_t, BasicBlock>::iterator inserted_block;
  126. bool success;
  127. for (uint32_t successor_id : next_list) {
  128. tie(inserted_block, success) =
  129. blocks_.insert({successor_id, BasicBlock(successor_id)});
  130. if (success) {
  131. undefined_blocks_.insert(successor_id);
  132. }
  133. next_blocks.push_back(&inserted_block->second);
  134. }
  135. if (current_block_->is_type(kBlockTypeLoop)) {
  136. // For each loop header, record the set of its successors, and include
  137. // its continue target if the continue target is not the loop header
  138. // itself.
  139. std::vector<BasicBlock*>& next_blocks_plus_continue_target =
  140. loop_header_successors_plus_continue_target_map_[current_block_];
  141. next_blocks_plus_continue_target = next_blocks;
  142. auto continue_target =
  143. FindConstructForEntryBlock(current_block_, ConstructType::kLoop)
  144. .corresponding_constructs()
  145. .back()
  146. ->entry_block();
  147. if (continue_target != current_block_) {
  148. next_blocks_plus_continue_target.push_back(continue_target);
  149. }
  150. }
  151. current_block_->RegisterSuccessors(next_blocks);
  152. current_block_ = nullptr;
  153. return;
  154. }
  155. void Function::RegisterFunctionEnd() {
  156. if (!end_has_been_registered_) {
  157. end_has_been_registered_ = true;
  158. ComputeAugmentedCFG();
  159. }
  160. }
  161. size_t Function::block_count() const { return blocks_.size(); }
  162. size_t Function::undefined_block_count() const {
  163. return undefined_blocks_.size();
  164. }
  165. const std::vector<BasicBlock*>& Function::ordered_blocks() const {
  166. return ordered_blocks_;
  167. }
  168. std::vector<BasicBlock*>& Function::ordered_blocks() { return ordered_blocks_; }
  169. const BasicBlock* Function::current_block() const { return current_block_; }
  170. BasicBlock* Function::current_block() { return current_block_; }
  171. const std::list<Construct>& Function::constructs() const {
  172. return cfg_constructs_;
  173. }
  174. std::list<Construct>& Function::constructs() { return cfg_constructs_; }
  175. const BasicBlock* Function::first_block() const {
  176. if (ordered_blocks_.empty()) return nullptr;
  177. return ordered_blocks_[0];
  178. }
  179. BasicBlock* Function::first_block() {
  180. if (ordered_blocks_.empty()) return nullptr;
  181. return ordered_blocks_[0];
  182. }
  183. bool Function::IsBlockType(uint32_t merge_block_id, BlockType type) const {
  184. bool ret = false;
  185. const BasicBlock* block;
  186. std::tie(block, std::ignore) = GetBlock(merge_block_id);
  187. if (block) {
  188. ret = block->is_type(type);
  189. }
  190. return ret;
  191. }
  192. std::pair<const BasicBlock*, bool> Function::GetBlock(uint32_t block_id) const {
  193. const auto b = blocks_.find(block_id);
  194. if (b != end(blocks_)) {
  195. const BasicBlock* block = &(b->second);
  196. bool defined =
  197. undefined_blocks_.find(block->id()) == std::end(undefined_blocks_);
  198. return std::make_pair(block, defined);
  199. } else {
  200. return std::make_pair(nullptr, false);
  201. }
  202. }
  203. std::pair<BasicBlock*, bool> Function::GetBlock(uint32_t block_id) {
  204. const BasicBlock* out;
  205. bool defined;
  206. std::tie(out, defined) =
  207. const_cast<const Function*>(this)->GetBlock(block_id);
  208. return std::make_pair(const_cast<BasicBlock*>(out), defined);
  209. }
  210. Function::GetBlocksFunction Function::AugmentedCFGSuccessorsFunction() const {
  211. return [this](const BasicBlock* block) {
  212. auto where = augmented_successors_map_.find(block);
  213. return where == augmented_successors_map_.end() ? block->successors()
  214. : &(*where).second;
  215. };
  216. }
  217. Function::GetBlocksFunction
  218. Function::AugmentedCFGSuccessorsFunctionIncludingHeaderToContinueEdge() const {
  219. return [this](const BasicBlock* block) {
  220. auto where = loop_header_successors_plus_continue_target_map_.find(block);
  221. return where == loop_header_successors_plus_continue_target_map_.end()
  222. ? AugmentedCFGSuccessorsFunction()(block)
  223. : &(*where).second;
  224. };
  225. }
  226. Function::GetBlocksFunction Function::AugmentedCFGPredecessorsFunction() const {
  227. return [this](const BasicBlock* block) {
  228. auto where = augmented_predecessors_map_.find(block);
  229. return where == augmented_predecessors_map_.end() ? block->predecessors()
  230. : &(*where).second;
  231. };
  232. }
  233. void Function::ComputeAugmentedCFG() {
  234. // Compute the successors of the pseudo-entry block, and
  235. // the predecessors of the pseudo exit block.
  236. auto succ_func = [](const BasicBlock* b) { return b->successors(); };
  237. auto pred_func = [](const BasicBlock* b) { return b->predecessors(); };
  238. CFA<BasicBlock>::ComputeAugmentedCFG(
  239. ordered_blocks_, &pseudo_entry_block_, &pseudo_exit_block_,
  240. &augmented_successors_map_, &augmented_predecessors_map_, succ_func,
  241. pred_func);
  242. }
  243. Construct& Function::AddConstruct(const Construct& new_construct) {
  244. cfg_constructs_.push_back(new_construct);
  245. auto& result = cfg_constructs_.back();
  246. entry_block_to_construct_[std::make_pair(new_construct.entry_block(),
  247. new_construct.type())] = &result;
  248. return result;
  249. }
  250. Construct& Function::FindConstructForEntryBlock(const BasicBlock* entry_block,
  251. ConstructType type) {
  252. auto where =
  253. entry_block_to_construct_.find(std::make_pair(entry_block, type));
  254. assert(where != entry_block_to_construct_.end());
  255. auto construct_ptr = (*where).second;
  256. assert(construct_ptr);
  257. return *construct_ptr;
  258. }
  259. int Function::GetBlockDepth(BasicBlock* bb) {
  260. // Guard against nullptr.
  261. if (!bb) {
  262. return 0;
  263. }
  264. // Only calculate the depth if it's not already calculated.
  265. // This function uses memoization to avoid duplicate CFG depth calculations.
  266. if (block_depth_.find(bb) != block_depth_.end()) {
  267. return block_depth_[bb];
  268. }
  269. // Avoid recursion. Something is wrong if the same block is encountered
  270. // multiple times.
  271. block_depth_[bb] = 0;
  272. BasicBlock* bb_dom = bb->immediate_dominator();
  273. if (!bb_dom || bb == bb_dom) {
  274. // This block has no dominator, so it's at depth 0.
  275. block_depth_[bb] = 0;
  276. } else if (bb->is_type(kBlockTypeContinue)) {
  277. // This rule must precede the rule for merge blocks in order to set up
  278. // depths correctly. If a block is both a merge and continue then the merge
  279. // is nested within the continue's loop (or the graph is incorrect).
  280. // The depth of the continue block entry point is 1 + loop header depth.
  281. Construct* continue_construct =
  282. entry_block_to_construct_[std::make_pair(bb, ConstructType::kContinue)];
  283. assert(continue_construct);
  284. // Continue construct has only 1 corresponding construct (loop header).
  285. Construct* loop_construct =
  286. continue_construct->corresponding_constructs()[0];
  287. assert(loop_construct);
  288. BasicBlock* loop_header = loop_construct->entry_block();
  289. // The continue target may be the loop itself (while 1).
  290. // In such cases, the depth of the continue block is: 1 + depth of the
  291. // loop's dominator block.
  292. if (loop_header == bb) {
  293. block_depth_[bb] = 1 + GetBlockDepth(bb_dom);
  294. } else {
  295. block_depth_[bb] = 1 + GetBlockDepth(loop_header);
  296. }
  297. } else if (bb->is_type(kBlockTypeMerge)) {
  298. // If this is a merge block, its depth is equal to the block before
  299. // branching.
  300. BasicBlock* header = merge_block_header_[bb];
  301. assert(header);
  302. block_depth_[bb] = GetBlockDepth(header);
  303. } else if (bb_dom->is_type(kBlockTypeSelection) ||
  304. bb_dom->is_type(kBlockTypeLoop)) {
  305. // The dominator of the given block is a header block. So, the nesting
  306. // depth of this block is: 1 + nesting depth of the header.
  307. block_depth_[bb] = 1 + GetBlockDepth(bb_dom);
  308. } else {
  309. block_depth_[bb] = GetBlockDepth(bb_dom);
  310. }
  311. return block_depth_[bb];
  312. }
  313. void Function::RegisterExecutionModelLimitation(SpvExecutionModel model,
  314. const std::string& message) {
  315. execution_model_limitations_.push_back(
  316. [model, message](SpvExecutionModel in_model, std::string* out_message) {
  317. if (model != in_model) {
  318. if (out_message) {
  319. *out_message = message;
  320. }
  321. return false;
  322. }
  323. return true;
  324. });
  325. }
  326. bool Function::IsCompatibleWithExecutionModel(SpvExecutionModel model,
  327. std::string* reason) const {
  328. bool return_value = true;
  329. std::stringstream ss_reason;
  330. for (const auto& is_compatible : execution_model_limitations_) {
  331. std::string message;
  332. if (!is_compatible(model, &message)) {
  333. if (!reason) return false;
  334. return_value = false;
  335. if (!message.empty()) {
  336. ss_reason << message << "\n";
  337. }
  338. }
  339. }
  340. if (!return_value && reason) {
  341. *reason = ss_reason.str();
  342. }
  343. return return_value;
  344. }
  345. bool Function::CheckLimitations(const ValidationState_t& _,
  346. const Function* entry_point,
  347. std::string* reason) const {
  348. bool return_value = true;
  349. std::stringstream ss_reason;
  350. for (const auto& is_compatible : limitations_) {
  351. std::string message;
  352. if (!is_compatible(_, entry_point, &message)) {
  353. if (!reason) return false;
  354. return_value = false;
  355. if (!message.empty()) {
  356. ss_reason << message << "\n";
  357. }
  358. }
  359. }
  360. if (!return_value && reason) {
  361. *reason = ss_reason.str();
  362. }
  363. return return_value;
  364. }
  365. } // namespace val
  366. } // namespace spvtools