LLVM  4.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 "AMDGPU.h"
15 #include "AMDGPUTargetStreamer.h"
16 #include "SIDefines.h"
17 #include "Utils/AMDGPUBaseInfo.h"
19 #include "llvm/ADT/Twine.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/Function.h"
22 #include "llvm/IR/Metadata.h"
23 #include "llvm/IR/Module.h"
24 #include "llvm/MC/MCContext.h"
25 #include "llvm/MC/MCELFStreamer.h"
27 #include "llvm/MC/MCSectionELF.h"
28 #include "llvm/Support/ELF.h"
30 #include "AMDGPURuntimeMD.h"
31 
32 namespace llvm {
33 #include "AMDGPUPTNote.h"
34 }
35 
36 using namespace llvm;
37 using namespace llvm::AMDGPU;
38 
40  : MCTargetStreamer(S) {}
41 
42 //===----------------------------------------------------------------------===//
43 // AMDGPUTargetAsmStreamer
44 //===----------------------------------------------------------------------===//
45 
48  : AMDGPUTargetStreamer(S), OS(OS) { }
49 
50 void
52  uint32_t Minor) {
53  OS << "\t.hsa_code_object_version " <<
54  Twine(Major) << "," << Twine(Minor) << '\n';
55 }
56 
57 void
59  uint32_t Minor,
60  uint32_t Stepping,
61  StringRef VendorName,
62  StringRef ArchName) {
63  OS << "\t.hsa_code_object_isa " <<
64  Twine(Major) << "," << Twine(Minor) << "," << Twine(Stepping) <<
65  ",\"" << VendorName << "\",\"" << ArchName << "\"\n";
66 
67 }
68 
69 void
71  OS << "\t.amd_kernel_code_t\n";
72  dumpAmdKernelCode(&Header, OS, "\t\t");
73  OS << "\t.end_amd_kernel_code_t\n";
74 }
75 
77  unsigned Type) {
78  switch (Type) {
79  default: llvm_unreachable("Invalid AMDGPU symbol type");
81  OS << "\t.amdgpu_hsa_kernel " << SymbolName << '\n' ;
82  break;
83  }
84 }
85 
87  StringRef GlobalName) {
88  OS << "\t.amdgpu_hsa_module_global " << GlobalName << '\n';
89 }
90 
92  StringRef GlobalName) {
93  OS << "\t.amdgpu_hsa_program_global " << GlobalName << '\n';
94 }
95 
97  OS << "\t.amdgpu_runtime_metadata\n";
98  OS << getRuntimeMDYAMLString(M);
99  OS << "\n\t.end_amdgpu_runtime_metadata\n";
100 }
101 
103  OS << "\t.amdgpu_runtime_metadata";
104  OS << Metadata;
105  OS << "\t.end_amdgpu_runtime_metadata\n";
106 }
107 
108 //===----------------------------------------------------------------------===//
109 // AMDGPUTargetELFStreamer
110 //===----------------------------------------------------------------------===//
111 
113  : AMDGPUTargetStreamer(S), Streamer(S) {}
114 
116  return static_cast<MCELFStreamer &>(Streamer);
117 }
118 
119 void
120 AMDGPUTargetELFStreamer::EmitAMDGPUNote(const MCExpr* DescSZ,
122  std::function<void(MCELFStreamer &)> EmitDesc) {
123  auto &S = getStreamer();
124  auto &Context = S.getContext();
125 
126  auto NameSZ = sizeof(PT_NOTE::NoteName);
127 
128  S.PushSection();
129  S.SwitchSection(Context.getELFSection(
131  S.EmitIntValue(NameSZ, 4); // namesz
132  S.EmitValue(DescSZ, 4); // descz
133  S.EmitIntValue(Type, 4); // type
134  S.EmitBytes(StringRef(PT_NOTE::NoteName, NameSZ)); // name
135  S.EmitValueToAlignment(4, 0, 1, 0); // padding 0
136  EmitDesc(S); // desc
137  S.EmitValueToAlignment(4, 0, 1, 0); // padding 0
138  S.PopSection();
139 }
140 
141 void
143  uint32_t Minor) {
144 
145  EmitAMDGPUNote(
148  [&](MCELFStreamer &OS){
149  OS.EmitIntValue(Major, 4);
150  OS.EmitIntValue(Minor, 4);
151  }
152  );
153 }
154 
155 void
157  uint32_t Minor,
158  uint32_t Stepping,
159  StringRef VendorName,
160  StringRef ArchName) {
161  uint16_t VendorNameSize = VendorName.size() + 1;
162  uint16_t ArchNameSize = ArchName.size() + 1;
163 
164  unsigned DescSZ = sizeof(VendorNameSize) + sizeof(ArchNameSize) +
165  sizeof(Major) + sizeof(Minor) + sizeof(Stepping) +
166  VendorNameSize + ArchNameSize;
167 
168  EmitAMDGPUNote(
171  [&](MCELFStreamer &OS) {
172  OS.EmitIntValue(VendorNameSize, 2);
173  OS.EmitIntValue(ArchNameSize, 2);
174  OS.EmitIntValue(Major, 4);
175  OS.EmitIntValue(Minor, 4);
176  OS.EmitIntValue(Stepping, 4);
177  OS.EmitBytes(VendorName);
178  OS.EmitIntValue(0, 1); // NULL terminate VendorName
179  OS.EmitBytes(ArchName);
180  OS.EmitIntValue(0, 1); // NULL terminte ArchName
181  }
182  );
183 }
184 
185 void
187 
188  MCStreamer &OS = getStreamer();
189  OS.PushSection();
190  OS.EmitBytes(StringRef((const char*)&Header, sizeof(Header)));
191  OS.PopSection();
192 }
193 
195  unsigned Type) {
196  MCSymbolELF *Symbol = cast<MCSymbolELF>(
197  getStreamer().getContext().getOrCreateSymbol(SymbolName));
199 }
200 
202  StringRef GlobalName) {
203 
204  MCSymbolELF *Symbol = cast<MCSymbolELF>(
205  getStreamer().getContext().getOrCreateSymbol(GlobalName));
206  Symbol->setType(ELF::STT_OBJECT);
207  Symbol->setBinding(ELF::STB_LOCAL);
208 }
209 
211  StringRef GlobalName) {
212 
213  MCSymbolELF *Symbol = cast<MCSymbolELF>(
214  getStreamer().getContext().getOrCreateSymbol(GlobalName));
215  Symbol->setType(ELF::STT_OBJECT);
216  Symbol->setBinding(ELF::STB_GLOBAL);
217 }
218 
220  // Create two labels to mark the beginning and end of the desc field
221  // and a MCExpr to calculate the size of the desc field.
222  auto &Context = getContext();
223  auto *DescBegin = Context.createTempSymbol();
224  auto *DescEnd = Context.createTempSymbol();
225  auto *DescSZ = MCBinaryExpr::createSub(
228 
229  EmitAMDGPUNote(
230  DescSZ,
232  [&](MCELFStreamer &OS) {
233  OS.EmitLabel(DescBegin);
234  OS.EmitBytes(Metadata);
235  OS.EmitLabel(DescEnd);
236  }
237  );
238 }
239 
242 }
void EmitBytes(StringRef Data) override
Emit the bytes in Data into the output.
LLVMContext & Context
static const MCSymbolRefExpr * create(const MCSymbol *Symbol, MCContext &Ctx)
Definition: MCExpr.h:298
void EmitRuntimeMetadata(Module &M) override
void EmitRuntimeMetadata(Module &M) override
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:52
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
void EmitAMDGPUHsaProgramScopeGlobal(StringRef GlobalName) override
virtual void EmitBytes(StringRef Data)
Emit the bytes in Data into the output.
Definition: MCStreamer.cpp:807
Target specific streamer interface.
Definition: MCStreamer.h:73
This file contains the declarations for metadata subclasses.
void PushSection()
Save the current and previous section on the section stack.
Definition: MCStreamer.h:324
const char NoteName[]
Definition: AMDGPUPTNote.h:26
void setType(unsigned Type) const
Definition: MCSymbolELF.cpp:94
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
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:34
void EmitLabel(MCSymbol *Symbol) override
Emit a label for Symbol into the current section.
void EmitAMDGPUHsaModuleScopeGlobal(StringRef GlobalName) override
MCContext & getContext() const
Definition: MCStreamer.h:221
static const MCBinaryExpr * createSub(const MCExpr *LHS, const MCExpr *RHS, MCContext &Ctx)
Definition: MCExpr.h:497
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:135
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:85
void EmitAMDGPUHsaProgramScopeGlobal(StringRef GlobalName) override
Streaming machine code generation interface.
Definition: MCStreamer.h:161
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
void EmitDirectiveHSACodeObjectVersion(uint32_t Major, uint32_t Minor) override
This file contains the declarations for the subclasses of Constant, which represent the different fla...
AMDGPUTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS)
void EmitAMDGPUHsaModuleScopeGlobal(StringRef GlobalName) override
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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
void EmitAMDGPUSymbolType(StringRef SymbolName, unsigned Type) override
MCSymbol * getOrCreateSymbol(const Twine &Name)
Lookup the symbol inside with the specified Name.
Definition: MCContext.cpp:114
std::string getRuntimeMDYAMLString(Module &M)
const char SectionName[]
Definition: AMDGPUPTNote.h:24
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:333
print Print MemDeps of function
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
Root of the metadata hierarchy.
Definition: Metadata.h:55
static const MCConstantExpr * create(int64_t Value, MCContext &Ctx)
Definition: MCExpr.cpp:149
void setBinding(unsigned Binding) const
Definition: MCSymbolELF.cpp:43