lima/ppir: simplify select op lowering and scheduling
[mesa.git] / src / gallium / drivers / lima / ir / pp / node_to_instr.c
1 /*
2 * Copyright (c) 2017 Lima Project
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, sub license,
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
12 * next paragraph) shall be included in all copies or substantial portions
13 * of the 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 NON-INFRINGEMENT. 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
21 * DEALINGS IN THE SOFTWARE.
22 *
23 */
24
25 #include "ppir.h"
26
27
28 static bool create_new_instr(ppir_block *block, ppir_node *node)
29 {
30 ppir_instr *instr = ppir_instr_create(block);
31 if (unlikely(!instr))
32 return false;
33
34 if (!ppir_instr_insert_node(instr, node))
35 return false;
36
37 return true;
38 }
39
40 static bool insert_to_each_succ_instr(ppir_block *block, ppir_node *node)
41 {
42 ppir_dest *dest = ppir_node_get_dest(node);
43 assert(dest->type == ppir_target_ssa);
44
45 ppir_node *move = NULL;
46
47 ppir_node_foreach_succ_safe(node, dep) {
48 ppir_node *succ = dep->succ;
49 assert(succ->type == ppir_node_type_alu ||
50 succ->type == ppir_node_type_branch);
51
52 if (!ppir_instr_insert_node(succ->instr, node)) {
53 /* create a move node to insert for failed node */
54 if (!move) {
55 move = ppir_node_create(block, ppir_op_mov, -1, 0);
56 if (unlikely(!move))
57 return false;
58
59 ppir_debug("node_to_instr create move %d for %d\n",
60 move->index, node->index);
61
62 ppir_alu_node *alu = ppir_node_to_alu(move);
63 alu->dest = *dest;
64 alu->num_src = 1;
65 ppir_node_target_assign(alu->src, dest);
66 for (int i = 0; i < 4; i++)
67 alu->src->swizzle[i] = i;
68 }
69
70 ppir_node_replace_pred(dep, move);
71 ppir_node_replace_child(succ, node, move);
72 }
73 }
74
75 if (move) {
76 if (!create_new_instr(block, move))
77 return false;
78
79 ASSERTED bool insert_result =
80 ppir_instr_insert_node(move->instr, node);
81 assert(insert_result);
82
83 ppir_node_add_dep(move, node);
84 list_addtail(&move->list, &node->list);
85 }
86
87 /* dupliacte node for each successor */
88
89 bool first = true;
90 struct list_head dup_list;
91 list_inithead(&dup_list);
92
93 ppir_node_foreach_succ_safe(node, dep) {
94 ppir_node *succ = dep->succ;
95
96 if (first) {
97 first = false;
98 node->instr = succ->instr;
99 continue;
100 }
101
102 if (succ->instr == node->instr)
103 continue;
104
105 list_for_each_entry(ppir_node, dup, &dup_list, list) {
106 if (succ->instr == dup->instr) {
107 ppir_node_replace_pred(dep, dup);
108 continue;
109 }
110 }
111
112 ppir_node *dup = ppir_node_create(block, node->op, -1, 0);
113 if (unlikely(!dup))
114 return false;
115 list_addtail(&dup->list, &dup_list);
116
117 ppir_debug("node_to_instr duplicate %s %d from %d\n",
118 ppir_op_infos[dup->op].name, dup->index, node->index);
119
120 ppir_instr *instr = succ->instr;
121 dup->instr = instr;
122 dup->instr_pos = node->instr_pos;
123 ppir_node_replace_pred(dep, dup);
124
125 if ((node->op == ppir_op_load_uniform) || (node->op == ppir_op_load_temp)) {
126 ppir_load_node *load = ppir_node_to_load(node);
127 ppir_load_node *dup_load = ppir_node_to_load(dup);
128 dup_load->dest = load->dest;
129 dup_load->index = load->index;
130 dup_load->num_components = load->num_components;
131 instr->slots[node->instr_pos] = dup;
132 }
133 }
134
135 list_splicetail(&dup_list, &node->list);
136
137 return true;
138 }
139
140 /*
141 * If a node has a pipeline dest, schedule it in the same instruction as its
142 * successor.
143 * Since it has a pipeline dest, it must have only one successor and since we
144 * schedule nodes backwards, its successor must have already been scheduled.
145 */
146 static bool ppir_do_node_to_instr_pipeline(ppir_block *block, ppir_node *node)
147 {
148 ppir_dest *dest = ppir_node_get_dest(node);
149
150 if (!dest || dest->type != ppir_target_pipeline)
151 return false;
152
153 assert(ppir_node_has_single_succ(node));
154 ppir_node *succ = ppir_node_first_succ(node);
155 assert(succ);
156 assert(succ->instr);
157
158 if (!ppir_instr_insert_node(succ->instr, node))
159 return false;
160
161 return true;
162 }
163
164 static bool ppir_do_one_node_to_instr(ppir_block *block, ppir_node *node, ppir_node **next)
165 {
166 switch (node->type) {
167 case ppir_node_type_alu:
168 {
169 /* merge pred mul and succ add in the same instr can save a reg
170 * by using pipeline reg ^vmul/^fmul */
171 ppir_alu_node *alu = ppir_node_to_alu(node);
172 if (alu->dest.type == ppir_target_ssa &&
173 ppir_node_has_single_succ(node)) {
174 ppir_node *succ = ppir_node_first_succ(node);
175 if (succ->instr_pos == PPIR_INSTR_SLOT_ALU_VEC_ADD) {
176 node->instr_pos = PPIR_INSTR_SLOT_ALU_VEC_MUL;
177 ppir_instr_insert_mul_node(succ, node);
178 }
179 else if (succ->instr_pos == PPIR_INSTR_SLOT_ALU_SCL_ADD &&
180 alu->dest.ssa.num_components == 1) {
181 node->instr_pos = PPIR_INSTR_SLOT_ALU_SCL_MUL;
182 ppir_instr_insert_mul_node(succ, node);
183 }
184 }
185
186 /* can't inserted to any existing instr, create one */
187 if (!node->instr && !create_new_instr(block, node))
188 return false;
189
190 break;
191 }
192 case ppir_node_type_load:
193 if ((node->op == ppir_op_load_uniform) || (node->op == ppir_op_load_temp)) {
194 /* merge pred load_uniform into succ instr can save a reg
195 * by using pipeline reg */
196 if (!insert_to_each_succ_instr(block, node))
197 return false;
198
199 ppir_load_node *load = ppir_node_to_load(node);
200 load->dest.type = ppir_target_pipeline;
201 load->dest.pipeline = ppir_pipeline_reg_uniform;
202 }
203 else if (node->op == ppir_op_load_temp) {
204 /* merge pred load_temp into succ instr can save a reg
205 * by using pipeline reg */
206 if (!insert_to_each_succ_instr(block, node))
207 return false;
208
209 ppir_load_node *load = ppir_node_to_load(node);
210 load->dest.type = ppir_target_pipeline;
211 load->dest.pipeline = ppir_pipeline_reg_uniform;
212 }
213 else if (node->op == ppir_op_load_varying ||
214 node->op == ppir_op_load_fragcoord ||
215 node->op == ppir_op_load_pointcoord ||
216 node->op == ppir_op_load_frontface) {
217 /* delay the load varying dup to scheduler */
218 if (!create_new_instr(block, node))
219 return false;
220 }
221 else {
222 /* not supported yet */
223 assert(0);
224 return false;
225 }
226 break;
227 case ppir_node_type_load_texture:
228 if (!create_new_instr(block, node))
229 return false;
230 break;
231 case ppir_node_type_const:
232 if (!insert_to_each_succ_instr(block, node))
233 return false;
234 break;
235 case ppir_node_type_store:
236 {
237 if (node->op == ppir_op_store_temp) {
238 if (!create_new_instr(block, node))
239 return false;
240 break;
241 }
242
243 /* Only the store color node should appear here.
244 * Currently we always insert a move node as the end instr.
245 * But it should only be done when:
246 * 1. store a const node
247 * 2. store a load node
248 * 3. store a reg assigned in another block like loop/if
249 */
250
251 assert(node->op == ppir_op_store_color);
252
253 ppir_node *move = ppir_node_create(block, ppir_op_mov, -1, 0);
254 if (unlikely(!move))
255 return false;
256
257 ppir_debug("node_to_instr create move %d from store %d\n",
258 move->index, node->index);
259
260 ppir_node_foreach_pred_safe(node, dep) {
261 ppir_node *pred = dep->pred;
262 /* we can't do this in this function except here as this
263 * store is the root of this recursion */
264 ppir_node_remove_dep(dep);
265 ppir_node_add_dep(move, pred);
266 }
267
268 ppir_node_add_dep(node, move);
269 list_addtail(&move->list, &node->list);
270
271 ppir_alu_node *alu = ppir_node_to_alu(move);
272 ppir_store_node *store = ppir_node_to_store(node);
273 alu->src[0] = store->src;
274 alu->num_src = 1;
275
276 alu->dest.type = ppir_target_ssa;
277 alu->dest.ssa.num_components = 4;
278 alu->dest.ssa.live_in = INT_MAX;
279 alu->dest.ssa.live_out = 0;
280 alu->dest.write_mask = 0xf;
281
282 store->src.type = ppir_target_ssa;
283 store->src.ssa = &alu->dest.ssa;
284
285 if (!create_new_instr(block, move))
286 return false;
287
288 move->instr->is_end = true;
289 node->instr = move->instr;
290
291 /* use move for the following recursion */
292 *next = move;
293 break;
294 }
295 case ppir_node_type_discard:
296 if (!create_new_instr(block, node))
297 return false;
298 node->instr->is_end = true;
299 break;
300 case ppir_node_type_branch:
301 if (!create_new_instr(block, node))
302 return false;
303 break;
304 default:
305 return false;
306 }
307
308 return true;
309 }
310
311 static bool ppir_do_node_to_instr(ppir_block *block, ppir_node *node)
312 {
313 ppir_node *next = node;
314
315 /* first try pipeline sched, if that didn't succeed try normal scheduling */
316 if (!ppir_do_node_to_instr_pipeline(block, node))
317 if (!ppir_do_one_node_to_instr(block, node, &next))
318 return false;
319
320 /* next may have been updated in ppir_do_one_node_to_instr */
321 node = next;
322
323 /* we have to make sure the dep not be destroyed (due to
324 * succ change) in ppir_do_node_to_instr, otherwise we can't
325 * do recursion like this */
326 ppir_node_foreach_pred(node, dep) {
327 ppir_node *pred = dep->pred;
328 bool ready = true;
329
330 /* pred may already be processed by the previous pred
331 * (this pred may be both node and previous pred's child) */
332 if (pred->instr)
333 continue;
334
335 /* insert pred only when all its successors have been inserted */
336 ppir_node_foreach_succ(pred, dep) {
337 ppir_node *succ = dep->succ;
338 if (!succ->instr) {
339 ready = false;
340 break;
341 }
342 }
343
344 if (ready) {
345 if (!ppir_do_node_to_instr(block, pred))
346 return false;
347 }
348 }
349
350 return true;
351 }
352
353 static bool ppir_create_instr_from_node(ppir_compiler *comp)
354 {
355 list_for_each_entry(ppir_block, block, &comp->block_list, list) {
356 list_for_each_entry(ppir_node, node, &block->node_list, list) {
357 if (ppir_node_is_root(node)) {
358 if (!ppir_do_node_to_instr(block, node))
359 return false;
360 }
361 }
362 }
363
364 return true;
365 }
366
367 static void ppir_build_instr_dependency(ppir_compiler *comp)
368 {
369 list_for_each_entry(ppir_block, block, &comp->block_list, list) {
370 list_for_each_entry(ppir_instr, instr, &block->instr_list, list) {
371 for (int i = 0; i < PPIR_INSTR_SLOT_NUM; i++) {
372 ppir_node *node = instr->slots[i];
373 if (node) {
374 ppir_node_foreach_pred(node, dep) {
375 ppir_node *pred = dep->pred;
376 if (pred->instr && pred->instr != instr)
377 ppir_instr_add_dep(instr, pred->instr);
378 }
379 }
380 }
381 }
382 }
383 }
384
385 bool ppir_node_to_instr(ppir_compiler *comp)
386 {
387 if (!ppir_create_instr_from_node(comp))
388 return false;
389 ppir_instr_print_list(comp);
390
391 ppir_build_instr_dependency(comp);
392 ppir_instr_print_dep(comp);
393
394 return true;
395 }