lima/gp: Support exp2 and log2
[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 .slots = (int []) { GPIR_INSTR_SLOT_PASS, GPIR_INSTR_SLOT_END },
145 .spillless = true,
146 .schedule_first = true,
147 },
148 [gpir_op_postlog2] = {
149 .name = "postlog2",
150 .slots = (int []) { GPIR_INSTR_SLOT_PASS, GPIR_INSTR_SLOT_END },
151 },
152 [gpir_op_exp2_impl] = {
153 .name = "exp2_impl",
154 .slots = (int []) { GPIR_INSTR_SLOT_COMPLEX, GPIR_INSTR_SLOT_END },
155 .spillless = true,
156 .schedule_first = true,
157 },
158 [gpir_op_log2_impl] = {
159 .name = "log2_impl",
160 .slots = (int []) { GPIR_INSTR_SLOT_COMPLEX, GPIR_INSTR_SLOT_END },
161 .spillless = true,
162 .schedule_first = true,
163 },
164 [gpir_op_rcp_impl] = {
165 .name = "rcp_impl",
166 .slots = (int []) { GPIR_INSTR_SLOT_COMPLEX, GPIR_INSTR_SLOT_END },
167 .spillless = true,
168 .schedule_first = true,
169 },
170 [gpir_op_rsqrt_impl] = {
171 .name = "rsqrt_impl",
172 .slots = (int []) { GPIR_INSTR_SLOT_COMPLEX, GPIR_INSTR_SLOT_END },
173 .spillless = true,
174 .schedule_first = true,
175 },
176 [gpir_op_load_uniform] = {
177 .name = "ld_uni",
178 .slots = (int []) {
179 GPIR_INSTR_SLOT_MEM_LOAD0, GPIR_INSTR_SLOT_MEM_LOAD1,
180 GPIR_INSTR_SLOT_MEM_LOAD2, GPIR_INSTR_SLOT_MEM_LOAD3,
181 GPIR_INSTR_SLOT_END
182 },
183 .type = gpir_node_type_load,
184 },
185 [gpir_op_load_temp] = {
186 .name = "ld_tmp",
187 .type = gpir_node_type_load,
188 },
189 [gpir_op_load_attribute] = {
190 .name = "ld_att",
191 .slots = (int []) {
192 GPIR_INSTR_SLOT_REG0_LOAD0, GPIR_INSTR_SLOT_REG0_LOAD1,
193 GPIR_INSTR_SLOT_REG0_LOAD2, GPIR_INSTR_SLOT_REG0_LOAD3,
194 GPIR_INSTR_SLOT_END
195 },
196 .type = gpir_node_type_load,
197 },
198 [gpir_op_load_reg] = {
199 .name = "ld_reg",
200 .slots = (int []) {
201 GPIR_INSTR_SLOT_REG1_LOAD0, GPIR_INSTR_SLOT_REG1_LOAD1,
202 GPIR_INSTR_SLOT_REG1_LOAD2, GPIR_INSTR_SLOT_REG1_LOAD3,
203 GPIR_INSTR_SLOT_REG0_LOAD0, GPIR_INSTR_SLOT_REG0_LOAD1,
204 GPIR_INSTR_SLOT_REG0_LOAD2, GPIR_INSTR_SLOT_REG0_LOAD3,
205 GPIR_INSTR_SLOT_END
206 },
207 .type = gpir_node_type_load,
208 .spillless = true,
209 },
210 [gpir_op_store_temp] = {
211 .name = "st_tmp",
212 .type = gpir_node_type_store,
213 },
214 [gpir_op_store_reg] = {
215 .name = "st_reg",
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_varying] = {
225 .name = "st_var",
226 .slots = (int []) {
227 GPIR_INSTR_SLOT_STORE0, GPIR_INSTR_SLOT_STORE1,
228 GPIR_INSTR_SLOT_STORE2, GPIR_INSTR_SLOT_STORE3,
229 GPIR_INSTR_SLOT_END
230 },
231 .type = gpir_node_type_store,
232 .spillless = true,
233 },
234 [gpir_op_store_temp_load_off0] = {
235 .name = "st_of0",
236 .type = gpir_node_type_store,
237 },
238 [gpir_op_store_temp_load_off1] = {
239 .name = "st_of1",
240 .type = gpir_node_type_store,
241 },
242 [gpir_op_store_temp_load_off2] = {
243 .name = "st_of2",
244 .type = gpir_node_type_store,
245 },
246 [gpir_op_branch_cond] = {
247 .name = "branch_cond",
248 .type = gpir_node_type_branch,
249 },
250 [gpir_op_const] = {
251 .name = "const",
252 .type = gpir_node_type_const,
253 },
254 [gpir_op_exp2] = {
255 .name = "exp2",
256 },
257 [gpir_op_log2] = {
258 .name = "log2",
259 },
260 [gpir_op_rcp] = {
261 .name = "rcp",
262 },
263 [gpir_op_rsqrt] = {
264 .name = "rsqrt",
265 },
266 [gpir_op_ceil] = {
267 .name = "ceil",
268 },
269 [gpir_op_exp] = {
270 .name = "exp",
271 },
272 [gpir_op_log] = {
273 .name = "log",
274 },
275 [gpir_op_sin] = {
276 .name = "sin",
277 },
278 [gpir_op_cos] = {
279 .name = "cos",
280 },
281 [gpir_op_tan] = {
282 .name = "tan",
283 },
284 [gpir_op_dummy_f] = {
285 .name = "dummy_f",
286 .type = gpir_node_type_alu,
287 .spillless = true,
288 },
289 [gpir_op_dummy_m] = {
290 .name = "dummy_m",
291 .type = gpir_node_type_alu,
292 },
293 [gpir_op_branch_uncond] = {
294 .name = "branch_uncond",
295 .type = gpir_node_type_branch,
296 },
297 };
298
299 void *gpir_node_create(gpir_block *block, gpir_op op)
300 {
301 static const int node_size[] = {
302 [gpir_node_type_alu] = sizeof(gpir_alu_node),
303 [gpir_node_type_const] = sizeof(gpir_const_node),
304 [gpir_node_type_load] = sizeof(gpir_load_node),
305 [gpir_node_type_store] = sizeof(gpir_store_node),
306 [gpir_node_type_branch] = sizeof(gpir_branch_node),
307 };
308
309 gpir_node_type type = gpir_op_infos[op].type;
310 int size = node_size[type];
311 gpir_node *node = rzalloc_size(block, size);
312 if (unlikely(!node))
313 return NULL;
314
315 snprintf(node->name, sizeof(node->name), "new");
316
317 list_inithead(&node->succ_list);
318 list_inithead(&node->pred_list);
319
320 node->op = op;
321 node->type = type;
322 node->index = block->comp->cur_index++;
323 node->block = block;
324
325 return node;
326 }
327
328 gpir_dep *gpir_node_add_dep(gpir_node *succ, gpir_node *pred, int type)
329 {
330 /* don't add dep for two nodes from different block */
331 if (succ->block != pred->block)
332 return NULL;
333
334 /* don't add self loop dep */
335 if (succ == pred)
336 return NULL;
337
338 /* don't add duplicated dep */
339 gpir_node_foreach_pred(succ, dep) {
340 if (dep->pred == pred) {
341 /* use stronger dependency */
342 if (dep->type > type)
343 dep->type = type;
344 return dep;
345 }
346 }
347
348 gpir_dep *dep = ralloc(succ, gpir_dep);
349 dep->type = type;
350 dep->pred = pred;
351 dep->succ = succ;
352 list_addtail(&dep->pred_link, &succ->pred_list);
353 list_addtail(&dep->succ_link, &pred->succ_list);
354 return dep;
355 }
356
357 void gpir_node_remove_dep(gpir_node *succ, gpir_node *pred)
358 {
359 gpir_node_foreach_pred(succ, dep) {
360 if (dep->pred == pred) {
361 list_del(&dep->succ_link);
362 list_del(&dep->pred_link);
363 ralloc_free(dep);
364 return;
365 }
366 }
367 }
368
369 void gpir_node_replace_child(gpir_node *parent, gpir_node *old_child,
370 gpir_node *new_child)
371 {
372 if (parent->type == gpir_node_type_alu) {
373 gpir_alu_node *alu = gpir_node_to_alu(parent);
374 for (int i = 0; i < alu->num_child; i++) {
375 if (alu->children[i] == old_child)
376 alu->children[i] = new_child;
377 }
378 }
379 else if (parent->type == gpir_node_type_store) {
380 gpir_store_node *store = gpir_node_to_store(parent);
381 if (store->child == old_child)
382 store->child = new_child;
383 }
384 }
385
386 void gpir_node_replace_pred(gpir_dep *dep, gpir_node *new_pred)
387 {
388 list_del(&dep->succ_link);
389 dep->pred = new_pred;
390 list_addtail(&dep->succ_link, &new_pred->succ_list);
391 }
392
393 void gpir_node_replace_succ(gpir_node *dst, gpir_node *src)
394 {
395 gpir_node_foreach_succ_safe(src, dep) {
396 if (dep->type != GPIR_DEP_INPUT)
397 continue;
398
399 gpir_node_replace_pred(dep, dst);
400 gpir_node_replace_child(dep->succ, src, dst);
401 }
402 }
403
404 void gpir_node_insert_child(gpir_node *parent, gpir_node *child,
405 gpir_node *insert_child)
406 {
407 gpir_node_foreach_pred(parent, dep) {
408 if (dep->pred == child) {
409 gpir_node_replace_pred(dep, insert_child);
410 break;
411 }
412 }
413 gpir_node_add_dep(insert_child, child, GPIR_DEP_INPUT);
414 }
415
416 void gpir_node_delete(gpir_node *node)
417 {
418 gpir_node_foreach_succ_safe(node, dep) {
419 list_del(&dep->succ_link);
420 list_del(&dep->pred_link);
421 ralloc_free(dep);
422 }
423
424 gpir_node_foreach_pred_safe(node, dep) {
425 list_del(&dep->succ_link);
426 list_del(&dep->pred_link);
427 ralloc_free(dep);
428 }
429
430 if (node->type == gpir_node_type_store) {
431 gpir_store_node *store = gpir_node_to_store(node);
432 if (store->reg)
433 list_del(&store->reg_link);
434 }
435 else if (node->type == gpir_node_type_load) {
436 gpir_load_node *load = gpir_node_to_load(node);
437 if (load->reg)
438 list_del(&load->reg_link);
439 }
440
441 list_del(&node->list);
442 ralloc_free(node);
443 }
444
445 static void gpir_node_print_node(gpir_node *node, int type, int space)
446 {
447 static char *dep_name[] = {
448 [GPIR_DEP_INPUT] = "input",
449 [GPIR_DEP_OFFSET] = "offset",
450 [GPIR_DEP_READ_AFTER_WRITE] = "RaW",
451 [GPIR_DEP_WRITE_AFTER_READ] = "WaR",
452 };
453
454 for (int i = 0; i < space; i++)
455 printf(" ");
456 printf("%s%s %d %s %s\n", node->printed && !gpir_node_is_leaf(node) ? "+" : "",
457 gpir_op_infos[node->op].name, node->index, node->name, dep_name[type]);
458
459 if (!node->printed) {
460 gpir_node_foreach_pred(node, dep) {
461 gpir_node_print_node(dep->pred, dep->type, space + 2);
462 }
463
464 node->printed = true;
465 }
466 }
467
468 void gpir_node_print_prog_dep(gpir_compiler *comp)
469 {
470 if (!(lima_debug & LIMA_DEBUG_GP))
471 return;
472
473 list_for_each_entry(gpir_block, block, &comp->block_list, list) {
474 list_for_each_entry(gpir_node, node, &block->node_list, list) {
475 node->printed = false;
476 }
477 }
478
479 printf("======== node prog dep ========\n");
480 list_for_each_entry(gpir_block, block, &comp->block_list, list) {
481 list_for_each_entry(gpir_node, node, &block->node_list, list) {
482 if (gpir_node_is_root(node))
483 gpir_node_print_node(node, GPIR_DEP_INPUT, 0);
484 }
485 printf("----------------------------\n");
486 }
487 }
488
489 void gpir_node_print_prog_seq(gpir_compiler *comp)
490 {
491 if (!(lima_debug & LIMA_DEBUG_GP))
492 return;
493
494 int index = 0;
495 printf("======== node prog seq ========\n");
496 list_for_each_entry(gpir_block, block, &comp->block_list, list) {
497 list_for_each_entry(gpir_node, node, &block->node_list, list) {
498 printf("%03d: %s %d %s pred", index++, gpir_op_infos[node->op].name,
499 node->index, node->name);
500 gpir_node_foreach_pred(node, dep) {
501 printf(" %d", dep->pred->index);
502 }
503 printf(" succ");
504 gpir_node_foreach_succ(node, dep) {
505 printf(" %d", dep->succ->index);
506 }
507 printf("\n");
508 }
509 printf("----------------------------\n");
510 }
511 }