pan/bi: Allow inlining constants
[mesa.git] / src / panfrost / bifrost / bifrost_compile.c
1 /*
2 * Copyright (C) 2020 Collabora Ltd.
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 (Collabora):
24 * Alyssa Rosenzweig <alyssa.rosenzweig@collabora.com>
25 */
26
27 #include "main/mtypes.h"
28 #include "compiler/glsl/glsl_to_nir.h"
29 #include "compiler/nir_types.h"
30 #include "main/imports.h"
31 #include "compiler/nir/nir_builder.h"
32
33 #include "disassemble.h"
34 #include "bifrost_compile.h"
35 #include "compiler.h"
36 #include "bi_quirks.h"
37 #include "bi_print.h"
38
39 static bi_block *emit_cf_list(bi_context *ctx, struct exec_list *list);
40 static bi_instruction *bi_emit_branch(bi_context *ctx);
41 static void bi_block_add_successor(bi_block *block, bi_block *successor);
42 static void bi_schedule_barrier(bi_context *ctx);
43
44 static void
45 emit_jump(bi_context *ctx, nir_jump_instr *instr)
46 {
47 bi_instruction *branch = bi_emit_branch(ctx);
48
49 switch (instr->type) {
50 case nir_jump_break:
51 branch->branch.target = ctx->break_block;
52 break;
53 case nir_jump_continue:
54 branch->branch.target = ctx->continue_block;
55 break;
56 default:
57 unreachable("Unhandled jump type");
58 }
59
60 bi_block_add_successor(ctx->current_block, branch->branch.target);
61 }
62
63 /* Gets a bytemask for a complete vecN write */
64 static unsigned
65 bi_mask_for_channels_32(unsigned i)
66 {
67 return (1 << (4 * i)) - 1;
68 }
69
70 static bi_instruction
71 bi_load(enum bi_class T, nir_intrinsic_instr *instr)
72 {
73 bi_instruction load = {
74 .type = T,
75 .writemask = bi_mask_for_channels_32(instr->num_components),
76 .src = { BIR_INDEX_CONSTANT },
77 .constant = { .u64 = nir_intrinsic_base(instr) },
78 };
79
80 const nir_intrinsic_info *info = &nir_intrinsic_infos[instr->intrinsic];
81
82 if (info->has_dest)
83 load.dest = bir_dest_index(&instr->dest);
84
85 if (info->has_dest && info->index_map[NIR_INTRINSIC_TYPE] > 0)
86 load.dest_type = nir_intrinsic_type(instr);
87
88 nir_src *offset = nir_get_io_offset_src(instr);
89
90 if (nir_src_is_const(*offset))
91 load.constant.u64 += nir_src_as_uint(*offset);
92 else
93 load.src[0] = bir_src_index(offset);
94
95 return load;
96 }
97
98 static void
99 bi_emit_ld_vary(bi_context *ctx, nir_intrinsic_instr *instr)
100 {
101 bi_instruction ins = bi_load(BI_LOAD_VAR, instr);
102 ins.load_vary.interp_mode = BIFROST_INTERP_DEFAULT; /* TODO */
103 ins.load_vary.reuse = false; /* TODO */
104 ins.load_vary.flat = instr->intrinsic != nir_intrinsic_load_interpolated_input;
105 ins.dest_type = nir_type_float | nir_dest_bit_size(instr->dest),
106 bi_emit(ctx, ins);
107 }
108
109 static void
110 bi_emit_frag_out(bi_context *ctx, nir_intrinsic_instr *instr)
111 {
112 if (!ctx->emitted_atest) {
113 bi_instruction ins = {
114 .type = BI_ATEST
115 };
116
117 bi_emit(ctx, ins);
118 bi_schedule_barrier(ctx);
119 ctx->emitted_atest = true;
120 }
121
122 bi_instruction blend = {
123 .type = BI_BLEND,
124 .blend_location = nir_intrinsic_base(instr),
125 .src = {
126 bir_src_index(&instr->src[0])
127 },
128 .swizzle = {
129 { 0, 1, 2, 3 }
130 }
131 };
132
133 bi_emit(ctx, blend);
134 bi_schedule_barrier(ctx);
135 }
136
137 static void
138 bi_emit_st_vary(bi_context *ctx, nir_intrinsic_instr *instr)
139 {
140 bi_instruction address = bi_load(BI_LOAD_VAR_ADDRESS, instr);
141 address.dest = bi_make_temp(ctx);
142 address.dest_type = nir_type_uint64;
143 address.writemask = (1 << 8) - 1;
144
145 bi_instruction st = {
146 .type = BI_STORE_VAR,
147 .src = {
148 address.dest,
149 bir_src_index(&instr->src[0])
150 },
151 .swizzle = {
152 { 0, 1, 2, 3 }
153 }
154 };
155
156 bi_emit(ctx, address);
157 bi_emit(ctx, st);
158 }
159
160 static void
161 bi_emit_ld_uniform(bi_context *ctx, nir_intrinsic_instr *instr)
162 {
163 bi_instruction ld = bi_load(BI_LOAD_UNIFORM, instr);
164 ld.src[1] = BIR_INDEX_ZERO; /* TODO: UBO index */
165 bi_emit(ctx, ld);
166 }
167
168 static void
169 emit_intrinsic(bi_context *ctx, nir_intrinsic_instr *instr)
170 {
171
172 switch (instr->intrinsic) {
173 case nir_intrinsic_load_barycentric_pixel:
174 /* stub */
175 break;
176 case nir_intrinsic_load_interpolated_input:
177 case nir_intrinsic_load_input:
178 if (ctx->stage == MESA_SHADER_FRAGMENT)
179 bi_emit_ld_vary(ctx, instr);
180 else if (ctx->stage == MESA_SHADER_VERTEX)
181 bi_emit(ctx, bi_load(BI_LOAD_ATTR, instr));
182 else {
183 unreachable("Unsupported shader stage");
184 }
185 break;
186
187 case nir_intrinsic_store_output:
188 if (ctx->stage == MESA_SHADER_FRAGMENT)
189 bi_emit_frag_out(ctx, instr);
190 else if (ctx->stage == MESA_SHADER_VERTEX)
191 bi_emit_st_vary(ctx, instr);
192 else
193 unreachable("Unsupported shader stage");
194 break;
195
196 case nir_intrinsic_load_uniform:
197 bi_emit_ld_uniform(ctx, instr);
198 break;
199
200 default:
201 /* todo */
202 break;
203 }
204 }
205
206 static void
207 emit_load_const(bi_context *ctx, nir_load_const_instr *instr)
208 {
209 /* Make sure we've been lowered */
210 assert(instr->def.num_components == 1);
211
212 bi_instruction move = {
213 .type = BI_MOV,
214 .dest = bir_ssa_index(&instr->def),
215 .dest_type = instr->def.bit_size | nir_type_uint,
216 .writemask = (1 << (instr->def.bit_size / 8)) - 1,
217 .src = {
218 BIR_INDEX_CONSTANT
219 },
220 .constant = {
221 .u64 = nir_const_value_as_uint(instr->value[0], instr->def.bit_size)
222 }
223 };
224
225 bi_emit(ctx, move);
226 }
227
228 static enum bi_class
229 bi_class_for_nir_alu(nir_op op)
230 {
231 switch (op) {
232 case nir_op_fadd: return BI_ADD;
233 case nir_op_fmul: return BI_FMA;
234 case nir_op_mov: return BI_MOV;
235 default: unreachable("Unknown ALU op");
236 }
237 }
238
239 static void
240 emit_alu(bi_context *ctx, nir_alu_instr *instr)
241 {
242 /* Assume it's something we can handle normally */
243 bi_instruction alu = {
244 .type = bi_class_for_nir_alu(instr->op),
245 .dest = bir_dest_index(&instr->dest.dest),
246 .dest_type = nir_op_infos[instr->op].output_type
247 | nir_dest_bit_size(instr->dest.dest),
248 };
249
250 if (instr->dest.dest.is_ssa) {
251 /* Construct a writemask */
252 unsigned bits_per_comp = instr->dest.dest.ssa.bit_size;
253 unsigned comps = instr->dest.dest.ssa.num_components;
254 assert(comps == 1);
255 unsigned bits = bits_per_comp * comps;
256 unsigned bytes = MAX2(bits / 8, 1);
257 alu.writemask = (1 << bytes) - 1;
258 } else {
259 unsigned comp_mask = instr->dest.write_mask;
260
261 alu.writemask = pan_to_bytemask(nir_dest_bit_size(instr->dest.dest),
262 comp_mask);
263 }
264
265 /* We inline constants as we go. This tracks how many constants have
266 * been inlined, since we're limited to 64-bits of constants per
267 * instruction */
268
269 unsigned dest_bits = nir_dest_bit_size(instr->dest.dest);
270 unsigned constants_left = (64 / dest_bits);
271 unsigned constant_shift = 0;
272
273 /* Copy sources */
274
275 unsigned num_inputs = nir_op_infos[instr->op].num_inputs;
276 assert(num_inputs <= ARRAY_SIZE(alu.src));
277
278 for (unsigned i = 0; i < num_inputs; ++i) {
279 unsigned bits = nir_src_bit_size(instr->src[i].src);
280 alu.src_types[i] = nir_op_infos[instr->op].input_types[i]
281 | bits;
282
283 /* Try to inline a constant */
284 if (nir_src_is_const(instr->src[i].src) && constants_left && (dest_bits == bits)) {
285 alu.constant.u64 |=
286 (nir_src_as_uint(instr->src[i].src)) << constant_shift;
287
288 alu.src[i] = BIR_INDEX_CONSTANT | constant_shift;
289 --constants_left;
290 constant_shift += dest_bits;
291 continue;
292 }
293
294 alu.src[i] = bir_src_index(&instr->src[i].src);
295
296 /* We assert scalarization above */
297 alu.swizzle[i][0] = instr->src[i].swizzle[0];
298 }
299
300 /* Op-specific fixup */
301 switch (instr->op) {
302 case nir_op_fmul:
303 alu.src[2] = BIR_INDEX_ZERO; /* FMA */
304 break;
305 default:
306 break;
307 }
308
309 bi_emit(ctx, alu);
310 }
311
312 static void
313 emit_instr(bi_context *ctx, struct nir_instr *instr)
314 {
315 switch (instr->type) {
316 case nir_instr_type_load_const:
317 emit_load_const(ctx, nir_instr_as_load_const(instr));
318 break;
319
320 case nir_instr_type_intrinsic:
321 emit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
322 break;
323
324 case nir_instr_type_alu:
325 emit_alu(ctx, nir_instr_as_alu(instr));
326 break;
327
328 #if 0
329 case nir_instr_type_tex:
330 emit_tex(ctx, nir_instr_as_tex(instr));
331 break;
332 #endif
333
334 case nir_instr_type_jump:
335 emit_jump(ctx, nir_instr_as_jump(instr));
336 break;
337
338 case nir_instr_type_ssa_undef:
339 /* Spurious */
340 break;
341
342 default:
343 //unreachable("Unhandled instruction type");
344 break;
345 }
346 }
347
348
349
350 static bi_block *
351 create_empty_block(bi_context *ctx)
352 {
353 bi_block *blk = rzalloc(ctx, bi_block);
354
355 blk->predecessors = _mesa_set_create(blk,
356 _mesa_hash_pointer,
357 _mesa_key_pointer_equal);
358
359 blk->name = ctx->block_name_count++;
360
361 return blk;
362 }
363
364 static void
365 bi_block_add_successor(bi_block *block, bi_block *successor)
366 {
367 assert(block);
368 assert(successor);
369
370 for (unsigned i = 0; i < ARRAY_SIZE(block->successors); ++i) {
371 if (block->successors[i]) {
372 if (block->successors[i] == successor)
373 return;
374 else
375 continue;
376 }
377
378 block->successors[i] = successor;
379 _mesa_set_add(successor->predecessors, block);
380 return;
381 }
382
383 unreachable("Too many successors");
384 }
385
386 static void
387 bi_schedule_barrier(bi_context *ctx)
388 {
389 bi_block *temp = ctx->after_block;
390 ctx->after_block = create_empty_block(ctx);
391 list_addtail(&ctx->after_block->link, &ctx->blocks);
392 list_inithead(&ctx->after_block->instructions);
393 bi_block_add_successor(ctx->current_block, ctx->after_block);
394 ctx->current_block = ctx->after_block;
395 ctx->after_block = temp;
396 }
397
398 static bi_block *
399 emit_block(bi_context *ctx, nir_block *block)
400 {
401 if (ctx->after_block) {
402 ctx->current_block = ctx->after_block;
403 ctx->after_block = NULL;
404 } else {
405 ctx->current_block = create_empty_block(ctx);
406 }
407
408 list_addtail(&ctx->current_block->link, &ctx->blocks);
409 list_inithead(&ctx->current_block->instructions);
410
411 nir_foreach_instr(instr, block) {
412 emit_instr(ctx, instr);
413 ++ctx->instruction_count;
414 }
415
416 return ctx->current_block;
417 }
418
419 /* Emits an unconditional branch to the end of the current block, returning a
420 * pointer so the user can fill in details */
421
422 static bi_instruction *
423 bi_emit_branch(bi_context *ctx)
424 {
425 bi_instruction branch = {
426 .type = BI_BRANCH,
427 .branch = {
428 .cond = BI_COND_ALWAYS
429 }
430 };
431
432 return bi_emit(ctx, branch);
433 }
434
435 /* Sets a condition for a branch by examing the NIR condition. If we're
436 * familiar with the condition, we unwrap it to fold it into the branch
437 * instruction. Otherwise, we consume the condition directly. We
438 * generally use 1-bit booleans which allows us to use small types for
439 * the conditions.
440 */
441
442 static void
443 bi_set_branch_cond(bi_instruction *branch, nir_src *cond, bool invert)
444 {
445 /* TODO: Try to unwrap instead of always bailing */
446 branch->src[0] = bir_src_index(cond);
447 branch->src[1] = BIR_INDEX_ZERO;
448 branch->src_types[0] = branch->src_types[1] = nir_type_uint16;
449 branch->branch.cond = invert ? BI_COND_EQ : BI_COND_NE;
450 }
451
452 static void
453 emit_if(bi_context *ctx, nir_if *nif)
454 {
455 bi_block *before_block = ctx->current_block;
456
457 /* Speculatively emit the branch, but we can't fill it in until later */
458 bi_instruction *then_branch = bi_emit_branch(ctx);
459 bi_set_branch_cond(then_branch, &nif->condition, true);
460
461 /* Emit the two subblocks. */
462 bi_block *then_block = emit_cf_list(ctx, &nif->then_list);
463 bi_block *end_then_block = ctx->current_block;
464
465 /* Emit a jump from the end of the then block to the end of the else */
466 bi_instruction *then_exit = bi_emit_branch(ctx);
467
468 /* Emit second block, and check if it's empty */
469
470 int count_in = ctx->instruction_count;
471 bi_block *else_block = emit_cf_list(ctx, &nif->else_list);
472 bi_block *end_else_block = ctx->current_block;
473 ctx->after_block = create_empty_block(ctx);
474
475 /* Now that we have the subblocks emitted, fix up the branches */
476
477 assert(then_block);
478 assert(else_block);
479
480 if (ctx->instruction_count == count_in) {
481 /* The else block is empty, so don't emit an exit jump */
482 bi_remove_instruction(then_exit);
483 then_branch->branch.target = ctx->after_block;
484 } else {
485 then_branch->branch.target = else_block;
486 then_exit->branch.target = ctx->after_block;
487 bi_block_add_successor(end_then_block, then_exit->branch.target);
488 }
489
490 /* Wire up the successors */
491
492 bi_block_add_successor(before_block, then_branch->branch.target); /* then_branch */
493
494 bi_block_add_successor(before_block, then_block); /* fallthrough */
495 bi_block_add_successor(end_else_block, ctx->after_block); /* fallthrough */
496 }
497
498 static void
499 emit_loop(bi_context *ctx, nir_loop *nloop)
500 {
501 /* Remember where we are */
502 bi_block *start_block = ctx->current_block;
503
504 bi_block *saved_break = ctx->break_block;
505 bi_block *saved_continue = ctx->continue_block;
506
507 ctx->continue_block = create_empty_block(ctx);
508 ctx->break_block = create_empty_block(ctx);
509 ctx->after_block = ctx->continue_block;
510
511 /* Emit the body itself */
512 emit_cf_list(ctx, &nloop->body);
513
514 /* Branch back to loop back */
515 bi_instruction *br_back = bi_emit_branch(ctx);
516 br_back->branch.target = ctx->continue_block;
517 bi_block_add_successor(start_block, ctx->continue_block);
518 bi_block_add_successor(ctx->current_block, ctx->continue_block);
519
520 ctx->after_block = ctx->break_block;
521
522 /* Pop off */
523 ctx->break_block = saved_break;
524 ctx->continue_block = saved_continue;
525 ++ctx->loop_count;
526 }
527
528 static bi_block *
529 emit_cf_list(bi_context *ctx, struct exec_list *list)
530 {
531 bi_block *start_block = NULL;
532
533 foreach_list_typed(nir_cf_node, node, node, list) {
534 switch (node->type) {
535 case nir_cf_node_block: {
536 bi_block *block = emit_block(ctx, nir_cf_node_as_block(node));
537
538 if (!start_block)
539 start_block = block;
540
541 break;
542 }
543
544 case nir_cf_node_if:
545 emit_if(ctx, nir_cf_node_as_if(node));
546 break;
547
548 case nir_cf_node_loop:
549 emit_loop(ctx, nir_cf_node_as_loop(node));
550 break;
551
552 default:
553 unreachable("Unknown control flow");
554 }
555 }
556
557 return start_block;
558 }
559
560 static int
561 glsl_type_size(const struct glsl_type *type, bool bindless)
562 {
563 return glsl_count_attribute_slots(type, false);
564 }
565
566 static void
567 bi_optimize_nir(nir_shader *nir)
568 {
569 bool progress;
570 unsigned lower_flrp = 16 | 32 | 64;
571
572 NIR_PASS(progress, nir, nir_lower_regs_to_ssa);
573 NIR_PASS(progress, nir, nir_lower_idiv, nir_lower_idiv_fast);
574
575 nir_lower_tex_options lower_tex_options = {
576 .lower_txs_lod = true,
577 .lower_txp = ~0,
578 .lower_tex_without_implicit_lod = true,
579 .lower_txd = true,
580 };
581
582 NIR_PASS(progress, nir, nir_lower_tex, &lower_tex_options);
583 NIR_PASS(progress, nir, nir_lower_alu_to_scalar, NULL, NULL);
584 NIR_PASS(progress, nir, nir_lower_load_const_to_scalar);
585
586 do {
587 progress = false;
588
589 NIR_PASS(progress, nir, nir_lower_var_copies);
590 NIR_PASS(progress, nir, nir_lower_vars_to_ssa);
591
592 NIR_PASS(progress, nir, nir_copy_prop);
593 NIR_PASS(progress, nir, nir_opt_remove_phis);
594 NIR_PASS(progress, nir, nir_opt_dce);
595 NIR_PASS(progress, nir, nir_opt_dead_cf);
596 NIR_PASS(progress, nir, nir_opt_cse);
597 NIR_PASS(progress, nir, nir_opt_peephole_select, 64, false, true);
598 NIR_PASS(progress, nir, nir_opt_algebraic);
599 NIR_PASS(progress, nir, nir_opt_constant_folding);
600
601 if (lower_flrp != 0) {
602 bool lower_flrp_progress = false;
603 NIR_PASS(lower_flrp_progress,
604 nir,
605 nir_lower_flrp,
606 lower_flrp,
607 false /* always_precise */,
608 nir->options->lower_ffma);
609 if (lower_flrp_progress) {
610 NIR_PASS(progress, nir,
611 nir_opt_constant_folding);
612 progress = true;
613 }
614
615 /* Nothing should rematerialize any flrps, so we only
616 * need to do this lowering once.
617 */
618 lower_flrp = 0;
619 }
620
621 NIR_PASS(progress, nir, nir_opt_undef);
622 NIR_PASS(progress, nir, nir_opt_loop_unroll,
623 nir_var_shader_in |
624 nir_var_shader_out |
625 nir_var_function_temp);
626 } while (progress);
627
628 NIR_PASS(progress, nir, nir_opt_algebraic_late);
629 NIR_PASS(progress, nir, nir_lower_alu_to_scalar, NULL, NULL);
630 NIR_PASS(progress, nir, nir_lower_load_const_to_scalar);
631
632 /* Take us out of SSA */
633 NIR_PASS(progress, nir, nir_lower_locals_to_regs);
634 NIR_PASS(progress, nir, nir_convert_from_ssa, true);
635
636 /* We're a primary scalar architecture but there's enough vector that
637 * we use a vector IR so let's not also deal with scalar hacks on top
638 * of the vector hacks */
639
640 NIR_PASS(progress, nir, nir_move_vec_src_uses_to_dest);
641 NIR_PASS(progress, nir, nir_lower_vec_to_movs);
642 NIR_PASS(progress, nir, nir_opt_dce);
643 }
644
645 void
646 bifrost_compile_shader_nir(nir_shader *nir, bifrost_program *program, unsigned product_id)
647 {
648 bi_context *ctx = rzalloc(NULL, bi_context);
649 ctx->nir = nir;
650 ctx->stage = nir->info.stage;
651 ctx->quirks = bifrost_get_quirks(product_id);
652 list_inithead(&ctx->blocks);
653
654 /* Lower gl_Position pre-optimisation, but after lowering vars to ssa
655 * (so we don't accidentally duplicate the epilogue since mesa/st has
656 * messed with our I/O quite a bit already) */
657
658 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
659
660 if (ctx->stage == MESA_SHADER_VERTEX) {
661 NIR_PASS_V(nir, nir_lower_viewport_transform);
662 NIR_PASS_V(nir, nir_lower_point_size, 1.0, 1024.0);
663 }
664
665 NIR_PASS_V(nir, nir_split_var_copies);
666 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
667 NIR_PASS_V(nir, nir_lower_var_copies);
668 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
669 NIR_PASS_V(nir, nir_lower_io, nir_var_all, glsl_type_size, 0);
670 NIR_PASS_V(nir, nir_lower_ssbo);
671
672 bi_optimize_nir(nir);
673 nir_print_shader(nir, stdout);
674
675 nir_foreach_function(func, nir) {
676 if (!func->impl)
677 continue;
678
679 ctx->impl = func->impl;
680 emit_cf_list(ctx, &func->impl->body);
681 break; /* TODO: Multi-function shaders */
682 }
683
684 bi_print_shader(ctx, stdout);
685 bi_schedule(ctx);
686
687 ralloc_free(ctx);
688 }