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