• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright © 2018 Valve Corporation
3  * Copyright © 2018 Google
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  *
24  * Authors:
25  *    Daniel Schürmann (daniel.schuermann@campus.tu-berlin.de)
26  *    Bas Nieuwenhuizen (bas@basnieuwenhuizen.nl)
27  *
28  */
29 
30 #include "aco_ir.h"
31 #include "util/u_math.h"
32 
33 #include <set>
34 #include <vector>
35 
36 namespace aco {
get_live_changes(aco_ptr<Instruction> & instr)37 RegisterDemand get_live_changes(aco_ptr<Instruction>& instr)
38 {
39    RegisterDemand changes;
40    for (const Definition& def : instr->definitions) {
41       if (!def.isTemp() || def.isKill())
42          continue;
43       changes += def.getTemp();
44    }
45 
46    for (const Operand& op : instr->operands) {
47       if (!op.isTemp() || !op.isFirstKill())
48          continue;
49       changes -= op.getTemp();
50    }
51 
52    return changes;
53 }
54 
get_temp_registers(aco_ptr<Instruction> & instr)55 RegisterDemand get_temp_registers(aco_ptr<Instruction>& instr)
56 {
57    RegisterDemand temp_registers;
58 
59    for (Definition def : instr->definitions) {
60       if (!def.isTemp())
61          continue;
62       if (def.isKill())
63          temp_registers += def.getTemp();
64    }
65 
66    for (Operand op : instr->operands) {
67       if (op.isTemp() && op.isLateKill() && op.isFirstKill())
68          temp_registers += op.getTemp();
69    }
70 
71    return temp_registers;
72 }
73 
get_demand_before(RegisterDemand demand,aco_ptr<Instruction> & instr,aco_ptr<Instruction> & instr_before)74 RegisterDemand get_demand_before(RegisterDemand demand, aco_ptr<Instruction>& instr, aco_ptr<Instruction>& instr_before)
75 {
76    demand -= get_live_changes(instr);
77    demand -= get_temp_registers(instr);
78    if (instr_before)
79       demand += get_temp_registers(instr_before);
80    return demand;
81 }
82 
83 namespace {
process_live_temps_per_block(Program * program,live & lives,Block * block,std::set<unsigned> & worklist,std::vector<uint16_t> & phi_sgpr_ops)84 void process_live_temps_per_block(Program *program, live& lives, Block* block,
85                                   std::set<unsigned>& worklist, std::vector<uint16_t>& phi_sgpr_ops)
86 {
87    std::vector<RegisterDemand>& register_demand = lives.register_demand[block->index];
88    RegisterDemand new_demand;
89 
90    register_demand.resize(block->instructions.size());
91    block->register_demand = RegisterDemand();
92    IDSet live = lives.live_out[block->index];
93 
94    /* add the live_out_exec to live */
95    bool exec_live = false;
96    if (block->live_out_exec != Temp()) {
97       live.insert(block->live_out_exec.id());
98       exec_live = true;
99    }
100 
101    /* initialize register demand */
102    for (unsigned t : live)
103       new_demand += Temp(t, program->temp_rc[t]);
104    new_demand.sgpr -= phi_sgpr_ops[block->index];
105 
106    /* traverse the instructions backwards */
107    int idx;
108    for (idx = block->instructions.size() -1; idx >= 0; idx--) {
109       Instruction *insn = block->instructions[idx].get();
110       if (is_phi(insn))
111          break;
112 
113       /* substract the 1 or 2 sgprs from exec */
114       if (exec_live)
115          assert(new_demand.sgpr >= (int16_t) program->lane_mask.size());
116       register_demand[idx] = RegisterDemand(new_demand.vgpr, new_demand.sgpr - (exec_live ? program->lane_mask.size() : 0));
117 
118       /* KILL */
119       for (Definition& definition : insn->definitions) {
120          if (!definition.isTemp()) {
121             continue;
122          }
123          if ((definition.isFixed() || definition.hasHint()) && definition.physReg() == vcc)
124             program->needs_vcc = true;
125 
126          const Temp temp = definition.getTemp();
127          const size_t n = live.erase(temp.id());
128 
129          if (n) {
130             new_demand -= temp;
131             definition.setKill(false);
132          } else {
133             register_demand[idx] += temp;
134             definition.setKill(true);
135          }
136 
137          if (definition.isFixed() && definition.physReg() == exec)
138             exec_live = false;
139       }
140 
141       /* GEN */
142       if (insn->opcode == aco_opcode::p_logical_end) {
143          new_demand.sgpr += phi_sgpr_ops[block->index];
144       } else {
145          /* we need to do this in a separate loop because the next one can
146           * setKill() for several operands at once and we don't want to
147           * overwrite that in a later iteration */
148          for (Operand& op : insn->operands)
149             op.setKill(false);
150 
151          for (unsigned i = 0; i < insn->operands.size(); ++i)
152          {
153             Operand& operand = insn->operands[i];
154             if (!operand.isTemp())
155                continue;
156             if (operand.isFixed() && operand.physReg() == vcc)
157                program->needs_vcc = true;
158             const Temp temp = operand.getTemp();
159             const bool inserted = live.insert(temp.id()).second;
160             if (inserted) {
161                operand.setFirstKill(true);
162                for (unsigned j = i + 1; j < insn->operands.size(); ++j) {
163                   if (insn->operands[j].isTemp() && insn->operands[j].tempId() == operand.tempId()) {
164                      insn->operands[j].setFirstKill(false);
165                      insn->operands[j].setKill(true);
166                   }
167                }
168                if (operand.isLateKill())
169                   register_demand[idx] += temp;
170                new_demand += temp;
171             }
172 
173             if (operand.isFixed() && operand.physReg() == exec)
174                exec_live = true;
175          }
176       }
177 
178       block->register_demand.update(register_demand[idx]);
179    }
180 
181    /* update block's register demand for a last time */
182    if (exec_live)
183       assert(new_demand.sgpr >= (int16_t) program->lane_mask.size());
184    new_demand.sgpr -= exec_live ? program->lane_mask.size() : 0;
185    block->register_demand.update(new_demand);
186 
187    /* handle phi definitions */
188    int phi_idx = idx;
189    while (phi_idx >= 0) {
190       register_demand[phi_idx] = new_demand;
191       Instruction *insn = block->instructions[phi_idx].get();
192 
193       assert(is_phi(insn));
194       assert(insn->definitions.size() == 1 && insn->definitions[0].isTemp());
195       Definition& definition = insn->definitions[0];
196       if ((definition.isFixed() || definition.hasHint()) && definition.physReg() == vcc)
197          program->needs_vcc = true;
198       const Temp temp = definition.getTemp();
199       const size_t n = live.erase(temp.id());
200 
201       if (n)
202          definition.setKill(false);
203       else
204          definition.setKill(true);
205 
206       phi_idx--;
207    }
208 
209    /* now, we need to merge the live-ins into the live-out sets */
210    for (unsigned t : live) {
211       RegClass rc = program->temp_rc[t];
212       std::vector<unsigned>& preds = rc.is_linear() ? block->linear_preds : block->logical_preds;
213 
214 #ifndef NDEBUG
215       if (preds.empty())
216          aco_err(program, "Temporary never defined or are defined after use: %%%d in BB%d", t, block->index);
217 #endif
218 
219       for (unsigned pred_idx : preds) {
220          auto it = lives.live_out[pred_idx].insert(t);
221          if (it.second)
222             worklist.insert(pred_idx);
223       }
224    }
225 
226    /* handle phi operands */
227    phi_idx = idx;
228    while (phi_idx >= 0) {
229       Instruction *insn = block->instructions[phi_idx].get();
230       assert(is_phi(insn));
231       /* directly insert into the predecessors live-out set */
232       std::vector<unsigned>& preds = insn->opcode == aco_opcode::p_phi
233                                    ? block->logical_preds
234                                    : block->linear_preds;
235       for (unsigned i = 0; i < preds.size(); ++i) {
236          Operand &operand = insn->operands[i];
237          if (!operand.isTemp())
238             continue;
239          if (operand.isFixed() && operand.physReg() == vcc)
240             program->needs_vcc = true;
241          /* check if we changed an already processed block */
242          const bool inserted = lives.live_out[preds[i]].insert(operand.tempId()).second;
243          if (inserted) {
244             operand.setKill(true);
245             worklist.insert(preds[i]);
246             if (insn->opcode == aco_opcode::p_phi && operand.getTemp().type() == RegType::sgpr)
247                phi_sgpr_ops[preds[i]] += operand.size();
248          }
249       }
250       phi_idx--;
251    }
252 
253    assert(block->index != 0 || (new_demand == RegisterDemand() && live.empty()));
254 }
255 
calc_waves_per_workgroup(Program * program)256 unsigned calc_waves_per_workgroup(Program *program)
257 {
258    /* When workgroup size is not known, just go with wave_size */
259    unsigned workgroup_size = program->workgroup_size == UINT_MAX
260                              ? program->wave_size
261                              : program->workgroup_size;
262 
263    return align(workgroup_size, program->wave_size) / program->wave_size;
264 }
265 } /* end namespace */
266 
get_extra_sgprs(Program * program)267 uint16_t get_extra_sgprs(Program *program)
268 {
269    if (program->chip_class >= GFX10) {
270       assert(!program->needs_flat_scr);
271       assert(!program->xnack_enabled);
272       return 2;
273    } else if (program->chip_class >= GFX8) {
274       if (program->needs_flat_scr)
275          return 6;
276       else if (program->xnack_enabled)
277          return 4;
278       else if (program->needs_vcc)
279          return 2;
280       else
281          return 0;
282    } else {
283       assert(!program->xnack_enabled);
284       if (program->needs_flat_scr)
285          return 4;
286       else if (program->needs_vcc)
287          return 2;
288       else
289          return 0;
290    }
291 }
292 
get_sgpr_alloc(Program * program,uint16_t addressable_sgprs)293 uint16_t get_sgpr_alloc(Program *program, uint16_t addressable_sgprs)
294 {
295    assert(addressable_sgprs <= program->sgpr_limit);
296    uint16_t sgprs = addressable_sgprs + get_extra_sgprs(program);
297    uint16_t granule = program->sgpr_alloc_granule + 1;
298    return align(std::max(sgprs, granule), granule);
299 }
300 
get_vgpr_alloc(Program * program,uint16_t addressable_vgprs)301 uint16_t get_vgpr_alloc(Program *program, uint16_t addressable_vgprs)
302 {
303    assert(addressable_vgprs <= program->vgpr_limit);
304    uint16_t granule = program->vgpr_alloc_granule + 1;
305    return align(std::max(addressable_vgprs, granule), granule);
306 }
307 
get_addr_sgpr_from_waves(Program * program,uint16_t max_waves)308 uint16_t get_addr_sgpr_from_waves(Program *program, uint16_t max_waves)
309 {
310     uint16_t sgprs = program->physical_sgprs / max_waves & ~program->sgpr_alloc_granule;
311     sgprs -= get_extra_sgprs(program);
312     return std::min(sgprs, program->sgpr_limit);
313 }
314 
get_addr_vgpr_from_waves(Program * program,uint16_t max_waves)315 uint16_t get_addr_vgpr_from_waves(Program *program, uint16_t max_waves)
316 {
317     uint16_t vgprs = 256 / max_waves & ~program->vgpr_alloc_granule;
318     return std::min(vgprs, program->vgpr_limit);
319 }
320 
calc_min_waves(Program * program)321 void calc_min_waves(Program* program)
322 {
323    unsigned waves_per_workgroup = calc_waves_per_workgroup(program);
324    /* currently min_waves is in wave64 waves */
325    if (program->wave_size == 32)
326       waves_per_workgroup = DIV_ROUND_UP(waves_per_workgroup, 2);
327 
328    unsigned simd_per_cu = 4; /* TODO: different on Navi */
329    bool wgp = program->chip_class >= GFX10; /* assume WGP is used on Navi */
330    unsigned simd_per_cu_wgp = wgp ? simd_per_cu * 2 : simd_per_cu;
331 
332    program->min_waves = DIV_ROUND_UP(waves_per_workgroup, simd_per_cu_wgp);
333 }
334 
update_vgpr_sgpr_demand(Program * program,const RegisterDemand new_demand)335 void update_vgpr_sgpr_demand(Program* program, const RegisterDemand new_demand)
336 {
337    /* TODO: max_waves_per_simd, simd_per_cu and the number of physical vgprs for Navi */
338    unsigned max_waves_per_simd = 10;
339    if ((program->family >= CHIP_POLARIS10 && program->family <= CHIP_VEGAM) || program->chip_class >= GFX10_3)
340       max_waves_per_simd = 8;
341    unsigned simd_per_cu = 4;
342 
343    bool wgp = program->chip_class >= GFX10; /* assume WGP is used on Navi */
344    unsigned simd_per_cu_wgp = wgp ? simd_per_cu * 2 : simd_per_cu;
345    unsigned lds_limit = wgp ? program->lds_limit * 2 : program->lds_limit;
346 
347    /* this won't compile, register pressure reduction necessary */
348    if (new_demand.vgpr > program->vgpr_limit || new_demand.sgpr > program->sgpr_limit) {
349       program->num_waves = 0;
350       program->max_reg_demand = new_demand;
351    } else {
352       program->num_waves = program->physical_sgprs / get_sgpr_alloc(program, new_demand.sgpr);
353       program->num_waves = std::min<uint16_t>(program->num_waves, 256 / get_vgpr_alloc(program, new_demand.vgpr));
354       program->max_waves = max_waves_per_simd;
355 
356       /* adjust max_waves for workgroup and LDS limits */
357       unsigned waves_per_workgroup = calc_waves_per_workgroup(program);
358       unsigned workgroups_per_cu_wgp = max_waves_per_simd * simd_per_cu_wgp / waves_per_workgroup;
359       if (program->config->lds_size) {
360          unsigned lds = program->config->lds_size * program->lds_alloc_granule;
361          workgroups_per_cu_wgp = std::min(workgroups_per_cu_wgp, lds_limit / lds);
362       }
363       if (waves_per_workgroup > 1 && program->chip_class < GFX10)
364          workgroups_per_cu_wgp = std::min(workgroups_per_cu_wgp, 16u); /* TODO: is this a SI-only limit? what about Navi? */
365 
366       /* in cases like waves_per_workgroup=3 or lds=65536 and
367        * waves_per_workgroup=1, we want the maximum possible number of waves per
368        * SIMD and not the minimum. so DIV_ROUND_UP is used */
369       program->max_waves = std::min<uint16_t>(program->max_waves, DIV_ROUND_UP(workgroups_per_cu_wgp * waves_per_workgroup, simd_per_cu_wgp));
370 
371       /* incorporate max_waves and calculate max_reg_demand */
372       program->num_waves = std::min<uint16_t>(program->num_waves, program->max_waves);
373       program->max_reg_demand.vgpr = get_addr_vgpr_from_waves(program, program->num_waves);
374       program->max_reg_demand.sgpr = get_addr_sgpr_from_waves(program, program->num_waves);
375    }
376 }
377 
live_var_analysis(Program * program)378 live live_var_analysis(Program* program)
379 {
380    live result;
381    result.live_out.resize(program->blocks.size());
382    result.register_demand.resize(program->blocks.size());
383    std::set<unsigned> worklist;
384    std::vector<uint16_t> phi_sgpr_ops(program->blocks.size());
385    RegisterDemand new_demand;
386 
387    program->needs_vcc = false;
388 
389    /* this implementation assumes that the block idx corresponds to the block's position in program->blocks vector */
390    for (Block& block : program->blocks)
391       worklist.insert(block.index);
392    while (!worklist.empty()) {
393       std::set<unsigned>::reverse_iterator b_it = worklist.rbegin();
394       unsigned block_idx = *b_it;
395       worklist.erase(block_idx);
396       process_live_temps_per_block(program, result, &program->blocks[block_idx], worklist, phi_sgpr_ops);
397       new_demand.update(program->blocks[block_idx].register_demand);
398    }
399 
400    /* calculate the program's register demand and number of waves */
401    update_vgpr_sgpr_demand(program, new_demand);
402 
403    return result;
404 }
405 
406 }
407 
408