blob: 4b1d9cb5059f21237c54d1fe1ece82b1ace3228e [file] [log] [blame]
Konstantin Zhuravlyov30f03b32018-06-27 05:36:03 +00001//===-- AMDGPUAsmPrinter.cpp - AMDGPU assembly printer -------------------===//
Tom Stellard45bb48e2015-06-13 03:28:10 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Tom Stellard45bb48e2015-06-13 03:28:10 +00006//
7//===----------------------------------------------------------------------===//
8//
9/// \file
10///
11/// The AMDGPUAsmPrinter is used to print both assembly string and also binary
12/// code. When passed an MCAsmStreamer it prints assembly and when passed
13/// an MCObjectStreamer it outputs binary code.
14//
15//===----------------------------------------------------------------------===//
16//
17
18#include "AMDGPUAsmPrinter.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000019#include "AMDGPU.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000020#include "AMDGPUSubtarget.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000021#include "AMDGPUTargetMachine.h"
Richard Trieuc0bd7bd2019-05-11 00:03:35 +000022#include "MCTargetDesc/AMDGPUInstPrinter.h"
Tom Stellard44b30b42018-05-22 02:03:23 +000023#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000024#include "MCTargetDesc/AMDGPUTargetStreamer.h"
Tom Stellardc5015012018-05-24 20:02:01 +000025#include "R600AsmPrinter.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000026#include "R600Defines.h"
27#include "R600MachineFunctionInfo.h"
28#include "R600RegisterInfo.h"
29#include "SIDefines.h"
Matt Arsenaulta9720c62016-06-20 17:51:32 +000030#include "SIInstrInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000031#include "SIMachineFunctionInfo.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000032#include "SIRegisterInfo.h"
Richard Trieu8ce2ee92019-05-14 21:54:37 +000033#include "TargetInfo/AMDGPUTargetInfo.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"
Tim Renouf33cb8f52019-05-14 16:17:14 +000038#include "llvm/MC/MCAssembler.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000039#include "llvm/MC/MCContext.h"
40#include "llvm/MC/MCSectionELF.h"
41#include "llvm/MC/MCStreamer.h"
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +000042#include "llvm/Support/AMDGPUMetadata.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000043#include "llvm/Support/MathExtras.h"
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +000044#include "llvm/Support/TargetParser.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000045#include "llvm/Support/TargetRegistry.h"
David Blaikie6054e652018-03-23 23:58:19 +000046#include "llvm/Target/TargetLoweringObjectFile.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000047
48using namespace llvm;
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +000049using namespace llvm::AMDGPU;
Scott Linderf5b36e52018-12-12 19:39:27 +000050using namespace llvm::AMDGPU::HSAMD;
Tom Stellard45bb48e2015-06-13 03:28:10 +000051
52// TODO: This should get the default rounding mode from the kernel. We just set
53// the default here, but this could change if the OpenCL rounding mode pragmas
54// are used.
55//
56// The denormal mode here should match what is reported by the OpenCL runtime
57// for the CL_FP_DENORM bit from CL_DEVICE_{HALF|SINGLE|DOUBLE}_FP_CONFIG, but
58// can also be override to flush with the -cl-denorms-are-zero compiler flag.
59//
60// AMD OpenCL only sets flush none and reports CL_FP_DENORM for double
61// precision, and leaves single precision to flush all and does not report
62// CL_FP_DENORM for CL_DEVICE_SINGLE_FP_CONFIG. Mesa's OpenCL currently reports
63// CL_FP_DENORM for both.
64//
65// FIXME: It seems some instructions do not support single precision denormals
66// regardless of the mode (exp_*_f32, rcp_*_f32, rsq_*_f32, rsq_*f32, sqrt_f32,
67// and sin_f32, cos_f32 on most parts).
68
69// We want to use these instructions, and using fp32 denormals also causes
70// instructions to run at the double precision rate for the device so it's
71// probably best to just report no single precision denormals.
72static uint32_t getFPMode(const MachineFunction &F) {
Tom Stellard5bfbae52018-07-11 20:59:01 +000073 const GCNSubtarget& ST = F.getSubtarget<GCNSubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +000074 // TODO: Is there any real use for the flush in only / flush out only modes?
75
76 uint32_t FP32Denormals =
77 ST.hasFP32Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
78
79 uint32_t FP64Denormals =
80 ST.hasFP64Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
81
82 return FP_ROUND_MODE_SP(FP_ROUND_ROUND_TO_NEAREST) |
83 FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_NEAREST) |
84 FP_DENORM_MODE_SP(FP32Denormals) |
85 FP_DENORM_MODE_DP(FP64Denormals);
86}
87
88static AsmPrinter *
89createAMDGPUAsmPrinterPass(TargetMachine &tm,
90 std::unique_ptr<MCStreamer> &&Streamer) {
91 return new AMDGPUAsmPrinter(tm, std::move(Streamer));
92}
93
Tom Stellard4b0b2612019-06-11 03:21:13 +000094extern "C" void LLVMInitializeAMDGPUAsmPrinter() {
Mehdi Aminif42454b2016-10-09 23:00:34 +000095 TargetRegistry::RegisterAsmPrinter(getTheAMDGPUTarget(),
Tom Stellardc5015012018-05-24 20:02:01 +000096 llvm::createR600AsmPrinterPass);
Mehdi Aminif42454b2016-10-09 23:00:34 +000097 TargetRegistry::RegisterAsmPrinter(getTheGCNTarget(),
98 createAMDGPUAsmPrinterPass);
Tom Stellard45bb48e2015-06-13 03:28:10 +000099}
100
101AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM,
102 std::unique_ptr<MCStreamer> Streamer)
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000103 : AsmPrinter(TM, std::move(Streamer)) {
Matt Arsenault4cd95092019-02-12 23:44:13 +0000104 if (IsaInfo::hasCodeObjectV3(getGlobalSTI()))
Scott Linderf5b36e52018-12-12 19:39:27 +0000105 HSAMetadataStream.reset(new MetadataStreamerV3());
106 else
107 HSAMetadataStream.reset(new MetadataStreamerV2());
Matt Arsenault0da63502018-08-31 05:49:54 +0000108}
Tom Stellard45bb48e2015-06-13 03:28:10 +0000109
Mehdi Amini117296c2016-10-01 02:56:57 +0000110StringRef AMDGPUAsmPrinter::getPassName() const {
Matt Arsenaultf9245b72016-07-22 17:01:25 +0000111 return "AMDGPU Assembly Printer";
112}
113
Matt Arsenault4cd95092019-02-12 23:44:13 +0000114const MCSubtargetInfo *AMDGPUAsmPrinter::getGlobalSTI() const {
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000115 return TM.getMCSubtargetInfo();
116}
117
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000118AMDGPUTargetStreamer* AMDGPUAsmPrinter::getTargetStreamer() const {
119 if (!OutStreamer)
120 return nullptr;
121 return static_cast<AMDGPUTargetStreamer*>(OutStreamer->getTargetStreamer());
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000122}
123
Tom Stellardf4218372016-01-12 17:18:17 +0000124void AMDGPUAsmPrinter::EmitStartOfAsmFile(Module &M) {
Matt Arsenault4cd95092019-02-12 23:44:13 +0000125 if (IsaInfo::hasCodeObjectV3(getGlobalSTI())) {
Konstantin Zhuravlyov94dfcc2e2018-10-15 20:37:47 +0000126 std::string ExpectedTarget;
127 raw_string_ostream ExpectedTargetOS(ExpectedTarget);
Matt Arsenault4cd95092019-02-12 23:44:13 +0000128 IsaInfo::streamIsaVersion(getGlobalSTI(), ExpectedTargetOS);
Konstantin Zhuravlyov94dfcc2e2018-10-15 20:37:47 +0000129
130 getTargetStreamer()->EmitDirectiveAMDGCNTarget(ExpectedTarget);
Konstantin Zhuravlyov94dfcc2e2018-10-15 20:37:47 +0000131 }
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000132
Konstantin Zhuravlyoveda425e2017-10-14 15:59:07 +0000133 if (TM.getTargetTriple().getOS() != Triple::AMDHSA &&
134 TM.getTargetTriple().getOS() != Triple::AMDPAL)
135 return;
136
137 if (TM.getTargetTriple().getOS() == Triple::AMDHSA)
Scott Linderf5b36e52018-12-12 19:39:27 +0000138 HSAMetadataStream->begin(M);
Konstantin Zhuravlyoveda425e2017-10-14 15:59:07 +0000139
140 if (TM.getTargetTriple().getOS() == Triple::AMDPAL)
Tim Renoufd737b552019-03-20 17:42:00 +0000141 getTargetStreamer()->getPALMetadata()->readFromIR(M);
Konstantin Zhuravlyoveda425e2017-10-14 15:59:07 +0000142
Matt Arsenault4cd95092019-02-12 23:44:13 +0000143 if (IsaInfo::hasCodeObjectV3(getGlobalSTI()))
Scott Linderf5b36e52018-12-12 19:39:27 +0000144 return;
145
Konstantin Zhuravlyoveda425e2017-10-14 15:59:07 +0000146 // HSA emits NT_AMDGPU_HSA_CODE_OBJECT_VERSION for code objects v2.
147 if (TM.getTargetTriple().getOS() == Triple::AMDHSA)
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000148 getTargetStreamer()->EmitDirectiveHSACodeObjectVersion(2, 1);
Konstantin Zhuravlyoveda425e2017-10-14 15:59:07 +0000149
150 // HSA and PAL emit NT_AMDGPU_HSA_ISA for code objects v2.
Matt Arsenault4cd95092019-02-12 23:44:13 +0000151 IsaVersion Version = getIsaVersion(getGlobalSTI()->getCPU());
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000152 getTargetStreamer()->EmitDirectiveHSACodeObjectISA(
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000153 Version.Major, Version.Minor, Version.Stepping, "AMD", "AMDGPU");
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000154}
155
156void AMDGPUAsmPrinter::EmitEndOfAsmFile(Module &M) {
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000157 // Following code requires TargetStreamer to be present.
158 if (!getTargetStreamer())
159 return;
160
Matt Arsenault4cd95092019-02-12 23:44:13 +0000161 if (!IsaInfo::hasCodeObjectV3(getGlobalSTI())) {
Scott Linderf5b36e52018-12-12 19:39:27 +0000162 // Emit ISA Version (NT_AMD_AMDGPU_ISA).
163 std::string ISAVersionString;
164 raw_string_ostream ISAVersionStream(ISAVersionString);
Matt Arsenault4cd95092019-02-12 23:44:13 +0000165 IsaInfo::streamIsaVersion(getGlobalSTI(), ISAVersionStream);
Scott Linderf5b36e52018-12-12 19:39:27 +0000166 getTargetStreamer()->EmitISAVersion(ISAVersionStream.str());
167 }
Konstantin Zhuravlyov9c05b2b2017-10-14 15:40:33 +0000168
169 // Emit HSA Metadata (NT_AMD_AMDGPU_HSA_METADATA).
170 if (TM.getTargetTriple().getOS() == Triple::AMDHSA) {
Scott Linderf5b36e52018-12-12 19:39:27 +0000171 HSAMetadataStream->end();
172 bool Success = HSAMetadataStream->emitTo(*getTargetStreamer());
173 (void)Success;
174 assert(Success && "Malformed HSA Metadata");
Konstantin Zhuravlyov9c05b2b2017-10-14 15:40:33 +0000175 }
Tom Stellardf4218372016-01-12 17:18:17 +0000176}
177
Matt Arsenault6bc43d82016-10-06 16:20:41 +0000178bool AMDGPUAsmPrinter::isBlockOnlyReachableByFallthrough(
179 const MachineBasicBlock *MBB) const {
180 if (!AsmPrinter::isBlockOnlyReachableByFallthrough(MBB))
181 return false;
182
183 if (MBB->empty())
184 return true;
185
186 // If this is a block implementing a long branch, an expression relative to
187 // the start of the block is needed. to the start of the block.
188 // XXX - Is there a smarter way to check this?
189 return (MBB->back().getOpcode() != AMDGPU::S_SETPC_B64);
190}
191
Tom Stellardf151a452015-06-26 21:14:58 +0000192void AMDGPUAsmPrinter::EmitFunctionBodyStart() {
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000193 const SIMachineFunctionInfo &MFI = *MF->getInfo<SIMachineFunctionInfo>();
194 if (!MFI.isEntryFunction())
195 return;
Matt Arsenault021a2182017-04-19 19:38:10 +0000196
Tom Stellard5bfbae52018-07-11 20:59:01 +0000197 const GCNSubtarget &STM = MF->getSubtarget<GCNSubtarget>();
Matt Arsenault4bec7d42018-07-20 09:05:08 +0000198 const Function &F = MF->getFunction();
Konstantin Zhuravlyovaa067cb2018-10-04 21:02:16 +0000199 if (!STM.hasCodeObjectV3() && STM.isAmdHsaOrMesa(F) &&
Matt Arsenault4bec7d42018-07-20 09:05:08 +0000200 (F.getCallingConv() == CallingConv::AMDGPU_KERNEL ||
201 F.getCallingConv() == CallingConv::SPIR_KERNEL)) {
202 amd_kernel_code_t KernelCode;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000203 getAmdKernelCode(KernelCode, CurrentProgramInfo, *MF);
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000204 getTargetStreamer()->EmitAMDKernelCodeT(KernelCode);
Tom Stellardf151a452015-06-26 21:14:58 +0000205 }
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000206
Scott Linderf5b36e52018-12-12 19:39:27 +0000207 if (STM.isAmdHsaOS())
208 HSAMetadataStream->emitKernel(*MF, CurrentProgramInfo);
Tom Stellardf151a452015-06-26 21:14:58 +0000209}
210
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000211void AMDGPUAsmPrinter::EmitFunctionBodyEnd() {
212 const SIMachineFunctionInfo &MFI = *MF->getInfo<SIMachineFunctionInfo>();
213 if (!MFI.isEntryFunction())
214 return;
Matt Arsenault4cd95092019-02-12 23:44:13 +0000215
216 if (!IsaInfo::hasCodeObjectV3(getGlobalSTI()) ||
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000217 TM.getTargetTriple().getOS() != Triple::AMDHSA)
218 return;
219
Konstantin Zhuravlyovce25bc32018-06-12 18:33:51 +0000220 auto &Streamer = getTargetStreamer()->getStreamer();
221 auto &Context = Streamer.getContext();
222 auto &ObjectFileInfo = *Context.getObjectFileInfo();
223 auto &ReadOnlySection = *ObjectFileInfo.getReadOnlySection();
224
225 Streamer.PushSection();
226 Streamer.SwitchSection(&ReadOnlySection);
227
228 // CP microcode requires the kernel descriptor to be allocated on 64 byte
229 // alignment.
230 Streamer.EmitValueToAlignment(64, 0, 1, 0);
231 if (ReadOnlySection.getAlignment() < 64)
Guillaume Chatelet36202632019-09-13 09:29:59 +0000232 ReadOnlySection.setAlignment(llvm::Align(64));
Konstantin Zhuravlyovce25bc32018-06-12 18:33:51 +0000233
Matt Arsenault4cd95092019-02-12 23:44:13 +0000234 const MCSubtargetInfo &STI = MF->getSubtarget();
235
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000236 SmallString<128> KernelName;
237 getNameWithPrefix(KernelName, &MF->getFunction());
238 getTargetStreamer()->EmitAmdhsaKernelDescriptor(
Matt Arsenault4cd95092019-02-12 23:44:13 +0000239 STI, KernelName, getAmdhsaKernelDescriptor(*MF, CurrentProgramInfo),
Scott Linder1e8c2c72018-06-21 19:38:56 +0000240 CurrentProgramInfo.NumVGPRsForWavesPerEU,
241 CurrentProgramInfo.NumSGPRsForWavesPerEU -
Matt Arsenault4cd95092019-02-12 23:44:13 +0000242 IsaInfo::getNumExtraSGPRs(&STI,
Scott Linder1e8c2c72018-06-21 19:38:56 +0000243 CurrentProgramInfo.VCCUsed,
244 CurrentProgramInfo.FlatUsed),
245 CurrentProgramInfo.VCCUsed, CurrentProgramInfo.FlatUsed,
Matt Arsenault4cd95092019-02-12 23:44:13 +0000246 hasXNACK(STI));
Konstantin Zhuravlyovce25bc32018-06-12 18:33:51 +0000247
248 Streamer.PopSection();
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000249}
250
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000251void AMDGPUAsmPrinter::EmitFunctionEntryLabel() {
Matt Arsenault4cd95092019-02-12 23:44:13 +0000252 if (IsaInfo::hasCodeObjectV3(getGlobalSTI()) &&
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000253 TM.getTargetTriple().getOS() == Triple::AMDHSA) {
254 AsmPrinter::EmitFunctionEntryLabel();
255 return;
256 }
257
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000258 const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
Tom Stellard5bfbae52018-07-11 20:59:01 +0000259 const GCNSubtarget &STM = MF->getSubtarget<GCNSubtarget>();
Konstantin Zhuravlyovaa067cb2018-10-04 21:02:16 +0000260 if (MFI->isEntryFunction() && STM.isAmdHsaOrMesa(MF->getFunction())) {
Tom Stellard1b9748c2016-09-26 17:29:25 +0000261 SmallString<128> SymbolName;
Matthias Braunf1caa282017-12-15 22:22:58 +0000262 getNameWithPrefix(SymbolName, &MF->getFunction()),
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000263 getTargetStreamer()->EmitAMDGPUSymbolType(
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000264 SymbolName, ELF::STT_AMDGPU_HSA_KERNEL);
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000265 }
Tim Renouf33cb8f52019-05-14 16:17:14 +0000266 if (DumpCodeInstEmitter) {
Tim Renoufcead41d2017-12-08 14:09:34 +0000267 // Disassemble function name label to text.
Matthias Braunf1caa282017-12-15 22:22:58 +0000268 DisasmLines.push_back(MF->getName().str() + ":");
Tim Renoufcead41d2017-12-08 14:09:34 +0000269 DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLines.back().size());
270 HexLines.push_back("");
271 }
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000272
273 AsmPrinter::EmitFunctionEntryLabel();
274}
275
Karl-Johan Karlsson40da6be2019-08-20 05:13:57 +0000276void AMDGPUAsmPrinter::EmitBasicBlockStart(const MachineBasicBlock &MBB) {
Tim Renouf33cb8f52019-05-14 16:17:14 +0000277 if (DumpCodeInstEmitter && !isBlockOnlyReachableByFallthrough(&MBB)) {
Tim Renoufcead41d2017-12-08 14:09:34 +0000278 // Write a line for the basic block label if it is not only fallthrough.
279 DisasmLines.push_back(
280 (Twine("BB") + Twine(getFunctionNumber())
281 + "_" + Twine(MBB.getNumber()) + ":").str());
282 DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLines.back().size());
283 HexLines.push_back("");
284 }
285 AsmPrinter::EmitBasicBlockStart(MBB);
286}
287
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000288void AMDGPUAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
Nicolai Haehnle27101712019-06-25 11:52:30 +0000289 if (GV->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
290 if (GV->hasInitializer() && !isa<UndefValue>(GV->getInitializer())) {
291 OutContext.reportError({},
292 Twine(GV->getName()) +
293 ": unsupported initializer for address space");
294 return;
295 }
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000296
Nicolai Haehnle27101712019-06-25 11:52:30 +0000297 // LDS variables aren't emitted in HSA or PAL yet.
298 const Triple::OSType OS = TM.getTargetTriple().getOS();
299 if (OS == Triple::AMDHSA || OS == Triple::AMDPAL)
300 return;
301
302 MCSymbol *GVSym = getSymbol(GV);
303
304 GVSym->redefineIfPossible();
305 if (GVSym->isDefined() || GVSym->isVariable())
306 report_fatal_error("symbol '" + Twine(GVSym->getName()) +
307 "' is already defined");
308
309 const DataLayout &DL = GV->getParent()->getDataLayout();
310 uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
311 unsigned Align = GV->getAlignment();
312 if (!Align)
313 Align = 4;
314
315 EmitVisibility(GVSym, GV->getVisibility(), !GV->isDeclaration());
316 EmitLinkage(GV, GVSym);
Michael Liaof0a665a2019-06-25 14:06:34 +0000317 if (auto TS = getTargetStreamer())
318 TS->emitAMDGPULDS(GVSym, Size, Align);
Tom Stellard00f2f912015-12-02 19:47:57 +0000319 return;
Nicolai Haehnle27101712019-06-25 11:52:30 +0000320 }
Tom Stellard00f2f912015-12-02 19:47:57 +0000321
Tom Stellardfcfaea42016-05-05 17:03:33 +0000322 AsmPrinter::EmitGlobalVariable(GV);
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000323}
324
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000325bool AMDGPUAsmPrinter::doFinalization(Module &M) {
326 CallGraphResourceInfo.clear();
Stanislav Mekhanoshin41bbe102019-05-03 21:26:39 +0000327
Nicolai Haehnleae4fcb92019-06-17 19:28:43 +0000328 // Pad with s_code_end to help tools and guard against instruction prefetch
329 // causing stale data in caches. Arguably this should be done by the linker,
330 // which is why this isn't done for Mesa.
331 const MCSubtargetInfo &STI = *getGlobalSTI();
332 if (AMDGPU::isGFX10(STI) &&
333 (STI.getTargetTriple().getOS() == Triple::AMDHSA ||
334 STI.getTargetTriple().getOS() == Triple::AMDPAL)) {
Stanislav Mekhanoshin41bbe102019-05-03 21:26:39 +0000335 OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
336 getTargetStreamer()->EmitCodeEnd();
337 }
338
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000339 return AsmPrinter::doFinalization(M);
340}
341
342// Print comments that apply to both callable functions and entry points.
343void AMDGPUAsmPrinter::emitCommonFunctionComments(
344 uint32_t NumVGPR,
345 uint32_t NumSGPR,
Matt Arsenault9ba465a2017-11-14 20:33:14 +0000346 uint64_t ScratchSize,
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000347 uint64_t CodeSize,
348 const AMDGPUMachineFunction *MFI) {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000349 OutStreamer->emitRawComment(" codeLenInByte = " + Twine(CodeSize), false);
350 OutStreamer->emitRawComment(" NumSgprs: " + Twine(NumSGPR), false);
351 OutStreamer->emitRawComment(" NumVgprs: " + Twine(NumVGPR), false);
352 OutStreamer->emitRawComment(" ScratchSize: " + Twine(ScratchSize), false);
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000353 OutStreamer->emitRawComment(" MemoryBound: " + Twine(MFI->isMemoryBound()),
354 false);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000355}
356
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000357uint16_t AMDGPUAsmPrinter::getAmdhsaKernelCodeProperties(
358 const MachineFunction &MF) const {
359 const SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>();
360 uint16_t KernelCodeProperties = 0;
361
362 if (MFI.hasPrivateSegmentBuffer()) {
363 KernelCodeProperties |=
364 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER;
365 }
366 if (MFI.hasDispatchPtr()) {
367 KernelCodeProperties |=
368 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
369 }
370 if (MFI.hasQueuePtr()) {
371 KernelCodeProperties |=
372 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR;
373 }
374 if (MFI.hasKernargSegmentPtr()) {
375 KernelCodeProperties |=
376 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR;
377 }
378 if (MFI.hasDispatchID()) {
379 KernelCodeProperties |=
380 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID;
381 }
382 if (MFI.hasFlatScratchInit()) {
383 KernelCodeProperties |=
384 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT;
385 }
Stanislav Mekhanoshin5d00c302019-06-17 16:48:56 +0000386 if (MF.getSubtarget<GCNSubtarget>().isWave32()) {
387 KernelCodeProperties |=
388 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32;
389 }
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000390
391 return KernelCodeProperties;
392}
393
394amdhsa::kernel_descriptor_t AMDGPUAsmPrinter::getAmdhsaKernelDescriptor(
395 const MachineFunction &MF,
396 const SIProgramInfo &PI) const {
397 amdhsa::kernel_descriptor_t KernelDescriptor;
398 memset(&KernelDescriptor, 0x0, sizeof(KernelDescriptor));
399
400 assert(isUInt<32>(PI.ScratchSize));
401 assert(isUInt<32>(PI.ComputePGMRSrc1));
402 assert(isUInt<32>(PI.ComputePGMRSrc2));
403
404 KernelDescriptor.group_segment_fixed_size = PI.LDSSize;
405 KernelDescriptor.private_segment_fixed_size = PI.ScratchSize;
406 KernelDescriptor.compute_pgm_rsrc1 = PI.ComputePGMRSrc1;
407 KernelDescriptor.compute_pgm_rsrc2 = PI.ComputePGMRSrc2;
408 KernelDescriptor.kernel_code_properties = getAmdhsaKernelCodeProperties(MF);
409
410 return KernelDescriptor;
411}
412
Tom Stellard45bb48e2015-06-13 03:28:10 +0000413bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000414 CurrentProgramInfo = SIProgramInfo();
415
Matt Arsenault6cb7b8a2017-04-19 17:42:39 +0000416 const AMDGPUMachineFunction *MFI = MF.getInfo<AMDGPUMachineFunction>();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000417
418 // The starting address of all shader programs must be 256 bytes aligned.
Matt Arsenault6cb7b8a2017-04-19 17:42:39 +0000419 // Regular functions just need the basic required instruction alignment.
Guillaume Chatelet48904e92019-09-11 11:16:48 +0000420 MF.setAlignment(MFI->isEntryFunction() ? llvm::Align(256) : llvm::Align(4));
Tom Stellard45bb48e2015-06-13 03:28:10 +0000421
422 SetupMachineFunction(MF);
423
Tom Stellard5bfbae52018-07-11 20:59:01 +0000424 const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>();
Konstantin Zhuravlyov67a6d542017-01-06 17:02:10 +0000425 MCContext &Context = getObjFileLowering().getContext();
Tim Renouf807ecc32018-02-06 13:39:38 +0000426 // FIXME: This should be an explicit check for Mesa.
427 if (!STM.isAmdHsaOS() && !STM.isAmdPalOS()) {
Konstantin Zhuravlyov67a6d542017-01-06 17:02:10 +0000428 MCSectionELF *ConfigSection =
429 Context.getELFSection(".AMDGPU.config", ELF::SHT_PROGBITS, 0);
430 OutStreamer->SwitchSection(ConfigSection);
431 }
432
Tom Stellardc5015012018-05-24 20:02:01 +0000433 if (MFI->isEntryFunction()) {
434 getSIProgramInfo(CurrentProgramInfo, MF);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000435 } else {
Tom Stellardc5015012018-05-24 20:02:01 +0000436 auto I = CallGraphResourceInfo.insert(
437 std::make_pair(&MF.getFunction(), SIFunctionResourceInfo()));
438 SIFunctionResourceInfo &Info = I.first->second;
439 assert(I.second && "should only be called once per function");
440 Info = analyzeResourceUsage(MF);
441 }
442
443 if (STM.isAmdPalOS())
444 EmitPALMetadata(MF, CurrentProgramInfo);
445 else if (!STM.isAmdHsaOS()) {
446 EmitProgramInfoSI(MF, CurrentProgramInfo);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000447 }
448
Jay Foad84792402019-06-27 08:19:28 +0000449 DumpCodeInstEmitter = nullptr;
450 if (STM.dumpCode()) {
451 // For -dumpcode, get the assembler out of the streamer, even if it does
452 // not really want to let us have it. This only works with -filetype=obj.
453 bool SaveFlag = OutStreamer->getUseAssemblerInfoForParsing();
454 OutStreamer->setUseAssemblerInfoForParsing(true);
455 MCAssembler *Assembler = OutStreamer->getAssemblerPtr();
456 OutStreamer->setUseAssemblerInfoForParsing(SaveFlag);
457 if (Assembler)
458 DumpCodeInstEmitter = Assembler->getEmitterPtr();
459 }
460
Tom Stellard45bb48e2015-06-13 03:28:10 +0000461 DisasmLines.clear();
462 HexLines.clear();
463 DisasmLineMaxLen = 0;
464
465 EmitFunctionBody();
466
467 if (isVerbose()) {
468 MCSectionELF *CommentSection =
469 Context.getELFSection(".AMDGPU.csdata", ELF::SHT_PROGBITS, 0);
470 OutStreamer->SwitchSection(CommentSection);
471
Tom Stellardc5015012018-05-24 20:02:01 +0000472 if (!MFI->isEntryFunction()) {
473 OutStreamer->emitRawComment(" Function info:", false);
474 SIFunctionResourceInfo &Info = CallGraphResourceInfo[&MF.getFunction()];
475 emitCommonFunctionComments(
476 Info.NumVGPR,
Tom Stellard5bfbae52018-07-11 20:59:01 +0000477 Info.getTotalNumSGPRs(MF.getSubtarget<GCNSubtarget>()),
Tom Stellardc5015012018-05-24 20:02:01 +0000478 Info.PrivateSegmentSize,
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000479 getFunctionCodeSize(MF), MFI);
Tom Stellardc5015012018-05-24 20:02:01 +0000480 return false;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000481 }
Tom Stellardc5015012018-05-24 20:02:01 +0000482
483 OutStreamer->emitRawComment(" Kernel info:", false);
484 emitCommonFunctionComments(CurrentProgramInfo.NumVGPR,
485 CurrentProgramInfo.NumSGPR,
486 CurrentProgramInfo.ScratchSize,
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000487 getFunctionCodeSize(MF), MFI);
Tom Stellardc5015012018-05-24 20:02:01 +0000488
489 OutStreamer->emitRawComment(
490 " FloatMode: " + Twine(CurrentProgramInfo.FloatMode), false);
491 OutStreamer->emitRawComment(
492 " IeeeMode: " + Twine(CurrentProgramInfo.IEEEMode), false);
493 OutStreamer->emitRawComment(
494 " LDSByteSize: " + Twine(CurrentProgramInfo.LDSSize) +
495 " bytes/workgroup (compile time only)", false);
496
497 OutStreamer->emitRawComment(
498 " SGPRBlocks: " + Twine(CurrentProgramInfo.SGPRBlocks), false);
499 OutStreamer->emitRawComment(
500 " VGPRBlocks: " + Twine(CurrentProgramInfo.VGPRBlocks), false);
501
502 OutStreamer->emitRawComment(
503 " NumSGPRsForWavesPerEU: " +
504 Twine(CurrentProgramInfo.NumSGPRsForWavesPerEU), false);
505 OutStreamer->emitRawComment(
506 " NumVGPRsForWavesPerEU: " +
507 Twine(CurrentProgramInfo.NumVGPRsForWavesPerEU), false);
508
509 OutStreamer->emitRawComment(
Stanislav Mekhanoshin2594fa82019-07-31 01:07:10 +0000510 " Occupancy: " +
511 Twine(CurrentProgramInfo.Occupancy), false);
512
513 OutStreamer->emitRawComment(
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000514 " WaveLimiterHint : " + Twine(MFI->needsWaveLimiter()), false);
515
Tom Stellardc5015012018-05-24 20:02:01 +0000516 OutStreamer->emitRawComment(
517 " COMPUTE_PGM_RSRC2:USER_SGPR: " +
518 Twine(G_00B84C_USER_SGPR(CurrentProgramInfo.ComputePGMRSrc2)), false);
519 OutStreamer->emitRawComment(
520 " COMPUTE_PGM_RSRC2:TRAP_HANDLER: " +
521 Twine(G_00B84C_TRAP_HANDLER(CurrentProgramInfo.ComputePGMRSrc2)), false);
522 OutStreamer->emitRawComment(
523 " COMPUTE_PGM_RSRC2:TGID_X_EN: " +
524 Twine(G_00B84C_TGID_X_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
525 OutStreamer->emitRawComment(
526 " COMPUTE_PGM_RSRC2:TGID_Y_EN: " +
527 Twine(G_00B84C_TGID_Y_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
528 OutStreamer->emitRawComment(
529 " COMPUTE_PGM_RSRC2:TGID_Z_EN: " +
530 Twine(G_00B84C_TGID_Z_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
531 OutStreamer->emitRawComment(
532 " COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: " +
533 Twine(G_00B84C_TIDIG_COMP_CNT(CurrentProgramInfo.ComputePGMRSrc2)),
534 false);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000535 }
536
Tim Renouf33cb8f52019-05-14 16:17:14 +0000537 if (DumpCodeInstEmitter) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000538
539 OutStreamer->SwitchSection(
540 Context.getELFSection(".AMDGPU.disasm", ELF::SHT_NOTE, 0));
541
542 for (size_t i = 0; i < DisasmLines.size(); ++i) {
Tim Renoufcead41d2017-12-08 14:09:34 +0000543 std::string Comment = "\n";
544 if (!HexLines[i].empty()) {
545 Comment = std::string(DisasmLineMaxLen - DisasmLines[i].size(), ' ');
546 Comment += " ; " + HexLines[i] + "\n";
547 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000548
549 OutStreamer->EmitBytes(StringRef(DisasmLines[i]));
550 OutStreamer->EmitBytes(StringRef(Comment));
551 }
552 }
553
554 return false;
555}
556
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000557uint64_t AMDGPUAsmPrinter::getFunctionCodeSize(const MachineFunction &MF) const {
Tom Stellard5bfbae52018-07-11 20:59:01 +0000558 const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000559 const SIInstrInfo *TII = STM.getInstrInfo();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000560
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000561 uint64_t CodeSize = 0;
562
Tom Stellard45bb48e2015-06-13 03:28:10 +0000563 for (const MachineBasicBlock &MBB : MF) {
564 for (const MachineInstr &MI : MBB) {
565 // TODO: CodeSize should account for multiple functions.
Matt Arsenaultc5746862015-08-12 09:04:44 +0000566
567 // TODO: Should we count size of debug info?
Shiva Chen801bf7e2018-05-09 02:42:00 +0000568 if (MI.isDebugInstr())
Matt Arsenaultc5746862015-08-12 09:04:44 +0000569 continue;
570
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000571 CodeSize += TII->getInstSizeInBytes(MI);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000572 }
573 }
574
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000575 return CodeSize;
576}
577
578static bool hasAnyNonFlatUseOfReg(const MachineRegisterInfo &MRI,
579 const SIInstrInfo &TII,
580 unsigned Reg) {
581 for (const MachineOperand &UseOp : MRI.reg_operands(Reg)) {
582 if (!UseOp.isImplicit() || !TII.isFLAT(*UseOp.getParent()))
583 return true;
584 }
585
586 return false;
587}
588
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000589int32_t AMDGPUAsmPrinter::SIFunctionResourceInfo::getTotalNumSGPRs(
Tom Stellard5bfbae52018-07-11 20:59:01 +0000590 const GCNSubtarget &ST) const {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000591 return NumExplicitSGPR + IsaInfo::getNumExtraSGPRs(&ST,
Scott Linder1e8c2c72018-06-21 19:38:56 +0000592 UsesVCC, UsesFlatScratch);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000593}
594
595AMDGPUAsmPrinter::SIFunctionResourceInfo AMDGPUAsmPrinter::analyzeResourceUsage(
596 const MachineFunction &MF) const {
597 SIFunctionResourceInfo Info;
598
599 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellard5bfbae52018-07-11 20:59:01 +0000600 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000601 const MachineFrameInfo &FrameInfo = MF.getFrameInfo();
602 const MachineRegisterInfo &MRI = MF.getRegInfo();
603 const SIInstrInfo *TII = ST.getInstrInfo();
604 const SIRegisterInfo &TRI = TII->getRegisterInfo();
605
606 Info.UsesFlatScratch = MRI.isPhysRegUsed(AMDGPU::FLAT_SCR_LO) ||
607 MRI.isPhysRegUsed(AMDGPU::FLAT_SCR_HI);
608
609 // Even if FLAT_SCRATCH is implicitly used, it has no effect if flat
610 // instructions aren't used to access the scratch buffer. Inline assembly may
611 // need it though.
612 //
613 // If we only have implicit uses of flat_scr on flat instructions, it is not
614 // really needed.
615 if (Info.UsesFlatScratch && !MFI->hasFlatScratchInit() &&
616 (!hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR) &&
617 !hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR_LO) &&
618 !hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR_HI))) {
619 Info.UsesFlatScratch = false;
620 }
621
622 Info.HasDynamicallySizedStack = FrameInfo.hasVarSizedObjects();
623 Info.PrivateSegmentSize = FrameInfo.getStackSize();
Matt Arsenault03ae3992018-03-29 21:30:06 +0000624 if (MFI->isStackRealigned())
625 Info.PrivateSegmentSize += FrameInfo.getMaxAlignment();
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000626
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000627
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000628 Info.UsesVCC = MRI.isPhysRegUsed(AMDGPU::VCC_LO) ||
629 MRI.isPhysRegUsed(AMDGPU::VCC_HI);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000630
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000631 // If there are no calls, MachineRegisterInfo can tell us the used register
632 // count easily.
Matt Arsenault22cdb612017-09-05 18:36:36 +0000633 // A tail call isn't considered a call for MachineFrameInfo's purposes.
634 if (!FrameInfo.hasCalls() && !FrameInfo.hasTailCall()) {
Matt Arsenault2738ede2017-08-02 17:15:01 +0000635 MCPhysReg HighestVGPRReg = AMDGPU::NoRegister;
636 for (MCPhysReg Reg : reverse(AMDGPU::VGPR_32RegClass.getRegisters())) {
637 if (MRI.isPhysRegUsed(Reg)) {
638 HighestVGPRReg = Reg;
639 break;
640 }
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000641 MCPhysReg AReg = AMDGPU::AGPR0 + TRI.getHWRegIndex(Reg);
642 if (MRI.isPhysRegUsed(AReg)) {
643 HighestVGPRReg = AReg;
644 break;
645 }
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000646 }
Matt Arsenault2738ede2017-08-02 17:15:01 +0000647
648 MCPhysReg HighestSGPRReg = AMDGPU::NoRegister;
649 for (MCPhysReg Reg : reverse(AMDGPU::SGPR_32RegClass.getRegisters())) {
650 if (MRI.isPhysRegUsed(Reg)) {
651 HighestSGPRReg = Reg;
652 break;
653 }
654 }
655
656 // We found the maximum register index. They start at 0, so add one to get the
657 // number of registers.
658 Info.NumVGPR = HighestVGPRReg == AMDGPU::NoRegister ? 0 :
659 TRI.getHWRegIndex(HighestVGPRReg) + 1;
660 Info.NumExplicitSGPR = HighestSGPRReg == AMDGPU::NoRegister ? 0 :
661 TRI.getHWRegIndex(HighestSGPRReg) + 1;
662
663 return Info;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000664 }
665
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000666 int32_t MaxVGPR = -1;
667 int32_t MaxSGPR = -1;
Matt Arsenault9ba465a2017-11-14 20:33:14 +0000668 uint64_t CalleeFrameSize = 0;
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000669
670 for (const MachineBasicBlock &MBB : MF) {
671 for (const MachineInstr &MI : MBB) {
672 // TODO: Check regmasks? Do they occur anywhere except calls?
673 for (const MachineOperand &MO : MI.operands()) {
674 unsigned Width = 0;
675 bool IsSGPR = false;
676
677 if (!MO.isReg())
678 continue;
679
Daniel Sanders0c476112019-08-15 19:22:08 +0000680 Register Reg = MO.getReg();
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000681 switch (Reg) {
682 case AMDGPU::EXEC:
683 case AMDGPU::EXEC_LO:
684 case AMDGPU::EXEC_HI:
685 case AMDGPU::SCC:
686 case AMDGPU::M0:
687 case AMDGPU::SRC_SHARED_BASE:
688 case AMDGPU::SRC_SHARED_LIMIT:
689 case AMDGPU::SRC_PRIVATE_BASE:
690 case AMDGPU::SRC_PRIVATE_LIMIT:
Stanislav Mekhanoshin33d806a2019-04-24 17:28:30 +0000691 case AMDGPU::SGPR_NULL:
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000692 continue;
693
Dmitry Preobrazhensky137976f2019-03-20 15:40:52 +0000694 case AMDGPU::SRC_POPS_EXITING_WAVE_ID:
695 llvm_unreachable("src_pops_exiting_wave_id should not be used");
696
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000697 case AMDGPU::NoRegister:
Shiva Chen801bf7e2018-05-09 02:42:00 +0000698 assert(MI.isDebugInstr());
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000699 continue;
700
701 case AMDGPU::VCC:
702 case AMDGPU::VCC_LO:
703 case AMDGPU::VCC_HI:
704 Info.UsesVCC = true;
705 continue;
706
707 case AMDGPU::FLAT_SCR:
708 case AMDGPU::FLAT_SCR_LO:
709 case AMDGPU::FLAT_SCR_HI:
710 continue;
711
Dmitry Preobrazhensky3afbd822018-01-10 14:22:19 +0000712 case AMDGPU::XNACK_MASK:
713 case AMDGPU::XNACK_MASK_LO:
714 case AMDGPU::XNACK_MASK_HI:
715 llvm_unreachable("xnack_mask registers should not be used");
716
Dmitry Preobrazhensky942c2732019-02-08 14:57:37 +0000717 case AMDGPU::LDS_DIRECT:
718 llvm_unreachable("lds_direct register should not be used");
719
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000720 case AMDGPU::TBA:
721 case AMDGPU::TBA_LO:
722 case AMDGPU::TBA_HI:
723 case AMDGPU::TMA:
724 case AMDGPU::TMA_LO:
725 case AMDGPU::TMA_HI:
726 llvm_unreachable("trap handler registers should not be used");
727
Dmitry Preobrazhensky9111f352019-06-03 13:51:24 +0000728 case AMDGPU::SRC_VCCZ:
729 llvm_unreachable("src_vccz register should not be used");
730
731 case AMDGPU::SRC_EXECZ:
732 llvm_unreachable("src_execz register should not be used");
733
734 case AMDGPU::SRC_SCC:
735 llvm_unreachable("src_scc register should not be used");
736
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000737 default:
738 break;
739 }
740
741 if (AMDGPU::SReg_32RegClass.contains(Reg)) {
742 assert(!AMDGPU::TTMP_32RegClass.contains(Reg) &&
743 "trap handler registers should not be used");
744 IsSGPR = true;
745 Width = 1;
746 } else if (AMDGPU::VGPR_32RegClass.contains(Reg)) {
747 IsSGPR = false;
748 Width = 1;
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000749 } else if (AMDGPU::AGPR_32RegClass.contains(Reg)) {
750 IsSGPR = false;
751 Width = 1;
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000752 } else if (AMDGPU::SReg_64RegClass.contains(Reg)) {
753 assert(!AMDGPU::TTMP_64RegClass.contains(Reg) &&
754 "trap handler registers should not be used");
755 IsSGPR = true;
756 Width = 2;
757 } else if (AMDGPU::VReg_64RegClass.contains(Reg)) {
758 IsSGPR = false;
759 Width = 2;
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000760 } else if (AMDGPU::AReg_64RegClass.contains(Reg)) {
761 IsSGPR = false;
762 Width = 2;
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000763 } else if (AMDGPU::VReg_96RegClass.contains(Reg)) {
764 IsSGPR = false;
765 Width = 3;
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000766 } else if (AMDGPU::SReg_96RegClass.contains(Reg)) {
767 Width = 3;
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000768 } else if (AMDGPU::SReg_128RegClass.contains(Reg)) {
Dmitry Preobrazhensky27134952017-12-22 15:18:06 +0000769 assert(!AMDGPU::TTMP_128RegClass.contains(Reg) &&
770 "trap handler registers should not be used");
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000771 IsSGPR = true;
772 Width = 4;
773 } else if (AMDGPU::VReg_128RegClass.contains(Reg)) {
774 IsSGPR = false;
775 Width = 4;
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000776 } else if (AMDGPU::AReg_128RegClass.contains(Reg)) {
777 IsSGPR = false;
778 Width = 4;
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000779 } else if (AMDGPU::SReg_256RegClass.contains(Reg)) {
Dmitry Preobrazhensky27134952017-12-22 15:18:06 +0000780 assert(!AMDGPU::TTMP_256RegClass.contains(Reg) &&
781 "trap handler registers should not be used");
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000782 IsSGPR = true;
783 Width = 8;
784 } else if (AMDGPU::VReg_256RegClass.contains(Reg)) {
785 IsSGPR = false;
786 Width = 8;
787 } else if (AMDGPU::SReg_512RegClass.contains(Reg)) {
Dmitry Preobrazhensky27134952017-12-22 15:18:06 +0000788 assert(!AMDGPU::TTMP_512RegClass.contains(Reg) &&
789 "trap handler registers should not be used");
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000790 IsSGPR = true;
791 Width = 16;
792 } else if (AMDGPU::VReg_512RegClass.contains(Reg)) {
793 IsSGPR = false;
794 Width = 16;
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000795 } else if (AMDGPU::AReg_512RegClass.contains(Reg)) {
796 IsSGPR = false;
797 Width = 16;
798 } else if (AMDGPU::SReg_1024RegClass.contains(Reg)) {
Matt Arsenault101abd22019-04-15 20:51:12 +0000799 IsSGPR = true;
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000800 Width = 32;
801 } else if (AMDGPU::VReg_1024RegClass.contains(Reg)) {
802 IsSGPR = false;
803 Width = 32;
804 } else if (AMDGPU::AReg_1024RegClass.contains(Reg)) {
805 IsSGPR = false;
806 Width = 32;
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000807 } else {
808 llvm_unreachable("Unknown register class");
809 }
810 unsigned HWReg = TRI.getHWRegIndex(Reg);
811 int MaxUsed = HWReg + Width - 1;
812 if (IsSGPR) {
813 MaxSGPR = MaxUsed > MaxSGPR ? MaxUsed : MaxSGPR;
814 } else {
815 MaxVGPR = MaxUsed > MaxVGPR ? MaxUsed : MaxVGPR;
816 }
817 }
818
819 if (MI.isCall()) {
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000820 // Pseudo used just to encode the underlying global. Is there a better
821 // way to track this?
Matt Arsenault71bcbd42017-08-11 20:42:08 +0000822
823 const MachineOperand *CalleeOp
824 = TII->getNamedOperand(MI, AMDGPU::OpName::callee);
825 const Function *Callee = cast<Function>(CalleeOp->getGlobal());
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000826 if (Callee->isDeclaration()) {
827 // If this is a call to an external function, we can't do much. Make
828 // conservative guesses.
829
830 // 48 SGPRs - vcc, - flat_scr, -xnack
Scott Linder1e8c2c72018-06-21 19:38:56 +0000831 int MaxSGPRGuess =
Matt Arsenault4cd95092019-02-12 23:44:13 +0000832 47 - IsaInfo::getNumExtraSGPRs(&ST, true, ST.hasFlatAddressSpace());
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000833 MaxSGPR = std::max(MaxSGPR, MaxSGPRGuess);
834 MaxVGPR = std::max(MaxVGPR, 23);
835
Matt Arsenault9ba465a2017-11-14 20:33:14 +0000836 CalleeFrameSize = std::max(CalleeFrameSize, UINT64_C(16384));
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000837 Info.UsesVCC = true;
838 Info.UsesFlatScratch = ST.hasFlatAddressSpace();
839 Info.HasDynamicallySizedStack = true;
840 } else {
841 // We force CodeGen to run in SCC order, so the callee's register
842 // usage etc. should be the cumulative usage of all callees.
Matt Arsenaultaa03bcd2019-02-28 00:28:44 +0000843
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000844 auto I = CallGraphResourceInfo.find(Callee);
Matt Arsenaultaa03bcd2019-02-28 00:28:44 +0000845 if (I == CallGraphResourceInfo.end()) {
846 // Avoid crashing on undefined behavior with an illegal call to a
847 // kernel. If a callsite's calling convention doesn't match the
848 // function's, it's undefined behavior. If the callsite calling
849 // convention does match, that would have errored earlier.
850 // FIXME: The verifier shouldn't allow this.
851 if (AMDGPU::isEntryFunctionCC(Callee->getCallingConv()))
852 report_fatal_error("invalid call to entry function");
853
854 llvm_unreachable("callee should have been handled before caller");
855 }
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000856
857 MaxSGPR = std::max(I->second.NumExplicitSGPR - 1, MaxSGPR);
858 MaxVGPR = std::max(I->second.NumVGPR - 1, MaxVGPR);
859 CalleeFrameSize
860 = std::max(I->second.PrivateSegmentSize, CalleeFrameSize);
861 Info.UsesVCC |= I->second.UsesVCC;
862 Info.UsesFlatScratch |= I->second.UsesFlatScratch;
863 Info.HasDynamicallySizedStack |= I->second.HasDynamicallySizedStack;
864 Info.HasRecursion |= I->second.HasRecursion;
865 }
866
867 if (!Callee->doesNotRecurse())
868 Info.HasRecursion = true;
869 }
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000870 }
871 }
872
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000873 Info.NumExplicitSGPR = MaxSGPR + 1;
874 Info.NumVGPR = MaxVGPR + 1;
875 Info.PrivateSegmentSize += CalleeFrameSize;
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000876
877 return Info;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000878}
879
880void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo,
881 const MachineFunction &MF) {
882 SIFunctionResourceInfo Info = analyzeResourceUsage(MF);
883
884 ProgInfo.NumVGPR = Info.NumVGPR;
885 ProgInfo.NumSGPR = Info.NumExplicitSGPR;
886 ProgInfo.ScratchSize = Info.PrivateSegmentSize;
887 ProgInfo.VCCUsed = Info.UsesVCC;
888 ProgInfo.FlatUsed = Info.UsesFlatScratch;
889 ProgInfo.DynamicCallStack = Info.HasDynamicallySizedStack || Info.HasRecursion;
890
Matt Arsenault9ba465a2017-11-14 20:33:14 +0000891 if (!isUInt<32>(ProgInfo.ScratchSize)) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000892 DiagnosticInfoStackSize DiagStackSize(MF.getFunction(),
Matt Arsenault9ba465a2017-11-14 20:33:14 +0000893 ProgInfo.ScratchSize, DS_Error);
Matthias Braunf1caa282017-12-15 22:22:58 +0000894 MF.getFunction().getContext().diagnose(DiagStackSize);
Matt Arsenault9ba465a2017-11-14 20:33:14 +0000895 }
896
Tom Stellard5bfbae52018-07-11 20:59:01 +0000897 const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>();
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000898 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000899
Scott Linder1e8c2c72018-06-21 19:38:56 +0000900 // TODO(scott.linder): The calculations related to SGPR/VGPR blocks are
901 // duplicated in part in AMDGPUAsmParser::calculateGPRBlocks, and could be
902 // unified.
903 unsigned ExtraSGPRs = IsaInfo::getNumExtraSGPRs(
Matt Arsenault4cd95092019-02-12 23:44:13 +0000904 &STM, ProgInfo.VCCUsed, ProgInfo.FlatUsed);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000905
Marek Olsak91f22fb2016-12-09 19:49:40 +0000906 // Check the addressable register limit before we add ExtraSGPRs.
907 if (STM.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS &&
908 !STM.hasSGPRInitBug()) {
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000909 unsigned MaxAddressableNumSGPRs = STM.getAddressableNumSGPRs();
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000910 if (ProgInfo.NumSGPR > MaxAddressableNumSGPRs) {
Marek Olsak91f22fb2016-12-09 19:49:40 +0000911 // This can happen due to a compiler bug or when using inline asm.
Matthias Braunf1caa282017-12-15 22:22:58 +0000912 LLVMContext &Ctx = MF.getFunction().getContext();
913 DiagnosticInfoResourceLimit Diag(MF.getFunction(),
Marek Olsak91f22fb2016-12-09 19:49:40 +0000914 "addressable scalar registers",
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000915 ProgInfo.NumSGPR, DS_Error,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000916 DK_ResourceLimit,
917 MaxAddressableNumSGPRs);
Marek Olsak91f22fb2016-12-09 19:49:40 +0000918 Ctx.diagnose(Diag);
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000919 ProgInfo.NumSGPR = MaxAddressableNumSGPRs - 1;
Marek Olsak91f22fb2016-12-09 19:49:40 +0000920 }
921 }
922
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000923 // Account for extra SGPRs and VGPRs reserved for debugger use.
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000924 ProgInfo.NumSGPR += ExtraSGPRs;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000925
Tim Renouffd8d4af2018-04-11 17:18:36 +0000926 // Ensure there are enough SGPRs and VGPRs for wave dispatch, where wave
927 // dispatch registers are function args.
928 unsigned WaveDispatchNumSGPR = 0, WaveDispatchNumVGPR = 0;
929 for (auto &Arg : MF.getFunction().args()) {
930 unsigned NumRegs = (Arg.getType()->getPrimitiveSizeInBits() + 31) / 32;
931 if (Arg.hasAttribute(Attribute::InReg))
932 WaveDispatchNumSGPR += NumRegs;
933 else
934 WaveDispatchNumVGPR += NumRegs;
935 }
936 ProgInfo.NumSGPR = std::max(ProgInfo.NumSGPR, WaveDispatchNumSGPR);
937 ProgInfo.NumVGPR = std::max(ProgInfo.NumVGPR, WaveDispatchNumVGPR);
938
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000939 // Adjust number of registers used to meet default/requested minimum/maximum
940 // number of waves per execution unit request.
941 ProgInfo.NumSGPRsForWavesPerEU = std::max(
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000942 std::max(ProgInfo.NumSGPR, 1u), STM.getMinNumSGPRs(MFI->getMaxWavesPerEU()));
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000943 ProgInfo.NumVGPRsForWavesPerEU = std::max(
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000944 std::max(ProgInfo.NumVGPR, 1u), STM.getMinNumVGPRs(MFI->getMaxWavesPerEU()));
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000945
Marek Olsak91f22fb2016-12-09 19:49:40 +0000946 if (STM.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ||
947 STM.hasSGPRInitBug()) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000948 unsigned MaxAddressableNumSGPRs = STM.getAddressableNumSGPRs();
949 if (ProgInfo.NumSGPR > MaxAddressableNumSGPRs) {
950 // This can happen due to a compiler bug or when using inline asm to use
951 // the registers which are usually reserved for vcc etc.
Matthias Braunf1caa282017-12-15 22:22:58 +0000952 LLVMContext &Ctx = MF.getFunction().getContext();
953 DiagnosticInfoResourceLimit Diag(MF.getFunction(),
Marek Olsak91f22fb2016-12-09 19:49:40 +0000954 "scalar registers",
955 ProgInfo.NumSGPR, DS_Error,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000956 DK_ResourceLimit,
957 MaxAddressableNumSGPRs);
Marek Olsak91f22fb2016-12-09 19:49:40 +0000958 Ctx.diagnose(Diag);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000959 ProgInfo.NumSGPR = MaxAddressableNumSGPRs;
960 ProgInfo.NumSGPRsForWavesPerEU = MaxAddressableNumSGPRs;
Marek Olsak91f22fb2016-12-09 19:49:40 +0000961 }
Matt Arsenault4eae3012016-10-28 20:31:47 +0000962 }
963
964 if (STM.hasSGPRInitBug()) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000965 ProgInfo.NumSGPR =
966 AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG;
967 ProgInfo.NumSGPRsForWavesPerEU =
968 AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000969 }
970
Matt Arsenault161e2b42017-04-18 20:59:40 +0000971 if (MFI->getNumUserSGPRs() > STM.getMaxNumUserSGPRs()) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000972 LLVMContext &Ctx = MF.getFunction().getContext();
973 DiagnosticInfoResourceLimit Diag(MF.getFunction(), "user SGPRs",
Matt Arsenault161e2b42017-04-18 20:59:40 +0000974 MFI->getNumUserSGPRs(), DS_Error);
Matt Arsenaultff982412016-06-20 18:13:04 +0000975 Ctx.diagnose(Diag);
Matt Arsenault41003af2015-11-30 21:16:07 +0000976 }
977
Matt Arsenault52ef4012016-07-26 16:45:58 +0000978 if (MFI->getLDSSize() > static_cast<unsigned>(STM.getLocalMemorySize())) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000979 LLVMContext &Ctx = MF.getFunction().getContext();
980 DiagnosticInfoResourceLimit Diag(MF.getFunction(), "local memory",
Matt Arsenault52ef4012016-07-26 16:45:58 +0000981 MFI->getLDSSize(), DS_Error);
Matt Arsenaultff982412016-06-20 18:13:04 +0000982 Ctx.diagnose(Diag);
Matt Arsenault1c4d0ef2016-04-28 19:37:35 +0000983 }
984
Scott Linder1e8c2c72018-06-21 19:38:56 +0000985 ProgInfo.SGPRBlocks = IsaInfo::getNumSGPRBlocks(
David Stuttardbe3d7ba2018-11-19 15:44:20 +0000986 &STM, ProgInfo.NumSGPRsForWavesPerEU);
Scott Linder1e8c2c72018-06-21 19:38:56 +0000987 ProgInfo.VGPRBlocks = IsaInfo::getNumVGPRBlocks(
David Stuttardbe3d7ba2018-11-19 15:44:20 +0000988 &STM, ProgInfo.NumVGPRsForWavesPerEU);
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000989
Tom Stellard45bb48e2015-06-13 03:28:10 +0000990 // Set the value to initialize FP_ROUND and FP_DENORM parts of the mode
991 // register.
992 ProgInfo.FloatMode = getFPMode(MF);
993
Matt Arsenault055e4dc2019-03-29 19:14:54 +0000994 const SIModeRegisterDefaults Mode = MFI->getMode();
995 ProgInfo.IEEEMode = Mode.IEEE;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000996
Matt Arsenault7293f982016-01-28 20:53:35 +0000997 // Make clamp modifier on NaN input returns 0.
Matt Arsenault055e4dc2019-03-29 19:14:54 +0000998 ProgInfo.DX10Clamp = Mode.DX10Clamp;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000999
Tom Stellard45bb48e2015-06-13 03:28:10 +00001000 unsigned LDSAlignShift;
Tom Stellard5bfbae52018-07-11 20:59:01 +00001001 if (STM.getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) {
Tom Stellard45bb48e2015-06-13 03:28:10 +00001002 // LDS is allocated in 64 dword blocks.
1003 LDSAlignShift = 8;
1004 } else {
1005 // LDS is allocated in 128 dword blocks.
1006 LDSAlignShift = 9;
1007 }
1008
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +00001009 unsigned LDSSpillSize =
Matt Arsenault161e2b42017-04-18 20:59:40 +00001010 MFI->getLDSWaveSpillSize() * MFI->getMaxFlatWorkGroupSize();
Tom Stellard45bb48e2015-06-13 03:28:10 +00001011
Matt Arsenault52ef4012016-07-26 16:45:58 +00001012 ProgInfo.LDSSize = MFI->getLDSSize() + LDSSpillSize;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001013 ProgInfo.LDSBlocks =
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +00001014 alignTo(ProgInfo.LDSSize, 1ULL << LDSAlignShift) >> LDSAlignShift;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001015
1016 // Scratch is allocated in 256 dword blocks.
1017 unsigned ScratchAlignShift = 10;
1018 // We need to program the hardware with the amount of scratch memory that
1019 // is used by the entire wave. ProgInfo.ScratchSize is the amount of
1020 // scratch memory used per thread.
1021 ProgInfo.ScratchBlocks =
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001022 alignTo(ProgInfo.ScratchSize * STM.getWavefrontSize(),
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +00001023 1ULL << ScratchAlignShift) >>
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001024 ScratchAlignShift;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001025
Stanislav Mekhanoshin41bbe102019-05-03 21:26:39 +00001026 if (getIsaVersion(getGlobalSTI()->getCPU()).Major >= 10) {
1027 ProgInfo.WgpMode = STM.isCuModeEnabled() ? 0 : 1;
1028 ProgInfo.MemOrdered = 1;
1029 }
1030
Tom Stellard45bb48e2015-06-13 03:28:10 +00001031 ProgInfo.ComputePGMRSrc1 =
1032 S_00B848_VGPRS(ProgInfo.VGPRBlocks) |
1033 S_00B848_SGPRS(ProgInfo.SGPRBlocks) |
1034 S_00B848_PRIORITY(ProgInfo.Priority) |
1035 S_00B848_FLOAT_MODE(ProgInfo.FloatMode) |
1036 S_00B848_PRIV(ProgInfo.Priv) |
1037 S_00B848_DX10_CLAMP(ProgInfo.DX10Clamp) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001038 S_00B848_DEBUG_MODE(ProgInfo.DebugMode) |
Stanislav Mekhanoshin41bbe102019-05-03 21:26:39 +00001039 S_00B848_IEEE_MODE(ProgInfo.IEEEMode) |
1040 S_00B848_WGP_MODE(ProgInfo.WgpMode) |
1041 S_00B848_MEM_ORDERED(ProgInfo.MemOrdered);
Tom Stellard45bb48e2015-06-13 03:28:10 +00001042
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001043 // 0 = X, 1 = XY, 2 = XYZ
1044 unsigned TIDIGCompCnt = 0;
1045 if (MFI->hasWorkItemIDZ())
1046 TIDIGCompCnt = 2;
1047 else if (MFI->hasWorkItemIDY())
1048 TIDIGCompCnt = 1;
1049
Tom Stellard45bb48e2015-06-13 03:28:10 +00001050 ProgInfo.ComputePGMRSrc2 =
1051 S_00B84C_SCRATCH_EN(ProgInfo.ScratchBlocks > 0) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001052 S_00B84C_USER_SGPR(MFI->getNumUserSGPRs()) |
Konstantin Zhuravlyov2ca6b1f2018-05-29 19:09:13 +00001053 // For AMDHSA, TRAP_HANDLER must be zero, as it is populated by the CP.
1054 S_00B84C_TRAP_HANDLER(STM.isAmdHsaOS() ? 0 : STM.isTrapHandlerEnabled()) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001055 S_00B84C_TGID_X_EN(MFI->hasWorkGroupIDX()) |
1056 S_00B84C_TGID_Y_EN(MFI->hasWorkGroupIDY()) |
1057 S_00B84C_TGID_Z_EN(MFI->hasWorkGroupIDZ()) |
1058 S_00B84C_TG_SIZE_EN(MFI->hasWorkGroupInfo()) |
1059 S_00B84C_TIDIG_COMP_CNT(TIDIGCompCnt) |
1060 S_00B84C_EXCP_EN_MSB(0) |
Konstantin Zhuravlyov6ccb0762017-05-05 20:13:55 +00001061 // For AMDHSA, LDS_SIZE must be zero, as it is populated by the CP.
1062 S_00B84C_LDS_SIZE(STM.isAmdHsaOS() ? 0 : ProgInfo.LDSBlocks) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001063 S_00B84C_EXCP_EN(0);
Stanislav Mekhanoshin2594fa82019-07-31 01:07:10 +00001064
1065 ProgInfo.Occupancy = STM.computeOccupancy(MF, ProgInfo.LDSSize,
1066 ProgInfo.NumSGPRsForWavesPerEU,
1067 ProgInfo.NumVGPRsForWavesPerEU);
Tom Stellard45bb48e2015-06-13 03:28:10 +00001068}
1069
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001070static unsigned getRsrcReg(CallingConv::ID CallConv) {
1071 switch (CallConv) {
Justin Bognercd1d5aa2016-08-17 20:30:52 +00001072 default: LLVM_FALLTHROUGH;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001073 case CallingConv::AMDGPU_CS: return R_00B848_COMPUTE_PGM_RSRC1;
Tim Renoufef1ae8f2017-09-29 09:51:22 +00001074 case CallingConv::AMDGPU_LS: return R_00B528_SPI_SHADER_PGM_RSRC1_LS;
Marek Olsaka302a7362017-05-02 15:41:10 +00001075 case CallingConv::AMDGPU_HS: return R_00B428_SPI_SHADER_PGM_RSRC1_HS;
Tim Renoufef1ae8f2017-09-29 09:51:22 +00001076 case CallingConv::AMDGPU_ES: return R_00B328_SPI_SHADER_PGM_RSRC1_ES;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001077 case CallingConv::AMDGPU_GS: return R_00B228_SPI_SHADER_PGM_RSRC1_GS;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001078 case CallingConv::AMDGPU_VS: return R_00B128_SPI_SHADER_PGM_RSRC1_VS;
Tim Renoufef1ae8f2017-09-29 09:51:22 +00001079 case CallingConv::AMDGPU_PS: return R_00B028_SPI_SHADER_PGM_RSRC1_PS;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001080 }
1081}
1082
1083void AMDGPUAsmPrinter::EmitProgramInfoSI(const MachineFunction &MF,
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001084 const SIProgramInfo &CurrentProgramInfo) {
Tom Stellard45bb48e2015-06-13 03:28:10 +00001085 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Matthias Braunf1caa282017-12-15 22:22:58 +00001086 unsigned RsrcReg = getRsrcReg(MF.getFunction().getCallingConv());
Tom Stellard45bb48e2015-06-13 03:28:10 +00001087
Matthias Braunf1caa282017-12-15 22:22:58 +00001088 if (AMDGPU::isCompute(MF.getFunction().getCallingConv())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +00001089 OutStreamer->EmitIntValue(R_00B848_COMPUTE_PGM_RSRC1, 4);
1090
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001091 OutStreamer->EmitIntValue(CurrentProgramInfo.ComputePGMRSrc1, 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +00001092
1093 OutStreamer->EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001094 OutStreamer->EmitIntValue(CurrentProgramInfo.ComputePGMRSrc2, 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +00001095
1096 OutStreamer->EmitIntValue(R_00B860_COMPUTE_TMPRING_SIZE, 4);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001097 OutStreamer->EmitIntValue(S_00B860_WAVESIZE(CurrentProgramInfo.ScratchBlocks), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +00001098
1099 // TODO: Should probably note flat usage somewhere. SC emits a "FlatPtr32 =
1100 // 0" comment but I don't see a corresponding field in the register spec.
1101 } else {
1102 OutStreamer->EmitIntValue(RsrcReg, 4);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001103 OutStreamer->EmitIntValue(S_00B028_VGPRS(CurrentProgramInfo.VGPRBlocks) |
1104 S_00B028_SGPRS(CurrentProgramInfo.SGPRBlocks), 4);
Scott Linderc6c62722018-10-31 18:54:06 +00001105 OutStreamer->EmitIntValue(R_0286E8_SPI_TMPRING_SIZE, 4);
1106 OutStreamer->EmitIntValue(
1107 S_0286E8_WAVESIZE(CurrentProgramInfo.ScratchBlocks), 4);
Tim Renouf807ecc32018-02-06 13:39:38 +00001108 }
1109
1110 if (MF.getFunction().getCallingConv() == CallingConv::AMDGPU_PS) {
1111 OutStreamer->EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4);
1112 OutStreamer->EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(CurrentProgramInfo.LDSBlocks), 4);
1113 OutStreamer->EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4);
1114 OutStreamer->EmitIntValue(MFI->getPSInputEnable(), 4);
1115 OutStreamer->EmitIntValue(R_0286D0_SPI_PS_INPUT_ADDR, 4);
1116 OutStreamer->EmitIntValue(MFI->getPSInputAddr(), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +00001117 }
Marek Olsak0532c192016-07-13 17:35:15 +00001118
1119 OutStreamer->EmitIntValue(R_SPILLED_SGPRS, 4);
1120 OutStreamer->EmitIntValue(MFI->getNumSpilledSGPRs(), 4);
1121 OutStreamer->EmitIntValue(R_SPILLED_VGPRS, 4);
1122 OutStreamer->EmitIntValue(MFI->getNumSpilledVGPRs(), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +00001123}
1124
Tim Renouf72800f02017-10-03 19:03:52 +00001125// This is the equivalent of EmitProgramInfoSI above, but for when the OS type
1126// is AMDPAL. It stores each compute/SPI register setting and other PAL
Tim Renoufd737b552019-03-20 17:42:00 +00001127// metadata items into the PALMD::Metadata, combining with any provided by the
1128// frontend as LLVM metadata. Once all functions are written, the PAL metadata
1129// is then written as a single block in the .note section.
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001130void AMDGPUAsmPrinter::EmitPALMetadata(const MachineFunction &MF,
Tim Renouf72800f02017-10-03 19:03:52 +00001131 const SIProgramInfo &CurrentProgramInfo) {
1132 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tim Renoufd737b552019-03-20 17:42:00 +00001133 auto CC = MF.getFunction().getCallingConv();
1134 auto MD = getTargetStreamer()->getPALMetadata();
1135
Tim Renoufe7bd52f2019-03-20 18:47:21 +00001136 MD->setEntryPoint(CC, MF.getFunction().getName());
Tim Renoufd737b552019-03-20 17:42:00 +00001137 MD->setNumUsedVgprs(CC, CurrentProgramInfo.NumVGPRsForWavesPerEU);
1138 MD->setNumUsedSgprs(CC, CurrentProgramInfo.NumSGPRsForWavesPerEU);
Matthias Braunf1caa282017-12-15 22:22:58 +00001139 if (AMDGPU::isCompute(MF.getFunction().getCallingConv())) {
Tim Renoufd737b552019-03-20 17:42:00 +00001140 MD->setRsrc1(CC, CurrentProgramInfo.ComputePGMRSrc1);
1141 MD->setRsrc2(CC, CurrentProgramInfo.ComputePGMRSrc2);
Tim Renouf72800f02017-10-03 19:03:52 +00001142 } else {
Tim Renoufd737b552019-03-20 17:42:00 +00001143 MD->setRsrc1(CC, S_00B028_VGPRS(CurrentProgramInfo.VGPRBlocks) |
1144 S_00B028_SGPRS(CurrentProgramInfo.SGPRBlocks));
Tim Renouf72800f02017-10-03 19:03:52 +00001145 if (CurrentProgramInfo.ScratchBlocks > 0)
Tim Renoufd737b552019-03-20 17:42:00 +00001146 MD->setRsrc2(CC, S_00B84C_SCRATCH_EN(1));
Tim Renouf72800f02017-10-03 19:03:52 +00001147 }
Tim Renoufd737b552019-03-20 17:42:00 +00001148 // ScratchSize is in bytes, 16 aligned.
1149 MD->setScratchSize(CC, alignTo(CurrentProgramInfo.ScratchSize, 16));
Matthias Braunf1caa282017-12-15 22:22:58 +00001150 if (MF.getFunction().getCallingConv() == CallingConv::AMDGPU_PS) {
Tim Renoufd737b552019-03-20 17:42:00 +00001151 MD->setRsrc2(CC, S_00B02C_EXTRA_LDS_SIZE(CurrentProgramInfo.LDSBlocks));
1152 MD->setSpiPsInputEna(MFI->getPSInputEnable());
1153 MD->setSpiPsInputAddr(MFI->getPSInputAddr());
Tim Renouf72800f02017-10-03 19:03:52 +00001154 }
Stanislav Mekhanoshin5d00c302019-06-17 16:48:56 +00001155
1156 const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>();
1157 if (STM.isWave32())
1158 MD->setWave32(MF.getFunction().getCallingConv());
Tim Renouf72800f02017-10-03 19:03:52 +00001159}
1160
Matt Arsenault24ee0782016-02-12 02:40:47 +00001161// This is supposed to be log2(Size)
1162static amd_element_byte_size_t getElementByteSizeValue(unsigned Size) {
1163 switch (Size) {
1164 case 4:
1165 return AMD_ELEMENT_4_BYTES;
1166 case 8:
1167 return AMD_ELEMENT_8_BYTES;
1168 case 16:
1169 return AMD_ELEMENT_16_BYTES;
1170 default:
1171 llvm_unreachable("invalid private_element_size");
1172 }
1173}
1174
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001175void AMDGPUAsmPrinter::getAmdKernelCode(amd_kernel_code_t &Out,
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001176 const SIProgramInfo &CurrentProgramInfo,
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001177 const MachineFunction &MF) const {
Matt Arsenault4bec7d42018-07-20 09:05:08 +00001178 const Function &F = MF.getFunction();
1179 assert(F.getCallingConv() == CallingConv::AMDGPU_KERNEL ||
1180 F.getCallingConv() == CallingConv::SPIR_KERNEL);
1181
Tom Stellard45bb48e2015-06-13 03:28:10 +00001182 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellard5bfbae52018-07-11 20:59:01 +00001183 const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +00001184
Matt Arsenault4cd95092019-02-12 23:44:13 +00001185 AMDGPU::initDefaultAMDKernelCodeT(Out, &STM);
Tom Stellard45bb48e2015-06-13 03:28:10 +00001186
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001187 Out.compute_pgm_resource_registers =
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001188 CurrentProgramInfo.ComputePGMRSrc1 |
1189 (CurrentProgramInfo.ComputePGMRSrc2 << 32);
Stanislav Mekhanoshin41bbe102019-05-03 21:26:39 +00001190 Out.code_properties |= AMD_CODE_PROPERTY_IS_PTR64;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001191
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001192 if (CurrentProgramInfo.DynamicCallStack)
1193 Out.code_properties |= AMD_CODE_PROPERTY_IS_DYNAMIC_CALLSTACK;
1194
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001195 AMD_HSA_BITS_SET(Out.code_properties,
Matt Arsenault24ee0782016-02-12 02:40:47 +00001196 AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE,
1197 getElementByteSizeValue(STM.getMaxPrivateElementSize()));
1198
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001199 if (MFI->hasPrivateSegmentBuffer()) {
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001200 Out.code_properties |=
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001201 AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER;
1202 }
1203
1204 if (MFI->hasDispatchPtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001205 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001206
1207 if (MFI->hasQueuePtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001208 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001209
1210 if (MFI->hasKernargSegmentPtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001211 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001212
1213 if (MFI->hasDispatchID())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001214 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001215
1216 if (MFI->hasFlatScratchInit())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001217 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001218
Tom Stellard48f29f22015-11-26 00:43:29 +00001219 if (MFI->hasDispatchPtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001220 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
Tom Stellard48f29f22015-11-26 00:43:29 +00001221
Nicolai Haehnle5b504972016-01-04 23:35:53 +00001222 if (STM.isXNACKEnabled())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001223 Out.code_properties |= AMD_CODE_PROPERTY_IS_XNACK_SUPPORTED;
Nicolai Haehnle5b504972016-01-04 23:35:53 +00001224
Matt Arsenault4bec7d42018-07-20 09:05:08 +00001225 unsigned MaxKernArgAlign;
1226 Out.kernarg_segment_byte_size = STM.getKernArgSegmentSize(F, MaxKernArgAlign);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001227 Out.wavefront_sgpr_count = CurrentProgramInfo.NumSGPR;
1228 Out.workitem_vgpr_count = CurrentProgramInfo.NumVGPR;
1229 Out.workitem_private_segment_byte_size = CurrentProgramInfo.ScratchSize;
1230 Out.workgroup_group_segment_byte_size = CurrentProgramInfo.LDSSize;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001231
Tom Stellard175959e2016-12-06 21:53:10 +00001232 // These alignment values are specified in powers of two, so alignment =
1233 // 2^n. The minimum alignment is 2^4 = 16.
Shawn Landden669775f2019-06-04 14:51:15 +00001234 Out.kernarg_segment_alignment = std::max<size_t>(4,
Matt Arsenault4bec7d42018-07-20 09:05:08 +00001235 countTrailingZeros(MaxKernArgAlign));
Tom Stellard45bb48e2015-06-13 03:28:10 +00001236}
1237
1238bool AMDGPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Tom Stellard45bb48e2015-06-13 03:28:10 +00001239 const char *ExtraCode, raw_ostream &O) {
Matt Arsenault36cd1852017-08-09 20:09:35 +00001240 // First try the generic code, which knows about modifiers like 'c' and 'n'.
Nick Desaulniers5277b3f2019-04-10 16:38:43 +00001241 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O))
Matt Arsenault36cd1852017-08-09 20:09:35 +00001242 return false;
1243
Tom Stellard45bb48e2015-06-13 03:28:10 +00001244 if (ExtraCode && ExtraCode[0]) {
1245 if (ExtraCode[1] != 0)
1246 return true; // Unknown modifier.
1247
1248 switch (ExtraCode[0]) {
Tom Stellard45bb48e2015-06-13 03:28:10 +00001249 case 'r':
1250 break;
Matt Arsenault36cd1852017-08-09 20:09:35 +00001251 default:
1252 return true;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001253 }
1254 }
1255
Matt Arsenault36cd1852017-08-09 20:09:35 +00001256 // TODO: Should be able to support other operand types like globals.
1257 const MachineOperand &MO = MI->getOperand(OpNo);
1258 if (MO.isReg()) {
1259 AMDGPUInstPrinter::printRegOperand(MO.getReg(), O,
1260 *MF->getSubtarget().getRegisterInfo());
1261 return false;
1262 }
1263
1264 return true;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001265}