CLRX  1
An unofficial OpenCL extensions designed for Radeon GPUs
AmdCL2Binaries.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_AMDCL2BINARIES_H__
24 #define __CLRX_AMDCL2BINARIES_H__
25 
26 #include <CLRX/Config.h>
27 #include <cstddef>
28 #include <cstdint>
29 #include <memory>
30 #include <string>
31 #include <CLRX/amdbin/Elf.h>
32 #include <CLRX/utils/MemAccess.h>
34 #include <CLRX/utils/Containers.h>
35 #include <CLRX/utils/Utilities.h>
36 
38 namespace CLRX
39 {
40 
41 enum : Flags {
45 
49 };
50 
53 {
55  size_t setupSize;
56  cxbyte* setup;
57  size_t codeSize;
58  cxbyte* code;
59 };
60 
63 {
64  size_t size;
65  cxbyte* data;
66 };
67 
69 
72 {
73 public:
76 protected:
78  KernelDataMap kernelDataMap;
79 public:
80  virtual ~AmdCL2InnerGPUBinaryBase();
82  size_t getKernelsNum() const
83  { return kernels.size(); }
84 
86  const AmdCL2GPUKernel& getKernelData(size_t index) const
87  { return kernels[index]; }
88 
91  { return kernels[index]; }
92 
94  const AmdCL2GPUKernel& getKernelData(const char* name) const;
95 };
96 
100 {
101 private:
102  Flags creationFlags;
103  size_t binarySize;
104  cxbyte* binary;
105  std::unique_ptr<AmdCL2GPUKernelStub[]> kernelStubs;
106 public:
108  AmdCL2OldInnerGPUBinary() = default;
110 
116  AmdCL2OldInnerGPUBinary(AmdCL2MainGPUBinary* mainBinary, size_t binaryCodeSize,
117  cxbyte* binaryCode, Flags creationFlags = AMDBIN_CREATE_ALL);
119  ~AmdCL2OldInnerGPUBinary() = default;
120 
122  size_t getSize() const
123  { return binarySize; }
125  const cxbyte* getBinaryCode() const
126  { return binary; }
128  cxbyte* getBinaryCode()
129  { return binary; }
130 
132  bool hasKernelData() const
133  { return creationFlags & AMDCL2BIN_CREATE_KERNELDATA; }
135  bool hasKernelDataMap() const
136  { return creationFlags & AMDCL2BIN_CREATE_KERNELDATAMAP; }
138  bool hasKernelStubs() const
139  { return creationFlags & AMDCL2BIN_CREATE_KERNELSTUBS; }
140 
142  const AmdCL2GPUKernelStub& getKernelStub(size_t index) const
143  { return kernelStubs[index]; }
144 
146  const AmdCL2GPUKernelStub& getKernelStub(const char* name) const;
147 };
148 
150 
154 {
155 private:
156  size_t globalDataSize;
157  cxbyte* globalData;
158  size_t rwDataSize;
159  cxbyte* rwData;
160  size_t bssAlignment;
161  size_t bssSize;
162  size_t samplerInitSize;
163  cxbyte* samplerInit;
164  size_t textRelsNum;
165  size_t textRelEntrySize;
166  cxbyte* textRela;
167  size_t globalDataRelsNum;
168  size_t globalDataRelEntrySize;
169  cxbyte* globalDataRela;
170 public:
172  AmdCL2InnerGPUBinary() = default;
174 
179  AmdCL2InnerGPUBinary(size_t binaryCodeSize, cxbyte* binaryCode,
180  Flags creationFlags = AMDBIN_CREATE_ALL);
182  ~AmdCL2InnerGPUBinary() = default;
183 
185  bool hasKernelData() const
186  { return creationFlags & AMDCL2BIN_CREATE_KERNELDATA; }
188  bool hasKernelDataMap() const
189  { return creationFlags & AMDCL2BIN_CREATE_KERNELDATAMAP; }
190 
192  size_t getGlobalDataSize() const
193  { return globalDataSize; }
194 
196  const cxbyte* getGlobalData() const
197  { return globalData; }
199  cxbyte* getGlobalData()
200  { return globalData; }
201 
203  size_t getRwDataSize() const
204  { return rwDataSize; }
205 
207  const cxbyte* getRwData() const
208  { return rwData; }
210  cxbyte* getRwData()
211  { return rwData; }
213  size_t getBssAlignment() const
214  { return bssAlignment; }
216  size_t getBssSize() const
217  { return bssSize; }
218 
220  size_t getSamplerInitSize() const
221  { return samplerInitSize; }
222 
224  const cxbyte* getSamplerInit() const
225  { return samplerInit; }
227  cxbyte* getSamplerInit()
228  { return samplerInit; }
229 
231  size_t getTextRelaEntriesNum() const
232  { return textRelsNum; }
234  const Elf64_Rela& getTextRelaEntry(size_t index) const
235  { return *reinterpret_cast<const Elf64_Rela*>(textRela + textRelEntrySize*index); }
238  { return *reinterpret_cast<Elf64_Rela*>(textRela + textRelEntrySize*index); }
239 
242  { return globalDataRelsNum; }
244  const Elf64_Rela& getGlobalDataRelaEntry(size_t index) const
245  { return *reinterpret_cast<const Elf64_Rela*>(globalDataRela +
246  globalDataRelEntrySize*index); }
249  { return *reinterpret_cast<Elf64_Rela*>(globalDataRela +
250  globalDataRelEntrySize*index); }
251 };
252 
255 {
257  size_t size;
258  cxbyte* data;
259 };
260 
263 {
264  uint64_t size;
265  uint64_t metadataSize;
266  uint32_t unknown1[3];
267  uint32_t options;
268  uint16_t kernelId;
269  uint16_t unknownx;
270  uint32_t unknowny;
271  uint64_t unknown2[2];
272  uint64_t reqdWorkGroupSize[3];
273  uint64_t unknown3[2];
274  uint64_t firstNameLength;
275  uint64_t secondNameLength;
276  uint64_t unknown4[3];
277  uint64_t pipesUsage;
278  uint64_t unknown5[2];
279  uint64_t argsNum;
280 };
281 
284 {
285  uint64_t size;
286  uint64_t argNameSize;
287  uint64_t typeNameSize;
288  uint64_t unknown1, unknown2;
289  union {
290  uint32_t vectorLength;
291  uint32_t resId;
292  uint32_t structSize;
293  };
294  uint32_t unknown3;
295  uint32_t argOffset;
296  uint32_t argType;
297  uint32_t ptrAlignment;
298  uint32_t ptrType;
299  uint32_t ptrSpace;
300  uint32_t isPointerOrPipe;
301  cxbyte isVolatile;
302  cxbyte isRestrict;
303  cxbyte isPipe;
304  cxbyte unknown4;
305  uint32_t kindOfType;
306  uint32_t isConst;
307  uint32_t unknown5;
308 };
309 
311 
315 {
316 public:
318 protected:
319  cxuint driverVersion;
320  size_t kernelsNum;
321  std::unique_ptr<AmdCL2GPUKernelMetadata[]> metadatas;
323  std::unique_ptr<AmdGPUKernelHeader[]> kernelHeaders;
324  MetadataMap isaMetadataMap;
325 
327  std::unique_ptr<AmdCL2InnerGPUBinaryBase> innerBinary;
328 public:
329  AmdCL2MainGPUBinary(size_t binaryCodeSize, cxbyte* binaryCode,
330  Flags creationFlags = AMDBIN_CREATE_ALL);
331  ~AmdCL2MainGPUBinary() = default;
332 
334  bool hasKernelInfo() const
335  { return (creationFlags & AMDBIN_CREATE_KERNELINFO) != 0; }
336 
338  bool hasKernelInfoMap() const
339  { return (creationFlags & AMDBIN_CREATE_KERNELINFOMAP) != 0; }
340 
342  bool hasInfoStrings() const
343  { return (creationFlags & AMDBIN_CREATE_INFOSTRINGS) != 0; }
344 
345  // returns true if inner binary exists
346  bool hasInnerBinary() const
347  { return innerBinary.get()!=nullptr; }
348 
350  cxuint getDriverVersion() const
351  { return driverVersion; }
352 
355  { return *innerBinary; }
356 
359  { return *innerBinary; }
360 
363  { return *static_cast<const AmdCL2InnerGPUBinary*>(innerBinary.get()); }
364 
367  { return *static_cast<AmdCL2InnerGPUBinary*>(innerBinary.get()); }
368 
371  { return *static_cast<const AmdCL2OldInnerGPUBinary*>(innerBinary.get()); }
372 
375  { return *static_cast<AmdCL2OldInnerGPUBinary*>(innerBinary.get()); }
376 
378  const AmdGPUKernelHeader& getKernelHeaderEntry(size_t index) const
379  { return kernelHeaders[index]; }
381  const AmdGPUKernelHeader& getKernelHeaderEntry(const char* name) const;
382 
384  size_t getISAMetadatasNum() const
385  { return isaMetadatas.size(); }
386 
388  const AmdCL2GPUKernelMetadata& getISAMetadataEntry(size_t index) const
389  { return isaMetadatas[index]; }
391  const AmdCL2GPUKernelMetadata& getISAMetadataEntry(const char* name) const;
392 
394  size_t getISAMetadataSize(size_t index) const
395  { return isaMetadatas[index].size; }
396 
398  const cxbyte* getISAMetadata(size_t index) const
399  { return isaMetadatas[index].data; }
400 
402  cxbyte* getISAMetadata(size_t index)
403  { return isaMetadatas[index].data; }
404 
406  const AmdCL2GPUKernelMetadata& getMetadataEntry(size_t index) const
407  { return metadatas[index]; }
409  const AmdCL2GPUKernelMetadata& getMetadataEntry(const char* name) const;
410 
412  size_t getMetadataSize(size_t index) const
413  { return metadatas[index].size; }
414 
416  const cxbyte* getMetadata(size_t index) const
417  { return metadatas[index].data; }
418 
420  cxbyte* getMetadata(size_t index)
421  { return metadatas[index].data; }
422 
425  { return aclVersionString; }
426 };
427 
429 extern bool isAmdCL2Binary(size_t binarySize, const cxbyte* binary);
430 
431 };
432 
433 #endif
header for metadata
Definition: AmdCL2Binaries.h:262
size_t codeSize
size
Definition: AmdCL2Binaries.h:57
bool hasKernelData() const
return if binary has kernel datas
Definition: AmdCL2Binaries.h:132
create kernel setup
Definition: AmdCL2Binaries.h:42
non copyable and non movable base structure (class)
Definition: Utilities.h:43
const cxbyte * getRwData() const
get readwrite atomic data
Definition: AmdCL2Binaries.h:207
uint64_t argsNum
number of arguments
Definition: AmdCL2Binaries.h:279
uint32_t Flags
type for declaring various flags
Definition: Utilities.h:97
size_t getTextRelaEntriesNum() const
get text rel entries number
Definition: AmdCL2Binaries.h:231
AMD GPU header for kernel.
Definition: AmdBinaries.h:453
cxbyte isRestrict
if pointer is restrict
Definition: AmdCL2Binaries.h:302
cxbyte * code
data
Definition: AmdCL2Binaries.h:58
create kernel setups map
Definition: AmdCL2Binaries.h:47
Array< std::pair< CString, size_t > > KernelDataMap
inner binary map type
Definition: AmdCL2Binaries.h:75
size_t getSize() const
return binary size
Definition: AmdCL2Binaries.h:122
std::unique_ptr< AmdCL2GPUKernelMetadata[]> metadatas
AMD metadatas.
Definition: AmdCL2Binaries.h:321
uint32_t ptrSpace
pointer space
Definition: AmdCL2Binaries.h:299
cxbyte * getBinaryCode()
return binary code
Definition: AmdCL2Binaries.h:128
uint64_t firstNameLength
first name length
Definition: AmdCL2Binaries.h:274
const AmdCL2InnerGPUBinary & getInnerBinary() const
get inner binary
Definition: AmdCL2Binaries.h:362
size_t getGlobalDataRelaEntriesNum() const
get global data rel entries number
Definition: AmdCL2Binaries.h:241
class ElfBinaryTemplate< Elf64Types > ElfBinary64
type for 64-bit ELF binary
Definition: ElfBinaries.h:432
Array< AmdCL2GPUKernel > kernels
kernel headers
Definition: AmdCL2Binaries.h:77
uint32_t ptrAlignment
pointer alignment
Definition: AmdCL2Binaries.h:297
cxbyte * getGlobalData()
get global data
Definition: AmdCL2Binaries.h:199
CString kernelName
kernel name
Definition: AmdCL2Binaries.h:256
Elf64_Rela & getTextRelaEntry(size_t index)
get text rela entry
Definition: AmdCL2Binaries.h:237
const Elf64_Rela & getTextRelaEntry(size_t index) const
get text rela entry
Definition: AmdCL2Binaries.h:234
const AmdCL2GPUKernelMetadata & getMetadataEntry(size_t index) const
get kernel metadata by index
Definition: AmdCL2Binaries.h:406
uint32_t argOffset
virtual argument offset
Definition: AmdCL2Binaries.h:295
an array class
Definition: Containers.h:38
const AmdCL2GPUKernelStub & getKernelStub(size_t index) const
get kernel stub for specified index
Definition: AmdCL2Binaries.h:142
size_t getMetadataSize(size_t index) const
get metadata size for specified inner binary
Definition: AmdCL2Binaries.h:412
uint32_t resId
resource id
Definition: AmdCL2Binaries.h:291
uint32_t kindOfType
kind of type
Definition: AmdCL2Binaries.h:305
size_t getBssSize() const
get bss section&#39;s size
Definition: AmdCL2Binaries.h:216
size_t getBssAlignment() const
get bss alignment
Definition: AmdCL2Binaries.h:213
all AMD binaries creation flags
Definition: AmdBinaries.h:58
size_t getISAMetadataSize(size_t index) const
get ISA metadata size for specified inner binary
Definition: AmdCL2Binaries.h:394
AMD OpenCL 2.0 GPU kernel stub.
Definition: AmdCL2Binaries.h:62
Definition: Elf.h:458
const cxbyte * getBinaryCode() const
return binary code
Definition: AmdCL2Binaries.h:125
size_t getKernelsNum() const
get kernels number
Definition: AmdCL2Binaries.h:82
bool isAmdCL2Binary(size_t binarySize, const cxbyte *binary)
check whether is Amd OpenCL 2.0 binary
AMD OpenCL 2.0 main binary for GPU for 64-bit mode.
Definition: AmdCL2Binaries.h:314
AmdCL2InnerGPUBinaryBase & getInnerBinaryBase()
get inner binary base
Definition: AmdCL2Binaries.h:358
size_t getISAMetadatasNum() const
get number of ISA metadatas
Definition: AmdCL2Binaries.h:384
uint64_t size
entry size
Definition: AmdCL2Binaries.h:285
uint64_t secondNameLength
second name length
Definition: AmdCL2Binaries.h:275
AmdCL2GPUKernel & getKernelData(size_t index)
get kernel data for specified index
Definition: AmdCL2Binaries.h:90
create kernel setup
Definition: AmdCL2Binaries.h:46
uint16_t kernelId
kernel id
Definition: AmdCL2Binaries.h:268
cxbyte isVolatile
nonzero if pointer or pipe
Definition: AmdCL2Binaries.h:301
CString aclVersionString
acl version string
Definition: AmdCL2Binaries.h:326
AMD binaries handling.
const cxbyte * getISAMetadata(size_t index) const
get ISA metadata for specified inner binary
Definition: AmdCL2Binaries.h:398
uint32_t isConst
is const pointer
Definition: AmdCL2Binaries.h:306
const AmdCL2GPUKernelMetadata & getISAMetadataEntry(size_t index) const
get kernel ISA metadata by kernel index
Definition: AmdCL2Binaries.h:388
main namespace
Definition: AsmFormats.h:41
cxbyte * data
setup data
Definition: AmdCL2Binaries.h:65
bool hasKernelInfo() const
returns true if binary has kernel informations
Definition: AmdCL2Binaries.h:334
bool hasInfoStrings() const
returns true if binary has info strings
Definition: AmdCL2Binaries.h:342
const cxbyte * getGlobalData() const
get global data
Definition: AmdCL2Binaries.h:196
const AmdCL2OldInnerGPUBinary & getOldInnerBinary() const
get old inner binary
Definition: AmdCL2Binaries.h:370
cxbyte * getISAMetadata(size_t index)
get ISA metadata for specified inner binary
Definition: AmdCL2Binaries.h:402
uint64_t argNameSize
argument name size
Definition: AmdCL2Binaries.h:286
main AMD binary base class
Definition: AmdBinaries.h:399
AMD OpenCL 2.0 GPU metadata for kernel.
Definition: AmdCL2Binaries.h:52
inlines for accessing memory words in LittleEndian and unaligned
cxbyte * getMetadata(size_t index)
get metadata for specified inner binary
Definition: AmdCL2Binaries.h:420
AmdCL2InnerGPUBinary & getInnerBinary()
get inner binary
Definition: AmdCL2Binaries.h:366
const CString & getAclVersionString() const
get acl version string
Definition: AmdCL2Binaries.h:424
size_t getGlobalDataSize() const
get global data size
Definition: AmdCL2Binaries.h:192
bool hasKernelDataMap() const
return if binary has kernel datas map
Definition: AmdCL2Binaries.h:135
const AmdGPUKernelHeader & getKernelHeaderEntry(size_t index) const
get kernel header for specified index
Definition: AmdCL2Binaries.h:378
cxbyte * data
data
Definition: AmdCL2Binaries.h:258
size_t size
size
Definition: AmdCL2Binaries.h:257
bool hasKernelDataMap() const
return if binary has kernel datas map
Definition: AmdCL2Binaries.h:188
AMD OpenCL 2.0 inner binary for GPU binaries that represent a single kernel.
Definition: AmdCL2Binaries.h:153
KernelDataMap kernelDataMap
kernel data map
Definition: AmdCL2Binaries.h:78
bool hasKernelInfoMap() const
returns true if binary has kernel informations map
Definition: AmdCL2Binaries.h:338
utilities for other libraries and programs
create compile options and driver info
Definition: AmdBinaries.h:49
const AmdCL2GPUKernel & getKernelData(size_t index) const
get kernel data for specified index
Definition: AmdCL2Binaries.h:86
uint64_t size
size
Definition: AmdCL2Binaries.h:264
size_t size
setup size
Definition: AmdCL2Binaries.h:64
GPU kernel argument entry.
Definition: AmdCL2Binaries.h:283
size_t getSamplerInitSize() const
get global data size
Definition: AmdCL2Binaries.h:220
const AmdCL2InnerGPUBinaryBase & getInnerBinaryBase() const
get inner binary base
Definition: AmdCL2Binaries.h:354
uint32_t vectorLength
vector length (for old drivers not aligned)
Definition: AmdCL2Binaries.h:290
Array< AmdCL2GPUKernelMetadata > isaMetadatas
AMD metadatas.
Definition: AmdCL2Binaries.h:322
AMD OpenCL 2.0 old inner binary for GPU binaries that represent a single kernel.
Definition: AmdCL2Binaries.h:98
bool hasKernelStubs() const
return if binary has kernel stubs
Definition: AmdCL2Binaries.h:138
Elf64_Rela & getGlobalDataRelaEntry(size_t index)
get global data rela entry
Definition: AmdCL2Binaries.h:248
size_t size() const
returns number of elements
Definition: Containers.h:169
create kernel informations
Definition: AmdBinaries.h:46
const T * data() const
get data
Definition: Containers.h:242
uint32_t argType
argument type
Definition: AmdCL2Binaries.h:296
uint64_t typeNameSize
type name size
Definition: AmdCL2Binaries.h:287
size_t setupSize
setup size
Definition: AmdCL2Binaries.h:55
const Elf64_Rela & getGlobalDataRelaEntry(size_t index) const
get global data rela entry
Definition: AmdCL2Binaries.h:244
CString kernelName
kernel name
Definition: AmdCL2Binaries.h:54
create kernel setups map
Definition: AmdCL2Binaries.h:43
const cxbyte * getSamplerInit() const
get global data
Definition: AmdCL2Binaries.h:224
cxbyte * setup
setup data
Definition: AmdCL2Binaries.h:56
create map of kernel informations
Definition: AmdBinaries.h:47
size_t getRwDataSize() const
get readwrite global data size
Definition: AmdCL2Binaries.h:203
cxbyte * getRwData()
get readwrite atomic data
Definition: AmdCL2Binaries.h:210
cxbyte isPipe
if pipe
Definition: AmdCL2Binaries.h:303
AmdCL2OldInnerGPUBinary & getOldInnerBinary()
get old inner binary
Definition: AmdCL2Binaries.h:374
create kernel stub
Definition: AmdCL2Binaries.h:48
const cxbyte * getMetadata(size_t index) const
get metadata for specified inner binary
Definition: AmdCL2Binaries.h:416
uint32_t ptrType
pointer type
Definition: AmdCL2Binaries.h:298
simple C-string container
Definition: CString.h:38
create kernel stub
Definition: AmdCL2Binaries.h:44
containers and other utils for other libraries and programs
AMD OpenCL 2.0 inner binary base class.
Definition: AmdCL2Binaries.h:71
cxuint getDriverVersion() const
get driver version
Definition: AmdCL2Binaries.h:350
cxbyte * getSamplerInit()
get global data
Definition: AmdCL2Binaries.h:227
AMD OpenCL 2.0 GPU metadata for kernel.
Definition: AmdCL2Binaries.h:254
bool hasKernelData() const
return if binary has kernel datas
Definition: AmdCL2Binaries.h:185