2 * Copyright © 2011 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
25 #include "main/macros.h"
26 #include "program/register_allocate.h"
36 assign(unsigned int *reg_hw_locations
, reg
*reg
)
38 if (reg
->file
== GRF
) {
39 reg
->reg
= reg_hw_locations
[reg
->reg
];
44 vec4_visitor::reg_allocate_trivial()
46 unsigned int hw_reg_mapping
[this->virtual_grf_count
];
47 bool virtual_grf_used
[this->virtual_grf_count
];
51 /* Calculate which virtual GRFs are actually in use after whatever
52 * optimization passes have occurred.
54 for (int i
= 0; i
< this->virtual_grf_count
; i
++) {
55 virtual_grf_used
[i
] = false;
58 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
59 vec4_instruction
*inst
= (vec4_instruction
*)iter
.get();
61 if (inst
->dst
.file
== GRF
)
62 virtual_grf_used
[inst
->dst
.reg
] = true;
64 for (int i
= 0; i
< 3; i
++) {
65 if (inst
->src
[i
].file
== GRF
)
66 virtual_grf_used
[inst
->src
[i
].reg
] = true;
70 hw_reg_mapping
[0] = this->first_non_payload_grf
;
71 next
= hw_reg_mapping
[0] + this->virtual_grf_sizes
[0];
72 for (i
= 1; i
< this->virtual_grf_count
; i
++) {
73 if (virtual_grf_used
[i
]) {
74 hw_reg_mapping
[i
] = next
;
75 next
+= this->virtual_grf_sizes
[i
];
78 prog_data
->total_grf
= next
;
80 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
81 vec4_instruction
*inst
= (vec4_instruction
*)iter
.get();
83 assign(hw_reg_mapping
, &inst
->dst
);
84 assign(hw_reg_mapping
, &inst
->src
[0]);
85 assign(hw_reg_mapping
, &inst
->src
[1]);
86 assign(hw_reg_mapping
, &inst
->src
[2]);
89 if (prog_data
->total_grf
> max_grf
) {
90 fail("Ran out of regs on trivial allocator (%d/%d)\n",
91 prog_data
->total_grf
, max_grf
);
99 brw_alloc_reg_set_for_classes(struct brw_context
*brw
,
104 struct intel_context
*intel
= &brw
->intel
;
106 /* Compute the total number of registers across all classes. */
107 int ra_reg_count
= 0;
108 for (int i
= 0; i
< class_count
; i
++) {
109 ra_reg_count
+= base_reg_count
- (class_sizes
[i
] - 1);
112 ralloc_free(brw
->vs
.ra_reg_to_grf
);
113 brw
->vs
.ra_reg_to_grf
= ralloc_array(brw
, uint8_t, ra_reg_count
);
114 ralloc_free(brw
->vs
.regs
);
115 brw
->vs
.regs
= ra_alloc_reg_set(brw
, ra_reg_count
);
117 ra_set_allocate_round_robin(brw
->vs
.regs
);
118 ralloc_free(brw
->vs
.classes
);
119 brw
->vs
.classes
= ralloc_array(brw
, int, class_count
+ 1);
121 /* Now, add the registers to their classes, and add the conflicts
122 * between them and the base GRF registers (and also each other).
125 for (int i
= 0; i
< class_count
; i
++) {
126 int class_reg_count
= base_reg_count
- (class_sizes
[i
] - 1);
127 brw
->vs
.classes
[i
] = ra_alloc_reg_class(brw
->vs
.regs
);
129 for (int j
= 0; j
< class_reg_count
; j
++) {
130 ra_class_add_reg(brw
->vs
.regs
, brw
->vs
.classes
[i
], reg
);
132 brw
->vs
.ra_reg_to_grf
[reg
] = j
;
134 for (int base_reg
= j
;
135 base_reg
< j
+ class_sizes
[i
];
137 ra_add_transitive_reg_conflict(brw
->vs
.regs
, base_reg
, reg
);
143 assert(reg
== ra_reg_count
);
145 ra_set_finalize(brw
->vs
.regs
, NULL
);
149 vec4_visitor::reg_allocate()
151 unsigned int hw_reg_mapping
[virtual_grf_count
];
152 int first_assigned_grf
= this->first_non_payload_grf
;
153 int base_reg_count
= max_grf
- first_assigned_grf
;
154 int class_sizes
[base_reg_count
];
157 /* Using the trivial allocator can be useful in debugging undefined
158 * register access as a result of broken optimization passes.
161 return reg_allocate_trivial();
163 calculate_live_intervals();
165 /* Set up the register classes.
167 * The base registers store a vec4. However, we'll need larger
168 * storage for arrays, structures, and matrices, which will be sets
169 * of contiguous registers.
171 class_sizes
[class_count
++] = 1;
173 for (int r
= 0; r
< virtual_grf_count
; r
++) {
176 for (i
= 0; i
< class_count
; i
++) {
177 if (class_sizes
[i
] == this->virtual_grf_sizes
[r
])
180 if (i
== class_count
) {
181 if (this->virtual_grf_sizes
[r
] >= base_reg_count
) {
182 fail("Object too large to register allocate.\n");
185 class_sizes
[class_count
++] = this->virtual_grf_sizes
[r
];
189 brw_alloc_reg_set_for_classes(brw
, class_sizes
, class_count
, base_reg_count
);
191 struct ra_graph
*g
= ra_alloc_interference_graph(brw
->vs
.regs
,
194 for (int i
= 0; i
< virtual_grf_count
; i
++) {
195 for (int c
= 0; c
< class_count
; c
++) {
196 if (class_sizes
[c
] == this->virtual_grf_sizes
[i
]) {
197 ra_set_node_class(g
, i
, brw
->vs
.classes
[c
]);
202 for (int j
= 0; j
< i
; j
++) {
203 if (virtual_grf_interferes(i
, j
)) {
204 ra_add_node_interference(g
, i
, j
);
209 if (!ra_allocate_no_spills(g
)) {
210 /* Failed to allocate registers. Spill a reg, and the caller will
211 * loop back into here to try again.
213 int reg
= choose_spill_reg(g
);
215 fail("no register to spill\n");
223 /* Get the chosen virtual registers for each node, and map virtual
224 * regs in the register classes back down to real hardware reg
227 prog_data
->total_grf
= first_assigned_grf
;
228 for (int i
= 0; i
< virtual_grf_count
; i
++) {
229 int reg
= ra_get_node_reg(g
, i
);
231 hw_reg_mapping
[i
] = first_assigned_grf
+ brw
->vs
.ra_reg_to_grf
[reg
];
232 prog_data
->total_grf
= MAX2(prog_data
->total_grf
,
233 hw_reg_mapping
[i
] + virtual_grf_sizes
[i
]);
236 foreach_list(node
, &this->instructions
) {
237 vec4_instruction
*inst
= (vec4_instruction
*)node
;
239 assign(hw_reg_mapping
, &inst
->dst
);
240 assign(hw_reg_mapping
, &inst
->src
[0]);
241 assign(hw_reg_mapping
, &inst
->src
[1]);
242 assign(hw_reg_mapping
, &inst
->src
[2]);
251 vec4_visitor::evaluate_spill_costs(float *spill_costs
, bool *no_spill
)
253 float loop_scale
= 1.0;
255 for (int i
= 0; i
< this->virtual_grf_count
; i
++) {
256 spill_costs
[i
] = 0.0;
257 no_spill
[i
] = virtual_grf_sizes
[i
] != 1;
260 /* Calculate costs for spilling nodes. Call it a cost of 1 per
261 * spill/unspill we'll have to do, and guess that the insides of
262 * loops run 10 times.
264 foreach_list(node
, &this->instructions
) {
265 vec4_instruction
*inst
= (vec4_instruction
*) node
;
267 for (unsigned int i
= 0; i
< 3; i
++) {
268 if (inst
->src
[i
].file
== GRF
) {
269 spill_costs
[inst
->src
[i
].reg
] += loop_scale
;
270 if (inst
->src
[i
].reladdr
)
271 no_spill
[inst
->src
[i
].reg
] = true;
275 if (inst
->dst
.file
== GRF
) {
276 spill_costs
[inst
->dst
.reg
] += loop_scale
;
277 if (inst
->dst
.reladdr
)
278 no_spill
[inst
->dst
.reg
] = true;
281 switch (inst
->opcode
) {
287 case BRW_OPCODE_WHILE
:
291 case VS_OPCODE_SCRATCH_READ
:
292 case VS_OPCODE_SCRATCH_WRITE
:
293 for (int i
= 0; i
< 3; i
++) {
294 if (inst
->src
[i
].file
== GRF
)
295 no_spill
[inst
->src
[i
].reg
] = true;
297 if (inst
->dst
.file
== GRF
)
298 no_spill
[inst
->dst
.reg
] = true;
308 vec4_visitor::choose_spill_reg(struct ra_graph
*g
)
310 float spill_costs
[this->virtual_grf_count
];
311 bool no_spill
[this->virtual_grf_count
];
313 evaluate_spill_costs(spill_costs
, no_spill
);
315 for (int i
= 0; i
< this->virtual_grf_count
; i
++) {
317 ra_set_node_spill_cost(g
, i
, spill_costs
[i
]);
320 return ra_get_best_spill_node(g
);
324 vec4_visitor::spill_reg(int spill_reg_nr
)
326 assert(virtual_grf_sizes
[spill_reg_nr
] == 1);
327 unsigned int spill_offset
= c
->last_scratch
++;
329 /* Generate spill/unspill instructions for the objects being spilled. */
330 foreach_list(node
, &this->instructions
) {
331 vec4_instruction
*inst
= (vec4_instruction
*) node
;
333 for (unsigned int i
= 0; i
< 3; i
++) {
334 if (inst
->src
[i
].file
== GRF
&& inst
->src
[i
].reg
== spill_reg_nr
) {
335 src_reg spill_reg
= inst
->src
[i
];
336 inst
->src
[i
].reg
= virtual_grf_alloc(1);
337 dst_reg temp
= dst_reg(inst
->src
[i
]);
339 /* Only read the necessary channels, to avoid overwriting the rest
340 * with data that may not have been written to scratch.
343 for (int c
= 0; c
< 4; c
++)
344 temp
.writemask
|= (1 << BRW_GET_SWZ(inst
->src
[i
].swizzle
, c
));
345 assert(temp
.writemask
!= 0);
347 emit_scratch_read(inst
, temp
, spill_reg
, spill_offset
);
351 if (inst
->dst
.file
== GRF
&& inst
->dst
.reg
== spill_reg_nr
) {
352 emit_scratch_write(inst
, spill_offset
);
356 this->live_intervals_valid
= false;
359 } /* namespace brw */