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-2018 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 <memory>
32 #include <unordered_set>
33 #include <unordered_map>
36 #include <CLRX/amdbin/AmdBinGen.h>
39 #include <CLRX/utils/Utilities.h>
40 #include <CLRX/utils/GPUId.h>
41 #include <CLRX/amdasm/Commons.h>
42 
43 namespace CLRX
44 {
45 
48 {
49  DATA = 0,
50  CODE,
51  CONFIG,
53 
54  AMD_HEADER = LAST_COMMON+1,
55  AMD_METADATA,
56  AMD_CALNOTE,
57 
58  AMDCL2_RWDATA = LAST_COMMON+1,
59  AMDCL2_BSS,
60  AMDCL2_SAMPLERINIT,
61  AMDCL2_SETUP,
62  AMDCL2_STUB,
63  AMDCL2_METADATA,
64  AMDCL2_ISAMETADATA,
65  AMDCL2_CONFIG_CTRL_DIRECTIVE,
66 
67  GALLIUM_COMMENT = LAST_COMMON+1,
68  GALLIUM_CONFIG_CTRL_DIRECTIVE,
70 
71  ROCM_COMMENT = LAST_COMMON+1,
72  ROCM_CONFIG_CTRL_DIRECTIVE,
73  ROCM_METADATA,
74  ROCM_GOT,
75 
76  EXTRA_FIRST = 0xfc,
77  EXTRA_PROGBITS = 0xfc,
78  EXTRA_NOBITS = 0xfd,
79  EXTRA_NOTE = 0xfe,
80  EXTRA_SECTION = 0xff
81 };
82 
83 enum: cxuint
84 {
85  ASMSECT_ABS = UINT_MAX,
86  ASMSECT_NONE = UINT_MAX,
87  ASMKERN_GLOBAL = UINT_MAX,
88  ASMKERN_INNER = UINT_MAX-1
89 };
90 
91 enum: Flags
92 {
93  ASMSECT_WRITEABLE = 1,
94  ASMSECT_ADDRESSABLE = 2,
95  ASMSECT_ABS_ADDRESSABLE = 4,
97 
98  ASMELFSECT_ALLOCATABLE = 0x10,
99  ASMELFSECT_WRITEABLE = 0x20,
100  ASMELFSECT_EXECUTABLE = 0x40
101 };
102 
103 class Assembler;
104 class AsmExpression;
105 struct AsmRelocation;
106 struct AsmSymbol;
107 
110 {
111 public:
113  AsmFormatException() = default;
115  explicit AsmFormatException(const std::string& message);
117  virtual ~AsmFormatException() noexcept = default;
118 };
119 
122 {
123 public:
125  struct SectionInfo
126  {
127  const char* name;
131 
132  SectionInfo() : name(nullptr), type(AsmSectionType::DATA), flags(0),
133  relSpace(UINT_MAX)
134  { }
135  SectionInfo(const char* _name, AsmSectionType _type, Flags _flags = 0,
136  cxuint _relSpace = UINT_MAX) : name(_name), type(_type), flags(_flags),
137  relSpace(_relSpace)
138  { }
139  };
140 protected:
142  bool sectionDiffsResolvable;
143 
145  explicit AsmFormatHandler(Assembler& assembler);
146 
147  // resolve LO32BIT/HI32BIT relocations (partially, helper)
148  bool resolveLoHiRelocExpression(const AsmExpression* expr, RelocType& relType,
149  cxuint& relSectionId, uint64_t& relValue);
150 public:
151  virtual ~AsmFormatHandler();
152 
155  { return sectionDiffsResolvable; }
156 
158 
164  virtual cxuint addKernel(const char* kernelName) = 0;
166 
173  virtual cxuint addSection(const char* sectionName, cxuint kernelId) = 0;
174 
176  virtual cxuint getSectionId(const char* sectionName) const = 0;
177 
179  virtual void setCurrentKernel(cxuint kernel) = 0;
181  virtual void setCurrentSection(cxuint sectionId) = 0;
182 
184  virtual SectionInfo getSectionInfo(cxuint sectionId) const = 0;
186  virtual bool parsePseudoOp(const CString& firstName,
187  const char* stmtPlace, const char* linePtr) = 0;
189  virtual void handleLabel(const CString& label);
191  virtual bool resolveSymbol(const AsmSymbol& symbol,
192  uint64_t& value, cxuint& sectionId);
194  virtual bool resolveRelocation(const AsmExpression* expr,
195  uint64_t& value, cxuint& sectionId);
197  virtual bool prepareBinary() = 0;
199  virtual void writeBinary(std::ostream& os) const = 0;
201  virtual void writeBinary(Array<cxbyte>& array) const = 0;
202 
204  virtual bool prepareSectionDiffsResolving();
205 };
206 
209 {
210 public:
212  explicit AsmRawCodeHandler(Assembler& assembler);
214  ~AsmRawCodeHandler() = default;
215 
216  cxuint addKernel(const char* kernelName);
217  cxuint addSection(const char* sectionName, cxuint kernelId);
218 
219  cxuint getSectionId(const char* sectionName) const;
220 
221  void setCurrentKernel(cxuint kernel);
222  void setCurrentSection(cxuint sectionId);
223 
224  SectionInfo getSectionInfo(cxuint sectionId) const;
225  bool parsePseudoOp(const CString& firstName,
226  const char* stmtPlace, const char* linePtr);
227 
228  bool prepareBinary();
229  void writeBinary(std::ostream& os) const;
230  void writeBinary(Array<cxbyte>& array) const;
231 };
232 
235 {
236 private:
237  typedef std::unordered_map<CString, cxuint> SectionMap;
238  friend struct AsmAmdPseudoOps;
239  AmdInput output;
240  struct Section
241  {
242  cxuint kernelId;
243  AsmSectionType type;
244  cxuint elfBinSectId;
245  const char* name;
246  uint32_t extraId; // for example CALNote id
247  };
248  struct Kernel
249  {
250  cxuint headerSection;
251  cxuint metadataSection;
252  cxuint configSection;
253  cxuint codeSection;
254  cxuint dataSection;
255  std::vector<cxuint> calNoteSections;
256  SectionMap extraSectionMap;
257  cxuint extraSectionCount;
258  cxuint savedSection;
259  std::unordered_set<CString> argNamesSet;
260  cxuint allocRegs[MAX_REGTYPES_NUM];
261  Flags allocRegFlags;
262  };
263  std::vector<Section> sections;
264  // use pointer to prevents copying Kernel objects
265  std::vector<Kernel*> kernelStates;
266  SectionMap extraSectionMap;
267  cxuint dataSection; // global
268  cxuint savedSection;
269  cxuint extraSectionCount;
270 
271  cxuint detectedDriverVersion;
272 
273  void saveCurrentSection();
274  void restoreCurrentAllocRegs();
275  void saveCurrentAllocRegs();
276 
277  cxuint determineDriverVersion() const;
278 public:
280  explicit AsmAmdHandler(Assembler& assembler);
282  ~AsmAmdHandler();
283 
284  cxuint addKernel(const char* kernelName);
285  cxuint addSection(const char* sectionName, cxuint kernelId);
286 
287  cxuint getSectionId(const char* sectionName) const;
288  void setCurrentKernel(cxuint kernel);
289  void setCurrentSection(cxuint sectionId);
290 
291  SectionInfo getSectionInfo(cxuint sectionId) const;
292  bool parsePseudoOp(const CString& firstName,
293  const char* stmtPlace, const char* linePtr);
294 
295  bool prepareBinary();
296  void writeBinary(std::ostream& os) const;
297  void writeBinary(Array<cxbyte>& array) const;
299  const AmdInput* getOutput() const
300  { return &output; }
301 };
302 
305 {
310  bool ieeeMode;
314  bool tgSize;
315  bool debugMode;
317  bool dx10Clamp;
318 
319  void initialize();
320 };
321 
324 {
325 private:
326  typedef std::unordered_map<CString, cxuint> SectionMap;
327  friend struct AsmAmdCL2PseudoOps;
328  AmdCL2Input output;
329  struct Section
330  {
331  cxuint kernelId;
332  AsmSectionType type;
333  cxuint elfBinSectId;
334  const char* name;
335  uint32_t extraId;
336  };
337  struct Relocation
338  {
339  RelocType type;
340  cxuint symbol; // 0,1,2
341  size_t addend;
342  };
343  /* relocmap: key - symbol, value - relocation */
344  typedef std::unordered_map<CString, Relocation> RelocMap;
345  struct Kernel
346  {
347  cxuint stubSection;
348  cxuint setupSection;
349  cxuint metadataSection;
350  cxuint isaMetadataSection;
351  cxuint configSection;
352  cxuint ctrlDirSection;
353  cxuint codeSection;
354  cxuint savedSection;
355  bool useHsaConfig; //
356  std::unique_ptr<AsmAmdHsaKernelConfig> hsaConfig; // hsaConfig
357  std::unordered_set<CString> argNamesSet;
358  cxuint allocRegs[MAX_REGTYPES_NUM];
359  Flags allocRegFlags;
360 
361  void initializeKernelConfig();
362  };
363  std::vector<Section> sections;
364  // use pointer to prevents copying Kernel objects
365  std::vector<Kernel*> kernelStates;
366  RelocMap relocsMap;
367  SectionMap extraSectionMap;
368  SectionMap innerExtraSectionMap;
369  cxuint rodataSection; // global inner
370  cxuint dataSection; // global inner
371  cxuint bssSection; // global inner
372  cxuint samplerInitSection;
373  cxuint savedSection;
374  cxuint innerSavedSection;
375  cxuint extraSectionCount;
376  cxuint innerExtraSectionCount;
377 
378  cxuint detectedDriverVersion;
379 
380  void saveCurrentSection();
381  void restoreCurrentAllocRegs();
382  void saveCurrentAllocRegs();
383  cxuint getDriverVersion() const;
384 public:
386  explicit AsmAmdCL2Handler(Assembler& assembler);
388  ~AsmAmdCL2Handler();
389 
390  cxuint addKernel(const char* kernelName);
391  cxuint addSection(const char* sectionName, cxuint kernelId);
392 
393  cxuint getSectionId(const char* sectionName) const;
394  void setCurrentKernel(cxuint kernel);
395  void setCurrentSection(cxuint sectionId);
396 
397  SectionInfo getSectionInfo(cxuint sectionId) const;
398  bool parsePseudoOp(const CString& firstName,
399  const char* stmtPlace, const char* linePtr);
400 
401  bool resolveSymbol(const AsmSymbol& symbol, uint64_t& value, cxuint& sectionId);
402  bool resolveRelocation(const AsmExpression* expr, uint64_t& value, cxuint& sectionId);
403  bool prepareBinary();
404  void writeBinary(std::ostream& os) const;
405  void writeBinary(Array<cxbyte>& array) const;
407  const AmdCL2Input* getOutput() const
408  { return &output; }
409 };
410 
413 {
414 private:
415  enum class Inside : cxbyte {
416  MAINLAYOUT, CONFIG, ARGS, PROGINFO
417  };
418 
419  typedef std::unordered_map<CString, cxuint> SectionMap;
420  friend struct AsmGalliumPseudoOps;
421  GalliumInput output;
422  struct Section
423  {
424  cxuint kernelId;
425  AsmSectionType type;
426  cxuint elfBinSectId;
427  const char* name; // must be available by whole lifecycle
428  };
429  struct Kernel
430  {
431  cxuint defaultSection;
432  std::unique_ptr<AsmAmdHsaKernelConfig> hsaConfig;
433  cxuint ctrlDirSection;
434  bool hasProgInfo;
435  cxbyte progInfoEntries;
436  cxuint allocRegs[MAX_REGTYPES_NUM];
437  Flags allocRegFlags;
438 
439  void initializeAmdHsaKernelConfig();
440  };
441  std::vector<Kernel*> kernelStates;
442  std::vector<Section> sections;
443  std::vector<cxuint> kcodeSelection; // kcode
444  std::stack<std::vector<cxuint> > kcodeSelStack;
445  cxuint currentKcodeKernel;
446  SectionMap extraSectionMap;
447  cxuint codeSection;
448  cxuint dataSection;
449  cxuint commentSection;
450  cxuint scratchSection;
451  cxuint savedSection;
452  Inside inside;
453  cxuint extraSectionCount;
454 
455  cxuint detectedDriverVersion;
456  cxuint detectedLLVMVersion;
457 
458  uint32_t archMinor;
459  uint32_t archStepping;
460 
461  void restoreKcodeCurrentAllocRegs();
462  void saveKcodeCurrentAllocRegs();
463 
464  cxuint determineDriverVersion() const;
465  cxuint determineLLVMVersion() const;
466 public:
468  explicit AsmGalliumHandler(Assembler& assembler);
471 
472  cxuint addKernel(const char* kernelName);
473  cxuint addSection(const char* sectionName, cxuint kernelId);
474 
475  cxuint getSectionId(const char* sectionName) const;
476  void setCurrentKernel(cxuint kernel);
477  void setCurrentSection(cxuint sectionId);
478 
479  SectionInfo getSectionInfo(cxuint sectionId) const;
480  bool parsePseudoOp(const CString& firstName,
481  const char* stmtPlace, const char* linePtr);
482  void handleLabel(const CString& label);
483 
484  bool resolveSymbol(const AsmSymbol& symbol, uint64_t& value, cxuint& sectionId);
485  bool resolveRelocation(const AsmExpression* expr, uint64_t& value, cxuint& sectionId);
486  bool prepareBinary();
487  void writeBinary(std::ostream& os) const;
488  void writeBinary(Array<cxbyte>& array) const;
490  const GalliumInput* getOutput() const
491  { return &output; }
492 };
493 
495 
498 {
499 private:
500  typedef std::unordered_map<CString, cxuint> SectionMap;
501  friend struct AsmROCmPseudoOps;
502  ROCmInput output;
503  std::unique_ptr<ROCmBinGenerator> binGen;
504  struct Section
505  {
506  cxuint kernelId;
507  AsmSectionType type;
508  cxuint elfBinSectId;
509  const char* name; // must be available by whole lifecycle
510  };
511  struct Kernel
512  {
513  cxuint configSection;
514  std::unique_ptr<AsmROCmKernelConfig> config;
515  bool isFKernel;
516  cxuint ctrlDirSection;
517  cxuint savedSection;
518  Flags allocRegFlags;
519  cxuint allocRegs[MAX_REGTYPES_NUM];
520 
521  void initializeKernelConfig();
522  };
523  std::vector<Kernel*> kernelStates;
524  std::vector<Section> sections;
525  std::vector<cxuint> kcodeSelection; // kcode
526  std::stack<std::vector<cxuint> > kcodeSelStack;
527  std::vector<CString> gotSymbols;
528  cxuint currentKcodeKernel;
529  SectionMap extraSectionMap;
530  cxuint codeSection;
531  cxuint commentSection;
532  cxuint metadataSection;
533  cxuint dataSection;
534  cxuint gotSection;
535  cxuint savedSection;
536  cxuint extraSectionCount;
537 
538  size_t prevSymbolsCount;
539 
540  bool unresolvedGlobals;
541  bool good;
542 
543  void restoreKcodeCurrentAllocRegs();
544  void saveKcodeCurrentAllocRegs();
545 
546  void addSymbols(bool sectionDiffsPrepared);
547 public:
549  explicit AsmROCmHandler(Assembler& assembler);
551  ~AsmROCmHandler();
552 
553  cxuint addKernel(const char* kernelName);
554  cxuint addSection(const char* sectionName, cxuint kernelId);
555 
556  cxuint getSectionId(const char* sectionName) const;
557  void setCurrentKernel(cxuint kernel);
558  void setCurrentSection(cxuint sectionId);
559 
560  SectionInfo getSectionInfo(cxuint sectionId) const;
561  bool parsePseudoOp(const CString& firstName,
562  const char* stmtPlace, const char* linePtr);
563  void handleLabel(const CString& label);
564 
565  bool prepareBinary();
566  void writeBinary(std::ostream& os) const;
567  void writeBinary(Array<cxbyte>& array) const;
569  const ROCmInput* getOutput() const
570  { return &output; }
571 
572  bool prepareSectionDiffsResolving();
573 };
574 
575 };
576 
577 #endif
code of program or kernel
common definitions for assembler and disassembler
main class of assembler
Definition: Assembler.h:404
Assembler & assembler
assembler reference
Definition: AsmFormats.h:141
non copyable and non movable base structure (class)
Definition: Utilities.h:43
assembler expression class
Definition: AsmDefs.h:301
uint32_t Flags
type for declaring various flags
Definition: Utilities.h:97
ROCm binary input structure.
Definition: ROCmBinaries.h:372
cxbyte userDataNum
number of user data
Definition: AsmFormats.h:309
Flags flags
section flags
Definition: AsmFormats.h:129
configuration (global or for kernel)
bool privilegedMode
prvileged mode
Definition: AsmFormats.h:316
AMD Catalyst kernel&#39;s metadata.
handles raw code format
Definition: AsmFormats.h:208
Gallium input.
Definition: GalliumBinaries.h:443
AsmSectionType type
section type
Definition: AsmFormats.h:128
bool debugMode
debug mode
Definition: AsmFormats.h:315
AMD HSA kernel configuration structure.
Definition: Commons.h:64
ROCm binaries handling.
const char * name
section name
Definition: AsmFormats.h:127
an array class
Definition: Containers.h:38
bool isSectionDiffsResolvable() const
return true if format handler can resolve differences between sections
Definition: AsmFormats.h:154
bool ieeeMode
IEEE mode.
Definition: AsmFormats.h:310
cxbyte floatMode
float mode
Definition: AsmFormats.h:311
Configuration header.
handles ROCM binary format
Definition: AsmFormats.h:497
cxuint RelocType
relocation type
Definition: Commons.h:33
bool tgSize
enable TG_SIZE_EN bit
Definition: AsmFormats.h:314
data object
Definition: ROCmBinaries.h:55
empty section for scratch symbol
absolute section id
Definition: AsmFormats.h:85
GalliumCompute binaries handling (only with LLVM 3.6)
assembler relocation
Definition: AsmDefs.h:280
const AmdCL2Input * getOutput() const
get output structure pointer
Definition: AsmFormats.h:407
AsmSectionType
assembler section type
Definition: AsmFormats.h:47
none section id
Definition: AsmFormats.h:86
AMD binaries handling.
assdembler format handler
Definition: AsmFormats.h:121
unsigned char cxbyte
unsigned byte
Definition: Config.h:215
AMD OpenCL2 binaries generator.
main namespace
Definition: AsmDefs.h:38
unsigned int cxuint
unsigned int
Definition: Config.h:223
const GalliumInput * getOutput() const
get output object (input for bingenerator)
Definition: AsmFormats.h:490
section information
Definition: AsmFormats.h:125
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:118
cxuint usedVGPRsNum
number of used VGPRs
Definition: AsmFormats.h:307
cxbyte priority
priority
Definition: AsmFormats.h:312
cxuint relSpace
relative space
Definition: AsmFormats.h:130
GPU identification utilities.
const AmdInput * getOutput() const
get output structure pointer
Definition: AsmFormats.h:299
main Input for AmdGPUBinGenerator
Definition: AmdBinGen.h:154
bool dx10Clamp
DX10 CLAMP mode.
Definition: AsmFormats.h:317
const ROCmInput * getOutput() const
get output object (input for bingenerator)
Definition: AsmFormats.h:569
cxuint dimMask
mask of dimension (bits: 0 - X, 1 - Y, 2 - Z)
Definition: AsmFormats.h:306
handles GalliumCompute format
Definition: AsmFormats.h:412
cxuint usedSGPRsNum
number of used SGPRs
Definition: AsmFormats.h:308
assembler symbol structure
Definition: AsmDefs.h:158
cxbyte exceptions
enabled exceptions
Definition: AsmFormats.h:313
handles AMD Catalyst format
Definition: AsmFormats.h:234
handles AMD OpenCL 2.0 binary format
Definition: AsmFormats.h:323
assembler format exception
Definition: AsmFormats.h:109
Asm AMD HSA kernel configuration.
Definition: AsmFormats.h:304
section is unresolvable
Definition: AsmFormats.h:96
no kernel, inner global space
Definition: AsmFormats.h:88
simple C-string container
Definition: CString.h:38
no kernel, global space
Definition: AsmFormats.h:87