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 "SIDefines.h"
16 #include "Utils/AMDGPUBaseInfo.h"
17 #include "Utils/AMDKernelCodeTUtils.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/MC/MCContext.h"
20 #include "llvm/MC/MCELFStreamer.h"
21 #include "llvm/MC/MCObjectFileInfo.h"
22 #include "llvm/MC/MCSectionELF.h"
23 #include "llvm/Support/ELF.h"
24 #include "llvm/Support/FormattedStream.h"
25 
26 using namespace llvm;
27 
28 AMDGPUTargetStreamer::AMDGPUTargetStreamer(MCStreamer &S)
29     : MCTargetStreamer(S) { }
30 
31 //===----------------------------------------------------------------------===//
32 // AMDGPUTargetAsmStreamer
33 //===----------------------------------------------------------------------===//
34 
35 AMDGPUTargetAsmStreamer::AMDGPUTargetAsmStreamer(MCStreamer &S,
36                                                  formatted_raw_ostream &OS)
37     : AMDGPUTargetStreamer(S), OS(OS) { }
38 
39 void
40 AMDGPUTargetAsmStreamer::EmitDirectiveHSACodeObjectVersion(uint32_t Major,
41                                                            uint32_t Minor) {
42   OS << "\t.hsa_code_object_version " <<
43         Twine(Major) << "," << Twine(Minor) << '\n';
44 }
45 
46 void
47 AMDGPUTargetAsmStreamer::EmitDirectiveHSACodeObjectISA(uint32_t Major,
48                                                        uint32_t Minor,
49                                                        uint32_t Stepping,
50                                                        StringRef VendorName,
51                                                        StringRef ArchName) {
52   OS << "\t.hsa_code_object_isa " <<
53         Twine(Major) << "," << Twine(Minor) << "," << Twine(Stepping) <<
54         ",\"" << VendorName << "\",\"" << ArchName << "\"\n";
55 
56 }
57 
58 void
59 AMDGPUTargetAsmStreamer::EmitAMDKernelCodeT(const amd_kernel_code_t &Header) {
60   OS << "\t.amd_kernel_code_t\n";
61   dumpAmdKernelCode(&Header, OS, "\t\t");
62   OS << "\t.end_amd_kernel_code_t\n";
63 }
64 
65 void AMDGPUTargetAsmStreamer::EmitAMDGPUSymbolType(StringRef SymbolName,
66                                                    unsigned Type) {
67   switch (Type) {
68     default: llvm_unreachable("Invalid AMDGPU symbol type");
69     case ELF::STT_AMDGPU_HSA_KERNEL:
70       OS << "\t.amdgpu_hsa_kernel " << SymbolName << '\n' ;
71       break;
72   }
73 }
74 
75 void AMDGPUTargetAsmStreamer::EmitAMDGPUHsaModuleScopeGlobal(
76     StringRef GlobalName) {
77   OS << "\t.amdgpu_hsa_module_global " << GlobalName << '\n';
78 }
79 
80 void AMDGPUTargetAsmStreamer::EmitAMDGPUHsaProgramScopeGlobal(
81     StringRef GlobalName) {
82   OS << "\t.amdgpu_hsa_program_global " << GlobalName << '\n';
83 }
84 
85 //===----------------------------------------------------------------------===//
86 // AMDGPUTargetELFStreamer
87 //===----------------------------------------------------------------------===//
88 
89 AMDGPUTargetELFStreamer::AMDGPUTargetELFStreamer(MCStreamer &S)
90     : AMDGPUTargetStreamer(S), Streamer(S) { }
91 
92 MCELFStreamer &AMDGPUTargetELFStreamer::getStreamer() {
93   return static_cast<MCELFStreamer &>(Streamer);
94 }
95 
96 void
97 AMDGPUTargetELFStreamer::EmitDirectiveHSACodeObjectVersion(uint32_t Major,
98                                                            uint32_t Minor) {
99   MCStreamer &OS = getStreamer();
100   MCSectionELF *Note = OS.getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
101 
102   unsigned NameSZ = 4;
103 
104   OS.PushSection();
105   OS.SwitchSection(Note);
106   OS.EmitIntValue(NameSZ, 4);                            // namesz
107   OS.EmitIntValue(8, 4);                                 // descz
108   OS.EmitIntValue(NT_AMDGPU_HSA_CODE_OBJECT_VERSION, 4); // type
109   OS.EmitBytes(StringRef("AMD", NameSZ));                // name
110   OS.EmitIntValue(Major, 4);                             // desc
111   OS.EmitIntValue(Minor, 4);
112   OS.EmitValueToAlignment(4);
113   OS.PopSection();
114 }
115 
116 void
117 AMDGPUTargetELFStreamer::EmitDirectiveHSACodeObjectISA(uint32_t Major,
118                                                        uint32_t Minor,
119                                                        uint32_t Stepping,
120                                                        StringRef VendorName,
121                                                        StringRef ArchName) {
122   MCStreamer &OS = getStreamer();
123   MCSectionELF *Note = OS.getContext().getELFSection(".note", ELF::SHT_NOTE, 0);
124 
125   unsigned NameSZ = 4;
126   uint16_t VendorNameSize = VendorName.size() + 1;
127   uint16_t ArchNameSize = ArchName.size() + 1;
128   unsigned DescSZ = sizeof(VendorNameSize) + sizeof(ArchNameSize) +
129                     sizeof(Major) + sizeof(Minor) + sizeof(Stepping) +
130                     VendorNameSize + ArchNameSize;
131 
132   OS.PushSection();
133   OS.SwitchSection(Note);
134   OS.EmitIntValue(NameSZ, 4);                            // namesz
135   OS.EmitIntValue(DescSZ, 4);                            // descsz
136   OS.EmitIntValue(NT_AMDGPU_HSA_ISA, 4);                 // type
137   OS.EmitBytes(StringRef("AMD", 4));                     // name
138   OS.EmitIntValue(VendorNameSize, 2);                    // desc
139   OS.EmitIntValue(ArchNameSize, 2);
140   OS.EmitIntValue(Major, 4);
141   OS.EmitIntValue(Minor, 4);
142   OS.EmitIntValue(Stepping, 4);
143   OS.EmitBytes(VendorName);
144   OS.EmitIntValue(0, 1); // NULL terminate VendorName
145   OS.EmitBytes(ArchName);
146   OS.EmitIntValue(0, 1); // NULL terminte ArchName
147   OS.EmitValueToAlignment(4);
148   OS.PopSection();
149 }
150 
151 void
152 AMDGPUTargetELFStreamer::EmitAMDKernelCodeT(const amd_kernel_code_t &Header) {
153 
154   MCStreamer &OS = getStreamer();
155   OS.PushSection();
156   OS.EmitBytes(StringRef((const char*)&Header, sizeof(Header)));
157   OS.PopSection();
158 }
159 
160 void AMDGPUTargetELFStreamer::EmitAMDGPUSymbolType(StringRef SymbolName,
161                                                    unsigned Type) {
162   MCSymbolELF *Symbol = cast<MCSymbolELF>(
163       getStreamer().getContext().getOrCreateSymbol(SymbolName));
164   Symbol->setType(ELF::STT_AMDGPU_HSA_KERNEL);
165 }
166 
167 void AMDGPUTargetELFStreamer::EmitAMDGPUHsaModuleScopeGlobal(
168     StringRef GlobalName) {
169 
170   MCSymbolELF *Symbol = cast<MCSymbolELF>(
171       getStreamer().getContext().getOrCreateSymbol(GlobalName));
172   Symbol->setType(ELF::STT_OBJECT);
173   Symbol->setBinding(ELF::STB_LOCAL);
174 }
175 
176 void AMDGPUTargetELFStreamer::EmitAMDGPUHsaProgramScopeGlobal(
177     StringRef GlobalName) {
178 
179   MCSymbolELF *Symbol = cast<MCSymbolELF>(
180       getStreamer().getContext().getOrCreateSymbol(GlobalName));
181   Symbol->setType(ELF::STT_OBJECT);
182   Symbol->setBinding(ELF::STB_GLOBAL);
183 }
184