Debian Codegraph @

src/panfrost/compiler/midgard/midgard_emit.c

raw
1 /*
2 * Copyright (C) 2018-2019 Alyssa Rosenzweig
3 * Copyright (C) 2019-2020 Collabora, Ltd.
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "compiler.h"
8 #include "midgard_ops.h"
9 #include "midgard_quirks.h"
10
11 static midgard_int_mod
12 mir_get_imod(bool shift, nir_alu_type T, bool half, bool scalar)
13 {
14 if (!half) {
15 assert(!shift);
16 /* Doesn't matter, src mods are only used when expanding */
17 return midgard_int_sign_extend;
18 }
19
20 if (shift)
21 return midgard_int_left_shift;
22
23 if (nir_alu_type_get_base_type(T) == nir_type_int)
24 return midgard_int_sign_extend;
25 else
26 return midgard_int_zero_extend;
27 }
28
29 void
30 midgard_pack_ubo_index_imm(midgard_load_store_word *word, unsigned index)
31 {
32 word->arg_comp = index & 0x3;
33 word->arg_reg = (index >> 2) & 0x7;
34 word->bitsize_toggle = (index >> 5) & 0x1;
35 word->index_format = (index >> 6) & 0x3;
36 }
37
38 void
39 midgard_pack_varying_params(midgard_load_store_word *word,
40 midgard_varying_params p)
41 {
42 /* Currently these parameters are not supported. */
43 assert(p.direct_sample_pos_x == 0 && p.direct_sample_pos_y == 0);
44
45 unsigned u;
46 memcpy(&u, &p, sizeof(p));
47
48 word->signed_offset |= u & 0x1FF;
49 }
50
51 midgard_varying_params
52 midgard_unpack_varying_params(midgard_load_store_word word)
53 {
54 unsigned params = word.signed_offset & 0x1FF;
55
56 midgard_varying_params p;
57 memcpy(&p, &params, sizeof(p));
58
59 return p;
60 }
61
62 unsigned
63 mir_pack_mod(const midgard_instruction *ins, unsigned i, bool scalar)
64 {
65 bool integer = midgard_is_integer_op(ins->op);
66 unsigned base_size = max_bitsize_for_alu(ins);
67 unsigned sz = nir_alu_type_get_type_size(ins->src_types[i]);
68 bool half = (sz == (base_size >> 1));
69
70 return integer
71 ? mir_get_imod(ins->src_shift[i], ins->src_types[i], half, scalar)
72 : ((ins->src_abs[i] << 0) | ((ins->src_neg[i] << 1)));
73 }
74
75 /* Midgard IR only knows vector ALU types, but we sometimes need to actually
76 * use scalar ALU instructions, for functional or performance reasons. To do
77 * this, we just demote vector ALU payloads to scalar. */
78
79 static int
80 component_from_mask(unsigned mask)
81 {
82 for (int c = 0; c < 8; ++c) {
83 if (mask & (1 << c))
84 return c;
85 }
86
87 assert(0);
88 return 0;
89 }
90
91 static unsigned
92 mir_pack_scalar_source(unsigned mod, bool is_full, unsigned component)
93 {
94 midgard_scalar_alu_src s = {
95 .mod = mod,
96 .full = is_full,
97 .component = component << (is_full ? 1 : 0),
98 };
99
100 unsigned o;
101 memcpy(&o, &s, sizeof(s));
102
103 return o & ((1 << 6) - 1);
104 }
105
106 static midgard_scalar_alu
107 vector_to_scalar_alu(midgard_vector_alu v, midgard_instruction *ins)
108 {
109 bool is_full = nir_alu_type_get_type_size(ins->dest_type) == 32;
110
111 bool half_0 = nir_alu_type_get_type_size(ins->src_types[0]) == 16;
112 bool half_1 = nir_alu_type_get_type_size(ins->src_types[1]) == 16;
113 unsigned comp = component_from_mask(ins->mask);
114
115 unsigned packed_src[2] = {
116 mir_pack_scalar_source(mir_pack_mod(ins, 0, true), !half_0,
117 ins->swizzle[0][comp]),
118 mir_pack_scalar_source(mir_pack_mod(ins, 1, true), !half_1,
119 ins->swizzle[1][comp])};
120
121 /* The output component is from the mask */
122 midgard_scalar_alu s = {
123 .op = v.op,
124 .src1 = packed_src[0],
125 .src2 = packed_src[1],
126 .outmod = v.outmod,
127 .output_full = is_full,
128 .output_component = comp,
129 };
130
131 /* Full components are physically spaced out */
132 if (is_full) {
133 assert(s.output_component < 4);
134 s.output_component <<= 1;
135 }
136
137 /* Inline constant is passed along rather than trying to extract it
138 * from v */
139
140 if (ins->has_inline_constant) {
141 uint16_t imm = 0;
142 int lower_11 = ins->inline_constant & ((1 << 12) - 1);
143 imm |= (lower_11 >> 9) & 3;
144 imm |= (lower_11 >> 6) & 4;
145 imm |= (lower_11 >> 2) & 0x38;
146 imm |= (lower_11 & 63) << 6;
147
148 s.src2 = imm;
149 }
150
151 return s;
152 }
153
154 /* 64-bit swizzles are super easy since there are 2 components of 2 components
155 * in an 8-bit field ... lots of duplication to go around!
156 *
157 * Swizzles of 32-bit vectors accessed from 64-bit instructions are a little
158 * funny -- pack them *as if* they were native 64-bit, using rep_* flags to
159 * flag upper. For instance, xy would become 64-bit XY but that's just xyzw
160 * native. Likewise, zz would become 64-bit XX with rep* so it would be xyxy
161 * with rep. Pretty nifty, huh? */
162
163 static unsigned
164 mir_pack_swizzle_64(unsigned *swizzle, unsigned max_component, bool expand_high)
165 {
166 unsigned packed = 0;
167 unsigned base = expand_high ? 2 : 0;
168
169 for (unsigned i = base; i < base + 2; ++i) {
170 assert(swizzle[i] <= max_component);
171
172 unsigned a = (swizzle[i] & 1) ? (COMPONENT_W << 2) | COMPONENT_Z
173 : (COMPONENT_Y << 2) | COMPONENT_X;
174
175 if (i & 1)
176 packed |= a << 4;
177 else
178 packed |= a;
179 }
180
181 return packed;
182 }
183
184 static void
185 mir_pack_mask_alu(midgard_instruction *ins, midgard_vector_alu *alu)
186 {
187 unsigned effective = ins->mask;
188
189 /* If we have a destination override, we need to figure out whether to
190 * override to the lower or upper half, shifting the effective mask in
191 * the latter, so AAAA.... becomes AAAA */
192
193 unsigned inst_size = max_bitsize_for_alu(ins);
194 signed upper_shift = mir_upper_override(ins, inst_size);
195
196 if (upper_shift >= 0) {
197 effective >>= upper_shift;
198 alu->shrink_mode =
199 upper_shift ? midgard_shrink_mode_upper : midgard_shrink_mode_lower;
200 } else {
201 alu->shrink_mode = midgard_shrink_mode_none;
202 }
203
204 if (inst_size == 32)
205 alu->mask = expand_writemask(effective, 2);
206 else if (inst_size == 64)
207 alu->mask = expand_writemask(effective, 1);
208 else
209 alu->mask = effective;
210 }
211
212 static unsigned
213 mir_pack_swizzle(unsigned mask, unsigned *swizzle, unsigned sz,
214 unsigned base_size, bool op_channeled,
215 midgard_src_expand_mode *expand_mode)
216 {
217 unsigned packed = 0;
218
219 *expand_mode = midgard_src_passthrough;
220
221 midgard_reg_mode reg_mode = reg_mode_for_bitsize(base_size);
222
223 if (reg_mode == midgard_reg_mode_64) {
224 assert(sz == 64 || sz == 32);
225 unsigned components = (sz == 32) ? 4 : 2;
226
227 packed = mir_pack_swizzle_64(swizzle, components, mask & 0xc);
228
229 if (sz == 32) {
230 ASSERTED bool dontcare = true;
231 bool hi = false;
232
233 assert(util_bitcount(mask) <= 2);
234
235 u_foreach_bit(i, mask) {
236 bool hi_i = swizzle[i] >= COMPONENT_Z;
237
238 /* We can't mix halves */
239 assert(dontcare || (hi == hi_i));
240 hi = hi_i;
241 dontcare = false;
242 }
243
244 *expand_mode = hi ? midgard_src_expand_high : midgard_src_expand_low;
245 } else if (sz < 32) {
246 UNREACHABLE("Cannot encode 8/16 swizzle in 64-bit");
247 }
248 } else {
249 /* For 32-bit, swizzle packing is stupid-simple. For 16-bit,
250 * the strategy is to check whether the nibble we're on is
251 * upper or lower. We need all components to be on the same
252 * "side"; that much is enforced by the ISA and should have
253 * been lowered. TODO: 8-bit packing. TODO: vec8 */
254
255 unsigned first = mask ? ffs(mask) - 1 : 0;
256 bool upper = swizzle[first] > 3;
257
258 if (upper && mask)
259 assert(sz <= 16);
260
261 bool dest_up = !op_channeled && (first >= 4);
262
263 for (unsigned c = (dest_up ? 4 : 0); c < (dest_up ? 8 : 4); ++c) {
264 unsigned v = swizzle[c];
265
266 ASSERTED bool t_upper = v > (sz == 8 ? 7 : 3);
267
268 /* Ensure we're doing something sane */
269
270 if (mask & (1 << c)) {
271 assert(t_upper == upper);
272 assert(v <= (sz == 8 ? 15 : 7));
273 }
274
275 /* Use the non upper part */
276 v &= 0x3;
277
278 packed |= v << (2 * (c % 4));
279 }
280
281 /* Replicate for now.. should really pick a side for
282 * dot products */
283
284 if (reg_mode == midgard_reg_mode_16 && sz == 16) {
285 *expand_mode = upper ? midgard_src_rep_high : midgard_src_rep_low;
286 } else if (reg_mode == midgard_reg_mode_16 && sz == 8) {
287 if (base_size == 16) {
288 *expand_mode =
289 upper ? midgard_src_expand_high : midgard_src_expand_low;
290 } else if (upper) {
291 *expand_mode = midgard_src_swap;
292 }
293 } else if (reg_mode == midgard_reg_mode_32 && sz == 16) {
294 *expand_mode =
295 upper ? midgard_src_expand_high : midgard_src_expand_low;
296 } else if (reg_mode == midgard_reg_mode_8) {
297 UNREACHABLE("Unhandled reg mode");
298 }
299 }
300
301 return packed;
302 }
303
304 static void
305 mir_pack_vector_srcs(midgard_instruction *ins, midgard_vector_alu *alu)
306 {
307 bool channeled = GET_CHANNEL_COUNT(alu_opcode_props[ins->op].props);
308
309 unsigned base_size = max_bitsize_for_alu(ins);
310
311 for (unsigned i = 0; i < 2; ++i) {
312 if (ins->has_inline_constant && (i == 1))
313 continue;
314
315 if (ins->src[i] == ~0)
316 continue;
317
318 unsigned sz = nir_alu_type_get_type_size(ins->src_types[i]);
319 assert((sz == base_size) || (sz == base_size / 2));
320
321 midgard_src_expand_mode expand_mode = midgard_src_passthrough;
322 unsigned swizzle = mir_pack_swizzle(ins->mask, ins->swizzle[i], sz,
323 base_size, channeled, &expand_mode);
324
325 midgard_vector_alu_src pack = {
326 .mod = mir_pack_mod(ins, i, false),
327 .expand_mode = expand_mode,
328 .swizzle = swizzle,
329 };
330
331 unsigned p = vector_alu_srco_unsigned(pack);
332
333 if (i == 0)
334 alu->src1 = p;
335 else
336 alu->src2 = p;
337 }
338 }
339
340 static void
341 mir_pack_swizzle_ldst(midgard_instruction *ins)
342 {
343 unsigned compsz = OP_IS_STORE(ins->op)
344 ? nir_alu_type_get_type_size(ins->src_types[0])
345 : nir_alu_type_get_type_size(ins->dest_type);
346 unsigned maxcomps = 128 / compsz;
347 unsigned step = DIV_ROUND_UP(32, compsz);
348
349 for (unsigned c = 0; c < maxcomps; c += step) {
350 unsigned v = ins->swizzle[0][c];
351
352 /* Make sure the component index doesn't exceed the maximum
353 * number of components. */
354 assert(v <= maxcomps);
355
356 if (compsz <= 32)
357 ins->load_store.swizzle |= (v / step) << (2 * (c / step));
358 else
359 ins->load_store.swizzle |=
360 ((v / step) << (4 * c)) | (((v / step) + 1) << ((4 * c) + 2));
361 }
362
363 /* TODO: arg_1/2 */
364 }
365
366 static void
367 mir_pack_swizzle_tex(midgard_instruction *ins)
368 {
369 for (unsigned i = 0; i < 2; ++i) {
370 unsigned packed = 0;
371
372 for (unsigned c = 0; c < 4; ++c) {
373 unsigned v = ins->swizzle[i][c];
374
375 /* Check vec4 */
376 assert(v <= 3);
377
378 packed |= v << (2 * c);
379 }
380
381 if (i == 0)
382 ins->texture.swizzle = packed;
383 else
384 ins->texture.in_reg_swizzle = packed;
385 }
386
387 /* TODO: bias component */
388 }
389
390 /*
391 * Up to 15 { ALU, LDST } bundles can execute in parallel with a texture op.
392 * Given a texture op, lookahead to see how many such bundles we can flag for
393 * OoO execution
394 */
395 static bool
396 mir_can_run_ooo(midgard_block *block, midgard_bundle *bundle,
397 unsigned dependency)
398 {
399 /* Don't read out of bounds */
400 if (bundle >=
401 (midgard_bundle *)((char *)block->bundles.data + block->bundles.size))
402 return false;
403
404 /* Texture ops can't execute with other texture ops */
405 if (!IS_ALU(bundle->tag) && bundle->tag != TAG_LOAD_STORE_4)
406 return false;
407
408 for (unsigned i = 0; i < bundle->instruction_count; ++i) {
409 midgard_instruction *ins = bundle->instructions[i];
410
411 /* No branches, jumps, or discards */
412 if (ins->compact_branch)
413 return false;
414
415 /* No read-after-write data dependencies */
416 mir_foreach_src(ins, s) {
417 if (ins->src[s] == dependency)
418 return false;
419 }
420 }
421
422 /* Otherwise, we're okay */
423 return true;
424 }
425
426 static void
427 mir_pack_tex_ooo(midgard_block *block, midgard_bundle *bundle,
428 midgard_instruction *ins)
429 {
430 unsigned count = 0;
431
432 for (count = 0; count < 15; ++count) {
433 if (!mir_can_run_ooo(block, bundle + count + 1, ins->dest))
434 break;
435 }
436
437 ins->texture.out_of_order = count;
438 }
439
440 /* Load store masks are 4-bits. Load/store ops pack for that.
441 * For most operations, vec4 is the natural mask width; vec8 is constrained to
442 * be in pairs, vec2 is duplicated. TODO: 8-bit?
443 * For common stores (i.e. ST.*), each bit masks a single byte in the 32-bit
444 * case, 2 bytes in the 64-bit case and 4 bytes in the 128-bit case.
445 */
446
447 static unsigned
448 midgard_pack_common_store_mask(midgard_instruction *ins)
449 {
450 ASSERTED unsigned comp_sz = nir_alu_type_get_type_size(ins->src_types[0]);
451 unsigned bytemask = mir_bytemask(ins);
452 unsigned packed = 0;
453
454 switch (ins->op) {
455 case midgard_op_st_u8:
456 return mir_bytemask(ins) & 1;
457 case midgard_op_st_u16:
458 return mir_bytemask(ins) & 3;
459 case midgard_op_st_32:
460 return mir_bytemask(ins);
461 case midgard_op_st_64:
462 assert(comp_sz >= 16);
463 for (unsigned i = 0; i < 4; i++) {
464 if (bytemask & (3 << (i * 2)))
465 packed |= 1 << i;
466 }
467 return packed;
468 case midgard_op_st_128:
469 assert(comp_sz >= 32);
470 for (unsigned i = 0; i < 4; i++) {
471 if (bytemask & (0xf << (i * 4)))
472 packed |= 1 << i;
473 }
474 return packed;
475 default:
476 UNREACHABLE("unexpected ldst opcode");
477 }
478 }
479
480 static void
481 mir_pack_ldst_mask(midgard_instruction *ins)
482 {
483 unsigned sz = nir_alu_type_get_type_size(ins->dest_type);
484 unsigned packed = ins->mask;
485
486 if (OP_IS_COMMON_STORE(ins->op)) {
487 packed = midgard_pack_common_store_mask(ins);
488 } else {
489 if (sz == 64) {
490 packed = ((ins->mask & 0x2) ? (0x8 | 0x4) : 0) |
491 ((ins->mask & 0x1) ? (0x2 | 0x1) : 0);
492 } else if (sz < 32) {
493 unsigned comps_per_32b = 32 / sz;
494
495 packed = 0;
496
497 for (unsigned i = 0; i < 4; ++i) {
498 unsigned submask = (ins->mask >> (i * comps_per_32b)) &
499 BITFIELD_MASK(comps_per_32b);
500
501 /* Make sure we're duplicated */
502 assert(submask == 0 || submask == BITFIELD_MASK(comps_per_32b));
503 packed |= (submask != 0) << i;
504 }
505 } else {
506 assert(sz == 32);
507 }
508 }
509
510 ins->load_store.mask = packed;
511 }
512
513 static void
514 mir_lower_inverts(midgard_instruction *ins)
515 {
516 bool inv[3] = {ins->src_invert[0], ins->src_invert[1], ins->src_invert[2]};
517
518 switch (ins->op) {
519 case midgard_alu_op_iand:
520 /* a & ~b = iandnot(a, b) */
521 /* ~a & ~b = ~(a | b) = inor(a, b) */
522
523 if (inv[0] && inv[1])
524 ins->op = midgard_alu_op_inor;
525 else if (inv[1])
526 ins->op = midgard_alu_op_iandnot;
527
528 break;
529 case midgard_alu_op_ior:
530 /* a | ~b = iornot(a, b) */
531 /* ~a | ~b = ~(a & b) = inand(a, b) */
532
533 if (inv[0] && inv[1])
534 ins->op = midgard_alu_op_inand;
535 else if (inv[1])
536 ins->op = midgard_alu_op_iornot;
537
538 break;
539
540 case midgard_alu_op_ixor:
541 /* ~a ^ b = a ^ ~b = ~(a ^ b) = inxor(a, b) */
542 /* ~a ^ ~b = a ^ b */
543
544 if (inv[0] ^ inv[1])
545 ins->op = midgard_alu_op_inxor;
546
547 break;
548
549 default:
550 break;
551 }
552 }
553
554 /* Opcodes with ROUNDS are the base (rte/0) type so we can just add */
555
556 static void
557 mir_lower_roundmode(midgard_instruction *ins)
558 {
559 if (alu_opcode_props[ins->op].props & MIDGARD_ROUNDS) {
560 assert(ins->roundmode <= 0x3);
561 ins->op += ins->roundmode;
562 }
563 }
564
565 static midgard_load_store_word
566 load_store_from_instr(midgard_instruction *ins)
567 {
568 midgard_load_store_word ldst = ins->load_store;
569 ldst.op = ins->op;
570
571 if (OP_IS_STORE(ldst.op)) {
572 ldst.reg = SSA_REG_FROM_FIXED(ins->src[0]) & 1;
573 } else {
574 ldst.reg = SSA_REG_FROM_FIXED(ins->dest);
575 }
576
577 /* Atomic opcode swizzles have a special meaning:
578 * - The first two bits say which component of the implicit register should
579 * be used
580 * - The next two bits say if the implicit register is r26 or r27 */
581 if (OP_IS_ATOMIC(ins->op)) {
582 ldst.swizzle = 0;
583 ldst.swizzle |= ins->swizzle[3][0] & 3;
584 ldst.swizzle |= (SSA_REG_FROM_FIXED(ins->src[3]) & 1 ? 1 : 0) << 2;
585 }
586
587 if (ins->src[1] != ~0) {
588 ldst.arg_reg = SSA_REG_FROM_FIXED(ins->src[1]) - REGISTER_LDST_BASE;
589 unsigned sz = nir_alu_type_get_type_size(ins->src_types[1]);
590 ldst.arg_comp = midgard_ldst_comp(ldst.arg_reg, ins->swizzle[1][0], sz);
591 }
592
593 if (ins->src[2] != ~0) {
594 ldst.index_reg = SSA_REG_FROM_FIXED(ins->src[2]) - REGISTER_LDST_BASE;
595 unsigned sz = nir_alu_type_get_type_size(ins->src_types[2]);
596 ldst.index_comp =
597 midgard_ldst_comp(ldst.index_reg, ins->swizzle[2][0], sz);
598 }
599
600 return ldst;
601 }
602
603 static midgard_texture_word
604 texture_word_from_instr(midgard_instruction *ins)
605 {
606 midgard_texture_word tex = ins->texture;
607 tex.op = ins->op;
608
609 unsigned src1 =
610 ins->src[1] == ~0 ? REGISTER_UNUSED : SSA_REG_FROM_FIXED(ins->src[1]);
611 tex.in_reg_select = src1 & 1;
612
613 unsigned dest =
614 ins->dest == ~0 ? REGISTER_UNUSED : SSA_REG_FROM_FIXED(ins->dest);
615 tex.out_reg_select = dest & 1;
616
617 if (ins->src[2] != ~0) {
618 midgard_tex_register_select sel = {
619 .select = SSA_REG_FROM_FIXED(ins->src[2]) & 1,
620 .full = 1,
621 .component = ins->swizzle[2][0],
622 };
623 uint8_t packed;
624 memcpy(&packed, &sel, sizeof(packed));
625 tex.bias = packed;
626 }
627
628 if (ins->src[3] != ~0) {
629 unsigned x = ins->swizzle[3][0];
630 unsigned y = x + 1;
631 unsigned z = x + 2;
632
633 /* Check range, TODO: half-registers */
634 assert(z < 4);
635
636 unsigned offset_reg = SSA_REG_FROM_FIXED(ins->src[3]);
637 tex.offset = (1) | /* full */
638 (offset_reg & 1) << 1 | /* select */
639 (0 << 2) | /* upper */
640 (x << 3) | /* swizzle */
641 (y << 5) | /* swizzle */
642 (z << 7); /* swizzle */
643 }
644
645 return tex;
646 }
647
648 static midgard_vector_alu
649 vector_alu_from_instr(midgard_instruction *ins)
650 {
651 midgard_vector_alu alu = {
652 .op = ins->op,
653 .outmod = ins->outmod,
654 .reg_mode = reg_mode_for_bitsize(max_bitsize_for_alu(ins)),
655 };
656
657 if (ins->has_inline_constant) {
658 /* Encode inline 16-bit constant. See disassembler for
659 * where the algorithm is from */
660
661 int lower_11 = ins->inline_constant & ((1 << 12) - 1);
662 uint16_t imm = ((lower_11 >> 8) & 0x7) | ((lower_11 & 0xFF) << 3);
663
664 alu.src2 = imm << 2;
665 }
666
667 return alu;
668 }
669
670 static midgard_branch_extended
671 midgard_create_branch_extended(midgard_condition cond,
672 midgard_jmp_writeout_op op, unsigned dest_tag,
673 signed quadword_offset)
674 {
675 /* The condition code is actually a LUT describing a function to
676 * combine multiple condition codes. However, we only support a single
677 * condition code at the moment, so we just duplicate over a bunch of
678 * times. */
679
680 uint16_t duplicated_cond = (cond << 14) | (cond << 12) | (cond << 10) |
681 (cond << 8) | (cond << 6) | (cond << 4) |
682 (cond << 2) | (cond << 0);
683
684 midgard_branch_extended branch = {
685 .op = op,
686 .dest_tag = dest_tag,
687 .offset = quadword_offset,
688 .cond = duplicated_cond,
689 };
690
691 return branch;
692 }
693
694 static void
695 emit_branch(midgard_instruction *ins, compiler_context *ctx,
696 midgard_block *block, midgard_bundle *bundle,
697 struct util_dynarray *emission)
698 {
699 /* Parse some basic branch info */
700 bool is_compact = ins->unit == ALU_ENAB_BR_COMPACT;
701 bool is_conditional = ins->branch.conditional;
702 bool is_inverted = ins->branch.invert_conditional;
703 bool is_discard = ins->branch.target_type == TARGET_DISCARD;
704 bool is_tilebuf_wait = ins->branch.target_type == TARGET_TILEBUF_WAIT;
705 bool is_special = is_discard || is_tilebuf_wait;
706 bool is_writeout = ins->writeout;
707
708 /* Determine the block we're jumping to */
709 int target_number = ins->branch.target_block;
710
711 /* Report the destination tag */
712 int dest_tag = is_discard ? 0
713 : is_tilebuf_wait
714 ? bundle->tag
715 : midgard_get_first_tag_from_block(ctx, target_number);
716
717 /* Count up the number of quadwords we're
718 * jumping over = number of quadwords until
719 * (br_block_idx, target_number) */
720
721 int quadword_offset = 0;
722
723 if (is_discard) {
724 /* Fixed encoding, not actually an offset */
725 quadword_offset = 0x2;
726 } else if (is_tilebuf_wait) {
727 quadword_offset = -1;
728 } else if (target_number > block->name) {
729 /* Jump forward */
730
731 for (int idx = block->name + 1; idx < target_number; ++idx) {
732 midgard_block *blk = mir_get_block(ctx, idx);
733 assert(blk);
734
735 quadword_offset += blk->quadword_count;
736 }
737 } else {
738 /* Jump backwards */
739
740 for (int idx = block->name; idx >= target_number; --idx) {
741 midgard_block *blk = mir_get_block(ctx, idx);
742 assert(blk);
743
744 quadword_offset -= blk->quadword_count;
745 }
746 }
747
748 /* Unconditional extended branches (far jumps)
749 * have issues, so we always use a conditional
750 * branch, setting the condition to always for
751 * unconditional. For compact unconditional
752 * branches, cond isn't used so it doesn't
753 * matter what we pick. */
754
755 midgard_condition cond = !is_conditional ? midgard_condition_always
756 : is_inverted ? midgard_condition_false
757 : midgard_condition_true;
758
759 midgard_jmp_writeout_op op =
760 is_discard ? midgard_jmp_writeout_op_discard
761 : is_tilebuf_wait ? midgard_jmp_writeout_op_tilebuffer_pending
762 : is_writeout ? midgard_jmp_writeout_op_writeout
763 : (is_compact && !is_conditional) ? midgard_jmp_writeout_op_branch_uncond
764 : midgard_jmp_writeout_op_branch_cond;
765
766 if (is_compact) {
767 unsigned size = sizeof(midgard_branch_cond);
768
769 if (is_conditional || is_special) {
770 midgard_branch_cond branch = {
771 .op = op,
772 .dest_tag = dest_tag,
773 .offset = quadword_offset,
774 .cond = cond,
775 };
776 memcpy(util_dynarray_grow_bytes(emission, size, 1), &branch, size);
777 } else {
778 assert(op == midgard_jmp_writeout_op_branch_uncond);
779 midgard_branch_uncond branch = {
780 .op = op,
781 .dest_tag = dest_tag,
782 .offset = quadword_offset,
783 .call_mode = midgard_call_mode_default,
784 };
785 assert(branch.offset == quadword_offset);
786 memcpy(util_dynarray_grow_bytes(emission, size, 1), &branch, size);
787 }
788 } else { /* `ins->compact_branch`, misnomer */
789 unsigned size = sizeof(midgard_branch_extended);
790
791 midgard_branch_extended branch =
792 midgard_create_branch_extended(cond, op, dest_tag, quadword_offset);
793
794 memcpy(util_dynarray_grow_bytes(emission, size, 1), &branch, size);
795 }
796 }
797
798 static void
799 emit_alu_bundle(compiler_context *ctx, midgard_block *block,
800 midgard_bundle *bundle, struct util_dynarray *emission,
801 unsigned lookahead)
802 {
803 /* Emit the control word */
804 util_dynarray_append_typed(emission, uint32_t, bundle->control | lookahead);
805
806 /* Next up, emit register words */
807 for (unsigned i = 0; i < bundle->instruction_count; ++i) {
808 midgard_instruction *ins = bundle->instructions[i];
809
810 /* Check if this instruction has registers */
811 if (ins->compact_branch)
812 continue;
813
814 unsigned src2_reg = REGISTER_UNUSED;
815 if (ins->has_inline_constant)
816 src2_reg = ins->inline_constant >> 11;
817 else if (ins->src[1] != ~0)
818 src2_reg = SSA_REG_FROM_FIXED(ins->src[1]);
819
820 /* Otherwise, just emit the registers */
821 uint16_t reg_word = 0;
822 midgard_reg_info registers = {
823 .src1_reg = (ins->src[0] == ~0 ? REGISTER_UNUSED
824 : SSA_REG_FROM_FIXED(ins->src[0])),
825 .src2_reg = src2_reg,
826 .src2_imm = ins->has_inline_constant,
827 .out_reg =
828 (ins->dest == ~0 ? REGISTER_UNUSED : SSA_REG_FROM_FIXED(ins->dest)),
829 };
830 memcpy(&reg_word, &registers, sizeof(uint16_t));
831 util_dynarray_append(emission, reg_word);
832 }
833
834 /* Now, we emit the body itself */
835 for (unsigned i = 0; i < bundle->instruction_count; ++i) {
836 midgard_instruction *ins = bundle->instructions[i];
837
838 if (!ins->compact_branch) {
839 mir_lower_inverts(ins);
840 mir_lower_roundmode(ins);
841 }
842
843 if (midgard_is_branch_unit(ins->unit)) {
844 emit_branch(ins, ctx, block, bundle, emission);
845 } else if (ins->unit & UNITS_ANY_VECTOR) {
846 midgard_vector_alu source = vector_alu_from_instr(ins);
847 mir_pack_mask_alu(ins, &source);
848 mir_pack_vector_srcs(ins, &source);
849 unsigned size = sizeof(source);
850 memcpy(util_dynarray_grow_bytes(emission, size, 1), &source, size);
851 } else {
852 midgard_scalar_alu source =
853 vector_to_scalar_alu(vector_alu_from_instr(ins), ins);
854 unsigned size = sizeof(source);
855 memcpy(util_dynarray_grow_bytes(emission, size, 1), &source, size);
856 }
857 }
858
859 /* Emit padding (all zero) */
860 if (bundle->padding) {
861 memset(util_dynarray_grow_bytes(emission, bundle->padding, 1), 0,
862 bundle->padding);
863 }
864
865 /* Tack on constants */
866
867 if (bundle->has_embedded_constants)
868 util_dynarray_append_typed(emission, midgard_constants, bundle->constants);
869 }
870
871 /* Shift applied to the immediate used as an offset. Probably this is papering
872 * over some other semantic distinction else well, but it unifies things in the
873 * compiler so I don't mind. */
874
875 static void
876 mir_ldst_pack_offset(midgard_instruction *ins, int offset)
877 {
878 /* These opcodes don't support offsets */
879 assert(!OP_IS_REG2REG_LDST(ins->op) || ins->op == midgard_op_lea ||
880 ins->op == midgard_op_lea_image);
881
882 if (OP_IS_UBO_READ(ins->op))
883 ins->load_store.signed_offset |= PACK_LDST_UBO_OFS(offset);
884 else if (OP_IS_IMAGE(ins->op))
885 ins->load_store.signed_offset |= PACK_LDST_ATTRIB_OFS(offset);
886 else if (OP_IS_SPECIAL(ins->op))
887 ins->load_store.signed_offset |= PACK_LDST_SELECTOR_OFS(offset);
888 else
889 ins->load_store.signed_offset |= PACK_LDST_MEM_OFS(offset);
890 }
891
892 static enum mali_sampler_type
893 midgard_sampler_type(nir_alu_type t)
894 {
895 switch (nir_alu_type_get_base_type(t)) {
896 case nir_type_float:
897 return MALI_SAMPLER_FLOAT;
898 case nir_type_int:
899 return MALI_SAMPLER_SIGNED;
900 case nir_type_uint:
901 return MALI_SAMPLER_UNSIGNED;
902 default:
903 UNREACHABLE("Unknown sampler type");
904 }
905 }
906
907 /* After everything is scheduled, emit whole bundles at a time */
908
909 void
910 emit_binary_bundle(compiler_context *ctx, midgard_block *block,
911 midgard_bundle *bundle, struct util_dynarray *emission,
912 int next_tag)
913 {
914 int lookahead = next_tag << 4;
915
916 switch (bundle->tag) {
917 case TAG_ALU_4:
918 case TAG_ALU_8:
919 case TAG_ALU_12:
920 case TAG_ALU_16:
921 case TAG_ALU_4 + 4:
922 case TAG_ALU_8 + 4:
923 case TAG_ALU_12 + 4:
924 case TAG_ALU_16 + 4:
925 emit_alu_bundle(ctx, block, bundle, emission, lookahead);
926 break;
927
928 case TAG_LOAD_STORE_4: {
929 /* One or two composing instructions */
930
931 uint64_t current64, next64 = LDST_NOP;
932
933 /* Copy masks */
934
935 for (unsigned i = 0; i < bundle->instruction_count; ++i) {
936 midgard_instruction *ins = bundle->instructions[i];
937 mir_pack_ldst_mask(ins);
938
939 /* Atomic ops don't use this swizzle the same way as other ops */
940 if (!OP_IS_ATOMIC(ins->op))
941 mir_pack_swizzle_ldst(ins);
942
943 /* Apply a constant offset */
944 unsigned offset = ins->constants.u32[0];
945 if (offset)
946 mir_ldst_pack_offset(ins, offset);
947 }
948
949 midgard_load_store_word ldst0 =
950 load_store_from_instr(bundle->instructions[0]);
951 memcpy(&current64, &ldst0, sizeof(current64));
952
953 if (bundle->instruction_count == 2) {
954 midgard_load_store_word ldst1 =
955 load_store_from_instr(bundle->instructions[1]);
956 memcpy(&next64, &ldst1, sizeof(next64));
957 }
958
959 midgard_load_store instruction = {
960 .type = bundle->tag,
961 .next_type = next_tag,
962 .word1 = current64,
963 .word2 = next64,
964 };
965
966 util_dynarray_append(emission, instruction);
967
968 break;
969 }
970
971 case TAG_TEXTURE_4:
972 case TAG_TEXTURE_4_VTX:
973 case TAG_TEXTURE_4_BARRIER: {
974 /* Texture instructions are easy, since there is no pipelining
975 * nor VLIW to worry about. We may need to set .cont/.last
976 * flags. */
977
978 midgard_instruction *ins = bundle->instructions[0];
979
980 ins->texture.type = bundle->tag;
981 ins->texture.next_type = next_tag;
982 ins->texture.exec = MIDGARD_PARTIAL_EXECUTION_NONE; /* default */
983
984 /* Nothing else to pack for barriers */
985 if (ins->op == midgard_tex_op_barrier) {
986 ins->texture.op = ins->op;
987 util_dynarray_append(emission, ins->texture);
988 return;
989 }
990
991 signed override = mir_upper_override(ins, 32);
992
993 ins->texture.mask = override > 0 ? ins->mask >> override : ins->mask;
994
995 mir_pack_swizzle_tex(ins);
996
997 if (!(ctx->quirks & MIDGARD_NO_OOO))
998 mir_pack_tex_ooo(block, bundle, ins);
999
1000 unsigned osz = nir_alu_type_get_type_size(ins->dest_type);
1001 unsigned isz = nir_alu_type_get_type_size(ins->src_types[1]);
1002
1003 assert(osz == 32 || osz == 16);
1004 assert(isz == 32 || isz == 16);
1005
1006 ins->texture.out_full = (osz == 32);
1007 ins->texture.out_upper = override > 0;
1008 ins->texture.in_reg_full = (isz == 32);
1009 ins->texture.sampler_type = midgard_sampler_type(ins->dest_type);
1010 ins->texture.outmod = ins->outmod;
1011
1012 if (mir_op_computes_derivatives(ctx->stage, ins->op)) {
1013 if (ins->helper_terminate)
1014 ins->texture.exec = MIDGARD_PARTIAL_EXECUTION_KILL;
1015 else if (!ins->helper_execute)
1016 ins->texture.exec = MIDGARD_PARTIAL_EXECUTION_SKIP;
1017 }
1018
1019 midgard_texture_word texture = texture_word_from_instr(ins);
1020 util_dynarray_append(emission, texture);
1021 break;
1022 }
1023
1024 default:
1025 UNREACHABLE("Unknown midgard instruction type\n");
1026 }
1027 }