blob: 7204a34a8d79978e53c993afe1f826280f7c994e [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"
34#include "llvm/MC/MCContext.h"
35#include "llvm/MC/MCSectionELF.h"
36#include "llvm/MC/MCStreamer.h"
37#include "llvm/Support/ELF.h"
38#include "llvm/Support/MathExtras.h"
39#include "llvm/Support/TargetRegistry.h"
40#include "llvm/Target/TargetLoweringObjectFile.h"
41
42using namespace llvm;
43
44// TODO: This should get the default rounding mode from the kernel. We just set
45// the default here, but this could change if the OpenCL rounding mode pragmas
46// are used.
47//
48// The denormal mode here should match what is reported by the OpenCL runtime
49// for the CL_FP_DENORM bit from CL_DEVICE_{HALF|SINGLE|DOUBLE}_FP_CONFIG, but
50// can also be override to flush with the -cl-denorms-are-zero compiler flag.
51//
52// AMD OpenCL only sets flush none and reports CL_FP_DENORM for double
53// precision, and leaves single precision to flush all and does not report
54// CL_FP_DENORM for CL_DEVICE_SINGLE_FP_CONFIG. Mesa's OpenCL currently reports
55// CL_FP_DENORM for both.
56//
57// FIXME: It seems some instructions do not support single precision denormals
58// regardless of the mode (exp_*_f32, rcp_*_f32, rsq_*_f32, rsq_*f32, sqrt_f32,
59// and sin_f32, cos_f32 on most parts).
60
61// We want to use these instructions, and using fp32 denormals also causes
62// instructions to run at the double precision rate for the device so it's
63// probably best to just report no single precision denormals.
64static uint32_t getFPMode(const MachineFunction &F) {
65 const AMDGPUSubtarget& ST = F.getSubtarget<AMDGPUSubtarget>();
66 // TODO: Is there any real use for the flush in only / flush out only modes?
67
68 uint32_t FP32Denormals =
69 ST.hasFP32Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
70
71 uint32_t FP64Denormals =
72 ST.hasFP64Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
73
74 return FP_ROUND_MODE_SP(FP_ROUND_ROUND_TO_NEAREST) |
75 FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_NEAREST) |
76 FP_DENORM_MODE_SP(FP32Denormals) |
77 FP_DENORM_MODE_DP(FP64Denormals);
78}
79
80static AsmPrinter *
81createAMDGPUAsmPrinterPass(TargetMachine &tm,
82 std::unique_ptr<MCStreamer> &&Streamer) {
83 return new AMDGPUAsmPrinter(tm, std::move(Streamer));
84}
85
86extern "C" void LLVMInitializeAMDGPUAsmPrinter() {
87 TargetRegistry::RegisterAsmPrinter(TheAMDGPUTarget, createAMDGPUAsmPrinterPass);
88 TargetRegistry::RegisterAsmPrinter(TheGCNTarget, createAMDGPUAsmPrinterPass);
89}
90
91AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM,
92 std::unique_ptr<MCStreamer> Streamer)
93 : AsmPrinter(TM, std::move(Streamer)) {}
94
Tom Stellardf4218372016-01-12 17:18:17 +000095void AMDGPUAsmPrinter::EmitStartOfAsmFile(Module &M) {
96 if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
97 return;
98
99 // Need to construct an MCSubtargetInfo here in case we have no functions
100 // in the module.
101 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
102 TM.getTargetTriple().str(), TM.getTargetCPU(),
103 TM.getTargetFeatureString()));
104
105 AMDGPUTargetStreamer *TS =
106 static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
107
Tom Stellardfcfaea42016-05-05 17:03:33 +0000108 TS->EmitDirectiveHSACodeObjectVersion(2, 0);
109
Tom Stellardf4218372016-01-12 17:18:17 +0000110 AMDGPU::IsaVersion ISA = AMDGPU::getIsaVersion(STI->getFeatureBits());
111 TS->EmitDirectiveHSACodeObjectISA(ISA.Major, ISA.Minor, ISA.Stepping,
112 "AMD", "AMDGPU");
113}
114
Tom Stellardf151a452015-06-26 21:14:58 +0000115void AMDGPUAsmPrinter::EmitFunctionBodyStart() {
116 const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
117 SIProgramInfo KernelInfo;
118 if (STM.isAmdHsaOS()) {
119 getSIProgramInfo(KernelInfo, *MF);
120 EmitAmdKernelCodeT(*MF, KernelInfo);
121 }
122}
123
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000124void AMDGPUAsmPrinter::EmitFunctionEntryLabel() {
125 const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
126 const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
127 if (MFI->isKernel() && STM.isAmdHsaOS()) {
128 AMDGPUTargetStreamer *TS =
129 static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
130 TS->EmitAMDGPUSymbolType(CurrentFnSym->getName(),
131 ELF::STT_AMDGPU_HSA_KERNEL);
132 }
133
134 AsmPrinter::EmitFunctionEntryLabel();
135}
136
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000137void AMDGPUAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
138
Tom Stellard00f2f912015-12-02 19:47:57 +0000139 // Group segment variables aren't emitted in HSA.
140 if (AMDGPU::isGroupSegment(GV))
141 return;
142
Tom Stellardfcfaea42016-05-05 17:03:33 +0000143 AsmPrinter::EmitGlobalVariable(GV);
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000144}
145
Tom Stellard45bb48e2015-06-13 03:28:10 +0000146bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
147
148 // The starting address of all shader programs must be 256 bytes aligned.
149 MF.setAlignment(8);
150
151 SetupMachineFunction(MF);
152
153 MCContext &Context = getObjFileLowering().getContext();
154 MCSectionELF *ConfigSection =
155 Context.getELFSection(".AMDGPU.config", ELF::SHT_PROGBITS, 0);
156 OutStreamer->SwitchSection(ConfigSection);
157
158 const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
159 SIProgramInfo KernelInfo;
Tom Stellardf151a452015-06-26 21:14:58 +0000160 if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault297ae312015-08-15 00:12:39 +0000161 getSIProgramInfo(KernelInfo, MF);
Tom Stellardf151a452015-06-26 21:14:58 +0000162 if (!STM.isAmdHsaOS()) {
Tom Stellardf151a452015-06-26 21:14:58 +0000163 EmitProgramInfoSI(MF, KernelInfo);
164 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000165 } else {
166 EmitProgramInfoR600(MF);
167 }
168
169 DisasmLines.clear();
170 HexLines.clear();
171 DisasmLineMaxLen = 0;
172
173 EmitFunctionBody();
174
175 if (isVerbose()) {
176 MCSectionELF *CommentSection =
177 Context.getELFSection(".AMDGPU.csdata", ELF::SHT_PROGBITS, 0);
178 OutStreamer->SwitchSection(CommentSection);
179
180 if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
181 OutStreamer->emitRawComment(" Kernel info:", false);
182 OutStreamer->emitRawComment(" codeLenInByte = " + Twine(KernelInfo.CodeLen),
183 false);
184 OutStreamer->emitRawComment(" NumSgprs: " + Twine(KernelInfo.NumSGPR),
185 false);
186 OutStreamer->emitRawComment(" NumVgprs: " + Twine(KernelInfo.NumVGPR),
187 false);
188 OutStreamer->emitRawComment(" FloatMode: " + Twine(KernelInfo.FloatMode),
189 false);
190 OutStreamer->emitRawComment(" IeeeMode: " + Twine(KernelInfo.IEEEMode),
191 false);
192 OutStreamer->emitRawComment(" ScratchSize: " + Twine(KernelInfo.ScratchSize),
193 false);
Matt Arsenaultfd8ab092016-04-14 22:11:51 +0000194 OutStreamer->emitRawComment(" LDSByteSize: " + Twine(KernelInfo.LDSSize) +
195 " bytes/workgroup (compile time only)", false);
Matt Arsenaultd41c0db2015-11-05 05:27:07 +0000196
Konstantin Zhuravlyov1d99c4d2016-04-26 15:43:14 +0000197 OutStreamer->emitRawComment(" ReservedVGPRFirst: " + Twine(KernelInfo.ReservedVGPRFirst),
198 false);
199 OutStreamer->emitRawComment(" ReservedVGPRCount: " + Twine(KernelInfo.ReservedVGPRCount),
200 false);
201
Matt Arsenaultd41c0db2015-11-05 05:27:07 +0000202 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:USER_SGPR: " +
Matt Arsenault8246d4a2015-11-11 00:27:46 +0000203 Twine(G_00B84C_USER_SGPR(KernelInfo.ComputePGMRSrc2)),
Matt Arsenaultd41c0db2015-11-05 05:27:07 +0000204 false);
Matt Arsenault8246d4a2015-11-11 00:27:46 +0000205 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_X_EN: " +
206 Twine(G_00B84C_TGID_X_EN(KernelInfo.ComputePGMRSrc2)),
207 false);
208 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_Y_EN: " +
209 Twine(G_00B84C_TGID_Y_EN(KernelInfo.ComputePGMRSrc2)),
210 false);
211 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_Z_EN: " +
212 Twine(G_00B84C_TGID_Z_EN(KernelInfo.ComputePGMRSrc2)),
213 false);
214 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: " +
215 Twine(G_00B84C_TIDIG_COMP_CNT(KernelInfo.ComputePGMRSrc2)),
216 false);
217
Tom Stellard45bb48e2015-06-13 03:28:10 +0000218 } else {
219 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
220 OutStreamer->emitRawComment(
221 Twine("SQ_PGM_RESOURCES:STACK_SIZE = " + Twine(MFI->StackSize)));
222 }
223 }
224
225 if (STM.dumpCode()) {
226
227 OutStreamer->SwitchSection(
228 Context.getELFSection(".AMDGPU.disasm", ELF::SHT_NOTE, 0));
229
230 for (size_t i = 0; i < DisasmLines.size(); ++i) {
231 std::string Comment(DisasmLineMaxLen - DisasmLines[i].size(), ' ');
232 Comment += " ; " + HexLines[i] + "\n";
233
234 OutStreamer->EmitBytes(StringRef(DisasmLines[i]));
235 OutStreamer->EmitBytes(StringRef(Comment));
236 }
237 }
238
239 return false;
240}
241
242void AMDGPUAsmPrinter::EmitProgramInfoR600(const MachineFunction &MF) {
243 unsigned MaxGPR = 0;
244 bool killPixel = false;
245 const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
246 const R600RegisterInfo *RI =
247 static_cast<const R600RegisterInfo *>(STM.getRegisterInfo());
248 const R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
249
250 for (const MachineBasicBlock &MBB : MF) {
251 for (const MachineInstr &MI : MBB) {
252 if (MI.getOpcode() == AMDGPU::KILLGT)
253 killPixel = true;
254 unsigned numOperands = MI.getNumOperands();
255 for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
256 const MachineOperand &MO = MI.getOperand(op_idx);
257 if (!MO.isReg())
258 continue;
259 unsigned HWReg = RI->getEncodingValue(MO.getReg()) & 0xff;
260
261 // Register with value > 127 aren't GPR
262 if (HWReg > 127)
263 continue;
264 MaxGPR = std::max(MaxGPR, HWReg);
265 }
266 }
267 }
268
269 unsigned RsrcReg;
270 if (STM.getGeneration() >= AMDGPUSubtarget::EVERGREEN) {
271 // Evergreen / Northern Islands
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000272 switch (MF.getFunction()->getCallingConv()) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000273 default: // Fall through
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000274 case CallingConv::AMDGPU_CS: RsrcReg = R_0288D4_SQ_PGM_RESOURCES_LS; break;
275 case CallingConv::AMDGPU_GS: RsrcReg = R_028878_SQ_PGM_RESOURCES_GS; break;
276 case CallingConv::AMDGPU_PS: RsrcReg = R_028844_SQ_PGM_RESOURCES_PS; break;
277 case CallingConv::AMDGPU_VS: RsrcReg = R_028860_SQ_PGM_RESOURCES_VS; break;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000278 }
279 } else {
280 // R600 / R700
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000281 switch (MF.getFunction()->getCallingConv()) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000282 default: // Fall through
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000283 case CallingConv::AMDGPU_GS: // Fall through
284 case CallingConv::AMDGPU_CS: // Fall through
285 case CallingConv::AMDGPU_VS: RsrcReg = R_028868_SQ_PGM_RESOURCES_VS; break;
286 case CallingConv::AMDGPU_PS: RsrcReg = R_028850_SQ_PGM_RESOURCES_PS; break;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000287 }
288 }
289
290 OutStreamer->EmitIntValue(RsrcReg, 4);
291 OutStreamer->EmitIntValue(S_NUM_GPRS(MaxGPR + 1) |
292 S_STACK_SIZE(MFI->StackSize), 4);
293 OutStreamer->EmitIntValue(R_02880C_DB_SHADER_CONTROL, 4);
294 OutStreamer->EmitIntValue(S_02880C_KILL_ENABLE(killPixel), 4);
295
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000296 if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000297 OutStreamer->EmitIntValue(R_0288E8_SQ_LDS_ALLOC, 4);
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000298 OutStreamer->EmitIntValue(alignTo(MFI->LDSSize, 4) >> 2, 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000299 }
300}
301
302void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo,
303 const MachineFunction &MF) const {
304 const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
305 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
306 uint64_t CodeSize = 0;
307 unsigned MaxSGPR = 0;
308 unsigned MaxVGPR = 0;
309 bool VCCUsed = false;
310 bool FlatUsed = false;
311 const SIRegisterInfo *RI =
312 static_cast<const SIRegisterInfo *>(STM.getRegisterInfo());
Matt Arsenaulta9720c62016-06-20 17:51:32 +0000313 const SIInstrInfo *TII =
314 static_cast<const SIInstrInfo *>(STM.getInstrInfo());
Tom Stellard45bb48e2015-06-13 03:28:10 +0000315
316 for (const MachineBasicBlock &MBB : MF) {
317 for (const MachineInstr &MI : MBB) {
318 // TODO: CodeSize should account for multiple functions.
Matt Arsenaultc5746862015-08-12 09:04:44 +0000319
320 // TODO: Should we count size of debug info?
321 if (MI.isDebugValue())
322 continue;
323
Matt Arsenaulta9720c62016-06-20 17:51:32 +0000324 CodeSize += TII->getInstSizeInBytes(MI);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000325
326 unsigned numOperands = MI.getNumOperands();
327 for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
328 const MachineOperand &MO = MI.getOperand(op_idx);
329 unsigned width = 0;
330 bool isSGPR = false;
331
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000332 if (!MO.isReg())
Tom Stellard45bb48e2015-06-13 03:28:10 +0000333 continue;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000334
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000335 unsigned reg = MO.getReg();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000336 switch (reg) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000337 case AMDGPU::EXEC:
Nicolai Haehnle74839372016-04-19 21:58:17 +0000338 case AMDGPU::EXEC_LO:
339 case AMDGPU::EXEC_HI:
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000340 case AMDGPU::SCC:
Tom Stellard45bb48e2015-06-13 03:28:10 +0000341 case AMDGPU::M0:
342 continue;
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000343
344 case AMDGPU::VCC:
345 case AMDGPU::VCC_LO:
346 case AMDGPU::VCC_HI:
347 VCCUsed = true;
348 continue;
349
350 case AMDGPU::FLAT_SCR:
351 case AMDGPU::FLAT_SCR_LO:
352 case AMDGPU::FLAT_SCR_HI:
353 FlatUsed = true;
354 continue;
355
Artem Tamazoveb4d5a92016-04-13 16:18:41 +0000356 case AMDGPU::TBA:
357 case AMDGPU::TBA_LO:
358 case AMDGPU::TBA_HI:
359 case AMDGPU::TMA:
360 case AMDGPU::TMA_LO:
361 case AMDGPU::TMA_HI:
362 llvm_unreachable("Trap Handler registers should not be used");
363 continue;
364
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000365 default:
366 break;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000367 }
368
369 if (AMDGPU::SReg_32RegClass.contains(reg)) {
Artem Tamazoveb4d5a92016-04-13 16:18:41 +0000370 if (AMDGPU::TTMP_32RegClass.contains(reg)) {
371 llvm_unreachable("Trap Handler registers should not be used");
372 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000373 isSGPR = true;
374 width = 1;
375 } else if (AMDGPU::VGPR_32RegClass.contains(reg)) {
376 isSGPR = false;
377 width = 1;
378 } else if (AMDGPU::SReg_64RegClass.contains(reg)) {
Artem Tamazoveb4d5a92016-04-13 16:18:41 +0000379 if (AMDGPU::TTMP_64RegClass.contains(reg)) {
380 llvm_unreachable("Trap Handler registers should not be used");
381 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000382 isSGPR = true;
383 width = 2;
384 } else if (AMDGPU::VReg_64RegClass.contains(reg)) {
385 isSGPR = false;
386 width = 2;
387 } else if (AMDGPU::VReg_96RegClass.contains(reg)) {
388 isSGPR = false;
389 width = 3;
390 } else if (AMDGPU::SReg_128RegClass.contains(reg)) {
391 isSGPR = true;
392 width = 4;
393 } else if (AMDGPU::VReg_128RegClass.contains(reg)) {
394 isSGPR = false;
395 width = 4;
396 } else if (AMDGPU::SReg_256RegClass.contains(reg)) {
397 isSGPR = true;
398 width = 8;
399 } else if (AMDGPU::VReg_256RegClass.contains(reg)) {
400 isSGPR = false;
401 width = 8;
402 } else if (AMDGPU::SReg_512RegClass.contains(reg)) {
403 isSGPR = true;
404 width = 16;
405 } else if (AMDGPU::VReg_512RegClass.contains(reg)) {
406 isSGPR = false;
407 width = 16;
408 } else {
409 llvm_unreachable("Unknown register class");
410 }
411 unsigned hwReg = RI->getEncodingValue(reg) & 0xff;
412 unsigned maxUsed = hwReg + width - 1;
413 if (isSGPR) {
414 MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR;
415 } else {
416 MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR;
417 }
418 }
419 }
420 }
421
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000422 unsigned ExtraSGPRs = 0;
423
424 if (VCCUsed)
425 ExtraSGPRs = 2;
426
427 if (STM.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS) {
428 if (FlatUsed)
429 ExtraSGPRs = 4;
430 } else {
431 if (STM.isXNACKEnabled())
432 ExtraSGPRs = 4;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000433
Nicolai Haehnle5b504972016-01-04 23:35:53 +0000434 if (FlatUsed)
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000435 ExtraSGPRs = 6;
Tom Stellardcaaa3aa2015-12-17 17:05:09 +0000436 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000437
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000438 MaxSGPR += ExtraSGPRs;
439
Konstantin Zhuravlyov29ddd2b2016-05-24 18:37:18 +0000440 // Record first reserved register and reserved register count fields, and
441 // update max register counts if "amdgpu-debugger-reserve-regs" attribute was
442 // specified.
443 if (STM.debuggerReserveRegs()) {
Konstantin Zhuravlyov1d99c4d2016-04-26 15:43:14 +0000444 ProgInfo.ReservedVGPRFirst = MaxVGPR + 1;
Konstantin Zhuravlyov29ddd2b2016-05-24 18:37:18 +0000445 ProgInfo.ReservedVGPRCount = MFI->getDebuggerReservedVGPRCount();
446 MaxVGPR += MFI->getDebuggerReservedVGPRCount();
Konstantin Zhuravlyov1d99c4d2016-04-26 15:43:14 +0000447 }
448
Tom Stellard45bb48e2015-06-13 03:28:10 +0000449 // We found the maximum register index. They start at 0, so add one to get the
450 // number of registers.
451 ProgInfo.NumVGPR = MaxVGPR + 1;
452 ProgInfo.NumSGPR = MaxSGPR + 1;
453
454 if (STM.hasSGPRInitBug()) {
Matt Arsenault417c93e2015-06-17 20:55:25 +0000455 if (ProgInfo.NumSGPR > AMDGPUSubtarget::FIXED_SGPR_COUNT_FOR_INIT_BUG) {
456 LLVMContext &Ctx = MF.getFunction()->getContext();
457 Ctx.emitError("too many SGPRs used with the SGPR init bug");
458 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000459
460 ProgInfo.NumSGPR = AMDGPUSubtarget::FIXED_SGPR_COUNT_FOR_INIT_BUG;
461 }
462
Matt Arsenault41003af2015-11-30 21:16:07 +0000463 if (MFI->NumUserSGPRs > STM.getMaxNumUserSGPRs()) {
464 LLVMContext &Ctx = MF.getFunction()->getContext();
465 Ctx.emitError("too many user SGPRs used");
466 }
467
Matt Arsenault1c4d0ef2016-04-28 19:37:35 +0000468 if (MFI->LDSSize > static_cast<unsigned>(STM.getLocalMemorySize())) {
469 LLVMContext &Ctx = MF.getFunction()->getContext();
470 Ctx.emitError("LDS size exceeds device maximum");
471 }
472
Tom Stellard45bb48e2015-06-13 03:28:10 +0000473 ProgInfo.VGPRBlocks = (ProgInfo.NumVGPR - 1) / 4;
474 ProgInfo.SGPRBlocks = (ProgInfo.NumSGPR - 1) / 8;
475 // Set the value to initialize FP_ROUND and FP_DENORM parts of the mode
476 // register.
477 ProgInfo.FloatMode = getFPMode(MF);
478
Tom Stellard45bb48e2015-06-13 03:28:10 +0000479 ProgInfo.IEEEMode = 0;
480
Matt Arsenault7293f982016-01-28 20:53:35 +0000481 // Make clamp modifier on NaN input returns 0.
482 ProgInfo.DX10Clamp = 1;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000483
484 const MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Matt Arsenaultf4dfc1a2016-03-01 04:58:20 +0000485 ProgInfo.ScratchSize = FrameInfo->getStackSize();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000486
487 ProgInfo.FlatUsed = FlatUsed;
488 ProgInfo.VCCUsed = VCCUsed;
489 ProgInfo.CodeLen = CodeSize;
490
491 unsigned LDSAlignShift;
492 if (STM.getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) {
493 // LDS is allocated in 64 dword blocks.
494 LDSAlignShift = 8;
495 } else {
496 // LDS is allocated in 128 dword blocks.
497 LDSAlignShift = 9;
498 }
499
500 unsigned LDSSpillSize = MFI->LDSWaveSpillSize *
501 MFI->getMaximumWorkGroupSize(MF);
502
503 ProgInfo.LDSSize = MFI->LDSSize + LDSSpillSize;
504 ProgInfo.LDSBlocks =
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +0000505 alignTo(ProgInfo.LDSSize, 1ULL << LDSAlignShift) >> LDSAlignShift;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000506
507 // Scratch is allocated in 256 dword blocks.
508 unsigned ScratchAlignShift = 10;
509 // We need to program the hardware with the amount of scratch memory that
510 // is used by the entire wave. ProgInfo.ScratchSize is the amount of
511 // scratch memory used per thread.
512 ProgInfo.ScratchBlocks =
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000513 alignTo(ProgInfo.ScratchSize * STM.getWavefrontSize(),
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +0000514 1ULL << ScratchAlignShift) >>
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000515 ScratchAlignShift;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000516
517 ProgInfo.ComputePGMRSrc1 =
518 S_00B848_VGPRS(ProgInfo.VGPRBlocks) |
519 S_00B848_SGPRS(ProgInfo.SGPRBlocks) |
520 S_00B848_PRIORITY(ProgInfo.Priority) |
521 S_00B848_FLOAT_MODE(ProgInfo.FloatMode) |
522 S_00B848_PRIV(ProgInfo.Priv) |
523 S_00B848_DX10_CLAMP(ProgInfo.DX10Clamp) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000524 S_00B848_DEBUG_MODE(ProgInfo.DebugMode) |
Tom Stellard45bb48e2015-06-13 03:28:10 +0000525 S_00B848_IEEE_MODE(ProgInfo.IEEEMode);
526
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000527 // 0 = X, 1 = XY, 2 = XYZ
528 unsigned TIDIGCompCnt = 0;
529 if (MFI->hasWorkItemIDZ())
530 TIDIGCompCnt = 2;
531 else if (MFI->hasWorkItemIDY())
532 TIDIGCompCnt = 1;
533
Tom Stellard45bb48e2015-06-13 03:28:10 +0000534 ProgInfo.ComputePGMRSrc2 =
535 S_00B84C_SCRATCH_EN(ProgInfo.ScratchBlocks > 0) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000536 S_00B84C_USER_SGPR(MFI->getNumUserSGPRs()) |
537 S_00B84C_TGID_X_EN(MFI->hasWorkGroupIDX()) |
538 S_00B84C_TGID_Y_EN(MFI->hasWorkGroupIDY()) |
539 S_00B84C_TGID_Z_EN(MFI->hasWorkGroupIDZ()) |
540 S_00B84C_TG_SIZE_EN(MFI->hasWorkGroupInfo()) |
541 S_00B84C_TIDIG_COMP_CNT(TIDIGCompCnt) |
542 S_00B84C_EXCP_EN_MSB(0) |
543 S_00B84C_LDS_SIZE(ProgInfo.LDSBlocks) |
544 S_00B84C_EXCP_EN(0);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000545}
546
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000547static unsigned getRsrcReg(CallingConv::ID CallConv) {
548 switch (CallConv) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000549 default: // Fall through
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000550 case CallingConv::AMDGPU_CS: return R_00B848_COMPUTE_PGM_RSRC1;
551 case CallingConv::AMDGPU_GS: return R_00B228_SPI_SHADER_PGM_RSRC1_GS;
552 case CallingConv::AMDGPU_PS: return R_00B028_SPI_SHADER_PGM_RSRC1_PS;
553 case CallingConv::AMDGPU_VS: return R_00B128_SPI_SHADER_PGM_RSRC1_VS;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000554 }
555}
556
557void AMDGPUAsmPrinter::EmitProgramInfoSI(const MachineFunction &MF,
558 const SIProgramInfo &KernelInfo) {
559 const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
560 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000561 unsigned RsrcReg = getRsrcReg(MF.getFunction()->getCallingConv());
Tom Stellard45bb48e2015-06-13 03:28:10 +0000562
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000563 if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000564 OutStreamer->EmitIntValue(R_00B848_COMPUTE_PGM_RSRC1, 4);
565
566 OutStreamer->EmitIntValue(KernelInfo.ComputePGMRSrc1, 4);
567
568 OutStreamer->EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4);
569 OutStreamer->EmitIntValue(KernelInfo.ComputePGMRSrc2, 4);
570
571 OutStreamer->EmitIntValue(R_00B860_COMPUTE_TMPRING_SIZE, 4);
572 OutStreamer->EmitIntValue(S_00B860_WAVESIZE(KernelInfo.ScratchBlocks), 4);
573
574 // TODO: Should probably note flat usage somewhere. SC emits a "FlatPtr32 =
575 // 0" comment but I don't see a corresponding field in the register spec.
576 } else {
577 OutStreamer->EmitIntValue(RsrcReg, 4);
578 OutStreamer->EmitIntValue(S_00B028_VGPRS(KernelInfo.VGPRBlocks) |
579 S_00B028_SGPRS(KernelInfo.SGPRBlocks), 4);
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000580 if (STM.isVGPRSpillingEnabled(*MF.getFunction())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000581 OutStreamer->EmitIntValue(R_0286E8_SPI_TMPRING_SIZE, 4);
582 OutStreamer->EmitIntValue(S_0286E8_WAVESIZE(KernelInfo.ScratchBlocks), 4);
583 }
584 }
585
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000586 if (MF.getFunction()->getCallingConv() == CallingConv::AMDGPU_PS) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000587 OutStreamer->EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4);
588 OutStreamer->EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(KernelInfo.LDSBlocks), 4);
589 OutStreamer->EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000590 OutStreamer->EmitIntValue(MFI->PSInputEna, 4);
591 OutStreamer->EmitIntValue(R_0286D0_SPI_PS_INPUT_ADDR, 4);
592 OutStreamer->EmitIntValue(MFI->getPSInputAddr(), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000593 }
594}
595
Matt Arsenault24ee0782016-02-12 02:40:47 +0000596// This is supposed to be log2(Size)
597static amd_element_byte_size_t getElementByteSizeValue(unsigned Size) {
598 switch (Size) {
599 case 4:
600 return AMD_ELEMENT_4_BYTES;
601 case 8:
602 return AMD_ELEMENT_8_BYTES;
603 case 16:
604 return AMD_ELEMENT_16_BYTES;
605 default:
606 llvm_unreachable("invalid private_element_size");
607 }
608}
609
Tom Stellard45bb48e2015-06-13 03:28:10 +0000610void AMDGPUAsmPrinter::EmitAmdKernelCodeT(const MachineFunction &MF,
Tom Stellardff7416b2015-06-26 21:58:31 +0000611 const SIProgramInfo &KernelInfo) const {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000612 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
613 const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
614 amd_kernel_code_t header;
615
Tom Stellardff7416b2015-06-26 21:58:31 +0000616 AMDGPU::initDefaultAMDKernelCodeT(header, STM.getFeatureBits());
Tom Stellard45bb48e2015-06-13 03:28:10 +0000617
618 header.compute_pgm_resource_registers =
619 KernelInfo.ComputePGMRSrc1 |
620 (KernelInfo.ComputePGMRSrc2 << 32);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000621 header.code_properties = AMD_CODE_PROPERTY_IS_PTR64;
622
Matt Arsenault24ee0782016-02-12 02:40:47 +0000623
624 AMD_HSA_BITS_SET(header.code_properties,
625 AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE,
626 getElementByteSizeValue(STM.getMaxPrivateElementSize()));
627
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000628 if (MFI->hasPrivateSegmentBuffer()) {
629 header.code_properties |=
630 AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER;
631 }
632
633 if (MFI->hasDispatchPtr())
634 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
635
636 if (MFI->hasQueuePtr())
637 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR;
638
639 if (MFI->hasKernargSegmentPtr())
640 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR;
641
642 if (MFI->hasDispatchID())
643 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID;
644
645 if (MFI->hasFlatScratchInit())
646 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT;
647
648 // TODO: Private segment size
649
650 if (MFI->hasGridWorkgroupCountX()) {
651 header.code_properties |=
652 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X;
653 }
654
655 if (MFI->hasGridWorkgroupCountY()) {
656 header.code_properties |=
657 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y;
658 }
659
660 if (MFI->hasGridWorkgroupCountZ()) {
661 header.code_properties |=
662 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z;
663 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000664
Tom Stellard48f29f22015-11-26 00:43:29 +0000665 if (MFI->hasDispatchPtr())
666 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
667
Nicolai Haehnle5b504972016-01-04 23:35:53 +0000668 if (STM.isXNACKEnabled())
669 header.code_properties |= AMD_CODE_PROPERTY_IS_XNACK_SUPPORTED;
670
Tom Stellard45bb48e2015-06-13 03:28:10 +0000671 header.kernarg_segment_byte_size = MFI->ABIArgOffset;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000672 header.wavefront_sgpr_count = KernelInfo.NumSGPR;
673 header.workitem_vgpr_count = KernelInfo.NumVGPR;
Tom Stellarda4953072015-12-15 22:55:30 +0000674 header.workitem_private_segment_byte_size = KernelInfo.ScratchSize;
Tom Stellard7750f4e2015-12-15 23:15:25 +0000675 header.workgroup_group_segment_byte_size = KernelInfo.LDSSize;
Konstantin Zhuravlyov1d99c4d2016-04-26 15:43:14 +0000676 header.reserved_vgpr_first = KernelInfo.ReservedVGPRFirst;
677 header.reserved_vgpr_count = KernelInfo.ReservedVGPRCount;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000678
Tom Stellardff7416b2015-06-26 21:58:31 +0000679 AMDGPUTargetStreamer *TS =
680 static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
Tom Stellardfcfaea42016-05-05 17:03:33 +0000681
682 OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
Tom Stellardff7416b2015-06-26 21:58:31 +0000683 TS->EmitAMDKernelCodeT(header);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000684}
685
686bool AMDGPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
687 unsigned AsmVariant,
688 const char *ExtraCode, raw_ostream &O) {
689 if (ExtraCode && ExtraCode[0]) {
690 if (ExtraCode[1] != 0)
691 return true; // Unknown modifier.
692
693 switch (ExtraCode[0]) {
694 default:
695 // See if this is a generic print operand
696 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
697 case 'r':
698 break;
699 }
700 }
701
702 AMDGPUInstPrinter::printRegOperand(MI->getOperand(OpNo).getReg(), O,
703 *TM.getSubtargetImpl(*MF->getFunction())->getRegisterInfo());
704 return false;
705}