1 /*
2 * Copyright © 2010 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 #include "brw_eu.h"
29 #include "brw_fs.h"
30 #include "brw_builder.h"
31 #include "brw_cfg.h"
32 #include "util/set.h"
33 #include "util/register_allocate.h"
34
35 using namespace brw;
36
37 static void
assign_reg(const struct intel_device_info * devinfo,unsigned * reg_hw_locations,brw_reg * reg)38 assign_reg(const struct intel_device_info *devinfo,
39 unsigned *reg_hw_locations, brw_reg *reg)
40 {
41 if (reg->file == VGRF) {
42 reg->nr = reg_unit(devinfo) * reg_hw_locations[reg->nr] + reg->offset / REG_SIZE;
43 reg->offset %= REG_SIZE;
44 }
45 }
46
47 void
brw_assign_regs_trivial(fs_visitor & s)48 brw_assign_regs_trivial(fs_visitor &s)
49 {
50 const struct intel_device_info *devinfo = s.devinfo;
51 unsigned *hw_reg_mapping = ralloc_array(NULL, unsigned, s.alloc.count + 1);
52 unsigned i;
53 int reg_width = s.dispatch_width / 8;
54
55 /* Note that compressed instructions require alignment to 2 registers. */
56 hw_reg_mapping[0] = ALIGN(s.first_non_payload_grf, reg_width);
57 for (i = 1; i <= s.alloc.count; i++) {
58 hw_reg_mapping[i] = (hw_reg_mapping[i - 1] +
59 DIV_ROUND_UP(s.alloc.sizes[i - 1],
60 reg_unit(devinfo)));
61 }
62 s.grf_used = hw_reg_mapping[s.alloc.count];
63
64 foreach_block_and_inst(block, fs_inst, inst, s.cfg) {
65 assign_reg(devinfo, hw_reg_mapping, &inst->dst);
66 for (i = 0; i < inst->sources; i++) {
67 assign_reg(devinfo, hw_reg_mapping, &inst->src[i]);
68 }
69 }
70
71 if (s.grf_used >= BRW_MAX_GRF) {
72 s.fail("Ran out of regs on trivial allocator (%d/%d)\n",
73 s.grf_used, BRW_MAX_GRF);
74 } else {
75 s.alloc.count = s.grf_used;
76 }
77
78 ralloc_free(hw_reg_mapping);
79 }
80
81 extern "C" void
brw_fs_alloc_reg_sets(struct brw_compiler * compiler)82 brw_fs_alloc_reg_sets(struct brw_compiler *compiler)
83 {
84 const struct intel_device_info *devinfo = compiler->devinfo;
85 int base_reg_count = (devinfo->ver >= 30 ? XE3_MAX_GRF / reg_unit(devinfo) :
86 BRW_MAX_GRF);
87
88 /* The registers used to make up almost all values handled in the compiler
89 * are a scalar value occupying a single register (or 2 registers in the
90 * case of SIMD16, which is handled by dividing base_reg_count by 2 and
91 * multiplying allocated register numbers by 2). Things that were
92 * aggregates of scalar values at the GLSL level were split to scalar
93 * values by split_virtual_grfs().
94 *
95 * However, texture SEND messages return a series of contiguous registers
96 * to write into. We currently always ask for 4 registers, but we may
97 * convert that to use less some day.
98 *
99 * Additionally, on gfx5 we need aligned pairs of registers for the PLN
100 * instruction, and on gfx4 we need 8 contiguous regs for workaround simd16
101 * texturing.
102 */
103 assert(REG_CLASS_COUNT == MAX_VGRF_SIZE(devinfo) / reg_unit(devinfo));
104 int class_sizes[REG_CLASS_COUNT];
105 for (unsigned i = 0; i < REG_CLASS_COUNT; i++)
106 class_sizes[i] = i + 1;
107
108 struct ra_regs *regs = ra_alloc_reg_set(compiler, base_reg_count, false);
109 if (devinfo->ver < 30)
110 ra_set_allocate_round_robin(regs);
111 struct ra_class **classes = ralloc_array(compiler, struct ra_class *,
112 REG_CLASS_COUNT);
113
114 /* Now, make the register classes for each size of contiguous register
115 * allocation we might need to make.
116 */
117 for (int i = 0; i < REG_CLASS_COUNT; i++) {
118 classes[i] = ra_alloc_contig_reg_class(regs, class_sizes[i]);
119
120 for (int reg = 0; reg <= base_reg_count - class_sizes[i]; reg++)
121 ra_class_add_reg(classes[i], reg);
122 }
123
124 ra_set_finalize(regs, NULL);
125
126 compiler->reg_set.regs = regs;
127 for (unsigned i = 0; i < ARRAY_SIZE(compiler->reg_set.classes); i++)
128 compiler->reg_set.classes[i] = NULL;
129 for (int i = 0; i < REG_CLASS_COUNT; i++)
130 compiler->reg_set.classes[class_sizes[i] - 1] = classes[i];
131 }
132
133 static int
count_to_loop_end(const bblock_t * block)134 count_to_loop_end(const bblock_t *block)
135 {
136 if (block->end()->opcode == BRW_OPCODE_WHILE)
137 return block->end_ip;
138
139 int depth = 1;
140 /* Skip the first block, since we don't want to count the do the calling
141 * function found.
142 */
143 for (block = block->next();
144 depth > 0;
145 block = block->next()) {
146 if (block->start()->opcode == BRW_OPCODE_DO)
147 depth++;
148 if (block->end()->opcode == BRW_OPCODE_WHILE) {
149 depth--;
150 if (depth == 0)
151 return block->end_ip;
152 }
153 }
154 unreachable("not reached");
155 }
156
calculate_payload_ranges(bool allow_spilling,unsigned payload_node_count,int * payload_last_use_ip) const157 void fs_visitor::calculate_payload_ranges(bool allow_spilling,
158 unsigned payload_node_count,
159 int *payload_last_use_ip) const
160 {
161 int loop_depth = 0;
162 int loop_end_ip = 0;
163
164 for (unsigned i = 0; i < payload_node_count; i++)
165 payload_last_use_ip[i] = -1;
166
167 int ip = 0;
168 foreach_block_and_inst(block, fs_inst, inst, cfg) {
169 switch (inst->opcode) {
170 case BRW_OPCODE_DO:
171 loop_depth++;
172
173 /* Since payload regs are deffed only at the start of the shader
174 * execution, any uses of the payload within a loop mean the live
175 * interval extends to the end of the outermost loop. Find the ip of
176 * the end now.
177 */
178 if (loop_depth == 1)
179 loop_end_ip = count_to_loop_end(block);
180 break;
181 case BRW_OPCODE_WHILE:
182 loop_depth--;
183 break;
184 default:
185 break;
186 }
187
188 int use_ip;
189 if (loop_depth > 0)
190 use_ip = loop_end_ip;
191 else
192 use_ip = ip;
193
194 /* Note that UNIFORM args have been turned into FIXED_GRF by
195 * assign_curbe_setup(), and interpolation uses fixed hardware regs from
196 * the start (see interp_reg()).
197 */
198 for (int i = 0; i < inst->sources; i++) {
199 if (inst->src[i].file == FIXED_GRF) {
200 unsigned reg_nr = inst->src[i].nr;
201 if (reg_nr / reg_unit(devinfo) >= payload_node_count)
202 continue;
203
204 for (unsigned j = reg_nr / reg_unit(devinfo);
205 j < DIV_ROUND_UP(reg_nr + regs_read(devinfo, inst, i),
206 reg_unit(devinfo));
207 j++) {
208 payload_last_use_ip[j] = use_ip;
209 assert(j < payload_node_count);
210 }
211 }
212 }
213
214 if (inst->dst.file == FIXED_GRF) {
215 unsigned reg_nr = inst->dst.nr;
216 if (reg_nr / reg_unit(devinfo) < payload_node_count) {
217 for (unsigned j = reg_nr / reg_unit(devinfo);
218 j < DIV_ROUND_UP(reg_nr + regs_written(inst),
219 reg_unit(devinfo));
220 j++) {
221 payload_last_use_ip[j] = use_ip;
222 assert(j < payload_node_count);
223 }
224 }
225 }
226
227 ip++;
228 }
229
230 /* g0 is needed to construct scratch headers for spilling. While we could
231 * extend its live range each time we spill a register, and update the
232 * interference graph accordingly, this would get pretty messy. Instead,
233 * simply consider g0 live for the whole program if spilling is required.
234 */
235 if (allow_spilling)
236 payload_last_use_ip[0] = ip - 1;
237 }
238
239 class brw_reg_alloc {
240 public:
brw_reg_alloc(fs_visitor * fs)241 brw_reg_alloc(fs_visitor *fs):
242 fs(fs), devinfo(fs->devinfo), compiler(fs->compiler),
243 live(fs->live_analysis.require()), g(NULL),
244 have_spill_costs(false)
245 {
246 mem_ctx = ralloc_context(NULL);
247
248 /* Stash the number of instructions so we can sanity check that our
249 * counts still match liveness.
250 */
251 live_instr_count = fs->cfg->last_block()->end_ip + 1;
252
253 spill_insts = _mesa_pointer_set_create(mem_ctx);
254
255 /* Most of this allocation was written for a reg_width of 1
256 * (dispatch_width == 8). In extending to SIMD16, the code was
257 * left in place and it was converted to have the hardware
258 * registers it's allocating be contiguous physical pairs of regs
259 * for reg_width == 2.
260 */
261 int reg_width = fs->dispatch_width / 8;
262 payload_node_count = ALIGN(fs->first_non_payload_grf, reg_width);
263
264 /* Get payload IP information */
265 payload_last_use_ip = ralloc_array(mem_ctx, int, payload_node_count);
266
267 node_count = 0;
268 first_payload_node = 0;
269 grf127_send_hack_node = 0;
270 first_vgrf_node = 0;
271 last_vgrf_node = 0;
272 first_spill_node = 0;
273
274 spill_vgrf_ip = NULL;
275 spill_vgrf_ip_alloc = 0;
276 spill_node_count = 0;
277 }
278
~brw_reg_alloc()279 ~brw_reg_alloc()
280 {
281 ralloc_free(mem_ctx);
282 }
283
284 bool assign_regs(bool allow_spilling, bool spill_all);
285
286 private:
287 void setup_live_interference(unsigned node,
288 int node_start_ip, int node_end_ip);
289 void setup_inst_interference(const fs_inst *inst);
290
291 void build_interference_graph(bool allow_spilling);
292
293 brw_reg build_ex_desc(const brw_builder &bld, unsigned reg_size, bool unspill);
294
295 brw_reg build_lane_offsets(const brw_builder &bld,
296 uint32_t spill_offset, int ip);
297 brw_reg build_single_offset(const brw_builder &bld,
298 uint32_t spill_offset, int ip);
299 brw_reg build_legacy_scratch_header(const brw_builder &bld,
300 uint32_t spill_offset, int ip);
301
302 void emit_unspill(const brw_builder &bld, struct brw_shader_stats *stats,
303 brw_reg dst, uint32_t spill_offset, unsigned count, int ip);
304 void emit_spill(const brw_builder &bld, struct brw_shader_stats *stats,
305 brw_reg src, uint32_t spill_offset, unsigned count, int ip);
306
307 void set_spill_costs();
308 int choose_spill_reg();
309 brw_reg alloc_spill_reg(unsigned size, int ip);
310 void spill_reg(unsigned spill_reg);
311
312 void *mem_ctx;
313 fs_visitor *fs;
314 const intel_device_info *devinfo;
315 const brw_compiler *compiler;
316 const fs_live_variables &live;
317 int live_instr_count;
318
319 set *spill_insts;
320
321 ra_graph *g;
322 bool have_spill_costs;
323
324 int payload_node_count;
325 int *payload_last_use_ip;
326
327 int node_count;
328 int first_payload_node;
329 int grf127_send_hack_node;
330 int first_vgrf_node;
331 int last_vgrf_node;
332 int first_spill_node;
333
334 int *spill_vgrf_ip;
335 int spill_vgrf_ip_alloc;
336 int spill_node_count;
337 };
338
339 namespace {
340 /**
341 * Maximum spill block size we expect to encounter in 32B units.
342 *
343 * This is somewhat arbitrary and doesn't necessarily limit the maximum
344 * variable size that can be spilled -- A higher value will allow a
345 * variable of a given size to be spilled more efficiently with a smaller
346 * number of scratch messages, but will increase the likelihood of a
347 * collision between the MRFs reserved for spilling and other MRFs used by
348 * the program (and possibly increase GRF register pressure on platforms
349 * without hardware MRFs), what could cause register allocation to fail.
350 *
351 * For the moment reserve just enough space so a register of 32 bit
352 * component type and natural region width can be spilled without splitting
353 * into multiple (force_writemask_all) scratch messages.
354 */
355 unsigned
spill_max_size(const fs_visitor * s)356 spill_max_size(const fs_visitor *s)
357 {
358 /* LSC is limited to SIMD16 sends (SIMD32 on Xe2) */
359 if (s->devinfo->has_lsc)
360 return 2 * reg_unit(s->devinfo);
361
362 /* FINISHME - On Gfx7+ it should be possible to avoid this limit
363 * altogether by spilling directly from the temporary GRF
364 * allocated to hold the result of the instruction (and the
365 * scratch write header).
366 */
367 /* FINISHME - The shader's dispatch width probably belongs in
368 * backend_shader (or some nonexistent fs_shader class?)
369 * rather than in the visitor class.
370 */
371 return s->dispatch_width / 8;
372 }
373 }
374
375 void
setup_live_interference(unsigned node,int node_start_ip,int node_end_ip)376 brw_reg_alloc::setup_live_interference(unsigned node,
377 int node_start_ip, int node_end_ip)
378 {
379 /* Mark any virtual grf that is live between the start of the program and
380 * the last use of a payload node interfering with that payload node.
381 */
382 for (int i = 0; i < payload_node_count; i++) {
383 if (payload_last_use_ip[i] == -1)
384 continue;
385
386 /* Note that we use a <= comparison, unlike vgrfs_interfere(),
387 * in order to not have to worry about the uniform issue described in
388 * calculate_live_intervals().
389 */
390 if (node_start_ip <= payload_last_use_ip[i])
391 ra_add_node_interference(g, node, first_payload_node + i);
392 }
393
394 /* Add interference with every vgrf whose live range intersects this
395 * node's. We only need to look at nodes below this one as the reflexivity
396 * of interference will take care of the rest.
397 */
398 for (unsigned n2 = first_vgrf_node;
399 n2 <= (unsigned)last_vgrf_node && n2 < node; n2++) {
400 unsigned vgrf = n2 - first_vgrf_node;
401 if (!(node_end_ip <= live.vgrf_start[vgrf] ||
402 live.vgrf_end[vgrf] <= node_start_ip))
403 ra_add_node_interference(g, node, n2);
404 }
405 }
406
407 /**
408 * Returns true if this instruction's sources and destinations cannot
409 * safely be the same register.
410 *
411 * In most cases, a register can be written over safely by the same
412 * instruction that is its last use. For a single instruction, the
413 * sources are dereferenced before writing of the destination starts
414 * (naturally).
415 *
416 * However, there are a few cases where this can be problematic:
417 *
418 * - Virtual opcodes that translate to multiple instructions in the
419 * code generator: if src == dst and one instruction writes the
420 * destination before a later instruction reads the source, then
421 * src will have been clobbered.
422 *
423 * - SIMD16 compressed instructions with certain regioning (see below).
424 *
425 * The register allocator uses this information to set up conflicts between
426 * GRF sources and the destination.
427 */
428 static bool
brw_inst_has_source_and_destination_hazard(const fs_inst * inst)429 brw_inst_has_source_and_destination_hazard(const fs_inst *inst)
430 {
431 switch (inst->opcode) {
432 case FS_OPCODE_PACK_HALF_2x16_SPLIT:
433 /* Multiple partial writes to the destination */
434 return true;
435 case SHADER_OPCODE_SHUFFLE:
436 /* This instruction returns an arbitrary channel from the source and
437 * gets split into smaller instructions in the generator. It's possible
438 * that one of the instructions will read from a channel corresponding
439 * to an earlier instruction.
440 */
441 case SHADER_OPCODE_SEL_EXEC:
442 /* This is implemented as
443 *
444 * mov(16) g4<1>D 0D { align1 WE_all 1H };
445 * mov(16) g4<1>D g5<8,8,1>D { align1 1H }
446 *
447 * Because the source is only read in the second instruction, the first
448 * may stomp all over it.
449 */
450 return true;
451 case SHADER_OPCODE_QUAD_SWIZZLE:
452 switch (inst->src[1].ud) {
453 case BRW_SWIZZLE_XXXX:
454 case BRW_SWIZZLE_YYYY:
455 case BRW_SWIZZLE_ZZZZ:
456 case BRW_SWIZZLE_WWWW:
457 case BRW_SWIZZLE_XXZZ:
458 case BRW_SWIZZLE_YYWW:
459 case BRW_SWIZZLE_XYXY:
460 case BRW_SWIZZLE_ZWZW:
461 /* These can be implemented as a single Align1 region on all
462 * platforms, so there's never a hazard between source and
463 * destination. C.f. brw_generator::generate_quad_swizzle().
464 */
465 return false;
466 default:
467 return !is_uniform(inst->src[0]);
468 }
469 case BRW_OPCODE_DPAS:
470 /* This is overly conservative. The actual hazard is more complicated to
471 * describe. When the repeat count is N, the single instruction behaves
472 * like N instructions with a repeat count of one, but the destination
473 * and source registers are incremented (in somewhat complex ways) for
474 * each instruction.
475 *
476 * This means the source and destination register is actually a range of
477 * registers. The hazard exists of an earlier iteration would write a
478 * register that should be read by a later iteration.
479 *
480 * There may be some advantage to properly modeling this, but for now,
481 * be overly conservative.
482 */
483 return inst->rcount > 1;
484 default:
485 /* The SIMD16 compressed instruction
486 *
487 * add(16) g4<1>F g4<8,8,1>F g6<8,8,1>F
488 *
489 * is actually decoded in hardware as:
490 *
491 * add(8) g4<1>F g4<8,8,1>F g6<8,8,1>F
492 * add(8) g5<1>F g5<8,8,1>F g7<8,8,1>F
493 *
494 * Which is safe. However, if we have uniform accesses
495 * happening, we get into trouble:
496 *
497 * add(8) g4<1>F g4<0,1,0>F g6<8,8,1>F
498 * add(8) g5<1>F g4<0,1,0>F g7<8,8,1>F
499 *
500 * Now our destination for the first instruction overwrote the
501 * second instruction's src0, and we get garbage for those 8
502 * pixels. There's a similar issue for the pre-gfx6
503 * pixel_x/pixel_y, which are registers of 16-bit values and thus
504 * would get stomped by the first decode as well.
505 */
506 if (inst->exec_size == 16) {
507 for (int i = 0; i < inst->sources; i++) {
508 if (inst->src[i].file == VGRF && (inst->src[i].stride == 0 ||
509 inst->src[i].type == BRW_TYPE_UW ||
510 inst->src[i].type == BRW_TYPE_W ||
511 inst->src[i].type == BRW_TYPE_UB ||
512 inst->src[i].type == BRW_TYPE_B)) {
513 return true;
514 }
515 }
516 }
517 return false;
518 }
519 }
520
521 void
setup_inst_interference(const fs_inst * inst)522 brw_reg_alloc::setup_inst_interference(const fs_inst *inst)
523 {
524 /* Certain instructions can't safely use the same register for their
525 * sources and destination. Add interference.
526 */
527 if (inst->dst.file == VGRF && brw_inst_has_source_and_destination_hazard(inst)) {
528 for (unsigned i = 0; i < inst->sources; i++) {
529 if (inst->src[i].file == VGRF) {
530 ra_add_node_interference(g, first_vgrf_node + inst->dst.nr,
531 first_vgrf_node + inst->src[i].nr);
532 }
533 }
534 }
535
536 /* A compressed instruction is actually two instructions executed
537 * simultaneously. On most platforms, it ok to have the source and
538 * destination registers be the same. In this case, each instruction
539 * over-writes its own source and there's no problem. The real problem
540 * here is if the source and destination registers are off by one. Then
541 * you can end up in a scenario where the first instruction over-writes the
542 * source of the second instruction. Since the compiler doesn't know about
543 * this level of granularity, we simply make the source and destination
544 * interfere.
545 */
546 if (inst->dst.component_size(inst->exec_size) > REG_SIZE &&
547 inst->dst.file == VGRF) {
548 for (int i = 0; i < inst->sources; ++i) {
549 if (inst->src[i].file == VGRF) {
550 ra_add_node_interference(g, first_vgrf_node + inst->dst.nr,
551 first_vgrf_node + inst->src[i].nr);
552 }
553 }
554 }
555
556 if (grf127_send_hack_node >= 0) {
557 /* At Intel Broadwell PRM, vol 07, section "Instruction Set Reference",
558 * subsection "EUISA Instructions", Send Message (page 990):
559 *
560 * "r127 must not be used for return address when there is a src and
561 * dest overlap in send instruction."
562 *
563 * We are avoiding using grf127 as part of the destination of send
564 * messages adding a node interference to the grf127_send_hack_node.
565 * This node has a fixed assignment to grf127.
566 *
567 * We don't apply it to SIMD16 instructions because previous code avoids
568 * any register overlap between sources and destination.
569 */
570 if (inst->exec_size < 16 && inst->is_send_from_grf() &&
571 inst->dst.file == VGRF)
572 ra_add_node_interference(g, first_vgrf_node + inst->dst.nr,
573 grf127_send_hack_node);
574 }
575
576 /* From the Skylake PRM Vol. 2a docs for sends:
577 *
578 * "It is required that the second block of GRFs does not overlap with
579 * the first block."
580 *
581 * Normally, this is taken care of by fixup_sends_duplicate_payload() but
582 * in the case where one of the registers is an undefined value, the
583 * register allocator may decide that they don't interfere even though
584 * they're used as sources in the same instruction. We also need to add
585 * interference here.
586 */
587 if (inst->opcode == SHADER_OPCODE_SEND && inst->ex_mlen > 0 &&
588 inst->src[2].file == VGRF && inst->src[3].file == VGRF &&
589 inst->src[2].nr != inst->src[3].nr)
590 ra_add_node_interference(g, first_vgrf_node + inst->src[2].nr,
591 first_vgrf_node + inst->src[3].nr);
592
593 /* When we do send-from-GRF for FB writes, we need to ensure that the last
594 * write instruction sends from a high register. This is because the
595 * vertex fetcher wants to start filling the low payload registers while
596 * the pixel data port is still working on writing out the memory. If we
597 * don't do this, we get rendering artifacts.
598 *
599 * We could just do "something high". Instead, we just pick the highest
600 * register that works.
601 */
602 if (inst->eot && devinfo->ver < 30) {
603 const int vgrf = inst->opcode == SHADER_OPCODE_SEND ?
604 inst->src[2].nr : inst->src[0].nr;
605 const int size = DIV_ROUND_UP(fs->alloc.sizes[vgrf], reg_unit(devinfo));
606 int reg = BRW_MAX_GRF - size;
607
608 if (grf127_send_hack_node >= 0) {
609 /* Avoid r127 which might be unusable if the node was previously
610 * written by a SIMD8 SEND message with source/destination overlap.
611 */
612 reg--;
613 }
614
615 assert(reg >= 112);
616 ra_set_node_reg(g, first_vgrf_node + vgrf, reg);
617
618 if (inst->ex_mlen > 0) {
619 const int vgrf = inst->src[3].nr;
620 reg -= DIV_ROUND_UP(fs->alloc.sizes[vgrf], reg_unit(devinfo));
621 assert(reg >= 112);
622 ra_set_node_reg(g, first_vgrf_node + vgrf, reg);
623 }
624 }
625 }
626
627 void
build_interference_graph(bool allow_spilling)628 brw_reg_alloc::build_interference_graph(bool allow_spilling)
629 {
630 /* Compute the RA node layout */
631 node_count = 0;
632 first_payload_node = node_count;
633 node_count += payload_node_count;
634
635 grf127_send_hack_node = node_count;
636 node_count++;
637
638 first_vgrf_node = node_count;
639 node_count += fs->alloc.count;
640 last_vgrf_node = node_count - 1;
641 first_spill_node = node_count;
642
643 fs->calculate_payload_ranges(allow_spilling, payload_node_count,
644 payload_last_use_ip);
645
646 assert(g == NULL);
647 g = ra_alloc_interference_graph(compiler->reg_set.regs, node_count);
648 ralloc_steal(mem_ctx, g);
649
650 /* Set up the payload nodes */
651 for (int i = 0; i < payload_node_count; i++)
652 ra_set_node_reg(g, first_payload_node + i, i);
653
654 if (grf127_send_hack_node >= 0)
655 ra_set_node_reg(g, grf127_send_hack_node, 127);
656
657 /* Specify the classes of each virtual register. */
658 for (unsigned i = 0; i < fs->alloc.count; i++) {
659 unsigned size = DIV_ROUND_UP(fs->alloc.sizes[i], reg_unit(devinfo));
660
661 assert(size <= ARRAY_SIZE(compiler->reg_set.classes) &&
662 "Register allocation relies on split_virtual_grfs()");
663
664 ra_set_node_class(g, first_vgrf_node + i,
665 compiler->reg_set.classes[size - 1]);
666 }
667
668 /* Add interference based on the live range of the register */
669 for (unsigned i = 0; i < fs->alloc.count; i++) {
670 setup_live_interference(first_vgrf_node + i,
671 live.vgrf_start[i],
672 live.vgrf_end[i]);
673 }
674
675 /* Add interference based on the instructions in which a register is used.
676 */
677 foreach_block_and_inst(block, fs_inst, inst, fs->cfg)
678 setup_inst_interference(inst);
679 }
680
681 brw_reg
build_single_offset(const brw_builder & bld,uint32_t spill_offset,int ip)682 brw_reg_alloc::build_single_offset(const brw_builder &bld, uint32_t spill_offset, int ip)
683 {
684 brw_reg offset = retype(alloc_spill_reg(1, ip), BRW_TYPE_UD);
685 fs_inst *inst = bld.MOV(offset, brw_imm_ud(spill_offset));
686 _mesa_set_add(spill_insts, inst);
687 return offset;
688 }
689
690 brw_reg
build_ex_desc(const brw_builder & bld,unsigned reg_size,bool unspill)691 brw_reg_alloc::build_ex_desc(const brw_builder &bld, unsigned reg_size, bool unspill)
692 {
693 /* Use a different area of the address register than what is used in
694 * brw_lower_logical_sends.c (brw_address_reg(2)) so we don't have
695 * interactions between the spill/fill instructions and the other send
696 * messages.
697 */
698 brw_reg ex_desc = bld.vaddr(BRW_TYPE_UD,
699 BRW_ADDRESS_SUBREG_INDIRECT_SPILL_DESC);
700 fs_inst *inst = bld.exec_all().group(1, 0).AND(
701 ex_desc,
702 retype(brw_vec1_grf(0, 5), BRW_TYPE_UD),
703 brw_imm_ud(INTEL_MASK(31, 10)));
704 _mesa_set_add(spill_insts, inst);
705
706 const intel_device_info *devinfo = bld.shader->devinfo;
707 if (devinfo->verx10 >= 200) {
708 inst = bld.exec_all().group(1, 0).SHR(
709 ex_desc, ex_desc, brw_imm_ud(4));
710 _mesa_set_add(spill_insts, inst);
711 } else {
712 if (unspill) {
713 inst = bld.exec_all().group(1, 0).OR(
714 ex_desc, ex_desc, brw_imm_ud(GFX12_SFID_UGM));
715 _mesa_set_add(spill_insts, inst);
716 } else {
717 inst = bld.exec_all().group(1, 0).OR(
718 ex_desc, ex_desc,
719 brw_imm_ud(brw_message_ex_desc(devinfo, reg_size) |
720 GFX12_SFID_UGM));
721 _mesa_set_add(spill_insts, inst);
722 }
723 }
724
725 return ex_desc;
726 }
727
728 brw_reg
build_lane_offsets(const brw_builder & bld,uint32_t spill_offset,int ip)729 brw_reg_alloc::build_lane_offsets(const brw_builder &bld, uint32_t spill_offset, int ip)
730 {
731 assert(bld.dispatch_width() <= 16 * reg_unit(bld.shader->devinfo));
732
733 const brw_builder ubld = bld.exec_all();
734 const unsigned reg_count = ubld.dispatch_width() / 8;
735
736 brw_reg offset = retype(alloc_spill_reg(reg_count, ip), BRW_TYPE_UD);
737 fs_inst *inst;
738
739 /* Build an offset per lane in SIMD8 */
740 inst = ubld.group(8, 0).MOV(retype(offset, BRW_TYPE_UW),
741 brw_imm_uv(0x76543210));
742 _mesa_set_add(spill_insts, inst);
743 inst = ubld.group(8, 0).MOV(offset, retype(offset, BRW_TYPE_UW));
744 _mesa_set_add(spill_insts, inst);
745
746 /* Build offsets in the upper 8 lanes of SIMD16 */
747 if (ubld.dispatch_width() > 8) {
748 inst = ubld.group(8, 0).ADD(
749 byte_offset(offset, REG_SIZE),
750 byte_offset(offset, 0),
751 brw_imm_ud(8));
752 _mesa_set_add(spill_insts, inst);
753 }
754
755 /* Make the offset a dword */
756 inst = ubld.SHL(offset, offset, brw_imm_ud(2));
757 _mesa_set_add(spill_insts, inst);
758
759 /* Build offsets in the upper 16 lanes of SIMD32 */
760 if (ubld.dispatch_width() > 16) {
761 inst = ubld.group(16, 0).ADD(
762 byte_offset(offset, 2 * REG_SIZE),
763 byte_offset(offset, 0),
764 brw_imm_ud(16 << 2));
765 _mesa_set_add(spill_insts, inst);
766 }
767
768 /* Add the base offset */
769 if (spill_offset) {
770 inst = ubld.ADD(offset, offset, brw_imm_ud(spill_offset));
771 _mesa_set_add(spill_insts, inst);
772 }
773
774 return offset;
775 }
776
777 /**
778 * Generate a scratch header for pre-LSC platforms.
779 */
780 brw_reg
build_legacy_scratch_header(const brw_builder & bld,uint32_t spill_offset,int ip)781 brw_reg_alloc::build_legacy_scratch_header(const brw_builder &bld,
782 uint32_t spill_offset, int ip)
783 {
784 const brw_builder ubld8 = bld.exec_all().group(8, 0);
785 const brw_builder ubld1 = bld.exec_all().group(1, 0);
786
787 /* Allocate a spill header and make it interfere with g0 */
788 brw_reg header = retype(alloc_spill_reg(1, ip), BRW_TYPE_UD);
789 ra_add_node_interference(g, first_vgrf_node + header.nr, first_payload_node);
790
791 fs_inst *inst =
792 ubld8.emit(SHADER_OPCODE_SCRATCH_HEADER, header, brw_ud8_grf(0, 0));
793 _mesa_set_add(spill_insts, inst);
794
795 /* Write the scratch offset */
796 assert(spill_offset % 16 == 0);
797 inst = ubld1.MOV(component(header, 2), brw_imm_ud(spill_offset / 16));
798 _mesa_set_add(spill_insts, inst);
799
800 return header;
801 }
802
803 void
emit_unspill(const brw_builder & bld,struct brw_shader_stats * stats,brw_reg dst,uint32_t spill_offset,unsigned count,int ip)804 brw_reg_alloc::emit_unspill(const brw_builder &bld,
805 struct brw_shader_stats *stats,
806 brw_reg dst,
807 uint32_t spill_offset, unsigned count, int ip)
808 {
809 const intel_device_info *devinfo = bld.shader->devinfo;
810 const unsigned reg_size = dst.component_size(bld.dispatch_width()) /
811 REG_SIZE;
812
813 for (unsigned i = 0; i < DIV_ROUND_UP(count, reg_size); i++) {
814 ++stats->fill_count;
815
816 fs_inst *unspill_inst;
817 if (devinfo->verx10 >= 125) {
818 /* LSC is limited to SIMD16 (SIMD32 on Xe2) load/store but we can
819 * load more using transpose messages.
820 */
821 const bool use_transpose =
822 bld.dispatch_width() > 16 * reg_unit(devinfo) ||
823 bld.has_writemask_all();
824 const brw_builder ubld = use_transpose ? bld.exec_all().group(1, 0) : bld;
825 brw_reg offset;
826 if (use_transpose) {
827 offset = build_single_offset(ubld, spill_offset, ip);
828 } else {
829 offset = build_lane_offsets(ubld, spill_offset, ip);
830 }
831
832 brw_reg srcs[] = {
833 brw_imm_ud(0), /* desc */
834 build_ex_desc(bld, reg_size, true),
835 offset, /* payload */
836 brw_reg(), /* payload2 */
837 };
838
839 uint32_t desc = lsc_msg_desc(devinfo, LSC_OP_LOAD,
840 LSC_ADDR_SURFTYPE_SS,
841 LSC_ADDR_SIZE_A32,
842 LSC_DATA_SIZE_D32,
843 use_transpose ? reg_size * 8 : 1 /* num_channels */,
844 use_transpose,
845 LSC_CACHE(devinfo, LOAD, L1STATE_L3MOCS));
846
847
848 unspill_inst = ubld.emit(SHADER_OPCODE_SEND, dst,
849 srcs, ARRAY_SIZE(srcs));
850 unspill_inst->sfid = GFX12_SFID_UGM;
851 unspill_inst->header_size = 0;
852 unspill_inst->mlen = lsc_msg_addr_len(devinfo, LSC_ADDR_SIZE_A32,
853 unspill_inst->exec_size);
854 unspill_inst->ex_mlen = 0;
855 unspill_inst->size_written =
856 lsc_msg_dest_len(devinfo, LSC_DATA_SIZE_D32, bld.dispatch_width()) * REG_SIZE;
857 unspill_inst->send_has_side_effects = false;
858 unspill_inst->send_is_volatile = true;
859
860 unspill_inst->src[0] = brw_imm_ud(
861 desc |
862 brw_message_desc(devinfo,
863 unspill_inst->mlen,
864 unspill_inst->size_written / REG_SIZE,
865 unspill_inst->header_size));
866 } else {
867 brw_reg header = build_legacy_scratch_header(bld, spill_offset, ip);
868
869 const unsigned bti = GFX8_BTI_STATELESS_NON_COHERENT;
870
871 brw_reg srcs[] = {
872 brw_imm_ud(0), /* desc */
873 brw_imm_ud(0), /* ex_desc */
874 header
875 };
876 unspill_inst = bld.emit(SHADER_OPCODE_SEND, dst,
877 srcs, ARRAY_SIZE(srcs));
878 unspill_inst->mlen = 1;
879 unspill_inst->header_size = 1;
880 unspill_inst->size_written = reg_size * REG_SIZE;
881 unspill_inst->send_has_side_effects = false;
882 unspill_inst->send_is_volatile = true;
883 unspill_inst->sfid = GFX7_SFID_DATAPORT_DATA_CACHE;
884
885 unspill_inst->src[0] = brw_imm_ud(
886 brw_dp_desc(devinfo, bti,
887 BRW_DATAPORT_READ_MESSAGE_OWORD_BLOCK_READ,
888 BRW_DATAPORT_OWORD_BLOCK_DWORDS(reg_size * 8)) |
889 brw_message_desc(devinfo,
890 unspill_inst->mlen,
891 unspill_inst->size_written / REG_SIZE,
892 unspill_inst->header_size));
893 }
894 _mesa_set_add(spill_insts, unspill_inst);
895 assert(unspill_inst->force_writemask_all || count % reg_size == 0);
896
897 dst.offset += reg_size * REG_SIZE;
898 spill_offset += reg_size * REG_SIZE;
899 }
900 }
901
902 void
emit_spill(const brw_builder & bld,struct brw_shader_stats * stats,brw_reg src,uint32_t spill_offset,unsigned count,int ip)903 brw_reg_alloc::emit_spill(const brw_builder &bld,
904 struct brw_shader_stats *stats,
905 brw_reg src,
906 uint32_t spill_offset, unsigned count, int ip)
907 {
908 const intel_device_info *devinfo = bld.shader->devinfo;
909 const unsigned reg_size = src.component_size(bld.dispatch_width()) /
910 REG_SIZE;
911
912 for (unsigned i = 0; i < DIV_ROUND_UP(count, reg_size); i++) {
913 ++stats->spill_count;
914
915 fs_inst *spill_inst;
916 if (devinfo->verx10 >= 125) {
917 brw_reg offset = build_lane_offsets(bld, spill_offset, ip);
918
919 brw_reg srcs[] = {
920 brw_imm_ud(0), /* desc */
921 build_ex_desc(bld, reg_size, false),
922 offset, /* payload */
923 src, /* payload2 */
924 };
925 spill_inst = bld.emit(SHADER_OPCODE_SEND, bld.null_reg_f(),
926 srcs, ARRAY_SIZE(srcs));
927 spill_inst->sfid = GFX12_SFID_UGM;
928 uint32_t desc = lsc_msg_desc(devinfo, LSC_OP_STORE,
929 LSC_ADDR_SURFTYPE_SS,
930 LSC_ADDR_SIZE_A32,
931 LSC_DATA_SIZE_D32,
932 1 /* num_channels */,
933 false /* transpose */,
934 LSC_CACHE(devinfo, LOAD, L1STATE_L3MOCS));
935 spill_inst->header_size = 0;
936 spill_inst->mlen = lsc_msg_addr_len(devinfo, LSC_ADDR_SIZE_A32,
937 bld.dispatch_width());
938 spill_inst->ex_mlen = reg_size;
939 spill_inst->size_written = 0;
940 spill_inst->send_has_side_effects = true;
941 spill_inst->send_is_volatile = false;
942
943 spill_inst->src[0] = brw_imm_ud(
944 desc |
945 brw_message_desc(devinfo,
946 spill_inst->mlen,
947 spill_inst->size_written / REG_SIZE,
948 spill_inst->header_size));
949 } else {
950 brw_reg header = build_legacy_scratch_header(bld, spill_offset, ip);
951
952 const unsigned bti = GFX8_BTI_STATELESS_NON_COHERENT;
953 brw_reg srcs[] = {
954 brw_imm_ud(0), /* desc */
955 brw_imm_ud(0), /* ex_desc */
956 header,
957 src
958 };
959 spill_inst = bld.emit(SHADER_OPCODE_SEND, bld.null_reg_f(),
960 srcs, ARRAY_SIZE(srcs));
961 spill_inst->mlen = 1;
962 spill_inst->ex_mlen = reg_size;
963 spill_inst->size_written = 0;
964 spill_inst->header_size = 1;
965 spill_inst->send_has_side_effects = true;
966 spill_inst->send_is_volatile = false;
967 spill_inst->sfid = GFX7_SFID_DATAPORT_DATA_CACHE;
968
969 spill_inst->src[0] = brw_imm_ud(
970 brw_dp_desc(devinfo, bti,
971 GFX6_DATAPORT_WRITE_MESSAGE_OWORD_BLOCK_WRITE,
972 BRW_DATAPORT_OWORD_BLOCK_DWORDS(reg_size * 8)) |
973 brw_message_desc(devinfo,
974 spill_inst->mlen,
975 spill_inst->size_written / REG_SIZE,
976 spill_inst->header_size));
977 spill_inst->src[1] = brw_imm_ud(
978 brw_message_ex_desc(devinfo, spill_inst->ex_mlen));
979 }
980 _mesa_set_add(spill_insts, spill_inst);
981 assert(spill_inst->force_writemask_all || count % reg_size == 0);
982
983 src.offset += reg_size * REG_SIZE;
984 spill_offset += reg_size * REG_SIZE;
985 }
986 }
987
988 void
set_spill_costs()989 brw_reg_alloc::set_spill_costs()
990 {
991 float block_scale = 1.0;
992 float *spill_costs = rzalloc_array(NULL, float, fs->alloc.count);
993
994 /* Calculate costs for spilling nodes. Call it a cost of 1 per
995 * spill/unspill we'll have to do, and guess that the insides of
996 * loops run 10 times.
997 */
998 foreach_block_and_inst(block, fs_inst, inst, fs->cfg) {
999 for (unsigned int i = 0; i < inst->sources; i++) {
1000 if (inst->src[i].file == VGRF)
1001 spill_costs[inst->src[i].nr] += regs_read(devinfo, inst, i) * block_scale;
1002 }
1003
1004 if (inst->dst.file == VGRF)
1005 spill_costs[inst->dst.nr] += regs_written(inst) * block_scale;
1006
1007 /* Don't spill anything we generated while spilling */
1008 if (_mesa_set_search(spill_insts, inst)) {
1009 for (unsigned int i = 0; i < inst->sources; i++) {
1010 if (inst->src[i].file == VGRF)
1011 spill_costs[inst->src[i].nr] = INFINITY;
1012 }
1013 if (inst->dst.file == VGRF)
1014 spill_costs[inst->dst.nr] = INFINITY;
1015 }
1016
1017 switch (inst->opcode) {
1018
1019 case BRW_OPCODE_DO:
1020 block_scale *= 10;
1021 break;
1022
1023 case BRW_OPCODE_WHILE:
1024 block_scale /= 10;
1025 break;
1026
1027 case BRW_OPCODE_IF:
1028 block_scale *= 0.5;
1029 break;
1030
1031 case BRW_OPCODE_ENDIF:
1032 block_scale /= 0.5;
1033 break;
1034
1035 default:
1036 break;
1037 }
1038 }
1039
1040 for (unsigned i = 0; i < fs->alloc.count; i++) {
1041 /* Do the no_spill check first. Registers that are used as spill
1042 * temporaries may have been allocated after we calculated liveness so
1043 * we shouldn't look their liveness up. Fortunately, they're always
1044 * used in SCRATCH_READ/WRITE instructions so they'll always be flagged
1045 * no_spill.
1046 */
1047 if (isinf(spill_costs[i]))
1048 continue;
1049
1050 int live_length = live.vgrf_end[i] - live.vgrf_start[i];
1051 if (live_length <= 0)
1052 continue;
1053
1054 /* Divide the cost (in number of spills/fills) by the log of the length
1055 * of the live range of the register. This will encourage spill logic
1056 * to spill long-living things before spilling short-lived things where
1057 * spilling is less likely to actually do us any good. We use the log
1058 * of the length because it will fall off very quickly and not cause us
1059 * to spill medium length registers with more uses.
1060 */
1061 float adjusted_cost = spill_costs[i] / logf(live_length);
1062 ra_set_node_spill_cost(g, first_vgrf_node + i, adjusted_cost);
1063 }
1064
1065 have_spill_costs = true;
1066
1067 ralloc_free(spill_costs);
1068 }
1069
1070 int
choose_spill_reg()1071 brw_reg_alloc::choose_spill_reg()
1072 {
1073 if (!have_spill_costs)
1074 set_spill_costs();
1075
1076 int node = ra_get_best_spill_node(g);
1077 if (node < 0)
1078 return -1;
1079
1080 assert(node >= first_vgrf_node);
1081 return node - first_vgrf_node;
1082 }
1083
1084 brw_reg
alloc_spill_reg(unsigned size,int ip)1085 brw_reg_alloc::alloc_spill_reg(unsigned size, int ip)
1086 {
1087 int vgrf = fs->alloc.allocate(ALIGN(size, reg_unit(devinfo)));
1088 int class_idx = DIV_ROUND_UP(size, reg_unit(devinfo)) - 1;
1089 int n = ra_add_node(g, compiler->reg_set.classes[class_idx]);
1090 assert(n == first_vgrf_node + vgrf);
1091 assert(n == first_spill_node + spill_node_count);
1092
1093 setup_live_interference(n, ip - 1, ip + 1);
1094
1095 /* Add interference between this spill node and any other spill nodes for
1096 * the same instruction.
1097 */
1098 for (int s = 0; s < spill_node_count; s++) {
1099 if (spill_vgrf_ip[s] == ip)
1100 ra_add_node_interference(g, n, first_spill_node + s);
1101 }
1102
1103 /* Add this spill node to the list for next time */
1104 if (spill_node_count >= spill_vgrf_ip_alloc) {
1105 if (spill_vgrf_ip_alloc == 0)
1106 spill_vgrf_ip_alloc = 16;
1107 else
1108 spill_vgrf_ip_alloc *= 2;
1109 spill_vgrf_ip = reralloc(mem_ctx, spill_vgrf_ip, int,
1110 spill_vgrf_ip_alloc);
1111 }
1112 spill_vgrf_ip[spill_node_count++] = ip;
1113
1114 return brw_vgrf(vgrf, BRW_TYPE_F);
1115 }
1116
1117 void
spill_reg(unsigned spill_reg)1118 brw_reg_alloc::spill_reg(unsigned spill_reg)
1119 {
1120 int size = fs->alloc.sizes[spill_reg];
1121 unsigned int spill_offset = fs->last_scratch;
1122 assert(ALIGN(spill_offset, 16) == spill_offset); /* oword read/write req. */
1123
1124 fs->spilled_any_registers = true;
1125
1126 fs->last_scratch += align(size * REG_SIZE, REG_SIZE * reg_unit(devinfo));
1127
1128 /* We're about to replace all uses of this register. It no longer
1129 * conflicts with anything so we can get rid of its interference.
1130 */
1131 ra_set_node_spill_cost(g, first_vgrf_node + spill_reg, 0);
1132 ra_reset_node_interference(g, first_vgrf_node + spill_reg);
1133
1134 /* Generate spill/unspill instructions for the objects being
1135 * spilled. Right now, we spill or unspill the whole thing to a
1136 * virtual grf of the same size. For most instructions, though, we
1137 * could just spill/unspill the GRF being accessed.
1138 */
1139 int ip = 0;
1140 foreach_block_and_inst (block, fs_inst, inst, fs->cfg) {
1141 const brw_builder ibld = brw_builder(fs, block, inst);
1142 exec_node *before = inst->prev;
1143 exec_node *after = inst->next;
1144
1145 for (unsigned int i = 0; i < inst->sources; i++) {
1146 if (inst->src[i].file == VGRF &&
1147 inst->src[i].nr == spill_reg) {
1148 /* Count registers needed in units of physical registers */
1149 int count = align(regs_read(devinfo, inst, i), reg_unit(devinfo));
1150 /* Align the spilling offset the physical register size */
1151 int subset_spill_offset = spill_offset +
1152 ROUND_DOWN_TO(inst->src[i].offset, REG_SIZE * reg_unit(devinfo));
1153 brw_reg unspill_dst = alloc_spill_reg(count, ip);
1154
1155 inst->src[i].nr = unspill_dst.nr;
1156 /* The unspilled register is aligned to physical register, so
1157 * adjust the offset to the remaining within the physical register
1158 * size.
1159 */
1160 inst->src[i].offset %= REG_SIZE * reg_unit(devinfo);
1161
1162 /* We read the largest power-of-two divisor of the register count
1163 * (because only POT scratch read blocks are allowed by the
1164 * hardware) up to the maximum supported block size.
1165 */
1166 const unsigned width =
1167 MIN2(32, 1u << (ffs(MAX2(1, count) * 8) - 1));
1168
1169 /* Set exec_all() on unspill messages under the (rather
1170 * pessimistic) assumption that there is no one-to-one
1171 * correspondence between channels of the spilled variable in
1172 * scratch space and the scratch read message, which operates on
1173 * 32 bit channels. It shouldn't hurt in any case because the
1174 * unspill destination is a block-local temporary.
1175 */
1176 emit_unspill(ibld.exec_all().group(width, 0), &fs->shader_stats,
1177 unspill_dst, subset_spill_offset, count, ip);
1178 }
1179 }
1180
1181 if (inst->dst.file == VGRF &&
1182 inst->dst.nr == spill_reg &&
1183 inst->opcode != SHADER_OPCODE_UNDEF) {
1184 /* Count registers needed in units of physical registers */
1185 int count = align(regs_written(inst), reg_unit(devinfo));
1186 /* Align the spilling offset the physical register size */
1187 int subset_spill_offset = spill_offset +
1188 ROUND_DOWN_TO(inst->dst.offset, reg_unit(devinfo) * REG_SIZE);
1189 brw_reg spill_src = alloc_spill_reg(count, ip);
1190
1191 inst->dst.nr = spill_src.nr;
1192 /* The spilled register is aligned to physical register, so adjust
1193 * the offset to the remaining within the physical register size.
1194 */
1195 inst->dst.offset %= REG_SIZE * reg_unit(devinfo);
1196
1197 /* If we're immediately spilling the register, we should not use
1198 * destination dependency hints. Doing so will cause the GPU do
1199 * try to read and write the register at the same time and may
1200 * hang the GPU.
1201 */
1202 inst->no_dd_clear = false;
1203 inst->no_dd_check = false;
1204
1205 /* Calculate the execution width of the scratch messages (which work
1206 * in terms of 32 bit components so we have a fixed number of eight
1207 * channels per spilled register). We attempt to write one
1208 * exec_size-wide component of the variable at a time without
1209 * exceeding the maximum number of (fake) MRF registers reserved for
1210 * spills.
1211 */
1212 const unsigned width = 8 * reg_unit(devinfo) *
1213 DIV_ROUND_UP(MIN2(inst->dst.component_size(inst->exec_size),
1214 spill_max_size(fs) * REG_SIZE),
1215 reg_unit(devinfo) * REG_SIZE);
1216
1217 /* Spills should only write data initialized by the instruction for
1218 * whichever channels are enabled in the execution mask. If that's
1219 * not possible we'll have to emit a matching unspill before the
1220 * instruction and set force_writemask_all on the spill.
1221 */
1222 const bool per_channel =
1223 inst->dst.is_contiguous() &&
1224 brw_type_size_bytes(inst->dst.type) == 4 &&
1225 inst->exec_size == width;
1226
1227 /* Builder used to emit the scratch messages. */
1228 const brw_builder ubld = ibld.exec_all(!per_channel).group(width, 0);
1229
1230 /* If our write is going to affect just part of the
1231 * regs_written(inst), then we need to unspill the destination since
1232 * we write back out all of the regs_written(). If the original
1233 * instruction had force_writemask_all set and is not a partial
1234 * write, there should be no need for the unspill since the
1235 * instruction will be overwriting the whole destination in any case.
1236 */
1237 if (inst->is_partial_write() ||
1238 (!inst->force_writemask_all && !per_channel))
1239 emit_unspill(ubld, &fs->shader_stats, spill_src,
1240 subset_spill_offset, regs_written(inst), ip);
1241
1242 emit_spill(ubld.at(block, inst->next), &fs->shader_stats, spill_src,
1243 subset_spill_offset, regs_written(inst), ip);
1244 }
1245
1246 for (fs_inst *inst = (fs_inst *)before->next;
1247 inst != after; inst = (fs_inst *)inst->next)
1248 setup_inst_interference(inst);
1249
1250 /* We don't advance the ip for scratch read/write instructions
1251 * because we consider them to have the same ip as instruction we're
1252 * spilling around for the purposes of interference. Also, we're
1253 * inserting spill instructions without re-running liveness analysis
1254 * and we don't want to mess up our IPs.
1255 */
1256 if (!_mesa_set_search(spill_insts, inst))
1257 ip++;
1258 }
1259
1260 assert(ip == live_instr_count);
1261 }
1262
1263 bool
assign_regs(bool allow_spilling,bool spill_all)1264 brw_reg_alloc::assign_regs(bool allow_spilling, bool spill_all)
1265 {
1266 build_interference_graph(allow_spilling);
1267
1268 unsigned spilled = 0;
1269 while (1) {
1270 /* Debug of register spilling: Go spill everything. */
1271 if (unlikely(spill_all)) {
1272 int reg = choose_spill_reg();
1273 if (reg != -1) {
1274 spill_reg(reg);
1275 continue;
1276 }
1277 }
1278
1279 if (ra_allocate(g))
1280 break;
1281
1282 if (!allow_spilling)
1283 return false;
1284
1285 /* Failed to allocate registers. Spill some regs, and the caller will
1286 * loop back into here to try again.
1287 */
1288 unsigned nr_spills = 1;
1289 if (compiler->spilling_rate)
1290 nr_spills = MAX2(1, spilled / compiler->spilling_rate);
1291
1292 for (unsigned j = 0; j < nr_spills; j++) {
1293 int reg = choose_spill_reg();
1294 if (reg == -1) {
1295 if (j == 0)
1296 return false; /* Nothing to spill */
1297 break;
1298 }
1299
1300 spill_reg(reg);
1301 spilled++;
1302 }
1303 }
1304
1305 if (spilled)
1306 fs->invalidate_analysis(DEPENDENCY_INSTRUCTIONS | DEPENDENCY_VARIABLES);
1307
1308 /* Get the chosen virtual registers for each node, and map virtual
1309 * regs in the register classes back down to real hardware reg
1310 * numbers.
1311 */
1312 unsigned *hw_reg_mapping = ralloc_array(NULL, unsigned, fs->alloc.count);
1313 fs->grf_used = fs->first_non_payload_grf;
1314 for (unsigned i = 0; i < fs->alloc.count; i++) {
1315 int reg = ra_get_node_reg(g, first_vgrf_node + i);
1316
1317 hw_reg_mapping[i] = reg;
1318 fs->grf_used = MAX2(fs->grf_used,
1319 hw_reg_mapping[i] + DIV_ROUND_UP(fs->alloc.sizes[i],
1320 reg_unit(devinfo)));
1321 }
1322
1323 foreach_block_and_inst(block, fs_inst, inst, fs->cfg) {
1324 assign_reg(devinfo, hw_reg_mapping, &inst->dst);
1325 for (int i = 0; i < inst->sources; i++) {
1326 assign_reg(devinfo, hw_reg_mapping, &inst->src[i]);
1327 }
1328 }
1329
1330 fs->alloc.count = fs->grf_used;
1331
1332 ralloc_free(hw_reg_mapping);
1333
1334 return true;
1335 }
1336
1337 bool
brw_assign_regs(fs_visitor & s,bool allow_spilling,bool spill_all)1338 brw_assign_regs(fs_visitor &s, bool allow_spilling, bool spill_all)
1339 {
1340 brw_reg_alloc alloc(&s);
1341 bool success = alloc.assign_regs(allow_spilling, spill_all);
1342 if (!success && allow_spilling) {
1343 s.fail("no register to spill:\n");
1344 brw_print_instructions(s);
1345 }
1346 return success;
1347 }
1348