blob: 9e07b4d252b788025035931e0474a56b53d4cf07 [file] [log] [blame]
James Hendersond68904f2020-01-06 10:15:44 +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.
Matt Arsenaultdb0ed3e2019-10-31 18:50:30 -070072static uint32_t getFPMode(AMDGPU::SIModeRegisterDefaults Mode) {
Tom Stellard45bb48e2015-06-13 03:28:10 +000073
Matt Arsenaultdb0ed3e2019-10-31 18:50:30 -070074 // TODO: Is there any real use for the flush in only / flush out only modes?
Tom Stellard45bb48e2015-06-13 03:28:10 +000075 uint32_t FP32Denormals =
Matt Arsenaultdb0ed3e2019-10-31 18:50:30 -070076 Mode.FP32Denormals ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
Tom Stellard45bb48e2015-06-13 03:28:10 +000077
78 uint32_t FP64Denormals =
Matt Arsenaultdb0ed3e2019-10-31 18:50:30 -070079 Mode.FP64FP16Denormals ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
Tom Stellard45bb48e2015-06-13 03:28:10 +000080
81 return FP_ROUND_MODE_SP(FP_ROUND_ROUND_TO_NEAREST) |
82 FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_NEAREST) |
83 FP_DENORM_MODE_SP(FP32Denormals) |
84 FP_DENORM_MODE_DP(FP64Denormals);
85}
86
87static AsmPrinter *
88createAMDGPUAsmPrinterPass(TargetMachine &tm,
89 std::unique_ptr<MCStreamer> &&Streamer) {
90 return new AMDGPUAsmPrinter(tm, std::move(Streamer));
91}
92
Tom Stellard0dbcb362020-01-14 19:15:07 -080093extern "C" void LLVM_EXTERNAL_VISIBILITY LLVMInitializeAMDGPUAsmPrinter() {
Mehdi Aminif42454b2016-10-09 23:00:34 +000094 TargetRegistry::RegisterAsmPrinter(getTheAMDGPUTarget(),
Tom Stellardc5015012018-05-24 20:02:01 +000095 llvm::createR600AsmPrinterPass);
Mehdi Aminif42454b2016-10-09 23:00:34 +000096 TargetRegistry::RegisterAsmPrinter(getTheGCNTarget(),
97 createAMDGPUAsmPrinterPass);
Tom Stellard45bb48e2015-06-13 03:28:10 +000098}
99
100AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM,
101 std::unique_ptr<MCStreamer> Streamer)
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000102 : AsmPrinter(TM, std::move(Streamer)) {
Matt Arsenault4cd95092019-02-12 23:44:13 +0000103 if (IsaInfo::hasCodeObjectV3(getGlobalSTI()))
Scott Linderf5b36e52018-12-12 19:39:27 +0000104 HSAMetadataStream.reset(new MetadataStreamerV3());
105 else
106 HSAMetadataStream.reset(new MetadataStreamerV2());
Matt Arsenault0da63502018-08-31 05:49:54 +0000107}
Tom Stellard45bb48e2015-06-13 03:28:10 +0000108
Mehdi Amini117296c2016-10-01 02:56:57 +0000109StringRef AMDGPUAsmPrinter::getPassName() const {
Matt Arsenaultf9245b72016-07-22 17:01:25 +0000110 return "AMDGPU Assembly Printer";
111}
112
Matt Arsenault4cd95092019-02-12 23:44:13 +0000113const MCSubtargetInfo *AMDGPUAsmPrinter::getGlobalSTI() const {
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000114 return TM.getMCSubtargetInfo();
115}
116
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000117AMDGPUTargetStreamer* AMDGPUAsmPrinter::getTargetStreamer() const {
118 if (!OutStreamer)
119 return nullptr;
120 return static_cast<AMDGPUTargetStreamer*>(OutStreamer->getTargetStreamer());
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000121}
122
Tom Stellardf4218372016-01-12 17:18:17 +0000123void AMDGPUAsmPrinter::EmitStartOfAsmFile(Module &M) {
Matt Arsenault4cd95092019-02-12 23:44:13 +0000124 if (IsaInfo::hasCodeObjectV3(getGlobalSTI())) {
Konstantin Zhuravlyov94dfcc2e2018-10-15 20:37:47 +0000125 std::string ExpectedTarget;
126 raw_string_ostream ExpectedTargetOS(ExpectedTarget);
Matt Arsenault4cd95092019-02-12 23:44:13 +0000127 IsaInfo::streamIsaVersion(getGlobalSTI(), ExpectedTargetOS);
Konstantin Zhuravlyov94dfcc2e2018-10-15 20:37:47 +0000128
129 getTargetStreamer()->EmitDirectiveAMDGCNTarget(ExpectedTarget);
Konstantin Zhuravlyov94dfcc2e2018-10-15 20:37:47 +0000130 }
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000131
Konstantin Zhuravlyoveda425e2017-10-14 15:59:07 +0000132 if (TM.getTargetTriple().getOS() != Triple::AMDHSA &&
133 TM.getTargetTriple().getOS() != Triple::AMDPAL)
134 return;
135
136 if (TM.getTargetTriple().getOS() == Triple::AMDHSA)
Scott Linderf5b36e52018-12-12 19:39:27 +0000137 HSAMetadataStream->begin(M);
Konstantin Zhuravlyoveda425e2017-10-14 15:59:07 +0000138
139 if (TM.getTargetTriple().getOS() == Triple::AMDPAL)
Tim Renoufd737b552019-03-20 17:42:00 +0000140 getTargetStreamer()->getPALMetadata()->readFromIR(M);
Konstantin Zhuravlyoveda425e2017-10-14 15:59:07 +0000141
Matt Arsenault4cd95092019-02-12 23:44:13 +0000142 if (IsaInfo::hasCodeObjectV3(getGlobalSTI()))
Scott Linderf5b36e52018-12-12 19:39:27 +0000143 return;
144
Konstantin Zhuravlyoveda425e2017-10-14 15:59:07 +0000145 // HSA emits NT_AMDGPU_HSA_CODE_OBJECT_VERSION for code objects v2.
146 if (TM.getTargetTriple().getOS() == Triple::AMDHSA)
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000147 getTargetStreamer()->EmitDirectiveHSACodeObjectVersion(2, 1);
Konstantin Zhuravlyoveda425e2017-10-14 15:59:07 +0000148
149 // HSA and PAL emit NT_AMDGPU_HSA_ISA for code objects v2.
Matt Arsenault4cd95092019-02-12 23:44:13 +0000150 IsaVersion Version = getIsaVersion(getGlobalSTI()->getCPU());
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000151 getTargetStreamer()->EmitDirectiveHSACodeObjectISA(
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000152 Version.Major, Version.Minor, Version.Stepping, "AMD", "AMDGPU");
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000153}
154
155void AMDGPUAsmPrinter::EmitEndOfAsmFile(Module &M) {
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000156 // Following code requires TargetStreamer to be present.
157 if (!getTargetStreamer())
158 return;
159
Matt Arsenault4cd95092019-02-12 23:44:13 +0000160 if (!IsaInfo::hasCodeObjectV3(getGlobalSTI())) {
Scott Linderf5b36e52018-12-12 19:39:27 +0000161 // Emit ISA Version (NT_AMD_AMDGPU_ISA).
162 std::string ISAVersionString;
163 raw_string_ostream ISAVersionStream(ISAVersionString);
Matt Arsenault4cd95092019-02-12 23:44:13 +0000164 IsaInfo::streamIsaVersion(getGlobalSTI(), ISAVersionStream);
Scott Linderf5b36e52018-12-12 19:39:27 +0000165 getTargetStreamer()->EmitISAVersion(ISAVersionStream.str());
166 }
Konstantin Zhuravlyov9c05b2b2017-10-14 15:40:33 +0000167
168 // Emit HSA Metadata (NT_AMD_AMDGPU_HSA_METADATA).
169 if (TM.getTargetTriple().getOS() == Triple::AMDHSA) {
Scott Linderf5b36e52018-12-12 19:39:27 +0000170 HSAMetadataStream->end();
171 bool Success = HSAMetadataStream->emitTo(*getTargetStreamer());
172 (void)Success;
173 assert(Success && "Malformed HSA Metadata");
Konstantin Zhuravlyov9c05b2b2017-10-14 15:40:33 +0000174 }
Tom Stellardf4218372016-01-12 17:18:17 +0000175}
176
Matt Arsenault6bc43d82016-10-06 16:20:41 +0000177bool AMDGPUAsmPrinter::isBlockOnlyReachableByFallthrough(
178 const MachineBasicBlock *MBB) const {
179 if (!AsmPrinter::isBlockOnlyReachableByFallthrough(MBB))
180 return false;
181
182 if (MBB->empty())
183 return true;
184
185 // If this is a block implementing a long branch, an expression relative to
186 // the start of the block is needed. to the start of the block.
187 // XXX - Is there a smarter way to check this?
188 return (MBB->back().getOpcode() != AMDGPU::S_SETPC_B64);
189}
190
Tom Stellardf151a452015-06-26 21:14:58 +0000191void AMDGPUAsmPrinter::EmitFunctionBodyStart() {
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000192 const SIMachineFunctionInfo &MFI = *MF->getInfo<SIMachineFunctionInfo>();
193 if (!MFI.isEntryFunction())
194 return;
Matt Arsenault021a2182017-04-19 19:38:10 +0000195
Tom Stellard5bfbae52018-07-11 20:59:01 +0000196 const GCNSubtarget &STM = MF->getSubtarget<GCNSubtarget>();
Matt Arsenault4bec7d42018-07-20 09:05:08 +0000197 const Function &F = MF->getFunction();
Konstantin Zhuravlyovaa067cb2018-10-04 21:02:16 +0000198 if (!STM.hasCodeObjectV3() && STM.isAmdHsaOrMesa(F) &&
Matt Arsenault4bec7d42018-07-20 09:05:08 +0000199 (F.getCallingConv() == CallingConv::AMDGPU_KERNEL ||
200 F.getCallingConv() == CallingConv::SPIR_KERNEL)) {
201 amd_kernel_code_t KernelCode;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000202 getAmdKernelCode(KernelCode, CurrentProgramInfo, *MF);
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000203 getTargetStreamer()->EmitAMDKernelCodeT(KernelCode);
Tom Stellardf151a452015-06-26 21:14:58 +0000204 }
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000205
Scott Linderf5b36e52018-12-12 19:39:27 +0000206 if (STM.isAmdHsaOS())
207 HSAMetadataStream->emitKernel(*MF, CurrentProgramInfo);
Tom Stellardf151a452015-06-26 21:14:58 +0000208}
209
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000210void AMDGPUAsmPrinter::EmitFunctionBodyEnd() {
211 const SIMachineFunctionInfo &MFI = *MF->getInfo<SIMachineFunctionInfo>();
212 if (!MFI.isEntryFunction())
213 return;
Matt Arsenault4cd95092019-02-12 23:44:13 +0000214
215 if (!IsaInfo::hasCodeObjectV3(getGlobalSTI()) ||
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000216 TM.getTargetTriple().getOS() != Triple::AMDHSA)
217 return;
218
Konstantin Zhuravlyovce25bc32018-06-12 18:33:51 +0000219 auto &Streamer = getTargetStreamer()->getStreamer();
220 auto &Context = Streamer.getContext();
221 auto &ObjectFileInfo = *Context.getObjectFileInfo();
222 auto &ReadOnlySection = *ObjectFileInfo.getReadOnlySection();
223
224 Streamer.PushSection();
225 Streamer.SwitchSection(&ReadOnlySection);
226
227 // CP microcode requires the kernel descriptor to be allocated on 64 byte
228 // alignment.
229 Streamer.EmitValueToAlignment(64, 0, 1, 0);
230 if (ReadOnlySection.getAlignment() < 64)
Guillaume Chatelet18f805a2019-09-27 12:54:21 +0000231 ReadOnlySection.setAlignment(Align(64));
Konstantin Zhuravlyovce25bc32018-06-12 18:33:51 +0000232
Matt Arsenault4cd95092019-02-12 23:44:13 +0000233 const MCSubtargetInfo &STI = MF->getSubtarget();
234
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000235 SmallString<128> KernelName;
236 getNameWithPrefix(KernelName, &MF->getFunction());
237 getTargetStreamer()->EmitAmdhsaKernelDescriptor(
Matt Arsenault4cd95092019-02-12 23:44:13 +0000238 STI, KernelName, getAmdhsaKernelDescriptor(*MF, CurrentProgramInfo),
Scott Linder1e8c2c72018-06-21 19:38:56 +0000239 CurrentProgramInfo.NumVGPRsForWavesPerEU,
240 CurrentProgramInfo.NumSGPRsForWavesPerEU -
Matt Arsenault4cd95092019-02-12 23:44:13 +0000241 IsaInfo::getNumExtraSGPRs(&STI,
Scott Linder1e8c2c72018-06-21 19:38:56 +0000242 CurrentProgramInfo.VCCUsed,
243 CurrentProgramInfo.FlatUsed),
244 CurrentProgramInfo.VCCUsed, CurrentProgramInfo.FlatUsed,
Matt Arsenault4cd95092019-02-12 23:44:13 +0000245 hasXNACK(STI));
Konstantin Zhuravlyovce25bc32018-06-12 18:33:51 +0000246
247 Streamer.PopSection();
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000248}
249
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000250void AMDGPUAsmPrinter::EmitFunctionEntryLabel() {
Matt Arsenault4cd95092019-02-12 23:44:13 +0000251 if (IsaInfo::hasCodeObjectV3(getGlobalSTI()) &&
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000252 TM.getTargetTriple().getOS() == Triple::AMDHSA) {
253 AsmPrinter::EmitFunctionEntryLabel();
254 return;
255 }
256
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000257 const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
Tom Stellard5bfbae52018-07-11 20:59:01 +0000258 const GCNSubtarget &STM = MF->getSubtarget<GCNSubtarget>();
Konstantin Zhuravlyovaa067cb2018-10-04 21:02:16 +0000259 if (MFI->isEntryFunction() && STM.isAmdHsaOrMesa(MF->getFunction())) {
Tom Stellard1b9748c2016-09-26 17:29:25 +0000260 SmallString<128> SymbolName;
Matthias Braunf1caa282017-12-15 22:22:58 +0000261 getNameWithPrefix(SymbolName, &MF->getFunction()),
Konstantin Zhuravlyov8c18f5b2017-10-14 22:16:26 +0000262 getTargetStreamer()->EmitAMDGPUSymbolType(
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000263 SymbolName, ELF::STT_AMDGPU_HSA_KERNEL);
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000264 }
Tim Renouf33cb8f52019-05-14 16:17:14 +0000265 if (DumpCodeInstEmitter) {
Tim Renoufcead41d2017-12-08 14:09:34 +0000266 // Disassemble function name label to text.
Matthias Braunf1caa282017-12-15 22:22:58 +0000267 DisasmLines.push_back(MF->getName().str() + ":");
Tim Renoufcead41d2017-12-08 14:09:34 +0000268 DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLines.back().size());
269 HexLines.push_back("");
270 }
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000271
272 AsmPrinter::EmitFunctionEntryLabel();
273}
274
Karl-Johan Karlsson40da6be2019-08-20 05:13:57 +0000275void AMDGPUAsmPrinter::EmitBasicBlockStart(const MachineBasicBlock &MBB) {
Tim Renouf33cb8f52019-05-14 16:17:14 +0000276 if (DumpCodeInstEmitter && !isBlockOnlyReachableByFallthrough(&MBB)) {
Tim Renoufcead41d2017-12-08 14:09:34 +0000277 // Write a line for the basic block label if it is not only fallthrough.
278 DisasmLines.push_back(
279 (Twine("BB") + Twine(getFunctionNumber())
280 + "_" + Twine(MBB.getNumber()) + ":").str());
281 DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLines.back().size());
282 HexLines.push_back("");
283 }
284 AsmPrinter::EmitBasicBlockStart(MBB);
285}
286
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000287void AMDGPUAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
Nicolai Haehnle27101712019-06-25 11:52:30 +0000288 if (GV->getAddressSpace() == AMDGPUAS::LOCAL_ADDRESS) {
289 if (GV->hasInitializer() && !isa<UndefValue>(GV->getInitializer())) {
290 OutContext.reportError({},
291 Twine(GV->getName()) +
292 ": unsupported initializer for address space");
293 return;
294 }
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000295
Nicolai Haehnle27101712019-06-25 11:52:30 +0000296 // LDS variables aren't emitted in HSA or PAL yet.
297 const Triple::OSType OS = TM.getTargetTriple().getOS();
298 if (OS == Triple::AMDHSA || OS == Triple::AMDPAL)
299 return;
300
301 MCSymbol *GVSym = getSymbol(GV);
302
303 GVSym->redefineIfPossible();
304 if (GVSym->isDefined() || GVSym->isVariable())
305 report_fatal_error("symbol '" + Twine(GVSym->getName()) +
306 "' is already defined");
307
308 const DataLayout &DL = GV->getParent()->getDataLayout();
309 uint64_t Size = DL.getTypeAllocSize(GV->getValueType());
310 unsigned Align = GV->getAlignment();
311 if (!Align)
312 Align = 4;
313
314 EmitVisibility(GVSym, GV->getVisibility(), !GV->isDeclaration());
315 EmitLinkage(GV, GVSym);
Michael Liaof0a665a2019-06-25 14:06:34 +0000316 if (auto TS = getTargetStreamer())
317 TS->emitAMDGPULDS(GVSym, Size, Align);
Tom Stellard00f2f912015-12-02 19:47:57 +0000318 return;
Nicolai Haehnle27101712019-06-25 11:52:30 +0000319 }
Tom Stellard00f2f912015-12-02 19:47:57 +0000320
Tom Stellardfcfaea42016-05-05 17:03:33 +0000321 AsmPrinter::EmitGlobalVariable(GV);
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000322}
323
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000324bool AMDGPUAsmPrinter::doFinalization(Module &M) {
325 CallGraphResourceInfo.clear();
Stanislav Mekhanoshin41bbe102019-05-03 21:26:39 +0000326
Nicolai Haehnleae4fcb92019-06-17 19:28:43 +0000327 // Pad with s_code_end to help tools and guard against instruction prefetch
328 // causing stale data in caches. Arguably this should be done by the linker,
329 // which is why this isn't done for Mesa.
330 const MCSubtargetInfo &STI = *getGlobalSTI();
331 if (AMDGPU::isGFX10(STI) &&
332 (STI.getTargetTriple().getOS() == Triple::AMDHSA ||
333 STI.getTargetTriple().getOS() == Triple::AMDPAL)) {
Stanislav Mekhanoshin41bbe102019-05-03 21:26:39 +0000334 OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
335 getTargetStreamer()->EmitCodeEnd();
336 }
337
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000338 return AsmPrinter::doFinalization(M);
339}
340
341// Print comments that apply to both callable functions and entry points.
342void AMDGPUAsmPrinter::emitCommonFunctionComments(
343 uint32_t NumVGPR,
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000344 Optional<uint32_t> NumAGPR,
345 uint32_t TotalNumVGPR,
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000346 uint32_t NumSGPR,
Matt Arsenault9ba465a2017-11-14 20:33:14 +0000347 uint64_t ScratchSize,
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000348 uint64_t CodeSize,
349 const AMDGPUMachineFunction *MFI) {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000350 OutStreamer->emitRawComment(" codeLenInByte = " + Twine(CodeSize), false);
351 OutStreamer->emitRawComment(" NumSgprs: " + Twine(NumSGPR), false);
352 OutStreamer->emitRawComment(" NumVgprs: " + Twine(NumVGPR), false);
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000353 if (NumAGPR) {
354 OutStreamer->emitRawComment(" NumAgprs: " + Twine(*NumAGPR), false);
355 OutStreamer->emitRawComment(" TotalNumVgprs: " + Twine(TotalNumVGPR),
356 false);
357 }
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000358 OutStreamer->emitRawComment(" ScratchSize: " + Twine(ScratchSize), false);
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000359 OutStreamer->emitRawComment(" MemoryBound: " + Twine(MFI->isMemoryBound()),
360 false);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000361}
362
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000363uint16_t AMDGPUAsmPrinter::getAmdhsaKernelCodeProperties(
364 const MachineFunction &MF) const {
365 const SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>();
366 uint16_t KernelCodeProperties = 0;
367
368 if (MFI.hasPrivateSegmentBuffer()) {
369 KernelCodeProperties |=
370 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER;
371 }
372 if (MFI.hasDispatchPtr()) {
373 KernelCodeProperties |=
374 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
375 }
376 if (MFI.hasQueuePtr()) {
377 KernelCodeProperties |=
378 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR;
379 }
380 if (MFI.hasKernargSegmentPtr()) {
381 KernelCodeProperties |=
382 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR;
383 }
384 if (MFI.hasDispatchID()) {
385 KernelCodeProperties |=
386 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID;
387 }
388 if (MFI.hasFlatScratchInit()) {
389 KernelCodeProperties |=
390 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT;
391 }
Stanislav Mekhanoshin5d00c302019-06-17 16:48:56 +0000392 if (MF.getSubtarget<GCNSubtarget>().isWave32()) {
393 KernelCodeProperties |=
394 amdhsa::KERNEL_CODE_PROPERTY_ENABLE_WAVEFRONT_SIZE32;
395 }
Konstantin Zhuravlyov00f2cb12018-06-12 18:02:46 +0000396
397 return KernelCodeProperties;
398}
399
400amdhsa::kernel_descriptor_t AMDGPUAsmPrinter::getAmdhsaKernelDescriptor(
401 const MachineFunction &MF,
402 const SIProgramInfo &PI) const {
403 amdhsa::kernel_descriptor_t KernelDescriptor;
404 memset(&KernelDescriptor, 0x0, sizeof(KernelDescriptor));
405
406 assert(isUInt<32>(PI.ScratchSize));
407 assert(isUInt<32>(PI.ComputePGMRSrc1));
408 assert(isUInt<32>(PI.ComputePGMRSrc2));
409
410 KernelDescriptor.group_segment_fixed_size = PI.LDSSize;
411 KernelDescriptor.private_segment_fixed_size = PI.ScratchSize;
412 KernelDescriptor.compute_pgm_rsrc1 = PI.ComputePGMRSrc1;
413 KernelDescriptor.compute_pgm_rsrc2 = PI.ComputePGMRSrc2;
414 KernelDescriptor.kernel_code_properties = getAmdhsaKernelCodeProperties(MF);
415
416 return KernelDescriptor;
417}
418
Tom Stellard45bb48e2015-06-13 03:28:10 +0000419bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000420 CurrentProgramInfo = SIProgramInfo();
421
Matt Arsenault6cb7b8a2017-04-19 17:42:39 +0000422 const AMDGPUMachineFunction *MFI = MF.getInfo<AMDGPUMachineFunction>();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000423
424 // The starting address of all shader programs must be 256 bytes aligned.
Matt Arsenault6cb7b8a2017-04-19 17:42:39 +0000425 // Regular functions just need the basic required instruction alignment.
Guillaume Chatelet18f805a2019-09-27 12:54:21 +0000426 MF.setAlignment(MFI->isEntryFunction() ? Align(256) : Align(4));
Tom Stellard45bb48e2015-06-13 03:28:10 +0000427
428 SetupMachineFunction(MF);
429
Tom Stellard5bfbae52018-07-11 20:59:01 +0000430 const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>();
Konstantin Zhuravlyov67a6d542017-01-06 17:02:10 +0000431 MCContext &Context = getObjFileLowering().getContext();
Tim Renouf807ecc32018-02-06 13:39:38 +0000432 // FIXME: This should be an explicit check for Mesa.
433 if (!STM.isAmdHsaOS() && !STM.isAmdPalOS()) {
Konstantin Zhuravlyov67a6d542017-01-06 17:02:10 +0000434 MCSectionELF *ConfigSection =
435 Context.getELFSection(".AMDGPU.config", ELF::SHT_PROGBITS, 0);
436 OutStreamer->SwitchSection(ConfigSection);
437 }
438
Tom Stellardc5015012018-05-24 20:02:01 +0000439 if (MFI->isEntryFunction()) {
440 getSIProgramInfo(CurrentProgramInfo, MF);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000441 } else {
Tom Stellardc5015012018-05-24 20:02:01 +0000442 auto I = CallGraphResourceInfo.insert(
443 std::make_pair(&MF.getFunction(), SIFunctionResourceInfo()));
444 SIFunctionResourceInfo &Info = I.first->second;
445 assert(I.second && "should only be called once per function");
446 Info = analyzeResourceUsage(MF);
447 }
448
449 if (STM.isAmdPalOS())
450 EmitPALMetadata(MF, CurrentProgramInfo);
451 else if (!STM.isAmdHsaOS()) {
452 EmitProgramInfoSI(MF, CurrentProgramInfo);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000453 }
454
Jay Foad84792402019-06-27 08:19:28 +0000455 DumpCodeInstEmitter = nullptr;
456 if (STM.dumpCode()) {
457 // For -dumpcode, get the assembler out of the streamer, even if it does
458 // not really want to let us have it. This only works with -filetype=obj.
459 bool SaveFlag = OutStreamer->getUseAssemblerInfoForParsing();
460 OutStreamer->setUseAssemblerInfoForParsing(true);
461 MCAssembler *Assembler = OutStreamer->getAssemblerPtr();
462 OutStreamer->setUseAssemblerInfoForParsing(SaveFlag);
463 if (Assembler)
464 DumpCodeInstEmitter = Assembler->getEmitterPtr();
465 }
466
Tom Stellard45bb48e2015-06-13 03:28:10 +0000467 DisasmLines.clear();
468 HexLines.clear();
469 DisasmLineMaxLen = 0;
470
471 EmitFunctionBody();
472
473 if (isVerbose()) {
474 MCSectionELF *CommentSection =
475 Context.getELFSection(".AMDGPU.csdata", ELF::SHT_PROGBITS, 0);
476 OutStreamer->SwitchSection(CommentSection);
477
Tom Stellardc5015012018-05-24 20:02:01 +0000478 if (!MFI->isEntryFunction()) {
479 OutStreamer->emitRawComment(" Function info:", false);
480 SIFunctionResourceInfo &Info = CallGraphResourceInfo[&MF.getFunction()];
481 emitCommonFunctionComments(
482 Info.NumVGPR,
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000483 STM.hasMAIInsts() ? Info.NumAGPR : Optional<uint32_t>(),
484 Info.getTotalNumVGPRs(STM),
Tom Stellard5bfbae52018-07-11 20:59:01 +0000485 Info.getTotalNumSGPRs(MF.getSubtarget<GCNSubtarget>()),
Tom Stellardc5015012018-05-24 20:02:01 +0000486 Info.PrivateSegmentSize,
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000487 getFunctionCodeSize(MF), MFI);
Tom Stellardc5015012018-05-24 20:02:01 +0000488 return false;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000489 }
Tom Stellardc5015012018-05-24 20:02:01 +0000490
491 OutStreamer->emitRawComment(" Kernel info:", false);
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000492 emitCommonFunctionComments(CurrentProgramInfo.NumArchVGPR,
493 STM.hasMAIInsts()
494 ? CurrentProgramInfo.NumAccVGPR
495 : Optional<uint32_t>(),
496 CurrentProgramInfo.NumVGPR,
Tom Stellardc5015012018-05-24 20:02:01 +0000497 CurrentProgramInfo.NumSGPR,
498 CurrentProgramInfo.ScratchSize,
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000499 getFunctionCodeSize(MF), MFI);
Tom Stellardc5015012018-05-24 20:02:01 +0000500
501 OutStreamer->emitRawComment(
502 " FloatMode: " + Twine(CurrentProgramInfo.FloatMode), false);
503 OutStreamer->emitRawComment(
504 " IeeeMode: " + Twine(CurrentProgramInfo.IEEEMode), false);
505 OutStreamer->emitRawComment(
506 " LDSByteSize: " + Twine(CurrentProgramInfo.LDSSize) +
507 " bytes/workgroup (compile time only)", false);
508
509 OutStreamer->emitRawComment(
510 " SGPRBlocks: " + Twine(CurrentProgramInfo.SGPRBlocks), false);
511 OutStreamer->emitRawComment(
512 " VGPRBlocks: " + Twine(CurrentProgramInfo.VGPRBlocks), false);
513
514 OutStreamer->emitRawComment(
515 " NumSGPRsForWavesPerEU: " +
516 Twine(CurrentProgramInfo.NumSGPRsForWavesPerEU), false);
517 OutStreamer->emitRawComment(
518 " NumVGPRsForWavesPerEU: " +
519 Twine(CurrentProgramInfo.NumVGPRsForWavesPerEU), false);
520
521 OutStreamer->emitRawComment(
Stanislav Mekhanoshin2594fa82019-07-31 01:07:10 +0000522 " Occupancy: " +
523 Twine(CurrentProgramInfo.Occupancy), false);
524
525 OutStreamer->emitRawComment(
Stanislav Mekhanoshin1c538422018-05-25 17:25:12 +0000526 " WaveLimiterHint : " + Twine(MFI->needsWaveLimiter()), false);
527
Tom Stellardc5015012018-05-24 20:02:01 +0000528 OutStreamer->emitRawComment(
529 " COMPUTE_PGM_RSRC2:USER_SGPR: " +
530 Twine(G_00B84C_USER_SGPR(CurrentProgramInfo.ComputePGMRSrc2)), false);
531 OutStreamer->emitRawComment(
532 " COMPUTE_PGM_RSRC2:TRAP_HANDLER: " +
533 Twine(G_00B84C_TRAP_HANDLER(CurrentProgramInfo.ComputePGMRSrc2)), false);
534 OutStreamer->emitRawComment(
535 " COMPUTE_PGM_RSRC2:TGID_X_EN: " +
536 Twine(G_00B84C_TGID_X_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
537 OutStreamer->emitRawComment(
538 " COMPUTE_PGM_RSRC2:TGID_Y_EN: " +
539 Twine(G_00B84C_TGID_Y_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
540 OutStreamer->emitRawComment(
541 " COMPUTE_PGM_RSRC2:TGID_Z_EN: " +
542 Twine(G_00B84C_TGID_Z_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
543 OutStreamer->emitRawComment(
544 " COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: " +
545 Twine(G_00B84C_TIDIG_COMP_CNT(CurrentProgramInfo.ComputePGMRSrc2)),
546 false);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000547 }
548
Tim Renouf33cb8f52019-05-14 16:17:14 +0000549 if (DumpCodeInstEmitter) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000550
551 OutStreamer->SwitchSection(
552 Context.getELFSection(".AMDGPU.disasm", ELF::SHT_NOTE, 0));
553
554 for (size_t i = 0; i < DisasmLines.size(); ++i) {
Tim Renoufcead41d2017-12-08 14:09:34 +0000555 std::string Comment = "\n";
556 if (!HexLines[i].empty()) {
557 Comment = std::string(DisasmLineMaxLen - DisasmLines[i].size(), ' ');
558 Comment += " ; " + HexLines[i] + "\n";
559 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000560
561 OutStreamer->EmitBytes(StringRef(DisasmLines[i]));
562 OutStreamer->EmitBytes(StringRef(Comment));
563 }
564 }
565
566 return false;
567}
568
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000569uint64_t AMDGPUAsmPrinter::getFunctionCodeSize(const MachineFunction &MF) const {
Tom Stellard5bfbae52018-07-11 20:59:01 +0000570 const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000571 const SIInstrInfo *TII = STM.getInstrInfo();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000572
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000573 uint64_t CodeSize = 0;
574
Tom Stellard45bb48e2015-06-13 03:28:10 +0000575 for (const MachineBasicBlock &MBB : MF) {
576 for (const MachineInstr &MI : MBB) {
577 // TODO: CodeSize should account for multiple functions.
Matt Arsenaultc5746862015-08-12 09:04:44 +0000578
579 // TODO: Should we count size of debug info?
Shiva Chen801bf7e2018-05-09 02:42:00 +0000580 if (MI.isDebugInstr())
Matt Arsenaultc5746862015-08-12 09:04:44 +0000581 continue;
582
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000583 CodeSize += TII->getInstSizeInBytes(MI);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000584 }
585 }
586
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000587 return CodeSize;
588}
589
590static bool hasAnyNonFlatUseOfReg(const MachineRegisterInfo &MRI,
591 const SIInstrInfo &TII,
592 unsigned Reg) {
593 for (const MachineOperand &UseOp : MRI.reg_operands(Reg)) {
594 if (!UseOp.isImplicit() || !TII.isFLAT(*UseOp.getParent()))
595 return true;
596 }
597
598 return false;
599}
600
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000601int32_t AMDGPUAsmPrinter::SIFunctionResourceInfo::getTotalNumSGPRs(
Tom Stellard5bfbae52018-07-11 20:59:01 +0000602 const GCNSubtarget &ST) const {
Konstantin Zhuravlyov71e43ee2018-09-12 18:50:47 +0000603 return NumExplicitSGPR + IsaInfo::getNumExtraSGPRs(&ST,
Scott Linder1e8c2c72018-06-21 19:38:56 +0000604 UsesVCC, UsesFlatScratch);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000605}
606
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000607int32_t AMDGPUAsmPrinter::SIFunctionResourceInfo::getTotalNumVGPRs(
608 const GCNSubtarget &ST) const {
609 return std::max(NumVGPR, NumAGPR);
610}
611
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000612AMDGPUAsmPrinter::SIFunctionResourceInfo AMDGPUAsmPrinter::analyzeResourceUsage(
613 const MachineFunction &MF) const {
614 SIFunctionResourceInfo Info;
615
616 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellard5bfbae52018-07-11 20:59:01 +0000617 const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000618 const MachineFrameInfo &FrameInfo = MF.getFrameInfo();
619 const MachineRegisterInfo &MRI = MF.getRegInfo();
620 const SIInstrInfo *TII = ST.getInstrInfo();
621 const SIRegisterInfo &TRI = TII->getRegisterInfo();
622
623 Info.UsesFlatScratch = MRI.isPhysRegUsed(AMDGPU::FLAT_SCR_LO) ||
624 MRI.isPhysRegUsed(AMDGPU::FLAT_SCR_HI);
625
626 // Even if FLAT_SCRATCH is implicitly used, it has no effect if flat
627 // instructions aren't used to access the scratch buffer. Inline assembly may
628 // need it though.
629 //
630 // If we only have implicit uses of flat_scr on flat instructions, it is not
631 // really needed.
632 if (Info.UsesFlatScratch && !MFI->hasFlatScratchInit() &&
633 (!hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR) &&
634 !hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR_LO) &&
635 !hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR_HI))) {
636 Info.UsesFlatScratch = false;
637 }
638
639 Info.HasDynamicallySizedStack = FrameInfo.hasVarSizedObjects();
640 Info.PrivateSegmentSize = FrameInfo.getStackSize();
Matt Arsenault03ae3992018-03-29 21:30:06 +0000641 if (MFI->isStackRealigned())
642 Info.PrivateSegmentSize += FrameInfo.getMaxAlignment();
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000643
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000644
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000645 Info.UsesVCC = MRI.isPhysRegUsed(AMDGPU::VCC_LO) ||
646 MRI.isPhysRegUsed(AMDGPU::VCC_HI);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000647
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000648 // If there are no calls, MachineRegisterInfo can tell us the used register
649 // count easily.
Matt Arsenault22cdb612017-09-05 18:36:36 +0000650 // A tail call isn't considered a call for MachineFrameInfo's purposes.
651 if (!FrameInfo.hasCalls() && !FrameInfo.hasTailCall()) {
Matt Arsenault2738ede2017-08-02 17:15:01 +0000652 MCPhysReg HighestVGPRReg = AMDGPU::NoRegister;
653 for (MCPhysReg Reg : reverse(AMDGPU::VGPR_32RegClass.getRegisters())) {
654 if (MRI.isPhysRegUsed(Reg)) {
655 HighestVGPRReg = Reg;
656 break;
657 }
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000658 }
659
660 if (ST.hasMAIInsts()) {
661 MCPhysReg HighestAGPRReg = AMDGPU::NoRegister;
662 for (MCPhysReg Reg : reverse(AMDGPU::AGPR_32RegClass.getRegisters())) {
663 if (MRI.isPhysRegUsed(Reg)) {
664 HighestAGPRReg = Reg;
665 break;
666 }
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000667 }
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000668 Info.NumAGPR = HighestAGPRReg == AMDGPU::NoRegister ? 0 :
669 TRI.getHWRegIndex(HighestAGPRReg) + 1;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000670 }
Matt Arsenault2738ede2017-08-02 17:15:01 +0000671
672 MCPhysReg HighestSGPRReg = AMDGPU::NoRegister;
673 for (MCPhysReg Reg : reverse(AMDGPU::SGPR_32RegClass.getRegisters())) {
674 if (MRI.isPhysRegUsed(Reg)) {
675 HighestSGPRReg = Reg;
676 break;
677 }
678 }
679
680 // We found the maximum register index. They start at 0, so add one to get the
681 // number of registers.
682 Info.NumVGPR = HighestVGPRReg == AMDGPU::NoRegister ? 0 :
683 TRI.getHWRegIndex(HighestVGPRReg) + 1;
684 Info.NumExplicitSGPR = HighestSGPRReg == AMDGPU::NoRegister ? 0 :
685 TRI.getHWRegIndex(HighestSGPRReg) + 1;
686
687 return Info;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000688 }
689
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000690 int32_t MaxVGPR = -1;
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000691 int32_t MaxAGPR = -1;
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000692 int32_t MaxSGPR = -1;
Matt Arsenault9ba465a2017-11-14 20:33:14 +0000693 uint64_t CalleeFrameSize = 0;
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000694
695 for (const MachineBasicBlock &MBB : MF) {
696 for (const MachineInstr &MI : MBB) {
697 // TODO: Check regmasks? Do they occur anywhere except calls?
698 for (const MachineOperand &MO : MI.operands()) {
699 unsigned Width = 0;
700 bool IsSGPR = false;
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000701 bool IsAGPR = false;
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000702
703 if (!MO.isReg())
704 continue;
705
Daniel Sanders0c476112019-08-15 19:22:08 +0000706 Register Reg = MO.getReg();
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000707 switch (Reg) {
708 case AMDGPU::EXEC:
709 case AMDGPU::EXEC_LO:
710 case AMDGPU::EXEC_HI:
711 case AMDGPU::SCC:
712 case AMDGPU::M0:
713 case AMDGPU::SRC_SHARED_BASE:
714 case AMDGPU::SRC_SHARED_LIMIT:
715 case AMDGPU::SRC_PRIVATE_BASE:
716 case AMDGPU::SRC_PRIVATE_LIMIT:
Stanislav Mekhanoshin33d806a2019-04-24 17:28:30 +0000717 case AMDGPU::SGPR_NULL:
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000718 continue;
719
Dmitry Preobrazhensky137976f2019-03-20 15:40:52 +0000720 case AMDGPU::SRC_POPS_EXITING_WAVE_ID:
721 llvm_unreachable("src_pops_exiting_wave_id should not be used");
722
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000723 case AMDGPU::NoRegister:
Shiva Chen801bf7e2018-05-09 02:42:00 +0000724 assert(MI.isDebugInstr());
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000725 continue;
726
727 case AMDGPU::VCC:
728 case AMDGPU::VCC_LO:
729 case AMDGPU::VCC_HI:
730 Info.UsesVCC = true;
731 continue;
732
733 case AMDGPU::FLAT_SCR:
734 case AMDGPU::FLAT_SCR_LO:
735 case AMDGPU::FLAT_SCR_HI:
736 continue;
737
Dmitry Preobrazhensky3afbd822018-01-10 14:22:19 +0000738 case AMDGPU::XNACK_MASK:
739 case AMDGPU::XNACK_MASK_LO:
740 case AMDGPU::XNACK_MASK_HI:
741 llvm_unreachable("xnack_mask registers should not be used");
742
Dmitry Preobrazhensky942c2732019-02-08 14:57:37 +0000743 case AMDGPU::LDS_DIRECT:
744 llvm_unreachable("lds_direct register should not be used");
745
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000746 case AMDGPU::TBA:
747 case AMDGPU::TBA_LO:
748 case AMDGPU::TBA_HI:
749 case AMDGPU::TMA:
750 case AMDGPU::TMA_LO:
751 case AMDGPU::TMA_HI:
752 llvm_unreachable("trap handler registers should not be used");
753
Dmitry Preobrazhensky9111f352019-06-03 13:51:24 +0000754 case AMDGPU::SRC_VCCZ:
755 llvm_unreachable("src_vccz register should not be used");
756
757 case AMDGPU::SRC_EXECZ:
758 llvm_unreachable("src_execz register should not be used");
759
760 case AMDGPU::SRC_SCC:
761 llvm_unreachable("src_scc register should not be used");
762
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000763 default:
764 break;
765 }
766
767 if (AMDGPU::SReg_32RegClass.contains(Reg)) {
768 assert(!AMDGPU::TTMP_32RegClass.contains(Reg) &&
769 "trap handler registers should not be used");
770 IsSGPR = true;
771 Width = 1;
772 } else if (AMDGPU::VGPR_32RegClass.contains(Reg)) {
773 IsSGPR = false;
774 Width = 1;
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000775 } else if (AMDGPU::AGPR_32RegClass.contains(Reg)) {
776 IsSGPR = false;
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000777 IsAGPR = true;
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000778 Width = 1;
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000779 } else if (AMDGPU::SReg_64RegClass.contains(Reg)) {
780 assert(!AMDGPU::TTMP_64RegClass.contains(Reg) &&
781 "trap handler registers should not be used");
782 IsSGPR = true;
783 Width = 2;
784 } else if (AMDGPU::VReg_64RegClass.contains(Reg)) {
785 IsSGPR = false;
786 Width = 2;
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000787 } else if (AMDGPU::AReg_64RegClass.contains(Reg)) {
788 IsSGPR = false;
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000789 IsAGPR = true;
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000790 Width = 2;
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000791 } else if (AMDGPU::VReg_96RegClass.contains(Reg)) {
792 IsSGPR = false;
793 Width = 3;
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000794 } else if (AMDGPU::SReg_96RegClass.contains(Reg)) {
Stanislav Mekhanoshind17bcf2b2019-11-06 12:39:38 -0800795 IsSGPR = true;
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000796 Width = 3;
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000797 } else if (AMDGPU::SReg_128RegClass.contains(Reg)) {
Dmitry Preobrazhensky27134952017-12-22 15:18:06 +0000798 assert(!AMDGPU::TTMP_128RegClass.contains(Reg) &&
799 "trap handler registers should not be used");
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000800 IsSGPR = true;
801 Width = 4;
802 } else if (AMDGPU::VReg_128RegClass.contains(Reg)) {
803 IsSGPR = false;
804 Width = 4;
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000805 } else if (AMDGPU::AReg_128RegClass.contains(Reg)) {
806 IsSGPR = false;
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000807 IsAGPR = true;
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000808 Width = 4;
Stanislav Mekhanoshind17bcf2b2019-11-06 12:39:38 -0800809 } else if (AMDGPU::VReg_160RegClass.contains(Reg)) {
810 IsSGPR = false;
811 Width = 5;
812 } else if (AMDGPU::SReg_160RegClass.contains(Reg)) {
813 IsSGPR = true;
814 Width = 5;
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000815 } else if (AMDGPU::SReg_256RegClass.contains(Reg)) {
Dmitry Preobrazhensky27134952017-12-22 15:18:06 +0000816 assert(!AMDGPU::TTMP_256RegClass.contains(Reg) &&
817 "trap handler registers should not be used");
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000818 IsSGPR = true;
819 Width = 8;
820 } else if (AMDGPU::VReg_256RegClass.contains(Reg)) {
821 IsSGPR = false;
822 Width = 8;
823 } else if (AMDGPU::SReg_512RegClass.contains(Reg)) {
Dmitry Preobrazhensky27134952017-12-22 15:18:06 +0000824 assert(!AMDGPU::TTMP_512RegClass.contains(Reg) &&
825 "trap handler registers should not be used");
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000826 IsSGPR = true;
827 Width = 16;
828 } else if (AMDGPU::VReg_512RegClass.contains(Reg)) {
829 IsSGPR = false;
830 Width = 16;
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000831 } else if (AMDGPU::AReg_512RegClass.contains(Reg)) {
832 IsSGPR = false;
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000833 IsAGPR = true;
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000834 Width = 16;
835 } else if (AMDGPU::SReg_1024RegClass.contains(Reg)) {
Matt Arsenault101abd22019-04-15 20:51:12 +0000836 IsSGPR = true;
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000837 Width = 32;
838 } else if (AMDGPU::VReg_1024RegClass.contains(Reg)) {
839 IsSGPR = false;
840 Width = 32;
841 } else if (AMDGPU::AReg_1024RegClass.contains(Reg)) {
842 IsSGPR = false;
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000843 IsAGPR = true;
Stanislav Mekhanoshin50d7f4642019-07-09 21:43:09 +0000844 Width = 32;
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000845 } else {
846 llvm_unreachable("Unknown register class");
847 }
848 unsigned HWReg = TRI.getHWRegIndex(Reg);
849 int MaxUsed = HWReg + Width - 1;
850 if (IsSGPR) {
851 MaxSGPR = MaxUsed > MaxSGPR ? MaxUsed : MaxSGPR;
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000852 } else if (IsAGPR) {
853 MaxAGPR = MaxUsed > MaxAGPR ? MaxUsed : MaxAGPR;
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000854 } else {
855 MaxVGPR = MaxUsed > MaxVGPR ? MaxUsed : MaxVGPR;
856 }
857 }
858
859 if (MI.isCall()) {
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000860 // Pseudo used just to encode the underlying global. Is there a better
861 // way to track this?
Matt Arsenault71bcbd42017-08-11 20:42:08 +0000862
863 const MachineOperand *CalleeOp
864 = TII->getNamedOperand(MI, AMDGPU::OpName::callee);
865 const Function *Callee = cast<Function>(CalleeOp->getGlobal());
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000866 if (Callee->isDeclaration()) {
867 // If this is a call to an external function, we can't do much. Make
868 // conservative guesses.
869
870 // 48 SGPRs - vcc, - flat_scr, -xnack
Scott Linder1e8c2c72018-06-21 19:38:56 +0000871 int MaxSGPRGuess =
Matt Arsenault4cd95092019-02-12 23:44:13 +0000872 47 - IsaInfo::getNumExtraSGPRs(&ST, true, ST.hasFlatAddressSpace());
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000873 MaxSGPR = std::max(MaxSGPR, MaxSGPRGuess);
874 MaxVGPR = std::max(MaxVGPR, 23);
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000875 MaxAGPR = std::max(MaxAGPR, 23);
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000876
Matt Arsenault9ba465a2017-11-14 20:33:14 +0000877 CalleeFrameSize = std::max(CalleeFrameSize, UINT64_C(16384));
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000878 Info.UsesVCC = true;
879 Info.UsesFlatScratch = ST.hasFlatAddressSpace();
880 Info.HasDynamicallySizedStack = true;
881 } else {
882 // We force CodeGen to run in SCC order, so the callee's register
883 // usage etc. should be the cumulative usage of all callees.
Matt Arsenaultaa03bcd2019-02-28 00:28:44 +0000884
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000885 auto I = CallGraphResourceInfo.find(Callee);
Matt Arsenaultaa03bcd2019-02-28 00:28:44 +0000886 if (I == CallGraphResourceInfo.end()) {
887 // Avoid crashing on undefined behavior with an illegal call to a
888 // kernel. If a callsite's calling convention doesn't match the
889 // function's, it's undefined behavior. If the callsite calling
890 // convention does match, that would have errored earlier.
891 // FIXME: The verifier shouldn't allow this.
892 if (AMDGPU::isEntryFunctionCC(Callee->getCallingConv()))
893 report_fatal_error("invalid call to entry function");
894
895 llvm_unreachable("callee should have been handled before caller");
896 }
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000897
898 MaxSGPR = std::max(I->second.NumExplicitSGPR - 1, MaxSGPR);
899 MaxVGPR = std::max(I->second.NumVGPR - 1, MaxVGPR);
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000900 MaxAGPR = std::max(I->second.NumAGPR - 1, MaxAGPR);
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000901 CalleeFrameSize
902 = std::max(I->second.PrivateSegmentSize, CalleeFrameSize);
903 Info.UsesVCC |= I->second.UsesVCC;
904 Info.UsesFlatScratch |= I->second.UsesFlatScratch;
905 Info.HasDynamicallySizedStack |= I->second.HasDynamicallySizedStack;
906 Info.HasRecursion |= I->second.HasRecursion;
907 }
908
909 if (!Callee->doesNotRecurse())
910 Info.HasRecursion = true;
911 }
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000912 }
913 }
914
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000915 Info.NumExplicitSGPR = MaxSGPR + 1;
916 Info.NumVGPR = MaxVGPR + 1;
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000917 Info.NumAGPR = MaxAGPR + 1;
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000918 Info.PrivateSegmentSize += CalleeFrameSize;
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000919
920 return Info;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000921}
922
923void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo,
924 const MachineFunction &MF) {
925 SIFunctionResourceInfo Info = analyzeResourceUsage(MF);
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000926 const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>();
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000927
Stanislav Mekhanoshin075bc482019-10-02 00:26:58 +0000928 ProgInfo.NumArchVGPR = Info.NumVGPR;
929 ProgInfo.NumAccVGPR = Info.NumAGPR;
930 ProgInfo.NumVGPR = Info.getTotalNumVGPRs(STM);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000931 ProgInfo.NumSGPR = Info.NumExplicitSGPR;
932 ProgInfo.ScratchSize = Info.PrivateSegmentSize;
933 ProgInfo.VCCUsed = Info.UsesVCC;
934 ProgInfo.FlatUsed = Info.UsesFlatScratch;
935 ProgInfo.DynamicCallStack = Info.HasDynamicallySizedStack || Info.HasRecursion;
936
Matt Arsenault9ba465a2017-11-14 20:33:14 +0000937 if (!isUInt<32>(ProgInfo.ScratchSize)) {
Matthias Braunf1caa282017-12-15 22:22:58 +0000938 DiagnosticInfoStackSize DiagStackSize(MF.getFunction(),
Matt Arsenault9ba465a2017-11-14 20:33:14 +0000939 ProgInfo.ScratchSize, DS_Error);
Matthias Braunf1caa282017-12-15 22:22:58 +0000940 MF.getFunction().getContext().diagnose(DiagStackSize);
Matt Arsenault9ba465a2017-11-14 20:33:14 +0000941 }
942
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000943 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000944
Scott Linder1e8c2c72018-06-21 19:38:56 +0000945 // TODO(scott.linder): The calculations related to SGPR/VGPR blocks are
946 // duplicated in part in AMDGPUAsmParser::calculateGPRBlocks, and could be
947 // unified.
948 unsigned ExtraSGPRs = IsaInfo::getNumExtraSGPRs(
Matt Arsenault4cd95092019-02-12 23:44:13 +0000949 &STM, ProgInfo.VCCUsed, ProgInfo.FlatUsed);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000950
Marek Olsak91f22fb2016-12-09 19:49:40 +0000951 // Check the addressable register limit before we add ExtraSGPRs.
952 if (STM.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS &&
953 !STM.hasSGPRInitBug()) {
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000954 unsigned MaxAddressableNumSGPRs = STM.getAddressableNumSGPRs();
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000955 if (ProgInfo.NumSGPR > MaxAddressableNumSGPRs) {
Marek Olsak91f22fb2016-12-09 19:49:40 +0000956 // This can happen due to a compiler bug or when using inline asm.
Matthias Braunf1caa282017-12-15 22:22:58 +0000957 LLVMContext &Ctx = MF.getFunction().getContext();
958 DiagnosticInfoResourceLimit Diag(MF.getFunction(),
Marek Olsak91f22fb2016-12-09 19:49:40 +0000959 "addressable scalar registers",
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000960 ProgInfo.NumSGPR, DS_Error,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000961 DK_ResourceLimit,
962 MaxAddressableNumSGPRs);
Marek Olsak91f22fb2016-12-09 19:49:40 +0000963 Ctx.diagnose(Diag);
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000964 ProgInfo.NumSGPR = MaxAddressableNumSGPRs - 1;
Marek Olsak91f22fb2016-12-09 19:49:40 +0000965 }
966 }
967
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000968 // Account for extra SGPRs and VGPRs reserved for debugger use.
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000969 ProgInfo.NumSGPR += ExtraSGPRs;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000970
Tim Renouffd8d4af2018-04-11 17:18:36 +0000971 // Ensure there are enough SGPRs and VGPRs for wave dispatch, where wave
972 // dispatch registers are function args.
973 unsigned WaveDispatchNumSGPR = 0, WaveDispatchNumVGPR = 0;
974 for (auto &Arg : MF.getFunction().args()) {
975 unsigned NumRegs = (Arg.getType()->getPrimitiveSizeInBits() + 31) / 32;
976 if (Arg.hasAttribute(Attribute::InReg))
977 WaveDispatchNumSGPR += NumRegs;
978 else
979 WaveDispatchNumVGPR += NumRegs;
980 }
981 ProgInfo.NumSGPR = std::max(ProgInfo.NumSGPR, WaveDispatchNumSGPR);
982 ProgInfo.NumVGPR = std::max(ProgInfo.NumVGPR, WaveDispatchNumVGPR);
983
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000984 // Adjust number of registers used to meet default/requested minimum/maximum
985 // number of waves per execution unit request.
986 ProgInfo.NumSGPRsForWavesPerEU = std::max(
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000987 std::max(ProgInfo.NumSGPR, 1u), STM.getMinNumSGPRs(MFI->getMaxWavesPerEU()));
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000988 ProgInfo.NumVGPRsForWavesPerEU = std::max(
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000989 std::max(ProgInfo.NumVGPR, 1u), STM.getMinNumVGPRs(MFI->getMaxWavesPerEU()));
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000990
Marek Olsak91f22fb2016-12-09 19:49:40 +0000991 if (STM.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ||
992 STM.hasSGPRInitBug()) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000993 unsigned MaxAddressableNumSGPRs = STM.getAddressableNumSGPRs();
994 if (ProgInfo.NumSGPR > MaxAddressableNumSGPRs) {
995 // This can happen due to a compiler bug or when using inline asm to use
996 // the registers which are usually reserved for vcc etc.
Matthias Braunf1caa282017-12-15 22:22:58 +0000997 LLVMContext &Ctx = MF.getFunction().getContext();
998 DiagnosticInfoResourceLimit Diag(MF.getFunction(),
Marek Olsak91f22fb2016-12-09 19:49:40 +0000999 "scalar registers",
1000 ProgInfo.NumSGPR, DS_Error,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +00001001 DK_ResourceLimit,
1002 MaxAddressableNumSGPRs);
Marek Olsak91f22fb2016-12-09 19:49:40 +00001003 Ctx.diagnose(Diag);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +00001004 ProgInfo.NumSGPR = MaxAddressableNumSGPRs;
1005 ProgInfo.NumSGPRsForWavesPerEU = MaxAddressableNumSGPRs;
Marek Olsak91f22fb2016-12-09 19:49:40 +00001006 }
Matt Arsenault4eae3012016-10-28 20:31:47 +00001007 }
1008
1009 if (STM.hasSGPRInitBug()) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +00001010 ProgInfo.NumSGPR =
1011 AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG;
1012 ProgInfo.NumSGPRsForWavesPerEU =
1013 AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001014 }
1015
Matt Arsenault161e2b42017-04-18 20:59:40 +00001016 if (MFI->getNumUserSGPRs() > STM.getMaxNumUserSGPRs()) {
Matthias Braunf1caa282017-12-15 22:22:58 +00001017 LLVMContext &Ctx = MF.getFunction().getContext();
1018 DiagnosticInfoResourceLimit Diag(MF.getFunction(), "user SGPRs",
Matt Arsenault161e2b42017-04-18 20:59:40 +00001019 MFI->getNumUserSGPRs(), DS_Error);
Matt Arsenaultff982412016-06-20 18:13:04 +00001020 Ctx.diagnose(Diag);
Matt Arsenault41003af2015-11-30 21:16:07 +00001021 }
1022
Matt Arsenault52ef4012016-07-26 16:45:58 +00001023 if (MFI->getLDSSize() > static_cast<unsigned>(STM.getLocalMemorySize())) {
Matthias Braunf1caa282017-12-15 22:22:58 +00001024 LLVMContext &Ctx = MF.getFunction().getContext();
1025 DiagnosticInfoResourceLimit Diag(MF.getFunction(), "local memory",
Matt Arsenault52ef4012016-07-26 16:45:58 +00001026 MFI->getLDSSize(), DS_Error);
Matt Arsenaultff982412016-06-20 18:13:04 +00001027 Ctx.diagnose(Diag);
Matt Arsenault1c4d0ef2016-04-28 19:37:35 +00001028 }
1029
Scott Linder1e8c2c72018-06-21 19:38:56 +00001030 ProgInfo.SGPRBlocks = IsaInfo::getNumSGPRBlocks(
David Stuttardbe3d7ba2018-11-19 15:44:20 +00001031 &STM, ProgInfo.NumSGPRsForWavesPerEU);
Scott Linder1e8c2c72018-06-21 19:38:56 +00001032 ProgInfo.VGPRBlocks = IsaInfo::getNumVGPRBlocks(
David Stuttardbe3d7ba2018-11-19 15:44:20 +00001033 &STM, ProgInfo.NumVGPRsForWavesPerEU);
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +00001034
Matt Arsenaultdb0ed3e2019-10-31 18:50:30 -07001035 const SIModeRegisterDefaults Mode = MFI->getMode();
1036
Tom Stellard45bb48e2015-06-13 03:28:10 +00001037 // Set the value to initialize FP_ROUND and FP_DENORM parts of the mode
1038 // register.
Matt Arsenaultdb0ed3e2019-10-31 18:50:30 -07001039 ProgInfo.FloatMode = getFPMode(Mode);
Tom Stellard45bb48e2015-06-13 03:28:10 +00001040
Matt Arsenault055e4dc2019-03-29 19:14:54 +00001041 ProgInfo.IEEEMode = Mode.IEEE;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001042
Matt Arsenault7293f982016-01-28 20:53:35 +00001043 // Make clamp modifier on NaN input returns 0.
Matt Arsenault055e4dc2019-03-29 19:14:54 +00001044 ProgInfo.DX10Clamp = Mode.DX10Clamp;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001045
Tom Stellard45bb48e2015-06-13 03:28:10 +00001046 unsigned LDSAlignShift;
Tom Stellard5bfbae52018-07-11 20:59:01 +00001047 if (STM.getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) {
Tom Stellard45bb48e2015-06-13 03:28:10 +00001048 // LDS is allocated in 64 dword blocks.
1049 LDSAlignShift = 8;
1050 } else {
1051 // LDS is allocated in 128 dword blocks.
1052 LDSAlignShift = 9;
1053 }
1054
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +00001055 unsigned LDSSpillSize =
Matt Arsenault161e2b42017-04-18 20:59:40 +00001056 MFI->getLDSWaveSpillSize() * MFI->getMaxFlatWorkGroupSize();
Tom Stellard45bb48e2015-06-13 03:28:10 +00001057
Matt Arsenault52ef4012016-07-26 16:45:58 +00001058 ProgInfo.LDSSize = MFI->getLDSSize() + LDSSpillSize;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001059 ProgInfo.LDSBlocks =
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +00001060 alignTo(ProgInfo.LDSSize, 1ULL << LDSAlignShift) >> LDSAlignShift;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001061
1062 // Scratch is allocated in 256 dword blocks.
1063 unsigned ScratchAlignShift = 10;
1064 // We need to program the hardware with the amount of scratch memory that
1065 // is used by the entire wave. ProgInfo.ScratchSize is the amount of
1066 // scratch memory used per thread.
1067 ProgInfo.ScratchBlocks =
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001068 alignTo(ProgInfo.ScratchSize * STM.getWavefrontSize(),
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +00001069 1ULL << ScratchAlignShift) >>
Rui Ueyamada00f2f2016-01-14 21:06:47 +00001070 ScratchAlignShift;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001071
Stanislav Mekhanoshin41bbe102019-05-03 21:26:39 +00001072 if (getIsaVersion(getGlobalSTI()->getCPU()).Major >= 10) {
1073 ProgInfo.WgpMode = STM.isCuModeEnabled() ? 0 : 1;
1074 ProgInfo.MemOrdered = 1;
1075 }
1076
Tom Stellard45bb48e2015-06-13 03:28:10 +00001077 ProgInfo.ComputePGMRSrc1 =
1078 S_00B848_VGPRS(ProgInfo.VGPRBlocks) |
1079 S_00B848_SGPRS(ProgInfo.SGPRBlocks) |
1080 S_00B848_PRIORITY(ProgInfo.Priority) |
1081 S_00B848_FLOAT_MODE(ProgInfo.FloatMode) |
1082 S_00B848_PRIV(ProgInfo.Priv) |
1083 S_00B848_DX10_CLAMP(ProgInfo.DX10Clamp) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001084 S_00B848_DEBUG_MODE(ProgInfo.DebugMode) |
Stanislav Mekhanoshin41bbe102019-05-03 21:26:39 +00001085 S_00B848_IEEE_MODE(ProgInfo.IEEEMode) |
1086 S_00B848_WGP_MODE(ProgInfo.WgpMode) |
1087 S_00B848_MEM_ORDERED(ProgInfo.MemOrdered);
Tom Stellard45bb48e2015-06-13 03:28:10 +00001088
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001089 // 0 = X, 1 = XY, 2 = XYZ
1090 unsigned TIDIGCompCnt = 0;
1091 if (MFI->hasWorkItemIDZ())
1092 TIDIGCompCnt = 2;
1093 else if (MFI->hasWorkItemIDY())
1094 TIDIGCompCnt = 1;
1095
Tom Stellard45bb48e2015-06-13 03:28:10 +00001096 ProgInfo.ComputePGMRSrc2 =
1097 S_00B84C_SCRATCH_EN(ProgInfo.ScratchBlocks > 0) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001098 S_00B84C_USER_SGPR(MFI->getNumUserSGPRs()) |
Konstantin Zhuravlyov2ca6b1f2018-05-29 19:09:13 +00001099 // For AMDHSA, TRAP_HANDLER must be zero, as it is populated by the CP.
1100 S_00B84C_TRAP_HANDLER(STM.isAmdHsaOS() ? 0 : STM.isTrapHandlerEnabled()) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001101 S_00B84C_TGID_X_EN(MFI->hasWorkGroupIDX()) |
1102 S_00B84C_TGID_Y_EN(MFI->hasWorkGroupIDY()) |
1103 S_00B84C_TGID_Z_EN(MFI->hasWorkGroupIDZ()) |
1104 S_00B84C_TG_SIZE_EN(MFI->hasWorkGroupInfo()) |
1105 S_00B84C_TIDIG_COMP_CNT(TIDIGCompCnt) |
1106 S_00B84C_EXCP_EN_MSB(0) |
Konstantin Zhuravlyov6ccb0762017-05-05 20:13:55 +00001107 // For AMDHSA, LDS_SIZE must be zero, as it is populated by the CP.
1108 S_00B84C_LDS_SIZE(STM.isAmdHsaOS() ? 0 : ProgInfo.LDSBlocks) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001109 S_00B84C_EXCP_EN(0);
Stanislav Mekhanoshin2594fa82019-07-31 01:07:10 +00001110
1111 ProgInfo.Occupancy = STM.computeOccupancy(MF, ProgInfo.LDSSize,
1112 ProgInfo.NumSGPRsForWavesPerEU,
1113 ProgInfo.NumVGPRsForWavesPerEU);
Tom Stellard45bb48e2015-06-13 03:28:10 +00001114}
1115
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001116static unsigned getRsrcReg(CallingConv::ID CallConv) {
1117 switch (CallConv) {
Justin Bognercd1d5aa2016-08-17 20:30:52 +00001118 default: LLVM_FALLTHROUGH;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001119 case CallingConv::AMDGPU_CS: return R_00B848_COMPUTE_PGM_RSRC1;
Tim Renoufef1ae8f2017-09-29 09:51:22 +00001120 case CallingConv::AMDGPU_LS: return R_00B528_SPI_SHADER_PGM_RSRC1_LS;
Marek Olsaka302a7362017-05-02 15:41:10 +00001121 case CallingConv::AMDGPU_HS: return R_00B428_SPI_SHADER_PGM_RSRC1_HS;
Tim Renoufef1ae8f2017-09-29 09:51:22 +00001122 case CallingConv::AMDGPU_ES: return R_00B328_SPI_SHADER_PGM_RSRC1_ES;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001123 case CallingConv::AMDGPU_GS: return R_00B228_SPI_SHADER_PGM_RSRC1_GS;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +00001124 case CallingConv::AMDGPU_VS: return R_00B128_SPI_SHADER_PGM_RSRC1_VS;
Tim Renoufef1ae8f2017-09-29 09:51:22 +00001125 case CallingConv::AMDGPU_PS: return R_00B028_SPI_SHADER_PGM_RSRC1_PS;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001126 }
1127}
1128
1129void AMDGPUAsmPrinter::EmitProgramInfoSI(const MachineFunction &MF,
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001130 const SIProgramInfo &CurrentProgramInfo) {
Tom Stellard45bb48e2015-06-13 03:28:10 +00001131 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Matthias Braunf1caa282017-12-15 22:22:58 +00001132 unsigned RsrcReg = getRsrcReg(MF.getFunction().getCallingConv());
Tom Stellard45bb48e2015-06-13 03:28:10 +00001133
Matthias Braunf1caa282017-12-15 22:22:58 +00001134 if (AMDGPU::isCompute(MF.getFunction().getCallingConv())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +00001135 OutStreamer->EmitIntValue(R_00B848_COMPUTE_PGM_RSRC1, 4);
1136
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001137 OutStreamer->EmitIntValue(CurrentProgramInfo.ComputePGMRSrc1, 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +00001138
1139 OutStreamer->EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001140 OutStreamer->EmitIntValue(CurrentProgramInfo.ComputePGMRSrc2, 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +00001141
1142 OutStreamer->EmitIntValue(R_00B860_COMPUTE_TMPRING_SIZE, 4);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001143 OutStreamer->EmitIntValue(S_00B860_WAVESIZE(CurrentProgramInfo.ScratchBlocks), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +00001144
1145 // TODO: Should probably note flat usage somewhere. SC emits a "FlatPtr32 =
1146 // 0" comment but I don't see a corresponding field in the register spec.
1147 } else {
1148 OutStreamer->EmitIntValue(RsrcReg, 4);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001149 OutStreamer->EmitIntValue(S_00B028_VGPRS(CurrentProgramInfo.VGPRBlocks) |
1150 S_00B028_SGPRS(CurrentProgramInfo.SGPRBlocks), 4);
Scott Linderc6c62722018-10-31 18:54:06 +00001151 OutStreamer->EmitIntValue(R_0286E8_SPI_TMPRING_SIZE, 4);
1152 OutStreamer->EmitIntValue(
1153 S_0286E8_WAVESIZE(CurrentProgramInfo.ScratchBlocks), 4);
Tim Renouf807ecc32018-02-06 13:39:38 +00001154 }
1155
1156 if (MF.getFunction().getCallingConv() == CallingConv::AMDGPU_PS) {
1157 OutStreamer->EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4);
1158 OutStreamer->EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(CurrentProgramInfo.LDSBlocks), 4);
1159 OutStreamer->EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4);
1160 OutStreamer->EmitIntValue(MFI->getPSInputEnable(), 4);
1161 OutStreamer->EmitIntValue(R_0286D0_SPI_PS_INPUT_ADDR, 4);
1162 OutStreamer->EmitIntValue(MFI->getPSInputAddr(), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +00001163 }
Marek Olsak0532c192016-07-13 17:35:15 +00001164
1165 OutStreamer->EmitIntValue(R_SPILLED_SGPRS, 4);
1166 OutStreamer->EmitIntValue(MFI->getNumSpilledSGPRs(), 4);
1167 OutStreamer->EmitIntValue(R_SPILLED_VGPRS, 4);
1168 OutStreamer->EmitIntValue(MFI->getNumSpilledVGPRs(), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +00001169}
1170
Tim Renouf72800f02017-10-03 19:03:52 +00001171// This is the equivalent of EmitProgramInfoSI above, but for when the OS type
1172// is AMDPAL. It stores each compute/SPI register setting and other PAL
Tim Renoufd737b552019-03-20 17:42:00 +00001173// metadata items into the PALMD::Metadata, combining with any provided by the
1174// frontend as LLVM metadata. Once all functions are written, the PAL metadata
1175// is then written as a single block in the .note section.
Konstantin Zhuravlyovc3beb6a2017-10-11 22:41:09 +00001176void AMDGPUAsmPrinter::EmitPALMetadata(const MachineFunction &MF,
Tim Renouf72800f02017-10-03 19:03:52 +00001177 const SIProgramInfo &CurrentProgramInfo) {
1178 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tim Renoufd737b552019-03-20 17:42:00 +00001179 auto CC = MF.getFunction().getCallingConv();
1180 auto MD = getTargetStreamer()->getPALMetadata();
1181
Tim Renoufe7bd52f2019-03-20 18:47:21 +00001182 MD->setEntryPoint(CC, MF.getFunction().getName());
Tim Renoufd737b552019-03-20 17:42:00 +00001183 MD->setNumUsedVgprs(CC, CurrentProgramInfo.NumVGPRsForWavesPerEU);
1184 MD->setNumUsedSgprs(CC, CurrentProgramInfo.NumSGPRsForWavesPerEU);
Matthias Braunf1caa282017-12-15 22:22:58 +00001185 if (AMDGPU::isCompute(MF.getFunction().getCallingConv())) {
Tim Renoufd737b552019-03-20 17:42:00 +00001186 MD->setRsrc1(CC, CurrentProgramInfo.ComputePGMRSrc1);
1187 MD->setRsrc2(CC, CurrentProgramInfo.ComputePGMRSrc2);
Tim Renouf72800f02017-10-03 19:03:52 +00001188 } else {
Tim Renoufd737b552019-03-20 17:42:00 +00001189 MD->setRsrc1(CC, S_00B028_VGPRS(CurrentProgramInfo.VGPRBlocks) |
1190 S_00B028_SGPRS(CurrentProgramInfo.SGPRBlocks));
Tim Renouf72800f02017-10-03 19:03:52 +00001191 if (CurrentProgramInfo.ScratchBlocks > 0)
Tim Renoufd737b552019-03-20 17:42:00 +00001192 MD->setRsrc2(CC, S_00B84C_SCRATCH_EN(1));
Tim Renouf72800f02017-10-03 19:03:52 +00001193 }
Tim Renoufd737b552019-03-20 17:42:00 +00001194 // ScratchSize is in bytes, 16 aligned.
1195 MD->setScratchSize(CC, alignTo(CurrentProgramInfo.ScratchSize, 16));
Matthias Braunf1caa282017-12-15 22:22:58 +00001196 if (MF.getFunction().getCallingConv() == CallingConv::AMDGPU_PS) {
Tim Renoufd737b552019-03-20 17:42:00 +00001197 MD->setRsrc2(CC, S_00B02C_EXTRA_LDS_SIZE(CurrentProgramInfo.LDSBlocks));
1198 MD->setSpiPsInputEna(MFI->getPSInputEnable());
1199 MD->setSpiPsInputAddr(MFI->getPSInputAddr());
Tim Renouf72800f02017-10-03 19:03:52 +00001200 }
Stanislav Mekhanoshin5d00c302019-06-17 16:48:56 +00001201
1202 const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>();
1203 if (STM.isWave32())
1204 MD->setWave32(MF.getFunction().getCallingConv());
Tim Renouf72800f02017-10-03 19:03:52 +00001205}
1206
Matt Arsenault24ee0782016-02-12 02:40:47 +00001207// This is supposed to be log2(Size)
1208static amd_element_byte_size_t getElementByteSizeValue(unsigned Size) {
1209 switch (Size) {
1210 case 4:
1211 return AMD_ELEMENT_4_BYTES;
1212 case 8:
1213 return AMD_ELEMENT_8_BYTES;
1214 case 16:
1215 return AMD_ELEMENT_16_BYTES;
1216 default:
1217 llvm_unreachable("invalid private_element_size");
1218 }
1219}
1220
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001221void AMDGPUAsmPrinter::getAmdKernelCode(amd_kernel_code_t &Out,
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001222 const SIProgramInfo &CurrentProgramInfo,
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001223 const MachineFunction &MF) const {
Matt Arsenault4bec7d42018-07-20 09:05:08 +00001224 const Function &F = MF.getFunction();
1225 assert(F.getCallingConv() == CallingConv::AMDGPU_KERNEL ||
1226 F.getCallingConv() == CallingConv::SPIR_KERNEL);
1227
Tom Stellard45bb48e2015-06-13 03:28:10 +00001228 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Tom Stellard5bfbae52018-07-11 20:59:01 +00001229 const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +00001230
Matt Arsenault4cd95092019-02-12 23:44:13 +00001231 AMDGPU::initDefaultAMDKernelCodeT(Out, &STM);
Tom Stellard45bb48e2015-06-13 03:28:10 +00001232
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001233 Out.compute_pgm_resource_registers =
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001234 CurrentProgramInfo.ComputePGMRSrc1 |
1235 (CurrentProgramInfo.ComputePGMRSrc2 << 32);
Stanislav Mekhanoshin41bbe102019-05-03 21:26:39 +00001236 Out.code_properties |= AMD_CODE_PROPERTY_IS_PTR64;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001237
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001238 if (CurrentProgramInfo.DynamicCallStack)
1239 Out.code_properties |= AMD_CODE_PROPERTY_IS_DYNAMIC_CALLSTACK;
1240
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001241 AMD_HSA_BITS_SET(Out.code_properties,
Matt Arsenault24ee0782016-02-12 02:40:47 +00001242 AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE,
1243 getElementByteSizeValue(STM.getMaxPrivateElementSize()));
1244
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001245 if (MFI->hasPrivateSegmentBuffer()) {
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001246 Out.code_properties |=
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001247 AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER;
1248 }
1249
1250 if (MFI->hasDispatchPtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001251 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001252
1253 if (MFI->hasQueuePtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001254 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001255
1256 if (MFI->hasKernargSegmentPtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001257 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001258
1259 if (MFI->hasDispatchID())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001260 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001261
1262 if (MFI->hasFlatScratchInit())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001263 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001264
Tom Stellard48f29f22015-11-26 00:43:29 +00001265 if (MFI->hasDispatchPtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001266 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
Tom Stellard48f29f22015-11-26 00:43:29 +00001267
Nicolai Haehnle5b504972016-01-04 23:35:53 +00001268 if (STM.isXNACKEnabled())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001269 Out.code_properties |= AMD_CODE_PROPERTY_IS_XNACK_SUPPORTED;
Nicolai Haehnle5b504972016-01-04 23:35:53 +00001270
Guillaume Chateletb65fa482019-10-15 12:56:24 +00001271 Align MaxKernArgAlign;
Matt Arsenault4bec7d42018-07-20 09:05:08 +00001272 Out.kernarg_segment_byte_size = STM.getKernArgSegmentSize(F, MaxKernArgAlign);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001273 Out.wavefront_sgpr_count = CurrentProgramInfo.NumSGPR;
1274 Out.workitem_vgpr_count = CurrentProgramInfo.NumVGPR;
1275 Out.workitem_private_segment_byte_size = CurrentProgramInfo.ScratchSize;
1276 Out.workgroup_group_segment_byte_size = CurrentProgramInfo.LDSSize;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001277
Guillaume Chateletb65fa482019-10-15 12:56:24 +00001278 // kernarg_segment_alignment is specified as log of the alignment.
1279 // The minimum alignment is 16.
1280 Out.kernarg_segment_alignment = Log2(std::max(Align(16), MaxKernArgAlign));
Tom Stellard45bb48e2015-06-13 03:28:10 +00001281}
1282
1283bool AMDGPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
Tom Stellard45bb48e2015-06-13 03:28:10 +00001284 const char *ExtraCode, raw_ostream &O) {
Matt Arsenault36cd1852017-08-09 20:09:35 +00001285 // First try the generic code, which knows about modifiers like 'c' and 'n'.
Nick Desaulniers5277b3f2019-04-10 16:38:43 +00001286 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, ExtraCode, O))
Matt Arsenault36cd1852017-08-09 20:09:35 +00001287 return false;
1288
Tom Stellard45bb48e2015-06-13 03:28:10 +00001289 if (ExtraCode && ExtraCode[0]) {
1290 if (ExtraCode[1] != 0)
1291 return true; // Unknown modifier.
1292
1293 switch (ExtraCode[0]) {
Tom Stellard45bb48e2015-06-13 03:28:10 +00001294 case 'r':
1295 break;
Matt Arsenault36cd1852017-08-09 20:09:35 +00001296 default:
1297 return true;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001298 }
1299 }
1300
Matt Arsenault36cd1852017-08-09 20:09:35 +00001301 // TODO: Should be able to support other operand types like globals.
1302 const MachineOperand &MO = MI->getOperand(OpNo);
1303 if (MO.isReg()) {
1304 AMDGPUInstPrinter::printRegOperand(MO.getReg(), O,
1305 *MF->getSubtarget().getRegisterInfo());
1306 return false;
1307 }
1308
1309 return true;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001310}