f74174498d0f329c35306657a4cd8ade8bf78089
[mesa.git] / src / freedreno / ir3 / ir3_ra.c
1 /*
2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include "util/u_math.h"
28 #include "util/register_allocate.h"
29 #include "util/ralloc.h"
30 #include "util/bitset.h"
31
32 #include "ir3.h"
33 #include "ir3_compiler.h"
34 #include "ir3_ra.h"
35
36
37 #ifdef DEBUG
38 #define RA_DEBUG (ir3_shader_debug & IR3_DBG_RAMSGS)
39 #else
40 #define RA_DEBUG 0
41 #endif
42 #define d(fmt, ...) do { if (RA_DEBUG) { \
43 printf("RA: "fmt"\n", ##__VA_ARGS__); \
44 } } while (0)
45
46 #define di(instr, fmt, ...) do { if (RA_DEBUG) { \
47 printf("RA: "fmt": ", ##__VA_ARGS__); \
48 ir3_print_instr(instr); \
49 } } while (0)
50
51 /*
52 * Register Assignment:
53 *
54 * Uses the register_allocate util, which implements graph coloring
55 * algo with interference classes. To handle the cases where we need
56 * consecutive registers (for example, texture sample instructions),
57 * we model these as larger (double/quad/etc) registers which conflict
58 * with the corresponding registers in other classes.
59 *
60 * Additionally we create additional classes for half-regs, which
61 * do not conflict with the full-reg classes. We do need at least
62 * sizes 1-4 (to deal w/ texture sample instructions output to half-
63 * reg). At the moment we don't create the higher order half-reg
64 * classes as half-reg frequently does not have enough precision
65 * for texture coords at higher resolutions.
66 *
67 * There are some additional cases that we need to handle specially,
68 * as the graph coloring algo doesn't understand "partial writes".
69 * For example, a sequence like:
70 *
71 * add r0.z, ...
72 * sam (f32)(xy)r0.x, ...
73 * ...
74 * sam (f32)(xyzw)r0.w, r0.x, ... ; 3d texture, so r0.xyz are coord
75 *
76 * In this scenario, we treat r0.xyz as class size 3, which is written
77 * (from a use/def perspective) at the 'add' instruction and ignore the
78 * subsequent partial writes to r0.xy. So the 'add r0.z, ...' is the
79 * defining instruction, as it is the first to partially write r0.xyz.
80 *
81 * To address the fragmentation that this can potentially cause, a
82 * two pass register allocation is used. After the first pass the
83 * assignment of scalars is discarded, but the assignment of vecN (for
84 * N > 1) is used to pre-color in the second pass, which considers
85 * only scalars.
86 *
87 * Arrays of arbitrary size are handled via pre-coloring a consecutive
88 * sequence of registers. Additional scalar (single component) reg
89 * names are allocated starting at ctx->class_base[total_class_count]
90 * (see arr->base), which are pre-colored. In the use/def graph direct
91 * access is treated as a single element use/def, and indirect access
92 * is treated as use or def of all array elements. (Only the first
93 * def is tracked, in case of multiple indirect writes, etc.)
94 *
95 * TODO arrays that fit in one of the pre-defined class sizes should
96 * not need to be pre-colored, but instead could be given a normal
97 * vreg name. (Ignoring this for now since it is a good way to work
98 * out the kinks with arbitrary sized arrays.)
99 *
100 * TODO might be easier for debugging to split this into two passes,
101 * the first assigning vreg names in a way that we could ir3_print()
102 * the result.
103 */
104
105
106 static struct ir3_instruction * name_to_instr(struct ir3_ra_ctx *ctx, unsigned name);
107
108 static bool name_is_array(struct ir3_ra_ctx *ctx, unsigned name);
109 static struct ir3_array * name_to_array(struct ir3_ra_ctx *ctx, unsigned name);
110
111 /* does it conflict? */
112 static inline bool
113 intersects(unsigned a_start, unsigned a_end, unsigned b_start, unsigned b_end)
114 {
115 return !((a_start >= b_end) || (b_start >= a_end));
116 }
117
118 static unsigned
119 reg_size_for_array(struct ir3_array *arr)
120 {
121 if (arr->half)
122 return DIV_ROUND_UP(arr->length, 2);
123
124 return arr->length;
125 }
126
127 static bool
128 instr_before(struct ir3_instruction *a, struct ir3_instruction *b)
129 {
130 if (a->flags & IR3_INSTR_UNUSED)
131 return false;
132 return (a->ip < b->ip);
133 }
134
135 static struct ir3_instruction *
136 get_definer(struct ir3_ra_ctx *ctx, struct ir3_instruction *instr,
137 int *sz, int *off)
138 {
139 struct ir3_ra_instr_data *id = &ctx->instrd[instr->ip];
140 struct ir3_instruction *d = NULL;
141
142 if (ctx->scalar_pass) {
143 id->defn = instr;
144 id->off = 0;
145 id->sz = 1; /* considering things as N scalar regs now */
146 }
147
148 if (id->defn) {
149 *sz = id->sz;
150 *off = id->off;
151 return id->defn;
152 }
153
154 if (instr->opc == OPC_META_COLLECT) {
155 /* What about the case where collect is subset of array, we
156 * need to find the distance between where actual array starts
157 * and collect.. that probably doesn't happen currently.
158 */
159 struct ir3_register *src;
160 int dsz, doff;
161
162 /* note: don't use foreach_ssa_src as this gets called once
163 * while assigning regs (which clears SSA flag)
164 */
165 foreach_src_n (src, n, instr) {
166 struct ir3_instruction *dd;
167 if (!src->instr)
168 continue;
169
170 dd = get_definer(ctx, src->instr, &dsz, &doff);
171
172 if ((!d) || instr_before(dd, d)) {
173 d = dd;
174 *sz = dsz;
175 *off = doff - n;
176 }
177 }
178
179 } else if (instr->cp.right || instr->cp.left) {
180 /* covers also the meta:fo case, which ends up w/ single
181 * scalar instructions for each component:
182 */
183 struct ir3_instruction *f = ir3_neighbor_first(instr);
184
185 /* by definition, the entire sequence forms one linked list
186 * of single scalar register nodes (even if some of them may
187 * be splits from a texture sample (for example) instr. We
188 * just need to walk the list finding the first element of
189 * the group defined (lowest ip)
190 */
191 int cnt = 0;
192
193 /* need to skip over unused in the group: */
194 while (f && (f->flags & IR3_INSTR_UNUSED)) {
195 f = f->cp.right;
196 cnt++;
197 }
198
199 while (f) {
200 if ((!d) || instr_before(f, d))
201 d = f;
202 if (f == instr)
203 *off = cnt;
204 f = f->cp.right;
205 cnt++;
206 }
207
208 *sz = cnt;
209
210 } else {
211 /* second case is looking directly at the instruction which
212 * produces multiple values (eg, texture sample), rather
213 * than the split nodes that point back to that instruction.
214 * This isn't quite right, because it may be part of a larger
215 * group, such as:
216 *
217 * sam (f32)(xyzw)r0.x, ...
218 * add r1.x, ...
219 * add r1.y, ...
220 * sam (f32)(xyzw)r2.x, r0.w <-- (r0.w, r1.x, r1.y)
221 *
222 * need to come up with a better way to handle that case.
223 */
224 if (instr->address) {
225 *sz = instr->regs[0]->size;
226 } else {
227 *sz = util_last_bit(instr->regs[0]->wrmask);
228 }
229 *off = 0;
230 d = instr;
231 }
232
233 if (d->opc == OPC_META_SPLIT) {
234 struct ir3_instruction *dd;
235 int dsz, doff;
236
237 dd = get_definer(ctx, d->regs[1]->instr, &dsz, &doff);
238
239 /* by definition, should come before: */
240 debug_assert(instr_before(dd, d));
241
242 *sz = MAX2(*sz, dsz);
243
244 if (instr->opc == OPC_META_SPLIT)
245 *off = MAX2(*off, instr->split.off);
246
247 d = dd;
248 }
249
250 debug_assert(d->opc != OPC_META_SPLIT);
251
252 id->defn = d;
253 id->sz = *sz;
254 id->off = *off;
255
256 return d;
257 }
258
259 static void
260 ra_block_find_definers(struct ir3_ra_ctx *ctx, struct ir3_block *block)
261 {
262 foreach_instr (instr, &block->instr_list) {
263 struct ir3_ra_instr_data *id = &ctx->instrd[instr->ip];
264 if (instr->regs_count == 0)
265 continue;
266 /* couple special cases: */
267 if (writes_addr0(instr) || writes_addr1(instr) || writes_pred(instr)) {
268 id->cls = -1;
269 } else if (instr->regs[0]->flags & IR3_REG_ARRAY) {
270 id->cls = total_class_count;
271 } else {
272 /* and the normal case: */
273 id->defn = get_definer(ctx, instr, &id->sz, &id->off);
274 id->cls = ra_size_to_class(id->sz, is_half(id->defn), is_high(id->defn));
275
276 /* this is a bit of duct-tape.. if we have a scenario like:
277 *
278 * sam (f32)(x) out.x, ...
279 * sam (f32)(x) out.y, ...
280 *
281 * Then the fanout/split meta instructions for the two different
282 * tex instructions end up grouped as left/right neighbors. The
283 * upshot is that in when you get_definer() on one of the meta:fo's
284 * you get definer as the first sam with sz=2, but when you call
285 * get_definer() on the either of the sam's you get itself as the
286 * definer with sz=1.
287 *
288 * (We actually avoid this scenario exactly, the neighbor links
289 * prevent one of the output mov's from being eliminated, so this
290 * hack should be enough. But probably we need to rethink how we
291 * find the "defining" instruction.)
292 *
293 * TODO how do we figure out offset properly...
294 */
295 if (id->defn != instr) {
296 struct ir3_ra_instr_data *did = &ctx->instrd[id->defn->ip];
297 if (did->sz < id->sz) {
298 did->sz = id->sz;
299 did->cls = id->cls;
300 }
301 }
302 }
303 }
304 }
305
306 /* give each instruction a name (and ip), and count up the # of names
307 * of each class
308 */
309 static void
310 ra_block_name_instructions(struct ir3_ra_ctx *ctx, struct ir3_block *block)
311 {
312 foreach_instr (instr, &block->instr_list) {
313 struct ir3_ra_instr_data *id = &ctx->instrd[instr->ip];
314
315 #ifdef DEBUG
316 instr->name = ~0;
317 #endif
318
319 ctx->instr_cnt++;
320
321 if (!writes_gpr(instr))
322 continue;
323
324 if (id->defn != instr)
325 continue;
326
327 /* In scalar pass, collect/split don't get their own names,
328 * but instead inherit them from their src(s):
329 *
330 * Possibly we don't need this because of scalar_name(), but
331 * it does make the ir3_print() dumps easier to read.
332 */
333 if (ctx->scalar_pass) {
334 if (instr->opc == OPC_META_SPLIT) {
335 instr->name = instr->regs[1]->instr->name + instr->split.off;
336 continue;
337 }
338
339 if (instr->opc == OPC_META_COLLECT) {
340 instr->name = instr->regs[1]->instr->name;
341 continue;
342 }
343 }
344
345 /* arrays which don't fit in one of the pre-defined class
346 * sizes are pre-colored:
347 */
348 if ((id->cls >= 0) && (id->cls < total_class_count)) {
349 /* in the scalar pass, we generate a name for each
350 * scalar component, instr->name is the name of the
351 * first component.
352 */
353 unsigned n = ctx->scalar_pass ? dest_regs(instr) : 1;
354 instr->name = ctx->class_alloc_count[id->cls];
355 ctx->class_alloc_count[id->cls] += n;
356 ctx->alloc_count += n;
357 }
358 }
359 }
360
361 /**
362 * Set a value for max register target.
363 *
364 * Currently this just rounds up to a multiple of full-vec4 (ie. the
365 * granularity that we configure the hw for.. there is no point to
366 * using r3.x if you aren't going to make r3.yzw available). But
367 * in reality there seems to be multiple thresholds that affect the
368 * number of waves.. and we should round up the target to the next
369 * threshold when we round-robin registers, to give postsched more
370 * options. When we understand that better, this is where we'd
371 * implement that.
372 */
373 static void
374 ra_set_register_target(struct ir3_ra_ctx *ctx, unsigned max_target)
375 {
376 const unsigned hvec4 = 4;
377 const unsigned vec4 = 2 * hvec4;
378
379 ctx->max_target = align(max_target, vec4);
380
381 d("New max_target=%u", ctx->max_target);
382 }
383
384 static int
385 pick_in_range(BITSET_WORD *regs, unsigned min, unsigned max)
386 {
387 for (unsigned i = min; i <= max; i++) {
388 if (BITSET_TEST(regs, i)) {
389 return i;
390 }
391 }
392 return -1;
393 }
394
395 static int
396 pick_in_range_rev(BITSET_WORD *regs, int min, int max)
397 {
398 for (int i = max; i >= min; i--) {
399 if (BITSET_TEST(regs, i)) {
400 return i;
401 }
402 }
403 return -1;
404 }
405
406 /* register selector for the a6xx+ merged register file: */
407 static unsigned int
408 ra_select_reg_merged(unsigned int n, BITSET_WORD *regs, void *data)
409 {
410 struct ir3_ra_ctx *ctx = data;
411 unsigned int class = ra_get_node_class(ctx->g, n);
412 bool half, high;
413 int sz = ra_class_to_size(class, &half, &high);
414
415 assert (sz > 0);
416
417 /* dimensions within the register class: */
418 unsigned max_target, start;
419
420 /* the regs bitset will include *all* of the virtual regs, but we lay
421 * out the different classes consecutively in the virtual register
422 * space. So we just need to think about the base offset of a given
423 * class within the virtual register space, and offset the register
424 * space we search within by that base offset.
425 */
426 unsigned base;
427
428 /* TODO I think eventually we want to round-robin in vector pass
429 * as well, but needs some more work to calculate # of live vals
430 * for this. (Maybe with some work, we could just figure out
431 * the scalar target and use that, since that is what we care
432 * about in the end.. but that would mean setting up use-def/
433 * liveranges for scalar pass before doing vector pass.)
434 *
435 * For now, in the vector class, just move assignments for scalar
436 * vals higher to hopefully prevent them from limiting where vecN
437 * values can be placed. Since the scalar values are re-assigned
438 * in the 2nd pass, we don't really care where they end up in the
439 * vector pass.
440 */
441 if (!ctx->scalar_pass) {
442 base = ctx->set->gpr_to_ra_reg[class][0];
443 if (high) {
444 max_target = HIGH_CLASS_REGS(class - HIGH_OFFSET);
445 } else if (half) {
446 max_target = HALF_CLASS_REGS(class - HALF_OFFSET);
447 } else {
448 max_target = CLASS_REGS(class);
449 }
450
451 if ((sz == 1) && !high) {
452 return pick_in_range_rev(regs, base, base + max_target);
453 } else {
454 return pick_in_range(regs, base, base + max_target);
455 }
456 } else {
457 assert(sz == 1);
458 }
459
460 /* NOTE: this is only used in scalar pass, so the register
461 * class will be one of the scalar classes (ie. idx==0):
462 */
463 base = ctx->set->gpr_to_ra_reg[class][0];
464 if (high) {
465 max_target = HIGH_CLASS_REGS(0);
466 start = 0;
467 } else if (half) {
468 max_target = ctx->max_target;
469 start = ctx->start_search_reg;
470 } else {
471 max_target = ctx->max_target / 2;
472 start = ctx->start_search_reg;
473 }
474
475 /* For cat4 instructions, if the src reg is already assigned, and
476 * avail to pick, use it. Because this doesn't introduce unnecessary
477 * dependencies, and it potentially avoids needing (ss) syncs to
478 * for write after read hazards:
479 */
480 struct ir3_instruction *instr = name_to_instr(ctx, n);
481 if (is_sfu(instr)) {
482 struct ir3_register *src = instr->regs[1];
483 int src_n;
484
485 if ((src->flags & IR3_REG_ARRAY) && !(src->flags & IR3_REG_RELATIV)) {
486 struct ir3_array *arr = ir3_lookup_array(ctx->ir, src->array.id);
487 src_n = arr->base + src->array.offset;
488 } else {
489 src_n = scalar_name(ctx, src->instr, 0);
490 }
491
492 unsigned reg = ra_get_node_reg(ctx->g, src_n);
493
494 /* Check if the src register has been assigned yet: */
495 if (reg != NO_REG) {
496 if (BITSET_TEST(regs, reg)) {
497 return reg;
498 }
499 }
500 } else if (is_tex_or_prefetch(instr)) {
501 /* we could have a tex fetch w/ wrmask .z, for example.. these
502 * cannot land in r0.x since that would underflow when we
503 * subtract the offset. Ie. if we pick r0.z, and subtract
504 * the offset, the register encoded for dst will be r0.x
505 */
506 unsigned n = ffs(instr->regs[0]->wrmask);
507 debug_assert(n > 0);
508 unsigned offset = n - 1;
509 if (!half)
510 offset *= 2;
511 base += offset;
512 max_target -= offset;
513 }
514
515 int r = pick_in_range(regs, base + start, base + max_target);
516 if (r < 0) {
517 /* wrap-around: */
518 r = pick_in_range(regs, base, base + start);
519 }
520
521 if (r < 0) {
522 /* overflow, we need to increase max_target: */
523 ra_set_register_target(ctx, ctx->max_target + 1);
524 return ra_select_reg_merged(n, regs, data);
525 }
526
527 if (class == ctx->set->half_classes[0]) {
528 int n = r - base;
529 ctx->start_search_reg = (n + 1) % ctx->max_target;
530 } else if (class == ctx->set->classes[0]) {
531 int n = (r - base) * 2;
532 ctx->start_search_reg = (n + 1) % ctx->max_target;
533 }
534
535 return r;
536 }
537
538 static void
539 ra_init(struct ir3_ra_ctx *ctx)
540 {
541 unsigned n, base;
542
543 ir3_clear_mark(ctx->ir);
544 n = ir3_count_instructions_ra(ctx->ir);
545
546 ctx->instrd = rzalloc_array(NULL, struct ir3_ra_instr_data, n);
547
548 foreach_block (block, &ctx->ir->block_list) {
549 ra_block_find_definers(ctx, block);
550 }
551
552 foreach_block (block, &ctx->ir->block_list) {
553 ra_block_name_instructions(ctx, block);
554 }
555
556 /* figure out the base register name for each class. The
557 * actual ra name is class_base[cls] + instr->name;
558 */
559 ctx->class_base[0] = 0;
560 for (unsigned i = 1; i <= total_class_count; i++) {
561 ctx->class_base[i] = ctx->class_base[i-1] +
562 ctx->class_alloc_count[i-1];
563 }
564
565 /* and vreg names for array elements: */
566 base = ctx->class_base[total_class_count];
567 foreach_array (arr, &ctx->ir->array_list) {
568 arr->base = base;
569 ctx->class_alloc_count[total_class_count] += reg_size_for_array(arr);
570 base += reg_size_for_array(arr);
571 }
572 ctx->alloc_count += ctx->class_alloc_count[total_class_count];
573
574 ctx->g = ra_alloc_interference_graph(ctx->set->regs, ctx->alloc_count);
575 ralloc_steal(ctx->g, ctx->instrd);
576 ctx->def = rzalloc_array(ctx->g, unsigned, ctx->alloc_count);
577 ctx->use = rzalloc_array(ctx->g, unsigned, ctx->alloc_count);
578
579 /* TODO add selector callback for split (pre-a6xx) register file: */
580 if (ctx->ir->compiler->gpu_id >= 600) {
581 ra_set_select_reg_callback(ctx->g, ra_select_reg_merged, ctx);
582
583 if (ctx->scalar_pass) {
584 ctx->name_to_instr = _mesa_hash_table_create(ctx->g,
585 _mesa_hash_int, _mesa_key_int_equal);
586 }
587 }
588 }
589
590 /* Map the name back to instruction: */
591 static struct ir3_instruction *
592 name_to_instr(struct ir3_ra_ctx *ctx, unsigned name)
593 {
594 assert(!name_is_array(ctx, name));
595 struct hash_entry *entry = _mesa_hash_table_search(ctx->name_to_instr, &name);
596 if (entry)
597 return entry->data;
598 unreachable("invalid instr name");
599 return NULL;
600 }
601
602 static bool
603 name_is_array(struct ir3_ra_ctx *ctx, unsigned name)
604 {
605 return name >= ctx->class_base[total_class_count];
606 }
607
608 static struct ir3_array *
609 name_to_array(struct ir3_ra_ctx *ctx, unsigned name)
610 {
611 assert(name_is_array(ctx, name));
612 foreach_array (arr, &ctx->ir->array_list) {
613 unsigned sz = reg_size_for_array(arr);
614 if (name < (arr->base + sz))
615 return arr;
616 }
617 unreachable("invalid array name");
618 return NULL;
619 }
620
621 static void
622 ra_destroy(struct ir3_ra_ctx *ctx)
623 {
624 ralloc_free(ctx->g);
625 }
626
627 static void
628 __def(struct ir3_ra_ctx *ctx, struct ir3_ra_block_data *bd, unsigned name,
629 struct ir3_instruction *instr)
630 {
631 debug_assert(name < ctx->alloc_count);
632
633 /* split/collect do not actually define any real value */
634 if ((instr->opc == OPC_META_SPLIT) || (instr->opc == OPC_META_COLLECT))
635 return;
636
637 /* defined on first write: */
638 if (!ctx->def[name])
639 ctx->def[name] = instr->ip;
640 ctx->use[name] = MAX2(ctx->use[name], instr->ip);
641 BITSET_SET(bd->def, name);
642 }
643
644 static void
645 __use(struct ir3_ra_ctx *ctx, struct ir3_ra_block_data *bd, unsigned name,
646 struct ir3_instruction *instr)
647 {
648 debug_assert(name < ctx->alloc_count);
649 ctx->use[name] = MAX2(ctx->use[name], instr->ip);
650 if (!BITSET_TEST(bd->def, name))
651 BITSET_SET(bd->use, name);
652 }
653
654 static void
655 ra_block_compute_live_ranges(struct ir3_ra_ctx *ctx, struct ir3_block *block)
656 {
657 struct ir3_ra_block_data *bd;
658 unsigned bitset_words = BITSET_WORDS(ctx->alloc_count);
659
660 #define def(name, instr) __def(ctx, bd, name, instr)
661 #define use(name, instr) __use(ctx, bd, name, instr)
662
663 bd = rzalloc(ctx->g, struct ir3_ra_block_data);
664
665 bd->def = rzalloc_array(bd, BITSET_WORD, bitset_words);
666 bd->use = rzalloc_array(bd, BITSET_WORD, bitset_words);
667 bd->livein = rzalloc_array(bd, BITSET_WORD, bitset_words);
668 bd->liveout = rzalloc_array(bd, BITSET_WORD, bitset_words);
669
670 block->data = bd;
671
672 struct ir3_instruction *first_non_input = NULL;
673 foreach_instr (instr, &block->instr_list) {
674 if (instr->opc != OPC_META_INPUT) {
675 first_non_input = instr;
676 break;
677 }
678 }
679
680 foreach_instr (instr, &block->instr_list) {
681 foreach_def (name, ctx, instr) {
682 if (name_is_array(ctx, name)) {
683 struct ir3_array *arr = name_to_array(ctx, name);
684
685 arr->start_ip = MIN2(arr->start_ip, instr->ip);
686 arr->end_ip = MAX2(arr->end_ip, instr->ip);
687
688 for (unsigned i = 0; i < arr->length; i++) {
689 unsigned name = arr->base + i;
690 if(arr->half)
691 ra_set_node_class(ctx->g, name, ctx->set->half_classes[0]);
692 else
693 ra_set_node_class(ctx->g, name, ctx->set->classes[0]);
694 }
695 } else {
696 struct ir3_ra_instr_data *id = &ctx->instrd[instr->ip];
697 if (is_high(instr)) {
698 ra_set_node_class(ctx->g, name,
699 ctx->set->high_classes[id->cls - HIGH_OFFSET]);
700 } else if (is_half(instr)) {
701 ra_set_node_class(ctx->g, name,
702 ctx->set->half_classes[id->cls - HALF_OFFSET]);
703 } else {
704 ra_set_node_class(ctx->g, name,
705 ctx->set->classes[id->cls]);
706 }
707 }
708
709 def(name, instr);
710
711 if ((instr->opc == OPC_META_INPUT) && first_non_input)
712 use(name, first_non_input);
713 }
714
715 foreach_use (name, ctx, instr) {
716 if (name_is_array(ctx, name)) {
717 struct ir3_array *arr = name_to_array(ctx, name);
718
719 arr->start_ip = MIN2(arr->start_ip, instr->ip);
720 arr->end_ip = MAX2(arr->end_ip, instr->ip);
721
722 /* NOTE: arrays are not SSA so unconditionally
723 * set use bit:
724 */
725 BITSET_SET(bd->use, name);
726 }
727
728 use(name, instr);
729 }
730
731 foreach_name (name, ctx, instr) {
732 /* split/collect instructions have duplicate names
733 * as real instructions, so they skip the hashtable:
734 */
735 if (ctx->name_to_instr && !((instr->opc == OPC_META_SPLIT) ||
736 (instr->opc == OPC_META_COLLECT))) {
737 /* this is slightly annoying, we can't just use an
738 * integer on the stack
739 */
740 unsigned *key = ralloc(ctx->name_to_instr, unsigned);
741 *key = name;
742 debug_assert(!_mesa_hash_table_search(ctx->name_to_instr, key));
743 _mesa_hash_table_insert(ctx->name_to_instr, key, instr);
744 }
745 }
746 }
747 }
748
749 static bool
750 ra_compute_livein_liveout(struct ir3_ra_ctx *ctx)
751 {
752 unsigned bitset_words = BITSET_WORDS(ctx->alloc_count);
753 bool progress = false;
754
755 foreach_block (block, &ctx->ir->block_list) {
756 struct ir3_ra_block_data *bd = block->data;
757
758 /* update livein: */
759 for (unsigned i = 0; i < bitset_words; i++) {
760 /* anything used but not def'd within a block is
761 * by definition a live value coming into the block:
762 */
763 BITSET_WORD new_livein =
764 (bd->use[i] | (bd->liveout[i] & ~bd->def[i]));
765
766 if (new_livein & ~bd->livein[i]) {
767 bd->livein[i] |= new_livein;
768 progress = true;
769 }
770 }
771
772 /* update liveout: */
773 for (unsigned j = 0; j < ARRAY_SIZE(block->successors); j++) {
774 struct ir3_block *succ = block->successors[j];
775 struct ir3_ra_block_data *succ_bd;
776
777 if (!succ)
778 continue;
779
780 succ_bd = succ->data;
781
782 for (unsigned i = 0; i < bitset_words; i++) {
783 /* add anything that is livein in a successor block
784 * to our liveout:
785 */
786 BITSET_WORD new_liveout =
787 (succ_bd->livein[i] & ~bd->liveout[i]);
788
789 if (new_liveout) {
790 bd->liveout[i] |= new_liveout;
791 progress = true;
792 }
793 }
794 }
795 }
796
797 return progress;
798 }
799
800 static void
801 print_bitset(const char *name, BITSET_WORD *bs, unsigned cnt)
802 {
803 bool first = true;
804 debug_printf("RA: %s:", name);
805 for (unsigned i = 0; i < cnt; i++) {
806 if (BITSET_TEST(bs, i)) {
807 if (!first)
808 debug_printf(",");
809 debug_printf(" %04u", i);
810 first = false;
811 }
812 }
813 debug_printf("\n");
814 }
815
816 /* size of one component of instruction result, ie. half vs full: */
817 static unsigned
818 live_size(struct ir3_instruction *instr)
819 {
820 if (is_half(instr)) {
821 return 1;
822 } else if (is_high(instr)) {
823 /* doesn't count towards footprint */
824 return 0;
825 } else {
826 return 2;
827 }
828 }
829
830 static unsigned
831 name_size(struct ir3_ra_ctx *ctx, unsigned name)
832 {
833 if (name_is_array(ctx, name)) {
834 struct ir3_array *arr = name_to_array(ctx, name);
835 return arr->half ? 1 : 2;
836 } else {
837 struct ir3_instruction *instr = name_to_instr(ctx, name);
838 /* in scalar pass, each name represents on scalar value,
839 * half or full precision
840 */
841 return live_size(instr);
842 }
843 }
844
845 static unsigned
846 ra_calc_block_live_values(struct ir3_ra_ctx *ctx, struct ir3_block *block)
847 {
848 struct ir3_ra_block_data *bd = block->data;
849 unsigned name;
850
851 assert(ctx->name_to_instr);
852
853 /* TODO this gets a bit more complicated in non-scalar pass.. but
854 * possibly a lowball estimate is fine to start with if we do
855 * round-robin in non-scalar pass? Maybe we just want to handle
856 * that in a different fxn?
857 */
858 assert(ctx->scalar_pass);
859
860 BITSET_WORD *live =
861 rzalloc_array(bd, BITSET_WORD, BITSET_WORDS(ctx->alloc_count));
862
863 /* Add the live input values: */
864 unsigned livein = 0;
865 BITSET_FOREACH_SET (name, bd->livein, ctx->alloc_count) {
866 livein += name_size(ctx, name);
867 BITSET_SET(live, name);
868 }
869
870 d("---------------------");
871 d("block%u: LIVEIN: %u", block_id(block), livein);
872
873 unsigned max = livein;
874 int cur_live = max;
875
876 /* Now that we know the live inputs to the block, iterate the
877 * instructions adjusting the current # of live values as we
878 * see their last use:
879 */
880 foreach_instr (instr, &block->instr_list) {
881 if (RA_DEBUG)
882 print_bitset("LIVE", live, ctx->alloc_count);
883 di(instr, "CALC");
884
885 unsigned new_live = 0; /* newly live values */
886 unsigned new_dead = 0; /* newly no-longer live values */
887 unsigned next_dead = 0; /* newly dead following this instr */
888
889 foreach_def (name, ctx, instr) {
890 /* NOTE: checking ctx->def filters out things like split/
891 * collect which are just redefining existing live names
892 * or array writes to already live array elements:
893 */
894 if (ctx->def[name] != instr->ip)
895 continue;
896 new_live += live_size(instr);
897 d("NEW_LIVE: %u (new_live=%u, use=%u)", name, new_live, ctx->use[name]);
898 BITSET_SET(live, name);
899 /* There can be cases where this is *also* the last use
900 * of a value, for example instructions that write multiple
901 * values, only some of which are used. These values are
902 * dead *after* (rather than during) this instruction.
903 */
904 if (ctx->use[name] != instr->ip)
905 continue;
906 next_dead += live_size(instr);
907 d("NEXT_DEAD: %u (next_dead=%u)", name, next_dead);
908 BITSET_CLEAR(live, name);
909 }
910
911 /* To be more resilient against special cases where liverange
912 * is extended (like first_non_input), rather than using the
913 * foreach_use() iterator, we iterate the current live values
914 * instead:
915 */
916 BITSET_FOREACH_SET (name, live, ctx->alloc_count) {
917 /* Is this the last use? */
918 if (ctx->use[name] != instr->ip)
919 continue;
920 new_dead += name_size(ctx, name);
921 d("NEW_DEAD: %u (new_dead=%u)", name, new_dead);
922 BITSET_CLEAR(live, name);
923 }
924
925 cur_live += new_live;
926 cur_live -= new_dead;
927
928 assert(cur_live >= 0);
929 d("CUR_LIVE: %u", cur_live);
930
931 max = MAX2(max, cur_live);
932
933 /* account for written values which are not used later,
934 * but after updating max (since they are for one cycle
935 * live)
936 */
937 cur_live -= next_dead;
938 assert(cur_live >= 0);
939
940 if (RA_DEBUG) {
941 unsigned cnt = 0;
942 BITSET_FOREACH_SET (name, live, ctx->alloc_count) {
943 cnt += name_size(ctx, name);
944 }
945 assert(cur_live == cnt);
946 }
947 }
948
949 d("block%u max=%u", block_id(block), max);
950
951 /* the remaining live should match liveout (for extra sanity testing): */
952 if (RA_DEBUG) {
953 unsigned new_dead = 0;
954 BITSET_FOREACH_SET (name, live, ctx->alloc_count) {
955 /* Is this the last use? */
956 if (ctx->use[name] != block->end_ip)
957 continue;
958 new_dead += name_size(ctx, name);
959 d("NEW_DEAD: %u (new_dead=%u)", name, new_dead);
960 BITSET_CLEAR(live, name);
961 }
962 unsigned liveout = 0;
963 BITSET_FOREACH_SET (name, bd->liveout, ctx->alloc_count) {
964 liveout += name_size(ctx, name);
965 BITSET_CLEAR(live, name);
966 }
967
968 if (cur_live != liveout) {
969 print_bitset("LEAKED", live, ctx->alloc_count);
970 /* TODO there are a few edge cases where live-range extension
971 * tells us a value is livein. But not used by the block or
972 * liveout for the block. Possibly a bug in the liverange
973 * extension. But for now leave the assert disabled:
974 assert(cur_live == liveout);
975 */
976 }
977 }
978
979 ralloc_free(live);
980
981 return max;
982 }
983
984 static unsigned
985 ra_calc_max_live_values(struct ir3_ra_ctx *ctx)
986 {
987 unsigned max = 0;
988
989 foreach_block (block, &ctx->ir->block_list) {
990 unsigned block_live = ra_calc_block_live_values(ctx, block);
991 max = MAX2(max, block_live);
992 }
993
994 return max;
995 }
996
997 static void
998 ra_add_interference(struct ir3_ra_ctx *ctx)
999 {
1000 struct ir3 *ir = ctx->ir;
1001
1002 /* initialize array live ranges: */
1003 foreach_array (arr, &ir->array_list) {
1004 arr->start_ip = ~0;
1005 arr->end_ip = 0;
1006 }
1007
1008 /* compute live ranges (use/def) on a block level, also updating
1009 * block's def/use bitmasks (used below to calculate per-block
1010 * livein/liveout):
1011 */
1012 foreach_block (block, &ir->block_list) {
1013 ra_block_compute_live_ranges(ctx, block);
1014 }
1015
1016 /* update per-block livein/liveout: */
1017 while (ra_compute_livein_liveout(ctx)) {}
1018
1019 if (RA_DEBUG) {
1020 d("AFTER LIVEIN/OUT:");
1021 foreach_block (block, &ir->block_list) {
1022 struct ir3_ra_block_data *bd = block->data;
1023 d("block%u:", block_id(block));
1024 print_bitset(" def", bd->def, ctx->alloc_count);
1025 print_bitset(" use", bd->use, ctx->alloc_count);
1026 print_bitset(" l/i", bd->livein, ctx->alloc_count);
1027 print_bitset(" l/o", bd->liveout, ctx->alloc_count);
1028 }
1029 foreach_array (arr, &ir->array_list) {
1030 d("array%u:", arr->id);
1031 d(" length: %u", arr->length);
1032 d(" start_ip: %u", arr->start_ip);
1033 d(" end_ip: %u", arr->end_ip);
1034 }
1035 d("INSTRUCTION VREG NAMES:");
1036 foreach_block (block, &ctx->ir->block_list) {
1037 foreach_instr (instr, &block->instr_list) {
1038 if (!ctx->instrd[instr->ip].defn)
1039 continue;
1040 if (!writes_gpr(instr))
1041 continue;
1042 di(instr, "%04u", scalar_name(ctx, instr, 0));
1043 }
1044 }
1045 d("ARRAY VREG NAMES:");
1046 foreach_array (arr, &ctx->ir->array_list) {
1047 d("%04u: arr%u", arr->base, arr->id);
1048 }
1049 }
1050
1051 /* extend start/end ranges based on livein/liveout info from cfg: */
1052 foreach_block (block, &ir->block_list) {
1053 struct ir3_ra_block_data *bd = block->data;
1054
1055 for (unsigned i = 0; i < ctx->alloc_count; i++) {
1056 if (BITSET_TEST(bd->livein, i)) {
1057 ctx->def[i] = MIN2(ctx->def[i], block->start_ip);
1058 ctx->use[i] = MAX2(ctx->use[i], block->start_ip);
1059 }
1060
1061 if (BITSET_TEST(bd->liveout, i)) {
1062 ctx->def[i] = MIN2(ctx->def[i], block->end_ip);
1063 ctx->use[i] = MAX2(ctx->use[i], block->end_ip);
1064 }
1065 }
1066
1067 foreach_array (arr, &ctx->ir->array_list) {
1068 for (unsigned i = 0; i < arr->length; i++) {
1069 if (BITSET_TEST(bd->livein, i + arr->base)) {
1070 arr->start_ip = MIN2(arr->start_ip, block->start_ip);
1071 }
1072 if (BITSET_TEST(bd->liveout, i + arr->base)) {
1073 arr->end_ip = MAX2(arr->end_ip, block->end_ip);
1074 }
1075 }
1076 }
1077 }
1078
1079 if (ctx->name_to_instr) {
1080 unsigned max = ra_calc_max_live_values(ctx);
1081 ra_set_register_target(ctx, max);
1082 }
1083
1084 for (unsigned i = 0; i < ctx->alloc_count; i++) {
1085 for (unsigned j = 0; j < ctx->alloc_count; j++) {
1086 if (intersects(ctx->def[i], ctx->use[i],
1087 ctx->def[j], ctx->use[j])) {
1088 ra_add_node_interference(ctx->g, i, j);
1089 }
1090 }
1091 }
1092 }
1093
1094 /* some instructions need fix-up if dst register is half precision: */
1095 static void fixup_half_instr_dst(struct ir3_instruction *instr)
1096 {
1097 switch (opc_cat(instr->opc)) {
1098 case 1: /* move instructions */
1099 instr->cat1.dst_type = half_type(instr->cat1.dst_type);
1100 break;
1101 case 4:
1102 switch (instr->opc) {
1103 case OPC_RSQ:
1104 instr->opc = OPC_HRSQ;
1105 break;
1106 case OPC_LOG2:
1107 instr->opc = OPC_HLOG2;
1108 break;
1109 case OPC_EXP2:
1110 instr->opc = OPC_HEXP2;
1111 break;
1112 default:
1113 break;
1114 }
1115 break;
1116 case 5:
1117 instr->cat5.type = half_type(instr->cat5.type);
1118 break;
1119 }
1120 }
1121 /* some instructions need fix-up if src register is half precision: */
1122 static void fixup_half_instr_src(struct ir3_instruction *instr)
1123 {
1124 switch (instr->opc) {
1125 case OPC_MOV:
1126 instr->cat1.src_type = half_type(instr->cat1.src_type);
1127 break;
1128 case OPC_MAD_F32:
1129 instr->opc = OPC_MAD_F16;
1130 break;
1131 case OPC_SEL_B32:
1132 instr->opc = OPC_SEL_B16;
1133 break;
1134 case OPC_SEL_S32:
1135 instr->opc = OPC_SEL_S16;
1136 break;
1137 case OPC_SEL_F32:
1138 instr->opc = OPC_SEL_F16;
1139 break;
1140 case OPC_SAD_S32:
1141 instr->opc = OPC_SAD_S16;
1142 break;
1143 default:
1144 break;
1145 }
1146 }
1147
1148 /* NOTE: instr could be NULL for IR3_REG_ARRAY case, for the first
1149 * array access(es) which do not have any previous access to depend
1150 * on from scheduling point of view
1151 */
1152 static void
1153 reg_assign(struct ir3_ra_ctx *ctx, struct ir3_register *reg,
1154 struct ir3_instruction *instr)
1155 {
1156 struct ir3_ra_instr_data *id;
1157
1158 if (reg->flags & IR3_REG_ARRAY) {
1159 struct ir3_array *arr =
1160 ir3_lookup_array(ctx->ir, reg->array.id);
1161 unsigned name = arr->base + reg->array.offset;
1162 unsigned r = ra_get_node_reg(ctx->g, name);
1163 unsigned num = ctx->set->ra_reg_to_gpr[r];
1164
1165 if (reg->flags & IR3_REG_RELATIV) {
1166 reg->array.offset = num;
1167 } else {
1168 reg->num = num;
1169 reg->flags &= ~IR3_REG_SSA;
1170 }
1171
1172 reg->flags &= ~IR3_REG_ARRAY;
1173 } else if ((id = &ctx->instrd[instr->ip]) && id->defn) {
1174 unsigned first_component = 0;
1175
1176 /* Special case for tex instructions, which may use the wrmask
1177 * to mask off the first component(s). In the scalar pass,
1178 * this means the masked off component(s) are not def'd/use'd,
1179 * so we get a bogus value when we ask the register_allocate
1180 * algo to get the assigned reg for the unused/untouched
1181 * component. So we need to consider the first used component:
1182 */
1183 if (ctx->scalar_pass && is_tex_or_prefetch(id->defn)) {
1184 unsigned n = ffs(id->defn->regs[0]->wrmask);
1185 debug_assert(n > 0);
1186 first_component = n - 1;
1187 }
1188
1189 unsigned name = scalar_name(ctx, id->defn, first_component);
1190 unsigned r = ra_get_node_reg(ctx->g, name);
1191 unsigned num = ctx->set->ra_reg_to_gpr[r] + id->off;
1192
1193 debug_assert(!(reg->flags & IR3_REG_RELATIV));
1194
1195 debug_assert(num >= first_component);
1196
1197 if (is_high(id->defn))
1198 num += FIRST_HIGH_REG;
1199
1200 reg->num = num - first_component;
1201
1202 reg->flags &= ~IR3_REG_SSA;
1203
1204 if (is_half(id->defn))
1205 reg->flags |= IR3_REG_HALF;
1206 }
1207 }
1208
1209 /* helper to determine which regs to assign in which pass: */
1210 static bool
1211 should_assign(struct ir3_ra_ctx *ctx, struct ir3_instruction *instr)
1212 {
1213 if ((instr->opc == OPC_META_SPLIT) &&
1214 (util_bitcount(instr->regs[1]->wrmask) > 1))
1215 return !ctx->scalar_pass;
1216 if ((instr->opc == OPC_META_COLLECT) &&
1217 (util_bitcount(instr->regs[0]->wrmask) > 1))
1218 return !ctx->scalar_pass;
1219 return ctx->scalar_pass;
1220 }
1221
1222 static void
1223 ra_block_alloc(struct ir3_ra_ctx *ctx, struct ir3_block *block)
1224 {
1225 foreach_instr (instr, &block->instr_list) {
1226 struct ir3_register *reg;
1227
1228 if (writes_gpr(instr)) {
1229 if (should_assign(ctx, instr)) {
1230 reg_assign(ctx, instr->regs[0], instr);
1231 if (instr->regs[0]->flags & IR3_REG_HALF)
1232 fixup_half_instr_dst(instr);
1233 }
1234 }
1235
1236 foreach_src_n (reg, n, instr) {
1237 struct ir3_instruction *src = reg->instr;
1238
1239 if (src && !should_assign(ctx, src) && !should_assign(ctx, instr))
1240 continue;
1241
1242 if (src && should_assign(ctx, instr))
1243 reg_assign(ctx, src->regs[0], src);
1244
1245 /* Note: reg->instr could be null for IR3_REG_ARRAY */
1246 if (src || (reg->flags & IR3_REG_ARRAY))
1247 reg_assign(ctx, instr->regs[n+1], src);
1248
1249 if (instr->regs[n+1]->flags & IR3_REG_HALF)
1250 fixup_half_instr_src(instr);
1251 }
1252 }
1253
1254 /* We need to pre-color outputs for the scalar pass in
1255 * ra_precolor_assigned(), so we need to actually assign
1256 * them in the first pass:
1257 */
1258 if (!ctx->scalar_pass) {
1259 struct ir3_instruction *in, *out;
1260
1261 foreach_input (in, ctx->ir) {
1262 reg_assign(ctx, in->regs[0], in);
1263 }
1264 foreach_output (out, ctx->ir) {
1265 reg_assign(ctx, out->regs[0], out);
1266 }
1267 }
1268 }
1269
1270 /* handle pre-colored registers. This includes "arrays" (which could be of
1271 * length 1, used for phi webs lowered to registers in nir), as well as
1272 * special shader input values that need to be pinned to certain registers.
1273 */
1274 static void
1275 ra_precolor(struct ir3_ra_ctx *ctx, struct ir3_instruction **precolor, unsigned nprecolor)
1276 {
1277 unsigned num_precolor = 0;
1278 for (unsigned i = 0; i < nprecolor; i++) {
1279 if (precolor[i] && !(precolor[i]->flags & IR3_INSTR_UNUSED)) {
1280 struct ir3_instruction *instr = precolor[i];
1281
1282 if (instr->regs[0]->num == INVALID_REG)
1283 continue;
1284
1285 struct ir3_ra_instr_data *id = &ctx->instrd[instr->ip];
1286
1287 debug_assert(!(instr->regs[0]->flags & (IR3_REG_HALF | IR3_REG_HIGH)));
1288
1289 /* only consider the first component: */
1290 if (id->off > 0)
1291 continue;
1292
1293 if (ctx->scalar_pass && !should_assign(ctx, instr))
1294 continue;
1295
1296 /* 'base' is in scalar (class 0) but we need to map that
1297 * the conflicting register of the appropriate class (ie.
1298 * input could be vec2/vec3/etc)
1299 *
1300 * Note that the higher class (larger than scalar) regs
1301 * are setup to conflict with others in the same class,
1302 * so for example, R1 (scalar) is also the first component
1303 * of D1 (vec2/double):
1304 *
1305 * Single (base) | Double
1306 * --------------+---------------
1307 * R0 | D0
1308 * R1 | D0 D1
1309 * R2 | D1 D2
1310 * R3 | D2
1311 * .. and so on..
1312 */
1313 unsigned regid = instr->regs[0]->num;
1314 unsigned reg = ctx->set->gpr_to_ra_reg[id->cls][regid];
1315 unsigned name = ra_name(ctx, id);
1316 ra_set_node_reg(ctx->g, name, reg);
1317 num_precolor = MAX2(regid, num_precolor);
1318 }
1319 }
1320
1321 /* pre-assign array elements:
1322 *
1323 * TODO this is going to need some work for half-precision.. possibly
1324 * this is easier on a6xx, where we can just divide array size by two?
1325 * But on a5xx and earlier it will need to track two bases.
1326 */
1327 foreach_array (arr, &ctx->ir->array_list) {
1328 unsigned base = 0;
1329
1330 if (arr->end_ip == 0)
1331 continue;
1332
1333 /* figure out what else we conflict with which has already
1334 * been assigned:
1335 */
1336 retry:
1337 foreach_array (arr2, &ctx->ir->array_list) {
1338 if (arr2 == arr)
1339 break;
1340 if (arr2->end_ip == 0)
1341 continue;
1342 /* if it intersects with liverange AND register range.. */
1343 if (intersects(arr->start_ip, arr->end_ip,
1344 arr2->start_ip, arr2->end_ip) &&
1345 intersects(base, base + reg_size_for_array(arr),
1346 arr2->reg, arr2->reg + reg_size_for_array(arr2))) {
1347 base = MAX2(base, arr2->reg + reg_size_for_array(arr2));
1348 goto retry;
1349 }
1350 }
1351
1352 /* also need to not conflict with any pre-assigned inputs: */
1353 for (unsigned i = 0; i < nprecolor; i++) {
1354 struct ir3_instruction *instr = precolor[i];
1355
1356 if (!instr || (instr->flags & IR3_INSTR_UNUSED))
1357 continue;
1358
1359 struct ir3_ra_instr_data *id = &ctx->instrd[instr->ip];
1360
1361 /* only consider the first component: */
1362 if (id->off > 0)
1363 continue;
1364
1365 unsigned name = ra_name(ctx, id);
1366 unsigned regid = instr->regs[0]->num;
1367
1368 /* Check if array intersects with liverange AND register
1369 * range of the input:
1370 */
1371 if (intersects(arr->start_ip, arr->end_ip,
1372 ctx->def[name], ctx->use[name]) &&
1373 intersects(base, base + reg_size_for_array(arr),
1374 regid, regid + class_sizes[id->cls])) {
1375 base = MAX2(base, regid + class_sizes[id->cls]);
1376 goto retry;
1377 }
1378 }
1379
1380 arr->reg = base;
1381
1382 for (unsigned i = 0; i < arr->length; i++) {
1383 unsigned name, reg;
1384
1385 if (arr->half) {
1386 /* Doesn't need to do this on older generations than a6xx,
1387 * since there's no conflict between full regs and half regs
1388 * on them.
1389 *
1390 * TODO Presumably "base" could start from 0 respectively
1391 * for half regs of arrays on older generations.
1392 */
1393 unsigned base_half = base * 2 + i;
1394 reg = ctx->set->gpr_to_ra_reg[0+HALF_OFFSET][base_half];
1395 base = base_half / 2 + 1;
1396 } else {
1397 reg = ctx->set->gpr_to_ra_reg[0][base++];
1398 }
1399
1400 name = arr->base + i;
1401 ra_set_node_reg(ctx->g, name, reg);
1402 }
1403 }
1404
1405 if (ir3_shader_debug & IR3_DBG_OPTMSGS) {
1406 foreach_array (arr, &ctx->ir->array_list) {
1407 unsigned first = arr->reg;
1408 unsigned last = arr->reg + arr->length - 1;
1409 debug_printf("arr[%d] at r%d.%c->r%d.%c\n", arr->id,
1410 (first >> 2), "xyzw"[first & 0x3],
1411 (last >> 2), "xyzw"[last & 0x3]);
1412 }
1413 }
1414 }
1415
1416 static void
1417 precolor(struct ir3_ra_ctx *ctx, struct ir3_instruction *instr)
1418 {
1419 struct ir3_ra_instr_data *id = &ctx->instrd[instr->ip];
1420 unsigned n = dest_regs(instr);
1421 for (unsigned i = 0; i < n; i++) {
1422 /* tex instructions actually have a wrmask, and
1423 * don't touch masked out components. So we
1424 * shouldn't precolor them::
1425 */
1426 if (is_tex_or_prefetch(instr) &&
1427 !(instr->regs[0]->wrmask & (1 << i)))
1428 continue;
1429
1430 unsigned name = scalar_name(ctx, instr, i);
1431 unsigned regid = instr->regs[0]->num + i;
1432
1433 if (instr->regs[0]->flags & IR3_REG_HIGH)
1434 regid -= FIRST_HIGH_REG;
1435
1436 unsigned vreg = ctx->set->gpr_to_ra_reg[id->cls][regid];
1437 ra_set_node_reg(ctx->g, name, vreg);
1438 }
1439 }
1440
1441 /* pre-color non-scalar registers based on the registers assigned in previous
1442 * pass. Do this by looking actually at the fanout instructions.
1443 */
1444 static void
1445 ra_precolor_assigned(struct ir3_ra_ctx *ctx)
1446 {
1447 debug_assert(ctx->scalar_pass);
1448
1449 foreach_block (block, &ctx->ir->block_list) {
1450 foreach_instr (instr, &block->instr_list) {
1451
1452 if (!writes_gpr(instr))
1453 continue;
1454
1455 if (should_assign(ctx, instr))
1456 continue;
1457
1458 precolor(ctx, instr);
1459
1460 struct ir3_register *src;
1461 foreach_src (src, instr) {
1462 if (!src->instr)
1463 continue;
1464 precolor(ctx, src->instr);
1465 }
1466 }
1467 }
1468 }
1469
1470 static int
1471 ra_alloc(struct ir3_ra_ctx *ctx)
1472 {
1473 if (!ra_allocate(ctx->g))
1474 return -1;
1475
1476 foreach_block (block, &ctx->ir->block_list) {
1477 ra_block_alloc(ctx, block);
1478 }
1479
1480 return 0;
1481 }
1482
1483 /* if we end up with split/collect instructions with non-matching src
1484 * and dest regs, that means something has gone wrong. Which makes it
1485 * a pretty good sanity check.
1486 */
1487 static void
1488 ra_sanity_check(struct ir3 *ir)
1489 {
1490 foreach_block (block, &ir->block_list) {
1491 foreach_instr (instr, &block->instr_list) {
1492 if (instr->opc == OPC_META_SPLIT) {
1493 struct ir3_register *dst = instr->regs[0];
1494 struct ir3_register *src = instr->regs[1];
1495 debug_assert(dst->num == (src->num + instr->split.off));
1496 } else if (instr->opc == OPC_META_COLLECT) {
1497 struct ir3_register *dst = instr->regs[0];
1498 struct ir3_register *src;
1499
1500 foreach_src_n (src, n, instr) {
1501 debug_assert(dst->num == (src->num - n));
1502 }
1503 }
1504 }
1505 }
1506 }
1507
1508 static int
1509 ir3_ra_pass(struct ir3_shader_variant *v, struct ir3_instruction **precolor,
1510 unsigned nprecolor, bool scalar_pass)
1511 {
1512 struct ir3_ra_ctx ctx = {
1513 .v = v,
1514 .ir = v->ir,
1515 .set = v->ir->compiler->set,
1516 .scalar_pass = scalar_pass,
1517 };
1518 int ret;
1519
1520 ra_init(&ctx);
1521 ra_add_interference(&ctx);
1522 ra_precolor(&ctx, precolor, nprecolor);
1523 if (scalar_pass)
1524 ra_precolor_assigned(&ctx);
1525 ret = ra_alloc(&ctx);
1526 ra_destroy(&ctx);
1527
1528 return ret;
1529 }
1530
1531 int
1532 ir3_ra(struct ir3_shader_variant *v, struct ir3_instruction **precolor,
1533 unsigned nprecolor)
1534 {
1535 int ret;
1536
1537 /* First pass, assign the vecN (non-scalar) registers: */
1538 ret = ir3_ra_pass(v, precolor, nprecolor, false);
1539 if (ret)
1540 return ret;
1541
1542 if (ir3_shader_debug & IR3_DBG_OPTMSGS) {
1543 printf("AFTER RA (1st pass):\n");
1544 ir3_print(v->ir);
1545 }
1546
1547 /* Second pass, assign the scalar registers: */
1548 ret = ir3_ra_pass(v, precolor, nprecolor, true);
1549 if (ret)
1550 return ret;
1551
1552 if (ir3_shader_debug & IR3_DBG_OPTMSGS) {
1553 printf("AFTER RA (2nd pass):\n");
1554 ir3_print(v->ir);
1555 }
1556
1557 #ifdef DEBUG
1558 # define SANITY_CHECK DEBUG
1559 #else
1560 # define SANITY_CHECK 0
1561 #endif
1562 if (SANITY_CHECK)
1563 ra_sanity_check(v->ir);
1564
1565 return ret;
1566 }