lima/ir: print names of unsupported intrinsics
[mesa.git] / src / gallium / drivers / lima / ir / pp / nir.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 <string.h>
26
27 #include "util/ralloc.h"
28 #include "util/bitscan.h"
29 #include "compiler/nir/nir.h"
30
31 #include "ppir.h"
32
33 static void *ppir_node_create_ssa(ppir_block *block, ppir_op op, nir_ssa_def *ssa)
34 {
35 ppir_node *node = ppir_node_create(block, op, ssa->index, 0);
36 if (!node)
37 return NULL;
38
39 ppir_dest *dest = ppir_node_get_dest(node);
40 dest->type = ppir_target_ssa;
41 dest->ssa.num_components = ssa->num_components;
42 dest->ssa.live_in = INT_MAX;
43 dest->ssa.live_out = 0;
44 dest->write_mask = u_bit_consecutive(0, ssa->num_components);
45
46 if (node->type == ppir_node_type_load ||
47 node->type == ppir_node_type_store)
48 dest->ssa.is_head = true;
49
50 return node;
51 }
52
53 static void *ppir_node_create_reg(ppir_block *block, ppir_op op,
54 nir_reg_dest *reg, unsigned mask)
55 {
56 ppir_node *node = ppir_node_create(block, op, reg->reg->index, mask);
57 if (!node)
58 return NULL;
59
60 ppir_dest *dest = ppir_node_get_dest(node);
61
62 list_for_each_entry(ppir_reg, r, &block->comp->reg_list, list) {
63 if (r->index == reg->reg->index) {
64 dest->reg = r;
65 break;
66 }
67 }
68
69 dest->type = ppir_target_register;
70 dest->write_mask = mask;
71
72 if (node->type == ppir_node_type_load ||
73 node->type == ppir_node_type_store)
74 dest->reg->is_head = true;
75
76 return node;
77 }
78
79 static void *ppir_node_create_dest(ppir_block *block, ppir_op op,
80 nir_dest *dest, unsigned mask)
81 {
82 unsigned index = -1;
83
84 if (dest) {
85 if (dest->is_ssa)
86 return ppir_node_create_ssa(block, op, &dest->ssa);
87 else
88 return ppir_node_create_reg(block, op, &dest->reg, mask);
89 }
90
91 return ppir_node_create(block, op, index, 0);
92 }
93
94 static void ppir_node_add_src(ppir_compiler *comp, ppir_node *node,
95 ppir_src *ps, nir_src *ns, unsigned mask)
96 {
97 ppir_node *child = NULL;
98
99 if (ns->is_ssa) {
100 child = comp->var_nodes[ns->ssa->index];
101 ppir_node_add_dep(node, child);
102 }
103 else {
104 nir_register *reg = ns->reg.reg;
105 while (mask) {
106 int swizzle = ps->swizzle[u_bit_scan(&mask)];
107 child = comp->var_nodes[(reg->index << 2) + comp->reg_base + swizzle];
108 ppir_node_add_dep(node, child);
109 }
110 }
111
112 ppir_dest *dest = ppir_node_get_dest(child);
113 ppir_node_target_assign(ps, dest);
114 }
115
116 static int nir_to_ppir_opcodes[nir_num_opcodes] = {
117 /* not supported */
118 [0 ... nir_last_opcode] = -1,
119
120 [nir_op_fmov] = ppir_op_mov,
121 [nir_op_imov] = ppir_op_mov,
122 [nir_op_fmul] = ppir_op_mul,
123 [nir_op_fadd] = ppir_op_add,
124 [nir_op_fdot2] = ppir_op_dot2,
125 [nir_op_fdot3] = ppir_op_dot3,
126 [nir_op_fdot4] = ppir_op_dot4,
127 [nir_op_frsq] = ppir_op_rsqrt,
128 [nir_op_flog2] = ppir_op_log2,
129 [nir_op_fexp2] = ppir_op_exp2,
130 [nir_op_fsqrt] = ppir_op_sqrt,
131 [nir_op_fsin] = ppir_op_sin,
132 [nir_op_fcos] = ppir_op_cos,
133 [nir_op_fmax] = ppir_op_max,
134 [nir_op_fmin] = ppir_op_min,
135 [nir_op_frcp] = ppir_op_rcp,
136 [nir_op_ffloor] = ppir_op_floor,
137 [nir_op_fceil] = ppir_op_ceil,
138 [nir_op_ffract] = ppir_op_fract,
139 [nir_op_fand] = ppir_op_and,
140 [nir_op_for] = ppir_op_or,
141 [nir_op_fxor] = ppir_op_xor,
142 [nir_op_sge] = ppir_op_ge,
143 [nir_op_fge] = ppir_op_ge,
144 [nir_op_slt] = ppir_op_lt,
145 [nir_op_flt] = ppir_op_lt,
146 [nir_op_seq] = ppir_op_eq,
147 [nir_op_feq] = ppir_op_eq,
148 [nir_op_sne] = ppir_op_ne,
149 [nir_op_fne] = ppir_op_ne,
150 [nir_op_fnot] = ppir_op_not,
151 [nir_op_fcsel] = ppir_op_select,
152 [nir_op_inot] = ppir_op_not,
153 [nir_op_ftrunc] = ppir_op_trunc,
154 };
155
156 static ppir_node *ppir_emit_alu(ppir_block *block, nir_instr *ni)
157 {
158 nir_alu_instr *instr = nir_instr_as_alu(ni);
159 int op = nir_to_ppir_opcodes[instr->op];
160
161 if (op < 0) {
162 ppir_error("unsupported nir_op: %s\n", nir_op_infos[instr->op].name);
163 return NULL;
164 }
165
166 ppir_alu_node *node = ppir_node_create_dest(block, op, &instr->dest.dest,
167 instr->dest.write_mask);
168 if (!node)
169 return NULL;
170
171 ppir_dest *pd = &node->dest;
172 nir_alu_dest *nd = &instr->dest;
173 if (nd->saturate)
174 pd->modifier = ppir_outmod_clamp_fraction;
175
176 unsigned src_mask;
177 switch (op) {
178 case ppir_op_dot2:
179 src_mask = 0b0011;
180 break;
181 case ppir_op_dot3:
182 src_mask = 0b0111;
183 break;
184 case ppir_op_dot4:
185 src_mask = 0b1111;
186 break;
187 default:
188 src_mask = pd->write_mask;
189 break;
190 }
191
192 unsigned num_child = nir_op_infos[instr->op].num_inputs;
193 node->num_src = num_child;
194
195 for (int i = 0; i < num_child; i++) {
196 nir_alu_src *ns = instr->src + i;
197 ppir_src *ps = node->src + i;
198 memcpy(ps->swizzle, ns->swizzle, sizeof(ps->swizzle));
199 ppir_node_add_src(block->comp, &node->node, ps, &ns->src, src_mask);
200
201 ps->absolute = ns->abs;
202 ps->negate = ns->negate;
203 }
204
205 return &node->node;
206 }
207
208 static ppir_node *ppir_emit_intrinsic(ppir_block *block, nir_instr *ni)
209 {
210 nir_intrinsic_instr *instr = nir_instr_as_intrinsic(ni);
211 unsigned mask = 0;
212 ppir_load_node *lnode;
213 ppir_store_node *snode;
214
215 switch (instr->intrinsic) {
216 case nir_intrinsic_load_input:
217 if (!instr->dest.is_ssa)
218 mask = u_bit_consecutive(0, instr->num_components);
219
220 lnode = ppir_node_create_dest(block, ppir_op_load_varying, &instr->dest, mask);
221 if (!lnode)
222 return NULL;
223
224 lnode->num_components = instr->num_components;
225 lnode->index = nir_intrinsic_base(instr) * 4 + nir_intrinsic_component(instr);
226 return &lnode->node;
227
228 case nir_intrinsic_load_frag_coord:
229 if (!instr->dest.is_ssa)
230 mask = u_bit_consecutive(0, instr->num_components);
231
232 lnode = ppir_node_create_dest(block, ppir_op_load_fragcoord, &instr->dest, mask);
233 if (!lnode)
234 return NULL;
235
236 lnode->num_components = instr->num_components;
237 return &lnode->node;
238
239 case nir_intrinsic_load_uniform:
240 if (!instr->dest.is_ssa)
241 mask = u_bit_consecutive(0, instr->num_components);
242
243 lnode = ppir_node_create_dest(block, ppir_op_load_uniform, &instr->dest, mask);
244 if (!lnode)
245 return NULL;
246
247 lnode->num_components = instr->num_components;
248 lnode->index = nir_intrinsic_base(instr);
249 lnode->index += (uint32_t)nir_src_as_float(instr->src[0]);
250
251 return &lnode->node;
252
253 case nir_intrinsic_store_output:
254 snode = ppir_node_create_dest(block, ppir_op_store_color, NULL, 0);
255 if (!snode)
256 return NULL;
257
258 snode->index = nir_intrinsic_base(instr);
259
260 for (int i = 0; i < instr->num_components; i++)
261 snode->src.swizzle[i] = i;
262
263 ppir_node_add_src(block->comp, &snode->node, &snode->src, instr->src,
264 u_bit_consecutive(0, instr->num_components));
265
266 return &snode->node;
267
268 default:
269 ppir_error("unsupported nir_intrinsic_instr %s\n",
270 nir_intrinsic_infos[instr->intrinsic].name);
271 return NULL;
272 }
273 }
274
275 static ppir_node *ppir_emit_load_const(ppir_block *block, nir_instr *ni)
276 {
277 nir_load_const_instr *instr = nir_instr_as_load_const(ni);
278 ppir_const_node *node = ppir_node_create_ssa(block, ppir_op_const, &instr->def);
279 if (!node)
280 return NULL;
281
282 assert(instr->def.bit_size == 32);
283
284 for (int i = 0; i < instr->def.num_components; i++)
285 node->constant.value[i].i = instr->value[i].i32;
286 node->constant.num = instr->def.num_components;
287
288 return &node->node;
289 }
290
291 static ppir_node *ppir_emit_ssa_undef(ppir_block *block, nir_instr *ni)
292 {
293 ppir_error("nir_ssa_undef_instr not support\n");
294 return NULL;
295 }
296
297 static ppir_node *ppir_emit_tex(ppir_block *block, nir_instr *ni)
298 {
299 nir_tex_instr *instr = nir_instr_as_tex(ni);
300 ppir_load_texture_node *node;
301
302 if (instr->op != nir_texop_tex) {
303 ppir_error("unsupported texop %d\n", instr->op);
304 return NULL;
305 }
306
307 node = ppir_node_create_dest(block, ppir_op_load_texture, &instr->dest, 0);
308 if (!node)
309 return NULL;
310
311 node->sampler = instr->texture_index;
312
313 switch (instr->sampler_dim) {
314 case GLSL_SAMPLER_DIM_2D:
315 case GLSL_SAMPLER_DIM_RECT:
316 case GLSL_SAMPLER_DIM_EXTERNAL:
317 break;
318 default:
319 ppir_debug("unsupported sampler dim: %d\n", instr->sampler_dim);
320 return NULL;
321 }
322
323 node->sampler_dim = instr->sampler_dim;
324
325 for (int i = 0; i < instr->coord_components; i++)
326 node->src_coords.swizzle[i] = i;
327
328 assert(instr->num_srcs == 1);
329 for (int i = 0; i < instr->num_srcs; i++) {
330 switch (instr->src[i].src_type) {
331 case nir_tex_src_coord:
332 ppir_node_add_src(block->comp, &node->node, &node->src_coords, &instr->src[i].src,
333 u_bit_consecutive(0, instr->coord_components));
334 break;
335 default:
336 ppir_debug("unknown texture source");
337 return NULL;
338 }
339 }
340
341 return &node->node;
342 }
343
344 static ppir_node *ppir_emit_jump(ppir_block *block, nir_instr *ni)
345 {
346 ppir_error("nir_jump_instr not support\n");
347 return NULL;
348 }
349
350 static ppir_node *(*ppir_emit_instr[nir_instr_type_phi])(ppir_block *, nir_instr *) = {
351 [nir_instr_type_alu] = ppir_emit_alu,
352 [nir_instr_type_intrinsic] = ppir_emit_intrinsic,
353 [nir_instr_type_load_const] = ppir_emit_load_const,
354 [nir_instr_type_ssa_undef] = ppir_emit_ssa_undef,
355 [nir_instr_type_tex] = ppir_emit_tex,
356 [nir_instr_type_jump] = ppir_emit_jump,
357 };
358
359 static ppir_block *ppir_block_create(ppir_compiler *comp)
360 {
361 ppir_block *block = rzalloc(comp, ppir_block);
362 if (!block)
363 return NULL;
364
365 list_inithead(&block->node_list);
366 list_inithead(&block->instr_list);
367
368 return block;
369 }
370
371 static bool ppir_emit_block(ppir_compiler *comp, nir_block *nblock)
372 {
373 ppir_block *block = ppir_block_create(comp);
374 if (!block)
375 return false;
376
377 list_addtail(&block->list, &comp->block_list);
378 block->comp = comp;
379
380 nir_foreach_instr(instr, nblock) {
381 assert(instr->type < nir_instr_type_phi);
382 ppir_node *node = ppir_emit_instr[instr->type](block, instr);
383 if (node)
384 list_addtail(&node->list, &block->node_list);
385 }
386
387 return true;
388 }
389
390 static bool ppir_emit_if(ppir_compiler *comp, nir_if *nif)
391 {
392 ppir_error("if nir_cf_node not support\n");
393 return false;
394 }
395
396 static bool ppir_emit_loop(ppir_compiler *comp, nir_loop *nloop)
397 {
398 ppir_error("loop nir_cf_node not support\n");
399 return false;
400 }
401
402 static bool ppir_emit_function(ppir_compiler *comp, nir_function_impl *nfunc)
403 {
404 ppir_error("function nir_cf_node not support\n");
405 return false;
406 }
407
408 static bool ppir_emit_cf_list(ppir_compiler *comp, struct exec_list *list)
409 {
410 foreach_list_typed(nir_cf_node, node, node, list) {
411 bool ret;
412
413 switch (node->type) {
414 case nir_cf_node_block:
415 ret = ppir_emit_block(comp, nir_cf_node_as_block(node));
416 break;
417 case nir_cf_node_if:
418 ret = ppir_emit_if(comp, nir_cf_node_as_if(node));
419 break;
420 case nir_cf_node_loop:
421 ret = ppir_emit_loop(comp, nir_cf_node_as_loop(node));
422 break;
423 case nir_cf_node_function:
424 ret = ppir_emit_function(comp, nir_cf_node_as_function(node));
425 break;
426 default:
427 ppir_error("unknown NIR node type %d\n", node->type);
428 return false;
429 }
430
431 if (!ret)
432 return false;
433 }
434
435 return true;
436 }
437
438 static ppir_compiler *ppir_compiler_create(void *prog, unsigned num_reg, unsigned num_ssa)
439 {
440 ppir_compiler *comp = rzalloc_size(
441 prog, sizeof(*comp) + ((num_reg << 2) + num_ssa) * sizeof(ppir_node *));
442 if (!comp)
443 return NULL;
444
445 list_inithead(&comp->block_list);
446 list_inithead(&comp->reg_list);
447
448 comp->var_nodes = (ppir_node **)(comp + 1);
449 comp->reg_base = num_ssa;
450 comp->prog = prog;
451 return comp;
452 }
453
454 bool ppir_compile_nir(struct lima_fs_shader_state *prog, struct nir_shader *nir,
455 struct ra_regs *ra)
456 {
457 nir_function_impl *func = nir_shader_get_entrypoint(nir);
458 ppir_compiler *comp = ppir_compiler_create(prog, func->reg_alloc, func->ssa_alloc);
459 if (!comp)
460 return false;
461
462 comp->ra = ra;
463
464 foreach_list_typed(nir_register, reg, node, &func->registers) {
465 ppir_reg *r = rzalloc(comp, ppir_reg);
466 if (!r)
467 return false;
468
469 r->index = reg->index;
470 r->num_components = reg->num_components;
471 r->live_in = INT_MAX;
472 r->live_out = 0;
473 r->is_head = false;
474 list_addtail(&r->list, &comp->reg_list);
475 }
476
477 if (!ppir_emit_cf_list(comp, &func->body))
478 goto err_out0;
479 ppir_node_print_prog(comp);
480
481 if (!ppir_lower_prog(comp))
482 goto err_out0;
483
484 if (!ppir_node_to_instr(comp))
485 goto err_out0;
486
487 if (!ppir_schedule_prog(comp))
488 goto err_out0;
489
490 if (!ppir_regalloc_prog(comp))
491 goto err_out0;
492
493 if (!ppir_codegen_prog(comp))
494 goto err_out0;
495
496 ralloc_free(comp);
497 return true;
498
499 err_out0:
500 ralloc_free(comp);
501 return false;
502 }
503