pan/bi: Remove bi_load
[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 void
229 emit_instr(bi_context *ctx, struct nir_instr *instr)
230 {
231 switch (instr->type) {
232 case nir_instr_type_load_const:
233 emit_load_const(ctx, nir_instr_as_load_const(instr));
234 break;
235
236 case nir_instr_type_intrinsic:
237 emit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
238 break;
239
240 #if 0
241 case nir_instr_type_alu:
242 emit_alu(ctx, nir_instr_as_alu(instr));
243 break;
244
245 case nir_instr_type_tex:
246 emit_tex(ctx, nir_instr_as_tex(instr));
247 break;
248 #endif
249
250 case nir_instr_type_jump:
251 emit_jump(ctx, nir_instr_as_jump(instr));
252 break;
253
254 case nir_instr_type_ssa_undef:
255 /* Spurious */
256 break;
257
258 default:
259 //unreachable("Unhandled instruction type");
260 break;
261 }
262 }
263
264
265
266 static bi_block *
267 create_empty_block(bi_context *ctx)
268 {
269 bi_block *blk = rzalloc(ctx, bi_block);
270
271 blk->predecessors = _mesa_set_create(blk,
272 _mesa_hash_pointer,
273 _mesa_key_pointer_equal);
274
275 blk->name = ctx->block_name_count++;
276
277 return blk;
278 }
279
280 static void
281 bi_block_add_successor(bi_block *block, bi_block *successor)
282 {
283 assert(block);
284 assert(successor);
285
286 for (unsigned i = 0; i < ARRAY_SIZE(block->successors); ++i) {
287 if (block->successors[i]) {
288 if (block->successors[i] == successor)
289 return;
290 else
291 continue;
292 }
293
294 block->successors[i] = successor;
295 _mesa_set_add(successor->predecessors, block);
296 return;
297 }
298
299 unreachable("Too many successors");
300 }
301
302 static void
303 bi_schedule_barrier(bi_context *ctx)
304 {
305 bi_block *temp = ctx->after_block;
306 ctx->after_block = create_empty_block(ctx);
307 list_addtail(&ctx->after_block->link, &ctx->blocks);
308 list_inithead(&ctx->after_block->instructions);
309 bi_block_add_successor(ctx->current_block, ctx->after_block);
310 ctx->current_block = ctx->after_block;
311 ctx->after_block = temp;
312 }
313
314 static bi_block *
315 emit_block(bi_context *ctx, nir_block *block)
316 {
317 if (ctx->after_block) {
318 ctx->current_block = ctx->after_block;
319 ctx->after_block = NULL;
320 } else {
321 ctx->current_block = create_empty_block(ctx);
322 }
323
324 list_addtail(&ctx->current_block->link, &ctx->blocks);
325 list_inithead(&ctx->current_block->instructions);
326
327 nir_foreach_instr(instr, block) {
328 emit_instr(ctx, instr);
329 ++ctx->instruction_count;
330 }
331
332 return ctx->current_block;
333 }
334
335 /* Emits an unconditional branch to the end of the current block, returning a
336 * pointer so the user can fill in details */
337
338 static bi_instruction *
339 bi_emit_branch(bi_context *ctx)
340 {
341 bi_instruction branch = {
342 .type = BI_BRANCH,
343 .branch = {
344 .cond = BI_COND_ALWAYS
345 }
346 };
347
348 return bi_emit(ctx, branch);
349 }
350
351 /* Sets a condition for a branch by examing the NIR condition. If we're
352 * familiar with the condition, we unwrap it to fold it into the branch
353 * instruction. Otherwise, we consume the condition directly. We
354 * generally use 1-bit booleans which allows us to use small types for
355 * the conditions.
356 */
357
358 static void
359 bi_set_branch_cond(bi_instruction *branch, nir_src *cond, bool invert)
360 {
361 /* TODO: Try to unwrap instead of always bailing */
362 branch->src[0] = bir_src_index(cond);
363 branch->src[1] = BIR_INDEX_ZERO;
364 branch->src_types[0] = branch->src_types[1] = nir_type_uint16;
365 branch->branch.cond = invert ? BI_COND_EQ : BI_COND_NE;
366 }
367
368 static void
369 emit_if(bi_context *ctx, nir_if *nif)
370 {
371 bi_block *before_block = ctx->current_block;
372
373 /* Speculatively emit the branch, but we can't fill it in until later */
374 bi_instruction *then_branch = bi_emit_branch(ctx);
375 bi_set_branch_cond(then_branch, &nif->condition, true);
376
377 /* Emit the two subblocks. */
378 bi_block *then_block = emit_cf_list(ctx, &nif->then_list);
379 bi_block *end_then_block = ctx->current_block;
380
381 /* Emit a jump from the end of the then block to the end of the else */
382 bi_instruction *then_exit = bi_emit_branch(ctx);
383
384 /* Emit second block, and check if it's empty */
385
386 int count_in = ctx->instruction_count;
387 bi_block *else_block = emit_cf_list(ctx, &nif->else_list);
388 bi_block *end_else_block = ctx->current_block;
389 ctx->after_block = create_empty_block(ctx);
390
391 /* Now that we have the subblocks emitted, fix up the branches */
392
393 assert(then_block);
394 assert(else_block);
395
396 if (ctx->instruction_count == count_in) {
397 /* The else block is empty, so don't emit an exit jump */
398 bi_remove_instruction(then_exit);
399 then_branch->branch.target = ctx->after_block;
400 } else {
401 then_branch->branch.target = else_block;
402 then_exit->branch.target = ctx->after_block;
403 bi_block_add_successor(end_then_block, then_exit->branch.target);
404 }
405
406 /* Wire up the successors */
407
408 bi_block_add_successor(before_block, then_branch->branch.target); /* then_branch */
409
410 bi_block_add_successor(before_block, then_block); /* fallthrough */
411 bi_block_add_successor(end_else_block, ctx->after_block); /* fallthrough */
412 }
413
414 static void
415 emit_loop(bi_context *ctx, nir_loop *nloop)
416 {
417 /* Remember where we are */
418 bi_block *start_block = ctx->current_block;
419
420 bi_block *saved_break = ctx->break_block;
421 bi_block *saved_continue = ctx->continue_block;
422
423 ctx->continue_block = create_empty_block(ctx);
424 ctx->break_block = create_empty_block(ctx);
425 ctx->after_block = ctx->continue_block;
426
427 /* Emit the body itself */
428 emit_cf_list(ctx, &nloop->body);
429
430 /* Branch back to loop back */
431 bi_instruction *br_back = bi_emit_branch(ctx);
432 br_back->branch.target = ctx->continue_block;
433 bi_block_add_successor(start_block, ctx->continue_block);
434 bi_block_add_successor(ctx->current_block, ctx->continue_block);
435
436 ctx->after_block = ctx->break_block;
437
438 /* Pop off */
439 ctx->break_block = saved_break;
440 ctx->continue_block = saved_continue;
441 ++ctx->loop_count;
442 }
443
444 static bi_block *
445 emit_cf_list(bi_context *ctx, struct exec_list *list)
446 {
447 bi_block *start_block = NULL;
448
449 foreach_list_typed(nir_cf_node, node, node, list) {
450 switch (node->type) {
451 case nir_cf_node_block: {
452 bi_block *block = emit_block(ctx, nir_cf_node_as_block(node));
453
454 if (!start_block)
455 start_block = block;
456
457 break;
458 }
459
460 case nir_cf_node_if:
461 emit_if(ctx, nir_cf_node_as_if(node));
462 break;
463
464 case nir_cf_node_loop:
465 emit_loop(ctx, nir_cf_node_as_loop(node));
466 break;
467
468 default:
469 unreachable("Unknown control flow");
470 }
471 }
472
473 return start_block;
474 }
475
476 static int
477 glsl_type_size(const struct glsl_type *type, bool bindless)
478 {
479 return glsl_count_attribute_slots(type, false);
480 }
481
482 static void
483 bi_optimize_nir(nir_shader *nir)
484 {
485 bool progress;
486 unsigned lower_flrp = 16 | 32 | 64;
487
488 NIR_PASS(progress, nir, nir_lower_regs_to_ssa);
489 NIR_PASS(progress, nir, nir_lower_idiv, nir_lower_idiv_fast);
490
491 nir_lower_tex_options lower_tex_options = {
492 .lower_txs_lod = true,
493 .lower_txp = ~0,
494 .lower_tex_without_implicit_lod = true,
495 .lower_txd = true,
496 };
497
498 NIR_PASS(progress, nir, nir_lower_tex, &lower_tex_options);
499 NIR_PASS(progress, nir, nir_lower_alu_to_scalar, NULL, NULL);
500 NIR_PASS(progress, nir, nir_lower_load_const_to_scalar);
501
502 do {
503 progress = false;
504
505 NIR_PASS(progress, nir, nir_lower_var_copies);
506 NIR_PASS(progress, nir, nir_lower_vars_to_ssa);
507
508 NIR_PASS(progress, nir, nir_copy_prop);
509 NIR_PASS(progress, nir, nir_opt_remove_phis);
510 NIR_PASS(progress, nir, nir_opt_dce);
511 NIR_PASS(progress, nir, nir_opt_dead_cf);
512 NIR_PASS(progress, nir, nir_opt_cse);
513 NIR_PASS(progress, nir, nir_opt_peephole_select, 64, false, true);
514 NIR_PASS(progress, nir, nir_opt_algebraic);
515 NIR_PASS(progress, nir, nir_opt_constant_folding);
516
517 if (lower_flrp != 0) {
518 bool lower_flrp_progress = false;
519 NIR_PASS(lower_flrp_progress,
520 nir,
521 nir_lower_flrp,
522 lower_flrp,
523 false /* always_precise */,
524 nir->options->lower_ffma);
525 if (lower_flrp_progress) {
526 NIR_PASS(progress, nir,
527 nir_opt_constant_folding);
528 progress = true;
529 }
530
531 /* Nothing should rematerialize any flrps, so we only
532 * need to do this lowering once.
533 */
534 lower_flrp = 0;
535 }
536
537 NIR_PASS(progress, nir, nir_opt_undef);
538 NIR_PASS(progress, nir, nir_opt_loop_unroll,
539 nir_var_shader_in |
540 nir_var_shader_out |
541 nir_var_function_temp);
542 } while (progress);
543
544 NIR_PASS(progress, nir, nir_opt_algebraic_late);
545 NIR_PASS(progress, nir, nir_lower_alu_to_scalar, NULL, NULL);
546 NIR_PASS(progress, nir, nir_lower_load_const_to_scalar);
547
548 /* Take us out of SSA */
549 NIR_PASS(progress, nir, nir_lower_locals_to_regs);
550 NIR_PASS(progress, nir, nir_convert_from_ssa, true);
551 }
552
553 void
554 bifrost_compile_shader_nir(nir_shader *nir, bifrost_program *program, unsigned product_id)
555 {
556 bi_context *ctx = rzalloc(NULL, bi_context);
557 ctx->nir = nir;
558 ctx->stage = nir->info.stage;
559 ctx->quirks = bifrost_get_quirks(product_id);
560 list_inithead(&ctx->blocks);
561
562 /* Lower gl_Position pre-optimisation, but after lowering vars to ssa
563 * (so we don't accidentally duplicate the epilogue since mesa/st has
564 * messed with our I/O quite a bit already) */
565
566 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
567
568 if (ctx->stage == MESA_SHADER_VERTEX) {
569 NIR_PASS_V(nir, nir_lower_viewport_transform);
570 NIR_PASS_V(nir, nir_lower_point_size, 1.0, 1024.0);
571 }
572
573 NIR_PASS_V(nir, nir_split_var_copies);
574 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
575 NIR_PASS_V(nir, nir_lower_var_copies);
576 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
577 NIR_PASS_V(nir, nir_lower_io, nir_var_all, glsl_type_size, 0);
578 NIR_PASS_V(nir, nir_lower_ssbo);
579
580 bi_optimize_nir(nir);
581 nir_print_shader(nir, stdout);
582
583 nir_foreach_function(func, nir) {
584 if (!func->impl)
585 continue;
586
587 ctx->impl = func->impl;
588 emit_cf_list(ctx, &func->impl->body);
589 break; /* TODO: Multi-function shaders */
590 }
591
592 bi_print_shader(ctx, stdout);
593 bi_schedule(ctx);
594
595 ralloc_free(ctx);
596 }