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.

ir3_group.c 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
  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 FROM,
  20. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. * SOFTWARE.
  22. *
  23. * Authors:
  24. * Rob Clark <robclark@freedesktop.org>
  25. */
  26. #include "ir3.h"
  27. /*
  28. * Find/group instruction neighbors:
  29. */
  30. /* bleh.. we need to do the same group_n() thing for both inputs/outputs
  31. * (where we have a simple instr[] array), and fanin nodes (where we have
  32. * an extra indirection via reg->instr).
  33. */
  34. struct group_ops {
  35. struct ir3_instruction *(*get)(void *arr, int idx);
  36. void (*insert_mov)(void *arr, int idx, struct ir3_instruction *instr);
  37. };
  38. static struct ir3_instruction *arr_get(void *arr, int idx)
  39. {
  40. return ((struct ir3_instruction **)arr)[idx];
  41. }
  42. static void arr_insert_mov_out(void *arr, int idx, struct ir3_instruction *instr)
  43. {
  44. ((struct ir3_instruction **)arr)[idx] =
  45. ir3_MOV(instr->block, instr, TYPE_F32);
  46. }
  47. static void arr_insert_mov_in(void *arr, int idx, struct ir3_instruction *instr)
  48. {
  49. /* so, we can't insert a mov in front of a meta:in.. and the downstream
  50. * instruction already has a pointer to 'instr'. So we cheat a bit and
  51. * morph the meta:in instruction into a mov and insert a new meta:in
  52. * in front.
  53. */
  54. struct ir3_instruction *in;
  55. debug_assert(instr->regs_count == 1);
  56. in = ir3_instr_create(instr->block, OPC_META_INPUT);
  57. in->inout.block = instr->block;
  58. ir3_reg_create(in, instr->regs[0]->num, 0);
  59. /* create src reg for meta:in and fixup to now be a mov: */
  60. ir3_reg_create(instr, 0, IR3_REG_SSA)->instr = in;
  61. instr->opc = OPC_MOV;
  62. instr->cat1.src_type = TYPE_F32;
  63. instr->cat1.dst_type = TYPE_F32;
  64. ((struct ir3_instruction **)arr)[idx] = in;
  65. }
  66. static struct group_ops arr_ops_out = { arr_get, arr_insert_mov_out };
  67. static struct group_ops arr_ops_in = { arr_get, arr_insert_mov_in };
  68. static struct ir3_instruction *instr_get(void *arr, int idx)
  69. {
  70. return ssa(((struct ir3_instruction *)arr)->regs[idx+1]);
  71. }
  72. static void
  73. instr_insert_mov(void *arr, int idx, struct ir3_instruction *instr)
  74. {
  75. ((struct ir3_instruction *)arr)->regs[idx+1]->instr =
  76. ir3_MOV(instr->block, instr, TYPE_F32);
  77. }
  78. static struct group_ops instr_ops = { instr_get, instr_insert_mov };
  79. /* verify that cur != instr, but cur is also not in instr's neighbor-list: */
  80. static bool
  81. in_neighbor_list(struct ir3_instruction *instr, struct ir3_instruction *cur, int pos)
  82. {
  83. int idx = 0;
  84. if (!instr)
  85. return false;
  86. if (instr == cur)
  87. return true;
  88. for (instr = ir3_neighbor_first(instr); instr; instr = instr->cp.right)
  89. if ((idx++ != pos) && (instr == cur))
  90. return true;
  91. return false;
  92. }
  93. static void
  94. group_n(struct group_ops *ops, void *arr, unsigned n)
  95. {
  96. unsigned i, j;
  97. /* first pass, figure out what has conflicts and needs a mov
  98. * inserted. Do this up front, before starting to setup
  99. * left/right neighbor pointers. Trying to do it in a single
  100. * pass could result in a situation where we can't even setup
  101. * the mov's right neighbor ptr if the next instr also needs
  102. * a mov.
  103. */
  104. restart:
  105. for (i = 0; i < n; i++) {
  106. struct ir3_instruction *instr = ops->get(arr, i);
  107. if (instr) {
  108. struct ir3_instruction *left = (i > 0) ? ops->get(arr, i - 1) : NULL;
  109. struct ir3_instruction *right = (i < (n-1)) ? ops->get(arr, i + 1) : NULL;
  110. bool conflict;
  111. /* check for left/right neighbor conflicts: */
  112. conflict = conflicts(instr->cp.left, left) ||
  113. conflicts(instr->cp.right, right);
  114. /* Mixing array elements and higher register classes
  115. * (ie. groups) doesn't really work out in RA. See:
  116. *
  117. * https://trello.com/c/DqeDkeVf/156-bug-with-stk-70frag
  118. */
  119. if (instr->regs[0]->flags & IR3_REG_ARRAY)
  120. conflict = true;
  121. /* we also can't have an instr twice in the group: */
  122. for (j = i + 1; (j < n) && !conflict; j++)
  123. if (in_neighbor_list(ops->get(arr, j), instr, i))
  124. conflict = true;
  125. if (conflict) {
  126. ops->insert_mov(arr, i, instr);
  127. /* inserting the mov may have caused a conflict
  128. * against the previous:
  129. */
  130. goto restart;
  131. }
  132. }
  133. }
  134. /* second pass, now that we've inserted mov's, fixup left/right
  135. * neighbors. This is guaranteed to succeed, since by definition
  136. * the newly inserted mov's cannot conflict with anything.
  137. */
  138. for (i = 0; i < n; i++) {
  139. struct ir3_instruction *instr = ops->get(arr, i);
  140. if (instr) {
  141. struct ir3_instruction *left = (i > 0) ? ops->get(arr, i - 1) : NULL;
  142. struct ir3_instruction *right = (i < (n-1)) ? ops->get(arr, i + 1) : NULL;
  143. debug_assert(!conflicts(instr->cp.left, left));
  144. if (left) {
  145. instr->cp.left_cnt++;
  146. instr->cp.left = left;
  147. }
  148. debug_assert(!conflicts(instr->cp.right, right));
  149. if (right) {
  150. instr->cp.right_cnt++;
  151. instr->cp.right = right;
  152. }
  153. }
  154. }
  155. }
  156. static void
  157. instr_find_neighbors(struct ir3_instruction *instr)
  158. {
  159. struct ir3_instruction *src;
  160. if (ir3_instr_check_mark(instr))
  161. return;
  162. if (instr->opc == OPC_META_FI)
  163. group_n(&instr_ops, instr, instr->regs_count - 1);
  164. foreach_ssa_src(src, instr)
  165. instr_find_neighbors(src);
  166. }
  167. /* a bit of sadness.. we can't have "holes" in inputs from PoV of
  168. * register assignment, they still need to be grouped together. So
  169. * we need to insert dummy/padding instruction for grouping, and
  170. * then take it back out again before anyone notices.
  171. */
  172. static void
  173. pad_and_group_input(struct ir3_instruction **input, unsigned n)
  174. {
  175. int i, mask = 0;
  176. struct ir3_block *block = NULL;
  177. for (i = n - 1; i >= 0; i--) {
  178. struct ir3_instruction *instr = input[i];
  179. if (instr) {
  180. block = instr->block;
  181. } else if (block) {
  182. instr = ir3_NOP(block);
  183. ir3_reg_create(instr, 0, IR3_REG_SSA); /* dummy dst */
  184. input[i] = instr;
  185. mask |= (1 << i);
  186. }
  187. }
  188. group_n(&arr_ops_in, input, n);
  189. for (i = 0; i < n; i++) {
  190. if (mask & (1 << i))
  191. input[i] = NULL;
  192. }
  193. }
  194. static void
  195. find_neighbors(struct ir3 *ir)
  196. {
  197. unsigned i;
  198. /* shader inputs/outputs themselves must be contiguous as well:
  199. *
  200. * NOTE: group inputs first, since we only insert mov's
  201. * *before* the conflicted instr (and that would go badly
  202. * for inputs). By doing inputs first, we should never
  203. * have a conflict on inputs.. pushing any conflict to
  204. * resolve to the outputs, for stuff like:
  205. *
  206. * MOV OUT[n], IN[m].wzyx
  207. *
  208. * NOTE: we assume here inputs/outputs are grouped in vec4.
  209. * This logic won't quite cut it if we don't align smaller
  210. * on vec4 boundaries
  211. */
  212. for (i = 0; i < ir->ninputs; i += 4)
  213. pad_and_group_input(&ir->inputs[i], 4);
  214. for (i = 0; i < ir->noutputs; i += 4)
  215. group_n(&arr_ops_out, &ir->outputs[i], 4);
  216. for (i = 0; i < ir->noutputs; i++) {
  217. if (ir->outputs[i]) {
  218. struct ir3_instruction *instr = ir->outputs[i];
  219. instr_find_neighbors(instr);
  220. }
  221. }
  222. list_for_each_entry (struct ir3_block, block, &ir->block_list, node) {
  223. for (i = 0; i < block->keeps_count; i++) {
  224. struct ir3_instruction *instr = block->keeps[i];
  225. instr_find_neighbors(instr);
  226. }
  227. /* We also need to account for if-condition: */
  228. if (block->condition)
  229. instr_find_neighbors(block->condition);
  230. }
  231. }
  232. void
  233. ir3_group(struct ir3 *ir)
  234. {
  235. ir3_clear_mark(ir);
  236. find_neighbors(ir);
  237. }