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