cf7d15a0f9f591205cac4b9d312e8f2ddd511431
[mesa.git] / src / panfrost / midgard / midgard_compile.c
1 /*
2 * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
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
24 #include <sys/types.h>
25 #include <sys/stat.h>
26 #include <sys/mman.h>
27 #include <fcntl.h>
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <err.h>
32
33 #include "main/mtypes.h"
34 #include "compiler/glsl/glsl_to_nir.h"
35 #include "compiler/nir_types.h"
36 #include "main/imports.h"
37 #include "compiler/nir/nir_builder.h"
38 #include "util/half_float.h"
39 #include "util/u_math.h"
40 #include "util/u_debug.h"
41 #include "util/u_dynarray.h"
42 #include "util/list.h"
43 #include "main/mtypes.h"
44
45 #include "midgard.h"
46 #include "midgard_nir.h"
47 #include "midgard_compile.h"
48 #include "midgard_ops.h"
49 #include "helpers.h"
50 #include "compiler.h"
51 #include "midgard_quirks.h"
52
53 #include "disassemble.h"
54
55 static const struct debug_named_value debug_options[] = {
56 {"msgs", MIDGARD_DBG_MSGS, "Print debug messages"},
57 {"shaders", MIDGARD_DBG_SHADERS, "Dump shaders in NIR and MIR"},
58 {"shaderdb", MIDGARD_DBG_SHADERDB, "Prints shader-db statistics"},
59 DEBUG_NAMED_VALUE_END
60 };
61
62 DEBUG_GET_ONCE_FLAGS_OPTION(midgard_debug, "MIDGARD_MESA_DEBUG", debug_options, 0)
63
64 unsigned SHADER_DB_COUNT = 0;
65
66 int midgard_debug = 0;
67
68 #define DBG(fmt, ...) \
69 do { if (midgard_debug & MIDGARD_DBG_MSGS) \
70 fprintf(stderr, "%s:%d: "fmt, \
71 __FUNCTION__, __LINE__, ##__VA_ARGS__); } while (0)
72
73 static bool
74 midgard_is_branch_unit(unsigned unit)
75 {
76 return (unit == ALU_ENAB_BRANCH) || (unit == ALU_ENAB_BR_COMPACT);
77 }
78
79 static midgard_block *
80 create_empty_block(compiler_context *ctx)
81 {
82 midgard_block *blk = rzalloc(ctx, midgard_block);
83
84 blk->predecessors = _mesa_set_create(blk,
85 _mesa_hash_pointer,
86 _mesa_key_pointer_equal);
87
88 blk->source_id = ctx->block_source_count++;
89
90 return blk;
91 }
92
93 static void
94 midgard_block_add_successor(midgard_block *block, midgard_block *successor)
95 {
96 assert(block);
97 assert(successor);
98
99 /* Deduplicate */
100 for (unsigned i = 0; i < block->nr_successors; ++i) {
101 if (block->successors[i] == successor)
102 return;
103 }
104
105 block->successors[block->nr_successors++] = successor;
106 assert(block->nr_successors <= ARRAY_SIZE(block->successors));
107
108 /* Note the predecessor in the other direction */
109 _mesa_set_add(successor->predecessors, block);
110 }
111
112 static void
113 schedule_barrier(compiler_context *ctx)
114 {
115 midgard_block *temp = ctx->after_block;
116 ctx->after_block = create_empty_block(ctx);
117 ctx->block_count++;
118 list_addtail(&ctx->after_block->link, &ctx->blocks);
119 list_inithead(&ctx->after_block->instructions);
120 midgard_block_add_successor(ctx->current_block, ctx->after_block);
121 ctx->current_block = ctx->after_block;
122 ctx->after_block = temp;
123 }
124
125 /* Helpers to generate midgard_instruction's using macro magic, since every
126 * driver seems to do it that way */
127
128 #define EMIT(op, ...) emit_mir_instruction(ctx, v_##op(__VA_ARGS__));
129
130 #define M_LOAD_STORE(name, store) \
131 static midgard_instruction m_##name(unsigned ssa, unsigned address) { \
132 midgard_instruction i = { \
133 .type = TAG_LOAD_STORE_4, \
134 .mask = 0xF, \
135 .dest = ~0, \
136 .src = { ~0, ~0, ~0 }, \
137 .swizzle = SWIZZLE_IDENTITY_4, \
138 .load_store = { \
139 .op = midgard_op_##name, \
140 .address = address \
141 } \
142 }; \
143 \
144 if (store) \
145 i.src[0] = ssa; \
146 else \
147 i.dest = ssa; \
148 \
149 return i; \
150 }
151
152 #define M_LOAD(name) M_LOAD_STORE(name, false)
153 #define M_STORE(name) M_LOAD_STORE(name, true)
154
155 /* Inputs a NIR ALU source, with modifiers attached if necessary, and outputs
156 * the corresponding Midgard source */
157
158 static midgard_vector_alu_src
159 vector_alu_modifiers(nir_alu_src *src, bool is_int, unsigned broadcast_count,
160 bool half, bool sext)
161 {
162 /* Figure out how many components there are so we can adjust.
163 * Specifically we want to broadcast the last channel so things like
164 * ball2/3 work.
165 */
166
167 if (broadcast_count && src) {
168 uint8_t last_component = src->swizzle[broadcast_count - 1];
169
170 for (unsigned c = broadcast_count; c < NIR_MAX_VEC_COMPONENTS; ++c) {
171 src->swizzle[c] = last_component;
172 }
173 }
174
175 midgard_vector_alu_src alu_src = {
176 .rep_low = 0,
177 .rep_high = 0,
178 .half = half
179 };
180
181 if (is_int) {
182 alu_src.mod = midgard_int_normal;
183
184 /* Sign/zero-extend if needed */
185
186 if (half) {
187 alu_src.mod = sext ?
188 midgard_int_sign_extend
189 : midgard_int_zero_extend;
190 }
191
192 /* These should have been lowered away */
193 if (src)
194 assert(!(src->abs || src->negate));
195 } else {
196 if (src)
197 alu_src.mod = (src->abs << 0) | (src->negate << 1);
198 }
199
200 return alu_src;
201 }
202
203 /* load/store instructions have both 32-bit and 16-bit variants, depending on
204 * whether we are using vectors composed of highp or mediump. At the moment, we
205 * don't support half-floats -- this requires changes in other parts of the
206 * compiler -- therefore the 16-bit versions are commented out. */
207
208 //M_LOAD(ld_attr_16);
209 M_LOAD(ld_attr_32);
210 //M_LOAD(ld_vary_16);
211 M_LOAD(ld_vary_32);
212 M_LOAD(ld_ubo_int4);
213 M_LOAD(ld_int4);
214 M_STORE(st_int4);
215 M_LOAD(ld_color_buffer_8);
216 //M_STORE(st_vary_16);
217 M_STORE(st_vary_32);
218 M_LOAD(ld_cubemap_coords);
219 M_LOAD(ld_compute_id);
220
221 static midgard_instruction
222 v_alu_br_compact_cond(midgard_jmp_writeout_op op, unsigned tag, signed offset, unsigned cond)
223 {
224 midgard_branch_cond branch = {
225 .op = op,
226 .dest_tag = tag,
227 .offset = offset,
228 .cond = cond
229 };
230
231 uint16_t compact;
232 memcpy(&compact, &branch, sizeof(branch));
233
234 midgard_instruction ins = {
235 .type = TAG_ALU_4,
236 .unit = ALU_ENAB_BR_COMPACT,
237 .prepacked_branch = true,
238 .compact_branch = true,
239 .br_compact = compact,
240 .dest = ~0,
241 .src = { ~0, ~0, ~0 },
242 };
243
244 if (op == midgard_jmp_writeout_op_writeout)
245 ins.writeout = true;
246
247 return ins;
248 }
249
250 static midgard_instruction
251 v_branch(bool conditional, bool invert)
252 {
253 midgard_instruction ins = {
254 .type = TAG_ALU_4,
255 .unit = ALU_ENAB_BRANCH,
256 .compact_branch = true,
257 .branch = {
258 .conditional = conditional,
259 .invert_conditional = invert
260 },
261 .dest = ~0,
262 .src = { ~0, ~0, ~0 },
263 };
264
265 return ins;
266 }
267
268 static midgard_branch_extended
269 midgard_create_branch_extended( midgard_condition cond,
270 midgard_jmp_writeout_op op,
271 unsigned dest_tag,
272 signed quadword_offset)
273 {
274 /* The condition code is actually a LUT describing a function to
275 * combine multiple condition codes. However, we only support a single
276 * condition code at the moment, so we just duplicate over a bunch of
277 * times. */
278
279 uint16_t duplicated_cond =
280 (cond << 14) |
281 (cond << 12) |
282 (cond << 10) |
283 (cond << 8) |
284 (cond << 6) |
285 (cond << 4) |
286 (cond << 2) |
287 (cond << 0);
288
289 midgard_branch_extended branch = {
290 .op = op,
291 .dest_tag = dest_tag,
292 .offset = quadword_offset,
293 .cond = duplicated_cond
294 };
295
296 return branch;
297 }
298
299 static void
300 attach_constants(compiler_context *ctx, midgard_instruction *ins, void *constants, int name)
301 {
302 ins->has_constants = true;
303 memcpy(&ins->constants, constants, 16);
304 }
305
306 static int
307 glsl_type_size(const struct glsl_type *type, bool bindless)
308 {
309 return glsl_count_attribute_slots(type, false);
310 }
311
312 /* Lower fdot2 to a vector multiplication followed by channel addition */
313 static void
314 midgard_nir_lower_fdot2_body(nir_builder *b, nir_alu_instr *alu)
315 {
316 if (alu->op != nir_op_fdot2)
317 return;
318
319 b->cursor = nir_before_instr(&alu->instr);
320
321 nir_ssa_def *src0 = nir_ssa_for_alu_src(b, alu, 0);
322 nir_ssa_def *src1 = nir_ssa_for_alu_src(b, alu, 1);
323
324 nir_ssa_def *product = nir_fmul(b, src0, src1);
325
326 nir_ssa_def *sum = nir_fadd(b,
327 nir_channel(b, product, 0),
328 nir_channel(b, product, 1));
329
330 /* Replace the fdot2 with this sum */
331 nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, nir_src_for_ssa(sum));
332 }
333
334 static int
335 midgard_sysval_for_ssbo(nir_intrinsic_instr *instr)
336 {
337 /* This is way too meta */
338 bool is_store = instr->intrinsic == nir_intrinsic_store_ssbo;
339 unsigned idx_idx = is_store ? 1 : 0;
340
341 nir_src index = instr->src[idx_idx];
342 assert(nir_src_is_const(index));
343 uint32_t uindex = nir_src_as_uint(index);
344
345 return PAN_SYSVAL(SSBO, uindex);
346 }
347
348 static int
349 midgard_sysval_for_sampler(nir_intrinsic_instr *instr)
350 {
351 /* TODO: indirect samplers !!! */
352 nir_src index = instr->src[0];
353 assert(nir_src_is_const(index));
354 uint32_t uindex = nir_src_as_uint(index);
355
356 return PAN_SYSVAL(SAMPLER, uindex);
357 }
358
359 static int
360 midgard_nir_sysval_for_intrinsic(nir_intrinsic_instr *instr)
361 {
362 switch (instr->intrinsic) {
363 case nir_intrinsic_load_viewport_scale:
364 return PAN_SYSVAL_VIEWPORT_SCALE;
365 case nir_intrinsic_load_viewport_offset:
366 return PAN_SYSVAL_VIEWPORT_OFFSET;
367 case nir_intrinsic_load_num_work_groups:
368 return PAN_SYSVAL_NUM_WORK_GROUPS;
369 case nir_intrinsic_load_ssbo:
370 case nir_intrinsic_store_ssbo:
371 return midgard_sysval_for_ssbo(instr);
372 case nir_intrinsic_load_sampler_lod_parameters_pan:
373 return midgard_sysval_for_sampler(instr);
374 default:
375 return ~0;
376 }
377 }
378
379 static int sysval_for_instr(compiler_context *ctx, nir_instr *instr,
380 unsigned *dest)
381 {
382 nir_intrinsic_instr *intr;
383 nir_dest *dst = NULL;
384 nir_tex_instr *tex;
385 int sysval = -1;
386
387 bool is_store = false;
388
389 switch (instr->type) {
390 case nir_instr_type_intrinsic:
391 intr = nir_instr_as_intrinsic(instr);
392 sysval = midgard_nir_sysval_for_intrinsic(intr);
393 dst = &intr->dest;
394 is_store |= intr->intrinsic == nir_intrinsic_store_ssbo;
395 break;
396 case nir_instr_type_tex:
397 tex = nir_instr_as_tex(instr);
398 if (tex->op != nir_texop_txs)
399 break;
400
401 sysval = PAN_SYSVAL(TEXTURE_SIZE,
402 PAN_TXS_SYSVAL_ID(tex->texture_index,
403 nir_tex_instr_dest_size(tex) -
404 (tex->is_array ? 1 : 0),
405 tex->is_array));
406 dst = &tex->dest;
407 break;
408 default:
409 break;
410 }
411
412 if (dest && dst && !is_store)
413 *dest = nir_dest_index(ctx, dst);
414
415 return sysval;
416 }
417
418 static void
419 midgard_nir_assign_sysval_body(compiler_context *ctx, nir_instr *instr)
420 {
421 int sysval;
422
423 sysval = sysval_for_instr(ctx, instr, NULL);
424 if (sysval < 0)
425 return;
426
427 /* We have a sysval load; check if it's already been assigned */
428
429 if (_mesa_hash_table_u64_search(ctx->sysval_to_id, sysval))
430 return;
431
432 /* It hasn't -- so assign it now! */
433
434 unsigned id = ctx->sysval_count++;
435 _mesa_hash_table_u64_insert(ctx->sysval_to_id, sysval, (void *) ((uintptr_t) id + 1));
436 ctx->sysvals[id] = sysval;
437 }
438
439 static void
440 midgard_nir_assign_sysvals(compiler_context *ctx, nir_shader *shader)
441 {
442 ctx->sysval_count = 0;
443
444 nir_foreach_function(function, shader) {
445 if (!function->impl) continue;
446
447 nir_foreach_block(block, function->impl) {
448 nir_foreach_instr_safe(instr, block) {
449 midgard_nir_assign_sysval_body(ctx, instr);
450 }
451 }
452 }
453 }
454
455 static bool
456 midgard_nir_lower_fdot2(nir_shader *shader)
457 {
458 bool progress = false;
459
460 nir_foreach_function(function, shader) {
461 if (!function->impl) continue;
462
463 nir_builder _b;
464 nir_builder *b = &_b;
465 nir_builder_init(b, function->impl);
466
467 nir_foreach_block(block, function->impl) {
468 nir_foreach_instr_safe(instr, block) {
469 if (instr->type != nir_instr_type_alu) continue;
470
471 nir_alu_instr *alu = nir_instr_as_alu(instr);
472 midgard_nir_lower_fdot2_body(b, alu);
473
474 progress |= true;
475 }
476 }
477
478 nir_metadata_preserve(function->impl, nir_metadata_block_index | nir_metadata_dominance);
479
480 }
481
482 return progress;
483 }
484
485 /* Flushes undefined values to zero */
486
487 static void
488 optimise_nir(nir_shader *nir)
489 {
490 bool progress;
491 unsigned lower_flrp =
492 (nir->options->lower_flrp16 ? 16 : 0) |
493 (nir->options->lower_flrp32 ? 32 : 0) |
494 (nir->options->lower_flrp64 ? 64 : 0);
495
496 NIR_PASS(progress, nir, nir_lower_regs_to_ssa);
497 NIR_PASS(progress, nir, midgard_nir_lower_fdot2);
498 NIR_PASS(progress, nir, nir_lower_idiv, nir_lower_idiv_fast);
499
500 nir_lower_tex_options lower_tex_options = {
501 .lower_txs_lod = true,
502 .lower_txp = ~0
503 };
504
505 NIR_PASS(progress, nir, nir_lower_tex, &lower_tex_options);
506
507 do {
508 progress = false;
509
510 NIR_PASS(progress, nir, nir_lower_var_copies);
511 NIR_PASS(progress, nir, nir_lower_vars_to_ssa);
512
513 NIR_PASS(progress, nir, nir_copy_prop);
514 NIR_PASS(progress, nir, nir_opt_dce);
515 NIR_PASS(progress, nir, nir_opt_dead_cf);
516 NIR_PASS(progress, nir, nir_opt_cse);
517 NIR_PASS(progress, nir, nir_opt_peephole_select, 64, false, true);
518 NIR_PASS(progress, nir, nir_opt_algebraic);
519 NIR_PASS(progress, nir, nir_opt_constant_folding);
520
521 if (lower_flrp != 0) {
522 bool lower_flrp_progress = false;
523 NIR_PASS(lower_flrp_progress,
524 nir,
525 nir_lower_flrp,
526 lower_flrp,
527 false /* always_precise */,
528 nir->options->lower_ffma);
529 if (lower_flrp_progress) {
530 NIR_PASS(progress, nir,
531 nir_opt_constant_folding);
532 progress = true;
533 }
534
535 /* Nothing should rematerialize any flrps, so we only
536 * need to do this lowering once.
537 */
538 lower_flrp = 0;
539 }
540
541 NIR_PASS(progress, nir, nir_opt_undef);
542 NIR_PASS(progress, nir, nir_undef_to_zero);
543
544 NIR_PASS(progress, nir, nir_opt_loop_unroll,
545 nir_var_shader_in |
546 nir_var_shader_out |
547 nir_var_function_temp);
548
549 NIR_PASS(progress, nir, nir_opt_vectorize);
550 } while (progress);
551
552 /* Must be run at the end to prevent creation of fsin/fcos ops */
553 NIR_PASS(progress, nir, midgard_nir_scale_trig);
554
555 do {
556 progress = false;
557
558 NIR_PASS(progress, nir, nir_opt_dce);
559 NIR_PASS(progress, nir, nir_opt_algebraic);
560 NIR_PASS(progress, nir, nir_opt_constant_folding);
561 NIR_PASS(progress, nir, nir_copy_prop);
562 } while (progress);
563
564 NIR_PASS(progress, nir, nir_opt_algebraic_late);
565
566 /* We implement booleans as 32-bit 0/~0 */
567 NIR_PASS(progress, nir, nir_lower_bool_to_int32);
568
569 /* Now that booleans are lowered, we can run out late opts */
570 NIR_PASS(progress, nir, midgard_nir_lower_algebraic_late);
571
572 /* Lower mods for float ops only. Integer ops don't support modifiers
573 * (saturate doesn't make sense on integers, neg/abs require dedicated
574 * instructions) */
575
576 NIR_PASS(progress, nir, nir_lower_to_source_mods, nir_lower_float_source_mods);
577 NIR_PASS(progress, nir, nir_copy_prop);
578 NIR_PASS(progress, nir, nir_opt_dce);
579
580 /* Take us out of SSA */
581 NIR_PASS(progress, nir, nir_lower_locals_to_regs);
582 NIR_PASS(progress, nir, nir_convert_from_ssa, true);
583
584 /* We are a vector architecture; write combine where possible */
585 NIR_PASS(progress, nir, nir_move_vec_src_uses_to_dest);
586 NIR_PASS(progress, nir, nir_lower_vec_to_movs);
587
588 NIR_PASS(progress, nir, nir_opt_dce);
589 }
590
591 /* Do not actually emit a load; instead, cache the constant for inlining */
592
593 static void
594 emit_load_const(compiler_context *ctx, nir_load_const_instr *instr)
595 {
596 nir_ssa_def def = instr->def;
597
598 float *v = rzalloc_array(NULL, float, 4);
599 nir_const_value_to_array(v, instr->value, instr->def.num_components, f32);
600
601 /* Shifted for SSA, +1 for off-by-one */
602 _mesa_hash_table_u64_insert(ctx->ssa_constants, (def.index << 1) + 1, v);
603 }
604
605 /* Normally constants are embedded implicitly, but for I/O and such we have to
606 * explicitly emit a move with the constant source */
607
608 static void
609 emit_explicit_constant(compiler_context *ctx, unsigned node, unsigned to)
610 {
611 void *constant_value = _mesa_hash_table_u64_search(ctx->ssa_constants, node + 1);
612
613 if (constant_value) {
614 midgard_instruction ins = v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), to);
615 attach_constants(ctx, &ins, constant_value, node + 1);
616 emit_mir_instruction(ctx, ins);
617 }
618 }
619
620 static bool
621 nir_is_non_scalar_swizzle(nir_alu_src *src, unsigned nr_components)
622 {
623 unsigned comp = src->swizzle[0];
624
625 for (unsigned c = 1; c < nr_components; ++c) {
626 if (src->swizzle[c] != comp)
627 return true;
628 }
629
630 return false;
631 }
632
633 #define ALU_CASE(nir, _op) \
634 case nir_op_##nir: \
635 op = midgard_alu_op_##_op; \
636 assert(src_bitsize == dst_bitsize); \
637 break;
638
639 #define ALU_CASE_BCAST(nir, _op, count) \
640 case nir_op_##nir: \
641 op = midgard_alu_op_##_op; \
642 broadcast_swizzle = count; \
643 assert(src_bitsize == dst_bitsize); \
644 break;
645 static bool
646 nir_is_fzero_constant(nir_src src)
647 {
648 if (!nir_src_is_const(src))
649 return false;
650
651 for (unsigned c = 0; c < nir_src_num_components(src); ++c) {
652 if (nir_src_comp_as_float(src, c) != 0.0)
653 return false;
654 }
655
656 return true;
657 }
658
659 /* Analyze the sizes of the inputs to determine which reg mode. Ops needed
660 * special treatment override this anyway. */
661
662 static midgard_reg_mode
663 reg_mode_for_nir(nir_alu_instr *instr)
664 {
665 unsigned src_bitsize = nir_src_bit_size(instr->src[0].src);
666
667 switch (src_bitsize) {
668 case 8:
669 return midgard_reg_mode_8;
670 case 16:
671 return midgard_reg_mode_16;
672 case 32:
673 return midgard_reg_mode_32;
674 case 64:
675 return midgard_reg_mode_64;
676 default:
677 unreachable("Invalid bit size");
678 }
679 }
680
681 static void
682 emit_alu(compiler_context *ctx, nir_alu_instr *instr)
683 {
684 /* Derivatives end up emitted on the texture pipe, not the ALUs. This
685 * is handled elsewhere */
686
687 if (instr->op == nir_op_fddx || instr->op == nir_op_fddy) {
688 midgard_emit_derivatives(ctx, instr);
689 return;
690 }
691
692 bool is_ssa = instr->dest.dest.is_ssa;
693
694 unsigned dest = nir_dest_index(ctx, &instr->dest.dest);
695 unsigned nr_components = nir_dest_num_components(instr->dest.dest);
696 unsigned nr_inputs = nir_op_infos[instr->op].num_inputs;
697
698 /* Most Midgard ALU ops have a 1:1 correspondance to NIR ops; these are
699 * supported. A few do not and are commented for now. Also, there are a
700 * number of NIR ops which Midgard does not support and need to be
701 * lowered, also TODO. This switch block emits the opcode and calling
702 * convention of the Midgard instruction; actual packing is done in
703 * emit_alu below */
704
705 unsigned op;
706
707 /* Number of components valid to check for the instruction (the rest
708 * will be forced to the last), or 0 to use as-is. Relevant as
709 * ball-type instructions have a channel count in NIR but are all vec4
710 * in Midgard */
711
712 unsigned broadcast_swizzle = 0;
713
714 /* What register mode should we operate in? */
715 midgard_reg_mode reg_mode =
716 reg_mode_for_nir(instr);
717
718 /* Do we need a destination override? Used for inline
719 * type conversion */
720
721 midgard_dest_override dest_override =
722 midgard_dest_override_none;
723
724 /* Should we use a smaller respective source and sign-extend? */
725
726 bool half_1 = false, sext_1 = false;
727 bool half_2 = false, sext_2 = false;
728
729 unsigned src_bitsize = nir_src_bit_size(instr->src[0].src);
730 unsigned dst_bitsize = nir_dest_bit_size(instr->dest.dest);
731
732 switch (instr->op) {
733 ALU_CASE(fadd, fadd);
734 ALU_CASE(fmul, fmul);
735 ALU_CASE(fmin, fmin);
736 ALU_CASE(fmax, fmax);
737 ALU_CASE(imin, imin);
738 ALU_CASE(imax, imax);
739 ALU_CASE(umin, umin);
740 ALU_CASE(umax, umax);
741 ALU_CASE(ffloor, ffloor);
742 ALU_CASE(fround_even, froundeven);
743 ALU_CASE(ftrunc, ftrunc);
744 ALU_CASE(fceil, fceil);
745 ALU_CASE(fdot3, fdot3);
746 ALU_CASE(fdot4, fdot4);
747 ALU_CASE(iadd, iadd);
748 ALU_CASE(isub, isub);
749 ALU_CASE(imul, imul);
750
751 /* Zero shoved as second-arg */
752 ALU_CASE(iabs, iabsdiff);
753
754 ALU_CASE(mov, imov);
755
756 ALU_CASE(feq32, feq);
757 ALU_CASE(fne32, fne);
758 ALU_CASE(flt32, flt);
759 ALU_CASE(ieq32, ieq);
760 ALU_CASE(ine32, ine);
761 ALU_CASE(ilt32, ilt);
762 ALU_CASE(ult32, ult);
763
764 /* We don't have a native b2f32 instruction. Instead, like many
765 * GPUs, we exploit booleans as 0/~0 for false/true, and
766 * correspondingly AND
767 * by 1.0 to do the type conversion. For the moment, prime us
768 * to emit:
769 *
770 * iand [whatever], #0
771 *
772 * At the end of emit_alu (as MIR), we'll fix-up the constant
773 */
774
775 ALU_CASE(b2f32, iand);
776 ALU_CASE(b2i32, iand);
777
778 /* Likewise, we don't have a dedicated f2b32 instruction, but
779 * we can do a "not equal to 0.0" test. */
780
781 ALU_CASE(f2b32, fne);
782 ALU_CASE(i2b32, ine);
783
784 ALU_CASE(frcp, frcp);
785 ALU_CASE(frsq, frsqrt);
786 ALU_CASE(fsqrt, fsqrt);
787 ALU_CASE(fexp2, fexp2);
788 ALU_CASE(flog2, flog2);
789
790 ALU_CASE(f2i32, f2i_rtz);
791 ALU_CASE(f2u32, f2u_rtz);
792 ALU_CASE(i2f32, i2f_rtz);
793 ALU_CASE(u2f32, u2f_rtz);
794
795 ALU_CASE(f2i16, f2i_rtz);
796 ALU_CASE(f2u16, f2u_rtz);
797 ALU_CASE(i2f16, i2f_rtz);
798 ALU_CASE(u2f16, u2f_rtz);
799
800 ALU_CASE(fsin, fsin);
801 ALU_CASE(fcos, fcos);
802
803 /* We'll set invert */
804 ALU_CASE(inot, imov);
805 ALU_CASE(iand, iand);
806 ALU_CASE(ior, ior);
807 ALU_CASE(ixor, ixor);
808 ALU_CASE(ishl, ishl);
809 ALU_CASE(ishr, iasr);
810 ALU_CASE(ushr, ilsr);
811
812 ALU_CASE_BCAST(b32all_fequal2, fball_eq, 2);
813 ALU_CASE_BCAST(b32all_fequal3, fball_eq, 3);
814 ALU_CASE(b32all_fequal4, fball_eq);
815
816 ALU_CASE_BCAST(b32any_fnequal2, fbany_neq, 2);
817 ALU_CASE_BCAST(b32any_fnequal3, fbany_neq, 3);
818 ALU_CASE(b32any_fnequal4, fbany_neq);
819
820 ALU_CASE_BCAST(b32all_iequal2, iball_eq, 2);
821 ALU_CASE_BCAST(b32all_iequal3, iball_eq, 3);
822 ALU_CASE(b32all_iequal4, iball_eq);
823
824 ALU_CASE_BCAST(b32any_inequal2, ibany_neq, 2);
825 ALU_CASE_BCAST(b32any_inequal3, ibany_neq, 3);
826 ALU_CASE(b32any_inequal4, ibany_neq);
827
828 /* Source mods will be shoved in later */
829 ALU_CASE(fabs, fmov);
830 ALU_CASE(fneg, fmov);
831 ALU_CASE(fsat, fmov);
832
833 /* For size conversion, we use a move. Ideally though we would squash
834 * these ops together; maybe that has to happen after in NIR as part of
835 * propagation...? An earlier algebraic pass ensured we step down by
836 * only / exactly one size. If stepping down, we use a dest override to
837 * reduce the size; if stepping up, we use a larger-sized move with a
838 * half source and a sign/zero-extension modifier */
839
840 case nir_op_i2i8:
841 case nir_op_i2i16:
842 case nir_op_i2i32:
843 case nir_op_i2i64:
844 /* If we end up upscale, we'll need a sign-extend on the
845 * operand (the second argument) */
846
847 sext_2 = true;
848 /* fallthrough */
849 case nir_op_u2u8:
850 case nir_op_u2u16:
851 case nir_op_u2u32:
852 case nir_op_u2u64: {
853 op = midgard_alu_op_imov;
854
855 if (dst_bitsize == (src_bitsize * 2)) {
856 /* Converting up */
857 half_2 = true;
858
859 /* Use a greater register mode */
860 reg_mode++;
861 } else if (src_bitsize == (dst_bitsize * 2)) {
862 /* Converting down */
863 dest_override = midgard_dest_override_lower;
864 }
865
866 break;
867 }
868
869 case nir_op_f2f16: {
870 assert(src_bitsize == 32);
871
872 op = midgard_alu_op_fmov;
873 dest_override = midgard_dest_override_lower;
874 break;
875 }
876
877 case nir_op_f2f32: {
878 assert(src_bitsize == 16);
879
880 op = midgard_alu_op_fmov;
881 half_2 = true;
882 reg_mode++;
883 break;
884 }
885
886
887 /* For greater-or-equal, we lower to less-or-equal and flip the
888 * arguments */
889
890 case nir_op_fge:
891 case nir_op_fge32:
892 case nir_op_ige32:
893 case nir_op_uge32: {
894 op =
895 instr->op == nir_op_fge ? midgard_alu_op_fle :
896 instr->op == nir_op_fge32 ? midgard_alu_op_fle :
897 instr->op == nir_op_ige32 ? midgard_alu_op_ile :
898 instr->op == nir_op_uge32 ? midgard_alu_op_ule :
899 0;
900
901 /* Swap via temporary */
902 nir_alu_src temp = instr->src[1];
903 instr->src[1] = instr->src[0];
904 instr->src[0] = temp;
905
906 break;
907 }
908
909 case nir_op_b32csel: {
910 /* Midgard features both fcsel and icsel, depending on
911 * the type of the arguments/output. However, as long
912 * as we're careful we can _always_ use icsel and
913 * _never_ need fcsel, since the latter does additional
914 * floating-point-specific processing whereas the
915 * former just moves bits on the wire. It's not obvious
916 * why these are separate opcodes, save for the ability
917 * to do things like sat/pos/abs/neg for free */
918
919 bool mixed = nir_is_non_scalar_swizzle(&instr->src[0], nr_components);
920 op = mixed ? midgard_alu_op_icsel_v : midgard_alu_op_icsel;
921
922 /* The condition is the first argument; move the other
923 * arguments up one to be a binary instruction for
924 * Midgard with the condition last */
925
926 nir_alu_src temp = instr->src[2];
927
928 instr->src[2] = instr->src[0];
929 instr->src[0] = instr->src[1];
930 instr->src[1] = temp;
931
932 break;
933 }
934
935 default:
936 DBG("Unhandled ALU op %s\n", nir_op_infos[instr->op].name);
937 assert(0);
938 return;
939 }
940
941 /* Midgard can perform certain modifiers on output of an ALU op */
942 unsigned outmod;
943
944 if (midgard_is_integer_out_op(op)) {
945 outmod = midgard_outmod_int_wrap;
946 } else {
947 bool sat = instr->dest.saturate || instr->op == nir_op_fsat;
948 outmod = sat ? midgard_outmod_sat : midgard_outmod_none;
949 }
950
951 /* fmax(a, 0.0) can turn into a .pos modifier as an optimization */
952
953 if (instr->op == nir_op_fmax) {
954 if (nir_is_fzero_constant(instr->src[0].src)) {
955 op = midgard_alu_op_fmov;
956 nr_inputs = 1;
957 outmod = midgard_outmod_pos;
958 instr->src[0] = instr->src[1];
959 } else if (nir_is_fzero_constant(instr->src[1].src)) {
960 op = midgard_alu_op_fmov;
961 nr_inputs = 1;
962 outmod = midgard_outmod_pos;
963 }
964 }
965
966 /* Fetch unit, quirks, etc information */
967 unsigned opcode_props = alu_opcode_props[op].props;
968 bool quirk_flipped_r24 = opcode_props & QUIRK_FLIPPED_R24;
969
970 /* src0 will always exist afaik, but src1 will not for 1-argument
971 * instructions. The latter can only be fetched if the instruction
972 * needs it, or else we may segfault. */
973
974 unsigned src0 = nir_alu_src_index(ctx, &instr->src[0]);
975 unsigned src1 = nr_inputs >= 2 ? nir_alu_src_index(ctx, &instr->src[1]) : ~0;
976 unsigned src2 = nr_inputs == 3 ? nir_alu_src_index(ctx, &instr->src[2]) : ~0;
977 assert(nr_inputs <= 3);
978
979 /* Rather than use the instruction generation helpers, we do it
980 * ourselves here to avoid the mess */
981
982 midgard_instruction ins = {
983 .type = TAG_ALU_4,
984 .src = {
985 quirk_flipped_r24 ? ~0 : src0,
986 quirk_flipped_r24 ? src0 : src1,
987 src2,
988 },
989 .dest = dest,
990 };
991
992 nir_alu_src *nirmods[3] = { NULL };
993
994 if (nr_inputs >= 2) {
995 nirmods[0] = &instr->src[0];
996 nirmods[1] = &instr->src[1];
997 } else if (nr_inputs == 1) {
998 nirmods[quirk_flipped_r24] = &instr->src[0];
999 } else {
1000 assert(0);
1001 }
1002
1003 if (nr_inputs == 3)
1004 nirmods[2] = &instr->src[2];
1005
1006 /* These were lowered to a move, so apply the corresponding mod */
1007
1008 if (instr->op == nir_op_fneg || instr->op == nir_op_fabs) {
1009 nir_alu_src *s = nirmods[quirk_flipped_r24];
1010
1011 if (instr->op == nir_op_fneg)
1012 s->negate = !s->negate;
1013
1014 if (instr->op == nir_op_fabs)
1015 s->abs = !s->abs;
1016 }
1017
1018 bool is_int = midgard_is_integer_op(op);
1019
1020 ins.mask = mask_of(nr_components);
1021
1022 midgard_vector_alu alu = {
1023 .op = op,
1024 .reg_mode = reg_mode,
1025 .dest_override = dest_override,
1026 .outmod = outmod,
1027
1028 .src1 = vector_alu_srco_unsigned(vector_alu_modifiers(nirmods[0], is_int, broadcast_swizzle, half_1, sext_1)),
1029 .src2 = vector_alu_srco_unsigned(vector_alu_modifiers(nirmods[1], is_int, broadcast_swizzle, half_2, sext_2)),
1030 };
1031
1032 /* Apply writemask if non-SSA, keeping in mind that we can't write to components that don't exist */
1033
1034 if (!is_ssa)
1035 ins.mask &= instr->dest.write_mask;
1036
1037 for (unsigned m = 0; m < 3; ++m) {
1038 if (!nirmods[m])
1039 continue;
1040
1041 for (unsigned c = 0; c < NIR_MAX_VEC_COMPONENTS; ++c)
1042 ins.swizzle[m][c] = nirmods[m]->swizzle[c];
1043
1044 /* Replicate. TODO: remove when vec16 lands */
1045 for (unsigned c = NIR_MAX_VEC_COMPONENTS; c < MIR_VEC_COMPONENTS; ++c)
1046 ins.swizzle[m][c] = nirmods[m]->swizzle[NIR_MAX_VEC_COMPONENTS - 1];
1047 }
1048
1049 if (nr_inputs == 3) {
1050 /* Conditions can't have mods */
1051 assert(!nirmods[2]->abs);
1052 assert(!nirmods[2]->negate);
1053 }
1054
1055 ins.alu = alu;
1056
1057 /* Late fixup for emulated instructions */
1058
1059 if (instr->op == nir_op_b2f32 || instr->op == nir_op_b2i32) {
1060 /* Presently, our second argument is an inline #0 constant.
1061 * Switch over to an embedded 1.0 constant (that can't fit
1062 * inline, since we're 32-bit, not 16-bit like the inline
1063 * constants) */
1064
1065 ins.has_inline_constant = false;
1066 ins.src[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT);
1067 ins.has_constants = true;
1068
1069 if (instr->op == nir_op_b2f32) {
1070 float f = 1.0f;
1071 memcpy(&ins.constants, &f, sizeof(float));
1072 } else {
1073 ins.constants[0] = 1;
1074 }
1075
1076
1077 for (unsigned c = 0; c < 16; ++c)
1078 ins.swizzle[1][c] = 0;
1079 } else if (nr_inputs == 1 && !quirk_flipped_r24) {
1080 /* Lots of instructions need a 0 plonked in */
1081 ins.has_inline_constant = false;
1082 ins.src[1] = SSA_FIXED_REGISTER(REGISTER_CONSTANT);
1083 ins.has_constants = true;
1084 ins.constants[0] = 0;
1085
1086 for (unsigned c = 0; c < 16; ++c)
1087 ins.swizzle[1][c] = 0;
1088 } else if (instr->op == nir_op_inot) {
1089 ins.invert = true;
1090 }
1091
1092 if ((opcode_props & UNITS_ALL) == UNIT_VLUT) {
1093 /* To avoid duplicating the lookup tables (probably), true LUT
1094 * instructions can only operate as if they were scalars. Lower
1095 * them here by changing the component. */
1096
1097 unsigned orig_mask = ins.mask;
1098
1099 for (int i = 0; i < nr_components; ++i) {
1100 /* Mask the associated component, dropping the
1101 * instruction if needed */
1102
1103 ins.mask = 1 << i;
1104 ins.mask &= orig_mask;
1105
1106 if (!ins.mask)
1107 continue;
1108
1109 for (unsigned j = 0; j < MIR_VEC_COMPONENTS; ++j)
1110 ins.swizzle[0][j] = nirmods[0]->swizzle[i]; /* Pull from the correct component */
1111
1112 emit_mir_instruction(ctx, ins);
1113 }
1114 } else {
1115 emit_mir_instruction(ctx, ins);
1116 }
1117 }
1118
1119 #undef ALU_CASE
1120
1121 static void
1122 mir_set_intr_mask(nir_instr *instr, midgard_instruction *ins, bool is_read)
1123 {
1124 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
1125 unsigned nir_mask = 0;
1126 unsigned dsize = 0;
1127
1128 if (is_read) {
1129 nir_mask = mask_of(nir_intrinsic_dest_components(intr));
1130 dsize = nir_dest_bit_size(intr->dest);
1131 } else {
1132 nir_mask = nir_intrinsic_write_mask(intr);
1133 dsize = 32;
1134 }
1135
1136 /* Once we have the NIR mask, we need to normalize to work in 32-bit space */
1137 unsigned bytemask = mir_to_bytemask(mir_mode_for_destsize(dsize), nir_mask);
1138 mir_set_bytemask(ins, bytemask);
1139
1140 if (dsize == 64)
1141 ins->load_64 = true;
1142 }
1143
1144 /* Uniforms and UBOs use a shared code path, as uniforms are just (slightly
1145 * optimized) versions of UBO #0 */
1146
1147 midgard_instruction *
1148 emit_ubo_read(
1149 compiler_context *ctx,
1150 nir_instr *instr,
1151 unsigned dest,
1152 unsigned offset,
1153 nir_src *indirect_offset,
1154 unsigned index)
1155 {
1156 /* TODO: half-floats */
1157
1158 midgard_instruction ins = m_ld_ubo_int4(dest, 0);
1159 ins.constants[0] = offset;
1160 mir_set_intr_mask(instr, &ins, true);
1161
1162 if (indirect_offset) {
1163 ins.src[2] = nir_src_index(ctx, indirect_offset);
1164 ins.load_store.arg_2 = 0x80;
1165 } else {
1166 ins.load_store.arg_2 = 0x1E;
1167 }
1168
1169 ins.load_store.arg_1 = index;
1170
1171 return emit_mir_instruction(ctx, ins);
1172 }
1173
1174 /* SSBO reads are like UBO reads if you squint */
1175
1176 static void
1177 emit_ssbo_access(
1178 compiler_context *ctx,
1179 nir_instr *instr,
1180 bool is_read,
1181 unsigned srcdest,
1182 unsigned offset,
1183 nir_src *indirect_offset,
1184 unsigned index)
1185 {
1186 /* TODO: types */
1187
1188 midgard_instruction ins;
1189
1190 if (is_read)
1191 ins = m_ld_int4(srcdest, offset);
1192 else
1193 ins = m_st_int4(srcdest, offset);
1194
1195 /* SSBO reads use a generic memory read interface, so we need the
1196 * address of the SSBO as the first argument. This is a sysval. */
1197
1198 unsigned addr = make_compiler_temp(ctx);
1199 emit_sysval_read(ctx, instr, addr, 2);
1200
1201 /* The source array:
1202 *
1203 * src[0] = store ? value : unused
1204 * src[1] = arg_1
1205 * src[2] = arg_2
1206 *
1207 * We would like arg_1 = the address and
1208 * arg_2 = the offset.
1209 */
1210
1211 ins.src[1] = addr;
1212
1213 /* TODO: What is this? It looks superficially like a shift << 5, but
1214 * arg_1 doesn't take a shift Should it be E0 or A0? We also need the
1215 * indirect offset. */
1216
1217 if (indirect_offset) {
1218 ins.load_store.arg_1 |= 0xE0;
1219 ins.src[2] = nir_src_index(ctx, indirect_offset);
1220 } else {
1221 ins.load_store.arg_2 = 0x7E;
1222 }
1223
1224 /* TODO: Bounds check */
1225
1226 /* Finally, we emit the direct offset */
1227
1228 ins.load_store.varying_parameters = (offset & 0x1FF) << 1;
1229 ins.load_store.address = (offset >> 9);
1230 mir_set_intr_mask(instr, &ins, is_read);
1231
1232 emit_mir_instruction(ctx, ins);
1233 }
1234
1235 static void
1236 emit_varying_read(
1237 compiler_context *ctx,
1238 unsigned dest, unsigned offset,
1239 unsigned nr_comp, unsigned component,
1240 nir_src *indirect_offset, nir_alu_type type)
1241 {
1242 /* XXX: Half-floats? */
1243 /* TODO: swizzle, mask */
1244
1245 midgard_instruction ins = m_ld_vary_32(dest, offset);
1246 ins.mask = mask_of(nr_comp);
1247
1248 for (unsigned i = 0; i < ARRAY_SIZE(ins.swizzle[0]); ++i)
1249 ins.swizzle[0][i] = MIN2(i + component, COMPONENT_W);
1250
1251 midgard_varying_parameter p = {
1252 .is_varying = 1,
1253 .interpolation = midgard_interp_default,
1254 .flat = /*var->data.interpolation == INTERP_MODE_FLAT*/ 0
1255 };
1256
1257 unsigned u;
1258 memcpy(&u, &p, sizeof(p));
1259 ins.load_store.varying_parameters = u;
1260
1261 if (indirect_offset)
1262 ins.src[2] = nir_src_index(ctx, indirect_offset);
1263 else
1264 ins.load_store.arg_2 = 0x1E;
1265
1266 ins.load_store.arg_1 = 0x9E;
1267
1268 /* Use the type appropriate load */
1269 switch (type) {
1270 case nir_type_uint:
1271 case nir_type_bool:
1272 ins.load_store.op = midgard_op_ld_vary_32u;
1273 break;
1274 case nir_type_int:
1275 ins.load_store.op = midgard_op_ld_vary_32i;
1276 break;
1277 case nir_type_float:
1278 ins.load_store.op = midgard_op_ld_vary_32;
1279 break;
1280 default:
1281 unreachable("Attempted to load unknown type");
1282 break;
1283 }
1284
1285 emit_mir_instruction(ctx, ins);
1286 }
1287
1288 void
1289 emit_sysval_read(compiler_context *ctx, nir_instr *instr, signed dest_override,
1290 unsigned nr_components)
1291 {
1292 unsigned dest = 0;
1293
1294 /* Figure out which uniform this is */
1295 int sysval = sysval_for_instr(ctx, instr, &dest);
1296 void *val = _mesa_hash_table_u64_search(ctx->sysval_to_id, sysval);
1297
1298 if (dest_override >= 0)
1299 dest = dest_override;
1300
1301 /* Sysvals are prefix uniforms */
1302 unsigned uniform = ((uintptr_t) val) - 1;
1303
1304 /* Emit the read itself -- this is never indirect */
1305 midgard_instruction *ins =
1306 emit_ubo_read(ctx, instr, dest, uniform * 16, NULL, 0);
1307
1308 ins->mask = mask_of(nr_components);
1309 }
1310
1311 static unsigned
1312 compute_builtin_arg(nir_op op)
1313 {
1314 switch (op) {
1315 case nir_intrinsic_load_work_group_id:
1316 return 0x14;
1317 case nir_intrinsic_load_local_invocation_id:
1318 return 0x10;
1319 default:
1320 unreachable("Invalid compute paramater loaded");
1321 }
1322 }
1323
1324 /* Emit store for a fragment shader, which is encoded via a fancy branch. TODO:
1325 * Handle MRT here */
1326
1327 static void
1328 emit_fragment_store(compiler_context *ctx, unsigned src, unsigned rt)
1329 {
1330 emit_explicit_constant(ctx, src, src);
1331
1332 /* If we're doing MRT, we need to specify the render target */
1333
1334 midgard_instruction rt_move = {
1335 .dest = ~0
1336 };
1337
1338 if (rt != 0) {
1339 /* We'll write to r1.z */
1340 rt_move = v_mov(~0, SSA_FIXED_REGISTER(1));
1341 rt_move.mask = 1 << COMPONENT_Z;
1342 rt_move.unit = UNIT_SADD;
1343
1344 /* r1.z = (rt * 0x100) */
1345 rt_move.has_inline_constant = true;
1346 rt_move.inline_constant = (rt * 0x100);
1347
1348 /* r1 */
1349 ctx->work_registers = MAX2(ctx->work_registers, 1);
1350
1351 /* Do the write */
1352 emit_mir_instruction(ctx, rt_move);
1353 }
1354
1355 /* Next, generate the branch. For R render targets in the writeout, the
1356 * i'th render target jumps to pseudo-offset [2(R-1) + i] */
1357
1358 unsigned outputs = ctx->is_blend ? 1 : ctx->nir->num_outputs;
1359 unsigned offset = (2 * (outputs - 1)) + rt;
1360
1361 struct midgard_instruction ins =
1362 v_alu_br_compact_cond(midgard_jmp_writeout_op_writeout, TAG_ALU_4, offset, midgard_condition_always);
1363
1364 /* Add dependencies */
1365 ins.src[0] = src;
1366 ins.src[1] = rt_move.dest;
1367
1368 /* Emit the branch */
1369 emit_mir_instruction(ctx, ins);
1370 }
1371
1372 static void
1373 emit_compute_builtin(compiler_context *ctx, nir_intrinsic_instr *instr)
1374 {
1375 unsigned reg = nir_dest_index(ctx, &instr->dest);
1376 midgard_instruction ins = m_ld_compute_id(reg, 0);
1377 ins.mask = mask_of(3);
1378 ins.load_store.arg_1 = compute_builtin_arg(instr->intrinsic);
1379 emit_mir_instruction(ctx, ins);
1380 }
1381 static void
1382 emit_intrinsic(compiler_context *ctx, nir_intrinsic_instr *instr)
1383 {
1384 unsigned offset = 0, reg;
1385
1386 switch (instr->intrinsic) {
1387 case nir_intrinsic_discard_if:
1388 case nir_intrinsic_discard: {
1389 bool conditional = instr->intrinsic == nir_intrinsic_discard_if;
1390 struct midgard_instruction discard = v_branch(conditional, false);
1391 discard.branch.target_type = TARGET_DISCARD;
1392
1393 if (conditional)
1394 discard.src[0] = nir_src_index(ctx, &instr->src[0]);
1395
1396 emit_mir_instruction(ctx, discard);
1397 schedule_barrier(ctx);
1398
1399 break;
1400 }
1401
1402 case nir_intrinsic_load_uniform:
1403 case nir_intrinsic_load_ubo:
1404 case nir_intrinsic_load_ssbo:
1405 case nir_intrinsic_load_input: {
1406 bool is_uniform = instr->intrinsic == nir_intrinsic_load_uniform;
1407 bool is_ubo = instr->intrinsic == nir_intrinsic_load_ubo;
1408 bool is_ssbo = instr->intrinsic == nir_intrinsic_load_ssbo;
1409
1410 /* Get the base type of the intrinsic */
1411 /* TODO: Infer type? Does it matter? */
1412 nir_alu_type t =
1413 (is_ubo || is_ssbo) ? nir_type_uint : nir_intrinsic_type(instr);
1414 t = nir_alu_type_get_base_type(t);
1415
1416 if (!(is_ubo || is_ssbo)) {
1417 offset = nir_intrinsic_base(instr);
1418 }
1419
1420 unsigned nr_comp = nir_intrinsic_dest_components(instr);
1421
1422 nir_src *src_offset = nir_get_io_offset_src(instr);
1423
1424 bool direct = nir_src_is_const(*src_offset);
1425 nir_src *indirect_offset = direct ? NULL : src_offset;
1426
1427 if (direct)
1428 offset += nir_src_as_uint(*src_offset);
1429
1430 /* We may need to apply a fractional offset */
1431 int component = instr->intrinsic == nir_intrinsic_load_input ?
1432 nir_intrinsic_component(instr) : 0;
1433 reg = nir_dest_index(ctx, &instr->dest);
1434
1435 if (is_uniform && !ctx->is_blend) {
1436 emit_ubo_read(ctx, &instr->instr, reg, (ctx->sysval_count + offset) * 16, indirect_offset, 0);
1437 } else if (is_ubo) {
1438 nir_src index = instr->src[0];
1439
1440 /* We don't yet support indirect UBOs. For indirect
1441 * block numbers (if that's possible), we don't know
1442 * enough about the hardware yet. For indirect sources,
1443 * we know what we need but we need to add some NIR
1444 * support for lowering correctly with respect to
1445 * 128-bit reads */
1446
1447 assert(nir_src_is_const(index));
1448 assert(nir_src_is_const(*src_offset));
1449
1450 uint32_t uindex = nir_src_as_uint(index) + 1;
1451 emit_ubo_read(ctx, &instr->instr, reg, offset, NULL, uindex);
1452 } else if (is_ssbo) {
1453 nir_src index = instr->src[0];
1454 assert(nir_src_is_const(index));
1455 uint32_t uindex = nir_src_as_uint(index);
1456
1457 emit_ssbo_access(ctx, &instr->instr, true, reg, offset, indirect_offset, uindex);
1458 } else if (ctx->stage == MESA_SHADER_FRAGMENT && !ctx->is_blend) {
1459 emit_varying_read(ctx, reg, offset, nr_comp, component, !direct ? &instr->src[0] : NULL, t);
1460 } else if (ctx->is_blend) {
1461 /* For blend shaders, load the input color, which is
1462 * preloaded to r0 */
1463
1464 midgard_instruction move = v_mov(SSA_FIXED_REGISTER(0), reg);
1465 emit_mir_instruction(ctx, move);
1466 schedule_barrier(ctx);
1467 } else if (ctx->stage == MESA_SHADER_VERTEX) {
1468 midgard_instruction ins = m_ld_attr_32(reg, offset);
1469 ins.load_store.arg_1 = 0x1E;
1470 ins.load_store.arg_2 = 0x1E;
1471 ins.mask = mask_of(nr_comp);
1472
1473 /* Use the type appropriate load */
1474 switch (t) {
1475 case nir_type_uint:
1476 case nir_type_bool:
1477 ins.load_store.op = midgard_op_ld_attr_32u;
1478 break;
1479 case nir_type_int:
1480 ins.load_store.op = midgard_op_ld_attr_32i;
1481 break;
1482 case nir_type_float:
1483 ins.load_store.op = midgard_op_ld_attr_32;
1484 break;
1485 default:
1486 unreachable("Attempted to load unknown type");
1487 break;
1488 }
1489
1490 emit_mir_instruction(ctx, ins);
1491 } else {
1492 DBG("Unknown load\n");
1493 assert(0);
1494 }
1495
1496 break;
1497 }
1498
1499 /* Reads 128-bit value raw off the tilebuffer during blending, tasty */
1500
1501 case nir_intrinsic_load_raw_output_pan:
1502 case nir_intrinsic_load_output_u8_as_fp16_pan:
1503 reg = nir_dest_index(ctx, &instr->dest);
1504 assert(ctx->is_blend);
1505
1506 /* T720 and below use different blend opcodes with slightly
1507 * different semantics than T760 and up */
1508
1509 midgard_instruction ld = m_ld_color_buffer_8(reg, 0);
1510 bool old_blend = ctx->quirks & MIDGARD_OLD_BLEND;
1511
1512 if (instr->intrinsic == nir_intrinsic_load_output_u8_as_fp16_pan) {
1513 ld.load_store.op = old_blend ?
1514 midgard_op_ld_color_buffer_u8_as_fp16_old :
1515 midgard_op_ld_color_buffer_u8_as_fp16;
1516
1517 if (old_blend) {
1518 ld.load_store.address = 1;
1519 ld.load_store.arg_2 = 0x1E;
1520 }
1521
1522 for (unsigned c = 2; c < 16; ++c)
1523 ld.swizzle[0][c] = 0;
1524 }
1525
1526 emit_mir_instruction(ctx, ld);
1527 break;
1528
1529 case nir_intrinsic_load_blend_const_color_rgba: {
1530 assert(ctx->is_blend);
1531 reg = nir_dest_index(ctx, &instr->dest);
1532
1533 /* Blend constants are embedded directly in the shader and
1534 * patched in, so we use some magic routing */
1535
1536 midgard_instruction ins = v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), reg);
1537 ins.has_constants = true;
1538 ins.has_blend_constant = true;
1539 emit_mir_instruction(ctx, ins);
1540 break;
1541 }
1542
1543 case nir_intrinsic_store_output:
1544 assert(nir_src_is_const(instr->src[1]) && "no indirect outputs");
1545
1546 offset = nir_intrinsic_base(instr) + nir_src_as_uint(instr->src[1]);
1547
1548 reg = nir_src_index(ctx, &instr->src[0]);
1549
1550 if (ctx->stage == MESA_SHADER_FRAGMENT) {
1551 /* Determine number of render targets */
1552 emit_fragment_store(ctx, reg, offset);
1553 } else if (ctx->stage == MESA_SHADER_VERTEX) {
1554 /* We should have been vectorized, though we don't
1555 * currently check that st_vary is emitted only once
1556 * per slot (this is relevant, since there's not a mask
1557 * parameter available on the store [set to 0 by the
1558 * blob]). We do respect the component by adjusting the
1559 * swizzle. If this is a constant source, we'll need to
1560 * emit that explicitly. */
1561
1562 emit_explicit_constant(ctx, reg, reg);
1563
1564 unsigned component = nir_intrinsic_component(instr);
1565 unsigned nr_comp = nir_src_num_components(instr->src[0]);
1566
1567 midgard_instruction st = m_st_vary_32(reg, offset);
1568 st.load_store.arg_1 = 0x9E;
1569 st.load_store.arg_2 = 0x1E;
1570
1571 for (unsigned i = 0; i < ARRAY_SIZE(st.swizzle[0]); ++i)
1572 st.swizzle[0][i] = MIN2(i + component, nr_comp);
1573
1574 emit_mir_instruction(ctx, st);
1575 } else {
1576 DBG("Unknown store\n");
1577 assert(0);
1578 }
1579
1580 break;
1581
1582 /* Special case of store_output for lowered blend shaders */
1583 case nir_intrinsic_store_raw_output_pan:
1584 assert (ctx->stage == MESA_SHADER_FRAGMENT);
1585 reg = nir_src_index(ctx, &instr->src[0]);
1586 emit_fragment_store(ctx, reg, 0);
1587
1588 break;
1589
1590 case nir_intrinsic_store_ssbo:
1591 assert(nir_src_is_const(instr->src[1]));
1592
1593 bool direct_offset = nir_src_is_const(instr->src[2]);
1594 offset = direct_offset ? nir_src_as_uint(instr->src[2]) : 0;
1595 nir_src *indirect_offset = direct_offset ? NULL : &instr->src[2];
1596 reg = nir_src_index(ctx, &instr->src[0]);
1597
1598 uint32_t uindex = nir_src_as_uint(instr->src[1]);
1599
1600 emit_explicit_constant(ctx, reg, reg);
1601 emit_ssbo_access(ctx, &instr->instr, false, reg, offset, indirect_offset, uindex);
1602 break;
1603
1604 case nir_intrinsic_load_viewport_scale:
1605 case nir_intrinsic_load_viewport_offset:
1606 case nir_intrinsic_load_num_work_groups:
1607 case nir_intrinsic_load_sampler_lod_parameters_pan:
1608 emit_sysval_read(ctx, &instr->instr, ~0, 3);
1609 break;
1610
1611 case nir_intrinsic_load_work_group_id:
1612 case nir_intrinsic_load_local_invocation_id:
1613 emit_compute_builtin(ctx, instr);
1614 break;
1615
1616 default:
1617 printf ("Unhandled intrinsic\n");
1618 assert(0);
1619 break;
1620 }
1621 }
1622
1623 static unsigned
1624 midgard_tex_format(enum glsl_sampler_dim dim)
1625 {
1626 switch (dim) {
1627 case GLSL_SAMPLER_DIM_1D:
1628 case GLSL_SAMPLER_DIM_BUF:
1629 return MALI_TEX_1D;
1630
1631 case GLSL_SAMPLER_DIM_2D:
1632 case GLSL_SAMPLER_DIM_EXTERNAL:
1633 case GLSL_SAMPLER_DIM_RECT:
1634 return MALI_TEX_2D;
1635
1636 case GLSL_SAMPLER_DIM_3D:
1637 return MALI_TEX_3D;
1638
1639 case GLSL_SAMPLER_DIM_CUBE:
1640 return MALI_TEX_CUBE;
1641
1642 default:
1643 DBG("Unknown sampler dim type\n");
1644 assert(0);
1645 return 0;
1646 }
1647 }
1648
1649 /* Tries to attach an explicit LOD / bias as a constant. Returns whether this
1650 * was successful */
1651
1652 static bool
1653 pan_attach_constant_bias(
1654 compiler_context *ctx,
1655 nir_src lod,
1656 midgard_texture_word *word)
1657 {
1658 /* To attach as constant, it has to *be* constant */
1659
1660 if (!nir_src_is_const(lod))
1661 return false;
1662
1663 float f = nir_src_as_float(lod);
1664
1665 /* Break into fixed-point */
1666 signed lod_int = f;
1667 float lod_frac = f - lod_int;
1668
1669 /* Carry over negative fractions */
1670 if (lod_frac < 0.0) {
1671 lod_int--;
1672 lod_frac += 1.0;
1673 }
1674
1675 /* Encode */
1676 word->bias = float_to_ubyte(lod_frac);
1677 word->bias_int = lod_int;
1678
1679 return true;
1680 }
1681
1682 static enum mali_sampler_type
1683 midgard_sampler_type(nir_alu_type t) {
1684 switch (nir_alu_type_get_base_type(t))
1685 {
1686 case nir_type_float:
1687 return MALI_SAMPLER_FLOAT;
1688 case nir_type_int:
1689 return MALI_SAMPLER_SIGNED;
1690 case nir_type_uint:
1691 return MALI_SAMPLER_UNSIGNED;
1692 default:
1693 unreachable("Unknown sampler type");
1694 }
1695 }
1696
1697 static void
1698 emit_texop_native(compiler_context *ctx, nir_tex_instr *instr,
1699 unsigned midgard_texop)
1700 {
1701 /* TODO */
1702 //assert (!instr->sampler);
1703 //assert (!instr->texture_array_size);
1704
1705 int texture_index = instr->texture_index;
1706 int sampler_index = texture_index;
1707
1708 /* No helper to build texture words -- we do it all here */
1709 midgard_instruction ins = {
1710 .type = TAG_TEXTURE_4,
1711 .mask = 0xF,
1712 .dest = nir_dest_index(ctx, &instr->dest),
1713 .src = { ~0, ~0, ~0 },
1714 .swizzle = SWIZZLE_IDENTITY_4,
1715 .texture = {
1716 .op = midgard_texop,
1717 .format = midgard_tex_format(instr->sampler_dim),
1718 .texture_handle = texture_index,
1719 .sampler_handle = sampler_index,
1720
1721 /* TODO: half */
1722 .in_reg_full = 1,
1723 .out_full = 1,
1724
1725 .sampler_type = midgard_sampler_type(instr->dest_type),
1726 }
1727 };
1728
1729 for (unsigned i = 0; i < instr->num_srcs; ++i) {
1730 int index = nir_src_index(ctx, &instr->src[i].src);
1731 unsigned nr_components = nir_src_num_components(instr->src[i].src);
1732
1733 switch (instr->src[i].src_type) {
1734 case nir_tex_src_coord: {
1735 emit_explicit_constant(ctx, index, index);
1736
1737 /* Texelfetch coordinates uses all four elements
1738 * (xyz/index) regardless of texture dimensionality,
1739 * which means it's necessary to zero the unused
1740 * components to keep everything happy */
1741
1742 if (midgard_texop == TEXTURE_OP_TEXEL_FETCH) {
1743 unsigned old_index = index;
1744
1745 index = make_compiler_temp(ctx);
1746
1747 /* mov index, old_index */
1748 midgard_instruction mov = v_mov(old_index, index);
1749 mov.mask = 0x3;
1750 emit_mir_instruction(ctx, mov);
1751
1752 /* mov index.zw, #0 */
1753 mov = v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), index);
1754 mov.has_constants = true;
1755 mov.mask = (1 << COMPONENT_Z) | (1 << COMPONENT_W);
1756 emit_mir_instruction(ctx, mov);
1757 }
1758
1759 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
1760 /* texelFetch is undefined on samplerCube */
1761 assert(midgard_texop != TEXTURE_OP_TEXEL_FETCH);
1762
1763 /* For cubemaps, we use a special ld/st op to
1764 * select the face and copy the xy into the
1765 * texture register */
1766
1767 unsigned temp = make_compiler_temp(ctx);
1768 midgard_instruction ld = m_ld_cubemap_coords(temp, 0);
1769 ld.src[1] = index;
1770 ld.mask = 0x3; /* xy */
1771 ld.load_store.arg_1 = 0x20;
1772 ld.swizzle[1][3] = COMPONENT_X;
1773 emit_mir_instruction(ctx, ld);
1774
1775 ins.src[1] = temp;
1776 /* xyzw -> xyxx */
1777 ins.swizzle[1][2] = COMPONENT_X;
1778 ins.swizzle[1][3] = COMPONENT_X;
1779 } else {
1780 ins.src[1] = index;
1781 }
1782
1783 if (instr->sampler_dim == GLSL_SAMPLER_DIM_2D) {
1784 /* Array component in w but NIR wants it in z */
1785 if (nr_components == 3) {
1786 ins.swizzle[1][2] = COMPONENT_Z;
1787 ins.swizzle[1][3] = COMPONENT_Z;
1788 } else if (nr_components == 2) {
1789 ins.swizzle[1][2] = COMPONENT_X;
1790 ins.swizzle[1][3] = COMPONENT_X;
1791 } else
1792 unreachable("Invalid texture 2D components");
1793 }
1794
1795 break;
1796 }
1797
1798 case nir_tex_src_bias:
1799 case nir_tex_src_lod: {
1800 /* Try as a constant if we can */
1801
1802 bool is_txf = midgard_texop == TEXTURE_OP_TEXEL_FETCH;
1803 if (!is_txf && pan_attach_constant_bias(ctx, instr->src[i].src, &ins.texture))
1804 break;
1805
1806 ins.texture.lod_register = true;
1807 ins.src[2] = index;
1808 emit_explicit_constant(ctx, index, index);
1809
1810 break;
1811 };
1812
1813 default:
1814 unreachable("Unknown texture source type\n");
1815 }
1816 }
1817
1818 emit_mir_instruction(ctx, ins);
1819
1820 /* Used for .cont and .last hinting */
1821 ctx->texture_op_count++;
1822 }
1823
1824 static void
1825 emit_tex(compiler_context *ctx, nir_tex_instr *instr)
1826 {
1827 /* Fixup op, since only textureLod is permitted in VS on later Midgard
1828 * but NIR can give generic tex in some cases (which confuses the
1829 * hardware). Interestingly, early Midgard lines up with NIR */
1830
1831 bool is_vertex = ctx->stage == MESA_SHADER_VERTEX;
1832
1833 if (is_vertex && instr->op == nir_texop_tex && ctx->quirks & MIDGARD_EXPLICIT_LOD)
1834 instr->op = nir_texop_txl;
1835
1836 switch (instr->op) {
1837 case nir_texop_tex:
1838 case nir_texop_txb:
1839 emit_texop_native(ctx, instr, TEXTURE_OP_NORMAL);
1840 break;
1841 case nir_texop_txl:
1842 emit_texop_native(ctx, instr, TEXTURE_OP_LOD);
1843 break;
1844 case nir_texop_txf:
1845 emit_texop_native(ctx, instr, TEXTURE_OP_TEXEL_FETCH);
1846 break;
1847 case nir_texop_txs:
1848 emit_sysval_read(ctx, &instr->instr, ~0, 4);
1849 break;
1850 default:
1851 unreachable("Unhanlded texture op");
1852 }
1853 }
1854
1855 static void
1856 emit_jump(compiler_context *ctx, nir_jump_instr *instr)
1857 {
1858 switch (instr->type) {
1859 case nir_jump_break: {
1860 /* Emit a branch out of the loop */
1861 struct midgard_instruction br = v_branch(false, false);
1862 br.branch.target_type = TARGET_BREAK;
1863 br.branch.target_break = ctx->current_loop_depth;
1864 emit_mir_instruction(ctx, br);
1865 break;
1866 }
1867
1868 default:
1869 DBG("Unknown jump type %d\n", instr->type);
1870 break;
1871 }
1872 }
1873
1874 static void
1875 emit_instr(compiler_context *ctx, struct nir_instr *instr)
1876 {
1877 switch (instr->type) {
1878 case nir_instr_type_load_const:
1879 emit_load_const(ctx, nir_instr_as_load_const(instr));
1880 break;
1881
1882 case nir_instr_type_intrinsic:
1883 emit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
1884 break;
1885
1886 case nir_instr_type_alu:
1887 emit_alu(ctx, nir_instr_as_alu(instr));
1888 break;
1889
1890 case nir_instr_type_tex:
1891 emit_tex(ctx, nir_instr_as_tex(instr));
1892 break;
1893
1894 case nir_instr_type_jump:
1895 emit_jump(ctx, nir_instr_as_jump(instr));
1896 break;
1897
1898 case nir_instr_type_ssa_undef:
1899 /* Spurious */
1900 break;
1901
1902 default:
1903 DBG("Unhandled instruction type\n");
1904 break;
1905 }
1906 }
1907
1908
1909 /* ALU instructions can inline or embed constants, which decreases register
1910 * pressure and saves space. */
1911
1912 #define CONDITIONAL_ATTACH(idx) { \
1913 void *entry = _mesa_hash_table_u64_search(ctx->ssa_constants, alu->src[idx] + 1); \
1914 \
1915 if (entry) { \
1916 attach_constants(ctx, alu, entry, alu->src[idx] + 1); \
1917 alu->src[idx] = SSA_FIXED_REGISTER(REGISTER_CONSTANT); \
1918 } \
1919 }
1920
1921 static void
1922 inline_alu_constants(compiler_context *ctx, midgard_block *block)
1923 {
1924 mir_foreach_instr_in_block(block, alu) {
1925 /* Other instructions cannot inline constants */
1926 if (alu->type != TAG_ALU_4) continue;
1927 if (alu->compact_branch) continue;
1928
1929 /* If there is already a constant here, we can do nothing */
1930 if (alu->has_constants) continue;
1931
1932 CONDITIONAL_ATTACH(0);
1933
1934 if (!alu->has_constants) {
1935 CONDITIONAL_ATTACH(1)
1936 } else if (!alu->inline_constant) {
1937 /* Corner case: _two_ vec4 constants, for instance with a
1938 * csel. For this case, we can only use a constant
1939 * register for one, we'll have to emit a move for the
1940 * other. Note, if both arguments are constants, then
1941 * necessarily neither argument depends on the value of
1942 * any particular register. As the destination register
1943 * will be wiped, that means we can spill the constant
1944 * to the destination register.
1945 */
1946
1947 void *entry = _mesa_hash_table_u64_search(ctx->ssa_constants, alu->src[1] + 1);
1948 unsigned scratch = alu->dest;
1949
1950 if (entry) {
1951 midgard_instruction ins = v_mov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), scratch);
1952 attach_constants(ctx, &ins, entry, alu->src[1] + 1);
1953
1954 /* Set the source */
1955 alu->src[1] = scratch;
1956
1957 /* Inject us -before- the last instruction which set r31 */
1958 mir_insert_instruction_before(ctx, mir_prev_op(alu), ins);
1959 }
1960 }
1961 }
1962 }
1963
1964 /* Being a little silly with the names, but returns the op that is the bitwise
1965 * inverse of the op with the argument switched. I.e. (f and g are
1966 * contrapositives):
1967 *
1968 * f(a, b) = ~g(b, a)
1969 *
1970 * Corollary: if g is the contrapositve of f, f is the contrapositive of g:
1971 *
1972 * f(a, b) = ~g(b, a)
1973 * ~f(a, b) = g(b, a)
1974 * ~f(a, b) = ~h(a, b) where h is the contrapositive of g
1975 * f(a, b) = h(a, b)
1976 *
1977 * Thus we define this function in pairs.
1978 */
1979
1980 static inline midgard_alu_op
1981 mir_contrapositive(midgard_alu_op op)
1982 {
1983 switch (op) {
1984 case midgard_alu_op_flt:
1985 return midgard_alu_op_fle;
1986 case midgard_alu_op_fle:
1987 return midgard_alu_op_flt;
1988
1989 case midgard_alu_op_ilt:
1990 return midgard_alu_op_ile;
1991 case midgard_alu_op_ile:
1992 return midgard_alu_op_ilt;
1993
1994 default:
1995 unreachable("No known contrapositive");
1996 }
1997 }
1998
1999 /* Midgard supports two types of constants, embedded constants (128-bit) and
2000 * inline constants (16-bit). Sometimes, especially with scalar ops, embedded
2001 * constants can be demoted to inline constants, for space savings and
2002 * sometimes a performance boost */
2003
2004 static void
2005 embedded_to_inline_constant(compiler_context *ctx, midgard_block *block)
2006 {
2007 mir_foreach_instr_in_block(block, ins) {
2008 if (!ins->has_constants) continue;
2009 if (ins->has_inline_constant) continue;
2010
2011 /* Blend constants must not be inlined by definition */
2012 if (ins->has_blend_constant) continue;
2013
2014 /* We can inline 32-bit (sometimes) or 16-bit (usually) */
2015 bool is_16 = ins->alu.reg_mode == midgard_reg_mode_16;
2016 bool is_32 = ins->alu.reg_mode == midgard_reg_mode_32;
2017
2018 if (!(is_16 || is_32))
2019 continue;
2020
2021 /* src1 cannot be an inline constant due to encoding
2022 * restrictions. So, if possible we try to flip the arguments
2023 * in that case */
2024
2025 int op = ins->alu.op;
2026
2027 if (ins->src[0] == SSA_FIXED_REGISTER(REGISTER_CONSTANT)) {
2028 bool flip = alu_opcode_props[op].props & OP_COMMUTES;
2029
2030 switch (op) {
2031 /* Conditionals can be inverted */
2032 case midgard_alu_op_flt:
2033 case midgard_alu_op_ilt:
2034 case midgard_alu_op_fle:
2035 case midgard_alu_op_ile:
2036 ins->alu.op = mir_contrapositive(ins->alu.op);
2037 ins->invert = true;
2038 flip = true;
2039 break;
2040
2041 case midgard_alu_op_fcsel:
2042 case midgard_alu_op_icsel:
2043 DBG("Missed non-commutative flip (%s)\n", alu_opcode_props[op].name);
2044 default:
2045 break;
2046 }
2047
2048 if (flip)
2049 mir_flip(ins);
2050 }
2051
2052 if (ins->src[1] == SSA_FIXED_REGISTER(REGISTER_CONSTANT)) {
2053 /* Extract the source information */
2054
2055 midgard_vector_alu_src *src;
2056 int q = ins->alu.src2;
2057 midgard_vector_alu_src *m = (midgard_vector_alu_src *) &q;
2058 src = m;
2059
2060 /* Component is from the swizzle. Take a nonzero component */
2061 assert(ins->mask);
2062 unsigned first_comp = ffs(ins->mask) - 1;
2063 unsigned component = ins->swizzle[1][first_comp];
2064
2065 /* Scale constant appropriately, if we can legally */
2066 uint16_t scaled_constant = 0;
2067
2068 if (midgard_is_integer_op(op) || is_16) {
2069 unsigned int *iconstants = (unsigned int *) ins->constants;
2070 scaled_constant = (uint16_t) iconstants[component];
2071
2072 /* Constant overflow after resize */
2073 if (scaled_constant != iconstants[component])
2074 continue;
2075 } else {
2076 float *f = (float *) ins->constants;
2077 float original = f[component];
2078 scaled_constant = _mesa_float_to_half(original);
2079
2080 /* Check for loss of precision. If this is
2081 * mediump, we don't care, but for a highp
2082 * shader, we need to pay attention. NIR
2083 * doesn't yet tell us which mode we're in!
2084 * Practically this prevents most constants
2085 * from being inlined, sadly. */
2086
2087 float fp32 = _mesa_half_to_float(scaled_constant);
2088
2089 if (fp32 != original)
2090 continue;
2091 }
2092
2093 /* We don't know how to handle these with a constant */
2094
2095 if (mir_nontrivial_source2_mod_simple(ins) || src->rep_low || src->rep_high) {
2096 DBG("Bailing inline constant...\n");
2097 continue;
2098 }
2099
2100 /* Make sure that the constant is not itself a vector
2101 * by checking if all accessed values are the same. */
2102
2103 uint32_t *cons = ins->constants;
2104 uint32_t value = cons[component];
2105
2106 bool is_vector = false;
2107 unsigned mask = effective_writemask(&ins->alu, ins->mask);
2108
2109 for (unsigned c = 0; c < MIR_VEC_COMPONENTS; ++c) {
2110 /* We only care if this component is actually used */
2111 if (!(mask & (1 << c)))
2112 continue;
2113
2114 uint32_t test = cons[ins->swizzle[1][c]];
2115
2116 if (test != value) {
2117 is_vector = true;
2118 break;
2119 }
2120 }
2121
2122 if (is_vector)
2123 continue;
2124
2125 /* Get rid of the embedded constant */
2126 ins->has_constants = false;
2127 ins->src[1] = ~0;
2128 ins->has_inline_constant = true;
2129 ins->inline_constant = scaled_constant;
2130 }
2131 }
2132 }
2133
2134 /* Dead code elimination for branches at the end of a block - only one branch
2135 * per block is legal semantically */
2136
2137 static void
2138 midgard_opt_cull_dead_branch(compiler_context *ctx, midgard_block *block)
2139 {
2140 bool branched = false;
2141
2142 mir_foreach_instr_in_block_safe(block, ins) {
2143 if (!midgard_is_branch_unit(ins->unit)) continue;
2144
2145 if (branched)
2146 mir_remove_instruction(ins);
2147
2148 branched = true;
2149 }
2150 }
2151
2152 /* fmov.pos is an idiom for fpos. Propoagate the .pos up to the source, so then
2153 * the move can be propagated away entirely */
2154
2155 static bool
2156 mir_compose_float_outmod(midgard_outmod_float *outmod, midgard_outmod_float comp)
2157 {
2158 /* Nothing to do */
2159 if (comp == midgard_outmod_none)
2160 return true;
2161
2162 if (*outmod == midgard_outmod_none) {
2163 *outmod = comp;
2164 return true;
2165 }
2166
2167 /* TODO: Compose rules */
2168 return false;
2169 }
2170
2171 static bool
2172 midgard_opt_pos_propagate(compiler_context *ctx, midgard_block *block)
2173 {
2174 bool progress = false;
2175
2176 mir_foreach_instr_in_block_safe(block, ins) {
2177 if (ins->type != TAG_ALU_4) continue;
2178 if (ins->alu.op != midgard_alu_op_fmov) continue;
2179 if (ins->alu.outmod != midgard_outmod_pos) continue;
2180
2181 /* TODO: Registers? */
2182 unsigned src = ins->src[1];
2183 if (src & IS_REG) continue;
2184
2185 /* There might be a source modifier, too */
2186 if (mir_nontrivial_source2_mod(ins)) continue;
2187
2188 /* Backpropagate the modifier */
2189 mir_foreach_instr_in_block_from_rev(block, v, mir_prev_op(ins)) {
2190 if (v->type != TAG_ALU_4) continue;
2191 if (v->dest != src) continue;
2192
2193 /* Can we even take a float outmod? */
2194 if (midgard_is_integer_out_op(v->alu.op)) continue;
2195
2196 midgard_outmod_float temp = v->alu.outmod;
2197 progress |= mir_compose_float_outmod(&temp, ins->alu.outmod);
2198
2199 /* Throw in the towel.. */
2200 if (!progress) break;
2201
2202 /* Otherwise, transfer the modifier */
2203 v->alu.outmod = temp;
2204 ins->alu.outmod = midgard_outmod_none;
2205
2206 break;
2207 }
2208 }
2209
2210 return progress;
2211 }
2212
2213 static void
2214 emit_fragment_epilogue(compiler_context *ctx)
2215 {
2216 /* Just emit the last chunk with the branch */
2217 EMIT(alu_br_compact_cond, midgard_jmp_writeout_op_writeout, TAG_ALU_4, ~0, midgard_condition_always);
2218 }
2219
2220 static midgard_block *
2221 emit_block(compiler_context *ctx, nir_block *block)
2222 {
2223 midgard_block *this_block = ctx->after_block;
2224 ctx->after_block = NULL;
2225
2226 if (!this_block)
2227 this_block = create_empty_block(ctx);
2228
2229 list_addtail(&this_block->link, &ctx->blocks);
2230
2231 this_block->is_scheduled = false;
2232 ++ctx->block_count;
2233
2234 /* Set up current block */
2235 list_inithead(&this_block->instructions);
2236 ctx->current_block = this_block;
2237
2238 nir_foreach_instr(instr, block) {
2239 emit_instr(ctx, instr);
2240 ++ctx->instruction_count;
2241 }
2242
2243 return this_block;
2244 }
2245
2246 static midgard_block *emit_cf_list(struct compiler_context *ctx, struct exec_list *list);
2247
2248 static void
2249 emit_if(struct compiler_context *ctx, nir_if *nif)
2250 {
2251 midgard_block *before_block = ctx->current_block;
2252
2253 /* Speculatively emit the branch, but we can't fill it in until later */
2254 EMIT(branch, true, true);
2255 midgard_instruction *then_branch = mir_last_in_block(ctx->current_block);
2256 then_branch->src[0] = nir_src_index(ctx, &nif->condition);
2257
2258 /* Emit the two subblocks. */
2259 midgard_block *then_block = emit_cf_list(ctx, &nif->then_list);
2260 midgard_block *end_then_block = ctx->current_block;
2261
2262 /* Emit a jump from the end of the then block to the end of the else */
2263 EMIT(branch, false, false);
2264 midgard_instruction *then_exit = mir_last_in_block(ctx->current_block);
2265
2266 /* Emit second block, and check if it's empty */
2267
2268 int else_idx = ctx->block_count;
2269 int count_in = ctx->instruction_count;
2270 midgard_block *else_block = emit_cf_list(ctx, &nif->else_list);
2271 midgard_block *end_else_block = ctx->current_block;
2272 int after_else_idx = ctx->block_count;
2273
2274 /* Now that we have the subblocks emitted, fix up the branches */
2275
2276 assert(then_block);
2277 assert(else_block);
2278
2279 if (ctx->instruction_count == count_in) {
2280 /* The else block is empty, so don't emit an exit jump */
2281 mir_remove_instruction(then_exit);
2282 then_branch->branch.target_block = after_else_idx;
2283 } else {
2284 then_branch->branch.target_block = else_idx;
2285 then_exit->branch.target_block = after_else_idx;
2286 }
2287
2288 /* Wire up the successors */
2289
2290 ctx->after_block = create_empty_block(ctx);
2291
2292 midgard_block_add_successor(before_block, then_block);
2293 midgard_block_add_successor(before_block, else_block);
2294
2295 midgard_block_add_successor(end_then_block, ctx->after_block);
2296 midgard_block_add_successor(end_else_block, ctx->after_block);
2297 }
2298
2299 static void
2300 emit_loop(struct compiler_context *ctx, nir_loop *nloop)
2301 {
2302 /* Remember where we are */
2303 midgard_block *start_block = ctx->current_block;
2304
2305 /* Allocate a loop number, growing the current inner loop depth */
2306 int loop_idx = ++ctx->current_loop_depth;
2307
2308 /* Get index from before the body so we can loop back later */
2309 int start_idx = ctx->block_count;
2310
2311 /* Emit the body itself */
2312 midgard_block *loop_block = emit_cf_list(ctx, &nloop->body);
2313
2314 /* Branch back to loop back */
2315 struct midgard_instruction br_back = v_branch(false, false);
2316 br_back.branch.target_block = start_idx;
2317 emit_mir_instruction(ctx, br_back);
2318
2319 /* Mark down that branch in the graph. */
2320 midgard_block_add_successor(start_block, loop_block);
2321 midgard_block_add_successor(ctx->current_block, loop_block);
2322
2323 /* Find the index of the block about to follow us (note: we don't add
2324 * one; blocks are 0-indexed so we get a fencepost problem) */
2325 int break_block_idx = ctx->block_count;
2326
2327 /* Fix up the break statements we emitted to point to the right place,
2328 * now that we can allocate a block number for them */
2329 ctx->after_block = create_empty_block(ctx);
2330
2331 list_for_each_entry_from(struct midgard_block, block, start_block, &ctx->blocks, link) {
2332 mir_foreach_instr_in_block(block, ins) {
2333 if (ins->type != TAG_ALU_4) continue;
2334 if (!ins->compact_branch) continue;
2335 if (ins->prepacked_branch) continue;
2336
2337 /* We found a branch -- check the type to see if we need to do anything */
2338 if (ins->branch.target_type != TARGET_BREAK) continue;
2339
2340 /* It's a break! Check if it's our break */
2341 if (ins->branch.target_break != loop_idx) continue;
2342
2343 /* Okay, cool, we're breaking out of this loop.
2344 * Rewrite from a break to a goto */
2345
2346 ins->branch.target_type = TARGET_GOTO;
2347 ins->branch.target_block = break_block_idx;
2348
2349 midgard_block_add_successor(block, ctx->after_block);
2350 }
2351 }
2352
2353 /* Now that we've finished emitting the loop, free up the depth again
2354 * so we play nice with recursion amid nested loops */
2355 --ctx->current_loop_depth;
2356
2357 /* Dump loop stats */
2358 ++ctx->loop_count;
2359 }
2360
2361 static midgard_block *
2362 emit_cf_list(struct compiler_context *ctx, struct exec_list *list)
2363 {
2364 midgard_block *start_block = NULL;
2365
2366 foreach_list_typed(nir_cf_node, node, node, list) {
2367 switch (node->type) {
2368 case nir_cf_node_block: {
2369 midgard_block *block = emit_block(ctx, nir_cf_node_as_block(node));
2370
2371 if (!start_block)
2372 start_block = block;
2373
2374 break;
2375 }
2376
2377 case nir_cf_node_if:
2378 emit_if(ctx, nir_cf_node_as_if(node));
2379 break;
2380
2381 case nir_cf_node_loop:
2382 emit_loop(ctx, nir_cf_node_as_loop(node));
2383 break;
2384
2385 case nir_cf_node_function:
2386 assert(0);
2387 break;
2388 }
2389 }
2390
2391 return start_block;
2392 }
2393
2394 /* Due to lookahead, we need to report the first tag executed in the command
2395 * stream and in branch targets. An initial block might be empty, so iterate
2396 * until we find one that 'works' */
2397
2398 static unsigned
2399 midgard_get_first_tag_from_block(compiler_context *ctx, unsigned block_idx)
2400 {
2401 midgard_block *initial_block = mir_get_block(ctx, block_idx);
2402
2403 unsigned first_tag = 0;
2404
2405 mir_foreach_block_from(ctx, initial_block, v) {
2406 if (v->quadword_count) {
2407 midgard_bundle *initial_bundle =
2408 util_dynarray_element(&v->bundles, midgard_bundle, 0);
2409
2410 first_tag = initial_bundle->tag;
2411 break;
2412 }
2413 }
2414
2415 return first_tag;
2416 }
2417
2418 int
2419 midgard_compile_shader_nir(nir_shader *nir, midgard_program *program, bool is_blend, unsigned gpu_id)
2420 {
2421 struct util_dynarray *compiled = &program->compiled;
2422
2423 midgard_debug = debug_get_option_midgard_debug();
2424
2425 /* TODO: Bound against what? */
2426 compiler_context *ctx = rzalloc(NULL, compiler_context);
2427
2428 ctx->nir = nir;
2429 ctx->stage = nir->info.stage;
2430 ctx->is_blend = is_blend;
2431 ctx->alpha_ref = program->alpha_ref;
2432 ctx->quirks = midgard_get_quirks(gpu_id);
2433
2434 /* Start off with a safe cutoff, allowing usage of all 16 work
2435 * registers. Later, we'll promote uniform reads to uniform registers
2436 * if we determine it is beneficial to do so */
2437 ctx->uniform_cutoff = 8;
2438
2439 /* Initialize at a global (not block) level hash tables */
2440
2441 ctx->ssa_constants = _mesa_hash_table_u64_create(NULL);
2442 ctx->hash_to_temp = _mesa_hash_table_u64_create(NULL);
2443 ctx->sysval_to_id = _mesa_hash_table_u64_create(NULL);
2444
2445 /* Record the varying mapping for the command stream's bookkeeping */
2446
2447 struct exec_list *varyings =
2448 ctx->stage == MESA_SHADER_VERTEX ? &nir->outputs : &nir->inputs;
2449
2450 unsigned max_varying = 0;
2451 nir_foreach_variable(var, varyings) {
2452 unsigned loc = var->data.driver_location;
2453 unsigned sz = glsl_type_size(var->type, FALSE);
2454
2455 for (int c = 0; c < sz; ++c) {
2456 program->varyings[loc + c] = var->data.location + c;
2457 max_varying = MAX2(max_varying, loc + c);
2458 }
2459 }
2460
2461 /* Lower gl_Position pre-optimisation, but after lowering vars to ssa
2462 * (so we don't accidentally duplicate the epilogue since mesa/st has
2463 * messed with our I/O quite a bit already) */
2464
2465 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
2466
2467 if (ctx->stage == MESA_SHADER_VERTEX) {
2468 NIR_PASS_V(nir, nir_lower_viewport_transform);
2469 NIR_PASS_V(nir, nir_lower_point_size, 1.0, 1024.0);
2470 }
2471
2472 NIR_PASS_V(nir, nir_lower_var_copies);
2473 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
2474 NIR_PASS_V(nir, nir_split_var_copies);
2475 NIR_PASS_V(nir, nir_lower_var_copies);
2476 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
2477 NIR_PASS_V(nir, nir_lower_var_copies);
2478 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
2479
2480 NIR_PASS_V(nir, nir_lower_io, nir_var_all, glsl_type_size, 0);
2481
2482 /* Optimisation passes */
2483
2484 optimise_nir(nir);
2485
2486 if (midgard_debug & MIDGARD_DBG_SHADERS) {
2487 nir_print_shader(nir, stdout);
2488 }
2489
2490 /* Assign sysvals and counts, now that we're sure
2491 * (post-optimisation) */
2492
2493 midgard_nir_assign_sysvals(ctx, nir);
2494
2495 program->uniform_count = nir->num_uniforms;
2496 program->sysval_count = ctx->sysval_count;
2497 memcpy(program->sysvals, ctx->sysvals, sizeof(ctx->sysvals[0]) * ctx->sysval_count);
2498
2499 nir_foreach_function(func, nir) {
2500 if (!func->impl)
2501 continue;
2502
2503 list_inithead(&ctx->blocks);
2504 ctx->block_count = 0;
2505 ctx->func = func;
2506
2507 emit_cf_list(ctx, &func->impl->body);
2508
2509 /* Emit empty exit block with successor */
2510
2511 struct midgard_block *semi_end = ctx->current_block;
2512
2513 struct midgard_block *end =
2514 emit_block(ctx, func->impl->end_block);
2515
2516 if (ctx->stage == MESA_SHADER_FRAGMENT)
2517 emit_fragment_epilogue(ctx);
2518
2519 midgard_block_add_successor(semi_end, end);
2520
2521 break; /* TODO: Multi-function shaders */
2522 }
2523
2524 util_dynarray_init(compiled, NULL);
2525
2526 /* Per-block lowering before opts */
2527
2528 mir_foreach_block(ctx, block) {
2529 inline_alu_constants(ctx, block);
2530 midgard_opt_promote_fmov(ctx, block);
2531 embedded_to_inline_constant(ctx, block);
2532 }
2533 /* MIR-level optimizations */
2534
2535 bool progress = false;
2536
2537 do {
2538 progress = false;
2539
2540 mir_foreach_block(ctx, block) {
2541 progress |= midgard_opt_pos_propagate(ctx, block);
2542 progress |= midgard_opt_copy_prop(ctx, block);
2543 progress |= midgard_opt_dead_code_eliminate(ctx, block);
2544 progress |= midgard_opt_combine_projection(ctx, block);
2545 progress |= midgard_opt_varying_projection(ctx, block);
2546 progress |= midgard_opt_not_propagate(ctx, block);
2547 progress |= midgard_opt_fuse_src_invert(ctx, block);
2548 progress |= midgard_opt_fuse_dest_invert(ctx, block);
2549 progress |= midgard_opt_csel_invert(ctx, block);
2550 }
2551 } while (progress);
2552
2553 mir_foreach_block(ctx, block) {
2554 midgard_lower_invert(ctx, block);
2555 midgard_lower_derivatives(ctx, block);
2556 }
2557
2558 /* Nested control-flow can result in dead branches at the end of the
2559 * block. This messes with our analysis and is just dead code, so cull
2560 * them */
2561 mir_foreach_block(ctx, block) {
2562 midgard_opt_cull_dead_branch(ctx, block);
2563 }
2564
2565 /* Ensure we were lowered */
2566 mir_foreach_instr_global(ctx, ins) {
2567 assert(!ins->invert);
2568 }
2569
2570 /* Schedule! */
2571 schedule_program(ctx);
2572
2573 /* Now that all the bundles are scheduled and we can calculate block
2574 * sizes, emit actual branch instructions rather than placeholders */
2575
2576 int br_block_idx = 0;
2577
2578 mir_foreach_block(ctx, block) {
2579 util_dynarray_foreach(&block->bundles, midgard_bundle, bundle) {
2580 for (int c = 0; c < bundle->instruction_count; ++c) {
2581 midgard_instruction *ins = bundle->instructions[c];
2582
2583 if (!midgard_is_branch_unit(ins->unit)) continue;
2584
2585 if (ins->prepacked_branch) continue;
2586
2587 /* Parse some basic branch info */
2588 bool is_compact = ins->unit == ALU_ENAB_BR_COMPACT;
2589 bool is_conditional = ins->branch.conditional;
2590 bool is_inverted = ins->branch.invert_conditional;
2591 bool is_discard = ins->branch.target_type == TARGET_DISCARD;
2592
2593 /* Determine the block we're jumping to */
2594 int target_number = ins->branch.target_block;
2595
2596 /* Report the destination tag */
2597 int dest_tag = is_discard ? 0 : midgard_get_first_tag_from_block(ctx, target_number);
2598
2599 /* Count up the number of quadwords we're
2600 * jumping over = number of quadwords until
2601 * (br_block_idx, target_number) */
2602
2603 int quadword_offset = 0;
2604
2605 if (is_discard) {
2606 /* Ignored */
2607 } else if (target_number > br_block_idx) {
2608 /* Jump forward */
2609
2610 for (int idx = br_block_idx + 1; idx < target_number; ++idx) {
2611 midgard_block *blk = mir_get_block(ctx, idx);
2612 assert(blk);
2613
2614 quadword_offset += blk->quadword_count;
2615 }
2616 } else {
2617 /* Jump backwards */
2618
2619 for (int idx = br_block_idx; idx >= target_number; --idx) {
2620 midgard_block *blk = mir_get_block(ctx, idx);
2621 assert(blk);
2622
2623 quadword_offset -= blk->quadword_count;
2624 }
2625 }
2626
2627 /* Unconditional extended branches (far jumps)
2628 * have issues, so we always use a conditional
2629 * branch, setting the condition to always for
2630 * unconditional. For compact unconditional
2631 * branches, cond isn't used so it doesn't
2632 * matter what we pick. */
2633
2634 midgard_condition cond =
2635 !is_conditional ? midgard_condition_always :
2636 is_inverted ? midgard_condition_false :
2637 midgard_condition_true;
2638
2639 midgard_jmp_writeout_op op =
2640 is_discard ? midgard_jmp_writeout_op_discard :
2641 (is_compact && !is_conditional) ? midgard_jmp_writeout_op_branch_uncond :
2642 midgard_jmp_writeout_op_branch_cond;
2643
2644 if (!is_compact) {
2645 midgard_branch_extended branch =
2646 midgard_create_branch_extended(
2647 cond, op,
2648 dest_tag,
2649 quadword_offset);
2650
2651 memcpy(&ins->branch_extended, &branch, sizeof(branch));
2652 } else if (is_conditional || is_discard) {
2653 midgard_branch_cond branch = {
2654 .op = op,
2655 .dest_tag = dest_tag,
2656 .offset = quadword_offset,
2657 .cond = cond
2658 };
2659
2660 assert(branch.offset == quadword_offset);
2661
2662 memcpy(&ins->br_compact, &branch, sizeof(branch));
2663 } else {
2664 assert(op == midgard_jmp_writeout_op_branch_uncond);
2665
2666 midgard_branch_uncond branch = {
2667 .op = op,
2668 .dest_tag = dest_tag,
2669 .offset = quadword_offset,
2670 .unknown = 1
2671 };
2672
2673 assert(branch.offset == quadword_offset);
2674
2675 memcpy(&ins->br_compact, &branch, sizeof(branch));
2676 }
2677 }
2678 }
2679
2680 ++br_block_idx;
2681 }
2682
2683 /* Emit flat binary from the instruction arrays. Iterate each block in
2684 * sequence. Save instruction boundaries such that lookahead tags can
2685 * be assigned easily */
2686
2687 /* Cache _all_ bundles in source order for lookahead across failed branches */
2688
2689 int bundle_count = 0;
2690 mir_foreach_block(ctx, block) {
2691 bundle_count += block->bundles.size / sizeof(midgard_bundle);
2692 }
2693 midgard_bundle **source_order_bundles = malloc(sizeof(midgard_bundle *) * bundle_count);
2694 int bundle_idx = 0;
2695 mir_foreach_block(ctx, block) {
2696 util_dynarray_foreach(&block->bundles, midgard_bundle, bundle) {
2697 source_order_bundles[bundle_idx++] = bundle;
2698 }
2699 }
2700
2701 int current_bundle = 0;
2702
2703 /* Midgard prefetches instruction types, so during emission we
2704 * need to lookahead. Unless this is the last instruction, in
2705 * which we return 1. Or if this is the second to last and the
2706 * last is an ALU, then it's also 1... */
2707
2708 mir_foreach_block(ctx, block) {
2709 mir_foreach_bundle_in_block(block, bundle) {
2710 int lookahead = 1;
2711
2712 if (current_bundle + 1 < bundle_count) {
2713 uint8_t next = source_order_bundles[current_bundle + 1]->tag;
2714
2715 if (!(current_bundle + 2 < bundle_count) && IS_ALU(next)) {
2716 lookahead = 1;
2717 } else {
2718 lookahead = next;
2719 }
2720 }
2721
2722 emit_binary_bundle(ctx, bundle, compiled, lookahead);
2723 ++current_bundle;
2724 }
2725
2726 /* TODO: Free deeper */
2727 //util_dynarray_fini(&block->instructions);
2728 }
2729
2730 free(source_order_bundles);
2731
2732 /* Report the very first tag executed */
2733 program->first_tag = midgard_get_first_tag_from_block(ctx, 0);
2734
2735 /* Deal with off-by-one related to the fencepost problem */
2736 program->work_register_count = ctx->work_registers + 1;
2737 program->uniform_cutoff = ctx->uniform_cutoff;
2738
2739 program->blend_patch_offset = ctx->blend_constant_offset;
2740 program->tls_size = ctx->tls_size;
2741
2742 if (midgard_debug & MIDGARD_DBG_SHADERS)
2743 disassemble_midgard(program->compiled.data, program->compiled.size, gpu_id, ctx->stage);
2744
2745 if (midgard_debug & MIDGARD_DBG_SHADERDB) {
2746 unsigned nr_bundles = 0, nr_ins = 0;
2747
2748 /* Count instructions and bundles */
2749
2750 mir_foreach_block(ctx, block) {
2751 nr_bundles += util_dynarray_num_elements(
2752 &block->bundles, midgard_bundle);
2753
2754 mir_foreach_bundle_in_block(block, bun)
2755 nr_ins += bun->instruction_count;
2756 }
2757
2758 /* Calculate thread count. There are certain cutoffs by
2759 * register count for thread count */
2760
2761 unsigned nr_registers = program->work_register_count;
2762
2763 unsigned nr_threads =
2764 (nr_registers <= 4) ? 4 :
2765 (nr_registers <= 8) ? 2 :
2766 1;
2767
2768 /* Dump stats */
2769
2770 fprintf(stderr, "shader%d - %s shader: "
2771 "%u inst, %u bundles, %u quadwords, "
2772 "%u registers, %u threads, %u loops, "
2773 "%u:%u spills:fills\n",
2774 SHADER_DB_COUNT++,
2775 gl_shader_stage_name(ctx->stage),
2776 nr_ins, nr_bundles, ctx->quadword_count,
2777 nr_registers, nr_threads,
2778 ctx->loop_count,
2779 ctx->spills, ctx->fills);
2780 }
2781
2782 ralloc_free(ctx);
2783
2784 return 0;
2785 }