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