Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

aco_ssa_elimination.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright © 2018 Valve Corporation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  18. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  21. * IN THE SOFTWARE.
  22. *
  23. */
  24. #include "aco_ir.h"
  25. #include <map>
  26. namespace aco {
  27. namespace {
  28. /* map: block-id -> pair (dest, src) to store phi information */
  29. typedef std::map<uint32_t, std::vector<std::pair<Definition, Operand>>> phi_info;
  30. struct ssa_elimination_ctx {
  31. phi_info logical_phi_info;
  32. phi_info linear_phi_info;
  33. std::vector<bool> empty_blocks;
  34. Program* program;
  35. ssa_elimination_ctx(Program* program) : empty_blocks(program->blocks.size(), true), program(program) {}
  36. };
  37. void collect_phi_info(ssa_elimination_ctx& ctx)
  38. {
  39. for (Block& block : ctx.program->blocks) {
  40. for (aco_ptr<Instruction>& phi : block.instructions) {
  41. if (phi->opcode != aco_opcode::p_phi && phi->opcode != aco_opcode::p_linear_phi)
  42. break;
  43. for (unsigned i = 0; i < phi->operands.size(); i++) {
  44. if (phi->operands[i].isUndefined())
  45. continue;
  46. if (phi->operands[i].isTemp() && phi->operands[i].physReg() == phi->definitions[0].physReg())
  47. continue;
  48. std::vector<unsigned>& preds = phi->opcode == aco_opcode::p_phi ? block.logical_preds : block.linear_preds;
  49. phi_info& info = phi->opcode == aco_opcode::p_phi ? ctx.logical_phi_info : ctx.linear_phi_info;
  50. const auto result = info.emplace(preds[i], std::vector<std::pair<Definition, Operand>>());
  51. assert(phi->definitions[0].size() == phi->operands[i].size());
  52. result.first->second.emplace_back(phi->definitions[0], phi->operands[i]);
  53. ctx.empty_blocks[preds[i]] = false;
  54. }
  55. }
  56. }
  57. }
  58. void insert_parallelcopies(ssa_elimination_ctx& ctx)
  59. {
  60. /* insert the parallelcopies from logical phis before p_logical_end */
  61. for (auto&& entry : ctx.logical_phi_info) {
  62. Block& block = ctx.program->blocks[entry.first];
  63. unsigned idx = block.instructions.size() - 1;
  64. while (block.instructions[idx]->opcode != aco_opcode::p_logical_end) {
  65. assert(idx > 0);
  66. idx--;
  67. }
  68. std::vector<aco_ptr<Instruction>>::iterator it = std::next(block.instructions.begin(), idx);
  69. aco_ptr<Pseudo_instruction> pc{create_instruction<Pseudo_instruction>(aco_opcode::p_parallelcopy, Format::PSEUDO, entry.second.size(), entry.second.size())};
  70. unsigned i = 0;
  71. for (std::pair<Definition, Operand>& pair : entry.second)
  72. {
  73. pc->definitions[i] = pair.first;
  74. pc->operands[i] = pair.second;
  75. i++;
  76. }
  77. /* this shouldn't be needed since we're only copying vgprs */
  78. pc->tmp_in_scc = false;
  79. block.instructions.insert(it, std::move(pc));
  80. }
  81. /* insert parallelcopies for the linear phis at the end of blocks just before the branch */
  82. for (auto&& entry : ctx.linear_phi_info) {
  83. Block& block = ctx.program->blocks[entry.first];
  84. std::vector<aco_ptr<Instruction>>::iterator it = block.instructions.end();
  85. --it;
  86. assert((*it)->format == Format::PSEUDO_BRANCH);
  87. aco_ptr<Pseudo_instruction> pc{create_instruction<Pseudo_instruction>(aco_opcode::p_parallelcopy, Format::PSEUDO, entry.second.size(), entry.second.size())};
  88. unsigned i = 0;
  89. for (std::pair<Definition, Operand>& pair : entry.second)
  90. {
  91. pc->definitions[i] = pair.first;
  92. pc->operands[i] = pair.second;
  93. i++;
  94. }
  95. pc->tmp_in_scc = block.scc_live_out;
  96. pc->scratch_sgpr = block.scratch_sgpr;
  97. block.instructions.insert(it, std::move(pc));
  98. }
  99. }
  100. void try_remove_merge_block(ssa_elimination_ctx& ctx, Block* block)
  101. {
  102. /* check if the successor is another merge block which restores exec */
  103. // TODO: divergent loops also restore exec
  104. if (block->linear_succs.size() != 1 ||
  105. !(ctx.program->blocks[block->linear_succs[0]].kind & block_kind_merge))
  106. return;
  107. /* check if this block is empty and the exec mask is not needed */
  108. for (aco_ptr<Instruction>& instr : block->instructions) {
  109. if (instr->opcode == aco_opcode::p_parallelcopy) {
  110. if (instr->definitions[0].physReg() == exec)
  111. continue;
  112. else
  113. return;
  114. }
  115. if (instr->opcode != aco_opcode::p_linear_phi &&
  116. instr->opcode != aco_opcode::p_phi &&
  117. instr->opcode != aco_opcode::p_logical_start &&
  118. instr->opcode != aco_opcode::p_logical_end &&
  119. instr->opcode != aco_opcode::p_branch)
  120. return;
  121. }
  122. /* keep the branch instruction and remove the rest */
  123. aco_ptr<Instruction> branch = std::move(block->instructions.back());
  124. block->instructions.clear();
  125. block->instructions.emplace_back(std::move(branch));
  126. }
  127. void try_remove_invert_block(ssa_elimination_ctx& ctx, Block* block)
  128. {
  129. assert(block->linear_succs.size() == 2);
  130. if (block->linear_succs[0] != block->linear_succs[1])
  131. return;
  132. /* check if we can remove this block */
  133. for (aco_ptr<Instruction>& instr : block->instructions) {
  134. if (instr->opcode != aco_opcode::p_linear_phi &&
  135. instr->opcode != aco_opcode::p_phi &&
  136. (instr->opcode != aco_opcode::s_andn2_b64 || ctx.program->wave_size != 64) &&
  137. (instr->opcode != aco_opcode::s_andn2_b32 || ctx.program->wave_size != 32) &&
  138. instr->opcode != aco_opcode::p_branch)
  139. return;
  140. }
  141. unsigned succ_idx = block->linear_succs[0];
  142. assert(block->linear_preds.size() == 2);
  143. for (unsigned i = 0; i < 2; i++) {
  144. Block *pred = &ctx.program->blocks[block->linear_preds[i]];
  145. pred->linear_succs[0] = succ_idx;
  146. ctx.program->blocks[succ_idx].linear_preds[i] = pred->index;
  147. Pseudo_branch_instruction *branch = static_cast<Pseudo_branch_instruction*>(pred->instructions.back().get());
  148. assert(branch->format == Format::PSEUDO_BRANCH);
  149. branch->target[0] = succ_idx;
  150. branch->target[1] = succ_idx;
  151. }
  152. block->instructions.clear();
  153. block->linear_preds.clear();
  154. block->linear_succs.clear();
  155. }
  156. void try_remove_simple_block(ssa_elimination_ctx& ctx, Block* block)
  157. {
  158. for (aco_ptr<Instruction>& instr : block->instructions) {
  159. if (instr->opcode != aco_opcode::p_logical_start &&
  160. instr->opcode != aco_opcode::p_logical_end &&
  161. instr->opcode != aco_opcode::p_branch)
  162. return;
  163. }
  164. Block& pred = ctx.program->blocks[block->linear_preds[0]];
  165. Block& succ = ctx.program->blocks[block->linear_succs[0]];
  166. Pseudo_branch_instruction* branch = static_cast<Pseudo_branch_instruction*>(pred.instructions.back().get());
  167. if (branch->opcode == aco_opcode::p_branch) {
  168. branch->target[0] = succ.index;
  169. branch->target[1] = succ.index;
  170. } else if (branch->target[0] == block->index) {
  171. branch->target[0] = succ.index;
  172. } else if (branch->target[0] == succ.index) {
  173. assert(branch->target[1] == block->index);
  174. branch->target[1] = succ.index;
  175. branch->opcode = aco_opcode::p_branch;
  176. } else if (branch->target[1] == block->index) {
  177. /* check if there is a fall-through path from block to succ */
  178. bool falls_through = true;
  179. for (unsigned j = block->index + 1; falls_through && j < succ.index; j++) {
  180. assert(ctx.program->blocks[j].index == j);
  181. if (!ctx.program->blocks[j].instructions.empty())
  182. falls_through = false;
  183. }
  184. if (falls_through) {
  185. branch->target[1] = succ.index;
  186. } else {
  187. /* check if there is a fall-through path for the alternative target */
  188. for (unsigned j = block->index + 1; j < branch->target[0]; j++) {
  189. if (!ctx.program->blocks[j].instructions.empty())
  190. return;
  191. }
  192. /* This is a (uniform) break or continue block. The branch condition has to be inverted. */
  193. if (branch->opcode == aco_opcode::p_cbranch_z)
  194. branch->opcode = aco_opcode::p_cbranch_nz;
  195. else if (branch->opcode == aco_opcode::p_cbranch_nz)
  196. branch->opcode = aco_opcode::p_cbranch_z;
  197. else
  198. assert(false);
  199. /* also invert the linear successors */
  200. pred.linear_succs[0] = pred.linear_succs[1];
  201. pred.linear_succs[1] = succ.index;
  202. branch->target[1] = branch->target[0];
  203. branch->target[0] = succ.index;
  204. }
  205. } else {
  206. assert(false);
  207. }
  208. if (branch->target[0] == branch->target[1])
  209. branch->opcode = aco_opcode::p_branch;
  210. for (unsigned i = 0; i < pred.linear_succs.size(); i++)
  211. if (pred.linear_succs[i] == block->index)
  212. pred.linear_succs[i] = succ.index;
  213. for (unsigned i = 0; i < succ.linear_preds.size(); i++)
  214. if (succ.linear_preds[i] == block->index)
  215. succ.linear_preds[i] = pred.index;
  216. block->instructions.clear();
  217. block->linear_preds.clear();
  218. block->linear_succs.clear();
  219. }
  220. void jump_threading(ssa_elimination_ctx& ctx)
  221. {
  222. for (int i = ctx.program->blocks.size() - 1; i >= 0; i--) {
  223. Block* block = &ctx.program->blocks[i];
  224. if (!ctx.empty_blocks[i])
  225. continue;
  226. if (block->kind & block_kind_invert) {
  227. try_remove_invert_block(ctx, block);
  228. continue;
  229. }
  230. if (block->linear_succs.size() > 1)
  231. continue;
  232. if (block->kind & block_kind_merge ||
  233. block->kind & block_kind_loop_exit)
  234. try_remove_merge_block(ctx, block);
  235. if (block->linear_preds.size() == 1)
  236. try_remove_simple_block(ctx, block);
  237. }
  238. }
  239. } /* end namespace */
  240. void ssa_elimination(Program* program)
  241. {
  242. ssa_elimination_ctx ctx(program);
  243. /* Collect information about every phi-instruction */
  244. collect_phi_info(ctx);
  245. /* eliminate empty blocks */
  246. jump_threading(ctx);
  247. /* insert parallelcopies from SSA elimination */
  248. insert_parallelcopies(ctx);
  249. }
  250. }