LLVM  5.0.0
AMDGPUTargetStreamer.cpp
Go to the documentation of this file.
1 //===-- AMDGPUTargetStreamer.cpp - Mips Target Streamer Methods -----------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file provides AMDGPU specific target streamer methods.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "AMDGPUTargetStreamer.h"
15 #include "AMDGPU.h"
16 #include "SIDefines.h"
17 #include "Utils/AMDGPUBaseInfo.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/BinaryFormat/ELF.h"
21 #include "llvm/IR/Constants.h"
22 #include "llvm/IR/Function.h"
23 #include "llvm/IR/Metadata.h"
24 #include "llvm/IR/Module.h"
25 #include "llvm/MC/MCContext.h"
26 #include "llvm/MC/MCELFStreamer.h"
28 #include "llvm/MC/MCSectionELF.h"
30 
31 namespace llvm {
32 #include "AMDGPUPTNote.h"
33 }
34 
35 using namespace llvm;
36 using namespace llvm::AMDGPU;
37 
38 //===----------------------------------------------------------------------===//
39 // AMDGPUTargetStreamer
40 //===----------------------------------------------------------------------===//
41 
43  : MCTargetStreamer(S) {}
44 
47 }
48 
50  const Function &Func, const amd_kernel_code_t &KernelCode) {
51  CodeObjectMetadataStreamer.emitKernel(Func, KernelCode);
52 }
53 
57 }
58 
59 //===----------------------------------------------------------------------===//
60 // AMDGPUTargetAsmStreamer
61 //===----------------------------------------------------------------------===//
62 
65  : AMDGPUTargetStreamer(S), OS(OS) { }
66 
67 void
69  uint32_t Minor) {
70  OS << "\t.hsa_code_object_version " <<
71  Twine(Major) << "," << Twine(Minor) << '\n';
72 }
73 
74 void
76  uint32_t Minor,
77  uint32_t Stepping,
78  StringRef VendorName,
79  StringRef ArchName) {
80  OS << "\t.hsa_code_object_isa " <<
81  Twine(Major) << "," << Twine(Minor) << "," << Twine(Stepping) <<
82  ",\"" << VendorName << "\",\"" << ArchName << "\"\n";
83 
84 }
85 
86 void
88  OS << "\t.amd_kernel_code_t\n";
89  dumpAmdKernelCode(&Header, OS, "\t\t");
90  OS << "\t.end_amd_kernel_code_t\n";
91 }
92 
94  unsigned Type) {
95  switch (Type) {
96  default: llvm_unreachable("Invalid AMDGPU symbol type");
98  OS << "\t.amdgpu_hsa_kernel " << SymbolName << '\n' ;
99  break;
100  }
101 }
102 
104  auto VerifiedYamlString = CodeObjectMetadataStreamer.toYamlString(YamlString);
105  if (!VerifiedYamlString)
106  return false;
107 
109  OS << VerifiedYamlString.get();
111 
112  return true;
113 }
114 
115 //===----------------------------------------------------------------------===//
116 // AMDGPUTargetELFStreamer
117 //===----------------------------------------------------------------------===//
118 
120  : AMDGPUTargetStreamer(S), Streamer(S) {}
121 
123  return static_cast<MCELFStreamer &>(Streamer);
124 }
125 
126 void AMDGPUTargetELFStreamer::EmitAMDGPUNote(
127  const MCExpr *DescSZ, ElfNote::NoteType Type,
128  function_ref<void(MCELFStreamer &)> EmitDesc) {
129  auto &S = getStreamer();
130  auto &Context = S.getContext();
131 
132  auto NameSZ = sizeof(ElfNote::NoteName);
133 
134  S.PushSection();
135  S.SwitchSection(Context.getELFSection(
137  S.EmitIntValue(NameSZ, 4); // namesz
138  S.EmitValue(DescSZ, 4); // descz
139  S.EmitIntValue(Type, 4); // type
140  S.EmitBytes(StringRef(ElfNote::NoteName, NameSZ)); // name
141  S.EmitValueToAlignment(4, 0, 1, 0); // padding 0
142  EmitDesc(S); // desc
143  S.EmitValueToAlignment(4, 0, 1, 0); // padding 0
144  S.PopSection();
145 }
146 
147 void
149  uint32_t Minor) {
150 
151  EmitAMDGPUNote(
154  [&](MCELFStreamer &OS){
155  OS.EmitIntValue(Major, 4);
156  OS.EmitIntValue(Minor, 4);
157  }
158  );
159 }
160 
161 void
163  uint32_t Minor,
164  uint32_t Stepping,
165  StringRef VendorName,
166  StringRef ArchName) {
167  uint16_t VendorNameSize = VendorName.size() + 1;
168  uint16_t ArchNameSize = ArchName.size() + 1;
169 
170  unsigned DescSZ = sizeof(VendorNameSize) + sizeof(ArchNameSize) +
171  sizeof(Major) + sizeof(Minor) + sizeof(Stepping) +
172  VendorNameSize + ArchNameSize;
173 
174  EmitAMDGPUNote(
177  [&](MCELFStreamer &OS) {
178  OS.EmitIntValue(VendorNameSize, 2);
179  OS.EmitIntValue(ArchNameSize, 2);
180  OS.EmitIntValue(Major, 4);
181  OS.EmitIntValue(Minor, 4);
182  OS.EmitIntValue(Stepping, 4);
183  OS.EmitBytes(VendorName);
184  OS.EmitIntValue(0, 1); // NULL terminate VendorName
185  OS.EmitBytes(ArchName);
186  OS.EmitIntValue(0, 1); // NULL terminte ArchName
187  }
188  );
189 }
190 
191 void
193 
194  MCStreamer &OS = getStreamer();
195  OS.PushSection();
196  OS.EmitBytes(StringRef((const char*)&Header, sizeof(Header)));
197  OS.PopSection();
198 }
199 
201  unsigned Type) {
202  MCSymbolELF *Symbol = cast<MCSymbolELF>(
203  getStreamer().getContext().getOrCreateSymbol(SymbolName));
205 }
206 
208  auto VerifiedYamlString = CodeObjectMetadataStreamer.toYamlString(YamlString);
209  if (!VerifiedYamlString)
210  return false;
211 
212  // Create two labels to mark the beginning and end of the desc field
213  // and a MCExpr to calculate the size of the desc field.
214  auto &Context = getContext();
215  auto *DescBegin = Context.createTempSymbol();
216  auto *DescEnd = Context.createTempSymbol();
217  auto *DescSZ = MCBinaryExpr::createSub(
220 
221  EmitAMDGPUNote(
222  DescSZ,
224  [&](MCELFStreamer &OS) {
225  OS.EmitLabel(DescBegin);
226  OS.EmitBytes(VerifiedYamlString.get());
227  OS.EmitLabel(DescEnd);
228  }
229  );
230 
231  return true;
232 }
void EmitBytes(StringRef Data) override
Emit the bytes in Data into the output.
LLVMContext & Context
virtual void EmitStartOfCodeObjectMetadata(const Module &Mod)
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:305
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
virtual void EmitBytes(StringRef Data)
Emit the bytes in Data into the output.
Definition: MCStreamer.cpp:832
Target specific streamer interface.
Definition: MCStreamer.h:78
This file contains the declarations for metadata subclasses.
An efficient, type-erasing, non-owning reference to a callable.
Definition: STLExtras.h:89
void PushSection()
Save the current and previous section on the section stack.
Definition: MCStreamer.h:330
void setType(unsigned Type) const
Definition: MCSymbolELF.cpp:96
void EmitDirectiveHSACodeObjectISA(uint32_t Major, uint32_t Minor, uint32_t Stepping, StringRef VendorName, StringRef ArchName) override
void EmitDirectiveHSACodeObjectISA(uint32_t Major, uint32_t Minor, uint32_t Stepping, StringRef VendorName, StringRef ArchName) override
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
AMDGPU::CodeObject::MetadataStreamer CodeObjectMetadataStreamer
AMD Kernel Code Object (amd_kernel_code_t).
MCContext & getContext() const
Base class for the full range of assembler expressions which are needed for parsing.
Definition: MCExpr.h:36
MCContext & getContext() const
Definition: MCStreamer.h:226
constexpr char MetadataAssemblerDirectiveBegin[]
Code object metadata beginning assembler directive.
virtual bool EmitCodeObjectMetadata(StringRef YamlString)=0
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:528
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
virtual void EmitIntValue(uint64_t Value, unsigned Size)
Special case of EmitValue that avoids the client having to pass in a MCExpr for constant integers...
Definition: MCStreamer.cpp:93
constexpr char MetadataAssemblerDirectiveEnd[]
Code object metadata ending assembler directive.
Streaming machine code generation interface.
Definition: MCStreamer.h:167
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:46
void EmitDirectiveHSACodeObjectVersion(uint32_t Major, uint32_t Minor) override
bool EmitCodeObjectMetadata(StringRef YamlString) override
This file contains the declarations for the subclasses of Constant, which represent the different fla...
bool EmitCodeObjectMetadata(StringRef YamlString) override
AMDGPUTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
void emitKernel(const Function &Func, const amd_kernel_code_t &KernelCode)
virtual void EmitKernelCodeObjectMetadata(const Function &Func, const amd_kernel_code_t &KernelCode)
const char NoteName[]
Definition: AMDGPUPTNote.h:26
void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) override
Enums and constants for AMDGPU PT_NOTE sections.
Module.h This file contains the declarations for the Module class.
void EmitAMDKernelCodeT(const amd_kernel_code_t &Header) override
void EmitDirectiveHSACodeObjectVersion(uint32_t Major, uint32_t Minor) override
llvm_unreachable("Unknown RegBank!")
void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) override
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:121
void EmitLabel(MCSymbol *Symbol, SMLoc Loc=SMLoc()) override
Emit a label for Symbol into the current section.
void EmitAMDKernelCodeT(const amd_kernel_code_t &Header) override
void dumpAmdKernelCode(const amd_kernel_code_t *C, raw_ostream &OS, const char *tab)
bool PopSection()
Restore the current and previous section from the section stack.
Definition: MCStreamer.h:339
const char SectionName[]
Definition: AMDGPUPTNote.h:24
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:159