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