panfrost: Use ralloc() to allocate instructions to avoid leaking those objs
[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) && g)
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 (tag == TAG_ALU_4);
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 (tag == TAG_ALU_4) || (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 */
398
399 unsigned *alur = calloc(sz, 1);
400 unsigned *aluw = calloc(sz, 1);
401 unsigned *ldst = calloc(sz, 1);
402 unsigned *texr = calloc(sz, 1);
403 unsigned *texw = calloc(sz, 1);
404
405 /* Pass #1 is analysis, a linear scan to fill out the bitfields */
406
407 mir_foreach_instr_global(ctx, ins) {
408 switch (ins->type) {
409 case TAG_ALU_4:
410 mark_node_class(aluw, ins->dest);
411 mark_node_class(alur, ins->src[0]);
412 mark_node_class(alur, ins->src[1]);
413 break;
414
415 case TAG_LOAD_STORE_4:
416 mark_node_class(ldst, ins->src[0]);
417 mark_node_class(ldst, ins->src[1]);
418 mark_node_class(ldst, ins->src[2]);
419 break;
420
421 case TAG_TEXTURE_4:
422 mark_node_class(texr, ins->src[0]);
423 mark_node_class(texr, ins->src[1]);
424 mark_node_class(texr, ins->src[2]);
425 mark_node_class(texw, ins->dest);
426 break;
427 }
428 }
429
430 /* Pass #2 is lowering now that we've analyzed all the classes.
431 * Conceptually, if an index is only marked for a single type of use,
432 * there is nothing to lower. If it is marked for different uses, we
433 * split up based on the number of types of uses. To do so, we divide
434 * into N distinct classes of use (where N>1 by definition), emit N-1
435 * moves from the index to copies of the index, and finally rewrite N-1
436 * of the types of uses to use the corresponding move */
437
438 unsigned spill_idx = ctx->temp_count;
439
440 for (unsigned i = 0; i < ctx->temp_count; ++i) {
441 bool is_alur = BITSET_TEST(alur, i);
442 bool is_aluw = BITSET_TEST(aluw, i);
443 bool is_ldst = BITSET_TEST(ldst, i);
444 bool is_texr = BITSET_TEST(texr, i);
445 bool is_texw = BITSET_TEST(texw, i);
446
447 /* Analyse to check how many distinct uses there are. ALU ops
448 * (alur) can read the results of the texture pipeline (texw)
449 * but not ldst or texr. Load/store ops (ldst) cannot read
450 * anything but load/store inputs. Texture pipeline cannot read
451 * anything but texture inputs. TODO: Simplify. */
452
453 bool collision =
454 (is_alur && (is_ldst || is_texr)) ||
455 (is_ldst && (is_alur || is_texr || is_texw)) ||
456 (is_texr && (is_alur || is_ldst || is_texw)) ||
457 (is_texw && (is_aluw || is_ldst || is_texr));
458
459 if (!collision)
460 continue;
461
462 /* Use the index as-is as the work copy. Emit copies for
463 * special uses */
464
465 unsigned classes[] = { TAG_LOAD_STORE_4, TAG_TEXTURE_4, TAG_TEXTURE_4 };
466 bool collisions[] = { is_ldst, is_texr, is_texw && is_aluw };
467
468 for (unsigned j = 0; j < ARRAY_SIZE(collisions); ++j) {
469 if (!collisions[j]) continue;
470
471 /* When the hazard is from reading, we move and rewrite
472 * sources (typical case). When it's from writing, we
473 * flip the move and rewrite destinations (obscure,
474 * only from control flow -- impossible in SSA) */
475
476 bool hazard_write = (j == 2);
477
478 unsigned idx = spill_idx++;
479
480 midgard_instruction m = hazard_write ?
481 v_mov(idx, blank_alu_src, i) :
482 v_mov(i, blank_alu_src, idx);
483
484 /* Insert move before each read/write, depending on the
485 * hazard we're trying to account for */
486
487 mir_foreach_instr_global_safe(ctx, pre_use) {
488 if (pre_use->type != classes[j])
489 continue;
490
491 if (hazard_write) {
492 if (pre_use->dest != i)
493 continue;
494 } else {
495 if (!mir_has_arg(pre_use, i))
496 continue;
497 }
498
499 if (hazard_write) {
500 midgard_instruction *use = mir_next_op(pre_use);
501 assert(use);
502 mir_insert_instruction_before(ctx, use, m);
503 mir_rewrite_index_dst_single(pre_use, i, idx);
504 } else {
505 idx = spill_idx++;
506 m = v_mov(i, blank_alu_src, idx);
507 m.mask = mir_mask_of_read_components(pre_use, i);
508 mir_insert_instruction_before(ctx, pre_use, m);
509 mir_rewrite_index_src_single(pre_use, i, idx);
510 }
511 }
512 }
513 }
514
515 free(alur);
516 free(aluw);
517 free(ldst);
518 free(texr);
519 free(texw);
520 }
521
522 /* Routines for liveness analysis */
523
524 static void
525 liveness_gen(uint8_t *live, unsigned node, unsigned max, unsigned mask)
526 {
527 if (node >= max)
528 return;
529
530 live[node] |= mask;
531 }
532
533 static void
534 liveness_kill(uint8_t *live, unsigned node, unsigned max, unsigned mask)
535 {
536 if (node >= max)
537 return;
538
539 live[node] &= ~mask;
540 }
541
542 /* Updates live_in for a single instruction */
543
544 static void
545 liveness_ins_update(uint8_t *live, midgard_instruction *ins, unsigned max)
546 {
547 /* live_in[s] = GEN[s] + (live_out[s] - KILL[s]) */
548
549 liveness_kill(live, ins->dest, max, ins->mask);
550
551 mir_foreach_src(ins, src) {
552 unsigned node = ins->src[src];
553 unsigned mask = mir_mask_of_read_components(ins, node);
554
555 liveness_gen(live, node, max, mask);
556 }
557 }
558
559 /* live_out[s] = sum { p in succ[s] } ( live_in[p] ) */
560
561 static void
562 liveness_block_live_out(compiler_context *ctx, midgard_block *blk)
563 {
564 mir_foreach_successor(blk, succ) {
565 for (unsigned i = 0; i < ctx->temp_count; ++i)
566 blk->live_out[i] |= succ->live_in[i];
567 }
568 }
569
570 /* Liveness analysis is a backwards-may dataflow analysis pass. Within a block,
571 * we compute live_out from live_in. The intrablock pass is linear-time. It
572 * returns whether progress was made. */
573
574 static bool
575 liveness_block_update(compiler_context *ctx, midgard_block *blk)
576 {
577 bool progress = false;
578
579 liveness_block_live_out(ctx, blk);
580
581 uint8_t *live = mem_dup(blk->live_out, ctx->temp_count);
582
583 mir_foreach_instr_in_block_rev(blk, ins)
584 liveness_ins_update(live, ins, ctx->temp_count);
585
586 /* To figure out progress, diff live_in */
587
588 for (unsigned i = 0; (i < ctx->temp_count) && !progress; ++i)
589 progress |= (blk->live_in[i] != live[i]);
590
591 free(blk->live_in);
592 blk->live_in = live;
593
594 return progress;
595 }
596
597 /* Globally, liveness analysis uses a fixed-point algorithm based on a
598 * worklist. We initialize a work list with the exit block. We iterate the work
599 * list to compute live_in from live_out for each block on the work list,
600 * adding the predecessors of the block to the work list if we made progress.
601 */
602
603 static void
604 mir_compute_liveness(
605 compiler_context *ctx,
606 struct ra_graph *g)
607 {
608 /* List of midgard_block */
609 struct set *work_list;
610
611 work_list = _mesa_set_create(ctx,
612 _mesa_hash_pointer,
613 _mesa_key_pointer_equal);
614
615 /* Allocate */
616
617 mir_foreach_block(ctx, block) {
618 block->live_in = calloc(ctx->temp_count, 1);
619 block->live_out = calloc(ctx->temp_count, 1);
620 }
621
622 /* Initialize the work list with the exit block */
623 struct set_entry *cur;
624
625 midgard_block *exit = mir_exit_block(ctx);
626 cur = _mesa_set_add(work_list, exit);
627
628 /* Iterate the work list */
629
630 do {
631 /* Pop off a block */
632 midgard_block *blk = (struct midgard_block *) cur->key;
633 _mesa_set_remove(work_list, cur);
634
635 /* Update its liveness information */
636 bool progress = liveness_block_update(ctx, blk);
637
638 /* If we made progress, we need to process the predecessors */
639
640 if (progress || (blk == exit)) {
641 mir_foreach_predecessor(blk, pred)
642 _mesa_set_add(work_list, pred);
643 }
644 } while((cur = _mesa_set_next_entry(work_list, NULL)) != NULL);
645
646 /* Now that every block has live_in/live_out computed, we can determine
647 * interference by walking each block linearly. Take live_out at the
648 * end of each block and walk the block backwards. */
649
650 mir_foreach_block(ctx, blk) {
651 uint8_t *live = calloc(ctx->temp_count, 1);
652
653 mir_foreach_successor(blk, succ) {
654 for (unsigned i = 0; i < ctx->temp_count; ++i)
655 live[i] |= succ->live_in[i];
656 }
657
658 mir_foreach_instr_in_block_rev(blk, ins) {
659 /* Mark all registers live after the instruction as
660 * interfering with the destination */
661
662 unsigned dest = ins->dest;
663
664 if (dest < ctx->temp_count) {
665 for (unsigned i = 0; i < ctx->temp_count; ++i)
666 if (live[i])
667 ra_add_node_interference(g, dest, i);
668 }
669
670 /* Update live_in */
671 liveness_ins_update(live, ins, ctx->temp_count);
672 }
673
674 free(live);
675 }
676
677 mir_foreach_block(ctx, blk) {
678 free(blk->live_in);
679 free(blk->live_out);
680 }
681 }
682
683 /* This routine performs the actual register allocation. It should be succeeded
684 * by install_registers */
685
686 struct ra_graph *
687 allocate_registers(compiler_context *ctx, bool *spilled)
688 {
689 /* The number of vec4 work registers available depends on when the
690 * uniforms start, so compute that first */
691 int work_count = 16 - MAX2((ctx->uniform_cutoff - 8), 0);
692 unsigned *classes = NULL;
693 struct ra_regs *regs = get_register_set(ctx->screen, work_count, &classes);
694
695 assert(regs != NULL);
696 assert(classes != NULL);
697
698 /* No register allocation to do with no SSA */
699
700 if (!ctx->temp_count)
701 return NULL;
702
703 /* Let's actually do register allocation */
704 int nodes = ctx->temp_count;
705 struct ra_graph *g = ra_alloc_interference_graph(regs, nodes);
706
707 /* Register class (as known to the Mesa register allocator) is actually
708 * the product of both semantic class (work, load/store, texture..) and
709 * size (vec2/vec3..). First, we'll go through and determine the
710 * minimum size needed to hold values */
711
712 unsigned *found_class = calloc(sizeof(unsigned), ctx->temp_count);
713
714 mir_foreach_instr_global(ctx, ins) {
715 if (ins->dest >= SSA_FIXED_MINIMUM) continue;
716
717 /* 0 for x, 1 for xy, 2 for xyz, 3 for xyzw */
718 int class = util_logbase2(ins->mask);
719
720 /* Use the largest class if there's ambiguity, this
721 * handles partial writes */
722
723 int dest = ins->dest;
724 found_class[dest] = MAX2(found_class[dest], class);
725 }
726
727 /* Next, we'll determine semantic class. We default to zero (work).
728 * But, if we're used with a special operation, that will force us to a
729 * particular class. Each node must be assigned to exactly one class; a
730 * prepass before RA should have lowered what-would-have-been
731 * multiclass nodes into a series of moves to break it up into multiple
732 * nodes (TODO) */
733
734 mir_foreach_instr_global(ctx, ins) {
735 /* Check if this operation imposes any classes */
736
737 if (ins->type == TAG_LOAD_STORE_4) {
738 bool force_vec4_only = OP_IS_VEC4_ONLY(ins->load_store.op);
739
740 set_class(found_class, ins->src[0], REG_CLASS_LDST);
741 set_class(found_class, ins->src[1], REG_CLASS_LDST);
742 set_class(found_class, ins->src[2], REG_CLASS_LDST);
743
744 if (force_vec4_only) {
745 force_vec4(found_class, ins->dest);
746 force_vec4(found_class, ins->src[0]);
747 force_vec4(found_class, ins->src[1]);
748 force_vec4(found_class, ins->src[2]);
749 }
750 } else if (ins->type == TAG_TEXTURE_4) {
751 set_class(found_class, ins->dest, REG_CLASS_TEXW);
752 set_class(found_class, ins->src[0], REG_CLASS_TEXR);
753 set_class(found_class, ins->src[1], REG_CLASS_TEXR);
754 set_class(found_class, ins->src[2], REG_CLASS_TEXR);
755 }
756 }
757
758 /* Check that the semantics of the class are respected */
759 mir_foreach_instr_global(ctx, ins) {
760 assert(check_write_class(found_class, ins->type, ins->dest));
761 assert(check_read_class(found_class, ins->type, ins->src[0]));
762 assert(check_read_class(found_class, ins->type, ins->src[1]));
763 assert(check_read_class(found_class, ins->type, ins->src[2]));
764 }
765
766 for (unsigned i = 0; i < ctx->temp_count; ++i) {
767 unsigned class = found_class[i];
768 ra_set_node_class(g, i, classes[class]);
769 }
770
771 mir_compute_liveness(ctx, g);
772
773 if (!ra_allocate(g)) {
774 *spilled = true;
775 } else {
776 *spilled = false;
777 }
778
779 /* Whether we were successful or not, report the graph so we can
780 * compute spill nodes */
781
782 return g;
783 }
784
785 /* Once registers have been decided via register allocation
786 * (allocate_registers), we need to rewrite the MIR to use registers instead of
787 * indices */
788
789 static void
790 install_registers_instr(
791 compiler_context *ctx,
792 struct ra_graph *g,
793 midgard_instruction *ins)
794 {
795 switch (ins->type) {
796 case TAG_ALU_4: {
797 struct phys_reg src1 = index_to_reg(ctx, g, ins->src[0]);
798 struct phys_reg src2 = index_to_reg(ctx, g, ins->src[1]);
799 struct phys_reg dest = index_to_reg(ctx, g, ins->dest);
800
801 unsigned uncomposed_mask = ins->mask;
802 ins->mask = compose_writemask(uncomposed_mask, dest);
803
804 /* Adjust the dest mask if necessary. Mostly this is a no-op
805 * but it matters for dot products */
806 dest.mask = effective_writemask(&ins->alu, ins->mask);
807
808 midgard_vector_alu_src mod1 =
809 vector_alu_from_unsigned(ins->alu.src1);
810 mod1.swizzle = compose_swizzle(mod1.swizzle, uncomposed_mask, src1, dest);
811 ins->alu.src1 = vector_alu_srco_unsigned(mod1);
812
813 ins->registers.src1_reg = src1.reg;
814
815 ins->registers.src2_imm = ins->has_inline_constant;
816
817 if (ins->has_inline_constant) {
818 /* Encode inline 16-bit constant. See disassembler for
819 * where the algorithm is from */
820
821 ins->registers.src2_reg = ins->inline_constant >> 11;
822
823 int lower_11 = ins->inline_constant & ((1 << 12) - 1);
824 uint16_t imm = ((lower_11 >> 8) & 0x7) |
825 ((lower_11 & 0xFF) << 3);
826
827 ins->alu.src2 = imm << 2;
828 } else {
829 midgard_vector_alu_src mod2 =
830 vector_alu_from_unsigned(ins->alu.src2);
831 mod2.swizzle = compose_swizzle(
832 mod2.swizzle, uncomposed_mask, src2, dest);
833 ins->alu.src2 = vector_alu_srco_unsigned(mod2);
834
835 ins->registers.src2_reg = src2.reg;
836 }
837
838 ins->registers.out_reg = dest.reg;
839 break;
840 }
841
842 case TAG_LOAD_STORE_4: {
843 /* Which physical register we read off depends on
844 * whether we are loading or storing -- think about the
845 * logical dataflow */
846
847 bool encodes_src = OP_IS_STORE(ins->load_store.op);
848
849 if (encodes_src) {
850 struct phys_reg src = index_to_reg(ctx, g, ins->src[0]);
851 assert(src.reg == 26 || src.reg == 27);
852
853 ins->load_store.reg = src.reg - 26;
854
855 unsigned shift = __builtin_ctz(src.mask);
856 unsigned adjusted_mask = src.mask >> shift;
857 assert(((adjusted_mask + 1) & adjusted_mask) == 0);
858
859 unsigned new_swizzle = 0;
860 for (unsigned q = 0; q < 4; ++q) {
861 unsigned c = (ins->load_store.swizzle >> (2*q)) & 3;
862 new_swizzle |= (c + shift) << (2*q);
863 }
864
865 ins->load_store.swizzle = compose_swizzle(
866 new_swizzle, src.mask,
867 default_phys_reg(0), src);
868 } else {
869 struct phys_reg src = index_to_reg(ctx, g, ins->dest);
870
871 ins->load_store.reg = src.reg;
872
873 ins->load_store.swizzle = compose_swizzle(
874 ins->load_store.swizzle, 0xF,
875 default_phys_reg(0), src);
876
877 ins->mask = compose_writemask(
878 ins->mask, src);
879 }
880
881 /* We also follow up by actual arguments */
882
883 int src2 =
884 encodes_src ? ins->src[1] : ins->src[0];
885
886 int src3 =
887 encodes_src ? ins->src[2] : ins->src[1];
888
889 if (src2 >= 0) {
890 struct phys_reg src = index_to_reg(ctx, g, src2);
891 unsigned component = __builtin_ctz(src.mask);
892 ins->load_store.arg_1 |= midgard_ldst_reg(src.reg, component);
893 }
894
895 if (src3 >= 0) {
896 struct phys_reg src = index_to_reg(ctx, g, src3);
897 unsigned component = __builtin_ctz(src.mask);
898 ins->load_store.arg_2 |= midgard_ldst_reg(src.reg, component);
899 }
900
901 break;
902 }
903
904 case TAG_TEXTURE_4: {
905 /* Grab RA results */
906 struct phys_reg dest = index_to_reg(ctx, g, ins->dest);
907 struct phys_reg coord = index_to_reg(ctx, g, ins->src[0]);
908 struct phys_reg lod = index_to_reg(ctx, g, ins->src[1]);
909
910 assert(dest.reg == 28 || dest.reg == 29);
911 assert(coord.reg == 28 || coord.reg == 29);
912
913 /* First, install the texture coordinate */
914 ins->texture.in_reg_full = 1;
915 ins->texture.in_reg_upper = 0;
916 ins->texture.in_reg_select = coord.reg - 28;
917 ins->texture.in_reg_swizzle =
918 compose_swizzle(ins->texture.in_reg_swizzle, 0xF, coord, dest);
919
920 /* Next, install the destination */
921 ins->texture.out_full = 1;
922 ins->texture.out_upper = 0;
923 ins->texture.out_reg_select = dest.reg - 28;
924 ins->texture.swizzle =
925 compose_swizzle(ins->texture.swizzle, dest.mask, dest, dest);
926 ins->mask =
927 compose_writemask(ins->mask, dest);
928
929 /* If there is a register LOD/bias, use it */
930 if (ins->src[1] != ~0) {
931 midgard_tex_register_select sel = {
932 .select = lod.reg,
933 .full = 1,
934 .component = lod.swizzle & 3,
935 };
936
937 uint8_t packed;
938 memcpy(&packed, &sel, sizeof(packed));
939 ins->texture.bias = packed;
940 }
941
942 break;
943 }
944
945 default:
946 break;
947 }
948 }
949
950 void
951 install_registers(compiler_context *ctx, struct ra_graph *g)
952 {
953 mir_foreach_block(ctx, block) {
954 mir_foreach_instr_in_block(block, ins) {
955 install_registers_instr(ctx, g, ins);
956 }
957 }
958
959 }