CLRX  1
An unofficial OpenCL extensions designed for Radeon GPUs
GalliumBinaries.h
Go to the documentation of this file.
1 /*
2  * CLRadeonExtender - Unofficial OpenCL Radeon Extensions Library
3  * Copyright (C) 2014-2016 Mateusz Szpakowski
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
23 #ifndef __CLRX_GALLIUMBINARIES_H__
24 #define __CLRX_GALLIUMBINARIES_H__
25 
26 #include <CLRX/Config.h>
27 #include <cstdint>
28 #include <string>
29 #include <vector>
30 #include <ostream>
31 #include <utility>
32 #include <memory>
33 #include <CLRX/amdbin/Elf.h>
35 #include <CLRX/utils/MemAccess.h>
36 #include <CLRX/utils/Utilities.h>
37 #include <CLRX/utils/GPUId.h>
38 #include <CLRX/utils/Containers.h>
39 #include <CLRX/utils/InputOutput.h>
40 
42 namespace CLRX
43 {
44 
45 enum : Flags {
48 
51 
53 
56 };
57 
59 enum class GalliumArgType: cxbyte
60 {
61  SCALAR = 0,
62  CONSTANT,
63  GLOBAL,
64  LOCAL,
69  SAMPLER,
70  MAX_VALUE = SAMPLER
71 };
72 
74 enum class GalliumArgSemantic: cxbyte
75 {
76  GENERAL = 0,
78  GRID_OFFSET,
79  IMAGE_SIZE,
80  IMAGE_FORMAT,
81  MAX_VALUE = IMAGE_FORMAT
82 };
83 
86 {
87  uint32_t address;
88  uint32_t value;
89 };
90 
93 {
95  bool signExtended;
97  uint32_t size;
98  uint32_t targetSize;
99  uint32_t targetAlign;
100 };
101 
104 {
106  uint32_t sectionId;
107  uint32_t offset;
109 };
110 
112 enum class GalliumSectionType: cxbyte
113 {
114  TEXT = 0,
115  DATA_CONSTANT,
116  DATA_GLOBAL,
117  DATA_LOCAL,
118  DATA_PRIVATE,
119  MAX_VALUE = DATA_PRIVATE
120 };
121 
124 {
125  uint32_t sectionId;
127  uint32_t offset;
128  uint32_t size;
129 };
130 
133 {
134 public:
137 protected:
138  uint32_t progInfosNum;
140  ProgInfoEntryIndexMap progInfoEntryMap;
141  size_t disasmSize;
142  size_t disasmOffset;
143 
145  template<typename ElfBinary>
146  void loadFromElf(ElfBinary& elfBinary);
147 public:
151  virtual ~GalliumElfBinaryBase();
152 
154  uint32_t getProgramInfosNum() const
155  { return progInfosNum; }
156 
158  uint32_t getProgramInfoEntriesNum(uint32_t index) const;
159 
161  uint32_t getProgramInfoEntryIndex(const char* name) const;
162 
164  const GalliumProgInfoEntry* getProgramInfo(const char* name) const
165  { return reinterpret_cast<GalliumProgInfoEntry*>(progInfoEntries) +
166  getProgramInfoEntryIndex(name); }
167 
170  { return reinterpret_cast<GalliumProgInfoEntry*>(progInfoEntries) +
171  getProgramInfoEntryIndex(name); }
172 
174  const GalliumProgInfoEntry* getProgramInfo(uint32_t index) const;
176  GalliumProgInfoEntry* getProgramInfo(uint32_t index);
177 
179  bool hasDisassembly() const
180  { return disasmOffset != 0; }
181 
183  size_t getDisassemblySize() const
184  { return disasmSize; }
185 };
186 
187 /* INFO: in this file is used ULEV function for conversion
188  * from LittleEndian and unaligned access to other memory access policy and endianness
189  * Please use this function whenever you want to get or set word in ELF binary,
190  * because ELF binaries can be unaligned in memory (as inner binaries).
191  */
193 
195 {
196 public:
200  GalliumElfBinary32(size_t binaryCodeSize, cxbyte* binaryCode, Flags creationFlags);
202  virtual ~GalliumElfBinary32();
203 
205  bool hasProgInfoMap() const
206  { return (creationFlags & GALLIUM_ELF_CREATE_PROGINFOMAP) != 0; }
207 
209  const char* getDisassembly() const
210  { return reinterpret_cast<const char*>(binaryCode + disasmOffset); }
211 };
212 
215 {
216 public:
220  GalliumElfBinary64(size_t binaryCodeSize, cxbyte* binaryCode, Flags creationFlags);
222  virtual ~GalliumElfBinary64();
223 
225  bool hasProgInfoMap() const
226  { return (creationFlags & GALLIUM_ELF_CREATE_PROGINFOMAP) != 0; }
227 
229  const char* getDisassembly() const
230  { return reinterpret_cast<const char*>(binaryCode + disasmOffset); }
231 };
232 
236 {
237 private:
238  Flags creationFlags;
239  size_t binaryCodeSize;
240  cxbyte* binaryCode;
241  uint32_t kernelsNum;
242  uint32_t sectionsNum;
243  std::unique_ptr<GalliumKernel[]> kernels;
244  std::unique_ptr<GalliumSection[]> sections;
245 
246  bool elf64BitBinary;
247  std::unique_ptr<GalliumElfBinaryBase> elfBinary;
248 public:
250  GalliumBinary(size_t binaryCodeSize, cxbyte* binaryCode, Flags creationFlags);
252  ~GalliumBinary() = default;
253 
256  { return creationFlags; }
257 
259  size_t getSize() const
260  { return binaryCodeSize; }
261 
263  operator bool() const
264  { return binaryCode!=nullptr; }
265 
267  bool operator!() const
268  { return binaryCode==nullptr; }
269 
271  const cxbyte* getBinaryCode() const
272  { return binaryCode; }
273 
275  cxbyte* getBinaryCode()
276  { return binaryCode; }
278  bool is64BitElfBinary() const
279  { return elf64BitBinary; }
280 
283  { return *static_cast<GalliumElfBinary32*>(elfBinary.get()); }
284 
287  { return *static_cast<const GalliumElfBinary32*>(elfBinary.get()); }
288 
291  { return *static_cast<GalliumElfBinary64*>(elfBinary.get()); }
292 
295  { return *static_cast<const GalliumElfBinary64*>(elfBinary.get()); }
296 
298  uint32_t getSectionsNum() const
299  { return sectionsNum; }
300 
302  uint32_t getSectionSize(uint32_t index) const
303  { return sections[index].size; }
304 
306  const cxbyte* getSectionContent(uint32_t index) const
307  { return binaryCode + sections[index].offset; }
308 
310  cxbyte* getSectionContent(uint32_t index)
311  { return binaryCode + sections[index].offset; }
312 
314  const GalliumSection& getSection(uint32_t index) const
315  { return sections[index]; }
316 
318  uint32_t getKernelsNum() const
319  { return kernelsNum;; }
320 
322  uint32_t getKernelIndex(const char* name) const;
323 
325  const GalliumKernel& getKernel(uint32_t index) const
326  { return kernels[index]; }
327 
329  const GalliumKernel& getKernel(const char* name) const
330  { return kernels[getKernelIndex(name)]; }
331 };
332 
333 enum: cxuint {
334  GALLIUMSECTID_GPUCONFIG = ELFSECTID_OTHER_BUILTIN,
335  GALLIUMSECTID_NOTEGNUSTACK,
336  GALLIUMSECTID_MAX = GALLIUMSECTID_NOTEGNUSTACK
337 };
338 
339 /*
340  * Gallium Binary generator
341  */
342 
345 {
346  cxuint dimMask;
347  cxuint usedVGPRsNum;
348  cxuint usedSGPRsNum;
349  uint32_t pgmRSRC1;
350  uint32_t pgmRSRC2;
351  cxbyte userDataNum;
352  bool ieeeMode;
353  cxbyte floatMode;
354  cxbyte priority;
355  cxbyte exceptions;
356  bool tgSize;
357  bool debugMode;
359  bool dx10Clamp;
360  size_t localSize;
361  uint32_t scratchBufferSize;
362 };
363 
366 {
368  GalliumProgInfoEntry progInfo[3];
369  bool useConfig;
371  uint32_t offset;
372  std::vector<GalliumArgInfo> argInfos;
373 };
374 
377 {
378  bool is64BitElf;
380  size_t globalDataSize;
381  const cxbyte* globalData;
382  std::vector<GalliumKernelInput> kernels;
383  size_t codeSize;
384  const cxbyte* code;
385  size_t commentSize;
386  const char* comment;
387  std::vector<BinSection> extraSections;
388  std::vector<BinSymbol> extraSymbols;
389 
391  void addEmptyKernel(const char* kernelName);
392 };
393 
396 {
397 private:
398  bool manageable;
399  const GalliumInput* input;
400 
401  void generateInternal(std::ostream* osPtr, std::vector<char>* vPtr,
402  Array<cxbyte>* aPtr) const;
403 public:
406  GalliumBinGenerator(const GalliumInput* galliumInput);
407 
409 
418  GalliumBinGenerator(bool _64bitMode, GPUDeviceType deviceType, size_t codeSize,
419  const cxbyte* code, size_t globalDataSize, const cxbyte* globalData,
420  const std::vector<GalliumKernelInput>& kernels);
422  GalliumBinGenerator(bool _64bitMode, GPUDeviceType deviceType, size_t codeSize,
423  const cxbyte* code, size_t globalDataSize, const cxbyte* globalData,
424  std::vector<GalliumKernelInput>&& kernels);
426 
428  const GalliumInput* getInput() const
429  { return input; }
430 
432  void setInput(const GalliumInput* input);
433 
435  void generate(Array<cxbyte>& array) const;
436 
438  void generate(std::ostream& os) const;
439 
441  void generate(std::vector<char>& vector) const;
442 };
443 
444 };
445 
446 #endif
uint32_t pgmRSRC2
pgmRSRC2 register value
Definition: GalliumBinaries.h:350
bool privilegedMode
prvileged mode
Definition: GalliumBinaries.h:358
GalliumElfBinary64 & getElfBinary64()
returns Gallium inner ELF 64-bit binary
Definition: GalliumBinaries.h:290
Flags getCreationFlags()
get creation flags
Definition: GalliumBinaries.h:255
pointer to local memory
bool is64BitElf
is 64-bit elf binary
Definition: GalliumBinaries.h:378
kernel config
Definition: GalliumBinaries.h:344
non copyable and non movable base structure (class)
Definition: Utilities.h:43
uint32_t Flags
type for declaring various flags
Definition: Utilities.h:97
pointer to global memory
uint32_t sectionId
section id
Definition: GalliumBinaries.h:106
const cxbyte * getSectionContent(uint32_t index) const
get content for section with specified index
Definition: GalliumBinaries.h:306
GalliumProgInfoEntry * getProgramInfo(const char *name)
returns program info entries for specified kernel name
Definition: GalliumBinaries.h:169
size_t localSize
used local size (not local defined in kernel arguments)
Definition: GalliumBinaries.h:360
Array< std::pair< const char *, size_t > > ProgInfoEntryIndexMap
program info entry index map
Definition: GalliumBinaries.h:136
uint32_t address
address
Definition: GalliumBinaries.h:87
create elf proginfomap
Definition: GalliumBinaries.h:52
Gallium binarie&#39;s Section.
Definition: GalliumBinaries.h:123
cxbyte priority
priority
Definition: GalliumBinaries.h:354
uint32_t value
value
Definition: GalliumBinaries.h:88
GalliumProgInfoEntry * progInfoEntries
program info entries
Definition: GalliumBinaries.h:139
class ElfBinaryTemplate< Elf64Types > ElfBinary64
type for 64-bit ELF binary
Definition: ElfBinaries.h:432
uint32_t targetSize
target size
Definition: GalliumBinaries.h:98
Gallium input.
Definition: GalliumBinaries.h:376
ProgInfoEntryIndexMap progInfoEntryMap
program info map
Definition: GalliumBinaries.h:140
GalliumSectionType type
type of section
Definition: GalliumBinaries.h:126
kernel argument (Gallium binaries)
Definition: GalliumBinaries.h:92
GalliumArgType type
type of argument
Definition: GalliumBinaries.h:94
bool debugMode
debug mode
Definition: GalliumBinaries.h:357
const GalliumKernel & getKernel(const char *name) const
get kernel with speciified name
Definition: GalliumBinaries.h:329
Definition: GalliumBinaries.h:235
const GalliumElfBinary64 & getElfBinary64() const
returns Gallium inner ELF 64-bit binary
Definition: GalliumBinaries.h:294
create prinfomap for inner binaries
Definition: GalliumBinaries.h:50
cxbyte * getBinaryCode()
returns binary code data
Definition: GalliumBinaries.h:275
an array class
Definition: Containers.h:38
const GalliumProgInfoEntry * getProgramInfo(const char *name) const
returns program info entries for specified kernel name
Definition: GalliumBinaries.h:164
uint32_t getKernelsNum() const
returns kernels number
Definition: GalliumBinaries.h:318
size_t getSize() const
get size of binaries
Definition: GalliumBinaries.h:259
std::vector< GalliumKernelInput > kernels
input kernel list
Definition: GalliumBinaries.h:382
cxuint usedVGPRsNum
number of used VGPRs
Definition: GalliumBinaries.h:347
CString kernelName
kernel&#39;s name
Definition: GalliumBinaries.h:367
uint32_t getSectionsNum() const
get sections number
Definition: GalliumBinaries.h:298
size_t commentSize
comment size (can be null)
Definition: GalliumBinaries.h:385
size_t disasmOffset
disassembly offset
Definition: GalliumBinaries.h:142
size_t disasmSize
disassembly size
Definition: GalliumBinaries.h:141
GPUDeviceType deviceType
GPU device type.
Definition: GalliumBinaries.h:379
cxbyte userDataNum
number of user data
Definition: GalliumBinaries.h:351
const GalliumInput * getInput() const
get input
Definition: GalliumBinaries.h:428
uint32_t size
size of argument
Definition: GalliumBinaries.h:97
bool ieeeMode
IEEE mode.
Definition: GalliumBinaries.h:352
cxuint usedSGPRsNum
number of used SGPRs
Definition: GalliumBinaries.h:348
input output utilities
uint32_t offset
offset in binary
Definition: GalliumBinaries.h:127
size_t globalDataSize
global constant data size
Definition: GalliumBinaries.h:380
Array< GalliumArgInfo > argInfos
arguments
Definition: GalliumBinaries.h:108
size_t codeSize
code size
Definition: GalliumBinaries.h:383
kernel program info entry for Gallium binaries
Definition: GalliumBinaries.h:85
main namespace
Definition: AsmFormats.h:41
std::vector< BinSymbol > extraSymbols
extra symbols
Definition: GalliumBinaries.h:388
const cxbyte * globalData
global constant data
Definition: GalliumBinaries.h:381
cxbyte floatMode
float mode
Definition: GalliumBinaries.h:353
const GalliumElfBinary32 & getElfBinary32() const
returns Gallium inner ELF 32-bit binary
Definition: GalliumBinaries.h:286
bool useConfig
true if configuration has been used to generate binary
Definition: GalliumBinaries.h:369
uint32_t sectionId
section id
Definition: GalliumBinaries.h:125
gallium code binary generator
Definition: GalliumBinaries.h:395
creation flags for ELF binaries
Definition: ElfBinaries.h:73
uint32_t targetAlign
target alignment
Definition: GalliumBinaries.h:99
kernel info structure (Gallium binaries)
Definition: GalliumBinaries.h:365
inlines for accessing memory words in LittleEndian and unaligned
bool operator!() const
returns true if object is uninitialized
Definition: GalliumBinaries.h:267
bool hasProgInfoMap() const
return true if binary has program info map
Definition: GalliumBinaries.h:225
section for text (binary code)
const char * getDisassembly() const
return disassembly content (without null-character)
Definition: GalliumBinaries.h:209
create map of kernels for inner binaries
Definition: GalliumBinaries.h:47
GPUDeviceType
type of GPU device
Definition: GPUId.h:38
cxbyte exceptions
enabled exceptions
Definition: GalliumBinaries.h:355
std::vector< GalliumArgInfo > argInfos
arguments
Definition: GalliumBinaries.h:372
32-bit Gallium ELF binary
Definition: GalliumBinaries.h:194
const GalliumKernel & getKernel(uint32_t index) const
get kernel by index
Definition: GalliumBinaries.h:325
GalliumSectionType
Gallium format section type.
Definition: GalliumBinaries.h:112
bool dx10Clamp
DX10 CLAMP mode.
Definition: GalliumBinaries.h:359
const cxbyte * code
code
Definition: GalliumBinaries.h:384
64-bit Gallium ELF binary
Definition: GalliumBinaries.h:214
utilities for other libraries and programs
uint32_t size
size of section
Definition: GalliumBinaries.h:128
uint32_t scratchBufferSize
size of scratch buffer
Definition: GalliumBinaries.h:361
uint32_t getProgramInfosNum() const
returns program infos number
Definition: GalliumBinaries.h:154
CString kernelName
kernel&#39;s name
Definition: GalliumBinaries.h:105
create map of sections for inner binaries
Definition: GalliumBinaries.h:46
bool is64BitElfBinary() const
return true if inner binary is 64-bit
Definition: GalliumBinaries.h:278
const char * getDisassembly() const
return disassembly content (without null-character)
Definition: GalliumBinaries.h:229
uint32_t progInfosNum
program info entries number
Definition: GalliumBinaries.h:138
const GalliumSection & getSection(uint32_t index) const
get section with specified index
Definition: GalliumBinaries.h:314
bool tgSize
enable TG_SIZE_EN bit
Definition: GalliumBinaries.h:356
uint32_t pgmRSRC1
pgmRSRC1 register value
Definition: GalliumBinaries.h:349
GPU identification utilities.
uint32_t getSectionSize(uint32_t index) const
get size of section with specified index
Definition: GalliumBinaries.h:302
Definition: GalliumBinaries.h:49
class ElfBinaryTemplate< Elf32Types > ElfBinary32
type for 32-bit ELF binary
Definition: ElfBinaries.h:430
GalliumArgSemantic semantic
semantic of array
Definition: GalliumBinaries.h:96
kernel info structure (Gallium binaries)
Definition: GalliumBinaries.h:103
GalliumArgType
GalliumCompute Kernel Arg type.
Definition: GalliumBinaries.h:59
GalliumArgSemantic
Gallium semantic field type.
Definition: GalliumBinaries.h:74
const char * comment
comment
Definition: GalliumBinaries.h:386
bool hasProgInfoMap() const
return true if binary has program info map
Definition: GalliumBinaries.h:205
uint32_t offset
offset in ElfBinary
Definition: GalliumBinaries.h:107
size_t getDisassemblySize() const
returns size of disassembly
Definition: GalliumBinaries.h:183
shift for convert inner binary flags into elf binary flags
Definition: GalliumBinaries.h:55
std::vector< BinSection > extraSections
extra sections
Definition: GalliumBinaries.h:387
cxbyte * getSectionContent(uint32_t index)
get content for section with specified index
Definition: GalliumBinaries.h:310
cxuint dimMask
mask of dimension (bits: 0 - X, 1 - Y, 2 - Z)
Definition: GalliumBinaries.h:346
GalliumElfBinary32 & getElfBinary32()
returns Gallium inner ELF 32-bit binary
Definition: GalliumBinaries.h:282
GalliumKernelConfig config
kernel&#39;s configuration
Definition: GalliumBinaries.h:370
bool hasDisassembly() const
returns true if disassembly available
Definition: GalliumBinaries.h:179
const cxbyte * getBinaryCode() const
returns binary code data
Definition: GalliumBinaries.h:271
Elf binaries handling.
bool signExtended
is signed extended
Definition: GalliumBinaries.h:95
simple C-string container
Definition: CString.h:38
containers and other utils for other libraries and programs
Gallium elf binary base (for 32-bit and 64-bit)
Definition: GalliumBinaries.h:132
all Gallium binaries flags
Definition: GalliumBinaries.h:54
uint32_t offset
offset of kernel code
Definition: GalliumBinaries.h:371
pointer to constant memory