blob: 0cb9cc0ff0c3a3fe976c61d579039cf249dca53d [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"
Tom Stellard44b30b42018-05-22 02:03:23 +000024#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000025#include "MCTargetDesc/AMDGPUTargetStreamer.h"
Tom Stellardc5015012018-05-24 20:02:01 +000026#include "R600AsmPrinter.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000027#include "R600Defines.h"
28#include "R600MachineFunctionInfo.h"
29#include "R600RegisterInfo.h"
30#include "SIDefines.h"
Matt Arsenaulta9720c62016-06-20 17:51:32 +000031#include "SIInstrInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000032#include "SIMachineFunctionInfo.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000033#include "SIRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000034#include "Utils/AMDGPUBaseInfo.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000035#include "llvm/BinaryFormat/ELF.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000036#include "llvm/CodeGen/MachineFrameInfo.h"
Matt Arsenaultff982412016-06-20 18:13:04 +000037#include "llvm/IR/DiagnosticInfo.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000038#include "llvm/MC/MCContext.h"
39#include "llvm/MC/MCSectionELF.h"
40#include "llvm/MC/MCStreamer.h"
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +000041#include "llvm/Support/AMDGPUMetadata.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000042#include "llvm/Support/MathExtras.h"
43#include "llvm/Support/TargetRegistry.h"
David Blaikie6054e652018-03-23 23:58:19 +000044#include "llvm/Target/TargetLoweringObjectFile.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000045
46using namespace llvm;
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +000047using namespace llvm::AMDGPU;
Tom Stellard45bb48e2015-06-13 03:28:10 +000048
49// TODO: This should get the default rounding mode from the kernel. We just set
50// the default here, but this could change if the OpenCL rounding mode pragmas
51// are used.
52//
53// The denormal mode here should match what is reported by the OpenCL runtime
54// for the CL_FP_DENORM bit from CL_DEVICE_{HALF|SINGLE|DOUBLE}_FP_CONFIG, but
55// can also be override to flush with the -cl-denorms-are-zero compiler flag.
56//
57// AMD OpenCL only sets flush none and reports CL_FP_DENORM for double
58// precision, and leaves single precision to flush all and does not report
59// CL_FP_DENORM for CL_DEVICE_SINGLE_FP_CONFIG. Mesa's OpenCL currently reports
60// CL_FP_DENORM for both.
61//
62// FIXME: It seems some instructions do not support single precision denormals
63// regardless of the mode (exp_*_f32, rcp_*_f32, rsq_*_f32, rsq_*f32, sqrt_f32,
64// and sin_f32, cos_f32 on most parts).
65
66// We want to use these instructions, and using fp32 denormals also causes
67// instructions to run at the double precision rate for the device so it's
68// probably best to just report no single precision denormals.
69static uint32_t getFPMode(const MachineFunction &F) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +000070 const SISubtarget& ST = F.getSubtarget<SISubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +000071 // TODO: Is there any real use for the flush in only / flush out only modes?
72
73 uint32_t FP32Denormals =
74 ST.hasFP32Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
75
76 uint32_t FP64Denormals =
77 ST.hasFP64Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
78
79 return FP_ROUND_MODE_SP(FP_ROUND_ROUND_TO_NEAREST) |
80 FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_NEAREST) |
81 FP_DENORM_MODE_SP(FP32Denormals) |
82 FP_DENORM_MODE_DP(FP64Denormals);
83}
84
85static AsmPrinter *
86createAMDGPUAsmPrinterPass(TargetMachine &tm,
87 std::unique_ptr<MCStreamer> &&Streamer) {
88 return new AMDGPUAsmPrinter(tm, std::move(Streamer));
89}
90
91extern "C" void LLVMInitializeAMDGPUAsmPrinter() {
Mehdi Aminif42454b2016-10-09 23:00:34 +000092 TargetRegistry::RegisterAsmPrinter(getTheAMDGPUTarget(),
Tom Stellardc5015012018-05-24 20:02:01 +000093 llvm::createR600AsmPrinterPass);
Mehdi Aminif42454b2016-10-09 23:00:34 +000094 TargetRegistry::RegisterAsmPrinter(getTheGCNTarget(),
95 createAMDGPUAsmPrinterPass);
Tom Stellard45bb48e2015-06-13 03:28:10 +000096}
97
98AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM,
99 std::unique_ptr<MCStreamer> Streamer)
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000100 : AsmPrinter(TM, std::move(Streamer)) {
101 AMDGPUASI = static_cast<AMDGPUTargetMachine*>(&TM)->getAMDGPUAS();
102 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000103
Mehdi Amini117296c2016-10-01 02:56:57 +0000104StringRef AMDGPUAsmPrinter::getPassName() const {
Matt Arsenaultf9245b72016-07-22 17:01:25 +0000105 return "AMDGPU Assembly Printer";
106}
107
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000108const MCSubtargetInfo* AMDGPUAsmPrinter::getSTI() const {
109 return TM.getMCSubtargetInfo();
110}
111
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000112AMDGPUTargetStreamer* AMDGPUAsmPrinter::getTargetStreamer() const {
113 if (!OutStreamer)
114 return nullptr;
115 return static_cast<AMDGPUTargetStreamer*>(OutStreamer->getTargetStreamer());
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000116}
117
Tom Stellardf4218372016-01-12 17:18:17 +0000118void AMDGPUAsmPrinter::EmitStartOfAsmFile(Module &M) {
Konstantin Zhuravlyoveda425e2017-10-14 15:59:07 +0000119 if (TM.getTargetTriple().getOS() != Triple::AMDHSA &&
120 TM.getTargetTriple().getOS() != Triple::AMDPAL)
121 return;
122
123 if (TM.getTargetTriple().getOS() == Triple::AMDHSA)
124 HSAMetadataStream.begin(M);
125
126 if (TM.getTargetTriple().getOS() == Triple::AMDPAL)
127 readPALMetadata(M);
128
129 // Deprecated notes are not emitted for code object v3.
130 if (IsaInfo::hasCodeObjectV3(getSTI()->getFeatureBits()))
131 return;
132
133 // HSA emits NT_AMDGPU_HSA_CODE_OBJECT_VERSION for code objects v2.
134 if (TM.getTargetTriple().getOS() == Triple::AMDHSA)
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000135 getTargetStreamer()->EmitDirectiveHSACodeObjectVersion(2, 1);
Konstantin Zhuravlyoveda425e2017-10-14 15:59:07 +0000136
137 // HSA and PAL emit NT_AMDGPU_HSA_ISA for code objects v2.
138 IsaInfo::IsaVersion ISA = IsaInfo::getIsaVersion(getSTI()->getFeatureBits());
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000139 getTargetStreamer()->EmitDirectiveHSACodeObjectISA(
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000140 ISA.Major, ISA.Minor, ISA.Stepping, "AMD", "AMDGPU");
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000141}
142
143void AMDGPUAsmPrinter::EmitEndOfAsmFile(Module &M) {
Konstantin Zhuravlyov9c05b2b2017-10-14 15:40:33 +0000144
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000145 // Following code requires TargetStreamer to be present.
146 if (!getTargetStreamer())
147 return;
148
Konstantin Zhuravlyov9c05b2b2017-10-14 15:40:33 +0000149 // Emit ISA Version (NT_AMD_AMDGPU_ISA).
150 std::string ISAVersionString;
151 raw_string_ostream ISAVersionStream(ISAVersionString);
152 IsaInfo::streamIsaVersion(getSTI(), ISAVersionStream);
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000153 getTargetStreamer()->EmitISAVersion(ISAVersionStream.str());
Konstantin Zhuravlyov9c05b2b2017-10-14 15:40:33 +0000154
155 // Emit HSA Metadata (NT_AMD_AMDGPU_HSA_METADATA).
156 if (TM.getTargetTriple().getOS() == Triple::AMDHSA) {
157 HSAMetadataStream.end();
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000158 getTargetStreamer()->EmitHSAMetadata(HSAMetadataStream.getHSAMetadata());
Konstantin Zhuravlyov9c05b2b2017-10-14 15:40:33 +0000159 }
160
161 // Emit PAL Metadata (NT_AMD_AMDGPU_PAL_METADATA).
Tim Renouf72800f02017-10-03 19:03:52 +0000162 if (TM.getTargetTriple().getOS() == Triple::AMDPAL) {
163 // Copy the PAL metadata from the map where we collected it into a vector,
164 // then write it as a .note.
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000165 PALMD::Metadata PALMetadataVector;
166 for (auto i : PALMetadataMap) {
167 PALMetadataVector.push_back(i.first);
168 PALMetadataVector.push_back(i.second);
Tim Renouf72800f02017-10-03 19:03:52 +0000169 }
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000170 getTargetStreamer()->EmitPALMetadata(PALMetadataVector);
Tim Renouf72800f02017-10-03 19:03:52 +0000171 }
Tom Stellardf4218372016-01-12 17:18:17 +0000172}
173
Matt Arsenault6bc43d82016-10-06 16:20:41 +0000174bool AMDGPUAsmPrinter::isBlockOnlyReachableByFallthrough(
175 const MachineBasicBlock *MBB) const {
176 if (!AsmPrinter::isBlockOnlyReachableByFallthrough(MBB))
177 return false;
178
179 if (MBB->empty())
180 return true;
181
182 // If this is a block implementing a long branch, an expression relative to
183 // the start of the block is needed. to the start of the block.
184 // XXX - Is there a smarter way to check this?
185 return (MBB->back().getOpcode() != AMDGPU::S_SETPC_B64);
186}
187
Tom Stellardf151a452015-06-26 21:14:58 +0000188void AMDGPUAsmPrinter::EmitFunctionBodyStart() {
Matt Arsenault021a2182017-04-19 19:38:10 +0000189 const AMDGPUMachineFunction *MFI = MF->getInfo<AMDGPUMachineFunction>();
190 if (!MFI->isEntryFunction())
191 return;
192
Tom Stellardf151a452015-06-26 21:14:58 +0000193 const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +0000194 amd_kernel_code_t KernelCode;
Matt Arsenaultceafc552018-05-29 17:42:50 +0000195 if (STM.isAmdCodeObjectV2(MF->getFunction())) {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000196 getAmdKernelCode(KernelCode, CurrentProgramInfo, *MF);
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000197 getTargetStreamer()->EmitAMDKernelCodeT(KernelCode);
Tom Stellardf151a452015-06-26 21:14:58 +0000198 }
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000199
200 if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
201 return;
Konstantin Zhuravlyov516651b2017-10-11 22:59:35 +0000202
Matthias Braunf1caa282017-12-15 22:22:58 +0000203 HSAMetadataStream.emitKernel(MF->getFunction(),
Konstantin Zhuravlyova01d8b02017-10-14 19:03:51 +0000204 getHSACodeProps(*MF, CurrentProgramInfo),
205 getHSADebugProps(*MF, CurrentProgramInfo));
Tom Stellardf151a452015-06-26 21:14:58 +0000206}
207
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000208void AMDGPUAsmPrinter::EmitFunctionEntryLabel() {
209 const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
210 const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
Matt Arsenaultceafc552018-05-29 17:42:50 +0000211 if (MFI->isEntryFunction() && STM.isAmdCodeObjectV2(MF->getFunction())) {
Tom Stellard1b9748c2016-09-26 17:29:25 +0000212 SmallString<128> SymbolName;
Matthias Braunf1caa282017-12-15 22:22:58 +0000213 getNameWithPrefix(SymbolName, &MF->getFunction()),
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000214 getTargetStreamer()->EmitAMDGPUSymbolType(
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000215 SymbolName, ELF::STT_AMDGPU_HSA_KERNEL);
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000216 }
Tim Renoufcead41d2017-12-08 14:09:34 +0000217 const AMDGPUSubtarget &STI = MF->getSubtarget<AMDGPUSubtarget>();
218 if (STI.dumpCode()) {
219 // Disassemble function name label to text.
Matthias Braunf1caa282017-12-15 22:22:58 +0000220 DisasmLines.push_back(MF->getName().str() + ":");
Tim Renoufcead41d2017-12-08 14:09:34 +0000221 DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLines.back().size());
222 HexLines.push_back("");
223 }
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000224
225 AsmPrinter::EmitFunctionEntryLabel();
226}
227
Tim Renoufcead41d2017-12-08 14:09:34 +0000228void AMDGPUAsmPrinter::EmitBasicBlockStart(const MachineBasicBlock &MBB) const {
229 const AMDGPUSubtarget &STI = MBB.getParent()->getSubtarget<AMDGPUSubtarget>();
230 if (STI.dumpCode() && !isBlockOnlyReachableByFallthrough(&MBB)) {
231 // Write a line for the basic block label if it is not only fallthrough.
232 DisasmLines.push_back(
233 (Twine("BB") + Twine(getFunctionNumber())
234 + "_" + Twine(MBB.getNumber()) + ":").str());
235 DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLines.back().size());
236 HexLines.push_back("");
237 }
238 AsmPrinter::EmitBasicBlockStart(MBB);
239}
240
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000241void AMDGPUAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
242
Tom Stellard00f2f912015-12-02 19:47:57 +0000243 // Group segment variables aren't emitted in HSA.
Konstantin Zhuravlyov435151a2017-11-01 19:12:38 +0000244 if (AMDGPU::isGroupSegment(GV))
Tom Stellard00f2f912015-12-02 19:47:57 +0000245 return;
246
Tom Stellardfcfaea42016-05-05 17:03:33 +0000247 AsmPrinter::EmitGlobalVariable(GV);
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000248}
249
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000250bool AMDGPUAsmPrinter::doFinalization(Module &M) {
251 CallGraphResourceInfo.clear();
252 return AsmPrinter::doFinalization(M);
253}
254
Tim Renouf72800f02017-10-03 19:03:52 +0000255// For the amdpal OS type, read the amdgpu.pal.metadata supplied by the
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000256// frontend into our PALMetadataMap, ready for per-function modification. It
Tim Renouf72800f02017-10-03 19:03:52 +0000257// is a NamedMD containing an MDTuple containing a number of MDNodes each of
258// which is an integer value, and each two integer values forms a key=value
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000259// pair that we store as PALMetadataMap[key]=value in the map.
260void AMDGPUAsmPrinter::readPALMetadata(Module &M) {
Tim Renouf72800f02017-10-03 19:03:52 +0000261 auto NamedMD = M.getNamedMetadata("amdgpu.pal.metadata");
262 if (!NamedMD || !NamedMD->getNumOperands())
263 return;
264 auto Tuple = dyn_cast<MDTuple>(NamedMD->getOperand(0));
265 if (!Tuple)
266 return;
267 for (unsigned I = 0, E = Tuple->getNumOperands() & -2; I != E; I += 2) {
268 auto Key = mdconst::dyn_extract<ConstantInt>(Tuple->getOperand(I));
269 auto Val = mdconst::dyn_extract<ConstantInt>(Tuple->getOperand(I + 1));
270 if (!Key || !Val)
271 continue;
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000272 PALMetadataMap[Key->getZExtValue()] = Val->getZExtValue();
Tim Renouf72800f02017-10-03 19:03:52 +0000273 }
274}
275
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000276// Print comments that apply to both callable functions and entry points.
277void AMDGPUAsmPrinter::emitCommonFunctionComments(
278 uint32_t NumVGPR,
279 uint32_t NumSGPR,
Matt Arsenault9ba465a2017-11-14 20:33:14 +0000280 uint64_t ScratchSize,
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000281 uint64_t CodeSize,
282 const AMDGPUMachineFunction *MFI) {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000283 OutStreamer->emitRawComment(" codeLenInByte = " + Twine(CodeSize), false);
284 OutStreamer->emitRawComment(" NumSgprs: " + Twine(NumSGPR), false);
285 OutStreamer->emitRawComment(" NumVgprs: " + Twine(NumVGPR), false);
286 OutStreamer->emitRawComment(" ScratchSize: " + Twine(ScratchSize), false);
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000287 OutStreamer->emitRawComment(" MemoryBound: " + Twine(MFI->isMemoryBound()),
288 false);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000289}
290
Tom Stellard45bb48e2015-06-13 03:28:10 +0000291bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000292 CurrentProgramInfo = SIProgramInfo();
293
Matt Arsenault6cb7b8a2017-04-19 17:42:39 +0000294 const AMDGPUMachineFunction *MFI = MF.getInfo<AMDGPUMachineFunction>();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000295
296 // The starting address of all shader programs must be 256 bytes aligned.
Matt Arsenault6cb7b8a2017-04-19 17:42:39 +0000297 // Regular functions just need the basic required instruction alignment.
298 MF.setAlignment(MFI->isEntryFunction() ? 8 : 2);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000299
300 SetupMachineFunction(MF);
301
Tom Stellard45bb48e2015-06-13 03:28:10 +0000302 const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
Konstantin Zhuravlyov67a6d542017-01-06 17:02:10 +0000303 MCContext &Context = getObjFileLowering().getContext();
Tim Renouf807ecc32018-02-06 13:39:38 +0000304 // FIXME: This should be an explicit check for Mesa.
305 if (!STM.isAmdHsaOS() && !STM.isAmdPalOS()) {
Konstantin Zhuravlyov67a6d542017-01-06 17:02:10 +0000306 MCSectionELF *ConfigSection =
307 Context.getELFSection(".AMDGPU.config", ELF::SHT_PROGBITS, 0);
308 OutStreamer->SwitchSection(ConfigSection);
309 }
310
Tom Stellardc5015012018-05-24 20:02:01 +0000311 if (MFI->isEntryFunction()) {
312 getSIProgramInfo(CurrentProgramInfo, MF);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000313 } else {
Tom Stellardc5015012018-05-24 20:02:01 +0000314 auto I = CallGraphResourceInfo.insert(
315 std::make_pair(&MF.getFunction(), SIFunctionResourceInfo()));
316 SIFunctionResourceInfo &Info = I.first->second;
317 assert(I.second && "should only be called once per function");
318 Info = analyzeResourceUsage(MF);
319 }
320
321 if (STM.isAmdPalOS())
322 EmitPALMetadata(MF, CurrentProgramInfo);
323 else if (!STM.isAmdHsaOS()) {
324 EmitProgramInfoSI(MF, CurrentProgramInfo);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000325 }
326
327 DisasmLines.clear();
328 HexLines.clear();
329 DisasmLineMaxLen = 0;
330
331 EmitFunctionBody();
332
333 if (isVerbose()) {
334 MCSectionELF *CommentSection =
335 Context.getELFSection(".AMDGPU.csdata", ELF::SHT_PROGBITS, 0);
336 OutStreamer->SwitchSection(CommentSection);
337
Tom Stellardc5015012018-05-24 20:02:01 +0000338 if (!MFI->isEntryFunction()) {
339 OutStreamer->emitRawComment(" Function info:", false);
340 SIFunctionResourceInfo &Info = CallGraphResourceInfo[&MF.getFunction()];
341 emitCommonFunctionComments(
342 Info.NumVGPR,
343 Info.getTotalNumSGPRs(MF.getSubtarget<SISubtarget>()),
344 Info.PrivateSegmentSize,
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000345 getFunctionCodeSize(MF), MFI);
Tom Stellardc5015012018-05-24 20:02:01 +0000346 return false;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000347 }
Tom Stellardc5015012018-05-24 20:02:01 +0000348
349 OutStreamer->emitRawComment(" Kernel info:", false);
350 emitCommonFunctionComments(CurrentProgramInfo.NumVGPR,
351 CurrentProgramInfo.NumSGPR,
352 CurrentProgramInfo.ScratchSize,
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000353 getFunctionCodeSize(MF), MFI);
Tom Stellardc5015012018-05-24 20:02:01 +0000354
355 OutStreamer->emitRawComment(
356 " FloatMode: " + Twine(CurrentProgramInfo.FloatMode), false);
357 OutStreamer->emitRawComment(
358 " IeeeMode: " + Twine(CurrentProgramInfo.IEEEMode), false);
359 OutStreamer->emitRawComment(
360 " LDSByteSize: " + Twine(CurrentProgramInfo.LDSSize) +
361 " bytes/workgroup (compile time only)", false);
362
363 OutStreamer->emitRawComment(
364 " SGPRBlocks: " + Twine(CurrentProgramInfo.SGPRBlocks), false);
365 OutStreamer->emitRawComment(
366 " VGPRBlocks: " + Twine(CurrentProgramInfo.VGPRBlocks), false);
367
368 OutStreamer->emitRawComment(
369 " NumSGPRsForWavesPerEU: " +
370 Twine(CurrentProgramInfo.NumSGPRsForWavesPerEU), false);
371 OutStreamer->emitRawComment(
372 " NumVGPRsForWavesPerEU: " +
373 Twine(CurrentProgramInfo.NumVGPRsForWavesPerEU), false);
374
375 OutStreamer->emitRawComment(
376 " ReservedVGPRFirst: " + Twine(CurrentProgramInfo.ReservedVGPRFirst),
377 false);
378 OutStreamer->emitRawComment(
379 " ReservedVGPRCount: " + Twine(CurrentProgramInfo.ReservedVGPRCount),
380 false);
381
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000382 OutStreamer->emitRawComment(
383 " WaveLimiterHint : " + Twine(MFI->needsWaveLimiter()), false);
384
Tom Stellardc5015012018-05-24 20:02:01 +0000385 if (MF.getSubtarget<SISubtarget>().debuggerEmitPrologue()) {
386 OutStreamer->emitRawComment(
387 " DebuggerWavefrontPrivateSegmentOffsetSGPR: s" +
388 Twine(CurrentProgramInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR), false);
389 OutStreamer->emitRawComment(
390 " DebuggerPrivateSegmentBufferSGPR: s" +
391 Twine(CurrentProgramInfo.DebuggerPrivateSegmentBufferSGPR), false);
392 }
393
394 OutStreamer->emitRawComment(
395 " COMPUTE_PGM_RSRC2:USER_SGPR: " +
396 Twine(G_00B84C_USER_SGPR(CurrentProgramInfo.ComputePGMRSrc2)), false);
397 OutStreamer->emitRawComment(
398 " COMPUTE_PGM_RSRC2:TRAP_HANDLER: " +
399 Twine(G_00B84C_TRAP_HANDLER(CurrentProgramInfo.ComputePGMRSrc2)), false);
400 OutStreamer->emitRawComment(
401 " COMPUTE_PGM_RSRC2:TGID_X_EN: " +
402 Twine(G_00B84C_TGID_X_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
403 OutStreamer->emitRawComment(
404 " COMPUTE_PGM_RSRC2:TGID_Y_EN: " +
405 Twine(G_00B84C_TGID_Y_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
406 OutStreamer->emitRawComment(
407 " COMPUTE_PGM_RSRC2:TGID_Z_EN: " +
408 Twine(G_00B84C_TGID_Z_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
409 OutStreamer->emitRawComment(
410 " COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: " +
411 Twine(G_00B84C_TIDIG_COMP_CNT(CurrentProgramInfo.ComputePGMRSrc2)),
412 false);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000413 }
414
415 if (STM.dumpCode()) {
416
417 OutStreamer->SwitchSection(
418 Context.getELFSection(".AMDGPU.disasm", ELF::SHT_NOTE, 0));
419
420 for (size_t i = 0; i < DisasmLines.size(); ++i) {
Tim Renoufcead41d2017-12-08 14:09:34 +0000421 std::string Comment = "\n";
422 if (!HexLines[i].empty()) {
423 Comment = std::string(DisasmLineMaxLen - DisasmLines[i].size(), ' ');
424 Comment += " ; " + HexLines[i] + "\n";
425 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000426
427 OutStreamer->EmitBytes(StringRef(DisasmLines[i]));
428 OutStreamer->EmitBytes(StringRef(Comment));
429 }
430 }
431
432 return false;
433}
434
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000435uint64_t AMDGPUAsmPrinter::getFunctionCodeSize(const MachineFunction &MF) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000436 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000437 const SIInstrInfo *TII = STM.getInstrInfo();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000438
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000439 uint64_t CodeSize = 0;
440
Tom Stellard45bb48e2015-06-13 03:28:10 +0000441 for (const MachineBasicBlock &MBB : MF) {
442 for (const MachineInstr &MI : MBB) {
443 // TODO: CodeSize should account for multiple functions.
Matt Arsenaultc5746862015-08-12 09:04:44 +0000444
445 // TODO: Should we count size of debug info?
Shiva Chen801bf7e2018-05-09 02:42:00 +0000446 if (MI.isDebugInstr())
Matt Arsenaultc5746862015-08-12 09:04:44 +0000447 continue;
448
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000449 CodeSize += TII->getInstSizeInBytes(MI);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000450 }
451 }
452
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000453 return CodeSize;
454}
455
456static bool hasAnyNonFlatUseOfReg(const MachineRegisterInfo &MRI,
457 const SIInstrInfo &TII,
458 unsigned Reg) {
459 for (const MachineOperand &UseOp : MRI.reg_operands(Reg)) {
460 if (!UseOp.isImplicit() || !TII.isFLAT(*UseOp.getParent()))
461 return true;
462 }
463
464 return false;
465}
466
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000467static unsigned getNumExtraSGPRs(const SISubtarget &ST,
468 bool VCCUsed,
469 bool FlatScrUsed) {
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000470 unsigned ExtraSGPRs = 0;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000471 if (VCCUsed)
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000472 ExtraSGPRs = 2;
473
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000474 if (ST.getGeneration() < SISubtarget::VOLCANIC_ISLANDS) {
475 if (FlatScrUsed)
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000476 ExtraSGPRs = 4;
477 } else {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000478 if (ST.isXNACKEnabled())
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000479 ExtraSGPRs = 4;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000480
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000481 if (FlatScrUsed)
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000482 ExtraSGPRs = 6;
Tom Stellardcaaa3aa2015-12-17 17:05:09 +0000483 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000484
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000485 return ExtraSGPRs;
486}
487
488int32_t AMDGPUAsmPrinter::SIFunctionResourceInfo::getTotalNumSGPRs(
489 const SISubtarget &ST) const {
490 return NumExplicitSGPR + getNumExtraSGPRs(ST, UsesVCC, UsesFlatScratch);
491}
492
493AMDGPUAsmPrinter::SIFunctionResourceInfo AMDGPUAsmPrinter::analyzeResourceUsage(
494 const MachineFunction &MF) const {
495 SIFunctionResourceInfo Info;
496
497 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
498 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
499 const MachineFrameInfo &FrameInfo = MF.getFrameInfo();
500 const MachineRegisterInfo &MRI = MF.getRegInfo();
501 const SIInstrInfo *TII = ST.getInstrInfo();
502 const SIRegisterInfo &TRI = TII->getRegisterInfo();
503
504 Info.UsesFlatScratch = MRI.isPhysRegUsed(AMDGPU::FLAT_SCR_LO) ||
505 MRI.isPhysRegUsed(AMDGPU::FLAT_SCR_HI);
506
507 // Even if FLAT_SCRATCH is implicitly used, it has no effect if flat
508 // instructions aren't used to access the scratch buffer. Inline assembly may
509 // need it though.
510 //
511 // If we only have implicit uses of flat_scr on flat instructions, it is not
512 // really needed.
513 if (Info.UsesFlatScratch && !MFI->hasFlatScratchInit() &&
514 (!hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR) &&
515 !hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR_LO) &&
516 !hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR_HI))) {
517 Info.UsesFlatScratch = false;
518 }
519
520 Info.HasDynamicallySizedStack = FrameInfo.hasVarSizedObjects();
521 Info.PrivateSegmentSize = FrameInfo.getStackSize();
Matt Arsenault03ae3992018-03-29 21:30:06 +0000522 if (MFI->isStackRealigned())
523 Info.PrivateSegmentSize += FrameInfo.getMaxAlignment();
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000524
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000525
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000526 Info.UsesVCC = MRI.isPhysRegUsed(AMDGPU::VCC_LO) ||
527 MRI.isPhysRegUsed(AMDGPU::VCC_HI);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000528
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000529 // If there are no calls, MachineRegisterInfo can tell us the used register
530 // count easily.
Matt Arsenault22cdb612017-09-05 18:36:36 +0000531 // A tail call isn't considered a call for MachineFrameInfo's purposes.
532 if (!FrameInfo.hasCalls() && !FrameInfo.hasTailCall()) {
Matt Arsenault2738ede2017-08-02 17:15:01 +0000533 MCPhysReg HighestVGPRReg = AMDGPU::NoRegister;
534 for (MCPhysReg Reg : reverse(AMDGPU::VGPR_32RegClass.getRegisters())) {
535 if (MRI.isPhysRegUsed(Reg)) {
536 HighestVGPRReg = Reg;
537 break;
538 }
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000539 }
Matt Arsenault2738ede2017-08-02 17:15:01 +0000540
541 MCPhysReg HighestSGPRReg = AMDGPU::NoRegister;
542 for (MCPhysReg Reg : reverse(AMDGPU::SGPR_32RegClass.getRegisters())) {
543 if (MRI.isPhysRegUsed(Reg)) {
544 HighestSGPRReg = Reg;
545 break;
546 }
547 }
548
549 // We found the maximum register index. They start at 0, so add one to get the
550 // number of registers.
551 Info.NumVGPR = HighestVGPRReg == AMDGPU::NoRegister ? 0 :
552 TRI.getHWRegIndex(HighestVGPRReg) + 1;
553 Info.NumExplicitSGPR = HighestSGPRReg == AMDGPU::NoRegister ? 0 :
554 TRI.getHWRegIndex(HighestSGPRReg) + 1;
555
556 return Info;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000557 }
558
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000559 int32_t MaxVGPR = -1;
560 int32_t MaxSGPR = -1;
Matt Arsenault9ba465a2017-11-14 20:33:14 +0000561 uint64_t CalleeFrameSize = 0;
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000562
563 for (const MachineBasicBlock &MBB : MF) {
564 for (const MachineInstr &MI : MBB) {
565 // TODO: Check regmasks? Do they occur anywhere except calls?
566 for (const MachineOperand &MO : MI.operands()) {
567 unsigned Width = 0;
568 bool IsSGPR = false;
569
570 if (!MO.isReg())
571 continue;
572
573 unsigned Reg = MO.getReg();
574 switch (Reg) {
575 case AMDGPU::EXEC:
576 case AMDGPU::EXEC_LO:
577 case AMDGPU::EXEC_HI:
578 case AMDGPU::SCC:
579 case AMDGPU::M0:
580 case AMDGPU::SRC_SHARED_BASE:
581 case AMDGPU::SRC_SHARED_LIMIT:
582 case AMDGPU::SRC_PRIVATE_BASE:
583 case AMDGPU::SRC_PRIVATE_LIMIT:
584 continue;
585
586 case AMDGPU::NoRegister:
Shiva Chen801bf7e2018-05-09 02:42:00 +0000587 assert(MI.isDebugInstr());
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000588 continue;
589
590 case AMDGPU::VCC:
591 case AMDGPU::VCC_LO:
592 case AMDGPU::VCC_HI:
593 Info.UsesVCC = true;
594 continue;
595
596 case AMDGPU::FLAT_SCR:
597 case AMDGPU::FLAT_SCR_LO:
598 case AMDGPU::FLAT_SCR_HI:
599 continue;
600
Dmitry Preobrazhensky3afbd822018-01-10 14:22:19 +0000601 case AMDGPU::XNACK_MASK:
602 case AMDGPU::XNACK_MASK_LO:
603 case AMDGPU::XNACK_MASK_HI:
604 llvm_unreachable("xnack_mask registers should not be used");
605
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000606 case AMDGPU::TBA:
607 case AMDGPU::TBA_LO:
608 case AMDGPU::TBA_HI:
609 case AMDGPU::TMA:
610 case AMDGPU::TMA_LO:
611 case AMDGPU::TMA_HI:
612 llvm_unreachable("trap handler registers should not be used");
613
614 default:
615 break;
616 }
617
618 if (AMDGPU::SReg_32RegClass.contains(Reg)) {
619 assert(!AMDGPU::TTMP_32RegClass.contains(Reg) &&
620 "trap handler registers should not be used");
621 IsSGPR = true;
622 Width = 1;
623 } else if (AMDGPU::VGPR_32RegClass.contains(Reg)) {
624 IsSGPR = false;
625 Width = 1;
626 } else if (AMDGPU::SReg_64RegClass.contains(Reg)) {
627 assert(!AMDGPU::TTMP_64RegClass.contains(Reg) &&
628 "trap handler registers should not be used");
629 IsSGPR = true;
630 Width = 2;
631 } else if (AMDGPU::VReg_64RegClass.contains(Reg)) {
632 IsSGPR = false;
633 Width = 2;
634 } else if (AMDGPU::VReg_96RegClass.contains(Reg)) {
635 IsSGPR = false;
636 Width = 3;
637 } else if (AMDGPU::SReg_128RegClass.contains(Reg)) {
Dmitry Preobrazhensky27134952017-12-22 15:18:06 +0000638 assert(!AMDGPU::TTMP_128RegClass.contains(Reg) &&
639 "trap handler registers should not be used");
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000640 IsSGPR = true;
641 Width = 4;
642 } else if (AMDGPU::VReg_128RegClass.contains(Reg)) {
643 IsSGPR = false;
644 Width = 4;
645 } else if (AMDGPU::SReg_256RegClass.contains(Reg)) {
Dmitry Preobrazhensky27134952017-12-22 15:18:06 +0000646 assert(!AMDGPU::TTMP_256RegClass.contains(Reg) &&
647 "trap handler registers should not be used");
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000648 IsSGPR = true;
649 Width = 8;
650 } else if (AMDGPU::VReg_256RegClass.contains(Reg)) {
651 IsSGPR = false;
652 Width = 8;
653 } else if (AMDGPU::SReg_512RegClass.contains(Reg)) {
Dmitry Preobrazhensky27134952017-12-22 15:18:06 +0000654 assert(!AMDGPU::TTMP_512RegClass.contains(Reg) &&
655 "trap handler registers should not be used");
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000656 IsSGPR = true;
657 Width = 16;
658 } else if (AMDGPU::VReg_512RegClass.contains(Reg)) {
659 IsSGPR = false;
660 Width = 16;
661 } else {
662 llvm_unreachable("Unknown register class");
663 }
664 unsigned HWReg = TRI.getHWRegIndex(Reg);
665 int MaxUsed = HWReg + Width - 1;
666 if (IsSGPR) {
667 MaxSGPR = MaxUsed > MaxSGPR ? MaxUsed : MaxSGPR;
668 } else {
669 MaxVGPR = MaxUsed > MaxVGPR ? MaxUsed : MaxVGPR;
670 }
671 }
672
673 if (MI.isCall()) {
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000674 // Pseudo used just to encode the underlying global. Is there a better
675 // way to track this?
Matt Arsenault71bcbd42017-08-11 20:42:08 +0000676
677 const MachineOperand *CalleeOp
678 = TII->getNamedOperand(MI, AMDGPU::OpName::callee);
679 const Function *Callee = cast<Function>(CalleeOp->getGlobal());
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000680 if (Callee->isDeclaration()) {
681 // If this is a call to an external function, we can't do much. Make
682 // conservative guesses.
683
684 // 48 SGPRs - vcc, - flat_scr, -xnack
685 int MaxSGPRGuess = 47 - getNumExtraSGPRs(ST, true,
686 ST.hasFlatAddressSpace());
687 MaxSGPR = std::max(MaxSGPR, MaxSGPRGuess);
688 MaxVGPR = std::max(MaxVGPR, 23);
689
Matt Arsenault9ba465a2017-11-14 20:33:14 +0000690 CalleeFrameSize = std::max(CalleeFrameSize, UINT64_C(16384));
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000691 Info.UsesVCC = true;
692 Info.UsesFlatScratch = ST.hasFlatAddressSpace();
693 Info.HasDynamicallySizedStack = true;
694 } else {
695 // We force CodeGen to run in SCC order, so the callee's register
696 // usage etc. should be the cumulative usage of all callees.
697 auto I = CallGraphResourceInfo.find(Callee);
698 assert(I != CallGraphResourceInfo.end() &&
699 "callee should have been handled before caller");
700
701 MaxSGPR = std::max(I->second.NumExplicitSGPR - 1, MaxSGPR);
702 MaxVGPR = std::max(I->second.NumVGPR - 1, MaxVGPR);
703 CalleeFrameSize
704 = std::max(I->second.PrivateSegmentSize, CalleeFrameSize);
705 Info.UsesVCC |= I->second.UsesVCC;
706 Info.UsesFlatScratch |= I->second.UsesFlatScratch;
707 Info.HasDynamicallySizedStack |= I->second.HasDynamicallySizedStack;
708 Info.HasRecursion |= I->second.HasRecursion;
709 }
710
711 if (!Callee->doesNotRecurse())
712 Info.HasRecursion = true;
713 }
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000714 }
715 }
716
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000717 Info.NumExplicitSGPR = MaxSGPR + 1;
718 Info.NumVGPR = MaxVGPR + 1;
719 Info.PrivateSegmentSize += CalleeFrameSize;
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000720
721 return Info;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000722}
723
724void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo,
725 const MachineFunction &MF) {
726 SIFunctionResourceInfo Info = analyzeResourceUsage(MF);
727
728 ProgInfo.NumVGPR = Info.NumVGPR;
729 ProgInfo.NumSGPR = Info.NumExplicitSGPR;
730 ProgInfo.ScratchSize = Info.PrivateSegmentSize;
731 ProgInfo.VCCUsed = Info.UsesVCC;
732 ProgInfo.FlatUsed = Info.UsesFlatScratch;
733 ProgInfo.DynamicCallStack = Info.HasDynamicallySizedStack || Info.HasRecursion;
734
Matt Arsenault9ba465a2017-11-14 20:33:14 +0000735 if (!isUInt<32>(ProgInfo.ScratchSize)) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000736 DiagnosticInfoStackSize DiagStackSize(MF.getFunction(),
Matt Arsenault9ba465a2017-11-14 20:33:14 +0000737 ProgInfo.ScratchSize, DS_Error);
Matthias Braunf1caa282017-12-15 22:22:58 +0000738 MF.getFunction().getContext().diagnose(DiagStackSize);
Matt Arsenault9ba465a2017-11-14 20:33:14 +0000739 }
740
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000741 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
742 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
743 const SIInstrInfo *TII = STM.getInstrInfo();
744 const SIRegisterInfo *RI = &TII->getRegisterInfo();
745
746 unsigned ExtraSGPRs = getNumExtraSGPRs(STM,
747 ProgInfo.VCCUsed,
748 ProgInfo.FlatUsed);
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000749 unsigned ExtraVGPRs = STM.getReservedNumVGPRs(MF);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000750
Marek Olsak91f22fb2016-12-09 19:49:40 +0000751 // Check the addressable register limit before we add ExtraSGPRs.
752 if (STM.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS &&
753 !STM.hasSGPRInitBug()) {
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000754 unsigned MaxAddressableNumSGPRs = STM.getAddressableNumSGPRs();
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000755 if (ProgInfo.NumSGPR > MaxAddressableNumSGPRs) {
Marek Olsak91f22fb2016-12-09 19:49:40 +0000756 // This can happen due to a compiler bug or when using inline asm.
Matthias Braunf1caa282017-12-15 22:22:58 +0000757 LLVMContext &Ctx = MF.getFunction().getContext();
758 DiagnosticInfoResourceLimit Diag(MF.getFunction(),
Marek Olsak91f22fb2016-12-09 19:49:40 +0000759 "addressable scalar registers",
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000760 ProgInfo.NumSGPR, DS_Error,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000761 DK_ResourceLimit,
762 MaxAddressableNumSGPRs);
Marek Olsak91f22fb2016-12-09 19:49:40 +0000763 Ctx.diagnose(Diag);
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000764 ProgInfo.NumSGPR = MaxAddressableNumSGPRs - 1;
Marek Olsak91f22fb2016-12-09 19:49:40 +0000765 }
766 }
767
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000768 // Account for extra SGPRs and VGPRs reserved for debugger use.
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000769 ProgInfo.NumSGPR += ExtraSGPRs;
770 ProgInfo.NumVGPR += ExtraVGPRs;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000771
Tim Renouffd8d4af2018-04-11 17:18:36 +0000772 // Ensure there are enough SGPRs and VGPRs for wave dispatch, where wave
773 // dispatch registers are function args.
774 unsigned WaveDispatchNumSGPR = 0, WaveDispatchNumVGPR = 0;
775 for (auto &Arg : MF.getFunction().args()) {
776 unsigned NumRegs = (Arg.getType()->getPrimitiveSizeInBits() + 31) / 32;
777 if (Arg.hasAttribute(Attribute::InReg))
778 WaveDispatchNumSGPR += NumRegs;
779 else
780 WaveDispatchNumVGPR += NumRegs;
781 }
782 ProgInfo.NumSGPR = std::max(ProgInfo.NumSGPR, WaveDispatchNumSGPR);
783 ProgInfo.NumVGPR = std::max(ProgInfo.NumVGPR, WaveDispatchNumVGPR);
784
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000785 // Adjust number of registers used to meet default/requested minimum/maximum
786 // number of waves per execution unit request.
787 ProgInfo.NumSGPRsForWavesPerEU = std::max(
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000788 std::max(ProgInfo.NumSGPR, 1u), STM.getMinNumSGPRs(MFI->getMaxWavesPerEU()));
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000789 ProgInfo.NumVGPRsForWavesPerEU = std::max(
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000790 std::max(ProgInfo.NumVGPR, 1u), STM.getMinNumVGPRs(MFI->getMaxWavesPerEU()));
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000791
Marek Olsak91f22fb2016-12-09 19:49:40 +0000792 if (STM.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ||
793 STM.hasSGPRInitBug()) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000794 unsigned MaxAddressableNumSGPRs = STM.getAddressableNumSGPRs();
795 if (ProgInfo.NumSGPR > MaxAddressableNumSGPRs) {
796 // This can happen due to a compiler bug or when using inline asm to use
797 // the registers which are usually reserved for vcc etc.
Matthias Braunf1caa282017-12-15 22:22:58 +0000798 LLVMContext &Ctx = MF.getFunction().getContext();
799 DiagnosticInfoResourceLimit Diag(MF.getFunction(),
Marek Olsak91f22fb2016-12-09 19:49:40 +0000800 "scalar registers",
801 ProgInfo.NumSGPR, DS_Error,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000802 DK_ResourceLimit,
803 MaxAddressableNumSGPRs);
Marek Olsak91f22fb2016-12-09 19:49:40 +0000804 Ctx.diagnose(Diag);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000805 ProgInfo.NumSGPR = MaxAddressableNumSGPRs;
806 ProgInfo.NumSGPRsForWavesPerEU = MaxAddressableNumSGPRs;
Marek Olsak91f22fb2016-12-09 19:49:40 +0000807 }
Matt Arsenault4eae3012016-10-28 20:31:47 +0000808 }
809
810 if (STM.hasSGPRInitBug()) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000811 ProgInfo.NumSGPR =
812 AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG;
813 ProgInfo.NumSGPRsForWavesPerEU =
814 AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000815 }
816
Matt Arsenault161e2b42017-04-18 20:59:40 +0000817 if (MFI->getNumUserSGPRs() > STM.getMaxNumUserSGPRs()) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000818 LLVMContext &Ctx = MF.getFunction().getContext();
819 DiagnosticInfoResourceLimit Diag(MF.getFunction(), "user SGPRs",
Matt Arsenault161e2b42017-04-18 20:59:40 +0000820 MFI->getNumUserSGPRs(), DS_Error);
Matt Arsenaultff982412016-06-20 18:13:04 +0000821 Ctx.diagnose(Diag);
Matt Arsenault41003af2015-11-30 21:16:07 +0000822 }
823
Matt Arsenault52ef4012016-07-26 16:45:58 +0000824 if (MFI->getLDSSize() > static_cast<unsigned>(STM.getLocalMemorySize())) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000825 LLVMContext &Ctx = MF.getFunction().getContext();
826 DiagnosticInfoResourceLimit Diag(MF.getFunction(), "local memory",
Matt Arsenault52ef4012016-07-26 16:45:58 +0000827 MFI->getLDSSize(), DS_Error);
Matt Arsenaultff982412016-06-20 18:13:04 +0000828 Ctx.diagnose(Diag);
Matt Arsenault1c4d0ef2016-04-28 19:37:35 +0000829 }
830
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000831 // SGPRBlocks is actual number of SGPR blocks minus 1.
832 ProgInfo.SGPRBlocks = alignTo(ProgInfo.NumSGPRsForWavesPerEU,
Konstantin Zhuravlyove22fbcb2017-02-08 13:18:40 +0000833 STM.getSGPREncodingGranule());
834 ProgInfo.SGPRBlocks = ProgInfo.SGPRBlocks / STM.getSGPREncodingGranule() - 1;
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000835
836 // VGPRBlocks is actual number of VGPR blocks minus 1.
837 ProgInfo.VGPRBlocks = alignTo(ProgInfo.NumVGPRsForWavesPerEU,
Konstantin Zhuravlyove22fbcb2017-02-08 13:18:40 +0000838 STM.getVGPREncodingGranule());
839 ProgInfo.VGPRBlocks = ProgInfo.VGPRBlocks / STM.getVGPREncodingGranule() - 1;
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000840
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000841 // Record first reserved VGPR and number of reserved VGPRs.
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000842 ProgInfo.ReservedVGPRFirst = STM.debuggerReserveRegs() ? ProgInfo.NumVGPR : 0;
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000843 ProgInfo.ReservedVGPRCount = STM.getReservedNumVGPRs(MF);
844
845 // Update DebuggerWavefrontPrivateSegmentOffsetSGPR and
846 // DebuggerPrivateSegmentBufferSGPR fields if "amdgpu-debugger-emit-prologue"
847 // attribute was requested.
848 if (STM.debuggerEmitPrologue()) {
849 ProgInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR =
850 RI->getHWRegIndex(MFI->getScratchWaveOffsetReg());
851 ProgInfo.DebuggerPrivateSegmentBufferSGPR =
852 RI->getHWRegIndex(MFI->getScratchRSrcReg());
853 }
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000854
Tom Stellard45bb48e2015-06-13 03:28:10 +0000855 // Set the value to initialize FP_ROUND and FP_DENORM parts of the mode
856 // register.
857 ProgInfo.FloatMode = getFPMode(MF);
858
Wei Ding3cb2a1e2016-10-19 22:34:49 +0000859 ProgInfo.IEEEMode = STM.enableIEEEBit(MF);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000860
Matt Arsenault7293f982016-01-28 20:53:35 +0000861 // Make clamp modifier on NaN input returns 0.
Matt Arsenault2fdf2a12017-02-21 23:35:48 +0000862 ProgInfo.DX10Clamp = STM.enableDX10Clamp();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000863
Tom Stellard45bb48e2015-06-13 03:28:10 +0000864 unsigned LDSAlignShift;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000865 if (STM.getGeneration() < SISubtarget::SEA_ISLANDS) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000866 // LDS is allocated in 64 dword blocks.
867 LDSAlignShift = 8;
868 } else {
869 // LDS is allocated in 128 dword blocks.
870 LDSAlignShift = 9;
871 }
872
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000873 unsigned LDSSpillSize =
Matt Arsenault161e2b42017-04-18 20:59:40 +0000874 MFI->getLDSWaveSpillSize() * MFI->getMaxFlatWorkGroupSize();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000875
Matt Arsenault52ef4012016-07-26 16:45:58 +0000876 ProgInfo.LDSSize = MFI->getLDSSize() + LDSSpillSize;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000877 ProgInfo.LDSBlocks =
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +0000878 alignTo(ProgInfo.LDSSize, 1ULL << LDSAlignShift) >> LDSAlignShift;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000879
880 // Scratch is allocated in 256 dword blocks.
881 unsigned ScratchAlignShift = 10;
882 // We need to program the hardware with the amount of scratch memory that
883 // is used by the entire wave. ProgInfo.ScratchSize is the amount of
884 // scratch memory used per thread.
885 ProgInfo.ScratchBlocks =
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000886 alignTo(ProgInfo.ScratchSize * STM.getWavefrontSize(),
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +0000887 1ULL << ScratchAlignShift) >>
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000888 ScratchAlignShift;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000889
890 ProgInfo.ComputePGMRSrc1 =
891 S_00B848_VGPRS(ProgInfo.VGPRBlocks) |
892 S_00B848_SGPRS(ProgInfo.SGPRBlocks) |
893 S_00B848_PRIORITY(ProgInfo.Priority) |
894 S_00B848_FLOAT_MODE(ProgInfo.FloatMode) |
895 S_00B848_PRIV(ProgInfo.Priv) |
896 S_00B848_DX10_CLAMP(ProgInfo.DX10Clamp) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000897 S_00B848_DEBUG_MODE(ProgInfo.DebugMode) |
Tom Stellard45bb48e2015-06-13 03:28:10 +0000898 S_00B848_IEEE_MODE(ProgInfo.IEEEMode);
899
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000900 // 0 = X, 1 = XY, 2 = XYZ
901 unsigned TIDIGCompCnt = 0;
902 if (MFI->hasWorkItemIDZ())
903 TIDIGCompCnt = 2;
904 else if (MFI->hasWorkItemIDY())
905 TIDIGCompCnt = 1;
906
Tom Stellard45bb48e2015-06-13 03:28:10 +0000907 ProgInfo.ComputePGMRSrc2 =
908 S_00B84C_SCRATCH_EN(ProgInfo.ScratchBlocks > 0) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000909 S_00B84C_USER_SGPR(MFI->getNumUserSGPRs()) |
Konstantin Zhuravlyov2ca6b1f2018-05-29 19:09:13 +0000910 // For AMDHSA, TRAP_HANDLER must be zero, as it is populated by the CP.
911 S_00B84C_TRAP_HANDLER(STM.isAmdHsaOS() ? 0 : STM.isTrapHandlerEnabled()) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000912 S_00B84C_TGID_X_EN(MFI->hasWorkGroupIDX()) |
913 S_00B84C_TGID_Y_EN(MFI->hasWorkGroupIDY()) |
914 S_00B84C_TGID_Z_EN(MFI->hasWorkGroupIDZ()) |
915 S_00B84C_TG_SIZE_EN(MFI->hasWorkGroupInfo()) |
916 S_00B84C_TIDIG_COMP_CNT(TIDIGCompCnt) |
917 S_00B84C_EXCP_EN_MSB(0) |
Konstantin Zhuravlyov6ccb0762017-05-05 20:13:55 +0000918 // For AMDHSA, LDS_SIZE must be zero, as it is populated by the CP.
919 S_00B84C_LDS_SIZE(STM.isAmdHsaOS() ? 0 : ProgInfo.LDSBlocks) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000920 S_00B84C_EXCP_EN(0);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000921}
922
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000923static unsigned getRsrcReg(CallingConv::ID CallConv) {
924 switch (CallConv) {
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000925 default: LLVM_FALLTHROUGH;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000926 case CallingConv::AMDGPU_CS: return R_00B848_COMPUTE_PGM_RSRC1;
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000927 case CallingConv::AMDGPU_LS: return R_00B528_SPI_SHADER_PGM_RSRC1_LS;
Marek Olsaka302a7362017-05-02 15:41:10 +0000928 case CallingConv::AMDGPU_HS: return R_00B428_SPI_SHADER_PGM_RSRC1_HS;
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000929 case CallingConv::AMDGPU_ES: return R_00B328_SPI_SHADER_PGM_RSRC1_ES;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000930 case CallingConv::AMDGPU_GS: return R_00B228_SPI_SHADER_PGM_RSRC1_GS;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000931 case CallingConv::AMDGPU_VS: return R_00B128_SPI_SHADER_PGM_RSRC1_VS;
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000932 case CallingConv::AMDGPU_PS: return R_00B028_SPI_SHADER_PGM_RSRC1_PS;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000933 }
934}
935
936void AMDGPUAsmPrinter::EmitProgramInfoSI(const MachineFunction &MF,
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000937 const SIProgramInfo &CurrentProgramInfo) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000938 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000939 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Matthias Braunf1caa282017-12-15 22:22:58 +0000940 unsigned RsrcReg = getRsrcReg(MF.getFunction().getCallingConv());
Tom Stellard45bb48e2015-06-13 03:28:10 +0000941
Matthias Braunf1caa282017-12-15 22:22:58 +0000942 if (AMDGPU::isCompute(MF.getFunction().getCallingConv())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000943 OutStreamer->EmitIntValue(R_00B848_COMPUTE_PGM_RSRC1, 4);
944
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000945 OutStreamer->EmitIntValue(CurrentProgramInfo.ComputePGMRSrc1, 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000946
947 OutStreamer->EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000948 OutStreamer->EmitIntValue(CurrentProgramInfo.ComputePGMRSrc2, 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000949
950 OutStreamer->EmitIntValue(R_00B860_COMPUTE_TMPRING_SIZE, 4);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000951 OutStreamer->EmitIntValue(S_00B860_WAVESIZE(CurrentProgramInfo.ScratchBlocks), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000952
953 // TODO: Should probably note flat usage somewhere. SC emits a "FlatPtr32 =
954 // 0" comment but I don't see a corresponding field in the register spec.
955 } else {
956 OutStreamer->EmitIntValue(RsrcReg, 4);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000957 OutStreamer->EmitIntValue(S_00B028_VGPRS(CurrentProgramInfo.VGPRBlocks) |
958 S_00B028_SGPRS(CurrentProgramInfo.SGPRBlocks), 4);
Matthias Braunf1caa282017-12-15 22:22:58 +0000959 if (STM.isVGPRSpillingEnabled(MF.getFunction())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000960 OutStreamer->EmitIntValue(R_0286E8_SPI_TMPRING_SIZE, 4);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000961 OutStreamer->EmitIntValue(S_0286E8_WAVESIZE(CurrentProgramInfo.ScratchBlocks), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000962 }
Tim Renouf807ecc32018-02-06 13:39:38 +0000963 }
964
965 if (MF.getFunction().getCallingConv() == CallingConv::AMDGPU_PS) {
966 OutStreamer->EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4);
967 OutStreamer->EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(CurrentProgramInfo.LDSBlocks), 4);
968 OutStreamer->EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4);
969 OutStreamer->EmitIntValue(MFI->getPSInputEnable(), 4);
970 OutStreamer->EmitIntValue(R_0286D0_SPI_PS_INPUT_ADDR, 4);
971 OutStreamer->EmitIntValue(MFI->getPSInputAddr(), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000972 }
Marek Olsak0532c192016-07-13 17:35:15 +0000973
974 OutStreamer->EmitIntValue(R_SPILLED_SGPRS, 4);
975 OutStreamer->EmitIntValue(MFI->getNumSpilledSGPRs(), 4);
976 OutStreamer->EmitIntValue(R_SPILLED_VGPRS, 4);
977 OutStreamer->EmitIntValue(MFI->getNumSpilledVGPRs(), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000978}
979
Tim Renouf72800f02017-10-03 19:03:52 +0000980// This is the equivalent of EmitProgramInfoSI above, but for when the OS type
981// is AMDPAL. It stores each compute/SPI register setting and other PAL
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000982// metadata items into the PALMetadataMap, combining with any provided by the
983// frontend as LLVM metadata. Once all functions are written, PALMetadataMap is
Tim Renouf72800f02017-10-03 19:03:52 +0000984// then written as a single block in the .note section.
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000985void AMDGPUAsmPrinter::EmitPALMetadata(const MachineFunction &MF,
Tim Renouf72800f02017-10-03 19:03:52 +0000986 const SIProgramInfo &CurrentProgramInfo) {
987 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
988 // Given the calling convention, calculate the register number for rsrc1. In
989 // principle the register number could change in future hardware, but we know
990 // it is the same for gfx6-9 (except that LS and ES don't exist on gfx9), so
991 // we can use the same fixed value that .AMDGPU.config has for Mesa. Note
992 // that we use a register number rather than a byte offset, so we need to
993 // divide by 4.
Matthias Braunf1caa282017-12-15 22:22:58 +0000994 unsigned Rsrc1Reg = getRsrcReg(MF.getFunction().getCallingConv()) / 4;
Tim Renouf72800f02017-10-03 19:03:52 +0000995 unsigned Rsrc2Reg = Rsrc1Reg + 1;
996 // Also calculate the PAL metadata key for *S_SCRATCH_SIZE. It can be used
997 // with a constant offset to access any non-register shader-specific PAL
998 // metadata key.
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +0000999 unsigned ScratchSizeKey = PALMD::Key::CS_SCRATCH_SIZE;
Matthias Braunf1caa282017-12-15 22:22:58 +00001000 switch (MF.getFunction().getCallingConv()) {
Tim Renouf72800f02017-10-03 19:03:52 +00001001 case CallingConv::AMDGPU_PS:
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001002 ScratchSizeKey = PALMD::Key::PS_SCRATCH_SIZE;
Tim Renouf72800f02017-10-03 19:03:52 +00001003 break;
1004 case CallingConv::AMDGPU_VS:
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001005 ScratchSizeKey = PALMD::Key::VS_SCRATCH_SIZE;
Tim Renouf72800f02017-10-03 19:03:52 +00001006 break;
1007 case CallingConv::AMDGPU_GS:
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001008 ScratchSizeKey = PALMD::Key::GS_SCRATCH_SIZE;
Tim Renouf72800f02017-10-03 19:03:52 +00001009 break;
1010 case CallingConv::AMDGPU_ES:
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001011 ScratchSizeKey = PALMD::Key::ES_SCRATCH_SIZE;
Tim Renouf72800f02017-10-03 19:03:52 +00001012 break;
1013 case CallingConv::AMDGPU_HS:
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001014 ScratchSizeKey = PALMD::Key::HS_SCRATCH_SIZE;
Tim Renouf72800f02017-10-03 19:03:52 +00001015 break;
1016 case CallingConv::AMDGPU_LS:
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001017 ScratchSizeKey = PALMD::Key::LS_SCRATCH_SIZE;
Tim Renouf72800f02017-10-03 19:03:52 +00001018 break;
1019 }
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001020 unsigned NumUsedVgprsKey = ScratchSizeKey +
1021 PALMD::Key::VS_NUM_USED_VGPRS - PALMD::Key::VS_SCRATCH_SIZE;
1022 unsigned NumUsedSgprsKey = ScratchSizeKey +
1023 PALMD::Key::VS_NUM_USED_SGPRS - PALMD::Key::VS_SCRATCH_SIZE;
1024 PALMetadataMap[NumUsedVgprsKey] = CurrentProgramInfo.NumVGPRsForWavesPerEU;
1025 PALMetadataMap[NumUsedSgprsKey] = CurrentProgramInfo.NumSGPRsForWavesPerEU;
Matthias Braunf1caa282017-12-15 22:22:58 +00001026 if (AMDGPU::isCompute(MF.getFunction().getCallingConv())) {
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001027 PALMetadataMap[Rsrc1Reg] |= CurrentProgramInfo.ComputePGMRSrc1;
1028 PALMetadataMap[Rsrc2Reg] |= CurrentProgramInfo.ComputePGMRSrc2;
Tim Renouf72800f02017-10-03 19:03:52 +00001029 // ScratchSize is in bytes, 16 aligned.
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001030 PALMetadataMap[ScratchSizeKey] |=
1031 alignTo(CurrentProgramInfo.ScratchSize, 16);
Tim Renouf72800f02017-10-03 19:03:52 +00001032 } else {
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001033 PALMetadataMap[Rsrc1Reg] |= S_00B028_VGPRS(CurrentProgramInfo.VGPRBlocks) |
1034 S_00B028_SGPRS(CurrentProgramInfo.SGPRBlocks);
Tim Renouf72800f02017-10-03 19:03:52 +00001035 if (CurrentProgramInfo.ScratchBlocks > 0)
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001036 PALMetadataMap[Rsrc2Reg] |= S_00B84C_SCRATCH_EN(1);
Tim Renouf72800f02017-10-03 19:03:52 +00001037 // ScratchSize is in bytes, 16 aligned.
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001038 PALMetadataMap[ScratchSizeKey] |=
1039 alignTo(CurrentProgramInfo.ScratchSize, 16);
Tim Renouf72800f02017-10-03 19:03:52 +00001040 }
Matthias Braunf1caa282017-12-15 22:22:58 +00001041 if (MF.getFunction().getCallingConv() == CallingConv::AMDGPU_PS) {
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001042 PALMetadataMap[Rsrc2Reg] |=
1043 S_00B02C_EXTRA_LDS_SIZE(CurrentProgramInfo.LDSBlocks);
1044 PALMetadataMap[R_0286CC_SPI_PS_INPUT_ENA / 4] |= MFI->getPSInputEnable();
1045 PALMetadataMap[R_0286D0_SPI_PS_INPUT_ADDR / 4] |= MFI->getPSInputAddr();
Tim Renouf72800f02017-10-03 19:03:52 +00001046 }
1047}
1048
Matt Arsenault24ee0782016-02-12 02:40:47 +00001049// This is supposed to be log2(Size)
1050static amd_element_byte_size_t getElementByteSizeValue(unsigned Size) {
1051 switch (Size) {
1052 case 4:
1053 return AMD_ELEMENT_4_BYTES;
1054 case 8:
1055 return AMD_ELEMENT_8_BYTES;
1056 case 16:
1057 return AMD_ELEMENT_16_BYTES;
1058 default:
1059 llvm_unreachable("invalid private_element_size");
1060 }
1061}
1062
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001063void AMDGPUAsmPrinter::getAmdKernelCode(amd_kernel_code_t &Out,
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001064 const SIProgramInfo &CurrentProgramInfo,
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001065 const MachineFunction &MF) const {
Tom Stellard45bb48e2015-06-13 03:28:10 +00001066 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001067 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +00001068
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001069 AMDGPU::initDefaultAMDKernelCodeT(Out, STM.getFeatureBits());
Tom Stellard45bb48e2015-06-13 03:28:10 +00001070
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001071 Out.compute_pgm_resource_registers =
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001072 CurrentProgramInfo.ComputePGMRSrc1 |
1073 (CurrentProgramInfo.ComputePGMRSrc2 << 32);
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001074 Out.code_properties = AMD_CODE_PROPERTY_IS_PTR64;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001075
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001076 if (CurrentProgramInfo.DynamicCallStack)
1077 Out.code_properties |= AMD_CODE_PROPERTY_IS_DYNAMIC_CALLSTACK;
1078
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001079 AMD_HSA_BITS_SET(Out.code_properties,
Matt Arsenault24ee0782016-02-12 02:40:47 +00001080 AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE,
1081 getElementByteSizeValue(STM.getMaxPrivateElementSize()));
1082
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001083 if (MFI->hasPrivateSegmentBuffer()) {
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001084 Out.code_properties |=
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001085 AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER;
1086 }
1087
1088 if (MFI->hasDispatchPtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001089 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001090
1091 if (MFI->hasQueuePtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001092 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001093
1094 if (MFI->hasKernargSegmentPtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001095 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001096
1097 if (MFI->hasDispatchID())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001098 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001099
1100 if (MFI->hasFlatScratchInit())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001101 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001102
1103 if (MFI->hasGridWorkgroupCountX()) {
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_X;
1106 }
1107
1108 if (MFI->hasGridWorkgroupCountY()) {
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001109 Out.code_properties |=
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001110 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y;
1111 }
1112
1113 if (MFI->hasGridWorkgroupCountZ()) {
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001114 Out.code_properties |=
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001115 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z;
1116 }
Tom Stellard45bb48e2015-06-13 03:28:10 +00001117
Tom Stellard48f29f22015-11-26 00:43:29 +00001118 if (MFI->hasDispatchPtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001119 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
Tom Stellard48f29f22015-11-26 00:43:29 +00001120
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001121 if (STM.debuggerSupported())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001122 Out.code_properties |= AMD_CODE_PROPERTY_IS_DEBUG_SUPPORTED;
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001123
Nicolai Haehnle5b504972016-01-04 23:35:53 +00001124 if (STM.isXNACKEnabled())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001125 Out.code_properties |= AMD_CODE_PROPERTY_IS_XNACK_SUPPORTED;
Nicolai Haehnle5b504972016-01-04 23:35:53 +00001126
Matt Arsenault52ef4012016-07-26 16:45:58 +00001127 // FIXME: Should use getKernArgSize
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001128 Out.kernarg_segment_byte_size =
Matt Arsenaultceafc552018-05-29 17:42:50 +00001129 STM.getKernArgSegmentSize(MF.getFunction(), MFI->getABIArgOffset());
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001130 Out.wavefront_sgpr_count = CurrentProgramInfo.NumSGPR;
1131 Out.workitem_vgpr_count = CurrentProgramInfo.NumVGPR;
1132 Out.workitem_private_segment_byte_size = CurrentProgramInfo.ScratchSize;
1133 Out.workgroup_group_segment_byte_size = CurrentProgramInfo.LDSSize;
1134 Out.reserved_vgpr_first = CurrentProgramInfo.ReservedVGPRFirst;
1135 Out.reserved_vgpr_count = CurrentProgramInfo.ReservedVGPRCount;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001136
Tom Stellard175959e2016-12-06 21:53:10 +00001137 // These alignment values are specified in powers of two, so alignment =
1138 // 2^n. The minimum alignment is 2^4 = 16.
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001139 Out.kernarg_segment_alignment = std::max((size_t)4,
Tom Stellard175959e2016-12-06 21:53:10 +00001140 countTrailingZeros(MFI->getMaxKernArgAlign()));
1141
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001142 if (STM.debuggerEmitPrologue()) {
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001143 Out.debug_wavefront_private_segment_offset_sgpr =
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001144 CurrentProgramInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR;
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001145 Out.debug_private_segment_buffer_sgpr =
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001146 CurrentProgramInfo.DebuggerPrivateSegmentBufferSGPR;
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001147 }
Tom Stellard45bb48e2015-06-13 03:28:10 +00001148}
1149
Konstantin Zhuravlyova01d8b02017-10-14 19:03:51 +00001150AMDGPU::HSAMD::Kernel::CodeProps::Metadata AMDGPUAsmPrinter::getHSACodeProps(
1151 const MachineFunction &MF,
1152 const SIProgramInfo &ProgramInfo) const {
1153 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
1154 const SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>();
1155 HSAMD::Kernel::CodeProps::Metadata HSACodeProps;
1156
1157 HSACodeProps.mKernargSegmentSize =
Matt Arsenaultceafc552018-05-29 17:42:50 +00001158 STM.getKernArgSegmentSize(MF.getFunction(), MFI.getABIArgOffset());
Konstantin Zhuravlyova01d8b02017-10-14 19:03:51 +00001159 HSACodeProps.mGroupSegmentFixedSize = ProgramInfo.LDSSize;
1160 HSACodeProps.mPrivateSegmentFixedSize = ProgramInfo.ScratchSize;
1161 HSACodeProps.mKernargSegmentAlign =
1162 std::max(uint32_t(4), MFI.getMaxKernArgAlign());
1163 HSACodeProps.mWavefrontSize = STM.getWavefrontSize();
1164 HSACodeProps.mNumSGPRs = CurrentProgramInfo.NumSGPR;
1165 HSACodeProps.mNumVGPRs = CurrentProgramInfo.NumVGPR;
Konstantin Zhuravlyov8d5e9e12017-10-18 17:31:09 +00001166 HSACodeProps.mMaxFlatWorkGroupSize = MFI.getMaxFlatWorkGroupSize();
Konstantin Zhuravlyova01d8b02017-10-14 19:03:51 +00001167 HSACodeProps.mIsDynamicCallStack = ProgramInfo.DynamicCallStack;
1168 HSACodeProps.mIsXNACKEnabled = STM.isXNACKEnabled();
Konstantin Zhuravlyov06ae4ec2017-11-28 17:51:08 +00001169 HSACodeProps.mNumSpilledSGPRs = MFI.getNumSpilledSGPRs();
1170 HSACodeProps.mNumSpilledVGPRs = MFI.getNumSpilledVGPRs();
Konstantin Zhuravlyova01d8b02017-10-14 19:03:51 +00001171
1172 return HSACodeProps;
1173}
1174
1175AMDGPU::HSAMD::Kernel::DebugProps::Metadata AMDGPUAsmPrinter::getHSADebugProps(
1176 const MachineFunction &MF,
1177 const SIProgramInfo &ProgramInfo) const {
1178 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
1179 HSAMD::Kernel::DebugProps::Metadata HSADebugProps;
1180
1181 if (!STM.debuggerSupported())
1182 return HSADebugProps;
1183
1184 HSADebugProps.mDebuggerABIVersion.push_back(1);
1185 HSADebugProps.mDebuggerABIVersion.push_back(0);
1186 HSADebugProps.mReservedNumVGPRs = ProgramInfo.ReservedVGPRCount;
1187 HSADebugProps.mReservedFirstVGPR = ProgramInfo.ReservedVGPRFirst;
1188
1189 if (STM.debuggerEmitPrologue()) {
1190 HSADebugProps.mPrivateSegmentBufferSGPR =
1191 ProgramInfo.DebuggerPrivateSegmentBufferSGPR;
1192 HSADebugProps.mWavefrontPrivateSegmentOffsetSGPR =
1193 ProgramInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR;
1194 }
1195
1196 return HSADebugProps;
1197}
1198
Tom Stellard45bb48e2015-06-13 03:28:10 +00001199bool AMDGPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1200 unsigned AsmVariant,
1201 const char *ExtraCode, raw_ostream &O) {
Matt Arsenault36cd1852017-08-09 20:09:35 +00001202 // First try the generic code, which knows about modifiers like 'c' and 'n'.
1203 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O))
1204 return false;
1205
Tom Stellard45bb48e2015-06-13 03:28:10 +00001206 if (ExtraCode && ExtraCode[0]) {
1207 if (ExtraCode[1] != 0)
1208 return true; // Unknown modifier.
1209
1210 switch (ExtraCode[0]) {
Tom Stellard45bb48e2015-06-13 03:28:10 +00001211 case 'r':
1212 break;
Matt Arsenault36cd1852017-08-09 20:09:35 +00001213 default:
1214 return true;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001215 }
1216 }
1217
Matt Arsenault36cd1852017-08-09 20:09:35 +00001218 // TODO: Should be able to support other operand types like globals.
1219 const MachineOperand &MO = MI->getOperand(OpNo);
1220 if (MO.isReg()) {
1221 AMDGPUInstPrinter::printRegOperand(MO.getReg(), O,
1222 *MF->getSubtarget().getRegisterInfo());
1223 return false;
1224 }
1225
1226 return true;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001227}