CLRX  1
An unofficial OpenCL extensions designed for Radeon GPUs
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
AsmFormats.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_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>
37 #include <CLRX/utils/Utilities.h>
38 #include <CLRX/amdasm/Commons.h>
39 
40 namespace CLRX
41 {
42 
44 enum class AsmSectionType: cxbyte
45 {
46  DATA = 0,
47  CODE,
48  CONFIG,
50 
52  AMD_METADATA,
53  AMD_CALNOTE,
54 
55  AMDCL2_RWDATA = LAST_COMMON+1,
56  AMDCL2_BSS,
57  AMDCL2_SAMPLERINIT,
58  AMDCL2_SETUP,
59  AMDCL2_STUB,
60  AMDCL2_METADATA,
61  AMDCL2_ISAMETADATA,
62 
64  EXTRA_FIRST = 0xfc,
65  EXTRA_PROGBITS = 0xfc,
66  EXTRA_NOBITS = 0xfd,
67  EXTRA_NOTE = 0xfe,
68  EXTRA_SECTION = 0xff
69 };
70 
71 enum: cxuint
72 {
73  ASMSECT_ABS = UINT_MAX,
74  ASMSECT_NONE = UINT_MAX,
75  ASMKERN_GLOBAL = UINT_MAX,
76  ASMKERN_INNER = UINT_MAX-1
77 };
78 
79 enum: Flags
80 {
81  ASMSECT_WRITEABLE = 1,
82  ASMSECT_ADDRESSABLE = 2,
83  ASMSECT_ABS_ADDRESSABLE = 4,
85 
86  ASMELFSECT_ALLOCATABLE = 0x10,
87  ASMELFSECT_WRITEABLE = 0x20,
88  ASMELFSECT_EXECUTABLE = 0x40
89 };
90 
91 class Assembler;
92 class AsmExpression;
93 struct AsmRelocation;
94 struct AsmSymbol;
95 
98 {
99 public:
101  AsmFormatException() = default;
103  explicit AsmFormatException(const std::string& message);
105  virtual ~AsmFormatException() noexcept = default;
106 };
107 
110 {
111 public:
113  struct SectionInfo
114  {
115  const char* name;
118  };
119 protected:
121 
124 public:
125  virtual ~AsmFormatHandler();
126 
128 
134  virtual cxuint addKernel(const char* kernelName) = 0;
136 
143  virtual cxuint addSection(const char* sectionName, cxuint kernelId) = 0;
144 
146  virtual cxuint getSectionId(const char* sectionName) const = 0;
147 
149  virtual void setCurrentKernel(cxuint kernel) = 0;
151  virtual void setCurrentSection(cxuint sectionId) = 0;
152 
154  virtual SectionInfo getSectionInfo(cxuint sectionId) const = 0;
156  virtual bool parsePseudoOp(const CString& firstName,
157  const char* stmtPlace, const char* linePtr) = 0;
159  virtual void handleLabel(const CString& label);
161  virtual bool resolveSymbol(const AsmSymbol& symbol,
162  uint64_t& value, cxuint& sectionId);
164  virtual bool resolveRelocation(const AsmExpression* expr,
165  uint64_t& value, cxuint& sectionId);
167  virtual bool prepareBinary() = 0;
169  virtual void writeBinary(std::ostream& os) const = 0;
171  virtual void writeBinary(Array<cxbyte>& array) const = 0;
172 };
173 
176 {
177 public:
181  ~AsmRawCodeHandler() = default;
182 
183  cxuint addKernel(const char* kernelName);
184  cxuint addSection(const char* sectionName, cxuint kernelId);
185 
186  cxuint getSectionId(const char* sectionName) const;
187 
188  void setCurrentKernel(cxuint kernel);
189  void setCurrentSection(cxuint sectionId);
190 
191  SectionInfo getSectionInfo(cxuint sectionId) const;
192  bool parsePseudoOp(const CString& firstName,
193  const char* stmtPlace, const char* linePtr);
194 
195  bool prepareBinary();
196  void writeBinary(std::ostream& os) const;
197  void writeBinary(Array<cxbyte>& array) const;
198 };
199 
202 {
203 private:
204  typedef std::unordered_map<CString, cxuint> SectionMap;
205  friend struct AsmAmdPseudoOps;
206  AmdInput output;
207  struct Section
208  {
209  cxuint kernelId;
210  AsmSectionType type;
211  cxuint elfBinSectId;
212  const char* name;
213  uint32_t extraId; // for example CALNote id
214  };
215  struct Kernel
216  {
217  cxuint headerSection;
218  cxuint metadataSection;
219  cxuint configSection;
220  cxuint codeSection;
221  cxuint dataSection;
222  std::vector<cxuint> calNoteSections;
223  SectionMap extraSectionMap;
224  cxuint extraSectionCount;
225  cxuint savedSection;
226  std::unordered_set<CString> argNamesSet;
227  cxuint allocRegs[2];
228  Flags allocRegFlags;
229  };
230  std::vector<Section> sections;
231  // use pointer to prevents copying Kernel objects
232  std::vector<Kernel*> kernelStates;
233  SectionMap extraSectionMap;
234  cxuint dataSection; // global
235  cxuint savedSection;
236  cxuint extraSectionCount;
237 
238  void saveCurrentSection();
239  void restoreCurrentAllocRegs();
240  void saveCurrentAllocRegs();
241 public:
243  explicit AsmAmdHandler(Assembler& assembler);
245  ~AsmAmdHandler();
246 
247  cxuint addKernel(const char* kernelName);
248  cxuint addSection(const char* sectionName, cxuint kernelId);
249 
250  cxuint getSectionId(const char* sectionName) const;
251  void setCurrentKernel(cxuint kernel);
252  void setCurrentSection(cxuint sectionId);
253 
254  SectionInfo getSectionInfo(cxuint sectionId) const;
255  bool parsePseudoOp(const CString& firstName,
256  const char* stmtPlace, const char* linePtr);
257 
258  bool prepareBinary();
259  void writeBinary(std::ostream& os) const;
260  void writeBinary(Array<cxbyte>& array) const;
262  const AmdInput* getOutput() const
263  { return &output; }
264 };
265 
268 {
269 private:
270  typedef std::unordered_map<CString, cxuint> SectionMap;
271  friend struct AsmAmdCL2PseudoOps;
272  AmdCL2Input output;
273  struct Section
274  {
275  cxuint kernelId;
276  AsmSectionType type;
277  cxuint elfBinSectId;
278  const char* name;
279  uint32_t extraId;
280  };
281  struct Relocation
282  {
283  RelocType type;
284  cxuint symbol; // 0,1,2
285  size_t addend;
286  };
287  /* relocmap: key - symbol, value - relocation */
288  typedef std::unordered_map<CString, Relocation> RelocMap;
289  struct Kernel
290  {
291  cxuint stubSection;
292  cxuint setupSection;
293  cxuint metadataSection;
294  cxuint isaMetadataSection;
295  cxuint configSection;
296  cxuint codeSection;
297  cxuint savedSection;
298  std::unordered_set<CString> argNamesSet;
299  cxuint allocRegs[2];
300  Flags allocRegFlags;
301  };
302  std::vector<Section> sections;
303  // use pointer to prevents copying Kernel objects
304  std::vector<Kernel*> kernelStates;
305  RelocMap relocsMap;
306  SectionMap extraSectionMap;
307  SectionMap innerExtraSectionMap;
308  cxuint rodataSection; // global inner
309  cxuint dataSection; // global inner
310  cxuint bssSection; // global inner
311  cxuint samplerInitSection;
312  cxuint savedSection;
313  cxuint innerSavedSection;
314  cxuint extraSectionCount;
315  cxuint innerExtraSectionCount;
316 
317  void saveCurrentSection();
318  void restoreCurrentAllocRegs();
319  void saveCurrentAllocRegs();
320  cxuint getDriverVersion() const;
321 public:
326 
327  cxuint addKernel(const char* kernelName);
328  cxuint addSection(const char* sectionName, cxuint kernelId);
329 
330  cxuint getSectionId(const char* sectionName) const;
331  void setCurrentKernel(cxuint kernel);
332  void setCurrentSection(cxuint sectionId);
333 
334  SectionInfo getSectionInfo(cxuint sectionId) const;
335  bool parsePseudoOp(const CString& firstName,
336  const char* stmtPlace, const char* linePtr);
337 
338  bool resolveSymbol(const AsmSymbol& symbol, uint64_t& value, cxuint& sectionId);
339  bool resolveRelocation(const AsmExpression* expr, uint64_t& value, cxuint& sectionId);
340  bool prepareBinary();
341  void writeBinary(std::ostream& os) const;
342  void writeBinary(Array<cxbyte>& array) const;
344  const AmdCL2Input* getOutput() const
345  { return &output; }
346 };
347 
350 {
351 private:
352  enum class Inside : cxbyte {
353  MAINLAYOUT, CONFIG, ARGS, PROGINFO
354  };
355 
356  typedef std::unordered_map<CString, cxuint> SectionMap;
357  friend struct AsmGalliumPseudoOps;
358  GalliumInput output;
359  struct Section
360  {
361  cxuint kernelId;
362  AsmSectionType type;
363  cxuint elfBinSectId;
364  const char* name; // must be available by whole lifecycle
365  };
366  struct Kernel
367  {
368  cxuint defaultSection;
369  bool hasProgInfo;
370  cxbyte progInfoEntries;
371  cxuint allocRegs[2];
372  Flags allocRegFlags;
373  };
374  std::vector<Kernel> kernelStates;
375  std::vector<Section> sections;
376  std::vector<cxuint> kcodeSelection; // kcode
377  std::stack<std::vector<cxuint> > kcodeSelStack;
378  cxuint currentKcodeKernel;
379  SectionMap extraSectionMap;
380  cxuint codeSection;
381  cxuint dataSection;
382  cxuint commentSection;
383  cxuint savedSection;
384  Inside inside;
385  cxuint extraSectionCount;
386 
387  void restoreKcodeCurrentAllocRegs();
388  void saveKcodeCurrentAllocRegs();
389 public:
393  ~AsmGalliumHandler() = default;
394 
395  cxuint addKernel(const char* kernelName);
396  cxuint addSection(const char* sectionName, cxuint kernelId);
397 
398  cxuint getSectionId(const char* sectionName) const;
399  void setCurrentKernel(cxuint kernel);
400  void setCurrentSection(cxuint sectionId);
401 
402  SectionInfo getSectionInfo(cxuint sectionId) const;
403  bool parsePseudoOp(const CString& firstName,
404  const char* stmtPlace, const char* linePtr);
405  void handleLabel(const CString& label);
406 
407  bool prepareBinary();
408  void writeBinary(std::ostream& os) const;
409  void writeBinary(Array<cxbyte>& array) const;
411  const GalliumInput* getOutput() const
412  { return &output; }
413 };
414 
415 };
416 
417 #endif
void writeBinary(std::ostream &os) const
write binary to output stream
virtual bool resolveSymbol(const AsmSymbol &symbol, uint64_t &value, cxuint &sectionId)
resolve symbol if needed (for example that comes from unresolvable sections)
code of program or kernel
~AsmRawCodeHandler()=default
destructor
common definitions for assembler and disassembler
main class of assembler
Definition: Assembler.h:559
cxuint addKernel(const char *kernelName)
add/set kernel
Assembler & assembler
assembler reference
Definition: AsmFormats.h:120
cxuint addSection(const char *sectionName, cxuint kernelId)
add section
non copyable and non movable base structure (class)
Definition: Utilities.h:43
assembler expression class
Definition: Assembler.h:350
cxuint getSectionId(const char *sectionName) const
get section id if exists in current context, otherwise returns ASMSECT_NONE
uint32_t Flags
type for declaring various flags
Definition: Utilities.h:97
void writeBinary(std::ostream &os) const
write binary to output stream
bool prepareBinary()
prepare binary for use
virtual void writeBinary(std::ostream &os) const =0
write binary to output stream
~AsmAmdCL2Handler()
destructor
virtual bool parsePseudoOp(const CString &firstName, const char *stmtPlace, const char *linePtr)=0
parse pseudo-op (return true if recognized pseudo-op)
Flags flags
section flags
Definition: AsmFormats.h:117
configuration (global or for kernel)
AMD Catalyst kernel's metadata.
void setCurrentSection(cxuint sectionId)
set current section, this method can change current kernel if that required
handles raw code format
Definition: AsmFormats.h:175
Gallium input.
Definition: GalliumBinaries.h:385
AsmSectionType type
section type
Definition: AsmFormats.h:116
AsmAmdCL2Handler(Assembler &assembler)
constructor
cxuint addKernel(const char *kernelName)
add/set kernel
const char * name
section name
Definition: AsmFormats.h:115
void writeBinary(std::ostream &os) const
write binary to output stream
virtual cxuint addKernel(const char *kernelName)=0
add/set kernel
an array class
Definition: Containers.h:38
SectionInfo getSectionInfo(cxuint sectionId) const
get current section flags and type
SectionInfo getSectionInfo(cxuint sectionId) const
get current section flags and type
cxuint RelocType
relocation type
Definition: Commons.h:32
bool resolveSymbol(const AsmSymbol &symbol, uint64_t &value, cxuint &sectionId)
resolve symbol if needed (for example that comes from unresolvable sections)
cxuint addSection(const char *sectionName, cxuint kernelId)
add section
void setCurrentKernel(cxuint kernel)
set current kernel
virtual SectionInfo getSectionInfo(cxuint sectionId) const =0
get current section flags and type
~AsmAmdHandler()
destructor
cxuint getSectionId(const char *sectionName) const
get section id if exists in current context, otherwise returns ASMSECT_NONE
GalliumCompute binaries handling (only with LLVM 3.6)
void setCurrentKernel(cxuint kernel)
set current kernel
const AmdCL2Input * getOutput() const
get output structure pointer
Definition: AsmFormats.h:344
AsmSectionType
assembler section type
Definition: AsmFormats.h:44
AMD binaries handling.
assdembler format handler
Definition: AsmFormats.h:109
absolute section id
Definition: AsmFormats.h:73
cxuint addSection(const char *sectionName, cxuint kernelId)
add section
bool prepareBinary()
prepare binary for use
AMD OpenCL2 binaries generator.
virtual cxuint addSection(const char *sectionName, cxuint kernelId)=0
add section
void setCurrentSection(cxuint sectionId)
set current section, this method can change current kernel if that required
bool parsePseudoOp(const CString &firstName, const char *stmtPlace, const char *linePtr)
parse pseudo-op (return true if recognized pseudo-op)
AsmFormatException()=default
default constructor
bool resolveRelocation(const AsmExpression *expr, uint64_t &value, cxuint &sectionId)
resolve relocation for specified expression
cxuint addSection(const char *sectionName, cxuint kernelId)
add section
const GalliumInput * getOutput() const
get output object (input for bingenerator)
Definition: AsmFormats.h:411
no kernel, inner global space
Definition: AsmFormats.h:76
cxuint addKernel(const char *kernelName)
add/set kernel
void setCurrentKernel(cxuint kernel)
set current kernel
section information
Definition: AsmFormats.h:113
cxuint getSectionId(const char *sectionName) const
get section id if exists in current context, otherwise returns ASMSECT_NONE
bool parsePseudoOp(const CString &firstName, const char *stmtPlace, const char *linePtr)
parse pseudo-op (return true if recognized pseudo-op)
void handleLabel(const CString &label)
handle labels
none section id
Definition: AsmFormats.h:74
bool parsePseudoOp(const CString &firstName, const char *stmtPlace, const char *linePtr)
parse pseudo-op (return true if recognized pseudo-op)
AMD Catalyst kernel's header.
AsmRawCodeHandler(Assembler &assembler)
constructor
utilities for other libraries and programs
AsmGalliumHandler(Assembler &assembler)
construcror
virtual bool prepareBinary()=0
prepare binary for use
std::string message
message
Definition: Utilities.h:61
bool prepareBinary()
prepare binary for use
AsmAmdHandler(Assembler &assembler)
constructor
virtual bool resolveRelocation(const AsmExpression *expr, uint64_t &value, cxuint &sectionId)
resolve relocation for specified expression
no kernel, global space
Definition: AsmFormats.h:75
~AsmGalliumHandler()=default
destructor
exception class
Definition: Utilities.h:58
AMD binaries generator.
cxuint addKernel(const char *kernelName)
add/set kernel
main Input for AmdCL2GPUBinGenerator
Definition: AmdCL2BinGen.h:112
virtual void setCurrentSection(cxuint sectionId)=0
set current section, this method can change current kernel if that required
bool prepareBinary()
prepare binary for use
SectionInfo getSectionInfo(cxuint sectionId) const
get current section flags and type
virtual void setCurrentKernel(cxuint kernel)=0
set current kernel
const AmdInput * getOutput() const
get output structure pointer
Definition: AsmFormats.h:262
main Input for AmdGPUBinGenerator
Definition: AmdBinGen.h:154
kernel or global data
virtual ~AsmFormatException() noexcept=default
destructor
AsmFormatHandler(Assembler &assembler)
constructor
void setCurrentSection(cxuint sectionId)
set current section, this method can change current kernel if that required
void setCurrentSection(cxuint sectionId)
set current section, this method can change current kernel if that required
handles GalliumCompute format
Definition: AsmFormats.h:349
void writeBinary(std::ostream &os) const
write binary to output stream
cxuint getSectionId(const char *sectionName) const
get section id if exists in current context, otherwise returns ASMSECT_NONE
assembler symbol structure
Definition: Assembler.h:235
SectionInfo getSectionInfo(cxuint sectionId) const
get current section flags and type
void setCurrentKernel(cxuint kernel)
set current kernel
handles AMD Catalyst format
Definition: AsmFormats.h:201
handles AMD OpenCL 2.0 binary format
Definition: AsmFormats.h:267
assembler format exception
Definition: AsmFormats.h:97
bool parsePseudoOp(const CString &firstName, const char *stmtPlace, const char *linePtr)
parse pseudo-op (return true if recognized pseudo-op)
section is unresolvable
Definition: AsmFormats.h:84
simple C-string container
Definition: CString.h:38
virtual void handleLabel(const CString &label)
handle labels
virtual cxuint getSectionId(const char *sectionName) const =0
get section id if exists in current context, otherwise returns ASMSECT_NONE