b5bfd00d5fe256a4074122a0556c887ff88d33f1
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"
47 #include "../glsl/glsl_types.h"
48 #include "../glsl/ir_optimization.h"
49 #include "../glsl/ir_print_visitor.h"
52 assign_reg(int *reg_hw_locations
, fs_reg
*reg
)
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
;
62 fs_visitor::assign_regs_trivial()
65 int hw_reg_mapping
[this->virtual_grf_next
];
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]);
74 last_grf
= hw_reg_mapping
[i
- 1] + this->virtual_grf_sizes
[i
- 1];
76 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
77 fs_inst
*inst
= (fs_inst
*)iter
.get();
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]);
84 this->grf_used
= last_grf
+ 1;
88 fs_visitor::assign_regs()
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
];
95 int aligned_pair_class
= -1;
97 /* Set up the register classes.
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
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.
110 class_sizes
[class_count
++] = 2;
112 for (int r
= 1; r
< this->virtual_grf_next
; r
++) {
115 for (i
= 0; i
< class_count
; i
++) {
116 if (class_sizes
[i
] == this->virtual_grf_sizes
[r
])
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");
125 class_sizes
[class_count
++] = this->virtual_grf_sizes
[r
];
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];
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
];
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
);
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
);
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].
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
]);
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
);
164 ra_add_reg_conflict(regs
,
165 class_base_reg
[i
] + i_r
,
166 class_base_reg
[c
] + c_r
);
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.
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);
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;
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
);
194 ra_set_finalize(regs
);
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
201 ra_set_node_class(g
, 0, classes
[0]);
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
]);
210 ra_set_node_class(g
, i
, classes
[c
]);
216 for (int j
= 1; j
< i
; j
++) {
217 if (virtual_grf_interferes(i
, j
)) {
218 ra_add_node_interference(g
, i
, j
);
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.
227 int reg
= choose_spill_reg(g
);
241 /* Get the chosen virtual registers for each node, and map virtual
242 * regs in the register classes back down to real hardware reg
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
);
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
];
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);
264 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
265 fs_inst
*inst
= (fs_inst
*)iter
.get();
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]);
272 this->grf_used
= last_grf
+ 1;
281 fs_visitor::emit_unspill(fs_inst
*inst
, fs_reg dst
, uint32_t spill_offset
)
283 int size
= virtual_grf_sizes
[dst
.reg
];
286 for (int chan
= 0; chan
< size
; chan
++) {
287 fs_inst
*unspill_inst
= new(mem_ctx
) fs_inst(FS_OPCODE_UNSPILL
,
290 unspill_inst
->offset
= spill_offset
+ chan
* REG_SIZE
;
291 unspill_inst
->ir
= inst
->ir
;
292 unspill_inst
->annotation
= inst
->annotation
;
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.
297 unspill_inst
->base_mrf
= 14;
298 unspill_inst
->mlen
= 1; /* header contains offset */
299 inst
->insert_before(unspill_inst
);
304 fs_visitor::choose_spill_reg(struct ra_graph
*g
)
306 float loop_scale
= 1.0;
307 float spill_costs
[this->virtual_grf_next
];
308 bool no_spill
[this->virtual_grf_next
];
310 for (int i
= 0; i
< this->virtual_grf_next
; i
++) {
311 spill_costs
[i
] = 0.0;
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.
319 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
320 fs_inst
*inst
= (fs_inst
*)iter
.get();
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
;
329 if (inst
->dst
.file
== GRF
) {
330 int size
= virtual_grf_sizes
[inst
->dst
.reg
];
331 spill_costs
[inst
->dst
.reg
] += size
* loop_scale
;
334 switch (inst
->opcode
) {
340 case BRW_OPCODE_WHILE
:
344 case FS_OPCODE_SPILL
:
345 if (inst
->src
[0].file
== GRF
)
346 no_spill
[inst
->src
[0].reg
] = true;
349 case FS_OPCODE_UNSPILL
:
350 if (inst
->dst
.file
== GRF
)
351 no_spill
[inst
->dst
.reg
] = true;
356 for (int i
= 0; i
< this->virtual_grf_next
; i
++) {
358 ra_set_node_spill_cost(g
, i
, spill_costs
[i
]);
361 return ra_get_best_spill_node(g
);
365 fs_visitor::spill_reg(int spill_reg
)
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
;
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.
377 foreach_iter(exec_list_iterator
, iter
, this->instructions
) {
378 fs_inst
*inst
= (fs_inst
*)iter
.get();
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
);
388 if (inst
->dst
.file
== GRF
&&
389 inst
->dst
.reg
== spill_reg
) {
390 inst
->dst
.reg
= virtual_grf_alloc(size
);
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.
396 if (size
!= 1 || inst
->predicated
) {
397 emit_unspill(inst
, inst
->dst
, spill_offset
);
400 fs_reg spill_src
= inst
->dst
;
401 spill_src
.reg_offset
= 0;
402 spill_src
.abs
= false;
403 spill_src
.negate
= false;
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
);