a8706627f38e8c5c6bc75ab5f57bec21acfa456a
[mesa.git] / src / gallium / drivers / lima / ir / gp / node.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 "util/u_math.h"
26 #include "util/ralloc.h"
27
28 #include "gpir.h"
29
30 const gpir_op_info gpir_op_infos[] = {
31 [gpir_op_mov] = {
32 .name = "mov",
33 .slots = (int []) {
34 GPIR_INSTR_SLOT_ADD0, GPIR_INSTR_SLOT_MUL1,
35 GPIR_INSTR_SLOT_ADD1, GPIR_INSTR_SLOT_MUL0,
36 GPIR_INSTR_SLOT_PASS, GPIR_INSTR_SLOT_COMPLEX,
37 GPIR_INSTR_SLOT_END
38 },
39 },
40 [gpir_op_mul] = {
41 .name = "mul",
42 .dest_neg = true,
43 .slots = (int []) { GPIR_INSTR_SLOT_MUL1, GPIR_INSTR_SLOT_MUL0, GPIR_INSTR_SLOT_END },
44 },
45 [gpir_op_select] = {
46 .name = "select",
47 .dest_neg = true,
48 .slots = (int []) { GPIR_INSTR_SLOT_MUL0, GPIR_INSTR_SLOT_END },
49 .may_consume_two_slots = true,
50 },
51 [gpir_op_complex1] = {
52 .name = "complex1",
53 .slots = (int []) { GPIR_INSTR_SLOT_MUL0, GPIR_INSTR_SLOT_END },
54 .spillless = true,
55 .may_consume_two_slots = true,
56 },
57 [gpir_op_complex2] = {
58 .name = "complex2",
59 .slots = (int []) { GPIR_INSTR_SLOT_MUL0, GPIR_INSTR_SLOT_END },
60 .spillless = true,
61 .schedule_first = true,
62 },
63 [gpir_op_add] = {
64 .name = "add",
65 .src_neg = {true, true, false, false},
66 .slots = (int []) { GPIR_INSTR_SLOT_ADD0, GPIR_INSTR_SLOT_ADD1, GPIR_INSTR_SLOT_END },
67 },
68 [gpir_op_floor] = {
69 .name = "floor",
70 .src_neg = {true, false, false, false},
71 .slots = (int []) { GPIR_INSTR_SLOT_ADD0, GPIR_INSTR_SLOT_ADD1, GPIR_INSTR_SLOT_END },
72 .spillless = true,
73 .may_consume_two_slots = true,
74 },
75 [gpir_op_sign] = {
76 .name = "sign",
77 .src_neg = {true, false, false, false},
78 .slots = (int []) { GPIR_INSTR_SLOT_ADD0, GPIR_INSTR_SLOT_ADD1, GPIR_INSTR_SLOT_END },
79 .spillless = true,
80 .may_consume_two_slots = true,
81 },
82 [gpir_op_ge] = {
83 .name = "ge",
84 .src_neg = {true, true, false, false},
85 .slots = (int []) { GPIR_INSTR_SLOT_ADD0, GPIR_INSTR_SLOT_ADD1, GPIR_INSTR_SLOT_END },
86 .spillless = true,
87 .may_consume_two_slots = true,
88 },
89 [gpir_op_lt] = {
90 .name = "lt",
91 .src_neg = {true, true, false, false},
92 .slots = (int []) { GPIR_INSTR_SLOT_ADD0, GPIR_INSTR_SLOT_ADD1, GPIR_INSTR_SLOT_END },
93 .spillless = true,
94 .may_consume_two_slots = true,
95 },
96 [gpir_op_min] = {
97 .name = "min",
98 .src_neg = {true, true, false, false},
99 .slots = (int []) { GPIR_INSTR_SLOT_ADD0, GPIR_INSTR_SLOT_ADD1, GPIR_INSTR_SLOT_END },
100 .spillless = true,
101 .may_consume_two_slots = true,
102 },
103 [gpir_op_max] = {
104 .name = "max",
105 .src_neg = {true, true, false, false},
106 .slots = (int []) { GPIR_INSTR_SLOT_ADD0, GPIR_INSTR_SLOT_ADD1, GPIR_INSTR_SLOT_END },
107 .spillless = true,
108 .may_consume_two_slots = true,
109 },
110 [gpir_op_abs] = {
111 .name = "abs",
112 .src_neg = {true, true, false, false},
113 },
114 [gpir_op_neg] = {
115 .name = "neg",
116 .slots = (int []) {
117 GPIR_INSTR_SLOT_ADD0, GPIR_INSTR_SLOT_MUL1,
118 GPIR_INSTR_SLOT_ADD1, GPIR_INSTR_SLOT_MUL0,
119 GPIR_INSTR_SLOT_END
120 },
121 },
122 [gpir_op_not] = {
123 .name = "not",
124 .src_neg = {true, true, false, false},
125 .slots = (int []) { GPIR_INSTR_SLOT_ADD0, GPIR_INSTR_SLOT_ADD1, GPIR_INSTR_SLOT_END },
126 },
127 [gpir_op_eq] = {
128 .name = "eq",
129 .slots = (int []) {
130 GPIR_INSTR_SLOT_ADD0, GPIR_INSTR_SLOT_ADD1, GPIR_INSTR_SLOT_END
131 },
132 },
133 [gpir_op_ne] = {
134 .name = "ne",
135 .slots = (int []) {
136 GPIR_INSTR_SLOT_ADD0, GPIR_INSTR_SLOT_ADD1, GPIR_INSTR_SLOT_END
137 },
138 },
139 [gpir_op_clamp_const] = {
140 .name = "clamp_const",
141 },
142 [gpir_op_preexp2] = {
143 .name = "preexp2",
144 },
145 [gpir_op_postlog2] = {
146 .name = "postlog2",
147 },
148 [gpir_op_exp2_impl] = {
149 .name = "exp2_impl",
150 },
151 [gpir_op_log2_impl] = {
152 .name = "log2_impl",
153 },
154 [gpir_op_rcp_impl] = {
155 .name = "rcp_impl",
156 .slots = (int []) { GPIR_INSTR_SLOT_COMPLEX, GPIR_INSTR_SLOT_END },
157 .spillless = true,
158 .schedule_first = true,
159 },
160 [gpir_op_rsqrt_impl] = {
161 .name = "rsqrt_impl",
162 .slots = (int []) { GPIR_INSTR_SLOT_COMPLEX, GPIR_INSTR_SLOT_END },
163 .spillless = true,
164 .schedule_first = true,
165 },
166 [gpir_op_load_uniform] = {
167 .name = "ld_uni",
168 .slots = (int []) {
169 GPIR_INSTR_SLOT_MEM_LOAD0, GPIR_INSTR_SLOT_MEM_LOAD1,
170 GPIR_INSTR_SLOT_MEM_LOAD2, GPIR_INSTR_SLOT_MEM_LOAD3,
171 GPIR_INSTR_SLOT_END
172 },
173 .type = gpir_node_type_load,
174 },
175 [gpir_op_load_temp] = {
176 .name = "ld_tmp",
177 .type = gpir_node_type_load,
178 },
179 [gpir_op_load_attribute] = {
180 .name = "ld_att",
181 .slots = (int []) {
182 GPIR_INSTR_SLOT_REG0_LOAD0, GPIR_INSTR_SLOT_REG0_LOAD1,
183 GPIR_INSTR_SLOT_REG0_LOAD2, GPIR_INSTR_SLOT_REG0_LOAD3,
184 GPIR_INSTR_SLOT_END
185 },
186 .type = gpir_node_type_load,
187 },
188 [gpir_op_load_reg] = {
189 .name = "ld_reg",
190 .slots = (int []) {
191 GPIR_INSTR_SLOT_REG1_LOAD0, GPIR_INSTR_SLOT_REG1_LOAD1,
192 GPIR_INSTR_SLOT_REG1_LOAD2, GPIR_INSTR_SLOT_REG1_LOAD3,
193 GPIR_INSTR_SLOT_REG0_LOAD0, GPIR_INSTR_SLOT_REG0_LOAD1,
194 GPIR_INSTR_SLOT_REG0_LOAD2, GPIR_INSTR_SLOT_REG0_LOAD3,
195 GPIR_INSTR_SLOT_END
196 },
197 .type = gpir_node_type_load,
198 .spillless = true,
199 },
200 [gpir_op_store_temp] = {
201 .name = "st_tmp",
202 .type = gpir_node_type_store,
203 },
204 [gpir_op_store_reg] = {
205 .name = "st_reg",
206 .slots = (int []) {
207 GPIR_INSTR_SLOT_STORE0, GPIR_INSTR_SLOT_STORE1,
208 GPIR_INSTR_SLOT_STORE2, GPIR_INSTR_SLOT_STORE3,
209 GPIR_INSTR_SLOT_END
210 },
211 .type = gpir_node_type_store,
212 .spillless = true,
213 },
214 [gpir_op_store_varying] = {
215 .name = "st_var",
216 .slots = (int []) {
217 GPIR_INSTR_SLOT_STORE0, GPIR_INSTR_SLOT_STORE1,
218 GPIR_INSTR_SLOT_STORE2, GPIR_INSTR_SLOT_STORE3,
219 GPIR_INSTR_SLOT_END
220 },
221 .type = gpir_node_type_store,
222 .spillless = true,
223 },
224 [gpir_op_store_temp_load_off0] = {
225 .name = "st_of0",
226 .type = gpir_node_type_store,
227 },
228 [gpir_op_store_temp_load_off1] = {
229 .name = "st_of1",
230 .type = gpir_node_type_store,
231 },
232 [gpir_op_store_temp_load_off2] = {
233 .name = "st_of2",
234 .type = gpir_node_type_store,
235 },
236 [gpir_op_branch_cond] = {
237 .name = "branch_cond",
238 .type = gpir_node_type_branch,
239 },
240 [gpir_op_const] = {
241 .name = "const",
242 .type = gpir_node_type_const,
243 },
244 [gpir_op_exp2] = {
245 .name = "exp2",
246 },
247 [gpir_op_log2] = {
248 .name = "log2",
249 },
250 [gpir_op_rcp] = {
251 .name = "rcp",
252 },
253 [gpir_op_rsqrt] = {
254 .name = "rsqrt",
255 },
256 [gpir_op_ceil] = {
257 .name = "ceil",
258 },
259 [gpir_op_exp] = {
260 .name = "exp",
261 },
262 [gpir_op_log] = {
263 .name = "log",
264 },
265 [gpir_op_sin] = {
266 .name = "sin",
267 },
268 [gpir_op_cos] = {
269 .name = "cos",
270 },
271 [gpir_op_tan] = {
272 .name = "tan",
273 },
274 [gpir_op_dummy_f] = {
275 .name = "dummy_f",
276 .type = gpir_node_type_alu,
277 .spillless = true,
278 },
279 [gpir_op_dummy_m] = {
280 .name = "dummy_m",
281 .type = gpir_node_type_alu,
282 },
283 [gpir_op_branch_uncond] = {
284 .name = "branch_uncond",
285 .type = gpir_node_type_branch,
286 },
287 };
288
289 void *gpir_node_create(gpir_block *block, gpir_op op)
290 {
291 static const int node_size[] = {
292 [gpir_node_type_alu] = sizeof(gpir_alu_node),
293 [gpir_node_type_const] = sizeof(gpir_const_node),
294 [gpir_node_type_load] = sizeof(gpir_load_node),
295 [gpir_node_type_store] = sizeof(gpir_store_node),
296 [gpir_node_type_branch] = sizeof(gpir_branch_node),
297 };
298
299 gpir_node_type type = gpir_op_infos[op].type;
300 int size = node_size[type];
301 gpir_node *node = rzalloc_size(block, size);
302 if (unlikely(!node))
303 return NULL;
304
305 snprintf(node->name, sizeof(node->name), "new");
306
307 list_inithead(&node->succ_list);
308 list_inithead(&node->pred_list);
309
310 node->op = op;
311 node->type = type;
312 node->index = block->comp->cur_index++;
313 node->block = block;
314
315 return node;
316 }
317
318 gpir_dep *gpir_node_add_dep(gpir_node *succ, gpir_node *pred, int type)
319 {
320 /* don't add dep for two nodes from different block */
321 if (succ->block != pred->block)
322 return NULL;
323
324 /* don't add self loop dep */
325 if (succ == pred)
326 return NULL;
327
328 /* don't add duplicated dep */
329 gpir_node_foreach_pred(succ, dep) {
330 if (dep->pred == pred) {
331 /* use stronger dependency */
332 if (dep->type > type)
333 dep->type = type;
334 return dep;
335 }
336 }
337
338 gpir_dep *dep = ralloc(succ, gpir_dep);
339 dep->type = type;
340 dep->pred = pred;
341 dep->succ = succ;
342 list_addtail(&dep->pred_link, &succ->pred_list);
343 list_addtail(&dep->succ_link, &pred->succ_list);
344 return dep;
345 }
346
347 void gpir_node_remove_dep(gpir_node *succ, gpir_node *pred)
348 {
349 gpir_node_foreach_pred(succ, dep) {
350 if (dep->pred == pred) {
351 list_del(&dep->succ_link);
352 list_del(&dep->pred_link);
353 ralloc_free(dep);
354 return;
355 }
356 }
357 }
358
359 void gpir_node_replace_child(gpir_node *parent, gpir_node *old_child,
360 gpir_node *new_child)
361 {
362 if (parent->type == gpir_node_type_alu) {
363 gpir_alu_node *alu = gpir_node_to_alu(parent);
364 for (int i = 0; i < alu->num_child; i++) {
365 if (alu->children[i] == old_child)
366 alu->children[i] = new_child;
367 }
368 }
369 else if (parent->type == gpir_node_type_store) {
370 gpir_store_node *store = gpir_node_to_store(parent);
371 if (store->child == old_child)
372 store->child = new_child;
373 }
374 }
375
376 void gpir_node_replace_pred(gpir_dep *dep, gpir_node *new_pred)
377 {
378 list_del(&dep->succ_link);
379 dep->pred = new_pred;
380 list_addtail(&dep->succ_link, &new_pred->succ_list);
381 }
382
383 void gpir_node_replace_succ(gpir_node *dst, gpir_node *src)
384 {
385 gpir_node_foreach_succ_safe(src, dep) {
386 if (dep->type != GPIR_DEP_INPUT)
387 continue;
388
389 gpir_node_replace_pred(dep, dst);
390 gpir_node_replace_child(dep->succ, src, dst);
391 }
392 }
393
394 void gpir_node_insert_child(gpir_node *parent, gpir_node *child,
395 gpir_node *insert_child)
396 {
397 gpir_node_foreach_pred(parent, dep) {
398 if (dep->pred == child) {
399 gpir_node_replace_pred(dep, insert_child);
400 break;
401 }
402 }
403 gpir_node_add_dep(insert_child, child, GPIR_DEP_INPUT);
404 }
405
406 void gpir_node_delete(gpir_node *node)
407 {
408 gpir_node_foreach_succ_safe(node, dep) {
409 list_del(&dep->succ_link);
410 list_del(&dep->pred_link);
411 ralloc_free(dep);
412 }
413
414 gpir_node_foreach_pred_safe(node, dep) {
415 list_del(&dep->succ_link);
416 list_del(&dep->pred_link);
417 ralloc_free(dep);
418 }
419
420 if (node->type == gpir_node_type_store) {
421 gpir_store_node *store = gpir_node_to_store(node);
422 if (store->reg)
423 list_del(&store->reg_link);
424 }
425 else if (node->type == gpir_node_type_load) {
426 gpir_load_node *load = gpir_node_to_load(node);
427 if (load->reg)
428 list_del(&load->reg_link);
429 }
430
431 list_del(&node->list);
432 ralloc_free(node);
433 }
434
435 static void gpir_node_print_node(gpir_node *node, int type, int space)
436 {
437 static char *dep_name[] = {
438 [GPIR_DEP_INPUT] = "input",
439 [GPIR_DEP_OFFSET] = "offset",
440 [GPIR_DEP_READ_AFTER_WRITE] = "RaW",
441 [GPIR_DEP_WRITE_AFTER_READ] = "WaR",
442 };
443
444 for (int i = 0; i < space; i++)
445 printf(" ");
446 printf("%s%s %d %s %s\n", node->printed && !gpir_node_is_leaf(node) ? "+" : "",
447 gpir_op_infos[node->op].name, node->index, node->name, dep_name[type]);
448
449 if (!node->printed) {
450 gpir_node_foreach_pred(node, dep) {
451 gpir_node_print_node(dep->pred, dep->type, space + 2);
452 }
453
454 node->printed = true;
455 }
456 }
457
458 void gpir_node_print_prog_dep(gpir_compiler *comp)
459 {
460 if (!(lima_debug & LIMA_DEBUG_GP))
461 return;
462
463 list_for_each_entry(gpir_block, block, &comp->block_list, list) {
464 list_for_each_entry(gpir_node, node, &block->node_list, list) {
465 node->printed = false;
466 }
467 }
468
469 printf("======== node prog dep ========\n");
470 list_for_each_entry(gpir_block, block, &comp->block_list, list) {
471 list_for_each_entry(gpir_node, node, &block->node_list, list) {
472 if (gpir_node_is_root(node))
473 gpir_node_print_node(node, GPIR_DEP_INPUT, 0);
474 }
475 printf("----------------------------\n");
476 }
477 }
478
479 void gpir_node_print_prog_seq(gpir_compiler *comp)
480 {
481 if (!(lima_debug & LIMA_DEBUG_GP))
482 return;
483
484 int index = 0;
485 printf("======== node prog seq ========\n");
486 list_for_each_entry(gpir_block, block, &comp->block_list, list) {
487 list_for_each_entry(gpir_node, node, &block->node_list, list) {
488 printf("%03d: %s %d %s pred", index++, gpir_op_infos[node->op].name,
489 node->index, node->name);
490 gpir_node_foreach_pred(node, dep) {
491 printf(" %d", dep->pred->index);
492 }
493 printf(" succ");
494 gpir_node_foreach_succ(node, dep) {
495 printf(" %d", dep->succ->index);
496 }
497 printf("\n");
498 }
499 printf("----------------------------\n");
500 }
501 }