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-2017 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 
60 {
61  SCALAR = 0,
62  CONSTANT,
63  GLOBAL,
64  LOCAL,
69  SAMPLER,
70  MAX_VALUE = SAMPLER
71 };
72 
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 
113 {
114  TEXT = 0,
115  DATA_CONSTANT,
116  DATA_GLOBAL,
117  DATA_LOCAL,
118  DATA_PRIVATE,
119  TEXT_INTERMEDIATE_170 = 0,
120  TEXT_LIBRARY_170,
121  TEXT_EXECUTABLE_170,
122  DATA_CONSTANT_170,
123  DATA_GLOBAL_170,
124  DATA_LOCAL_170,
125  DATA_PRIVATE_170,
126  MAX_VALUE = DATA_PRIVATE_170
127 };
128 
131 {
132  uint32_t sectionId;
134  uint32_t offset;
135  uint32_t size;
136 };
137 
140 {
141 public:
144 protected:
145  uint32_t progInfosNum;
147  ProgInfoEntryIndexMap progInfoEntryMap;
148  size_t disasmSize;
149  size_t disasmOffset;
150  bool llvm390;
151 
153  template<typename ElfBinary>
154  void loadFromElf(ElfBinary& elfBinary, size_t kernelsNum);
155 public:
159  virtual ~GalliumElfBinaryBase();
160 
162  uint32_t getProgramInfosNum() const
163  { return progInfosNum; }
164 
166  uint32_t getProgramInfoEntriesNum(uint32_t index) const;
167 
169  uint32_t getProgramInfoEntryIndex(const char* name) const;
170 
172  const GalliumProgInfoEntry* getProgramInfo(const char* name) const
173  { return progInfoEntries + getProgramInfoEntryIndex(name); }
174 
177  { return progInfoEntries + getProgramInfoEntryIndex(name); }
178 
180  const GalliumProgInfoEntry* getProgramInfo(uint32_t index) const;
182  GalliumProgInfoEntry* getProgramInfo(uint32_t index);
183 
185  bool hasDisassembly() const
186  { return disasmOffset != 0; }
187 
189  size_t getDisassemblySize() const
190  { return disasmSize; }
192  bool isLLVM390() const
193  { return llvm390; }
194 };
195 
196 /* INFO: in this file is used ULEV function for conversion
197  * from LittleEndian and unaligned access to other memory access policy and endianness
198  * Please use this function whenever you want to get or set word in ELF binary,
199  * because ELF binaries can be unaligned in memory (as inner binaries).
200  */
202 
204 {
205 public:
209  GalliumElfBinary32(size_t binaryCodeSize, cxbyte* binaryCode, Flags creationFlags,
210  size_t kernelsNum);
212  virtual ~GalliumElfBinary32();
213 
215  bool hasProgInfoMap() const
216  { return (creationFlags & GALLIUM_ELF_CREATE_PROGINFOMAP) != 0; }
217 
219  const char* getDisassembly() const
220  { return reinterpret_cast<const char*>(binaryCode + disasmOffset); }
221 };
222 
225 {
226 public:
230  GalliumElfBinary64(size_t binaryCodeSize, cxbyte* binaryCode, Flags creationFlags,
231  size_t kernelsNum);
233  virtual ~GalliumElfBinary64();
234 
236  bool hasProgInfoMap() const
237  { return (creationFlags & GALLIUM_ELF_CREATE_PROGINFOMAP) != 0; }
238 
240  const char* getDisassembly() const
241  { return reinterpret_cast<const char*>(binaryCode + disasmOffset); }
242 };
243 
247 {
248 private:
249  Flags creationFlags;
250  size_t binaryCodeSize;
251  cxbyte* binaryCode;
252  uint32_t kernelsNum;
253  uint32_t sectionsNum;
254  std::unique_ptr<GalliumKernel[]> kernels;
255  std::unique_ptr<GalliumSection[]> sections;
256 
257  bool elf64BitBinary;
258  std::unique_ptr<GalliumElfBinaryBase> elfBinary;
259  bool mesa170;
260 
261 public:
263  GalliumBinary(size_t binaryCodeSize, cxbyte* binaryCode, Flags creationFlags);
265  ~GalliumBinary() = default;
266 
269  { return creationFlags; }
270 
272  size_t getSize() const
273  { return binaryCodeSize; }
274 
276  operator bool() const
277  { return binaryCode!=nullptr; }
278 
280  bool operator!() const
281  { return binaryCode==nullptr; }
282 
284  const cxbyte* getBinaryCode() const
285  { return binaryCode; }
286 
289  { return binaryCode; }
291  bool is64BitElfBinary() const
292  { return elf64BitBinary; }
293 
296  { return *static_cast<GalliumElfBinary32*>(elfBinary.get()); }
297 
300  { return *static_cast<const GalliumElfBinary32*>(elfBinary.get()); }
301 
304  { return *static_cast<GalliumElfBinary64*>(elfBinary.get()); }
305 
308  { return *static_cast<const GalliumElfBinary64*>(elfBinary.get()); }
309 
311  uint32_t getSectionsNum() const
312  { return sectionsNum; }
313 
315  uint32_t getSectionSize(uint32_t index) const
316  { return sections[index].size; }
317 
319  const cxbyte* getSectionContent(uint32_t index) const
320  { return binaryCode + sections[index].offset; }
321 
323  cxbyte* getSectionContent(uint32_t index)
324  { return binaryCode + sections[index].offset; }
325 
327  const GalliumSection& getSection(uint32_t index) const
328  { return sections[index]; }
329 
331  uint32_t getKernelsNum() const
332  { return kernelsNum;; }
333 
335  uint32_t getKernelIndex(const char* name) const;
336 
338  const GalliumKernel& getKernel(uint32_t index) const
339  { return kernels[index]; }
340 
342  const GalliumKernel& getKernel(const char* name) const
343  { return kernels[getKernelIndex(name)]; }
345  bool isMesa170() const
346  { return mesa170; };
347 };
348 
349 enum: cxuint {
350  GALLIUMSECTID_GPUCONFIG = ELFSECTID_OTHER_BUILTIN,
351  GALLIUMSECTID_NOTEGNUSTACK,
352  GALLIUMSECTID_MAX = GALLIUMSECTID_NOTEGNUSTACK
353 };
354 
355 /*
356  * Gallium Binary generator
357  */
358 
361 {
365  uint32_t pgmRSRC1;
366  uint32_t pgmRSRC2;
368  bool ieeeMode;
372  bool tgSize;
373  bool debugMode;
375  bool dx10Clamp;
376  size_t localSize;
377  uint32_t scratchBufferSize;
380 };
381 
384 {
386  GalliumProgInfoEntry progInfo[5];
387  bool useConfig;
389  uint32_t offset;
390  std::vector<GalliumArgInfo> argInfos;
391 };
392 
395 {
396  bool is64BitElf;
397  bool isLLVM390;
398  bool isMesa170;
400  size_t globalDataSize;
402  std::vector<GalliumKernelInput> kernels;
403  size_t codeSize;
404  const cxbyte* code;
405  size_t commentSize;
406  const char* comment;
407  std::vector<BinSection> extraSections;
408  std::vector<BinSymbol> extraSymbols;
409 
411  void addEmptyKernel(const char* kernelName, cxuint llvmVersion);
412 };
413 
416 {
417 private:
418  bool manageable;
419  const GalliumInput* input;
420 
421  void generateInternal(std::ostream* osPtr, std::vector<char>* vPtr,
422  Array<cxbyte>* aPtr) const;
423 public:
426  GalliumBinGenerator(const GalliumInput* galliumInput);
427 
429 
438  GalliumBinGenerator(bool _64bitMode, GPUDeviceType deviceType, size_t codeSize,
439  const cxbyte* code, size_t globalDataSize, const cxbyte* globalData,
440  const std::vector<GalliumKernelInput>& kernels);
442  GalliumBinGenerator(bool _64bitMode, GPUDeviceType deviceType, size_t codeSize,
443  const cxbyte* code, size_t globalDataSize, const cxbyte* globalData,
444  std::vector<GalliumKernelInput>&& kernels);
446 
448  const GalliumInput* getInput() const
449  { return input; }
450 
452  void setInput(const GalliumInput* input);
453 
455  void generate(Array<cxbyte>& array) const;
456 
458  void generate(std::ostream& os) const;
459 
461  void generate(std::vector<char>& vector) const;
462 };
463 
465 extern uint32_t detectMesaDriverVersion();
467 extern uint32_t detectLLVMCompilerVersion();
468 
469 };
470 
471 #endif
uint32_t pgmRSRC2
pgmRSRC2 register value
Definition: GalliumBinaries.h:366
bool privilegedMode
prvileged mode
Definition: GalliumBinaries.h:374
GalliumElfBinary64 & getElfBinary64()
returns Gallium inner ELF 64-bit binary
Definition: GalliumBinaries.h:303
Flags getCreationFlags()
get creation flags
Definition: GalliumBinaries.h:268
create prinfomap for inner binaries
Definition: GalliumBinaries.h:50
pointer to local memory
bool is64BitElf
is 64-bit elf binary
Definition: GalliumBinaries.h:396
kernel config
Definition: GalliumBinaries.h:360
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:319
GalliumProgInfoEntry * getProgramInfo(const char *name)
returns program info entries for specified kernel name
Definition: GalliumBinaries.h:176
size_t localSize
used local size (not local defined in kernel arguments)
Definition: GalliumBinaries.h:376
Array< std::pair< const char *, size_t > > ProgInfoEntryIndexMap
program info entry index map
Definition: GalliumBinaries.h:143
uint32_t address
address
Definition: GalliumBinaries.h:87
all Gallium binaries flags
Definition: GalliumBinaries.h:54
bool isMesa170
true if binary for >= Mesa3D 17.0
Definition: GalliumBinaries.h:398
Gallium binarie&#39;s Section.
Definition: GalliumBinaries.h:130
cxbyte priority
priority
Definition: GalliumBinaries.h:370
uint32_t value
value
Definition: GalliumBinaries.h:88
create map of sections for inner binaries
Definition: GalliumBinaries.h:46
GalliumProgInfoEntry * progInfoEntries
program info entries
Definition: GalliumBinaries.h:146
class ElfBinaryTemplate< Elf64Types > ElfBinary64
type for 64-bit ELF binary
Definition: ElfBinaries.h:461
uint32_t targetSize
target size
Definition: GalliumBinaries.h:98
Gallium input.
Definition: GalliumBinaries.h:394
ProgInfoEntryIndexMap progInfoEntryMap
program info map
Definition: GalliumBinaries.h:147
GalliumSectionType type
type of section
Definition: GalliumBinaries.h:133
kernel argument (Gallium binaries)
Definition: GalliumBinaries.h:92
GalliumArgType type
type of argument
Definition: GalliumBinaries.h:94
bool debugMode
debug mode
Definition: GalliumBinaries.h:373
const GalliumKernel & getKernel(const char *name) const
get kernel with speciified name
Definition: GalliumBinaries.h:342
Definition: GalliumBinaries.h:246
const GalliumElfBinary64 & getElfBinary64() const
returns Gallium inner ELF 64-bit binary
Definition: GalliumBinaries.h:307
cxbyte * getBinaryCode()
returns binary code data
Definition: GalliumBinaries.h:288
an array class
Definition: Containers.h:38
uint32_t detectMesaDriverVersion()
detect driver version in the system
const GalliumProgInfoEntry * getProgramInfo(const char *name) const
returns program info entries for specified kernel name
Definition: GalliumBinaries.h:172
uint32_t getKernelsNum() const
returns kernels number
Definition: GalliumBinaries.h:331
size_t getSize() const
get size of binaries
Definition: GalliumBinaries.h:272
std::vector< GalliumKernelInput > kernels
input kernel list
Definition: GalliumBinaries.h:402
cxuint usedVGPRsNum
number of used VGPRs
Definition: GalliumBinaries.h:363
Configuration header.
CString kernelName
kernel&#39;s name
Definition: GalliumBinaries.h:385
create map of kernels for inner binaries
Definition: GalliumBinaries.h:47
uint32_t getSectionsNum() const
get sections number
Definition: GalliumBinaries.h:311
size_t commentSize
comment size (can be null)
Definition: GalliumBinaries.h:405
size_t disasmOffset
disassembly offset
Definition: GalliumBinaries.h:149
size_t disasmSize
disassembly size
Definition: GalliumBinaries.h:148
GPUDeviceType deviceType
GPU device type.
Definition: GalliumBinaries.h:399
shift for convert inner binary flags into elf binary flags
Definition: GalliumBinaries.h:55
cxbyte userDataNum
number of user data
Definition: GalliumBinaries.h:367
const GalliumInput * getInput() const
get input
Definition: GalliumBinaries.h:448
uint32_t size
size of argument
Definition: GalliumBinaries.h:97
bool ieeeMode
IEEE mode.
Definition: GalliumBinaries.h:368
cxuint usedSGPRsNum
number of used SGPRs
Definition: GalliumBinaries.h:364
input output utilities
uint32_t offset
offset in binary
Definition: GalliumBinaries.h:134
size_t globalDataSize
global constant data size
Definition: GalliumBinaries.h:400
Array< GalliumArgInfo > argInfos
arguments
Definition: GalliumBinaries.h:108
size_t codeSize
code size
Definition: GalliumBinaries.h:403
unsigned char cxbyte
unsigned byte
Definition: Config.h:215
kernel program info entry for Gallium binaries
Definition: GalliumBinaries.h:85
main namespace
Definition: AsmDefs.h:38
std::vector< BinSymbol > extraSymbols
extra symbols
Definition: GalliumBinaries.h:408
const cxbyte * globalData
global constant data
Definition: GalliumBinaries.h:401
cxbyte floatMode
float mode
Definition: GalliumBinaries.h:369
const GalliumElfBinary32 & getElfBinary32() const
returns Gallium inner ELF 32-bit binary
Definition: GalliumBinaries.h:299
unsigned int cxuint
unsigned int
Definition: Config.h:223
bool useConfig
true if configuration has been used to generate binary
Definition: GalliumBinaries.h:387
uint32_t sectionId
section id
Definition: GalliumBinaries.h:132
gallium code binary generator
Definition: GalliumBinaries.h:415
cxuint spilledVGPRs
number of spilled vector registers
Definition: GalliumBinaries.h:378
bool isLLVM390
true if binary for >= LLVM 3.9
Definition: GalliumBinaries.h:397
bool isMesa170() const
returns true if binary for >=Mesa3D 17.0
Definition: GalliumBinaries.h:345
uint32_t targetAlign
target alignment
Definition: GalliumBinaries.h:99
kernel info structure (Gallium binaries)
Definition: GalliumBinaries.h:383
inlines for accessing memory words in LittleEndian and unaligned
bool operator!() const
returns true if object is uninitialized
Definition: GalliumBinaries.h:280
bool hasProgInfoMap() const
return true if binary has program info map
Definition: GalliumBinaries.h:236
section for text (binary code)
const char * getDisassembly() const
return disassembly content (without null-character)
Definition: GalliumBinaries.h:219
create elf proginfomap
Definition: GalliumBinaries.h:52
GPUDeviceType
type of GPU device
Definition: GPUId.h:51
uint32_t detectLLVMCompilerVersion()
detect LLVM compiler version in the system
cxbyte exceptions
enabled exceptions
Definition: GalliumBinaries.h:371
std::vector< GalliumArgInfo > argInfos
arguments
Definition: GalliumBinaries.h:390
32-bit Gallium ELF binary
Definition: GalliumBinaries.h:203
const GalliumKernel & getKernel(uint32_t index) const
get kernel by index
Definition: GalliumBinaries.h:338
GalliumSectionType
Gallium format section type.
Definition: GalliumBinaries.h:112
bool dx10Clamp
DX10 CLAMP mode.
Definition: GalliumBinaries.h:375
const cxbyte * code
code
Definition: GalliumBinaries.h:404
64-bit Gallium ELF binary
Definition: GalliumBinaries.h:224
utilities for other libraries and programs
uint32_t size
size of section
Definition: GalliumBinaries.h:135
uint32_t scratchBufferSize
size of scratch buffer
Definition: GalliumBinaries.h:377
uint32_t getProgramInfosNum() const
returns program infos number
Definition: GalliumBinaries.h:162
CString kernelName
kernel&#39;s name
Definition: GalliumBinaries.h:105
bool is64BitElfBinary() const
return true if inner binary is 64-bit
Definition: GalliumBinaries.h:291
const char * getDisassembly() const
return disassembly content (without null-character)
Definition: GalliumBinaries.h:240
uint32_t progInfosNum
program info entries number
Definition: GalliumBinaries.h:145
const GalliumSection & getSection(uint32_t index) const
get section with specified index
Definition: GalliumBinaries.h:327
bool tgSize
enable TG_SIZE_EN bit
Definition: GalliumBinaries.h:372
uint32_t pgmRSRC1
pgmRSRC1 register value
Definition: GalliumBinaries.h:365
GPU identification utilities.
uint32_t getSectionSize(uint32_t index) const
get size of section with specified index
Definition: GalliumBinaries.h:315
creation flags for ELF binaries
Definition: ElfBinaries.h:73
class ElfBinaryTemplate< Elf32Types > ElfBinary32
type for 32-bit ELF binary
Definition: ElfBinaries.h:459
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
bool isLLVM390() const
returns true binary for if >=LLVM 3.9
Definition: GalliumBinaries.h:192
GalliumArgSemantic
Gallium semantic field type.
Definition: GalliumBinaries.h:74
const char * comment
comment
Definition: GalliumBinaries.h:406
bool llvm390
true if >= LLVM 3.9
Definition: GalliumBinaries.h:150
bool hasProgInfoMap() const
return true if binary has program info map
Definition: GalliumBinaries.h:215
uint32_t offset
offset in ElfBinary
Definition: GalliumBinaries.h:107
size_t getDisassemblySize() const
returns size of disassembly
Definition: GalliumBinaries.h:189
std::vector< BinSection > extraSections
extra sections
Definition: GalliumBinaries.h:407
cxbyte * getSectionContent(uint32_t index)
get content for section with specified index
Definition: GalliumBinaries.h:323
cxuint dimMask
mask of dimension (bits: 0 - X, 1 - Y, 2 - Z)
Definition: GalliumBinaries.h:362
GalliumElfBinary32 & getElfBinary32()
returns Gallium inner ELF 32-bit binary
Definition: GalliumBinaries.h:295
GalliumKernelConfig config
kernel&#39;s configuration
Definition: GalliumBinaries.h:388
bool hasDisassembly() const
returns true if disassembly available
Definition: GalliumBinaries.h:185
const cxbyte * getBinaryCode() const
returns binary code data
Definition: GalliumBinaries.h:284
Definition: GalliumBinaries.h:49
Elf binaries handling.
bool signExtended
is signed extended
Definition: GalliumBinaries.h:95
simple C-string container
Definition: CString.h:38
Elf.h definitions.
containers and other utils for other libraries and programs
Gallium elf binary base (for 32-bit and 64-bit)
Definition: GalliumBinaries.h:139
cxuint spilledSGPRs
number of spilled scalar registers
Definition: GalliumBinaries.h:379
uint32_t offset
offset of kernel code
Definition: GalliumBinaries.h:389
pointer to constant memory