ir3: Plumb through bindless support
[mesa.git] / src / freedreno / ir3 / ir3_group.c
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
27 #include "ir3.h"
28
29 /*
30 * Find/group instruction neighbors:
31 */
32
33 /* bleh.. we need to do the same group_n() thing for both inputs/outputs
34 * (where we have a simple instr[] array), and fanin nodes (where we have
35 * an extra indirection via reg->instr).
36 */
37 struct group_ops {
38 struct ir3_instruction *(*get)(void *arr, int idx);
39 void (*insert_mov)(void *arr, int idx, struct ir3_instruction *instr);
40 };
41
42 static struct ir3_instruction *instr_get(void *arr, int idx)
43 {
44 return ssa(((struct ir3_instruction *)arr)->regs[idx+1]);
45 }
46 static void
47 instr_insert_mov(void *arr, int idx, struct ir3_instruction *instr)
48 {
49 ((struct ir3_instruction *)arr)->regs[idx+1]->instr =
50 ir3_MOV(instr->block, instr, TYPE_F32);
51 }
52 static struct group_ops instr_ops = { instr_get, instr_insert_mov };
53
54 /* verify that cur != instr, but cur is also not in instr's neighbor-list: */
55 static bool
56 in_neighbor_list(struct ir3_instruction *instr, struct ir3_instruction *cur, int pos)
57 {
58 int idx = 0;
59
60 if (!instr)
61 return false;
62
63 if (instr == cur)
64 return true;
65
66 for (instr = ir3_neighbor_first(instr); instr; instr = instr->cp.right)
67 if ((idx++ != pos) && (instr == cur))
68 return true;
69
70 return false;
71 }
72
73 static void
74 group_n(struct group_ops *ops, void *arr, unsigned n)
75 {
76 unsigned i, j;
77
78 /* first pass, figure out what has conflicts and needs a mov
79 * inserted. Do this up front, before starting to setup
80 * left/right neighbor pointers. Trying to do it in a single
81 * pass could result in a situation where we can't even setup
82 * the mov's right neighbor ptr if the next instr also needs
83 * a mov.
84 */
85 restart:
86 for (i = 0; i < n; i++) {
87 struct ir3_instruction *instr = ops->get(arr, i);
88 if (instr) {
89 struct ir3_instruction *left = (i > 0) ? ops->get(arr, i - 1) : NULL;
90 struct ir3_instruction *right = (i < (n-1)) ? ops->get(arr, i + 1) : NULL;
91 bool conflict;
92
93 /* check for left/right neighbor conflicts: */
94 conflict = conflicts(instr->cp.left, left) ||
95 conflicts(instr->cp.right, right);
96
97 /* Mixing array elements and higher register classes
98 * (ie. groups) doesn't really work out in RA. See:
99 *
100 * https://trello.com/c/DqeDkeVf/156-bug-with-stk-70frag
101 */
102 if (instr->regs[0]->flags & IR3_REG_ARRAY)
103 conflict = true;
104
105 /* we also can't have an instr twice in the group: */
106 for (j = i + 1; (j < n) && !conflict; j++)
107 if (in_neighbor_list(ops->get(arr, j), instr, i))
108 conflict = true;
109
110 if (conflict) {
111 ops->insert_mov(arr, i, instr);
112 /* inserting the mov may have caused a conflict
113 * against the previous:
114 */
115 goto restart;
116 }
117 }
118 }
119
120 /* second pass, now that we've inserted mov's, fixup left/right
121 * neighbors. This is guaranteed to succeed, since by definition
122 * the newly inserted mov's cannot conflict with anything.
123 */
124 for (i = 0; i < n; i++) {
125 struct ir3_instruction *instr = ops->get(arr, i);
126 if (instr) {
127 struct ir3_instruction *left = (i > 0) ? ops->get(arr, i - 1) : NULL;
128 struct ir3_instruction *right = (i < (n-1)) ? ops->get(arr, i + 1) : NULL;
129
130 debug_assert(!conflicts(instr->cp.left, left));
131 if (left) {
132 instr->cp.left_cnt++;
133 instr->cp.left = left;
134 }
135
136 debug_assert(!conflicts(instr->cp.right, right));
137 if (right) {
138 instr->cp.right_cnt++;
139 instr->cp.right = right;
140 }
141 }
142 }
143 }
144
145 static void
146 instr_find_neighbors(struct ir3_instruction *instr)
147 {
148 struct ir3_instruction *src;
149
150 if (ir3_instr_check_mark(instr))
151 return;
152
153 if (instr->opc == OPC_META_COLLECT)
154 group_n(&instr_ops, instr, instr->regs_count - 1);
155
156 foreach_ssa_src (src, instr)
157 instr_find_neighbors(src);
158 }
159
160 static void
161 find_neighbors(struct ir3 *ir)
162 {
163 unsigned i;
164
165 struct ir3_instruction *out;
166 foreach_output (out, ir)
167 instr_find_neighbors(out);
168
169 foreach_block (block, &ir->block_list) {
170 for (i = 0; i < block->keeps_count; i++) {
171 struct ir3_instruction *instr = block->keeps[i];
172 instr_find_neighbors(instr);
173 }
174
175 /* We also need to account for if-condition: */
176 if (block->condition)
177 instr_find_neighbors(block->condition);
178 }
179 }
180
181 void
182 ir3_group(struct ir3 *ir)
183 {
184 ir3_clear_mark(ir);
185 find_neighbors(ir);
186 }