spirv_cfg.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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. {
  121. const auto &cases = compiler.get_case_list(block);
  122. for (const auto &target : cases)
  123. {
  124. if (post_order_visit(target.block))
  125. add_branch(block_id, target.block);
  126. }
  127. if (block.default_block && post_order_visit(block.default_block))
  128. add_branch(block_id, block.default_block);
  129. break;
  130. }
  131. default:
  132. break;
  133. }
  134. // If this is a selection merge, add an implied branch to the merge target.
  135. // This is needed to avoid cases where an inner branch dominates the outer branch.
  136. // This can happen if one of the branches exit early, e.g.:
  137. // if (cond) { ...; break; } else { var = 100 } use_var(var);
  138. // We can use the variable without a Phi since there is only one possible parent here.
  139. // However, in this case, we need to hoist out the inner variable to outside the branch.
  140. // Use same strategy as loops.
  141. if (block.merge == SPIRBlock::MergeSelection && post_order_visit(block.next_block))
  142. {
  143. // If there is only one preceding edge to the merge block and it's not ourselves, we need a fixup.
  144. // Add a fake branch so any dominator in either the if (), or else () block, or a lone case statement
  145. // will be hoisted out to outside the selection merge.
  146. // If size > 1, the variable will be automatically hoisted, so we should not mess with it.
  147. // The exception here is switch blocks, where we can have multiple edges to merge block,
  148. // all coming from same scope, so be more conservative in this case.
  149. // Adding fake branches unconditionally breaks parameter preservation analysis,
  150. // which looks at how variables are accessed through the CFG.
  151. auto pred_itr = preceding_edges.find(block.next_block);
  152. if (pred_itr != end(preceding_edges))
  153. {
  154. auto &pred = pred_itr->second;
  155. auto succ_itr = succeeding_edges.find(block_id);
  156. size_t num_succeeding_edges = 0;
  157. if (succ_itr != end(succeeding_edges))
  158. num_succeeding_edges = succ_itr->second.size();
  159. if (block.terminator == SPIRBlock::MultiSelect && num_succeeding_edges == 1)
  160. {
  161. // Multiple branches can come from the same scope due to "break;", so we need to assume that all branches
  162. // come from same case scope in worst case, even if there are multiple preceding edges.
  163. // If we have more than one succeeding edge from the block header, it should be impossible
  164. // to have a dominator be inside the block.
  165. // Only case this can go wrong is if we have 2 or more edges from block header and
  166. // 2 or more edges to merge block, and still have dominator be inside a case label.
  167. if (!pred.empty())
  168. add_branch(block_id, block.next_block);
  169. }
  170. else
  171. {
  172. if (pred.size() == 1 && *pred.begin() != block_id)
  173. add_branch(block_id, block.next_block);
  174. }
  175. }
  176. else
  177. {
  178. // If the merge block does not have any preceding edges, i.e. unreachable, hallucinate it.
  179. // We're going to do code-gen for it, and domination analysis requires that we have at least one preceding edge.
  180. add_branch(block_id, block.next_block);
  181. }
  182. }
  183. // Then visit ourselves. Start counting at one, to let 0 be a magic value for testing back vs. crossing edges.
  184. visit_order[block_id].get() = ++visit_count;
  185. post_order.push_back(block_id);
  186. return true;
  187. }
  188. void CFG::build_post_order_visit_order()
  189. {
  190. uint32_t block = func.entry_block;
  191. visit_count = 0;
  192. visit_order.clear();
  193. post_order.clear();
  194. post_order_visit(block);
  195. }
  196. void CFG::add_branch(uint32_t from, uint32_t to)
  197. {
  198. const auto add_unique = [](SmallVector<uint32_t> &l, uint32_t value) {
  199. auto itr = find(begin(l), end(l), value);
  200. if (itr == end(l))
  201. l.push_back(value);
  202. };
  203. add_unique(preceding_edges[to], from);
  204. add_unique(succeeding_edges[from], to);
  205. }
  206. uint32_t CFG::find_loop_dominator(uint32_t block_id) const
  207. {
  208. while (block_id != SPIRBlock::NoDominator)
  209. {
  210. auto itr = preceding_edges.find(block_id);
  211. if (itr == end(preceding_edges))
  212. return SPIRBlock::NoDominator;
  213. if (itr->second.empty())
  214. return SPIRBlock::NoDominator;
  215. uint32_t pred_block_id = SPIRBlock::NoDominator;
  216. bool ignore_loop_header = false;
  217. // If we are a merge block, go directly to the header block.
  218. // Only consider a loop dominator if we are branching from inside a block to a loop header.
  219. // NOTE: In the CFG we forced an edge from header to merge block always to support variable scopes properly.
  220. for (auto &pred : itr->second)
  221. {
  222. auto &pred_block = compiler.get<SPIRBlock>(pred);
  223. if (pred_block.merge == SPIRBlock::MergeLoop && pred_block.merge_block == ID(block_id))
  224. {
  225. pred_block_id = pred;
  226. ignore_loop_header = true;
  227. break;
  228. }
  229. else if (pred_block.merge == SPIRBlock::MergeSelection && pred_block.next_block == ID(block_id))
  230. {
  231. pred_block_id = pred;
  232. break;
  233. }
  234. }
  235. // No merge block means we can just pick any edge. Loop headers dominate the inner loop, so any path we
  236. // take will lead there.
  237. if (pred_block_id == SPIRBlock::NoDominator)
  238. pred_block_id = itr->second.front();
  239. block_id = pred_block_id;
  240. if (!ignore_loop_header && block_id)
  241. {
  242. auto &block = compiler.get<SPIRBlock>(block_id);
  243. if (block.merge == SPIRBlock::MergeLoop)
  244. return block_id;
  245. }
  246. }
  247. return block_id;
  248. }
  249. bool CFG::node_terminates_control_flow_in_sub_graph(BlockID from, BlockID to) const
  250. {
  251. // Walk backwards, starting from "to" block.
  252. // Only follow pred edges if they have a 1:1 relationship, or a merge relationship.
  253. // If we cannot find a path to "from", we must assume that to is inside control flow in some way.
  254. auto &from_block = compiler.get<SPIRBlock>(from);
  255. BlockID ignore_block_id = 0;
  256. if (from_block.merge == SPIRBlock::MergeLoop)
  257. ignore_block_id = from_block.merge_block;
  258. while (to != from)
  259. {
  260. auto pred_itr = preceding_edges.find(to);
  261. if (pred_itr == end(preceding_edges))
  262. return false;
  263. DominatorBuilder builder(*this);
  264. for (auto &edge : pred_itr->second)
  265. builder.add_block(edge);
  266. uint32_t dominator = builder.get_dominator();
  267. if (dominator == 0)
  268. return false;
  269. auto &dom = compiler.get<SPIRBlock>(dominator);
  270. bool true_path_ignore = false;
  271. bool false_path_ignore = false;
  272. bool merges_to_nothing = dom.merge == SPIRBlock::MergeNone ||
  273. (dom.merge == SPIRBlock::MergeSelection && dom.next_block &&
  274. compiler.get<SPIRBlock>(dom.next_block).terminator == SPIRBlock::Unreachable) ||
  275. (dom.merge == SPIRBlock::MergeLoop && dom.merge_block &&
  276. compiler.get<SPIRBlock>(dom.merge_block).terminator == SPIRBlock::Unreachable);
  277. if (dom.self == from || merges_to_nothing)
  278. {
  279. // We can only ignore inner branchy paths if there is no merge,
  280. // i.e. no code is generated afterwards. E.g. this allows us to elide continue:
  281. // for (;;) { if (cond) { continue; } else { break; } }.
  282. // Codegen here in SPIR-V will be something like either no merge if one path directly breaks, or
  283. // we merge to Unreachable.
  284. if (ignore_block_id && dom.terminator == SPIRBlock::Select)
  285. {
  286. auto &true_block = compiler.get<SPIRBlock>(dom.true_block);
  287. auto &false_block = compiler.get<SPIRBlock>(dom.false_block);
  288. auto &ignore_block = compiler.get<SPIRBlock>(ignore_block_id);
  289. true_path_ignore = compiler.execution_is_branchless(true_block, ignore_block);
  290. false_path_ignore = compiler.execution_is_branchless(false_block, ignore_block);
  291. }
  292. }
  293. // Cases where we allow traversal. This serves as a proxy for post-dominance in a loop body.
  294. // TODO: Might want to do full post-dominance analysis, but it's a lot of churn for something like this ...
  295. // - We're the merge block of a selection construct. Jump to header.
  296. // - We're the merge block of a loop. Jump to header.
  297. // - Direct branch. Trivial.
  298. // - Allow cases inside a branch if the header cannot merge execution before loop exit.
  299. if ((dom.merge == SPIRBlock::MergeSelection && dom.next_block == to) ||
  300. (dom.merge == SPIRBlock::MergeLoop && dom.merge_block == to) ||
  301. (dom.terminator == SPIRBlock::Direct && dom.next_block == to) ||
  302. (dom.terminator == SPIRBlock::Select && dom.true_block == to && false_path_ignore) ||
  303. (dom.terminator == SPIRBlock::Select && dom.false_block == to && true_path_ignore))
  304. {
  305. // Allow walking selection constructs if the other branch reaches out of a loop construct.
  306. // It cannot be in-scope anymore.
  307. to = dominator;
  308. }
  309. else
  310. return false;
  311. }
  312. return true;
  313. }
  314. DominatorBuilder::DominatorBuilder(const CFG &cfg_)
  315. : cfg(cfg_)
  316. {
  317. }
  318. void DominatorBuilder::add_block(uint32_t block)
  319. {
  320. if (!cfg.get_immediate_dominator(block))
  321. {
  322. // Unreachable block via the CFG, we will never emit this code anyways.
  323. return;
  324. }
  325. if (!dominator)
  326. {
  327. dominator = block;
  328. return;
  329. }
  330. if (block != dominator)
  331. dominator = cfg.find_common_dominator(block, dominator);
  332. }
  333. void DominatorBuilder::lift_continue_block_dominator()
  334. {
  335. // 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.
  336. // We cannot safely declare variables inside a continue block, so move any variable declared
  337. // in a continue block to the entry block to simplify.
  338. // It makes very little sense for a continue block to ever be a dominator, so fall back to the simplest
  339. // solution.
  340. if (!dominator)
  341. return;
  342. auto &block = cfg.get_compiler().get<SPIRBlock>(dominator);
  343. auto post_order = cfg.get_visit_order(dominator);
  344. // If we are branching to a block with a higher post-order traversal index (continue blocks), we have a problem
  345. // since we cannot create sensible GLSL code for this, fallback to entry block.
  346. bool back_edge_dominator = false;
  347. switch (block.terminator)
  348. {
  349. case SPIRBlock::Direct:
  350. if (cfg.get_visit_order(block.next_block) > post_order)
  351. back_edge_dominator = true;
  352. break;
  353. case SPIRBlock::Select:
  354. if (cfg.get_visit_order(block.true_block) > post_order)
  355. back_edge_dominator = true;
  356. if (cfg.get_visit_order(block.false_block) > post_order)
  357. back_edge_dominator = true;
  358. break;
  359. case SPIRBlock::MultiSelect:
  360. {
  361. auto &cases = cfg.get_compiler().get_case_list(block);
  362. for (auto &target : cases)
  363. {
  364. if (cfg.get_visit_order(target.block) > post_order)
  365. back_edge_dominator = true;
  366. }
  367. if (block.default_block && cfg.get_visit_order(block.default_block) > post_order)
  368. back_edge_dominator = true;
  369. break;
  370. }
  371. default:
  372. break;
  373. }
  374. if (back_edge_dominator)
  375. dominator = cfg.get_function().entry_block;
  376. }
  377. } // namespace SPIRV_CROSS_NAMESPACE