blob: 142543cc47e3068958a9513495791b904ae5ba8e [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 Stellard45bb48e2015-06-13 03:28:10 +000020#include "AMDGPU.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000021#include "AMDGPUSubtarget.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000022#include "AMDGPUTargetMachine.h"
23#include "InstPrinter/AMDGPUInstPrinter.h"
24#include "MCTargetDesc/AMDGPUTargetStreamer.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000025#include "R600Defines.h"
26#include "R600MachineFunctionInfo.h"
27#include "R600RegisterInfo.h"
28#include "SIDefines.h"
Matt Arsenaulta9720c62016-06-20 17:51:32 +000029#include "SIInstrInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000030#include "SIMachineFunctionInfo.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000031#include "SIRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000032#include "Utils/AMDGPUBaseInfo.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000033#include "llvm/BinaryFormat/ELF.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000034#include "llvm/CodeGen/MachineFrameInfo.h"
Matt Arsenaultff982412016-06-20 18:13:04 +000035#include "llvm/IR/DiagnosticInfo.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000036#include "llvm/MC/MCContext.h"
37#include "llvm/MC/MCSectionELF.h"
38#include "llvm/MC/MCStreamer.h"
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +000039#include "llvm/Support/AMDGPUMetadata.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000040#include "llvm/Support/MathExtras.h"
41#include "llvm/Support/TargetRegistry.h"
42#include "llvm/Target/TargetLoweringObjectFile.h"
43
44using namespace llvm;
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +000045using namespace llvm::AMDGPU;
Tom Stellard45bb48e2015-06-13 03:28:10 +000046
47// TODO: This should get the default rounding mode from the kernel. We just set
48// the default here, but this could change if the OpenCL rounding mode pragmas
49// are used.
50//
51// The denormal mode here should match what is reported by the OpenCL runtime
52// for the CL_FP_DENORM bit from CL_DEVICE_{HALF|SINGLE|DOUBLE}_FP_CONFIG, but
53// can also be override to flush with the -cl-denorms-are-zero compiler flag.
54//
55// AMD OpenCL only sets flush none and reports CL_FP_DENORM for double
56// precision, and leaves single precision to flush all and does not report
57// CL_FP_DENORM for CL_DEVICE_SINGLE_FP_CONFIG. Mesa's OpenCL currently reports
58// CL_FP_DENORM for both.
59//
60// FIXME: It seems some instructions do not support single precision denormals
61// regardless of the mode (exp_*_f32, rcp_*_f32, rsq_*_f32, rsq_*f32, sqrt_f32,
62// and sin_f32, cos_f32 on most parts).
63
64// We want to use these instructions, and using fp32 denormals also causes
65// instructions to run at the double precision rate for the device so it's
66// probably best to just report no single precision denormals.
67static uint32_t getFPMode(const MachineFunction &F) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +000068 const SISubtarget& ST = F.getSubtarget<SISubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +000069 // TODO: Is there any real use for the flush in only / flush out only modes?
70
71 uint32_t FP32Denormals =
72 ST.hasFP32Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
73
74 uint32_t FP64Denormals =
75 ST.hasFP64Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
76
77 return FP_ROUND_MODE_SP(FP_ROUND_ROUND_TO_NEAREST) |
78 FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_NEAREST) |
79 FP_DENORM_MODE_SP(FP32Denormals) |
80 FP_DENORM_MODE_DP(FP64Denormals);
81}
82
83static AsmPrinter *
84createAMDGPUAsmPrinterPass(TargetMachine &tm,
85 std::unique_ptr<MCStreamer> &&Streamer) {
86 return new AMDGPUAsmPrinter(tm, std::move(Streamer));
87}
88
89extern "C" void LLVMInitializeAMDGPUAsmPrinter() {
Mehdi Aminif42454b2016-10-09 23:00:34 +000090 TargetRegistry::RegisterAsmPrinter(getTheAMDGPUTarget(),
91 createAMDGPUAsmPrinterPass);
92 TargetRegistry::RegisterAsmPrinter(getTheGCNTarget(),
93 createAMDGPUAsmPrinterPass);
Tom Stellard45bb48e2015-06-13 03:28:10 +000094}
95
96AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM,
97 std::unique_ptr<MCStreamer> Streamer)
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000098 : AsmPrinter(TM, std::move(Streamer)) {
99 AMDGPUASI = static_cast<AMDGPUTargetMachine*>(&TM)->getAMDGPUAS();
100 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000101
Mehdi Amini117296c2016-10-01 02:56:57 +0000102StringRef AMDGPUAsmPrinter::getPassName() const {
Matt Arsenaultf9245b72016-07-22 17:01:25 +0000103 return "AMDGPU Assembly Printer";
104}
105
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000106const MCSubtargetInfo* AMDGPUAsmPrinter::getSTI() const {
107 return TM.getMCSubtargetInfo();
108}
109
110AMDGPUTargetStreamer& AMDGPUAsmPrinter::getTargetStreamer() const {
111 return static_cast<AMDGPUTargetStreamer&>(*OutStreamer->getTargetStreamer());
112}
113
Tom Stellardf4218372016-01-12 17:18:17 +0000114void AMDGPUAsmPrinter::EmitStartOfAsmFile(Module &M) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000115 AMDGPU::IsaInfo::IsaVersion ISA =
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000116 AMDGPU::IsaInfo::getIsaVersion(getSTI()->getFeatureBits());
Yaxun Liud6fbe652016-11-10 21:18:49 +0000117
Tim Renouf72800f02017-10-03 19:03:52 +0000118 if (TM.getTargetTriple().getOS() == Triple::AMDPAL) {
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000119 readPALMetadata(M);
Tim Renouf72800f02017-10-03 19:03:52 +0000120 // AMDPAL wants an HSA_ISA .note.
121 getTargetStreamer().EmitDirectiveHSACodeObjectISA(
122 ISA.Major, ISA.Minor, ISA.Stepping, "AMD", "AMDGPU");
123 }
124 if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
125 return;
126
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000127 getTargetStreamer().EmitDirectiveHSACodeObjectVersion(2, 1);
128 getTargetStreamer().EmitDirectiveHSACodeObjectISA(
129 ISA.Major, ISA.Minor, ISA.Stepping, "AMD", "AMDGPU");
Konstantin Zhuravlyov516651b2017-10-11 22:59:35 +0000130
131 HSAMetadataStream.begin(M);
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000132}
133
134void AMDGPUAsmPrinter::EmitEndOfAsmFile(Module &M) {
Tim Renouf72800f02017-10-03 19:03:52 +0000135 if (TM.getTargetTriple().getOS() == Triple::AMDPAL) {
136 // Copy the PAL metadata from the map where we collected it into a vector,
137 // then write it as a .note.
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000138 PALMD::Metadata PALMetadataVector;
139 for (auto i : PALMetadataMap) {
140 PALMetadataVector.push_back(i.first);
141 PALMetadataVector.push_back(i.second);
Tim Renouf72800f02017-10-03 19:03:52 +0000142 }
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000143 getTargetStreamer().EmitPALMetadata(PALMetadataVector);
Tim Renouf72800f02017-10-03 19:03:52 +0000144 }
145
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000146 if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
147 return;
148
Konstantin Zhuravlyov516651b2017-10-11 22:59:35 +0000149 HSAMetadataStream.end();
150 getTargetStreamer().EmitHSAMetadata(HSAMetadataStream.getHSAMetadata());
Tom Stellardf4218372016-01-12 17:18:17 +0000151}
152
Matt Arsenault6bc43d82016-10-06 16:20:41 +0000153bool AMDGPUAsmPrinter::isBlockOnlyReachableByFallthrough(
154 const MachineBasicBlock *MBB) const {
155 if (!AsmPrinter::isBlockOnlyReachableByFallthrough(MBB))
156 return false;
157
158 if (MBB->empty())
159 return true;
160
161 // If this is a block implementing a long branch, an expression relative to
162 // the start of the block is needed. to the start of the block.
163 // XXX - Is there a smarter way to check this?
164 return (MBB->back().getOpcode() != AMDGPU::S_SETPC_B64);
165}
166
Tom Stellardf151a452015-06-26 21:14:58 +0000167void AMDGPUAsmPrinter::EmitFunctionBodyStart() {
Matt Arsenault021a2182017-04-19 19:38:10 +0000168 const AMDGPUMachineFunction *MFI = MF->getInfo<AMDGPUMachineFunction>();
169 if (!MFI->isEntryFunction())
170 return;
171
Tom Stellardf151a452015-06-26 21:14:58 +0000172 const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +0000173 amd_kernel_code_t KernelCode;
Tom Stellard2f3f9852017-01-25 01:25:13 +0000174 if (STM.isAmdCodeObjectV2(*MF)) {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000175 getAmdKernelCode(KernelCode, CurrentProgramInfo, *MF);
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +0000176
177 OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
178 getTargetStreamer().EmitAMDKernelCodeT(KernelCode);
Tom Stellardf151a452015-06-26 21:14:58 +0000179 }
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000180
181 if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
182 return;
Konstantin Zhuravlyov516651b2017-10-11 22:59:35 +0000183
184 HSAMetadataStream.emitKernel(*MF->getFunction(), KernelCode);
Tom Stellardf151a452015-06-26 21:14:58 +0000185}
186
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000187void AMDGPUAsmPrinter::EmitFunctionEntryLabel() {
188 const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
189 const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
Matt Arsenault1074cb52017-03-30 23:58:04 +0000190 if (MFI->isEntryFunction() && STM.isAmdCodeObjectV2(*MF)) {
Tom Stellard1b9748c2016-09-26 17:29:25 +0000191 SmallString<128> SymbolName;
192 getNameWithPrefix(SymbolName, MF->getFunction()),
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000193 getTargetStreamer().EmitAMDGPUSymbolType(
194 SymbolName, ELF::STT_AMDGPU_HSA_KERNEL);
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000195 }
196
197 AsmPrinter::EmitFunctionEntryLabel();
198}
199
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000200void AMDGPUAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
201
Tom Stellard00f2f912015-12-02 19:47:57 +0000202 // Group segment variables aren't emitted in HSA.
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000203 if (AMDGPU::isGroupSegment(GV, AMDGPUASI))
Tom Stellard00f2f912015-12-02 19:47:57 +0000204 return;
205
Tom Stellardfcfaea42016-05-05 17:03:33 +0000206 AsmPrinter::EmitGlobalVariable(GV);
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000207}
208
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000209bool AMDGPUAsmPrinter::doFinalization(Module &M) {
210 CallGraphResourceInfo.clear();
211 return AsmPrinter::doFinalization(M);
212}
213
Tim Renouf72800f02017-10-03 19:03:52 +0000214// For the amdpal OS type, read the amdgpu.pal.metadata supplied by the
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000215// frontend into our PALMetadataMap, ready for per-function modification. It
Tim Renouf72800f02017-10-03 19:03:52 +0000216// is a NamedMD containing an MDTuple containing a number of MDNodes each of
217// which is an integer value, and each two integer values forms a key=value
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000218// pair that we store as PALMetadataMap[key]=value in the map.
219void AMDGPUAsmPrinter::readPALMetadata(Module &M) {
Tim Renouf72800f02017-10-03 19:03:52 +0000220 auto NamedMD = M.getNamedMetadata("amdgpu.pal.metadata");
221 if (!NamedMD || !NamedMD->getNumOperands())
222 return;
223 auto Tuple = dyn_cast<MDTuple>(NamedMD->getOperand(0));
224 if (!Tuple)
225 return;
226 for (unsigned I = 0, E = Tuple->getNumOperands() & -2; I != E; I += 2) {
227 auto Key = mdconst::dyn_extract<ConstantInt>(Tuple->getOperand(I));
228 auto Val = mdconst::dyn_extract<ConstantInt>(Tuple->getOperand(I + 1));
229 if (!Key || !Val)
230 continue;
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000231 PALMetadataMap[Key->getZExtValue()] = Val->getZExtValue();
Tim Renouf72800f02017-10-03 19:03:52 +0000232 }
233}
234
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000235// Print comments that apply to both callable functions and entry points.
236void AMDGPUAsmPrinter::emitCommonFunctionComments(
237 uint32_t NumVGPR,
238 uint32_t NumSGPR,
239 uint32_t ScratchSize,
240 uint64_t CodeSize) {
241 OutStreamer->emitRawComment(" codeLenInByte = " + Twine(CodeSize), false);
242 OutStreamer->emitRawComment(" NumSgprs: " + Twine(NumSGPR), false);
243 OutStreamer->emitRawComment(" NumVgprs: " + Twine(NumVGPR), false);
244 OutStreamer->emitRawComment(" ScratchSize: " + Twine(ScratchSize), false);
245}
246
Tom Stellard45bb48e2015-06-13 03:28:10 +0000247bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000248 CurrentProgramInfo = SIProgramInfo();
249
Matt Arsenault6cb7b8a2017-04-19 17:42:39 +0000250 const AMDGPUMachineFunction *MFI = MF.getInfo<AMDGPUMachineFunction>();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000251
252 // The starting address of all shader programs must be 256 bytes aligned.
Matt Arsenault6cb7b8a2017-04-19 17:42:39 +0000253 // Regular functions just need the basic required instruction alignment.
254 MF.setAlignment(MFI->isEntryFunction() ? 8 : 2);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000255
256 SetupMachineFunction(MF);
257
Tom Stellard45bb48e2015-06-13 03:28:10 +0000258 const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
Konstantin Zhuravlyov67a6d542017-01-06 17:02:10 +0000259 MCContext &Context = getObjFileLowering().getContext();
260 if (!STM.isAmdHsaOS()) {
261 MCSectionELF *ConfigSection =
262 Context.getELFSection(".AMDGPU.config", ELF::SHT_PROGBITS, 0);
263 OutStreamer->SwitchSection(ConfigSection);
264 }
265
Tom Stellardf151a452015-06-26 21:14:58 +0000266 if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000267 if (MFI->isEntryFunction()) {
268 getSIProgramInfo(CurrentProgramInfo, MF);
269 } else {
270 auto I = CallGraphResourceInfo.insert(
271 std::make_pair(MF.getFunction(), SIFunctionResourceInfo()));
272 SIFunctionResourceInfo &Info = I.first->second;
273 assert(I.second && "should only be called once per function");
274 Info = analyzeResourceUsage(MF);
275 }
276
Tim Renouf72800f02017-10-03 19:03:52 +0000277 if (STM.isAmdPalOS())
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000278 EmitPALMetadata(MF, CurrentProgramInfo);
Tom Stellardf151a452015-06-26 21:14:58 +0000279 if (!STM.isAmdHsaOS()) {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000280 EmitProgramInfoSI(MF, CurrentProgramInfo);
Tom Stellardf151a452015-06-26 21:14:58 +0000281 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000282 } else {
283 EmitProgramInfoR600(MF);
284 }
285
286 DisasmLines.clear();
287 HexLines.clear();
288 DisasmLineMaxLen = 0;
289
290 EmitFunctionBody();
291
292 if (isVerbose()) {
293 MCSectionELF *CommentSection =
294 Context.getELFSection(".AMDGPU.csdata", ELF::SHT_PROGBITS, 0);
295 OutStreamer->SwitchSection(CommentSection);
296
297 if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000298 if (!MFI->isEntryFunction()) {
Matt Arsenault021a2182017-04-19 19:38:10 +0000299 OutStreamer->emitRawComment(" Function info:", false);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000300 SIFunctionResourceInfo &Info = CallGraphResourceInfo[MF.getFunction()];
301 emitCommonFunctionComments(
302 Info.NumVGPR,
303 Info.getTotalNumSGPRs(MF.getSubtarget<SISubtarget>()),
304 Info.PrivateSegmentSize,
305 getFunctionCodeSize(MF));
306 return false;
Matt Arsenault021a2182017-04-19 19:38:10 +0000307 }
308
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000309 OutStreamer->emitRawComment(" Kernel info:", false);
310 emitCommonFunctionComments(CurrentProgramInfo.NumVGPR,
311 CurrentProgramInfo.NumSGPR,
312 CurrentProgramInfo.ScratchSize,
313 getFunctionCodeSize(MF));
314
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000315 OutStreamer->emitRawComment(
316 " FloatMode: " + Twine(CurrentProgramInfo.FloatMode), false);
317 OutStreamer->emitRawComment(
318 " IeeeMode: " + Twine(CurrentProgramInfo.IEEEMode), false);
319 OutStreamer->emitRawComment(
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000320 " LDSByteSize: " + Twine(CurrentProgramInfo.LDSSize) +
321 " bytes/workgroup (compile time only)", false);
Matt Arsenaultd41c0db2015-11-05 05:27:07 +0000322
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000323 OutStreamer->emitRawComment(
324 " SGPRBlocks: " + Twine(CurrentProgramInfo.SGPRBlocks), false);
325 OutStreamer->emitRawComment(
326 " VGPRBlocks: " + Twine(CurrentProgramInfo.VGPRBlocks), false);
Matt Arsenault021a2182017-04-19 19:38:10 +0000327
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000328 OutStreamer->emitRawComment(
329 " NumSGPRsForWavesPerEU: " +
330 Twine(CurrentProgramInfo.NumSGPRsForWavesPerEU), false);
331 OutStreamer->emitRawComment(
332 " NumVGPRsForWavesPerEU: " +
333 Twine(CurrentProgramInfo.NumVGPRsForWavesPerEU), false);
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000334
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000335 OutStreamer->emitRawComment(
336 " ReservedVGPRFirst: " + Twine(CurrentProgramInfo.ReservedVGPRFirst),
337 false);
338 OutStreamer->emitRawComment(
339 " ReservedVGPRCount: " + Twine(CurrentProgramInfo.ReservedVGPRCount),
340 false);
Konstantin Zhuravlyov1d99c4d2016-04-26 15:43:14 +0000341
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000342 if (MF.getSubtarget<SISubtarget>().debuggerEmitPrologue()) {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000343 OutStreamer->emitRawComment(
344 " DebuggerWavefrontPrivateSegmentOffsetSGPR: s" +
345 Twine(CurrentProgramInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR), false);
346 OutStreamer->emitRawComment(
347 " DebuggerPrivateSegmentBufferSGPR: s" +
348 Twine(CurrentProgramInfo.DebuggerPrivateSegmentBufferSGPR), false);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000349 }
350
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000351 OutStreamer->emitRawComment(
352 " COMPUTE_PGM_RSRC2:USER_SGPR: " +
353 Twine(G_00B84C_USER_SGPR(CurrentProgramInfo.ComputePGMRSrc2)), false);
354 OutStreamer->emitRawComment(
355 " COMPUTE_PGM_RSRC2:TRAP_HANDLER: " +
356 Twine(G_00B84C_TRAP_HANDLER(CurrentProgramInfo.ComputePGMRSrc2)), false);
357 OutStreamer->emitRawComment(
358 " COMPUTE_PGM_RSRC2:TGID_X_EN: " +
359 Twine(G_00B84C_TGID_X_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
360 OutStreamer->emitRawComment(
361 " COMPUTE_PGM_RSRC2:TGID_Y_EN: " +
362 Twine(G_00B84C_TGID_Y_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
363 OutStreamer->emitRawComment(
364 " COMPUTE_PGM_RSRC2:TGID_Z_EN: " +
365 Twine(G_00B84C_TGID_Z_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
366 OutStreamer->emitRawComment(
367 " COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: " +
368 Twine(G_00B84C_TIDIG_COMP_CNT(CurrentProgramInfo.ComputePGMRSrc2)),
369 false);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000370 } else {
371 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
372 OutStreamer->emitRawComment(
Matt Arsenaultf9245b72016-07-22 17:01:25 +0000373 Twine("SQ_PGM_RESOURCES:STACK_SIZE = " + Twine(MFI->CFStackSize)));
Tom Stellard45bb48e2015-06-13 03:28:10 +0000374 }
375 }
376
377 if (STM.dumpCode()) {
378
379 OutStreamer->SwitchSection(
380 Context.getELFSection(".AMDGPU.disasm", ELF::SHT_NOTE, 0));
381
382 for (size_t i = 0; i < DisasmLines.size(); ++i) {
383 std::string Comment(DisasmLineMaxLen - DisasmLines[i].size(), ' ');
384 Comment += " ; " + HexLines[i] + "\n";
385
386 OutStreamer->EmitBytes(StringRef(DisasmLines[i]));
387 OutStreamer->EmitBytes(StringRef(Comment));
388 }
389 }
390
391 return false;
392}
393
394void AMDGPUAsmPrinter::EmitProgramInfoR600(const MachineFunction &MF) {
395 unsigned MaxGPR = 0;
396 bool killPixel = false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000397 const R600Subtarget &STM = MF.getSubtarget<R600Subtarget>();
398 const R600RegisterInfo *RI = STM.getRegisterInfo();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000399 const R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
400
401 for (const MachineBasicBlock &MBB : MF) {
402 for (const MachineInstr &MI : MBB) {
403 if (MI.getOpcode() == AMDGPU::KILLGT)
404 killPixel = true;
405 unsigned numOperands = MI.getNumOperands();
406 for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
407 const MachineOperand &MO = MI.getOperand(op_idx);
408 if (!MO.isReg())
409 continue;
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000410 unsigned HWReg = RI->getHWRegIndex(MO.getReg());
Tom Stellard45bb48e2015-06-13 03:28:10 +0000411
412 // Register with value > 127 aren't GPR
413 if (HWReg > 127)
414 continue;
415 MaxGPR = std::max(MaxGPR, HWReg);
416 }
417 }
418 }
419
420 unsigned RsrcReg;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000421 if (STM.getGeneration() >= R600Subtarget::EVERGREEN) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000422 // Evergreen / Northern Islands
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000423 switch (MF.getFunction()->getCallingConv()) {
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000424 default: LLVM_FALLTHROUGH;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000425 case CallingConv::AMDGPU_CS: RsrcReg = R_0288D4_SQ_PGM_RESOURCES_LS; break;
426 case CallingConv::AMDGPU_GS: RsrcReg = R_028878_SQ_PGM_RESOURCES_GS; break;
427 case CallingConv::AMDGPU_PS: RsrcReg = R_028844_SQ_PGM_RESOURCES_PS; break;
428 case CallingConv::AMDGPU_VS: RsrcReg = R_028860_SQ_PGM_RESOURCES_VS; break;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000429 }
430 } else {
431 // R600 / R700
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000432 switch (MF.getFunction()->getCallingConv()) {
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000433 default: LLVM_FALLTHROUGH;
434 case CallingConv::AMDGPU_GS: LLVM_FALLTHROUGH;
435 case CallingConv::AMDGPU_CS: LLVM_FALLTHROUGH;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000436 case CallingConv::AMDGPU_VS: RsrcReg = R_028868_SQ_PGM_RESOURCES_VS; break;
437 case CallingConv::AMDGPU_PS: RsrcReg = R_028850_SQ_PGM_RESOURCES_PS; break;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000438 }
439 }
440
441 OutStreamer->EmitIntValue(RsrcReg, 4);
442 OutStreamer->EmitIntValue(S_NUM_GPRS(MaxGPR + 1) |
Matt Arsenaultf9245b72016-07-22 17:01:25 +0000443 S_STACK_SIZE(MFI->CFStackSize), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000444 OutStreamer->EmitIntValue(R_02880C_DB_SHADER_CONTROL, 4);
445 OutStreamer->EmitIntValue(S_02880C_KILL_ENABLE(killPixel), 4);
446
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000447 if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000448 OutStreamer->EmitIntValue(R_0288E8_SQ_LDS_ALLOC, 4);
Matt Arsenault52ef4012016-07-26 16:45:58 +0000449 OutStreamer->EmitIntValue(alignTo(MFI->getLDSSize(), 4) >> 2, 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000450 }
451}
452
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000453uint64_t AMDGPUAsmPrinter::getFunctionCodeSize(const MachineFunction &MF) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000454 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000455 const SIInstrInfo *TII = STM.getInstrInfo();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000456
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000457 uint64_t CodeSize = 0;
458
Tom Stellard45bb48e2015-06-13 03:28:10 +0000459 for (const MachineBasicBlock &MBB : MF) {
460 for (const MachineInstr &MI : MBB) {
461 // TODO: CodeSize should account for multiple functions.
Matt Arsenaultc5746862015-08-12 09:04:44 +0000462
463 // TODO: Should we count size of debug info?
464 if (MI.isDebugValue())
465 continue;
466
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000467 CodeSize += TII->getInstSizeInBytes(MI);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000468 }
469 }
470
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000471 return CodeSize;
472}
473
474static bool hasAnyNonFlatUseOfReg(const MachineRegisterInfo &MRI,
475 const SIInstrInfo &TII,
476 unsigned Reg) {
477 for (const MachineOperand &UseOp : MRI.reg_operands(Reg)) {
478 if (!UseOp.isImplicit() || !TII.isFLAT(*UseOp.getParent()))
479 return true;
480 }
481
482 return false;
483}
484
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000485static unsigned getNumExtraSGPRs(const SISubtarget &ST,
486 bool VCCUsed,
487 bool FlatScrUsed) {
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000488 unsigned ExtraSGPRs = 0;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000489 if (VCCUsed)
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000490 ExtraSGPRs = 2;
491
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000492 if (ST.getGeneration() < SISubtarget::VOLCANIC_ISLANDS) {
493 if (FlatScrUsed)
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000494 ExtraSGPRs = 4;
495 } else {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000496 if (ST.isXNACKEnabled())
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000497 ExtraSGPRs = 4;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000498
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000499 if (FlatScrUsed)
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000500 ExtraSGPRs = 6;
Tom Stellardcaaa3aa2015-12-17 17:05:09 +0000501 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000502
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000503 return ExtraSGPRs;
504}
505
506int32_t AMDGPUAsmPrinter::SIFunctionResourceInfo::getTotalNumSGPRs(
507 const SISubtarget &ST) const {
508 return NumExplicitSGPR + getNumExtraSGPRs(ST, UsesVCC, UsesFlatScratch);
509}
510
511AMDGPUAsmPrinter::SIFunctionResourceInfo AMDGPUAsmPrinter::analyzeResourceUsage(
512 const MachineFunction &MF) const {
513 SIFunctionResourceInfo Info;
514
515 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
516 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
517 const MachineFrameInfo &FrameInfo = MF.getFrameInfo();
518 const MachineRegisterInfo &MRI = MF.getRegInfo();
519 const SIInstrInfo *TII = ST.getInstrInfo();
520 const SIRegisterInfo &TRI = TII->getRegisterInfo();
521
522 Info.UsesFlatScratch = MRI.isPhysRegUsed(AMDGPU::FLAT_SCR_LO) ||
523 MRI.isPhysRegUsed(AMDGPU::FLAT_SCR_HI);
524
525 // Even if FLAT_SCRATCH is implicitly used, it has no effect if flat
526 // instructions aren't used to access the scratch buffer. Inline assembly may
527 // need it though.
528 //
529 // If we only have implicit uses of flat_scr on flat instructions, it is not
530 // really needed.
531 if (Info.UsesFlatScratch && !MFI->hasFlatScratchInit() &&
532 (!hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR) &&
533 !hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR_LO) &&
534 !hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR_HI))) {
535 Info.UsesFlatScratch = false;
536 }
537
538 Info.HasDynamicallySizedStack = FrameInfo.hasVarSizedObjects();
539 Info.PrivateSegmentSize = FrameInfo.getStackSize();
540
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000541
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000542 Info.UsesVCC = MRI.isPhysRegUsed(AMDGPU::VCC_LO) ||
543 MRI.isPhysRegUsed(AMDGPU::VCC_HI);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000544
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000545 // If there are no calls, MachineRegisterInfo can tell us the used register
546 // count easily.
Matt Arsenault22cdb612017-09-05 18:36:36 +0000547 // A tail call isn't considered a call for MachineFrameInfo's purposes.
548 if (!FrameInfo.hasCalls() && !FrameInfo.hasTailCall()) {
Matt Arsenault2738ede2017-08-02 17:15:01 +0000549 MCPhysReg HighestVGPRReg = AMDGPU::NoRegister;
550 for (MCPhysReg Reg : reverse(AMDGPU::VGPR_32RegClass.getRegisters())) {
551 if (MRI.isPhysRegUsed(Reg)) {
552 HighestVGPRReg = Reg;
553 break;
554 }
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000555 }
Matt Arsenault2738ede2017-08-02 17:15:01 +0000556
557 MCPhysReg HighestSGPRReg = AMDGPU::NoRegister;
558 for (MCPhysReg Reg : reverse(AMDGPU::SGPR_32RegClass.getRegisters())) {
559 if (MRI.isPhysRegUsed(Reg)) {
560 HighestSGPRReg = Reg;
561 break;
562 }
563 }
564
565 // We found the maximum register index. They start at 0, so add one to get the
566 // number of registers.
567 Info.NumVGPR = HighestVGPRReg == AMDGPU::NoRegister ? 0 :
568 TRI.getHWRegIndex(HighestVGPRReg) + 1;
569 Info.NumExplicitSGPR = HighestSGPRReg == AMDGPU::NoRegister ? 0 :
570 TRI.getHWRegIndex(HighestSGPRReg) + 1;
571
572 return Info;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000573 }
574
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000575 int32_t MaxVGPR = -1;
576 int32_t MaxSGPR = -1;
577 uint32_t CalleeFrameSize = 0;
578
579 for (const MachineBasicBlock &MBB : MF) {
580 for (const MachineInstr &MI : MBB) {
581 // TODO: Check regmasks? Do they occur anywhere except calls?
582 for (const MachineOperand &MO : MI.operands()) {
583 unsigned Width = 0;
584 bool IsSGPR = false;
585
586 if (!MO.isReg())
587 continue;
588
589 unsigned Reg = MO.getReg();
590 switch (Reg) {
591 case AMDGPU::EXEC:
592 case AMDGPU::EXEC_LO:
593 case AMDGPU::EXEC_HI:
594 case AMDGPU::SCC:
595 case AMDGPU::M0:
596 case AMDGPU::SRC_SHARED_BASE:
597 case AMDGPU::SRC_SHARED_LIMIT:
598 case AMDGPU::SRC_PRIVATE_BASE:
599 case AMDGPU::SRC_PRIVATE_LIMIT:
600 continue;
601
602 case AMDGPU::NoRegister:
603 assert(MI.isDebugValue());
604 continue;
605
606 case AMDGPU::VCC:
607 case AMDGPU::VCC_LO:
608 case AMDGPU::VCC_HI:
609 Info.UsesVCC = true;
610 continue;
611
612 case AMDGPU::FLAT_SCR:
613 case AMDGPU::FLAT_SCR_LO:
614 case AMDGPU::FLAT_SCR_HI:
615 continue;
616
617 case AMDGPU::TBA:
618 case AMDGPU::TBA_LO:
619 case AMDGPU::TBA_HI:
620 case AMDGPU::TMA:
621 case AMDGPU::TMA_LO:
622 case AMDGPU::TMA_HI:
623 llvm_unreachable("trap handler registers should not be used");
624
625 default:
626 break;
627 }
628
629 if (AMDGPU::SReg_32RegClass.contains(Reg)) {
630 assert(!AMDGPU::TTMP_32RegClass.contains(Reg) &&
631 "trap handler registers should not be used");
632 IsSGPR = true;
633 Width = 1;
634 } else if (AMDGPU::VGPR_32RegClass.contains(Reg)) {
635 IsSGPR = false;
636 Width = 1;
637 } else if (AMDGPU::SReg_64RegClass.contains(Reg)) {
638 assert(!AMDGPU::TTMP_64RegClass.contains(Reg) &&
639 "trap handler registers should not be used");
640 IsSGPR = true;
641 Width = 2;
642 } else if (AMDGPU::VReg_64RegClass.contains(Reg)) {
643 IsSGPR = false;
644 Width = 2;
645 } else if (AMDGPU::VReg_96RegClass.contains(Reg)) {
646 IsSGPR = false;
647 Width = 3;
648 } else if (AMDGPU::SReg_128RegClass.contains(Reg)) {
649 IsSGPR = true;
650 Width = 4;
651 } else if (AMDGPU::VReg_128RegClass.contains(Reg)) {
652 IsSGPR = false;
653 Width = 4;
654 } else if (AMDGPU::SReg_256RegClass.contains(Reg)) {
655 IsSGPR = true;
656 Width = 8;
657 } else if (AMDGPU::VReg_256RegClass.contains(Reg)) {
658 IsSGPR = false;
659 Width = 8;
660 } else if (AMDGPU::SReg_512RegClass.contains(Reg)) {
661 IsSGPR = true;
662 Width = 16;
663 } else if (AMDGPU::VReg_512RegClass.contains(Reg)) {
664 IsSGPR = false;
665 Width = 16;
666 } else {
667 llvm_unreachable("Unknown register class");
668 }
669 unsigned HWReg = TRI.getHWRegIndex(Reg);
670 int MaxUsed = HWReg + Width - 1;
671 if (IsSGPR) {
672 MaxSGPR = MaxUsed > MaxSGPR ? MaxUsed : MaxSGPR;
673 } else {
674 MaxVGPR = MaxUsed > MaxVGPR ? MaxUsed : MaxVGPR;
675 }
676 }
677
678 if (MI.isCall()) {
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000679 // Pseudo used just to encode the underlying global. Is there a better
680 // way to track this?
Matt Arsenault71bcbd42017-08-11 20:42:08 +0000681
682 const MachineOperand *CalleeOp
683 = TII->getNamedOperand(MI, AMDGPU::OpName::callee);
684 const Function *Callee = cast<Function>(CalleeOp->getGlobal());
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000685 if (Callee->isDeclaration()) {
686 // If this is a call to an external function, we can't do much. Make
687 // conservative guesses.
688
689 // 48 SGPRs - vcc, - flat_scr, -xnack
690 int MaxSGPRGuess = 47 - getNumExtraSGPRs(ST, true,
691 ST.hasFlatAddressSpace());
692 MaxSGPR = std::max(MaxSGPR, MaxSGPRGuess);
693 MaxVGPR = std::max(MaxVGPR, 23);
694
695 CalleeFrameSize = std::max(CalleeFrameSize, 16384u);
696 Info.UsesVCC = true;
697 Info.UsesFlatScratch = ST.hasFlatAddressSpace();
698 Info.HasDynamicallySizedStack = true;
699 } else {
700 // We force CodeGen to run in SCC order, so the callee's register
701 // usage etc. should be the cumulative usage of all callees.
702 auto I = CallGraphResourceInfo.find(Callee);
703 assert(I != CallGraphResourceInfo.end() &&
704 "callee should have been handled before caller");
705
706 MaxSGPR = std::max(I->second.NumExplicitSGPR - 1, MaxSGPR);
707 MaxVGPR = std::max(I->second.NumVGPR - 1, MaxVGPR);
708 CalleeFrameSize
709 = std::max(I->second.PrivateSegmentSize, CalleeFrameSize);
710 Info.UsesVCC |= I->second.UsesVCC;
711 Info.UsesFlatScratch |= I->second.UsesFlatScratch;
712 Info.HasDynamicallySizedStack |= I->second.HasDynamicallySizedStack;
713 Info.HasRecursion |= I->second.HasRecursion;
714 }
715
716 if (!Callee->doesNotRecurse())
717 Info.HasRecursion = true;
718 }
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000719 }
720 }
721
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000722 Info.NumExplicitSGPR = MaxSGPR + 1;
723 Info.NumVGPR = MaxVGPR + 1;
724 Info.PrivateSegmentSize += CalleeFrameSize;
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000725
726 return Info;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000727}
728
729void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo,
730 const MachineFunction &MF) {
731 SIFunctionResourceInfo Info = analyzeResourceUsage(MF);
732
733 ProgInfo.NumVGPR = Info.NumVGPR;
734 ProgInfo.NumSGPR = Info.NumExplicitSGPR;
735 ProgInfo.ScratchSize = Info.PrivateSegmentSize;
736 ProgInfo.VCCUsed = Info.UsesVCC;
737 ProgInfo.FlatUsed = Info.UsesFlatScratch;
738 ProgInfo.DynamicCallStack = Info.HasDynamicallySizedStack || Info.HasRecursion;
739
740 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
741 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
742 const SIInstrInfo *TII = STM.getInstrInfo();
743 const SIRegisterInfo *RI = &TII->getRegisterInfo();
744
745 unsigned ExtraSGPRs = getNumExtraSGPRs(STM,
746 ProgInfo.VCCUsed,
747 ProgInfo.FlatUsed);
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000748 unsigned ExtraVGPRs = STM.getReservedNumVGPRs(MF);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000749
Marek Olsak91f22fb2016-12-09 19:49:40 +0000750 // Check the addressable register limit before we add ExtraSGPRs.
751 if (STM.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS &&
752 !STM.hasSGPRInitBug()) {
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000753 unsigned MaxAddressableNumSGPRs = STM.getAddressableNumSGPRs();
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000754 if (ProgInfo.NumSGPR > MaxAddressableNumSGPRs) {
Marek Olsak91f22fb2016-12-09 19:49:40 +0000755 // This can happen due to a compiler bug or when using inline asm.
756 LLVMContext &Ctx = MF.getFunction()->getContext();
757 DiagnosticInfoResourceLimit Diag(*MF.getFunction(),
758 "addressable scalar registers",
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000759 ProgInfo.NumSGPR, DS_Error,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000760 DK_ResourceLimit,
761 MaxAddressableNumSGPRs);
Marek Olsak91f22fb2016-12-09 19:49:40 +0000762 Ctx.diagnose(Diag);
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000763 ProgInfo.NumSGPR = MaxAddressableNumSGPRs - 1;
Marek Olsak91f22fb2016-12-09 19:49:40 +0000764 }
765 }
766
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000767 // Account for extra SGPRs and VGPRs reserved for debugger use.
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000768 ProgInfo.NumSGPR += ExtraSGPRs;
769 ProgInfo.NumVGPR += ExtraVGPRs;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000770
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000771 // Adjust number of registers used to meet default/requested minimum/maximum
772 // number of waves per execution unit request.
773 ProgInfo.NumSGPRsForWavesPerEU = std::max(
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000774 std::max(ProgInfo.NumSGPR, 1u), STM.getMinNumSGPRs(MFI->getMaxWavesPerEU()));
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000775 ProgInfo.NumVGPRsForWavesPerEU = std::max(
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000776 std::max(ProgInfo.NumVGPR, 1u), STM.getMinNumVGPRs(MFI->getMaxWavesPerEU()));
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000777
Marek Olsak91f22fb2016-12-09 19:49:40 +0000778 if (STM.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ||
779 STM.hasSGPRInitBug()) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000780 unsigned MaxAddressableNumSGPRs = STM.getAddressableNumSGPRs();
781 if (ProgInfo.NumSGPR > MaxAddressableNumSGPRs) {
782 // This can happen due to a compiler bug or when using inline asm to use
783 // the registers which are usually reserved for vcc etc.
Marek Olsak91f22fb2016-12-09 19:49:40 +0000784 LLVMContext &Ctx = MF.getFunction()->getContext();
785 DiagnosticInfoResourceLimit Diag(*MF.getFunction(),
786 "scalar registers",
787 ProgInfo.NumSGPR, DS_Error,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000788 DK_ResourceLimit,
789 MaxAddressableNumSGPRs);
Marek Olsak91f22fb2016-12-09 19:49:40 +0000790 Ctx.diagnose(Diag);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000791 ProgInfo.NumSGPR = MaxAddressableNumSGPRs;
792 ProgInfo.NumSGPRsForWavesPerEU = MaxAddressableNumSGPRs;
Marek Olsak91f22fb2016-12-09 19:49:40 +0000793 }
Matt Arsenault4eae3012016-10-28 20:31:47 +0000794 }
795
796 if (STM.hasSGPRInitBug()) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000797 ProgInfo.NumSGPR =
798 AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG;
799 ProgInfo.NumSGPRsForWavesPerEU =
800 AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000801 }
802
Matt Arsenault161e2b42017-04-18 20:59:40 +0000803 if (MFI->getNumUserSGPRs() > STM.getMaxNumUserSGPRs()) {
Matt Arsenault41003af2015-11-30 21:16:07 +0000804 LLVMContext &Ctx = MF.getFunction()->getContext();
Matt Arsenaultff982412016-06-20 18:13:04 +0000805 DiagnosticInfoResourceLimit Diag(*MF.getFunction(), "user SGPRs",
Matt Arsenault161e2b42017-04-18 20:59:40 +0000806 MFI->getNumUserSGPRs(), DS_Error);
Matt Arsenaultff982412016-06-20 18:13:04 +0000807 Ctx.diagnose(Diag);
Matt Arsenault41003af2015-11-30 21:16:07 +0000808 }
809
Matt Arsenault52ef4012016-07-26 16:45:58 +0000810 if (MFI->getLDSSize() > static_cast<unsigned>(STM.getLocalMemorySize())) {
Matt Arsenault1c4d0ef2016-04-28 19:37:35 +0000811 LLVMContext &Ctx = MF.getFunction()->getContext();
Matt Arsenaultff982412016-06-20 18:13:04 +0000812 DiagnosticInfoResourceLimit Diag(*MF.getFunction(), "local memory",
Matt Arsenault52ef4012016-07-26 16:45:58 +0000813 MFI->getLDSSize(), DS_Error);
Matt Arsenaultff982412016-06-20 18:13:04 +0000814 Ctx.diagnose(Diag);
Matt Arsenault1c4d0ef2016-04-28 19:37:35 +0000815 }
816
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000817 // SGPRBlocks is actual number of SGPR blocks minus 1.
818 ProgInfo.SGPRBlocks = alignTo(ProgInfo.NumSGPRsForWavesPerEU,
Konstantin Zhuravlyove22fbcb2017-02-08 13:18:40 +0000819 STM.getSGPREncodingGranule());
820 ProgInfo.SGPRBlocks = ProgInfo.SGPRBlocks / STM.getSGPREncodingGranule() - 1;
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000821
822 // VGPRBlocks is actual number of VGPR blocks minus 1.
823 ProgInfo.VGPRBlocks = alignTo(ProgInfo.NumVGPRsForWavesPerEU,
Konstantin Zhuravlyove22fbcb2017-02-08 13:18:40 +0000824 STM.getVGPREncodingGranule());
825 ProgInfo.VGPRBlocks = ProgInfo.VGPRBlocks / STM.getVGPREncodingGranule() - 1;
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000826
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000827 // Record first reserved VGPR and number of reserved VGPRs.
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000828 ProgInfo.ReservedVGPRFirst = STM.debuggerReserveRegs() ? ProgInfo.NumVGPR : 0;
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000829 ProgInfo.ReservedVGPRCount = STM.getReservedNumVGPRs(MF);
830
831 // Update DebuggerWavefrontPrivateSegmentOffsetSGPR and
832 // DebuggerPrivateSegmentBufferSGPR fields if "amdgpu-debugger-emit-prologue"
833 // attribute was requested.
834 if (STM.debuggerEmitPrologue()) {
835 ProgInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR =
836 RI->getHWRegIndex(MFI->getScratchWaveOffsetReg());
837 ProgInfo.DebuggerPrivateSegmentBufferSGPR =
838 RI->getHWRegIndex(MFI->getScratchRSrcReg());
839 }
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000840
Tom Stellard45bb48e2015-06-13 03:28:10 +0000841 // Set the value to initialize FP_ROUND and FP_DENORM parts of the mode
842 // register.
843 ProgInfo.FloatMode = getFPMode(MF);
844
Wei Ding3cb2a1e2016-10-19 22:34:49 +0000845 ProgInfo.IEEEMode = STM.enableIEEEBit(MF);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000846
Matt Arsenault7293f982016-01-28 20:53:35 +0000847 // Make clamp modifier on NaN input returns 0.
Matt Arsenault2fdf2a12017-02-21 23:35:48 +0000848 ProgInfo.DX10Clamp = STM.enableDX10Clamp();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000849
Tom Stellard45bb48e2015-06-13 03:28:10 +0000850 unsigned LDSAlignShift;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000851 if (STM.getGeneration() < SISubtarget::SEA_ISLANDS) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000852 // LDS is allocated in 64 dword blocks.
853 LDSAlignShift = 8;
854 } else {
855 // LDS is allocated in 128 dword blocks.
856 LDSAlignShift = 9;
857 }
858
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000859 unsigned LDSSpillSize =
Matt Arsenault161e2b42017-04-18 20:59:40 +0000860 MFI->getLDSWaveSpillSize() * MFI->getMaxFlatWorkGroupSize();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000861
Matt Arsenault52ef4012016-07-26 16:45:58 +0000862 ProgInfo.LDSSize = MFI->getLDSSize() + LDSSpillSize;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000863 ProgInfo.LDSBlocks =
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +0000864 alignTo(ProgInfo.LDSSize, 1ULL << LDSAlignShift) >> LDSAlignShift;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000865
866 // Scratch is allocated in 256 dword blocks.
867 unsigned ScratchAlignShift = 10;
868 // We need to program the hardware with the amount of scratch memory that
869 // is used by the entire wave. ProgInfo.ScratchSize is the amount of
870 // scratch memory used per thread.
871 ProgInfo.ScratchBlocks =
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000872 alignTo(ProgInfo.ScratchSize * STM.getWavefrontSize(),
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +0000873 1ULL << ScratchAlignShift) >>
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000874 ScratchAlignShift;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000875
876 ProgInfo.ComputePGMRSrc1 =
877 S_00B848_VGPRS(ProgInfo.VGPRBlocks) |
878 S_00B848_SGPRS(ProgInfo.SGPRBlocks) |
879 S_00B848_PRIORITY(ProgInfo.Priority) |
880 S_00B848_FLOAT_MODE(ProgInfo.FloatMode) |
881 S_00B848_PRIV(ProgInfo.Priv) |
882 S_00B848_DX10_CLAMP(ProgInfo.DX10Clamp) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000883 S_00B848_DEBUG_MODE(ProgInfo.DebugMode) |
Tom Stellard45bb48e2015-06-13 03:28:10 +0000884 S_00B848_IEEE_MODE(ProgInfo.IEEEMode);
885
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000886 // 0 = X, 1 = XY, 2 = XYZ
887 unsigned TIDIGCompCnt = 0;
888 if (MFI->hasWorkItemIDZ())
889 TIDIGCompCnt = 2;
890 else if (MFI->hasWorkItemIDY())
891 TIDIGCompCnt = 1;
892
Tom Stellard45bb48e2015-06-13 03:28:10 +0000893 ProgInfo.ComputePGMRSrc2 =
894 S_00B84C_SCRATCH_EN(ProgInfo.ScratchBlocks > 0) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000895 S_00B84C_USER_SGPR(MFI->getNumUserSGPRs()) |
Wei Ding205bfdb2017-02-10 02:15:29 +0000896 S_00B84C_TRAP_HANDLER(STM.isTrapHandlerEnabled()) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000897 S_00B84C_TGID_X_EN(MFI->hasWorkGroupIDX()) |
898 S_00B84C_TGID_Y_EN(MFI->hasWorkGroupIDY()) |
899 S_00B84C_TGID_Z_EN(MFI->hasWorkGroupIDZ()) |
900 S_00B84C_TG_SIZE_EN(MFI->hasWorkGroupInfo()) |
901 S_00B84C_TIDIG_COMP_CNT(TIDIGCompCnt) |
902 S_00B84C_EXCP_EN_MSB(0) |
Konstantin Zhuravlyov6ccb0762017-05-05 20:13:55 +0000903 // For AMDHSA, LDS_SIZE must be zero, as it is populated by the CP.
904 S_00B84C_LDS_SIZE(STM.isAmdHsaOS() ? 0 : ProgInfo.LDSBlocks) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000905 S_00B84C_EXCP_EN(0);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000906}
907
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000908static unsigned getRsrcReg(CallingConv::ID CallConv) {
909 switch (CallConv) {
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000910 default: LLVM_FALLTHROUGH;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000911 case CallingConv::AMDGPU_CS: return R_00B848_COMPUTE_PGM_RSRC1;
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000912 case CallingConv::AMDGPU_LS: return R_00B528_SPI_SHADER_PGM_RSRC1_LS;
Marek Olsaka302a7362017-05-02 15:41:10 +0000913 case CallingConv::AMDGPU_HS: return R_00B428_SPI_SHADER_PGM_RSRC1_HS;
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000914 case CallingConv::AMDGPU_ES: return R_00B328_SPI_SHADER_PGM_RSRC1_ES;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000915 case CallingConv::AMDGPU_GS: return R_00B228_SPI_SHADER_PGM_RSRC1_GS;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000916 case CallingConv::AMDGPU_VS: return R_00B128_SPI_SHADER_PGM_RSRC1_VS;
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000917 case CallingConv::AMDGPU_PS: return R_00B028_SPI_SHADER_PGM_RSRC1_PS;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000918 }
919}
920
921void AMDGPUAsmPrinter::EmitProgramInfoSI(const MachineFunction &MF,
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000922 const SIProgramInfo &CurrentProgramInfo) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000923 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000924 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000925 unsigned RsrcReg = getRsrcReg(MF.getFunction()->getCallingConv());
Tom Stellard45bb48e2015-06-13 03:28:10 +0000926
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000927 if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000928 OutStreamer->EmitIntValue(R_00B848_COMPUTE_PGM_RSRC1, 4);
929
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000930 OutStreamer->EmitIntValue(CurrentProgramInfo.ComputePGMRSrc1, 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000931
932 OutStreamer->EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000933 OutStreamer->EmitIntValue(CurrentProgramInfo.ComputePGMRSrc2, 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000934
935 OutStreamer->EmitIntValue(R_00B860_COMPUTE_TMPRING_SIZE, 4);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000936 OutStreamer->EmitIntValue(S_00B860_WAVESIZE(CurrentProgramInfo.ScratchBlocks), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000937
938 // TODO: Should probably note flat usage somewhere. SC emits a "FlatPtr32 =
939 // 0" comment but I don't see a corresponding field in the register spec.
940 } else {
941 OutStreamer->EmitIntValue(RsrcReg, 4);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000942 OutStreamer->EmitIntValue(S_00B028_VGPRS(CurrentProgramInfo.VGPRBlocks) |
943 S_00B028_SGPRS(CurrentProgramInfo.SGPRBlocks), 4);
Tim Renouf13229152017-09-29 09:49:35 +0000944 unsigned Rsrc2Val = 0;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000945 if (STM.isVGPRSpillingEnabled(*MF.getFunction())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000946 OutStreamer->EmitIntValue(R_0286E8_SPI_TMPRING_SIZE, 4);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000947 OutStreamer->EmitIntValue(S_0286E8_WAVESIZE(CurrentProgramInfo.ScratchBlocks), 4);
Tim Renouf13229152017-09-29 09:49:35 +0000948 if (TM.getTargetTriple().getOS() == Triple::AMDPAL)
949 Rsrc2Val = S_00B84C_SCRATCH_EN(CurrentProgramInfo.ScratchBlocks > 0);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000950 }
Tim Renouf13229152017-09-29 09:49:35 +0000951 if (MF.getFunction()->getCallingConv() == CallingConv::AMDGPU_PS) {
952 OutStreamer->EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4);
953 OutStreamer->EmitIntValue(MFI->getPSInputEnable(), 4);
954 OutStreamer->EmitIntValue(R_0286D0_SPI_PS_INPUT_ADDR, 4);
955 OutStreamer->EmitIntValue(MFI->getPSInputAddr(), 4);
956 Rsrc2Val |= S_00B02C_EXTRA_LDS_SIZE(CurrentProgramInfo.LDSBlocks);
957 }
958 if (Rsrc2Val) {
959 OutStreamer->EmitIntValue(RsrcReg + 4 /*rsrc2*/, 4);
960 OutStreamer->EmitIntValue(Rsrc2Val, 4);
961 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000962 }
Marek Olsak0532c192016-07-13 17:35:15 +0000963
964 OutStreamer->EmitIntValue(R_SPILLED_SGPRS, 4);
965 OutStreamer->EmitIntValue(MFI->getNumSpilledSGPRs(), 4);
966 OutStreamer->EmitIntValue(R_SPILLED_VGPRS, 4);
967 OutStreamer->EmitIntValue(MFI->getNumSpilledVGPRs(), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000968}
969
Tim Renouf72800f02017-10-03 19:03:52 +0000970// This is the equivalent of EmitProgramInfoSI above, but for when the OS type
971// is AMDPAL. It stores each compute/SPI register setting and other PAL
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000972// metadata items into the PALMetadataMap, combining with any provided by the
973// frontend as LLVM metadata. Once all functions are written, PALMetadataMap is
Tim Renouf72800f02017-10-03 19:03:52 +0000974// then written as a single block in the .note section.
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000975void AMDGPUAsmPrinter::EmitPALMetadata(const MachineFunction &MF,
Tim Renouf72800f02017-10-03 19:03:52 +0000976 const SIProgramInfo &CurrentProgramInfo) {
977 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
978 // Given the calling convention, calculate the register number for rsrc1. In
979 // principle the register number could change in future hardware, but we know
980 // it is the same for gfx6-9 (except that LS and ES don't exist on gfx9), so
981 // we can use the same fixed value that .AMDGPU.config has for Mesa. Note
982 // that we use a register number rather than a byte offset, so we need to
983 // divide by 4.
984 unsigned Rsrc1Reg = getRsrcReg(MF.getFunction()->getCallingConv()) / 4;
985 unsigned Rsrc2Reg = Rsrc1Reg + 1;
986 // Also calculate the PAL metadata key for *S_SCRATCH_SIZE. It can be used
987 // with a constant offset to access any non-register shader-specific PAL
988 // metadata key.
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000989 unsigned ScratchSizeKey = PALMD::Key::CS_SCRATCH_SIZE;
Tim Renouf72800f02017-10-03 19:03:52 +0000990 switch (MF.getFunction()->getCallingConv()) {
991 case CallingConv::AMDGPU_PS:
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000992 ScratchSizeKey = PALMD::Key::PS_SCRATCH_SIZE;
Tim Renouf72800f02017-10-03 19:03:52 +0000993 break;
994 case CallingConv::AMDGPU_VS:
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000995 ScratchSizeKey = PALMD::Key::VS_SCRATCH_SIZE;
Tim Renouf72800f02017-10-03 19:03:52 +0000996 break;
997 case CallingConv::AMDGPU_GS:
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000998 ScratchSizeKey = PALMD::Key::GS_SCRATCH_SIZE;
Tim Renouf72800f02017-10-03 19:03:52 +0000999 break;
1000 case CallingConv::AMDGPU_ES:
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001001 ScratchSizeKey = PALMD::Key::ES_SCRATCH_SIZE;
Tim Renouf72800f02017-10-03 19:03:52 +00001002 break;
1003 case CallingConv::AMDGPU_HS:
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001004 ScratchSizeKey = PALMD::Key::HS_SCRATCH_SIZE;
Tim Renouf72800f02017-10-03 19:03:52 +00001005 break;
1006 case CallingConv::AMDGPU_LS:
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001007 ScratchSizeKey = PALMD::Key::LS_SCRATCH_SIZE;
Tim Renouf72800f02017-10-03 19:03:52 +00001008 break;
1009 }
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001010 unsigned NumUsedVgprsKey = ScratchSizeKey +
1011 PALMD::Key::VS_NUM_USED_VGPRS - PALMD::Key::VS_SCRATCH_SIZE;
1012 unsigned NumUsedSgprsKey = ScratchSizeKey +
1013 PALMD::Key::VS_NUM_USED_SGPRS - PALMD::Key::VS_SCRATCH_SIZE;
1014 PALMetadataMap[NumUsedVgprsKey] = CurrentProgramInfo.NumVGPRsForWavesPerEU;
1015 PALMetadataMap[NumUsedSgprsKey] = CurrentProgramInfo.NumSGPRsForWavesPerEU;
Tim Renouf72800f02017-10-03 19:03:52 +00001016 if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) {
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001017 PALMetadataMap[Rsrc1Reg] |= CurrentProgramInfo.ComputePGMRSrc1;
1018 PALMetadataMap[Rsrc2Reg] |= CurrentProgramInfo.ComputePGMRSrc2;
Tim Renouf72800f02017-10-03 19:03:52 +00001019 // ScratchSize is in bytes, 16 aligned.
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001020 PALMetadataMap[ScratchSizeKey] |=
1021 alignTo(CurrentProgramInfo.ScratchSize, 16);
Tim Renouf72800f02017-10-03 19:03:52 +00001022 } else {
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001023 PALMetadataMap[Rsrc1Reg] |= S_00B028_VGPRS(CurrentProgramInfo.VGPRBlocks) |
1024 S_00B028_SGPRS(CurrentProgramInfo.SGPRBlocks);
Tim Renouf72800f02017-10-03 19:03:52 +00001025 if (CurrentProgramInfo.ScratchBlocks > 0)
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001026 PALMetadataMap[Rsrc2Reg] |= S_00B84C_SCRATCH_EN(1);
Tim Renouf72800f02017-10-03 19:03:52 +00001027 // ScratchSize is in bytes, 16 aligned.
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001028 PALMetadataMap[ScratchSizeKey] |=
1029 alignTo(CurrentProgramInfo.ScratchSize, 16);
Tim Renouf72800f02017-10-03 19:03:52 +00001030 }
1031 if (MF.getFunction()->getCallingConv() == CallingConv::AMDGPU_PS) {
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001032 PALMetadataMap[Rsrc2Reg] |=
1033 S_00B02C_EXTRA_LDS_SIZE(CurrentProgramInfo.LDSBlocks);
1034 PALMetadataMap[R_0286CC_SPI_PS_INPUT_ENA / 4] |= MFI->getPSInputEnable();
1035 PALMetadataMap[R_0286D0_SPI_PS_INPUT_ADDR / 4] |= MFI->getPSInputAddr();
Tim Renouf72800f02017-10-03 19:03:52 +00001036 }
1037}
1038
Matt Arsenault24ee0782016-02-12 02:40:47 +00001039// This is supposed to be log2(Size)
1040static amd_element_byte_size_t getElementByteSizeValue(unsigned Size) {
1041 switch (Size) {
1042 case 4:
1043 return AMD_ELEMENT_4_BYTES;
1044 case 8:
1045 return AMD_ELEMENT_8_BYTES;
1046 case 16:
1047 return AMD_ELEMENT_16_BYTES;
1048 default:
1049 llvm_unreachable("invalid private_element_size");
1050 }
1051}
1052
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001053void AMDGPUAsmPrinter::getAmdKernelCode(amd_kernel_code_t &Out,
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001054 const SIProgramInfo &CurrentProgramInfo,
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001055 const MachineFunction &MF) const {
Tom Stellard45bb48e2015-06-13 03:28:10 +00001056 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001057 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +00001058
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001059 AMDGPU::initDefaultAMDKernelCodeT(Out, STM.getFeatureBits());
Tom Stellard45bb48e2015-06-13 03:28:10 +00001060
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001061 Out.compute_pgm_resource_registers =
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001062 CurrentProgramInfo.ComputePGMRSrc1 |
1063 (CurrentProgramInfo.ComputePGMRSrc2 << 32);
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001064 Out.code_properties = AMD_CODE_PROPERTY_IS_PTR64;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001065
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001066 if (CurrentProgramInfo.DynamicCallStack)
1067 Out.code_properties |= AMD_CODE_PROPERTY_IS_DYNAMIC_CALLSTACK;
1068
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001069 AMD_HSA_BITS_SET(Out.code_properties,
Matt Arsenault24ee0782016-02-12 02:40:47 +00001070 AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE,
1071 getElementByteSizeValue(STM.getMaxPrivateElementSize()));
1072
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001073 if (MFI->hasPrivateSegmentBuffer()) {
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001074 Out.code_properties |=
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001075 AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER;
1076 }
1077
1078 if (MFI->hasDispatchPtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001079 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001080
1081 if (MFI->hasQueuePtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001082 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001083
1084 if (MFI->hasKernargSegmentPtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001085 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001086
1087 if (MFI->hasDispatchID())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001088 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001089
1090 if (MFI->hasFlatScratchInit())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001091 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001092
1093 if (MFI->hasGridWorkgroupCountX()) {
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001094 Out.code_properties |=
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001095 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X;
1096 }
1097
1098 if (MFI->hasGridWorkgroupCountY()) {
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001099 Out.code_properties |=
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001100 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y;
1101 }
1102
1103 if (MFI->hasGridWorkgroupCountZ()) {
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001104 Out.code_properties |=
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001105 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z;
1106 }
Tom Stellard45bb48e2015-06-13 03:28:10 +00001107
Tom Stellard48f29f22015-11-26 00:43:29 +00001108 if (MFI->hasDispatchPtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001109 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
Tom Stellard48f29f22015-11-26 00:43:29 +00001110
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001111 if (STM.debuggerSupported())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001112 Out.code_properties |= AMD_CODE_PROPERTY_IS_DEBUG_SUPPORTED;
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001113
Nicolai Haehnle5b504972016-01-04 23:35:53 +00001114 if (STM.isXNACKEnabled())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001115 Out.code_properties |= AMD_CODE_PROPERTY_IS_XNACK_SUPPORTED;
Nicolai Haehnle5b504972016-01-04 23:35:53 +00001116
Matt Arsenault52ef4012016-07-26 16:45:58 +00001117 // FIXME: Should use getKernArgSize
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001118 Out.kernarg_segment_byte_size =
Tom Stellard2f3f9852017-01-25 01:25:13 +00001119 STM.getKernArgSegmentSize(MF, MFI->getABIArgOffset());
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001120 Out.wavefront_sgpr_count = CurrentProgramInfo.NumSGPR;
1121 Out.workitem_vgpr_count = CurrentProgramInfo.NumVGPR;
1122 Out.workitem_private_segment_byte_size = CurrentProgramInfo.ScratchSize;
1123 Out.workgroup_group_segment_byte_size = CurrentProgramInfo.LDSSize;
1124 Out.reserved_vgpr_first = CurrentProgramInfo.ReservedVGPRFirst;
1125 Out.reserved_vgpr_count = CurrentProgramInfo.ReservedVGPRCount;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001126
Tom Stellard175959e2016-12-06 21:53:10 +00001127 // These alignment values are specified in powers of two, so alignment =
1128 // 2^n. The minimum alignment is 2^4 = 16.
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001129 Out.kernarg_segment_alignment = std::max((size_t)4,
Tom Stellard175959e2016-12-06 21:53:10 +00001130 countTrailingZeros(MFI->getMaxKernArgAlign()));
1131
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001132 if (STM.debuggerEmitPrologue()) {
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001133 Out.debug_wavefront_private_segment_offset_sgpr =
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001134 CurrentProgramInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR;
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001135 Out.debug_private_segment_buffer_sgpr =
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001136 CurrentProgramInfo.DebuggerPrivateSegmentBufferSGPR;
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001137 }
Tom Stellard45bb48e2015-06-13 03:28:10 +00001138}
1139
1140bool AMDGPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1141 unsigned AsmVariant,
1142 const char *ExtraCode, raw_ostream &O) {
Matt Arsenault36cd1852017-08-09 20:09:35 +00001143 // First try the generic code, which knows about modifiers like 'c' and 'n'.
1144 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O))
1145 return false;
1146
Tom Stellard45bb48e2015-06-13 03:28:10 +00001147 if (ExtraCode && ExtraCode[0]) {
1148 if (ExtraCode[1] != 0)
1149 return true; // Unknown modifier.
1150
1151 switch (ExtraCode[0]) {
Tom Stellard45bb48e2015-06-13 03:28:10 +00001152 case 'r':
1153 break;
Matt Arsenault36cd1852017-08-09 20:09:35 +00001154 default:
1155 return true;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001156 }
1157 }
1158
Matt Arsenault36cd1852017-08-09 20:09:35 +00001159 // TODO: Should be able to support other operand types like globals.
1160 const MachineOperand &MO = MI->getOperand(OpNo);
1161 if (MO.isReg()) {
1162 AMDGPUInstPrinter::printRegOperand(MO.getReg(), O,
1163 *MF->getSubtarget().getRegisterInfo());
1164 return false;
1165 }
1166
1167 return true;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001168}