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
)
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
;
61 fs_visitor::assign_regs_trivial()
64 int hw_reg_mapping
[this->virtual_grf_next
];
67 hw_reg_mapping
[0] = 0;
68 hw_reg_mapping
[1] = this->first_non_payload_grf
;
69 for (i
= 2; i
< this->virtual_grf_next
; i
++) {
70 hw_reg_mapping
[i
] = (hw_reg_mapping
[i
- 1] +
71 this->virtual_grf_sizes
[i
- 1]);
73 last_grf
= hw_reg_mapping
[i
- 1] + this->virtual_grf_sizes
[i
- 1];
75 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
76 fs_inst
*inst
= (fs_inst
*)iter
.get();
78 assign_reg(hw_reg_mapping
, &inst
->dst
);
79 assign_reg(hw_reg_mapping
, &inst
->src
[0]);
80 assign_reg(hw_reg_mapping
, &inst
->src
[1]);
83 this->grf_used
= last_grf
+ 1;
87 fs_visitor::assign_regs()
90 int hw_reg_mapping
[this->virtual_grf_next
+ 1];
91 int base_reg_count
= BRW_MAX_GRF
- this->first_non_payload_grf
;
92 int class_sizes
[base_reg_count
];
94 int aligned_pair_class
= -1;
96 calculate_live_intervals();
98 /* Set up the register classes.
100 * The base registers store a scalar value. For texture samples,
101 * we get virtual GRFs composed of 4 contiguous hw register. For
102 * structures and arrays, we store them as contiguous larger things
103 * than that, though we should be able to do better most of the
106 class_sizes
[class_count
++] = 1;
107 if (brw
->has_pln
&& intel
->gen
< 6) {
108 /* Always set up the (unaligned) pairs for gen5, so we can find
109 * them for making the aligned pair class.
111 class_sizes
[class_count
++] = 2;
113 for (int r
= 1; r
< this->virtual_grf_next
; r
++) {
116 for (i
= 0; i
< class_count
; i
++) {
117 if (class_sizes
[i
] == this->virtual_grf_sizes
[r
])
120 if (i
== class_count
) {
121 if (this->virtual_grf_sizes
[r
] >= base_reg_count
) {
122 fprintf(stderr
, "Object too large to register allocate.\n");
126 class_sizes
[class_count
++] = this->virtual_grf_sizes
[r
];
130 int ra_reg_count
= 0;
131 int class_base_reg
[class_count
];
132 int class_reg_count
[class_count
];
133 int classes
[class_count
+ 1];
135 for (int i
= 0; i
< class_count
; i
++) {
136 class_base_reg
[i
] = ra_reg_count
;
137 class_reg_count
[i
] = base_reg_count
- (class_sizes
[i
] - 1);
138 ra_reg_count
+= class_reg_count
[i
];
141 struct ra_regs
*regs
= ra_alloc_reg_set(ra_reg_count
);
142 for (int i
= 0; i
< class_count
; i
++) {
143 classes
[i
] = ra_alloc_reg_class(regs
);
145 for (int i_r
= 0; i_r
< class_reg_count
[i
]; i_r
++) {
146 ra_class_add_reg(regs
, classes
[i
], class_base_reg
[i
] + i_r
);
149 /* Add conflicts between our contiguous registers aliasing
150 * base regs and other register classes' contiguous registers
151 * that alias base regs, or the base regs themselves for classes[0].
153 for (int c
= 0; c
<= i
; c
++) {
154 for (int i_r
= 0; i_r
< class_reg_count
[i
]; i_r
++) {
155 for (int c_r
= MAX2(0, i_r
- (class_sizes
[c
] - 1));
156 c_r
< MIN2(class_reg_count
[c
], i_r
+ class_sizes
[i
]);
160 printf("%d/%d conflicts %d/%d\n",
161 class_sizes
[i
], this->first_non_payload_grf
+ i_r
,
162 class_sizes
[c
], this->first_non_payload_grf
+ c_r
);
165 ra_add_reg_conflict(regs
,
166 class_base_reg
[i
] + i_r
,
167 class_base_reg
[c
] + c_r
);
173 /* Add a special class for aligned pairs, which we'll put delta_x/y
174 * in on gen5 so that we can do PLN.
176 if (brw
->has_pln
&& intel
->gen
< 6) {
177 int reg_count
= (base_reg_count
- 1) / 2;
178 int unaligned_pair_class
= 1;
179 assert(class_sizes
[unaligned_pair_class
] == 2);
181 aligned_pair_class
= class_count
;
182 classes
[aligned_pair_class
] = ra_alloc_reg_class(regs
);
183 class_sizes
[aligned_pair_class
] = 2;
184 class_base_reg
[aligned_pair_class
] = 0;
185 class_reg_count
[aligned_pair_class
] = 0;
186 int start
= (this->first_non_payload_grf
& 1) ? 1 : 0;
188 for (int i
= 0; i
< reg_count
; i
++) {
189 ra_class_add_reg(regs
, classes
[aligned_pair_class
],
190 class_base_reg
[unaligned_pair_class
] + i
* 2 + start
);
195 ra_set_finalize(regs
);
197 struct ra_graph
*g
= ra_alloc_interference_graph(regs
,
198 this->virtual_grf_next
);
199 /* Node 0 is just a placeholder to keep virtual_grf[] mapping 1:1
202 ra_set_node_class(g
, 0, classes
[0]);
204 for (int i
= 1; i
< this->virtual_grf_next
; i
++) {
205 for (int c
= 0; c
< class_count
; c
++) {
206 if (class_sizes
[c
] == this->virtual_grf_sizes
[i
]) {
207 if (aligned_pair_class
>= 0 &&
208 this->delta_x
.reg
== i
) {
209 ra_set_node_class(g
, i
, classes
[aligned_pair_class
]);
211 ra_set_node_class(g
, i
, classes
[c
]);
217 for (int j
= 1; j
< i
; j
++) {
218 if (virtual_grf_interferes(i
, j
)) {
219 ra_add_node_interference(g
, i
, j
);
224 if (!ra_allocate_no_spills(g
)) {
225 /* Failed to allocate registers. Spill a reg, and the caller will
226 * loop back into here to try again.
228 int reg
= choose_spill_reg(g
);
229 if (reg
== -1 || intel
->gen
>= 6) {
242 /* Get the chosen virtual registers for each node, and map virtual
243 * regs in the register classes back down to real hardware reg
246 hw_reg_mapping
[0] = 0; /* unused */
247 for (int i
= 1; i
< this->virtual_grf_next
; i
++) {
248 int reg
= ra_get_node_reg(g
, i
);
251 for (int c
= 0; c
< class_count
; c
++) {
252 if (reg
>= class_base_reg
[c
] &&
253 reg
< class_base_reg
[c
] + class_reg_count
[c
]) {
254 hw_reg
= reg
- class_base_reg
[c
];
260 hw_reg_mapping
[i
] = this->first_non_payload_grf
+ hw_reg
;
261 last_grf
= MAX2(last_grf
,
262 hw_reg_mapping
[i
] + this->virtual_grf_sizes
[i
] - 1);
265 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
266 fs_inst
*inst
= (fs_inst
*)iter
.get();
268 assign_reg(hw_reg_mapping
, &inst
->dst
);
269 assign_reg(hw_reg_mapping
, &inst
->src
[0]);
270 assign_reg(hw_reg_mapping
, &inst
->src
[1]);
273 this->grf_used
= last_grf
+ 1;
282 fs_visitor::emit_unspill(fs_inst
*inst
, fs_reg dst
, uint32_t spill_offset
)
284 int size
= virtual_grf_sizes
[dst
.reg
];
287 for (int chan
= 0; chan
< size
; chan
++) {
288 fs_inst
*unspill_inst
= new(mem_ctx
) fs_inst(FS_OPCODE_UNSPILL
,
291 unspill_inst
->offset
= spill_offset
+ chan
* REG_SIZE
;
292 unspill_inst
->ir
= inst
->ir
;
293 unspill_inst
->annotation
= inst
->annotation
;
295 /* Choose a MRF that won't conflict with an MRF that's live across the
296 * spill. Nothing else will make it up to MRF 14/15.
298 unspill_inst
->base_mrf
= 14;
299 unspill_inst
->mlen
= 1; /* header contains offset */
300 inst
->insert_before(unspill_inst
);
305 fs_visitor::choose_spill_reg(struct ra_graph
*g
)
307 float loop_scale
= 1.0;
308 float spill_costs
[this->virtual_grf_next
];
309 bool no_spill
[this->virtual_grf_next
];
311 for (int i
= 0; i
< this->virtual_grf_next
; i
++) {
312 spill_costs
[i
] = 0.0;
316 /* Calculate costs for spilling nodes. Call it a cost of 1 per
317 * spill/unspill we'll have to do, and guess that the insides of
318 * loops run 10 times.
320 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
321 fs_inst
*inst
= (fs_inst
*)iter
.get();
323 for (unsigned int i
= 0; i
< 3; i
++) {
324 if (inst
->src
[i
].file
== GRF
) {
325 int size
= virtual_grf_sizes
[inst
->src
[i
].reg
];
326 spill_costs
[inst
->src
[i
].reg
] += size
* loop_scale
;
330 if (inst
->dst
.file
== GRF
) {
331 int size
= virtual_grf_sizes
[inst
->dst
.reg
];
332 spill_costs
[inst
->dst
.reg
] += size
* loop_scale
;
335 switch (inst
->opcode
) {
341 case BRW_OPCODE_WHILE
:
345 case FS_OPCODE_SPILL
:
346 if (inst
->src
[0].file
== GRF
)
347 no_spill
[inst
->src
[0].reg
] = true;
350 case FS_OPCODE_UNSPILL
:
351 if (inst
->dst
.file
== GRF
)
352 no_spill
[inst
->dst
.reg
] = true;
357 for (int i
= 0; i
< this->virtual_grf_next
; i
++) {
359 ra_set_node_spill_cost(g
, i
, spill_costs
[i
]);
362 return ra_get_best_spill_node(g
);
366 fs_visitor::spill_reg(int spill_reg
)
368 int size
= virtual_grf_sizes
[spill_reg
];
369 unsigned int spill_offset
= c
->last_scratch
;
370 assert(ALIGN(spill_offset
, 16) == spill_offset
); /* oword read/write req. */
371 c
->last_scratch
+= size
* REG_SIZE
;
373 /* Generate spill/unspill instructions for the objects being
374 * spilled. Right now, we spill or unspill the whole thing to a
375 * virtual grf of the same size. For most instructions, though, we
376 * could just spill/unspill the GRF being accessed.
378 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
379 fs_inst
*inst
= (fs_inst
*)iter
.get();
381 for (unsigned int i
= 0; i
< 3; i
++) {
382 if (inst
->src
[i
].file
== GRF
&&
383 inst
->src
[i
].reg
== spill_reg
) {
384 inst
->src
[i
].reg
= virtual_grf_alloc(size
);
385 emit_unspill(inst
, inst
->src
[i
], spill_offset
);
389 if (inst
->dst
.file
== GRF
&&
390 inst
->dst
.reg
== spill_reg
) {
391 inst
->dst
.reg
= virtual_grf_alloc(size
);
393 /* Since we spill/unspill the whole thing even if we access
394 * just a component, we may need to unspill before the
395 * instruction we're spilling for.
397 if (size
!= 1 || inst
->predicated
) {
398 emit_unspill(inst
, inst
->dst
, spill_offset
);
401 fs_reg spill_src
= inst
->dst
;
402 spill_src
.reg_offset
= 0;
403 spill_src
.abs
= false;
404 spill_src
.negate
= false;
405 spill_src
.smear
= -1;
407 for (int chan
= 0; chan
< size
; chan
++) {
408 fs_inst
*spill_inst
= new(mem_ctx
) fs_inst(FS_OPCODE_SPILL
,
409 reg_null_f
, spill_src
);
410 spill_src
.reg_offset
++;
411 spill_inst
->offset
= spill_offset
+ chan
* REG_SIZE
;
412 spill_inst
->ir
= inst
->ir
;
413 spill_inst
->annotation
= inst
->annotation
;
414 spill_inst
->base_mrf
= 14;
415 spill_inst
->mlen
= 2; /* header, value */
416 inst
->insert_after(spill_inst
);
421 this->live_intervals_valid
= false;