gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
hsail_code.hh
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-2015 Advanced Micro Devices, Inc.
3  * All rights reserved.
4  *
5  * For use for simulation and test purposes only
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its contributors
18  * may be used to endorse or promote products derived from this software
19  * without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Author: Steve Reinhardt
34  */
35 
36 #ifndef __HSAIL_CODE_HH__
37 #define __HSAIL_CODE_HH__
38 
39 #include <cassert>
40 #include <list>
41 #include <map>
42 #include <string>
43 #include <vector>
44 
45 #include "arch/gpu_decoder.hh"
46 #include "arch/hsail/Brig.h"
47 #include "base/addr_range_map.hh"
48 #include "base/intmath.hh"
49 #include "config/the_gpu_isa.hh"
50 #include "gpu-compute/hsa_code.hh"
52 #include "gpu-compute/misc.hh"
53 
54 class BrigObject;
55 class GPUStaticInst;
56 
57 inline int
58 popcount(uint64_t src, int sz)
59 {
60  int cnt = 0;
61 
62  for (int i = 0; i < sz; ++i) {
63  if (src & 1)
64  ++cnt;
65  src >>= 1;
66  }
67 
68  return cnt;
69 }
70 
71 inline int
72 firstbit(uint64_t src, int sz)
73 {
74  int i;
75 
76  for (i = 0; i < sz; ++i) {
77  if (src & 1)
78  break;
79  src >>= 1;
80  }
81 
82  return i;
83 }
84 
85 inline int
86 lastbit(uint64_t src, int sz)
87 {
88  int i0 = -1;
89 
90  for (int i = 0; i < sz; ++i) {
91  if (src & 1)
92  i0 = i;
93  src >>= 1;
94  }
95 
96  return i0;
97 }
98 
99 inline int
100 signbit(uint64_t src, int sz)
101 {
102  int i0 = -1;
103 
104  if (src & (1 << (sz - 1))) {
105  for (int i = 0; i < sz - 1; ++i) {
106  if (!(src & 1))
107  i0 = i;
108  src >>= 1;
109  }
110  } else {
111  for (int i = 0; i < sz - 1; ++i) {
112  if (src & 1)
113  i0 = i;
114  src >>= 1;
115  }
116  }
117 
118  return i0;
119 }
120 
121 inline uint64_t
122 bitrev(uint64_t src, int sz)
123 {
124  uint64_t r = 0;
125 
126  for (int i = 0; i < sz; ++i) {
127  r <<= 1;
128  if (src & 1)
129  r |= 1;
130  src >>= 1;
131  }
132 
133  return r;
134 }
135 
136 inline uint64_t
137 mul_hi(uint32_t a, uint32_t b)
138 {
139  return ((uint64_t)a * (uint64_t)b) >> 32;
140 }
141 
142 inline uint64_t
143 mul_hi(int32_t a, int32_t b)
144 {
145  return ((int64_t)a * (int64_t)b) >> 32;
146 }
147 
148 inline uint64_t
149 mul_hi(uint64_t a, uint64_t b)
150 {
151  return ((uint64_t)a * (uint64_t)b) >> 32;
152 }
153 
154 inline uint64_t
155 mul_hi(int64_t a, int64_t b)
156 {
157  return ((int64_t)a * (int64_t)b) >> 32;
158 }
159 
160 inline uint64_t
161 mul_hi(double a, double b)
162 {
163  return 0;
164 }
165 
166 class Label
167 {
168  public:
169  std::string name;
170  int value;
171 
172  Label() : value(-1)
173  {
174  }
175 
176  bool defined() { return value != -1; }
177 
178  void
179  checkName(std::string &_name)
180  {
181  if (name.empty()) {
182  name = _name;
183  } else {
184  assert(name == _name);
185  }
186  }
187 
188  void
189  define(std::string &_name, int _value)
190  {
191  assert(!defined());
192  assert(_value != -1);
193  value = _value;
194  checkName(_name);
195  }
196 
197  int
198  get()
199  {
200  assert(defined());
201  return value;
202  }
203 };
204 
205 class LabelMap
206 {
207  std::map<std::string, Label> map;
208 
209  public:
210  LabelMap() { }
211 
212  void addLabel(const Brig::BrigDirectiveLabel *lbl, int inst_index,
213  const BrigObject *obj);
214 
216  const BrigObject *obj);
217 };
218 
220 
221 extern const char *segmentNames[];
222 
224 {
225  public:
226  std::string name;
227  uint64_t offset;
228 
229  uint64_t size;
231  StorageElement(const char *_name, uint64_t _offset, int _size,
232  const Brig::BrigDirectiveVariable *sym)
233  : name(_name), offset(_offset), size(_size), brigSymbol(sym)
234  {
235  }
236 };
237 
239 {
240  typedef std::map<const Brig::BrigDirectiveVariable*, StorageElement*>
242 
246 
247  uint64_t nextOffset;
249 
250  public:
252  : nextOffset(0), segment(_class)
253  {
254  }
255 
257  const BrigObject *obj);
258 
259  StorageElement* findSymbol(std::string name);
260  StorageElement* findSymbol(uint64_t addr);
262 
263  int getSize() { return nextOffset; }
264  void resetOffset() { nextOffset = 0; }
265 };
266 
268 {
271 
272  public:
273  StorageMap(StorageMap *outerScope = nullptr);
274 
276  const BrigObject *obj);
277 
278  StorageElement* findSymbol(Brig::BrigSegment segment, std::string name);
279  StorageElement* findSymbol(Brig::BrigSegment segment, uint64_t addr);
280 
282  const Brig::BrigDirectiveVariable *brigptr);
283 
284  // overloaded version to avoid casting
286  findSymbol(Brig::BrigSegment8_t segment, std::string name)
287  {
288  return findSymbol((Brig::BrigSegment)segment, name);
289  }
290 
291  int getSize(Brig::BrigSegment segment);
292  void resetOffset(Brig::BrigSegment segment);
293 };
294 
295 typedef enum
296 {
311 } base_type_e;
312 
313 /* @class HsailCode
314  * the HsailCode class is used to store information
315  * about HSA kernels stored in the BRIG format. it holds
316  * all information about a kernel, function, or variable
317  * symbol and provides methods for accessing that
318  * information.
319  */
320 
321 class HsailCode final : public HsaCode
322 {
323  public:
324  TheGpuISA::Decoder decoder;
325 
328  uint32_t kernarg_start;
329  uint32_t kernarg_end;
330  int32_t private_size;
331 
332  int32_t readonly_size;
333 
334  // We track the maximum register index used for each register
335  // class when we load the code so we can size the register files
336  // appropriately (i.e., one more than the max index).
337  uint32_t max_creg; // maximum c-register index
338  uint32_t max_sreg; // maximum s-register index
339  uint32_t max_dreg; // maximum d-register index
340 
341  HsailCode(const std::string &name_str,
342  const Brig::BrigDirectiveExecutable *code_dir,
343  const BrigObject *obj,
344  StorageMap *objStorageMap);
345 
346  // this version is used to create a placeholder when
347  // we encounter a kernel-related directive before the
348  // kernel itself
349  HsailCode(const std::string &name_str);
350 
351  void init(const Brig::BrigDirectiveExecutable *code_dir,
352  const BrigObject *obj, StorageMap *objStorageMap);
353 
354  void
355  generateHsaKernelInfo(HsaKernelInfo *hsaKernelInfo) const
356  {
357  hsaKernelInfo->sRegCount = max_sreg + 1;
358  hsaKernelInfo->dRegCount = max_dreg + 1;
359  hsaKernelInfo->cRegCount = max_creg + 1;
360 
362 
363  hsaKernelInfo->private_mem_size =
365 
366  hsaKernelInfo->spill_mem_size =
368  }
369 
370  int
371  getSize(MemorySegment segment) const
372  {
373  Brig::BrigSegment brigSeg;
374 
375  switch (segment) {
376  case MemorySegment::NONE:
377  brigSeg = Brig::BRIG_SEGMENT_NONE;
378  break;
379  case MemorySegment::FLAT:
380  brigSeg = Brig::BRIG_SEGMENT_FLAT;
381  break;
383  brigSeg = Brig::BRIG_SEGMENT_GLOBAL;
384  break;
386  brigSeg = Brig::BRIG_SEGMENT_READONLY;
387  break;
389  brigSeg = Brig::BRIG_SEGMENT_KERNARG;
390  break;
392  brigSeg = Brig::BRIG_SEGMENT_GROUP;
393  break;
395  brigSeg = Brig::BRIG_SEGMENT_PRIVATE;
396  break;
398  brigSeg = Brig::BRIG_SEGMENT_SPILL;
399  break;
400  case MemorySegment::ARG:
401  brigSeg = Brig::BRIG_SEGMENT_ARG;
402  break;
404  brigSeg = Brig::BRIG_SEGMENT_AMD_GCN;
405  break;
406  default:
407  fatal("Unknown BrigSegment type.\n");
408  }
409 
410  return getSize(brigSeg);
411  }
412 
413  private:
414  int
415  getSize(Brig::BrigSegment segment) const
416  {
417  if (segment == Brig::BRIG_SEGMENT_PRIVATE) {
418  // with the code generated by new HSA compiler the assertion
419  // does not hold anymore..
420  //assert(private_size != -1);
421  return private_size;
422  } else {
423  return storageMap->getSize(segment);
424  }
425  }
426 
427  public:
429  findSymbol(Brig::BrigSegment segment, uint64_t addr)
430  {
431  return storageMap->findSymbol(segment, addr);
432  }
433 
434  void
435  setPrivateSize(int32_t _private_size)
436  {
437  private_size = _private_size;
438  }
439 
440  Label*
442  {
443  return labelMap.refLabel(lbl, obj);
444  }
445 };
446 
447 #endif // __HSAIL_CODE_HH__
StorageElement(const char *_name, uint64_t _offset, int _size, const Brig::BrigDirectiveVariable *sym)
Definition: hsail_code.hh:231
int getSize(Brig::BrigSegment segment)
Definition: hsail_code.cc:378
int getSize(MemorySegment segment) const
Definition: hsail_code.hh:371
const std::string & name()
Definition: trace.cc:49
Bitfield< 7 > i
Definition: miscregs.hh:1378
uint32_t spill_mem_size
Label * refLabel(const Brig::BrigDirectiveLabel *lbl, const BrigObject *obj)
Definition: hsail_code.cc:242
Label * refLabel(const Brig::BrigDirectiveLabel *lbl, const BrigObject *obj)
Definition: hsail_code.hh:441
StorageElement * addSymbol(const Brig::BrigDirectiveVariable *sym, const BrigObject *obj)
Definition: hsail_code.cc:367
Bitfield< 8 > a
Definition: miscregs.hh:1377
void generateHsaKernelInfo(HsaKernelInfo *hsaKernelInfo) const
Definition: hsail_code.hh:355
ip6_addr_t addr
Definition: inet.hh:335
void checkName(std::string &_name)
Definition: hsail_code.hh:179
bool defined()
Definition: hsail_code.hh:176
int lastbit(uint64_t src, int sz)
Definition: hsail_code.hh:86
DirVarToSE_map elements_by_brigptr
Definition: hsail_code.hh:245
void setPrivateSize(int32_t _private_size)
Definition: hsail_code.hh:435
StorageElement * findSymbol(Brig::BrigSegment segment, std::string name)
Definition: hsail_code.cc:404
LabelMap labelMap
Definition: hsail_code.hh:327
Brig::BrigSegment segment
Definition: hsail_code.hh:248
T roundUp(const T &val, const U &align)
Definition: intmath.hh:205
StorageSpace * space[NumSegments]
Definition: hsail_code.hh:270
uint32_t static_lds_size
StorageElement * addSymbol(const Brig::BrigDirectiveVariable *sym, const BrigObject *obj)
Definition: hsail_code.cc:288
const Brig::BrigDirectiveVariable * brigSymbol
Definition: hsail_code.hh:230
void define(std::string &_name, int _value)
Definition: hsail_code.hh:189
int32_t private_size
Definition: hsail_code.hh:330
uint32_t dRegCount
void resetOffset(Brig::BrigSegment segment)
Definition: hsail_code.cc:398
Bitfield< 7 > b
Definition: miscregs.hh:1564
StorageElement * findSymbol(Brig::BrigSegment8_t segment, std::string name)
Definition: hsail_code.hh:286
uint64_t offset
Definition: hsail_code.hh:227
int32_t readonly_size
Definition: hsail_code.hh:332
StorageElement * findSymbol(Brig::BrigSegment segment, uint64_t addr)
Definition: hsail_code.hh:429
uint64_t nextOffset
Definition: hsail_code.hh:247
#define fatal(...)
Definition: misc.hh:163
StorageElement * findSymbol(std::string name)
Definition: hsail_code.cc:320
MemorySegment
Definition: hsa_code.hh:63
StorageMap * storageMap
Definition: hsail_code.hh:326
uint32_t private_mem_size
std::string name
Definition: hsail_code.hh:226
const char * segmentNames[]
Definition: brig_object.cc:69
int value
Definition: hsail_code.hh:170
const int NumSegments
Definition: hsail_code.hh:219
StorageMap(StorageMap *outerScope=nullptr)
Definition: hsail_code.cc:359
base_type_e
Definition: hsail_code.hh:295
std::string name
Definition: hsail_code.hh:169
uint64_t bitrev(uint64_t src, int sz)
Definition: hsail_code.hh:122
uint64_t size
Definition: hsail_code.hh:229
uint32_t kernarg_end
Definition: hsail_code.hh:329
std::list< StorageElement * > elements
Definition: hsail_code.hh:243
BrigSegment
Definition: Brig.h:925
TheGpuISA::Decoder decoder
Definition: hsail_code.hh:324
AddrRangeMap< StorageElement * > elements_by_addr
Definition: hsail_code.hh:244
uint32_t cRegCount
int getSize(Brig::BrigSegment segment) const
Definition: hsail_code.hh:415
uint32_t max_creg
Definition: hsail_code.hh:337
void init(const Brig::BrigDirectiveExecutable *code_dir, const BrigObject *obj, StorageMap *objStorageMap)
Definition: hsail_code.cc:58
int signbit(uint64_t src, int sz)
Definition: hsail_code.hh:100
std::map< std::string, Label > map
Definition: hsail_code.hh:207
void resetOffset()
Definition: hsail_code.hh:264
uint32_t max_dreg
Definition: hsail_code.hh:339
uint32_t max_sreg
Definition: hsail_code.hh:338
uint8_t BrigSegment8_t
Definition: Brig.h:138
HsailCode(const std::string &name_str, const Brig::BrigDirectiveExecutable *code_dir, const BrigObject *obj, StorageMap *objStorageMap)
Definition: hsail_code.cc:218
StorageMap * outerScopeMap
Definition: hsail_code.hh:269
StorageSpace(Brig::BrigSegment _class)
Definition: hsail_code.hh:251
uint32_t kernarg_start
Definition: hsail_code.hh:328
uint32_t sRegCount
int firstbit(uint64_t src, int sz)
Definition: hsail_code.hh:72
uint64_t mul_hi(uint32_t a, uint32_t b)
Definition: hsail_code.hh:137
int popcount(uint64_t src, int sz)
Definition: hsail_code.hh:58
std::map< const Brig::BrigDirectiveVariable *, StorageElement * > DirVarToSE_map
Definition: hsail_code.hh:241
void addLabel(const Brig::BrigDirectiveLabel *lbl, int inst_index, const BrigObject *obj)
Definition: hsail_code.cc:227

Generated on Fri Jun 9 2017 13:03:48 for gem5 by doxygen 1.8.6