blob: 2673a331fd8107abe326bb8c22df30059669e8e2 [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"
31#include "SIRegisterInfo.h"
32#include "llvm/CodeGen/MachineFrameInfo.h"
33#include "llvm/MC/MCContext.h"
34#include "llvm/MC/MCSectionELF.h"
35#include "llvm/MC/MCStreamer.h"
36#include "llvm/Support/ELF.h"
37#include "llvm/Support/MathExtras.h"
38#include "llvm/Support/TargetRegistry.h"
39#include "llvm/Target/TargetLoweringObjectFile.h"
40
41using namespace llvm;
42
43// TODO: This should get the default rounding mode from the kernel. We just set
44// the default here, but this could change if the OpenCL rounding mode pragmas
45// are used.
46//
47// The denormal mode here should match what is reported by the OpenCL runtime
48// for the CL_FP_DENORM bit from CL_DEVICE_{HALF|SINGLE|DOUBLE}_FP_CONFIG, but
49// can also be override to flush with the -cl-denorms-are-zero compiler flag.
50//
51// AMD OpenCL only sets flush none and reports CL_FP_DENORM for double
52// precision, and leaves single precision to flush all and does not report
53// CL_FP_DENORM for CL_DEVICE_SINGLE_FP_CONFIG. Mesa's OpenCL currently reports
54// CL_FP_DENORM for both.
55//
56// FIXME: It seems some instructions do not support single precision denormals
57// regardless of the mode (exp_*_f32, rcp_*_f32, rsq_*_f32, rsq_*f32, sqrt_f32,
58// and sin_f32, cos_f32 on most parts).
59
60// We want to use these instructions, and using fp32 denormals also causes
61// instructions to run at the double precision rate for the device so it's
62// probably best to just report no single precision denormals.
63static uint32_t getFPMode(const MachineFunction &F) {
64 const AMDGPUSubtarget& ST = F.getSubtarget<AMDGPUSubtarget>();
65 // TODO: Is there any real use for the flush in only / flush out only modes?
66
67 uint32_t FP32Denormals =
68 ST.hasFP32Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
69
70 uint32_t FP64Denormals =
71 ST.hasFP64Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
72
73 return FP_ROUND_MODE_SP(FP_ROUND_ROUND_TO_NEAREST) |
74 FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_NEAREST) |
75 FP_DENORM_MODE_SP(FP32Denormals) |
76 FP_DENORM_MODE_DP(FP64Denormals);
77}
78
79static AsmPrinter *
80createAMDGPUAsmPrinterPass(TargetMachine &tm,
81 std::unique_ptr<MCStreamer> &&Streamer) {
82 return new AMDGPUAsmPrinter(tm, std::move(Streamer));
83}
84
85extern "C" void LLVMInitializeAMDGPUAsmPrinter() {
86 TargetRegistry::RegisterAsmPrinter(TheAMDGPUTarget, createAMDGPUAsmPrinterPass);
87 TargetRegistry::RegisterAsmPrinter(TheGCNTarget, createAMDGPUAsmPrinterPass);
88}
89
90AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM,
91 std::unique_ptr<MCStreamer> Streamer)
92 : AsmPrinter(TM, std::move(Streamer)) {}
93
Tom Stellardf4218372016-01-12 17:18:17 +000094void AMDGPUAsmPrinter::EmitStartOfAsmFile(Module &M) {
95 if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
96 return;
97
98 // Need to construct an MCSubtargetInfo here in case we have no functions
99 // in the module.
100 std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
101 TM.getTargetTriple().str(), TM.getTargetCPU(),
102 TM.getTargetFeatureString()));
103
104 AMDGPUTargetStreamer *TS =
105 static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
106
107 TS->EmitDirectiveHSACodeObjectVersion(1, 0);
108 AMDGPU::IsaVersion ISA = AMDGPU::getIsaVersion(STI->getFeatureBits());
109 TS->EmitDirectiveHSACodeObjectISA(ISA.Major, ISA.Minor, ISA.Stepping,
110 "AMD", "AMDGPU");
111}
112
Tom Stellardf151a452015-06-26 21:14:58 +0000113void AMDGPUAsmPrinter::EmitFunctionBodyStart() {
114 const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
115 SIProgramInfo KernelInfo;
116 if (STM.isAmdHsaOS()) {
117 getSIProgramInfo(KernelInfo, *MF);
118 EmitAmdKernelCodeT(*MF, KernelInfo);
119 }
120}
121
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000122void AMDGPUAsmPrinter::EmitFunctionEntryLabel() {
123 const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
124 const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
125 if (MFI->isKernel() && STM.isAmdHsaOS()) {
126 AMDGPUTargetStreamer *TS =
127 static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
128 TS->EmitAMDGPUSymbolType(CurrentFnSym->getName(),
129 ELF::STT_AMDGPU_HSA_KERNEL);
130 }
131
132 AsmPrinter::EmitFunctionEntryLabel();
133}
134
Tom Stellard00f2f912015-12-02 19:47:57 +0000135static bool isModuleLinkage(const GlobalValue *GV) {
136 switch (GV->getLinkage()) {
Konstantin Zhuravlyove63e02c2016-04-05 16:00:58 +0000137 case GlobalValue::LinkOnceODRLinkage:
138 case GlobalValue::LinkOnceAnyLinkage:
Tom Stellard00f2f912015-12-02 19:47:57 +0000139 case GlobalValue::InternalLinkage:
140 case GlobalValue::CommonLinkage:
141 return true;
142 case GlobalValue::ExternalLinkage:
143 return false;
144 default: llvm_unreachable("unknown linkage type");
145 }
146}
147
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000148void AMDGPUAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
149
Tom Stellard29dd05e2015-12-15 22:39:36 +0000150 if (TM.getTargetTriple().getOS() != Triple::AMDHSA) {
151 AsmPrinter::EmitGlobalVariable(GV);
152 return;
153 }
154
155 if (GV->isDeclaration() || GV->getLinkage() == GlobalValue::PrivateLinkage) {
Tom Stellard00f2f912015-12-02 19:47:57 +0000156 AsmPrinter::EmitGlobalVariable(GV);
157 return;
158 }
159
160 // Group segment variables aren't emitted in HSA.
161 if (AMDGPU::isGroupSegment(GV))
162 return;
163
164 AMDGPUTargetStreamer *TS =
165 static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
166 if (isModuleLinkage(GV)) {
167 TS->EmitAMDGPUHsaModuleScopeGlobal(GV->getName());
168 } else {
169 TS->EmitAMDGPUHsaProgramScopeGlobal(GV->getName());
170 }
171
Tom Stellard4c4c72d2016-01-08 14:50:28 +0000172 MCSymbolELF *GVSym = cast<MCSymbolELF>(getSymbol(GV));
Tom Stellard00f2f912015-12-02 19:47:57 +0000173 const DataLayout &DL = getDataLayout();
Tom Stellard4c4c72d2016-01-08 14:50:28 +0000174
175 // Emit the size
176 uint64_t Size = DL.getTypeAllocSize(GV->getType()->getElementType());
177 OutStreamer->emitELFSize(GVSym, MCConstantExpr::create(Size, OutContext));
Tom Stellard00f2f912015-12-02 19:47:57 +0000178 OutStreamer->PushSection();
179 OutStreamer->SwitchSection(
180 getObjFileLowering().SectionForGlobal(GV, *Mang, TM));
Tom Stellard00f2f912015-12-02 19:47:57 +0000181 const Constant *C = GV->getInitializer();
182 OutStreamer->EmitLabel(GVSym);
183 EmitGlobalConstant(DL, C);
184 OutStreamer->PopSection();
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000185}
186
Tom Stellard45bb48e2015-06-13 03:28:10 +0000187bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
188
189 // The starting address of all shader programs must be 256 bytes aligned.
190 MF.setAlignment(8);
191
192 SetupMachineFunction(MF);
193
194 MCContext &Context = getObjFileLowering().getContext();
195 MCSectionELF *ConfigSection =
196 Context.getELFSection(".AMDGPU.config", ELF::SHT_PROGBITS, 0);
197 OutStreamer->SwitchSection(ConfigSection);
198
199 const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
200 SIProgramInfo KernelInfo;
Tom Stellardf151a452015-06-26 21:14:58 +0000201 if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault297ae312015-08-15 00:12:39 +0000202 getSIProgramInfo(KernelInfo, MF);
Tom Stellardf151a452015-06-26 21:14:58 +0000203 if (!STM.isAmdHsaOS()) {
Tom Stellardf151a452015-06-26 21:14:58 +0000204 EmitProgramInfoSI(MF, KernelInfo);
205 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000206 } else {
207 EmitProgramInfoR600(MF);
208 }
209
210 DisasmLines.clear();
211 HexLines.clear();
212 DisasmLineMaxLen = 0;
213
214 EmitFunctionBody();
215
216 if (isVerbose()) {
217 MCSectionELF *CommentSection =
218 Context.getELFSection(".AMDGPU.csdata", ELF::SHT_PROGBITS, 0);
219 OutStreamer->SwitchSection(CommentSection);
220
221 if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
222 OutStreamer->emitRawComment(" Kernel info:", false);
223 OutStreamer->emitRawComment(" codeLenInByte = " + Twine(KernelInfo.CodeLen),
224 false);
225 OutStreamer->emitRawComment(" NumSgprs: " + Twine(KernelInfo.NumSGPR),
226 false);
227 OutStreamer->emitRawComment(" NumVgprs: " + Twine(KernelInfo.NumVGPR),
228 false);
229 OutStreamer->emitRawComment(" FloatMode: " + Twine(KernelInfo.FloatMode),
230 false);
231 OutStreamer->emitRawComment(" IeeeMode: " + Twine(KernelInfo.IEEEMode),
232 false);
233 OutStreamer->emitRawComment(" ScratchSize: " + Twine(KernelInfo.ScratchSize),
234 false);
Matt Arsenaultd41c0db2015-11-05 05:27:07 +0000235
Matt Arsenaultd41c0db2015-11-05 05:27:07 +0000236 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:USER_SGPR: " +
Matt Arsenault8246d4a2015-11-11 00:27:46 +0000237 Twine(G_00B84C_USER_SGPR(KernelInfo.ComputePGMRSrc2)),
Matt Arsenaultd41c0db2015-11-05 05:27:07 +0000238 false);
Matt Arsenault8246d4a2015-11-11 00:27:46 +0000239 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_X_EN: " +
240 Twine(G_00B84C_TGID_X_EN(KernelInfo.ComputePGMRSrc2)),
241 false);
242 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_Y_EN: " +
243 Twine(G_00B84C_TGID_Y_EN(KernelInfo.ComputePGMRSrc2)),
244 false);
245 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_Z_EN: " +
246 Twine(G_00B84C_TGID_Z_EN(KernelInfo.ComputePGMRSrc2)),
247 false);
248 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: " +
249 Twine(G_00B84C_TIDIG_COMP_CNT(KernelInfo.ComputePGMRSrc2)),
250 false);
251
Tom Stellard45bb48e2015-06-13 03:28:10 +0000252 } else {
253 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
254 OutStreamer->emitRawComment(
255 Twine("SQ_PGM_RESOURCES:STACK_SIZE = " + Twine(MFI->StackSize)));
256 }
257 }
258
259 if (STM.dumpCode()) {
260
261 OutStreamer->SwitchSection(
262 Context.getELFSection(".AMDGPU.disasm", ELF::SHT_NOTE, 0));
263
264 for (size_t i = 0; i < DisasmLines.size(); ++i) {
265 std::string Comment(DisasmLineMaxLen - DisasmLines[i].size(), ' ');
266 Comment += " ; " + HexLines[i] + "\n";
267
268 OutStreamer->EmitBytes(StringRef(DisasmLines[i]));
269 OutStreamer->EmitBytes(StringRef(Comment));
270 }
271 }
272
273 return false;
274}
275
276void AMDGPUAsmPrinter::EmitProgramInfoR600(const MachineFunction &MF) {
277 unsigned MaxGPR = 0;
278 bool killPixel = false;
279 const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
280 const R600RegisterInfo *RI =
281 static_cast<const R600RegisterInfo *>(STM.getRegisterInfo());
282 const R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
283
284 for (const MachineBasicBlock &MBB : MF) {
285 for (const MachineInstr &MI : MBB) {
286 if (MI.getOpcode() == AMDGPU::KILLGT)
287 killPixel = true;
288 unsigned numOperands = MI.getNumOperands();
289 for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
290 const MachineOperand &MO = MI.getOperand(op_idx);
291 if (!MO.isReg())
292 continue;
293 unsigned HWReg = RI->getEncodingValue(MO.getReg()) & 0xff;
294
295 // Register with value > 127 aren't GPR
296 if (HWReg > 127)
297 continue;
298 MaxGPR = std::max(MaxGPR, HWReg);
299 }
300 }
301 }
302
303 unsigned RsrcReg;
304 if (STM.getGeneration() >= AMDGPUSubtarget::EVERGREEN) {
305 // Evergreen / Northern Islands
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000306 switch (MF.getFunction()->getCallingConv()) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000307 default: // Fall through
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000308 case CallingConv::AMDGPU_CS: RsrcReg = R_0288D4_SQ_PGM_RESOURCES_LS; break;
309 case CallingConv::AMDGPU_GS: RsrcReg = R_028878_SQ_PGM_RESOURCES_GS; break;
310 case CallingConv::AMDGPU_PS: RsrcReg = R_028844_SQ_PGM_RESOURCES_PS; break;
311 case CallingConv::AMDGPU_VS: RsrcReg = R_028860_SQ_PGM_RESOURCES_VS; break;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000312 }
313 } else {
314 // R600 / R700
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000315 switch (MF.getFunction()->getCallingConv()) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000316 default: // Fall through
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000317 case CallingConv::AMDGPU_GS: // Fall through
318 case CallingConv::AMDGPU_CS: // Fall through
319 case CallingConv::AMDGPU_VS: RsrcReg = R_028868_SQ_PGM_RESOURCES_VS; break;
320 case CallingConv::AMDGPU_PS: RsrcReg = R_028850_SQ_PGM_RESOURCES_PS; break;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000321 }
322 }
323
324 OutStreamer->EmitIntValue(RsrcReg, 4);
325 OutStreamer->EmitIntValue(S_NUM_GPRS(MaxGPR + 1) |
326 S_STACK_SIZE(MFI->StackSize), 4);
327 OutStreamer->EmitIntValue(R_02880C_DB_SHADER_CONTROL, 4);
328 OutStreamer->EmitIntValue(S_02880C_KILL_ENABLE(killPixel), 4);
329
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000330 if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000331 OutStreamer->EmitIntValue(R_0288E8_SQ_LDS_ALLOC, 4);
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000332 OutStreamer->EmitIntValue(alignTo(MFI->LDSSize, 4) >> 2, 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000333 }
334}
335
336void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo,
337 const MachineFunction &MF) const {
338 const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
339 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
340 uint64_t CodeSize = 0;
341 unsigned MaxSGPR = 0;
342 unsigned MaxVGPR = 0;
343 bool VCCUsed = false;
344 bool FlatUsed = false;
345 const SIRegisterInfo *RI =
346 static_cast<const SIRegisterInfo *>(STM.getRegisterInfo());
347
348 for (const MachineBasicBlock &MBB : MF) {
349 for (const MachineInstr &MI : MBB) {
350 // TODO: CodeSize should account for multiple functions.
Matt Arsenaultc5746862015-08-12 09:04:44 +0000351
352 // TODO: Should we count size of debug info?
353 if (MI.isDebugValue())
354 continue;
355
356 // FIXME: This is reporting 0 for many instructions.
Tom Stellard45bb48e2015-06-13 03:28:10 +0000357 CodeSize += MI.getDesc().Size;
358
359 unsigned numOperands = MI.getNumOperands();
360 for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
361 const MachineOperand &MO = MI.getOperand(op_idx);
362 unsigned width = 0;
363 bool isSGPR = false;
364
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000365 if (!MO.isReg())
Tom Stellard45bb48e2015-06-13 03:28:10 +0000366 continue;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000367
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000368 unsigned reg = MO.getReg();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000369 switch (reg) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000370 case AMDGPU::EXEC:
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000371 case AMDGPU::SCC:
Tom Stellard45bb48e2015-06-13 03:28:10 +0000372 case AMDGPU::M0:
373 continue;
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000374
375 case AMDGPU::VCC:
376 case AMDGPU::VCC_LO:
377 case AMDGPU::VCC_HI:
378 VCCUsed = true;
379 continue;
380
381 case AMDGPU::FLAT_SCR:
382 case AMDGPU::FLAT_SCR_LO:
383 case AMDGPU::FLAT_SCR_HI:
384 FlatUsed = true;
385 continue;
386
Artem Tamazoveb4d5a92016-04-13 16:18:41 +0000387 case AMDGPU::TBA:
388 case AMDGPU::TBA_LO:
389 case AMDGPU::TBA_HI:
390 case AMDGPU::TMA:
391 case AMDGPU::TMA_LO:
392 case AMDGPU::TMA_HI:
393 llvm_unreachable("Trap Handler registers should not be used");
394 continue;
395
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000396 default:
397 break;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000398 }
399
400 if (AMDGPU::SReg_32RegClass.contains(reg)) {
Artem Tamazoveb4d5a92016-04-13 16:18:41 +0000401 if (AMDGPU::TTMP_32RegClass.contains(reg)) {
402 llvm_unreachable("Trap Handler registers should not be used");
403 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000404 isSGPR = true;
405 width = 1;
406 } else if (AMDGPU::VGPR_32RegClass.contains(reg)) {
407 isSGPR = false;
408 width = 1;
409 } else if (AMDGPU::SReg_64RegClass.contains(reg)) {
Artem Tamazoveb4d5a92016-04-13 16:18:41 +0000410 if (AMDGPU::TTMP_64RegClass.contains(reg)) {
411 llvm_unreachable("Trap Handler registers should not be used");
412 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000413 isSGPR = true;
414 width = 2;
415 } else if (AMDGPU::VReg_64RegClass.contains(reg)) {
416 isSGPR = false;
417 width = 2;
418 } else if (AMDGPU::VReg_96RegClass.contains(reg)) {
419 isSGPR = false;
420 width = 3;
421 } else if (AMDGPU::SReg_128RegClass.contains(reg)) {
422 isSGPR = true;
423 width = 4;
424 } else if (AMDGPU::VReg_128RegClass.contains(reg)) {
425 isSGPR = false;
426 width = 4;
427 } else if (AMDGPU::SReg_256RegClass.contains(reg)) {
428 isSGPR = true;
429 width = 8;
430 } else if (AMDGPU::VReg_256RegClass.contains(reg)) {
431 isSGPR = false;
432 width = 8;
433 } else if (AMDGPU::SReg_512RegClass.contains(reg)) {
434 isSGPR = true;
435 width = 16;
436 } else if (AMDGPU::VReg_512RegClass.contains(reg)) {
437 isSGPR = false;
438 width = 16;
439 } else {
440 llvm_unreachable("Unknown register class");
441 }
442 unsigned hwReg = RI->getEncodingValue(reg) & 0xff;
443 unsigned maxUsed = hwReg + width - 1;
444 if (isSGPR) {
445 MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR;
446 } else {
447 MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR;
448 }
449 }
450 }
451 }
452
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000453 unsigned ExtraSGPRs = 0;
454
455 if (VCCUsed)
456 ExtraSGPRs = 2;
457
458 if (STM.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS) {
459 if (FlatUsed)
460 ExtraSGPRs = 4;
461 } else {
462 if (STM.isXNACKEnabled())
463 ExtraSGPRs = 4;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000464
Nicolai Haehnle5b504972016-01-04 23:35:53 +0000465 if (FlatUsed)
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000466 ExtraSGPRs = 6;
Tom Stellardcaaa3aa2015-12-17 17:05:09 +0000467 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000468
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000469 MaxSGPR += ExtraSGPRs;
470
Tom Stellard45bb48e2015-06-13 03:28:10 +0000471 // We found the maximum register index. They start at 0, so add one to get the
472 // number of registers.
473 ProgInfo.NumVGPR = MaxVGPR + 1;
474 ProgInfo.NumSGPR = MaxSGPR + 1;
475
476 if (STM.hasSGPRInitBug()) {
Matt Arsenault417c93e2015-06-17 20:55:25 +0000477 if (ProgInfo.NumSGPR > AMDGPUSubtarget::FIXED_SGPR_COUNT_FOR_INIT_BUG) {
478 LLVMContext &Ctx = MF.getFunction()->getContext();
479 Ctx.emitError("too many SGPRs used with the SGPR init bug");
480 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000481
482 ProgInfo.NumSGPR = AMDGPUSubtarget::FIXED_SGPR_COUNT_FOR_INIT_BUG;
483 }
484
Matt Arsenault41003af2015-11-30 21:16:07 +0000485 if (MFI->NumUserSGPRs > STM.getMaxNumUserSGPRs()) {
486 LLVMContext &Ctx = MF.getFunction()->getContext();
487 Ctx.emitError("too many user SGPRs used");
488 }
489
Tom Stellard45bb48e2015-06-13 03:28:10 +0000490 ProgInfo.VGPRBlocks = (ProgInfo.NumVGPR - 1) / 4;
491 ProgInfo.SGPRBlocks = (ProgInfo.NumSGPR - 1) / 8;
492 // Set the value to initialize FP_ROUND and FP_DENORM parts of the mode
493 // register.
494 ProgInfo.FloatMode = getFPMode(MF);
495
Tom Stellard45bb48e2015-06-13 03:28:10 +0000496 ProgInfo.IEEEMode = 0;
497
Matt Arsenault7293f982016-01-28 20:53:35 +0000498 // Make clamp modifier on NaN input returns 0.
499 ProgInfo.DX10Clamp = 1;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000500
501 const MachineFrameInfo *FrameInfo = MF.getFrameInfo();
Matt Arsenaultf4dfc1a2016-03-01 04:58:20 +0000502 ProgInfo.ScratchSize = FrameInfo->getStackSize();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000503
504 ProgInfo.FlatUsed = FlatUsed;
505 ProgInfo.VCCUsed = VCCUsed;
506 ProgInfo.CodeLen = CodeSize;
507
508 unsigned LDSAlignShift;
509 if (STM.getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) {
510 // LDS is allocated in 64 dword blocks.
511 LDSAlignShift = 8;
512 } else {
513 // LDS is allocated in 128 dword blocks.
514 LDSAlignShift = 9;
515 }
516
517 unsigned LDSSpillSize = MFI->LDSWaveSpillSize *
518 MFI->getMaximumWorkGroupSize(MF);
519
520 ProgInfo.LDSSize = MFI->LDSSize + LDSSpillSize;
521 ProgInfo.LDSBlocks =
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +0000522 alignTo(ProgInfo.LDSSize, 1ULL << LDSAlignShift) >> LDSAlignShift;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000523
524 // Scratch is allocated in 256 dword blocks.
525 unsigned ScratchAlignShift = 10;
526 // We need to program the hardware with the amount of scratch memory that
527 // is used by the entire wave. ProgInfo.ScratchSize is the amount of
528 // scratch memory used per thread.
529 ProgInfo.ScratchBlocks =
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000530 alignTo(ProgInfo.ScratchSize * STM.getWavefrontSize(),
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +0000531 1ULL << ScratchAlignShift) >>
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000532 ScratchAlignShift;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000533
534 ProgInfo.ComputePGMRSrc1 =
535 S_00B848_VGPRS(ProgInfo.VGPRBlocks) |
536 S_00B848_SGPRS(ProgInfo.SGPRBlocks) |
537 S_00B848_PRIORITY(ProgInfo.Priority) |
538 S_00B848_FLOAT_MODE(ProgInfo.FloatMode) |
539 S_00B848_PRIV(ProgInfo.Priv) |
540 S_00B848_DX10_CLAMP(ProgInfo.DX10Clamp) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000541 S_00B848_DEBUG_MODE(ProgInfo.DebugMode) |
Tom Stellard45bb48e2015-06-13 03:28:10 +0000542 S_00B848_IEEE_MODE(ProgInfo.IEEEMode);
543
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000544 // 0 = X, 1 = XY, 2 = XYZ
545 unsigned TIDIGCompCnt = 0;
546 if (MFI->hasWorkItemIDZ())
547 TIDIGCompCnt = 2;
548 else if (MFI->hasWorkItemIDY())
549 TIDIGCompCnt = 1;
550
Tom Stellard45bb48e2015-06-13 03:28:10 +0000551 ProgInfo.ComputePGMRSrc2 =
552 S_00B84C_SCRATCH_EN(ProgInfo.ScratchBlocks > 0) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000553 S_00B84C_USER_SGPR(MFI->getNumUserSGPRs()) |
554 S_00B84C_TGID_X_EN(MFI->hasWorkGroupIDX()) |
555 S_00B84C_TGID_Y_EN(MFI->hasWorkGroupIDY()) |
556 S_00B84C_TGID_Z_EN(MFI->hasWorkGroupIDZ()) |
557 S_00B84C_TG_SIZE_EN(MFI->hasWorkGroupInfo()) |
558 S_00B84C_TIDIG_COMP_CNT(TIDIGCompCnt) |
559 S_00B84C_EXCP_EN_MSB(0) |
560 S_00B84C_LDS_SIZE(ProgInfo.LDSBlocks) |
561 S_00B84C_EXCP_EN(0);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000562}
563
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000564static unsigned getRsrcReg(CallingConv::ID CallConv) {
565 switch (CallConv) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000566 default: // Fall through
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000567 case CallingConv::AMDGPU_CS: return R_00B848_COMPUTE_PGM_RSRC1;
568 case CallingConv::AMDGPU_GS: return R_00B228_SPI_SHADER_PGM_RSRC1_GS;
569 case CallingConv::AMDGPU_PS: return R_00B028_SPI_SHADER_PGM_RSRC1_PS;
570 case CallingConv::AMDGPU_VS: return R_00B128_SPI_SHADER_PGM_RSRC1_VS;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000571 }
572}
573
574void AMDGPUAsmPrinter::EmitProgramInfoSI(const MachineFunction &MF,
575 const SIProgramInfo &KernelInfo) {
576 const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
577 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000578 unsigned RsrcReg = getRsrcReg(MF.getFunction()->getCallingConv());
Tom Stellard45bb48e2015-06-13 03:28:10 +0000579
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000580 if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000581 OutStreamer->EmitIntValue(R_00B848_COMPUTE_PGM_RSRC1, 4);
582
583 OutStreamer->EmitIntValue(KernelInfo.ComputePGMRSrc1, 4);
584
585 OutStreamer->EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4);
586 OutStreamer->EmitIntValue(KernelInfo.ComputePGMRSrc2, 4);
587
588 OutStreamer->EmitIntValue(R_00B860_COMPUTE_TMPRING_SIZE, 4);
589 OutStreamer->EmitIntValue(S_00B860_WAVESIZE(KernelInfo.ScratchBlocks), 4);
590
591 // TODO: Should probably note flat usage somewhere. SC emits a "FlatPtr32 =
592 // 0" comment but I don't see a corresponding field in the register spec.
593 } else {
594 OutStreamer->EmitIntValue(RsrcReg, 4);
595 OutStreamer->EmitIntValue(S_00B028_VGPRS(KernelInfo.VGPRBlocks) |
596 S_00B028_SGPRS(KernelInfo.SGPRBlocks), 4);
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000597 if (STM.isVGPRSpillingEnabled(*MF.getFunction())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000598 OutStreamer->EmitIntValue(R_0286E8_SPI_TMPRING_SIZE, 4);
599 OutStreamer->EmitIntValue(S_0286E8_WAVESIZE(KernelInfo.ScratchBlocks), 4);
600 }
601 }
602
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000603 if (MF.getFunction()->getCallingConv() == CallingConv::AMDGPU_PS) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000604 OutStreamer->EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4);
605 OutStreamer->EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(KernelInfo.LDSBlocks), 4);
606 OutStreamer->EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000607 OutStreamer->EmitIntValue(MFI->PSInputEna, 4);
608 OutStreamer->EmitIntValue(R_0286D0_SPI_PS_INPUT_ADDR, 4);
609 OutStreamer->EmitIntValue(MFI->getPSInputAddr(), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000610 }
611}
612
Matt Arsenault24ee0782016-02-12 02:40:47 +0000613// This is supposed to be log2(Size)
614static amd_element_byte_size_t getElementByteSizeValue(unsigned Size) {
615 switch (Size) {
616 case 4:
617 return AMD_ELEMENT_4_BYTES;
618 case 8:
619 return AMD_ELEMENT_8_BYTES;
620 case 16:
621 return AMD_ELEMENT_16_BYTES;
622 default:
623 llvm_unreachable("invalid private_element_size");
624 }
625}
626
Tom Stellard45bb48e2015-06-13 03:28:10 +0000627void AMDGPUAsmPrinter::EmitAmdKernelCodeT(const MachineFunction &MF,
Tom Stellardff7416b2015-06-26 21:58:31 +0000628 const SIProgramInfo &KernelInfo) const {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000629 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
630 const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
631 amd_kernel_code_t header;
632
Tom Stellardff7416b2015-06-26 21:58:31 +0000633 AMDGPU::initDefaultAMDKernelCodeT(header, STM.getFeatureBits());
Tom Stellard45bb48e2015-06-13 03:28:10 +0000634
635 header.compute_pgm_resource_registers =
636 KernelInfo.ComputePGMRSrc1 |
637 (KernelInfo.ComputePGMRSrc2 << 32);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000638 header.code_properties = AMD_CODE_PROPERTY_IS_PTR64;
639
Matt Arsenault24ee0782016-02-12 02:40:47 +0000640
641 AMD_HSA_BITS_SET(header.code_properties,
642 AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE,
643 getElementByteSizeValue(STM.getMaxPrivateElementSize()));
644
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000645 if (MFI->hasPrivateSegmentBuffer()) {
646 header.code_properties |=
647 AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER;
648 }
649
650 if (MFI->hasDispatchPtr())
651 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
652
653 if (MFI->hasQueuePtr())
654 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR;
655
656 if (MFI->hasKernargSegmentPtr())
657 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR;
658
659 if (MFI->hasDispatchID())
660 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID;
661
662 if (MFI->hasFlatScratchInit())
663 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT;
664
665 // TODO: Private segment size
666
667 if (MFI->hasGridWorkgroupCountX()) {
668 header.code_properties |=
669 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X;
670 }
671
672 if (MFI->hasGridWorkgroupCountY()) {
673 header.code_properties |=
674 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y;
675 }
676
677 if (MFI->hasGridWorkgroupCountZ()) {
678 header.code_properties |=
679 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z;
680 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000681
Tom Stellard48f29f22015-11-26 00:43:29 +0000682 if (MFI->hasDispatchPtr())
683 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
684
Nicolai Haehnle5b504972016-01-04 23:35:53 +0000685 if (STM.isXNACKEnabled())
686 header.code_properties |= AMD_CODE_PROPERTY_IS_XNACK_SUPPORTED;
687
Tom Stellard45bb48e2015-06-13 03:28:10 +0000688 header.kernarg_segment_byte_size = MFI->ABIArgOffset;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000689 header.wavefront_sgpr_count = KernelInfo.NumSGPR;
690 header.workitem_vgpr_count = KernelInfo.NumVGPR;
Tom Stellarda4953072015-12-15 22:55:30 +0000691 header.workitem_private_segment_byte_size = KernelInfo.ScratchSize;
Tom Stellard7750f4e2015-12-15 23:15:25 +0000692 header.workgroup_group_segment_byte_size = KernelInfo.LDSSize;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000693
Tom Stellardff7416b2015-06-26 21:58:31 +0000694 AMDGPUTargetStreamer *TS =
695 static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
696 TS->EmitAMDKernelCodeT(header);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000697}
698
699bool AMDGPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
700 unsigned AsmVariant,
701 const char *ExtraCode, raw_ostream &O) {
702 if (ExtraCode && ExtraCode[0]) {
703 if (ExtraCode[1] != 0)
704 return true; // Unknown modifier.
705
706 switch (ExtraCode[0]) {
707 default:
708 // See if this is a generic print operand
709 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
710 case 'r':
711 break;
712 }
713 }
714
715 AMDGPUInstPrinter::printRegOperand(MI->getOperand(OpNo).getReg(), O,
716 *TM.getSubtargetImpl(*MF->getFunction())->getRegisterInfo());
717 return false;
718}