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