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