pan/mdg: Implement raw colourbuf loads on T720
[mesa.git] / src / panfrost / midgard / midgard_ra.c
1 /*
2 * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
3 * Copyright (C) 2019 Collabora, Ltd.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25 #include "compiler.h"
26 #include "midgard_ops.h"
27 #include "util/u_math.h"
28 #include "util/u_memory.h"
29 #include "midgard_quirks.h"
30
31 struct phys_reg {
32 /* Physical register: 0-31 */
33 unsigned reg;
34
35 /* Byte offset into the physical register: 0-15 */
36 unsigned offset;
37
38 /* log2(bytes per component) for fast mul/div */
39 unsigned shift;
40 };
41
42 /* Shift up by reg_offset and horizontally by dst_offset. */
43
44 static void
45 offset_swizzle(unsigned *swizzle, unsigned reg_offset, unsigned srcshift, unsigned dstshift, unsigned dst_offset)
46 {
47 unsigned out[MIR_VEC_COMPONENTS];
48
49 signed reg_comp = reg_offset >> srcshift;
50 signed dst_comp = dst_offset >> dstshift;
51
52 unsigned max_component = (16 >> srcshift) - 1;
53
54 assert(reg_comp << srcshift == reg_offset);
55 assert(dst_comp << dstshift == dst_offset);
56
57 for (signed c = 0; c < MIR_VEC_COMPONENTS; ++c) {
58 signed comp = MAX2(c - dst_comp, 0);
59 out[c] = MIN2(swizzle[comp] + reg_comp, max_component);
60 }
61
62 memcpy(swizzle, out, sizeof(out));
63 }
64
65 /* Helper to return the default phys_reg for a given register */
66
67 static struct phys_reg
68 default_phys_reg(int reg, unsigned shift)
69 {
70 struct phys_reg r = {
71 .reg = reg,
72 .offset = 0,
73 .shift = shift
74 };
75
76 return r;
77 }
78
79 /* Determine which physical register, swizzle, and mask a virtual
80 * register corresponds to */
81
82 static struct phys_reg
83 index_to_reg(compiler_context *ctx, struct lcra_state *l, unsigned reg, unsigned shift)
84 {
85 /* Check for special cases */
86 if (reg == ~0)
87 return default_phys_reg(REGISTER_UNUSED, shift);
88 else if (reg >= SSA_FIXED_MINIMUM)
89 return default_phys_reg(SSA_REG_FROM_FIXED(reg), shift);
90 else if (!l)
91 return default_phys_reg(REGISTER_UNUSED, shift);
92
93 struct phys_reg r = {
94 .reg = l->solutions[reg] / 16,
95 .offset = l->solutions[reg] & 0xF,
96 .shift = shift
97 };
98
99 /* Report that we actually use this register, and return it */
100
101 if (r.reg < 16)
102 ctx->work_registers = MAX2(ctx->work_registers, r.reg);
103
104 return r;
105 }
106
107 static void
108 set_class(unsigned *classes, unsigned node, unsigned class)
109 {
110 if (node < SSA_FIXED_MINIMUM && class != classes[node]) {
111 assert(classes[node] == REG_CLASS_WORK);
112 classes[node] = class;
113 }
114 }
115
116 /* Special register classes impose special constraints on who can read their
117 * values, so check that */
118
119 static bool
120 check_read_class(unsigned *classes, unsigned tag, unsigned node)
121 {
122 /* Non-nodes are implicitly ok */
123 if (node >= SSA_FIXED_MINIMUM)
124 return true;
125
126 switch (classes[node]) {
127 case REG_CLASS_LDST:
128 return (tag == TAG_LOAD_STORE_4);
129 case REG_CLASS_TEXR:
130 return (tag == TAG_TEXTURE_4);
131 case REG_CLASS_TEXW:
132 return (tag != TAG_LOAD_STORE_4);
133 case REG_CLASS_WORK:
134 return IS_ALU(tag);
135 default:
136 unreachable("Invalid class");
137 }
138 }
139
140 static bool
141 check_write_class(unsigned *classes, unsigned tag, unsigned node)
142 {
143 /* Non-nodes are implicitly ok */
144 if (node >= SSA_FIXED_MINIMUM)
145 return true;
146
147 switch (classes[node]) {
148 case REG_CLASS_TEXR:
149 return true;
150 case REG_CLASS_TEXW:
151 return (tag == TAG_TEXTURE_4);
152 case REG_CLASS_LDST:
153 case REG_CLASS_WORK:
154 return IS_ALU(tag) || (tag == TAG_LOAD_STORE_4);
155 default:
156 unreachable("Invalid class");
157 }
158 }
159
160 /* Prepass before RA to ensure special class restrictions are met. The idea is
161 * to create a bit field of types of instructions that read a particular index.
162 * Later, we'll add moves as appropriate and rewrite to specialize by type. */
163
164 static void
165 mark_node_class (unsigned *bitfield, unsigned node)
166 {
167 if (node < SSA_FIXED_MINIMUM)
168 BITSET_SET(bitfield, node);
169 }
170
171 void
172 mir_lower_special_reads(compiler_context *ctx)
173 {
174 size_t sz = BITSET_WORDS(ctx->temp_count) * sizeof(BITSET_WORD);
175
176 /* Bitfields for the various types of registers we could have. aluw can
177 * be written by either ALU or load/store */
178
179 unsigned *alur = calloc(sz, 1);
180 unsigned *aluw = calloc(sz, 1);
181 unsigned *brar = calloc(sz, 1);
182 unsigned *ldst = calloc(sz, 1);
183 unsigned *texr = calloc(sz, 1);
184 unsigned *texw = calloc(sz, 1);
185
186 /* Pass #1 is analysis, a linear scan to fill out the bitfields */
187
188 mir_foreach_instr_global(ctx, ins) {
189 switch (ins->type) {
190 case TAG_ALU_4:
191 mark_node_class(aluw, ins->dest);
192 mark_node_class(alur, ins->src[0]);
193 mark_node_class(alur, ins->src[1]);
194 mark_node_class(alur, ins->src[2]);
195
196 if (ins->compact_branch && ins->writeout)
197 mark_node_class(brar, ins->src[0]);
198
199 break;
200
201 case TAG_LOAD_STORE_4:
202 mark_node_class(aluw, ins->dest);
203 mark_node_class(ldst, ins->src[0]);
204 mark_node_class(ldst, ins->src[1]);
205 mark_node_class(ldst, ins->src[2]);
206 break;
207
208 case TAG_TEXTURE_4:
209 mark_node_class(texr, ins->src[0]);
210 mark_node_class(texr, ins->src[1]);
211 mark_node_class(texr, ins->src[2]);
212 mark_node_class(texw, ins->dest);
213 break;
214 }
215 }
216
217 /* Pass #2 is lowering now that we've analyzed all the classes.
218 * Conceptually, if an index is only marked for a single type of use,
219 * there is nothing to lower. If it is marked for different uses, we
220 * split up based on the number of types of uses. To do so, we divide
221 * into N distinct classes of use (where N>1 by definition), emit N-1
222 * moves from the index to copies of the index, and finally rewrite N-1
223 * of the types of uses to use the corresponding move */
224
225 unsigned spill_idx = ctx->temp_count;
226
227 for (unsigned i = 0; i < ctx->temp_count; ++i) {
228 bool is_alur = BITSET_TEST(alur, i);
229 bool is_aluw = BITSET_TEST(aluw, i);
230 bool is_brar = BITSET_TEST(brar, i);
231 bool is_ldst = BITSET_TEST(ldst, i);
232 bool is_texr = BITSET_TEST(texr, i);
233 bool is_texw = BITSET_TEST(texw, i);
234
235 /* Analyse to check how many distinct uses there are. ALU ops
236 * (alur) can read the results of the texture pipeline (texw)
237 * but not ldst or texr. Load/store ops (ldst) cannot read
238 * anything but load/store inputs. Texture pipeline cannot read
239 * anything but texture inputs. TODO: Simplify. */
240
241 bool collision =
242 (is_alur && (is_ldst || is_texr)) ||
243 (is_ldst && (is_alur || is_texr || is_texw)) ||
244 (is_texr && (is_alur || is_ldst || is_texw)) ||
245 (is_texw && (is_aluw || is_ldst || is_texr)) ||
246 (is_brar && is_texw);
247
248 if (!collision)
249 continue;
250
251 /* Use the index as-is as the work copy. Emit copies for
252 * special uses */
253
254 unsigned classes[] = { TAG_LOAD_STORE_4, TAG_TEXTURE_4, TAG_TEXTURE_4, TAG_ALU_4};
255 bool collisions[] = { is_ldst, is_texr, is_texw && is_aluw, is_brar };
256
257 for (unsigned j = 0; j < ARRAY_SIZE(collisions); ++j) {
258 if (!collisions[j]) continue;
259
260 /* When the hazard is from reading, we move and rewrite
261 * sources (typical case). When it's from writing, we
262 * flip the move and rewrite destinations (obscure,
263 * only from control flow -- impossible in SSA) */
264
265 bool hazard_write = (j == 2);
266
267 unsigned idx = spill_idx++;
268
269 midgard_instruction m = hazard_write ?
270 v_mov(idx, i) : v_mov(i, idx);
271
272 /* Insert move before each read/write, depending on the
273 * hazard we're trying to account for */
274
275 mir_foreach_instr_global_safe(ctx, pre_use) {
276 if (pre_use->type != classes[j])
277 continue;
278
279 if (hazard_write) {
280 if (pre_use->dest != i)
281 continue;
282 } else {
283 if (!mir_has_arg(pre_use, i))
284 continue;
285 }
286
287 if (hazard_write) {
288 midgard_instruction *use = mir_next_op(pre_use);
289 assert(use);
290 mir_insert_instruction_before(ctx, use, m);
291 mir_rewrite_index_dst_single(pre_use, i, idx);
292 } else {
293 idx = spill_idx++;
294 m = v_mov(i, idx);
295 m.mask = mir_from_bytemask(mir_round_bytemask_up(
296 mir_bytemask_of_read_components(pre_use, i), 32), 32);
297 mir_insert_instruction_before(ctx, pre_use, m);
298 mir_rewrite_index_src_single(pre_use, i, idx);
299 }
300 }
301 }
302 }
303
304 free(alur);
305 free(aluw);
306 free(brar);
307 free(ldst);
308 free(texr);
309 free(texw);
310 }
311
312 /* We register allocate after scheduling, so we need to ensure instructions
313 * executing in parallel within a segment of a bundle don't clobber each
314 * other's registers. This is mostly a non-issue thanks to scheduling, but
315 * there are edge cases. In particular, after a register is written in a
316 * segment, it interferes with anything reading. */
317
318 static void
319 mir_compute_segment_interference(
320 compiler_context *ctx,
321 struct lcra_state *l,
322 midgard_bundle *bun,
323 unsigned pivot,
324 unsigned i)
325 {
326 for (unsigned j = pivot; j < i; ++j) {
327 mir_foreach_src(bun->instructions[j], s) {
328 if (bun->instructions[j]->src[s] >= ctx->temp_count)
329 continue;
330
331 for (unsigned q = pivot; q < i; ++q) {
332 if (bun->instructions[q]->dest >= ctx->temp_count)
333 continue;
334
335 /* See dEQP-GLES2.functional.shaders.return.output_write_in_func_dynamic_fragment */
336
337 if (q >= j) {
338 if (!(bun->instructions[j]->unit == UNIT_SMUL && bun->instructions[q]->unit == UNIT_VLUT))
339 continue;
340 }
341
342 unsigned mask = mir_bytemask(bun->instructions[q]);
343 unsigned rmask = mir_bytemask_of_read_components(bun->instructions[j], bun->instructions[j]->src[s]);
344 lcra_add_node_interference(l, bun->instructions[q]->dest, mask, bun->instructions[j]->src[s], rmask);
345 }
346 }
347 }
348 }
349
350 static void
351 mir_compute_bundle_interference(
352 compiler_context *ctx,
353 struct lcra_state *l,
354 midgard_bundle *bun)
355 {
356 if (!IS_ALU(bun->tag))
357 return;
358
359 bool old = bun->instructions[0]->unit >= UNIT_VADD;
360 unsigned pivot = 0;
361
362 for (unsigned i = 1; i < bun->instruction_count; ++i) {
363 bool new = bun->instructions[i]->unit >= UNIT_VADD;
364
365 if (old != new) {
366 mir_compute_segment_interference(ctx, l, bun, 0, i);
367 pivot = i;
368 break;
369 }
370 }
371
372 mir_compute_segment_interference(ctx, l, bun, pivot, bun->instruction_count);
373 }
374
375 static void
376 mir_compute_interference(
377 compiler_context *ctx,
378 struct lcra_state *l)
379 {
380 /* First, we need liveness information to be computed per block */
381 mir_compute_liveness(ctx);
382
383 /* We need to force r1.w live throughout a blend shader */
384
385 if (ctx->is_blend) {
386 unsigned r1w = ~0;
387
388 mir_foreach_block(ctx, _block) {
389 midgard_block *block = (midgard_block *) _block;
390 mir_foreach_instr_in_block_rev(block, ins) {
391 if (ins->writeout)
392 r1w = ins->src[2];
393 }
394
395 if (r1w != ~0)
396 break;
397 }
398
399 mir_foreach_instr_global(ctx, ins) {
400 if (ins->dest < ctx->temp_count)
401 lcra_add_node_interference(l, ins->dest, mir_bytemask(ins), r1w, 0xF);
402 }
403 }
404
405 /* Now that every block has live_in/live_out computed, we can determine
406 * interference by walking each block linearly. Take live_out at the
407 * end of each block and walk the block backwards. */
408
409 mir_foreach_block(ctx, _blk) {
410 midgard_block *blk = (midgard_block *) _blk;
411 uint16_t *live = mem_dup(_blk->live_out, ctx->temp_count * sizeof(uint16_t));
412
413 mir_foreach_instr_in_block_rev(blk, ins) {
414 /* Mark all registers live after the instruction as
415 * interfering with the destination */
416
417 unsigned dest = ins->dest;
418
419 if (dest < ctx->temp_count) {
420 for (unsigned i = 0; i < ctx->temp_count; ++i)
421 if (live[i]) {
422 unsigned mask = mir_bytemask(ins);
423 lcra_add_node_interference(l, dest, mask, i, live[i]);
424 }
425 }
426
427 /* Update live_in */
428 mir_liveness_ins_update(live, ins, ctx->temp_count);
429 }
430
431 mir_foreach_bundle_in_block(blk, bun)
432 mir_compute_bundle_interference(ctx, l, bun);
433
434 free(live);
435 }
436 }
437
438 /* This routine performs the actual register allocation. It should be succeeded
439 * by install_registers */
440
441 static struct lcra_state *
442 allocate_registers(compiler_context *ctx, bool *spilled)
443 {
444 /* The number of vec4 work registers available depends on when the
445 * uniforms start and the shader stage. By ABI we limit blend shaders
446 * to 8 registers, should be lower XXX */
447 int work_count = ctx->is_blend ? 8 :
448 16 - MAX2((ctx->uniform_cutoff - 8), 0);
449
450 /* No register allocation to do with no SSA */
451
452 if (!ctx->temp_count)
453 return NULL;
454
455 struct lcra_state *l = lcra_alloc_equations(ctx->temp_count, 5);
456
457 /* Starts of classes, in bytes */
458 l->class_start[REG_CLASS_WORK] = 16 * 0;
459 l->class_start[REG_CLASS_LDST] = 16 * 26;
460 l->class_start[REG_CLASS_TEXR] = 16 * 28;
461 l->class_start[REG_CLASS_TEXW] = 16 * 28;
462
463 l->class_size[REG_CLASS_WORK] = 16 * work_count;
464 l->class_size[REG_CLASS_LDST] = 16 * 2;
465 l->class_size[REG_CLASS_TEXR] = 16 * 2;
466 l->class_size[REG_CLASS_TEXW] = 16 * 2;
467
468 lcra_set_disjoint_class(l, REG_CLASS_TEXR, REG_CLASS_TEXW);
469
470 /* To save space on T*20, we don't have real texture registers.
471 * Instead, tex inputs reuse the load/store pipeline registers, and
472 * tex outputs use work r0/r1. Note we still use TEXR/TEXW classes,
473 * noting that this handles interferences and sizes correctly. */
474
475 if (ctx->quirks & MIDGARD_INTERPIPE_REG_ALIASING) {
476 l->class_start[REG_CLASS_TEXR] = l->class_start[REG_CLASS_LDST];
477 l->class_start[REG_CLASS_TEXW] = l->class_start[REG_CLASS_WORK];
478 }
479
480 unsigned *found_class = calloc(sizeof(unsigned), ctx->temp_count);
481 unsigned *min_alignment = calloc(sizeof(unsigned), ctx->temp_count);
482 unsigned *min_bound = calloc(sizeof(unsigned), ctx->temp_count);
483
484 mir_foreach_instr_global(ctx, ins) {
485 /* Swizzles of 32-bit sources on 64-bit instructions need to be
486 * aligned to either bottom (xy) or top (zw). More general
487 * swizzle lowering should happen prior to scheduling (TODO),
488 * but once we get RA we shouldn't disrupt this further. Align
489 * sources of 64-bit instructions. */
490
491 if (ins->type == TAG_ALU_4 && ins->alu.reg_mode == midgard_reg_mode_64) {
492 mir_foreach_src(ins, v) {
493 unsigned s = ins->src[v];
494
495 if (s < ctx->temp_count)
496 min_alignment[s] = 3;
497 }
498 }
499
500 if (ins->type == TAG_LOAD_STORE_4 && OP_HAS_ADDRESS(ins->load_store.op)) {
501 mir_foreach_src(ins, v) {
502 unsigned s = ins->src[v];
503 unsigned size = nir_alu_type_get_type_size(ins->src_types[v]);
504
505 if (s < ctx->temp_count)
506 min_alignment[s] = (size == 64) ? 3 : 2;
507 }
508 }
509
510 if (ins->dest >= SSA_FIXED_MINIMUM) continue;
511
512 unsigned size = nir_alu_type_get_type_size(ins->dest_type);
513
514 if (ins->is_pack)
515 size = 32;
516
517 /* 0 for x, 1 for xy, 2 for xyz, 3 for xyzw */
518 int comps1 = util_logbase2(ins->mask);
519
520 int bytes = (comps1 + 1) * (size / 8);
521
522 /* Use the largest class if there's ambiguity, this
523 * handles partial writes */
524
525 int dest = ins->dest;
526 found_class[dest] = MAX2(found_class[dest], bytes);
527
528 min_alignment[dest] =
529 (size == 16) ? 1 : /* (1 << 1) = 2-byte */
530 (size == 32) ? 2 : /* (1 << 2) = 4-byte */
531 (size == 64) ? 3 : /* (1 << 3) = 8-byte */
532 3; /* 8-bit todo */
533
534 /* We can't cross xy/zw boundaries. TODO: vec8 can */
535 if (size == 16)
536 min_bound[dest] = 8;
537
538 /* We don't have a swizzle for the conditional and we don't
539 * want to muck with the conditional itself, so just force
540 * alignment for now */
541
542 if (ins->type == TAG_ALU_4 && OP_IS_CSEL_V(ins->alu.op)) {
543 min_alignment[dest] = 4; /* 1 << 4= 16-byte = vec4 */
544
545 /* LCRA assumes bound >= alignment */
546 min_bound[dest] = 16;
547 }
548
549 /* Since ld/st swizzles and masks are 32-bit only, we need them
550 * aligned to enable final packing */
551 if (ins->type == TAG_LOAD_STORE_4)
552 min_alignment[dest] = MAX2(min_alignment[dest], 2);
553 }
554
555 for (unsigned i = 0; i < ctx->temp_count; ++i) {
556 lcra_set_alignment(l, i, min_alignment[i] ? min_alignment[i] : 2,
557 min_bound[i] ? min_bound[i] : 16);
558 lcra_restrict_range(l, i, found_class[i]);
559 }
560
561 free(found_class);
562 free(min_alignment);
563 free(min_bound);
564
565 /* Next, we'll determine semantic class. We default to zero (work).
566 * But, if we're used with a special operation, that will force us to a
567 * particular class. Each node must be assigned to exactly one class; a
568 * prepass before RA should have lowered what-would-have-been
569 * multiclass nodes into a series of moves to break it up into multiple
570 * nodes (TODO) */
571
572 mir_foreach_instr_global(ctx, ins) {
573 /* Check if this operation imposes any classes */
574
575 if (ins->type == TAG_LOAD_STORE_4) {
576 set_class(l->class, ins->src[0], REG_CLASS_LDST);
577 set_class(l->class, ins->src[1], REG_CLASS_LDST);
578 set_class(l->class, ins->src[2], REG_CLASS_LDST);
579
580 if (OP_IS_VEC4_ONLY(ins->load_store.op)) {
581 lcra_restrict_range(l, ins->dest, 16);
582 lcra_restrict_range(l, ins->src[0], 16);
583 lcra_restrict_range(l, ins->src[1], 16);
584 lcra_restrict_range(l, ins->src[2], 16);
585 }
586 } else if (ins->type == TAG_TEXTURE_4) {
587 set_class(l->class, ins->dest, REG_CLASS_TEXW);
588 set_class(l->class, ins->src[0], REG_CLASS_TEXR);
589 set_class(l->class, ins->src[1], REG_CLASS_TEXR);
590 set_class(l->class, ins->src[2], REG_CLASS_TEXR);
591 set_class(l->class, ins->src[3], REG_CLASS_TEXR);
592 }
593 }
594
595 /* Check that the semantics of the class are respected */
596 mir_foreach_instr_global(ctx, ins) {
597 assert(check_write_class(l->class, ins->type, ins->dest));
598 assert(check_read_class(l->class, ins->type, ins->src[0]));
599 assert(check_read_class(l->class, ins->type, ins->src[1]));
600 assert(check_read_class(l->class, ins->type, ins->src[2]));
601 }
602
603 /* Mark writeout to r0, render target to r1.z, unknown to r1.w */
604 mir_foreach_instr_global(ctx, ins) {
605 if (!(ins->compact_branch && ins->writeout)) continue;
606
607 if (ins->src[0] < ctx->temp_count) {
608 if (ins->writeout_depth)
609 l->solutions[ins->src[0]] = (16 * 1) + COMPONENT_X * 4;
610 else if (ins->writeout_stencil)
611 l->solutions[ins->src[0]] = (16 * 1) + COMPONENT_Y * 4;
612 else
613 l->solutions[ins->src[0]] = 0;
614 }
615
616 if (ins->src[1] < ctx->temp_count)
617 l->solutions[ins->src[1]] = (16 * 1) + COMPONENT_Z * 4;
618
619 if (ins->src[2] < ctx->temp_count)
620 l->solutions[ins->src[2]] = (16 * 1) + COMPONENT_W * 4;
621 }
622
623 mir_compute_interference(ctx, l);
624
625 *spilled = !lcra_solve(l);
626 return l;
627 }
628
629
630 /* Once registers have been decided via register allocation
631 * (allocate_registers), we need to rewrite the MIR to use registers instead of
632 * indices */
633
634 static void
635 install_registers_instr(
636 compiler_context *ctx,
637 struct lcra_state *l,
638 midgard_instruction *ins)
639 {
640 unsigned src_shift[MIR_SRC_COUNT];
641
642 for (unsigned i = 0; i < MIR_SRC_COUNT; ++i) {
643 src_shift[i] =
644 util_logbase2(nir_alu_type_get_type_size(ins->src_types[i]) / 8);
645 }
646
647 unsigned dest_shift =
648 util_logbase2(nir_alu_type_get_type_size(ins->dest_type) / 8);
649
650 switch (ins->type) {
651 case TAG_ALU_4:
652 case TAG_ALU_8:
653 case TAG_ALU_12:
654 case TAG_ALU_16: {
655 if (ins->compact_branch)
656 return;
657
658 struct phys_reg src1 = index_to_reg(ctx, l, ins->src[0], src_shift[0]);
659 struct phys_reg src2 = index_to_reg(ctx, l, ins->src[1], src_shift[1]);
660 struct phys_reg dest = index_to_reg(ctx, l, ins->dest, dest_shift);
661
662 mir_set_bytemask(ins, mir_bytemask(ins) << dest.offset);
663
664 unsigned dest_offset =
665 GET_CHANNEL_COUNT(alu_opcode_props[ins->alu.op].props) ? 0 :
666 dest.offset;
667
668 offset_swizzle(ins->swizzle[0], src1.offset, src1.shift, dest.shift, dest_offset);
669
670 ins->registers.src1_reg = src1.reg;
671
672 ins->registers.src2_imm = ins->has_inline_constant;
673
674 if (ins->has_inline_constant) {
675 /* Encode inline 16-bit constant. See disassembler for
676 * where the algorithm is from */
677
678 ins->registers.src2_reg = ins->inline_constant >> 11;
679
680 int lower_11 = ins->inline_constant & ((1 << 12) - 1);
681 uint16_t imm = ((lower_11 >> 8) & 0x7) |
682 ((lower_11 & 0xFF) << 3);
683
684 ins->alu.src2 = imm << 2;
685 } else {
686 offset_swizzle(ins->swizzle[1], src2.offset, src2.shift, dest.shift, dest_offset);
687
688 ins->registers.src2_reg = src2.reg;
689 }
690
691 ins->registers.out_reg = dest.reg;
692 break;
693 }
694
695 case TAG_LOAD_STORE_4: {
696 /* Which physical register we read off depends on
697 * whether we are loading or storing -- think about the
698 * logical dataflow */
699
700 bool encodes_src = OP_IS_STORE(ins->load_store.op);
701
702 if (encodes_src) {
703 struct phys_reg src = index_to_reg(ctx, l, ins->src[0], src_shift[0]);
704 assert(src.reg == 26 || src.reg == 27);
705
706 ins->load_store.reg = src.reg - 26;
707 offset_swizzle(ins->swizzle[0], src.offset, src.shift, 0, 0);
708 } else {
709 struct phys_reg dst = index_to_reg(ctx, l, ins->dest, dest_shift);
710
711 ins->load_store.reg = dst.reg;
712 offset_swizzle(ins->swizzle[0], 0, 2, 2, dst.offset);
713 mir_set_bytemask(ins, mir_bytemask(ins) << dst.offset);
714 }
715
716 /* We also follow up by actual arguments */
717
718 unsigned src2 = ins->src[1];
719 unsigned src3 = ins->src[2];
720
721 if (src2 != ~0) {
722 struct phys_reg src = index_to_reg(ctx, l, src2, 2);
723 unsigned component = src.offset >> src.shift;
724 assert(component << src.shift == src.offset);
725 ins->load_store.arg_1 |= midgard_ldst_reg(src.reg, component);
726 }
727
728 if (src3 != ~0) {
729 struct phys_reg src = index_to_reg(ctx, l, src3, 2);
730 unsigned component = src.offset >> src.shift;
731 assert(component << src.shift == src.offset);
732 ins->load_store.arg_2 |= midgard_ldst_reg(src.reg, component);
733 }
734
735 break;
736 }
737
738 case TAG_TEXTURE_4: {
739 if (ins->texture.op == TEXTURE_OP_BARRIER)
740 break;
741
742 /* Grab RA results */
743 struct phys_reg dest = index_to_reg(ctx, l, ins->dest, dest_shift);
744 struct phys_reg coord = index_to_reg(ctx, l, ins->src[1], src_shift[1]);
745 struct phys_reg lod = index_to_reg(ctx, l, ins->src[2], src_shift[2]);
746 struct phys_reg offset = index_to_reg(ctx, l, ins->src[3], src_shift[3]);
747
748 /* First, install the texture coordinate */
749 ins->texture.in_reg_select = coord.reg & 1;
750 offset_swizzle(ins->swizzle[1], coord.offset, coord.shift, dest.shift, 0);
751
752 /* Next, install the destination */
753 ins->texture.out_reg_select = dest.reg & 1;
754 offset_swizzle(ins->swizzle[0], 0, 2, dest.shift,
755 dest_shift == 1 ? dest.offset % 8 :
756 dest.offset);
757 mir_set_bytemask(ins, mir_bytemask(ins) << dest.offset);
758
759 /* If there is a register LOD/bias, use it */
760 if (ins->src[2] != ~0) {
761 assert(!(lod.offset & 3));
762 midgard_tex_register_select sel = {
763 .select = lod.reg & 1,
764 .full = 1,
765 .component = lod.offset / 4
766 };
767
768 uint8_t packed;
769 memcpy(&packed, &sel, sizeof(packed));
770 ins->texture.bias = packed;
771 }
772
773 /* If there is an offset register, install it */
774 if (ins->src[3] != ~0) {
775 unsigned x = offset.offset / 4;
776 unsigned y = x + 1;
777 unsigned z = x + 2;
778
779 /* Check range, TODO: half-registers */
780 assert(z < 4);
781
782 ins->texture.offset =
783 (1) | /* full */
784 (offset.reg & 1) << 1 | /* select */
785 (0 << 2) | /* upper */
786 (x << 3) | /* swizzle */
787 (y << 5) | /* swizzle */
788 (z << 7); /* swizzle */
789 }
790
791 break;
792 }
793
794 default:
795 break;
796 }
797 }
798
799 static void
800 install_registers(compiler_context *ctx, struct lcra_state *l)
801 {
802 mir_foreach_instr_global(ctx, ins)
803 install_registers_instr(ctx, l, ins);
804 }
805
806
807 /* If register allocation fails, find the best spill node */
808
809 static signed
810 mir_choose_spill_node(
811 compiler_context *ctx,
812 struct lcra_state *l)
813 {
814 /* We can't spill a previously spilled value or an unspill */
815
816 mir_foreach_instr_global(ctx, ins) {
817 if (ins->no_spill & (1 << l->spill_class)) {
818 lcra_set_node_spill_cost(l, ins->dest, -1);
819
820 if (l->spill_class != REG_CLASS_WORK) {
821 mir_foreach_src(ins, s)
822 lcra_set_node_spill_cost(l, ins->src[s], -1);
823 }
824 }
825 }
826
827 return lcra_get_best_spill_node(l);
828 }
829
830 /* Once we've chosen a spill node, spill it */
831
832 static void
833 mir_spill_register(
834 compiler_context *ctx,
835 unsigned spill_node,
836 unsigned spill_class,
837 unsigned *spill_count)
838 {
839 if (spill_class == REG_CLASS_WORK && ctx->is_blend)
840 unreachable("Blend shader spilling is currently unimplemented");
841
842 unsigned spill_index = ctx->temp_count;
843
844 /* We have a spill node, so check the class. Work registers
845 * legitimately spill to TLS, but special registers just spill to work
846 * registers */
847
848 bool is_special = spill_class != REG_CLASS_WORK;
849 bool is_special_w = spill_class == REG_CLASS_TEXW;
850
851 /* Allocate TLS slot (maybe) */
852 unsigned spill_slot = !is_special ? (*spill_count)++ : 0;
853
854 /* For TLS, replace all stores to the spilled node. For
855 * special reads, just keep as-is; the class will be demoted
856 * implicitly. For special writes, spill to a work register */
857
858 if (!is_special || is_special_w) {
859 if (is_special_w)
860 spill_slot = spill_index++;
861
862 mir_foreach_block(ctx, _block) {
863 midgard_block *block = (midgard_block *) _block;
864 mir_foreach_instr_in_block_safe(block, ins) {
865 if (ins->dest != spill_node) continue;
866
867 midgard_instruction st;
868
869 if (is_special_w) {
870 st = v_mov(spill_node, spill_slot);
871 st.no_spill |= (1 << spill_class);
872 } else {
873 ins->dest = spill_index++;
874 ins->no_spill |= (1 << spill_class);
875 st = v_load_store_scratch(ins->dest, spill_slot, true, ins->mask);
876 }
877
878 /* Hint: don't rewrite this node */
879 st.hint = true;
880
881 mir_insert_instruction_after_scheduled(ctx, block, ins, st);
882
883 if (!is_special)
884 ctx->spills++;
885 }
886 }
887 }
888
889 /* For special reads, figure out how many bytes we need */
890 unsigned read_bytemask = 0;
891
892 mir_foreach_instr_global_safe(ctx, ins) {
893 read_bytemask |= mir_bytemask_of_read_components(ins, spill_node);
894 }
895
896 /* Insert a load from TLS before the first consecutive
897 * use of the node, rewriting to use spilled indices to
898 * break up the live range. Or, for special, insert a
899 * move. Ironically the latter *increases* register
900 * pressure, but the two uses of the spilling mechanism
901 * are somewhat orthogonal. (special spilling is to use
902 * work registers to back special registers; TLS
903 * spilling is to use memory to back work registers) */
904
905 mir_foreach_block(ctx, _block) {
906 midgard_block *block = (midgard_block *) _block;
907 mir_foreach_instr_in_block(block, ins) {
908 /* We can't rewrite the moves used to spill in the
909 * first place. These moves are hinted. */
910 if (ins->hint) continue;
911
912 /* If we don't use the spilled value, nothing to do */
913 if (!mir_has_arg(ins, spill_node)) continue;
914
915 unsigned index = 0;
916
917 if (!is_special_w) {
918 index = ++spill_index;
919
920 midgard_instruction *before = ins;
921 midgard_instruction st;
922
923 if (is_special) {
924 /* Move */
925 st = v_mov(spill_node, index);
926 st.no_spill |= (1 << spill_class);
927 } else {
928 /* TLS load */
929 st = v_load_store_scratch(index, spill_slot, false, 0xF);
930 }
931
932 /* Mask the load based on the component count
933 * actually needed to prevent RA loops */
934
935 st.mask = mir_from_bytemask(mir_round_bytemask_up(
936 read_bytemask, 32), 32);
937
938 mir_insert_instruction_before_scheduled(ctx, block, before, st);
939 } else {
940 /* Special writes already have their move spilled in */
941 index = spill_slot;
942 }
943
944
945 /* Rewrite to use */
946 mir_rewrite_index_src_single(ins, spill_node, index);
947
948 if (!is_special)
949 ctx->fills++;
950 }
951 }
952
953 /* Reset hints */
954
955 mir_foreach_instr_global(ctx, ins) {
956 ins->hint = false;
957 }
958 }
959
960 /* Run register allocation in a loop, spilling until we succeed */
961
962 void
963 mir_ra(compiler_context *ctx)
964 {
965 struct lcra_state *l = NULL;
966 bool spilled = false;
967 int iter_count = 1000; /* max iterations */
968
969 /* Number of 128-bit slots in memory we've spilled into */
970 unsigned spill_count = 0;
971
972
973 mir_create_pipeline_registers(ctx);
974
975 do {
976 if (spilled) {
977 signed spill_node = mir_choose_spill_node(ctx, l);
978
979 if (spill_node == -1) {
980 fprintf(stderr, "ERROR: Failed to choose spill node\n");
981 return;
982 }
983
984 mir_spill_register(ctx, spill_node, l->spill_class, &spill_count);
985 }
986
987 mir_squeeze_index(ctx);
988 mir_invalidate_liveness(ctx);
989
990 if (l) {
991 lcra_free(l);
992 l = NULL;
993 }
994
995 l = allocate_registers(ctx, &spilled);
996 } while(spilled && ((iter_count--) > 0));
997
998 if (iter_count <= 0) {
999 fprintf(stderr, "panfrost: Gave up allocating registers, rendering will be incomplete\n");
1000 assert(0);
1001 }
1002
1003 /* Report spilling information. spill_count is in 128-bit slots (vec4 x
1004 * fp32), but tls_size is in bytes, so multiply by 16 */
1005
1006 ctx->tls_size = spill_count * 16;
1007
1008 install_registers(ctx, l);
1009
1010 lcra_free(l);
1011 }