2 * Copyright © 2010 Intel Corporation
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Eric Anholt <eric@anholt.net>
28 /** @file register_allocate.c
30 * Graph-coloring register allocator.
32 * The basic idea of graph coloring is to make a node in a graph for
33 * every thing that needs a register (color) number assigned, and make
34 * edges in the graph between nodes that interfere (can't be allocated
35 * to the same register at the same time).
37 * During the "simplify" process, any any node with fewer edges than
38 * there are registers means that that edge can get assigned a
39 * register regardless of what its neighbors choose, so that node is
40 * pushed on a stack and removed (with its edges) from the graph.
41 * That likely causes other nodes to become trivially colorable as well.
43 * Then during the "select" process, nodes are popped off of that
44 * stack, their edges restored, and assigned a color different from
45 * their neighbors. Because they were pushed on the stack only when
46 * they were trivially colorable, any color chosen won't interfere
47 * with the registers to be popped later.
49 * The downside to most graph coloring is that real hardware often has
50 * limitations, like registers that need to be allocated to a node in
51 * pairs, or aligned on some boundary. This implementation follows
52 * the paper "Retargetable Graph-Coloring Register Allocation for
53 * Irregular Architectures" by Johan Runeson and Sven-Olof Nyström.
55 * In this system, there are register classes each containing various
56 * registers, and registers may interfere with other registers. For
57 * example, one might have a class of base registers, and a class of
58 * aligned register pairs that would each interfere with their pair of
59 * the base registers. Each node has a register class it needs to be
60 * assigned to. Define p(B) to be the size of register class B, and
61 * q(B,C) to be the number of registers in B that the worst choice
62 * register in C could conflict with. Then, this system replaces the
63 * basic graph coloring test of "fewer edges from this node than there
64 * are registers" with "For this node of class B, the sum of q(B,C)
65 * for each neighbor node of class C is less than pB".
67 * A nice feature of the pq test is that q(B,C) can be computed once
68 * up front and stored in a 2-dimensional array, so that the cost of
69 * coloring a node is constant with the number of registers. We do
70 * this during ra_set_finalize().
76 #include "main/imports.h"
77 #include "main/macros.h"
78 #include "main/mtypes.h"
79 #include "main/bitset.h"
80 #include "register_allocate.h"
85 BITSET_WORD
*conflicts
;
86 unsigned int *conflict_list
;
87 unsigned int conflict_list_size
;
88 unsigned int num_conflicts
;
95 struct ra_class
**classes
;
96 unsigned int class_count
;
103 * Bitset indicating which registers belong to this class.
105 * (If bit N is set, then register N belongs to this class.)
110 * p(B) in Runeson/Nyström paper.
112 * This is "how many regs are in the set."
117 * q(B,C) (indexed by C, B is this register class) in
118 * Runeson/Nyström paper. This is "how many registers of B could
119 * the worst choice register from C conflict with".
127 * List of which nodes this node interferes with. This should be
128 * symmetric with the other node.
130 BITSET_WORD
*adjacency
;
131 unsigned int *adjacency_list
;
132 unsigned int adjacency_list_size
;
133 unsigned int adjacency_count
;
138 /* Register, if assigned, or NO_REG. */
142 * Set when the node is in the trivially colorable stack. When
143 * set, the adjacency to this node is ignored, to implement the
144 * "remove the edge from the graph" in simplification without
145 * having to actually modify the adjacency_list.
150 * The q total, as defined in the Runeson/Nyström paper, for all the
151 * interfering nodes not in the stack.
153 unsigned int q_total
;
155 /* For an implementation that needs register spilling, this is the
156 * approximate cost of spilling this node.
162 struct ra_regs
*regs
;
164 * the variables that need register allocation.
166 struct ra_node
*nodes
;
167 unsigned int count
; /**< count of nodes. */
170 unsigned int stack_count
;
174 * Creates a set of registers for the allocator.
176 * mem_ctx is a ralloc context for the allocator. The reg set may be freed
177 * using ralloc_free().
180 ra_alloc_reg_set(void *mem_ctx
, unsigned int count
)
183 struct ra_regs
*regs
;
185 regs
= rzalloc(mem_ctx
, struct ra_regs
);
187 regs
->regs
= rzalloc_array(regs
, struct ra_reg
, count
);
189 for (i
= 0; i
< count
; i
++) {
190 regs
->regs
[i
].conflicts
= rzalloc_array(regs
->regs
, BITSET_WORD
,
191 BITSET_WORDS(count
));
192 BITSET_SET(regs
->regs
[i
].conflicts
, i
);
194 regs
->regs
[i
].conflict_list
= ralloc_array(regs
->regs
, unsigned int, 4);
195 regs
->regs
[i
].conflict_list_size
= 4;
196 regs
->regs
[i
].conflict_list
[0] = i
;
197 regs
->regs
[i
].num_conflicts
= 1;
204 * The register allocator by default prefers to allocate low register numbers,
205 * since it was written for hardware (gen4/5 Intel) that is limited in its
206 * multithreadedness by the number of registers used in a given shader.
208 * However, for hardware without that restriction, densely packed register
209 * allocation can put serious constraints on instruction scheduling. This
210 * function tells the allocator to rotate around the registers if possible as
211 * it allocates the nodes.
214 ra_set_allocate_round_robin(struct ra_regs
*regs
)
216 regs
->round_robin
= true;
220 ra_add_conflict_list(struct ra_regs
*regs
, unsigned int r1
, unsigned int r2
)
222 struct ra_reg
*reg1
= ®s
->regs
[r1
];
224 if (reg1
->conflict_list_size
== reg1
->num_conflicts
) {
225 reg1
->conflict_list_size
*= 2;
226 reg1
->conflict_list
= reralloc(regs
->regs
, reg1
->conflict_list
,
227 unsigned int, reg1
->conflict_list_size
);
229 reg1
->conflict_list
[reg1
->num_conflicts
++] = r2
;
230 BITSET_SET(reg1
->conflicts
, r2
);
234 ra_add_reg_conflict(struct ra_regs
*regs
, unsigned int r1
, unsigned int r2
)
236 if (!BITSET_TEST(regs
->regs
[r1
].conflicts
, r2
)) {
237 ra_add_conflict_list(regs
, r1
, r2
);
238 ra_add_conflict_list(regs
, r2
, r1
);
243 * Adds a conflict between base_reg and reg, and also between reg and
244 * anything that base_reg conflicts with.
246 * This can simplify code for setting up multiple register classes
247 * which are aggregates of some base hardware registers, compared to
248 * explicitly using ra_add_reg_conflict.
251 ra_add_transitive_reg_conflict(struct ra_regs
*regs
,
252 unsigned int base_reg
, unsigned int reg
)
256 ra_add_reg_conflict(regs
, reg
, base_reg
);
258 for (i
= 0; i
< regs
->regs
[base_reg
].num_conflicts
; i
++) {
259 ra_add_reg_conflict(regs
, reg
, regs
->regs
[base_reg
].conflict_list
[i
]);
264 ra_alloc_reg_class(struct ra_regs
*regs
)
266 struct ra_class
*class;
268 regs
->classes
= reralloc(regs
->regs
, regs
->classes
, struct ra_class
*,
269 regs
->class_count
+ 1);
271 class = rzalloc(regs
, struct ra_class
);
272 regs
->classes
[regs
->class_count
] = class;
274 class->regs
= rzalloc_array(class, BITSET_WORD
, BITSET_WORDS(regs
->count
));
276 return regs
->class_count
++;
280 ra_class_add_reg(struct ra_regs
*regs
, unsigned int c
, unsigned int r
)
282 struct ra_class
*class = regs
->classes
[c
];
284 BITSET_SET(class->regs
, r
);
289 * Returns true if the register belongs to the given class.
292 reg_belongs_to_class(unsigned int r
, struct ra_class
*c
)
294 return BITSET_TEST(c
->regs
, r
);
298 * Must be called after all conflicts and register classes have been
299 * set up and before the register set is used for allocation.
300 * To avoid costly q value computation, use the q_values paramater
301 * to pass precomputed q values to this function.
304 ra_set_finalize(struct ra_regs
*regs
, unsigned int **q_values
)
308 for (b
= 0; b
< regs
->class_count
; b
++) {
309 regs
->classes
[b
]->q
= ralloc_array(regs
, unsigned int, regs
->class_count
);
313 for (b
= 0; b
< regs
->class_count
; b
++) {
314 for (c
= 0; c
< regs
->class_count
; c
++) {
315 regs
->classes
[b
]->q
[c
] = q_values
[b
][c
];
321 /* Compute, for each class B and C, how many regs of B an
322 * allocation to C could conflict with.
324 for (b
= 0; b
< regs
->class_count
; b
++) {
325 for (c
= 0; c
< regs
->class_count
; c
++) {
327 int max_conflicts
= 0;
329 for (rc
= 0; rc
< regs
->count
; rc
++) {
333 if (!reg_belongs_to_class(rc
, regs
->classes
[c
]))
336 for (i
= 0; i
< regs
->regs
[rc
].num_conflicts
; i
++) {
337 unsigned int rb
= regs
->regs
[rc
].conflict_list
[i
];
338 if (BITSET_TEST(regs
->classes
[b
]->regs
, rb
))
341 max_conflicts
= MAX2(max_conflicts
, conflicts
);
343 regs
->classes
[b
]->q
[c
] = max_conflicts
;
349 ra_add_node_adjacency(struct ra_graph
*g
, unsigned int n1
, unsigned int n2
)
351 BITSET_SET(g
->nodes
[n1
].adjacency
, n2
);
354 int n1_class
= g
->nodes
[n1
].class;
355 int n2_class
= g
->nodes
[n2
].class;
356 g
->nodes
[n1
].q_total
+= g
->regs
->classes
[n1_class
]->q
[n2_class
];
359 if (g
->nodes
[n1
].adjacency_count
>=
360 g
->nodes
[n1
].adjacency_list_size
) {
361 g
->nodes
[n1
].adjacency_list_size
*= 2;
362 g
->nodes
[n1
].adjacency_list
= reralloc(g
, g
->nodes
[n1
].adjacency_list
,
364 g
->nodes
[n1
].adjacency_list_size
);
367 g
->nodes
[n1
].adjacency_list
[g
->nodes
[n1
].adjacency_count
] = n2
;
368 g
->nodes
[n1
].adjacency_count
++;
372 ra_alloc_interference_graph(struct ra_regs
*regs
, unsigned int count
)
377 g
= rzalloc(regs
, struct ra_graph
);
379 g
->nodes
= rzalloc_array(g
, struct ra_node
, count
);
382 g
->stack
= rzalloc_array(g
, unsigned int, count
);
384 for (i
= 0; i
< count
; i
++) {
385 int bitset_count
= BITSET_WORDS(count
);
386 g
->nodes
[i
].adjacency
= rzalloc_array(g
, BITSET_WORD
, bitset_count
);
388 g
->nodes
[i
].adjacency_list_size
= 4;
389 g
->nodes
[i
].adjacency_list
=
390 ralloc_array(g
, unsigned int, g
->nodes
[i
].adjacency_list_size
);
391 g
->nodes
[i
].adjacency_count
= 0;
392 g
->nodes
[i
].q_total
= 0;
394 ra_add_node_adjacency(g
, i
, i
);
395 g
->nodes
[i
].reg
= NO_REG
;
402 ra_set_node_class(struct ra_graph
*g
,
403 unsigned int n
, unsigned int class)
405 g
->nodes
[n
].class = class;
409 ra_add_node_interference(struct ra_graph
*g
,
410 unsigned int n1
, unsigned int n2
)
412 if (!BITSET_TEST(g
->nodes
[n1
].adjacency
, n2
)) {
413 ra_add_node_adjacency(g
, n1
, n2
);
414 ra_add_node_adjacency(g
, n2
, n1
);
419 pq_test(struct ra_graph
*g
, unsigned int n
)
421 int n_class
= g
->nodes
[n
].class;
423 return g
->nodes
[n
].q_total
< g
->regs
->classes
[n_class
]->p
;
427 decrement_q(struct ra_graph
*g
, unsigned int n
)
430 int n_class
= g
->nodes
[n
].class;
432 for (i
= 0; i
< g
->nodes
[n
].adjacency_count
; i
++) {
433 unsigned int n2
= g
->nodes
[n
].adjacency_list
[i
];
434 unsigned int n2_class
= g
->nodes
[n2
].class;
436 if (n
!= n2
&& !g
->nodes
[n2
].in_stack
) {
437 assert(g
->nodes
[n2
].q_total
>= g
->regs
->classes
[n2_class
]->q
[n_class
]);
438 g
->nodes
[n2
].q_total
-= g
->regs
->classes
[n2_class
]->q
[n_class
];
444 * Simplifies the interference graph by pushing all
445 * trivially-colorable nodes into a stack of nodes to be colored,
446 * removing them from the graph, and rinsing and repeating.
448 * If we encounter a case where we can't push any nodes on the stack, then
449 * we optimistically choose a node and push it on the stack. We heuristically
450 * push the node with the lowest total q value, since it has the fewest
451 * neighbors and therefore is most likely to be allocated.
454 ra_simplify(struct ra_graph
*g
)
456 bool progress
= true;
460 unsigned int best_optimistic_node
= ~0;
461 unsigned int lowest_q_total
= ~0;
465 for (i
= g
->count
- 1; i
>= 0; i
--) {
466 if (g
->nodes
[i
].in_stack
|| g
->nodes
[i
].reg
!= NO_REG
)
471 g
->stack
[g
->stack_count
] = i
;
473 g
->nodes
[i
].in_stack
= true;
476 unsigned int new_q_total
= g
->nodes
[i
].q_total
;
477 if (new_q_total
< lowest_q_total
) {
478 best_optimistic_node
= i
;
479 lowest_q_total
= new_q_total
;
484 if (!progress
&& best_optimistic_node
!= ~0) {
485 decrement_q(g
, best_optimistic_node
);
486 g
->stack
[g
->stack_count
] = best_optimistic_node
;
488 g
->nodes
[best_optimistic_node
].in_stack
= true;
495 * Pops nodes from the stack back into the graph, coloring them with
496 * registers as they go.
498 * If all nodes were trivially colorable, then this must succeed. If
499 * not (optimistic coloring), then it may return false;
502 ra_select(struct ra_graph
*g
)
505 int start_search_reg
= 0;
507 while (g
->stack_count
!= 0) {
510 int n
= g
->stack
[g
->stack_count
- 1];
511 struct ra_class
*c
= g
->regs
->classes
[g
->nodes
[n
].class];
513 /* Find the lowest-numbered reg which is not used by a member
514 * of the graph adjacent to us.
516 for (ri
= 0; ri
< g
->regs
->count
; ri
++) {
517 r
= (start_search_reg
+ ri
) % g
->regs
->count
;
518 if (!reg_belongs_to_class(r
, c
))
521 /* Check if any of our neighbors conflict with this register choice. */
522 for (i
= 0; i
< g
->nodes
[n
].adjacency_count
; i
++) {
523 unsigned int n2
= g
->nodes
[n
].adjacency_list
[i
];
525 if (!g
->nodes
[n2
].in_stack
&&
526 BITSET_TEST(g
->regs
->regs
[r
].conflicts
, g
->nodes
[n2
].reg
)) {
530 if (i
== g
->nodes
[n
].adjacency_count
)
534 /* set this to false even if we return here so that
535 * ra_get_best_spill_node() considers this node later.
537 g
->nodes
[n
].in_stack
= false;
539 if (ri
== g
->regs
->count
)
545 if (g
->regs
->round_robin
)
546 start_search_reg
= r
+ 1;
553 ra_allocate(struct ra_graph
*g
)
560 ra_get_node_reg(struct ra_graph
*g
, unsigned int n
)
562 return g
->nodes
[n
].reg
;
566 * Forces a node to a specific register. This can be used to avoid
567 * creating a register class containing one node when handling data
568 * that must live in a fixed location and is known to not conflict
569 * with other forced register assignment (as is common with shader
570 * input data). These nodes do not end up in the stack during
571 * ra_simplify(), and thus at ra_select() time it is as if they were
572 * the first popped off the stack and assigned their fixed locations.
573 * Nodes that use this function do not need to be assigned a register
576 * Must be called before ra_simplify().
579 ra_set_node_reg(struct ra_graph
*g
, unsigned int n
, unsigned int reg
)
581 g
->nodes
[n
].reg
= reg
;
582 g
->nodes
[n
].in_stack
= false;
586 ra_get_spill_benefit(struct ra_graph
*g
, unsigned int n
)
590 int n_class
= g
->nodes
[n
].class;
592 /* Define the benefit of eliminating an interference between n, n2
593 * through spilling as q(C, B) / p(C). This is similar to the
594 * "count number of edges" approach of traditional graph coloring,
595 * but takes classes into account.
597 for (j
= 0; j
< g
->nodes
[n
].adjacency_count
; j
++) {
598 unsigned int n2
= g
->nodes
[n
].adjacency_list
[j
];
600 unsigned int n2_class
= g
->nodes
[n2
].class;
601 benefit
+= ((float)g
->regs
->classes
[n_class
]->q
[n2_class
] /
602 g
->regs
->classes
[n_class
]->p
);
610 * Returns a node number to be spilled according to the cost/benefit using
611 * the pq test, or -1 if there are no spillable nodes.
614 ra_get_best_spill_node(struct ra_graph
*g
)
616 unsigned int best_node
= -1;
617 float best_benefit
= 0.0;
620 /* Consider any nodes that we colored successfully or the node we failed to
621 * color for spilling. When we failed to color a node in ra_select(), we
622 * only considered these nodes, so spilling any other ones would not result
623 * in us making progress.
625 for (n
= 0; n
< g
->count
; n
++) {
626 float cost
= g
->nodes
[n
].spill_cost
;
632 if (g
->nodes
[n
].in_stack
)
635 benefit
= ra_get_spill_benefit(g
, n
);
637 if (benefit
/ cost
> best_benefit
) {
638 best_benefit
= benefit
/ cost
;
647 * Only nodes with a spill cost set (cost != 0.0) will be considered
648 * for register spilling.
651 ra_set_node_spill_cost(struct ra_graph
*g
, unsigned int n
, float cost
)
653 g
->nodes
[n
].spill_cost
= cost
;