CLRX  1
An unofficial OpenCL extensions designed for Radeon GPUs
AsmFormats.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_ASMFORMATS_H__
24 #define __CLRX_ASMFORMATS_H__
25 
26 #include <CLRX/Config.h>
27 #include <cstdint>
28 #include <string>
29 #include <vector>
30 #include <utility>
31 #include <unordered_set>
32 #include <unordered_map>
35 #include <CLRX/amdbin/AmdBinGen.h>
38 #include <CLRX/utils/Utilities.h>
39 #include <CLRX/utils/GPUId.h>
40 #include <CLRX/amdasm/Commons.h>
41 
42 namespace CLRX
43 {
44 
46 enum class AsmSectionType: cxbyte
47 {
48  DATA = 0,
49  CODE,
50  CONFIG,
52 
53  AMD_HEADER = LAST_COMMON+1,
54  AMD_METADATA,
55  AMD_CALNOTE,
56 
57  AMDCL2_RWDATA = LAST_COMMON+1,
58  AMDCL2_BSS,
59  AMDCL2_SAMPLERINIT,
60  AMDCL2_SETUP,
61  AMDCL2_STUB,
62  AMDCL2_METADATA,
63  AMDCL2_ISAMETADATA,
64 
65  GALLIUM_COMMENT = LAST_COMMON+1,
66 
67  ROCM_COMMENT = LAST_COMMON+1,
68  ROCM_CONFIG_CTRL_DIRECTIVE,
69 
70  EXTRA_FIRST = 0xfc,
71  EXTRA_PROGBITS = 0xfc,
72  EXTRA_NOBITS = 0xfd,
73  EXTRA_NOTE = 0xfe,
74  EXTRA_SECTION = 0xff
75 };
76 
77 enum: cxuint
78 {
79  ASMSECT_ABS = UINT_MAX,
80  ASMSECT_NONE = UINT_MAX,
81  ASMKERN_GLOBAL = UINT_MAX,
82  ASMKERN_INNER = UINT_MAX-1
83 };
84 
85 enum: Flags
86 {
87  ASMSECT_WRITEABLE = 1,
88  ASMSECT_ADDRESSABLE = 2,
89  ASMSECT_ABS_ADDRESSABLE = 4,
91 
92  ASMELFSECT_ALLOCATABLE = 0x10,
93  ASMELFSECT_WRITEABLE = 0x20,
94  ASMELFSECT_EXECUTABLE = 0x40
95 };
96 
97 class Assembler;
98 class AsmExpression;
99 struct AsmRelocation;
100 struct AsmSymbol;
101 
104 {
105 public:
107  AsmFormatException() = default;
109  explicit AsmFormatException(const std::string& message);
111  virtual ~AsmFormatException() noexcept = default;
112 };
113 
116 {
117 public:
119  struct SectionInfo
120  {
121  const char* name;
124  };
125 protected:
127 
129  explicit AsmFormatHandler(Assembler& assembler);
130 public:
131  virtual ~AsmFormatHandler();
132 
134 
140  virtual cxuint addKernel(const char* kernelName) = 0;
142 
149  virtual cxuint addSection(const char* sectionName, cxuint kernelId) = 0;
150 
152  virtual cxuint getSectionId(const char* sectionName) const = 0;
153 
155  virtual void setCurrentKernel(cxuint kernel) = 0;
157  virtual void setCurrentSection(cxuint sectionId) = 0;
158 
160  virtual SectionInfo getSectionInfo(cxuint sectionId) const = 0;
162  virtual bool parsePseudoOp(const CString& firstName,
163  const char* stmtPlace, const char* linePtr) = 0;
165  virtual void handleLabel(const CString& label);
167  virtual bool resolveSymbol(const AsmSymbol& symbol,
168  uint64_t& value, cxuint& sectionId);
170  virtual bool resolveRelocation(const AsmExpression* expr,
171  uint64_t& value, cxuint& sectionId);
173  virtual bool prepareBinary() = 0;
175  virtual void writeBinary(std::ostream& os) const = 0;
177  virtual void writeBinary(Array<cxbyte>& array) const = 0;
178 };
179 
182 {
183 public:
185  explicit AsmRawCodeHandler(Assembler& assembler);
187  ~AsmRawCodeHandler() = default;
188 
189  cxuint addKernel(const char* kernelName);
190  cxuint addSection(const char* sectionName, cxuint kernelId);
191 
192  cxuint getSectionId(const char* sectionName) const;
193 
194  void setCurrentKernel(cxuint kernel);
195  void setCurrentSection(cxuint sectionId);
196 
197  SectionInfo getSectionInfo(cxuint sectionId) const;
198  bool parsePseudoOp(const CString& firstName,
199  const char* stmtPlace, const char* linePtr);
200 
201  bool prepareBinary();
202  void writeBinary(std::ostream& os) const;
203  void writeBinary(Array<cxbyte>& array) const;
204 };
205 
208 {
209 private:
210  typedef std::unordered_map<CString, cxuint> SectionMap;
211  friend struct AsmAmdPseudoOps;
212  AmdInput output;
213  struct Section
214  {
215  cxuint kernelId;
216  AsmSectionType type;
217  cxuint elfBinSectId;
218  const char* name;
219  uint32_t extraId; // for example CALNote id
220  };
221  struct Kernel
222  {
223  cxuint headerSection;
224  cxuint metadataSection;
225  cxuint configSection;
226  cxuint codeSection;
227  cxuint dataSection;
228  std::vector<cxuint> calNoteSections;
229  SectionMap extraSectionMap;
230  cxuint extraSectionCount;
231  cxuint savedSection;
232  std::unordered_set<CString> argNamesSet;
233  cxuint allocRegs[MAX_REGTYPES_NUM];
234  Flags allocRegFlags;
235  };
236  std::vector<Section> sections;
237  // use pointer to prevents copying Kernel objects
238  std::vector<Kernel*> kernelStates;
239  SectionMap extraSectionMap;
240  cxuint dataSection; // global
241  cxuint savedSection;
242  cxuint extraSectionCount;
243 
244  void saveCurrentSection();
245  void restoreCurrentAllocRegs();
246  void saveCurrentAllocRegs();
247 public:
249  explicit AsmAmdHandler(Assembler& assembler);
251  ~AsmAmdHandler();
252 
253  cxuint addKernel(const char* kernelName);
254  cxuint addSection(const char* sectionName, cxuint kernelId);
255 
256  cxuint getSectionId(const char* sectionName) const;
257  void setCurrentKernel(cxuint kernel);
258  void setCurrentSection(cxuint sectionId);
259 
260  SectionInfo getSectionInfo(cxuint sectionId) const;
261  bool parsePseudoOp(const CString& firstName,
262  const char* stmtPlace, const char* linePtr);
263 
264  bool prepareBinary();
265  void writeBinary(std::ostream& os) const;
266  void writeBinary(Array<cxbyte>& array) const;
268  const AmdInput* getOutput() const
269  { return &output; }
270 };
271 
274 {
275 private:
276  typedef std::unordered_map<CString, cxuint> SectionMap;
277  friend struct AsmAmdCL2PseudoOps;
278  AmdCL2Input output;
279  struct Section
280  {
281  cxuint kernelId;
282  AsmSectionType type;
283  cxuint elfBinSectId;
284  const char* name;
285  uint32_t extraId;
286  };
287  struct Relocation
288  {
289  RelocType type;
290  cxuint symbol; // 0,1,2
291  size_t addend;
292  };
293  /* relocmap: key - symbol, value - relocation */
294  typedef std::unordered_map<CString, Relocation> RelocMap;
295  struct Kernel
296  {
297  cxuint stubSection;
298  cxuint setupSection;
299  cxuint metadataSection;
300  cxuint isaMetadataSection;
301  cxuint configSection;
302  cxuint codeSection;
303  cxuint savedSection;
304  std::unordered_set<CString> argNamesSet;
305  cxuint allocRegs[MAX_REGTYPES_NUM];
306  Flags allocRegFlags;
307  };
308  std::vector<Section> sections;
309  // use pointer to prevents copying Kernel objects
310  std::vector<Kernel*> kernelStates;
311  RelocMap relocsMap;
312  SectionMap extraSectionMap;
313  SectionMap innerExtraSectionMap;
314  cxuint rodataSection; // global inner
315  cxuint dataSection; // global inner
316  cxuint bssSection; // global inner
317  cxuint samplerInitSection;
318  cxuint savedSection;
319  cxuint innerSavedSection;
320  cxuint extraSectionCount;
321  cxuint innerExtraSectionCount;
322 
323  void saveCurrentSection();
324  void restoreCurrentAllocRegs();
325  void saveCurrentAllocRegs();
326  cxuint getDriverVersion() const;
327 public:
329  explicit AsmAmdCL2Handler(Assembler& assembler);
331  ~AsmAmdCL2Handler();
332 
333  cxuint addKernel(const char* kernelName);
334  cxuint addSection(const char* sectionName, cxuint kernelId);
335 
336  cxuint getSectionId(const char* sectionName) const;
337  void setCurrentKernel(cxuint kernel);
338  void setCurrentSection(cxuint sectionId);
339 
340  SectionInfo getSectionInfo(cxuint sectionId) const;
341  bool parsePseudoOp(const CString& firstName,
342  const char* stmtPlace, const char* linePtr);
343 
344  bool resolveSymbol(const AsmSymbol& symbol, uint64_t& value, cxuint& sectionId);
345  bool resolveRelocation(const AsmExpression* expr, uint64_t& value, cxuint& sectionId);
346  bool prepareBinary();
347  void writeBinary(std::ostream& os) const;
348  void writeBinary(Array<cxbyte>& array) const;
350  const AmdCL2Input* getOutput() const
351  { return &output; }
352 };
353 
356 {
357 private:
358  enum class Inside : cxbyte {
359  MAINLAYOUT, CONFIG, ARGS, PROGINFO
360  };
361 
362  typedef std::unordered_map<CString, cxuint> SectionMap;
363  friend struct AsmGalliumPseudoOps;
364  GalliumInput output;
365  struct Section
366  {
367  cxuint kernelId;
368  AsmSectionType type;
369  cxuint elfBinSectId;
370  const char* name; // must be available by whole lifecycle
371  };
372  struct Kernel
373  {
374  cxuint defaultSection;
375  bool hasProgInfo;
376  cxbyte progInfoEntries;
377  cxuint allocRegs[MAX_REGTYPES_NUM];
378  Flags allocRegFlags;
379  };
380  std::vector<Kernel> kernelStates;
381  std::vector<Section> sections;
382  std::vector<cxuint> kcodeSelection; // kcode
383  std::stack<std::vector<cxuint> > kcodeSelStack;
384  cxuint currentKcodeKernel;
385  SectionMap extraSectionMap;
386  cxuint codeSection;
387  cxuint dataSection;
388  cxuint commentSection;
389  cxuint savedSection;
390  Inside inside;
391  cxuint extraSectionCount;
392 
393  void restoreKcodeCurrentAllocRegs();
394  void saveKcodeCurrentAllocRegs();
395 public:
397  explicit AsmGalliumHandler(Assembler& assembler);
399  ~AsmGalliumHandler() = default;
400 
401  cxuint addKernel(const char* kernelName);
402  cxuint addSection(const char* sectionName, cxuint kernelId);
403 
404  cxuint getSectionId(const char* sectionName) const;
405  void setCurrentKernel(cxuint kernel);
406  void setCurrentSection(cxuint sectionId);
407 
408  SectionInfo getSectionInfo(cxuint sectionId) const;
409  bool parsePseudoOp(const CString& firstName,
410  const char* stmtPlace, const char* linePtr);
411  void handleLabel(const CString& label);
412 
413  bool prepareBinary();
414  void writeBinary(std::ostream& os) const;
415  void writeBinary(Array<cxbyte>& array) const;
417  const GalliumInput* getOutput() const
418  { return &output; }
419 };
420 
423 {
424  cxuint dimMask;
425  cxuint usedVGPRsNum;
426  cxuint usedSGPRsNum;
427  cxbyte userDataNum;
428  bool ieeeMode;
429  cxbyte floatMode;
430  cxbyte priority;
431  cxbyte exceptions;
432  bool tgSize;
433  bool debugMode;
435  bool dx10Clamp;
436 };
437 
440 {
441 private:
442  typedef std::unordered_map<CString, cxuint> SectionMap;
443  friend struct AsmROCmPseudoOps;
444  ROCmInput output;
445  struct Section
446  {
447  cxuint kernelId;
448  AsmSectionType type;
449  cxuint elfBinSectId;
450  const char* name; // must be available by whole lifecycle
451  };
452  struct Kernel
453  {
454  cxuint configSection;
455  std::unique_ptr<AsmROCmKernelConfig> config;
456  bool isFKernel;
457  cxuint ctrlDirSection;
458  cxuint savedSection;
459  Flags allocRegFlags;
460  cxuint allocRegs[MAX_REGTYPES_NUM];
461 
462  void initializeKernelConfig();
463  };
464  std::vector<Kernel*> kernelStates;
465  std::vector<Section> sections;
466  std::vector<cxuint> kcodeSelection; // kcode
467  std::stack<std::vector<cxuint> > kcodeSelStack;
468  cxuint currentKcodeKernel;
469  SectionMap extraSectionMap;
470  cxuint codeSection;
471  cxuint commentSection;
472  cxuint savedSection;
473  cxuint extraSectionCount;
474 
475  void restoreKcodeCurrentAllocRegs();
476  void saveKcodeCurrentAllocRegs();
477 
478 public:
480  explicit AsmROCmHandler(Assembler& assembler);
482  ~AsmROCmHandler();
483 
484  cxuint addKernel(const char* kernelName);
485  cxuint addSection(const char* sectionName, cxuint kernelId);
486 
487  cxuint getSectionId(const char* sectionName) const;
488  void setCurrentKernel(cxuint kernel);
489  void setCurrentSection(cxuint sectionId);
490 
491  SectionInfo getSectionInfo(cxuint sectionId) const;
492  bool parsePseudoOp(const CString& firstName,
493  const char* stmtPlace, const char* linePtr);
494  void handleLabel(const CString& label);
495 
496  bool prepareBinary();
497  void writeBinary(std::ostream& os) const;
498  void writeBinary(Array<cxbyte>& array) const;
500  const ROCmInput* getOutput() const
501  { return &output; }
502 };
503 
504 };
505 
506 #endif
cxbyte userDataNum
number of user data
Definition: AsmFormats.h:427
code of program or kernel
bool dx10Clamp
DX10 CLAMP mode.
Definition: AsmFormats.h:435
common definitions for assembler and disassembler
main class of assembler
Definition: Assembler.h:401
Assembler & assembler
assembler reference
Definition: AsmFormats.h:126
non copyable and non movable base structure (class)
Definition: Utilities.h:43
assembler expression class
Definition: AsmDefs.h:277
uint32_t Flags
type for declaring various flags
Definition: Utilities.h:97
ROCm binary input structure.
Definition: ROCmBinaries.h:164
cxuint usedSGPRsNum
number of used SGPRs
Definition: AsmFormats.h:426
Flags flags
section flags
Definition: AsmFormats.h:123
configuration (global or for kernel)
AMD Catalyst kernel&#39;s metadata.
bool tgSize
enable TG_SIZE_EN bit
Definition: AsmFormats.h:432
handles raw code format
Definition: AsmFormats.h:181
cxuint dimMask
mask of dimension (bits: 0 - X, 1 - Y, 2 - Z)
Definition: AsmFormats.h:424
Gallium input.
Definition: GalliumBinaries.h:396
AsmSectionType type
section type
Definition: AsmFormats.h:122
cxbyte priority
priority
Definition: AsmFormats.h:430
AMD HSA kernel configuration structure.
Definition: Commons.h:63
ROCm binaries handling.
const char * name
section name
Definition: AsmFormats.h:121
an array class
Definition: Containers.h:38
bool debugMode
debug mode
Definition: AsmFormats.h:433
handles ROCM binary format
Definition: AsmFormats.h:439
cxuint RelocType
relocation type
Definition: Commons.h:32
cxbyte exceptions
enabled exceptions
Definition: AsmFormats.h:431
data object
Definition: ROCmBinaries.h:52
absolute section id
Definition: AsmFormats.h:79
GalliumCompute binaries handling (only with LLVM 3.6)
assembler relocation
Definition: AsmDefs.h:264
const AmdCL2Input * getOutput() const
get output structure pointer
Definition: AsmFormats.h:350
AsmSectionType
assembler section type
Definition: AsmFormats.h:46
none section id
Definition: AsmFormats.h:80
AMD binaries handling.
assdembler format handler
Definition: AsmFormats.h:115
AMD OpenCL2 binaries generator.
main namespace
Definition: AsmDefs.h:38
const GalliumInput * getOutput() const
get output object (input for bingenerator)
Definition: AsmFormats.h:417
bool ieeeMode
IEEE mode.
Definition: AsmFormats.h:428
cxbyte floatMode
float mode
Definition: AsmFormats.h:429
section information
Definition: AsmFormats.h:119
AMD Catalyst kernel&#39;s header.
utilities for other libraries and programs
exception class
Definition: Utilities.h:58
AMD binaries generator.
main Input for AmdCL2GPUBinGenerator
Definition: AmdCL2BinGen.h:113
GPU identification utilities.
const AmdInput * getOutput() const
get output structure pointer
Definition: AsmFormats.h:268
main Input for AmdGPUBinGenerator
Definition: AmdBinGen.h:154
const ROCmInput * getOutput() const
get output object (input for bingenerator)
Definition: AsmFormats.h:500
handles GalliumCompute format
Definition: AsmFormats.h:355
assembler symbol structure
Definition: AsmDefs.h:147
ROCm kernel configuration.
Definition: AsmFormats.h:422
cxuint usedVGPRsNum
number of used VGPRs
Definition: AsmFormats.h:425
handles AMD Catalyst format
Definition: AsmFormats.h:207
handles AMD OpenCL 2.0 binary format
Definition: AsmFormats.h:273
assembler format exception
Definition: AsmFormats.h:103
section is unresolvable
Definition: AsmFormats.h:90
no kernel, inner global space
Definition: AsmFormats.h:82
simple C-string container
Definition: CString.h:38
bool privilegedMode
prvileged mode
Definition: AsmFormats.h:434
no kernel, global space
Definition: AsmFormats.h:81