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