blob: a5c3745fcdf771c7d31e4100fc31f9390bb09002 [file] [log] [blame]
Tom Stellard45bb48e2015-06-13 03:28:10 +00001//===-- AMDGPUAsmPrinter.cpp - AMDGPU Assebly printer --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10/// \file
11///
12/// The AMDGPUAsmPrinter is used to print both assembly string and also binary
13/// code. When passed an MCAsmStreamer it prints assembly and when passed
14/// an MCObjectStreamer it outputs binary code.
15//
16//===----------------------------------------------------------------------===//
17//
18
19#include "AMDGPUAsmPrinter.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000020#include "AMDGPU.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000021#include "AMDGPUSubtarget.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000022#include "AMDGPUTargetMachine.h"
23#include "InstPrinter/AMDGPUInstPrinter.h"
24#include "MCTargetDesc/AMDGPUTargetStreamer.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000025#include "R600Defines.h"
26#include "R600MachineFunctionInfo.h"
27#include "R600RegisterInfo.h"
28#include "SIDefines.h"
Matt Arsenaulta9720c62016-06-20 17:51:32 +000029#include "SIInstrInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000030#include "SIMachineFunctionInfo.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000031#include "SIRegisterInfo.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000032#include "Utils/AMDGPUBaseInfo.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000033#include "llvm/BinaryFormat/ELF.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000034#include "llvm/CodeGen/MachineFrameInfo.h"
Matt Arsenaultff982412016-06-20 18:13:04 +000035#include "llvm/IR/DiagnosticInfo.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000036#include "llvm/MC/MCContext.h"
37#include "llvm/MC/MCSectionELF.h"
38#include "llvm/MC/MCStreamer.h"
Tom Stellard45bb48e2015-06-13 03:28:10 +000039#include "llvm/Support/MathExtras.h"
40#include "llvm/Support/TargetRegistry.h"
41#include "llvm/Target/TargetLoweringObjectFile.h"
42
43using namespace llvm;
44
45// TODO: This should get the default rounding mode from the kernel. We just set
46// the default here, but this could change if the OpenCL rounding mode pragmas
47// are used.
48//
49// The denormal mode here should match what is reported by the OpenCL runtime
50// for the CL_FP_DENORM bit from CL_DEVICE_{HALF|SINGLE|DOUBLE}_FP_CONFIG, but
51// can also be override to flush with the -cl-denorms-are-zero compiler flag.
52//
53// AMD OpenCL only sets flush none and reports CL_FP_DENORM for double
54// precision, and leaves single precision to flush all and does not report
55// CL_FP_DENORM for CL_DEVICE_SINGLE_FP_CONFIG. Mesa's OpenCL currently reports
56// CL_FP_DENORM for both.
57//
58// FIXME: It seems some instructions do not support single precision denormals
59// regardless of the mode (exp_*_f32, rcp_*_f32, rsq_*_f32, rsq_*f32, sqrt_f32,
60// and sin_f32, cos_f32 on most parts).
61
62// We want to use these instructions, and using fp32 denormals also causes
63// instructions to run at the double precision rate for the device so it's
64// probably best to just report no single precision denormals.
65static uint32_t getFPMode(const MachineFunction &F) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +000066 const SISubtarget& ST = F.getSubtarget<SISubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +000067 // TODO: Is there any real use for the flush in only / flush out only modes?
68
69 uint32_t FP32Denormals =
70 ST.hasFP32Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
71
72 uint32_t FP64Denormals =
73 ST.hasFP64Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
74
75 return FP_ROUND_MODE_SP(FP_ROUND_ROUND_TO_NEAREST) |
76 FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_NEAREST) |
77 FP_DENORM_MODE_SP(FP32Denormals) |
78 FP_DENORM_MODE_DP(FP64Denormals);
79}
80
81static AsmPrinter *
82createAMDGPUAsmPrinterPass(TargetMachine &tm,
83 std::unique_ptr<MCStreamer> &&Streamer) {
84 return new AMDGPUAsmPrinter(tm, std::move(Streamer));
85}
86
87extern "C" void LLVMInitializeAMDGPUAsmPrinter() {
Mehdi Aminif42454b2016-10-09 23:00:34 +000088 TargetRegistry::RegisterAsmPrinter(getTheAMDGPUTarget(),
89 createAMDGPUAsmPrinterPass);
90 TargetRegistry::RegisterAsmPrinter(getTheGCNTarget(),
91 createAMDGPUAsmPrinterPass);
Tom Stellard45bb48e2015-06-13 03:28:10 +000092}
93
94AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM,
95 std::unique_ptr<MCStreamer> Streamer)
Yaxun Liu1a14bfa2017-03-27 14:04:01 +000096 : AsmPrinter(TM, std::move(Streamer)) {
97 AMDGPUASI = static_cast<AMDGPUTargetMachine*>(&TM)->getAMDGPUAS();
98 }
Tom Stellard45bb48e2015-06-13 03:28:10 +000099
Mehdi Amini117296c2016-10-01 02:56:57 +0000100StringRef AMDGPUAsmPrinter::getPassName() const {
Matt Arsenaultf9245b72016-07-22 17:01:25 +0000101 return "AMDGPU Assembly Printer";
102}
103
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000104const MCSubtargetInfo* AMDGPUAsmPrinter::getSTI() const {
105 return TM.getMCSubtargetInfo();
106}
107
108AMDGPUTargetStreamer& AMDGPUAsmPrinter::getTargetStreamer() const {
109 return static_cast<AMDGPUTargetStreamer&>(*OutStreamer->getTargetStreamer());
110}
111
Tom Stellardf4218372016-01-12 17:18:17 +0000112void AMDGPUAsmPrinter::EmitStartOfAsmFile(Module &M) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000113 AMDGPU::IsaInfo::IsaVersion ISA =
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000114 AMDGPU::IsaInfo::getIsaVersion(getSTI()->getFeatureBits());
Yaxun Liud6fbe652016-11-10 21:18:49 +0000115
Tim Renouf72800f02017-10-03 19:03:52 +0000116 if (TM.getTargetTriple().getOS() == Triple::AMDPAL) {
117 readPalMetadata(M);
118 // AMDPAL wants an HSA_ISA .note.
119 getTargetStreamer().EmitDirectiveHSACodeObjectISA(
120 ISA.Major, ISA.Minor, ISA.Stepping, "AMD", "AMDGPU");
121 }
122 if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
123 return;
124
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000125 getTargetStreamer().EmitDirectiveHSACodeObjectVersion(2, 1);
126 getTargetStreamer().EmitDirectiveHSACodeObjectISA(
127 ISA.Major, ISA.Minor, ISA.Stepping, "AMD", "AMDGPU");
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +0000128 getTargetStreamer().EmitStartOfHSAMetadata(M);
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000129}
130
131void AMDGPUAsmPrinter::EmitEndOfAsmFile(Module &M) {
Tim Renouf72800f02017-10-03 19:03:52 +0000132 if (TM.getTargetTriple().getOS() == Triple::AMDPAL) {
133 // Copy the PAL metadata from the map where we collected it into a vector,
134 // then write it as a .note.
135 std::vector<uint32_t> Data;
136 for (auto i : PalMetadata) {
137 Data.push_back(i.first);
138 Data.push_back(i.second);
139 }
140 getTargetStreamer().EmitPalMetadata(Data);
141 }
142
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000143 if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
144 return;
145
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +0000146 getTargetStreamer().EmitEndOfHSAMetadata();
Tom Stellardf4218372016-01-12 17:18:17 +0000147}
148
Matt Arsenault6bc43d82016-10-06 16:20:41 +0000149bool AMDGPUAsmPrinter::isBlockOnlyReachableByFallthrough(
150 const MachineBasicBlock *MBB) const {
151 if (!AsmPrinter::isBlockOnlyReachableByFallthrough(MBB))
152 return false;
153
154 if (MBB->empty())
155 return true;
156
157 // If this is a block implementing a long branch, an expression relative to
158 // the start of the block is needed. to the start of the block.
159 // XXX - Is there a smarter way to check this?
160 return (MBB->back().getOpcode() != AMDGPU::S_SETPC_B64);
161}
162
Tom Stellardf151a452015-06-26 21:14:58 +0000163void AMDGPUAsmPrinter::EmitFunctionBodyStart() {
Matt Arsenault021a2182017-04-19 19:38:10 +0000164 const AMDGPUMachineFunction *MFI = MF->getInfo<AMDGPUMachineFunction>();
165 if (!MFI->isEntryFunction())
166 return;
167
Tom Stellardf151a452015-06-26 21:14:58 +0000168 const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +0000169 amd_kernel_code_t KernelCode;
Tom Stellard2f3f9852017-01-25 01:25:13 +0000170 if (STM.isAmdCodeObjectV2(*MF)) {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000171 getAmdKernelCode(KernelCode, CurrentProgramInfo, *MF);
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +0000172
173 OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
174 getTargetStreamer().EmitAMDKernelCodeT(KernelCode);
Tom Stellardf151a452015-06-26 21:14:58 +0000175 }
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000176
177 if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
178 return;
Konstantin Zhuravlyova63b0f92017-10-11 22:18:53 +0000179 getTargetStreamer().EmitKernelHSAMetadata(*MF->getFunction(), KernelCode);
Tom Stellardf151a452015-06-26 21:14:58 +0000180}
181
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000182void AMDGPUAsmPrinter::EmitFunctionEntryLabel() {
183 const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
184 const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
Matt Arsenault1074cb52017-03-30 23:58:04 +0000185 if (MFI->isEntryFunction() && STM.isAmdCodeObjectV2(*MF)) {
Tom Stellard1b9748c2016-09-26 17:29:25 +0000186 SmallString<128> SymbolName;
187 getNameWithPrefix(SymbolName, MF->getFunction()),
Konstantin Zhuravlyov7498cd62017-03-22 22:32:22 +0000188 getTargetStreamer().EmitAMDGPUSymbolType(
189 SymbolName, ELF::STT_AMDGPU_HSA_KERNEL);
Tom Stellard1e1b05d2015-11-06 11:45:14 +0000190 }
191
192 AsmPrinter::EmitFunctionEntryLabel();
193}
194
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000195void AMDGPUAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
196
Tom Stellard00f2f912015-12-02 19:47:57 +0000197 // Group segment variables aren't emitted in HSA.
Yaxun Liu1a14bfa2017-03-27 14:04:01 +0000198 if (AMDGPU::isGroupSegment(GV, AMDGPUASI))
Tom Stellard00f2f912015-12-02 19:47:57 +0000199 return;
200
Tom Stellardfcfaea42016-05-05 17:03:33 +0000201 AsmPrinter::EmitGlobalVariable(GV);
Tom Stellarde3b5aea2015-12-02 17:00:42 +0000202}
203
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000204bool AMDGPUAsmPrinter::doFinalization(Module &M) {
205 CallGraphResourceInfo.clear();
206 return AsmPrinter::doFinalization(M);
207}
208
Tim Renouf72800f02017-10-03 19:03:52 +0000209// For the amdpal OS type, read the amdgpu.pal.metadata supplied by the
210// frontend into our PalMetadata map, ready for per-function modification. It
211// is a NamedMD containing an MDTuple containing a number of MDNodes each of
212// which is an integer value, and each two integer values forms a key=value
213// pair that we store as PalMetadata[key]=value in the map.
214void AMDGPUAsmPrinter::readPalMetadata(Module &M) {
215 auto NamedMD = M.getNamedMetadata("amdgpu.pal.metadata");
216 if (!NamedMD || !NamedMD->getNumOperands())
217 return;
218 auto Tuple = dyn_cast<MDTuple>(NamedMD->getOperand(0));
219 if (!Tuple)
220 return;
221 for (unsigned I = 0, E = Tuple->getNumOperands() & -2; I != E; I += 2) {
222 auto Key = mdconst::dyn_extract<ConstantInt>(Tuple->getOperand(I));
223 auto Val = mdconst::dyn_extract<ConstantInt>(Tuple->getOperand(I + 1));
224 if (!Key || !Val)
225 continue;
226 PalMetadata[Key->getZExtValue()] = Val->getZExtValue();
227 }
228}
229
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000230// Print comments that apply to both callable functions and entry points.
231void AMDGPUAsmPrinter::emitCommonFunctionComments(
232 uint32_t NumVGPR,
233 uint32_t NumSGPR,
234 uint32_t ScratchSize,
235 uint64_t CodeSize) {
236 OutStreamer->emitRawComment(" codeLenInByte = " + Twine(CodeSize), false);
237 OutStreamer->emitRawComment(" NumSgprs: " + Twine(NumSGPR), false);
238 OutStreamer->emitRawComment(" NumVgprs: " + Twine(NumVGPR), false);
239 OutStreamer->emitRawComment(" ScratchSize: " + Twine(ScratchSize), false);
240}
241
Tom Stellard45bb48e2015-06-13 03:28:10 +0000242bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000243 CurrentProgramInfo = SIProgramInfo();
244
Matt Arsenault6cb7b8a2017-04-19 17:42:39 +0000245 const AMDGPUMachineFunction *MFI = MF.getInfo<AMDGPUMachineFunction>();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000246
247 // The starting address of all shader programs must be 256 bytes aligned.
Matt Arsenault6cb7b8a2017-04-19 17:42:39 +0000248 // Regular functions just need the basic required instruction alignment.
249 MF.setAlignment(MFI->isEntryFunction() ? 8 : 2);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000250
251 SetupMachineFunction(MF);
252
Tom Stellard45bb48e2015-06-13 03:28:10 +0000253 const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
Konstantin Zhuravlyov67a6d542017-01-06 17:02:10 +0000254 MCContext &Context = getObjFileLowering().getContext();
255 if (!STM.isAmdHsaOS()) {
256 MCSectionELF *ConfigSection =
257 Context.getELFSection(".AMDGPU.config", ELF::SHT_PROGBITS, 0);
258 OutStreamer->SwitchSection(ConfigSection);
259 }
260
Tom Stellardf151a452015-06-26 21:14:58 +0000261 if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000262 if (MFI->isEntryFunction()) {
263 getSIProgramInfo(CurrentProgramInfo, MF);
264 } else {
265 auto I = CallGraphResourceInfo.insert(
266 std::make_pair(MF.getFunction(), SIFunctionResourceInfo()));
267 SIFunctionResourceInfo &Info = I.first->second;
268 assert(I.second && "should only be called once per function");
269 Info = analyzeResourceUsage(MF);
270 }
271
Tim Renouf72800f02017-10-03 19:03:52 +0000272 if (STM.isAmdPalOS())
273 EmitPalMetadata(MF, CurrentProgramInfo);
Tom Stellardf151a452015-06-26 21:14:58 +0000274 if (!STM.isAmdHsaOS()) {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000275 EmitProgramInfoSI(MF, CurrentProgramInfo);
Tom Stellardf151a452015-06-26 21:14:58 +0000276 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000277 } else {
278 EmitProgramInfoR600(MF);
279 }
280
281 DisasmLines.clear();
282 HexLines.clear();
283 DisasmLineMaxLen = 0;
284
285 EmitFunctionBody();
286
287 if (isVerbose()) {
288 MCSectionELF *CommentSection =
289 Context.getELFSection(".AMDGPU.csdata", ELF::SHT_PROGBITS, 0);
290 OutStreamer->SwitchSection(CommentSection);
291
292 if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000293 if (!MFI->isEntryFunction()) {
Matt Arsenault021a2182017-04-19 19:38:10 +0000294 OutStreamer->emitRawComment(" Function info:", false);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000295 SIFunctionResourceInfo &Info = CallGraphResourceInfo[MF.getFunction()];
296 emitCommonFunctionComments(
297 Info.NumVGPR,
298 Info.getTotalNumSGPRs(MF.getSubtarget<SISubtarget>()),
299 Info.PrivateSegmentSize,
300 getFunctionCodeSize(MF));
301 return false;
Matt Arsenault021a2182017-04-19 19:38:10 +0000302 }
303
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000304 OutStreamer->emitRawComment(" Kernel info:", false);
305 emitCommonFunctionComments(CurrentProgramInfo.NumVGPR,
306 CurrentProgramInfo.NumSGPR,
307 CurrentProgramInfo.ScratchSize,
308 getFunctionCodeSize(MF));
309
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000310 OutStreamer->emitRawComment(
311 " FloatMode: " + Twine(CurrentProgramInfo.FloatMode), false);
312 OutStreamer->emitRawComment(
313 " IeeeMode: " + Twine(CurrentProgramInfo.IEEEMode), false);
314 OutStreamer->emitRawComment(
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000315 " LDSByteSize: " + Twine(CurrentProgramInfo.LDSSize) +
316 " bytes/workgroup (compile time only)", false);
Matt Arsenaultd41c0db2015-11-05 05:27:07 +0000317
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000318 OutStreamer->emitRawComment(
319 " SGPRBlocks: " + Twine(CurrentProgramInfo.SGPRBlocks), false);
320 OutStreamer->emitRawComment(
321 " VGPRBlocks: " + Twine(CurrentProgramInfo.VGPRBlocks), false);
Matt Arsenault021a2182017-04-19 19:38:10 +0000322
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000323 OutStreamer->emitRawComment(
324 " NumSGPRsForWavesPerEU: " +
325 Twine(CurrentProgramInfo.NumSGPRsForWavesPerEU), false);
326 OutStreamer->emitRawComment(
327 " NumVGPRsForWavesPerEU: " +
328 Twine(CurrentProgramInfo.NumVGPRsForWavesPerEU), false);
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000329
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000330 OutStreamer->emitRawComment(
331 " ReservedVGPRFirst: " + Twine(CurrentProgramInfo.ReservedVGPRFirst),
332 false);
333 OutStreamer->emitRawComment(
334 " ReservedVGPRCount: " + Twine(CurrentProgramInfo.ReservedVGPRCount),
335 false);
Konstantin Zhuravlyov1d99c4d2016-04-26 15:43:14 +0000336
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000337 if (MF.getSubtarget<SISubtarget>().debuggerEmitPrologue()) {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000338 OutStreamer->emitRawComment(
339 " DebuggerWavefrontPrivateSegmentOffsetSGPR: s" +
340 Twine(CurrentProgramInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR), false);
341 OutStreamer->emitRawComment(
342 " DebuggerPrivateSegmentBufferSGPR: s" +
343 Twine(CurrentProgramInfo.DebuggerPrivateSegmentBufferSGPR), false);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000344 }
345
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000346 OutStreamer->emitRawComment(
347 " COMPUTE_PGM_RSRC2:USER_SGPR: " +
348 Twine(G_00B84C_USER_SGPR(CurrentProgramInfo.ComputePGMRSrc2)), false);
349 OutStreamer->emitRawComment(
350 " COMPUTE_PGM_RSRC2:TRAP_HANDLER: " +
351 Twine(G_00B84C_TRAP_HANDLER(CurrentProgramInfo.ComputePGMRSrc2)), false);
352 OutStreamer->emitRawComment(
353 " COMPUTE_PGM_RSRC2:TGID_X_EN: " +
354 Twine(G_00B84C_TGID_X_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
355 OutStreamer->emitRawComment(
356 " COMPUTE_PGM_RSRC2:TGID_Y_EN: " +
357 Twine(G_00B84C_TGID_Y_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
358 OutStreamer->emitRawComment(
359 " COMPUTE_PGM_RSRC2:TGID_Z_EN: " +
360 Twine(G_00B84C_TGID_Z_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
361 OutStreamer->emitRawComment(
362 " COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: " +
363 Twine(G_00B84C_TIDIG_COMP_CNT(CurrentProgramInfo.ComputePGMRSrc2)),
364 false);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000365 } else {
366 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
367 OutStreamer->emitRawComment(
Matt Arsenaultf9245b72016-07-22 17:01:25 +0000368 Twine("SQ_PGM_RESOURCES:STACK_SIZE = " + Twine(MFI->CFStackSize)));
Tom Stellard45bb48e2015-06-13 03:28:10 +0000369 }
370 }
371
372 if (STM.dumpCode()) {
373
374 OutStreamer->SwitchSection(
375 Context.getELFSection(".AMDGPU.disasm", ELF::SHT_NOTE, 0));
376
377 for (size_t i = 0; i < DisasmLines.size(); ++i) {
378 std::string Comment(DisasmLineMaxLen - DisasmLines[i].size(), ' ');
379 Comment += " ; " + HexLines[i] + "\n";
380
381 OutStreamer->EmitBytes(StringRef(DisasmLines[i]));
382 OutStreamer->EmitBytes(StringRef(Comment));
383 }
384 }
385
386 return false;
387}
388
389void AMDGPUAsmPrinter::EmitProgramInfoR600(const MachineFunction &MF) {
390 unsigned MaxGPR = 0;
391 bool killPixel = false;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000392 const R600Subtarget &STM = MF.getSubtarget<R600Subtarget>();
393 const R600RegisterInfo *RI = STM.getRegisterInfo();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000394 const R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
395
396 for (const MachineBasicBlock &MBB : MF) {
397 for (const MachineInstr &MI : MBB) {
398 if (MI.getOpcode() == AMDGPU::KILLGT)
399 killPixel = true;
400 unsigned numOperands = MI.getNumOperands();
401 for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
402 const MachineOperand &MO = MI.getOperand(op_idx);
403 if (!MO.isReg())
404 continue;
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000405 unsigned HWReg = RI->getHWRegIndex(MO.getReg());
Tom Stellard45bb48e2015-06-13 03:28:10 +0000406
407 // Register with value > 127 aren't GPR
408 if (HWReg > 127)
409 continue;
410 MaxGPR = std::max(MaxGPR, HWReg);
411 }
412 }
413 }
414
415 unsigned RsrcReg;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000416 if (STM.getGeneration() >= R600Subtarget::EVERGREEN) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000417 // Evergreen / Northern Islands
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000418 switch (MF.getFunction()->getCallingConv()) {
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000419 default: LLVM_FALLTHROUGH;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000420 case CallingConv::AMDGPU_CS: RsrcReg = R_0288D4_SQ_PGM_RESOURCES_LS; break;
421 case CallingConv::AMDGPU_GS: RsrcReg = R_028878_SQ_PGM_RESOURCES_GS; break;
422 case CallingConv::AMDGPU_PS: RsrcReg = R_028844_SQ_PGM_RESOURCES_PS; break;
423 case CallingConv::AMDGPU_VS: RsrcReg = R_028860_SQ_PGM_RESOURCES_VS; break;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000424 }
425 } else {
426 // R600 / R700
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000427 switch (MF.getFunction()->getCallingConv()) {
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000428 default: LLVM_FALLTHROUGH;
429 case CallingConv::AMDGPU_GS: LLVM_FALLTHROUGH;
430 case CallingConv::AMDGPU_CS: LLVM_FALLTHROUGH;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000431 case CallingConv::AMDGPU_VS: RsrcReg = R_028868_SQ_PGM_RESOURCES_VS; break;
432 case CallingConv::AMDGPU_PS: RsrcReg = R_028850_SQ_PGM_RESOURCES_PS; break;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000433 }
434 }
435
436 OutStreamer->EmitIntValue(RsrcReg, 4);
437 OutStreamer->EmitIntValue(S_NUM_GPRS(MaxGPR + 1) |
Matt Arsenaultf9245b72016-07-22 17:01:25 +0000438 S_STACK_SIZE(MFI->CFStackSize), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000439 OutStreamer->EmitIntValue(R_02880C_DB_SHADER_CONTROL, 4);
440 OutStreamer->EmitIntValue(S_02880C_KILL_ENABLE(killPixel), 4);
441
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000442 if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000443 OutStreamer->EmitIntValue(R_0288E8_SQ_LDS_ALLOC, 4);
Matt Arsenault52ef4012016-07-26 16:45:58 +0000444 OutStreamer->EmitIntValue(alignTo(MFI->getLDSSize(), 4) >> 2, 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000445 }
446}
447
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000448uint64_t AMDGPUAsmPrinter::getFunctionCodeSize(const MachineFunction &MF) const {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000449 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000450 const SIInstrInfo *TII = STM.getInstrInfo();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000451
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000452 uint64_t CodeSize = 0;
453
Tom Stellard45bb48e2015-06-13 03:28:10 +0000454 for (const MachineBasicBlock &MBB : MF) {
455 for (const MachineInstr &MI : MBB) {
456 // TODO: CodeSize should account for multiple functions.
Matt Arsenaultc5746862015-08-12 09:04:44 +0000457
458 // TODO: Should we count size of debug info?
459 if (MI.isDebugValue())
460 continue;
461
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000462 CodeSize += TII->getInstSizeInBytes(MI);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000463 }
464 }
465
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000466 return CodeSize;
467}
468
469static bool hasAnyNonFlatUseOfReg(const MachineRegisterInfo &MRI,
470 const SIInstrInfo &TII,
471 unsigned Reg) {
472 for (const MachineOperand &UseOp : MRI.reg_operands(Reg)) {
473 if (!UseOp.isImplicit() || !TII.isFLAT(*UseOp.getParent()))
474 return true;
475 }
476
477 return false;
478}
479
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000480static unsigned getNumExtraSGPRs(const SISubtarget &ST,
481 bool VCCUsed,
482 bool FlatScrUsed) {
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000483 unsigned ExtraSGPRs = 0;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000484 if (VCCUsed)
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000485 ExtraSGPRs = 2;
486
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000487 if (ST.getGeneration() < SISubtarget::VOLCANIC_ISLANDS) {
488 if (FlatScrUsed)
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000489 ExtraSGPRs = 4;
490 } else {
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000491 if (ST.isXNACKEnabled())
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000492 ExtraSGPRs = 4;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000493
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000494 if (FlatScrUsed)
Nicolai Haehnle3c05d6d2016-01-07 17:10:20 +0000495 ExtraSGPRs = 6;
Tom Stellardcaaa3aa2015-12-17 17:05:09 +0000496 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000497
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000498 return ExtraSGPRs;
499}
500
501int32_t AMDGPUAsmPrinter::SIFunctionResourceInfo::getTotalNumSGPRs(
502 const SISubtarget &ST) const {
503 return NumExplicitSGPR + getNumExtraSGPRs(ST, UsesVCC, UsesFlatScratch);
504}
505
506AMDGPUAsmPrinter::SIFunctionResourceInfo AMDGPUAsmPrinter::analyzeResourceUsage(
507 const MachineFunction &MF) const {
508 SIFunctionResourceInfo Info;
509
510 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
511 const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
512 const MachineFrameInfo &FrameInfo = MF.getFrameInfo();
513 const MachineRegisterInfo &MRI = MF.getRegInfo();
514 const SIInstrInfo *TII = ST.getInstrInfo();
515 const SIRegisterInfo &TRI = TII->getRegisterInfo();
516
517 Info.UsesFlatScratch = MRI.isPhysRegUsed(AMDGPU::FLAT_SCR_LO) ||
518 MRI.isPhysRegUsed(AMDGPU::FLAT_SCR_HI);
519
520 // Even if FLAT_SCRATCH is implicitly used, it has no effect if flat
521 // instructions aren't used to access the scratch buffer. Inline assembly may
522 // need it though.
523 //
524 // If we only have implicit uses of flat_scr on flat instructions, it is not
525 // really needed.
526 if (Info.UsesFlatScratch && !MFI->hasFlatScratchInit() &&
527 (!hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR) &&
528 !hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR_LO) &&
529 !hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR_HI))) {
530 Info.UsesFlatScratch = false;
531 }
532
533 Info.HasDynamicallySizedStack = FrameInfo.hasVarSizedObjects();
534 Info.PrivateSegmentSize = FrameInfo.getStackSize();
535
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000536
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000537 Info.UsesVCC = MRI.isPhysRegUsed(AMDGPU::VCC_LO) ||
538 MRI.isPhysRegUsed(AMDGPU::VCC_HI);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000539
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000540 // If there are no calls, MachineRegisterInfo can tell us the used register
541 // count easily.
Matt Arsenault22cdb612017-09-05 18:36:36 +0000542 // A tail call isn't considered a call for MachineFrameInfo's purposes.
543 if (!FrameInfo.hasCalls() && !FrameInfo.hasTailCall()) {
Matt Arsenault2738ede2017-08-02 17:15:01 +0000544 MCPhysReg HighestVGPRReg = AMDGPU::NoRegister;
545 for (MCPhysReg Reg : reverse(AMDGPU::VGPR_32RegClass.getRegisters())) {
546 if (MRI.isPhysRegUsed(Reg)) {
547 HighestVGPRReg = Reg;
548 break;
549 }
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000550 }
Matt Arsenault2738ede2017-08-02 17:15:01 +0000551
552 MCPhysReg HighestSGPRReg = AMDGPU::NoRegister;
553 for (MCPhysReg Reg : reverse(AMDGPU::SGPR_32RegClass.getRegisters())) {
554 if (MRI.isPhysRegUsed(Reg)) {
555 HighestSGPRReg = Reg;
556 break;
557 }
558 }
559
560 // We found the maximum register index. They start at 0, so add one to get the
561 // number of registers.
562 Info.NumVGPR = HighestVGPRReg == AMDGPU::NoRegister ? 0 :
563 TRI.getHWRegIndex(HighestVGPRReg) + 1;
564 Info.NumExplicitSGPR = HighestSGPRReg == AMDGPU::NoRegister ? 0 :
565 TRI.getHWRegIndex(HighestSGPRReg) + 1;
566
567 return Info;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000568 }
569
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000570 int32_t MaxVGPR = -1;
571 int32_t MaxSGPR = -1;
572 uint32_t CalleeFrameSize = 0;
573
574 for (const MachineBasicBlock &MBB : MF) {
575 for (const MachineInstr &MI : MBB) {
576 // TODO: Check regmasks? Do they occur anywhere except calls?
577 for (const MachineOperand &MO : MI.operands()) {
578 unsigned Width = 0;
579 bool IsSGPR = false;
580
581 if (!MO.isReg())
582 continue;
583
584 unsigned Reg = MO.getReg();
585 switch (Reg) {
586 case AMDGPU::EXEC:
587 case AMDGPU::EXEC_LO:
588 case AMDGPU::EXEC_HI:
589 case AMDGPU::SCC:
590 case AMDGPU::M0:
591 case AMDGPU::SRC_SHARED_BASE:
592 case AMDGPU::SRC_SHARED_LIMIT:
593 case AMDGPU::SRC_PRIVATE_BASE:
594 case AMDGPU::SRC_PRIVATE_LIMIT:
595 continue;
596
597 case AMDGPU::NoRegister:
598 assert(MI.isDebugValue());
599 continue;
600
601 case AMDGPU::VCC:
602 case AMDGPU::VCC_LO:
603 case AMDGPU::VCC_HI:
604 Info.UsesVCC = true;
605 continue;
606
607 case AMDGPU::FLAT_SCR:
608 case AMDGPU::FLAT_SCR_LO:
609 case AMDGPU::FLAT_SCR_HI:
610 continue;
611
612 case AMDGPU::TBA:
613 case AMDGPU::TBA_LO:
614 case AMDGPU::TBA_HI:
615 case AMDGPU::TMA:
616 case AMDGPU::TMA_LO:
617 case AMDGPU::TMA_HI:
618 llvm_unreachable("trap handler registers should not be used");
619
620 default:
621 break;
622 }
623
624 if (AMDGPU::SReg_32RegClass.contains(Reg)) {
625 assert(!AMDGPU::TTMP_32RegClass.contains(Reg) &&
626 "trap handler registers should not be used");
627 IsSGPR = true;
628 Width = 1;
629 } else if (AMDGPU::VGPR_32RegClass.contains(Reg)) {
630 IsSGPR = false;
631 Width = 1;
632 } else if (AMDGPU::SReg_64RegClass.contains(Reg)) {
633 assert(!AMDGPU::TTMP_64RegClass.contains(Reg) &&
634 "trap handler registers should not be used");
635 IsSGPR = true;
636 Width = 2;
637 } else if (AMDGPU::VReg_64RegClass.contains(Reg)) {
638 IsSGPR = false;
639 Width = 2;
640 } else if (AMDGPU::VReg_96RegClass.contains(Reg)) {
641 IsSGPR = false;
642 Width = 3;
643 } else if (AMDGPU::SReg_128RegClass.contains(Reg)) {
644 IsSGPR = true;
645 Width = 4;
646 } else if (AMDGPU::VReg_128RegClass.contains(Reg)) {
647 IsSGPR = false;
648 Width = 4;
649 } else if (AMDGPU::SReg_256RegClass.contains(Reg)) {
650 IsSGPR = true;
651 Width = 8;
652 } else if (AMDGPU::VReg_256RegClass.contains(Reg)) {
653 IsSGPR = false;
654 Width = 8;
655 } else if (AMDGPU::SReg_512RegClass.contains(Reg)) {
656 IsSGPR = true;
657 Width = 16;
658 } else if (AMDGPU::VReg_512RegClass.contains(Reg)) {
659 IsSGPR = false;
660 Width = 16;
661 } else {
662 llvm_unreachable("Unknown register class");
663 }
664 unsigned HWReg = TRI.getHWRegIndex(Reg);
665 int MaxUsed = HWReg + Width - 1;
666 if (IsSGPR) {
667 MaxSGPR = MaxUsed > MaxSGPR ? MaxUsed : MaxSGPR;
668 } else {
669 MaxVGPR = MaxUsed > MaxVGPR ? MaxUsed : MaxVGPR;
670 }
671 }
672
673 if (MI.isCall()) {
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000674 // Pseudo used just to encode the underlying global. Is there a better
675 // way to track this?
Matt Arsenault71bcbd42017-08-11 20:42:08 +0000676
677 const MachineOperand *CalleeOp
678 = TII->getNamedOperand(MI, AMDGPU::OpName::callee);
679 const Function *Callee = cast<Function>(CalleeOp->getGlobal());
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000680 if (Callee->isDeclaration()) {
681 // If this is a call to an external function, we can't do much. Make
682 // conservative guesses.
683
684 // 48 SGPRs - vcc, - flat_scr, -xnack
685 int MaxSGPRGuess = 47 - getNumExtraSGPRs(ST, true,
686 ST.hasFlatAddressSpace());
687 MaxSGPR = std::max(MaxSGPR, MaxSGPRGuess);
688 MaxVGPR = std::max(MaxVGPR, 23);
689
690 CalleeFrameSize = std::max(CalleeFrameSize, 16384u);
691 Info.UsesVCC = true;
692 Info.UsesFlatScratch = ST.hasFlatAddressSpace();
693 Info.HasDynamicallySizedStack = true;
694 } else {
695 // We force CodeGen to run in SCC order, so the callee's register
696 // usage etc. should be the cumulative usage of all callees.
697 auto I = CallGraphResourceInfo.find(Callee);
698 assert(I != CallGraphResourceInfo.end() &&
699 "callee should have been handled before caller");
700
701 MaxSGPR = std::max(I->second.NumExplicitSGPR - 1, MaxSGPR);
702 MaxVGPR = std::max(I->second.NumVGPR - 1, MaxVGPR);
703 CalleeFrameSize
704 = std::max(I->second.PrivateSegmentSize, CalleeFrameSize);
705 Info.UsesVCC |= I->second.UsesVCC;
706 Info.UsesFlatScratch |= I->second.UsesFlatScratch;
707 Info.HasDynamicallySizedStack |= I->second.HasDynamicallySizedStack;
708 Info.HasRecursion |= I->second.HasRecursion;
709 }
710
711 if (!Callee->doesNotRecurse())
712 Info.HasRecursion = true;
713 }
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000714 }
715 }
716
Matt Arsenault6ed7b9b2017-08-02 01:31:28 +0000717 Info.NumExplicitSGPR = MaxSGPR + 1;
718 Info.NumVGPR = MaxVGPR + 1;
719 Info.PrivateSegmentSize += CalleeFrameSize;
Matt Arsenault3416b8c2017-06-01 15:05:15 +0000720
721 return Info;
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000722}
723
724void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo,
725 const MachineFunction &MF) {
726 SIFunctionResourceInfo Info = analyzeResourceUsage(MF);
727
728 ProgInfo.NumVGPR = Info.NumVGPR;
729 ProgInfo.NumSGPR = Info.NumExplicitSGPR;
730 ProgInfo.ScratchSize = Info.PrivateSegmentSize;
731 ProgInfo.VCCUsed = Info.UsesVCC;
732 ProgInfo.FlatUsed = Info.UsesFlatScratch;
733 ProgInfo.DynamicCallStack = Info.HasDynamicallySizedStack || Info.HasRecursion;
734
735 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
736 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
737 const SIInstrInfo *TII = STM.getInstrInfo();
738 const SIRegisterInfo *RI = &TII->getRegisterInfo();
739
740 unsigned ExtraSGPRs = getNumExtraSGPRs(STM,
741 ProgInfo.VCCUsed,
742 ProgInfo.FlatUsed);
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000743 unsigned ExtraVGPRs = STM.getReservedNumVGPRs(MF);
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +0000744
Marek Olsak91f22fb2016-12-09 19:49:40 +0000745 // Check the addressable register limit before we add ExtraSGPRs.
746 if (STM.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS &&
747 !STM.hasSGPRInitBug()) {
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000748 unsigned MaxAddressableNumSGPRs = STM.getAddressableNumSGPRs();
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000749 if (ProgInfo.NumSGPR > MaxAddressableNumSGPRs) {
Marek Olsak91f22fb2016-12-09 19:49:40 +0000750 // This can happen due to a compiler bug or when using inline asm.
751 LLVMContext &Ctx = MF.getFunction()->getContext();
752 DiagnosticInfoResourceLimit Diag(*MF.getFunction(),
753 "addressable scalar registers",
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000754 ProgInfo.NumSGPR, DS_Error,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000755 DK_ResourceLimit,
756 MaxAddressableNumSGPRs);
Marek Olsak91f22fb2016-12-09 19:49:40 +0000757 Ctx.diagnose(Diag);
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000758 ProgInfo.NumSGPR = MaxAddressableNumSGPRs - 1;
Marek Olsak91f22fb2016-12-09 19:49:40 +0000759 }
760 }
761
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000762 // Account for extra SGPRs and VGPRs reserved for debugger use.
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000763 ProgInfo.NumSGPR += ExtraSGPRs;
764 ProgInfo.NumVGPR += ExtraVGPRs;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000765
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000766 // Adjust number of registers used to meet default/requested minimum/maximum
767 // number of waves per execution unit request.
768 ProgInfo.NumSGPRsForWavesPerEU = std::max(
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000769 std::max(ProgInfo.NumSGPR, 1u), STM.getMinNumSGPRs(MFI->getMaxWavesPerEU()));
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000770 ProgInfo.NumVGPRsForWavesPerEU = std::max(
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000771 std::max(ProgInfo.NumVGPR, 1u), STM.getMinNumVGPRs(MFI->getMaxWavesPerEU()));
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000772
Marek Olsak91f22fb2016-12-09 19:49:40 +0000773 if (STM.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ||
774 STM.hasSGPRInitBug()) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000775 unsigned MaxAddressableNumSGPRs = STM.getAddressableNumSGPRs();
776 if (ProgInfo.NumSGPR > MaxAddressableNumSGPRs) {
777 // This can happen due to a compiler bug or when using inline asm to use
778 // the registers which are usually reserved for vcc etc.
Marek Olsak91f22fb2016-12-09 19:49:40 +0000779 LLVMContext &Ctx = MF.getFunction()->getContext();
780 DiagnosticInfoResourceLimit Diag(*MF.getFunction(),
781 "scalar registers",
782 ProgInfo.NumSGPR, DS_Error,
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000783 DK_ResourceLimit,
784 MaxAddressableNumSGPRs);
Marek Olsak91f22fb2016-12-09 19:49:40 +0000785 Ctx.diagnose(Diag);
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000786 ProgInfo.NumSGPR = MaxAddressableNumSGPRs;
787 ProgInfo.NumSGPRsForWavesPerEU = MaxAddressableNumSGPRs;
Marek Olsak91f22fb2016-12-09 19:49:40 +0000788 }
Matt Arsenault4eae3012016-10-28 20:31:47 +0000789 }
790
791 if (STM.hasSGPRInitBug()) {
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000792 ProgInfo.NumSGPR =
793 AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG;
794 ProgInfo.NumSGPRsForWavesPerEU =
795 AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000796 }
797
Matt Arsenault161e2b42017-04-18 20:59:40 +0000798 if (MFI->getNumUserSGPRs() > STM.getMaxNumUserSGPRs()) {
Matt Arsenault41003af2015-11-30 21:16:07 +0000799 LLVMContext &Ctx = MF.getFunction()->getContext();
Matt Arsenaultff982412016-06-20 18:13:04 +0000800 DiagnosticInfoResourceLimit Diag(*MF.getFunction(), "user SGPRs",
Matt Arsenault161e2b42017-04-18 20:59:40 +0000801 MFI->getNumUserSGPRs(), DS_Error);
Matt Arsenaultff982412016-06-20 18:13:04 +0000802 Ctx.diagnose(Diag);
Matt Arsenault41003af2015-11-30 21:16:07 +0000803 }
804
Matt Arsenault52ef4012016-07-26 16:45:58 +0000805 if (MFI->getLDSSize() > static_cast<unsigned>(STM.getLocalMemorySize())) {
Matt Arsenault1c4d0ef2016-04-28 19:37:35 +0000806 LLVMContext &Ctx = MF.getFunction()->getContext();
Matt Arsenaultff982412016-06-20 18:13:04 +0000807 DiagnosticInfoResourceLimit Diag(*MF.getFunction(), "local memory",
Matt Arsenault52ef4012016-07-26 16:45:58 +0000808 MFI->getLDSSize(), DS_Error);
Matt Arsenaultff982412016-06-20 18:13:04 +0000809 Ctx.diagnose(Diag);
Matt Arsenault1c4d0ef2016-04-28 19:37:35 +0000810 }
811
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000812 // SGPRBlocks is actual number of SGPR blocks minus 1.
813 ProgInfo.SGPRBlocks = alignTo(ProgInfo.NumSGPRsForWavesPerEU,
Konstantin Zhuravlyove22fbcb2017-02-08 13:18:40 +0000814 STM.getSGPREncodingGranule());
815 ProgInfo.SGPRBlocks = ProgInfo.SGPRBlocks / STM.getSGPREncodingGranule() - 1;
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000816
817 // VGPRBlocks is actual number of VGPR blocks minus 1.
818 ProgInfo.VGPRBlocks = alignTo(ProgInfo.NumVGPRsForWavesPerEU,
Konstantin Zhuravlyove22fbcb2017-02-08 13:18:40 +0000819 STM.getVGPREncodingGranule());
820 ProgInfo.VGPRBlocks = ProgInfo.VGPRBlocks / STM.getVGPREncodingGranule() - 1;
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000821
Konstantin Zhuravlyov9f89ede2017-02-08 14:05:23 +0000822 // Record first reserved VGPR and number of reserved VGPRs.
Matt Arsenaulta3566f22017-04-17 19:48:30 +0000823 ProgInfo.ReservedVGPRFirst = STM.debuggerReserveRegs() ? ProgInfo.NumVGPR : 0;
Konstantin Zhuravlyove03b1d72017-02-08 13:02:33 +0000824 ProgInfo.ReservedVGPRCount = STM.getReservedNumVGPRs(MF);
825
826 // Update DebuggerWavefrontPrivateSegmentOffsetSGPR and
827 // DebuggerPrivateSegmentBufferSGPR fields if "amdgpu-debugger-emit-prologue"
828 // attribute was requested.
829 if (STM.debuggerEmitPrologue()) {
830 ProgInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR =
831 RI->getHWRegIndex(MFI->getScratchWaveOffsetReg());
832 ProgInfo.DebuggerPrivateSegmentBufferSGPR =
833 RI->getHWRegIndex(MFI->getScratchRSrcReg());
834 }
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000835
Tom Stellard45bb48e2015-06-13 03:28:10 +0000836 // Set the value to initialize FP_ROUND and FP_DENORM parts of the mode
837 // register.
838 ProgInfo.FloatMode = getFPMode(MF);
839
Wei Ding3cb2a1e2016-10-19 22:34:49 +0000840 ProgInfo.IEEEMode = STM.enableIEEEBit(MF);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000841
Matt Arsenault7293f982016-01-28 20:53:35 +0000842 // Make clamp modifier on NaN input returns 0.
Matt Arsenault2fdf2a12017-02-21 23:35:48 +0000843 ProgInfo.DX10Clamp = STM.enableDX10Clamp();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000844
Tom Stellard45bb48e2015-06-13 03:28:10 +0000845 unsigned LDSAlignShift;
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000846 if (STM.getGeneration() < SISubtarget::SEA_ISLANDS) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000847 // LDS is allocated in 64 dword blocks.
848 LDSAlignShift = 8;
849 } else {
850 // LDS is allocated in 128 dword blocks.
851 LDSAlignShift = 9;
852 }
853
Konstantin Zhuravlyov1d650262016-09-06 20:22:28 +0000854 unsigned LDSSpillSize =
Matt Arsenault161e2b42017-04-18 20:59:40 +0000855 MFI->getLDSWaveSpillSize() * MFI->getMaxFlatWorkGroupSize();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000856
Matt Arsenault52ef4012016-07-26 16:45:58 +0000857 ProgInfo.LDSSize = MFI->getLDSSize() + LDSSpillSize;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000858 ProgInfo.LDSBlocks =
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +0000859 alignTo(ProgInfo.LDSSize, 1ULL << LDSAlignShift) >> LDSAlignShift;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000860
861 // Scratch is allocated in 256 dword blocks.
862 unsigned ScratchAlignShift = 10;
863 // We need to program the hardware with the amount of scratch memory that
864 // is used by the entire wave. ProgInfo.ScratchSize is the amount of
865 // scratch memory used per thread.
866 ProgInfo.ScratchBlocks =
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000867 alignTo(ProgInfo.ScratchSize * STM.getWavefrontSize(),
Aaron Ballmanef0fe1e2016-03-30 21:30:00 +0000868 1ULL << ScratchAlignShift) >>
Rui Ueyamada00f2f2016-01-14 21:06:47 +0000869 ScratchAlignShift;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000870
871 ProgInfo.ComputePGMRSrc1 =
872 S_00B848_VGPRS(ProgInfo.VGPRBlocks) |
873 S_00B848_SGPRS(ProgInfo.SGPRBlocks) |
874 S_00B848_PRIORITY(ProgInfo.Priority) |
875 S_00B848_FLOAT_MODE(ProgInfo.FloatMode) |
876 S_00B848_PRIV(ProgInfo.Priv) |
877 S_00B848_DX10_CLAMP(ProgInfo.DX10Clamp) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000878 S_00B848_DEBUG_MODE(ProgInfo.DebugMode) |
Tom Stellard45bb48e2015-06-13 03:28:10 +0000879 S_00B848_IEEE_MODE(ProgInfo.IEEEMode);
880
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000881 // 0 = X, 1 = XY, 2 = XYZ
882 unsigned TIDIGCompCnt = 0;
883 if (MFI->hasWorkItemIDZ())
884 TIDIGCompCnt = 2;
885 else if (MFI->hasWorkItemIDY())
886 TIDIGCompCnt = 1;
887
Tom Stellard45bb48e2015-06-13 03:28:10 +0000888 ProgInfo.ComputePGMRSrc2 =
889 S_00B84C_SCRATCH_EN(ProgInfo.ScratchBlocks > 0) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000890 S_00B84C_USER_SGPR(MFI->getNumUserSGPRs()) |
Wei Ding205bfdb2017-02-10 02:15:29 +0000891 S_00B84C_TRAP_HANDLER(STM.isTrapHandlerEnabled()) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000892 S_00B84C_TGID_X_EN(MFI->hasWorkGroupIDX()) |
893 S_00B84C_TGID_Y_EN(MFI->hasWorkGroupIDY()) |
894 S_00B84C_TGID_Z_EN(MFI->hasWorkGroupIDZ()) |
895 S_00B84C_TG_SIZE_EN(MFI->hasWorkGroupInfo()) |
896 S_00B84C_TIDIG_COMP_CNT(TIDIGCompCnt) |
897 S_00B84C_EXCP_EN_MSB(0) |
Konstantin Zhuravlyov6ccb0762017-05-05 20:13:55 +0000898 // For AMDHSA, LDS_SIZE must be zero, as it is populated by the CP.
899 S_00B84C_LDS_SIZE(STM.isAmdHsaOS() ? 0 : ProgInfo.LDSBlocks) |
Matt Arsenault26f8f3d2015-11-30 21:16:03 +0000900 S_00B84C_EXCP_EN(0);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000901}
902
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000903static unsigned getRsrcReg(CallingConv::ID CallConv) {
904 switch (CallConv) {
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000905 default: LLVM_FALLTHROUGH;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000906 case CallingConv::AMDGPU_CS: return R_00B848_COMPUTE_PGM_RSRC1;
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000907 case CallingConv::AMDGPU_LS: return R_00B528_SPI_SHADER_PGM_RSRC1_LS;
Marek Olsaka302a7362017-05-02 15:41:10 +0000908 case CallingConv::AMDGPU_HS: return R_00B428_SPI_SHADER_PGM_RSRC1_HS;
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000909 case CallingConv::AMDGPU_ES: return R_00B328_SPI_SHADER_PGM_RSRC1_ES;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000910 case CallingConv::AMDGPU_GS: return R_00B228_SPI_SHADER_PGM_RSRC1_GS;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000911 case CallingConv::AMDGPU_VS: return R_00B128_SPI_SHADER_PGM_RSRC1_VS;
Tim Renoufef1ae8f2017-09-29 09:51:22 +0000912 case CallingConv::AMDGPU_PS: return R_00B028_SPI_SHADER_PGM_RSRC1_PS;
Tom Stellard45bb48e2015-06-13 03:28:10 +0000913 }
914}
915
916void AMDGPUAsmPrinter::EmitProgramInfoSI(const MachineFunction &MF,
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000917 const SIProgramInfo &CurrentProgramInfo) {
Matt Arsenault43e92fe2016-06-24 06:30:11 +0000918 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +0000919 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000920 unsigned RsrcReg = getRsrcReg(MF.getFunction()->getCallingConv());
Tom Stellard45bb48e2015-06-13 03:28:10 +0000921
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000922 if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000923 OutStreamer->EmitIntValue(R_00B848_COMPUTE_PGM_RSRC1, 4);
924
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000925 OutStreamer->EmitIntValue(CurrentProgramInfo.ComputePGMRSrc1, 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000926
927 OutStreamer->EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000928 OutStreamer->EmitIntValue(CurrentProgramInfo.ComputePGMRSrc2, 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000929
930 OutStreamer->EmitIntValue(R_00B860_COMPUTE_TMPRING_SIZE, 4);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000931 OutStreamer->EmitIntValue(S_00B860_WAVESIZE(CurrentProgramInfo.ScratchBlocks), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000932
933 // TODO: Should probably note flat usage somewhere. SC emits a "FlatPtr32 =
934 // 0" comment but I don't see a corresponding field in the register spec.
935 } else {
936 OutStreamer->EmitIntValue(RsrcReg, 4);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000937 OutStreamer->EmitIntValue(S_00B028_VGPRS(CurrentProgramInfo.VGPRBlocks) |
938 S_00B028_SGPRS(CurrentProgramInfo.SGPRBlocks), 4);
Tim Renouf13229152017-09-29 09:49:35 +0000939 unsigned Rsrc2Val = 0;
Nicolai Haehnledf3a20c2016-04-06 19:40:20 +0000940 if (STM.isVGPRSpillingEnabled(*MF.getFunction())) {
Tom Stellard45bb48e2015-06-13 03:28:10 +0000941 OutStreamer->EmitIntValue(R_0286E8_SPI_TMPRING_SIZE, 4);
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +0000942 OutStreamer->EmitIntValue(S_0286E8_WAVESIZE(CurrentProgramInfo.ScratchBlocks), 4);
Tim Renouf13229152017-09-29 09:49:35 +0000943 if (TM.getTargetTriple().getOS() == Triple::AMDPAL)
944 Rsrc2Val = S_00B84C_SCRATCH_EN(CurrentProgramInfo.ScratchBlocks > 0);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000945 }
Tim Renouf13229152017-09-29 09:49:35 +0000946 if (MF.getFunction()->getCallingConv() == CallingConv::AMDGPU_PS) {
947 OutStreamer->EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4);
948 OutStreamer->EmitIntValue(MFI->getPSInputEnable(), 4);
949 OutStreamer->EmitIntValue(R_0286D0_SPI_PS_INPUT_ADDR, 4);
950 OutStreamer->EmitIntValue(MFI->getPSInputAddr(), 4);
951 Rsrc2Val |= S_00B02C_EXTRA_LDS_SIZE(CurrentProgramInfo.LDSBlocks);
952 }
953 if (Rsrc2Val) {
954 OutStreamer->EmitIntValue(RsrcReg + 4 /*rsrc2*/, 4);
955 OutStreamer->EmitIntValue(Rsrc2Val, 4);
956 }
Tom Stellard45bb48e2015-06-13 03:28:10 +0000957 }
Marek Olsak0532c192016-07-13 17:35:15 +0000958
959 OutStreamer->EmitIntValue(R_SPILLED_SGPRS, 4);
960 OutStreamer->EmitIntValue(MFI->getNumSpilledSGPRs(), 4);
961 OutStreamer->EmitIntValue(R_SPILLED_VGPRS, 4);
962 OutStreamer->EmitIntValue(MFI->getNumSpilledVGPRs(), 4);
Tom Stellard45bb48e2015-06-13 03:28:10 +0000963}
964
Tim Renouf72800f02017-10-03 19:03:52 +0000965// This is the equivalent of EmitProgramInfoSI above, but for when the OS type
966// is AMDPAL. It stores each compute/SPI register setting and other PAL
967// metadata items into the PalMetadata map, combining with any provided by the
968// frontend as LLVM metadata. Once all functions are written, PalMetadata is
969// then written as a single block in the .note section.
970void AMDGPUAsmPrinter::EmitPalMetadata(const MachineFunction &MF,
971 const SIProgramInfo &CurrentProgramInfo) {
972 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
973 // Given the calling convention, calculate the register number for rsrc1. In
974 // principle the register number could change in future hardware, but we know
975 // it is the same for gfx6-9 (except that LS and ES don't exist on gfx9), so
976 // we can use the same fixed value that .AMDGPU.config has for Mesa. Note
977 // that we use a register number rather than a byte offset, so we need to
978 // divide by 4.
979 unsigned Rsrc1Reg = getRsrcReg(MF.getFunction()->getCallingConv()) / 4;
980 unsigned Rsrc2Reg = Rsrc1Reg + 1;
981 // Also calculate the PAL metadata key for *S_SCRATCH_SIZE. It can be used
982 // with a constant offset to access any non-register shader-specific PAL
983 // metadata key.
984 unsigned ScratchSizeKey = AMDGPU::ElfNote::AMDGPU_PAL_METADATA_CS_SCRATCH_SIZE;
985 switch (MF.getFunction()->getCallingConv()) {
986 case CallingConv::AMDGPU_PS:
987 ScratchSizeKey = AMDGPU::ElfNote::AMDGPU_PAL_METADATA_PS_SCRATCH_SIZE;
988 break;
989 case CallingConv::AMDGPU_VS:
990 ScratchSizeKey = AMDGPU::ElfNote::AMDGPU_PAL_METADATA_VS_SCRATCH_SIZE;
991 break;
992 case CallingConv::AMDGPU_GS:
993 ScratchSizeKey = AMDGPU::ElfNote::AMDGPU_PAL_METADATA_GS_SCRATCH_SIZE;
994 break;
995 case CallingConv::AMDGPU_ES:
996 ScratchSizeKey = AMDGPU::ElfNote::AMDGPU_PAL_METADATA_ES_SCRATCH_SIZE;
997 break;
998 case CallingConv::AMDGPU_HS:
999 ScratchSizeKey = AMDGPU::ElfNote::AMDGPU_PAL_METADATA_HS_SCRATCH_SIZE;
1000 break;
1001 case CallingConv::AMDGPU_LS:
1002 ScratchSizeKey = AMDGPU::ElfNote::AMDGPU_PAL_METADATA_LS_SCRATCH_SIZE;
1003 break;
1004 }
1005 unsigned NumUsedVgprsKey = ScratchSizeKey
1006 + AMDGPU::ElfNote::AMDGPU_PAL_METADATA_VS_NUM_USED_VGPRS
1007 - AMDGPU::ElfNote::AMDGPU_PAL_METADATA_VS_SCRATCH_SIZE;
1008 unsigned NumUsedSgprsKey = ScratchSizeKey
1009 + AMDGPU::ElfNote::AMDGPU_PAL_METADATA_VS_NUM_USED_SGPRS
1010 - AMDGPU::ElfNote::AMDGPU_PAL_METADATA_VS_SCRATCH_SIZE;
1011 PalMetadata[NumUsedVgprsKey] = CurrentProgramInfo.NumVGPRsForWavesPerEU;
1012 PalMetadata[NumUsedSgprsKey] = CurrentProgramInfo.NumSGPRsForWavesPerEU;
1013 if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) {
1014 PalMetadata[Rsrc1Reg] |= CurrentProgramInfo.ComputePGMRSrc1;
1015 PalMetadata[Rsrc2Reg] |= CurrentProgramInfo.ComputePGMRSrc2;
1016 // ScratchSize is in bytes, 16 aligned.
1017 PalMetadata[ScratchSizeKey] |= alignTo(CurrentProgramInfo.ScratchSize, 16);
1018 } else {
1019 PalMetadata[Rsrc1Reg] |= S_00B028_VGPRS(CurrentProgramInfo.VGPRBlocks)
1020 | S_00B028_SGPRS(CurrentProgramInfo.SGPRBlocks);
1021 if (CurrentProgramInfo.ScratchBlocks > 0)
1022 PalMetadata[Rsrc2Reg] |= S_00B84C_SCRATCH_EN(1);
1023 // ScratchSize is in bytes, 16 aligned.
1024 PalMetadata[ScratchSizeKey] |= alignTo(CurrentProgramInfo.ScratchSize, 16);
1025 }
1026 if (MF.getFunction()->getCallingConv() == CallingConv::AMDGPU_PS) {
1027 PalMetadata[Rsrc2Reg] |= S_00B02C_EXTRA_LDS_SIZE(CurrentProgramInfo.LDSBlocks);
1028 PalMetadata[R_0286CC_SPI_PS_INPUT_ENA / 4] |= MFI->getPSInputEnable();
1029 PalMetadata[R_0286D0_SPI_PS_INPUT_ADDR / 4] |= MFI->getPSInputAddr();
1030 }
1031}
1032
Matt Arsenault24ee0782016-02-12 02:40:47 +00001033// This is supposed to be log2(Size)
1034static amd_element_byte_size_t getElementByteSizeValue(unsigned Size) {
1035 switch (Size) {
1036 case 4:
1037 return AMD_ELEMENT_4_BYTES;
1038 case 8:
1039 return AMD_ELEMENT_8_BYTES;
1040 case 16:
1041 return AMD_ELEMENT_16_BYTES;
1042 default:
1043 llvm_unreachable("invalid private_element_size");
1044 }
1045}
1046
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001047void AMDGPUAsmPrinter::getAmdKernelCode(amd_kernel_code_t &Out,
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001048 const SIProgramInfo &CurrentProgramInfo,
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001049 const MachineFunction &MF) const {
Tom Stellard45bb48e2015-06-13 03:28:10 +00001050 const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
Matt Arsenault43e92fe2016-06-24 06:30:11 +00001051 const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
Tom Stellard45bb48e2015-06-13 03:28:10 +00001052
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001053 AMDGPU::initDefaultAMDKernelCodeT(Out, STM.getFeatureBits());
Tom Stellard45bb48e2015-06-13 03:28:10 +00001054
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001055 Out.compute_pgm_resource_registers =
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001056 CurrentProgramInfo.ComputePGMRSrc1 |
1057 (CurrentProgramInfo.ComputePGMRSrc2 << 32);
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001058 Out.code_properties = AMD_CODE_PROPERTY_IS_PTR64;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001059
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001060 if (CurrentProgramInfo.DynamicCallStack)
1061 Out.code_properties |= AMD_CODE_PROPERTY_IS_DYNAMIC_CALLSTACK;
1062
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001063 AMD_HSA_BITS_SET(Out.code_properties,
Matt Arsenault24ee0782016-02-12 02:40:47 +00001064 AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE,
1065 getElementByteSizeValue(STM.getMaxPrivateElementSize()));
1066
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001067 if (MFI->hasPrivateSegmentBuffer()) {
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001068 Out.code_properties |=
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001069 AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER;
1070 }
1071
1072 if (MFI->hasDispatchPtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001073 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001074
1075 if (MFI->hasQueuePtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001076 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001077
1078 if (MFI->hasKernargSegmentPtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001079 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001080
1081 if (MFI->hasDispatchID())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001082 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001083
1084 if (MFI->hasFlatScratchInit())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001085 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT;
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001086
1087 if (MFI->hasGridWorkgroupCountX()) {
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001088 Out.code_properties |=
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001089 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X;
1090 }
1091
1092 if (MFI->hasGridWorkgroupCountY()) {
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001093 Out.code_properties |=
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001094 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y;
1095 }
1096
1097 if (MFI->hasGridWorkgroupCountZ()) {
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001098 Out.code_properties |=
Matt Arsenault26f8f3d2015-11-30 21:16:03 +00001099 AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z;
1100 }
Tom Stellard45bb48e2015-06-13 03:28:10 +00001101
Tom Stellard48f29f22015-11-26 00:43:29 +00001102 if (MFI->hasDispatchPtr())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001103 Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
Tom Stellard48f29f22015-11-26 00:43:29 +00001104
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001105 if (STM.debuggerSupported())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001106 Out.code_properties |= AMD_CODE_PROPERTY_IS_DEBUG_SUPPORTED;
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001107
Nicolai Haehnle5b504972016-01-04 23:35:53 +00001108 if (STM.isXNACKEnabled())
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001109 Out.code_properties |= AMD_CODE_PROPERTY_IS_XNACK_SUPPORTED;
Nicolai Haehnle5b504972016-01-04 23:35:53 +00001110
Matt Arsenault52ef4012016-07-26 16:45:58 +00001111 // FIXME: Should use getKernArgSize
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001112 Out.kernarg_segment_byte_size =
Tom Stellard2f3f9852017-01-25 01:25:13 +00001113 STM.getKernArgSegmentSize(MF, MFI->getABIArgOffset());
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001114 Out.wavefront_sgpr_count = CurrentProgramInfo.NumSGPR;
1115 Out.workitem_vgpr_count = CurrentProgramInfo.NumVGPR;
1116 Out.workitem_private_segment_byte_size = CurrentProgramInfo.ScratchSize;
1117 Out.workgroup_group_segment_byte_size = CurrentProgramInfo.LDSSize;
1118 Out.reserved_vgpr_first = CurrentProgramInfo.ReservedVGPRFirst;
1119 Out.reserved_vgpr_count = CurrentProgramInfo.ReservedVGPRCount;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001120
Tom Stellard175959e2016-12-06 21:53:10 +00001121 // These alignment values are specified in powers of two, so alignment =
1122 // 2^n. The minimum alignment is 2^4 = 16.
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001123 Out.kernarg_segment_alignment = std::max((size_t)4,
Tom Stellard175959e2016-12-06 21:53:10 +00001124 countTrailingZeros(MFI->getMaxKernArgAlign()));
1125
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001126 if (STM.debuggerEmitPrologue()) {
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001127 Out.debug_wavefront_private_segment_offset_sgpr =
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001128 CurrentProgramInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR;
Konstantin Zhuravlyovca0e7f62017-03-22 22:54:39 +00001129 Out.debug_private_segment_buffer_sgpr =
Matt Arsenaultb03dd8d2017-05-02 17:14:00 +00001130 CurrentProgramInfo.DebuggerPrivateSegmentBufferSGPR;
Konstantin Zhuravlyovf2f3d142016-06-25 03:11:28 +00001131 }
Tom Stellard45bb48e2015-06-13 03:28:10 +00001132}
1133
1134bool AMDGPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
1135 unsigned AsmVariant,
1136 const char *ExtraCode, raw_ostream &O) {
Matt Arsenault36cd1852017-08-09 20:09:35 +00001137 // First try the generic code, which knows about modifiers like 'c' and 'n'.
1138 if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O))
1139 return false;
1140
Tom Stellard45bb48e2015-06-13 03:28:10 +00001141 if (ExtraCode && ExtraCode[0]) {
1142 if (ExtraCode[1] != 0)
1143 return true; // Unknown modifier.
1144
1145 switch (ExtraCode[0]) {
Tom Stellard45bb48e2015-06-13 03:28:10 +00001146 case 'r':
1147 break;
Matt Arsenault36cd1852017-08-09 20:09:35 +00001148 default:
1149 return true;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001150 }
1151 }
1152
Matt Arsenault36cd1852017-08-09 20:09:35 +00001153 // TODO: Should be able to support other operand types like globals.
1154 const MachineOperand &MO = MI->getOperand(OpNo);
1155 if (MO.isReg()) {
1156 AMDGPUInstPrinter::printRegOperand(MO.getReg(), O,
1157 *MF->getSubtarget().getRegisterInfo());
1158 return false;
1159 }
1160
1161 return true;
Tom Stellard45bb48e2015-06-13 03:28:10 +00001162}