Debian Codegraph @

src/panfrost/compiler/midgard/midgard_ra.c

raw
1 /*
2 * Copyright (C) 2018-2019 Alyssa Rosenzweig
3 * Copyright (C) 2019 Collabora, Ltd.
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "util/u_math.h"
8 #include "util/u_memory.h"
9 #include "compiler.h"
10 #include "midgard_ops.h"
11 #include "midgard_quirks.h"
12
13 struct lcra_state {
14 unsigned node_count;
15
16 /* Alignment for node in log2(bytes)+1. Since alignment must be
17 * non-negative power-of-two, the elements are strictly positive
18 * integers. Zero is the sentinel for a missing node. In upper word,
19 * bound. */
20 unsigned *alignment;
21
22 /* Linear constraints imposed. Nested array sized upfront, organized as
23 * linear[node_left][node_right]. That is, calculate indices as:
24 *
25 * Each element is itself a bit field denoting whether (c_j - c_i) bias
26 * is present or not, including negative biases.
27 *
28 * Note for Midgard, there are 16 components so the bias is in range
29 * [-15, 15] so encoded by 32-bit field. */
30
31 uint32_t *linear;
32
33 /* Per node max modulus constraints */
34 uint8_t *modulus;
35
36 /* Classes allow nodes to be partitioned with a starting register.
37 * Classes cannot interfere; that is, they are true partitions in the
38 * usual sense of the word. class_count is the number of classes.
39 * class[] is indexed by a node to get the mapped class. class_start is
40 * biased to all solutions in the class. */
41
42 unsigned class_count;
43 unsigned *class;
44 unsigned *class_start;
45 unsigned *class_size;
46 bool *class_disjoint;
47
48 /* Before solving, forced registers; after solving, solutions. */
49 unsigned *solutions;
50
51 /* For register spilling, the costs to spill nodes (as set by the user)
52 * are in spill_cost[], negative if a node is unspillable. Internally,
53 * spill_class specifies which class to spill (whichever class failed
54 * to allocate) */
55
56 signed *spill_cost;
57 unsigned spill_class;
58 };
59
60 /* This module is the reference implementation of "Linearly Constrained
61 * Register Allocation". The paper is available in PDF form
62 * (https://people.collabora.com/~alyssa/LCRA.pdf) as well as Markdown+LaTeX
63 * (https://gitlab.freedesktop.org/alyssa/lcra/blob/master/LCRA.md)
64 */
65
66 static struct lcra_state *
67 lcra_alloc_equations(unsigned node_count, unsigned class_count)
68 {
69 struct lcra_state *l = calloc(1, sizeof(*l));
70
71 l->node_count = node_count;
72 l->class_count = class_count;
73
74 l->alignment = calloc(sizeof(l->alignment[0]), node_count);
75 l->linear = calloc(sizeof(l->linear[0]), node_count * node_count);
76 l->modulus = calloc(sizeof(l->modulus[0]), node_count);
77 l->class = calloc(sizeof(l->class[0]), node_count);
78 l->class_start = calloc(sizeof(l->class_start[0]), class_count);
79 l->class_disjoint =
80 calloc(sizeof(l->class_disjoint[0]), class_count * class_count);
81 l->class_size = calloc(sizeof(l->class_size[0]), class_count);
82 l->spill_cost = calloc(sizeof(l->spill_cost[0]), node_count);
83 l->solutions = calloc(sizeof(l->solutions[0]), node_count);
84
85 memset(l->solutions, ~0, sizeof(l->solutions[0]) * node_count);
86
87 return l;
88 }
89
90 static void
91 lcra_free(struct lcra_state *l)
92 {
93 if (!l)
94 return;
95
96 free(l->alignment);
97 free(l->linear);
98 free(l->modulus);
99 free(l->class);
100 free(l->class_start);
101 free(l->class_disjoint);
102 free(l->class_size);
103 free(l->spill_cost);
104 free(l->solutions);
105
106 free(l);
107 }
108
109 static void
110 lcra_set_alignment(struct lcra_state *l, unsigned node, unsigned align_log2,
111 unsigned bound)
112 {
113 l->alignment[node] = (align_log2 + 1) | (bound << 16);
114 }
115
116 static void
117 lcra_set_disjoint_class(struct lcra_state *l, unsigned c1, unsigned c2)
118 {
119 l->class_disjoint[(c1 * l->class_count) + c2] = true;
120 l->class_disjoint[(c2 * l->class_count) + c1] = true;
121 }
122
123 static void
124 lcra_restrict_range(struct lcra_state *l, unsigned node, unsigned len)
125 {
126 if (node < l->node_count && l->alignment[node]) {
127 unsigned BA = l->alignment[node];
128 unsigned alignment = (BA & 0xffff) - 1;
129 unsigned bound = BA >> 16;
130 l->modulus[node] = DIV_ROUND_UP(bound - len + 1, 1 << alignment);
131 }
132 }
133
134 static void
135 lcra_add_node_interference(struct lcra_state *l, unsigned i, unsigned cmask_i,
136 unsigned j, unsigned cmask_j)
137 {
138 if (i == j)
139 return;
140
141 if (l->class_disjoint[(l->class[i] * l->class_count) + l->class[j]])
142 return;
143
144 uint32_t constraint_fw = 0;
145 uint32_t constraint_bw = 0;
146
147 for (unsigned D = 0; D < 16; ++D) {
148 if (cmask_i & (cmask_j << D)) {
149 constraint_bw |= (1 << (15 + D));
150 constraint_fw |= (1 << (15 - D));
151 }
152
153 if (cmask_i & (cmask_j >> D)) {
154 constraint_fw |= (1 << (15 + D));
155 constraint_bw |= (1 << (15 - D));
156 }
157 }
158
159 l->linear[j * l->node_count + i] |= constraint_fw;
160 l->linear[i * l->node_count + j] |= constraint_bw;
161 }
162
163 static bool
164 lcra_test_linear(struct lcra_state *l, unsigned *solutions, unsigned i)
165 {
166 unsigned *row = &l->linear[i * l->node_count];
167 signed constant = solutions[i];
168
169 for (unsigned j = 0; j < l->node_count; ++j) {
170 if (solutions[j] == ~0)
171 continue;
172
173 signed lhs = solutions[j] - constant;
174
175 if (lhs < -15 || lhs > 15)
176 continue;
177
178 if (row[j] & (1 << (lhs + 15)))
179 return false;
180 }
181
182 return true;
183 }
184
185 static bool
186 lcra_solve(struct lcra_state *l)
187 {
188 for (unsigned step = 0; step < l->node_count; ++step) {
189 if (l->solutions[step] != ~0)
190 continue;
191 if (l->alignment[step] == 0)
192 continue;
193
194 unsigned _class = l->class[step];
195 unsigned class_start = l->class_start[_class];
196
197 unsigned BA = l->alignment[step];
198 unsigned shift = (BA & 0xffff) - 1;
199 unsigned bound = BA >> 16;
200
201 unsigned P = bound >> shift;
202 unsigned Q = l->modulus[step];
203 unsigned r_max = l->class_size[_class];
204 unsigned k_max = r_max >> shift;
205 unsigned m_max = k_max / P;
206 bool succ = false;
207
208 for (unsigned m = 0; m < m_max; ++m) {
209 for (unsigned n = 0; n < Q; ++n) {
210 l->solutions[step] = ((m * P + n) << shift) + class_start;
211 succ = lcra_test_linear(l, l->solutions, step);
212
213 if (succ)
214 break;
215 }
216
217 if (succ)
218 break;
219 }
220
221 /* Out of registers - prepare to spill */
222 if (!succ) {
223 l->spill_class = l->class[step];
224 return false;
225 }
226 }
227
228 return true;
229 }
230
231 /* Register spilling is implemented with a cost-benefit system. Costs are set
232 * by the user. Benefits are calculated from the constraints. */
233
234 static void
235 lcra_set_node_spill_cost(struct lcra_state *l, unsigned node, signed cost)
236 {
237 if (node < l->node_count)
238 l->spill_cost[node] = cost;
239 }
240
241 static unsigned
242 lcra_count_constraints(struct lcra_state *l, unsigned i)
243 {
244 unsigned count = 0;
245 unsigned *constraints = &l->linear[i * l->node_count];
246
247 for (unsigned j = 0; j < l->node_count; ++j)
248 count += util_bitcount(constraints[j]);
249
250 return count;
251 }
252
253 static signed
254 lcra_get_best_spill_node(struct lcra_state *l)
255 {
256 /* If there are no constraints on a node, do not pick it to spill under
257 * any circumstance, or else we would hang rather than fail RA */
258 float best_benefit = 0.0;
259 signed best_node = -1;
260
261 for (unsigned i = 0; i < l->node_count; ++i) {
262 /* Find spillable nodes */
263 if (l->class[i] != l->spill_class)
264 continue;
265 if (l->spill_cost[i] < 0)
266 continue;
267
268 /* Adapted from Chaitin's heuristic */
269 float constraints = lcra_count_constraints(l, i);
270 float cost = (l->spill_cost[i] + 1);
271 float benefit = constraints / cost;
272
273 if (benefit > best_benefit) {
274 best_benefit = benefit;
275 best_node = i;
276 }
277 }
278
279 return best_node;
280 }
281
282 struct phys_reg {
283 /* Physical register: 0-31 */
284 unsigned reg;
285
286 /* Byte offset into the physical register: 0-15 */
287 unsigned offset;
288
289 /* log2(bytes per component) for fast mul/div */
290 unsigned shift;
291 };
292
293 /* Shift up by reg_offset and horizontally by dst_offset. */
294
295 static void
296 offset_swizzle(unsigned *swizzle, unsigned reg_offset, unsigned srcshift,
297 unsigned dstshift, unsigned dst_offset)
298 {
299 unsigned out[MIR_VEC_COMPONENTS];
300
301 signed reg_comp = reg_offset >> srcshift;
302 signed dst_comp = dst_offset >> dstshift;
303
304 unsigned max_component = (16 >> srcshift) - 1;
305
306 assert(reg_comp << srcshift == reg_offset);
307 assert(dst_comp << dstshift == dst_offset);
308
309 for (signed c = 0; c < MIR_VEC_COMPONENTS; ++c) {
310 signed comp = MAX2(c - dst_comp, 0);
311 out[c] = MIN2(swizzle[comp] + reg_comp, max_component);
312 }
313
314 memcpy(swizzle, out, sizeof(out));
315 }
316
317 /* Helper to return the default phys_reg for a given register */
318
319 static struct phys_reg
320 default_phys_reg(int reg, unsigned shift)
321 {
322 struct phys_reg r = {
323 .reg = reg,
324 .offset = 0,
325 .shift = shift,
326 };
327
328 return r;
329 }
330
331 /* Determine which physical register, swizzle, and mask a virtual
332 * register corresponds to */
333
334 static struct phys_reg
335 index_to_reg(compiler_context *ctx, struct lcra_state *l, unsigned reg,
336 unsigned shift)
337 {
338 /* Check for special cases */
339 if (reg == ~0)
340 return default_phys_reg(REGISTER_UNUSED, shift);
341 else if (reg >= SSA_FIXED_MINIMUM)
342 return default_phys_reg(SSA_REG_FROM_FIXED(reg), shift);
343 else if (!l)
344 return default_phys_reg(REGISTER_UNUSED, shift);
345
346 struct phys_reg r = {
347 .reg = l->solutions[reg] / 16,
348 .offset = l->solutions[reg] & 0xF,
349 .shift = shift,
350 };
351
352 /* Report that we actually use this register, and return it */
353
354 if (r.reg < 16)
355 ctx->info->work_reg_count = MAX2(ctx->info->work_reg_count, r.reg + 1);
356
357 return r;
358 }
359
360 static void
361 set_class(unsigned *classes, unsigned node, unsigned class)
362 {
363 if (node < SSA_FIXED_MINIMUM && class != classes[node]) {
364 assert(classes[node] == REG_CLASS_WORK);
365 classes[node] = class;
366 }
367 }
368
369 /* Special register classes impose special constraints on who can read their
370 * values, so check that */
371
372 static bool ASSERTED
373 check_read_class(unsigned *classes, unsigned tag, unsigned node)
374 {
375 /* Non-nodes are implicitly ok */
376 if (node >= SSA_FIXED_MINIMUM)
377 return true;
378
379 switch (classes[node]) {
380 case REG_CLASS_LDST:
381 return (tag == TAG_LOAD_STORE_4);
382 case REG_CLASS_TEXR:
383 return (tag == TAG_TEXTURE_4);
384 case REG_CLASS_TEXW:
385 return (tag != TAG_LOAD_STORE_4);
386 case REG_CLASS_WORK:
387 return IS_ALU(tag);
388 default:
389 UNREACHABLE("Invalid class");
390 }
391 }
392
393 static bool ASSERTED
394 check_write_class(unsigned *classes, unsigned tag, unsigned node)
395 {
396 /* Non-nodes are implicitly ok */
397 if (node >= SSA_FIXED_MINIMUM)
398 return true;
399
400 switch (classes[node]) {
401 case REG_CLASS_TEXR:
402 return true;
403 case REG_CLASS_TEXW:
404 return (tag == TAG_TEXTURE_4);
405 case REG_CLASS_LDST:
406 case REG_CLASS_WORK:
407 return IS_ALU(tag) || (tag == TAG_LOAD_STORE_4);
408 default:
409 UNREACHABLE("Invalid class");
410 }
411 }
412
413 /* Prepass before RA to ensure special class restrictions are met. The idea is
414 * to create a bit field of types of instructions that read a particular index.
415 * Later, we'll add moves as appropriate and rewrite to specialize by type. */
416
417 static void
418 mark_node_class(unsigned *bitfield, unsigned node)
419 {
420 if (node < SSA_FIXED_MINIMUM)
421 BITSET_SET(bitfield, node);
422 }
423
424 void
425 mir_lower_special_reads(compiler_context *ctx)
426 {
427 mir_compute_temp_count(ctx);
428 size_t sz = BITSET_WORDS(ctx->temp_count) * sizeof(BITSET_WORD);
429
430 /* Bitfields for the various types of registers we could have. aluw can
431 * be written by either ALU or load/store */
432
433 unsigned *alur = calloc(sz, 1);
434 unsigned *aluw = calloc(sz, 1);
435 unsigned *brar = calloc(sz, 1);
436 unsigned *ldst = calloc(sz, 1);
437 unsigned *texr = calloc(sz, 1);
438 unsigned *texw = calloc(sz, 1);
439
440 /* Pass #1 is analysis, a linear scan to fill out the bitfields */
441
442 mir_foreach_instr_global(ctx, ins) {
443 switch (ins->type) {
444 case TAG_ALU_4:
445 mark_node_class(aluw, ins->dest);
446 mark_node_class(alur, ins->src[0]);
447 mark_node_class(alur, ins->src[1]);
448 mark_node_class(alur, ins->src[2]);
449
450 if (ins->compact_branch && ins->writeout)
451 mark_node_class(brar, ins->src[0]);
452
453 break;
454
455 case TAG_LOAD_STORE_4:
456 mark_node_class(aluw, ins->dest);
457 mark_node_class(ldst, ins->src[0]);
458 mark_node_class(ldst, ins->src[1]);
459 mark_node_class(ldst, ins->src[2]);
460 mark_node_class(ldst, ins->src[3]);
461 break;
462
463 case TAG_TEXTURE_4:
464 mark_node_class(texr, ins->src[0]);
465 mark_node_class(texr, ins->src[1]);
466 mark_node_class(texr, ins->src[2]);
467 mark_node_class(texw, ins->dest);
468 break;
469
470 default:
471 break;
472 }
473 }
474
475 /* Pass #2 is lowering now that we've analyzed all the classes.
476 * Conceptually, if an index is only marked for a single type of use,
477 * there is nothing to lower. If it is marked for different uses, we
478 * split up based on the number of types of uses. To do so, we divide
479 * into N distinct classes of use (where N>1 by definition), emit N-1
480 * moves from the index to copies of the index, and finally rewrite N-1
481 * of the types of uses to use the corresponding move */
482
483 unsigned spill_idx = ctx->temp_count;
484
485 for (unsigned i = 0; i < ctx->temp_count; ++i) {
486 bool is_alur = BITSET_TEST(alur, i);
487 bool is_aluw = BITSET_TEST(aluw, i);
488 bool is_brar = BITSET_TEST(brar, i);
489 bool is_ldst = BITSET_TEST(ldst, i);
490 bool is_texr = BITSET_TEST(texr, i);
491 bool is_texw = BITSET_TEST(texw, i);
492
493 /* Analyse to check how many distinct uses there are. ALU ops
494 * (alur) can read the results of the texture pipeline (texw)
495 * but not ldst or texr. Load/store ops (ldst) cannot read
496 * anything but load/store inputs. Texture pipeline cannot read
497 * anything but texture inputs. TODO: Simplify. */
498
499 bool collision = (is_alur && (is_ldst || is_texr)) ||
500 (is_ldst && (is_alur || is_texr || is_texw)) ||
501 (is_texr && (is_alur || is_ldst || is_texw)) ||
502 (is_texw && (is_aluw || is_ldst || is_texr)) ||
503 (is_brar && is_texw);
504
505 if (!collision)
506 continue;
507
508 /* Use the index as-is as the work copy. Emit copies for
509 * special uses */
510
511 unsigned classes[] = {TAG_LOAD_STORE_4, TAG_TEXTURE_4, TAG_TEXTURE_4,
512 TAG_ALU_4};
513 bool collisions[] = {is_ldst, is_texr, is_texw && is_aluw, is_brar};
514
515 for (unsigned j = 0; j < ARRAY_SIZE(collisions); ++j) {
516 if (!collisions[j])
517 continue;
518
519 /* When the hazard is from reading, we move and rewrite
520 * sources (typical case). When it's from writing, we
521 * flip the move and rewrite destinations (obscure,
522 * only from control flow -- impossible in SSA) */
523
524 bool hazard_write = (j == 2);
525
526 unsigned idx = spill_idx++;
527
528 /* Insert move before each read/write, depending on the
529 * hazard we're trying to account for */
530
531 mir_foreach_block(ctx, block_) {
532 midgard_block *block = (midgard_block *)block_;
533 midgard_instruction *mov = NULL;
534
535 mir_foreach_instr_in_block_safe(block, pre_use) {
536 if (pre_use->type != classes[j])
537 continue;
538
539 if (hazard_write) {
540 if (pre_use->dest != i)
541 continue;
542
543 midgard_instruction m = v_mov(idx, i);
544 m.dest_type = pre_use->dest_type;
545 m.src_types[1] = m.dest_type;
546 m.mask = pre_use->mask;
547
548 midgard_instruction *use = mir_next_op(pre_use);
549 assert(use);
550 mir_insert_instruction_before(ctx, use, &m);
551 mir_rewrite_index_dst_single(pre_use, i, idx);
552 } else {
553 if (!mir_has_arg(pre_use, i))
554 continue;
555
556 unsigned mask = mir_from_bytemask(
557 mir_round_bytemask_up(
558 mir_bytemask_of_read_components(pre_use, i), 32),
559 32);
560
561 if (mov == NULL || !mir_is_ssa(i)) {
562 midgard_instruction m = v_mov(i, spill_idx++);
563 m.mask = mask;
564 mov = mir_insert_instruction_before(ctx, pre_use, &m);
565 } else {
566 mov->mask |= mask;
567 }
568
569 mir_rewrite_index_src_single(pre_use, i, mov->dest);
570 }
571 }
572 }
573 }
574 }
575
576 free(alur);
577 free(aluw);
578 free(brar);
579 free(ldst);
580 free(texr);
581 free(texw);
582 }
583
584 static void
585 mir_compute_interference(compiler_context *ctx, struct lcra_state *l)
586 {
587 /* First, we need liveness information to be computed per block */
588 mir_compute_liveness(ctx);
589
590 /* We need to force r1.w live throughout a blend shader */
591
592 if (ctx->inputs->is_blend) {
593 unsigned r1w = ~0;
594
595 mir_foreach_block(ctx, _block) {
596 midgard_block *block = (midgard_block *)_block;
597 mir_foreach_instr_in_block_rev(block, ins) {
598 if (ins->writeout)
599 r1w = ins->dest;
600 }
601
602 if (r1w != ~0)
603 break;
604 }
605
606 assert(r1w != ~0);
607 mir_foreach_instr_global(ctx, ins) {
608 if (ins->dest < ctx->temp_count)
609 lcra_add_node_interference(l, ins->dest, mir_bytemask(ins), r1w,
610 0xF);
611 }
612 }
613
614 /* Now that every block has live_in/live_out computed, we can determine
615 * interference by walking each block linearly. Take live_out at the
616 * end of each block and walk the block backwards. */
617
618 mir_foreach_block(ctx, _blk) {
619 midgard_block *blk = (midgard_block *)_blk;
620
621 /* The scalar and vector units run in parallel. We need to make
622 * sure they don't write to same portion of the register file
623 * otherwise the result is undefined. Add interferences to
624 * avoid this situation.
625 */
626 util_dynarray_foreach(&blk->bundles, midgard_bundle, bundle) {
627 midgard_instruction *instrs[2][4];
628 unsigned instr_count[2] = {0, 0};
629
630 for (unsigned i = 0; i < bundle->instruction_count; i++) {
631 if (bundle->instructions[i]->unit == UNIT_VMUL ||
632 bundle->instructions[i]->unit == UNIT_SADD)
633 instrs[0][instr_count[0]++] = bundle->instructions[i];
634 else
635 instrs[1][instr_count[1]++] = bundle->instructions[i];
636 }
637
638 for (unsigned i = 0; i < ARRAY_SIZE(instr_count); i++) {
639 for (unsigned j = 0; j < instr_count[i]; j++) {
640 midgard_instruction *ins_a = instrs[i][j];
641
642 if (ins_a->dest >= ctx->temp_count)
643 continue;
644
645 for (unsigned k = j + 1; k < instr_count[i]; k++) {
646 midgard_instruction *ins_b = instrs[i][k];
647
648 if (ins_b->dest >= ctx->temp_count)
649 continue;
650
651 lcra_add_node_interference(l, ins_b->dest,
652 mir_bytemask(ins_b), ins_a->dest,
653 mir_bytemask(ins_a));
654 }
655 }
656 }
657 }
658
659 uint16_t *live =
660 mem_dup(_blk->live_out, ctx->temp_count * sizeof(uint16_t));
661
662 mir_foreach_instr_in_block_rev(blk, ins) {
663 /* Mark all registers live after the instruction as
664 * interfering with the destination */
665
666 unsigned dest = ins->dest;
667
668 if (dest < ctx->temp_count) {
669 for (unsigned i = 0; i < ctx->temp_count; ++i) {
670 if (live[i]) {
671 unsigned mask = mir_bytemask(ins);
672 lcra_add_node_interference(l, dest, mask, i, live[i]);
673 }
674 }
675 }
676
677 /* Add blend shader interference: blend shaders might
678 * clobber r0-r3. */
679 if (ins->compact_branch && ins->writeout) {
680 for (unsigned i = 0; i < ctx->temp_count; ++i) {
681 if (!live[i])
682 continue;
683
684 for (unsigned j = 0; j < 4; j++) {
685 lcra_add_node_interference(l, ctx->temp_count + j, 0xFFFF, i,
686 live[i]);
687 }
688 }
689 }
690
691 /* Update live_in */
692 mir_liveness_ins_update(live, ins, ctx->temp_count);
693 }
694
695 free(live);
696 }
697 }
698
699 static bool
700 mir_is_64(const midgard_instruction *ins)
701 {
702 if (nir_alu_type_get_type_size(ins->dest_type) == 64)
703 return true;
704
705 mir_foreach_src(ins, v) {
706 if (nir_alu_type_get_type_size(ins->src_types[v]) == 64)
707 return true;
708 }
709
710 return false;
711 }
712
713 /*
714 * Determine if a shader needs a contiguous workgroup. This impacts register
715 * allocation. TODO: Optimize if barriers and local memory are unused.
716 */
717 static bool
718 needs_contiguous_workgroup(const compiler_context *ctx)
719 {
720 return mesa_shader_stage_uses_workgroup(ctx->stage);
721 }
722
723 /*
724 * Determine an upper-bound on the number of threads in a workgroup. The GL
725 * driver reports 128 for the maximum number of threads (the minimum-maximum in
726 * OpenGL ES 3.1), so we pessimistically assume 128 threads for variable
727 * workgroups.
728 */
729 static unsigned
730 max_threads_per_workgroup(const compiler_context *ctx)
731 {
732 if (ctx->nir->info.workgroup_size_variable) {
733 return 128;
734 } else {
735 return ctx->nir->info.workgroup_size[0] *
736 ctx->nir->info.workgroup_size[1] *
737 ctx->nir->info.workgroup_size[2];
738 }
739 }
740
741 /*
742 * Calculate the maximum number of work registers available to the shader.
743 * Architecturally, Midgard shaders may address up to 16 work registers, but
744 * various features impose other limits:
745 *
746 * 1. Blend shaders are limited to 8 registers by ABI.
747 * 2. If there are more than 8 register-mapped uniforms, then additional
748 * register-mapped uniforms use space that otherwise would be used for work
749 * registers.
750 * 3. If more than 4 registers are used, at most 128 threads may be spawned. If
751 * more than 8 registers are used, at most 64 threads may be spawned. These
752 * limits are architecturally visible in compute kernels that require an
753 * entire workgroup to be spawned at once (for barriers or local memory to
754 * work properly).
755 */
756 static unsigned
757 max_work_registers(const compiler_context *ctx)
758 {
759 if (ctx->inputs->is_blend)
760 return 8;
761
762 unsigned rmu_vec4 = ctx->info->push.count / 4;
763 unsigned max_work_registers = (rmu_vec4 >= 8) ? (24 - rmu_vec4) : 16;
764
765 if (needs_contiguous_workgroup(ctx)) {
766 unsigned threads = max_threads_per_workgroup(ctx);
767 assert(threads <= 128 && "maximum threads in ABI exceeded");
768
769 if (threads > 64)
770 max_work_registers = MIN2(max_work_registers, 8);
771 }
772
773 return max_work_registers;
774 }
775
776 /* This routine performs the actual register allocation. It should be succeeded
777 * by install_registers */
778
779 static struct lcra_state *
780 allocate_registers(compiler_context *ctx, bool *spilled)
781 {
782 int work_count = max_work_registers(ctx);
783
784 /* No register allocation to do with no SSA */
785 mir_compute_temp_count(ctx);
786 if (!ctx->temp_count)
787 return NULL;
788
789 /* Initialize LCRA. Allocate extra node at the end for r1-r3 for
790 * interference */
791
792 struct lcra_state *l = lcra_alloc_equations(ctx->temp_count + 4, 5);
793 unsigned node_r1 = ctx->temp_count + 1;
794
795 /* Starts of classes, in bytes */
796 l->class_start[REG_CLASS_WORK] = 16 * 0;
797 l->class_start[REG_CLASS_LDST] = 16 * 26;
798 l->class_start[REG_CLASS_TEXR] = 16 * 28;
799 l->class_start[REG_CLASS_TEXW] = 16 * 28;
800
801 l->class_size[REG_CLASS_WORK] = 16 * work_count;
802 l->class_size[REG_CLASS_LDST] = 16 * 2;
803 l->class_size[REG_CLASS_TEXR] = 16 * 2;
804 l->class_size[REG_CLASS_TEXW] = 16 * 2;
805
806 lcra_set_disjoint_class(l, REG_CLASS_TEXR, REG_CLASS_TEXW);
807
808 /* To save space on T*20, we don't have real texture registers.
809 * Instead, tex inputs reuse the load/store pipeline registers, and
810 * tex outputs use work r0/r1. Note we still use TEXR/TEXW classes,
811 * noting that this handles interferences and sizes correctly. */
812
813 if (ctx->quirks & MIDGARD_INTERPIPE_REG_ALIASING) {
814 l->class_start[REG_CLASS_TEXR] = l->class_start[REG_CLASS_LDST];
815 l->class_start[REG_CLASS_TEXW] = l->class_start[REG_CLASS_WORK];
816 }
817
818 unsigned *found_class = calloc(sizeof(unsigned), ctx->temp_count);
819 unsigned *min_alignment = calloc(sizeof(unsigned), ctx->temp_count);
820 unsigned *min_bound = calloc(sizeof(unsigned), ctx->temp_count);
821
822 mir_foreach_instr_global(ctx, ins) {
823 /* Swizzles of 32-bit sources on 64-bit instructions need to be
824 * aligned to either bottom (xy) or top (zw). More general
825 * swizzle lowering should happen prior to scheduling (TODO),
826 * but once we get RA we shouldn't disrupt this further. Align
827 * sources of 64-bit instructions. */
828
829 if (ins->type == TAG_ALU_4 && mir_is_64(ins)) {
830 mir_foreach_src(ins, v) {
831 unsigned s = ins->src[v];
832
833 if (s < ctx->temp_count)
834 min_alignment[s] = MAX2(3, min_alignment[s]);
835 }
836 }
837
838 if (ins->type == TAG_LOAD_STORE_4 && OP_HAS_ADDRESS(ins->op)) {
839 mir_foreach_src(ins, v) {
840 unsigned s = ins->src[v];
841 unsigned size = nir_alu_type_get_type_size(ins->src_types[v]);
842
843 if (s < ctx->temp_count)
844 min_alignment[s] = MAX2((size == 64) ? 3 : 2, min_alignment[s]);
845 }
846 }
847
848 /* Anything read as 16-bit needs proper alignment to ensure the
849 * resulting code can be packed.
850 */
851 mir_foreach_src(ins, s) {
852 unsigned src_size = nir_alu_type_get_type_size(ins->src_types[s]);
853 if (src_size == 16 && ins->src[s] < SSA_FIXED_MINIMUM)
854 min_bound[ins->src[s]] = MAX2(min_bound[ins->src[s]], 8);
855 }
856
857 /* Everything after this concerns only the destination, not the
858 * sources.
859 */
860 if (ins->dest >= SSA_FIXED_MINIMUM)
861 continue;
862
863 unsigned size = nir_alu_type_get_type_size(ins->dest_type);
864
865 if (ins->is_pack)
866 size = 32;
867
868 /* 0 for x, 1 for xy, 2 for xyz, 3 for xyzw */
869 int comps1 = util_logbase2(ins->mask);
870
871 int bytes = (comps1 + 1) * (size / 8);
872
873 /* Use the largest class if there's ambiguity, this
874 * handles partial writes */
875
876 int dest = ins->dest;
877 found_class[dest] = MAX2(found_class[dest], bytes);
878
879 min_alignment[dest] =
880 MAX2(min_alignment[dest], (size == 16) ? 1 : /* (1 << 1) = 2-byte */
881 (size == 32) ? 2
882 : /* (1 << 2) = 4-byte */
883 (size == 64) ? 3
884 : /* (1 << 3) = 8-byte */
885 3); /* 8-bit todo */
886
887 /* We can't cross xy/zw boundaries. TODO: vec8 can */
888 if (size == 16 && min_alignment[dest] != 4)
889 min_bound[dest] = 8;
890
891 /* We don't have a swizzle for the conditional and we don't
892 * want to muck with the conditional itself, so just force
893 * alignment for now */
894
895 if (ins->type == TAG_ALU_4 && OP_IS_CSEL_V(ins->op)) {
896 min_alignment[dest] = 4; /* 1 << 4= 16-byte = vec4 */
897
898 /* LCRA assumes bound >= alignment */
899 min_bound[dest] = 16;
900 }
901
902 /* Since ld/st swizzles and masks are 32-bit only, we need them
903 * aligned to enable final packing */
904 if (ins->type == TAG_LOAD_STORE_4)
905 min_alignment[dest] = MAX2(min_alignment[dest], 2);
906 }
907
908 for (unsigned i = 0; i < ctx->temp_count; ++i) {
909 lcra_set_alignment(l, i, min_alignment[i] ? min_alignment[i] : 2,
910 min_bound[i] ? min_bound[i] : 16);
911 lcra_restrict_range(l, i, found_class[i]);
912 }
913
914 free(found_class);
915 free(min_alignment);
916 free(min_bound);
917
918 /* Next, we'll determine semantic class. We default to zero (work).
919 * But, if we're used with a special operation, that will force us to a
920 * particular class. Each node must be assigned to exactly one class; a
921 * prepass before RA should have lowered what-would-have-been
922 * multiclass nodes into a series of moves to break it up into multiple
923 * nodes (TODO) */
924
925 mir_foreach_instr_global(ctx, ins) {
926 /* Check if this operation imposes any classes */
927
928 if (ins->type == TAG_LOAD_STORE_4) {
929 set_class(l->class, ins->src[0], REG_CLASS_LDST);
930 set_class(l->class, ins->src[1], REG_CLASS_LDST);
931 set_class(l->class, ins->src[2], REG_CLASS_LDST);
932 set_class(l->class, ins->src[3], REG_CLASS_LDST);
933
934 if (OP_IS_VEC4_ONLY(ins->op)) {
935 lcra_restrict_range(l, ins->dest, 16);
936 lcra_restrict_range(l, ins->src[0], 16);
937 lcra_restrict_range(l, ins->src[1], 16);
938 lcra_restrict_range(l, ins->src[2], 16);
939 lcra_restrict_range(l, ins->src[3], 16);
940 }
941 } else if (ins->type == TAG_TEXTURE_4) {
942 set_class(l->class, ins->dest, REG_CLASS_TEXW);
943 set_class(l->class, ins->src[0], REG_CLASS_TEXR);
944 set_class(l->class, ins->src[1], REG_CLASS_TEXR);
945 set_class(l->class, ins->src[2], REG_CLASS_TEXR);
946 set_class(l->class, ins->src[3], REG_CLASS_TEXR);
947 }
948 }
949
950 /* Check that the semantics of the class are respected */
951 mir_foreach_instr_global(ctx, ins) {
952 assert(check_write_class(l->class, ins->type, ins->dest));
953 assert(check_read_class(l->class, ins->type, ins->src[0]));
954 assert(check_read_class(l->class, ins->type, ins->src[1]));
955 assert(check_read_class(l->class, ins->type, ins->src[2]));
956 assert(check_read_class(l->class, ins->type, ins->src[3]));
957 }
958
959 /* Mark writeout to r0, depth to r1.x, stencil to r1.y,
960 * render target to r1.z, unknown to r1.w */
961 mir_foreach_instr_global(ctx, ins) {
962 if (!(ins->compact_branch && ins->writeout))
963 continue;
964
965 if (ins->src[0] < ctx->temp_count)
966 l->solutions[ins->src[0]] = 0;
967
968 if (ins->src[2] < ctx->temp_count)
969 l->solutions[ins->src[2]] = (16 * 1) + COMPONENT_X * 4;
970
971 if (ins->src[3] < ctx->temp_count)
972 l->solutions[ins->src[3]] = (16 * 1) + COMPONENT_Y * 4;
973
974 if (ins->src[1] < ctx->temp_count)
975 l->solutions[ins->src[1]] = (16 * 1) + COMPONENT_Z * 4;
976
977 if (ins->dest < ctx->temp_count)
978 l->solutions[ins->dest] = (16 * 1) + COMPONENT_W * 4;
979 }
980
981 /* Destinations of instructions in a writeout block cannot be assigned
982 * to r1 unless they are actually used as r1 from the writeout itself,
983 * since the writes to r1 are special. A code sequence like:
984 *
985 * sadd.fmov r1.x, [...]
986 * vadd.fadd r0, r1, r2
987 * [writeout branch]
988 *
989 * will misbehave since the r1.x write will be interpreted as a
990 * gl_FragDepth write so it won't show up correctly when r1 is read in
991 * the following segment. We model this as interference.
992 */
993
994 for (unsigned i = 0; i < 4; ++i)
995 l->solutions[ctx->temp_count + i] = (16 * i);
996
997 mir_foreach_block(ctx, _blk) {
998 midgard_block *blk = (midgard_block *)_blk;
999
1000 mir_foreach_bundle_in_block(blk, v) {
1001 /* We need at least a writeout and nonwriteout instruction */
1002 if (v->instruction_count < 2)
1003 continue;
1004
1005 /* Branches always come at the end */
1006 midgard_instruction *br = v->instructions[v->instruction_count - 1];
1007
1008 if (!br->writeout)
1009 continue;
1010
1011 for (signed i = v->instruction_count - 2; i >= 0; --i) {
1012 midgard_instruction *ins = v->instructions[i];
1013
1014 if (ins->dest >= ctx->temp_count)
1015 continue;
1016
1017 bool used_as_r1 = (br->dest == ins->dest);
1018
1019 mir_foreach_src(br, s)
1020 used_as_r1 |= (s > 0) && (br->src[s] == ins->dest);
1021
1022 if (!used_as_r1)
1023 lcra_add_node_interference(l, ins->dest, mir_bytemask(ins),
1024 node_r1, 0xFFFF);
1025 }
1026 }
1027 }
1028
1029 /* Precolour blend input to r0. Note writeout is necessarily at the end
1030 * and blend shaders are single-RT only so there is only a single
1031 * writeout block, so this cannot conflict with the writeout r0 (there
1032 * is no need to have an intermediate move) */
1033
1034 if (ctx->blend_input != ~0) {
1035 assert(ctx->blend_input < ctx->temp_count);
1036 l->solutions[ctx->blend_input] = 0;
1037 }
1038
1039 /* Same for the dual-source blend input/output, except here we use r2,
1040 * which is also set in the fragment shader. */
1041
1042 if (ctx->blend_src1 != ~0) {
1043 assert(ctx->blend_src1 < ctx->temp_count);
1044 l->solutions[ctx->blend_src1] = (16 * 2);
1045 ctx->info->work_reg_count = MAX2(ctx->info->work_reg_count, 3);
1046 }
1047
1048 mir_compute_interference(ctx, l);
1049
1050 *spilled = !lcra_solve(l);
1051 return l;
1052 }
1053
1054 /* Once registers have been decided via register allocation
1055 * (allocate_registers), we need to rewrite the MIR to use registers instead of
1056 * indices */
1057
1058 static void
1059 install_registers_instr(compiler_context *ctx, struct lcra_state *l,
1060 midgard_instruction *ins)
1061 {
1062 unsigned src_shift[MIR_SRC_COUNT];
1063
1064 for (unsigned i = 0; i < MIR_SRC_COUNT; ++i) {
1065 src_shift[i] =
1066 util_logbase2(nir_alu_type_get_type_size(ins->src_types[i]) / 8);
1067 }
1068
1069 unsigned dest_shift =
1070 util_logbase2(nir_alu_type_get_type_size(ins->dest_type) / 8);
1071
1072 switch (ins->type) {
1073 case TAG_ALU_4:
1074 case TAG_ALU_8:
1075 case TAG_ALU_12:
1076 case TAG_ALU_16: {
1077 if (ins->compact_branch)
1078 return;
1079
1080 struct phys_reg src1 = index_to_reg(ctx, l, ins->src[0], src_shift[0]);
1081 struct phys_reg src2 = index_to_reg(ctx, l, ins->src[1], src_shift[1]);
1082 struct phys_reg dest = index_to_reg(ctx, l, ins->dest, dest_shift);
1083
1084 mir_set_bytemask(ins, mir_bytemask(ins) << dest.offset);
1085
1086 unsigned dest_offset =
1087 GET_CHANNEL_COUNT(alu_opcode_props[ins->op].props) ? 0 : dest.offset;
1088
1089 offset_swizzle(ins->swizzle[0], src1.offset, src1.shift, dest.shift,
1090 dest_offset);
1091 if (!ins->has_inline_constant)
1092 offset_swizzle(ins->swizzle[1], src2.offset, src2.shift, dest.shift,
1093 dest_offset);
1094 if (ins->src[0] != ~0)
1095 ins->src[0] = SSA_FIXED_REGISTER(src1.reg);
1096 if (ins->src[1] != ~0)
1097 ins->src[1] = SSA_FIXED_REGISTER(src2.reg);
1098 if (ins->dest != ~0)
1099 ins->dest = SSA_FIXED_REGISTER(dest.reg);
1100 break;
1101 }
1102
1103 case TAG_LOAD_STORE_4: {
1104 /* Which physical register we read off depends on
1105 * whether we are loading or storing -- think about the
1106 * logical dataflow */
1107
1108 bool encodes_src = OP_IS_STORE(ins->op);
1109
1110 if (encodes_src) {
1111 struct phys_reg src = index_to_reg(ctx, l, ins->src[0], src_shift[0]);
1112 assert(src.reg == 26 || src.reg == 27);
1113
1114 ins->src[0] = SSA_FIXED_REGISTER(src.reg);
1115 offset_swizzle(ins->swizzle[0], src.offset, src.shift, 0, 0);
1116 } else {
1117 struct phys_reg dst = index_to_reg(ctx, l, ins->dest, dest_shift);
1118
1119 ins->dest = SSA_FIXED_REGISTER(dst.reg);
1120 offset_swizzle(ins->swizzle[0], 0, 2, dest_shift, dst.offset);
1121 mir_set_bytemask(ins, mir_bytemask(ins) << dst.offset);
1122 }
1123
1124 /* We also follow up by actual arguments */
1125
1126 for (int i = 1; i <= 3; i++) {
1127 unsigned src_index = ins->src[i];
1128 if (src_index != ~0) {
1129 struct phys_reg src = index_to_reg(ctx, l, src_index, src_shift[i]);
1130 unsigned component = src.offset >> src.shift;
1131 assert(component << src.shift == src.offset);
1132 ins->src[i] = SSA_FIXED_REGISTER(src.reg);
1133 ins->swizzle[i][0] += component;
1134 }
1135 }
1136
1137 break;
1138 }
1139
1140 case TAG_TEXTURE_4: {
1141 if (ins->op == midgard_tex_op_barrier)
1142 break;
1143
1144 /* Grab RA results */
1145 struct phys_reg dest = index_to_reg(ctx, l, ins->dest, dest_shift);
1146 struct phys_reg coord = index_to_reg(ctx, l, ins->src[1], src_shift[1]);
1147 struct phys_reg lod = index_to_reg(ctx, l, ins->src[2], src_shift[2]);
1148 struct phys_reg offset = index_to_reg(ctx, l, ins->src[3], src_shift[3]);
1149
1150 /* First, install the texture coordinate */
1151 if (ins->src[1] != ~0)
1152 ins->src[1] = SSA_FIXED_REGISTER(coord.reg);
1153 offset_swizzle(ins->swizzle[1], coord.offset, coord.shift, dest.shift, 0);
1154
1155 /* Next, install the destination */
1156 if (ins->dest != ~0)
1157 ins->dest = SSA_FIXED_REGISTER(dest.reg);
1158 offset_swizzle(ins->swizzle[0], 0, 2, dest.shift,
1159 dest_shift == 1 ? dest.offset % 8 : dest.offset);
1160 mir_set_bytemask(ins, mir_bytemask(ins) << dest.offset);
1161
1162 /* If there is a register LOD/bias, use it */
1163 if (ins->src[2] != ~0) {
1164 assert(!(lod.offset & 3));
1165 ins->src[2] = SSA_FIXED_REGISTER(lod.reg);
1166 ins->swizzle[2][0] = lod.offset / 4;
1167 }
1168
1169 /* If there is an offset register, install it */
1170 if (ins->src[3] != ~0) {
1171 ins->src[3] = SSA_FIXED_REGISTER(offset.reg);
1172 ins->swizzle[3][0] = offset.offset / 4;
1173 }
1174
1175 break;
1176 }
1177
1178 default:
1179 break;
1180 }
1181 }
1182
1183 static void
1184 install_registers(compiler_context *ctx, struct lcra_state *l)
1185 {
1186 mir_foreach_instr_global(ctx, ins)
1187 install_registers_instr(ctx, l, ins);
1188 }
1189
1190 /* If register allocation fails, find the best spill node */
1191
1192 static signed
1193 mir_choose_spill_node(compiler_context *ctx, struct lcra_state *l)
1194 {
1195 /* We can't spill a previously spilled value or an unspill */
1196
1197 mir_foreach_instr_global(ctx, ins) {
1198 if (ins->no_spill & (1 << l->spill_class)) {
1199 lcra_set_node_spill_cost(l, ins->dest, -1);
1200
1201 if (l->spill_class != REG_CLASS_WORK) {
1202 mir_foreach_src(ins, s)
1203 lcra_set_node_spill_cost(l, ins->src[s], -1);
1204 }
1205 }
1206 }
1207
1208 return lcra_get_best_spill_node(l);
1209 }
1210
1211 /* Once we've chosen a spill node, spill it */
1212
1213 static void
1214 mir_spill_register(compiler_context *ctx, unsigned spill_node,
1215 unsigned spill_class, unsigned *spill_count)
1216 {
1217 if (spill_class == REG_CLASS_WORK && ctx->inputs->is_blend)
1218 UNREACHABLE("Blend shader spilling is currently unimplemented");
1219
1220 unsigned spill_index = ctx->temp_count;
1221
1222 /* We have a spill node, so check the class. Work registers
1223 * legitimately spill to TLS, but special registers just spill to work
1224 * registers */
1225
1226 bool is_special = spill_class != REG_CLASS_WORK;
1227 bool is_special_w = spill_class == REG_CLASS_TEXW;
1228
1229 /* Allocate TLS slot (maybe) */
1230 unsigned spill_slot = !is_special ? (*spill_count)++ : 0;
1231
1232 /* For special reads, figure out how many bytes we need */
1233 unsigned read_bytemask = 0;
1234
1235 /* If multiple instructions write to this destination, we'll have to
1236 * fill from TLS before writing */
1237 unsigned write_count = 0;
1238
1239 mir_foreach_instr_global_safe(ctx, ins) {
1240 read_bytemask |= mir_bytemask_of_read_components(ins, spill_node);
1241 if (ins->dest == spill_node)
1242 ++write_count;
1243 }
1244
1245 /* For TLS, replace all stores to the spilled node. For
1246 * special reads, just keep as-is; the class will be demoted
1247 * implicitly. For special writes, spill to a work register */
1248
1249 if (!is_special || is_special_w) {
1250 if (is_special_w)
1251 spill_slot = spill_index++;
1252
1253 unsigned last_id = ~0;
1254 unsigned last_fill = ~0;
1255 unsigned last_spill_index = ~0;
1256 midgard_instruction *last_spill = NULL;
1257
1258 mir_foreach_block(ctx, _block) {
1259 midgard_block *block = (midgard_block *)_block;
1260 mir_foreach_instr_in_block_safe(block, ins) {
1261 if (ins->dest != spill_node)
1262 continue;
1263
1264 /* Note: it's important to match the mask of the spill
1265 * with the mask of the instruction whose destination
1266 * we're spilling, or otherwise we'll read invalid
1267 * components and can fail RA in a subsequent iteration
1268 */
1269
1270 if (is_special_w) {
1271 midgard_instruction st = v_mov(spill_node, spill_slot);
1272 st.no_spill |= (1 << spill_class);
1273 st.mask = ins->mask;
1274 st.dest_type = st.src_types[1] = ins->dest_type;
1275
1276 /* Hint: don't rewrite this node */
1277 st.hint = true;
1278
1279 mir_insert_instruction_after_scheduled(ctx, block, ins, &st);
1280 } else {
1281 unsigned bundle = ins->bundle_id;
1282 unsigned dest =
1283 (bundle == last_id) ? last_spill_index : spill_index++;
1284
1285 unsigned bytemask = mir_bytemask(ins);
1286 unsigned write_mask =
1287 mir_from_bytemask(mir_round_bytemask_up(bytemask, 32), 32);
1288
1289 if (write_count > 1 && bytemask != 0xFFFF &&
1290 bundle != last_fill) {
1291 midgard_instruction read =
1292 v_load_store_scratch(dest, spill_slot, false, 0xF);
1293 mir_insert_instruction_before_scheduled(ctx, block, ins,
1294 &read);
1295 write_mask = 0xF;
1296 last_fill = bundle;
1297 }
1298
1299 ins->dest = dest;
1300 ins->no_spill |= (1 << spill_class);
1301
1302 bool move = false;
1303
1304 /* In the same bundle, reads of the destination
1305 * of the spilt instruction need to be direct */
1306 midgard_instruction *it = ins;
1307 while ((it = list_first_entry(&it->link, midgard_instruction,
1308 link)) &&
1309 (it->bundle_id == bundle)) {
1310
1311 if (!mir_has_arg(it, spill_node))
1312 continue;
1313
1314 mir_rewrite_index_src_single(it, spill_node, dest);
1315
1316 /* The spilt instruction will write to
1317 * a work register for `it` to read but
1318 * the spill needs an LD/ST register */
1319 move = true;
1320 }
1321
1322 if (move)
1323 dest = spill_index++;
1324
1325 if (last_id == bundle) {
1326 last_spill->mask |= write_mask;
1327 u_foreach_bit(c, write_mask)
1328 last_spill->swizzle[0][c] = c;
1329 } else {
1330 midgard_instruction st =
1331 v_load_store_scratch(dest, spill_slot, true, write_mask);
1332 last_spill = mir_insert_instruction_after_scheduled(
1333 ctx, block, ins, &st);
1334 }
1335
1336 if (move) {
1337 midgard_instruction mv = v_mov(ins->dest, dest);
1338 mv.no_spill |= (1 << spill_class);
1339
1340 mir_insert_instruction_after_scheduled(ctx, block, ins, &mv);
1341 }
1342
1343 last_id = bundle;
1344 last_spill_index = ins->dest;
1345 }
1346
1347 if (!is_special)
1348 ctx->spills++;
1349 }
1350 }
1351 }
1352
1353 /* Insert a load from TLS before the first consecutive
1354 * use of the node, rewriting to use spilled indices to
1355 * break up the live range. Or, for special, insert a
1356 * move. Ironically the latter *increases* register
1357 * pressure, but the two uses of the spilling mechanism
1358 * are somewhat orthogonal. (special spilling is to use
1359 * work registers to back special registers; TLS
1360 * spilling is to use memory to back work registers) */
1361
1362 mir_foreach_block(ctx, _block) {
1363 midgard_block *block = (midgard_block *)_block;
1364 mir_foreach_instr_in_block(block, ins) {
1365 /* We can't rewrite the moves used to spill in the
1366 * first place. These moves are hinted. */
1367 if (ins->hint)
1368 continue;
1369
1370 /* If we don't use the spilled value, nothing to do */
1371 if (!mir_has_arg(ins, spill_node))
1372 continue;
1373
1374 unsigned index = 0;
1375
1376 if (!is_special_w) {
1377 index = ++spill_index;
1378
1379 midgard_instruction *before = ins;
1380 midgard_instruction st;
1381
1382 if (is_special) {
1383 /* Move */
1384 st = v_mov(spill_node, index);
1385 st.no_spill |= (1 << spill_class);
1386 } else {
1387 /* TLS load */
1388 st = v_load_store_scratch(index, spill_slot, false, 0xF);
1389 }
1390
1391 /* Mask the load based on the component count
1392 * actually needed to prevent RA loops */
1393
1394 st.mask =
1395 mir_from_bytemask(mir_round_bytemask_up(read_bytemask, 32), 32);
1396
1397 mir_insert_instruction_before_scheduled(ctx, block, before, &st);
1398 } else {
1399 /* Special writes already have their move spilled in */
1400 index = spill_slot;
1401 }
1402
1403 /* Rewrite to use */
1404 mir_rewrite_index_src_single(ins, spill_node, index);
1405
1406 if (!is_special)
1407 ctx->fills++;
1408 }
1409 }
1410
1411 /* Reset hints */
1412
1413 mir_foreach_instr_global(ctx, ins) {
1414 ins->hint = false;
1415 }
1416 }
1417
1418 static void
1419 mir_demote_uniforms(compiler_context *ctx, unsigned new_cutoff)
1420 {
1421 unsigned uniforms = ctx->info->push.count / 4;
1422 unsigned old_work_count = 16 - MAX2(uniforms - 8, 0);
1423 unsigned work_count = 16 - MAX2((new_cutoff - 8), 0);
1424
1425 unsigned min_demote = SSA_FIXED_REGISTER(old_work_count);
1426 unsigned max_demote = SSA_FIXED_REGISTER(work_count);
1427
1428 mir_foreach_block(ctx, _block) {
1429 midgard_block *block = (midgard_block *)_block;
1430 mir_foreach_instr_in_block(block, ins) {
1431 mir_foreach_src(ins, i) {
1432 if (ins->src[i] < min_demote || ins->src[i] >= max_demote)
1433 continue;
1434
1435 midgard_instruction *before = ins;
1436
1437 unsigned temp = make_compiler_temp(ctx);
1438 unsigned idx = (23 - SSA_REG_FROM_FIXED(ins->src[i])) * 4;
1439 assert(idx < ctx->info->push.count);
1440
1441 ctx->ubo_mask |= BITSET_BIT(ctx->info->push.words[idx].ubo);
1442
1443 midgard_instruction ld = {
1444 .type = TAG_LOAD_STORE_4,
1445 .mask = 0xF,
1446 .dest = temp,
1447 .dest_type = ins->src_types[i],
1448 .src = {~0, ~0, ~0, ~0},
1449 .swizzle = SWIZZLE_IDENTITY_4,
1450 .op = midgard_op_ld_ubo_128,
1451 .load_store =
1452 {
1453 .index_reg = REGISTER_LDST_ZERO,
1454 },
1455 .constants.u32[0] = ctx->info->push.words[idx].offset,
1456 };
1457
1458 midgard_pack_ubo_index_imm(&ld.load_store,
1459 ctx->info->push.words[idx].ubo);
1460
1461 mir_insert_instruction_before_scheduled(ctx, block, before, &ld);
1462
1463 mir_rewrite_index_src_single(ins, ins->src[i], temp);
1464 }
1465 }
1466 }
1467
1468 ctx->info->push.count = MIN2(ctx->info->push.count, new_cutoff * 4);
1469 }
1470
1471 /* Run register allocation in a loop, spilling until we succeed */
1472
1473 void
1474 mir_ra(compiler_context *ctx)
1475 {
1476 struct lcra_state *l = NULL;
1477 bool spilled = false;
1478 int iter_count = 1000; /* max iterations */
1479
1480 /* Number of 128-bit slots in memory we've spilled into */
1481 unsigned spill_count = DIV_ROUND_UP(ctx->info->tls_size, 16);
1482
1483 mir_create_pipeline_registers(ctx);
1484
1485 do {
1486 if (spilled) {
1487 signed spill_node = mir_choose_spill_node(ctx, l);
1488 unsigned uniforms = ctx->info->push.count / 4;
1489
1490 /* It's a lot cheaper to demote uniforms to get more
1491 * work registers than to spill to TLS. */
1492 if (l->spill_class == REG_CLASS_WORK && uniforms > 8) {
1493 mir_demote_uniforms(ctx, MAX2(uniforms - 4, 8));
1494 } else if (spill_node == -1) {
1495 fprintf(stderr, "ERROR: Failed to choose spill node\n");
1496 lcra_free(l);
1497 return;
1498 } else {
1499 mir_spill_register(ctx, spill_node, l->spill_class, &spill_count);
1500 }
1501 }
1502
1503 mir_squeeze_index(ctx);
1504 mir_invalidate_liveness(ctx);
1505
1506 if (l)
1507 lcra_free(l);
1508
1509 l = allocate_registers(ctx, &spilled);
1510 } while (spilled && ((iter_count--) > 0));
1511
1512 if (iter_count <= 0) {
1513 fprintf(
1514 stderr,
1515 "panfrost: Gave up allocating registers, rendering will be incomplete\n");
1516 assert(0);
1517 }
1518
1519 /* Report spilling information. spill_count is in 128-bit slots (vec4 x
1520 * fp32), but tls_size is in bytes, so multiply by 16 */
1521
1522 ctx->info->tls_size = spill_count * 16;
1523
1524 install_registers(ctx, l);
1525
1526 lcra_free(l);
1527 }