2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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:
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
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
24 * Rob Clark <robclark@freedesktop.org>
27 #include "util/u_math.h"
28 #include "util/register_allocate.h"
29 #include "util/ralloc.h"
30 #include "util/bitset.h"
33 #include "ir3_compiler.h"
36 * Register Assignment:
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.
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.
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:
56 * sam (f32)(xy)r0.x, ...
58 * sam (f32)(xyzw)r0.w, r0.x, ... ; 3d texture, so r0.xyz are coord
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.
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.)
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.)
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.)
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()
92 static const unsigned class_sizes
[] = {
94 4 + 4, /* txd + 1d/2d */
97 #define class_count ARRAY_SIZE(class_sizes)
99 static const unsigned half_class_sizes
[] = {
102 #define half_class_count ARRAY_SIZE(half_class_sizes)
104 /* seems to just be used for compute shaders? Seems like vec1 and vec3
105 * are sufficient (for now?)
107 static const unsigned high_class_sizes
[] = {
110 #define high_class_count ARRAY_SIZE(high_class_sizes)
112 #define total_class_count (class_count + half_class_count + high_class_count)
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))
123 #define HALF_OFFSET (class_count)
124 #define HIGH_OFFSET (class_count + half_class_count)
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
;
139 build_q_values(unsigned int **q_values
, unsigned off
,
140 const unsigned *sizes
, unsigned count
)
142 for (unsigned i
= 0; i
< count
; i
++) {
143 q_values
[i
+ off
] = rzalloc_array(q_values
, unsigned, total_class_count
);
145 /* From register_allocate.c:
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".
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.
161 * +-+-+-+-+-+-+ +-+-+-+-+-+-+
162 * B | | | | | |n| --> | | | | | | |
163 * +-+-+-+-+-+-+ +-+-+-+-+-+-+
168 * (Idea copied from brw_fs_reg_allocate.cpp)
170 for (unsigned j
= 0; j
< count
; j
++)
171 q_values
[i
+ off
][j
+ off
] = sizes
[i
] + sizes
[j
] - 1;
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:
181 * Single (base) | Double
182 * --------------+---------------
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.)
192 struct ir3_ra_reg_set
*
193 ir3_ra_alloc_reg_set(struct ir3_compiler
*compiler
)
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
;
199 /* calculate # of regs across all classes: */
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
);
208 /* allocate and populate q_values: */
209 q_values
= ralloc_array(set
, unsigned *, total_class_count
);
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
);
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
);
222 for (unsigned i
= 0; i
< class_count
; i
++) {
223 set
->classes
[i
] = ra_alloc_reg_class(set
->regs
);
225 set
->gpr_to_ra_reg
[i
] = ralloc_array(set
, uint16_t, CLASS_REGS(i
));
227 for (unsigned j
= 0; j
< CLASS_REGS(i
); j
++) {
228 ra_class_add_reg(set
->regs
, set
->classes
[i
], reg
);
230 set
->ra_reg_to_gpr
[reg
] = j
;
231 set
->gpr_to_ra_reg
[i
][j
] = reg
;
233 for (unsigned br
= j
; br
< j
+ class_sizes
[i
]; br
++)
234 ra_add_transitive_reg_conflict(set
->regs
, br
, reg
);
240 first_half_reg
= reg
;
243 for (unsigned i
= 0; i
< half_class_count
; i
++) {
244 set
->half_classes
[i
] = ra_alloc_reg_class(set
->regs
);
246 set
->gpr_to_ra_reg
[base
+ i
] =
247 ralloc_array(set
, uint16_t, HALF_CLASS_REGS(i
));
249 for (unsigned j
= 0; j
< HALF_CLASS_REGS(i
); j
++) {
250 ra_class_add_reg(set
->regs
, set
->half_classes
[i
], reg
);
252 set
->ra_reg_to_gpr
[reg
] = j
;
253 set
->gpr_to_ra_reg
[base
+ i
][j
] = reg
;
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
);
262 first_high_reg
= reg
;
265 for (unsigned i
= 0; i
< high_class_count
; i
++) {
266 set
->high_classes
[i
] = ra_alloc_reg_class(set
->regs
);
268 set
->gpr_to_ra_reg
[base
+ i
] =
269 ralloc_array(set
, uint16_t, HIGH_CLASS_REGS(i
));
271 for (unsigned j
= 0; j
< HIGH_CLASS_REGS(i
); j
++) {
272 ra_class_add_reg(set
->regs
, set
->high_classes
[i
], reg
);
274 set
->ra_reg_to_gpr
[reg
] = j
;
275 set
->gpr_to_ra_reg
[base
+ i
][j
] = reg
;
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
);
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:
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:
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];
300 ra_add_transitive_reg_conflict(set
->regs
, freg
, hreg0
);
301 ra_add_transitive_reg_conflict(set
->regs
, freg
, hreg1
);
305 // TODO also need to update q_values, but for now:
306 ra_set_finalize(set
->regs
, NULL
);
308 ra_set_finalize(set
->regs
, q_values
);
311 ralloc_free(q_values
);
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 */
324 /* additional instruction-data (per-instruction) */
325 struct ir3_ra_instr_data
{
326 /* cached instruction 'definer' info: */
327 struct ir3_instruction
*defn
;
331 /* register-assign context, per-shader */
335 struct ir3_ra_reg_set
*set
;
337 unsigned alloc_count
;
338 /* one per class, plus one slot for arrays: */
339 unsigned class_alloc_count
[total_class_count
+ 1];
340 unsigned class_base
[total_class_count
+ 1];
342 unsigned *def
, *use
; /* def/use table */
343 struct ir3_ra_instr_data
*instrd
;
346 /* does it conflict? */
348 intersects(unsigned a_start
, unsigned a_end
, unsigned b_start
, unsigned b_end
)
350 return !((a_start
>= b_end
) || (b_start
>= a_end
));
354 is_half(struct ir3_instruction
*instr
)
356 return !!(instr
->regs
[0]->flags
& IR3_REG_HALF
);
360 is_high(struct ir3_instruction
*instr
)
362 return !!(instr
->regs
[0]->flags
& IR3_REG_HIGH
);
366 size_to_class(unsigned sz
, bool half
, bool high
)
369 for (unsigned i
= 0; i
< high_class_count
; i
++)
370 if (high_class_sizes
[i
] >= sz
)
371 return i
+ HIGH_OFFSET
;
373 for (unsigned i
= 0; i
< half_class_count
; i
++)
374 if (half_class_sizes
[i
] >= sz
)
375 return i
+ HALF_OFFSET
;
377 for (unsigned i
= 0; i
< class_count
; i
++)
378 if (class_sizes
[i
] >= sz
)
386 writes_gpr(struct ir3_instruction
*instr
)
390 /* is dest a normal temp register: */
391 struct ir3_register
*reg
= instr
->regs
[0];
392 if (reg
->flags
& (IR3_REG_CONST
| IR3_REG_IMMED
))
394 if ((reg
->num
== regid(REG_A0
, 0)) ||
395 (reg
->num
== regid(REG_P0
, 0)))
401 instr_before(struct ir3_instruction
*a
, struct ir3_instruction
*b
)
403 if (a
->flags
& IR3_INSTR_UNUSED
)
405 return (a
->ip
< b
->ip
);
408 static struct ir3_instruction
*
409 get_definer(struct ir3_ra_ctx
*ctx
, struct ir3_instruction
*instr
,
412 struct ir3_ra_instr_data
*id
= &ctx
->instrd
[instr
->ip
];
413 struct ir3_instruction
*d
= NULL
;
421 if (instr
->opc
== OPC_META_FI
) {
422 /* What about the case where collect is subset of array, we
423 * need to find the distance between where actual array starts
424 * and fanin.. that probably doesn't happen currently.
426 struct ir3_register
*src
;
429 /* note: don't use foreach_ssa_src as this gets called once
430 * while assigning regs (which clears SSA flag)
432 foreach_src_n(src
, n
, instr
) {
433 struct ir3_instruction
*dd
;
437 dd
= get_definer(ctx
, src
->instr
, &dsz
, &doff
);
439 if ((!d
) || instr_before(dd
, d
)) {
446 } else if (instr
->cp
.right
|| instr
->cp
.left
) {
447 /* covers also the meta:fo case, which ends up w/ single
448 * scalar instructions for each component:
450 struct ir3_instruction
*f
= ir3_neighbor_first(instr
);
452 /* by definition, the entire sequence forms one linked list
453 * of single scalar register nodes (even if some of them may
454 * be fanouts from a texture sample (for example) instr. We
455 * just need to walk the list finding the first element of
456 * the group defined (lowest ip)
460 /* need to skip over unused in the group: */
461 while (f
&& (f
->flags
& IR3_INSTR_UNUSED
)) {
467 if ((!d
) || instr_before(f
, d
))
478 /* second case is looking directly at the instruction which
479 * produces multiple values (eg, texture sample), rather
480 * than the fanout nodes that point back to that instruction.
481 * This isn't quite right, because it may be part of a larger
484 * sam (f32)(xyzw)r0.x, ...
487 * sam (f32)(xyzw)r2.x, r0.w <-- (r0.w, r1.x, r1.y)
489 * need to come up with a better way to handle that case.
491 if (instr
->address
) {
492 *sz
= instr
->regs
[0]->size
;
494 *sz
= util_last_bit(instr
->regs
[0]->wrmask
);
500 if (d
->opc
== OPC_META_FO
) {
501 struct ir3_instruction
*dd
;
504 dd
= get_definer(ctx
, d
->regs
[1]->instr
, &dsz
, &doff
);
506 /* by definition, should come before: */
507 debug_assert(instr_before(dd
, d
));
509 *sz
= MAX2(*sz
, dsz
);
511 if (instr
->opc
== OPC_META_FO
)
512 *off
= MAX2(*off
, instr
->fo
.off
);
517 debug_assert(d
->opc
!= OPC_META_FO
);
527 ra_block_find_definers(struct ir3_ra_ctx
*ctx
, struct ir3_block
*block
)
529 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
530 struct ir3_ra_instr_data
*id
= &ctx
->instrd
[instr
->ip
];
531 if (instr
->regs_count
== 0)
533 /* couple special cases: */
534 if (writes_addr(instr
) || writes_pred(instr
)) {
536 } else if (instr
->regs
[0]->flags
& IR3_REG_ARRAY
) {
537 id
->cls
= total_class_count
;
539 /* and the normal case: */
540 id
->defn
= get_definer(ctx
, instr
, &id
->sz
, &id
->off
);
541 id
->cls
= size_to_class(id
->sz
, is_half(id
->defn
), is_high(id
->defn
));
543 /* this is a bit of duct-tape.. if we have a scenario like:
545 * sam (f32)(x) out.x, ...
546 * sam (f32)(x) out.y, ...
548 * Then the fanout/split meta instructions for the two different
549 * tex instructions end up grouped as left/right neighbors. The
550 * upshot is that in when you get_definer() on one of the meta:fo's
551 * you get definer as the first sam with sz=2, but when you call
552 * get_definer() on the either of the sam's you get itself as the
555 * (We actually avoid this scenario exactly, the neighbor links
556 * prevent one of the output mov's from being eliminated, so this
557 * hack should be enough. But probably we need to rethink how we
558 * find the "defining" instruction.)
560 * TODO how do we figure out offset properly...
562 if (id
->defn
!= instr
) {
563 struct ir3_ra_instr_data
*did
= &ctx
->instrd
[id
->defn
->ip
];
564 if (did
->sz
< id
->sz
) {
573 /* give each instruction a name (and ip), and count up the # of names
577 ra_block_name_instructions(struct ir3_ra_ctx
*ctx
, struct ir3_block
*block
)
579 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
580 struct ir3_ra_instr_data
*id
= &ctx
->instrd
[instr
->ip
];
588 if (instr
->regs_count
== 0)
591 if (!writes_gpr(instr
))
594 if (id
->defn
!= instr
)
597 /* arrays which don't fit in one of the pre-defined class
598 * sizes are pre-colored:
600 if ((id
->cls
>= 0) && (id
->cls
< total_class_count
)) {
601 instr
->name
= ctx
->class_alloc_count
[id
->cls
]++;
608 ra_init(struct ir3_ra_ctx
*ctx
)
612 ir3_clear_mark(ctx
->ir
);
613 n
= ir3_count_instructions(ctx
->ir
);
615 ctx
->instrd
= rzalloc_array(NULL
, struct ir3_ra_instr_data
, n
);
617 list_for_each_entry (struct ir3_block
, block
, &ctx
->ir
->block_list
, node
) {
618 ra_block_find_definers(ctx
, block
);
621 list_for_each_entry (struct ir3_block
, block
, &ctx
->ir
->block_list
, node
) {
622 ra_block_name_instructions(ctx
, block
);
625 /* figure out the base register name for each class. The
626 * actual ra name is class_base[cls] + instr->name;
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];
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
) {
638 ctx
->class_alloc_count
[total_class_count
] += arr
->length
;
641 ctx
->alloc_count
+= ctx
->class_alloc_count
[total_class_count
];
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
);
650 __ra_name(struct ir3_ra_ctx
*ctx
, int cls
, struct ir3_instruction
*defn
)
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
);
661 ra_name(struct ir3_ra_ctx
*ctx
, struct ir3_ra_instr_data
*id
)
663 /* TODO handle name mapping for arrays */
664 return __ra_name(ctx
, id
->cls
, id
->defn
);
668 ra_destroy(struct ir3_ra_ctx
*ctx
)
674 ra_block_compute_live_ranges(struct ir3_ra_ctx
*ctx
, struct ir3_block
*block
)
676 struct ir3_ra_block_data
*bd
;
677 unsigned bitset_words
= BITSET_WORDS(ctx
->alloc_count
);
679 #define def(name, instr) \
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); \
688 #define use(name, instr) \
690 ctx->use[name] = MAX2(ctx->use[name], instr->ip); \
691 if (!BITSET_TEST(bd->def, name)) \
692 BITSET_SET(bd->use, name); \
695 bd
= rzalloc(ctx
->g
, struct ir3_ra_block_data
);
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
);
704 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
705 struct ir3_instruction
*src
;
706 struct ir3_register
*reg
;
708 if (instr
->regs_count
== 0)
711 /* There are a couple special cases to deal with here:
713 * fanout: used to split values from a higher class to a lower
714 * class, for example split the results of a texture fetch
715 * into individual scalar values; We skip over these from
716 * a 'def' perspective, and for a 'use' we walk the chain
717 * up to the defining instruction.
719 * fanin: used to collect values from lower class and assemble
720 * them together into a higher class, for example arguments
721 * to texture sample instructions; We consider these to be
722 * defined at the earliest fanin source.
724 * Most of this is handled in the get_definer() helper.
726 * In either case, we trace the instruction back to the original
727 * definer and consider that as the def/use ip.
730 if (writes_gpr(instr
)) {
731 struct ir3_ra_instr_data
*id
= &ctx
->instrd
[instr
->ip
];
732 struct ir3_register
*dst
= instr
->regs
[0];
734 if (dst
->flags
& IR3_REG_ARRAY
) {
735 struct ir3_array
*arr
=
736 ir3_lookup_array(ctx
->ir
, dst
->array
.id
);
739 arr
->start_ip
= MIN2(arr
->start_ip
, instr
->ip
);
740 arr
->end_ip
= MAX2(arr
->end_ip
, instr
->ip
);
742 /* set the node class now.. in case we don't encounter
743 * this array dst again. From register_alloc algo's
744 * perspective, these are all single/scalar regs:
746 for (i
= 0; i
< arr
->length
; i
++) {
747 unsigned name
= arr
->base
+ i
;
748 ra_set_node_class(ctx
->g
, name
, ctx
->set
->classes
[0]);
751 /* indirect write is treated like a write to all array
752 * elements, since we don't know which one is actually
755 if (dst
->flags
& IR3_REG_RELATIV
) {
756 for (i
= 0; i
< arr
->length
; i
++) {
757 unsigned name
= arr
->base
+ i
;
761 unsigned name
= arr
->base
+ dst
->array
.offset
;
765 } else if (id
->defn
== instr
) {
766 unsigned name
= ra_name(ctx
, id
);
768 /* since we are in SSA at this point: */
769 debug_assert(!BITSET_TEST(bd
->use
, name
));
773 if (is_high(id
->defn
)) {
774 ra_set_node_class(ctx
->g
, name
,
775 ctx
->set
->high_classes
[id
->cls
- HIGH_OFFSET
]);
776 } else if (is_half(id
->defn
)) {
777 ra_set_node_class(ctx
->g
, name
,
778 ctx
->set
->half_classes
[id
->cls
- HALF_OFFSET
]);
780 ra_set_node_class(ctx
->g
, name
,
781 ctx
->set
->classes
[id
->cls
]);
786 foreach_src(reg
, instr
) {
787 if (reg
->flags
& IR3_REG_ARRAY
) {
788 struct ir3_array
*arr
=
789 ir3_lookup_array(ctx
->ir
, reg
->array
.id
);
790 arr
->start_ip
= MIN2(arr
->start_ip
, instr
->ip
);
791 arr
->end_ip
= MAX2(arr
->end_ip
, instr
->ip
);
793 /* indirect read is treated like a read fromall array
794 * elements, since we don't know which one is actually
797 if (reg
->flags
& IR3_REG_RELATIV
) {
799 for (i
= 0; i
< arr
->length
; i
++) {
800 unsigned name
= arr
->base
+ i
;
804 unsigned name
= arr
->base
+ reg
->array
.offset
;
806 /* NOTE: arrays are not SSA so unconditionally
809 BITSET_SET(bd
->use
, name
);
810 debug_assert(reg
->array
.offset
< arr
->length
);
812 } else if ((src
= ssa(reg
)) && writes_gpr(src
)) {
813 unsigned name
= ra_name(ctx
, &ctx
->instrd
[src
->ip
]);
821 ra_compute_livein_liveout(struct ir3_ra_ctx
*ctx
)
823 unsigned bitset_words
= BITSET_WORDS(ctx
->alloc_count
);
824 bool progress
= false;
826 list_for_each_entry (struct ir3_block
, block
, &ctx
->ir
->block_list
, node
) {
827 struct ir3_ra_block_data
*bd
= block
->data
;
830 for (unsigned i
= 0; i
< bitset_words
; i
++) {
831 BITSET_WORD new_livein
=
832 (bd
->use
[i
] | (bd
->liveout
[i
] & ~bd
->def
[i
]));
834 if (new_livein
& ~bd
->livein
[i
]) {
835 bd
->livein
[i
] |= new_livein
;
840 /* update liveout: */
841 for (unsigned j
= 0; j
< ARRAY_SIZE(block
->successors
); j
++) {
842 struct ir3_block
*succ
= block
->successors
[j
];
843 struct ir3_ra_block_data
*succ_bd
;
848 succ_bd
= succ
->data
;
850 for (unsigned i
= 0; i
< bitset_words
; i
++) {
851 BITSET_WORD new_liveout
=
852 (succ_bd
->livein
[i
] & ~bd
->liveout
[i
]);
855 bd
->liveout
[i
] |= new_liveout
;
866 print_bitset(const char *name
, BITSET_WORD
*bs
, unsigned cnt
)
869 debug_printf(" %s:", name
);
870 for (unsigned i
= 0; i
< cnt
; i
++) {
871 if (BITSET_TEST(bs
, i
)) {
874 debug_printf(" %04u", i
);
882 ra_add_interference(struct ir3_ra_ctx
*ctx
)
884 struct ir3
*ir
= ctx
->ir
;
886 /* initialize array live ranges: */
887 list_for_each_entry (struct ir3_array
, arr
, &ir
->array_list
, node
) {
892 /* compute live ranges (use/def) on a block level, also updating
893 * block's def/use bitmasks (used below to calculate per-block
896 list_for_each_entry (struct ir3_block
, block
, &ir
->block_list
, node
) {
897 ra_block_compute_live_ranges(ctx
, block
);
900 /* update per-block livein/liveout: */
901 while (ra_compute_livein_liveout(ctx
)) {}
903 if (ir3_shader_debug
& IR3_DBG_OPTMSGS
) {
904 debug_printf("AFTER LIVEIN/OUT:\n");
906 list_for_each_entry (struct ir3_block
, block
, &ir
->block_list
, node
) {
907 struct ir3_ra_block_data
*bd
= block
->data
;
908 debug_printf("block%u:\n", block_id(block
));
909 print_bitset(" def", bd
->def
, ctx
->alloc_count
);
910 print_bitset(" use", bd
->use
, ctx
->alloc_count
);
911 print_bitset(" l/i", bd
->livein
, ctx
->alloc_count
);
912 print_bitset(" l/o", bd
->liveout
, ctx
->alloc_count
);
914 list_for_each_entry (struct ir3_array
, arr
, &ir
->array_list
, node
) {
915 debug_printf("array%u:\n", arr
->id
);
916 debug_printf(" length: %u\n", arr
->length
);
917 debug_printf(" start_ip: %u\n", arr
->start_ip
);
918 debug_printf(" end_ip: %u\n", arr
->end_ip
);
922 /* extend start/end ranges based on livein/liveout info from cfg: */
923 list_for_each_entry (struct ir3_block
, block
, &ir
->block_list
, node
) {
924 struct ir3_ra_block_data
*bd
= block
->data
;
926 for (unsigned i
= 0; i
< ctx
->alloc_count
; i
++) {
927 if (BITSET_TEST(bd
->livein
, i
)) {
928 ctx
->def
[i
] = MIN2(ctx
->def
[i
], block
->start_ip
);
929 ctx
->use
[i
] = MAX2(ctx
->use
[i
], block
->start_ip
);
932 if (BITSET_TEST(bd
->liveout
, i
)) {
933 ctx
->def
[i
] = MIN2(ctx
->def
[i
], block
->end_ip
);
934 ctx
->use
[i
] = MAX2(ctx
->use
[i
], block
->end_ip
);
938 list_for_each_entry (struct ir3_array
, arr
, &ctx
->ir
->array_list
, node
) {
939 for (unsigned i
= 0; i
< arr
->length
; i
++) {
940 if (BITSET_TEST(bd
->livein
, i
+ arr
->base
)) {
941 arr
->start_ip
= MIN2(arr
->start_ip
, block
->start_ip
);
943 if (BITSET_TEST(bd
->livein
, i
+ arr
->base
)) {
944 arr
->end_ip
= MAX2(arr
->end_ip
, block
->end_ip
);
950 /* need to fix things up to keep outputs live: */
951 for (unsigned i
= 0; i
< ir
->noutputs
; i
++) {
952 struct ir3_instruction
*instr
= ir
->outputs
[i
];
955 unsigned name
= ra_name(ctx
, &ctx
->instrd
[instr
->ip
]);
956 ctx
->use
[name
] = ctx
->instr_cnt
;
959 for (unsigned i
= 0; i
< ctx
->alloc_count
; i
++) {
960 for (unsigned j
= 0; j
< ctx
->alloc_count
; j
++) {
961 if (intersects(ctx
->def
[i
], ctx
->use
[i
],
962 ctx
->def
[j
], ctx
->use
[j
])) {
963 ra_add_node_interference(ctx
->g
, i
, j
);
969 /* some instructions need fix-up if dst register is half precision: */
970 static void fixup_half_instr_dst(struct ir3_instruction
*instr
)
972 switch (opc_cat(instr
->opc
)) {
973 case 1: /* move instructions */
974 instr
->cat1
.dst_type
= half_type(instr
->cat1
.dst_type
);
977 switch (instr
->opc
) {
979 instr
->opc
= OPC_MAD_F16
;
982 instr
->opc
= OPC_SEL_B16
;
985 instr
->opc
= OPC_SEL_S16
;
988 instr
->opc
= OPC_SEL_F16
;
991 instr
->opc
= OPC_SAD_S16
;
993 /* instructions may already be fixed up: */
1006 instr
->cat5
.type
= half_type(instr
->cat5
.type
);
1010 /* some instructions need fix-up if src register is half precision: */
1011 static void fixup_half_instr_src(struct ir3_instruction
*instr
)
1013 switch (instr
->opc
) {
1015 instr
->cat1
.src_type
= half_type(instr
->cat1
.src_type
);
1022 /* NOTE: instr could be NULL for IR3_REG_ARRAY case, for the first
1023 * array access(es) which do not have any previous access to depend
1024 * on from scheduling point of view
1027 reg_assign(struct ir3_ra_ctx
*ctx
, struct ir3_register
*reg
,
1028 struct ir3_instruction
*instr
)
1030 struct ir3_ra_instr_data
*id
;
1032 if (reg
->flags
& IR3_REG_ARRAY
) {
1033 struct ir3_array
*arr
=
1034 ir3_lookup_array(ctx
->ir
, reg
->array
.id
);
1035 unsigned name
= arr
->base
+ reg
->array
.offset
;
1036 unsigned r
= ra_get_node_reg(ctx
->g
, name
);
1037 unsigned num
= ctx
->set
->ra_reg_to_gpr
[r
];
1039 if (reg
->flags
& IR3_REG_RELATIV
) {
1040 reg
->array
.offset
= num
;
1043 reg
->flags
&= ~IR3_REG_SSA
;
1046 reg
->flags
&= ~IR3_REG_ARRAY
;
1047 } else if ((id
= &ctx
->instrd
[instr
->ip
]) && id
->defn
) {
1048 unsigned name
= ra_name(ctx
, id
);
1049 unsigned r
= ra_get_node_reg(ctx
->g
, name
);
1050 unsigned num
= ctx
->set
->ra_reg_to_gpr
[r
] + id
->off
;
1052 debug_assert(!(reg
->flags
& IR3_REG_RELATIV
));
1054 if (is_high(id
->defn
))
1055 num
+= FIRST_HIGH_REG
;
1058 reg
->flags
&= ~IR3_REG_SSA
;
1060 if (is_half(id
->defn
))
1061 reg
->flags
|= IR3_REG_HALF
;
1066 ra_block_alloc(struct ir3_ra_ctx
*ctx
, struct ir3_block
*block
)
1068 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
1069 struct ir3_register
*reg
;
1071 if (instr
->regs_count
== 0)
1074 if (writes_gpr(instr
)) {
1075 reg_assign(ctx
, instr
->regs
[0], instr
);
1076 if (instr
->regs
[0]->flags
& IR3_REG_HALF
)
1077 fixup_half_instr_dst(instr
);
1080 foreach_src_n(reg
, n
, instr
) {
1081 struct ir3_instruction
*src
= reg
->instr
;
1082 /* Note: reg->instr could be null for IR3_REG_ARRAY */
1083 if (src
|| (reg
->flags
& IR3_REG_ARRAY
))
1084 reg_assign(ctx
, instr
->regs
[n
+1], src
);
1085 if (instr
->regs
[n
+1]->flags
& IR3_REG_HALF
)
1086 fixup_half_instr_src(instr
);
1092 ra_alloc(struct ir3_ra_ctx
*ctx
)
1094 /* pre-assign array elements:
1096 list_for_each_entry (struct ir3_array
, arr
, &ctx
->ir
->array_list
, node
) {
1099 if (arr
->end_ip
== 0)
1102 /* figure out what else we conflict with which has already
1106 list_for_each_entry (struct ir3_array
, arr2
, &ctx
->ir
->array_list
, node
) {
1109 if (arr2
->end_ip
== 0)
1111 /* if it intersects with liverange AND register range.. */
1112 if (intersects(arr
->start_ip
, arr
->end_ip
,
1113 arr2
->start_ip
, arr2
->end_ip
) &&
1114 intersects(base
, base
+ arr
->length
,
1115 arr2
->reg
, arr2
->reg
+ arr2
->length
)) {
1116 base
= MAX2(base
, arr2
->reg
+ arr2
->length
);
1123 for (unsigned i
= 0; i
< arr
->length
; i
++) {
1126 name
= arr
->base
+ i
;
1127 reg
= ctx
->set
->gpr_to_ra_reg
[0][base
++];
1129 ra_set_node_reg(ctx
->g
, name
, reg
);
1133 if (!ra_allocate(ctx
->g
))
1136 list_for_each_entry (struct ir3_block
, block
, &ctx
->ir
->block_list
, node
) {
1137 ra_block_alloc(ctx
, block
);
1143 int ir3_ra(struct ir3
*ir
)
1145 struct ir3_ra_ctx ctx
= {
1147 .set
= ir
->compiler
->set
,
1152 ra_add_interference(&ctx
);
1153 ret
= ra_alloc(&ctx
);