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
&& reg
->reg
!= 0) {
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()
64 int hw_reg_mapping
[this->virtual_grf_next
];
66 int reg_width
= c
->dispatch_width
/ 8;
68 hw_reg_mapping
[0] = 0;
69 /* Note that compressed instructions require alignment to 2 registers. */
70 hw_reg_mapping
[1] = ALIGN(this->first_non_payload_grf
, reg_width
);
71 for (i
= 2; i
< this->virtual_grf_next
; i
++) {
72 hw_reg_mapping
[i
] = (hw_reg_mapping
[i
- 1] +
73 this->virtual_grf_sizes
[i
- 1] * reg_width
);
75 last_grf
= hw_reg_mapping
[i
- 1] + (this->virtual_grf_sizes
[i
- 1] *
78 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
79 fs_inst
*inst
= (fs_inst
*)iter
.get();
81 assign_reg(hw_reg_mapping
, &inst
->dst
, reg_width
);
82 assign_reg(hw_reg_mapping
, &inst
->src
[0], reg_width
);
83 assign_reg(hw_reg_mapping
, &inst
->src
[1], reg_width
);
86 if (last_grf
>= BRW_MAX_GRF
) {
87 fail("Ran out of regs on trivial allocator (%d/%d)\n",
88 last_grf
, BRW_MAX_GRF
);
91 this->grf_used
= last_grf
+ reg_width
;
95 fs_visitor::assign_regs()
97 /* Most of this allocation was written for a reg_width of 1
98 * (dispatch_width == 8). In extending to 16-wide, the code was
99 * left in place and it was converted to have the hardware
100 * registers it's allocating be contiguous physical pairs of regs
101 * for reg_width == 2.
103 int reg_width
= c
->dispatch_width
/ 8;
105 int hw_reg_mapping
[this->virtual_grf_next
+ 1];
106 int first_assigned_grf
= ALIGN(this->first_non_payload_grf
, reg_width
);
107 int base_reg_count
= (BRW_MAX_GRF
- first_assigned_grf
) / reg_width
;
108 int class_sizes
[base_reg_count
];
110 int aligned_pair_class
= -1;
112 calculate_live_intervals();
114 /* Set up the register classes.
116 * The base registers store a scalar value. For texture samples,
117 * we get virtual GRFs composed of 4 contiguous hw register. For
118 * structures and arrays, we store them as contiguous larger things
119 * than that, though we should be able to do better most of the
122 class_sizes
[class_count
++] = 1;
123 if (brw
->has_pln
&& intel
->gen
< 6) {
124 /* Always set up the (unaligned) pairs for gen5, so we can find
125 * them for making the aligned pair class.
127 class_sizes
[class_count
++] = 2;
129 for (int r
= 1; r
< this->virtual_grf_next
; r
++) {
132 for (i
= 0; i
< class_count
; i
++) {
133 if (class_sizes
[i
] == this->virtual_grf_sizes
[r
])
136 if (i
== class_count
) {
137 if (this->virtual_grf_sizes
[r
] >= base_reg_count
) {
138 fail("Object too large to register allocate.\n");
141 class_sizes
[class_count
++] = this->virtual_grf_sizes
[r
];
145 int ra_reg_count
= 0;
146 int class_base_reg
[class_count
];
147 int class_reg_count
[class_count
];
148 int classes
[class_count
+ 1];
150 for (int i
= 0; i
< class_count
; i
++) {
151 class_base_reg
[i
] = ra_reg_count
;
152 class_reg_count
[i
] = base_reg_count
- (class_sizes
[i
] - 1);
153 ra_reg_count
+= class_reg_count
[i
];
156 struct ra_regs
*regs
= ra_alloc_reg_set(ra_reg_count
);
157 for (int i
= 0; i
< class_count
; i
++) {
158 classes
[i
] = ra_alloc_reg_class(regs
);
160 for (int i_r
= 0; i_r
< class_reg_count
[i
]; i_r
++) {
161 ra_class_add_reg(regs
, classes
[i
], class_base_reg
[i
] + i_r
);
164 /* Add conflicts between our contiguous registers aliasing
165 * base regs and other register classes' contiguous registers
166 * that alias base regs, or the base regs themselves for classes[0].
168 for (int c
= 0; c
<= i
; c
++) {
169 for (int i_r
= 0; i_r
< class_reg_count
[i
]; i_r
++) {
170 for (int c_r
= MAX2(0, i_r
- (class_sizes
[c
] - 1));
171 c_r
< MIN2(class_reg_count
[c
], i_r
+ class_sizes
[i
]);
175 printf("%d/%d conflicts %d/%d\n",
176 class_sizes
[i
], first_assigned_grf
+ i_r
,
177 class_sizes
[c
], first_assigned_grf
+ c_r
);
180 ra_add_reg_conflict(regs
,
181 class_base_reg
[i
] + i_r
,
182 class_base_reg
[c
] + c_r
);
188 /* Add a special class for aligned pairs, which we'll put delta_x/y
189 * in on gen5 so that we can do PLN.
191 if (brw
->has_pln
&& reg_width
== 1 && intel
->gen
< 6) {
192 int reg_count
= (base_reg_count
- 1) / 2;
193 int unaligned_pair_class
= 1;
194 assert(class_sizes
[unaligned_pair_class
] == 2);
196 aligned_pair_class
= class_count
;
197 classes
[aligned_pair_class
] = ra_alloc_reg_class(regs
);
198 class_sizes
[aligned_pair_class
] = 2;
199 class_base_reg
[aligned_pair_class
] = 0;
200 class_reg_count
[aligned_pair_class
] = 0;
201 int start
= (first_assigned_grf
& 1) ? 1 : 0;
203 for (int i
= 0; i
< reg_count
; i
++) {
204 ra_class_add_reg(regs
, classes
[aligned_pair_class
],
205 class_base_reg
[unaligned_pair_class
] + i
* 2 + start
);
210 ra_set_finalize(regs
);
212 struct ra_graph
*g
= ra_alloc_interference_graph(regs
,
213 this->virtual_grf_next
);
214 /* Node 0 is just a placeholder to keep virtual_grf[] mapping 1:1
217 ra_set_node_class(g
, 0, classes
[0]);
219 for (int i
= 1; i
< this->virtual_grf_next
; i
++) {
220 for (int c
= 0; c
< class_count
; c
++) {
221 if (class_sizes
[c
] == this->virtual_grf_sizes
[i
]) {
222 if (aligned_pair_class
>= 0 &&
223 this->delta_x
.reg
== i
) {
224 ra_set_node_class(g
, i
, classes
[aligned_pair_class
]);
226 ra_set_node_class(g
, i
, classes
[c
]);
232 for (int j
= 1; j
< i
; j
++) {
233 if (virtual_grf_interferes(i
, j
)) {
234 ra_add_node_interference(g
, i
, j
);
239 if (!ra_allocate_no_spills(g
)) {
240 /* Failed to allocate registers. Spill a reg, and the caller will
241 * loop back into here to try again.
243 int reg
= choose_spill_reg(g
);
246 fail("no register to spill\n");
247 } else if (c
->dispatch_width
== 16) {
248 fail("no spilling support on 16-wide yet\n");
260 /* Get the chosen virtual registers for each node, and map virtual
261 * regs in the register classes back down to real hardware reg
264 hw_reg_mapping
[0] = 0; /* unused */
265 for (int i
= 1; i
< this->virtual_grf_next
; i
++) {
266 int reg
= ra_get_node_reg(g
, i
);
269 for (int c
= 0; c
< class_count
; c
++) {
270 if (reg
>= class_base_reg
[c
] &&
271 reg
< class_base_reg
[c
] + class_reg_count
[c
]) {
272 hw_reg
= reg
- class_base_reg
[c
];
278 hw_reg_mapping
[i
] = first_assigned_grf
+ hw_reg
* reg_width
;
279 last_grf
= MAX2(last_grf
,
280 hw_reg_mapping
[i
] + this->virtual_grf_sizes
[i
] - 1);
283 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
284 fs_inst
*inst
= (fs_inst
*)iter
.get();
286 assign_reg(hw_reg_mapping
, &inst
->dst
, reg_width
);
287 assign_reg(hw_reg_mapping
, &inst
->src
[0], reg_width
);
288 assign_reg(hw_reg_mapping
, &inst
->src
[1], reg_width
);
291 this->grf_used
= last_grf
+ reg_width
;
300 fs_visitor::emit_unspill(fs_inst
*inst
, fs_reg dst
, uint32_t spill_offset
)
302 int size
= virtual_grf_sizes
[dst
.reg
];
305 for (int chan
= 0; chan
< size
; chan
++) {
306 fs_inst
*unspill_inst
= new(mem_ctx
) fs_inst(FS_OPCODE_UNSPILL
,
309 unspill_inst
->offset
= spill_offset
+ chan
* REG_SIZE
;
310 unspill_inst
->ir
= inst
->ir
;
311 unspill_inst
->annotation
= inst
->annotation
;
313 /* Choose a MRF that won't conflict with an MRF that's live across the
314 * spill. Nothing else will make it up to MRF 14/15.
316 unspill_inst
->base_mrf
= 14;
317 unspill_inst
->mlen
= 1; /* header contains offset */
318 inst
->insert_before(unspill_inst
);
323 fs_visitor::choose_spill_reg(struct ra_graph
*g
)
325 float loop_scale
= 1.0;
326 float spill_costs
[this->virtual_grf_next
];
327 bool no_spill
[this->virtual_grf_next
];
329 for (int i
= 0; i
< this->virtual_grf_next
; i
++) {
330 spill_costs
[i
] = 0.0;
334 /* Calculate costs for spilling nodes. Call it a cost of 1 per
335 * spill/unspill we'll have to do, and guess that the insides of
336 * loops run 10 times.
338 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
339 fs_inst
*inst
= (fs_inst
*)iter
.get();
341 for (unsigned int i
= 0; i
< 3; i
++) {
342 if (inst
->src
[i
].file
== GRF
) {
343 int size
= virtual_grf_sizes
[inst
->src
[i
].reg
];
344 spill_costs
[inst
->src
[i
].reg
] += size
* loop_scale
;
348 if (inst
->dst
.file
== GRF
) {
349 int size
= virtual_grf_sizes
[inst
->dst
.reg
];
350 spill_costs
[inst
->dst
.reg
] += size
* loop_scale
;
353 switch (inst
->opcode
) {
359 case BRW_OPCODE_WHILE
:
363 case FS_OPCODE_SPILL
:
364 if (inst
->src
[0].file
== GRF
)
365 no_spill
[inst
->src
[0].reg
] = true;
368 case FS_OPCODE_UNSPILL
:
369 if (inst
->dst
.file
== GRF
)
370 no_spill
[inst
->dst
.reg
] = true;
375 for (int i
= 0; i
< this->virtual_grf_next
; i
++) {
377 ra_set_node_spill_cost(g
, i
, spill_costs
[i
]);
380 return ra_get_best_spill_node(g
);
384 fs_visitor::spill_reg(int spill_reg
)
386 int size
= virtual_grf_sizes
[spill_reg
];
387 unsigned int spill_offset
= c
->last_scratch
;
388 assert(ALIGN(spill_offset
, 16) == spill_offset
); /* oword read/write req. */
389 c
->last_scratch
+= size
* REG_SIZE
;
391 /* Generate spill/unspill instructions for the objects being
392 * spilled. Right now, we spill or unspill the whole thing to a
393 * virtual grf of the same size. For most instructions, though, we
394 * could just spill/unspill the GRF being accessed.
396 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
397 fs_inst
*inst
= (fs_inst
*)iter
.get();
399 for (unsigned int i
= 0; i
< 3; i
++) {
400 if (inst
->src
[i
].file
== GRF
&&
401 inst
->src
[i
].reg
== spill_reg
) {
402 inst
->src
[i
].reg
= virtual_grf_alloc(size
);
403 emit_unspill(inst
, inst
->src
[i
], spill_offset
);
407 if (inst
->dst
.file
== GRF
&&
408 inst
->dst
.reg
== spill_reg
) {
409 inst
->dst
.reg
= virtual_grf_alloc(size
);
411 /* Since we spill/unspill the whole thing even if we access
412 * just a component, we may need to unspill before the
413 * instruction we're spilling for.
415 if (size
!= 1 || inst
->predicated
) {
416 emit_unspill(inst
, inst
->dst
, spill_offset
);
419 fs_reg spill_src
= inst
->dst
;
420 spill_src
.reg_offset
= 0;
421 spill_src
.abs
= false;
422 spill_src
.negate
= false;
423 spill_src
.smear
= -1;
425 for (int chan
= 0; chan
< size
; chan
++) {
426 fs_inst
*spill_inst
= new(mem_ctx
) fs_inst(FS_OPCODE_SPILL
,
427 reg_null_f
, spill_src
);
428 spill_src
.reg_offset
++;
429 spill_inst
->offset
= spill_offset
+ chan
* REG_SIZE
;
430 spill_inst
->ir
= inst
->ir
;
431 spill_inst
->annotation
= inst
->annotation
;
432 spill_inst
->base_mrf
= 14;
433 spill_inst
->mlen
= 2; /* header, value */
434 inst
->insert_after(spill_inst
);
439 this->live_intervals_valid
= false;