18f0fd8f6SDimitry Andric //===-- AMDGPUAsmPrinter.cpp - AMDGPU Assebly printer  --------------------===//
28f0fd8f6SDimitry Andric //
38f0fd8f6SDimitry Andric //                     The LLVM Compiler Infrastructure
48f0fd8f6SDimitry Andric //
58f0fd8f6SDimitry Andric // This file is distributed under the University of Illinois Open Source
68f0fd8f6SDimitry Andric // License. See LICENSE.TXT for details.
78f0fd8f6SDimitry Andric //
88f0fd8f6SDimitry Andric //===----------------------------------------------------------------------===//
98f0fd8f6SDimitry Andric //
108f0fd8f6SDimitry Andric /// \file
118f0fd8f6SDimitry Andric ///
128f0fd8f6SDimitry Andric /// The AMDGPUAsmPrinter is used to print both assembly string and also binary
138f0fd8f6SDimitry Andric /// code.  When passed an MCAsmStreamer it prints assembly and when passed
148f0fd8f6SDimitry Andric /// an MCObjectStreamer it outputs binary code.
158f0fd8f6SDimitry Andric //
168f0fd8f6SDimitry Andric //===----------------------------------------------------------------------===//
178f0fd8f6SDimitry Andric //
188f0fd8f6SDimitry Andric 
198f0fd8f6SDimitry Andric #include "AMDGPUAsmPrinter.h"
208f0fd8f6SDimitry Andric #include "AMDGPU.h"
218f0fd8f6SDimitry Andric #include "AMDGPUSubtarget.h"
22db17bf38SDimitry Andric #include "AMDGPUTargetMachine.h"
23db17bf38SDimitry Andric #include "InstPrinter/AMDGPUInstPrinter.h"
24db17bf38SDimitry Andric #include "MCTargetDesc/AMDGPUTargetStreamer.h"
258f0fd8f6SDimitry Andric #include "R600Defines.h"
268f0fd8f6SDimitry Andric #include "R600MachineFunctionInfo.h"
278f0fd8f6SDimitry Andric #include "R600RegisterInfo.h"
288f0fd8f6SDimitry Andric #include "SIDefines.h"
293ca95b02SDimitry Andric #include "SIInstrInfo.h"
30db17bf38SDimitry Andric #include "SIMachineFunctionInfo.h"
318f0fd8f6SDimitry Andric #include "SIRegisterInfo.h"
32db17bf38SDimitry Andric #include "Utils/AMDGPUBaseInfo.h"
33db17bf38SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
348f0fd8f6SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
353ca95b02SDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
368f0fd8f6SDimitry Andric #include "llvm/MC/MCContext.h"
378f0fd8f6SDimitry Andric #include "llvm/MC/MCSectionELF.h"
388f0fd8f6SDimitry Andric #include "llvm/MC/MCStreamer.h"
398f0fd8f6SDimitry Andric #include "llvm/Support/MathExtras.h"
408f0fd8f6SDimitry Andric #include "llvm/Support/TargetRegistry.h"
418f0fd8f6SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h"
428f0fd8f6SDimitry Andric 
438f0fd8f6SDimitry Andric using namespace llvm;
448f0fd8f6SDimitry Andric 
458f0fd8f6SDimitry Andric // TODO: This should get the default rounding mode from the kernel. We just set
468f0fd8f6SDimitry Andric // the default here, but this could change if the OpenCL rounding mode pragmas
478f0fd8f6SDimitry Andric // are used.
488f0fd8f6SDimitry Andric //
498f0fd8f6SDimitry Andric // The denormal mode here should match what is reported by the OpenCL runtime
508f0fd8f6SDimitry Andric // for the CL_FP_DENORM bit from CL_DEVICE_{HALF|SINGLE|DOUBLE}_FP_CONFIG, but
518f0fd8f6SDimitry Andric // can also be override to flush with the -cl-denorms-are-zero compiler flag.
528f0fd8f6SDimitry Andric //
538f0fd8f6SDimitry Andric // AMD OpenCL only sets flush none and reports CL_FP_DENORM for double
548f0fd8f6SDimitry Andric // precision, and leaves single precision to flush all and does not report
558f0fd8f6SDimitry Andric // CL_FP_DENORM for CL_DEVICE_SINGLE_FP_CONFIG. Mesa's OpenCL currently reports
568f0fd8f6SDimitry Andric // CL_FP_DENORM for both.
578f0fd8f6SDimitry Andric //
588f0fd8f6SDimitry Andric // FIXME: It seems some instructions do not support single precision denormals
598f0fd8f6SDimitry Andric // regardless of the mode (exp_*_f32, rcp_*_f32, rsq_*_f32, rsq_*f32, sqrt_f32,
608f0fd8f6SDimitry Andric // and sin_f32, cos_f32 on most parts).
618f0fd8f6SDimitry Andric 
628f0fd8f6SDimitry Andric // We want to use these instructions, and using fp32 denormals also causes
638f0fd8f6SDimitry Andric // instructions to run at the double precision rate for the device so it's
648f0fd8f6SDimitry Andric // probably best to just report no single precision denormals.
658f0fd8f6SDimitry Andric static uint32_t getFPMode(const MachineFunction &F) {
663ca95b02SDimitry Andric   const SISubtarget& ST = F.getSubtarget<SISubtarget>();
678f0fd8f6SDimitry Andric   // TODO: Is there any real use for the flush in only / flush out only modes?
688f0fd8f6SDimitry Andric 
698f0fd8f6SDimitry Andric   uint32_t FP32Denormals =
708f0fd8f6SDimitry Andric     ST.hasFP32Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
718f0fd8f6SDimitry Andric 
728f0fd8f6SDimitry Andric   uint32_t FP64Denormals =
738f0fd8f6SDimitry Andric     ST.hasFP64Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
748f0fd8f6SDimitry Andric 
758f0fd8f6SDimitry Andric   return FP_ROUND_MODE_SP(FP_ROUND_ROUND_TO_NEAREST) |
768f0fd8f6SDimitry Andric          FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_NEAREST) |
778f0fd8f6SDimitry Andric          FP_DENORM_MODE_SP(FP32Denormals) |
788f0fd8f6SDimitry Andric          FP_DENORM_MODE_DP(FP64Denormals);
798f0fd8f6SDimitry Andric }
808f0fd8f6SDimitry Andric 
818f0fd8f6SDimitry Andric static AsmPrinter *
828f0fd8f6SDimitry Andric createAMDGPUAsmPrinterPass(TargetMachine &tm,
838f0fd8f6SDimitry Andric                            std::unique_ptr<MCStreamer> &&Streamer) {
848f0fd8f6SDimitry Andric   return new AMDGPUAsmPrinter(tm, std::move(Streamer));
858f0fd8f6SDimitry Andric }
868f0fd8f6SDimitry Andric 
878f0fd8f6SDimitry Andric extern "C" void LLVMInitializeAMDGPUAsmPrinter() {
88d88c1a5aSDimitry Andric   TargetRegistry::RegisterAsmPrinter(getTheAMDGPUTarget(),
89d88c1a5aSDimitry Andric                                      createAMDGPUAsmPrinterPass);
90d88c1a5aSDimitry Andric   TargetRegistry::RegisterAsmPrinter(getTheGCNTarget(),
91d88c1a5aSDimitry Andric                                      createAMDGPUAsmPrinterPass);
928f0fd8f6SDimitry Andric }
938f0fd8f6SDimitry Andric 
948f0fd8f6SDimitry Andric AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM,
958f0fd8f6SDimitry Andric                                    std::unique_ptr<MCStreamer> Streamer)
967a7e6055SDimitry Andric   : AsmPrinter(TM, std::move(Streamer)) {
977a7e6055SDimitry Andric     AMDGPUASI = static_cast<AMDGPUTargetMachine*>(&TM)->getAMDGPUAS();
987a7e6055SDimitry Andric   }
998f0fd8f6SDimitry Andric 
100d88c1a5aSDimitry Andric StringRef AMDGPUAsmPrinter::getPassName() const {
101d88c1a5aSDimitry Andric   return "AMDGPU Assembly Printer";
102d88c1a5aSDimitry Andric }
103d88c1a5aSDimitry Andric 
1047a7e6055SDimitry Andric const MCSubtargetInfo* AMDGPUAsmPrinter::getSTI() const {
1057a7e6055SDimitry Andric   return TM.getMCSubtargetInfo();
1067a7e6055SDimitry Andric }
1077a7e6055SDimitry Andric 
1087a7e6055SDimitry Andric AMDGPUTargetStreamer& AMDGPUAsmPrinter::getTargetStreamer() const {
1097a7e6055SDimitry Andric   return static_cast<AMDGPUTargetStreamer&>(*OutStreamer->getTargetStreamer());
1107a7e6055SDimitry Andric }
1117a7e6055SDimitry Andric 
112444ed5c5SDimitry Andric void AMDGPUAsmPrinter::EmitStartOfAsmFile(Module &M) {
113444ed5c5SDimitry Andric   if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
114444ed5c5SDimitry Andric     return;
115444ed5c5SDimitry Andric 
1167a7e6055SDimitry Andric   AMDGPU::IsaInfo::IsaVersion ISA =
1177a7e6055SDimitry Andric       AMDGPU::IsaInfo::getIsaVersion(getSTI()->getFeatureBits());
118444ed5c5SDimitry Andric 
1197a7e6055SDimitry Andric   getTargetStreamer().EmitDirectiveHSACodeObjectVersion(2, 1);
1207a7e6055SDimitry Andric   getTargetStreamer().EmitDirectiveHSACodeObjectISA(
1217a7e6055SDimitry Andric       ISA.Major, ISA.Minor, ISA.Stepping, "AMD", "AMDGPU");
1227a7e6055SDimitry Andric   getTargetStreamer().EmitStartOfCodeObjectMetadata(M);
1237a7e6055SDimitry Andric }
124444ed5c5SDimitry Andric 
1257a7e6055SDimitry Andric void AMDGPUAsmPrinter::EmitEndOfAsmFile(Module &M) {
1267a7e6055SDimitry Andric   if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
1277a7e6055SDimitry Andric     return;
1283ca95b02SDimitry Andric 
1297a7e6055SDimitry Andric   getTargetStreamer().EmitEndOfCodeObjectMetadata();
130444ed5c5SDimitry Andric }
131444ed5c5SDimitry Andric 
132d88c1a5aSDimitry Andric bool AMDGPUAsmPrinter::isBlockOnlyReachableByFallthrough(
133d88c1a5aSDimitry Andric   const MachineBasicBlock *MBB) const {
134d88c1a5aSDimitry Andric   if (!AsmPrinter::isBlockOnlyReachableByFallthrough(MBB))
135d88c1a5aSDimitry Andric     return false;
136d88c1a5aSDimitry Andric 
137d88c1a5aSDimitry Andric   if (MBB->empty())
138d88c1a5aSDimitry Andric     return true;
139d88c1a5aSDimitry Andric 
140d88c1a5aSDimitry Andric   // If this is a block implementing a long branch, an expression relative to
141d88c1a5aSDimitry Andric   // the start of the block is needed.  to the start of the block.
142d88c1a5aSDimitry Andric   // XXX - Is there a smarter way to check this?
143d88c1a5aSDimitry Andric   return (MBB->back().getOpcode() != AMDGPU::S_SETPC_B64);
144d88c1a5aSDimitry Andric }
145d88c1a5aSDimitry Andric 
1463dac3a9bSDimitry Andric void AMDGPUAsmPrinter::EmitFunctionBodyStart() {
1476bc11b14SDimitry Andric   const AMDGPUMachineFunction *MFI = MF->getInfo<AMDGPUMachineFunction>();
1486bc11b14SDimitry Andric   if (!MFI->isEntryFunction())
1496bc11b14SDimitry Andric     return;
1506bc11b14SDimitry Andric 
1513dac3a9bSDimitry Andric   const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
1527a7e6055SDimitry Andric   amd_kernel_code_t KernelCode;
15398221d2eSDimitry Andric   if (STM.isAmdCodeObjectV2(*MF)) {
154f37b6182SDimitry Andric     getAmdKernelCode(KernelCode, CurrentProgramInfo, *MF);
1557a7e6055SDimitry Andric 
1567a7e6055SDimitry Andric     OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
1577a7e6055SDimitry Andric     getTargetStreamer().EmitAMDKernelCodeT(KernelCode);
1583dac3a9bSDimitry Andric   }
1597a7e6055SDimitry Andric 
1607a7e6055SDimitry Andric   if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
1617a7e6055SDimitry Andric     return;
1627a7e6055SDimitry Andric   getTargetStreamer().EmitKernelCodeObjectMetadata(*MF->getFunction(),
1637a7e6055SDimitry Andric                                                    KernelCode);
1643dac3a9bSDimitry Andric }
1653dac3a9bSDimitry Andric 
1667d523365SDimitry Andric void AMDGPUAsmPrinter::EmitFunctionEntryLabel() {
1677d523365SDimitry Andric   const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
1687d523365SDimitry Andric   const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
1697a7e6055SDimitry Andric   if (MFI->isEntryFunction() && STM.isAmdCodeObjectV2(*MF)) {
170d88c1a5aSDimitry Andric     SmallString<128> SymbolName;
171d88c1a5aSDimitry Andric     getNameWithPrefix(SymbolName, MF->getFunction()),
1727a7e6055SDimitry Andric     getTargetStreamer().EmitAMDGPUSymbolType(
1737a7e6055SDimitry Andric         SymbolName, ELF::STT_AMDGPU_HSA_KERNEL);
1747d523365SDimitry Andric   }
1758f0fd8f6SDimitry Andric 
1767d523365SDimitry Andric   AsmPrinter::EmitFunctionEntryLabel();
1777d523365SDimitry Andric }
1787d523365SDimitry Andric 
1797d523365SDimitry Andric void AMDGPUAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
1807d523365SDimitry Andric 
1817d523365SDimitry Andric   // Group segment variables aren't emitted in HSA.
1827a7e6055SDimitry Andric   if (AMDGPU::isGroupSegment(GV, AMDGPUASI))
1837d523365SDimitry Andric     return;
1847d523365SDimitry Andric 
1853ca95b02SDimitry Andric   AsmPrinter::EmitGlobalVariable(GV);
1868f0fd8f6SDimitry Andric }
1878f0fd8f6SDimitry Andric 
188f37b6182SDimitry Andric bool AMDGPUAsmPrinter::doFinalization(Module &M) {
189f37b6182SDimitry Andric   CallGraphResourceInfo.clear();
190f37b6182SDimitry Andric   return AsmPrinter::doFinalization(M);
191f37b6182SDimitry Andric }
192f37b6182SDimitry Andric 
193f37b6182SDimitry Andric // Print comments that apply to both callable functions and entry points.
194f37b6182SDimitry Andric void AMDGPUAsmPrinter::emitCommonFunctionComments(
195f37b6182SDimitry Andric   uint32_t NumVGPR,
196f37b6182SDimitry Andric   uint32_t NumSGPR,
197f37b6182SDimitry Andric   uint32_t ScratchSize,
198f37b6182SDimitry Andric   uint64_t CodeSize) {
199f37b6182SDimitry Andric   OutStreamer->emitRawComment(" codeLenInByte = " + Twine(CodeSize), false);
200f37b6182SDimitry Andric   OutStreamer->emitRawComment(" NumSgprs: " + Twine(NumSGPR), false);
201f37b6182SDimitry Andric   OutStreamer->emitRawComment(" NumVgprs: " + Twine(NumVGPR), false);
202f37b6182SDimitry Andric   OutStreamer->emitRawComment(" ScratchSize: " + Twine(ScratchSize), false);
203f37b6182SDimitry Andric }
204f37b6182SDimitry Andric 
2058f0fd8f6SDimitry Andric bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
206f37b6182SDimitry Andric   CurrentProgramInfo = SIProgramInfo();
207f37b6182SDimitry Andric 
2086bc11b14SDimitry Andric   const AMDGPUMachineFunction *MFI = MF.getInfo<AMDGPUMachineFunction>();
2098f0fd8f6SDimitry Andric 
2108f0fd8f6SDimitry Andric   // The starting address of all shader programs must be 256 bytes aligned.
2116bc11b14SDimitry Andric   // Regular functions just need the basic required instruction alignment.
2126bc11b14SDimitry Andric   MF.setAlignment(MFI->isEntryFunction() ? 8 : 2);
2138f0fd8f6SDimitry Andric 
2148f0fd8f6SDimitry Andric   SetupMachineFunction(MF);
2158f0fd8f6SDimitry Andric 
21695ec533aSDimitry Andric   const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
2178f0fd8f6SDimitry Andric   MCContext &Context = getObjFileLowering().getContext();
21895ec533aSDimitry Andric   if (!STM.isAmdHsaOS()) {
2198f0fd8f6SDimitry Andric     MCSectionELF *ConfigSection =
2208f0fd8f6SDimitry Andric         Context.getELFSection(".AMDGPU.config", ELF::SHT_PROGBITS, 0);
2218f0fd8f6SDimitry Andric     OutStreamer->SwitchSection(ConfigSection);
22295ec533aSDimitry Andric   }
2238f0fd8f6SDimitry Andric 
2243dac3a9bSDimitry Andric   if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
225f37b6182SDimitry Andric     if (MFI->isEntryFunction()) {
226f37b6182SDimitry Andric       getSIProgramInfo(CurrentProgramInfo, MF);
227f37b6182SDimitry Andric     } else {
228f37b6182SDimitry Andric       auto I = CallGraphResourceInfo.insert(
229f37b6182SDimitry Andric         std::make_pair(MF.getFunction(), SIFunctionResourceInfo()));
230f37b6182SDimitry Andric       SIFunctionResourceInfo &Info = I.first->second;
231f37b6182SDimitry Andric       assert(I.second && "should only be called once per function");
232f37b6182SDimitry Andric       Info = analyzeResourceUsage(MF);
233f37b6182SDimitry Andric     }
234f37b6182SDimitry Andric 
2357d523365SDimitry Andric     if (!STM.isAmdHsaOS()) {
236f37b6182SDimitry Andric       EmitProgramInfoSI(MF, CurrentProgramInfo);
2373dac3a9bSDimitry Andric     }
2388f0fd8f6SDimitry Andric   } else {
2398f0fd8f6SDimitry Andric     EmitProgramInfoR600(MF);
2408f0fd8f6SDimitry Andric   }
2418f0fd8f6SDimitry Andric 
2428f0fd8f6SDimitry Andric   DisasmLines.clear();
2438f0fd8f6SDimitry Andric   HexLines.clear();
2448f0fd8f6SDimitry Andric   DisasmLineMaxLen = 0;
2458f0fd8f6SDimitry Andric 
2468f0fd8f6SDimitry Andric   EmitFunctionBody();
2478f0fd8f6SDimitry Andric 
2488f0fd8f6SDimitry Andric   if (isVerbose()) {
2498f0fd8f6SDimitry Andric     MCSectionELF *CommentSection =
2508f0fd8f6SDimitry Andric         Context.getELFSection(".AMDGPU.csdata", ELF::SHT_PROGBITS, 0);
2518f0fd8f6SDimitry Andric     OutStreamer->SwitchSection(CommentSection);
2528f0fd8f6SDimitry Andric 
2538f0fd8f6SDimitry Andric     if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
254f37b6182SDimitry Andric       if (!MFI->isEntryFunction()) {
2556bc11b14SDimitry Andric         OutStreamer->emitRawComment(" Function info:", false);
256f37b6182SDimitry Andric         SIFunctionResourceInfo &Info = CallGraphResourceInfo[MF.getFunction()];
257f37b6182SDimitry Andric         emitCommonFunctionComments(
258f37b6182SDimitry Andric           Info.NumVGPR,
259f37b6182SDimitry Andric           Info.getTotalNumSGPRs(MF.getSubtarget<SISubtarget>()),
260f37b6182SDimitry Andric           Info.PrivateSegmentSize,
261f37b6182SDimitry Andric           getFunctionCodeSize(MF));
262f37b6182SDimitry Andric         return false;
2636bc11b14SDimitry Andric       }
2646bc11b14SDimitry Andric 
265f37b6182SDimitry Andric       OutStreamer->emitRawComment(" Kernel info:", false);
266f37b6182SDimitry Andric       emitCommonFunctionComments(CurrentProgramInfo.NumVGPR,
267f37b6182SDimitry Andric                                  CurrentProgramInfo.NumSGPR,
268f37b6182SDimitry Andric                                  CurrentProgramInfo.ScratchSize,
269f37b6182SDimitry Andric                                  getFunctionCodeSize(MF));
270f37b6182SDimitry Andric 
2716bc11b14SDimitry Andric       OutStreamer->emitRawComment(" codeLenInByte = " +
2726bc11b14SDimitry Andric                                   Twine(getFunctionCodeSize(MF)), false);
273f37b6182SDimitry Andric       OutStreamer->emitRawComment(
274f37b6182SDimitry Andric         " NumSgprs: " + Twine(CurrentProgramInfo.NumSGPR), false);
275f37b6182SDimitry Andric       OutStreamer->emitRawComment(
276f37b6182SDimitry Andric         " NumVgprs: " + Twine(CurrentProgramInfo.NumVGPR), false);
2776bc11b14SDimitry Andric 
278f37b6182SDimitry Andric       OutStreamer->emitRawComment(
279f37b6182SDimitry Andric         " FloatMode: " + Twine(CurrentProgramInfo.FloatMode), false);
280f37b6182SDimitry Andric       OutStreamer->emitRawComment(
281f37b6182SDimitry Andric         " IeeeMode: " + Twine(CurrentProgramInfo.IEEEMode), false);
282f37b6182SDimitry Andric       OutStreamer->emitRawComment(
283f37b6182SDimitry Andric         " ScratchSize: " + Twine(CurrentProgramInfo.ScratchSize), false);
284f37b6182SDimitry Andric       OutStreamer->emitRawComment(
285f37b6182SDimitry Andric         " LDSByteSize: " + Twine(CurrentProgramInfo.LDSSize) +
2863ca95b02SDimitry Andric         " bytes/workgroup (compile time only)", false);
2873ca95b02SDimitry Andric 
288f37b6182SDimitry Andric       OutStreamer->emitRawComment(
289f37b6182SDimitry Andric         " SGPRBlocks: " + Twine(CurrentProgramInfo.SGPRBlocks), false);
290f37b6182SDimitry Andric       OutStreamer->emitRawComment(
291f37b6182SDimitry Andric         " VGPRBlocks: " + Twine(CurrentProgramInfo.VGPRBlocks), false);
2926bc11b14SDimitry Andric 
293f37b6182SDimitry Andric       OutStreamer->emitRawComment(
294f37b6182SDimitry Andric         " NumSGPRsForWavesPerEU: " +
295f37b6182SDimitry Andric         Twine(CurrentProgramInfo.NumSGPRsForWavesPerEU), false);
296f37b6182SDimitry Andric       OutStreamer->emitRawComment(
297f37b6182SDimitry Andric         " NumVGPRsForWavesPerEU: " +
298f37b6182SDimitry Andric         Twine(CurrentProgramInfo.NumVGPRsForWavesPerEU), false);
299d88c1a5aSDimitry Andric 
300f37b6182SDimitry Andric       OutStreamer->emitRawComment(
301f37b6182SDimitry Andric         " ReservedVGPRFirst: " + Twine(CurrentProgramInfo.ReservedVGPRFirst),
3023ca95b02SDimitry Andric         false);
303f37b6182SDimitry Andric       OutStreamer->emitRawComment(
304f37b6182SDimitry Andric         " ReservedVGPRCount: " + Twine(CurrentProgramInfo.ReservedVGPRCount),
3053ca95b02SDimitry Andric         false);
3063ca95b02SDimitry Andric 
3073ca95b02SDimitry Andric       if (MF.getSubtarget<SISubtarget>().debuggerEmitPrologue()) {
308f37b6182SDimitry Andric         OutStreamer->emitRawComment(
309f37b6182SDimitry Andric           " DebuggerWavefrontPrivateSegmentOffsetSGPR: s" +
310f37b6182SDimitry Andric           Twine(CurrentProgramInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR), false);
311f37b6182SDimitry Andric         OutStreamer->emitRawComment(
312f37b6182SDimitry Andric           " DebuggerPrivateSegmentBufferSGPR: s" +
313f37b6182SDimitry Andric           Twine(CurrentProgramInfo.DebuggerPrivateSegmentBufferSGPR), false);
3143ca95b02SDimitry Andric       }
3157d523365SDimitry Andric 
316f37b6182SDimitry Andric       OutStreamer->emitRawComment(
317f37b6182SDimitry Andric         " COMPUTE_PGM_RSRC2:USER_SGPR: " +
318f37b6182SDimitry Andric         Twine(G_00B84C_USER_SGPR(CurrentProgramInfo.ComputePGMRSrc2)), false);
319f37b6182SDimitry Andric       OutStreamer->emitRawComment(
320f37b6182SDimitry Andric         " COMPUTE_PGM_RSRC2:TRAP_HANDLER: " +
321f37b6182SDimitry Andric         Twine(G_00B84C_TRAP_HANDLER(CurrentProgramInfo.ComputePGMRSrc2)), false);
322f37b6182SDimitry Andric       OutStreamer->emitRawComment(
323f37b6182SDimitry Andric         " COMPUTE_PGM_RSRC2:TGID_X_EN: " +
324f37b6182SDimitry Andric         Twine(G_00B84C_TGID_X_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
325f37b6182SDimitry Andric       OutStreamer->emitRawComment(
326f37b6182SDimitry Andric         " COMPUTE_PGM_RSRC2:TGID_Y_EN: " +
327f37b6182SDimitry Andric         Twine(G_00B84C_TGID_Y_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
328f37b6182SDimitry Andric       OutStreamer->emitRawComment(
329f37b6182SDimitry Andric         " COMPUTE_PGM_RSRC2:TGID_Z_EN: " +
330f37b6182SDimitry Andric         Twine(G_00B84C_TGID_Z_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
331f37b6182SDimitry Andric       OutStreamer->emitRawComment(
332f37b6182SDimitry Andric         " COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: " +
333f37b6182SDimitry Andric         Twine(G_00B84C_TIDIG_COMP_CNT(CurrentProgramInfo.ComputePGMRSrc2)),
3347d523365SDimitry Andric         false);
3358f0fd8f6SDimitry Andric     } else {
3368f0fd8f6SDimitry Andric       R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
3378f0fd8f6SDimitry Andric       OutStreamer->emitRawComment(
338d88c1a5aSDimitry Andric         Twine("SQ_PGM_RESOURCES:STACK_SIZE = " + Twine(MFI->CFStackSize)));
3398f0fd8f6SDimitry Andric     }
3408f0fd8f6SDimitry Andric   }
3418f0fd8f6SDimitry Andric 
3428f0fd8f6SDimitry Andric   if (STM.dumpCode()) {
3438f0fd8f6SDimitry Andric 
3448f0fd8f6SDimitry Andric     OutStreamer->SwitchSection(
3458f0fd8f6SDimitry Andric         Context.getELFSection(".AMDGPU.disasm", ELF::SHT_NOTE, 0));
3468f0fd8f6SDimitry Andric 
3478f0fd8f6SDimitry Andric     for (size_t i = 0; i < DisasmLines.size(); ++i) {
3488f0fd8f6SDimitry Andric       std::string Comment(DisasmLineMaxLen - DisasmLines[i].size(), ' ');
3498f0fd8f6SDimitry Andric       Comment += " ; " + HexLines[i] + "\n";
3508f0fd8f6SDimitry Andric 
3518f0fd8f6SDimitry Andric       OutStreamer->EmitBytes(StringRef(DisasmLines[i]));
3528f0fd8f6SDimitry Andric       OutStreamer->EmitBytes(StringRef(Comment));
3538f0fd8f6SDimitry Andric     }
3548f0fd8f6SDimitry Andric   }
3558f0fd8f6SDimitry Andric 
3568f0fd8f6SDimitry Andric   return false;
3578f0fd8f6SDimitry Andric }
3588f0fd8f6SDimitry Andric 
3598f0fd8f6SDimitry Andric void AMDGPUAsmPrinter::EmitProgramInfoR600(const MachineFunction &MF) {
3608f0fd8f6SDimitry Andric   unsigned MaxGPR = 0;
3618f0fd8f6SDimitry Andric   bool killPixel = false;
3623ca95b02SDimitry Andric   const R600Subtarget &STM = MF.getSubtarget<R600Subtarget>();
3633ca95b02SDimitry Andric   const R600RegisterInfo *RI = STM.getRegisterInfo();
3648f0fd8f6SDimitry Andric   const R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
3658f0fd8f6SDimitry Andric 
3668f0fd8f6SDimitry Andric   for (const MachineBasicBlock &MBB : MF) {
3678f0fd8f6SDimitry Andric     for (const MachineInstr &MI : MBB) {
3688f0fd8f6SDimitry Andric       if (MI.getOpcode() == AMDGPU::KILLGT)
3698f0fd8f6SDimitry Andric         killPixel = true;
3708f0fd8f6SDimitry Andric       unsigned numOperands = MI.getNumOperands();
3718f0fd8f6SDimitry Andric       for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
3728f0fd8f6SDimitry Andric         const MachineOperand &MO = MI.getOperand(op_idx);
3738f0fd8f6SDimitry Andric         if (!MO.isReg())
3748f0fd8f6SDimitry Andric           continue;
3756bc11b14SDimitry Andric         unsigned HWReg = RI->getHWRegIndex(MO.getReg());
3768f0fd8f6SDimitry Andric 
3778f0fd8f6SDimitry Andric         // Register with value > 127 aren't GPR
3788f0fd8f6SDimitry Andric         if (HWReg > 127)
3798f0fd8f6SDimitry Andric           continue;
3808f0fd8f6SDimitry Andric         MaxGPR = std::max(MaxGPR, HWReg);
3818f0fd8f6SDimitry Andric       }
3828f0fd8f6SDimitry Andric     }
3838f0fd8f6SDimitry Andric   }
3848f0fd8f6SDimitry Andric 
3858f0fd8f6SDimitry Andric   unsigned RsrcReg;
3863ca95b02SDimitry Andric   if (STM.getGeneration() >= R600Subtarget::EVERGREEN) {
3878f0fd8f6SDimitry Andric     // Evergreen / Northern Islands
3883ca95b02SDimitry Andric     switch (MF.getFunction()->getCallingConv()) {
389d88c1a5aSDimitry Andric     default: LLVM_FALLTHROUGH;
3903ca95b02SDimitry Andric     case CallingConv::AMDGPU_CS: RsrcReg = R_0288D4_SQ_PGM_RESOURCES_LS; break;
3913ca95b02SDimitry Andric     case CallingConv::AMDGPU_GS: RsrcReg = R_028878_SQ_PGM_RESOURCES_GS; break;
3923ca95b02SDimitry Andric     case CallingConv::AMDGPU_PS: RsrcReg = R_028844_SQ_PGM_RESOURCES_PS; break;
3933ca95b02SDimitry Andric     case CallingConv::AMDGPU_VS: RsrcReg = R_028860_SQ_PGM_RESOURCES_VS; break;
3948f0fd8f6SDimitry Andric     }
3958f0fd8f6SDimitry Andric   } else {
3968f0fd8f6SDimitry Andric     // R600 / R700
3973ca95b02SDimitry Andric     switch (MF.getFunction()->getCallingConv()) {
398d88c1a5aSDimitry Andric     default: LLVM_FALLTHROUGH;
399d88c1a5aSDimitry Andric     case CallingConv::AMDGPU_GS: LLVM_FALLTHROUGH;
400d88c1a5aSDimitry Andric     case CallingConv::AMDGPU_CS: LLVM_FALLTHROUGH;
4013ca95b02SDimitry Andric     case CallingConv::AMDGPU_VS: RsrcReg = R_028868_SQ_PGM_RESOURCES_VS; break;
4023ca95b02SDimitry Andric     case CallingConv::AMDGPU_PS: RsrcReg = R_028850_SQ_PGM_RESOURCES_PS; break;
4038f0fd8f6SDimitry Andric     }
4048f0fd8f6SDimitry Andric   }
4058f0fd8f6SDimitry Andric 
4068f0fd8f6SDimitry Andric   OutStreamer->EmitIntValue(RsrcReg, 4);
4078f0fd8f6SDimitry Andric   OutStreamer->EmitIntValue(S_NUM_GPRS(MaxGPR + 1) |
408d88c1a5aSDimitry Andric                            S_STACK_SIZE(MFI->CFStackSize), 4);
4098f0fd8f6SDimitry Andric   OutStreamer->EmitIntValue(R_02880C_DB_SHADER_CONTROL, 4);
4108f0fd8f6SDimitry Andric   OutStreamer->EmitIntValue(S_02880C_KILL_ENABLE(killPixel), 4);
4118f0fd8f6SDimitry Andric 
4123ca95b02SDimitry Andric   if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) {
4138f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_0288E8_SQ_LDS_ALLOC, 4);
414d88c1a5aSDimitry Andric     OutStreamer->EmitIntValue(alignTo(MFI->getLDSSize(), 4) >> 2, 4);
4158f0fd8f6SDimitry Andric   }
4168f0fd8f6SDimitry Andric }
4178f0fd8f6SDimitry Andric 
4186bc11b14SDimitry Andric uint64_t AMDGPUAsmPrinter::getFunctionCodeSize(const MachineFunction &MF) const {
4193ca95b02SDimitry Andric   const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
4203ca95b02SDimitry Andric   const SIInstrInfo *TII = STM.getInstrInfo();
4218f0fd8f6SDimitry Andric 
4226bc11b14SDimitry Andric   uint64_t CodeSize = 0;
4236bc11b14SDimitry Andric 
4248f0fd8f6SDimitry Andric   for (const MachineBasicBlock &MBB : MF) {
4258f0fd8f6SDimitry Andric     for (const MachineInstr &MI : MBB) {
4268f0fd8f6SDimitry Andric       // TODO: CodeSize should account for multiple functions.
4279a4b3118SDimitry Andric 
4289a4b3118SDimitry Andric       // TODO: Should we count size of debug info?
4299a4b3118SDimitry Andric       if (MI.isDebugValue())
4309a4b3118SDimitry Andric         continue;
4319a4b3118SDimitry Andric 
4323ca95b02SDimitry Andric       CodeSize += TII->getInstSizeInBytes(MI);
4336bc11b14SDimitry Andric     }
4346bc11b14SDimitry Andric   }
4358f0fd8f6SDimitry Andric 
4366bc11b14SDimitry Andric   return CodeSize;
4376bc11b14SDimitry Andric }
4388f0fd8f6SDimitry Andric 
4396bc11b14SDimitry Andric static bool hasAnyNonFlatUseOfReg(const MachineRegisterInfo &MRI,
4406bc11b14SDimitry Andric                                   const SIInstrInfo &TII,
4416bc11b14SDimitry Andric                                   unsigned Reg) {
4426bc11b14SDimitry Andric   for (const MachineOperand &UseOp : MRI.reg_operands(Reg)) {
4436bc11b14SDimitry Andric     if (!UseOp.isImplicit() || !TII.isFLAT(*UseOp.getParent()))
4446bc11b14SDimitry Andric       return true;
4456bc11b14SDimitry Andric   }
4468f0fd8f6SDimitry Andric 
4476bc11b14SDimitry Andric   return false;
4486bc11b14SDimitry Andric }
4497d523365SDimitry Andric 
450f37b6182SDimitry Andric static unsigned getNumExtraSGPRs(const SISubtarget &ST,
451f37b6182SDimitry Andric                                  bool VCCUsed,
452f37b6182SDimitry Andric                                  bool FlatScrUsed) {
453f37b6182SDimitry Andric   unsigned ExtraSGPRs = 0;
454f37b6182SDimitry Andric   if (VCCUsed)
455f37b6182SDimitry Andric     ExtraSGPRs = 2;
456f37b6182SDimitry Andric 
457f37b6182SDimitry Andric   if (ST.getGeneration() < SISubtarget::VOLCANIC_ISLANDS) {
458f37b6182SDimitry Andric     if (FlatScrUsed)
459f37b6182SDimitry Andric       ExtraSGPRs = 4;
460f37b6182SDimitry Andric   } else {
461f37b6182SDimitry Andric     if (ST.isXNACKEnabled())
462f37b6182SDimitry Andric       ExtraSGPRs = 4;
463f37b6182SDimitry Andric 
464f37b6182SDimitry Andric     if (FlatScrUsed)
465f37b6182SDimitry Andric       ExtraSGPRs = 6;
466f37b6182SDimitry Andric   }
467f37b6182SDimitry Andric 
468f37b6182SDimitry Andric   return ExtraSGPRs;
469f37b6182SDimitry Andric }
470f37b6182SDimitry Andric 
471f37b6182SDimitry Andric int32_t AMDGPUAsmPrinter::SIFunctionResourceInfo::getTotalNumSGPRs(
472f37b6182SDimitry Andric   const SISubtarget &ST) const {
473f37b6182SDimitry Andric   return NumExplicitSGPR + getNumExtraSGPRs(ST, UsesVCC, UsesFlatScratch);
474f37b6182SDimitry Andric }
475f37b6182SDimitry Andric 
476f37b6182SDimitry Andric AMDGPUAsmPrinter::SIFunctionResourceInfo AMDGPUAsmPrinter::analyzeResourceUsage(
4776bc11b14SDimitry Andric   const MachineFunction &MF) const {
478f37b6182SDimitry Andric   SIFunctionResourceInfo Info;
479f37b6182SDimitry Andric 
4806bc11b14SDimitry Andric   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
481f37b6182SDimitry Andric   const SISubtarget &ST = MF.getSubtarget<SISubtarget>();
482f37b6182SDimitry Andric   const MachineFrameInfo &FrameInfo = MF.getFrameInfo();
4836bc11b14SDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
484f37b6182SDimitry Andric   const SIInstrInfo *TII = ST.getInstrInfo();
485f37b6182SDimitry Andric   const SIRegisterInfo &TRI = TII->getRegisterInfo();
4867d523365SDimitry Andric 
487f37b6182SDimitry Andric   Info.UsesFlatScratch = MRI.isPhysRegUsed(AMDGPU::FLAT_SCR_LO) ||
488f37b6182SDimitry Andric                          MRI.isPhysRegUsed(AMDGPU::FLAT_SCR_HI);
4897d523365SDimitry Andric 
490f37b6182SDimitry Andric   // Even if FLAT_SCRATCH is implicitly used, it has no effect if flat
491f37b6182SDimitry Andric   // instructions aren't used to access the scratch buffer. Inline assembly may
492f37b6182SDimitry Andric   // need it though.
493f37b6182SDimitry Andric   //
494f37b6182SDimitry Andric   // If we only have implicit uses of flat_scr on flat instructions, it is not
495f37b6182SDimitry Andric   // really needed.
496f37b6182SDimitry Andric   if (Info.UsesFlatScratch && !MFI->hasFlatScratchInit() &&
497f37b6182SDimitry Andric       (!hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR) &&
498f37b6182SDimitry Andric        !hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR_LO) &&
499f37b6182SDimitry Andric        !hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR_HI))) {
500f37b6182SDimitry Andric     Info.UsesFlatScratch = false;
501f37b6182SDimitry Andric   }
502f37b6182SDimitry Andric 
503f37b6182SDimitry Andric   Info.HasDynamicallySizedStack = FrameInfo.hasVarSizedObjects();
504f37b6182SDimitry Andric   Info.PrivateSegmentSize = FrameInfo.getStackSize();
505f37b6182SDimitry Andric 
506f9448bf3SDimitry Andric 
507f37b6182SDimitry Andric   Info.UsesVCC = MRI.isPhysRegUsed(AMDGPU::VCC_LO) ||
508f37b6182SDimitry Andric                  MRI.isPhysRegUsed(AMDGPU::VCC_HI);
509f37b6182SDimitry Andric 
510f37b6182SDimitry Andric   // If there are no calls, MachineRegisterInfo can tell us the used register
511f37b6182SDimitry Andric   // count easily.
512f37b6182SDimitry Andric 
513f37b6182SDimitry Andric   MCPhysReg HighestVGPRReg = AMDGPU::NoRegister;
5146bc11b14SDimitry Andric   for (MCPhysReg Reg : reverse(AMDGPU::VGPR_32RegClass.getRegisters())) {
5156bc11b14SDimitry Andric     if (MRI.isPhysRegUsed(Reg)) {
516f37b6182SDimitry Andric       HighestVGPRReg = Reg;
5177d523365SDimitry Andric       break;
5188f0fd8f6SDimitry Andric     }
5196bc11b14SDimitry Andric   }
5208f0fd8f6SDimitry Andric 
521f37b6182SDimitry Andric   MCPhysReg HighestSGPRReg = AMDGPU::NoRegister;
5226bc11b14SDimitry Andric   for (MCPhysReg Reg : reverse(AMDGPU::SGPR_32RegClass.getRegisters())) {
5236bc11b14SDimitry Andric     if (MRI.isPhysRegUsed(Reg)) {
524f37b6182SDimitry Andric       HighestSGPRReg = Reg;
5256bc11b14SDimitry Andric       break;
5268f0fd8f6SDimitry Andric     }
5278f0fd8f6SDimitry Andric   }
5288f0fd8f6SDimitry Andric 
5296bc11b14SDimitry Andric   // We found the maximum register index. They start at 0, so add one to get the
5306bc11b14SDimitry Andric   // number of registers.
531f37b6182SDimitry Andric   Info.NumVGPR = HighestVGPRReg == AMDGPU::NoRegister ? 0 :
532f37b6182SDimitry Andric     TRI.getHWRegIndex(HighestVGPRReg) + 1;
533f37b6182SDimitry Andric   Info.NumExplicitSGPR = HighestSGPRReg == AMDGPU::NoRegister ? 0 :
534f37b6182SDimitry Andric     TRI.getHWRegIndex(HighestSGPRReg) + 1;
535444ed5c5SDimitry Andric 
536f37b6182SDimitry Andric   return Info;
5376bc11b14SDimitry Andric }
5386bc11b14SDimitry Andric 
539f37b6182SDimitry Andric void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo,
540f37b6182SDimitry Andric                                         const MachineFunction &MF) {
541f37b6182SDimitry Andric   SIFunctionResourceInfo Info = analyzeResourceUsage(MF);
542f37b6182SDimitry Andric 
543f37b6182SDimitry Andric   ProgInfo.NumVGPR = Info.NumVGPR;
544f37b6182SDimitry Andric   ProgInfo.NumSGPR = Info.NumExplicitSGPR;
545f37b6182SDimitry Andric   ProgInfo.ScratchSize = Info.PrivateSegmentSize;
546f37b6182SDimitry Andric   ProgInfo.VCCUsed = Info.UsesVCC;
547f37b6182SDimitry Andric   ProgInfo.FlatUsed = Info.UsesFlatScratch;
548f37b6182SDimitry Andric   ProgInfo.DynamicCallStack = Info.HasDynamicallySizedStack || Info.HasRecursion;
549f37b6182SDimitry Andric 
550f37b6182SDimitry Andric   const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
551f37b6182SDimitry Andric   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
552f37b6182SDimitry Andric   const SIInstrInfo *TII = STM.getInstrInfo();
553f37b6182SDimitry Andric   const SIRegisterInfo *RI = &TII->getRegisterInfo();
554f37b6182SDimitry Andric 
555f37b6182SDimitry Andric   unsigned ExtraSGPRs = getNumExtraSGPRs(STM,
556f37b6182SDimitry Andric                                          ProgInfo.VCCUsed,
557f37b6182SDimitry Andric                                          ProgInfo.FlatUsed);
5587a7e6055SDimitry Andric   unsigned ExtraVGPRs = STM.getReservedNumVGPRs(MF);
5593ca95b02SDimitry Andric 
560d88c1a5aSDimitry Andric   // Check the addressable register limit before we add ExtraSGPRs.
561d88c1a5aSDimitry Andric   if (STM.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS &&
562d88c1a5aSDimitry Andric       !STM.hasSGPRInitBug()) {
5637a7e6055SDimitry Andric     unsigned MaxAddressableNumSGPRs = STM.getAddressableNumSGPRs();
5646bc11b14SDimitry Andric     if (ProgInfo.NumSGPR > MaxAddressableNumSGPRs) {
565d88c1a5aSDimitry Andric       // This can happen due to a compiler bug or when using inline asm.
566d88c1a5aSDimitry Andric       LLVMContext &Ctx = MF.getFunction()->getContext();
567d88c1a5aSDimitry Andric       DiagnosticInfoResourceLimit Diag(*MF.getFunction(),
568d88c1a5aSDimitry Andric                                        "addressable scalar registers",
5696bc11b14SDimitry Andric                                        ProgInfo.NumSGPR, DS_Error,
5707a7e6055SDimitry Andric                                        DK_ResourceLimit,
5717a7e6055SDimitry Andric                                        MaxAddressableNumSGPRs);
572d88c1a5aSDimitry Andric       Ctx.diagnose(Diag);
5736bc11b14SDimitry Andric       ProgInfo.NumSGPR = MaxAddressableNumSGPRs - 1;
574d88c1a5aSDimitry Andric     }
575d88c1a5aSDimitry Andric   }
576d88c1a5aSDimitry Andric 
577d88c1a5aSDimitry Andric   // Account for extra SGPRs and VGPRs reserved for debugger use.
5786bc11b14SDimitry Andric   ProgInfo.NumSGPR += ExtraSGPRs;
5796bc11b14SDimitry Andric   ProgInfo.NumVGPR += ExtraVGPRs;
5808f0fd8f6SDimitry Andric 
581d88c1a5aSDimitry Andric   // Adjust number of registers used to meet default/requested minimum/maximum
582d88c1a5aSDimitry Andric   // number of waves per execution unit request.
583d88c1a5aSDimitry Andric   ProgInfo.NumSGPRsForWavesPerEU = std::max(
5846bc11b14SDimitry Andric     std::max(ProgInfo.NumSGPR, 1u), STM.getMinNumSGPRs(MFI->getMaxWavesPerEU()));
585d88c1a5aSDimitry Andric   ProgInfo.NumVGPRsForWavesPerEU = std::max(
5866bc11b14SDimitry Andric     std::max(ProgInfo.NumVGPR, 1u), STM.getMinNumVGPRs(MFI->getMaxWavesPerEU()));
587d88c1a5aSDimitry Andric 
588d88c1a5aSDimitry Andric   if (STM.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ||
589d88c1a5aSDimitry Andric       STM.hasSGPRInitBug()) {
5907a7e6055SDimitry Andric     unsigned MaxAddressableNumSGPRs = STM.getAddressableNumSGPRs();
5917a7e6055SDimitry Andric     if (ProgInfo.NumSGPR > MaxAddressableNumSGPRs) {
5927a7e6055SDimitry Andric       // This can happen due to a compiler bug or when using inline asm to use
5937a7e6055SDimitry Andric       // the registers which are usually reserved for vcc etc.
5948f0fd8f6SDimitry Andric       LLVMContext &Ctx = MF.getFunction()->getContext();
5953ca95b02SDimitry Andric       DiagnosticInfoResourceLimit Diag(*MF.getFunction(),
596d88c1a5aSDimitry Andric                                        "scalar registers",
597d88c1a5aSDimitry Andric                                        ProgInfo.NumSGPR, DS_Error,
5987a7e6055SDimitry Andric                                        DK_ResourceLimit,
5997a7e6055SDimitry Andric                                        MaxAddressableNumSGPRs);
6003ca95b02SDimitry Andric       Ctx.diagnose(Diag);
6017a7e6055SDimitry Andric       ProgInfo.NumSGPR = MaxAddressableNumSGPRs;
6027a7e6055SDimitry Andric       ProgInfo.NumSGPRsForWavesPerEU = MaxAddressableNumSGPRs;
603d88c1a5aSDimitry Andric     }
6048f0fd8f6SDimitry Andric   }
6058f0fd8f6SDimitry Andric 
606d88c1a5aSDimitry Andric   if (STM.hasSGPRInitBug()) {
6077a7e6055SDimitry Andric     ProgInfo.NumSGPR =
6087a7e6055SDimitry Andric         AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG;
6097a7e6055SDimitry Andric     ProgInfo.NumSGPRsForWavesPerEU =
6107a7e6055SDimitry Andric         AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG;
6118f0fd8f6SDimitry Andric   }
6128f0fd8f6SDimitry Andric 
6136bc11b14SDimitry Andric   if (MFI->getNumUserSGPRs() > STM.getMaxNumUserSGPRs()) {
6147d523365SDimitry Andric     LLVMContext &Ctx = MF.getFunction()->getContext();
6153ca95b02SDimitry Andric     DiagnosticInfoResourceLimit Diag(*MF.getFunction(), "user SGPRs",
6166bc11b14SDimitry Andric                                      MFI->getNumUserSGPRs(), DS_Error);
6173ca95b02SDimitry Andric     Ctx.diagnose(Diag);
6183ca95b02SDimitry Andric   }
6193ca95b02SDimitry Andric 
620d88c1a5aSDimitry Andric   if (MFI->getLDSSize() > static_cast<unsigned>(STM.getLocalMemorySize())) {
6213ca95b02SDimitry Andric     LLVMContext &Ctx = MF.getFunction()->getContext();
6223ca95b02SDimitry Andric     DiagnosticInfoResourceLimit Diag(*MF.getFunction(), "local memory",
623d88c1a5aSDimitry Andric                                      MFI->getLDSSize(), DS_Error);
6243ca95b02SDimitry Andric     Ctx.diagnose(Diag);
6257d523365SDimitry Andric   }
6267d523365SDimitry Andric 
627d88c1a5aSDimitry Andric   // SGPRBlocks is actual number of SGPR blocks minus 1.
628d88c1a5aSDimitry Andric   ProgInfo.SGPRBlocks = alignTo(ProgInfo.NumSGPRsForWavesPerEU,
6297a7e6055SDimitry Andric                                 STM.getSGPREncodingGranule());
6307a7e6055SDimitry Andric   ProgInfo.SGPRBlocks = ProgInfo.SGPRBlocks / STM.getSGPREncodingGranule() - 1;
631d88c1a5aSDimitry Andric 
632d88c1a5aSDimitry Andric   // VGPRBlocks is actual number of VGPR blocks minus 1.
633d88c1a5aSDimitry Andric   ProgInfo.VGPRBlocks = alignTo(ProgInfo.NumVGPRsForWavesPerEU,
6347a7e6055SDimitry Andric                                 STM.getVGPREncodingGranule());
6357a7e6055SDimitry Andric   ProgInfo.VGPRBlocks = ProgInfo.VGPRBlocks / STM.getVGPREncodingGranule() - 1;
6367a7e6055SDimitry Andric 
6377a7e6055SDimitry Andric   // Record first reserved VGPR and number of reserved VGPRs.
6386bc11b14SDimitry Andric   ProgInfo.ReservedVGPRFirst = STM.debuggerReserveRegs() ? ProgInfo.NumVGPR : 0;
6397a7e6055SDimitry Andric   ProgInfo.ReservedVGPRCount = STM.getReservedNumVGPRs(MF);
6407a7e6055SDimitry Andric 
6417a7e6055SDimitry Andric   // Update DebuggerWavefrontPrivateSegmentOffsetSGPR and
6427a7e6055SDimitry Andric   // DebuggerPrivateSegmentBufferSGPR fields if "amdgpu-debugger-emit-prologue"
6437a7e6055SDimitry Andric   // attribute was requested.
6447a7e6055SDimitry Andric   if (STM.debuggerEmitPrologue()) {
6457a7e6055SDimitry Andric     ProgInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR =
6467a7e6055SDimitry Andric       RI->getHWRegIndex(MFI->getScratchWaveOffsetReg());
6477a7e6055SDimitry Andric     ProgInfo.DebuggerPrivateSegmentBufferSGPR =
6487a7e6055SDimitry Andric       RI->getHWRegIndex(MFI->getScratchRSrcReg());
6497a7e6055SDimitry Andric   }
650d88c1a5aSDimitry Andric 
6518f0fd8f6SDimitry Andric   // Set the value to initialize FP_ROUND and FP_DENORM parts of the mode
6528f0fd8f6SDimitry Andric   // register.
6538f0fd8f6SDimitry Andric   ProgInfo.FloatMode = getFPMode(MF);
6548f0fd8f6SDimitry Andric 
655d88c1a5aSDimitry Andric   ProgInfo.IEEEMode = STM.enableIEEEBit(MF);
6568f0fd8f6SDimitry Andric 
6573ca95b02SDimitry Andric   // Make clamp modifier on NaN input returns 0.
6587a7e6055SDimitry Andric   ProgInfo.DX10Clamp = STM.enableDX10Clamp();
6598f0fd8f6SDimitry Andric 
6608f0fd8f6SDimitry Andric   unsigned LDSAlignShift;
6613ca95b02SDimitry Andric   if (STM.getGeneration() < SISubtarget::SEA_ISLANDS) {
6628f0fd8f6SDimitry Andric     // LDS is allocated in 64 dword blocks.
6638f0fd8f6SDimitry Andric     LDSAlignShift = 8;
6648f0fd8f6SDimitry Andric   } else {
6658f0fd8f6SDimitry Andric     // LDS is allocated in 128 dword blocks.
6668f0fd8f6SDimitry Andric     LDSAlignShift = 9;
6678f0fd8f6SDimitry Andric   }
6688f0fd8f6SDimitry Andric 
669d88c1a5aSDimitry Andric   unsigned LDSSpillSize =
6706bc11b14SDimitry Andric     MFI->getLDSWaveSpillSize() * MFI->getMaxFlatWorkGroupSize();
6718f0fd8f6SDimitry Andric 
672d88c1a5aSDimitry Andric   ProgInfo.LDSSize = MFI->getLDSSize() + LDSSpillSize;
6738f0fd8f6SDimitry Andric   ProgInfo.LDSBlocks =
6743ca95b02SDimitry Andric       alignTo(ProgInfo.LDSSize, 1ULL << LDSAlignShift) >> LDSAlignShift;
6758f0fd8f6SDimitry Andric 
6768f0fd8f6SDimitry Andric   // Scratch is allocated in 256 dword blocks.
6778f0fd8f6SDimitry Andric   unsigned ScratchAlignShift = 10;
6788f0fd8f6SDimitry Andric   // We need to program the hardware with the amount of scratch memory that
6798f0fd8f6SDimitry Andric   // is used by the entire wave.  ProgInfo.ScratchSize is the amount of
6808f0fd8f6SDimitry Andric   // scratch memory used per thread.
6818f0fd8f6SDimitry Andric   ProgInfo.ScratchBlocks =
6823ca95b02SDimitry Andric       alignTo(ProgInfo.ScratchSize * STM.getWavefrontSize(),
6833ca95b02SDimitry Andric               1ULL << ScratchAlignShift) >>
6843ca95b02SDimitry Andric       ScratchAlignShift;
6858f0fd8f6SDimitry Andric 
6868f0fd8f6SDimitry Andric   ProgInfo.ComputePGMRSrc1 =
6878f0fd8f6SDimitry Andric       S_00B848_VGPRS(ProgInfo.VGPRBlocks) |
6888f0fd8f6SDimitry Andric       S_00B848_SGPRS(ProgInfo.SGPRBlocks) |
6898f0fd8f6SDimitry Andric       S_00B848_PRIORITY(ProgInfo.Priority) |
6908f0fd8f6SDimitry Andric       S_00B848_FLOAT_MODE(ProgInfo.FloatMode) |
6918f0fd8f6SDimitry Andric       S_00B848_PRIV(ProgInfo.Priv) |
6928f0fd8f6SDimitry Andric       S_00B848_DX10_CLAMP(ProgInfo.DX10Clamp) |
6937d523365SDimitry Andric       S_00B848_DEBUG_MODE(ProgInfo.DebugMode) |
6948f0fd8f6SDimitry Andric       S_00B848_IEEE_MODE(ProgInfo.IEEEMode);
6958f0fd8f6SDimitry Andric 
6967d523365SDimitry Andric   // 0 = X, 1 = XY, 2 = XYZ
6977d523365SDimitry Andric   unsigned TIDIGCompCnt = 0;
6987d523365SDimitry Andric   if (MFI->hasWorkItemIDZ())
6997d523365SDimitry Andric     TIDIGCompCnt = 2;
7007d523365SDimitry Andric   else if (MFI->hasWorkItemIDY())
7017d523365SDimitry Andric     TIDIGCompCnt = 1;
7027d523365SDimitry Andric 
7038f0fd8f6SDimitry Andric   ProgInfo.ComputePGMRSrc2 =
7048f0fd8f6SDimitry Andric       S_00B84C_SCRATCH_EN(ProgInfo.ScratchBlocks > 0) |
7057d523365SDimitry Andric       S_00B84C_USER_SGPR(MFI->getNumUserSGPRs()) |
7067a7e6055SDimitry Andric       S_00B84C_TRAP_HANDLER(STM.isTrapHandlerEnabled()) |
7077d523365SDimitry Andric       S_00B84C_TGID_X_EN(MFI->hasWorkGroupIDX()) |
7087d523365SDimitry Andric       S_00B84C_TGID_Y_EN(MFI->hasWorkGroupIDY()) |
7097d523365SDimitry Andric       S_00B84C_TGID_Z_EN(MFI->hasWorkGroupIDZ()) |
7107d523365SDimitry Andric       S_00B84C_TG_SIZE_EN(MFI->hasWorkGroupInfo()) |
7117d523365SDimitry Andric       S_00B84C_TIDIG_COMP_CNT(TIDIGCompCnt) |
7127d523365SDimitry Andric       S_00B84C_EXCP_EN_MSB(0) |
7130f5676f4SDimitry Andric       // For AMDHSA, LDS_SIZE must be zero, as it is populated by the CP.
7140f5676f4SDimitry Andric       S_00B84C_LDS_SIZE(STM.isAmdHsaOS() ? 0 : ProgInfo.LDSBlocks) |
7157d523365SDimitry Andric       S_00B84C_EXCP_EN(0);
7168f0fd8f6SDimitry Andric }
7178f0fd8f6SDimitry Andric 
7183ca95b02SDimitry Andric static unsigned getRsrcReg(CallingConv::ID CallConv) {
7193ca95b02SDimitry Andric   switch (CallConv) {
720d88c1a5aSDimitry Andric   default: LLVM_FALLTHROUGH;
7213ca95b02SDimitry Andric   case CallingConv::AMDGPU_CS: return R_00B848_COMPUTE_PGM_RSRC1;
722f37b6182SDimitry Andric   case CallingConv::AMDGPU_HS: return R_00B428_SPI_SHADER_PGM_RSRC1_HS;
7233ca95b02SDimitry Andric   case CallingConv::AMDGPU_GS: return R_00B228_SPI_SHADER_PGM_RSRC1_GS;
7243ca95b02SDimitry Andric   case CallingConv::AMDGPU_PS: return R_00B028_SPI_SHADER_PGM_RSRC1_PS;
7253ca95b02SDimitry Andric   case CallingConv::AMDGPU_VS: return R_00B128_SPI_SHADER_PGM_RSRC1_VS;
7268f0fd8f6SDimitry Andric   }
7278f0fd8f6SDimitry Andric }
7288f0fd8f6SDimitry Andric 
7298f0fd8f6SDimitry Andric void AMDGPUAsmPrinter::EmitProgramInfoSI(const MachineFunction &MF,
730f37b6182SDimitry Andric                                          const SIProgramInfo &CurrentProgramInfo) {
7313ca95b02SDimitry Andric   const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
7328f0fd8f6SDimitry Andric   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
7333ca95b02SDimitry Andric   unsigned RsrcReg = getRsrcReg(MF.getFunction()->getCallingConv());
7348f0fd8f6SDimitry Andric 
7353ca95b02SDimitry Andric   if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) {
7368f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_00B848_COMPUTE_PGM_RSRC1, 4);
7378f0fd8f6SDimitry Andric 
738f37b6182SDimitry Andric     OutStreamer->EmitIntValue(CurrentProgramInfo.ComputePGMRSrc1, 4);
7398f0fd8f6SDimitry Andric 
7408f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4);
741f37b6182SDimitry Andric     OutStreamer->EmitIntValue(CurrentProgramInfo.ComputePGMRSrc2, 4);
7428f0fd8f6SDimitry Andric 
7438f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_00B860_COMPUTE_TMPRING_SIZE, 4);
744f37b6182SDimitry Andric     OutStreamer->EmitIntValue(S_00B860_WAVESIZE(CurrentProgramInfo.ScratchBlocks), 4);
7458f0fd8f6SDimitry Andric 
7468f0fd8f6SDimitry Andric     // TODO: Should probably note flat usage somewhere. SC emits a "FlatPtr32 =
7478f0fd8f6SDimitry Andric     // 0" comment but I don't see a corresponding field in the register spec.
7488f0fd8f6SDimitry Andric   } else {
7498f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(RsrcReg, 4);
750f37b6182SDimitry Andric     OutStreamer->EmitIntValue(S_00B028_VGPRS(CurrentProgramInfo.VGPRBlocks) |
751f37b6182SDimitry Andric                               S_00B028_SGPRS(CurrentProgramInfo.SGPRBlocks), 4);
7523ca95b02SDimitry Andric     if (STM.isVGPRSpillingEnabled(*MF.getFunction())) {
7538f0fd8f6SDimitry Andric       OutStreamer->EmitIntValue(R_0286E8_SPI_TMPRING_SIZE, 4);
754f37b6182SDimitry Andric       OutStreamer->EmitIntValue(S_0286E8_WAVESIZE(CurrentProgramInfo.ScratchBlocks), 4);
7558f0fd8f6SDimitry Andric     }
7568f0fd8f6SDimitry Andric   }
7578f0fd8f6SDimitry Andric 
7583ca95b02SDimitry Andric   if (MF.getFunction()->getCallingConv() == CallingConv::AMDGPU_PS) {
7598f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4);
760f37b6182SDimitry Andric     OutStreamer->EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(CurrentProgramInfo.LDSBlocks), 4);
7618f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4);
7627a7e6055SDimitry Andric     OutStreamer->EmitIntValue(MFI->getPSInputEnable(), 4);
763444ed5c5SDimitry Andric     OutStreamer->EmitIntValue(R_0286D0_SPI_PS_INPUT_ADDR, 4);
764444ed5c5SDimitry Andric     OutStreamer->EmitIntValue(MFI->getPSInputAddr(), 4);
7658f0fd8f6SDimitry Andric   }
7663ca95b02SDimitry Andric 
7673ca95b02SDimitry Andric   OutStreamer->EmitIntValue(R_SPILLED_SGPRS, 4);
7683ca95b02SDimitry Andric   OutStreamer->EmitIntValue(MFI->getNumSpilledSGPRs(), 4);
7693ca95b02SDimitry Andric   OutStreamer->EmitIntValue(R_SPILLED_VGPRS, 4);
7703ca95b02SDimitry Andric   OutStreamer->EmitIntValue(MFI->getNumSpilledVGPRs(), 4);
7713ca95b02SDimitry Andric }
7723ca95b02SDimitry Andric 
7733ca95b02SDimitry Andric // This is supposed to be log2(Size)
7743ca95b02SDimitry Andric static amd_element_byte_size_t getElementByteSizeValue(unsigned Size) {
7753ca95b02SDimitry Andric   switch (Size) {
7763ca95b02SDimitry Andric   case 4:
7773ca95b02SDimitry Andric     return AMD_ELEMENT_4_BYTES;
7783ca95b02SDimitry Andric   case 8:
7793ca95b02SDimitry Andric     return AMD_ELEMENT_8_BYTES;
7803ca95b02SDimitry Andric   case 16:
7813ca95b02SDimitry Andric     return AMD_ELEMENT_16_BYTES;
7823ca95b02SDimitry Andric   default:
7833ca95b02SDimitry Andric     llvm_unreachable("invalid private_element_size");
7843ca95b02SDimitry Andric   }
7858f0fd8f6SDimitry Andric }
7868f0fd8f6SDimitry Andric 
7877a7e6055SDimitry Andric void AMDGPUAsmPrinter::getAmdKernelCode(amd_kernel_code_t &Out,
788f37b6182SDimitry Andric                                         const SIProgramInfo &CurrentProgramInfo,
7897a7e6055SDimitry Andric                                         const MachineFunction &MF) const {
7908f0fd8f6SDimitry Andric   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
7913ca95b02SDimitry Andric   const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
7928f0fd8f6SDimitry Andric 
7937a7e6055SDimitry Andric   AMDGPU::initDefaultAMDKernelCodeT(Out, STM.getFeatureBits());
7948f0fd8f6SDimitry Andric 
7957a7e6055SDimitry Andric   Out.compute_pgm_resource_registers =
796f37b6182SDimitry Andric       CurrentProgramInfo.ComputePGMRSrc1 |
797f37b6182SDimitry Andric       (CurrentProgramInfo.ComputePGMRSrc2 << 32);
7987a7e6055SDimitry Andric   Out.code_properties = AMD_CODE_PROPERTY_IS_PTR64;
7997d523365SDimitry Andric 
800f37b6182SDimitry Andric   if (CurrentProgramInfo.DynamicCallStack)
801f37b6182SDimitry Andric     Out.code_properties |= AMD_CODE_PROPERTY_IS_DYNAMIC_CALLSTACK;
802f37b6182SDimitry Andric 
8037a7e6055SDimitry Andric   AMD_HSA_BITS_SET(Out.code_properties,
8043ca95b02SDimitry Andric                    AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE,
8053ca95b02SDimitry Andric                    getElementByteSizeValue(STM.getMaxPrivateElementSize()));
8063ca95b02SDimitry Andric 
8077d523365SDimitry Andric   if (MFI->hasPrivateSegmentBuffer()) {
8087a7e6055SDimitry Andric     Out.code_properties |=
8097d523365SDimitry Andric       AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER;
8107d523365SDimitry Andric   }
8117d523365SDimitry Andric 
8127d523365SDimitry Andric   if (MFI->hasDispatchPtr())
8137a7e6055SDimitry Andric     Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
8147d523365SDimitry Andric 
8157d523365SDimitry Andric   if (MFI->hasQueuePtr())
8167a7e6055SDimitry Andric     Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR;
8177d523365SDimitry Andric 
8187d523365SDimitry Andric   if (MFI->hasKernargSegmentPtr())
8197a7e6055SDimitry Andric     Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR;
8207d523365SDimitry Andric 
8217d523365SDimitry Andric   if (MFI->hasDispatchID())
8227a7e6055SDimitry Andric     Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID;
8237d523365SDimitry Andric 
8247d523365SDimitry Andric   if (MFI->hasFlatScratchInit())
8257a7e6055SDimitry Andric     Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT;
8267d523365SDimitry Andric 
8277d523365SDimitry Andric   if (MFI->hasGridWorkgroupCountX()) {
8287a7e6055SDimitry Andric     Out.code_properties |=
8297d523365SDimitry Andric       AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X;
8307d523365SDimitry Andric   }
8317d523365SDimitry Andric 
8327d523365SDimitry Andric   if (MFI->hasGridWorkgroupCountY()) {
8337a7e6055SDimitry Andric     Out.code_properties |=
8347d523365SDimitry Andric       AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y;
8357d523365SDimitry Andric   }
8367d523365SDimitry Andric 
8377d523365SDimitry Andric   if (MFI->hasGridWorkgroupCountZ()) {
8387a7e6055SDimitry Andric     Out.code_properties |=
8397d523365SDimitry Andric       AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z;
8407d523365SDimitry Andric   }
8417d523365SDimitry Andric 
8427d523365SDimitry Andric   if (MFI->hasDispatchPtr())
8437a7e6055SDimitry Andric     Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
8448f0fd8f6SDimitry Andric 
8453ca95b02SDimitry Andric   if (STM.debuggerSupported())
8467a7e6055SDimitry Andric     Out.code_properties |= AMD_CODE_PROPERTY_IS_DEBUG_SUPPORTED;
8473ca95b02SDimitry Andric 
8484d0b32cdSDimitry Andric   if (STM.isXNACKEnabled())
8497a7e6055SDimitry Andric     Out.code_properties |= AMD_CODE_PROPERTY_IS_XNACK_SUPPORTED;
8504d0b32cdSDimitry Andric 
851d88c1a5aSDimitry Andric   // FIXME: Should use getKernArgSize
8527a7e6055SDimitry Andric   Out.kernarg_segment_byte_size =
85398221d2eSDimitry Andric     STM.getKernArgSegmentSize(MF, MFI->getABIArgOffset());
854f37b6182SDimitry Andric   Out.wavefront_sgpr_count = CurrentProgramInfo.NumSGPR;
855f37b6182SDimitry Andric   Out.workitem_vgpr_count = CurrentProgramInfo.NumVGPR;
856f37b6182SDimitry Andric   Out.workitem_private_segment_byte_size = CurrentProgramInfo.ScratchSize;
857f37b6182SDimitry Andric   Out.workgroup_group_segment_byte_size = CurrentProgramInfo.LDSSize;
858f37b6182SDimitry Andric   Out.reserved_vgpr_first = CurrentProgramInfo.ReservedVGPRFirst;
859f37b6182SDimitry Andric   Out.reserved_vgpr_count = CurrentProgramInfo.ReservedVGPRCount;
8603ca95b02SDimitry Andric 
861d88c1a5aSDimitry Andric   // These alignment values are specified in powers of two, so alignment =
862d88c1a5aSDimitry Andric   // 2^n.  The minimum alignment is 2^4 = 16.
8637a7e6055SDimitry Andric   Out.kernarg_segment_alignment = std::max((size_t)4,
864d88c1a5aSDimitry Andric       countTrailingZeros(MFI->getMaxKernArgAlign()));
865d88c1a5aSDimitry Andric 
8663ca95b02SDimitry Andric   if (STM.debuggerEmitPrologue()) {
8677a7e6055SDimitry Andric     Out.debug_wavefront_private_segment_offset_sgpr =
868f37b6182SDimitry Andric       CurrentProgramInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR;
8697a7e6055SDimitry Andric     Out.debug_private_segment_buffer_sgpr =
870f37b6182SDimitry Andric       CurrentProgramInfo.DebuggerPrivateSegmentBufferSGPR;
8713ca95b02SDimitry Andric   }
8728f0fd8f6SDimitry Andric }
8738f0fd8f6SDimitry Andric 
8748f0fd8f6SDimitry Andric bool AMDGPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
8758f0fd8f6SDimitry Andric                                        unsigned AsmVariant,
8768f0fd8f6SDimitry Andric                                        const char *ExtraCode, raw_ostream &O) {
8778f0fd8f6SDimitry Andric   if (ExtraCode && ExtraCode[0]) {
8788f0fd8f6SDimitry Andric     if (ExtraCode[1] != 0)
8798f0fd8f6SDimitry Andric       return true; // Unknown modifier.
8808f0fd8f6SDimitry Andric 
8818f0fd8f6SDimitry Andric     switch (ExtraCode[0]) {
8828f0fd8f6SDimitry Andric     default:
8838f0fd8f6SDimitry Andric       // See if this is a generic print operand
8848f0fd8f6SDimitry Andric       return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
8858f0fd8f6SDimitry Andric     case 'r':
8868f0fd8f6SDimitry Andric       break;
8878f0fd8f6SDimitry Andric     }
8888f0fd8f6SDimitry Andric   }
8898f0fd8f6SDimitry Andric 
8908f0fd8f6SDimitry Andric   AMDGPUInstPrinter::printRegOperand(MI->getOperand(OpNo).getReg(), O,
8918f0fd8f6SDimitry Andric                    *TM.getSubtargetImpl(*MF->getFunction())->getRegisterInfo());
8928f0fd8f6SDimitry Andric   return false;
8938f0fd8f6SDimitry Andric }
894