xref: /aosp_15_r20/external/mesa3d/src/intel/compiler/brw_fs_live_variables.cpp (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2012 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 <[email protected]>
25  *
26  */
27 
28 #include "brw_fs.h"
29 #include "brw_fs_live_variables.h"
30 
31 using namespace brw;
32 
33 #define MAX_INSTRUCTION (1 << 30)
34 
35 /** @file
36  *
37  * Support for calculating liveness information about virtual GRFs.
38  *
39  * This produces a live interval for each whole virtual GRF.  We could
40  * choose to expose per-component live intervals for VGRFs of size > 1,
41  * but we currently do not.  It is easier for the consumers of this
42  * information to work with whole VGRFs.
43  *
44  * However, we internally track use/def information at the per-GRF level for
45  * greater accuracy.  Large VGRFs may be accessed piecemeal over many
46  * (possibly non-adjacent) instructions.  In this case, examining a single
47  * instruction is insufficient to decide whether a whole VGRF is ultimately
48  * used or defined.  Tracking individual components allows us to easily
49  * assemble this information.
50  *
51  * See Muchnick's Advanced Compiler Design and Implementation, section
52  * 14.1 (p444).
53  */
54 
55 void
setup_one_read(struct block_data * bd,int ip,const brw_reg & reg)56 fs_live_variables::setup_one_read(struct block_data *bd,
57                                   int ip, const brw_reg &reg)
58 {
59    int var = var_from_reg(reg);
60    assert(var < num_vars);
61 
62    start[var] = MIN2(start[var], ip);
63    end[var] = MAX2(end[var], ip);
64 
65    /* The use[] bitset marks when the block makes use of a variable (VGRF
66     * channel) without having completely defined that variable within the
67     * block.
68     */
69    if (!BITSET_TEST(bd->def, var))
70       BITSET_SET(bd->use, var);
71 }
72 
73 void
setup_one_write(struct block_data * bd,fs_inst * inst,int ip,const brw_reg & reg)74 fs_live_variables::setup_one_write(struct block_data *bd, fs_inst *inst,
75                                    int ip, const brw_reg &reg)
76 {
77    int var = var_from_reg(reg);
78    assert(var < num_vars);
79 
80    start[var] = MIN2(start[var], ip);
81    end[var] = MAX2(end[var], ip);
82 
83    /* The def[] bitset marks when an initialization in a block completely
84     * screens off previous updates of that variable (VGRF channel).
85     */
86    if (inst->dst.file == VGRF) {
87       if (!inst->is_partial_write() && !BITSET_TEST(bd->use, var))
88          BITSET_SET(bd->def, var);
89 
90       BITSET_SET(bd->defout, var);
91    }
92 }
93 
94 /**
95  * Sets up the use[] and def[] bitsets.
96  *
97  * The basic-block-level live variable analysis needs to know which
98  * variables get used before they're completely defined, and which
99  * variables are completely defined before they're used.
100  *
101  * These are tracked at the per-component level, rather than whole VGRFs.
102  */
103 void
setup_def_use()104 fs_live_variables::setup_def_use()
105 {
106    int ip = 0;
107 
108    foreach_block (block, cfg) {
109       assert(ip == block->start_ip);
110       if (block->num > 0)
111          assert(cfg->blocks[block->num - 1]->end_ip == ip - 1);
112 
113       struct block_data *bd = &block_data[block->num];
114 
115       foreach_inst_in_block(fs_inst, inst, block) {
116          /* Set use[] for this instruction */
117          for (unsigned int i = 0; i < inst->sources; i++) {
118             brw_reg reg = inst->src[i];
119 
120             if (reg.file != VGRF)
121                continue;
122 
123             for (unsigned j = 0; j < regs_read(inst, i); j++) {
124                setup_one_read(bd, ip, reg);
125                reg.offset += REG_SIZE;
126             }
127          }
128 
129          bd->flag_use[0] |= inst->flags_read(devinfo) & ~bd->flag_def[0];
130 
131          /* Set def[] for this instruction */
132          if (inst->dst.file == VGRF) {
133             brw_reg reg = inst->dst;
134             for (unsigned j = 0; j < regs_written(inst); j++) {
135                setup_one_write(bd, inst, ip, reg);
136                reg.offset += REG_SIZE;
137             }
138          }
139 
140          if (!inst->predicate && inst->exec_size >= 8)
141             bd->flag_def[0] |= inst->flags_written(devinfo) & ~bd->flag_use[0];
142 
143          ip++;
144       }
145    }
146 }
147 
148 /**
149  * The algorithm incrementally sets bits in liveout and livein,
150  * propagating it through control flow.  It will eventually terminate
151  * because it only ever adds bits, and stops when no bits are added in
152  * a pass.
153  */
154 void
compute_live_variables()155 fs_live_variables::compute_live_variables()
156 {
157    bool cont = true;
158 
159    /* Propagate defin and defout down the CFG to calculate the union of live
160     * variables potentially defined along any possible control flow path.
161     */
162    do {
163       cont = false;
164 
165       foreach_block (block, cfg) {
166          const struct block_data *bd = &block_data[block->num];
167 
168          foreach_list_typed(bblock_link, child_link, link, &block->children) {
169             struct block_data *child_bd = &block_data[child_link->block->num];
170 
171             for (int i = 0; i < bitset_words; i++) {
172                const BITSET_WORD new_def = bd->defout[i] & ~child_bd->defin[i];
173                child_bd->defin[i] |= new_def;
174                child_bd->defout[i] |= new_def;
175                cont |= new_def;
176             }
177          }
178       }
179    } while (cont);
180 
181    do {
182       cont = false;
183 
184       foreach_block_reverse (block, cfg) {
185          struct block_data *bd = &block_data[block->num];
186 
187          /* Update liveout */
188          foreach_list_typed(bblock_link, child_link, link, &block->children) {
189             struct block_data *child_bd = &block_data[child_link->block->num];
190 
191             for (int i = 0; i < bitset_words; i++) {
192                BITSET_WORD new_liveout = (child_bd->livein[i] &
193                                           ~bd->liveout[i]);
194                new_liveout &= bd->defout[i]; /* Screen off uses with no reaching def */
195                if (new_liveout)
196                   bd->liveout[i] |= new_liveout;
197             }
198             BITSET_WORD new_liveout = (child_bd->flag_livein[0] &
199                                        ~bd->flag_liveout[0]);
200             if (new_liveout)
201                bd->flag_liveout[0] |= new_liveout;
202          }
203 
204          /* Update livein */
205          for (int i = 0; i < bitset_words; i++) {
206             BITSET_WORD new_livein = (bd->use[i] |
207                                       (bd->liveout[i] &
208                                        ~bd->def[i]));
209             new_livein &= bd->defin[i]; /* Screen off uses with no reaching def */
210             if (new_livein & ~bd->livein[i]) {
211                bd->livein[i] |= new_livein;
212                cont = true;
213             }
214          }
215          BITSET_WORD new_livein = (bd->flag_use[0] |
216                                    (bd->flag_liveout[0] &
217                                     ~bd->flag_def[0]));
218          if (new_livein & ~bd->flag_livein[0]) {
219             bd->flag_livein[0] |= new_livein;
220             cont = true;
221          }
222       }
223    } while (cont);
224 }
225 
226 /**
227  * Extend the start/end ranges for each variable to account for the
228  * new information calculated from control flow.
229  */
230 void
compute_start_end()231 fs_live_variables::compute_start_end()
232 {
233    foreach_block (block, cfg) {
234       struct block_data *bd = &block_data[block->num];
235       unsigned i;
236 
237       BITSET_FOREACH_SET(i, bd->livein, (unsigned)num_vars) {
238          start[i] = MIN2(start[i], block->start_ip);
239          end[i] = MAX2(end[i], block->start_ip);
240       }
241 
242       BITSET_FOREACH_SET(i, bd->liveout, (unsigned)num_vars) {
243          start[i] = MIN2(start[i], block->end_ip);
244          end[i] = MAX2(end[i], block->end_ip);
245       }
246    }
247 }
248 
fs_live_variables(const fs_visitor * s)249 fs_live_variables::fs_live_variables(const fs_visitor *s)
250    : devinfo(s->devinfo), cfg(s->cfg)
251 {
252    mem_ctx = ralloc_context(NULL);
253    linear_ctx *lin_ctx = linear_context(mem_ctx);
254 
255    num_vgrfs = s->alloc.count;
256    num_vars = 0;
257    var_from_vgrf = linear_alloc_array(lin_ctx, int, num_vgrfs);
258    for (int i = 0; i < num_vgrfs; i++) {
259       var_from_vgrf[i] = num_vars;
260       num_vars += s->alloc.sizes[i];
261    }
262 
263    vgrf_from_var = linear_alloc_array(lin_ctx, int, num_vars);
264    for (int i = 0; i < num_vgrfs; i++) {
265       for (unsigned j = 0; j < s->alloc.sizes[i]; j++) {
266          vgrf_from_var[var_from_vgrf[i] + j] = i;
267       }
268    }
269 
270    start = linear_alloc_array(lin_ctx, int, num_vars);
271    end = linear_alloc_array(lin_ctx, int, num_vars);
272    for (int i = 0; i < num_vars; i++) {
273       start[i] = MAX_INSTRUCTION;
274       end[i] = -1;
275    }
276 
277    vgrf_start = linear_alloc_array(lin_ctx, int, num_vgrfs);
278    vgrf_end = linear_alloc_array(lin_ctx, int, num_vgrfs);
279    for (int i = 0; i < num_vgrfs; i++) {
280       vgrf_start[i] = MAX_INSTRUCTION;
281       vgrf_end[i] = -1;
282    }
283 
284    block_data = linear_alloc_array(lin_ctx, struct block_data, cfg->num_blocks);
285 
286    bitset_words = BITSET_WORDS(num_vars);
287    for (int i = 0; i < cfg->num_blocks; i++) {
288       block_data[i].def = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
289       block_data[i].use = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
290       block_data[i].livein = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
291       block_data[i].liveout = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
292       block_data[i].defin = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
293       block_data[i].defout = linear_zalloc_array(lin_ctx, BITSET_WORD, bitset_words);
294 
295       block_data[i].flag_def[0] = 0;
296       block_data[i].flag_use[0] = 0;
297       block_data[i].flag_livein[0] = 0;
298       block_data[i].flag_liveout[0] = 0;
299    }
300 
301    setup_def_use();
302    compute_live_variables();
303    compute_start_end();
304 
305    /* Merge the per-component live ranges to whole VGRF live ranges. */
306    for (int i = 0; i < num_vars; i++) {
307       const unsigned vgrf = vgrf_from_var[i];
308       vgrf_start[vgrf] = MIN2(vgrf_start[vgrf], start[i]);
309       vgrf_end[vgrf] = MAX2(vgrf_end[vgrf], end[i]);
310    }
311 }
312 
~fs_live_variables()313 fs_live_variables::~fs_live_variables()
314 {
315    ralloc_free(mem_ctx);
316 }
317 
318 static bool
check_register_live_range(const fs_live_variables * live,int ip,const brw_reg & reg,unsigned n)319 check_register_live_range(const fs_live_variables *live, int ip,
320                           const brw_reg &reg, unsigned n)
321 {
322    const unsigned var = live->var_from_reg(reg);
323 
324    if (var + n > unsigned(live->num_vars) ||
325        live->vgrf_start[reg.nr] > ip || live->vgrf_end[reg.nr] < ip)
326       return false;
327 
328    for (unsigned j = 0; j < n; j++) {
329       if (live->start[var + j] > ip || live->end[var + j] < ip)
330          return false;
331    }
332 
333    return true;
334 }
335 
336 bool
validate(const fs_visitor * s) const337 fs_live_variables::validate(const fs_visitor *s) const
338 {
339    int ip = 0;
340 
341    foreach_block_and_inst(block, fs_inst, inst, s->cfg) {
342       for (unsigned i = 0; i < inst->sources; i++) {
343          if (inst->src[i].file == VGRF &&
344              !check_register_live_range(this, ip,
345                                         inst->src[i], regs_read(inst, i)))
346             return false;
347       }
348 
349       if (inst->dst.file == VGRF &&
350           !check_register_live_range(this, ip, inst->dst, regs_written(inst)))
351          return false;
352 
353       ip++;
354    }
355 
356    return true;
357 }
358 
359 bool
vars_interfere(int a,int b) const360 fs_live_variables::vars_interfere(int a, int b) const
361 {
362    return !(end[b] <= start[a] ||
363             end[a] <= start[b]);
364 }
365 
366 bool
vgrfs_interfere(int a,int b) const367 fs_live_variables::vgrfs_interfere(int a, int b) const
368 {
369    return !(vgrf_end[a] <= vgrf_start[b] ||
370             vgrf_end[b] <= vgrf_start[a]);
371 }
372