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