2f312a5ff1569998ed0a0152a37eb549ca723e57
[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
35 /*
36 * Register Assignment:
37 *
38 * Uses the register_allocate util, which implements graph coloring
39 * algo with interference classes. To handle the cases where we need
40 * consecutive registers (for example, texture sample instructions),
41 * we model these as larger (double/quad/etc) registers which conflict
42 * with the corresponding registers in other classes.
43 *
44 * Additionally we create additional classes for half-regs, which
45 * do not conflict with the full-reg classes. We do need at least
46 * sizes 1-4 (to deal w/ texture sample instructions output to half-
47 * reg). At the moment we don't create the higher order half-reg
48 * classes as half-reg frequently does not have enough precision
49 * for texture coords at higher resolutions.
50 *
51 * There are some additional cases that we need to handle specially,
52 * as the graph coloring algo doesn't understand "partial writes".
53 * For example, a sequence like:
54 *
55 * add r0.z, ...
56 * sam (f32)(xy)r0.x, ...
57 * ...
58 * sam (f32)(xyzw)r0.w, r0.x, ... ; 3d texture, so r0.xyz are coord
59 *
60 * In this scenario, we treat r0.xyz as class size 3, which is written
61 * (from a use/def perspective) at the 'add' instruction and ignore the
62 * subsequent partial writes to r0.xy. So the 'add r0.z, ...' is the
63 * defining instruction, as it is the first to partially write r0.xyz.
64 *
65 * Note i965 has a similar scenario, which they solve with a virtual
66 * LOAD_PAYLOAD instruction which gets turned into multiple MOV's after
67 * register assignment. But for us that is horrible from a scheduling
68 * standpoint. Instead what we do is use idea of 'definer' instruction.
69 * Ie. the first instruction (lowest ip) to write to the variable is the
70 * one we consider from use/def perspective when building interference
71 * graph. (Other instructions which write other variable components
72 * just define the variable some more.)
73 *
74 * Arrays of arbitrary size are handled via pre-coloring a consecutive
75 * sequence of registers. Additional scalar (single component) reg
76 * names are allocated starting at ctx->class_base[total_class_count]
77 * (see arr->base), which are pre-colored. In the use/def graph direct
78 * access is treated as a single element use/def, and indirect access
79 * is treated as use or def of all array elements. (Only the first
80 * def is tracked, in case of multiple indirect writes, etc.)
81 *
82 * TODO arrays that fit in one of the pre-defined class sizes should
83 * not need to be pre-colored, but instead could be given a normal
84 * vreg name. (Ignoring this for now since it is a good way to work
85 * out the kinks with arbitrary sized arrays.)
86 *
87 * TODO might be easier for debugging to split this into two passes,
88 * the first assigning vreg names in a way that we could ir3_print()
89 * the result.
90 */
91
92 static const unsigned class_sizes[] = {
93 1, 2, 3, 4,
94 4 + 4, /* txd + 1d/2d */
95 4 + 6, /* txd + 3d */
96 };
97 #define class_count ARRAY_SIZE(class_sizes)
98
99 static const unsigned half_class_sizes[] = {
100 1, 2, 3, 4,
101 };
102 #define half_class_count ARRAY_SIZE(half_class_sizes)
103
104 /* seems to just be used for compute shaders? Seems like vec1 and vec3
105 * are sufficient (for now?)
106 */
107 static const unsigned high_class_sizes[] = {
108 1, 3,
109 };
110 #define high_class_count ARRAY_SIZE(high_class_sizes)
111
112 #define total_class_count (class_count + half_class_count + high_class_count)
113
114 /* Below a0.x are normal regs. RA doesn't need to assign a0.x/p0.x. */
115 #define NUM_REGS (4 * 48) /* r0 to r47 */
116 #define NUM_HIGH_REGS (4 * 8) /* r48 to r55 */
117 #define FIRST_HIGH_REG (4 * 48)
118 /* Number of virtual regs in a given class: */
119 #define CLASS_REGS(i) (NUM_REGS - (class_sizes[i] - 1))
120 #define HALF_CLASS_REGS(i) (NUM_REGS - (half_class_sizes[i] - 1))
121 #define HIGH_CLASS_REGS(i) (NUM_HIGH_REGS - (high_class_sizes[i] - 1))
122
123 #define HALF_OFFSET (class_count)
124 #define HIGH_OFFSET (class_count + half_class_count)
125
126 /* register-set, created one time, used for all shaders: */
127 struct ir3_ra_reg_set {
128 struct ra_regs *regs;
129 unsigned int classes[class_count];
130 unsigned int half_classes[half_class_count];
131 unsigned int high_classes[high_class_count];
132 /* maps flat virtual register space to base gpr: */
133 uint16_t *ra_reg_to_gpr;
134 /* maps cls,gpr to flat virtual register space: */
135 uint16_t **gpr_to_ra_reg;
136 };
137
138 static void
139 build_q_values(unsigned int **q_values, unsigned off,
140 const unsigned *sizes, unsigned count)
141 {
142 for (unsigned i = 0; i < count; i++) {
143 q_values[i + off] = rzalloc_array(q_values, unsigned, total_class_count);
144
145 /* From register_allocate.c:
146 *
147 * q(B,C) (indexed by C, B is this register class) in
148 * Runeson/Nyström paper. This is "how many registers of B could
149 * the worst choice register from C conflict with".
150 *
151 * If we just let the register allocation algorithm compute these
152 * values, is extremely expensive. However, since all of our
153 * registers are laid out, we can very easily compute them
154 * ourselves. View the register from C as fixed starting at GRF n
155 * somewhere in the middle, and the register from B as sliding back
156 * and forth. Then the first register to conflict from B is the
157 * one starting at n - class_size[B] + 1 and the last register to
158 * conflict will start at n + class_size[B] - 1. Therefore, the
159 * number of conflicts from B is class_size[B] + class_size[C] - 1.
160 *
161 * +-+-+-+-+-+-+ +-+-+-+-+-+-+
162 * B | | | | | |n| --> | | | | | | |
163 * +-+-+-+-+-+-+ +-+-+-+-+-+-+
164 * +-+-+-+-+-+
165 * C |n| | | | |
166 * +-+-+-+-+-+
167 *
168 * (Idea copied from brw_fs_reg_allocate.cpp)
169 */
170 for (unsigned j = 0; j < count; j++)
171 q_values[i + off][j + off] = sizes[i] + sizes[j] - 1;
172 }
173 }
174
175 /* One-time setup of RA register-set, which describes all the possible
176 * "virtual" registers and their interferences. Ie. double register
177 * occupies (and conflicts with) two single registers, and so forth.
178 * Since registers do not need to be aligned to their class size, they
179 * can conflict with other registers in the same class too. Ie:
180 *
181 * Single (base) | Double
182 * --------------+---------------
183 * R0 | D0
184 * R1 | D0 D1
185 * R2 | D1 D2
186 * R3 | D2
187 * .. and so on..
188 *
189 * (NOTE the disassembler uses notation like r0.x/y/z/w but those are
190 * really just four scalar registers. Don't let that confuse you.)
191 */
192 struct ir3_ra_reg_set *
193 ir3_ra_alloc_reg_set(struct ir3_compiler *compiler)
194 {
195 struct ir3_ra_reg_set *set = rzalloc(compiler, struct ir3_ra_reg_set);
196 unsigned ra_reg_count, reg, first_half_reg, first_high_reg, base;
197 unsigned int **q_values;
198
199 /* calculate # of regs across all classes: */
200 ra_reg_count = 0;
201 for (unsigned i = 0; i < class_count; i++)
202 ra_reg_count += CLASS_REGS(i);
203 for (unsigned i = 0; i < half_class_count; i++)
204 ra_reg_count += HALF_CLASS_REGS(i);
205 for (unsigned i = 0; i < high_class_count; i++)
206 ra_reg_count += HIGH_CLASS_REGS(i);
207
208 /* allocate and populate q_values: */
209 q_values = ralloc_array(set, unsigned *, total_class_count);
210
211 build_q_values(q_values, 0, class_sizes, class_count);
212 build_q_values(q_values, HALF_OFFSET, half_class_sizes, half_class_count);
213 build_q_values(q_values, HIGH_OFFSET, high_class_sizes, high_class_count);
214
215 /* allocate the reg-set.. */
216 set->regs = ra_alloc_reg_set(set, ra_reg_count, true);
217 set->ra_reg_to_gpr = ralloc_array(set, uint16_t, ra_reg_count);
218 set->gpr_to_ra_reg = ralloc_array(set, uint16_t *, total_class_count);
219
220 /* .. and classes */
221 reg = 0;
222 for (unsigned i = 0; i < class_count; i++) {
223 set->classes[i] = ra_alloc_reg_class(set->regs);
224
225 set->gpr_to_ra_reg[i] = ralloc_array(set, uint16_t, CLASS_REGS(i));
226
227 for (unsigned j = 0; j < CLASS_REGS(i); j++) {
228 ra_class_add_reg(set->regs, set->classes[i], reg);
229
230 set->ra_reg_to_gpr[reg] = j;
231 set->gpr_to_ra_reg[i][j] = reg;
232
233 for (unsigned br = j; br < j + class_sizes[i]; br++)
234 ra_add_transitive_reg_conflict(set->regs, br, reg);
235
236 reg++;
237 }
238 }
239
240 first_half_reg = reg;
241 base = HALF_OFFSET;
242
243 for (unsigned i = 0; i < half_class_count; i++) {
244 set->half_classes[i] = ra_alloc_reg_class(set->regs);
245
246 set->gpr_to_ra_reg[base + i] =
247 ralloc_array(set, uint16_t, HALF_CLASS_REGS(i));
248
249 for (unsigned j = 0; j < HALF_CLASS_REGS(i); j++) {
250 ra_class_add_reg(set->regs, set->half_classes[i], reg);
251
252 set->ra_reg_to_gpr[reg] = j;
253 set->gpr_to_ra_reg[base + i][j] = reg;
254
255 for (unsigned br = j; br < j + half_class_sizes[i]; br++)
256 ra_add_transitive_reg_conflict(set->regs, br + first_half_reg, reg);
257
258 reg++;
259 }
260 }
261
262 first_high_reg = reg;
263 base = HIGH_OFFSET;
264
265 for (unsigned i = 0; i < high_class_count; i++) {
266 set->high_classes[i] = ra_alloc_reg_class(set->regs);
267
268 set->gpr_to_ra_reg[base + i] =
269 ralloc_array(set, uint16_t, HIGH_CLASS_REGS(i));
270
271 for (unsigned j = 0; j < HIGH_CLASS_REGS(i); j++) {
272 ra_class_add_reg(set->regs, set->high_classes[i], reg);
273
274 set->ra_reg_to_gpr[reg] = j;
275 set->gpr_to_ra_reg[base + i][j] = reg;
276
277 for (unsigned br = j; br < j + high_class_sizes[i]; br++)
278 ra_add_transitive_reg_conflict(set->regs, br + first_high_reg, reg);
279
280 reg++;
281 }
282 }
283
284 /* starting a6xx, half precision regs conflict w/ full precision regs: */
285 if (compiler->gpu_id >= 600) {
286 /* because of transitivity, we can get away with just setting up
287 * conflicts between the first class of full and half regs:
288 */
289 for (unsigned i = 0; i < half_class_count; i++) {
290 /* NOTE there are fewer half class sizes, but they match the
291 * first N full class sizes.. but assert in case that ever
292 * accidentially changes:
293 */
294 debug_assert(class_sizes[i] == half_class_sizes[i]);
295 for (unsigned j = 0; j < CLASS_REGS(i) / 2; j++) {
296 unsigned freg = set->gpr_to_ra_reg[i][j];
297 unsigned hreg0 = set->gpr_to_ra_reg[i + HALF_OFFSET][(j * 2) + 0];
298 unsigned hreg1 = set->gpr_to_ra_reg[i + HALF_OFFSET][(j * 2) + 1];
299
300 ra_add_transitive_reg_conflict(set->regs, freg, hreg0);
301 ra_add_transitive_reg_conflict(set->regs, freg, hreg1);
302 }
303 }
304
305 // TODO also need to update q_values, but for now:
306 ra_set_finalize(set->regs, NULL);
307 } else {
308 ra_set_finalize(set->regs, q_values);
309 }
310
311 ralloc_free(q_values);
312
313 return set;
314 }
315
316 /* additional block-data (per-block) */
317 struct ir3_ra_block_data {
318 BITSET_WORD *def; /* variables defined before used in block */
319 BITSET_WORD *use; /* variables used before defined in block */
320 BITSET_WORD *livein; /* which defs reach entry point of block */
321 BITSET_WORD *liveout; /* which defs reach exit point of block */
322 };
323
324 /* additional instruction-data (per-instruction) */
325 struct ir3_ra_instr_data {
326 /* cached instruction 'definer' info: */
327 struct ir3_instruction *defn;
328 int off, sz, cls;
329 };
330
331 /* register-assign context, per-shader */
332 struct ir3_ra_ctx {
333 struct ir3_shader_variant *v;
334 struct ir3 *ir;
335
336 struct ir3_ra_reg_set *set;
337 struct ra_graph *g;
338 unsigned alloc_count;
339 /* one per class, plus one slot for arrays: */
340 unsigned class_alloc_count[total_class_count + 1];
341 unsigned class_base[total_class_count + 1];
342 unsigned instr_cnt;
343 unsigned *def, *use; /* def/use table */
344 struct ir3_ra_instr_data *instrd;
345 };
346
347 /* does it conflict? */
348 static inline bool
349 intersects(unsigned a_start, unsigned a_end, unsigned b_start, unsigned b_end)
350 {
351 return !((a_start >= b_end) || (b_start >= a_end));
352 }
353
354 static bool
355 is_half(struct ir3_instruction *instr)
356 {
357 return !!(instr->regs[0]->flags & IR3_REG_HALF);
358 }
359
360 static bool
361 is_high(struct ir3_instruction *instr)
362 {
363 return !!(instr->regs[0]->flags & IR3_REG_HIGH);
364 }
365
366 static int
367 size_to_class(unsigned sz, bool half, bool high)
368 {
369 if (high) {
370 for (unsigned i = 0; i < high_class_count; i++)
371 if (high_class_sizes[i] >= sz)
372 return i + HIGH_OFFSET;
373 } else if (half) {
374 for (unsigned i = 0; i < half_class_count; i++)
375 if (half_class_sizes[i] >= sz)
376 return i + HALF_OFFSET;
377 } else {
378 for (unsigned i = 0; i < class_count; i++)
379 if (class_sizes[i] >= sz)
380 return i;
381 }
382 debug_assert(0);
383 return -1;
384 }
385
386 static bool
387 writes_gpr(struct ir3_instruction *instr)
388 {
389 if (is_store(instr))
390 return false;
391 if (instr->regs_count == 0)
392 return false;
393 /* is dest a normal temp register: */
394 struct ir3_register *reg = instr->regs[0];
395 if (reg->flags & (IR3_REG_CONST | IR3_REG_IMMED))
396 return false;
397 if ((reg->num == regid(REG_A0, 0)) ||
398 (reg->num == regid(REG_P0, 0)))
399 return false;
400 return true;
401 }
402
403 static bool
404 instr_before(struct ir3_instruction *a, struct ir3_instruction *b)
405 {
406 if (a->flags & IR3_INSTR_UNUSED)
407 return false;
408 return (a->ip < b->ip);
409 }
410
411 static struct ir3_instruction *
412 get_definer(struct ir3_ra_ctx *ctx, struct ir3_instruction *instr,
413 int *sz, int *off)
414 {
415 struct ir3_ra_instr_data *id = &ctx->instrd[instr->ip];
416 struct ir3_instruction *d = NULL;
417
418 if (id->defn) {
419 *sz = id->sz;
420 *off = id->off;
421 return id->defn;
422 }
423
424 if (instr->opc == OPC_META_FI) {
425 /* What about the case where collect is subset of array, we
426 * need to find the distance between where actual array starts
427 * and fanin.. that probably doesn't happen currently.
428 */
429 struct ir3_register *src;
430 int dsz, doff;
431
432 /* note: don't use foreach_ssa_src as this gets called once
433 * while assigning regs (which clears SSA flag)
434 */
435 foreach_src_n(src, n, instr) {
436 struct ir3_instruction *dd;
437 if (!src->instr)
438 continue;
439
440 dd = get_definer(ctx, src->instr, &dsz, &doff);
441
442 if ((!d) || instr_before(dd, d)) {
443 d = dd;
444 *sz = dsz;
445 *off = doff - n;
446 }
447 }
448
449 } else if (instr->cp.right || instr->cp.left) {
450 /* covers also the meta:fo case, which ends up w/ single
451 * scalar instructions for each component:
452 */
453 struct ir3_instruction *f = ir3_neighbor_first(instr);
454
455 /* by definition, the entire sequence forms one linked list
456 * of single scalar register nodes (even if some of them may
457 * be fanouts from a texture sample (for example) instr. We
458 * just need to walk the list finding the first element of
459 * the group defined (lowest ip)
460 */
461 int cnt = 0;
462
463 /* need to skip over unused in the group: */
464 while (f && (f->flags & IR3_INSTR_UNUSED)) {
465 f = f->cp.right;
466 cnt++;
467 }
468
469 while (f) {
470 if ((!d) || instr_before(f, d))
471 d = f;
472 if (f == instr)
473 *off = cnt;
474 f = f->cp.right;
475 cnt++;
476 }
477
478 *sz = cnt;
479
480 } else {
481 /* second case is looking directly at the instruction which
482 * produces multiple values (eg, texture sample), rather
483 * than the fanout nodes that point back to that instruction.
484 * This isn't quite right, because it may be part of a larger
485 * group, such as:
486 *
487 * sam (f32)(xyzw)r0.x, ...
488 * add r1.x, ...
489 * add r1.y, ...
490 * sam (f32)(xyzw)r2.x, r0.w <-- (r0.w, r1.x, r1.y)
491 *
492 * need to come up with a better way to handle that case.
493 */
494 if (instr->address) {
495 *sz = instr->regs[0]->size;
496 } else {
497 *sz = util_last_bit(instr->regs[0]->wrmask);
498 }
499 *off = 0;
500 d = instr;
501 }
502
503 if (d->opc == OPC_META_FO) {
504 struct ir3_instruction *dd;
505 int dsz, doff;
506
507 dd = get_definer(ctx, d->regs[1]->instr, &dsz, &doff);
508
509 /* by definition, should come before: */
510 debug_assert(instr_before(dd, d));
511
512 *sz = MAX2(*sz, dsz);
513
514 if (instr->opc == OPC_META_FO)
515 *off = MAX2(*off, instr->fo.off);
516
517 d = dd;
518 }
519
520 debug_assert(d->opc != OPC_META_FO);
521
522 id->defn = d;
523 id->sz = *sz;
524 id->off = *off;
525
526 return d;
527 }
528
529 static void
530 ra_block_find_definers(struct ir3_ra_ctx *ctx, struct ir3_block *block)
531 {
532 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
533 struct ir3_ra_instr_data *id = &ctx->instrd[instr->ip];
534 if (instr->regs_count == 0)
535 continue;
536 /* couple special cases: */
537 if (writes_addr(instr) || writes_pred(instr)) {
538 id->cls = -1;
539 } else if (instr->regs[0]->flags & IR3_REG_ARRAY) {
540 id->cls = total_class_count;
541 } else {
542 /* and the normal case: */
543 id->defn = get_definer(ctx, instr, &id->sz, &id->off);
544 id->cls = size_to_class(id->sz, is_half(id->defn), is_high(id->defn));
545
546 /* this is a bit of duct-tape.. if we have a scenario like:
547 *
548 * sam (f32)(x) out.x, ...
549 * sam (f32)(x) out.y, ...
550 *
551 * Then the fanout/split meta instructions for the two different
552 * tex instructions end up grouped as left/right neighbors. The
553 * upshot is that in when you get_definer() on one of the meta:fo's
554 * you get definer as the first sam with sz=2, but when you call
555 * get_definer() on the either of the sam's you get itself as the
556 * definer with sz=1.
557 *
558 * (We actually avoid this scenario exactly, the neighbor links
559 * prevent one of the output mov's from being eliminated, so this
560 * hack should be enough. But probably we need to rethink how we
561 * find the "defining" instruction.)
562 *
563 * TODO how do we figure out offset properly...
564 */
565 if (id->defn != instr) {
566 struct ir3_ra_instr_data *did = &ctx->instrd[id->defn->ip];
567 if (did->sz < id->sz) {
568 did->sz = id->sz;
569 did->cls = id->cls;
570 }
571 }
572 }
573 }
574 }
575
576 /* give each instruction a name (and ip), and count up the # of names
577 * of each class
578 */
579 static void
580 ra_block_name_instructions(struct ir3_ra_ctx *ctx, struct ir3_block *block)
581 {
582 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
583 struct ir3_ra_instr_data *id = &ctx->instrd[instr->ip];
584
585 #ifdef DEBUG
586 instr->name = ~0;
587 #endif
588
589 ctx->instr_cnt++;
590
591 if (!writes_gpr(instr))
592 continue;
593
594 if (id->defn != instr)
595 continue;
596
597 /* arrays which don't fit in one of the pre-defined class
598 * sizes are pre-colored:
599 */
600 if ((id->cls >= 0) && (id->cls < total_class_count)) {
601 instr->name = ctx->class_alloc_count[id->cls]++;
602 ctx->alloc_count++;
603 }
604 }
605 }
606
607 static void
608 ra_init(struct ir3_ra_ctx *ctx)
609 {
610 unsigned n, base;
611
612 ir3_clear_mark(ctx->ir);
613 n = ir3_count_instructions(ctx->ir);
614
615 ctx->instrd = rzalloc_array(NULL, struct ir3_ra_instr_data, n);
616
617 list_for_each_entry (struct ir3_block, block, &ctx->ir->block_list, node) {
618 ra_block_find_definers(ctx, block);
619 }
620
621 list_for_each_entry (struct ir3_block, block, &ctx->ir->block_list, node) {
622 ra_block_name_instructions(ctx, block);
623 }
624
625 /* figure out the base register name for each class. The
626 * actual ra name is class_base[cls] + instr->name;
627 */
628 ctx->class_base[0] = 0;
629 for (unsigned i = 1; i <= total_class_count; i++) {
630 ctx->class_base[i] = ctx->class_base[i-1] +
631 ctx->class_alloc_count[i-1];
632 }
633
634 /* and vreg names for array elements: */
635 base = ctx->class_base[total_class_count];
636 list_for_each_entry (struct ir3_array, arr, &ctx->ir->array_list, node) {
637 arr->base = base;
638 ctx->class_alloc_count[total_class_count] += arr->length;
639 base += arr->length;
640 }
641 ctx->alloc_count += ctx->class_alloc_count[total_class_count];
642
643 ctx->g = ra_alloc_interference_graph(ctx->set->regs, ctx->alloc_count);
644 ralloc_steal(ctx->g, ctx->instrd);
645 ctx->def = rzalloc_array(ctx->g, unsigned, ctx->alloc_count);
646 ctx->use = rzalloc_array(ctx->g, unsigned, ctx->alloc_count);
647 }
648
649 static unsigned
650 __ra_name(struct ir3_ra_ctx *ctx, int cls, struct ir3_instruction *defn)
651 {
652 unsigned name;
653 debug_assert(cls >= 0);
654 debug_assert(cls < total_class_count); /* we shouldn't get arrays here.. */
655 name = ctx->class_base[cls] + defn->name;
656 debug_assert(name < ctx->alloc_count);
657 return name;
658 }
659
660 static int
661 ra_name(struct ir3_ra_ctx *ctx, struct ir3_ra_instr_data *id)
662 {
663 /* TODO handle name mapping for arrays */
664 return __ra_name(ctx, id->cls, id->defn);
665 }
666
667 static void
668 ra_destroy(struct ir3_ra_ctx *ctx)
669 {
670 ralloc_free(ctx->g);
671 }
672
673 static void
674 ra_block_compute_live_ranges(struct ir3_ra_ctx *ctx, struct ir3_block *block)
675 {
676 struct ir3_ra_block_data *bd;
677 unsigned bitset_words = BITSET_WORDS(ctx->alloc_count);
678
679 #define def(name, instr) \
680 do { \
681 /* defined on first write: */ \
682 if (!ctx->def[name]) \
683 ctx->def[name] = instr->ip; \
684 ctx->use[name] = instr->ip; \
685 BITSET_SET(bd->def, name); \
686 } while(0);
687
688 #define use(name, instr) \
689 do { \
690 ctx->use[name] = MAX2(ctx->use[name], instr->ip); \
691 if (!BITSET_TEST(bd->def, name)) \
692 BITSET_SET(bd->use, name); \
693 } while(0);
694
695 bd = rzalloc(ctx->g, struct ir3_ra_block_data);
696
697 bd->def = rzalloc_array(bd, BITSET_WORD, bitset_words);
698 bd->use = rzalloc_array(bd, BITSET_WORD, bitset_words);
699 bd->livein = rzalloc_array(bd, BITSET_WORD, bitset_words);
700 bd->liveout = rzalloc_array(bd, BITSET_WORD, bitset_words);
701
702 block->data = bd;
703
704 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
705 struct ir3_instruction *src;
706 struct ir3_register *reg;
707
708 /* There are a couple special cases to deal with here:
709 *
710 * fanout: used to split values from a higher class to a lower
711 * class, for example split the results of a texture fetch
712 * into individual scalar values; We skip over these from
713 * a 'def' perspective, and for a 'use' we walk the chain
714 * up to the defining instruction.
715 *
716 * fanin: used to collect values from lower class and assemble
717 * them together into a higher class, for example arguments
718 * to texture sample instructions; We consider these to be
719 * defined at the earliest fanin source.
720 *
721 * Most of this is handled in the get_definer() helper.
722 *
723 * In either case, we trace the instruction back to the original
724 * definer and consider that as the def/use ip.
725 */
726
727 if (writes_gpr(instr)) {
728 struct ir3_ra_instr_data *id = &ctx->instrd[instr->ip];
729 struct ir3_register *dst = instr->regs[0];
730
731 if (dst->flags & IR3_REG_ARRAY) {
732 struct ir3_array *arr =
733 ir3_lookup_array(ctx->ir, dst->array.id);
734 unsigned i;
735
736 arr->start_ip = MIN2(arr->start_ip, instr->ip);
737 arr->end_ip = MAX2(arr->end_ip, instr->ip);
738
739 /* set the node class now.. in case we don't encounter
740 * this array dst again. From register_alloc algo's
741 * perspective, these are all single/scalar regs:
742 */
743 for (i = 0; i < arr->length; i++) {
744 unsigned name = arr->base + i;
745 ra_set_node_class(ctx->g, name, ctx->set->classes[0]);
746 }
747
748 /* indirect write is treated like a write to all array
749 * elements, since we don't know which one is actually
750 * written:
751 */
752 if (dst->flags & IR3_REG_RELATIV) {
753 for (i = 0; i < arr->length; i++) {
754 unsigned name = arr->base + i;
755 def(name, instr);
756 }
757 } else {
758 unsigned name = arr->base + dst->array.offset;
759 def(name, instr);
760 }
761
762 } else if (id->defn == instr) {
763 unsigned name = ra_name(ctx, id);
764
765 /* since we are in SSA at this point: */
766 debug_assert(!BITSET_TEST(bd->use, name));
767
768 def(name, id->defn);
769
770 if (is_high(id->defn)) {
771 ra_set_node_class(ctx->g, name,
772 ctx->set->high_classes[id->cls - HIGH_OFFSET]);
773 } else if (is_half(id->defn)) {
774 ra_set_node_class(ctx->g, name,
775 ctx->set->half_classes[id->cls - HALF_OFFSET]);
776 } else {
777 ra_set_node_class(ctx->g, name,
778 ctx->set->classes[id->cls]);
779 }
780 }
781 }
782
783 foreach_src(reg, instr) {
784 if (reg->flags & IR3_REG_ARRAY) {
785 struct ir3_array *arr =
786 ir3_lookup_array(ctx->ir, reg->array.id);
787 arr->start_ip = MIN2(arr->start_ip, instr->ip);
788 arr->end_ip = MAX2(arr->end_ip, instr->ip);
789
790 /* indirect read is treated like a read fromall array
791 * elements, since we don't know which one is actually
792 * read:
793 */
794 if (reg->flags & IR3_REG_RELATIV) {
795 unsigned i;
796 for (i = 0; i < arr->length; i++) {
797 unsigned name = arr->base + i;
798 use(name, instr);
799 }
800 } else {
801 unsigned name = arr->base + reg->array.offset;
802 use(name, instr);
803 /* NOTE: arrays are not SSA so unconditionally
804 * set use bit:
805 */
806 BITSET_SET(bd->use, name);
807 debug_assert(reg->array.offset < arr->length);
808 }
809 } else if ((src = ssa(reg)) && writes_gpr(src)) {
810 unsigned name = ra_name(ctx, &ctx->instrd[src->ip]);
811 use(name, instr);
812 }
813 }
814 }
815 }
816
817 static bool
818 ra_compute_livein_liveout(struct ir3_ra_ctx *ctx)
819 {
820 unsigned bitset_words = BITSET_WORDS(ctx->alloc_count);
821 bool progress = false;
822
823 list_for_each_entry (struct ir3_block, block, &ctx->ir->block_list, node) {
824 struct ir3_ra_block_data *bd = block->data;
825
826 /* update livein: */
827 for (unsigned i = 0; i < bitset_words; i++) {
828 BITSET_WORD new_livein =
829 (bd->use[i] | (bd->liveout[i] & ~bd->def[i]));
830
831 if (new_livein & ~bd->livein[i]) {
832 bd->livein[i] |= new_livein;
833 progress = true;
834 }
835 }
836
837 /* update liveout: */
838 for (unsigned j = 0; j < ARRAY_SIZE(block->successors); j++) {
839 struct ir3_block *succ = block->successors[j];
840 struct ir3_ra_block_data *succ_bd;
841
842 if (!succ)
843 continue;
844
845 succ_bd = succ->data;
846
847 for (unsigned i = 0; i < bitset_words; i++) {
848 BITSET_WORD new_liveout =
849 (succ_bd->livein[i] & ~bd->liveout[i]);
850
851 if (new_liveout) {
852 bd->liveout[i] |= new_liveout;
853 progress = true;
854 }
855 }
856 }
857 }
858
859 return progress;
860 }
861
862 static void
863 print_bitset(const char *name, BITSET_WORD *bs, unsigned cnt)
864 {
865 bool first = true;
866 debug_printf(" %s:", name);
867 for (unsigned i = 0; i < cnt; i++) {
868 if (BITSET_TEST(bs, i)) {
869 if (!first)
870 debug_printf(",");
871 debug_printf(" %04u", i);
872 first = false;
873 }
874 }
875 debug_printf("\n");
876 }
877
878 static void
879 ra_add_interference(struct ir3_ra_ctx *ctx)
880 {
881 struct ir3 *ir = ctx->ir;
882
883 /* initialize array live ranges: */
884 list_for_each_entry (struct ir3_array, arr, &ir->array_list, node) {
885 arr->start_ip = ~0;
886 arr->end_ip = 0;
887 }
888
889 /* compute live ranges (use/def) on a block level, also updating
890 * block's def/use bitmasks (used below to calculate per-block
891 * livein/liveout):
892 */
893 list_for_each_entry (struct ir3_block, block, &ir->block_list, node) {
894 ra_block_compute_live_ranges(ctx, block);
895 }
896
897 /* update per-block livein/liveout: */
898 while (ra_compute_livein_liveout(ctx)) {}
899
900 if (ir3_shader_debug & IR3_DBG_OPTMSGS) {
901 debug_printf("AFTER LIVEIN/OUT:\n");
902 ir3_print(ir);
903 list_for_each_entry (struct ir3_block, block, &ir->block_list, node) {
904 struct ir3_ra_block_data *bd = block->data;
905 debug_printf("block%u:\n", block_id(block));
906 print_bitset(" def", bd->def, ctx->alloc_count);
907 print_bitset(" use", bd->use, ctx->alloc_count);
908 print_bitset(" l/i", bd->livein, ctx->alloc_count);
909 print_bitset(" l/o", bd->liveout, ctx->alloc_count);
910 }
911 list_for_each_entry (struct ir3_array, arr, &ir->array_list, node) {
912 debug_printf("array%u:\n", arr->id);
913 debug_printf(" length: %u\n", arr->length);
914 debug_printf(" start_ip: %u\n", arr->start_ip);
915 debug_printf(" end_ip: %u\n", arr->end_ip);
916 }
917 }
918
919 /* extend start/end ranges based on livein/liveout info from cfg: */
920 list_for_each_entry (struct ir3_block, block, &ir->block_list, node) {
921 struct ir3_ra_block_data *bd = block->data;
922
923 for (unsigned i = 0; i < ctx->alloc_count; i++) {
924 if (BITSET_TEST(bd->livein, i)) {
925 ctx->def[i] = MIN2(ctx->def[i], block->start_ip);
926 ctx->use[i] = MAX2(ctx->use[i], block->start_ip);
927 }
928
929 if (BITSET_TEST(bd->liveout, i)) {
930 ctx->def[i] = MIN2(ctx->def[i], block->end_ip);
931 ctx->use[i] = MAX2(ctx->use[i], block->end_ip);
932 }
933 }
934
935 list_for_each_entry (struct ir3_array, arr, &ctx->ir->array_list, node) {
936 for (unsigned i = 0; i < arr->length; i++) {
937 if (BITSET_TEST(bd->livein, i + arr->base)) {
938 arr->start_ip = MIN2(arr->start_ip, block->start_ip);
939 }
940 if (BITSET_TEST(bd->livein, i + arr->base)) {
941 arr->end_ip = MAX2(arr->end_ip, block->end_ip);
942 }
943 }
944 }
945 }
946
947 /* need to fix things up to keep outputs live: */
948 for (unsigned i = 0; i < ir->noutputs; i++) {
949 struct ir3_instruction *instr = ir->outputs[i];
950 if (!instr)
951 continue;
952 unsigned name = ra_name(ctx, &ctx->instrd[instr->ip]);
953 ctx->use[name] = ctx->instr_cnt;
954 }
955
956 for (unsigned i = 0; i < ctx->alloc_count; i++) {
957 for (unsigned j = 0; j < ctx->alloc_count; j++) {
958 if (intersects(ctx->def[i], ctx->use[i],
959 ctx->def[j], ctx->use[j])) {
960 ra_add_node_interference(ctx->g, i, j);
961 }
962 }
963 }
964 }
965
966 /* some instructions need fix-up if dst register is half precision: */
967 static void fixup_half_instr_dst(struct ir3_instruction *instr)
968 {
969 switch (opc_cat(instr->opc)) {
970 case 1: /* move instructions */
971 instr->cat1.dst_type = half_type(instr->cat1.dst_type);
972 break;
973 case 3:
974 switch (instr->opc) {
975 case OPC_MAD_F32:
976 instr->opc = OPC_MAD_F16;
977 break;
978 case OPC_SEL_B32:
979 instr->opc = OPC_SEL_B16;
980 break;
981 case OPC_SEL_S32:
982 instr->opc = OPC_SEL_S16;
983 break;
984 case OPC_SEL_F32:
985 instr->opc = OPC_SEL_F16;
986 break;
987 case OPC_SAD_S32:
988 instr->opc = OPC_SAD_S16;
989 break;
990 /* instructions may already be fixed up: */
991 case OPC_MAD_F16:
992 case OPC_SEL_B16:
993 case OPC_SEL_S16:
994 case OPC_SEL_F16:
995 case OPC_SAD_S16:
996 break;
997 default:
998 assert(0);
999 break;
1000 }
1001 break;
1002 case 5:
1003 instr->cat5.type = half_type(instr->cat5.type);
1004 break;
1005 }
1006 }
1007 /* some instructions need fix-up if src register is half precision: */
1008 static void fixup_half_instr_src(struct ir3_instruction *instr)
1009 {
1010 switch (instr->opc) {
1011 case OPC_MOV:
1012 instr->cat1.src_type = half_type(instr->cat1.src_type);
1013 break;
1014 default:
1015 break;
1016 }
1017 }
1018
1019 /* NOTE: instr could be NULL for IR3_REG_ARRAY case, for the first
1020 * array access(es) which do not have any previous access to depend
1021 * on from scheduling point of view
1022 */
1023 static void
1024 reg_assign(struct ir3_ra_ctx *ctx, struct ir3_register *reg,
1025 struct ir3_instruction *instr)
1026 {
1027 struct ir3_ra_instr_data *id;
1028
1029 if (reg->flags & IR3_REG_ARRAY) {
1030 struct ir3_array *arr =
1031 ir3_lookup_array(ctx->ir, reg->array.id);
1032 unsigned name = arr->base + reg->array.offset;
1033 unsigned r = ra_get_node_reg(ctx->g, name);
1034 unsigned num = ctx->set->ra_reg_to_gpr[r];
1035
1036 if (reg->flags & IR3_REG_RELATIV) {
1037 reg->array.offset = num;
1038 } else {
1039 reg->num = num;
1040 reg->flags &= ~IR3_REG_SSA;
1041 }
1042
1043 reg->flags &= ~IR3_REG_ARRAY;
1044 } else if ((id = &ctx->instrd[instr->ip]) && id->defn) {
1045 unsigned name = ra_name(ctx, id);
1046 unsigned r = ra_get_node_reg(ctx->g, name);
1047 unsigned num = ctx->set->ra_reg_to_gpr[r] + id->off;
1048
1049 debug_assert(!(reg->flags & IR3_REG_RELATIV));
1050
1051 if (is_high(id->defn))
1052 num += FIRST_HIGH_REG;
1053
1054 reg->num = num;
1055 reg->flags &= ~IR3_REG_SSA;
1056
1057 if (is_half(id->defn))
1058 reg->flags |= IR3_REG_HALF;
1059 }
1060 }
1061
1062 static void
1063 ra_block_alloc(struct ir3_ra_ctx *ctx, struct ir3_block *block)
1064 {
1065 list_for_each_entry (struct ir3_instruction, instr, &block->instr_list, node) {
1066 struct ir3_register *reg;
1067
1068 if (writes_gpr(instr)) {
1069 reg_assign(ctx, instr->regs[0], instr);
1070 if (instr->regs[0]->flags & IR3_REG_HALF)
1071 fixup_half_instr_dst(instr);
1072 }
1073
1074 foreach_src_n(reg, n, instr) {
1075 struct ir3_instruction *src = reg->instr;
1076 /* Note: reg->instr could be null for IR3_REG_ARRAY */
1077 if (src || (reg->flags & IR3_REG_ARRAY))
1078 reg_assign(ctx, instr->regs[n+1], src);
1079 if (instr->regs[n+1]->flags & IR3_REG_HALF)
1080 fixup_half_instr_src(instr);
1081 }
1082 }
1083 }
1084
1085 /* handle pre-colored registers. This includes "arrays" (which could be of
1086 * length 1, used for phi webs lowered to registers in nir), as well as
1087 * special shader input values that need to be pinned to certain registers.
1088 */
1089 static void
1090 ra_precolor(struct ir3_ra_ctx *ctx, struct ir3_instruction **precolor, unsigned nprecolor)
1091 {
1092 unsigned num_precolor = 0;
1093 for (unsigned i = 0; i < nprecolor; i++) {
1094 if (precolor[i] && !(precolor[i]->flags & IR3_INSTR_UNUSED)) {
1095 struct ir3_instruction *instr = precolor[i];
1096 struct ir3_ra_instr_data *id = &ctx->instrd[instr->ip];
1097
1098 debug_assert(!(instr->regs[0]->flags & (IR3_REG_HALF | IR3_REG_HIGH)));
1099
1100 /* only consider the first component: */
1101 if (id->off > 0)
1102 continue;
1103
1104 /* 'base' is in scalar (class 0) but we need to map that
1105 * the conflicting register of the appropriate class (ie.
1106 * input could be vec2/vec3/etc)
1107 *
1108 * Note that the higher class (larger than scalar) regs
1109 * are setup to conflict with others in the same class,
1110 * so for example, R1 (scalar) is also the first component
1111 * of D1 (vec2/double):
1112 *
1113 * Single (base) | Double
1114 * --------------+---------------
1115 * R0 | D0
1116 * R1 | D0 D1
1117 * R2 | D1 D2
1118 * R3 | D2
1119 * .. and so on..
1120 */
1121 unsigned regid = instr->regs[0]->num;
1122 unsigned reg = ctx->set->gpr_to_ra_reg[id->cls][regid];
1123 unsigned name = ra_name(ctx, id);
1124 ra_set_node_reg(ctx->g, name, reg);
1125 num_precolor = MAX2(regid, num_precolor);
1126 }
1127 }
1128
1129 /* pre-assign array elements:
1130 */
1131 list_for_each_entry (struct ir3_array, arr, &ctx->ir->array_list, node) {
1132 unsigned base = 0;
1133
1134 if (arr->end_ip == 0)
1135 continue;
1136
1137 /* figure out what else we conflict with which has already
1138 * been assigned:
1139 */
1140 retry:
1141 list_for_each_entry (struct ir3_array, arr2, &ctx->ir->array_list, node) {
1142 if (arr2 == arr)
1143 break;
1144 if (arr2->end_ip == 0)
1145 continue;
1146 /* if it intersects with liverange AND register range.. */
1147 if (intersects(arr->start_ip, arr->end_ip,
1148 arr2->start_ip, arr2->end_ip) &&
1149 intersects(base, base + arr->length,
1150 arr2->reg, arr2->reg + arr2->length)) {
1151 base = MAX2(base, arr2->reg + arr2->length);
1152 goto retry;
1153 }
1154 }
1155
1156 /* also need to not conflict with any pre-assigned inputs: */
1157 for (unsigned i = 0; i < nprecolor; i++) {
1158 struct ir3_instruction *instr = precolor[i];
1159
1160 if (!instr)
1161 continue;
1162
1163 struct ir3_ra_instr_data *id = &ctx->instrd[instr->ip];
1164
1165 /* only consider the first component: */
1166 if (id->off > 0)
1167 continue;
1168
1169 unsigned name = ra_name(ctx, id);
1170 unsigned regid = instr->regs[0]->num;
1171
1172 /* Check if array intersects with liverange AND register
1173 * range of the input:
1174 */
1175 if (intersects(arr->start_ip, arr->end_ip,
1176 ctx->def[name], ctx->use[name]) &&
1177 intersects(base, base + arr->length,
1178 regid, regid + class_sizes[id->cls])) {
1179 base = MAX2(base, regid + class_sizes[id->cls]);
1180 goto retry;
1181 }
1182 }
1183
1184 arr->reg = base;
1185
1186 for (unsigned i = 0; i < arr->length; i++) {
1187 unsigned name, reg;
1188
1189 name = arr->base + i;
1190 reg = ctx->set->gpr_to_ra_reg[0][base++];
1191
1192 ra_set_node_reg(ctx->g, name, reg);
1193 }
1194 }
1195 }
1196
1197 static int
1198 ra_alloc(struct ir3_ra_ctx *ctx)
1199 {
1200 if (!ra_allocate(ctx->g))
1201 return -1;
1202
1203 list_for_each_entry (struct ir3_block, block, &ctx->ir->block_list, node) {
1204 ra_block_alloc(ctx, block);
1205 }
1206
1207 return 0;
1208 }
1209
1210 int ir3_ra(struct ir3_shader_variant *v, struct ir3_instruction **precolor, unsigned nprecolor)
1211 {
1212 struct ir3_ra_ctx ctx = {
1213 .v = v,
1214 .ir = v->ir,
1215 .set = v->ir->compiler->set,
1216 };
1217 int ret;
1218
1219 ra_init(&ctx);
1220 ra_add_interference(&ctx);
1221 ra_precolor(&ctx, precolor, nprecolor);
1222 ret = ra_alloc(&ctx);
1223 ra_destroy(&ctx);
1224
1225 return ret;
1226 }