pan/midgard: Fix misc. RA issues
[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/register_allocate.h"
28 #include "util/u_math.h"
29 #include "util/u_memory.h"
30
31 /* For work registers, we can subdivide in various ways. So we create
32 * classes for the various sizes and conflict accordingly, keeping in
33 * mind that physical registers are divided along 128-bit boundaries.
34 * The important part is that 128-bit boundaries are not crossed.
35 *
36 * For each 128-bit register, we can subdivide to 32-bits 10 ways
37 *
38 * vec4: xyzw
39 * vec3: xyz, yzw
40 * vec2: xy, yz, zw,
41 * vec1: x, y, z, w
42 *
43 * For each 64-bit register, we can subdivide similarly to 16-bit
44 * (TODO: half-float RA, not that we support fp16 yet)
45 */
46
47 #define WORK_STRIDE 10
48
49 /* We have overlapping register classes for special registers, handled via
50 * shadows */
51
52 #define SHADOW_R28 18
53 #define SHADOW_R29 19
54
55 /* Prepacked masks/swizzles for virtual register types */
56 static unsigned reg_type_to_mask[WORK_STRIDE] = {
57 0xF, /* xyzw */
58 0x7, 0x7 << 1, /* xyz */
59 0x3, 0x3 << 1, 0x3 << 2, /* xy */
60 0x1, 0x1 << 1, 0x1 << 2, 0x1 << 3 /* x */
61 };
62
63 static unsigned reg_type_to_swizzle[WORK_STRIDE] = {
64 SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
65
66 SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
67 SWIZZLE(COMPONENT_Y, COMPONENT_Z, COMPONENT_W, COMPONENT_W),
68
69 SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
70 SWIZZLE(COMPONENT_Y, COMPONENT_Z, COMPONENT_Z, COMPONENT_W),
71 SWIZZLE(COMPONENT_Z, COMPONENT_W, COMPONENT_Z, COMPONENT_W),
72
73 SWIZZLE(COMPONENT_X, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
74 SWIZZLE(COMPONENT_Y, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
75 SWIZZLE(COMPONENT_Z, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
76 SWIZZLE(COMPONENT_W, COMPONENT_Y, COMPONENT_Z, COMPONENT_W),
77 };
78
79 struct phys_reg {
80 unsigned reg;
81 unsigned mask;
82 unsigned swizzle;
83 };
84
85 /* Given the mask/swizzle of both the register and the original source,
86 * compose to find the actual mask/swizzle to give the hardware */
87
88 static unsigned
89 compose_writemask(unsigned mask, struct phys_reg reg)
90 {
91 /* Note: the reg mask is guaranteed to be contiguous. So we shift
92 * into the X place, compose via a simple AND, and shift back */
93
94 unsigned shift = __builtin_ctz(reg.mask);
95 return ((reg.mask >> shift) & mask) << shift;
96 }
97
98 static unsigned
99 compose_swizzle(unsigned swizzle, unsigned mask,
100 struct phys_reg reg, struct phys_reg dst)
101 {
102 unsigned out = pan_compose_swizzle(swizzle, reg.swizzle);
103
104 /* Based on the register mask, we need to adjust over. E.g if we're
105 * writing to yz, a base swizzle of xy__ becomes _xy_. Save the
106 * original first component (x). But to prevent duplicate shifting
107 * (only applies to ALU -- mask param is set to xyzw out on L/S to
108 * prevent changes), we have to account for the shift inherent to the
109 * original writemask */
110
111 unsigned rep = out & 0x3;
112 unsigned shift = __builtin_ctz(dst.mask) - __builtin_ctz(mask);
113 unsigned shifted = out << (2*shift);
114
115 /* ..but we fill in the gaps so it appears to replicate */
116
117 for (unsigned s = 0; s < shift; ++s)
118 shifted |= rep << (2*s);
119
120 return shifted;
121 }
122
123 /* Helper to return the default phys_reg for a given register */
124
125 static struct phys_reg
126 default_phys_reg(int reg)
127 {
128 struct phys_reg r = {
129 .reg = reg,
130 .mask = 0xF, /* xyzw */
131 .swizzle = 0xE4 /* xyzw */
132 };
133
134 return r;
135 }
136
137 /* Determine which physical register, swizzle, and mask a virtual
138 * register corresponds to */
139
140 static struct phys_reg
141 index_to_reg(compiler_context *ctx, struct ra_graph *g, unsigned reg)
142 {
143 /* Check for special cases */
144 if (reg == ~0)
145 return default_phys_reg(REGISTER_UNUSED);
146 else if (reg >= SSA_FIXED_MINIMUM)
147 return default_phys_reg(SSA_REG_FROM_FIXED(reg));
148 else if (!g)
149 return default_phys_reg(REGISTER_UNUSED);
150
151 /* Special cases aside, we pick the underlying register */
152 int virt = ra_get_node_reg(g, reg);
153
154 /* Divide out the register and classification */
155 int phys = virt / WORK_STRIDE;
156 int type = virt % WORK_STRIDE;
157
158 /* Apply shadow registers */
159
160 if (phys >= SHADOW_R28 && phys <= SHADOW_R29)
161 phys += 28 - SHADOW_R28;
162
163 struct phys_reg r = {
164 .reg = phys,
165 .mask = reg_type_to_mask[type],
166 .swizzle = reg_type_to_swizzle[type]
167 };
168
169 /* Report that we actually use this register, and return it */
170
171 if (phys < 16)
172 ctx->work_registers = MAX2(ctx->work_registers, phys);
173
174 return r;
175 }
176
177 /* This routine creates a register set. Should be called infrequently since
178 * it's slow and can be cached. For legibility, variables are named in terms of
179 * work registers, although it is also used to create the register set for
180 * special register allocation */
181
182 static void
183 add_shadow_conflicts (struct ra_regs *regs, unsigned base, unsigned shadow)
184 {
185 for (unsigned a = 0; a < WORK_STRIDE; ++a) {
186 unsigned reg_a = (WORK_STRIDE * base) + a;
187
188 for (unsigned b = 0; b < WORK_STRIDE; ++b) {
189 unsigned reg_b = (WORK_STRIDE * shadow) + b;
190
191 ra_add_reg_conflict(regs, reg_a, reg_b);
192 ra_add_reg_conflict(regs, reg_b, reg_a);
193 }
194 }
195 }
196
197 static struct ra_regs *
198 create_register_set(unsigned work_count, unsigned *classes)
199 {
200 int virtual_count = 32 * WORK_STRIDE;
201
202 /* First, initialize the RA */
203 struct ra_regs *regs = ra_alloc_reg_set(NULL, virtual_count, true);
204
205 for (unsigned c = 0; c < NR_REG_CLASSES; ++c) {
206 int work_vec4 = ra_alloc_reg_class(regs);
207 int work_vec3 = ra_alloc_reg_class(regs);
208 int work_vec2 = ra_alloc_reg_class(regs);
209 int work_vec1 = ra_alloc_reg_class(regs);
210
211 classes[4*c + 0] = work_vec1;
212 classes[4*c + 1] = work_vec2;
213 classes[4*c + 2] = work_vec3;
214 classes[4*c + 3] = work_vec4;
215
216 /* Special register classes have other register counts */
217 unsigned count =
218 (c == REG_CLASS_WORK) ? work_count : 2;
219
220 unsigned first_reg =
221 (c == REG_CLASS_LDST) ? 26 :
222 (c == REG_CLASS_TEXR) ? 28 :
223 (c == REG_CLASS_TEXW) ? SHADOW_R28 :
224 0;
225
226 /* Add the full set of work registers */
227 for (unsigned i = first_reg; i < (first_reg + count); ++i) {
228 int base = WORK_STRIDE * i;
229
230 /* Build a full set of subdivisions */
231 ra_class_add_reg(regs, work_vec4, base);
232 ra_class_add_reg(regs, work_vec3, base + 1);
233 ra_class_add_reg(regs, work_vec3, base + 2);
234 ra_class_add_reg(regs, work_vec2, base + 3);
235 ra_class_add_reg(regs, work_vec2, base + 4);
236 ra_class_add_reg(regs, work_vec2, base + 5);
237 ra_class_add_reg(regs, work_vec1, base + 6);
238 ra_class_add_reg(regs, work_vec1, base + 7);
239 ra_class_add_reg(regs, work_vec1, base + 8);
240 ra_class_add_reg(regs, work_vec1, base + 9);
241
242 for (unsigned a = 0; a < 10; ++a) {
243 unsigned mask1 = reg_type_to_mask[a];
244
245 for (unsigned b = 0; b < 10; ++b) {
246 unsigned mask2 = reg_type_to_mask[b];
247
248 if (mask1 & mask2)
249 ra_add_reg_conflict(regs,
250 base + a, base + b);
251 }
252 }
253 }
254 }
255
256
257 /* We have duplicate classes */
258 add_shadow_conflicts(regs, 28, SHADOW_R28);
259 add_shadow_conflicts(regs, 29, SHADOW_R29);
260
261 /* We're done setting up */
262 ra_set_finalize(regs, NULL);
263
264 return regs;
265 }
266
267 /* This routine gets a precomputed register set off the screen if it's able, or
268 * otherwise it computes one on the fly */
269
270 static struct ra_regs *
271 get_register_set(struct midgard_screen *screen, unsigned work_count, unsigned **classes)
272 {
273 /* Bounds check */
274 assert(work_count >= 8);
275 assert(work_count <= 16);
276
277 /* Compute index */
278 unsigned index = work_count - 8;
279
280 /* Find the reg set */
281 struct ra_regs *cached = screen->regs[index];
282
283 if (cached) {
284 assert(screen->reg_classes[index]);
285 *classes = screen->reg_classes[index];
286 return cached;
287 }
288
289 /* Otherwise, create one */
290 struct ra_regs *created = create_register_set(work_count, screen->reg_classes[index]);
291
292 /* Cache it and use it */
293 screen->regs[index] = created;
294
295 *classes = screen->reg_classes[index];
296 return created;
297 }
298
299 /* Assign a (special) class, ensuring that it is compatible with whatever class
300 * was already set */
301
302 static void
303 set_class(unsigned *classes, unsigned node, unsigned class)
304 {
305 /* Check that we're even a node */
306 if (node >= SSA_FIXED_MINIMUM)
307 return;
308
309 /* First 4 are work, next 4 are load/store.. */
310 unsigned current_class = classes[node] >> 2;
311
312 /* Nothing to do */
313 if (class == current_class)
314 return;
315
316 /* If we're changing, we haven't assigned a special class */
317 assert(current_class == REG_CLASS_WORK);
318
319 classes[node] &= 0x3;
320 classes[node] |= (class << 2);
321 }
322
323 static void
324 force_vec4(unsigned *classes, unsigned node)
325 {
326 if (node >= SSA_FIXED_MINIMUM)
327 return;
328
329 /* Force vec4 = 3 */
330 classes[node] |= 0x3;
331 }
332
333 /* Special register classes impose special constraints on who can read their
334 * values, so check that */
335
336 static bool
337 check_read_class(unsigned *classes, unsigned tag, unsigned node)
338 {
339 /* Non-nodes are implicitly ok */
340 if (node >= SSA_FIXED_MINIMUM)
341 return true;
342
343 unsigned current_class = classes[node] >> 2;
344
345 switch (current_class) {
346 case REG_CLASS_LDST:
347 return (tag == TAG_LOAD_STORE_4);
348 case REG_CLASS_TEXR:
349 return (tag == TAG_TEXTURE_4);
350 case REG_CLASS_TEXW:
351 return (tag != TAG_LOAD_STORE_4);
352 case REG_CLASS_WORK:
353 return IS_ALU(tag);
354 default:
355 unreachable("Invalid class");
356 }
357 }
358
359 static bool
360 check_write_class(unsigned *classes, unsigned tag, unsigned node)
361 {
362 /* Non-nodes are implicitly ok */
363 if (node >= SSA_FIXED_MINIMUM)
364 return true;
365
366 unsigned current_class = classes[node] >> 2;
367
368 switch (current_class) {
369 case REG_CLASS_TEXR:
370 return true;
371 case REG_CLASS_TEXW:
372 return (tag == TAG_TEXTURE_4);
373 case REG_CLASS_LDST:
374 case REG_CLASS_WORK:
375 return IS_ALU(tag) || (tag == TAG_LOAD_STORE_4);
376 default:
377 unreachable("Invalid class");
378 }
379 }
380
381 /* Prepass before RA to ensure special class restrictions are met. The idea is
382 * to create a bit field of types of instructions that read a particular index.
383 * Later, we'll add moves as appropriate and rewrite to specialize by type. */
384
385 static void
386 mark_node_class (unsigned *bitfield, unsigned node)
387 {
388 if (node < SSA_FIXED_MINIMUM)
389 BITSET_SET(bitfield, node);
390 }
391
392 void
393 mir_lower_special_reads(compiler_context *ctx)
394 {
395 size_t sz = BITSET_WORDS(ctx->temp_count) * sizeof(BITSET_WORD);
396
397 /* Bitfields for the various types of registers we could have. aluw can
398 * be written by either ALU or load/store */
399
400 unsigned *alur = calloc(sz, 1);
401 unsigned *aluw = calloc(sz, 1);
402 unsigned *ldst = calloc(sz, 1);
403 unsigned *texr = calloc(sz, 1);
404 unsigned *texw = calloc(sz, 1);
405
406 /* Pass #1 is analysis, a linear scan to fill out the bitfields */
407
408 mir_foreach_instr_global(ctx, ins) {
409 switch (ins->type) {
410 case TAG_ALU_4:
411 mark_node_class(aluw, ins->dest);
412 mark_node_class(alur, ins->src[0]);
413 mark_node_class(alur, ins->src[1]);
414 mark_node_class(alur, ins->src[2]);
415 break;
416
417 case TAG_LOAD_STORE_4:
418 mark_node_class(aluw, ins->dest);
419 mark_node_class(ldst, ins->src[0]);
420 mark_node_class(ldst, ins->src[1]);
421 mark_node_class(ldst, ins->src[2]);
422 break;
423
424 case TAG_TEXTURE_4:
425 mark_node_class(texr, ins->src[0]);
426 mark_node_class(texr, ins->src[1]);
427 mark_node_class(texr, ins->src[2]);
428 mark_node_class(texw, ins->dest);
429 break;
430 }
431 }
432
433 /* Pass #2 is lowering now that we've analyzed all the classes.
434 * Conceptually, if an index is only marked for a single type of use,
435 * there is nothing to lower. If it is marked for different uses, we
436 * split up based on the number of types of uses. To do so, we divide
437 * into N distinct classes of use (where N>1 by definition), emit N-1
438 * moves from the index to copies of the index, and finally rewrite N-1
439 * of the types of uses to use the corresponding move */
440
441 unsigned spill_idx = ctx->temp_count;
442
443 for (unsigned i = 0; i < ctx->temp_count; ++i) {
444 bool is_alur = BITSET_TEST(alur, i);
445 bool is_aluw = BITSET_TEST(aluw, i);
446 bool is_ldst = BITSET_TEST(ldst, i);
447 bool is_texr = BITSET_TEST(texr, i);
448 bool is_texw = BITSET_TEST(texw, i);
449
450 /* Analyse to check how many distinct uses there are. ALU ops
451 * (alur) can read the results of the texture pipeline (texw)
452 * but not ldst or texr. Load/store ops (ldst) cannot read
453 * anything but load/store inputs. Texture pipeline cannot read
454 * anything but texture inputs. TODO: Simplify. */
455
456 bool collision =
457 (is_alur && (is_ldst || is_texr)) ||
458 (is_ldst && (is_alur || is_texr || is_texw)) ||
459 (is_texr && (is_alur || is_ldst || is_texw)) ||
460 (is_texw && (is_aluw || is_ldst || is_texr));
461
462 if (!collision)
463 continue;
464
465 /* Use the index as-is as the work copy. Emit copies for
466 * special uses */
467
468 unsigned classes[] = { TAG_LOAD_STORE_4, TAG_TEXTURE_4, TAG_TEXTURE_4 };
469 bool collisions[] = { is_ldst, is_texr, is_texw && is_aluw };
470
471 for (unsigned j = 0; j < ARRAY_SIZE(collisions); ++j) {
472 if (!collisions[j]) continue;
473
474 /* When the hazard is from reading, we move and rewrite
475 * sources (typical case). When it's from writing, we
476 * flip the move and rewrite destinations (obscure,
477 * only from control flow -- impossible in SSA) */
478
479 bool hazard_write = (j == 2);
480
481 unsigned idx = spill_idx++;
482
483 midgard_instruction m = hazard_write ?
484 v_mov(idx, blank_alu_src, i) :
485 v_mov(i, blank_alu_src, idx);
486
487 /* Insert move before each read/write, depending on the
488 * hazard we're trying to account for */
489
490 mir_foreach_instr_global_safe(ctx, pre_use) {
491 if (pre_use->type != classes[j])
492 continue;
493
494 if (hazard_write) {
495 if (pre_use->dest != i)
496 continue;
497 } else {
498 if (!mir_has_arg(pre_use, i))
499 continue;
500 }
501
502 if (hazard_write) {
503 midgard_instruction *use = mir_next_op(pre_use);
504 assert(use);
505 mir_insert_instruction_before(ctx, use, m);
506 mir_rewrite_index_dst_single(pre_use, i, idx);
507 } else {
508 idx = spill_idx++;
509 m = v_mov(i, blank_alu_src, idx);
510 m.mask = mir_mask_of_read_components(pre_use, i);
511 mir_insert_instruction_before(ctx, pre_use, m);
512 mir_rewrite_index_src_single(pre_use, i, idx);
513 }
514 }
515 }
516 }
517
518 free(alur);
519 free(aluw);
520 free(ldst);
521 free(texr);
522 free(texw);
523 }
524
525 /* Routines for liveness analysis */
526
527 static void
528 liveness_gen(uint8_t *live, unsigned node, unsigned max, unsigned mask)
529 {
530 if (node >= max)
531 return;
532
533 live[node] |= mask;
534 }
535
536 static void
537 liveness_kill(uint8_t *live, unsigned node, unsigned max, unsigned mask)
538 {
539 if (node >= max)
540 return;
541
542 live[node] &= ~mask;
543 }
544
545 /* Updates live_in for a single instruction */
546
547 static void
548 liveness_ins_update(uint8_t *live, midgard_instruction *ins, unsigned max)
549 {
550 /* live_in[s] = GEN[s] + (live_out[s] - KILL[s]) */
551
552 liveness_kill(live, ins->dest, max, ins->mask);
553
554 mir_foreach_src(ins, src) {
555 unsigned node = ins->src[src];
556 unsigned mask = mir_mask_of_read_components(ins, node);
557
558 liveness_gen(live, node, max, mask);
559 }
560 }
561
562 /* live_out[s] = sum { p in succ[s] } ( live_in[p] ) */
563
564 static void
565 liveness_block_live_out(compiler_context *ctx, midgard_block *blk)
566 {
567 mir_foreach_successor(blk, succ) {
568 for (unsigned i = 0; i < ctx->temp_count; ++i)
569 blk->live_out[i] |= succ->live_in[i];
570 }
571 }
572
573 /* Liveness analysis is a backwards-may dataflow analysis pass. Within a block,
574 * we compute live_out from live_in. The intrablock pass is linear-time. It
575 * returns whether progress was made. */
576
577 static bool
578 liveness_block_update(compiler_context *ctx, midgard_block *blk)
579 {
580 bool progress = false;
581
582 liveness_block_live_out(ctx, blk);
583
584 uint8_t *live = mem_dup(blk->live_out, ctx->temp_count);
585
586 mir_foreach_instr_in_block_rev(blk, ins)
587 liveness_ins_update(live, ins, ctx->temp_count);
588
589 /* To figure out progress, diff live_in */
590
591 for (unsigned i = 0; (i < ctx->temp_count) && !progress; ++i)
592 progress |= (blk->live_in[i] != live[i]);
593
594 free(blk->live_in);
595 blk->live_in = live;
596
597 return progress;
598 }
599
600 /* Globally, liveness analysis uses a fixed-point algorithm based on a
601 * worklist. We initialize a work list with the exit block. We iterate the work
602 * list to compute live_in from live_out for each block on the work list,
603 * adding the predecessors of the block to the work list if we made progress.
604 */
605
606 static void
607 mir_compute_liveness(
608 compiler_context *ctx,
609 struct ra_graph *g)
610 {
611 /* List of midgard_block */
612 struct set *work_list;
613
614 work_list = _mesa_set_create(ctx,
615 _mesa_hash_pointer,
616 _mesa_key_pointer_equal);
617
618 /* Allocate */
619
620 mir_foreach_block(ctx, block) {
621 block->live_in = calloc(ctx->temp_count, 1);
622 block->live_out = calloc(ctx->temp_count, 1);
623 }
624
625 /* Initialize the work list with the exit block */
626 struct set_entry *cur;
627
628 midgard_block *exit = mir_exit_block(ctx);
629 cur = _mesa_set_add(work_list, exit);
630
631 /* Iterate the work list */
632
633 do {
634 /* Pop off a block */
635 midgard_block *blk = (struct midgard_block *) cur->key;
636 _mesa_set_remove(work_list, cur);
637
638 /* Update its liveness information */
639 bool progress = liveness_block_update(ctx, blk);
640
641 /* If we made progress, we need to process the predecessors */
642
643 if (progress || (blk == exit)) {
644 mir_foreach_predecessor(blk, pred)
645 _mesa_set_add(work_list, pred);
646 }
647 } while((cur = _mesa_set_next_entry(work_list, NULL)) != NULL);
648
649 /* Now that every block has live_in/live_out computed, we can determine
650 * interference by walking each block linearly. Take live_out at the
651 * end of each block and walk the block backwards. */
652
653 mir_foreach_block(ctx, blk) {
654 uint8_t *live = calloc(ctx->temp_count, 1);
655
656 mir_foreach_successor(blk, succ) {
657 for (unsigned i = 0; i < ctx->temp_count; ++i)
658 live[i] |= succ->live_in[i];
659 }
660
661 mir_foreach_instr_in_block_rev(blk, ins) {
662 /* Mark all registers live after the instruction as
663 * interfering with the destination */
664
665 unsigned dest = ins->dest;
666
667 if (dest < ctx->temp_count) {
668 for (unsigned i = 0; i < ctx->temp_count; ++i)
669 if (live[i])
670 ra_add_node_interference(g, dest, i);
671 }
672
673 /* Update live_in */
674 liveness_ins_update(live, ins, ctx->temp_count);
675 }
676
677 free(live);
678 }
679
680 mir_foreach_block(ctx, blk) {
681 free(blk->live_in);
682 free(blk->live_out);
683 }
684 }
685
686 /* This routine performs the actual register allocation. It should be succeeded
687 * by install_registers */
688
689 struct ra_graph *
690 allocate_registers(compiler_context *ctx, bool *spilled)
691 {
692 /* The number of vec4 work registers available depends on when the
693 * uniforms start, so compute that first */
694 int work_count = 16 - MAX2((ctx->uniform_cutoff - 8), 0);
695 unsigned *classes = NULL;
696 struct ra_regs *regs = get_register_set(ctx->screen, work_count, &classes);
697
698 assert(regs != NULL);
699 assert(classes != NULL);
700
701 /* No register allocation to do with no SSA */
702
703 if (!ctx->temp_count)
704 return NULL;
705
706 /* Let's actually do register allocation */
707 int nodes = ctx->temp_count;
708 struct ra_graph *g = ra_alloc_interference_graph(regs, nodes);
709
710 /* Register class (as known to the Mesa register allocator) is actually
711 * the product of both semantic class (work, load/store, texture..) and
712 * size (vec2/vec3..). First, we'll go through and determine the
713 * minimum size needed to hold values */
714
715 unsigned *found_class = calloc(sizeof(unsigned), ctx->temp_count);
716
717 mir_foreach_instr_global(ctx, ins) {
718 if (ins->dest >= SSA_FIXED_MINIMUM) continue;
719
720 /* 0 for x, 1 for xy, 2 for xyz, 3 for xyzw */
721 int class = util_logbase2(ins->mask);
722
723 /* Use the largest class if there's ambiguity, this
724 * handles partial writes */
725
726 int dest = ins->dest;
727 found_class[dest] = MAX2(found_class[dest], class);
728 }
729
730 /* Next, we'll determine semantic class. We default to zero (work).
731 * But, if we're used with a special operation, that will force us to a
732 * particular class. Each node must be assigned to exactly one class; a
733 * prepass before RA should have lowered what-would-have-been
734 * multiclass nodes into a series of moves to break it up into multiple
735 * nodes (TODO) */
736
737 mir_foreach_instr_global(ctx, ins) {
738 /* Check if this operation imposes any classes */
739
740 if (ins->type == TAG_LOAD_STORE_4) {
741 bool force_vec4_only = OP_IS_VEC4_ONLY(ins->load_store.op);
742
743 set_class(found_class, ins->src[0], REG_CLASS_LDST);
744 set_class(found_class, ins->src[1], REG_CLASS_LDST);
745 set_class(found_class, ins->src[2], REG_CLASS_LDST);
746
747 if (force_vec4_only) {
748 force_vec4(found_class, ins->dest);
749 force_vec4(found_class, ins->src[0]);
750 force_vec4(found_class, ins->src[1]);
751 force_vec4(found_class, ins->src[2]);
752 }
753 } else if (ins->type == TAG_TEXTURE_4) {
754 set_class(found_class, ins->dest, REG_CLASS_TEXW);
755 set_class(found_class, ins->src[0], REG_CLASS_TEXR);
756 set_class(found_class, ins->src[1], REG_CLASS_TEXR);
757 set_class(found_class, ins->src[2], REG_CLASS_TEXR);
758 }
759 }
760
761 /* Check that the semantics of the class are respected */
762 mir_foreach_instr_global(ctx, ins) {
763 assert(check_write_class(found_class, ins->type, ins->dest));
764 assert(check_read_class(found_class, ins->type, ins->src[0]));
765 assert(check_read_class(found_class, ins->type, ins->src[1]));
766 assert(check_read_class(found_class, ins->type, ins->src[2]));
767 }
768
769 for (unsigned i = 0; i < ctx->temp_count; ++i) {
770 unsigned class = found_class[i];
771 ra_set_node_class(g, i, classes[class]);
772 }
773
774 mir_compute_liveness(ctx, g);
775
776 if (!ra_allocate(g)) {
777 *spilled = true;
778 } else {
779 *spilled = false;
780 }
781
782 /* Whether we were successful or not, report the graph so we can
783 * compute spill nodes */
784
785 return g;
786 }
787
788 /* Once registers have been decided via register allocation
789 * (allocate_registers), we need to rewrite the MIR to use registers instead of
790 * indices */
791
792 static void
793 install_registers_instr(
794 compiler_context *ctx,
795 struct ra_graph *g,
796 midgard_instruction *ins)
797 {
798 switch (ins->type) {
799 case TAG_ALU_4:
800 case TAG_ALU_8:
801 case TAG_ALU_12:
802 case TAG_ALU_16: {
803 if (ins->compact_branch)
804 return;
805
806 struct phys_reg src1 = index_to_reg(ctx, g, ins->src[0]);
807 struct phys_reg src2 = index_to_reg(ctx, g, ins->src[1]);
808 struct phys_reg dest = index_to_reg(ctx, g, ins->dest);
809
810 unsigned uncomposed_mask = ins->mask;
811 ins->mask = compose_writemask(uncomposed_mask, dest);
812
813 /* Adjust the dest mask if necessary. Mostly this is a no-op
814 * but it matters for dot products */
815 dest.mask = effective_writemask(&ins->alu, ins->mask);
816
817 midgard_vector_alu_src mod1 =
818 vector_alu_from_unsigned(ins->alu.src1);
819 mod1.swizzle = compose_swizzle(mod1.swizzle, uncomposed_mask, src1, dest);
820 ins->alu.src1 = vector_alu_srco_unsigned(mod1);
821
822 ins->registers.src1_reg = src1.reg;
823
824 ins->registers.src2_imm = ins->has_inline_constant;
825
826 if (ins->has_inline_constant) {
827 /* Encode inline 16-bit constant. See disassembler for
828 * where the algorithm is from */
829
830 ins->registers.src2_reg = ins->inline_constant >> 11;
831
832 int lower_11 = ins->inline_constant & ((1 << 12) - 1);
833 uint16_t imm = ((lower_11 >> 8) & 0x7) |
834 ((lower_11 & 0xFF) << 3);
835
836 ins->alu.src2 = imm << 2;
837 } else {
838 midgard_vector_alu_src mod2 =
839 vector_alu_from_unsigned(ins->alu.src2);
840 mod2.swizzle = compose_swizzle(
841 mod2.swizzle, uncomposed_mask, src2, dest);
842 ins->alu.src2 = vector_alu_srco_unsigned(mod2);
843
844 ins->registers.src2_reg = src2.reg;
845 }
846
847 ins->registers.out_reg = dest.reg;
848 break;
849 }
850
851 case TAG_LOAD_STORE_4: {
852 /* Which physical register we read off depends on
853 * whether we are loading or storing -- think about the
854 * logical dataflow */
855
856 bool encodes_src = OP_IS_STORE(ins->load_store.op);
857
858 if (encodes_src) {
859 struct phys_reg src = index_to_reg(ctx, g, ins->src[0]);
860 assert(src.reg == 26 || src.reg == 27);
861
862 ins->load_store.reg = src.reg - 26;
863
864 unsigned shift = __builtin_ctz(src.mask);
865 unsigned adjusted_mask = src.mask >> shift;
866 assert(((adjusted_mask + 1) & adjusted_mask) == 0);
867
868 unsigned new_swizzle = 0;
869 for (unsigned q = 0; q < 4; ++q) {
870 unsigned c = (ins->load_store.swizzle >> (2*q)) & 3;
871 new_swizzle |= (c + shift) << (2*q);
872 }
873
874 ins->load_store.swizzle = compose_swizzle(
875 new_swizzle, src.mask,
876 default_phys_reg(0), src);
877 } else {
878 struct phys_reg src = index_to_reg(ctx, g, ins->dest);
879
880 ins->load_store.reg = src.reg;
881
882 ins->load_store.swizzle = compose_swizzle(
883 ins->load_store.swizzle, 0xF,
884 default_phys_reg(0), src);
885
886 ins->mask = compose_writemask(
887 ins->mask, src);
888 }
889
890 /* We also follow up by actual arguments */
891
892 int src2 =
893 encodes_src ? ins->src[1] : ins->src[0];
894
895 int src3 =
896 encodes_src ? ins->src[2] : ins->src[1];
897
898 if (src2 >= 0) {
899 struct phys_reg src = index_to_reg(ctx, g, src2);
900 unsigned component = __builtin_ctz(src.mask);
901 ins->load_store.arg_1 |= midgard_ldst_reg(src.reg, component);
902 }
903
904 if (src3 >= 0) {
905 struct phys_reg src = index_to_reg(ctx, g, src3);
906 unsigned component = __builtin_ctz(src.mask);
907 ins->load_store.arg_2 |= midgard_ldst_reg(src.reg, component);
908 }
909
910 break;
911 }
912
913 case TAG_TEXTURE_4: {
914 /* Grab RA results */
915 struct phys_reg dest = index_to_reg(ctx, g, ins->dest);
916 struct phys_reg coord = index_to_reg(ctx, g, ins->src[0]);
917 struct phys_reg lod = index_to_reg(ctx, g, ins->src[1]);
918
919 assert(dest.reg == 28 || dest.reg == 29);
920 assert(coord.reg == 28 || coord.reg == 29);
921
922 /* First, install the texture coordinate */
923 ins->texture.in_reg_full = 1;
924 ins->texture.in_reg_upper = 0;
925 ins->texture.in_reg_select = coord.reg - 28;
926 ins->texture.in_reg_swizzle =
927 compose_swizzle(ins->texture.in_reg_swizzle, 0xF, coord, dest);
928
929 /* Next, install the destination */
930 ins->texture.out_full = 1;
931 ins->texture.out_upper = 0;
932 ins->texture.out_reg_select = dest.reg - 28;
933 ins->texture.swizzle =
934 compose_swizzle(ins->texture.swizzle, dest.mask, dest, dest);
935 ins->mask =
936 compose_writemask(ins->mask, dest);
937
938 /* If there is a register LOD/bias, use it */
939 if (ins->src[1] != ~0) {
940 midgard_tex_register_select sel = {
941 .select = lod.reg,
942 .full = 1,
943 .component = lod.swizzle & 3,
944 };
945
946 uint8_t packed;
947 memcpy(&packed, &sel, sizeof(packed));
948 ins->texture.bias = packed;
949 }
950
951 break;
952 }
953
954 default:
955 break;
956 }
957 }
958
959 void
960 install_registers(compiler_context *ctx, struct ra_graph *g)
961 {
962 mir_foreach_instr_global(ctx, ins)
963 install_registers_instr(ctx, g, ins);
964 }