• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -*- mesa-c++  -*-
2  *
3  * Copyright (c) 2018-2019 Collabora LTD
4  *
5  * Author: Gert Wollny <gert.wollny@collabora.com>
6  *
7  * Permission is hereby granted, free of charge, to any person obtaining a
8  * copy of this software and associated documentation files (the "Software"),
9  * to deal in the Software without restriction, including without limitation
10  * on the rights to use, copy, modify, merge, publish, distribute, sub
11  * license, and/or sell copies of the Software, and to permit persons to whom
12  * the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the next
15  * paragraph) shall be included in all copies or substantial portions of the
16  * Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
21  * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
22  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
23  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
24  * USE OR OTHER DEALINGS IN THE SOFTWARE.
25  */
26 
27 #include "sfn_debug.h"
28 #include "sfn_value_gpr.h"
29 #include "sfn_valuepool.h"
30 
31 #include <iostream>
32 #include <queue>
33 
34 namespace r600 {
35 
36 using std::vector;
37 using std::pair;
38 using std::make_pair;
39 using std::queue;
40 
ValuePool()41 ValuePool::ValuePool():
42    m_next_register_index(0),
43    current_temp_reg_index(0),
44    next_temp_reg_comp(4)
45 {
46 }
47 
48 PValue ValuePool::m_undef = Value::zero;
49 
vec_from_nir(const nir_dest & dst,int num_components)50 GPRVector ValuePool::vec_from_nir(const nir_dest& dst, int num_components)
51 {
52    std::array<PValue, 4> result;
53    for (int i = 0; i < 4; ++i)
54       result[i] = from_nir(dst, i < num_components ? i : 7);
55    return GPRVector(result);
56 }
57 
varvec_from_nir(const nir_dest & dst,int num_components)58 std::vector<PValue> ValuePool::varvec_from_nir(const nir_dest& dst, int num_components)
59 {
60    std::vector<PValue> result(num_components);
61    for (int i = 0; i < num_components; ++i)
62       result[i] = from_nir(dst, i);
63    return result;
64 }
65 
66 
varvec_from_nir(const nir_src & src,int num_components)67 std::vector<PValue> ValuePool::varvec_from_nir(const nir_src& src, int num_components)
68 {
69    std::vector<PValue> result(num_components);
70    int i;
71    for (i = 0; i < num_components; ++i)
72       result[i] = from_nir(src, i);
73 
74    return result;
75 }
76 
77 
from_nir(const nir_src & v,unsigned component,unsigned swizzled)78 PValue ValuePool::from_nir(const nir_src& v, unsigned component, unsigned swizzled)
79 {
80    sfn_log << SfnLog::reg << "Search " << (v.is_ssa ? "ssa_reg " : "reg ")
81            << (v.is_ssa ? v.ssa->index : v.reg.reg->index);
82 
83    if (!v.is_ssa) {
84       int idx = lookup_register_index(v);
85       sfn_log << SfnLog::reg << "  -> got index " <<  idx << "\n";
86       if (idx >= 0) {
87          auto reg = lookup_register(idx, swizzled, false);
88          if (reg) {
89             if (reg->type() == Value::gpr_vector) {
90                auto& array = static_cast<GPRArray&>(*reg);
91                reg = array.get_indirect(v.reg.base_offset,
92                                         v.reg.indirect ?
93                                            from_nir(*v.reg.indirect, 0, 0) : nullptr,
94                                         component);
95             }
96             return reg;
97          }
98       }
99       assert(0 && "local registers should always be found");
100    }
101 
102    unsigned index = v.ssa->index;
103    /* For undefs we use zero and let ()yet to be implemeneted dce deal with it */
104    if (m_ssa_undef.find(index) != m_ssa_undef.end())
105       return Value::zero;
106 
107 
108    int idx = lookup_register_index(v);
109    sfn_log << SfnLog::reg << "  -> got index " <<  idx << "\n";
110    if (idx >= 0) {
111       auto reg = lookup_register(idx, swizzled, false);
112       if (reg)
113          return reg;
114    }
115 
116    auto literal_val = nir_src_as_const_value(v);
117    if (literal_val) {
118       assert(v.is_ssa);
119       switch (v.ssa->bit_size) {
120       case 1:
121          return PValue(new LiteralValue(literal_val[swizzled].b ? 0xffffffff : 0, component));
122       case 32:
123          return literal(literal_val[swizzled].u32);
124       default:
125          sfn_log << SfnLog::reg << "Unsupported bit size " << v.ssa->bit_size
126                  << " fall back to 32\n";
127          return PValue(new LiteralValue(literal_val[swizzled].u32, component));
128       }
129    }
130 
131    return PValue();
132 }
133 
from_nir(const nir_src & v,unsigned component)134 PValue ValuePool::from_nir(const nir_src& v, unsigned component)
135 {
136    return from_nir(v, component, component);
137 }
138 
from_nir(const nir_tex_src & v,unsigned component)139 PValue ValuePool::from_nir(const nir_tex_src &v, unsigned component)
140 {
141    return from_nir(v.src, component, component);
142 }
143 
from_nir(const nir_alu_src & v,unsigned component)144 PValue ValuePool::from_nir(const nir_alu_src &v, unsigned component)
145 {
146    return from_nir(v.src, component, v.swizzle[component]);
147 }
148 
get_temp_register(int channel)149 PGPRValue ValuePool::get_temp_register(int channel)
150 {
151    /* Skip to next register to get the channel we want */
152    if (channel >= 0) {
153       if (next_temp_reg_comp <= channel)
154          next_temp_reg_comp = channel;
155       else
156          next_temp_reg_comp = 4;
157    }
158 
159    if (next_temp_reg_comp > 3) {
160       current_temp_reg_index = allocate_temp_register();
161       next_temp_reg_comp = 0;
162    }
163    return std::make_shared<GPRValue>(current_temp_reg_index, next_temp_reg_comp++);
164 }
165 
get_temp_vec4()166 GPRVector ValuePool::get_temp_vec4()
167 {
168    int sel = allocate_temp_register();
169    return GPRVector(sel, {0,1,2,3});
170 }
171 
create_register_from_nir_src(const nir_src & src,int comp)172 PValue ValuePool::create_register_from_nir_src(const nir_src& src, int comp)
173 {
174    int idx = src.is_ssa ? get_dst_ssa_register_index(*src.ssa):
175                           get_local_register_index(*src.reg.reg);
176 
177    auto retval = lookup_register(idx, comp, false);
178    if (!retval || retval->type() != Value::gpr || retval->type() != Value::gpr_array_value)
179       retval = create_register(idx, comp);
180    return retval;
181 }
182 
from_nir(const nir_alu_dest & v,unsigned component)183 PValue ValuePool::from_nir(const nir_alu_dest &v, unsigned component)
184 {
185    //assert(v->write_mask & (1 << component));
186    return from_nir(v.dest, component);
187 }
188 
lookup_register_index(const nir_dest & dst)189 int ValuePool::lookup_register_index(const nir_dest& dst)
190 {
191    return dst.is_ssa ? get_dst_ssa_register_index(dst.ssa):
192                        get_local_register_index(*dst.reg.reg);
193 }
194 
lookup_register_index(const nir_src & src) const195 int ValuePool::lookup_register_index(const nir_src& src) const
196 {
197    int index = 0;
198 
199    index = src.is_ssa ?
200               get_ssa_register_index(*src.ssa) :
201               get_local_register_index(*src.reg.reg);
202 
203    sfn_log << SfnLog::reg << " LIDX:" << index;
204 
205    auto r = m_register_map.find(index);
206    if (r == m_register_map.end()) {
207       return -1;
208    }
209    return static_cast<int>(r->second.index);
210 }
211 
212 
allocate_component(unsigned index,unsigned comp,bool pre_alloc)213 int ValuePool::allocate_component(unsigned index, unsigned comp, bool pre_alloc)
214 {
215    assert(comp < 8);
216    return allocate_with_mask(index, 1 << comp, pre_alloc);
217 }
218 
allocate_temp_register()219 int ValuePool::allocate_temp_register()
220 {
221    return m_next_register_index++;
222 }
223 
224 
from_nir(const nir_dest & v,unsigned component)225 PValue ValuePool::from_nir(const nir_dest& v, unsigned component)
226 {
227    int idx = lookup_register_index(v);
228    sfn_log << SfnLog::reg << __func__  << ": ";
229    if (v.is_ssa)
230       sfn_log << "ssa_" << v.ssa.index;
231    else
232       sfn_log << "r" << v.reg.reg->index;
233    sfn_log << " -> " << idx << "\n";
234 
235    auto retval = lookup_register(idx, component, false);
236    if (!retval)
237       retval = create_register(idx, component);
238 
239    if (retval->type() == Value::gpr_vector) {
240       assert(!v.is_ssa);
241       auto& array = static_cast<GPRArray&>(*retval);
242       retval = array.get_indirect(v.reg.base_offset,
243                                   v.reg.indirect ?
244                                   from_nir(*v.reg.indirect, 0, 0) : nullptr,
245                                   component);
246    }
247 
248    return retval;
249 }
250 
get_temp_registers() const251 ValueMap ValuePool::get_temp_registers() const
252 {
253    ValueMap result;
254 
255    for (auto& v : m_registers) {
256       if (v.second->type() == Value::gpr)
257          result.insert(v.second);
258       else if (v.second->type() == Value::gpr_vector) {
259          auto& array = static_cast<GPRArray&>(*v.second);
260          array.collect_registers(result);
261       }
262    }
263    return result;
264 }
265 
266 static const char swz[] = "xyzw01?_";
267 
create_register(unsigned sel,unsigned swizzle)268 PValue ValuePool::create_register(unsigned sel, unsigned swizzle)
269 {
270    sfn_log << SfnLog::reg
271            <<"Create register " << sel  << '.' << swz[swizzle] << "\n";
272    auto retval = PValue(new GPRValue(sel, swizzle));
273    m_registers[(sel << 3) + swizzle] = retval;
274    return retval;
275 }
276 
inject_register(unsigned sel,unsigned swizzle,const PValue & reg,bool map)277 bool ValuePool::inject_register(unsigned sel, unsigned swizzle,
278                                 const PValue& reg, bool map)
279 {
280    uint32_t ssa_index = sel;
281 
282    if (map) {
283       auto pos = m_ssa_register_map.find(sel);
284       if (pos == m_ssa_register_map.end())
285          ssa_index = m_next_register_index++;
286       else
287          ssa_index = pos->second;
288    }
289 
290    sfn_log << SfnLog::reg
291            << "Inject register " << sel  << '.' << swz[swizzle]
292            << " at index " <<  ssa_index << " ...";
293 
294    if (map)
295       m_ssa_register_map[sel] = ssa_index;
296 
297    allocate_with_mask(ssa_index, swizzle, true);
298 
299    unsigned idx = (ssa_index << 3) + swizzle;
300    auto p = m_registers.find(idx);
301    if ( (p != m_registers.end()) && *p->second != *reg) {
302       std::cerr << "Register location (" << ssa_index << ", " << swizzle << ") was already reserved\n";
303       assert(0);
304       return false;
305    }
306    sfn_log << SfnLog::reg << " at idx:" << idx << " to " << *reg << "\n";
307    m_registers[idx] = reg;
308 
309    if (m_next_register_index <= ssa_index)
310       m_next_register_index = ssa_index + 1;
311    return true;
312 }
313 
314 
lookup_register(unsigned sel,unsigned swizzle,bool required)315 PValue ValuePool::lookup_register(unsigned sel, unsigned swizzle,
316                                   bool required)
317 {
318 
319    PValue retval;
320    sfn_log << SfnLog::reg
321            << "lookup register " << sel  << '.' << swz[swizzle] << "("
322            << ((sel << 3) + swizzle) << ")...";
323 
324 
325    auto reg = m_registers.find((sel << 3) + swizzle);
326    if (reg != m_registers.end()) {
327       sfn_log << SfnLog::reg << " -> Found " << *reg->second << "\n";
328       retval = reg->second;
329    } else if (swizzle == 7) {
330       PValue retval = create_register(sel, swizzle);
331       sfn_log << SfnLog::reg << " -> Created " << *retval << "\n";
332    } else if (required) {
333       sfn_log << SfnLog::reg << "Register (" << sel << ", "
334               << swizzle << ") not found but required\n";
335       assert(0 && "Unallocated register value requested\n");
336    }
337    sfn_log << SfnLog::reg << " -> Not required and not  allocated\n";
338    return retval;
339 }
340 
get_dst_ssa_register_index(const nir_ssa_def & ssa)341 unsigned ValuePool::get_dst_ssa_register_index(const nir_ssa_def& ssa)
342 {
343    sfn_log << SfnLog::reg << __func__ << ": search dst ssa "
344            << ssa.index;
345 
346    auto pos = m_ssa_register_map.find(ssa.index);
347    if (pos == m_ssa_register_map.end()) {
348       sfn_log << SfnLog::reg << " Need to allocate ...";
349       allocate_ssa_register(ssa);
350       pos = m_ssa_register_map.find(ssa.index);
351       assert(pos != m_ssa_register_map.end());
352    }
353    sfn_log << SfnLog::reg << "... got " << pos->second << "\n";
354    return pos->second;
355 }
356 
get_ssa_register_index(const nir_ssa_def & ssa) const357 unsigned ValuePool::get_ssa_register_index(const nir_ssa_def& ssa) const
358 {
359    sfn_log << SfnLog::reg << __func__ << ": search ssa "
360            << ssa.index;
361 
362    auto pos = m_ssa_register_map.find(ssa.index);
363    sfn_log << SfnLog::reg << " got " << pos->second<< "\n";
364    if (pos == m_ssa_register_map.end()) {
365       sfn_log << SfnLog::reg << __func__ << ": ssa register "
366               << ssa.index << " lookup failed\n";
367       return -1;
368    }
369    return pos->second;
370 }
371 
get_local_register_index(const nir_register & reg)372 unsigned ValuePool::get_local_register_index(const nir_register& reg)
373 {
374    auto pos = m_local_register_map.find(reg.index);
375    if (pos == m_local_register_map.end()) {
376       allocate_local_register(reg);
377       pos = m_local_register_map.find(reg.index);
378       assert(pos != m_local_register_map.end());
379    }
380    return pos->second;
381 }
382 
get_local_register_index(const nir_register & reg) const383 unsigned ValuePool::get_local_register_index(const nir_register& reg) const
384 {
385    auto pos = m_local_register_map.find(reg.index);
386    if (pos == m_local_register_map.end()) {
387       sfn_log << SfnLog::err << __func__ << ": local register "
388               << reg.index << " lookup failed";
389       return -1;
390    }
391    return pos->second;
392 }
393 
allocate_ssa_register(const nir_ssa_def & ssa)394 void ValuePool::allocate_ssa_register(const nir_ssa_def& ssa)
395 {
396    sfn_log << SfnLog::reg << "ValuePool: Allocate ssa register " << ssa.index
397            << " as " << m_next_register_index << "\n";
398    int index = m_next_register_index++;
399    m_ssa_register_map[ssa.index] = index;
400    allocate_with_mask(index, 0xf, true);
401 }
402 
allocate_arrays(array_list & arrays)403 void ValuePool::allocate_arrays(array_list& arrays)
404 {
405    int ncomponents = 0;
406    int current_index = m_next_register_index;
407    unsigned instance = 0;
408 
409    while (!arrays.empty()) {
410       auto a = arrays.top();
411       arrays.pop();
412 
413       /* This is a bit hackish, return an id that encodes the array merge. To make sure
414        * that the mapping doesn't go wrong we have to make sure the arrays is longer than
415        * the number of instances in this arrays slot */
416       if (a.ncomponents + ncomponents > 4 ||
417           a.length < instance) {
418          current_index = m_next_register_index;
419          ncomponents = 0;
420          instance = 0;
421       }
422 
423       if (ncomponents == 0)
424          m_next_register_index += a.length;
425 
426       uint32_t mask = ((1 << a.ncomponents) - 1) << ncomponents;
427 
428       PValue  array = PValue(new GPRArray(current_index, a.length, mask, ncomponents));
429 
430       sfn_log << SfnLog::reg << "Add array at "<< current_index
431               << " of size " << a.length << " with " << a.ncomponents
432               << " components, mask " << mask << "\n";
433 
434       m_local_register_map[a.index] = current_index + instance;
435 
436       for (unsigned  i = 0; i < a.ncomponents; ++i)
437          m_registers[((current_index  + instance) << 3) + i] = array;
438 
439       VRec next_reg = {current_index + instance, mask, mask};
440       m_register_map[current_index + instance] = next_reg;
441 
442       ncomponents += a.ncomponents;
443       ++instance;
444    }
445 }
446 
allocate_local_register(const nir_register & reg)447 void ValuePool::allocate_local_register(const nir_register& reg)
448 {
449    int index = m_next_register_index++;
450    m_local_register_map[reg.index] = index;
451    allocate_with_mask(index, 0xf, true);
452 
453    /* Create actual register and map it */;
454    for (int i = 0; i < 4; ++i) {
455       int k = (index << 3) + i;
456       m_registers[k] = PValue(new GPRValue(index, i));
457    }
458 }
459 
allocate_local_register(const nir_register & reg,array_list & arrays)460 void ValuePool::allocate_local_register(const nir_register& reg, array_list& arrays)
461 {
462    sfn_log << SfnLog::reg << "ValuePool: Allocate local register " << reg.index
463            << " as " << m_next_register_index << "\n";
464 
465    if (reg.num_array_elems) {
466       array_entry ae = {reg.index, reg.num_array_elems, reg.num_components};
467       arrays.push(ae);
468    }
469    else
470       allocate_local_register(reg);
471 }
472 
create_undef(nir_ssa_undef_instr * instr)473 bool ValuePool::create_undef(nir_ssa_undef_instr* instr)
474 {
475    m_ssa_undef.insert(instr->def.index);
476    return true;
477 }
478 
allocate_with_mask(unsigned index,unsigned mask,bool pre_alloc)479 int ValuePool::allocate_with_mask(unsigned index, unsigned mask, bool pre_alloc)
480 {
481    int retval;
482    VRec next_register = { index, mask };
483 
484    sfn_log << SfnLog::reg << (pre_alloc ? "Pre-alloc" : "Allocate")
485            << " register (" << index << ", " << mask << ")\n";
486    retval = index;
487    auto r = m_register_map.find(index);
488 
489    if (r != m_register_map.end()) {
490       if ((r->second.mask & next_register.mask) &&
491           !(r->second.pre_alloc_mask & next_register.mask)) {
492          std::cerr << "r600 ERR: register ("
493                    << index << ", " << mask
494                    << ") already allocated as (" << r->second.index << ", "
495                    << r->second.mask << ", " << r->second.pre_alloc_mask
496                    << ") \n";
497          retval = -1;
498       } else {
499          r->second.mask |= next_register.mask;
500          if (pre_alloc)
501             r->second.pre_alloc_mask |= next_register.mask;
502          retval = r->second.index;
503       }
504    } else  {
505       if (pre_alloc)
506          next_register.pre_alloc_mask = mask;
507       m_register_map[index] = next_register;
508       retval = next_register.index;
509    }
510 
511    sfn_log << SfnLog::reg << "Allocate register (" << index << "," << mask << ") in R"
512            << retval << "\n";
513 
514    return retval;
515 }
516 
literal(uint32_t value)517 PValue ValuePool::literal(uint32_t value)
518 {
519    auto l = m_literals.find(value);
520    if (l != m_literals.end())
521       return l->second;
522 
523    m_literals[value] = PValue(new LiteralValue(value));
524    return m_literals[value];
525 }
526 
527 }
528