42ab66df6d8bb2ab6ae50d2521b2b72b9818a99b
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>
30 #include <sys/types.h>
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"
46 #include "../glsl/glsl_types.h"
47 #include "../glsl/ir_optimization.h"
48 #include "../glsl/ir_print_visitor.h"
51 assign_reg(int *reg_hw_locations
, fs_reg
*reg
, int reg_width
)
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
;
61 fs_visitor::assign_regs_trivial()
63 int hw_reg_mapping
[this->virtual_grf_next
+ 1];
65 int reg_width
= c
->dispatch_width
/ 8;
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
);
73 this->grf_used
= hw_reg_mapping
[this->virtual_grf_next
];
75 foreach_list(node
, &this->instructions
) {
76 fs_inst
*inst
= (fs_inst
*)node
;
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
);
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
);
91 fs_visitor::assign_regs()
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
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
];
105 int aligned_pairs_class
= -1;
107 calculate_live_intervals();
109 /* Set up the register classes.
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
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.
122 class_sizes
[class_count
++] = 2;
124 for (int r
= 0; r
< this->virtual_grf_next
; r
++) {
127 for (i
= 0; i
< class_count
; i
++) {
128 if (class_sizes
[i
] == this->virtual_grf_sizes
[r
])
131 if (i
== class_count
) {
132 if (this->virtual_grf_sizes
[r
] >= base_reg_count
) {
133 fail("Object too large to register allocate.\n");
136 class_sizes
[class_count
++] = this->virtual_grf_sizes
[r
];
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);
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];
150 /* Now, add the registers to their classes, and add the conflicts
151 * between them and the base GRF registers (and also each other).
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
);
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
;
166 for (int j
= 0; j
< class_reg_count
; j
++) {
167 ra_class_add_reg(regs
, classes
[i
], reg
);
169 ra_reg_to_grf
[reg
] = j
;
171 for (int base_reg
= j
;
172 base_reg
< j
+ class_sizes
[i
];
174 ra_add_transitive_reg_conflict(regs
, base_reg
, reg
);
180 assert(reg
== ra_reg_count
);
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.
185 if (brw
->has_pln
&& reg_width
== 1 && intel
->gen
< 6) {
186 aligned_pairs_class
= ra_alloc_reg_class(regs
);
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
,
197 ra_set_finalize(regs
);
199 struct ra_graph
*g
= ra_alloc_interference_graph(regs
,
200 this->virtual_grf_next
);
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
);
209 ra_set_node_class(g
, i
, classes
[c
]);
215 for (int j
= 0; j
< i
; j
++) {
216 if (virtual_grf_interferes(i
, j
)) {
217 ra_add_node_interference(g
, i
, j
);
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.
226 int reg
= choose_spill_reg(g
);
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");
245 /* Get the chosen virtual registers for each node, and map virtual
246 * regs in the register classes back down to real hardware reg
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
);
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
] *
259 foreach_list(node
, &this->instructions
) {
260 fs_inst
*inst
= (fs_inst
*)node
;
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
);
274 fs_visitor::emit_unspill(fs_inst
*inst
, fs_reg dst
, uint32_t spill_offset
)
276 int size
= virtual_grf_sizes
[dst
.reg
];
279 for (int chan
= 0; chan
< size
; chan
++) {
280 fs_inst
*unspill_inst
= new(mem_ctx
) fs_inst(FS_OPCODE_UNSPILL
,
283 unspill_inst
->offset
= spill_offset
+ chan
* REG_SIZE
;
284 unspill_inst
->ir
= inst
->ir
;
285 unspill_inst
->annotation
= inst
->annotation
;
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.
290 unspill_inst
->base_mrf
= 14;
291 unspill_inst
->mlen
= 1; /* header contains offset */
292 inst
->insert_before(unspill_inst
);
297 fs_visitor::choose_spill_reg(struct ra_graph
*g
)
299 float loop_scale
= 1.0;
300 float spill_costs
[this->virtual_grf_next
];
301 bool no_spill
[this->virtual_grf_next
];
303 for (int i
= 0; i
< this->virtual_grf_next
; i
++) {
304 spill_costs
[i
] = 0.0;
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.
312 foreach_list(node
, &this->instructions
) {
313 fs_inst
*inst
= (fs_inst
*)node
;
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
;
322 if (inst
->dst
.file
== GRF
) {
323 int size
= virtual_grf_sizes
[inst
->dst
.reg
];
324 spill_costs
[inst
->dst
.reg
] += size
* loop_scale
;
327 switch (inst
->opcode
) {
333 case BRW_OPCODE_WHILE
:
337 case FS_OPCODE_SPILL
:
338 if (inst
->src
[0].file
== GRF
)
339 no_spill
[inst
->src
[0].reg
] = true;
342 case FS_OPCODE_UNSPILL
:
343 if (inst
->dst
.file
== GRF
)
344 no_spill
[inst
->dst
.reg
] = true;
349 for (int i
= 0; i
< this->virtual_grf_next
; i
++) {
351 ra_set_node_spill_cost(g
, i
, spill_costs
[i
]);
354 return ra_get_best_spill_node(g
);
358 fs_visitor::spill_reg(int spill_reg
)
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
;
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.
370 foreach_list(node
, &this->instructions
) {
371 fs_inst
*inst
= (fs_inst
*)node
;
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
);
381 if (inst
->dst
.file
== GRF
&&
382 inst
->dst
.reg
== spill_reg
) {
383 inst
->dst
.reg
= virtual_grf_alloc(size
);
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.
389 if (size
!= 1 || inst
->predicated
) {
390 emit_unspill(inst
, inst
->dst
, spill_offset
);
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;
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
);
413 this->live_intervals_valid
= false;