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