panfrost/midgard: Fix crash with unused SSA values
[mesa.git] / src / gallium / drivers / 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_debug.h"
40 #include "util/u_dynarray.h"
41 #include "util/list.h"
42 #include "main/mtypes.h"
43
44 #include "midgard.h"
45 #include "midgard_nir.h"
46 #include "midgard_compile.h"
47 #include "midgard_ops.h"
48 #include "helpers.h"
49 #include "compiler.h"
50
51 #include "disassemble.h"
52
53 static const struct debug_named_value debug_options[] = {
54 {"msgs", MIDGARD_DBG_MSGS, "Print debug messages"},
55 {"shaders", MIDGARD_DBG_SHADERS, "Dump shaders in NIR and MIR"},
56 DEBUG_NAMED_VALUE_END
57 };
58
59 DEBUG_GET_ONCE_FLAGS_OPTION(midgard_debug, "MIDGARD_MESA_DEBUG", debug_options, 0)
60
61 int midgard_debug = 0;
62
63 #define DBG(fmt, ...) \
64 do { if (midgard_debug & MIDGARD_DBG_MSGS) \
65 fprintf(stderr, "%s:%d: "fmt, \
66 __FUNCTION__, __LINE__, ##__VA_ARGS__); } while (0)
67
68 static bool
69 midgard_is_branch_unit(unsigned unit)
70 {
71 return (unit == ALU_ENAB_BRANCH) || (unit == ALU_ENAB_BR_COMPACT);
72 }
73
74 static void
75 midgard_block_add_successor(midgard_block *block, midgard_block *successor)
76 {
77 block->successors[block->nr_successors++] = successor;
78 assert(block->nr_successors <= ARRAY_SIZE(block->successors));
79 }
80
81 /* Helpers to generate midgard_instruction's using macro magic, since every
82 * driver seems to do it that way */
83
84 #define EMIT(op, ...) emit_mir_instruction(ctx, v_##op(__VA_ARGS__));
85 #define SWIZZLE_XYZW SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_W)
86
87 #define M_LOAD_STORE(name, rname, uname) \
88 static midgard_instruction m_##name(unsigned ssa, unsigned address) { \
89 midgard_instruction i = { \
90 .type = TAG_LOAD_STORE_4, \
91 .ssa_args = { \
92 .rname = ssa, \
93 .uname = -1, \
94 .src1 = -1 \
95 }, \
96 .load_store = { \
97 .op = midgard_op_##name, \
98 .mask = 0xF, \
99 .swizzle = SWIZZLE_XYZW, \
100 .address = address \
101 } \
102 }; \
103 \
104 return i; \
105 }
106
107 #define M_LOAD(name) M_LOAD_STORE(name, dest, src0)
108 #define M_STORE(name) M_LOAD_STORE(name, src0, dest)
109
110 /* Inputs a NIR ALU source, with modifiers attached if necessary, and outputs
111 * the corresponding Midgard source */
112
113 static midgard_vector_alu_src
114 vector_alu_modifiers(nir_alu_src *src, bool is_int)
115 {
116 if (!src) return blank_alu_src;
117
118 midgard_vector_alu_src alu_src = {
119 .rep_low = 0,
120 .rep_high = 0,
121 .half = 0, /* TODO */
122 .swizzle = SWIZZLE_FROM_ARRAY(src->swizzle)
123 };
124
125 if (is_int) {
126 /* TODO: sign-extend/zero-extend */
127 alu_src.mod = midgard_int_normal;
128
129 /* These should have been lowered away */
130 assert(!(src->abs || src->negate));
131 } else {
132 alu_src.mod = (src->abs << 0) | (src->negate << 1);
133 }
134
135 return alu_src;
136 }
137
138 /* load/store instructions have both 32-bit and 16-bit variants, depending on
139 * whether we are using vectors composed of highp or mediump. At the moment, we
140 * don't support half-floats -- this requires changes in other parts of the
141 * compiler -- therefore the 16-bit versions are commented out. */
142
143 //M_LOAD(ld_attr_16);
144 M_LOAD(ld_attr_32);
145 //M_LOAD(ld_vary_16);
146 M_LOAD(ld_vary_32);
147 //M_LOAD(ld_uniform_16);
148 M_LOAD(ld_uniform_32);
149 M_LOAD(ld_color_buffer_8);
150 //M_STORE(st_vary_16);
151 M_STORE(st_vary_32);
152 M_STORE(st_cubemap_coords);
153
154 static midgard_instruction
155 v_alu_br_compact_cond(midgard_jmp_writeout_op op, unsigned tag, signed offset, unsigned cond)
156 {
157 midgard_branch_cond branch = {
158 .op = op,
159 .dest_tag = tag,
160 .offset = offset,
161 .cond = cond
162 };
163
164 uint16_t compact;
165 memcpy(&compact, &branch, sizeof(branch));
166
167 midgard_instruction ins = {
168 .type = TAG_ALU_4,
169 .unit = ALU_ENAB_BR_COMPACT,
170 .prepacked_branch = true,
171 .compact_branch = true,
172 .br_compact = compact
173 };
174
175 if (op == midgard_jmp_writeout_op_writeout)
176 ins.writeout = true;
177
178 return ins;
179 }
180
181 static midgard_instruction
182 v_branch(bool conditional, bool invert)
183 {
184 midgard_instruction ins = {
185 .type = TAG_ALU_4,
186 .unit = ALU_ENAB_BRANCH,
187 .compact_branch = true,
188 .branch = {
189 .conditional = conditional,
190 .invert_conditional = invert
191 }
192 };
193
194 return ins;
195 }
196
197 static midgard_branch_extended
198 midgard_create_branch_extended( midgard_condition cond,
199 midgard_jmp_writeout_op op,
200 unsigned dest_tag,
201 signed quadword_offset)
202 {
203 /* For unclear reasons, the condition code is repeated 8 times */
204 uint16_t duplicated_cond =
205 (cond << 14) |
206 (cond << 12) |
207 (cond << 10) |
208 (cond << 8) |
209 (cond << 6) |
210 (cond << 4) |
211 (cond << 2) |
212 (cond << 0);
213
214 midgard_branch_extended branch = {
215 .op = op,
216 .dest_tag = dest_tag,
217 .offset = quadword_offset,
218 .cond = duplicated_cond
219 };
220
221 return branch;
222 }
223
224 static void
225 attach_constants(compiler_context *ctx, midgard_instruction *ins, void *constants, int name)
226 {
227 ins->has_constants = true;
228 memcpy(&ins->constants, constants, 16);
229 }
230
231 static int
232 glsl_type_size(const struct glsl_type *type, bool bindless)
233 {
234 return glsl_count_attribute_slots(type, false);
235 }
236
237 /* Lower fdot2 to a vector multiplication followed by channel addition */
238 static void
239 midgard_nir_lower_fdot2_body(nir_builder *b, nir_alu_instr *alu)
240 {
241 if (alu->op != nir_op_fdot2)
242 return;
243
244 b->cursor = nir_before_instr(&alu->instr);
245
246 nir_ssa_def *src0 = nir_ssa_for_alu_src(b, alu, 0);
247 nir_ssa_def *src1 = nir_ssa_for_alu_src(b, alu, 1);
248
249 nir_ssa_def *product = nir_fmul(b, src0, src1);
250
251 nir_ssa_def *sum = nir_fadd(b,
252 nir_channel(b, product, 0),
253 nir_channel(b, product, 1));
254
255 /* Replace the fdot2 with this sum */
256 nir_ssa_def_rewrite_uses(&alu->dest.dest.ssa, nir_src_for_ssa(sum));
257 }
258
259 static int
260 midgard_nir_sysval_for_intrinsic(nir_intrinsic_instr *instr)
261 {
262 switch (instr->intrinsic) {
263 case nir_intrinsic_load_viewport_scale:
264 return PAN_SYSVAL_VIEWPORT_SCALE;
265 case nir_intrinsic_load_viewport_offset:
266 return PAN_SYSVAL_VIEWPORT_OFFSET;
267 default:
268 return -1;
269 }
270 }
271
272 static void
273 midgard_nir_assign_sysval_body(compiler_context *ctx, nir_instr *instr)
274 {
275 int sysval = -1;
276
277 if (instr->type == nir_instr_type_intrinsic) {
278 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
279 sysval = midgard_nir_sysval_for_intrinsic(intr);
280 }
281
282 if (sysval < 0)
283 return;
284
285 /* We have a sysval load; check if it's already been assigned */
286
287 if (_mesa_hash_table_u64_search(ctx->sysval_to_id, sysval))
288 return;
289
290 /* It hasn't -- so assign it now! */
291
292 unsigned id = ctx->sysval_count++;
293 _mesa_hash_table_u64_insert(ctx->sysval_to_id, sysval, (void *) ((uintptr_t) id + 1));
294 ctx->sysvals[id] = sysval;
295 }
296
297 static void
298 midgard_nir_assign_sysvals(compiler_context *ctx, nir_shader *shader)
299 {
300 ctx->sysval_count = 0;
301
302 nir_foreach_function(function, shader) {
303 if (!function->impl) continue;
304
305 nir_foreach_block(block, function->impl) {
306 nir_foreach_instr_safe(instr, block) {
307 midgard_nir_assign_sysval_body(ctx, instr);
308 }
309 }
310 }
311 }
312
313 static bool
314 midgard_nir_lower_fdot2(nir_shader *shader)
315 {
316 bool progress = false;
317
318 nir_foreach_function(function, shader) {
319 if (!function->impl) continue;
320
321 nir_builder _b;
322 nir_builder *b = &_b;
323 nir_builder_init(b, function->impl);
324
325 nir_foreach_block(block, function->impl) {
326 nir_foreach_instr_safe(instr, block) {
327 if (instr->type != nir_instr_type_alu) continue;
328
329 nir_alu_instr *alu = nir_instr_as_alu(instr);
330 midgard_nir_lower_fdot2_body(b, alu);
331
332 progress |= true;
333 }
334 }
335
336 nir_metadata_preserve(function->impl, nir_metadata_block_index | nir_metadata_dominance);
337
338 }
339
340 return progress;
341 }
342
343 static void
344 optimise_nir(nir_shader *nir)
345 {
346 bool progress;
347 unsigned lower_flrp =
348 (nir->options->lower_flrp16 ? 16 : 0) |
349 (nir->options->lower_flrp32 ? 32 : 0) |
350 (nir->options->lower_flrp64 ? 64 : 0);
351
352 NIR_PASS(progress, nir, nir_lower_regs_to_ssa);
353 NIR_PASS(progress, nir, midgard_nir_lower_fdot2);
354 NIR_PASS(progress, nir, nir_lower_idiv);
355
356 nir_lower_tex_options lower_tex_options = {
357 .lower_rect = true
358 };
359
360 NIR_PASS(progress, nir, nir_lower_tex, &lower_tex_options);
361
362 do {
363 progress = false;
364
365 NIR_PASS(progress, nir, nir_lower_var_copies);
366 NIR_PASS(progress, nir, nir_lower_vars_to_ssa);
367
368 NIR_PASS(progress, nir, nir_copy_prop);
369 NIR_PASS(progress, nir, nir_opt_dce);
370 NIR_PASS(progress, nir, nir_opt_dead_cf);
371 NIR_PASS(progress, nir, nir_opt_cse);
372 NIR_PASS(progress, nir, nir_opt_peephole_select, 64, false, true);
373 NIR_PASS(progress, nir, nir_opt_algebraic);
374 NIR_PASS(progress, nir, nir_opt_constant_folding);
375
376 if (lower_flrp != 0) {
377 bool lower_flrp_progress = false;
378 NIR_PASS(lower_flrp_progress,
379 nir,
380 nir_lower_flrp,
381 lower_flrp,
382 false /* always_precise */,
383 nir->options->lower_ffma);
384 if (lower_flrp_progress) {
385 NIR_PASS(progress, nir,
386 nir_opt_constant_folding);
387 progress = true;
388 }
389
390 /* Nothing should rematerialize any flrps, so we only
391 * need to do this lowering once.
392 */
393 lower_flrp = 0;
394 }
395
396 NIR_PASS(progress, nir, nir_opt_undef);
397 NIR_PASS(progress, nir, nir_opt_loop_unroll,
398 nir_var_shader_in |
399 nir_var_shader_out |
400 nir_var_function_temp);
401
402 /* TODO: Enable vectorize when merged upstream */
403 // NIR_PASS(progress, nir, nir_opt_vectorize);
404 } while (progress);
405
406 /* Must be run at the end to prevent creation of fsin/fcos ops */
407 NIR_PASS(progress, nir, midgard_nir_scale_trig);
408
409 do {
410 progress = false;
411
412 NIR_PASS(progress, nir, nir_opt_dce);
413 NIR_PASS(progress, nir, nir_opt_algebraic);
414 NIR_PASS(progress, nir, nir_opt_constant_folding);
415 NIR_PASS(progress, nir, nir_copy_prop);
416 } while (progress);
417
418 NIR_PASS(progress, nir, nir_opt_algebraic_late);
419
420 /* We implement booleans as 32-bit 0/~0 */
421 NIR_PASS(progress, nir, nir_lower_bool_to_int32);
422
423 /* Now that booleans are lowered, we can run out late opts */
424 NIR_PASS(progress, nir, midgard_nir_lower_algebraic_late);
425
426 /* Lower mods for float ops only. Integer ops don't support modifiers
427 * (saturate doesn't make sense on integers, neg/abs require dedicated
428 * instructions) */
429
430 NIR_PASS(progress, nir, nir_lower_to_source_mods, nir_lower_float_source_mods);
431 NIR_PASS(progress, nir, nir_copy_prop);
432 NIR_PASS(progress, nir, nir_opt_dce);
433
434 /* Take us out of SSA */
435 NIR_PASS(progress, nir, nir_lower_locals_to_regs);
436 NIR_PASS(progress, nir, nir_convert_from_ssa, true);
437
438 /* We are a vector architecture; write combine where possible */
439 NIR_PASS(progress, nir, nir_move_vec_src_uses_to_dest);
440 NIR_PASS(progress, nir, nir_lower_vec_to_movs);
441
442 NIR_PASS(progress, nir, nir_opt_dce);
443 }
444
445 /* Front-half of aliasing the SSA slots, merely by inserting the flag in the
446 * appropriate hash table. Intentional off-by-one to avoid confusing NULL with
447 * r0. See the comments in compiler_context */
448
449 static void
450 alias_ssa(compiler_context *ctx, int dest, int src)
451 {
452 _mesa_hash_table_u64_insert(ctx->ssa_to_alias, dest + 1, (void *) ((uintptr_t) src + 1));
453 _mesa_set_add(ctx->leftover_ssa_to_alias, (void *) (uintptr_t) (dest + 1));
454 }
455
456 /* ...or undo it, after which the original index will be used (dummy move should be emitted alongside this) */
457
458 static void
459 unalias_ssa(compiler_context *ctx, int dest)
460 {
461 _mesa_hash_table_u64_remove(ctx->ssa_to_alias, dest + 1);
462 /* TODO: Remove from leftover or no? */
463 }
464
465 /* Do not actually emit a load; instead, cache the constant for inlining */
466
467 static void
468 emit_load_const(compiler_context *ctx, nir_load_const_instr *instr)
469 {
470 nir_ssa_def def = instr->def;
471
472 float *v = rzalloc_array(NULL, float, 4);
473 nir_const_load_to_arr(v, instr, f32);
474 _mesa_hash_table_u64_insert(ctx->ssa_constants, def.index + 1, v);
475 }
476
477 static unsigned
478 nir_src_index(compiler_context *ctx, nir_src *src)
479 {
480 if (src->is_ssa)
481 return src->ssa->index;
482 else {
483 assert(!src->reg.indirect);
484 return ctx->func->impl->ssa_alloc + src->reg.reg->index;
485 }
486 }
487
488 static unsigned
489 nir_dest_index(compiler_context *ctx, nir_dest *dst)
490 {
491 if (dst->is_ssa)
492 return dst->ssa.index;
493 else {
494 assert(!dst->reg.indirect);
495 return ctx->func->impl->ssa_alloc + dst->reg.reg->index;
496 }
497 }
498
499 static unsigned
500 nir_alu_src_index(compiler_context *ctx, nir_alu_src *src)
501 {
502 return nir_src_index(ctx, &src->src);
503 }
504
505 static bool
506 nir_is_non_scalar_swizzle(nir_alu_src *src, unsigned nr_components)
507 {
508 unsigned comp = src->swizzle[0];
509
510 for (unsigned c = 1; c < nr_components; ++c) {
511 if (src->swizzle[c] != comp)
512 return true;
513 }
514
515 return false;
516 }
517
518 /* Midgard puts scalar conditionals in r31.w; move an arbitrary source (the
519 * output of a conditional test) into that register */
520
521 static void
522 emit_condition(compiler_context *ctx, nir_src *src, bool for_branch, unsigned component)
523 {
524 int condition = nir_src_index(ctx, src);
525
526 /* Source to swizzle the desired component into w */
527
528 const midgard_vector_alu_src alu_src = {
529 .swizzle = SWIZZLE(component, component, component, component),
530 };
531
532 /* There is no boolean move instruction. Instead, we simulate a move by
533 * ANDing the condition with itself to get it into r31.w */
534
535 midgard_instruction ins = {
536 .type = TAG_ALU_4,
537
538 /* We need to set the conditional as close as possible */
539 .precede_break = true,
540 .unit = for_branch ? UNIT_SMUL : UNIT_SADD,
541
542 .ssa_args = {
543
544 .src0 = condition,
545 .src1 = condition,
546 .dest = SSA_FIXED_REGISTER(31),
547 },
548 .alu = {
549 .op = midgard_alu_op_iand,
550 .outmod = midgard_outmod_int,
551 .reg_mode = midgard_reg_mode_32,
552 .dest_override = midgard_dest_override_none,
553 .mask = (0x3 << 6), /* w */
554 .src1 = vector_alu_srco_unsigned(alu_src),
555 .src2 = vector_alu_srco_unsigned(alu_src)
556 },
557 };
558
559 emit_mir_instruction(ctx, ins);
560 }
561
562 /* Or, for mixed conditions (with csel_v), here's a vector version using all of
563 * r31 instead */
564
565 static void
566 emit_condition_mixed(compiler_context *ctx, nir_alu_src *src, unsigned nr_comp)
567 {
568 int condition = nir_src_index(ctx, &src->src);
569
570 /* Source to swizzle the desired component into w */
571
572 const midgard_vector_alu_src alu_src = {
573 .swizzle = SWIZZLE_FROM_ARRAY(src->swizzle),
574 };
575
576 /* There is no boolean move instruction. Instead, we simulate a move by
577 * ANDing the condition with itself to get it into r31.w */
578
579 midgard_instruction ins = {
580 .type = TAG_ALU_4,
581 .precede_break = true,
582 .ssa_args = {
583 .src0 = condition,
584 .src1 = condition,
585 .dest = SSA_FIXED_REGISTER(31),
586 },
587 .alu = {
588 .op = midgard_alu_op_iand,
589 .outmod = midgard_outmod_int,
590 .reg_mode = midgard_reg_mode_32,
591 .dest_override = midgard_dest_override_none,
592 .mask = expand_writemask((1 << nr_comp) - 1),
593 .src1 = vector_alu_srco_unsigned(alu_src),
594 .src2 = vector_alu_srco_unsigned(alu_src)
595 },
596 };
597
598 emit_mir_instruction(ctx, ins);
599 }
600
601
602
603 /* Likewise, indirect offsets are put in r27.w. TODO: Allow componentwise
604 * pinning to eliminate this move in all known cases */
605
606 static void
607 emit_indirect_offset(compiler_context *ctx, nir_src *src)
608 {
609 int offset = nir_src_index(ctx, src);
610
611 midgard_instruction ins = {
612 .type = TAG_ALU_4,
613 .ssa_args = {
614 .src0 = SSA_UNUSED_1,
615 .src1 = offset,
616 .dest = SSA_FIXED_REGISTER(REGISTER_OFFSET),
617 },
618 .alu = {
619 .op = midgard_alu_op_imov,
620 .outmod = midgard_outmod_int,
621 .reg_mode = midgard_reg_mode_32,
622 .dest_override = midgard_dest_override_none,
623 .mask = (0x3 << 6), /* w */
624 .src1 = vector_alu_srco_unsigned(zero_alu_src),
625 .src2 = vector_alu_srco_unsigned(blank_alu_src_xxxx)
626 },
627 };
628
629 emit_mir_instruction(ctx, ins);
630 }
631
632 #define ALU_CASE(nir, _op) \
633 case nir_op_##nir: \
634 op = midgard_alu_op_##_op; \
635 break;
636 static bool
637 nir_is_fzero_constant(nir_src src)
638 {
639 if (!nir_src_is_const(src))
640 return false;
641
642 for (unsigned c = 0; c < nir_src_num_components(src); ++c) {
643 if (nir_src_comp_as_float(src, c) != 0.0)
644 return false;
645 }
646
647 return true;
648 }
649
650 static void
651 emit_alu(compiler_context *ctx, nir_alu_instr *instr)
652 {
653 bool is_ssa = instr->dest.dest.is_ssa;
654
655 unsigned dest = nir_dest_index(ctx, &instr->dest.dest);
656 unsigned nr_components = is_ssa ? instr->dest.dest.ssa.num_components : instr->dest.dest.reg.reg->num_components;
657 unsigned nr_inputs = nir_op_infos[instr->op].num_inputs;
658
659 /* Most Midgard ALU ops have a 1:1 correspondance to NIR ops; these are
660 * supported. A few do not and are commented for now. Also, there are a
661 * number of NIR ops which Midgard does not support and need to be
662 * lowered, also TODO. This switch block emits the opcode and calling
663 * convention of the Midgard instruction; actual packing is done in
664 * emit_alu below */
665
666 unsigned op;
667
668 switch (instr->op) {
669 ALU_CASE(fadd, fadd);
670 ALU_CASE(fmul, fmul);
671 ALU_CASE(fmin, fmin);
672 ALU_CASE(fmax, fmax);
673 ALU_CASE(imin, imin);
674 ALU_CASE(imax, imax);
675 ALU_CASE(umin, umin);
676 ALU_CASE(umax, umax);
677 ALU_CASE(ffloor, ffloor);
678 ALU_CASE(fround_even, froundeven);
679 ALU_CASE(ftrunc, ftrunc);
680 ALU_CASE(fceil, fceil);
681 ALU_CASE(fdot3, fdot3);
682 ALU_CASE(fdot4, fdot4);
683 ALU_CASE(iadd, iadd);
684 ALU_CASE(isub, isub);
685 ALU_CASE(imul, imul);
686
687 /* Zero shoved as second-arg */
688 ALU_CASE(iabs, iabsdiff);
689
690 ALU_CASE(mov, imov);
691
692 ALU_CASE(feq32, feq);
693 ALU_CASE(fne32, fne);
694 ALU_CASE(flt32, flt);
695 ALU_CASE(ieq32, ieq);
696 ALU_CASE(ine32, ine);
697 ALU_CASE(ilt32, ilt);
698 ALU_CASE(ult32, ult);
699
700 /* We don't have a native b2f32 instruction. Instead, like many
701 * GPUs, we exploit booleans as 0/~0 for false/true, and
702 * correspondingly AND
703 * by 1.0 to do the type conversion. For the moment, prime us
704 * to emit:
705 *
706 * iand [whatever], #0
707 *
708 * At the end of emit_alu (as MIR), we'll fix-up the constant
709 */
710
711 ALU_CASE(b2f32, iand);
712 ALU_CASE(b2i32, iand);
713
714 /* Likewise, we don't have a dedicated f2b32 instruction, but
715 * we can do a "not equal to 0.0" test. */
716
717 ALU_CASE(f2b32, fne);
718 ALU_CASE(i2b32, ine);
719
720 ALU_CASE(frcp, frcp);
721 ALU_CASE(frsq, frsqrt);
722 ALU_CASE(fsqrt, fsqrt);
723 ALU_CASE(fexp2, fexp2);
724 ALU_CASE(flog2, flog2);
725
726 ALU_CASE(f2i32, f2i);
727 ALU_CASE(f2u32, f2u);
728 ALU_CASE(i2f32, i2f);
729 ALU_CASE(u2f32, u2f);
730
731 ALU_CASE(fsin, fsin);
732 ALU_CASE(fcos, fcos);
733
734 ALU_CASE(iand, iand);
735 ALU_CASE(ior, ior);
736 ALU_CASE(ixor, ixor);
737 ALU_CASE(inot, inand);
738 ALU_CASE(ishl, ishl);
739 ALU_CASE(ishr, iasr);
740 ALU_CASE(ushr, ilsr);
741
742 ALU_CASE(b32all_fequal2, fball_eq);
743 ALU_CASE(b32all_fequal3, fball_eq);
744 ALU_CASE(b32all_fequal4, fball_eq);
745
746 ALU_CASE(b32any_fnequal2, fbany_neq);
747 ALU_CASE(b32any_fnequal3, fbany_neq);
748 ALU_CASE(b32any_fnequal4, fbany_neq);
749
750 ALU_CASE(b32all_iequal2, iball_eq);
751 ALU_CASE(b32all_iequal3, iball_eq);
752 ALU_CASE(b32all_iequal4, iball_eq);
753
754 ALU_CASE(b32any_inequal2, ibany_neq);
755 ALU_CASE(b32any_inequal3, ibany_neq);
756 ALU_CASE(b32any_inequal4, ibany_neq);
757
758 /* Source mods will be shoved in later */
759 ALU_CASE(fabs, fmov);
760 ALU_CASE(fneg, fmov);
761 ALU_CASE(fsat, fmov);
762
763 /* For greater-or-equal, we lower to less-or-equal and flip the
764 * arguments */
765
766 case nir_op_fge:
767 case nir_op_fge32:
768 case nir_op_ige32:
769 case nir_op_uge32: {
770 op =
771 instr->op == nir_op_fge ? midgard_alu_op_fle :
772 instr->op == nir_op_fge32 ? midgard_alu_op_fle :
773 instr->op == nir_op_ige32 ? midgard_alu_op_ile :
774 instr->op == nir_op_uge32 ? midgard_alu_op_ule :
775 0;
776
777 /* Swap via temporary */
778 nir_alu_src temp = instr->src[1];
779 instr->src[1] = instr->src[0];
780 instr->src[0] = temp;
781
782 break;
783 }
784
785 case nir_op_b32csel: {
786 /* Midgard features both fcsel and icsel, depending on
787 * the type of the arguments/output. However, as long
788 * as we're careful we can _always_ use icsel and
789 * _never_ need fcsel, since the latter does additional
790 * floating-point-specific processing whereas the
791 * former just moves bits on the wire. It's not obvious
792 * why these are separate opcodes, save for the ability
793 * to do things like sat/pos/abs/neg for free */
794
795 bool mixed = nir_is_non_scalar_swizzle(&instr->src[0], nr_components);
796 op = mixed ? midgard_alu_op_icsel_v : midgard_alu_op_icsel;
797
798 /* csel works as a two-arg in Midgard, since the condition is hardcoded in r31.w */
799 nr_inputs = 2;
800
801 /* Emit the condition into r31 */
802
803 if (mixed)
804 emit_condition_mixed(ctx, &instr->src[0], nr_components);
805 else
806 emit_condition(ctx, &instr->src[0].src, false, instr->src[0].swizzle[0]);
807
808 /* The condition is the first argument; move the other
809 * arguments up one to be a binary instruction for
810 * Midgard */
811
812 memmove(instr->src, instr->src + 1, 2 * sizeof(nir_alu_src));
813 break;
814 }
815
816 default:
817 DBG("Unhandled ALU op %s\n", nir_op_infos[instr->op].name);
818 assert(0);
819 return;
820 }
821
822 /* Midgard can perform certain modifiers on output of an ALU op */
823 midgard_outmod outmod =
824 midgard_is_integer_out_op(op) ? midgard_outmod_int :
825 instr->dest.saturate ? midgard_outmod_sat : midgard_outmod_none;
826
827 if (instr->op == nir_op_fsat)
828 outmod = midgard_outmod_sat;
829
830 /* fmax(a, 0.0) can turn into a .pos modifier as an optimization */
831
832 if (instr->op == nir_op_fmax) {
833 if (nir_is_fzero_constant(instr->src[0].src)) {
834 op = midgard_alu_op_fmov;
835 nr_inputs = 1;
836 outmod = midgard_outmod_pos;
837 instr->src[0] = instr->src[1];
838 } else if (nir_is_fzero_constant(instr->src[1].src)) {
839 op = midgard_alu_op_fmov;
840 nr_inputs = 1;
841 outmod = midgard_outmod_pos;
842 }
843 }
844
845 /* Fetch unit, quirks, etc information */
846 unsigned opcode_props = alu_opcode_props[op].props;
847 bool quirk_flipped_r24 = opcode_props & QUIRK_FLIPPED_R24;
848
849 /* src0 will always exist afaik, but src1 will not for 1-argument
850 * instructions. The latter can only be fetched if the instruction
851 * needs it, or else we may segfault. */
852
853 unsigned src0 = nir_alu_src_index(ctx, &instr->src[0]);
854 unsigned src1 = nr_inputs == 2 ? nir_alu_src_index(ctx, &instr->src[1]) : SSA_UNUSED_0;
855
856 /* Rather than use the instruction generation helpers, we do it
857 * ourselves here to avoid the mess */
858
859 midgard_instruction ins = {
860 .type = TAG_ALU_4,
861 .ssa_args = {
862 .src0 = quirk_flipped_r24 ? SSA_UNUSED_1 : src0,
863 .src1 = quirk_flipped_r24 ? src0 : src1,
864 .dest = dest,
865 }
866 };
867
868 nir_alu_src *nirmods[2] = { NULL };
869
870 if (nr_inputs == 2) {
871 nirmods[0] = &instr->src[0];
872 nirmods[1] = &instr->src[1];
873 } else if (nr_inputs == 1) {
874 nirmods[quirk_flipped_r24] = &instr->src[0];
875 } else {
876 assert(0);
877 }
878
879 /* These were lowered to a move, so apply the corresponding mod */
880
881 if (instr->op == nir_op_fneg || instr->op == nir_op_fabs) {
882 nir_alu_src *s = nirmods[quirk_flipped_r24];
883
884 if (instr->op == nir_op_fneg)
885 s->negate = !s->negate;
886
887 if (instr->op == nir_op_fabs)
888 s->abs = !s->abs;
889 }
890
891 bool is_int = midgard_is_integer_op(op);
892
893 midgard_vector_alu alu = {
894 .op = op,
895 .reg_mode = midgard_reg_mode_32,
896 .dest_override = midgard_dest_override_none,
897 .outmod = outmod,
898
899 /* Writemask only valid for non-SSA NIR */
900 .mask = expand_writemask((1 << nr_components) - 1),
901
902 .src1 = vector_alu_srco_unsigned(vector_alu_modifiers(nirmods[0], is_int)),
903 .src2 = vector_alu_srco_unsigned(vector_alu_modifiers(nirmods[1], is_int)),
904 };
905
906 /* Apply writemask if non-SSA, keeping in mind that we can't write to components that don't exist */
907
908 if (!is_ssa)
909 alu.mask &= expand_writemask(instr->dest.write_mask);
910
911 ins.alu = alu;
912
913 /* Late fixup for emulated instructions */
914
915 if (instr->op == nir_op_b2f32 || instr->op == nir_op_b2i32) {
916 /* Presently, our second argument is an inline #0 constant.
917 * Switch over to an embedded 1.0 constant (that can't fit
918 * inline, since we're 32-bit, not 16-bit like the inline
919 * constants) */
920
921 ins.ssa_args.inline_constant = false;
922 ins.ssa_args.src1 = SSA_FIXED_REGISTER(REGISTER_CONSTANT);
923 ins.has_constants = true;
924
925 if (instr->op == nir_op_b2f32) {
926 ins.constants[0] = 1.0f;
927 } else {
928 /* Type pun it into place */
929 uint32_t one = 0x1;
930 memcpy(&ins.constants[0], &one, sizeof(uint32_t));
931 }
932
933 ins.alu.src2 = vector_alu_srco_unsigned(blank_alu_src_xxxx);
934 } else if (nr_inputs == 1 && !quirk_flipped_r24) {
935 /* Lots of instructions need a 0 plonked in */
936 ins.ssa_args.inline_constant = false;
937 ins.ssa_args.src1 = SSA_FIXED_REGISTER(REGISTER_CONSTANT);
938 ins.has_constants = true;
939 ins.constants[0] = 0.0f;
940 ins.alu.src2 = vector_alu_srco_unsigned(blank_alu_src_xxxx);
941 } else if (instr->op == nir_op_inot) {
942 /* ~b = ~(b & b), so duplicate the source */
943 ins.ssa_args.src1 = ins.ssa_args.src0;
944 ins.alu.src2 = ins.alu.src1;
945 }
946
947 if ((opcode_props & UNITS_ALL) == UNIT_VLUT) {
948 /* To avoid duplicating the lookup tables (probably), true LUT
949 * instructions can only operate as if they were scalars. Lower
950 * them here by changing the component. */
951
952 uint8_t original_swizzle[4];
953 memcpy(original_swizzle, nirmods[0]->swizzle, sizeof(nirmods[0]->swizzle));
954
955 for (int i = 0; i < nr_components; ++i) {
956 ins.alu.mask = (0x3) << (2 * i); /* Mask the associated component */
957
958 for (int j = 0; j < 4; ++j)
959 nirmods[0]->swizzle[j] = original_swizzle[i]; /* Pull from the correct component */
960
961 ins.alu.src1 = vector_alu_srco_unsigned(vector_alu_modifiers(nirmods[0], is_int));
962 emit_mir_instruction(ctx, ins);
963 }
964 } else {
965 emit_mir_instruction(ctx, ins);
966 }
967 }
968
969 #undef ALU_CASE
970
971 static void
972 emit_uniform_read(compiler_context *ctx, unsigned dest, unsigned offset, nir_src *indirect_offset)
973 {
974 /* TODO: half-floats */
975
976 if (!indirect_offset && offset < ctx->uniform_cutoff) {
977 /* Fast path: For the first 16 uniforms, direct accesses are
978 * 0-cycle, since they're just a register fetch in the usual
979 * case. So, we alias the registers while we're still in
980 * SSA-space */
981
982 int reg_slot = 23 - offset;
983 alias_ssa(ctx, dest, SSA_FIXED_REGISTER(reg_slot));
984 } else {
985 /* Otherwise, read from the 'special' UBO to access
986 * higher-indexed uniforms, at a performance cost. More
987 * generally, we're emitting a UBO read instruction. */
988
989 midgard_instruction ins = m_ld_uniform_32(dest, offset);
990
991 /* TODO: Don't split */
992 ins.load_store.varying_parameters = (offset & 7) << 7;
993 ins.load_store.address = offset >> 3;
994
995 if (indirect_offset) {
996 emit_indirect_offset(ctx, indirect_offset);
997 ins.load_store.unknown = 0x8700; /* xxx: what is this? */
998 } else {
999 ins.load_store.unknown = 0x1E00; /* xxx: what is this? */
1000 }
1001
1002 emit_mir_instruction(ctx, ins);
1003 }
1004 }
1005
1006 static void
1007 emit_sysval_read(compiler_context *ctx, nir_intrinsic_instr *instr)
1008 {
1009 /* First, pull out the destination */
1010 unsigned dest = nir_dest_index(ctx, &instr->dest);
1011
1012 /* Now, figure out which uniform this is */
1013 int sysval = midgard_nir_sysval_for_intrinsic(instr);
1014 void *val = _mesa_hash_table_u64_search(ctx->sysval_to_id, sysval);
1015
1016 /* Sysvals are prefix uniforms */
1017 unsigned uniform = ((uintptr_t) val) - 1;
1018
1019 /* Emit the read itself -- this is never indirect */
1020 emit_uniform_read(ctx, dest, uniform, NULL);
1021 }
1022
1023 /* Reads RGBA8888 value from the tilebuffer and converts to a RGBA32F register,
1024 * using scalar ops functional on earlier Midgard generations. Newer Midgard
1025 * generations have faster vectorized reads. This operation is for blend
1026 * shaders in particular; reading the tilebuffer from the fragment shader
1027 * remains an open problem. */
1028
1029 static void
1030 emit_fb_read_blend_scalar(compiler_context *ctx, unsigned reg)
1031 {
1032 midgard_instruction ins = m_ld_color_buffer_8(reg, 0);
1033 ins.load_store.swizzle = 0; /* xxxx */
1034
1035 /* Read each component sequentially */
1036
1037 for (unsigned c = 0; c < 4; ++c) {
1038 ins.load_store.mask = (1 << c);
1039 ins.load_store.unknown = c;
1040 emit_mir_instruction(ctx, ins);
1041 }
1042
1043 /* vadd.u2f hr2, zext(hr2), #0 */
1044
1045 midgard_vector_alu_src alu_src = blank_alu_src;
1046 alu_src.mod = midgard_int_zero_extend;
1047 alu_src.half = true;
1048
1049 midgard_instruction u2f = {
1050 .type = TAG_ALU_4,
1051 .ssa_args = {
1052 .src0 = reg,
1053 .src1 = SSA_UNUSED_0,
1054 .dest = reg,
1055 .inline_constant = true
1056 },
1057 .alu = {
1058 .op = midgard_alu_op_u2f,
1059 .reg_mode = midgard_reg_mode_16,
1060 .dest_override = midgard_dest_override_none,
1061 .mask = 0xF,
1062 .src1 = vector_alu_srco_unsigned(alu_src),
1063 .src2 = vector_alu_srco_unsigned(blank_alu_src),
1064 }
1065 };
1066
1067 emit_mir_instruction(ctx, u2f);
1068
1069 /* vmul.fmul.sat r1, hr2, #0.00392151 */
1070
1071 alu_src.mod = 0;
1072
1073 midgard_instruction fmul = {
1074 .type = TAG_ALU_4,
1075 .inline_constant = _mesa_float_to_half(1.0 / 255.0),
1076 .ssa_args = {
1077 .src0 = reg,
1078 .dest = reg,
1079 .src1 = SSA_UNUSED_0,
1080 .inline_constant = true
1081 },
1082 .alu = {
1083 .op = midgard_alu_op_fmul,
1084 .reg_mode = midgard_reg_mode_32,
1085 .dest_override = midgard_dest_override_none,
1086 .outmod = midgard_outmod_sat,
1087 .mask = 0xFF,
1088 .src1 = vector_alu_srco_unsigned(alu_src),
1089 .src2 = vector_alu_srco_unsigned(blank_alu_src),
1090 }
1091 };
1092
1093 emit_mir_instruction(ctx, fmul);
1094 }
1095
1096 static void
1097 emit_intrinsic(compiler_context *ctx, nir_intrinsic_instr *instr)
1098 {
1099 unsigned offset, reg;
1100
1101 switch (instr->intrinsic) {
1102 case nir_intrinsic_discard_if:
1103 emit_condition(ctx, &instr->src[0], true, COMPONENT_X);
1104
1105 /* fallthrough */
1106
1107 case nir_intrinsic_discard: {
1108 bool conditional = instr->intrinsic == nir_intrinsic_discard_if;
1109 struct midgard_instruction discard = v_branch(conditional, false);
1110 discard.branch.target_type = TARGET_DISCARD;
1111 emit_mir_instruction(ctx, discard);
1112
1113 ctx->can_discard = true;
1114 break;
1115 }
1116
1117 case nir_intrinsic_load_uniform:
1118 case nir_intrinsic_load_input:
1119 offset = nir_intrinsic_base(instr);
1120
1121 unsigned nr_comp = nir_intrinsic_dest_components(instr);
1122 bool direct = nir_src_is_const(instr->src[0]);
1123
1124 if (direct) {
1125 offset += nir_src_as_uint(instr->src[0]);
1126 }
1127
1128 reg = nir_dest_index(ctx, &instr->dest);
1129
1130 if (instr->intrinsic == nir_intrinsic_load_uniform && !ctx->is_blend) {
1131 emit_uniform_read(ctx, reg, ctx->sysval_count + offset, !direct ? &instr->src[0] : NULL);
1132 } else if (ctx->stage == MESA_SHADER_FRAGMENT && !ctx->is_blend) {
1133 /* XXX: Half-floats? */
1134 /* TODO: swizzle, mask */
1135
1136 midgard_instruction ins = m_ld_vary_32(reg, offset);
1137 ins.load_store.mask = (1 << nr_comp) - 1;
1138
1139 midgard_varying_parameter p = {
1140 .is_varying = 1,
1141 .interpolation = midgard_interp_default,
1142 .flat = /*var->data.interpolation == INTERP_MODE_FLAT*/ 0
1143 };
1144
1145 unsigned u;
1146 memcpy(&u, &p, sizeof(p));
1147 ins.load_store.varying_parameters = u;
1148
1149 if (direct) {
1150 /* We have the offset totally ready */
1151 ins.load_store.unknown = 0x1e9e; /* xxx: what is this? */
1152 } else {
1153 /* We have it partially ready, but we need to
1154 * add in the dynamic index, moved to r27.w */
1155 emit_indirect_offset(ctx, &instr->src[0]);
1156 ins.load_store.unknown = 0x79e; /* xxx: what is this? */
1157 }
1158
1159 emit_mir_instruction(ctx, ins);
1160 } else if (ctx->is_blend) {
1161 /* For blend shaders, load the input color, which is
1162 * preloaded to r0 */
1163
1164 midgard_instruction move = v_fmov(reg, blank_alu_src, SSA_FIXED_REGISTER(0));
1165 emit_mir_instruction(ctx, move);
1166 } else if (ctx->stage == MESA_SHADER_VERTEX) {
1167 midgard_instruction ins = m_ld_attr_32(reg, offset);
1168 ins.load_store.unknown = 0x1E1E; /* XXX: What is this? */
1169 ins.load_store.mask = (1 << nr_comp) - 1;
1170 emit_mir_instruction(ctx, ins);
1171 } else {
1172 DBG("Unknown load\n");
1173 assert(0);
1174 }
1175
1176 break;
1177
1178 case nir_intrinsic_load_output:
1179 assert(nir_src_is_const(instr->src[0]));
1180 reg = nir_dest_index(ctx, &instr->dest);
1181
1182 if (ctx->is_blend) {
1183 /* TODO: MRT */
1184 emit_fb_read_blend_scalar(ctx, reg);
1185 } else {
1186 DBG("Unknown output load\n");
1187 assert(0);
1188 }
1189
1190 break;
1191
1192 case nir_intrinsic_load_blend_const_color_rgba: {
1193 assert(ctx->is_blend);
1194 reg = nir_dest_index(ctx, &instr->dest);
1195
1196 /* Blend constants are embedded directly in the shader and
1197 * patched in, so we use some magic routing */
1198
1199 midgard_instruction ins = v_fmov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), blank_alu_src, reg);
1200 ins.has_constants = true;
1201 ins.has_blend_constant = true;
1202 emit_mir_instruction(ctx, ins);
1203 break;
1204 }
1205
1206 case nir_intrinsic_store_output:
1207 assert(nir_src_is_const(instr->src[1]) && "no indirect outputs");
1208
1209 offset = nir_intrinsic_base(instr) + nir_src_as_uint(instr->src[1]);
1210
1211 reg = nir_src_index(ctx, &instr->src[0]);
1212
1213 if (ctx->stage == MESA_SHADER_FRAGMENT) {
1214 /* gl_FragColor is not emitted with load/store
1215 * instructions. Instead, it gets plonked into
1216 * r0 at the end of the shader and we do the
1217 * framebuffer writeout dance. TODO: Defer
1218 * writes */
1219
1220 midgard_instruction move = v_fmov(reg, blank_alu_src, SSA_FIXED_REGISTER(0));
1221 emit_mir_instruction(ctx, move);
1222
1223 /* Save the index we're writing to for later reference
1224 * in the epilogue */
1225
1226 ctx->fragment_output = reg;
1227 } else if (ctx->stage == MESA_SHADER_VERTEX) {
1228 /* Varyings are written into one of two special
1229 * varying register, r26 or r27. The register itself is selected as the register
1230 * in the st_vary instruction, minus the base of 26. E.g. write into r27 and then call st_vary(1)
1231 *
1232 * Normally emitting fmov's is frowned upon,
1233 * but due to unique constraints of
1234 * REGISTER_VARYING, fmov emission + a
1235 * dedicated cleanup pass is the only way to
1236 * guarantee correctness when considering some
1237 * (common) edge cases XXX: FIXME */
1238
1239 /* If this varying corresponds to a constant (why?!),
1240 * emit that now since it won't get picked up by
1241 * hoisting (since there is no corresponding move
1242 * emitted otherwise) */
1243
1244 void *constant_value = _mesa_hash_table_u64_search(ctx->ssa_constants, reg + 1);
1245
1246 if (constant_value) {
1247 /* Special case: emit the varying write
1248 * directly to r26 (looks funny in asm but it's
1249 * fine) and emit the store _now_. Possibly
1250 * slightly slower, but this is a really stupid
1251 * special case anyway (why on earth would you
1252 * have a constant varying? Your own fault for
1253 * slightly worse perf :P) */
1254
1255 midgard_instruction ins = v_fmov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), blank_alu_src, SSA_FIXED_REGISTER(26));
1256 attach_constants(ctx, &ins, constant_value, reg + 1);
1257 emit_mir_instruction(ctx, ins);
1258
1259 midgard_instruction st = m_st_vary_32(SSA_FIXED_REGISTER(0), offset);
1260 st.load_store.unknown = 0x1E9E; /* XXX: What is this? */
1261 emit_mir_instruction(ctx, st);
1262 } else {
1263 /* Do not emit the varying yet -- instead, just mark down that we need to later */
1264
1265 _mesa_hash_table_u64_insert(ctx->ssa_varyings, reg + 1, (void *) ((uintptr_t) (offset + 1)));
1266 }
1267 } else {
1268 DBG("Unknown store\n");
1269 assert(0);
1270 }
1271
1272 break;
1273
1274 case nir_intrinsic_load_alpha_ref_float:
1275 assert(instr->dest.is_ssa);
1276
1277 float ref_value = ctx->alpha_ref;
1278
1279 float *v = ralloc_array(NULL, float, 4);
1280 memcpy(v, &ref_value, sizeof(float));
1281 _mesa_hash_table_u64_insert(ctx->ssa_constants, instr->dest.ssa.index + 1, v);
1282 break;
1283
1284 case nir_intrinsic_load_viewport_scale:
1285 case nir_intrinsic_load_viewport_offset:
1286 emit_sysval_read(ctx, instr);
1287 break;
1288
1289 default:
1290 printf ("Unhandled intrinsic\n");
1291 assert(0);
1292 break;
1293 }
1294 }
1295
1296 static unsigned
1297 midgard_tex_format(enum glsl_sampler_dim dim)
1298 {
1299 switch (dim) {
1300 case GLSL_SAMPLER_DIM_2D:
1301 case GLSL_SAMPLER_DIM_EXTERNAL:
1302 return TEXTURE_2D;
1303
1304 case GLSL_SAMPLER_DIM_3D:
1305 return TEXTURE_3D;
1306
1307 case GLSL_SAMPLER_DIM_CUBE:
1308 return TEXTURE_CUBE;
1309
1310 default:
1311 DBG("Unknown sampler dim type\n");
1312 assert(0);
1313 return 0;
1314 }
1315 }
1316
1317 static void
1318 emit_tex(compiler_context *ctx, nir_tex_instr *instr)
1319 {
1320 /* TODO */
1321 //assert (!instr->sampler);
1322 //assert (!instr->texture_array_size);
1323 assert (instr->op == nir_texop_tex);
1324
1325 /* Allocate registers via a round robin scheme to alternate between the two registers */
1326 int reg = ctx->texture_op_count & 1;
1327 int in_reg = reg, out_reg = reg;
1328
1329 /* Make room for the reg */
1330
1331 if (ctx->texture_index[reg] > -1)
1332 unalias_ssa(ctx, ctx->texture_index[reg]);
1333
1334 int texture_index = instr->texture_index;
1335 int sampler_index = texture_index;
1336
1337 for (unsigned i = 0; i < instr->num_srcs; ++i) {
1338 switch (instr->src[i].src_type) {
1339 case nir_tex_src_coord: {
1340 int index = nir_src_index(ctx, &instr->src[i].src);
1341
1342 midgard_vector_alu_src alu_src = blank_alu_src;
1343
1344 int reg = SSA_FIXED_REGISTER(REGISTER_TEXTURE_BASE + in_reg);
1345
1346 if (instr->sampler_dim == GLSL_SAMPLER_DIM_CUBE) {
1347 /* For cubemaps, we need to load coords into
1348 * special r27, and then use a special ld/st op
1349 * to copy into the texture register */
1350
1351 alu_src.swizzle = SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_X);
1352
1353 midgard_instruction move = v_fmov(index, alu_src, SSA_FIXED_REGISTER(27));
1354 emit_mir_instruction(ctx, move);
1355
1356 midgard_instruction st = m_st_cubemap_coords(reg, 0);
1357 st.load_store.unknown = 0x24; /* XXX: What is this? */
1358 st.load_store.mask = 0x3; /* xy? */
1359 st.load_store.swizzle = alu_src.swizzle;
1360 emit_mir_instruction(ctx, st);
1361
1362 } else {
1363 alu_src.swizzle = SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_X, COMPONENT_X);
1364
1365 midgard_instruction ins = v_fmov(index, alu_src, reg);
1366 emit_mir_instruction(ctx, ins);
1367 }
1368
1369 break;
1370 }
1371
1372 default: {
1373 DBG("Unknown source type\n");
1374 //assert(0);
1375 break;
1376 }
1377 }
1378 }
1379
1380 /* No helper to build texture words -- we do it all here */
1381 midgard_instruction ins = {
1382 .type = TAG_TEXTURE_4,
1383 .texture = {
1384 .op = TEXTURE_OP_NORMAL,
1385 .format = midgard_tex_format(instr->sampler_dim),
1386 .texture_handle = texture_index,
1387 .sampler_handle = sampler_index,
1388
1389 /* TODO: Don't force xyzw */
1390 .swizzle = SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
1391 .mask = 0xF,
1392
1393 /* TODO: half */
1394 //.in_reg_full = 1,
1395 .out_full = 1,
1396
1397 .filter = 1,
1398
1399 /* Always 1 */
1400 .unknown7 = 1,
1401
1402 /* Assume we can continue; hint it out later */
1403 .cont = 1,
1404 }
1405 };
1406
1407 /* Set registers to read and write from the same place */
1408 ins.texture.in_reg_select = in_reg;
1409 ins.texture.out_reg_select = out_reg;
1410
1411 /* TODO: Dynamic swizzle input selection, half-swizzles? */
1412 if (instr->sampler_dim == GLSL_SAMPLER_DIM_3D) {
1413 ins.texture.in_reg_swizzle_right = COMPONENT_X;
1414 ins.texture.in_reg_swizzle_left = COMPONENT_Y;
1415 //ins.texture.in_reg_swizzle_third = COMPONENT_Z;
1416 } else {
1417 ins.texture.in_reg_swizzle_left = COMPONENT_X;
1418 ins.texture.in_reg_swizzle_right = COMPONENT_Y;
1419 //ins.texture.in_reg_swizzle_third = COMPONENT_X;
1420 }
1421
1422 emit_mir_instruction(ctx, ins);
1423
1424 /* Simultaneously alias the destination and emit a move for it. The move will be eliminated if possible */
1425
1426 int o_reg = REGISTER_TEXTURE_BASE + out_reg, o_index = nir_dest_index(ctx, &instr->dest);
1427 alias_ssa(ctx, o_index, SSA_FIXED_REGISTER(o_reg));
1428 ctx->texture_index[reg] = o_index;
1429
1430 midgard_instruction ins2 = v_fmov(SSA_FIXED_REGISTER(o_reg), blank_alu_src, o_index);
1431 emit_mir_instruction(ctx, ins2);
1432
1433 /* Used for .cont and .last hinting */
1434 ctx->texture_op_count++;
1435 }
1436
1437 static void
1438 emit_jump(compiler_context *ctx, nir_jump_instr *instr)
1439 {
1440 switch (instr->type) {
1441 case nir_jump_break: {
1442 /* Emit a branch out of the loop */
1443 struct midgard_instruction br = v_branch(false, false);
1444 br.branch.target_type = TARGET_BREAK;
1445 br.branch.target_break = ctx->current_loop_depth;
1446 emit_mir_instruction(ctx, br);
1447
1448 DBG("break..\n");
1449 break;
1450 }
1451
1452 default:
1453 DBG("Unknown jump type %d\n", instr->type);
1454 break;
1455 }
1456 }
1457
1458 static void
1459 emit_instr(compiler_context *ctx, struct nir_instr *instr)
1460 {
1461 switch (instr->type) {
1462 case nir_instr_type_load_const:
1463 emit_load_const(ctx, nir_instr_as_load_const(instr));
1464 break;
1465
1466 case nir_instr_type_intrinsic:
1467 emit_intrinsic(ctx, nir_instr_as_intrinsic(instr));
1468 break;
1469
1470 case nir_instr_type_alu:
1471 emit_alu(ctx, nir_instr_as_alu(instr));
1472 break;
1473
1474 case nir_instr_type_tex:
1475 emit_tex(ctx, nir_instr_as_tex(instr));
1476 break;
1477
1478 case nir_instr_type_jump:
1479 emit_jump(ctx, nir_instr_as_jump(instr));
1480 break;
1481
1482 case nir_instr_type_ssa_undef:
1483 /* Spurious */
1484 break;
1485
1486 default:
1487 DBG("Unhandled instruction type\n");
1488 break;
1489 }
1490 }
1491
1492
1493 /* ALU instructions can inline or embed constants, which decreases register
1494 * pressure and saves space. */
1495
1496 #define CONDITIONAL_ATTACH(src) { \
1497 void *entry = _mesa_hash_table_u64_search(ctx->ssa_constants, alu->ssa_args.src + 1); \
1498 \
1499 if (entry) { \
1500 attach_constants(ctx, alu, entry, alu->ssa_args.src + 1); \
1501 alu->ssa_args.src = SSA_FIXED_REGISTER(REGISTER_CONSTANT); \
1502 } \
1503 }
1504
1505 static void
1506 inline_alu_constants(compiler_context *ctx)
1507 {
1508 mir_foreach_instr(ctx, alu) {
1509 /* Other instructions cannot inline constants */
1510 if (alu->type != TAG_ALU_4) continue;
1511
1512 /* If there is already a constant here, we can do nothing */
1513 if (alu->has_constants) continue;
1514
1515 /* It makes no sense to inline constants on a branch */
1516 if (alu->compact_branch || alu->prepacked_branch) continue;
1517
1518 CONDITIONAL_ATTACH(src0);
1519
1520 if (!alu->has_constants) {
1521 CONDITIONAL_ATTACH(src1)
1522 } else if (!alu->inline_constant) {
1523 /* Corner case: _two_ vec4 constants, for instance with a
1524 * csel. For this case, we can only use a constant
1525 * register for one, we'll have to emit a move for the
1526 * other. Note, if both arguments are constants, then
1527 * necessarily neither argument depends on the value of
1528 * any particular register. As the destination register
1529 * will be wiped, that means we can spill the constant
1530 * to the destination register.
1531 */
1532
1533 void *entry = _mesa_hash_table_u64_search(ctx->ssa_constants, alu->ssa_args.src1 + 1);
1534 unsigned scratch = alu->ssa_args.dest;
1535
1536 if (entry) {
1537 midgard_instruction ins = v_fmov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), blank_alu_src, scratch);
1538 attach_constants(ctx, &ins, entry, alu->ssa_args.src1 + 1);
1539
1540 /* Force a break XXX Defer r31 writes */
1541 ins.unit = UNIT_VLUT;
1542
1543 /* Set the source */
1544 alu->ssa_args.src1 = scratch;
1545
1546 /* Inject us -before- the last instruction which set r31 */
1547 mir_insert_instruction_before(mir_prev_op(alu), ins);
1548 }
1549 }
1550 }
1551 }
1552
1553 /* Midgard supports two types of constants, embedded constants (128-bit) and
1554 * inline constants (16-bit). Sometimes, especially with scalar ops, embedded
1555 * constants can be demoted to inline constants, for space savings and
1556 * sometimes a performance boost */
1557
1558 static void
1559 embedded_to_inline_constant(compiler_context *ctx)
1560 {
1561 mir_foreach_instr(ctx, ins) {
1562 if (!ins->has_constants) continue;
1563
1564 if (ins->ssa_args.inline_constant) continue;
1565
1566 /* Blend constants must not be inlined by definition */
1567 if (ins->has_blend_constant) continue;
1568
1569 /* src1 cannot be an inline constant due to encoding
1570 * restrictions. So, if possible we try to flip the arguments
1571 * in that case */
1572
1573 int op = ins->alu.op;
1574
1575 if (ins->ssa_args.src0 == SSA_FIXED_REGISTER(REGISTER_CONSTANT)) {
1576 switch (op) {
1577 /* These ops require an operational change to flip
1578 * their arguments TODO */
1579 case midgard_alu_op_flt:
1580 case midgard_alu_op_fle:
1581 case midgard_alu_op_ilt:
1582 case midgard_alu_op_ile:
1583 case midgard_alu_op_fcsel:
1584 case midgard_alu_op_icsel:
1585 DBG("Missed non-commutative flip (%s)\n", alu_opcode_props[op].name);
1586 default:
1587 break;
1588 }
1589
1590 if (alu_opcode_props[op].props & OP_COMMUTES) {
1591 /* Flip the SSA numbers */
1592 ins->ssa_args.src0 = ins->ssa_args.src1;
1593 ins->ssa_args.src1 = SSA_FIXED_REGISTER(REGISTER_CONSTANT);
1594
1595 /* And flip the modifiers */
1596
1597 unsigned src_temp;
1598
1599 src_temp = ins->alu.src2;
1600 ins->alu.src2 = ins->alu.src1;
1601 ins->alu.src1 = src_temp;
1602 }
1603 }
1604
1605 if (ins->ssa_args.src1 == SSA_FIXED_REGISTER(REGISTER_CONSTANT)) {
1606 /* Extract the source information */
1607
1608 midgard_vector_alu_src *src;
1609 int q = ins->alu.src2;
1610 midgard_vector_alu_src *m = (midgard_vector_alu_src *) &q;
1611 src = m;
1612
1613 /* Component is from the swizzle, e.g. r26.w -> w component. TODO: What if x is masked out? */
1614 int component = src->swizzle & 3;
1615
1616 /* Scale constant appropriately, if we can legally */
1617 uint16_t scaled_constant = 0;
1618
1619 if (midgard_is_integer_op(op)) {
1620 unsigned int *iconstants = (unsigned int *) ins->constants;
1621 scaled_constant = (uint16_t) iconstants[component];
1622
1623 /* Constant overflow after resize */
1624 if (scaled_constant != iconstants[component])
1625 continue;
1626 } else {
1627 float original = (float) ins->constants[component];
1628 scaled_constant = _mesa_float_to_half(original);
1629
1630 /* Check for loss of precision. If this is
1631 * mediump, we don't care, but for a highp
1632 * shader, we need to pay attention. NIR
1633 * doesn't yet tell us which mode we're in!
1634 * Practically this prevents most constants
1635 * from being inlined, sadly. */
1636
1637 float fp32 = _mesa_half_to_float(scaled_constant);
1638
1639 if (fp32 != original)
1640 continue;
1641 }
1642
1643 /* We don't know how to handle these with a constant */
1644
1645 if (src->mod || src->half || src->rep_low || src->rep_high) {
1646 DBG("Bailing inline constant...\n");
1647 continue;
1648 }
1649
1650 /* Make sure that the constant is not itself a
1651 * vector by checking if all accessed values
1652 * (by the swizzle) are the same. */
1653
1654 uint32_t *cons = (uint32_t *) ins->constants;
1655 uint32_t value = cons[component];
1656
1657 bool is_vector = false;
1658 unsigned mask = effective_writemask(&ins->alu);
1659
1660 for (int c = 1; c < 4; ++c) {
1661 /* We only care if this component is actually used */
1662 if (!(mask & (1 << c)))
1663 continue;
1664
1665 uint32_t test = cons[(src->swizzle >> (2 * c)) & 3];
1666
1667 if (test != value) {
1668 is_vector = true;
1669 break;
1670 }
1671 }
1672
1673 if (is_vector)
1674 continue;
1675
1676 /* Get rid of the embedded constant */
1677 ins->has_constants = false;
1678 ins->ssa_args.src1 = SSA_UNUSED_0;
1679 ins->ssa_args.inline_constant = true;
1680 ins->inline_constant = scaled_constant;
1681 }
1682 }
1683 }
1684
1685 /* Map normal SSA sources to other SSA sources / fixed registers (like
1686 * uniforms) */
1687
1688 static void
1689 map_ssa_to_alias(compiler_context *ctx, int *ref)
1690 {
1691 /* Sign is used quite deliberately for unused */
1692 if (*ref < 0)
1693 return;
1694
1695 unsigned int alias = (uintptr_t) _mesa_hash_table_u64_search(ctx->ssa_to_alias, *ref + 1);
1696
1697 if (alias) {
1698 /* Remove entry in leftovers to avoid a redunant fmov */
1699
1700 struct set_entry *leftover = _mesa_set_search(ctx->leftover_ssa_to_alias, ((void *) (uintptr_t) (*ref + 1)));
1701
1702 if (leftover)
1703 _mesa_set_remove(ctx->leftover_ssa_to_alias, leftover);
1704
1705 /* Assign the alias map */
1706 *ref = alias - 1;
1707 return;
1708 }
1709 }
1710
1711 /* Basic dead code elimination on the MIR itself, which cleans up e.g. the
1712 * texture pipeline */
1713
1714 static bool
1715 midgard_opt_dead_code_eliminate(compiler_context *ctx, midgard_block *block)
1716 {
1717 bool progress = false;
1718
1719 mir_foreach_instr_in_block_safe(block, ins) {
1720 if (ins->type != TAG_ALU_4) continue;
1721 if (ins->compact_branch) continue;
1722
1723 if (ins->ssa_args.dest >= SSA_FIXED_MINIMUM) continue;
1724 if (mir_is_live_after(ctx, block, ins, ins->ssa_args.dest)) continue;
1725
1726 mir_remove_instruction(ins);
1727 progress = true;
1728 }
1729
1730 return progress;
1731 }
1732
1733 static bool
1734 mir_nontrivial_mod(midgard_vector_alu_src src, bool is_int, unsigned mask)
1735 {
1736 /* abs or neg */
1737 if (!is_int && src.mod) return true;
1738
1739 /* swizzle */
1740 for (unsigned c = 0; c < 4; ++c) {
1741 if (!(mask & (1 << c))) continue;
1742 if (((src.swizzle >> (2*c)) & 3) != c) return true;
1743 }
1744
1745 return false;
1746 }
1747
1748 static bool
1749 mir_nontrivial_source2_mod(midgard_instruction *ins)
1750 {
1751 unsigned mask = squeeze_writemask(ins->alu.mask);
1752 bool is_int = midgard_is_integer_op(ins->alu.op);
1753
1754 midgard_vector_alu_src src2 =
1755 vector_alu_from_unsigned(ins->alu.src2);
1756
1757 return mir_nontrivial_mod(src2, is_int, mask);
1758 }
1759
1760 static bool
1761 midgard_opt_copy_prop(compiler_context *ctx, midgard_block *block)
1762 {
1763 bool progress = false;
1764
1765 mir_foreach_instr_in_block_safe(block, ins) {
1766 if (ins->type != TAG_ALU_4) continue;
1767 if (!OP_IS_MOVE(ins->alu.op)) continue;
1768
1769 unsigned from = ins->ssa_args.src1;
1770 unsigned to = ins->ssa_args.dest;
1771
1772 /* We only work on pure SSA */
1773
1774 if (to >= SSA_FIXED_MINIMUM) continue;
1775 if (from >= SSA_FIXED_MINIMUM) continue;
1776 if (to >= ctx->func->impl->ssa_alloc) continue;
1777 if (from >= ctx->func->impl->ssa_alloc) continue;
1778
1779 /* Constant propagation is not handled here, either */
1780 if (ins->ssa_args.inline_constant) continue;
1781 if (ins->has_constants) continue;
1782
1783 if (mir_nontrivial_source2_mod(ins)) continue;
1784 if (ins->alu.outmod != midgard_outmod_none) continue;
1785
1786 /* We're clear -- rewrite */
1787 mir_rewrite_index_src(ctx, to, from);
1788 mir_remove_instruction(ins);
1789 progress |= true;
1790 }
1791
1792 return progress;
1793 }
1794
1795 /* fmov.pos is an idiom for fpos. Propoagate the .pos up to the source, so then
1796 * the move can be propagated away entirely */
1797
1798 static bool
1799 mir_compose_outmod(midgard_outmod *outmod, midgard_outmod comp)
1800 {
1801 /* Nothing to do */
1802 if (comp == midgard_outmod_none)
1803 return true;
1804
1805 if (*outmod == midgard_outmod_none) {
1806 *outmod = comp;
1807 return true;
1808 }
1809
1810 /* TODO: Compose rules */
1811 return false;
1812 }
1813
1814 static bool
1815 midgard_opt_pos_propagate(compiler_context *ctx, midgard_block *block)
1816 {
1817 bool progress = false;
1818
1819 mir_foreach_instr_in_block_safe(block, ins) {
1820 if (ins->type != TAG_ALU_4) continue;
1821 if (ins->alu.op != midgard_alu_op_fmov) continue;
1822 if (ins->alu.outmod != midgard_outmod_pos) continue;
1823
1824 /* TODO: Registers? */
1825 unsigned src = ins->ssa_args.src1;
1826 if (src >= ctx->func->impl->ssa_alloc) continue;
1827
1828 /* There might be a source modifier, too */
1829 if (mir_nontrivial_source2_mod(ins)) continue;
1830
1831 /* Backpropagate the modifier */
1832 mir_foreach_instr_in_block_from_rev(block, v, mir_prev_op(ins)) {
1833 if (v->type != TAG_ALU_4) continue;
1834 if (v->ssa_args.dest != src) continue;
1835
1836 midgard_outmod temp = v->alu.outmod;
1837 progress |= mir_compose_outmod(&temp, ins->alu.outmod);
1838
1839 /* Throw in the towel.. */
1840 if (!progress) break;
1841
1842 /* Otherwise, transfer the modifier */
1843 v->alu.outmod = temp;
1844 ins->alu.outmod = midgard_outmod_none;
1845
1846 break;
1847 }
1848 }
1849
1850 return progress;
1851 }
1852
1853 static bool
1854 midgard_opt_copy_prop_tex(compiler_context *ctx, midgard_block *block)
1855 {
1856 bool progress = false;
1857
1858 mir_foreach_instr_in_block_safe(block, ins) {
1859 if (ins->type != TAG_ALU_4) continue;
1860 if (!OP_IS_MOVE(ins->alu.op)) continue;
1861
1862 unsigned from = ins->ssa_args.src1;
1863 unsigned to = ins->ssa_args.dest;
1864
1865 /* Make sure it's simple enough for us to handle */
1866
1867 if (from >= SSA_FIXED_MINIMUM) continue;
1868 if (from >= ctx->func->impl->ssa_alloc) continue;
1869 if (to < SSA_FIXED_REGISTER(REGISTER_TEXTURE_BASE)) continue;
1870 if (to > SSA_FIXED_REGISTER(REGISTER_TEXTURE_BASE + 1)) continue;
1871
1872 bool eliminated = false;
1873
1874 mir_foreach_instr_in_block_from_rev(block, v, mir_prev_op(ins)) {
1875 /* The texture registers are not SSA so be careful.
1876 * Conservatively, just stop if we hit a texture op
1877 * (even if it may not write) to where we are */
1878
1879 if (v->type != TAG_ALU_4)
1880 break;
1881
1882 if (v->ssa_args.dest == from) {
1883 /* We don't want to track partial writes ... */
1884 if (v->alu.mask == 0xF) {
1885 v->ssa_args.dest = to;
1886 eliminated = true;
1887 }
1888
1889 break;
1890 }
1891 }
1892
1893 if (eliminated)
1894 mir_remove_instruction(ins);
1895
1896 progress |= eliminated;
1897 }
1898
1899 return progress;
1900 }
1901
1902 /* The following passes reorder MIR instructions to enable better scheduling */
1903
1904 static void
1905 midgard_pair_load_store(compiler_context *ctx, midgard_block *block)
1906 {
1907 mir_foreach_instr_in_block_safe(block, ins) {
1908 if (ins->type != TAG_LOAD_STORE_4) continue;
1909
1910 /* We've found a load/store op. Check if next is also load/store. */
1911 midgard_instruction *next_op = mir_next_op(ins);
1912 if (&next_op->link != &block->instructions) {
1913 if (next_op->type == TAG_LOAD_STORE_4) {
1914 /* If so, we're done since we're a pair */
1915 ins = mir_next_op(ins);
1916 continue;
1917 }
1918
1919 /* Maximum search distance to pair, to avoid register pressure disasters */
1920 int search_distance = 8;
1921
1922 /* Otherwise, we have an orphaned load/store -- search for another load */
1923 mir_foreach_instr_in_block_from(block, c, mir_next_op(ins)) {
1924 /* Terminate search if necessary */
1925 if (!(search_distance--)) break;
1926
1927 if (c->type != TAG_LOAD_STORE_4) continue;
1928
1929 /* Stores cannot be reordered, since they have
1930 * dependencies. For the same reason, indirect
1931 * loads cannot be reordered as their index is
1932 * loaded in r27.w */
1933
1934 if (OP_IS_STORE(c->load_store.op)) continue;
1935
1936 /* It appears the 0x800 bit is set whenever a
1937 * load is direct, unset when it is indirect.
1938 * Skip indirect loads. */
1939
1940 if (!(c->load_store.unknown & 0x800)) continue;
1941
1942 /* We found one! Move it up to pair and remove it from the old location */
1943
1944 mir_insert_instruction_before(ins, *c);
1945 mir_remove_instruction(c);
1946
1947 break;
1948 }
1949 }
1950 }
1951 }
1952
1953 /* Emit varying stores late */
1954
1955 static void
1956 midgard_emit_store(compiler_context *ctx, midgard_block *block) {
1957 /* Iterate in reverse to get the final write, rather than the first */
1958
1959 mir_foreach_instr_in_block_safe_rev(block, ins) {
1960 /* Check if what we just wrote needs a store */
1961 int idx = ins->ssa_args.dest;
1962 uintptr_t varying = ((uintptr_t) _mesa_hash_table_u64_search(ctx->ssa_varyings, idx + 1));
1963
1964 if (!varying) continue;
1965
1966 varying -= 1;
1967
1968 /* We need to store to the appropriate varying, so emit the
1969 * move/store */
1970
1971 /* TODO: Integrate with special purpose RA (and scheduler?) */
1972 bool high_varying_register = false;
1973
1974 midgard_instruction mov = v_fmov(idx, blank_alu_src, SSA_FIXED_REGISTER(REGISTER_VARYING_BASE + high_varying_register));
1975
1976 midgard_instruction st = m_st_vary_32(SSA_FIXED_REGISTER(high_varying_register), varying);
1977 st.load_store.unknown = 0x1E9E; /* XXX: What is this? */
1978
1979 mir_insert_instruction_before(mir_next_op(ins), st);
1980 mir_insert_instruction_before(mir_next_op(ins), mov);
1981
1982 /* We no longer need to store this varying */
1983 _mesa_hash_table_u64_remove(ctx->ssa_varyings, idx + 1);
1984 }
1985 }
1986
1987 /* If there are leftovers after the below pass, emit actual fmov
1988 * instructions for the slow-but-correct path */
1989
1990 static void
1991 emit_leftover_move(compiler_context *ctx)
1992 {
1993 set_foreach(ctx->leftover_ssa_to_alias, leftover) {
1994 int base = ((uintptr_t) leftover->key) - 1;
1995 int mapped = base;
1996
1997 map_ssa_to_alias(ctx, &mapped);
1998 EMIT(fmov, mapped, blank_alu_src, base);
1999 }
2000 }
2001
2002 static void
2003 actualise_ssa_to_alias(compiler_context *ctx)
2004 {
2005 mir_foreach_instr(ctx, ins) {
2006 map_ssa_to_alias(ctx, &ins->ssa_args.src0);
2007 map_ssa_to_alias(ctx, &ins->ssa_args.src1);
2008 }
2009
2010 emit_leftover_move(ctx);
2011 }
2012
2013 static void
2014 emit_fragment_epilogue(compiler_context *ctx)
2015 {
2016 /* Special case: writing out constants requires us to include the move
2017 * explicitly now, so shove it into r0 */
2018
2019 void *constant_value = _mesa_hash_table_u64_search(ctx->ssa_constants, ctx->fragment_output + 1);
2020
2021 if (constant_value) {
2022 midgard_instruction ins = v_fmov(SSA_FIXED_REGISTER(REGISTER_CONSTANT), blank_alu_src, SSA_FIXED_REGISTER(0));
2023 attach_constants(ctx, &ins, constant_value, ctx->fragment_output + 1);
2024 emit_mir_instruction(ctx, ins);
2025 }
2026
2027 /* Perform the actual fragment writeout. We have two writeout/branch
2028 * instructions, forming a loop until writeout is successful as per the
2029 * docs. TODO: gl_FragDepth */
2030
2031 EMIT(alu_br_compact_cond, midgard_jmp_writeout_op_writeout, TAG_ALU_4, 0, midgard_condition_always);
2032 EMIT(alu_br_compact_cond, midgard_jmp_writeout_op_writeout, TAG_ALU_4, -1, midgard_condition_always);
2033 }
2034
2035 /* For the blend epilogue, we need to convert the blended fragment vec4 (stored
2036 * in r0) to a RGBA8888 value by scaling and type converting. We then output it
2037 * with the int8 analogue to the fragment epilogue */
2038
2039 static void
2040 emit_blend_epilogue(compiler_context *ctx)
2041 {
2042 /* vmul.fmul.none.fulllow hr48, r0, #255 */
2043
2044 midgard_instruction scale = {
2045 .type = TAG_ALU_4,
2046 .unit = UNIT_VMUL,
2047 .inline_constant = _mesa_float_to_half(255.0),
2048 .ssa_args = {
2049 .src0 = SSA_FIXED_REGISTER(0),
2050 .src1 = SSA_UNUSED_0,
2051 .dest = SSA_FIXED_REGISTER(24),
2052 .inline_constant = true
2053 },
2054 .alu = {
2055 .op = midgard_alu_op_fmul,
2056 .reg_mode = midgard_reg_mode_32,
2057 .dest_override = midgard_dest_override_lower,
2058 .mask = 0xFF,
2059 .src1 = vector_alu_srco_unsigned(blank_alu_src),
2060 .src2 = vector_alu_srco_unsigned(blank_alu_src),
2061 }
2062 };
2063
2064 emit_mir_instruction(ctx, scale);
2065
2066 /* vadd.f2u8.pos.low hr0, hr48, #0 */
2067
2068 midgard_vector_alu_src alu_src = blank_alu_src;
2069 alu_src.half = true;
2070
2071 midgard_instruction f2u8 = {
2072 .type = TAG_ALU_4,
2073 .ssa_args = {
2074 .src0 = SSA_FIXED_REGISTER(24),
2075 .src1 = SSA_UNUSED_0,
2076 .dest = SSA_FIXED_REGISTER(0),
2077 .inline_constant = true
2078 },
2079 .alu = {
2080 .op = midgard_alu_op_f2u8,
2081 .reg_mode = midgard_reg_mode_16,
2082 .dest_override = midgard_dest_override_lower,
2083 .outmod = midgard_outmod_pos,
2084 .mask = 0xF,
2085 .src1 = vector_alu_srco_unsigned(alu_src),
2086 .src2 = vector_alu_srco_unsigned(blank_alu_src),
2087 }
2088 };
2089
2090 emit_mir_instruction(ctx, f2u8);
2091
2092 /* vmul.imov.quarter r0, r0, r0 */
2093
2094 midgard_instruction imov_8 = {
2095 .type = TAG_ALU_4,
2096 .ssa_args = {
2097 .src0 = SSA_UNUSED_1,
2098 .src1 = SSA_FIXED_REGISTER(0),
2099 .dest = SSA_FIXED_REGISTER(0),
2100 },
2101 .alu = {
2102 .op = midgard_alu_op_imov,
2103 .reg_mode = midgard_reg_mode_8,
2104 .dest_override = midgard_dest_override_none,
2105 .outmod = midgard_outmod_int,
2106 .mask = 0xFF,
2107 .src1 = vector_alu_srco_unsigned(blank_alu_src),
2108 .src2 = vector_alu_srco_unsigned(blank_alu_src),
2109 }
2110 };
2111
2112 /* Emit branch epilogue with the 8-bit move as the source */
2113
2114 emit_mir_instruction(ctx, imov_8);
2115 EMIT(alu_br_compact_cond, midgard_jmp_writeout_op_writeout, TAG_ALU_4, 0, midgard_condition_always);
2116
2117 emit_mir_instruction(ctx, imov_8);
2118 EMIT(alu_br_compact_cond, midgard_jmp_writeout_op_writeout, TAG_ALU_4, -1, midgard_condition_always);
2119 }
2120
2121 static midgard_block *
2122 emit_block(compiler_context *ctx, nir_block *block)
2123 {
2124 midgard_block *this_block = calloc(sizeof(midgard_block), 1);
2125 list_addtail(&this_block->link, &ctx->blocks);
2126
2127 this_block->is_scheduled = false;
2128 ++ctx->block_count;
2129
2130 ctx->texture_index[0] = -1;
2131 ctx->texture_index[1] = -1;
2132
2133 /* Add us as a successor to the block we are following */
2134 if (ctx->current_block)
2135 midgard_block_add_successor(ctx->current_block, this_block);
2136
2137 /* Set up current block */
2138 list_inithead(&this_block->instructions);
2139 ctx->current_block = this_block;
2140
2141 nir_foreach_instr(instr, block) {
2142 emit_instr(ctx, instr);
2143 ++ctx->instruction_count;
2144 }
2145
2146 inline_alu_constants(ctx);
2147 embedded_to_inline_constant(ctx);
2148
2149 /* Perform heavylifting for aliasing */
2150 actualise_ssa_to_alias(ctx);
2151
2152 midgard_emit_store(ctx, this_block);
2153 midgard_pair_load_store(ctx, this_block);
2154
2155 /* Append fragment shader epilogue (value writeout) */
2156 if (ctx->stage == MESA_SHADER_FRAGMENT) {
2157 if (block == nir_impl_last_block(ctx->func->impl)) {
2158 if (ctx->is_blend)
2159 emit_blend_epilogue(ctx);
2160 else
2161 emit_fragment_epilogue(ctx);
2162 }
2163 }
2164
2165 if (block == nir_start_block(ctx->func->impl))
2166 ctx->initial_block = this_block;
2167
2168 if (block == nir_impl_last_block(ctx->func->impl))
2169 ctx->final_block = this_block;
2170
2171 /* Allow the next control flow to access us retroactively, for
2172 * branching etc */
2173 ctx->current_block = this_block;
2174
2175 /* Document the fallthrough chain */
2176 ctx->previous_source_block = this_block;
2177
2178 return this_block;
2179 }
2180
2181 static midgard_block *emit_cf_list(struct compiler_context *ctx, struct exec_list *list);
2182
2183 static void
2184 emit_if(struct compiler_context *ctx, nir_if *nif)
2185 {
2186 /* Conditional branches expect the condition in r31.w; emit a move for
2187 * that in the _previous_ block (which is the current block). */
2188 emit_condition(ctx, &nif->condition, true, COMPONENT_X);
2189
2190 /* Speculatively emit the branch, but we can't fill it in until later */
2191 EMIT(branch, true, true);
2192 midgard_instruction *then_branch = mir_last_in_block(ctx->current_block);
2193
2194 /* Emit the two subblocks */
2195 midgard_block *then_block = emit_cf_list(ctx, &nif->then_list);
2196
2197 /* Emit a jump from the end of the then block to the end of the else */
2198 EMIT(branch, false, false);
2199 midgard_instruction *then_exit = mir_last_in_block(ctx->current_block);
2200
2201 /* Emit second block, and check if it's empty */
2202
2203 int else_idx = ctx->block_count;
2204 int count_in = ctx->instruction_count;
2205 midgard_block *else_block = emit_cf_list(ctx, &nif->else_list);
2206 int after_else_idx = ctx->block_count;
2207
2208 /* Now that we have the subblocks emitted, fix up the branches */
2209
2210 assert(then_block);
2211 assert(else_block);
2212
2213 if (ctx->instruction_count == count_in) {
2214 /* The else block is empty, so don't emit an exit jump */
2215 mir_remove_instruction(then_exit);
2216 then_branch->branch.target_block = after_else_idx;
2217 } else {
2218 then_branch->branch.target_block = else_idx;
2219 then_exit->branch.target_block = after_else_idx;
2220 }
2221 }
2222
2223 static void
2224 emit_loop(struct compiler_context *ctx, nir_loop *nloop)
2225 {
2226 /* Remember where we are */
2227 midgard_block *start_block = ctx->current_block;
2228
2229 /* Allocate a loop number, growing the current inner loop depth */
2230 int loop_idx = ++ctx->current_loop_depth;
2231
2232 /* Get index from before the body so we can loop back later */
2233 int start_idx = ctx->block_count;
2234
2235 /* Emit the body itself */
2236 emit_cf_list(ctx, &nloop->body);
2237
2238 /* Branch back to loop back */
2239 struct midgard_instruction br_back = v_branch(false, false);
2240 br_back.branch.target_block = start_idx;
2241 emit_mir_instruction(ctx, br_back);
2242
2243 /* Mark down that branch in the graph. Note that we're really branching
2244 * to the block *after* we started in. TODO: Why doesn't the branch
2245 * itself have an off-by-one then...? */
2246 midgard_block_add_successor(ctx->current_block, start_block->successors[0]);
2247
2248 /* Find the index of the block about to follow us (note: we don't add
2249 * one; blocks are 0-indexed so we get a fencepost problem) */
2250 int break_block_idx = ctx->block_count;
2251
2252 /* Fix up the break statements we emitted to point to the right place,
2253 * now that we can allocate a block number for them */
2254
2255 list_for_each_entry_from(struct midgard_block, block, start_block, &ctx->blocks, link) {
2256 mir_foreach_instr_in_block(block, ins) {
2257 if (ins->type != TAG_ALU_4) continue;
2258 if (!ins->compact_branch) continue;
2259 if (ins->prepacked_branch) continue;
2260
2261 /* We found a branch -- check the type to see if we need to do anything */
2262 if (ins->branch.target_type != TARGET_BREAK) continue;
2263
2264 /* It's a break! Check if it's our break */
2265 if (ins->branch.target_break != loop_idx) continue;
2266
2267 /* Okay, cool, we're breaking out of this loop.
2268 * Rewrite from a break to a goto */
2269
2270 ins->branch.target_type = TARGET_GOTO;
2271 ins->branch.target_block = break_block_idx;
2272 }
2273 }
2274
2275 /* Now that we've finished emitting the loop, free up the depth again
2276 * so we play nice with recursion amid nested loops */
2277 --ctx->current_loop_depth;
2278 }
2279
2280 static midgard_block *
2281 emit_cf_list(struct compiler_context *ctx, struct exec_list *list)
2282 {
2283 midgard_block *start_block = NULL;
2284
2285 foreach_list_typed(nir_cf_node, node, node, list) {
2286 switch (node->type) {
2287 case nir_cf_node_block: {
2288 midgard_block *block = emit_block(ctx, nir_cf_node_as_block(node));
2289
2290 if (!start_block)
2291 start_block = block;
2292
2293 break;
2294 }
2295
2296 case nir_cf_node_if:
2297 emit_if(ctx, nir_cf_node_as_if(node));
2298 break;
2299
2300 case nir_cf_node_loop:
2301 emit_loop(ctx, nir_cf_node_as_loop(node));
2302 break;
2303
2304 case nir_cf_node_function:
2305 assert(0);
2306 break;
2307 }
2308 }
2309
2310 return start_block;
2311 }
2312
2313 /* Due to lookahead, we need to report the first tag executed in the command
2314 * stream and in branch targets. An initial block might be empty, so iterate
2315 * until we find one that 'works' */
2316
2317 static unsigned
2318 midgard_get_first_tag_from_block(compiler_context *ctx, unsigned block_idx)
2319 {
2320 midgard_block *initial_block = mir_get_block(ctx, block_idx);
2321
2322 unsigned first_tag = 0;
2323
2324 do {
2325 midgard_bundle *initial_bundle = util_dynarray_element(&initial_block->bundles, midgard_bundle, 0);
2326
2327 if (initial_bundle) {
2328 first_tag = initial_bundle->tag;
2329 break;
2330 }
2331
2332 /* Initial block is empty, try the next block */
2333 initial_block = list_first_entry(&(initial_block->link), midgard_block, link);
2334 } while(initial_block != NULL);
2335
2336 assert(first_tag);
2337 return first_tag;
2338 }
2339
2340 int
2341 midgard_compile_shader_nir(nir_shader *nir, midgard_program *program, bool is_blend)
2342 {
2343 struct util_dynarray *compiled = &program->compiled;
2344
2345 midgard_debug = debug_get_option_midgard_debug();
2346
2347 compiler_context ictx = {
2348 .nir = nir,
2349 .stage = nir->info.stage,
2350
2351 .is_blend = is_blend,
2352 .blend_constant_offset = -1,
2353
2354 .alpha_ref = program->alpha_ref
2355 };
2356
2357 compiler_context *ctx = &ictx;
2358
2359 /* TODO: Decide this at runtime */
2360 ctx->uniform_cutoff = 8;
2361
2362 /* Initialize at a global (not block) level hash tables */
2363
2364 ctx->ssa_constants = _mesa_hash_table_u64_create(NULL);
2365 ctx->ssa_varyings = _mesa_hash_table_u64_create(NULL);
2366 ctx->ssa_to_alias = _mesa_hash_table_u64_create(NULL);
2367 ctx->hash_to_temp = _mesa_hash_table_u64_create(NULL);
2368 ctx->sysval_to_id = _mesa_hash_table_u64_create(NULL);
2369 ctx->leftover_ssa_to_alias = _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
2370
2371 /* Record the varying mapping for the command stream's bookkeeping */
2372
2373 struct exec_list *varyings =
2374 ctx->stage == MESA_SHADER_VERTEX ? &nir->outputs : &nir->inputs;
2375
2376 nir_foreach_variable(var, varyings) {
2377 unsigned loc = var->data.driver_location;
2378 unsigned sz = glsl_type_size(var->type, FALSE);
2379
2380 for (int c = 0; c < sz; ++c) {
2381 program->varyings[loc + c] = var->data.location;
2382 }
2383 }
2384
2385 /* Lower gl_Position pre-optimisation */
2386
2387 if (ctx->stage == MESA_SHADER_VERTEX)
2388 NIR_PASS_V(nir, nir_lower_viewport_transform);
2389
2390 NIR_PASS_V(nir, nir_lower_var_copies);
2391 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
2392 NIR_PASS_V(nir, nir_split_var_copies);
2393 NIR_PASS_V(nir, nir_lower_var_copies);
2394 NIR_PASS_V(nir, nir_lower_global_vars_to_local);
2395 NIR_PASS_V(nir, nir_lower_var_copies);
2396 NIR_PASS_V(nir, nir_lower_vars_to_ssa);
2397
2398 NIR_PASS_V(nir, nir_lower_io, nir_var_all, glsl_type_size, 0);
2399
2400 /* Optimisation passes */
2401
2402 optimise_nir(nir);
2403
2404 if (midgard_debug & MIDGARD_DBG_SHADERS) {
2405 nir_print_shader(nir, stdout);
2406 }
2407
2408 /* Assign sysvals and counts, now that we're sure
2409 * (post-optimisation) */
2410
2411 midgard_nir_assign_sysvals(ctx, nir);
2412
2413 program->uniform_count = nir->num_uniforms;
2414 program->sysval_count = ctx->sysval_count;
2415 memcpy(program->sysvals, ctx->sysvals, sizeof(ctx->sysvals[0]) * ctx->sysval_count);
2416
2417 program->attribute_count = (ctx->stage == MESA_SHADER_VERTEX) ? nir->num_inputs : 0;
2418 program->varying_count = (ctx->stage == MESA_SHADER_VERTEX) ? nir->num_outputs : ((ctx->stage == MESA_SHADER_FRAGMENT) ? nir->num_inputs : 0);
2419
2420 nir_foreach_function(func, nir) {
2421 if (!func->impl)
2422 continue;
2423
2424 list_inithead(&ctx->blocks);
2425 ctx->block_count = 0;
2426 ctx->func = func;
2427
2428 emit_cf_list(ctx, &func->impl->body);
2429 emit_block(ctx, func->impl->end_block);
2430
2431 break; /* TODO: Multi-function shaders */
2432 }
2433
2434 util_dynarray_init(compiled, NULL);
2435
2436 /* MIR-level optimizations */
2437
2438 bool progress = false;
2439
2440 do {
2441 progress = false;
2442
2443 mir_foreach_block(ctx, block) {
2444 progress |= midgard_opt_pos_propagate(ctx, block);
2445 progress |= midgard_opt_copy_prop(ctx, block);
2446 progress |= midgard_opt_copy_prop_tex(ctx, block);
2447 progress |= midgard_opt_dead_code_eliminate(ctx, block);
2448 }
2449 } while (progress);
2450
2451 /* Schedule! */
2452 schedule_program(ctx);
2453
2454 /* Now that all the bundles are scheduled and we can calculate block
2455 * sizes, emit actual branch instructions rather than placeholders */
2456
2457 int br_block_idx = 0;
2458
2459 mir_foreach_block(ctx, block) {
2460 util_dynarray_foreach(&block->bundles, midgard_bundle, bundle) {
2461 for (int c = 0; c < bundle->instruction_count; ++c) {
2462 midgard_instruction *ins = bundle->instructions[c];
2463
2464 if (!midgard_is_branch_unit(ins->unit)) continue;
2465
2466 if (ins->prepacked_branch) continue;
2467
2468 /* Parse some basic branch info */
2469 bool is_compact = ins->unit == ALU_ENAB_BR_COMPACT;
2470 bool is_conditional = ins->branch.conditional;
2471 bool is_inverted = ins->branch.invert_conditional;
2472 bool is_discard = ins->branch.target_type == TARGET_DISCARD;
2473
2474 /* Determine the block we're jumping to */
2475 int target_number = ins->branch.target_block;
2476
2477 /* Report the destination tag */
2478 int dest_tag = is_discard ? 0 : midgard_get_first_tag_from_block(ctx, target_number);
2479
2480 /* Count up the number of quadwords we're
2481 * jumping over = number of quadwords until
2482 * (br_block_idx, target_number) */
2483
2484 int quadword_offset = 0;
2485
2486 if (is_discard) {
2487 /* Jump to the end of the shader. We
2488 * need to include not only the
2489 * following blocks, but also the
2490 * contents of our current block (since
2491 * discard can come in the middle of
2492 * the block) */
2493
2494 midgard_block *blk = mir_get_block(ctx, br_block_idx + 1);
2495
2496 for (midgard_bundle *bun = bundle + 1; bun < (midgard_bundle *)((char*) block->bundles.data + block->bundles.size); ++bun) {
2497 quadword_offset += quadword_size(bun->tag);
2498 }
2499
2500 mir_foreach_block_from(ctx, blk, b) {
2501 quadword_offset += b->quadword_count;
2502 }
2503
2504 } else if (target_number > br_block_idx) {
2505 /* Jump forward */
2506
2507 for (int idx = br_block_idx + 1; idx < target_number; ++idx) {
2508 midgard_block *blk = mir_get_block(ctx, idx);
2509 assert(blk);
2510
2511 quadword_offset += blk->quadword_count;
2512 }
2513 } else {
2514 /* Jump backwards */
2515
2516 for (int idx = br_block_idx; idx >= target_number; --idx) {
2517 midgard_block *blk = mir_get_block(ctx, idx);
2518 assert(blk);
2519
2520 quadword_offset -= blk->quadword_count;
2521 }
2522 }
2523
2524 /* Unconditional extended branches (far jumps)
2525 * have issues, so we always use a conditional
2526 * branch, setting the condition to always for
2527 * unconditional. For compact unconditional
2528 * branches, cond isn't used so it doesn't
2529 * matter what we pick. */
2530
2531 midgard_condition cond =
2532 !is_conditional ? midgard_condition_always :
2533 is_inverted ? midgard_condition_false :
2534 midgard_condition_true;
2535
2536 midgard_jmp_writeout_op op =
2537 is_discard ? midgard_jmp_writeout_op_discard :
2538 (is_compact && !is_conditional) ? midgard_jmp_writeout_op_branch_uncond :
2539 midgard_jmp_writeout_op_branch_cond;
2540
2541 if (!is_compact) {
2542 midgard_branch_extended branch =
2543 midgard_create_branch_extended(
2544 cond, op,
2545 dest_tag,
2546 quadword_offset);
2547
2548 memcpy(&ins->branch_extended, &branch, sizeof(branch));
2549 } else if (is_conditional || is_discard) {
2550 midgard_branch_cond branch = {
2551 .op = op,
2552 .dest_tag = dest_tag,
2553 .offset = quadword_offset,
2554 .cond = cond
2555 };
2556
2557 assert(branch.offset == quadword_offset);
2558
2559 memcpy(&ins->br_compact, &branch, sizeof(branch));
2560 } else {
2561 assert(op == midgard_jmp_writeout_op_branch_uncond);
2562
2563 midgard_branch_uncond branch = {
2564 .op = op,
2565 .dest_tag = dest_tag,
2566 .offset = quadword_offset,
2567 .unknown = 1
2568 };
2569
2570 assert(branch.offset == quadword_offset);
2571
2572 memcpy(&ins->br_compact, &branch, sizeof(branch));
2573 }
2574 }
2575 }
2576
2577 ++br_block_idx;
2578 }
2579
2580 /* Emit flat binary from the instruction arrays. Iterate each block in
2581 * sequence. Save instruction boundaries such that lookahead tags can
2582 * be assigned easily */
2583
2584 /* Cache _all_ bundles in source order for lookahead across failed branches */
2585
2586 int bundle_count = 0;
2587 mir_foreach_block(ctx, block) {
2588 bundle_count += block->bundles.size / sizeof(midgard_bundle);
2589 }
2590 midgard_bundle **source_order_bundles = malloc(sizeof(midgard_bundle *) * bundle_count);
2591 int bundle_idx = 0;
2592 mir_foreach_block(ctx, block) {
2593 util_dynarray_foreach(&block->bundles, midgard_bundle, bundle) {
2594 source_order_bundles[bundle_idx++] = bundle;
2595 }
2596 }
2597
2598 int current_bundle = 0;
2599
2600 /* Midgard prefetches instruction types, so during emission we
2601 * need to lookahead. Unless this is the last instruction, in
2602 * which we return 1. Or if this is the second to last and the
2603 * last is an ALU, then it's also 1... */
2604
2605 mir_foreach_block(ctx, block) {
2606 util_dynarray_foreach(&block->bundles, midgard_bundle, bundle) {
2607 int lookahead = 1;
2608
2609 if (current_bundle + 1 < bundle_count) {
2610 uint8_t next = source_order_bundles[current_bundle + 1]->tag;
2611
2612 if (!(current_bundle + 2 < bundle_count) && IS_ALU(next)) {
2613 lookahead = 1;
2614 } else {
2615 lookahead = next;
2616 }
2617 }
2618
2619 emit_binary_bundle(ctx, bundle, compiled, lookahead);
2620 ++current_bundle;
2621 }
2622
2623 /* TODO: Free deeper */
2624 //util_dynarray_fini(&block->instructions);
2625 }
2626
2627 free(source_order_bundles);
2628
2629 /* Report the very first tag executed */
2630 program->first_tag = midgard_get_first_tag_from_block(ctx, 0);
2631
2632 /* Deal with off-by-one related to the fencepost problem */
2633 program->work_register_count = ctx->work_registers + 1;
2634
2635 program->can_discard = ctx->can_discard;
2636 program->uniform_cutoff = ctx->uniform_cutoff;
2637
2638 program->blend_patch_offset = ctx->blend_constant_offset;
2639
2640 if (midgard_debug & MIDGARD_DBG_SHADERS)
2641 disassemble_midgard(program->compiled.data, program->compiled.size);
2642
2643 return 0;
2644 }