blob: 5126cccb0dcb7ebf8faf4b1fc0617de459865ed1 [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"
42
43using namespace llvm;
44
45// TODO: This should get the default rounding mode from the kernel. We just set
46// the default here, but this could change if the OpenCL rounding mode pragmas
47// are used.
48//
49// The denormal mode here should match what is reported by the OpenCL runtime
50// for the CL_FP_DENORM bit from CL_DEVICE_{HALF|SINGLE|DOUBLE}_FP_CONFIG, but
51// can also be override to flush with the -cl-denorms-are-zero compiler flag.
52//
53// AMD OpenCL only sets flush none and reports CL_FP_DENORM for double
54// precision, and leaves single precision to flush all and does not report
55// CL_FP_DENORM for CL_DEVICE_SINGLE_FP_CONFIG. Mesa's OpenCL currently reports
56// CL_FP_DENORM for both.
57//
58// FIXME: It seems some instructions do not support single precision denormals
59// regardless of the mode (exp_*_f32, rcp_*_f32, rsq_*_f32, rsq_*f32, sqrt_f32,
60// and sin_f32, cos_f32 on most parts).
61
62// We want to use these instructions, and using fp32 denormals also causes
63// instructions to run at the double precision rate for the device so it's
64// probably best to just report no single precision denormals.
65static uint32_t getFPMode(const MachineFunction &F) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +000066 const SISubtarget& ST = F.getSubtarget<SISubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +000067 // TODO: Is there any real use for the flush in only / flush out only modes?
68
69 uint32_t FP32Denormals =
70 ST.hasFP32Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
71
72 uint32_t FP64Denormals =
73 ST.hasFP64Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
74
75 return FP_ROUND_MODE_SP(FP_ROUND_ROUND_TO_NEAREST) |
76 FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_NEAREST) |
77 FP_DENORM_MODE_SP(FP32Denormals) |
78 FP_DENORM_MODE_DP(FP64Denormals);
79}
80
81static AsmPrinter *
82createAMDGPUAsmPrinterPass(TargetMachine &tm,
83 std::unique_ptr<MCStreamer> &&Streamer) {
84 return new AMDGPUAsmPrinter(tm, std::move(Streamer));
85}
86
87extern "C" void LLVMInitializeAMDGPUAsmPrinter() {
Mehdi Aminif42454b2016-10-09 23:00:34 +000088 TargetRegistry::RegisterAsmPrinter(getTheAMDGPUTarget(),
89 createAMDGPUAsmPrinterPass);
90 TargetRegistry::RegisterAsmPrinter(getTheGCNTarget(),
91 createAMDGPUAsmPrinterPass);
Tom Stellard45bb48e2015-06-13 03:28:10 +000092}
93
94AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM,
95 std::unique_ptr<MCStreamer> Streamer)
Matt Arsenault11f74022016-10-06 17:19:11 +000096 : AsmPrinter(TM, std::move(Streamer)) {}
Tom Stellard45bb48e2015-06-13 03:28:10 +000097
Mehdi Amini117296c2016-10-01 02:56:57 +000098StringRef AMDGPUAsmPrinter::getPassName() const {
Matt Arsenaultf9245b72016-07-22 17:01:25 +000099 return "AMDGPU Assembly Printer";
100}
101
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000102const MCSubtargetInfo* AMDGPUAsmPrinter::getSTI() const {
103 return TM.getMCSubtargetInfo();
104}
105
106AMDGPUTargetStreamer& AMDGPUAsmPrinter::getTargetStreamer() const {
107 return static_cast<AMDGPUTargetStreamer&>(*OutStreamer->getTargetStreamer());
108}
109
Tom Stellardf4218372016-01-12 17:18:17 +0000110void AMDGPUAsmPrinter::EmitStartOfAsmFile(Module &M) {
111 if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
112 return;
113
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000114 AMDGPU::IsaInfo::IsaVersion ISA =
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000115 AMDGPU::IsaInfo::getIsaVersion(getSTI()->getFeatureBits());
Yaxun Liud6fbe652016-11-10 21:18:49 +0000116
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000117 getTargetStreamer().EmitDirectiveHSACodeObjectVersion(2, 1);
118 getTargetStreamer().EmitDirectiveHSACodeObjectISA(
119 ISA.Major, ISA.Minor, ISA.Stepping, "AMD", "AMDGPU");
120 getTargetStreamer().EmitStartOfCodeObjectMetadata(
121 getSTI()->getFeatureBits(), M);
122}
123
124void AMDGPUAsmPrinter::EmitEndOfAsmFile(Module &M) {
125 if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
126 return;
127
128 getTargetStreamer().EmitEndOfCodeObjectMetadata(getSTI()->getFeatureBits());
Tom Stellardf4218372016-01-12 17:18:17 +0000129}
130
Matt Arsenault6bc43d82016-10-06 16:20:41 +0000131bool AMDGPUAsmPrinter::isBlockOnlyReachableByFallthrough(
132 const MachineBasicBlock *MBB) const {
133 if (!AsmPrinter::isBlockOnlyReachableByFallthrough(MBB))
134 return false;
135
136 if (MBB->empty())
137 return true;
138
139 // If this is a block implementing a long branch, an expression relative to
140 // the start of the block is needed. to the start of the block.
141 // XXX - Is there a smarter way to check this?
142 return (MBB->back().getOpcode() != AMDGPU::S_SETPC_B64);
143}
144
Tom Stellardf151a452015-06-26 21:14:58 +0000145void AMDGPUAsmPrinter::EmitFunctionBodyStart() {
146 const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
147 SIProgramInfo KernelInfo;
Tom Stellard2f3f9852017-01-25 01:25:13 +0000148 if (STM.isAmdCodeObjectV2(*MF)) {
Tom Stellardf151a452015-06-26 21:14:58 +0000149 getSIProgramInfo(KernelInfo, *MF);
150 EmitAmdKernelCodeT(*MF, KernelInfo);
151 }
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000152
153 if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
154 return;
155 getTargetStreamer().EmitKernelCodeObjectMetadata(*MF->getFunction());
Tom Stellardf151a452015-06-26 21:14:58 +0000156}
157
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000158void AMDGPUAsmPrinter::EmitFunctionEntryLabel() {
159 const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
160 const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
Tom Stellard2f3f9852017-01-25 01:25:13 +0000161 if (MFI->isKernel() && STM.isAmdCodeObjectV2(*MF)) {
Tom Stellard1b9748c2016-09-26 17:29:25 +0000162 SmallString<128> SymbolName;
163 getNameWithPrefix(SymbolName, MF->getFunction()),
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000164 getTargetStreamer().EmitAMDGPUSymbolType(
165 SymbolName, ELF::STT_AMDGPU_HSA_KERNEL);
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000166 }
167
168 AsmPrinter::EmitFunctionEntryLabel();
169}
170
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000171void AMDGPUAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
172
Tom Stellard00f2f912015-12-02 19:47:57 +0000173 // Group segment variables aren't emitted in HSA.
174 if (AMDGPU::isGroupSegment(GV))
175 return;
176
Tom Stellardfcfaea42016-05-05 17:03:33 +0000177 AsmPrinter::EmitGlobalVariable(GV);
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000178}
179
Tom Stellard45bb48e2015-06-13 03:28:10 +0000180bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
181
182 // The starting address of all shader programs must be 256 bytes aligned.
183 MF.setAlignment(8);
184
185 SetupMachineFunction(MF);
186
Tom Stellard45bb48e2015-06-13 03:28:10 +0000187 const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
Konstantin Zhuravlyov67a6d542017-01-06 17:02:10 +0000188 MCContext &Context = getObjFileLowering().getContext();
189 if (!STM.isAmdHsaOS()) {
190 MCSectionELF *ConfigSection =
191 Context.getELFSection(".AMDGPU.config", ELF::SHT_PROGBITS, 0);
192 OutStreamer->SwitchSection(ConfigSection);
193 }
194
Tom Stellard45bb48e2015-06-13 03:28:10 +0000195 SIProgramInfo KernelInfo;
Tom Stellardf151a452015-06-26 21:14:58 +0000196 if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
Matt Arsenault297ae312015-08-15 00:12:39 +0000197 getSIProgramInfo(KernelInfo, MF);
Tom Stellardf151a452015-06-26 21:14:58 +0000198 if (!STM.isAmdHsaOS()) {
Tom Stellardf151a452015-06-26 21:14:58 +0000199 EmitProgramInfoSI(MF, KernelInfo);
200 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000201 } else {
202 EmitProgramInfoR600(MF);
203 }
204
205 DisasmLines.clear();
206 HexLines.clear();
207 DisasmLineMaxLen = 0;
208
209 EmitFunctionBody();
210
211 if (isVerbose()) {
212 MCSectionELF *CommentSection =
213 Context.getELFSection(".AMDGPU.csdata", ELF::SHT_PROGBITS, 0);
214 OutStreamer->SwitchSection(CommentSection);
215
216 if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
217 OutStreamer->emitRawComment(" Kernel info:", false);
218 OutStreamer->emitRawComment(" codeLenInByte = " + Twine(KernelInfo.CodeLen),
219 false);
220 OutStreamer->emitRawComment(" NumSgprs: " + Twine(KernelInfo.NumSGPR),
221 false);
222 OutStreamer->emitRawComment(" NumVgprs: " + Twine(KernelInfo.NumVGPR),
223 false);
224 OutStreamer->emitRawComment(" FloatMode: " + Twine(KernelInfo.FloatMode),
225 false);
226 OutStreamer->emitRawComment(" IeeeMode: " + Twine(KernelInfo.IEEEMode),
227 false);
228 OutStreamer->emitRawComment(" ScratchSize: " + Twine(KernelInfo.ScratchSize),
229 false);
Matt Arsenaultfd8ab092016-04-14 22:11:51 +0000230 OutStreamer->emitRawComment(" LDSByteSize: " + Twine(KernelInfo.LDSSize) +
231 " bytes/workgroup (compile time only)", false);
Matt Arsenaultd41c0db2015-11-05 05:27:07 +0000232
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000233 OutStreamer->emitRawComment(" SGPRBlocks: " +
234 Twine(KernelInfo.SGPRBlocks), false);
235 OutStreamer->emitRawComment(" VGPRBlocks: " +
236 Twine(KernelInfo.VGPRBlocks), false);
237
238 OutStreamer->emitRawComment(" NumSGPRsForWavesPerEU: " +
239 Twine(KernelInfo.NumSGPRsForWavesPerEU), false);
240 OutStreamer->emitRawComment(" NumVGPRsForWavesPerEU: " +
241 Twine(KernelInfo.NumVGPRsForWavesPerEU), false);
242
Konstantin Zhuravlyov1d99c4d2016-04-26 15:43:14 +0000243 OutStreamer->emitRawComment(" ReservedVGPRFirst: " + Twine(KernelInfo.ReservedVGPRFirst),
244 false);
245 OutStreamer->emitRawComment(" ReservedVGPRCount: " + Twine(KernelInfo.ReservedVGPRCount),
246 false);
247
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000248 if (MF.getSubtarget<SISubtarget>().debuggerEmitPrologue()) {
249 OutStreamer->emitRawComment(" DebuggerWavefrontPrivateSegmentOffsetSGPR: s" +
250 Twine(KernelInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR), false);
251 OutStreamer->emitRawComment(" DebuggerPrivateSegmentBufferSGPR: s" +
252 Twine(KernelInfo.DebuggerPrivateSegmentBufferSGPR), false);
253 }
254
Matt Arsenaultd41c0db2015-11-05 05:27:07 +0000255 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:USER_SGPR: " +
Matt Arsenault8246d4a2015-11-11 00:27:46 +0000256 Twine(G_00B84C_USER_SGPR(KernelInfo.ComputePGMRSrc2)),
Matt Arsenaultd41c0db2015-11-05 05:27:07 +0000257 false);
Wei Ding205bfdb2017-02-10 02:15:29 +0000258 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TRAP_HANDLER: " +
259 Twine(G_00B84C_TRAP_HANDLER(KernelInfo.ComputePGMRSrc2)),
260 false);
Matt Arsenault8246d4a2015-11-11 00:27:46 +0000261 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_X_EN: " +
262 Twine(G_00B84C_TGID_X_EN(KernelInfo.ComputePGMRSrc2)),
263 false);
264 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_Y_EN: " +
265 Twine(G_00B84C_TGID_Y_EN(KernelInfo.ComputePGMRSrc2)),
266 false);
267 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_Z_EN: " +
268 Twine(G_00B84C_TGID_Z_EN(KernelInfo.ComputePGMRSrc2)),
269 false);
270 OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: " +
271 Twine(G_00B84C_TIDIG_COMP_CNT(KernelInfo.ComputePGMRSrc2)),
272 false);
273
Tom Stellard45bb48e2015-06-13 03:28:10 +0000274 } else {
275 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
276 OutStreamer->emitRawComment(
Matt Arsenaultf9245b72016-07-22 17:01:25 +0000277 Twine("SQ_PGM_RESOURCES:STACK_SIZE = " + Twine(MFI->CFStackSize)));
Tom Stellard45bb48e2015-06-13 03:28:10 +0000278 }
279 }
280
281 if (STM.dumpCode()) {
282
283 OutStreamer->SwitchSection(
284 Context.getELFSection(".AMDGPU.disasm", ELF::SHT_NOTE, 0));
285
286 for (size_t i = 0; i < DisasmLines.size(); ++i) {
287 std::string Comment(DisasmLineMaxLen - DisasmLines[i].size(), ' ');
288 Comment += " ; " + HexLines[i] + "\n";
289
290 OutStreamer->EmitBytes(StringRef(DisasmLines[i]));
291 OutStreamer->EmitBytes(StringRef(Comment));
292 }
293 }
294
295 return false;
296}
297
298void AMDGPUAsmPrinter::EmitProgramInfoR600(const MachineFunction &MF) {
299 unsigned MaxGPR = 0;
300 bool killPixel = false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000301 const R600Subtarget &STM = MF.getSubtarget<R600Subtarget>();
302 const R600RegisterInfo *RI = STM.getRegisterInfo();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000303 const R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
304
305 for (const MachineBasicBlock &MBB : MF) {
306 for (const MachineInstr &MI : MBB) {
307 if (MI.getOpcode() == AMDGPU::KILLGT)
308 killPixel = true;
309 unsigned numOperands = MI.getNumOperands();
310 for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
311 const MachineOperand &MO = MI.getOperand(op_idx);
312 if (!MO.isReg())
313 continue;
314 unsigned HWReg = RI->getEncodingValue(MO.getReg()) & 0xff;
315
316 // Register with value > 127 aren't GPR
317 if (HWReg > 127)
318 continue;
319 MaxGPR = std::max(MaxGPR, HWReg);
320 }
321 }
322 }
323
324 unsigned RsrcReg;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000325 if (STM.getGeneration() >= R600Subtarget::EVERGREEN) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000326 // Evergreen / Northern Islands
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000327 switch (MF.getFunction()->getCallingConv()) {
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000328 default: LLVM_FALLTHROUGH;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000329 case CallingConv::AMDGPU_CS: RsrcReg = R_0288D4_SQ_PGM_RESOURCES_LS; break;
330 case CallingConv::AMDGPU_GS: RsrcReg = R_028878_SQ_PGM_RESOURCES_GS; break;
331 case CallingConv::AMDGPU_PS: RsrcReg = R_028844_SQ_PGM_RESOURCES_PS; break;
332 case CallingConv::AMDGPU_VS: RsrcReg = R_028860_SQ_PGM_RESOURCES_VS; break;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000333 }
334 } else {
335 // R600 / R700
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000336 switch (MF.getFunction()->getCallingConv()) {
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000337 default: LLVM_FALLTHROUGH;
338 case CallingConv::AMDGPU_GS: LLVM_FALLTHROUGH;
339 case CallingConv::AMDGPU_CS: LLVM_FALLTHROUGH;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000340 case CallingConv::AMDGPU_VS: RsrcReg = R_028868_SQ_PGM_RESOURCES_VS; break;
341 case CallingConv::AMDGPU_PS: RsrcReg = R_028850_SQ_PGM_RESOURCES_PS; break;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000342 }
343 }
344
345 OutStreamer->EmitIntValue(RsrcReg, 4);
346 OutStreamer->EmitIntValue(S_NUM_GPRS(MaxGPR + 1) |
Matt Arsenaultf9245b72016-07-22 17:01:25 +0000347 S_STACK_SIZE(MFI->CFStackSize), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000348 OutStreamer->EmitIntValue(R_02880C_DB_SHADER_CONTROL, 4);
349 OutStreamer->EmitIntValue(S_02880C_KILL_ENABLE(killPixel), 4);
350
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000351 if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000352 OutStreamer->EmitIntValue(R_0288E8_SQ_LDS_ALLOC, 4);
Matt Arsenault52ef4012016-07-26 16:45:58 +0000353 OutStreamer->EmitIntValue(alignTo(MFI->getLDSSize(), 4) >> 2, 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000354 }
355}
356
357void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo,
358 const MachineFunction &MF) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000359 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000360 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
361 uint64_t CodeSize = 0;
362 unsigned MaxSGPR = 0;
363 unsigned MaxVGPR = 0;
364 bool VCCUsed = false;
365 bool FlatUsed = false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000366 const SIRegisterInfo *RI = STM.getRegisterInfo();
367 const SIInstrInfo *TII = STM.getInstrInfo();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000368
369 for (const MachineBasicBlock &MBB : MF) {
370 for (const MachineInstr &MI : MBB) {
371 // TODO: CodeSize should account for multiple functions.
Matt Arsenaultc5746862015-08-12 09:04:44 +0000372
373 // TODO: Should we count size of debug info?
374 if (MI.isDebugValue())
375 continue;
376
Matt Arsenault10c17ca2016-10-06 10:13:23 +0000377 if (isVerbose())
378 CodeSize += TII->getInstSizeInBytes(MI);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000379
380 unsigned numOperands = MI.getNumOperands();
381 for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
382 const MachineOperand &MO = MI.getOperand(op_idx);
383 unsigned width = 0;
384 bool isSGPR = false;
385
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000386 if (!MO.isReg())
Tom Stellard45bb48e2015-06-13 03:28:10 +0000387 continue;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000388
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000389 unsigned reg = MO.getReg();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000390 switch (reg) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000391 case AMDGPU::EXEC:
Nicolai Haehnle74839372016-04-19 21:58:17 +0000392 case AMDGPU::EXEC_LO:
393 case AMDGPU::EXEC_HI:
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000394 case AMDGPU::SCC:
Tom Stellard45bb48e2015-06-13 03:28:10 +0000395 case AMDGPU::M0:
Matt Arsenaulte823d922017-02-18 18:29:53 +0000396 case AMDGPU::SRC_SHARED_BASE:
397 case AMDGPU::SRC_SHARED_LIMIT:
398 case AMDGPU::SRC_PRIVATE_BASE:
399 case AMDGPU::SRC_PRIVATE_LIMIT:
Tom Stellard45bb48e2015-06-13 03:28:10 +0000400 continue;
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000401
402 case AMDGPU::VCC:
403 case AMDGPU::VCC_LO:
404 case AMDGPU::VCC_HI:
405 VCCUsed = true;
406 continue;
407
408 case AMDGPU::FLAT_SCR:
409 case AMDGPU::FLAT_SCR_LO:
410 case AMDGPU::FLAT_SCR_HI:
Marek Olsak693e9be2016-12-09 19:49:48 +0000411 // Even if FLAT_SCRATCH is implicitly used, it has no effect if flat
412 // instructions aren't used to access the scratch buffer.
413 if (MFI->hasFlatScratchInit())
414 FlatUsed = true;
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000415 continue;
416
Artem Tamazoveb4d5a92016-04-13 16:18:41 +0000417 case AMDGPU::TBA:
418 case AMDGPU::TBA_LO:
419 case AMDGPU::TBA_HI:
420 case AMDGPU::TMA:
421 case AMDGPU::TMA_LO:
422 case AMDGPU::TMA_HI:
Matt Arsenaultb06db8f2016-07-26 21:03:36 +0000423 llvm_unreachable("trap handler registers should not be used");
Artem Tamazoveb4d5a92016-04-13 16:18:41 +0000424
Matt Arsenaultd2c75892015-10-01 21:51:59 +0000425 default:
426 break;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000427 }
428
429 if (AMDGPU::SReg_32RegClass.contains(reg)) {
Matt Arsenaultb06db8f2016-07-26 21:03:36 +0000430 assert(!AMDGPU::TTMP_32RegClass.contains(reg) &&
431 "trap handler registers should not be used");
Tom Stellard45bb48e2015-06-13 03:28:10 +0000432 isSGPR = true;
433 width = 1;
434 } else if (AMDGPU::VGPR_32RegClass.contains(reg)) {
435 isSGPR = false;
436 width = 1;
437 } else if (AMDGPU::SReg_64RegClass.contains(reg)) {
Matt Arsenaultb06db8f2016-07-26 21:03:36 +0000438 assert(!AMDGPU::TTMP_64RegClass.contains(reg) &&
439 "trap handler registers should not be used");
Tom Stellard45bb48e2015-06-13 03:28:10 +0000440 isSGPR = true;
441 width = 2;
442 } else if (AMDGPU::VReg_64RegClass.contains(reg)) {
443 isSGPR = false;
444 width = 2;
445 } else if (AMDGPU::VReg_96RegClass.contains(reg)) {
446 isSGPR = false;
447 width = 3;
448 } else if (AMDGPU::SReg_128RegClass.contains(reg)) {
449 isSGPR = true;
450 width = 4;
451 } else if (AMDGPU::VReg_128RegClass.contains(reg)) {
452 isSGPR = false;
453 width = 4;
454 } else if (AMDGPU::SReg_256RegClass.contains(reg)) {
455 isSGPR = true;
456 width = 8;
457 } else if (AMDGPU::VReg_256RegClass.contains(reg)) {
458 isSGPR = false;
459 width = 8;
460 } else if (AMDGPU::SReg_512RegClass.contains(reg)) {
461 isSGPR = true;
462 width = 16;
463 } else if (AMDGPU::VReg_512RegClass.contains(reg)) {
464 isSGPR = false;
465 width = 16;
466 } else {
467 llvm_unreachable("Unknown register class");
468 }
469 unsigned hwReg = RI->getEncodingValue(reg) & 0xff;
470 unsigned maxUsed = hwReg + width - 1;
471 if (isSGPR) {
472 MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR;
473 } else {
474 MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR;
475 }
476 }
477 }
478 }
479
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000480 unsigned ExtraSGPRs = 0;
481
482 if (VCCUsed)
483 ExtraSGPRs = 2;
484
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000485 if (STM.getGeneration() < SISubtarget::VOLCANIC_ISLANDS) {
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000486 if (FlatUsed)
487 ExtraSGPRs = 4;
488 } else {
489 if (STM.isXNACKEnabled())
490 ExtraSGPRs = 4;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000491
Nicolai Haehnle5b504972016-01-04 23:35:53 +0000492 if (FlatUsed)
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000493 ExtraSGPRs = 6;
Tom Stellardcaaa3aa2015-12-17 17:05:09 +0000494 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000495
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000496 unsigned ExtraVGPRs = STM.getReservedNumVGPRs(MF);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000497
Marek Olsak91f22fb2016-12-09 19:49:40 +0000498 // Check the addressable register limit before we add ExtraSGPRs.
499 if (STM.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS &&
500 !STM.hasSGPRInitBug()) {
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000501 unsigned MaxAddressableNumSGPRs = STM.getAddressableNumSGPRs();
Marek Olsak91f22fb2016-12-09 19:49:40 +0000502 if (MaxSGPR + 1 > MaxAddressableNumSGPRs) {
503 // This can happen due to a compiler bug or when using inline asm.
504 LLVMContext &Ctx = MF.getFunction()->getContext();
505 DiagnosticInfoResourceLimit Diag(*MF.getFunction(),
506 "addressable scalar registers",
507 MaxSGPR + 1, DS_Error,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000508 DK_ResourceLimit,
509 MaxAddressableNumSGPRs);
Marek Olsak91f22fb2016-12-09 19:49:40 +0000510 Ctx.diagnose(Diag);
511 MaxSGPR = MaxAddressableNumSGPRs - 1;
512 }
513 }
514
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000515 // Account for extra SGPRs and VGPRs reserved for debugger use.
516 MaxSGPR += ExtraSGPRs;
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000517 MaxVGPR += ExtraVGPRs;
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000518
Tom Stellard45bb48e2015-06-13 03:28:10 +0000519 // We found the maximum register index. They start at 0, so add one to get the
520 // number of registers.
Tom Stellard45bb48e2015-06-13 03:28:10 +0000521 ProgInfo.NumSGPR = MaxSGPR + 1;
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000522 ProgInfo.NumVGPR = MaxVGPR + 1;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000523
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000524 // Adjust number of registers used to meet default/requested minimum/maximum
525 // number of waves per execution unit request.
526 ProgInfo.NumSGPRsForWavesPerEU = std::max(
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000527 ProgInfo.NumSGPR, STM.getMinNumSGPRs(MFI->getMaxWavesPerEU()));
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000528 ProgInfo.NumVGPRsForWavesPerEU = std::max(
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000529 ProgInfo.NumVGPR, STM.getMinNumVGPRs(MFI->getMaxWavesPerEU()));
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000530
Marek Olsak91f22fb2016-12-09 19:49:40 +0000531 if (STM.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ||
532 STM.hasSGPRInitBug()) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000533 unsigned MaxAddressableNumSGPRs = STM.getAddressableNumSGPRs();
534 if (ProgInfo.NumSGPR > MaxAddressableNumSGPRs) {
535 // This can happen due to a compiler bug or when using inline asm to use
536 // the registers which are usually reserved for vcc etc.
Marek Olsak91f22fb2016-12-09 19:49:40 +0000537 LLVMContext &Ctx = MF.getFunction()->getContext();
538 DiagnosticInfoResourceLimit Diag(*MF.getFunction(),
539 "scalar registers",
540 ProgInfo.NumSGPR, DS_Error,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000541 DK_ResourceLimit,
542 MaxAddressableNumSGPRs);
Marek Olsak91f22fb2016-12-09 19:49:40 +0000543 Ctx.diagnose(Diag);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000544 ProgInfo.NumSGPR = MaxAddressableNumSGPRs;
545 ProgInfo.NumSGPRsForWavesPerEU = MaxAddressableNumSGPRs;
Marek Olsak91f22fb2016-12-09 19:49:40 +0000546 }
Matt Arsenault4eae3012016-10-28 20:31:47 +0000547 }
548
549 if (STM.hasSGPRInitBug()) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000550 ProgInfo.NumSGPR =
551 AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG;
552 ProgInfo.NumSGPRsForWavesPerEU =
553 AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000554 }
555
Matt Arsenault41003af2015-11-30 21:16:07 +0000556 if (MFI->NumUserSGPRs > STM.getMaxNumUserSGPRs()) {
557 LLVMContext &Ctx = MF.getFunction()->getContext();
Matt Arsenaultff982412016-06-20 18:13:04 +0000558 DiagnosticInfoResourceLimit Diag(*MF.getFunction(), "user SGPRs",
559 MFI->NumUserSGPRs, DS_Error);
560 Ctx.diagnose(Diag);
Matt Arsenault41003af2015-11-30 21:16:07 +0000561 }
562
Matt Arsenault52ef4012016-07-26 16:45:58 +0000563 if (MFI->getLDSSize() > static_cast<unsigned>(STM.getLocalMemorySize())) {
Matt Arsenault1c4d0ef2016-04-28 19:37:35 +0000564 LLVMContext &Ctx = MF.getFunction()->getContext();
Matt Arsenaultff982412016-06-20 18:13:04 +0000565 DiagnosticInfoResourceLimit Diag(*MF.getFunction(), "local memory",
Matt Arsenault52ef4012016-07-26 16:45:58 +0000566 MFI->getLDSSize(), DS_Error);
Matt Arsenaultff982412016-06-20 18:13:04 +0000567 Ctx.diagnose(Diag);
Matt Arsenault1c4d0ef2016-04-28 19:37:35 +0000568 }
569
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000570 // SGPRBlocks is actual number of SGPR blocks minus 1.
571 ProgInfo.SGPRBlocks = alignTo(ProgInfo.NumSGPRsForWavesPerEU,
Konstantin Zhuravlyove22fbcb2017-02-08 13:18:40 +0000572 STM.getSGPREncodingGranule());
573 ProgInfo.SGPRBlocks = ProgInfo.SGPRBlocks / STM.getSGPREncodingGranule() - 1;
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000574
575 // VGPRBlocks is actual number of VGPR blocks minus 1.
576 ProgInfo.VGPRBlocks = alignTo(ProgInfo.NumVGPRsForWavesPerEU,
Konstantin Zhuravlyove22fbcb2017-02-08 13:18:40 +0000577 STM.getVGPREncodingGranule());
578 ProgInfo.VGPRBlocks = ProgInfo.VGPRBlocks / STM.getVGPREncodingGranule() - 1;
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000579
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000580 // Record first reserved VGPR and number of reserved VGPRs.
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000581 ProgInfo.ReservedVGPRFirst = STM.debuggerReserveRegs() ? MaxVGPR + 1 : 0;
582 ProgInfo.ReservedVGPRCount = STM.getReservedNumVGPRs(MF);
583
584 // Update DebuggerWavefrontPrivateSegmentOffsetSGPR and
585 // DebuggerPrivateSegmentBufferSGPR fields if "amdgpu-debugger-emit-prologue"
586 // attribute was requested.
587 if (STM.debuggerEmitPrologue()) {
588 ProgInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR =
589 RI->getHWRegIndex(MFI->getScratchWaveOffsetReg());
590 ProgInfo.DebuggerPrivateSegmentBufferSGPR =
591 RI->getHWRegIndex(MFI->getScratchRSrcReg());
592 }
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000593
Tom Stellard45bb48e2015-06-13 03:28:10 +0000594 // Set the value to initialize FP_ROUND and FP_DENORM parts of the mode
595 // register.
596 ProgInfo.FloatMode = getFPMode(MF);
597
Wei Ding3cb2a1e2016-10-19 22:34:49 +0000598 ProgInfo.IEEEMode = STM.enableIEEEBit(MF);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000599
Matt Arsenault7293f982016-01-28 20:53:35 +0000600 // Make clamp modifier on NaN input returns 0.
Matt Arsenault2fdf2a12017-02-21 23:35:48 +0000601 ProgInfo.DX10Clamp = STM.enableDX10Clamp();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000602
Matthias Braun941a7052016-07-28 18:40:00 +0000603 const MachineFrameInfo &FrameInfo = MF.getFrameInfo();
604 ProgInfo.ScratchSize = FrameInfo.getStackSize();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000605
606 ProgInfo.FlatUsed = FlatUsed;
607 ProgInfo.VCCUsed = VCCUsed;
608 ProgInfo.CodeLen = CodeSize;
609
610 unsigned LDSAlignShift;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000611 if (STM.getGeneration() < SISubtarget::SEA_ISLANDS) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000612 // LDS is allocated in 64 dword blocks.
613 LDSAlignShift = 8;
614 } else {
615 // LDS is allocated in 128 dword blocks.
616 LDSAlignShift = 9;
617 }
618
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000619 unsigned LDSSpillSize =
620 MFI->LDSWaveSpillSize * MFI->getMaxFlatWorkGroupSize();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000621
Matt Arsenault52ef4012016-07-26 16:45:58 +0000622 ProgInfo.LDSSize = MFI->getLDSSize() + LDSSpillSize;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000623 ProgInfo.LDSBlocks =
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +0000624 alignTo(ProgInfo.LDSSize, 1ULL << LDSAlignShift) >> LDSAlignShift;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000625
626 // Scratch is allocated in 256 dword blocks.
627 unsigned ScratchAlignShift = 10;
628 // We need to program the hardware with the amount of scratch memory that
629 // is used by the entire wave. ProgInfo.ScratchSize is the amount of
630 // scratch memory used per thread.
631 ProgInfo.ScratchBlocks =
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000632 alignTo(ProgInfo.ScratchSize * STM.getWavefrontSize(),
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +0000633 1ULL << ScratchAlignShift) >>
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000634 ScratchAlignShift;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000635
636 ProgInfo.ComputePGMRSrc1 =
637 S_00B848_VGPRS(ProgInfo.VGPRBlocks) |
638 S_00B848_SGPRS(ProgInfo.SGPRBlocks) |
639 S_00B848_PRIORITY(ProgInfo.Priority) |
640 S_00B848_FLOAT_MODE(ProgInfo.FloatMode) |
641 S_00B848_PRIV(ProgInfo.Priv) |
642 S_00B848_DX10_CLAMP(ProgInfo.DX10Clamp) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000643 S_00B848_DEBUG_MODE(ProgInfo.DebugMode) |
Tom Stellard45bb48e2015-06-13 03:28:10 +0000644 S_00B848_IEEE_MODE(ProgInfo.IEEEMode);
645
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000646 // 0 = X, 1 = XY, 2 = XYZ
647 unsigned TIDIGCompCnt = 0;
648 if (MFI->hasWorkItemIDZ())
649 TIDIGCompCnt = 2;
650 else if (MFI->hasWorkItemIDY())
651 TIDIGCompCnt = 1;
652
Tom Stellard45bb48e2015-06-13 03:28:10 +0000653 ProgInfo.ComputePGMRSrc2 =
654 S_00B84C_SCRATCH_EN(ProgInfo.ScratchBlocks > 0) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000655 S_00B84C_USER_SGPR(MFI->getNumUserSGPRs()) |
Wei Ding205bfdb2017-02-10 02:15:29 +0000656 S_00B84C_TRAP_HANDLER(STM.isTrapHandlerEnabled()) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000657 S_00B84C_TGID_X_EN(MFI->hasWorkGroupIDX()) |
658 S_00B84C_TGID_Y_EN(MFI->hasWorkGroupIDY()) |
659 S_00B84C_TGID_Z_EN(MFI->hasWorkGroupIDZ()) |
660 S_00B84C_TG_SIZE_EN(MFI->hasWorkGroupInfo()) |
661 S_00B84C_TIDIG_COMP_CNT(TIDIGCompCnt) |
662 S_00B84C_EXCP_EN_MSB(0) |
663 S_00B84C_LDS_SIZE(ProgInfo.LDSBlocks) |
664 S_00B84C_EXCP_EN(0);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000665}
666
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000667static unsigned getRsrcReg(CallingConv::ID CallConv) {
668 switch (CallConv) {
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000669 default: LLVM_FALLTHROUGH;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000670 case CallingConv::AMDGPU_CS: return R_00B848_COMPUTE_PGM_RSRC1;
671 case CallingConv::AMDGPU_GS: return R_00B228_SPI_SHADER_PGM_RSRC1_GS;
672 case CallingConv::AMDGPU_PS: return R_00B028_SPI_SHADER_PGM_RSRC1_PS;
673 case CallingConv::AMDGPU_VS: return R_00B128_SPI_SHADER_PGM_RSRC1_VS;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000674 }
675}
676
677void AMDGPUAsmPrinter::EmitProgramInfoSI(const MachineFunction &MF,
678 const SIProgramInfo &KernelInfo) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000679 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000680 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000681 unsigned RsrcReg = getRsrcReg(MF.getFunction()->getCallingConv());
Tom Stellard45bb48e2015-06-13 03:28:10 +0000682
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000683 if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000684 OutStreamer->EmitIntValue(R_00B848_COMPUTE_PGM_RSRC1, 4);
685
686 OutStreamer->EmitIntValue(KernelInfo.ComputePGMRSrc1, 4);
687
688 OutStreamer->EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4);
689 OutStreamer->EmitIntValue(KernelInfo.ComputePGMRSrc2, 4);
690
691 OutStreamer->EmitIntValue(R_00B860_COMPUTE_TMPRING_SIZE, 4);
692 OutStreamer->EmitIntValue(S_00B860_WAVESIZE(KernelInfo.ScratchBlocks), 4);
693
694 // TODO: Should probably note flat usage somewhere. SC emits a "FlatPtr32 =
695 // 0" comment but I don't see a corresponding field in the register spec.
696 } else {
697 OutStreamer->EmitIntValue(RsrcReg, 4);
698 OutStreamer->EmitIntValue(S_00B028_VGPRS(KernelInfo.VGPRBlocks) |
699 S_00B028_SGPRS(KernelInfo.SGPRBlocks), 4);
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000700 if (STM.isVGPRSpillingEnabled(*MF.getFunction())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000701 OutStreamer->EmitIntValue(R_0286E8_SPI_TMPRING_SIZE, 4);
702 OutStreamer->EmitIntValue(S_0286E8_WAVESIZE(KernelInfo.ScratchBlocks), 4);
703 }
704 }
705
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000706 if (MF.getFunction()->getCallingConv() == CallingConv::AMDGPU_PS) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000707 OutStreamer->EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4);
708 OutStreamer->EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(KernelInfo.LDSBlocks), 4);
709 OutStreamer->EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4);
Marek Olsakfccabaf2016-01-13 11:45:36 +0000710 OutStreamer->EmitIntValue(MFI->PSInputEna, 4);
711 OutStreamer->EmitIntValue(R_0286D0_SPI_PS_INPUT_ADDR, 4);
712 OutStreamer->EmitIntValue(MFI->getPSInputAddr(), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000713 }
Marek Olsak0532c192016-07-13 17:35:15 +0000714
715 OutStreamer->EmitIntValue(R_SPILLED_SGPRS, 4);
716 OutStreamer->EmitIntValue(MFI->getNumSpilledSGPRs(), 4);
717 OutStreamer->EmitIntValue(R_SPILLED_VGPRS, 4);
718 OutStreamer->EmitIntValue(MFI->getNumSpilledVGPRs(), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000719}
720
Matt Arsenault24ee0782016-02-12 02:40:47 +0000721// This is supposed to be log2(Size)
722static amd_element_byte_size_t getElementByteSizeValue(unsigned Size) {
723 switch (Size) {
724 case 4:
725 return AMD_ELEMENT_4_BYTES;
726 case 8:
727 return AMD_ELEMENT_8_BYTES;
728 case 16:
729 return AMD_ELEMENT_16_BYTES;
730 default:
731 llvm_unreachable("invalid private_element_size");
732 }
733}
734
Tom Stellard45bb48e2015-06-13 03:28:10 +0000735void AMDGPUAsmPrinter::EmitAmdKernelCodeT(const MachineFunction &MF,
Tom Stellardff7416b2015-06-26 21:58:31 +0000736 const SIProgramInfo &KernelInfo) const {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000737 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000738 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000739 amd_kernel_code_t header;
740
Tom Stellardff7416b2015-06-26 21:58:31 +0000741 AMDGPU::initDefaultAMDKernelCodeT(header, STM.getFeatureBits());
Tom Stellard45bb48e2015-06-13 03:28:10 +0000742
743 header.compute_pgm_resource_registers =
744 KernelInfo.ComputePGMRSrc1 |
745 (KernelInfo.ComputePGMRSrc2 << 32);
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000746 header.code_properties = AMD_CODE_PROPERTY_IS_PTR64;
747
Matt Arsenault24ee0782016-02-12 02:40:47 +0000748
749 AMD_HSA_BITS_SET(header.code_properties,
750 AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE,
751 getElementByteSizeValue(STM.getMaxPrivateElementSize()));
752
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000753 if (MFI->hasPrivateSegmentBuffer()) {
754 header.code_properties |=
755 AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER;
756 }
757
758 if (MFI->hasDispatchPtr())
759 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
760
761 if (MFI->hasQueuePtr())
762 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR;
763
764 if (MFI->hasKernargSegmentPtr())
765 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR;
766
767 if (MFI->hasDispatchID())
768 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID;
769
770 if (MFI->hasFlatScratchInit())
771 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT;
772
773 // TODO: Private segment size
774
775 if (MFI->hasGridWorkgroupCountX()) {
776 header.code_properties |=
777 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X;
778 }
779
780 if (MFI->hasGridWorkgroupCountY()) {
781 header.code_properties |=
782 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y;
783 }
784
785 if (MFI->hasGridWorkgroupCountZ()) {
786 header.code_properties |=
787 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z;
788 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000789
Tom Stellard48f29f22015-11-26 00:43:29 +0000790 if (MFI->hasDispatchPtr())
791 header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
792
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000793 if (STM.debuggerSupported())
794 header.code_properties |= AMD_CODE_PROPERTY_IS_DEBUG_SUPPORTED;
795
Nicolai Haehnle5b504972016-01-04 23:35:53 +0000796 if (STM.isXNACKEnabled())
797 header.code_properties |= AMD_CODE_PROPERTY_IS_XNACK_SUPPORTED;
798
Matt Arsenault52ef4012016-07-26 16:45:58 +0000799 // FIXME: Should use getKernArgSize
Tom Stellarde88bbc32016-09-23 01:33:26 +0000800 header.kernarg_segment_byte_size =
Tom Stellard2f3f9852017-01-25 01:25:13 +0000801 STM.getKernArgSegmentSize(MF, MFI->getABIArgOffset());
Tom Stellard45bb48e2015-06-13 03:28:10 +0000802 header.wavefront_sgpr_count = KernelInfo.NumSGPR;
803 header.workitem_vgpr_count = KernelInfo.NumVGPR;
Tom Stellarda4953072015-12-15 22:55:30 +0000804 header.workitem_private_segment_byte_size = KernelInfo.ScratchSize;
Tom Stellard7750f4e2015-12-15 23:15:25 +0000805 header.workgroup_group_segment_byte_size = KernelInfo.LDSSize;
Konstantin Zhuravlyov1d99c4d2016-04-26 15:43:14 +0000806 header.reserved_vgpr_first = KernelInfo.ReservedVGPRFirst;
807 header.reserved_vgpr_count = KernelInfo.ReservedVGPRCount;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000808
Tom Stellard175959e2016-12-06 21:53:10 +0000809 // These alignment values are specified in powers of two, so alignment =
810 // 2^n. The minimum alignment is 2^4 = 16.
811 header.kernarg_segment_alignment = std::max((size_t)4,
812 countTrailingZeros(MFI->getMaxKernArgAlign()));
813
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000814 if (STM.debuggerEmitPrologue()) {
815 header.debug_wavefront_private_segment_offset_sgpr =
816 KernelInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR;
817 header.debug_private_segment_buffer_sgpr =
818 KernelInfo.DebuggerPrivateSegmentBufferSGPR;
819 }
820
Tom Stellardfcfaea42016-05-05 17:03:33 +0000821 OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000822 getTargetStreamer().EmitAMDKernelCodeT(header);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000823}
824
825bool AMDGPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
826 unsigned AsmVariant,
827 const char *ExtraCode, raw_ostream &O) {
828 if (ExtraCode && ExtraCode[0]) {
829 if (ExtraCode[1] != 0)
830 return true; // Unknown modifier.
831
832 switch (ExtraCode[0]) {
833 default:
834 // See if this is a generic print operand
835 return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
836 case 'r':
837 break;
838 }
839 }
840
841 AMDGPUInstPrinter::printRegOperand(MI->getOperand(OpNo).getReg(), O,
842 *TM.getSubtargetImpl(*MF->getFunction())->getRegisterInfo());
843 return false;
844}