blob: c9c95c796a6944f24abe77392f2aeebdd8fd14a9 [file] [log] [blame]
Tom Stellard45bb48e2015-06-13 03:28:10 +00001//===-- AMDGPUAsmPrinter.cpp - AMDGPU Assebly printer --------------------===//
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/// \file
11///
12/// The AMDGPUAsmPrinter is used to print both assembly string and also binary
13/// code. When passed an MCAsmStreamer it prints assembly and when passed
14/// an MCObjectStreamer it outputs binary code.
15//
16//===----------------------------------------------------------------------===//
17//
18
19#include "AMDGPUAsmPrinter.h"
Tom Stellard347ac792015-06-26 21:15:07 +000020#include "MCTargetDesc/AMDGPUTargetStreamer.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000021#include "InstPrinter/AMDGPUInstPrinter.h"
Tom Stellard347ac792015-06-26 21:15:07 +000022#include "Utils/AMDGPUBaseInfo.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000023#include "AMDGPU.h"
24#include "AMDKernelCodeT.h"
25#include "AMDGPUSubtarget.h"
26#include "R600Defines.h"
27#include "R600MachineFunctionInfo.h"
28#include "R600RegisterInfo.h"
29#include "SIDefines.h"
30#include "SIMachineFunctionInfo.h"
Matt Arsenaulta9720c62016-06-20 17:51:32 +000031#include "SIInstrInfo.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000032#include "SIRegisterInfo.h"
33#include "llvm/CodeGen/MachineFrameInfo.h"
Matt Arsenaultff982412016-06-20 18:13:04 +000034#include "llvm/IR/DiagnosticInfo.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000035#include "llvm/MC/MCContext.h"
36#include "llvm/MC/MCSectionELF.h"
37#include "llvm/MC/MCStreamer.h"
38#include "llvm/Support/ELF.h"
39#include "llvm/Support/MathExtras.h"
40#include "llvm/Support/TargetRegistry.h"
41#include "llvm/Target/TargetLoweringObjectFile.h"
Yaxun Liua711cc72016-07-16 05:09:21 +000042#include "AMDGPURuntimeMetadata.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000043
Yaxun Liua711cc72016-07-16 05:09:21 +000044using namespace ::AMDGPU;
Tom Stellard45bb48e2015-06-13 03:28:10 +000045using namespace llvm;
46
47// TODO: This should get the default rounding mode from the kernel. We just set
48// the default here, but this could change if the OpenCL rounding mode pragmas
49// are used.
50//
51// The denormal mode here should match what is reported by the OpenCL runtime
52// for the CL_FP_DENORM bit from CL_DEVICE_{HALF|SINGLE|DOUBLE}_FP_CONFIG, but
53// can also be override to flush with the -cl-denorms-are-zero compiler flag.
54//
55// AMD OpenCL only sets flush none and reports CL_FP_DENORM for double
56// precision, and leaves single precision to flush all and does not report
57// CL_FP_DENORM for CL_DEVICE_SINGLE_FP_CONFIG. Mesa's OpenCL currently reports
58// CL_FP_DENORM for both.
59//
60// FIXME: It seems some instructions do not support single precision denormals
61// regardless of the mode (exp_*_f32, rcp_*_f32, rsq_*_f32, rsq_*f32, sqrt_f32,
62// and sin_f32, cos_f32 on most parts).
63
64// We want to use these instructions, and using fp32 denormals also causes
65// instructions to run at the double precision rate for the device so it's
66// probably best to just report no single precision denormals.
67static uint32_t getFPMode(const MachineFunction &F) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +000068 const SISubtarget& ST = F.getSubtarget<SISubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +000069 // TODO: Is there any real use for the flush in only / flush out only modes?
70
71 uint32_t FP32Denormals =
72 ST.hasFP32Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
73
74 uint32_t FP64Denormals =
75 ST.hasFP64Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
76
77 return FP_ROUND_MODE_SP(FP_ROUND_ROUND_TO_NEAREST) |
78 FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_NEAREST) |
79 FP_DENORM_MODE_SP(FP32Denormals) |
80 FP_DENORM_MODE_DP(FP64Denormals);
81}
82
83static AsmPrinter *
84createAMDGPUAsmPrinterPass(TargetMachine &tm,
85 std::unique_ptr<MCStreamer> &&Streamer) {
86 return new AMDGPUAsmPrinter(tm, std::move(Streamer));
87}
88
89extern "C" void LLVMInitializeAMDGPUAsmPrinter() {
90 TargetRegistry::RegisterAsmPrinter(TheAMDGPUTarget, createAMDGPUAsmPrinterPass);
91 TargetRegistry::RegisterAsmPrinter(TheGCNTarget, createAMDGPUAsmPrinterPass);
92}
93
94AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM,
95 std::unique_ptr<MCStreamer> Streamer)
96 : AsmPrinter(TM, std::move(Streamer)) {}
97
Tom Stellardf4218372016-01-12 17:18:17 +000098void AMDGPUAsmPrinter::EmitStartOfAsmFile(Module &M) {
99 if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
100 return;
101
102 // Need to construct an MCSubtargetInfo here in case we have no functions
103 // in the module.
104 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
105 TM.getTargetTriple().str(), TM.getTargetCPU(),
106 TM.getTargetFeatureString()));
107
108 AMDGPUTargetStreamer *TS =
109 static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
110
Tom Stellard418beb72016-07-13 14:23:33 +0000111 TS->EmitDirectiveHSACodeObjectVersion(2, 1);
Tom Stellardfcfaea42016-05-05 17:03:33 +0000112
Tom Stellardf4218372016-01-12 17:18:17 +0000113 AMDGPU::IsaVersion ISA = AMDGPU::getIsaVersion(STI->getFeatureBits());
114 TS->EmitDirectiveHSACodeObjectISA(ISA.Major, ISA.Minor, ISA.Stepping,
115 "AMD", "AMDGPU");
Yaxun Liua711cc72016-07-16 05:09:21 +0000116 emitStartOfRuntimeMetadata(M);
Tom Stellardf4218372016-01-12 17:18:17 +0000117}
118
Tom Stellardf151a452015-06-26 21:14:58 +0000119void AMDGPUAsmPrinter::EmitFunctionBodyStart() {
120 const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
121 SIProgramInfo KernelInfo;
122 if (STM.isAmdHsaOS()) {
123 getSIProgramInfo(KernelInfo, *MF);
124 EmitAmdKernelCodeT(*MF, KernelInfo);
125 }
126}
127
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000128void AMDGPUAsmPrinter::EmitFunctionEntryLabel() {
129 const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
130 const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
131 if (MFI->isKernel() && STM.isAmdHsaOS()) {
132 AMDGPUTargetStreamer *TS =
133 static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
134 TS->EmitAMDGPUSymbolType(CurrentFnSym->getName(),
135 ELF::STT_AMDGPU_HSA_KERNEL);
136 }
137
138 AsmPrinter::EmitFunctionEntryLabel();
139}
140
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000141void AMDGPUAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
142
Tom Stellard00f2f912015-12-02 19:47:57 +0000143 // Group segment variables aren't emitted in HSA.
144 if (AMDGPU::isGroupSegment(GV))
145 return;
146
Tom Stellardfcfaea42016-05-05 17:03:33 +0000147 AsmPrinter::EmitGlobalVariable(GV);
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000148}
149
Tom Stellard45bb48e2015-06-13 03:28:10 +0000150bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
151
152 // The starting address of all shader programs must be 256 bytes aligned.
153 MF.setAlignment(8);
154
155 SetupMachineFunction(MF);
156
157 MCContext &Context = getObjFileLowering().getContext();
158 MCSectionELF *ConfigSection =
159 Context.getELFSection(".AMDGPU.config", ELF::SHT_PROGBITS, 0);
160 OutStreamer->SwitchSection(ConfigSection);
161
162 const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
163 SIProgramInfo KernelInfo;
Tom Stellardf151a452015-06-26 21:14:58 +0000164 if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault297ae312015-08-15 00:12:39 +0000165 getSIProgramInfo(KernelInfo, MF);
Tom Stellardf151a452015-06-26 21:14:58 +0000166 if (!STM.isAmdHsaOS()) {
Tom Stellardf151a452015-06-26 21:14:58 +0000167 EmitProgramInfoSI(MF, KernelInfo);
168 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000169 } else {
170 EmitProgramInfoR600(MF);
171 }
172
173 DisasmLines.clear();
174 HexLines.clear();
175 DisasmLineMaxLen = 0;
176
177 EmitFunctionBody();
178
179 if (isVerbose()) {
180 MCSectionELF *CommentSection =
181 Context.getELFSection(".AMDGPU.csdata", ELF::SHT_PROGBITS, 0);
182 OutStreamer->SwitchSection(CommentSection);
183
184 if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
185 OutStreamer->emitRawComment(" Kernel info:", false);
186 OutStreamer->emitRawComment(" codeLenInByte = " + Twine(KernelInfo.CodeLen),
187 false);
188 OutStreamer->emitRawComment(" NumSgprs: " + Twine(KernelInfo.NumSGPR),
189 false);
190 OutStreamer->emitRawComment(" NumVgprs: " + Twine(KernelInfo.NumVGPR),
191 false);
192 OutStreamer->emitRawComment(" FloatMode: " + Twine(KernelInfo.FloatMode),
193 false);
194 OutStreamer->emitRawComment(" IeeeMode: " + Twine(KernelInfo.IEEEMode),
195 false);
196 OutStreamer->emitRawComment(" ScratchSize: " + Twine(KernelInfo.ScratchSize),
197 false);
Matt Arsenaultfd8ab092016-04-14 22:11:51 +0000198 OutStreamer->emitRawComment(" LDSByteSize: " + Twine(KernelInfo.LDSSize) +
199 " bytes/workgroup (compile time only)", false);
Matt Arsenaultd41c0db2015-11-05 05:27:07 +0000200
Konstantin Zhuravlyov1d99c4d2016-04-26 15:43:14 +0000201 OutStreamer->emitRawComment(" ReservedVGPRFirst: " + Twine(KernelInfo.ReservedVGPRFirst),
202 false);
203 OutStreamer->emitRawComment(" ReservedVGPRCount: " + Twine(KernelInfo.ReservedVGPRCount),
204 false);
205
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000206 if (MF.getSubtarget<SISubtarget>().debuggerEmitPrologue()) {
207 OutStreamer->emitRawComment(" DebuggerWavefrontPrivateSegmentOffsetSGPR: s" +
208 Twine(KernelInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR), false);
209 OutStreamer->emitRawComment(" DebuggerPrivateSegmentBufferSGPR: s" +
210 Twine(KernelInfo.DebuggerPrivateSegmentBufferSGPR), false);
211 }
212
Matt Arsenaultd41c0db2015-11-05 05:27:07 +0000213 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:USER_SGPR: " +
Matt Arsenault8246d4a2015-11-11 00:27:46 +0000214 Twine(G_00B84C_USER_SGPR(KernelInfo.ComputePGMRSrc2)),
Matt Arsenaultd41c0db2015-11-05 05:27:07 +0000215 false);
Matt Arsenault8246d4a2015-11-11 00:27:46 +0000216 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_X_EN: " +
217 Twine(G_00B84C_TGID_X_EN(KernelInfo.ComputePGMRSrc2)),
218 false);
219 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_Y_EN: " +
220 Twine(G_00B84C_TGID_Y_EN(KernelInfo.ComputePGMRSrc2)),
221 false);
222 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_Z_EN: " +
223 Twine(G_00B84C_TGID_Z_EN(KernelInfo.ComputePGMRSrc2)),
224 false);
225 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: " +
226 Twine(G_00B84C_TIDIG_COMP_CNT(KernelInfo.ComputePGMRSrc2)),
227 false);
228
Tom Stellard45bb48e2015-06-13 03:28:10 +0000229 } else {
230 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
231 OutStreamer->emitRawComment(
232 Twine("SQ_PGM_RESOURCES:STACK_SIZE = " + Twine(MFI->StackSize)));
233 }
234 }
235
236 if (STM.dumpCode()) {
237
238 OutStreamer->SwitchSection(
239 Context.getELFSection(".AMDGPU.disasm", ELF::SHT_NOTE, 0));
240
241 for (size_t i = 0; i < DisasmLines.size(); ++i) {
242 std::string Comment(DisasmLineMaxLen - DisasmLines[i].size(), ' ');
243 Comment += " ; " + HexLines[i] + "\n";
244
245 OutStreamer->EmitBytes(StringRef(DisasmLines[i]));
246 OutStreamer->EmitBytes(StringRef(Comment));
247 }
248 }
249
Yaxun Liua711cc72016-07-16 05:09:21 +0000250 emitRuntimeMetadata(*MF.getFunction());
251
Tom Stellard45bb48e2015-06-13 03:28:10 +0000252 return false;
253}
254
255void AMDGPUAsmPrinter::EmitProgramInfoR600(const MachineFunction &MF) {
256 unsigned MaxGPR = 0;
257 bool killPixel = false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000258 const R600Subtarget &STM = MF.getSubtarget<R600Subtarget>();
259 const R600RegisterInfo *RI = STM.getRegisterInfo();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000260 const R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
261
262 for (const MachineBasicBlock &MBB : MF) {
263 for (const MachineInstr &MI : MBB) {
264 if (MI.getOpcode() == AMDGPU::KILLGT)
265 killPixel = true;
266 unsigned numOperands = MI.getNumOperands();
267 for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
268 const MachineOperand &MO = MI.getOperand(op_idx);
269 if (!MO.isReg())
270 continue;
271 unsigned HWReg = RI->getEncodingValue(MO.getReg()) & 0xff;
272
273 // Register with value > 127 aren't GPR
274 if (HWReg > 127)
275 continue;
276 MaxGPR = std::max(MaxGPR, HWReg);
277 }
278 }
279 }
280
281 unsigned RsrcReg;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000282 if (STM.getGeneration() >= R600Subtarget::EVERGREEN) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000283 // Evergreen / Northern Islands
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000284 switch (MF.getFunction()->getCallingConv()) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000285 default: // Fall through
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000286 case CallingConv::AMDGPU_CS: RsrcReg = R_0288D4_SQ_PGM_RESOURCES_LS; break;
287 case CallingConv::AMDGPU_GS: RsrcReg = R_028878_SQ_PGM_RESOURCES_GS; break;
288 case CallingConv::AMDGPU_PS: RsrcReg = R_028844_SQ_PGM_RESOURCES_PS; break;
289 case CallingConv::AMDGPU_VS: RsrcReg = R_028860_SQ_PGM_RESOURCES_VS; break;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000290 }
291 } else {
292 // R600 / R700
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000293 switch (MF.getFunction()->getCallingConv()) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000294 default: // Fall through
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000295 case CallingConv::AMDGPU_GS: // Fall through
296 case CallingConv::AMDGPU_CS: // Fall through
297 case CallingConv::AMDGPU_VS: RsrcReg = R_028868_SQ_PGM_RESOURCES_VS; break;
298 case CallingConv::AMDGPU_PS: RsrcReg = R_028850_SQ_PGM_RESOURCES_PS; break;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000299 }
300 }
301
302 OutStreamer->EmitIntValue(RsrcReg, 4);
303 OutStreamer->EmitIntValue(S_NUM_GPRS(MaxGPR + 1) |
304 S_STACK_SIZE(MFI->StackSize), 4);
305 OutStreamer->EmitIntValue(R_02880C_DB_SHADER_CONTROL, 4);
306 OutStreamer->EmitIntValue(S_02880C_KILL_ENABLE(killPixel), 4);
307
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000308 if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000309 OutStreamer->EmitIntValue(R_0288E8_SQ_LDS_ALLOC, 4);
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000310 OutStreamer->EmitIntValue(alignTo(MFI->LDSSize, 4) >> 2, 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000311 }
312}
313
314void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo,
315 const MachineFunction &MF) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000316 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000317 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
318 uint64_t CodeSize = 0;
319 unsigned MaxSGPR = 0;
320 unsigned MaxVGPR = 0;
321 bool VCCUsed = false;
322 bool FlatUsed = false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000323 const SIRegisterInfo *RI = STM.getRegisterInfo();
324 const SIInstrInfo *TII = STM.getInstrInfo();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000325
326 for (const MachineBasicBlock &MBB : MF) {
327 for (const MachineInstr &MI : MBB) {
328 // TODO: CodeSize should account for multiple functions.
Matt Arsenaultc5746862015-08-12 09:04:44 +0000329
330 // TODO: Should we count size of debug info?
331 if (MI.isDebugValue())
332 continue;
333
Matt Arsenaulta9720c62016-06-20 17:51:32 +0000334 CodeSize += TII->getInstSizeInBytes(MI);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000335
336 unsigned numOperands = MI.getNumOperands();
337 for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
338 const MachineOperand &MO = MI.getOperand(op_idx);
339 unsigned width = 0;
340 bool isSGPR = false;
341
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000342 if (!MO.isReg())
Tom Stellard45bb48e2015-06-13 03:28:10 +0000343 continue;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000344
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000345 unsigned reg = MO.getReg();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000346 switch (reg) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000347 case AMDGPU::EXEC:
Nicolai Haehnle74839372016-04-19 21:58:17 +0000348 case AMDGPU::EXEC_LO:
349 case AMDGPU::EXEC_HI:
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000350 case AMDGPU::SCC:
Tom Stellard45bb48e2015-06-13 03:28:10 +0000351 case AMDGPU::M0:
352 continue;
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000353
354 case AMDGPU::VCC:
355 case AMDGPU::VCC_LO:
356 case AMDGPU::VCC_HI:
357 VCCUsed = true;
358 continue;
359
360 case AMDGPU::FLAT_SCR:
361 case AMDGPU::FLAT_SCR_LO:
362 case AMDGPU::FLAT_SCR_HI:
363 FlatUsed = true;
364 continue;
365
Artem Tamazoveb4d5a92016-04-13 16:18:41 +0000366 case AMDGPU::TBA:
367 case AMDGPU::TBA_LO:
368 case AMDGPU::TBA_HI:
369 case AMDGPU::TMA:
370 case AMDGPU::TMA_LO:
371 case AMDGPU::TMA_HI:
372 llvm_unreachable("Trap Handler registers should not be used");
373 continue;
374
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000375 default:
376 break;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000377 }
378
379 if (AMDGPU::SReg_32RegClass.contains(reg)) {
Artem Tamazoveb4d5a92016-04-13 16:18:41 +0000380 if (AMDGPU::TTMP_32RegClass.contains(reg)) {
381 llvm_unreachable("Trap Handler registers should not be used");
382 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000383 isSGPR = true;
384 width = 1;
385 } else if (AMDGPU::VGPR_32RegClass.contains(reg)) {
386 isSGPR = false;
387 width = 1;
388 } else if (AMDGPU::SReg_64RegClass.contains(reg)) {
Artem Tamazoveb4d5a92016-04-13 16:18:41 +0000389 if (AMDGPU::TTMP_64RegClass.contains(reg)) {
390 llvm_unreachable("Trap Handler registers should not be used");
391 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000392 isSGPR = true;
393 width = 2;
394 } else if (AMDGPU::VReg_64RegClass.contains(reg)) {
395 isSGPR = false;
396 width = 2;
397 } else if (AMDGPU::VReg_96RegClass.contains(reg)) {
398 isSGPR = false;
399 width = 3;
400 } else if (AMDGPU::SReg_128RegClass.contains(reg)) {
401 isSGPR = true;
402 width = 4;
403 } else if (AMDGPU::VReg_128RegClass.contains(reg)) {
404 isSGPR = false;
405 width = 4;
406 } else if (AMDGPU::SReg_256RegClass.contains(reg)) {
407 isSGPR = true;
408 width = 8;
409 } else if (AMDGPU::VReg_256RegClass.contains(reg)) {
410 isSGPR = false;
411 width = 8;
412 } else if (AMDGPU::SReg_512RegClass.contains(reg)) {
413 isSGPR = true;
414 width = 16;
415 } else if (AMDGPU::VReg_512RegClass.contains(reg)) {
416 isSGPR = false;
417 width = 16;
418 } else {
419 llvm_unreachable("Unknown register class");
420 }
421 unsigned hwReg = RI->getEncodingValue(reg) & 0xff;
422 unsigned maxUsed = hwReg + width - 1;
423 if (isSGPR) {
424 MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR;
425 } else {
426 MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR;
427 }
428 }
429 }
430 }
431
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000432 unsigned ExtraSGPRs = 0;
433
434 if (VCCUsed)
435 ExtraSGPRs = 2;
436
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000437 if (STM.getGeneration() < SISubtarget::VOLCANIC_ISLANDS) {
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000438 if (FlatUsed)
439 ExtraSGPRs = 4;
440 } else {
441 if (STM.isXNACKEnabled())
442 ExtraSGPRs = 4;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000443
Nicolai Haehnle5b504972016-01-04 23:35:53 +0000444 if (FlatUsed)
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000445 ExtraSGPRs = 6;
Tom Stellardcaaa3aa2015-12-17 17:05:09 +0000446 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000447
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000448 MaxSGPR += ExtraSGPRs;
449
Konstantin Zhuravlyov29ddd2b2016-05-24 18:37:18 +0000450 // Record first reserved register and reserved register count fields, and
451 // update max register counts if "amdgpu-debugger-reserve-regs" attribute was
452 // specified.
453 if (STM.debuggerReserveRegs()) {
Konstantin Zhuravlyov1d99c4d2016-04-26 15:43:14 +0000454 ProgInfo.ReservedVGPRFirst = MaxVGPR + 1;
Konstantin Zhuravlyov29ddd2b2016-05-24 18:37:18 +0000455 ProgInfo.ReservedVGPRCount = MFI->getDebuggerReservedVGPRCount();
456 MaxVGPR += MFI->getDebuggerReservedVGPRCount();
Konstantin Zhuravlyov1d99c4d2016-04-26 15:43:14 +0000457 }
458
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000459 // Update DebuggerWavefrontPrivateSegmentOffsetSGPR and
460 // DebuggerPrivateSegmentBufferSGPR fields if "amdgpu-debugger-emit-prologue"
461 // attribute was specified.
462 if (STM.debuggerEmitPrologue()) {
463 ProgInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR =
464 RI->getHWRegIndex(MFI->getScratchWaveOffsetReg());
465 ProgInfo.DebuggerPrivateSegmentBufferSGPR =
466 RI->getHWRegIndex(MFI->getScratchRSrcReg());
467 }
468
Tom Stellard45bb48e2015-06-13 03:28:10 +0000469 // We found the maximum register index. They start at 0, so add one to get the
470 // number of registers.
471 ProgInfo.NumVGPR = MaxVGPR + 1;
472 ProgInfo.NumSGPR = MaxSGPR + 1;
473
474 if (STM.hasSGPRInitBug()) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000475 if (ProgInfo.NumSGPR > SISubtarget::FIXED_SGPR_COUNT_FOR_INIT_BUG) {
Matt Arsenault417c93e2015-06-17 20:55:25 +0000476 LLVMContext &Ctx = MF.getFunction()->getContext();
Matt Arsenaultff982412016-06-20 18:13:04 +0000477 DiagnosticInfoResourceLimit Diag(*MF.getFunction(),
478 "SGPRs with SGPR init bug",
479 ProgInfo.NumSGPR, DS_Error);
480 Ctx.diagnose(Diag);
Matt Arsenault417c93e2015-06-17 20:55:25 +0000481 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000482
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000483 ProgInfo.NumSGPR = SISubtarget::FIXED_SGPR_COUNT_FOR_INIT_BUG;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000484 }
485
Matt Arsenault41003af2015-11-30 21:16:07 +0000486 if (MFI->NumUserSGPRs > STM.getMaxNumUserSGPRs()) {
487 LLVMContext &Ctx = MF.getFunction()->getContext();
Matt Arsenaultff982412016-06-20 18:13:04 +0000488 DiagnosticInfoResourceLimit Diag(*MF.getFunction(), "user SGPRs",
489 MFI->NumUserSGPRs, DS_Error);
490 Ctx.diagnose(Diag);
Matt Arsenault41003af2015-11-30 21:16:07 +0000491 }
492
Matt Arsenault1c4d0ef2016-04-28 19:37:35 +0000493 if (MFI->LDSSize > static_cast<unsigned>(STM.getLocalMemorySize())) {
494 LLVMContext &Ctx = MF.getFunction()->getContext();
Matt Arsenaultff982412016-06-20 18:13:04 +0000495 DiagnosticInfoResourceLimit Diag(*MF.getFunction(), "local memory",
496 MFI->LDSSize, DS_Error);
497 Ctx.diagnose(Diag);
Matt Arsenault1c4d0ef2016-04-28 19:37:35 +0000498 }
499
Tom Stellard45bb48e2015-06-13 03:28:10 +0000500 ProgInfo.VGPRBlocks = (ProgInfo.NumVGPR - 1) / 4;
501 ProgInfo.SGPRBlocks = (ProgInfo.NumSGPR - 1) / 8;
502 // Set the value to initialize FP_ROUND and FP_DENORM parts of the mode
503 // register.
504 ProgInfo.FloatMode = getFPMode(MF);
505
Tom Stellard45bb48e2015-06-13 03:28:10 +0000506 ProgInfo.IEEEMode = 0;
507
Matt Arsenault7293f982016-01-28 20:53:35 +0000508 // Make clamp modifier on NaN input returns 0.
509 ProgInfo.DX10Clamp = 1;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000510
511 const MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Matt Arsenaultf4dfc1a2016-03-01 04:58:20 +0000512 ProgInfo.ScratchSize = FrameInfo->getStackSize();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000513
514 ProgInfo.FlatUsed = FlatUsed;
515 ProgInfo.VCCUsed = VCCUsed;
516 ProgInfo.CodeLen = CodeSize;
517
518 unsigned LDSAlignShift;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000519 if (STM.getGeneration() < SISubtarget::SEA_ISLANDS) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000520 // LDS is allocated in 64 dword blocks.
521 LDSAlignShift = 8;
522 } else {
523 // LDS is allocated in 128 dword blocks.
524 LDSAlignShift = 9;
525 }
526
527 unsigned LDSSpillSize = MFI->LDSWaveSpillSize *
528 MFI->getMaximumWorkGroupSize(MF);
529
530 ProgInfo.LDSSize = MFI->LDSSize + LDSSpillSize;
531 ProgInfo.LDSBlocks =
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +0000532 alignTo(ProgInfo.LDSSize, 1ULL << LDSAlignShift) >> LDSAlignShift;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000533
534 // Scratch is allocated in 256 dword blocks.
535 unsigned ScratchAlignShift = 10;
536 // We need to program the hardware with the amount of scratch memory that
537 // is used by the entire wave. ProgInfo.ScratchSize is the amount of
538 // scratch memory used per thread.
539 ProgInfo.ScratchBlocks =
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000540 alignTo(ProgInfo.ScratchSize * STM.getWavefrontSize(),
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +0000541 1ULL << ScratchAlignShift) >>
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000542 ScratchAlignShift;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000543
544 ProgInfo.ComputePGMRSrc1 =
545 S_00B848_VGPRS(ProgInfo.VGPRBlocks) |
546 S_00B848_SGPRS(ProgInfo.SGPRBlocks) |
547 S_00B848_PRIORITY(ProgInfo.Priority) |
548 S_00B848_FLOAT_MODE(ProgInfo.FloatMode) |
549 S_00B848_PRIV(ProgInfo.Priv) |
550 S_00B848_DX10_CLAMP(ProgInfo.DX10Clamp) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000551 S_00B848_DEBUG_MODE(ProgInfo.DebugMode) |
Tom Stellard45bb48e2015-06-13 03:28:10 +0000552 S_00B848_IEEE_MODE(ProgInfo.IEEEMode);
553
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000554 // 0 = X, 1 = XY, 2 = XYZ
555 unsigned TIDIGCompCnt = 0;
556 if (MFI->hasWorkItemIDZ())
557 TIDIGCompCnt = 2;
558 else if (MFI->hasWorkItemIDY())
559 TIDIGCompCnt = 1;
560
Tom Stellard45bb48e2015-06-13 03:28:10 +0000561 ProgInfo.ComputePGMRSrc2 =
562 S_00B84C_SCRATCH_EN(ProgInfo.ScratchBlocks > 0) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000563 S_00B84C_USER_SGPR(MFI->getNumUserSGPRs()) |
564 S_00B84C_TGID_X_EN(MFI->hasWorkGroupIDX()) |
565 S_00B84C_TGID_Y_EN(MFI->hasWorkGroupIDY()) |
566 S_00B84C_TGID_Z_EN(MFI->hasWorkGroupIDZ()) |
567 S_00B84C_TG_SIZE_EN(MFI->hasWorkGroupInfo()) |
568 S_00B84C_TIDIG_COMP_CNT(TIDIGCompCnt) |
569 S_00B84C_EXCP_EN_MSB(0) |
570 S_00B84C_LDS_SIZE(ProgInfo.LDSBlocks) |
571 S_00B84C_EXCP_EN(0);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000572}
573
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000574static unsigned getRsrcReg(CallingConv::ID CallConv) {
575 switch (CallConv) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000576 default: // Fall through
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000577 case CallingConv::AMDGPU_CS: return R_00B848_COMPUTE_PGM_RSRC1;
578 case CallingConv::AMDGPU_GS: return R_00B228_SPI_SHADER_PGM_RSRC1_GS;
579 case CallingConv::AMDGPU_PS: return R_00B028_SPI_SHADER_PGM_RSRC1_PS;
580 case CallingConv::AMDGPU_VS: return R_00B128_SPI_SHADER_PGM_RSRC1_VS;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000581 }
582}
583
584void AMDGPUAsmPrinter::EmitProgramInfoSI(const MachineFunction &MF,
585 const SIProgramInfo &KernelInfo) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000586 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000587 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000588 unsigned RsrcReg = getRsrcReg(MF.getFunction()->getCallingConv());
Tom Stellard45bb48e2015-06-13 03:28:10 +0000589
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000590 if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000591 OutStreamer->EmitIntValue(R_00B848_COMPUTE_PGM_RSRC1, 4);
592
593 OutStreamer->EmitIntValue(KernelInfo.ComputePGMRSrc1, 4);
594
595 OutStreamer->EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4);
596 OutStreamer->EmitIntValue(KernelInfo.ComputePGMRSrc2, 4);
597
598 OutStreamer->EmitIntValue(R_00B860_COMPUTE_TMPRING_SIZE, 4);
599 OutStreamer->EmitIntValue(S_00B860_WAVESIZE(KernelInfo.ScratchBlocks), 4);
600
601 // TODO: Should probably note flat usage somewhere. SC emits a "FlatPtr32 =
602 // 0" comment but I don't see a corresponding field in the register spec.
603 } else {
604 OutStreamer->EmitIntValue(RsrcReg, 4);
605 OutStreamer->EmitIntValue(S_00B028_VGPRS(KernelInfo.VGPRBlocks) |
606 S_00B028_SGPRS(KernelInfo.SGPRBlocks), 4);
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000607 if (STM.isVGPRSpillingEnabled(*MF.getFunction())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000608 OutStreamer->EmitIntValue(R_0286E8_SPI_TMPRING_SIZE, 4);
609 OutStreamer->EmitIntValue(S_0286E8_WAVESIZE(KernelInfo.ScratchBlocks), 4);
610 }
611 }
612
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000613 if (MF.getFunction()->getCallingConv() == CallingConv::AMDGPU_PS) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000614 OutStreamer->EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4);
615 OutStreamer->EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(KernelInfo.LDSBlocks), 4);
616 OutStreamer->EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000617 OutStreamer->EmitIntValue(MFI->PSInputEna, 4);
618 OutStreamer->EmitIntValue(R_0286D0_SPI_PS_INPUT_ADDR, 4);
619 OutStreamer->EmitIntValue(MFI->getPSInputAddr(), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000620 }
Marek Olsak0532c192016-07-13 17:35:15 +0000621
622 OutStreamer->EmitIntValue(R_SPILLED_SGPRS, 4);
623 OutStreamer->EmitIntValue(MFI->getNumSpilledSGPRs(), 4);
624 OutStreamer->EmitIntValue(R_SPILLED_VGPRS, 4);
625 OutStreamer->EmitIntValue(MFI->getNumSpilledVGPRs(), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000626}
627
Matt Arsenault24ee0782016-02-12 02:40:47 +0000628// This is supposed to be log2(Size)
629static amd_element_byte_size_t getElementByteSizeValue(unsigned Size) {
630 switch (Size) {
631 case 4:
632 return AMD_ELEMENT_4_BYTES;
633 case 8:
634 return AMD_ELEMENT_8_BYTES;
635 case 16:
636 return AMD_ELEMENT_16_BYTES;
637 default:
638 llvm_unreachable("invalid private_element_size");
639 }
640}
641
Tom Stellard45bb48e2015-06-13 03:28:10 +0000642void AMDGPUAsmPrinter::EmitAmdKernelCodeT(const MachineFunction &MF,
Tom Stellardff7416b2015-06-26 21:58:31 +0000643 const SIProgramInfo &KernelInfo) const {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000644 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000645 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000646 amd_kernel_code_t header;
647
Tom Stellardff7416b2015-06-26 21:58:31 +0000648 AMDGPU::initDefaultAMDKernelCodeT(header, STM.getFeatureBits());
Tom Stellard45bb48e2015-06-13 03:28:10 +0000649
650 header.compute_pgm_resource_registers =
651 KernelInfo.ComputePGMRSrc1 |
652 (KernelInfo.ComputePGMRSrc2 << 32);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000653 header.code_properties = AMD_CODE_PROPERTY_IS_PTR64;
654
Matt Arsenault24ee0782016-02-12 02:40:47 +0000655
656 AMD_HSA_BITS_SET(header.code_properties,
657 AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE,
658 getElementByteSizeValue(STM.getMaxPrivateElementSize()));
659
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000660 if (MFI->hasPrivateSegmentBuffer()) {
661 header.code_properties |=
662 AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER;
663 }
664
665 if (MFI->hasDispatchPtr())
666 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
667
668 if (MFI->hasQueuePtr())
669 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR;
670
671 if (MFI->hasKernargSegmentPtr())
672 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR;
673
674 if (MFI->hasDispatchID())
675 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID;
676
677 if (MFI->hasFlatScratchInit())
678 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT;
679
680 // TODO: Private segment size
681
682 if (MFI->hasGridWorkgroupCountX()) {
683 header.code_properties |=
684 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X;
685 }
686
687 if (MFI->hasGridWorkgroupCountY()) {
688 header.code_properties |=
689 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y;
690 }
691
692 if (MFI->hasGridWorkgroupCountZ()) {
693 header.code_properties |=
694 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z;
695 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000696
Tom Stellard48f29f22015-11-26 00:43:29 +0000697 if (MFI->hasDispatchPtr())
698 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
699
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000700 if (STM.debuggerSupported())
701 header.code_properties |= AMD_CODE_PROPERTY_IS_DEBUG_SUPPORTED;
702
Nicolai Haehnle5b504972016-01-04 23:35:53 +0000703 if (STM.isXNACKEnabled())
704 header.code_properties |= AMD_CODE_PROPERTY_IS_XNACK_SUPPORTED;
705
Tom Stellard45bb48e2015-06-13 03:28:10 +0000706 header.kernarg_segment_byte_size = MFI->ABIArgOffset;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000707 header.wavefront_sgpr_count = KernelInfo.NumSGPR;
708 header.workitem_vgpr_count = KernelInfo.NumVGPR;
Tom Stellarda4953072015-12-15 22:55:30 +0000709 header.workitem_private_segment_byte_size = KernelInfo.ScratchSize;
Tom Stellard7750f4e2015-12-15 23:15:25 +0000710 header.workgroup_group_segment_byte_size = KernelInfo.LDSSize;
Konstantin Zhuravlyov1d99c4d2016-04-26 15:43:14 +0000711 header.reserved_vgpr_first = KernelInfo.ReservedVGPRFirst;
712 header.reserved_vgpr_count = KernelInfo.ReservedVGPRCount;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000713
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000714 if (STM.debuggerEmitPrologue()) {
715 header.debug_wavefront_private_segment_offset_sgpr =
716 KernelInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR;
717 header.debug_private_segment_buffer_sgpr =
718 KernelInfo.DebuggerPrivateSegmentBufferSGPR;
719 }
720
Tom Stellardff7416b2015-06-26 21:58:31 +0000721 AMDGPUTargetStreamer *TS =
722 static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
Tom Stellardfcfaea42016-05-05 17:03:33 +0000723
724 OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
Tom Stellardff7416b2015-06-26 21:58:31 +0000725 TS->EmitAMDKernelCodeT(header);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000726}
727
728bool AMDGPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
729 unsigned AsmVariant,
730 const char *ExtraCode, raw_ostream &O) {
731 if (ExtraCode && ExtraCode[0]) {
732 if (ExtraCode[1] != 0)
733 return true; // Unknown modifier.
734
735 switch (ExtraCode[0]) {
736 default:
737 // See if this is a generic print operand
738 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
739 case 'r':
740 break;
741 }
742 }
743
744 AMDGPUInstPrinter::printRegOperand(MI->getOperand(OpNo).getReg(), O,
745 *TM.getSubtargetImpl(*MF->getFunction())->getRegisterInfo());
746 return false;
747}
Yaxun Liua711cc72016-07-16 05:09:21 +0000748
749// Emit a key and an integer value for runtime metadata.
750static void emitRuntimeMDIntValue(std::unique_ptr<MCStreamer> &Streamer,
751 RuntimeMD::Key K, uint64_t V,
752 unsigned Size) {
753 Streamer->EmitIntValue(K, 1);
754 Streamer->EmitIntValue(V, Size);
755}
756
757// Emit a key and a string value for runtime metadata.
758static void emitRuntimeMDStringValue(std::unique_ptr<MCStreamer> &Streamer,
759 RuntimeMD::Key K, StringRef S) {
760 Streamer->EmitIntValue(K, 1);
761 Streamer->EmitIntValue(S.size(), 4);
762 Streamer->EmitBytes(S);
763}
764
765// Emit a key and three integer values for runtime metadata.
766// The three integer values are obtained from MDNode \p Node;
767static void emitRuntimeMDThreeIntValues(std::unique_ptr<MCStreamer> &Streamer,
768 RuntimeMD::Key K, MDNode *Node,
769 unsigned Size) {
770 Streamer->EmitIntValue(K, 1);
771 Streamer->EmitIntValue(mdconst::extract<ConstantInt>(
772 Node->getOperand(0))->getZExtValue(), Size);
773 Streamer->EmitIntValue(mdconst::extract<ConstantInt>(
774 Node->getOperand(1))->getZExtValue(), Size);
775 Streamer->EmitIntValue(mdconst::extract<ConstantInt>(
776 Node->getOperand(2))->getZExtValue(), Size);
777}
778
779void AMDGPUAsmPrinter::emitStartOfRuntimeMetadata(const Module &M) {
780 OutStreamer->SwitchSection(getObjFileLowering().getContext()
781 .getELFSection(RuntimeMD::SectionName, ELF::SHT_PROGBITS, 0));
782
783 emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyMDVersion,
784 RuntimeMD::MDVersion << 8 | RuntimeMD::MDRevision, 2);
785 if (auto MD = M.getNamedMetadata("opencl.ocl.version")) {
Yaxun Liu4b1d9f72016-07-20 14:38:06 +0000786 if (MD->getNumOperands()) {
787 auto Node = MD->getOperand(0);
788 if (Node->getNumOperands() > 1) {
789 emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyLanguage,
790 RuntimeMD::OpenCL_C, 1);
791 uint16_t Major = mdconst::extract<ConstantInt>(Node->getOperand(0))
792 ->getZExtValue();
793 uint16_t Minor = mdconst::extract<ConstantInt>(Node->getOperand(1))
794 ->getZExtValue();
795 emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyLanguageVersion,
796 Major * 100 + Minor * 10, 2);
797 }
798 }
Yaxun Liua711cc72016-07-16 05:09:21 +0000799 }
800}
801
802static std::string getOCLTypeName(Type *Ty, bool isSigned) {
803 if (VectorType* VecTy = dyn_cast<VectorType>(Ty)) {
804 Type* EleTy = VecTy->getElementType();
805 unsigned Size = VecTy->getVectorNumElements();
806 return (Twine(getOCLTypeName(EleTy, isSigned)) + Twine(Size)).str();
807 }
808 switch (Ty->getTypeID()) {
809 case Type::HalfTyID: return "half";
810 case Type::FloatTyID: return "float";
811 case Type::DoubleTyID: return "double";
812 case Type::IntegerTyID: {
813 if (!isSigned)
814 return (Twine('u') + Twine(getOCLTypeName(Ty, true))).str();
815 auto IntTy = cast<IntegerType>(Ty);
816 auto BW = IntTy->getIntegerBitWidth();
817 switch (BW) {
818 case 8:
819 return "char";
820 case 16:
821 return "short";
822 case 32:
823 return "int";
824 case 64:
825 return "long";
826 default:
827 return (Twine('i') + Twine(BW)).str();
828 }
829 }
830 default:
831 llvm_unreachable("invalid type");
832 }
833}
834
835static RuntimeMD::KernelArg::ValueType getRuntimeMDValueType(
836 Type *Ty, StringRef TypeName) {
837 if (auto VT = dyn_cast<VectorType>(Ty))
838 return getRuntimeMDValueType(VT->getElementType(), TypeName);
839 else if (auto PT = dyn_cast<PointerType>(Ty))
840 return getRuntimeMDValueType(PT->getElementType(), TypeName);
841 else if (Ty->isHalfTy())
842 return RuntimeMD::KernelArg::F16;
843 else if (Ty->isFloatTy())
844 return RuntimeMD::KernelArg::F32;
845 else if (Ty->isDoubleTy())
846 return RuntimeMD::KernelArg::F64;
847 else if (IntegerType* intTy = dyn_cast<IntegerType>(Ty)) {
848 bool Signed = !TypeName.startswith("u");
849 switch (intTy->getIntegerBitWidth()) {
850 case 8:
851 return Signed ? RuntimeMD::KernelArg::I8 : RuntimeMD::KernelArg::U8;
852 case 16:
853 return Signed ? RuntimeMD::KernelArg::I16 : RuntimeMD::KernelArg::U16;
854 case 32:
855 return Signed ? RuntimeMD::KernelArg::I32 : RuntimeMD::KernelArg::U32;
856 case 64:
857 return Signed ? RuntimeMD::KernelArg::I64 : RuntimeMD::KernelArg::U64;
858 default:
859 // Runtime does not recognize other integer types. Report as
860 // struct type.
861 return RuntimeMD::KernelArg::Struct;
862 }
863 } else
864 return RuntimeMD::KernelArg::Struct;
865}
866
867void AMDGPUAsmPrinter::emitRuntimeMetadata(const Function &F) {
868 if (!F.getMetadata("kernel_arg_type"))
869 return;
870
871 MCContext &Context = getObjFileLowering().getContext();
872 OutStreamer->SwitchSection(
873 Context.getELFSection(RuntimeMD::SectionName, ELF::SHT_PROGBITS, 0));
874 OutStreamer->EmitIntValue(RuntimeMD::KeyKernelBegin, 1);
875 emitRuntimeMDStringValue(OutStreamer, RuntimeMD::KeyKernelName, F.getName());
876
877 for (auto &Arg:F.args()) {
878 // Emit KeyArgBegin.
879 unsigned I = Arg.getArgNo();
880 OutStreamer->EmitIntValue(RuntimeMD::KeyArgBegin, 1);
881
882 // Emit KeyArgSize and KeyArgAlign.
883 auto T = Arg.getType();
884 auto DL = F.getParent()->getDataLayout();
885 emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyArgSize,
886 DL.getTypeAllocSize(T), 4);
887 emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyArgAlign,
888 DL.getABITypeAlignment(T), 4);
889
890 // Emit KeyArgTypeName.
891 auto TypeName = dyn_cast<MDString>(F.getMetadata(
892 "kernel_arg_type")->getOperand(I))->getString();
893 emitRuntimeMDStringValue(OutStreamer, RuntimeMD::KeyArgTypeName, TypeName);
894
895 // Emit KeyArgName.
896 if (auto ArgNameMD = F.getMetadata("kernel_arg_name")) {
897 auto ArgName = cast<MDString>(ArgNameMD->getOperand(
898 I))->getString();
899 emitRuntimeMDStringValue(OutStreamer, RuntimeMD::KeyArgName, ArgName);
900 }
901
902 // Emit KeyArgIsVolatile, KeyArgIsRestrict, KeyArgIsConst and KeyArgIsPipe.
903 auto TypeQual = cast<MDString>(F.getMetadata(
904 "kernel_arg_type_qual")->getOperand(I))->getString();
905 SmallVector<StringRef, 1> SplitQ;
906 TypeQual.split(SplitQ, " ", -1, false/* drop empty entry*/);
907 for (auto &I:SplitQ) {
908 auto Key = StringSwitch<RuntimeMD::Key>(I)
909 .Case("volatile", RuntimeMD::KeyArgIsVolatile)
910 .Case("restrict", RuntimeMD::KeyArgIsRestrict)
911 .Case("const", RuntimeMD::KeyArgIsConst)
912 .Case("pipe", RuntimeMD::KeyArgIsPipe)
913 .Default(RuntimeMD::KeyNull);
914 OutStreamer->EmitIntValue(Key, 1);
915 }
916
917 // Emit KeyArgTypeKind.
918 auto BaseTypeName = cast<MDString>(
919 F.getMetadata("kernel_arg_base_type")->getOperand(I))->getString();
920 auto TypeKind = StringSwitch<RuntimeMD::KernelArg::TypeKind>(BaseTypeName)
921 .Case("sampler_t", RuntimeMD::KernelArg::Sampler)
922 .Case("queue_t", RuntimeMD::KernelArg::Queue)
923 .Cases("image1d_t", "image1d_array_t", "image1d_buffer_t",
924 "image2d_t" , "image2d_array_t", RuntimeMD::KernelArg::Image)
925 .Cases("image2d_depth_t", "image2d_array_depth_t",
926 "image2d_msaa_t", "image2d_array_msaa_t",
927 "image2d_msaa_depth_t", RuntimeMD::KernelArg::Image)
928 .Cases("image2d_array_msaa_depth_t", "image3d_t",
929 RuntimeMD::KernelArg::Image)
930 .Default(isa<PointerType>(T) ? RuntimeMD::KernelArg::Pointer :
931 RuntimeMD::KernelArg::Value);
932 emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyArgTypeKind, TypeKind, 1);
933
934 // Emit KeyArgValueType.
935 emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyArgValueType,
936 getRuntimeMDValueType(T, BaseTypeName), 2);
937
938 // Emit KeyArgAccQual.
939 auto AccQual = cast<MDString>(F.getMetadata(
940 "kernel_arg_access_qual")->getOperand(I))->getString();
941 auto AQ = StringSwitch<RuntimeMD::KernelArg::AccessQualifer>(AccQual)
942 .Case("read_only", RuntimeMD::KernelArg::ReadOnly)
943 .Case("write_only", RuntimeMD::KernelArg::WriteOnly)
944 .Case("read_write", RuntimeMD::KernelArg::ReadWrite)
945 .Default(RuntimeMD::KernelArg::None);
946 emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyArgAccQual,
947 AQ, 1);
948
949 // Emit KeyArgAddrQual.
950 if (isa<PointerType>(T))
951 emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyArgAddrQual,
952 T->getPointerAddressSpace(), 1);
953
954 // Emit KeyArgEnd
955 OutStreamer->EmitIntValue(RuntimeMD::KeyArgEnd, 1);
956 }
957
958 // Emit KeyReqdWorkGroupSize, KeyWorkGroupSizeHint, and KeyVecTypeHint.
959 if (auto RWGS = F.getMetadata("reqd_work_group_size"))
960 emitRuntimeMDThreeIntValues(OutStreamer, RuntimeMD::KeyReqdWorkGroupSize,
961 RWGS, 4);
962 if (auto WGSH = F.getMetadata("work_group_size_hint"))
963 emitRuntimeMDThreeIntValues(OutStreamer, RuntimeMD::KeyWorkGroupSizeHint,
964 WGSH, 4);
965 if (auto VTH = F.getMetadata("vec_type_hint")) {
966 auto TypeName = getOCLTypeName(cast<ValueAsMetadata>(
967 VTH->getOperand(0))->getType(), mdconst::extract<ConstantInt>(
968 VTH->getOperand(1))->getZExtValue());
969 emitRuntimeMDStringValue(OutStreamer, RuntimeMD::KeyVecTypeHint,
970 TypeName);
971 }
972
973 // Emit KeyKernelEnd
974 OutStreamer->EmitIntValue(RuntimeMD::KeyKernelEnd, 1);
975}