spirv_cfg.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. /*
  2. * Copyright 2016-2021 Arm Limited
  3. * SPDX-License-Identifier: Apache-2.0 OR MIT
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /*
  18. * At your option, you may choose to accept this material under either:
  19. * 1. The Apache License, Version 2.0, found at <http://www.apache.org/licenses/LICENSE-2.0>, or
  20. * 2. The MIT License, found at <http://opensource.org/licenses/MIT>.
  21. */
  22. #include "spirv_cfg.hpp"
  23. #include "spirv_cross.hpp"
  24. #include <algorithm>
  25. #include <assert.h>
  26. using namespace std;
  27. namespace SPIRV_CROSS_NAMESPACE
  28. {
  29. CFG::CFG(Compiler &compiler_, const SPIRFunction &func_)
  30. : compiler(compiler_)
  31. , func(func_)
  32. {
  33. build_post_order_visit_order();
  34. build_immediate_dominators();
  35. }
  36. uint32_t CFG::find_common_dominator(uint32_t a, uint32_t b) const
  37. {
  38. while (a != b)
  39. {
  40. if (get_visit_order(a) < get_visit_order(b))
  41. a = get_immediate_dominator(a);
  42. else
  43. b = get_immediate_dominator(b);
  44. }
  45. return a;
  46. }
  47. void CFG::build_immediate_dominators()
  48. {
  49. // Traverse the post-order in reverse and build up the immediate dominator tree.
  50. immediate_dominators.clear();
  51. immediate_dominators[func.entry_block] = func.entry_block;
  52. for (auto i = post_order.size(); i; i--)
  53. {
  54. uint32_t block = post_order[i - 1];
  55. auto &pred = preceding_edges[block];
  56. if (pred.empty()) // This is for the entry block, but we've already set up the dominators.
  57. continue;
  58. for (auto &edge : pred)
  59. {
  60. if (immediate_dominators[block])
  61. {
  62. assert(immediate_dominators[edge]);
  63. immediate_dominators[block] = find_common_dominator(immediate_dominators[block], edge);
  64. }
  65. else
  66. immediate_dominators[block] = edge;
  67. }
  68. }
  69. }
  70. bool CFG::is_back_edge(uint32_t to) const
  71. {
  72. // We have a back edge if the visit order is set with the temporary magic value 0.
  73. // Crossing edges will have already been recorded with a visit order.
  74. auto itr = visit_order.find(to);
  75. return itr != end(visit_order) && itr->second.get() == 0;
  76. }
  77. bool CFG::has_visited_forward_edge(uint32_t to) const
  78. {
  79. // If > 0, we have visited the edge already, and this is not a back edge branch.
  80. auto itr = visit_order.find(to);
  81. return itr != end(visit_order) && itr->second.get() > 0;
  82. }
  83. bool CFG::post_order_visit(uint32_t block_id)
  84. {
  85. // If we have already branched to this block (back edge), stop recursion.
  86. // If our branches are back-edges, we do not record them.
  87. // We have to record crossing edges however.
  88. if (has_visited_forward_edge(block_id))
  89. return true;
  90. else if (is_back_edge(block_id))
  91. return false;
  92. // Block back-edges from recursively revisiting ourselves.
  93. visit_order[block_id].get() = 0;
  94. auto &block = compiler.get<SPIRBlock>(block_id);
  95. // If this is a loop header, add an implied branch to the merge target.
  96. // This is needed to avoid annoying cases with do { ... } while(false) loops often generated by inliners.
  97. // To the CFG, this is linear control flow, but we risk picking the do/while scope as our dominating block.
  98. // This makes sure that if we are accessing a variable outside the do/while, we choose the loop header as dominator.
  99. // We could use has_visited_forward_edge, but this break code-gen where the merge block is unreachable in the CFG.
  100. // Make a point out of visiting merge target first. This is to make sure that post visit order outside the loop
  101. // is lower than inside the loop, which is going to be key for some traversal algorithms like post-dominance analysis.
  102. // For selection constructs true/false blocks will end up visiting the merge block directly and it works out fine,
  103. // but for loops, only the header might end up actually branching to merge block.
  104. if (block.merge == SPIRBlock::MergeLoop && post_order_visit(block.merge_block))
  105. add_branch(block_id, block.merge_block);
  106. // First visit our branch targets.
  107. switch (block.terminator)
  108. {
  109. case SPIRBlock::Direct:
  110. if (post_order_visit(block.next_block))
  111. add_branch(block_id, block.next_block);
  112. break;
  113. case SPIRBlock::Select:
  114. if (post_order_visit(block.true_block))
  115. add_branch(block_id, block.true_block);
  116. if (post_order_visit(block.false_block))
  117. add_branch(block_id, block.false_block);
  118. break;
  119. case SPIRBlock::MultiSelect:
  120. for (auto &target : block.cases)
  121. {
  122. if (post_order_visit(target.block))
  123. add_branch(block_id, target.block);
  124. }
  125. if (block.default_block && post_order_visit(block.default_block))
  126. add_branch(block_id, block.default_block);
  127. break;
  128. default:
  129. break;
  130. }
  131. // If this is a selection merge, add an implied branch to the merge target.
  132. // This is needed to avoid cases where an inner branch dominates the outer branch.
  133. // This can happen if one of the branches exit early, e.g.:
  134. // if (cond) { ...; break; } else { var = 100 } use_var(var);
  135. // We can use the variable without a Phi since there is only one possible parent here.
  136. // However, in this case, we need to hoist out the inner variable to outside the branch.
  137. // Use same strategy as loops.
  138. if (block.merge == SPIRBlock::MergeSelection && post_order_visit(block.next_block))
  139. {
  140. // If there is only one preceding edge to the merge block and it's not ourselves, we need a fixup.
  141. // Add a fake branch so any dominator in either the if (), or else () block, or a lone case statement
  142. // will be hoisted out to outside the selection merge.
  143. // If size > 1, the variable will be automatically hoisted, so we should not mess with it.
  144. // The exception here is switch blocks, where we can have multiple edges to merge block,
  145. // all coming from same scope, so be more conservative in this case.
  146. // Adding fake branches unconditionally breaks parameter preservation analysis,
  147. // which looks at how variables are accessed through the CFG.
  148. auto pred_itr = preceding_edges.find(block.next_block);
  149. if (pred_itr != end(preceding_edges))
  150. {
  151. auto &pred = pred_itr->second;
  152. auto succ_itr = succeeding_edges.find(block_id);
  153. size_t num_succeeding_edges = 0;
  154. if (succ_itr != end(succeeding_edges))
  155. num_succeeding_edges = succ_itr->second.size();
  156. if (block.terminator == SPIRBlock::MultiSelect && num_succeeding_edges == 1)
  157. {
  158. // Multiple branches can come from the same scope due to "break;", so we need to assume that all branches
  159. // come from same case scope in worst case, even if there are multiple preceding edges.
  160. // If we have more than one succeeding edge from the block header, it should be impossible
  161. // to have a dominator be inside the block.
  162. // Only case this can go wrong is if we have 2 or more edges from block header and
  163. // 2 or more edges to merge block, and still have dominator be inside a case label.
  164. if (!pred.empty())
  165. add_branch(block_id, block.next_block);
  166. }
  167. else
  168. {
  169. if (pred.size() == 1 && *pred.begin() != block_id)
  170. add_branch(block_id, block.next_block);
  171. }
  172. }
  173. else
  174. {
  175. // If the merge block does not have any preceding edges, i.e. unreachable, hallucinate it.
  176. // We're going to do code-gen for it, and domination analysis requires that we have at least one preceding edge.
  177. add_branch(block_id, block.next_block);
  178. }
  179. }
  180. // Then visit ourselves. Start counting at one, to let 0 be a magic value for testing back vs. crossing edges.
  181. visit_order[block_id].get() = ++visit_count;
  182. post_order.push_back(block_id);
  183. return true;
  184. }
  185. void CFG::build_post_order_visit_order()
  186. {
  187. uint32_t block = func.entry_block;
  188. visit_count = 0;
  189. visit_order.clear();
  190. post_order.clear();
  191. post_order_visit(block);
  192. }
  193. void CFG::add_branch(uint32_t from, uint32_t to)
  194. {
  195. const auto add_unique = [](SmallVector<uint32_t> &l, uint32_t value) {
  196. auto itr = find(begin(l), end(l), value);
  197. if (itr == end(l))
  198. l.push_back(value);
  199. };
  200. add_unique(preceding_edges[to], from);
  201. add_unique(succeeding_edges[from], to);
  202. }
  203. uint32_t CFG::find_loop_dominator(uint32_t block_id) const
  204. {
  205. while (block_id != SPIRBlock::NoDominator)
  206. {
  207. auto itr = preceding_edges.find(block_id);
  208. if (itr == end(preceding_edges))
  209. return SPIRBlock::NoDominator;
  210. if (itr->second.empty())
  211. return SPIRBlock::NoDominator;
  212. uint32_t pred_block_id = SPIRBlock::NoDominator;
  213. bool ignore_loop_header = false;
  214. // If we are a merge block, go directly to the header block.
  215. // Only consider a loop dominator if we are branching from inside a block to a loop header.
  216. // NOTE: In the CFG we forced an edge from header to merge block always to support variable scopes properly.
  217. for (auto &pred : itr->second)
  218. {
  219. auto &pred_block = compiler.get<SPIRBlock>(pred);
  220. if (pred_block.merge == SPIRBlock::MergeLoop && pred_block.merge_block == ID(block_id))
  221. {
  222. pred_block_id = pred;
  223. ignore_loop_header = true;
  224. break;
  225. }
  226. else if (pred_block.merge == SPIRBlock::MergeSelection && pred_block.next_block == ID(block_id))
  227. {
  228. pred_block_id = pred;
  229. break;
  230. }
  231. }
  232. // No merge block means we can just pick any edge. Loop headers dominate the inner loop, so any path we
  233. // take will lead there.
  234. if (pred_block_id == SPIRBlock::NoDominator)
  235. pred_block_id = itr->second.front();
  236. block_id = pred_block_id;
  237. if (!ignore_loop_header && block_id)
  238. {
  239. auto &block = compiler.get<SPIRBlock>(block_id);
  240. if (block.merge == SPIRBlock::MergeLoop)
  241. return block_id;
  242. }
  243. }
  244. return block_id;
  245. }
  246. bool CFG::node_terminates_control_flow_in_sub_graph(BlockID from, BlockID to) const
  247. {
  248. // Walk backwards, starting from "to" block.
  249. // Only follow pred edges if they have a 1:1 relationship, or a merge relationship.
  250. // If we cannot find a path to "from", we must assume that to is inside control flow in some way.
  251. auto &from_block = compiler.get<SPIRBlock>(from);
  252. BlockID ignore_block_id = 0;
  253. if (from_block.merge == SPIRBlock::MergeLoop)
  254. ignore_block_id = from_block.merge_block;
  255. while (to != from)
  256. {
  257. auto pred_itr = preceding_edges.find(to);
  258. if (pred_itr == end(preceding_edges))
  259. return false;
  260. DominatorBuilder builder(*this);
  261. for (auto &edge : pred_itr->second)
  262. builder.add_block(edge);
  263. uint32_t dominator = builder.get_dominator();
  264. if (dominator == 0)
  265. return false;
  266. auto &dom = compiler.get<SPIRBlock>(dominator);
  267. bool true_path_ignore = false;
  268. bool false_path_ignore = false;
  269. if (ignore_block_id && dom.terminator == SPIRBlock::Select)
  270. {
  271. auto &true_block = compiler.get<SPIRBlock>(dom.true_block);
  272. auto &false_block = compiler.get<SPIRBlock>(dom.false_block);
  273. auto &ignore_block = compiler.get<SPIRBlock>(ignore_block_id);
  274. true_path_ignore = compiler.execution_is_branchless(true_block, ignore_block);
  275. false_path_ignore = compiler.execution_is_branchless(false_block, ignore_block);
  276. }
  277. if ((dom.merge == SPIRBlock::MergeSelection && dom.next_block == to) ||
  278. (dom.merge == SPIRBlock::MergeLoop && dom.merge_block == to) ||
  279. (dom.terminator == SPIRBlock::Direct && dom.next_block == to) ||
  280. (dom.terminator == SPIRBlock::Select && dom.true_block == to && false_path_ignore) ||
  281. (dom.terminator == SPIRBlock::Select && dom.false_block == to && true_path_ignore))
  282. {
  283. // Allow walking selection constructs if the other branch reaches out of a loop construct.
  284. // It cannot be in-scope anymore.
  285. to = dominator;
  286. }
  287. else
  288. return false;
  289. }
  290. return true;
  291. }
  292. DominatorBuilder::DominatorBuilder(const CFG &cfg_)
  293. : cfg(cfg_)
  294. {
  295. }
  296. void DominatorBuilder::add_block(uint32_t block)
  297. {
  298. if (!cfg.get_immediate_dominator(block))
  299. {
  300. // Unreachable block via the CFG, we will never emit this code anyways.
  301. return;
  302. }
  303. if (!dominator)
  304. {
  305. dominator = block;
  306. return;
  307. }
  308. if (block != dominator)
  309. dominator = cfg.find_common_dominator(block, dominator);
  310. }
  311. void DominatorBuilder::lift_continue_block_dominator()
  312. {
  313. // It is possible for a continue block to be the dominator of a variable is only accessed inside the while block of a do-while loop.
  314. // We cannot safely declare variables inside a continue block, so move any variable declared
  315. // in a continue block to the entry block to simplify.
  316. // It makes very little sense for a continue block to ever be a dominator, so fall back to the simplest
  317. // solution.
  318. if (!dominator)
  319. return;
  320. auto &block = cfg.get_compiler().get<SPIRBlock>(dominator);
  321. auto post_order = cfg.get_visit_order(dominator);
  322. // If we are branching to a block with a higher post-order traversal index (continue blocks), we have a problem
  323. // since we cannot create sensible GLSL code for this, fallback to entry block.
  324. bool back_edge_dominator = false;
  325. switch (block.terminator)
  326. {
  327. case SPIRBlock::Direct:
  328. if (cfg.get_visit_order(block.next_block) > post_order)
  329. back_edge_dominator = true;
  330. break;
  331. case SPIRBlock::Select:
  332. if (cfg.get_visit_order(block.true_block) > post_order)
  333. back_edge_dominator = true;
  334. if (cfg.get_visit_order(block.false_block) > post_order)
  335. back_edge_dominator = true;
  336. break;
  337. case SPIRBlock::MultiSelect:
  338. for (auto &target : block.cases)
  339. {
  340. if (cfg.get_visit_order(target.block) > post_order)
  341. back_edge_dominator = true;
  342. }
  343. if (block.default_block && cfg.get_visit_order(block.default_block) > post_order)
  344. back_edge_dominator = true;
  345. break;
  346. default:
  347. break;
  348. }
  349. if (back_edge_dominator)
  350. dominator = cfg.get_function().entry_block;
  351. }
  352. } // namespace SPIRV_CROSS_NAMESPACE