42ab66df6d8bb2ab6ae50d2521b2b72b9818a99b
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_reg_allocate.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 extern "C" {
29
30 #include <sys/types.h>
31
32 #include "main/macros.h"
33 #include "main/shaderobj.h"
34 #include "main/uniforms.h"
35 #include "program/prog_parameter.h"
36 #include "program/prog_print.h"
37 #include "program/prog_optimize.h"
38 #include "program/register_allocate.h"
39 #include "program/sampler.h"
40 #include "program/hash_table.h"
41 #include "brw_context.h"
42 #include "brw_eu.h"
43 #include "brw_wm.h"
44 }
45 #include "brw_fs.h"
46 #include "../glsl/glsl_types.h"
47 #include "../glsl/ir_optimization.h"
48 #include "../glsl/ir_print_visitor.h"
49
50 static void
51 assign_reg(int *reg_hw_locations, fs_reg *reg, int reg_width)
52 {
53 if (reg->file == GRF) {
54 assert(reg->reg_offset >= 0);
55 reg->hw_reg = reg_hw_locations[reg->reg] + reg->reg_offset * reg_width;
56 reg->reg = 0;
57 }
58 }
59
60 void
61 fs_visitor::assign_regs_trivial()
62 {
63 int hw_reg_mapping[this->virtual_grf_next + 1];
64 int i;
65 int reg_width = c->dispatch_width / 8;
66
67 /* Note that compressed instructions require alignment to 2 registers. */
68 hw_reg_mapping[0] = ALIGN(this->first_non_payload_grf, reg_width);
69 for (i = 1; i <= this->virtual_grf_next; i++) {
70 hw_reg_mapping[i] = (hw_reg_mapping[i - 1] +
71 this->virtual_grf_sizes[i - 1] * reg_width);
72 }
73 this->grf_used = hw_reg_mapping[this->virtual_grf_next];
74
75 foreach_list(node, &this->instructions) {
76 fs_inst *inst = (fs_inst *)node;
77
78 assign_reg(hw_reg_mapping, &inst->dst, reg_width);
79 assign_reg(hw_reg_mapping, &inst->src[0], reg_width);
80 assign_reg(hw_reg_mapping, &inst->src[1], reg_width);
81 }
82
83 if (this->grf_used >= BRW_MAX_GRF) {
84 fail("Ran out of regs on trivial allocator (%d/%d)\n",
85 this->grf_used, BRW_MAX_GRF);
86 }
87
88 }
89
90 bool
91 fs_visitor::assign_regs()
92 {
93 /* Most of this allocation was written for a reg_width of 1
94 * (dispatch_width == 8). In extending to 16-wide, the code was
95 * left in place and it was converted to have the hardware
96 * registers it's allocating be contiguous physical pairs of regs
97 * for reg_width == 2.
98 */
99 int reg_width = c->dispatch_width / 8;
100 int hw_reg_mapping[this->virtual_grf_next];
101 int first_assigned_grf = ALIGN(this->first_non_payload_grf, reg_width);
102 int base_reg_count = (BRW_MAX_GRF - first_assigned_grf) / reg_width;
103 int class_sizes[base_reg_count];
104 int class_count = 0;
105 int aligned_pairs_class = -1;
106
107 calculate_live_intervals();
108
109 /* Set up the register classes.
110 *
111 * The base registers store a scalar value. For texture samples,
112 * we get virtual GRFs composed of 4 contiguous hw register. For
113 * structures and arrays, we store them as contiguous larger things
114 * than that, though we should be able to do better most of the
115 * time.
116 */
117 class_sizes[class_count++] = 1;
118 if (brw->has_pln && intel->gen < 6) {
119 /* Always set up the (unaligned) pairs for gen5, so we can find
120 * them for making the aligned pair class.
121 */
122 class_sizes[class_count++] = 2;
123 }
124 for (int r = 0; r < this->virtual_grf_next; r++) {
125 int i;
126
127 for (i = 0; i < class_count; i++) {
128 if (class_sizes[i] == this->virtual_grf_sizes[r])
129 break;
130 }
131 if (i == class_count) {
132 if (this->virtual_grf_sizes[r] >= base_reg_count) {
133 fail("Object too large to register allocate.\n");
134 }
135
136 class_sizes[class_count++] = this->virtual_grf_sizes[r];
137 }
138 }
139
140 /* Compute the total number of registers across all classes. */
141 int ra_reg_count = 0;
142 for (int i = 0; i < class_count; i++) {
143 ra_reg_count += base_reg_count - (class_sizes[i] - 1);
144 }
145
146 struct ra_regs *regs = ra_alloc_reg_set(ra_reg_count);
147 uint8_t ra_reg_to_grf[ra_reg_count];
148 int classes[class_count + 1];
149
150 /* Now, add the registers to their classes, and add the conflicts
151 * between them and the base GRF registers (and also each other).
152 */
153 int reg = 0;
154 int pairs_base_reg = 0;
155 int pairs_reg_count = 0;
156 for (int i = 0; i < class_count; i++) {
157 int class_reg_count = base_reg_count - (class_sizes[i] - 1);
158 classes[i] = ra_alloc_reg_class(regs);
159
160 /* Save this off for the aligned pair class at the end. */
161 if (class_sizes[i] == 2) {
162 pairs_base_reg = reg;
163 pairs_reg_count = class_reg_count;
164 }
165
166 for (int j = 0; j < class_reg_count; j++) {
167 ra_class_add_reg(regs, classes[i], reg);
168
169 ra_reg_to_grf[reg] = j;
170
171 for (int base_reg = j;
172 base_reg < j + class_sizes[i];
173 base_reg++) {
174 ra_add_transitive_reg_conflict(regs, base_reg, reg);
175 }
176
177 reg++;
178 }
179 }
180 assert(reg == ra_reg_count);
181
182 /* Add a special class for aligned pairs, which we'll put delta_x/y
183 * in on gen5 so that we can do PLN.
184 */
185 if (brw->has_pln && reg_width == 1 && intel->gen < 6) {
186 aligned_pairs_class = ra_alloc_reg_class(regs);
187
188 for (int i = 0; i < pairs_reg_count; i++) {
189 if ((ra_reg_to_grf[pairs_base_reg + i] & 1) == 0) {
190 ra_class_add_reg(regs, aligned_pairs_class,
191 pairs_base_reg + i);
192 }
193 }
194 class_count++;
195 }
196
197 ra_set_finalize(regs);
198
199 struct ra_graph *g = ra_alloc_interference_graph(regs,
200 this->virtual_grf_next);
201
202 for (int i = 0; i < this->virtual_grf_next; i++) {
203 for (int c = 0; c < class_count; c++) {
204 if (class_sizes[c] == this->virtual_grf_sizes[i]) {
205 if (aligned_pairs_class >= 0 &&
206 this->delta_x.reg == i) {
207 ra_set_node_class(g, i, aligned_pairs_class);
208 } else {
209 ra_set_node_class(g, i, classes[c]);
210 }
211 break;
212 }
213 }
214
215 for (int j = 0; j < i; j++) {
216 if (virtual_grf_interferes(i, j)) {
217 ra_add_node_interference(g, i, j);
218 }
219 }
220 }
221
222 if (!ra_allocate_no_spills(g)) {
223 /* Failed to allocate registers. Spill a reg, and the caller will
224 * loop back into here to try again.
225 */
226 int reg = choose_spill_reg(g);
227
228 if (reg == -1) {
229 fail("no register to spill\n");
230 } else if (intel->gen >= 7) {
231 fail("no spilling support on gen7 yet\n");
232 } else if (c->dispatch_width == 16) {
233 fail("no spilling support on 16-wide yet\n");
234 } else {
235 spill_reg(reg);
236 }
237
238
239 ralloc_free(g);
240 ralloc_free(regs);
241
242 return false;
243 }
244
245 /* Get the chosen virtual registers for each node, and map virtual
246 * regs in the register classes back down to real hardware reg
247 * numbers.
248 */
249 this->grf_used = first_assigned_grf;
250 for (int i = 0; i < this->virtual_grf_next; i++) {
251 int reg = ra_get_node_reg(g, i);
252
253 hw_reg_mapping[i] = first_assigned_grf + ra_reg_to_grf[reg] * reg_width;
254 this->grf_used = MAX2(this->grf_used,
255 hw_reg_mapping[i] + this->virtual_grf_sizes[i] *
256 reg_width);
257 }
258
259 foreach_list(node, &this->instructions) {
260 fs_inst *inst = (fs_inst *)node;
261
262 assign_reg(hw_reg_mapping, &inst->dst, reg_width);
263 assign_reg(hw_reg_mapping, &inst->src[0], reg_width);
264 assign_reg(hw_reg_mapping, &inst->src[1], reg_width);
265 }
266
267 ralloc_free(g);
268 ralloc_free(regs);
269
270 return true;
271 }
272
273 void
274 fs_visitor::emit_unspill(fs_inst *inst, fs_reg dst, uint32_t spill_offset)
275 {
276 int size = virtual_grf_sizes[dst.reg];
277 dst.reg_offset = 0;
278
279 for (int chan = 0; chan < size; chan++) {
280 fs_inst *unspill_inst = new(mem_ctx) fs_inst(FS_OPCODE_UNSPILL,
281 dst);
282 dst.reg_offset++;
283 unspill_inst->offset = spill_offset + chan * REG_SIZE;
284 unspill_inst->ir = inst->ir;
285 unspill_inst->annotation = inst->annotation;
286
287 /* Choose a MRF that won't conflict with an MRF that's live across the
288 * spill. Nothing else will make it up to MRF 14/15.
289 */
290 unspill_inst->base_mrf = 14;
291 unspill_inst->mlen = 1; /* header contains offset */
292 inst->insert_before(unspill_inst);
293 }
294 }
295
296 int
297 fs_visitor::choose_spill_reg(struct ra_graph *g)
298 {
299 float loop_scale = 1.0;
300 float spill_costs[this->virtual_grf_next];
301 bool no_spill[this->virtual_grf_next];
302
303 for (int i = 0; i < this->virtual_grf_next; i++) {
304 spill_costs[i] = 0.0;
305 no_spill[i] = false;
306 }
307
308 /* Calculate costs for spilling nodes. Call it a cost of 1 per
309 * spill/unspill we'll have to do, and guess that the insides of
310 * loops run 10 times.
311 */
312 foreach_list(node, &this->instructions) {
313 fs_inst *inst = (fs_inst *)node;
314
315 for (unsigned int i = 0; i < 3; i++) {
316 if (inst->src[i].file == GRF) {
317 int size = virtual_grf_sizes[inst->src[i].reg];
318 spill_costs[inst->src[i].reg] += size * loop_scale;
319 }
320 }
321
322 if (inst->dst.file == GRF) {
323 int size = virtual_grf_sizes[inst->dst.reg];
324 spill_costs[inst->dst.reg] += size * loop_scale;
325 }
326
327 switch (inst->opcode) {
328
329 case BRW_OPCODE_DO:
330 loop_scale *= 10;
331 break;
332
333 case BRW_OPCODE_WHILE:
334 loop_scale /= 10;
335 break;
336
337 case FS_OPCODE_SPILL:
338 if (inst->src[0].file == GRF)
339 no_spill[inst->src[0].reg] = true;
340 break;
341
342 case FS_OPCODE_UNSPILL:
343 if (inst->dst.file == GRF)
344 no_spill[inst->dst.reg] = true;
345 break;
346 }
347 }
348
349 for (int i = 0; i < this->virtual_grf_next; i++) {
350 if (!no_spill[i])
351 ra_set_node_spill_cost(g, i, spill_costs[i]);
352 }
353
354 return ra_get_best_spill_node(g);
355 }
356
357 void
358 fs_visitor::spill_reg(int spill_reg)
359 {
360 int size = virtual_grf_sizes[spill_reg];
361 unsigned int spill_offset = c->last_scratch;
362 assert(ALIGN(spill_offset, 16) == spill_offset); /* oword read/write req. */
363 c->last_scratch += size * REG_SIZE;
364
365 /* Generate spill/unspill instructions for the objects being
366 * spilled. Right now, we spill or unspill the whole thing to a
367 * virtual grf of the same size. For most instructions, though, we
368 * could just spill/unspill the GRF being accessed.
369 */
370 foreach_list(node, &this->instructions) {
371 fs_inst *inst = (fs_inst *)node;
372
373 for (unsigned int i = 0; i < 3; i++) {
374 if (inst->src[i].file == GRF &&
375 inst->src[i].reg == spill_reg) {
376 inst->src[i].reg = virtual_grf_alloc(size);
377 emit_unspill(inst, inst->src[i], spill_offset);
378 }
379 }
380
381 if (inst->dst.file == GRF &&
382 inst->dst.reg == spill_reg) {
383 inst->dst.reg = virtual_grf_alloc(size);
384
385 /* Since we spill/unspill the whole thing even if we access
386 * just a component, we may need to unspill before the
387 * instruction we're spilling for.
388 */
389 if (size != 1 || inst->predicated) {
390 emit_unspill(inst, inst->dst, spill_offset);
391 }
392
393 fs_reg spill_src = inst->dst;
394 spill_src.reg_offset = 0;
395 spill_src.abs = false;
396 spill_src.negate = false;
397 spill_src.smear = -1;
398
399 for (int chan = 0; chan < size; chan++) {
400 fs_inst *spill_inst = new(mem_ctx) fs_inst(FS_OPCODE_SPILL,
401 reg_null_f, spill_src);
402 spill_src.reg_offset++;
403 spill_inst->offset = spill_offset + chan * REG_SIZE;
404 spill_inst->ir = inst->ir;
405 spill_inst->annotation = inst->annotation;
406 spill_inst->base_mrf = 14;
407 spill_inst->mlen = 2; /* header, value */
408 inst->insert_after(spill_inst);
409 }
410 }
411 }
412
413 this->live_intervals_valid = false;
414 }