1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
29 #include "util/u_math.h"
30 #include "util/register_allocate.h"
31 #include "util/ralloc.h"
32 #include "util/bitset.h"
35 #include "ir3_compiler.h"
38 * Register Assignment:
40 * Uses the register_allocate util, which implements graph coloring
41 * algo with interference classes. To handle the cases where we need
42 * consecutive registers (for example, texture sample instructions),
43 * we model these as larger (double/quad/etc) registers which conflict
44 * with the corresponding registers in other classes.
46 * Additionally we create additional classes for half-regs, which
47 * do not conflict with the full-reg classes. We do need at least
48 * sizes 1-4 (to deal w/ texture sample instructions output to half-
49 * reg). At the moment we don't create the higher order half-reg
50 * classes as half-reg frequently does not have enough precision
51 * for texture coords at higher resolutions.
53 * There are some additional cases that we need to handle specially,
54 * as the graph coloring algo doesn't understand "partial writes".
55 * For example, a sequence like:
58 * sam (f32)(xy)r0.x, ...
60 * sam (f32)(xyzw)r0.w, r0.x, ... ; 3d texture, so r0.xyz are coord
62 * In this scenario, we treat r0.xyz as class size 3, which is written
63 * (from a use/def perspective) at the 'add' instruction and ignore the
64 * subsequent partial writes to r0.xy. So the 'add r0.z, ...' is the
65 * defining instruction, as it is the first to partially write r0.xyz.
67 * Note i965 has a similar scenario, which they solve with a virtual
68 * LOAD_PAYLOAD instruction which gets turned into multiple MOV's after
69 * register assignment. But for us that is horrible from a scheduling
70 * standpoint. Instead what we do is use idea of 'definer' instruction.
71 * Ie. the first instruction (lowest ip) to write to the array is the
72 * one we consider from use/def perspective when building interference
73 * graph. (Other instructions which write other array elements just
74 * define the variable some more.)
77 static const unsigned class_sizes
[] = {
79 4 + 4, /* txd + 1d/2d */
81 /* temporary: until we can assign arrays, create classes so we
82 * can round up array to fit. NOTE with tgsi arrays should
83 * really all be multiples of four:
91 #define class_count ARRAY_SIZE(class_sizes)
93 static const unsigned half_class_sizes
[] = {
96 #define half_class_count ARRAY_SIZE(half_class_sizes)
97 #define total_class_count (class_count + half_class_count)
99 /* Below a0.x are normal regs. RA doesn't need to assign a0.x/p0.x. */
100 #define NUM_REGS (4 * (REG_A0 - 1))
101 /* Number of virtual regs in a given class: */
102 #define CLASS_REGS(i) (NUM_REGS - (class_sizes[i] - 1))
103 #define HALF_CLASS_REGS(i) (NUM_REGS - (half_class_sizes[i] - 1))
105 /* register-set, created one time, used for all shaders: */
106 struct ir3_ra_reg_set
{
107 struct ra_regs
*regs
;
108 unsigned int classes
[class_count
];
109 unsigned int half_classes
[half_class_count
];
110 /* maps flat virtual register space to base gpr: */
111 uint16_t *ra_reg_to_gpr
;
112 /* maps cls,gpr to flat virtual register space: */
113 uint16_t **gpr_to_ra_reg
;
116 /* One-time setup of RA register-set, which describes all the possible
117 * "virtual" registers and their interferences. Ie. double register
118 * occupies (and conflicts with) two single registers, and so forth.
119 * Since registers do not need to be aligned to their class size, they
120 * can conflict with other registers in the same class too. Ie:
122 * Single (base) | Double
123 * --------------+---------------
130 * (NOTE the disassembler uses notation like r0.x/y/z/w but those are
131 * really just four scalar registers. Don't let that confuse you.)
133 struct ir3_ra_reg_set
*
134 ir3_ra_alloc_reg_set(void *memctx
)
136 struct ir3_ra_reg_set
*set
= rzalloc(memctx
, struct ir3_ra_reg_set
);
137 unsigned ra_reg_count
, reg
, first_half_reg
;
138 unsigned int **q_values
;
140 /* calculate # of regs across all classes: */
142 for (unsigned i
= 0; i
< class_count
; i
++)
143 ra_reg_count
+= CLASS_REGS(i
);
144 for (unsigned i
= 0; i
< half_class_count
; i
++)
145 ra_reg_count
+= HALF_CLASS_REGS(i
);
147 /* allocate and populate q_values: */
148 q_values
= ralloc_array(set
, unsigned *, total_class_count
);
149 for (unsigned i
= 0; i
< class_count
; i
++) {
150 q_values
[i
] = rzalloc_array(q_values
, unsigned, total_class_count
);
152 /* From register_allocate.c:
154 * q(B,C) (indexed by C, B is this register class) in
155 * Runeson/Nyström paper. This is "how many registers of B could
156 * the worst choice register from C conflict with".
158 * If we just let the register allocation algorithm compute these
159 * values, is extremely expensive. However, since all of our
160 * registers are laid out, we can very easily compute them
161 * ourselves. View the register from C as fixed starting at GRF n
162 * somewhere in the middle, and the register from B as sliding back
163 * and forth. Then the first register to conflict from B is the
164 * one starting at n - class_size[B] + 1 and the last register to
165 * conflict will start at n + class_size[B] - 1. Therefore, the
166 * number of conflicts from B is class_size[B] + class_size[C] - 1.
168 * +-+-+-+-+-+-+ +-+-+-+-+-+-+
169 * B | | | | | |n| --> | | | | | | |
170 * +-+-+-+-+-+-+ +-+-+-+-+-+-+
175 * (Idea copied from brw_fs_reg_allocate.cpp)
177 for (unsigned j
= 0; j
< class_count
; j
++)
178 q_values
[i
][j
] = class_sizes
[i
] + class_sizes
[j
] - 1;
181 for (unsigned i
= class_count
; i
< total_class_count
; i
++) {
182 q_values
[i
] = ralloc_array(q_values
, unsigned, total_class_count
);
184 /* see comment above: */
185 for (unsigned j
= class_count
; j
< total_class_count
; j
++) {
186 q_values
[i
][j
] = half_class_sizes
[i
- class_count
] +
187 half_class_sizes
[j
- class_count
] - 1;
191 /* allocate the reg-set.. */
192 set
->regs
= ra_alloc_reg_set(set
, ra_reg_count
, true);
193 set
->ra_reg_to_gpr
= ralloc_array(set
, uint16_t, ra_reg_count
);
194 set
->gpr_to_ra_reg
= ralloc_array(set
, uint16_t *, total_class_count
);
198 for (unsigned i
= 0; i
< class_count
; i
++) {
199 set
->classes
[i
] = ra_alloc_reg_class(set
->regs
);
201 set
->gpr_to_ra_reg
[i
] = ralloc_array(set
, uint16_t, CLASS_REGS(i
));
203 for (unsigned j
= 0; j
< CLASS_REGS(i
); j
++) {
204 ra_class_add_reg(set
->regs
, set
->classes
[i
], reg
);
206 set
->ra_reg_to_gpr
[reg
] = j
;
207 set
->gpr_to_ra_reg
[i
][j
] = reg
;
209 for (unsigned br
= j
; br
< j
+ class_sizes
[i
]; br
++)
210 ra_add_transitive_reg_conflict(set
->regs
, br
, reg
);
216 first_half_reg
= reg
;
218 for (unsigned i
= 0; i
< half_class_count
; i
++) {
219 set
->half_classes
[i
] = ra_alloc_reg_class(set
->regs
);
221 set
->gpr_to_ra_reg
[class_count
+ i
] =
222 ralloc_array(set
, uint16_t, CLASS_REGS(i
));
224 for (unsigned j
= 0; j
< HALF_CLASS_REGS(i
); j
++) {
225 ra_class_add_reg(set
->regs
, set
->half_classes
[i
], reg
);
227 set
->ra_reg_to_gpr
[reg
] = j
;
228 set
->gpr_to_ra_reg
[class_count
+ i
][j
] = reg
;
230 for (unsigned br
= j
; br
< j
+ half_class_sizes
[i
]; br
++)
231 ra_add_transitive_reg_conflict(set
->regs
, br
+ first_half_reg
, reg
);
237 ra_set_finalize(set
->regs
, q_values
);
239 ralloc_free(q_values
);
244 /* additional block-data (per-block) */
245 struct ir3_ra_block_data
{
246 BITSET_WORD
*def
; /* variables defined before used in block */
247 BITSET_WORD
*use
; /* variables used before defined in block */
248 BITSET_WORD
*livein
; /* which defs reach entry point of block */
249 BITSET_WORD
*liveout
; /* which defs reach exit point of block */
252 /* additional instruction-data (per-instruction) */
253 struct ir3_ra_instr_data
{
254 /* cached instruction 'definer' info: */
255 struct ir3_instruction
*defn
;
259 /* register-assign context, per-shader */
265 struct ir3_ra_reg_set
*set
;
267 unsigned alloc_count
;
268 unsigned class_alloc_count
[total_class_count
];
269 unsigned class_base
[total_class_count
];
271 unsigned *def
, *use
; /* def/use table */
272 struct ir3_ra_instr_data
*instrd
;
276 is_half(struct ir3_instruction
*instr
)
278 return !!(instr
->regs
[0]->flags
& IR3_REG_HALF
);
282 size_to_class(unsigned sz
, bool half
)
285 for (unsigned i
= 0; i
< half_class_count
; i
++)
286 if (half_class_sizes
[i
] >= sz
)
287 return i
+ class_count
;
289 for (unsigned i
= 0; i
< class_count
; i
++)
290 if (class_sizes
[i
] >= sz
)
298 is_temp(struct ir3_register
*reg
)
300 if (reg
->flags
& (IR3_REG_CONST
| IR3_REG_IMMED
))
302 if ((reg
->num
== regid(REG_A0
, 0)) ||
303 (reg
->num
== regid(REG_P0
, 0)))
309 writes_gpr(struct ir3_instruction
*instr
)
313 /* is dest a normal temp register: */
314 return is_temp(instr
->regs
[0]);
318 instr_before(struct ir3_instruction
*a
, struct ir3_instruction
*b
)
320 if (a
->flags
& IR3_INSTR_UNUSED
)
322 return (a
->ip
< b
->ip
);
325 static struct ir3_instruction
*
326 get_definer(struct ir3_ra_ctx
*ctx
, struct ir3_instruction
*instr
,
329 struct ir3_ra_instr_data
*id
= &ctx
->instrd
[instr
->ip
];
330 struct ir3_instruction
*d
= NULL
;
333 return get_definer(ctx
, instr
->fanin
, sz
, off
);
341 if (is_meta(instr
) && (instr
->opc
== OPC_META_FI
)) {
342 /* What about the case where collect is subset of array, we
343 * need to find the distance between where actual array starts
344 * and fanin.. that probably doesn't happen currently.
346 struct ir3_register
*src
;
349 /* note: don't use foreach_ssa_src as this gets called once
350 * while assigning regs (which clears SSA flag)
352 foreach_src_n(src
, n
, instr
) {
353 struct ir3_instruction
*dd
;
357 dd
= get_definer(ctx
, src
->instr
, &dsz
, &doff
);
359 if ((!d
) || instr_before(dd
, d
)) {
366 } else if (instr
->cp
.right
|| instr
->cp
.left
) {
367 /* covers also the meta:fo case, which ends up w/ single
368 * scalar instructions for each component:
370 struct ir3_instruction
*f
= ir3_neighbor_first(instr
);
372 /* by definition, the entire sequence forms one linked list
373 * of single scalar register nodes (even if some of them may
374 * be fanouts from a texture sample (for example) instr. We
375 * just need to walk the list finding the first element of
376 * the group defined (lowest ip)
380 /* need to skip over unused in the group: */
381 while (f
&& (f
->flags
& IR3_INSTR_UNUSED
)) {
387 if ((!d
) || instr_before(f
, d
))
398 /* second case is looking directly at the instruction which
399 * produces multiple values (eg, texture sample), rather
400 * than the fanout nodes that point back to that instruction.
401 * This isn't quite right, because it may be part of a larger
404 * sam (f32)(xyzw)r0.x, ...
407 * sam (f32)(xyzw)r2.x, r0.w <-- (r0.w, r1.x, r1.y)
409 * need to come up with a better way to handle that case.
411 if (instr
->address
) {
412 *sz
= instr
->regs
[0]->size
;
414 *sz
= util_last_bit(instr
->regs
[0]->wrmask
);
420 if (d
->regs
[0]->flags
& IR3_REG_PHI_SRC
) {
421 struct ir3_instruction
*phi
= d
->regs
[0]->instr
;
422 struct ir3_instruction
*dd
;
425 dd
= get_definer(ctx
, phi
, &dsz
, &doff
);
427 *sz
= MAX2(*sz
, dsz
);
430 if (instr_before(dd
, d
)) {
435 if (is_meta(d
) && (d
->opc
== OPC_META_PHI
)) {
436 /* we have already inserted parallel-copies into
437 * the phi, so we don't need to chase definers
439 struct ir3_register
*src
;
440 struct ir3_instruction
*dd
= d
;
442 /* note: don't use foreach_ssa_src as this gets called once
443 * while assigning regs (which clears SSA flag)
445 foreach_src(src
, d
) {
448 if (instr_before(src
->instr
, dd
))
455 if (is_meta(d
) && (d
->opc
== OPC_META_FO
)) {
456 struct ir3_instruction
*dd
;
459 dd
= get_definer(ctx
, d
->regs
[1]->instr
, &dsz
, &doff
);
461 /* by definition, should come before: */
462 debug_assert(instr_before(dd
, d
));
464 *sz
= MAX2(*sz
, dsz
);
466 /* Fanout's are grouped, so *off should already valid */
479 ra_block_find_definers(struct ir3_ra_ctx
*ctx
, struct ir3_block
*block
)
481 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
482 struct ir3_ra_instr_data
*id
= &ctx
->instrd
[instr
->ip
];
483 if (instr
->regs_count
== 0)
485 /* couple special cases: */
486 if (writes_addr(instr
) || writes_pred(instr
)) {
490 id
->defn
= get_definer(ctx
, instr
, &id
->sz
, &id
->off
);
491 id
->cls
= size_to_class(id
->sz
, is_half(id
->defn
));
495 /* give each instruction a name (and ip), and count up the # of names
499 ra_block_name_instructions(struct ir3_ra_ctx
*ctx
, struct ir3_block
*block
)
501 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
502 struct ir3_ra_instr_data
*id
= &ctx
->instrd
[instr
->ip
];
510 if (instr
->regs_count
== 0)
513 if (!writes_gpr(instr
))
516 if (id
->defn
!= instr
)
519 /* arrays which don't fit in one of the pre-defined class
520 * sizes are pre-colored:
522 * TODO but we still need to allocate names for them, don't we??
525 instr
->name
= ctx
->class_alloc_count
[id
->cls
]++;
532 ra_init(struct ir3_ra_ctx
*ctx
)
536 ir3_clear_mark(ctx
->ir
);
537 n
= ir3_count_instructions(ctx
->ir
);
539 ctx
->instrd
= rzalloc_array(NULL
, struct ir3_ra_instr_data
, n
);
541 list_for_each_entry (struct ir3_block
, block
, &ctx
->ir
->block_list
, node
) {
542 ra_block_find_definers(ctx
, block
);
545 list_for_each_entry (struct ir3_block
, block
, &ctx
->ir
->block_list
, node
) {
546 ra_block_name_instructions(ctx
, block
);
549 /* figure out the base register name for each class. The
550 * actual ra name is class_base[cls] + instr->name;
552 ctx
->class_base
[0] = 0;
553 for (unsigned i
= 1; i
< total_class_count
; i
++) {
554 ctx
->class_base
[i
] = ctx
->class_base
[i
-1] +
555 ctx
->class_alloc_count
[i
-1];
558 ctx
->g
= ra_alloc_interference_graph(ctx
->set
->regs
, ctx
->alloc_count
);
559 ralloc_steal(ctx
->g
, ctx
->instrd
);
560 ctx
->def
= rzalloc_array(ctx
->g
, unsigned, ctx
->alloc_count
);
561 ctx
->use
= rzalloc_array(ctx
->g
, unsigned, ctx
->alloc_count
);
565 ra_name(struct ir3_ra_ctx
*ctx
, int cls
, struct ir3_instruction
*defn
)
568 debug_assert(cls
>= 0);
569 name
= ctx
->class_base
[cls
] + defn
->name
;
570 debug_assert(name
< ctx
->alloc_count
);
575 ra_destroy(struct ir3_ra_ctx
*ctx
)
581 ra_block_compute_live_ranges(struct ir3_ra_ctx
*ctx
, struct ir3_block
*block
)
583 struct ir3_ra_block_data
*bd
;
584 unsigned bitset_words
= BITSET_WORDS(ctx
->alloc_count
);
586 bd
= rzalloc(ctx
->g
, struct ir3_ra_block_data
);
588 bd
->def
= rzalloc_array(bd
, BITSET_WORD
, bitset_words
);
589 bd
->use
= rzalloc_array(bd
, BITSET_WORD
, bitset_words
);
590 bd
->livein
= rzalloc_array(bd
, BITSET_WORD
, bitset_words
);
591 bd
->liveout
= rzalloc_array(bd
, BITSET_WORD
, bitset_words
);
595 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
596 struct ir3_instruction
*src
;
598 if (instr
->regs_count
== 0)
601 /* There are a couple special cases to deal with here:
603 * fanout: used to split values from a higher class to a lower
604 * class, for example split the results of a texture fetch
605 * into individual scalar values; We skip over these from
606 * a 'def' perspective, and for a 'use' we walk the chain
607 * up to the defining instruction.
609 * fanin: used to collect values from lower class and assemble
610 * them together into a higher class, for example arguments
611 * to texture sample instructions; We consider these to be
612 * defined at the earliest fanin source.
614 * phi: used to merge values from different flow control paths
615 * to the same reg. Consider defined at earliest phi src,
616 * and update all the other phi src's (which may come later
617 * in the program) as users to extend the var's live range.
619 * Most of this, other than phi, is completely handled in the
620 * get_definer() helper.
622 * In either case, we trace the instruction back to the original
623 * definer and consider that as the def/use ip.
626 if (writes_gpr(instr
)) {
627 struct ir3_ra_instr_data
*id
= &ctx
->instrd
[instr
->ip
];
629 if (id
->defn
== instr
) {
630 /* arrays which don't fit in one of the pre-defined class
631 * sizes are pre-colored:
634 unsigned name
= ra_name(ctx
, id
->cls
, id
->defn
);
636 ctx
->def
[name
] = id
->defn
->ip
;
637 ctx
->use
[name
] = id
->defn
->ip
;
639 /* since we are in SSA at this point: */
640 debug_assert(!BITSET_TEST(bd
->use
, name
));
642 BITSET_SET(bd
->def
, name
);
644 if (is_half(id
->defn
)) {
645 ra_set_node_class(ctx
->g
, name
,
646 ctx
->set
->half_classes
[id
->cls
- class_count
]);
648 ra_set_node_class(ctx
->g
, name
,
649 ctx
->set
->classes
[id
->cls
]);
652 /* extend the live range for phi srcs, which may come
653 * from the bottom of the loop
655 if (id
->defn
->regs
[0]->flags
& IR3_REG_PHI_SRC
) {
656 struct ir3_instruction
*phi
= id
->defn
->regs
[0]->instr
;
657 foreach_ssa_src(src
, phi
) {
658 /* if src is after phi, then we need to extend
659 * the liverange to the end of src's block:
661 if (src
->ip
> phi
->ip
) {
662 struct ir3_instruction
*last
=
663 list_last_entry(&src
->block
->instr_list
,
664 struct ir3_instruction
, node
);
665 ctx
->use
[name
] = MAX2(ctx
->use
[name
], last
->ip
);
673 foreach_ssa_src(src
, instr
) {
674 if (writes_gpr(src
)) {
675 struct ir3_ra_instr_data
*id
= &ctx
->instrd
[src
->ip
];
678 unsigned name
= ra_name(ctx
, id
->cls
, id
->defn
);
679 ctx
->use
[name
] = MAX2(ctx
->use
[name
], instr
->ip
);
680 if (!BITSET_TEST(bd
->def
, name
))
681 BITSET_SET(bd
->use
, name
);
689 ra_compute_livein_liveout(struct ir3_ra_ctx
*ctx
)
691 unsigned bitset_words
= BITSET_WORDS(ctx
->alloc_count
);
692 bool progress
= false;
694 list_for_each_entry (struct ir3_block
, block
, &ctx
->ir
->block_list
, node
) {
695 struct ir3_ra_block_data
*bd
= block
->data
;
698 for (unsigned i
= 0; i
< bitset_words
; i
++) {
699 BITSET_WORD new_livein
=
700 (bd
->use
[i
] | (bd
->liveout
[i
] & ~bd
->def
[i
]));
702 if (new_livein
& ~bd
->livein
[i
]) {
703 bd
->livein
[i
] |= new_livein
;
708 /* update liveout: */
709 for (unsigned j
= 0; j
< ARRAY_SIZE(block
->successors
); j
++) {
710 struct ir3_block
*succ
= block
->successors
[j
];
711 struct ir3_ra_block_data
*succ_bd
;
716 succ_bd
= succ
->data
;
718 for (unsigned i
= 0; i
< bitset_words
; i
++) {
719 BITSET_WORD new_liveout
=
720 (succ_bd
->livein
[i
] & ~bd
->liveout
[i
]);
723 bd
->liveout
[i
] |= new_liveout
;
734 ra_add_interference(struct ir3_ra_ctx
*ctx
)
736 struct ir3
*ir
= ctx
->ir
;
738 /* compute live ranges (use/def) on a block level, also updating
739 * block's def/use bitmasks (used below to calculate per-block
742 list_for_each_entry (struct ir3_block
, block
, &ir
->block_list
, node
) {
743 ra_block_compute_live_ranges(ctx
, block
);
746 /* update per-block livein/liveout: */
747 while (ra_compute_livein_liveout(ctx
)) {}
749 /* extend start/end ranges based on livein/liveout info from cfg: */
750 unsigned bitset_words
= BITSET_WORDS(ctx
->alloc_count
);
751 list_for_each_entry (struct ir3_block
, block
, &ir
->block_list
, node
) {
752 struct ir3_ra_block_data
*bd
= block
->data
;
754 for (unsigned i
= 0; i
< bitset_words
; i
++) {
755 if (BITSET_TEST(bd
->livein
, i
)) {
756 ctx
->def
[i
] = MIN2(ctx
->def
[i
], block
->start_ip
);
757 ctx
->use
[i
] = MAX2(ctx
->use
[i
], block
->start_ip
);
760 if (BITSET_TEST(bd
->liveout
, i
)) {
761 ctx
->def
[i
] = MIN2(ctx
->def
[i
], block
->end_ip
);
762 ctx
->use
[i
] = MAX2(ctx
->use
[i
], block
->end_ip
);
767 /* need to fix things up to keep outputs live: */
768 for (unsigned i
= 0; i
< ir
->noutputs
; i
++) {
769 struct ir3_instruction
*instr
= ir
->outputs
[i
];
770 struct ir3_ra_instr_data
*id
= &ctx
->instrd
[instr
->ip
];
773 unsigned name
= ra_name(ctx
, id
->cls
, id
->defn
);
774 ctx
->use
[name
] = ctx
->instr_cnt
;
778 for (unsigned i
= 0; i
< ctx
->alloc_count
; i
++) {
779 for (unsigned j
= 0; j
< ctx
->alloc_count
; j
++) {
780 if (!((ctx
->def
[i
] >= ctx
->use
[j
]) ||
781 (ctx
->def
[j
] >= ctx
->use
[i
]))) {
782 ra_add_node_interference(ctx
->g
, i
, j
);
788 /* some instructions need fix-up if dst register is half precision: */
789 static void fixup_half_instr_dst(struct ir3_instruction
*instr
)
791 switch (instr
->category
) {
792 case 1: /* move instructions */
793 instr
->cat1
.dst_type
= half_type(instr
->cat1
.dst_type
);
796 switch (instr
->opc
) {
798 instr
->opc
= OPC_MAD_F16
;
801 instr
->opc
= OPC_SEL_B16
;
804 instr
->opc
= OPC_SEL_S16
;
807 instr
->opc
= OPC_SEL_F16
;
810 instr
->opc
= OPC_SAD_S16
;
812 /* instructions may already be fixed up: */
825 instr
->cat5
.type
= half_type(instr
->cat5
.type
);
829 /* some instructions need fix-up if src register is half precision: */
830 static void fixup_half_instr_src(struct ir3_instruction
*instr
)
832 switch (instr
->category
) {
833 case 1: /* move instructions */
834 instr
->cat1
.src_type
= half_type(instr
->cat1
.src_type
);
840 reg_assign(struct ir3_ra_ctx
*ctx
, struct ir3_register
*reg
,
841 struct ir3_instruction
*instr
)
843 struct ir3_ra_instr_data
*id
= &ctx
->instrd
[instr
->ip
];
846 unsigned name
= ra_name(ctx
, id
->cls
, id
->defn
);
847 unsigned r
= ra_get_node_reg(ctx
->g
, name
);
848 unsigned num
= ctx
->set
->ra_reg_to_gpr
[r
] + id
->off
;
850 if (reg
->flags
& IR3_REG_RELATIV
)
854 reg
->flags
&= ~(IR3_REG_SSA
| IR3_REG_PHI_SRC
);
856 if (is_half(id
->defn
))
857 reg
->flags
|= IR3_REG_HALF
;
862 ra_block_alloc(struct ir3_ra_ctx
*ctx
, struct ir3_block
*block
)
864 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
865 struct ir3_register
*reg
;
867 if (instr
->regs_count
== 0)
870 if (writes_gpr(instr
)) {
871 reg_assign(ctx
, instr
->regs
[0], instr
);
872 if (instr
->regs
[0]->flags
& IR3_REG_HALF
)
873 fixup_half_instr_dst(instr
);
876 foreach_src_n(reg
, n
, instr
) {
877 struct ir3_instruction
*src
= reg
->instr
;
881 reg_assign(ctx
, instr
->regs
[n
+1], src
);
882 if (instr
->regs
[n
+1]->flags
& IR3_REG_HALF
)
883 fixup_half_instr_src(instr
);
889 ra_alloc(struct ir3_ra_ctx
*ctx
)
891 /* frag shader inputs get pre-assigned, since we have some
892 * constraints/unknowns about setup for some of these regs:
894 if (ctx
->type
== SHADER_FRAGMENT
) {
895 struct ir3
*ir
= ctx
->ir
;
897 if (ctx
->frag_face
&& (i
< ir
->ninputs
) && ir
->inputs
[i
]) {
898 struct ir3_instruction
*instr
= ir
->inputs
[i
];
899 int cls
= size_to_class(1, true);
900 unsigned name
= ra_name(ctx
, cls
, instr
);
901 unsigned reg
= ctx
->set
->gpr_to_ra_reg
[cls
][0];
903 /* if we have frag_face, it gets hr0.x */
904 ra_set_node_reg(ctx
->g
, name
, reg
);
908 for (j
= 0; i
< ir
->ninputs
; i
++) {
909 struct ir3_instruction
*instr
= ir
->inputs
[i
];
911 struct ir3_ra_instr_data
*id
= &ctx
->instrd
[instr
->ip
];
913 if (id
->defn
== instr
) {
916 name
= ra_name(ctx
, id
->cls
, id
->defn
);
917 reg
= ctx
->set
->gpr_to_ra_reg
[id
->cls
][j
];
919 ra_set_node_reg(ctx
->g
, name
, reg
);
926 if (!ra_allocate(ctx
->g
))
929 list_for_each_entry (struct ir3_block
, block
, &ctx
->ir
->block_list
, node
) {
930 ra_block_alloc(ctx
, block
);
936 int ir3_ra(struct ir3
*ir
, enum shader_t type
,
937 bool frag_coord
, bool frag_face
)
939 struct ir3_ra_ctx ctx
= {
942 .frag_face
= frag_face
,
943 .set
= ir
->compiler
->set
,
948 ra_add_interference(&ctx
);
949 ret
= ra_alloc(&ctx
);