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 "glsl/glsl_types.h"
31 #include "glsl/ir_optimization.h"
34 assign_reg(unsigned *reg_hw_locations
, fs_reg
*reg
)
36 if (reg
->file
== GRF
) {
37 assert(reg
->reg_offset
>= 0);
38 reg
->reg
= reg_hw_locations
[reg
->reg
] + reg
->reg_offset
;
44 fs_visitor::assign_regs_trivial()
46 unsigned hw_reg_mapping
[this->alloc
.count
+ 1];
48 int reg_width
= dispatch_width
/ 8;
50 /* Note that compressed instructions require alignment to 2 registers. */
51 hw_reg_mapping
[0] = ALIGN(this->first_non_payload_grf
, reg_width
);
52 for (i
= 1; i
<= this->alloc
.count
; i
++) {
53 hw_reg_mapping
[i
] = (hw_reg_mapping
[i
- 1] +
54 this->alloc
.sizes
[i
- 1]);
56 this->grf_used
= hw_reg_mapping
[this->alloc
.count
];
58 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
59 assign_reg(hw_reg_mapping
, &inst
->dst
);
60 for (i
= 0; i
< inst
->sources
; i
++) {
61 assign_reg(hw_reg_mapping
, &inst
->src
[i
]);
65 if (this->grf_used
>= max_grf
) {
66 fail("Ran out of regs on trivial allocator (%d/%d)\n",
67 this->grf_used
, max_grf
);
69 this->alloc
.count
= this->grf_used
;
75 brw_alloc_reg_set(struct intel_screen
*screen
, int reg_width
)
77 const struct brw_device_info
*devinfo
= screen
->devinfo
;
78 int base_reg_count
= BRW_MAX_GRF
;
79 int index
= reg_width
- 1;
81 /* The registers used to make up almost all values handled in the compiler
82 * are a scalar value occupying a single register (or 2 registers in the
83 * case of SIMD16, which is handled by dividing base_reg_count by 2 and
84 * multiplying allocated register numbers by 2). Things that were
85 * aggregates of scalar values at the GLSL level were split to scalar
86 * values by split_virtual_grfs().
88 * However, texture SEND messages return a series of contiguous registers
89 * to write into. We currently always ask for 4 registers, but we may
90 * convert that to use less some day.
92 * Additionally, on gen5 we need aligned pairs of registers for the PLN
93 * instruction, and on gen4 we need 8 contiguous regs for workaround simd16
96 * So we have a need for classes for 1, 2, 4, and 8 registers currently,
97 * and we add in '3' to make indexing the array easier for the common case
98 * (since we'll probably want it for texturing later).
100 * And, on gen7 and newer, we do texturing SEND messages from GRFs, which
101 * means that we may need any size up to the sampler message size limit (11
105 int class_sizes
[MAX_VGRF_SIZE
];
107 if (devinfo
->gen
>= 7) {
108 for (class_count
= 0; class_count
< MAX_VGRF_SIZE
; class_count
++)
109 class_sizes
[class_count
] = class_count
+ 1;
111 for (class_count
= 0; class_count
< 4; class_count
++)
112 class_sizes
[class_count
] = class_count
+ 1;
113 class_sizes
[class_count
++] = 8;
116 memset(screen
->wm_reg_sets
[index
].class_to_ra_reg_range
, 0,
117 sizeof(screen
->wm_reg_sets
[index
].class_to_ra_reg_range
));
118 int *class_to_ra_reg_range
= screen
->wm_reg_sets
[index
].class_to_ra_reg_range
;
120 /* Compute the total number of registers across all classes. */
121 int ra_reg_count
= 0;
122 for (int i
= 0; i
< class_count
; i
++) {
123 if (devinfo
->gen
<= 5 && reg_width
== 2) {
126 * In order to reduce the hardware complexity, the following
127 * rules and restrictions apply to the compressed instruction:
129 * * Operand Alignment Rule: With the exceptions listed below, a
130 * source/destination operand in general should be aligned to
131 * even 256-bit physical register with a region size equal to
132 * two 256-bit physical register
134 ra_reg_count
+= (base_reg_count
- (class_sizes
[i
] - 1)) / 2;
136 ra_reg_count
+= base_reg_count
- (class_sizes
[i
] - 1);
138 /* Mark the last register. We'll fill in the beginnings later. */
139 class_to_ra_reg_range
[class_sizes
[i
]] = ra_reg_count
;
142 /* Fill out the rest of the range markers */
143 for (int i
= 1; i
< 17; ++i
) {
144 if (class_to_ra_reg_range
[i
] == 0)
145 class_to_ra_reg_range
[i
] = class_to_ra_reg_range
[i
-1];
148 uint8_t *ra_reg_to_grf
= ralloc_array(screen
, uint8_t, ra_reg_count
);
149 struct ra_regs
*regs
= ra_alloc_reg_set(screen
, ra_reg_count
);
150 if (devinfo
->gen
>= 6)
151 ra_set_allocate_round_robin(regs
);
152 int *classes
= ralloc_array(screen
, int, class_count
);
153 int aligned_pairs_class
= -1;
155 /* Allocate space for q values. We allocate class_count + 1 because we
156 * want to leave room for the aligned pairs class if we have it. */
157 unsigned int **q_values
= ralloc_array(screen
, unsigned int *,
159 for (int i
= 0; i
< class_count
+ 1; ++i
)
160 q_values
[i
] = ralloc_array(q_values
, unsigned int, class_count
+ 1);
162 /* Now, add the registers to their classes, and add the conflicts
163 * between them and the base GRF registers (and also each other).
166 int pairs_base_reg
= 0;
167 int pairs_reg_count
= 0;
168 for (int i
= 0; i
< class_count
; i
++) {
170 if (devinfo
->gen
<= 5 && reg_width
== 2) {
171 class_reg_count
= (base_reg_count
- (class_sizes
[i
] - 1)) / 2;
173 /* See comment below. The only difference here is that we are
174 * dealing with pairs of registers instead of single registers.
175 * Registers of odd sizes simply get rounded up. */
176 for (int j
= 0; j
< class_count
; j
++)
177 q_values
[i
][j
] = (class_sizes
[i
] + 1) / 2 +
178 (class_sizes
[j
] + 1) / 2 - 1;
180 class_reg_count
= base_reg_count
- (class_sizes
[i
] - 1);
182 /* From register_allocate.c:
184 * q(B,C) (indexed by C, B is this register class) in
185 * Runeson/Nyström paper. This is "how many registers of B could
186 * the worst choice register from C conflict with".
188 * If we just let the register allocation algorithm compute these
189 * values, is extremely expensive. However, since all of our
190 * registers are laid out, we can very easily compute them
191 * ourselves. View the register from C as fixed starting at GRF n
192 * somwhere in the middle, and the register from B as sliding back
193 * and forth. Then the first register to conflict from B is the
194 * one starting at n - class_size[B] + 1 and the last register to
195 * conflict will start at n + class_size[B] - 1. Therefore, the
196 * number of conflicts from B is class_size[B] + class_size[C] - 1.
198 * +-+-+-+-+-+-+ +-+-+-+-+-+-+
199 * B | | | | | |n| --> | | | | | | |
200 * +-+-+-+-+-+-+ +-+-+-+-+-+-+
205 for (int j
= 0; j
< class_count
; j
++)
206 q_values
[i
][j
] = class_sizes
[i
] + class_sizes
[j
] - 1;
208 classes
[i
] = ra_alloc_reg_class(regs
);
210 /* Save this off for the aligned pair class at the end. */
211 if (class_sizes
[i
] == 2) {
212 pairs_base_reg
= reg
;
213 pairs_reg_count
= class_reg_count
;
216 if (devinfo
->gen
<= 5 && reg_width
== 2) {
217 for (int j
= 0; j
< class_reg_count
; j
++) {
218 ra_class_add_reg(regs
, classes
[i
], reg
);
220 ra_reg_to_grf
[reg
] = j
* 2;
222 for (int base_reg
= j
;
223 base_reg
< j
+ (class_sizes
[i
] + 1) / 2;
225 ra_add_transitive_reg_conflict(regs
, base_reg
, reg
);
231 for (int j
= 0; j
< class_reg_count
; j
++) {
232 ra_class_add_reg(regs
, classes
[i
], reg
);
234 ra_reg_to_grf
[reg
] = j
;
236 for (int base_reg
= j
;
237 base_reg
< j
+ class_sizes
[i
];
239 ra_add_transitive_reg_conflict(regs
, base_reg
, reg
);
246 assert(reg
== ra_reg_count
);
248 /* Add a special class for aligned pairs, which we'll put delta_x/y
249 * in on gen5 so that we can do PLN.
251 if (devinfo
->has_pln
&& reg_width
== 1 && devinfo
->gen
< 6) {
252 aligned_pairs_class
= ra_alloc_reg_class(regs
);
254 for (int i
= 0; i
< pairs_reg_count
; i
++) {
255 if ((ra_reg_to_grf
[pairs_base_reg
+ i
] & 1) == 0) {
256 ra_class_add_reg(regs
, aligned_pairs_class
, pairs_base_reg
+ i
);
260 for (int i
= 0; i
< class_count
; i
++) {
261 /* These are a little counter-intuitive because the pair registers
262 * are required to be aligned while the register they are
263 * potentially interferring with are not. In the case where the
264 * size is even, the worst-case is that the register is
265 * odd-aligned. In the odd-size case, it doesn't matter.
267 q_values
[class_count
][i
] = class_sizes
[i
] / 2 + 1;
268 q_values
[i
][class_count
] = class_sizes
[i
] + 1;
270 q_values
[class_count
][class_count
] = 1;
273 ra_set_finalize(regs
, q_values
);
275 ralloc_free(q_values
);
277 screen
->wm_reg_sets
[index
].regs
= regs
;
278 for (unsigned i
= 0; i
< ARRAY_SIZE(screen
->wm_reg_sets
[index
].classes
); i
++)
279 screen
->wm_reg_sets
[index
].classes
[i
] = -1;
280 for (int i
= 0; i
< class_count
; i
++)
281 screen
->wm_reg_sets
[index
].classes
[class_sizes
[i
] - 1] = classes
[i
];
282 screen
->wm_reg_sets
[index
].ra_reg_to_grf
= ra_reg_to_grf
;
283 screen
->wm_reg_sets
[index
].aligned_pairs_class
= aligned_pairs_class
;
287 brw_fs_alloc_reg_sets(struct intel_screen
*screen
)
289 brw_alloc_reg_set(screen
, 1);
290 brw_alloc_reg_set(screen
, 2);
294 count_to_loop_end(const bblock_t
*block
)
296 if (block
->end()->opcode
== BRW_OPCODE_WHILE
)
297 return block
->end_ip
;
300 /* Skip the first block, since we don't want to count the do the calling
303 for (block
= block
->next();
305 block
= block
->next()) {
306 if (block
->start()->opcode
== BRW_OPCODE_DO
)
308 if (block
->end()->opcode
== BRW_OPCODE_WHILE
) {
311 return block
->end_ip
;
314 unreachable("not reached");
318 * Sets up interference between thread payload registers and the virtual GRFs
319 * to be allocated for program temporaries.
321 * We want to be able to reallocate the payload for our virtual GRFs, notably
322 * because the setup coefficients for a full set of 16 FS inputs takes up 8 of
325 * The layout of the payload registers is:
327 * 0..payload.num_regs-1: fixed function setup (including bary coordinates).
328 * payload.num_regs..payload.num_regs+curb_read_lengh-1: uniform data
329 * payload.num_regs+curb_read_lengh..first_non_payload_grf-1: setup coefficients.
331 * And we have payload_node_count nodes covering these registers in order
332 * (note that in SIMD16, a node is two registers).
335 fs_visitor::setup_payload_interference(struct ra_graph
*g
,
336 int payload_node_count
,
337 int first_payload_node
)
342 int payload_last_use_ip
[payload_node_count
];
343 memset(payload_last_use_ip
, 0, sizeof(payload_last_use_ip
));
345 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
346 switch (inst
->opcode
) {
350 /* Since payload regs are deffed only at the start of the shader
351 * execution, any uses of the payload within a loop mean the live
352 * interval extends to the end of the outermost loop. Find the ip of
356 loop_end_ip
= count_to_loop_end(block
);
358 case BRW_OPCODE_WHILE
:
367 use_ip
= loop_end_ip
;
371 /* Note that UNIFORM args have been turned into FIXED_HW_REG by
372 * assign_curbe_setup(), and interpolation uses fixed hardware regs from
373 * the start (see interp_reg()).
375 for (int i
= 0; i
< inst
->sources
; i
++) {
376 if (inst
->src
[i
].file
== HW_REG
&&
377 inst
->src
[i
].fixed_hw_reg
.file
== BRW_GENERAL_REGISTER_FILE
) {
378 int node_nr
= inst
->src
[i
].fixed_hw_reg
.nr
;
379 if (node_nr
>= payload_node_count
)
382 payload_last_use_ip
[node_nr
] = use_ip
;
386 /* Special case instructions which have extra implied registers used. */
387 switch (inst
->opcode
) {
388 case FS_OPCODE_LINTERP
:
389 /* On gen6+ in SIMD16, there are 4 adjacent registers used by
390 * PLN's sourcing of the deltas, while we list only the first one
391 * in the arguments. Pre-gen6, the deltas are computed in normal
396 if (inst
->src
[delta_x_arg
].file
== HW_REG
&&
397 inst
->src
[delta_x_arg
].fixed_hw_reg
.file
==
398 BRW_GENERAL_REGISTER_FILE
) {
399 for (int i
= 1; i
< 4; ++i
) {
400 int node
= inst
->src
[delta_x_arg
].fixed_hw_reg
.nr
+ i
;
401 assert(node
< payload_node_count
);
402 payload_last_use_ip
[node
] = use_ip
;
410 /* We could omit this for the !inst->header_present case, except
411 * that the simulator apparently incorrectly reads from g0/g1
412 * instead of sideband. It also really freaks out driver
413 * developers to see g0 used in unusual places, so just always
416 payload_last_use_ip
[0] = use_ip
;
417 payload_last_use_ip
[1] = use_ip
;
425 for (int i
= 0; i
< payload_node_count
; i
++) {
426 /* Mark the payload node as interfering with any virtual grf that is
427 * live between the start of the program and our last use of the payload
430 for (unsigned j
= 0; j
< this->alloc
.count
; j
++) {
431 /* Note that we use a <= comparison, unlike virtual_grf_interferes(),
432 * in order to not have to worry about the uniform issue described in
433 * calculate_live_intervals().
435 if (this->virtual_grf_start
[j
] <= payload_last_use_ip
[i
]) {
436 ra_add_node_interference(g
, first_payload_node
+ i
, j
);
441 for (int i
= 0; i
< payload_node_count
; i
++) {
442 /* Mark each payload node as being allocated to its physical register.
444 * The alternative would be to have per-physical-register classes, which
445 * would just be silly.
447 if (brw
->intelScreen
->devinfo
->gen
<= 5 && dispatch_width
== 16) {
448 /* We have to divide by 2 here because we only have even numbered
449 * registers. Some of the payload registers will be odd, but
450 * that's ok because their physical register numbers have already
451 * been assigned. The only thing this is used for is interference.
453 ra_set_node_reg(g
, first_payload_node
+ i
, i
/ 2);
455 ra_set_node_reg(g
, first_payload_node
+ i
, i
);
461 * Sets the mrf_used array to indicate which MRFs are used by the shader IR
463 * This is used in assign_regs() to decide which of the GRFs that we use as
464 * MRFs on gen7 get normally register allocated, and in register spilling to
465 * see if we can actually use MRFs to do spills without overwriting normal MRF
469 fs_visitor::get_used_mrfs(bool *mrf_used
)
471 int reg_width
= dispatch_width
/ 8;
473 memset(mrf_used
, 0, BRW_MAX_MRF
* sizeof(bool));
475 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
476 if (inst
->dst
.file
== MRF
) {
477 int reg
= inst
->dst
.reg
& ~BRW_MRF_COMPR4
;
478 mrf_used
[reg
] = true;
479 if (reg_width
== 2) {
480 if (inst
->dst
.reg
& BRW_MRF_COMPR4
) {
481 mrf_used
[reg
+ 4] = true;
483 mrf_used
[reg
+ 1] = true;
488 if (inst
->mlen
> 0) {
489 for (int i
= 0; i
< implied_mrf_writes(inst
); i
++) {
490 mrf_used
[inst
->base_mrf
+ i
] = true;
497 * Sets interference between virtual GRFs and usage of the high GRFs for SEND
498 * messages (treated as MRFs in code generation).
501 fs_visitor::setup_mrf_hack_interference(struct ra_graph
*g
, int first_mrf_node
)
503 bool mrf_used
[BRW_MAX_MRF
];
504 get_used_mrfs(mrf_used
);
506 for (int i
= 0; i
< BRW_MAX_MRF
; i
++) {
507 /* Mark each MRF reg node as being allocated to its physical register.
509 * The alternative would be to have per-physical-register classes, which
510 * would just be silly.
512 ra_set_node_reg(g
, first_mrf_node
+ i
, GEN7_MRF_HACK_START
+ i
);
514 /* Since we don't have any live/dead analysis on the MRFs, just mark all
515 * that are used as conflicting with all virtual GRFs.
518 for (unsigned j
= 0; j
< this->alloc
.count
; j
++) {
519 ra_add_node_interference(g
, first_mrf_node
+ i
, j
);
526 fs_visitor::assign_regs(bool allow_spilling
)
528 struct intel_screen
*screen
= brw
->intelScreen
;
529 /* Most of this allocation was written for a reg_width of 1
530 * (dispatch_width == 8). In extending to SIMD16, the code was
531 * left in place and it was converted to have the hardware
532 * registers it's allocating be contiguous physical pairs of regs
533 * for reg_width == 2.
535 int reg_width
= dispatch_width
/ 8;
536 unsigned hw_reg_mapping
[this->alloc
.count
];
537 int payload_node_count
= ALIGN(this->first_non_payload_grf
, reg_width
);
538 int rsi
= reg_width
- 1; /* Which screen->wm_reg_sets[] to use */
539 calculate_live_intervals();
541 int node_count
= this->alloc
.count
;
542 int first_payload_node
= node_count
;
543 node_count
+= payload_node_count
;
544 int first_mrf_hack_node
= node_count
;
546 node_count
+= BRW_MAX_GRF
- GEN7_MRF_HACK_START
;
548 ra_alloc_interference_graph(screen
->wm_reg_sets
[rsi
].regs
, node_count
);
550 for (unsigned i
= 0; i
< this->alloc
.count
; i
++) {
551 unsigned size
= this->alloc
.sizes
[i
];
554 assert(size
<= ARRAY_SIZE(screen
->wm_reg_sets
[rsi
].classes
) &&
555 "Register allocation relies on split_virtual_grfs()");
556 c
= screen
->wm_reg_sets
[rsi
].classes
[size
- 1];
558 /* Special case: on pre-GEN6 hardware that supports PLN, the
559 * second operand of a PLN instruction needs to be an
560 * even-numbered register, so we have a special register class
561 * wm_aligned_pairs_class to handle this case. pre-GEN6 always
562 * uses this->delta_x[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC] as the
563 * second operand of a PLN instruction (since it doesn't support
564 * any other interpolation modes). So all we need to do is find
565 * that register and set it to the appropriate class.
567 if (screen
->wm_reg_sets
[rsi
].aligned_pairs_class
>= 0 &&
568 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
].file
== GRF
&&
569 this->delta_x
[BRW_WM_PERSPECTIVE_PIXEL_BARYCENTRIC
].reg
== i
) {
570 c
= screen
->wm_reg_sets
[rsi
].aligned_pairs_class
;
573 ra_set_node_class(g
, i
, c
);
575 for (unsigned j
= 0; j
< i
; j
++) {
576 if (virtual_grf_interferes(i
, j
)) {
577 ra_add_node_interference(g
, i
, j
);
582 setup_payload_interference(g
, payload_node_count
, first_payload_node
);
584 setup_mrf_hack_interference(g
, first_mrf_hack_node
);
586 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
587 /* When we do send-from-GRF for FB writes, we need to ensure that
588 * the last write instruction sends from a high register. This is
589 * because the vertex fetcher wants to start filling the low
590 * payload registers while the pixel data port is still working on
591 * writing out the memory. If we don't do this, we get rendering
594 * We could just do "something high". Instead, we just pick the
595 * highest register that works.
598 int size
= alloc
.sizes
[inst
->src
[0].reg
];
599 int reg
= screen
->wm_reg_sets
[rsi
].class_to_ra_reg_range
[size
] - 1;
600 ra_set_node_reg(g
, inst
->src
[0].reg
, reg
);
606 if (dispatch_width
> 8) {
607 /* In 16-wide dispatch we have an issue where a compressed
608 * instruction is actually two instructions executed simultaneiously.
609 * It's actually ok to have the source and destination registers be
610 * the same. In this case, each instruction over-writes its own
611 * source and there's no problem. The real problem here is if the
612 * source and destination registers are off by one. Then you can end
613 * up in a scenario where the first instruction over-writes the
614 * source of the second instruction. Since the compiler doesn't know
615 * about this level of granularity, we simply make the source and
616 * destination interfere.
618 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
619 if (inst
->dst
.file
!= GRF
)
622 for (int i
= 0; i
< inst
->sources
; ++i
) {
623 if (inst
->src
[i
].file
== GRF
) {
624 ra_add_node_interference(g
, inst
->dst
.reg
, inst
->src
[i
].reg
);
630 /* Debug of register spilling: Go spill everything. */
632 int reg
= choose_spill_reg(g
);
641 if (!ra_allocate(g
)) {
642 /* Failed to allocate registers. Spill a reg, and the caller will
643 * loop back into here to try again.
645 int reg
= choose_spill_reg(g
);
648 fail("no register to spill:\n");
649 dump_instructions(NULL
);
650 } else if (allow_spilling
) {
659 /* Get the chosen virtual registers for each node, and map virtual
660 * regs in the register classes back down to real hardware reg
663 this->grf_used
= payload_node_count
;
664 for (unsigned i
= 0; i
< this->alloc
.count
; i
++) {
665 int reg
= ra_get_node_reg(g
, i
);
667 hw_reg_mapping
[i
] = screen
->wm_reg_sets
[rsi
].ra_reg_to_grf
[reg
];
668 this->grf_used
= MAX2(this->grf_used
,
669 hw_reg_mapping
[i
] + this->alloc
.sizes
[i
]);
672 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
673 assign_reg(hw_reg_mapping
, &inst
->dst
);
674 for (int i
= 0; i
< inst
->sources
; i
++) {
675 assign_reg(hw_reg_mapping
, &inst
->src
[i
]);
679 this->alloc
.count
= this->grf_used
;
687 fs_visitor::emit_unspill(bblock_t
*block
, fs_inst
*inst
, fs_reg dst
,
688 uint32_t spill_offset
, int count
)
691 if (dispatch_width
== 16 && count
% 2 == 0) {
696 for (int i
= 0; i
< count
/ reg_size
; i
++) {
697 /* The gen7 descriptor-based offset is 12 bits of HWORD units. */
698 bool gen7_read
= brw
->gen
>= 7 && spill_offset
< (1 << 12) * REG_SIZE
;
700 fs_inst
*unspill_inst
=
701 new(mem_ctx
) fs_inst(gen7_read
?
702 SHADER_OPCODE_GEN7_SCRATCH_READ
:
703 SHADER_OPCODE_GEN4_SCRATCH_READ
,
705 unspill_inst
->offset
= spill_offset
;
706 unspill_inst
->ir
= inst
->ir
;
707 unspill_inst
->annotation
= inst
->annotation
;
708 unspill_inst
->regs_written
= reg_size
;
711 unspill_inst
->base_mrf
= 14;
712 unspill_inst
->mlen
= 1; /* header contains offset */
714 inst
->insert_before(block
, unspill_inst
);
716 dst
.reg_offset
+= reg_size
;
717 spill_offset
+= reg_size
* REG_SIZE
;
722 fs_visitor::emit_spill(bblock_t
*block
, fs_inst
*inst
, fs_reg src
,
723 uint32_t spill_offset
, int count
)
726 int spill_base_mrf
= 14;
727 if (dispatch_width
== 16 && count
% 2 == 0) {
732 for (int i
= 0; i
< count
/ reg_size
; i
++) {
733 fs_inst
*spill_inst
=
734 new(mem_ctx
) fs_inst(SHADER_OPCODE_GEN4_SCRATCH_WRITE
,
735 reg_size
* 8, reg_null_f
, src
);
736 src
.reg_offset
+= reg_size
;
737 spill_inst
->offset
= spill_offset
+ i
* reg_size
* REG_SIZE
;
738 spill_inst
->ir
= inst
->ir
;
739 spill_inst
->annotation
= inst
->annotation
;
740 spill_inst
->mlen
= 1 + reg_size
; /* header, value */
741 spill_inst
->base_mrf
= spill_base_mrf
;
742 inst
->insert_after(block
, spill_inst
);
747 fs_visitor::choose_spill_reg(struct ra_graph
*g
)
749 float loop_scale
= 1.0;
750 float spill_costs
[this->alloc
.count
];
751 bool no_spill
[this->alloc
.count
];
753 for (unsigned i
= 0; i
< this->alloc
.count
; i
++) {
754 spill_costs
[i
] = 0.0;
758 /* Calculate costs for spilling nodes. Call it a cost of 1 per
759 * spill/unspill we'll have to do, and guess that the insides of
760 * loops run 10 times.
762 foreach_block_and_inst(block
, fs_inst
, inst
, cfg
) {
763 for (unsigned int i
= 0; i
< inst
->sources
; i
++) {
764 if (inst
->src
[i
].file
== GRF
) {
765 spill_costs
[inst
->src
[i
].reg
] += loop_scale
;
767 /* Register spilling logic assumes full-width registers; smeared
768 * registers have a width of 1 so if we try to spill them we'll
769 * generate invalid assembly. This shouldn't be a problem because
770 * smeared registers are only used as short-term temporaries when
771 * loading pull constants, so spilling them is unlikely to reduce
772 * register pressure anyhow.
774 if (!inst
->src
[i
].is_contiguous()) {
775 no_spill
[inst
->src
[i
].reg
] = true;
780 if (inst
->dst
.file
== GRF
) {
781 spill_costs
[inst
->dst
.reg
] += inst
->regs_written
* loop_scale
;
783 if (!inst
->dst
.is_contiguous()) {
784 no_spill
[inst
->dst
.reg
] = true;
788 switch (inst
->opcode
) {
794 case BRW_OPCODE_WHILE
:
798 case SHADER_OPCODE_GEN4_SCRATCH_WRITE
:
799 if (inst
->src
[0].file
== GRF
)
800 no_spill
[inst
->src
[0].reg
] = true;
803 case SHADER_OPCODE_GEN4_SCRATCH_READ
:
804 case SHADER_OPCODE_GEN7_SCRATCH_READ
:
805 if (inst
->dst
.file
== GRF
)
806 no_spill
[inst
->dst
.reg
] = true;
814 for (unsigned i
= 0; i
< this->alloc
.count
; i
++) {
816 ra_set_node_spill_cost(g
, i
, spill_costs
[i
]);
819 return ra_get_best_spill_node(g
);
823 fs_visitor::spill_reg(int spill_reg
)
825 int size
= alloc
.sizes
[spill_reg
];
826 unsigned int spill_offset
= last_scratch
;
827 assert(ALIGN(spill_offset
, 16) == spill_offset
); /* oword read/write req. */
828 int spill_base_mrf
= dispatch_width
> 8 ? 13 : 14;
830 /* Spills may use MRFs 13-15 in the SIMD16 case. Our texturing is done
831 * using up to 11 MRFs starting from either m1 or m2, and fb writes can use
832 * up to m13 (gen6+ simd16: 2 header + 8 color + 2 src0alpha + 2 omask) or
833 * m15 (gen4-5 simd16: 2 header + 8 color + 1 aads + 2 src depth + 2 dst
834 * depth), starting from m1. In summary: We may not be able to spill in
835 * SIMD16 mode, because we'd stomp the FB writes.
837 if (!spilled_any_registers
) {
838 bool mrf_used
[BRW_MAX_MRF
];
839 get_used_mrfs(mrf_used
);
841 for (int i
= spill_base_mrf
; i
< BRW_MAX_MRF
; i
++) {
843 fail("Register spilling not supported with m%d used", i
);
848 spilled_any_registers
= true;
851 last_scratch
+= size
* REG_SIZE
;
853 /* Generate spill/unspill instructions for the objects being
854 * spilled. Right now, we spill or unspill the whole thing to a
855 * virtual grf of the same size. For most instructions, though, we
856 * could just spill/unspill the GRF being accessed.
858 foreach_block_and_inst (block
, fs_inst
, inst
, cfg
) {
859 for (unsigned int i
= 0; i
< inst
->sources
; i
++) {
860 if (inst
->src
[i
].file
== GRF
&&
861 inst
->src
[i
].reg
== spill_reg
) {
862 int regs_read
= inst
->regs_read(i
);
863 int subset_spill_offset
= (spill_offset
+
864 REG_SIZE
* inst
->src
[i
].reg_offset
);
865 fs_reg
unspill_dst(GRF
, alloc
.allocate(regs_read
));
867 inst
->src
[i
].reg
= unspill_dst
.reg
;
868 inst
->src
[i
].reg_offset
= 0;
870 emit_unspill(block
, inst
, unspill_dst
, subset_spill_offset
,
875 if (inst
->dst
.file
== GRF
&&
876 inst
->dst
.reg
== spill_reg
) {
877 int subset_spill_offset
= (spill_offset
+
878 REG_SIZE
* inst
->dst
.reg_offset
);
879 fs_reg
spill_src(GRF
, alloc
.allocate(inst
->regs_written
));
881 inst
->dst
.reg
= spill_src
.reg
;
882 inst
->dst
.reg_offset
= 0;
884 /* If we're immediately spilling the register, we should not use
885 * destination dependency hints. Doing so will cause the GPU do
886 * try to read and write the register at the same time and may
889 inst
->no_dd_clear
= false;
890 inst
->no_dd_check
= false;
892 /* If our write is going to affect just part of the
893 * inst->regs_written(), then we need to unspill the destination
894 * since we write back out all of the regs_written().
896 if (inst
->is_partial_write())
897 emit_unspill(block
, inst
, spill_src
, subset_spill_offset
,
900 emit_spill(block
, inst
, spill_src
, subset_spill_offset
,
905 invalidate_live_intervals();