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