14ba319b5SDimitry Andric //===-- AMDGPUAsmPrinter.cpp - AMDGPU assembly 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"
244ba319b5SDimitry Andric #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
25db17bf38SDimitry Andric #include "MCTargetDesc/AMDGPUTargetStreamer.h"
264ba319b5SDimitry Andric #include "R600AsmPrinter.h"
278f0fd8f6SDimitry Andric #include "R600Defines.h"
288f0fd8f6SDimitry Andric #include "R600MachineFunctionInfo.h"
298f0fd8f6SDimitry Andric #include "R600RegisterInfo.h"
308f0fd8f6SDimitry Andric #include "SIDefines.h"
313ca95b02SDimitry Andric #include "SIInstrInfo.h"
32db17bf38SDimitry Andric #include "SIMachineFunctionInfo.h"
338f0fd8f6SDimitry Andric #include "SIRegisterInfo.h"
34db17bf38SDimitry Andric #include "Utils/AMDGPUBaseInfo.h"
35db17bf38SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
368f0fd8f6SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
373ca95b02SDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
388f0fd8f6SDimitry Andric #include "llvm/MC/MCContext.h"
398f0fd8f6SDimitry Andric #include "llvm/MC/MCSectionELF.h"
408f0fd8f6SDimitry Andric #include "llvm/MC/MCStreamer.h"
412cab237bSDimitry Andric #include "llvm/Support/AMDGPUMetadata.h"
428f0fd8f6SDimitry Andric #include "llvm/Support/MathExtras.h"
43*b5893f02SDimitry Andric #include "llvm/Support/TargetParser.h"
448f0fd8f6SDimitry Andric #include "llvm/Support/TargetRegistry.h"
454ba319b5SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h"
468f0fd8f6SDimitry Andric 
478f0fd8f6SDimitry Andric using namespace llvm;
482cab237bSDimitry Andric using namespace llvm::AMDGPU;
49*b5893f02SDimitry Andric using namespace llvm::AMDGPU::HSAMD;
508f0fd8f6SDimitry Andric 
518f0fd8f6SDimitry Andric // TODO: This should get the default rounding mode from the kernel. We just set
528f0fd8f6SDimitry Andric // the default here, but this could change if the OpenCL rounding mode pragmas
538f0fd8f6SDimitry Andric // are used.
548f0fd8f6SDimitry Andric //
558f0fd8f6SDimitry Andric // The denormal mode here should match what is reported by the OpenCL runtime
568f0fd8f6SDimitry Andric // for the CL_FP_DENORM bit from CL_DEVICE_{HALF|SINGLE|DOUBLE}_FP_CONFIG, but
578f0fd8f6SDimitry Andric // can also be override to flush with the -cl-denorms-are-zero compiler flag.
588f0fd8f6SDimitry Andric //
598f0fd8f6SDimitry Andric // AMD OpenCL only sets flush none and reports CL_FP_DENORM for double
608f0fd8f6SDimitry Andric // precision, and leaves single precision to flush all and does not report
618f0fd8f6SDimitry Andric // CL_FP_DENORM for CL_DEVICE_SINGLE_FP_CONFIG. Mesa's OpenCL currently reports
628f0fd8f6SDimitry Andric // CL_FP_DENORM for both.
638f0fd8f6SDimitry Andric //
648f0fd8f6SDimitry Andric // FIXME: It seems some instructions do not support single precision denormals
658f0fd8f6SDimitry Andric // regardless of the mode (exp_*_f32, rcp_*_f32, rsq_*_f32, rsq_*f32, sqrt_f32,
668f0fd8f6SDimitry Andric // and sin_f32, cos_f32 on most parts).
678f0fd8f6SDimitry Andric 
688f0fd8f6SDimitry Andric // We want to use these instructions, and using fp32 denormals also causes
698f0fd8f6SDimitry Andric // instructions to run at the double precision rate for the device so it's
708f0fd8f6SDimitry Andric // probably best to just report no single precision denormals.
getFPMode(const MachineFunction & F)718f0fd8f6SDimitry Andric static uint32_t getFPMode(const MachineFunction &F) {
724ba319b5SDimitry Andric   const GCNSubtarget& ST = F.getSubtarget<GCNSubtarget>();
738f0fd8f6SDimitry Andric   // TODO: Is there any real use for the flush in only / flush out only modes?
748f0fd8f6SDimitry Andric 
758f0fd8f6SDimitry Andric   uint32_t FP32Denormals =
768f0fd8f6SDimitry Andric     ST.hasFP32Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
778f0fd8f6SDimitry Andric 
788f0fd8f6SDimitry Andric   uint32_t FP64Denormals =
798f0fd8f6SDimitry Andric     ST.hasFP64Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
808f0fd8f6SDimitry Andric 
818f0fd8f6SDimitry Andric   return FP_ROUND_MODE_SP(FP_ROUND_ROUND_TO_NEAREST) |
828f0fd8f6SDimitry Andric          FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_NEAREST) |
838f0fd8f6SDimitry Andric          FP_DENORM_MODE_SP(FP32Denormals) |
848f0fd8f6SDimitry Andric          FP_DENORM_MODE_DP(FP64Denormals);
858f0fd8f6SDimitry Andric }
868f0fd8f6SDimitry Andric 
878f0fd8f6SDimitry Andric static AsmPrinter *
createAMDGPUAsmPrinterPass(TargetMachine & tm,std::unique_ptr<MCStreamer> && Streamer)888f0fd8f6SDimitry Andric createAMDGPUAsmPrinterPass(TargetMachine &tm,
898f0fd8f6SDimitry Andric                            std::unique_ptr<MCStreamer> &&Streamer) {
908f0fd8f6SDimitry Andric   return new AMDGPUAsmPrinter(tm, std::move(Streamer));
918f0fd8f6SDimitry Andric }
928f0fd8f6SDimitry Andric 
LLVMInitializeAMDGPUAsmPrinter()938f0fd8f6SDimitry Andric extern "C" void LLVMInitializeAMDGPUAsmPrinter() {
94d88c1a5aSDimitry Andric   TargetRegistry::RegisterAsmPrinter(getTheAMDGPUTarget(),
954ba319b5SDimitry Andric                                      llvm::createR600AsmPrinterPass);
96d88c1a5aSDimitry Andric   TargetRegistry::RegisterAsmPrinter(getTheGCNTarget(),
97d88c1a5aSDimitry Andric                                      createAMDGPUAsmPrinterPass);
988f0fd8f6SDimitry Andric }
998f0fd8f6SDimitry Andric 
AMDGPUAsmPrinter(TargetMachine & TM,std::unique_ptr<MCStreamer> Streamer)1008f0fd8f6SDimitry Andric AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM,
1018f0fd8f6SDimitry Andric                                    std::unique_ptr<MCStreamer> Streamer)
1027a7e6055SDimitry Andric   : AsmPrinter(TM, std::move(Streamer)) {
103*b5893f02SDimitry Andric     if (IsaInfo::hasCodeObjectV3(getSTI()))
104*b5893f02SDimitry Andric       HSAMetadataStream.reset(new MetadataStreamerV3());
105*b5893f02SDimitry Andric     else
106*b5893f02SDimitry Andric       HSAMetadataStream.reset(new MetadataStreamerV2());
1077a7e6055SDimitry Andric }
1088f0fd8f6SDimitry Andric 
getPassName() const109d88c1a5aSDimitry Andric StringRef AMDGPUAsmPrinter::getPassName() const {
110d88c1a5aSDimitry Andric   return "AMDGPU Assembly Printer";
111d88c1a5aSDimitry Andric }
112d88c1a5aSDimitry Andric 
getSTI() const1137a7e6055SDimitry Andric const MCSubtargetInfo* AMDGPUAsmPrinter::getSTI() const {
1147a7e6055SDimitry Andric   return TM.getMCSubtargetInfo();
1157a7e6055SDimitry Andric }
1167a7e6055SDimitry Andric 
getTargetStreamer() const1172cab237bSDimitry Andric AMDGPUTargetStreamer* AMDGPUAsmPrinter::getTargetStreamer() const {
1182cab237bSDimitry Andric   if (!OutStreamer)
1192cab237bSDimitry Andric     return nullptr;
1202cab237bSDimitry Andric   return static_cast<AMDGPUTargetStreamer*>(OutStreamer->getTargetStreamer());
1217a7e6055SDimitry Andric }
1227a7e6055SDimitry Andric 
EmitStartOfAsmFile(Module & M)123444ed5c5SDimitry Andric void AMDGPUAsmPrinter::EmitStartOfAsmFile(Module &M) {
124*b5893f02SDimitry Andric   if (IsaInfo::hasCodeObjectV3(getSTI())) {
125*b5893f02SDimitry Andric     std::string ExpectedTarget;
126*b5893f02SDimitry Andric     raw_string_ostream ExpectedTargetOS(ExpectedTarget);
127*b5893f02SDimitry Andric     IsaInfo::streamIsaVersion(getSTI(), ExpectedTargetOS);
128*b5893f02SDimitry Andric 
129*b5893f02SDimitry Andric     getTargetStreamer()->EmitDirectiveAMDGCNTarget(ExpectedTarget);
130*b5893f02SDimitry Andric   }
131444ed5c5SDimitry Andric 
1322cab237bSDimitry Andric   if (TM.getTargetTriple().getOS() != Triple::AMDHSA &&
1332cab237bSDimitry Andric       TM.getTargetTriple().getOS() != Triple::AMDPAL)
1342cab237bSDimitry Andric     return;
135444ed5c5SDimitry Andric 
1362cab237bSDimitry Andric   if (TM.getTargetTriple().getOS() == Triple::AMDHSA)
137*b5893f02SDimitry Andric     HSAMetadataStream->begin(M);
1382cab237bSDimitry Andric 
1392cab237bSDimitry Andric   if (TM.getTargetTriple().getOS() == Triple::AMDPAL)
1402cab237bSDimitry Andric     readPALMetadata(M);
1412cab237bSDimitry Andric 
142*b5893f02SDimitry Andric   if (IsaInfo::hasCodeObjectV3(getSTI()))
143*b5893f02SDimitry Andric     return;
144*b5893f02SDimitry Andric 
1452cab237bSDimitry Andric   // HSA emits NT_AMDGPU_HSA_CODE_OBJECT_VERSION for code objects v2.
1462cab237bSDimitry Andric   if (TM.getTargetTriple().getOS() == Triple::AMDHSA)
1472cab237bSDimitry Andric     getTargetStreamer()->EmitDirectiveHSACodeObjectVersion(2, 1);
1482cab237bSDimitry Andric 
1492cab237bSDimitry Andric   // HSA and PAL emit NT_AMDGPU_HSA_ISA for code objects v2.
150*b5893f02SDimitry Andric   IsaVersion Version = getIsaVersion(getSTI()->getCPU());
1512cab237bSDimitry Andric   getTargetStreamer()->EmitDirectiveHSACodeObjectISA(
152*b5893f02SDimitry Andric       Version.Major, Version.Minor, Version.Stepping, "AMD", "AMDGPU");
1537a7e6055SDimitry Andric }
154444ed5c5SDimitry Andric 
EmitEndOfAsmFile(Module & M)1557a7e6055SDimitry Andric void AMDGPUAsmPrinter::EmitEndOfAsmFile(Module &M) {
1562cab237bSDimitry Andric   // Following code requires TargetStreamer to be present.
1572cab237bSDimitry Andric   if (!getTargetStreamer())
1582cab237bSDimitry Andric     return;
1592cab237bSDimitry Andric 
160*b5893f02SDimitry Andric   if (!IsaInfo::hasCodeObjectV3(getSTI())) {
1612cab237bSDimitry Andric     // Emit ISA Version (NT_AMD_AMDGPU_ISA).
1622cab237bSDimitry Andric     std::string ISAVersionString;
1632cab237bSDimitry Andric     raw_string_ostream ISAVersionStream(ISAVersionString);
1642cab237bSDimitry Andric     IsaInfo::streamIsaVersion(getSTI(), ISAVersionStream);
1652cab237bSDimitry Andric     getTargetStreamer()->EmitISAVersion(ISAVersionStream.str());
166*b5893f02SDimitry Andric   }
1672cab237bSDimitry Andric 
1682cab237bSDimitry Andric   // Emit HSA Metadata (NT_AMD_AMDGPU_HSA_METADATA).
1692cab237bSDimitry Andric   if (TM.getTargetTriple().getOS() == Triple::AMDHSA) {
170*b5893f02SDimitry Andric     HSAMetadataStream->end();
171*b5893f02SDimitry Andric     bool Success = HSAMetadataStream->emitTo(*getTargetStreamer());
172*b5893f02SDimitry Andric     (void)Success;
173*b5893f02SDimitry Andric     assert(Success && "Malformed HSA Metadata");
1742cab237bSDimitry Andric   }
1752cab237bSDimitry Andric 
176*b5893f02SDimitry Andric   if (!IsaInfo::hasCodeObjectV3(getSTI())) {
1772cab237bSDimitry Andric     // Emit PAL Metadata (NT_AMD_AMDGPU_PAL_METADATA).
1782cab237bSDimitry Andric     if (TM.getTargetTriple().getOS() == Triple::AMDPAL) {
1792cab237bSDimitry Andric       // Copy the PAL metadata from the map where we collected it into a vector,
1802cab237bSDimitry Andric       // then write it as a .note.
1812cab237bSDimitry Andric       PALMD::Metadata PALMetadataVector;
1822cab237bSDimitry Andric       for (auto i : PALMetadataMap) {
1832cab237bSDimitry Andric         PALMetadataVector.push_back(i.first);
1842cab237bSDimitry Andric         PALMetadataVector.push_back(i.second);
1852cab237bSDimitry Andric       }
1862cab237bSDimitry Andric       getTargetStreamer()->EmitPALMetadata(PALMetadataVector);
1872cab237bSDimitry Andric     }
188444ed5c5SDimitry Andric   }
189*b5893f02SDimitry Andric }
190444ed5c5SDimitry Andric 
isBlockOnlyReachableByFallthrough(const MachineBasicBlock * MBB) const191d88c1a5aSDimitry Andric bool AMDGPUAsmPrinter::isBlockOnlyReachableByFallthrough(
192d88c1a5aSDimitry Andric   const MachineBasicBlock *MBB) const {
193d88c1a5aSDimitry Andric   if (!AsmPrinter::isBlockOnlyReachableByFallthrough(MBB))
194d88c1a5aSDimitry Andric     return false;
195d88c1a5aSDimitry Andric 
196d88c1a5aSDimitry Andric   if (MBB->empty())
197d88c1a5aSDimitry Andric     return true;
198d88c1a5aSDimitry Andric 
199d88c1a5aSDimitry Andric   // If this is a block implementing a long branch, an expression relative to
200d88c1a5aSDimitry Andric   // the start of the block is needed.  to the start of the block.
201d88c1a5aSDimitry Andric   // XXX - Is there a smarter way to check this?
202d88c1a5aSDimitry Andric   return (MBB->back().getOpcode() != AMDGPU::S_SETPC_B64);
203d88c1a5aSDimitry Andric }
204d88c1a5aSDimitry Andric 
EmitFunctionBodyStart()2053dac3a9bSDimitry Andric void AMDGPUAsmPrinter::EmitFunctionBodyStart() {
2064ba319b5SDimitry Andric   const SIMachineFunctionInfo &MFI = *MF->getInfo<SIMachineFunctionInfo>();
2074ba319b5SDimitry Andric   if (!MFI.isEntryFunction())
2084ba319b5SDimitry Andric     return;
2096bc11b14SDimitry Andric 
2104ba319b5SDimitry Andric   const GCNSubtarget &STM = MF->getSubtarget<GCNSubtarget>();
2114ba319b5SDimitry Andric   const Function &F = MF->getFunction();
212*b5893f02SDimitry Andric   if (!STM.hasCodeObjectV3() && STM.isAmdHsaOrMesa(F) &&
2134ba319b5SDimitry Andric       (F.getCallingConv() == CallingConv::AMDGPU_KERNEL ||
2144ba319b5SDimitry Andric        F.getCallingConv() == CallingConv::SPIR_KERNEL)) {
2157a7e6055SDimitry Andric     amd_kernel_code_t KernelCode;
216f37b6182SDimitry Andric     getAmdKernelCode(KernelCode, CurrentProgramInfo, *MF);
2172cab237bSDimitry Andric     getTargetStreamer()->EmitAMDKernelCodeT(KernelCode);
2183dac3a9bSDimitry Andric   }
2197a7e6055SDimitry Andric 
220*b5893f02SDimitry Andric   if (STM.isAmdHsaOS())
221*b5893f02SDimitry Andric     HSAMetadataStream->emitKernel(*MF, CurrentProgramInfo);
2224ba319b5SDimitry Andric }
2234ba319b5SDimitry Andric 
EmitFunctionBodyEnd()2244ba319b5SDimitry Andric void AMDGPUAsmPrinter::EmitFunctionBodyEnd() {
2254ba319b5SDimitry Andric   const SIMachineFunctionInfo &MFI = *MF->getInfo<SIMachineFunctionInfo>();
2264ba319b5SDimitry Andric   if (!MFI.isEntryFunction())
2274ba319b5SDimitry Andric     return;
2284ba319b5SDimitry Andric   if (!IsaInfo::hasCodeObjectV3(getSTI()) ||
2294ba319b5SDimitry Andric       TM.getTargetTriple().getOS() != Triple::AMDHSA)
2304ba319b5SDimitry Andric     return;
2314ba319b5SDimitry Andric 
2324ba319b5SDimitry Andric   auto &Streamer = getTargetStreamer()->getStreamer();
2334ba319b5SDimitry Andric   auto &Context = Streamer.getContext();
2344ba319b5SDimitry Andric   auto &ObjectFileInfo = *Context.getObjectFileInfo();
2354ba319b5SDimitry Andric   auto &ReadOnlySection = *ObjectFileInfo.getReadOnlySection();
2364ba319b5SDimitry Andric 
2374ba319b5SDimitry Andric   Streamer.PushSection();
2384ba319b5SDimitry Andric   Streamer.SwitchSection(&ReadOnlySection);
2394ba319b5SDimitry Andric 
2404ba319b5SDimitry Andric   // CP microcode requires the kernel descriptor to be allocated on 64 byte
2414ba319b5SDimitry Andric   // alignment.
2424ba319b5SDimitry Andric   Streamer.EmitValueToAlignment(64, 0, 1, 0);
2434ba319b5SDimitry Andric   if (ReadOnlySection.getAlignment() < 64)
2444ba319b5SDimitry Andric     ReadOnlySection.setAlignment(64);
2454ba319b5SDimitry Andric 
2464ba319b5SDimitry Andric   SmallString<128> KernelName;
2474ba319b5SDimitry Andric   getNameWithPrefix(KernelName, &MF->getFunction());
2484ba319b5SDimitry Andric   getTargetStreamer()->EmitAmdhsaKernelDescriptor(
2494ba319b5SDimitry Andric       *getSTI(), KernelName, getAmdhsaKernelDescriptor(*MF, CurrentProgramInfo),
2504ba319b5SDimitry Andric       CurrentProgramInfo.NumVGPRsForWavesPerEU,
2514ba319b5SDimitry Andric       CurrentProgramInfo.NumSGPRsForWavesPerEU -
252*b5893f02SDimitry Andric           IsaInfo::getNumExtraSGPRs(getSTI(),
2534ba319b5SDimitry Andric                                     CurrentProgramInfo.VCCUsed,
2544ba319b5SDimitry Andric                                     CurrentProgramInfo.FlatUsed),
2554ba319b5SDimitry Andric       CurrentProgramInfo.VCCUsed, CurrentProgramInfo.FlatUsed,
2564ba319b5SDimitry Andric       hasXNACK(*getSTI()));
2574ba319b5SDimitry Andric 
2584ba319b5SDimitry Andric   Streamer.PopSection();
2593dac3a9bSDimitry Andric }
2603dac3a9bSDimitry Andric 
EmitFunctionEntryLabel()2617d523365SDimitry Andric void AMDGPUAsmPrinter::EmitFunctionEntryLabel() {
2624ba319b5SDimitry Andric   if (IsaInfo::hasCodeObjectV3(getSTI()) &&
2634ba319b5SDimitry Andric       TM.getTargetTriple().getOS() == Triple::AMDHSA) {
2644ba319b5SDimitry Andric     AsmPrinter::EmitFunctionEntryLabel();
2654ba319b5SDimitry Andric     return;
2664ba319b5SDimitry Andric   }
2674ba319b5SDimitry Andric 
2687d523365SDimitry Andric   const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
2694ba319b5SDimitry Andric   const GCNSubtarget &STM = MF->getSubtarget<GCNSubtarget>();
270*b5893f02SDimitry Andric   if (MFI->isEntryFunction() && STM.isAmdHsaOrMesa(MF->getFunction())) {
271d88c1a5aSDimitry Andric     SmallString<128> SymbolName;
2722cab237bSDimitry Andric     getNameWithPrefix(SymbolName, &MF->getFunction()),
2732cab237bSDimitry Andric     getTargetStreamer()->EmitAMDGPUSymbolType(
2747a7e6055SDimitry Andric         SymbolName, ELF::STT_AMDGPU_HSA_KERNEL);
2757d523365SDimitry Andric   }
2764ba319b5SDimitry Andric   const GCNSubtarget &STI = MF->getSubtarget<GCNSubtarget>();
2772cab237bSDimitry Andric   if (STI.dumpCode()) {
2782cab237bSDimitry Andric     // Disassemble function name label to text.
2792cab237bSDimitry Andric     DisasmLines.push_back(MF->getName().str() + ":");
2802cab237bSDimitry Andric     DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLines.back().size());
2812cab237bSDimitry Andric     HexLines.push_back("");
2822cab237bSDimitry Andric   }
2838f0fd8f6SDimitry Andric 
2847d523365SDimitry Andric   AsmPrinter::EmitFunctionEntryLabel();
2857d523365SDimitry Andric }
2867d523365SDimitry Andric 
EmitBasicBlockStart(const MachineBasicBlock & MBB) const2872cab237bSDimitry Andric void AMDGPUAsmPrinter::EmitBasicBlockStart(const MachineBasicBlock &MBB) const {
2884ba319b5SDimitry Andric   const GCNSubtarget &STI = MBB.getParent()->getSubtarget<GCNSubtarget>();
2892cab237bSDimitry Andric   if (STI.dumpCode() && !isBlockOnlyReachableByFallthrough(&MBB)) {
2902cab237bSDimitry Andric     // Write a line for the basic block label if it is not only fallthrough.
2912cab237bSDimitry Andric     DisasmLines.push_back(
2922cab237bSDimitry Andric         (Twine("BB") + Twine(getFunctionNumber())
2932cab237bSDimitry Andric          + "_" + Twine(MBB.getNumber()) + ":").str());
2942cab237bSDimitry Andric     DisasmLineMaxLen = std::max(DisasmLineMaxLen, DisasmLines.back().size());
2952cab237bSDimitry Andric     HexLines.push_back("");
2962cab237bSDimitry Andric   }
2972cab237bSDimitry Andric   AsmPrinter::EmitBasicBlockStart(MBB);
2982cab237bSDimitry Andric }
2992cab237bSDimitry Andric 
EmitGlobalVariable(const GlobalVariable * GV)3007d523365SDimitry Andric void AMDGPUAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
3017d523365SDimitry Andric 
3027d523365SDimitry Andric   // Group segment variables aren't emitted in HSA.
3032cab237bSDimitry Andric   if (AMDGPU::isGroupSegment(GV))
3047d523365SDimitry Andric     return;
3057d523365SDimitry Andric 
3063ca95b02SDimitry Andric   AsmPrinter::EmitGlobalVariable(GV);
3078f0fd8f6SDimitry Andric }
3088f0fd8f6SDimitry Andric 
doFinalization(Module & M)309f37b6182SDimitry Andric bool AMDGPUAsmPrinter::doFinalization(Module &M) {
310f37b6182SDimitry Andric   CallGraphResourceInfo.clear();
311f37b6182SDimitry Andric   return AsmPrinter::doFinalization(M);
312f37b6182SDimitry Andric }
313f37b6182SDimitry Andric 
3142cab237bSDimitry Andric // For the amdpal OS type, read the amdgpu.pal.metadata supplied by the
3152cab237bSDimitry Andric // frontend into our PALMetadataMap, ready for per-function modification.  It
3162cab237bSDimitry Andric // is a NamedMD containing an MDTuple containing a number of MDNodes each of
3172cab237bSDimitry Andric // which is an integer value, and each two integer values forms a key=value
3182cab237bSDimitry Andric // pair that we store as PALMetadataMap[key]=value in the map.
readPALMetadata(Module & M)3192cab237bSDimitry Andric void AMDGPUAsmPrinter::readPALMetadata(Module &M) {
3202cab237bSDimitry Andric   auto NamedMD = M.getNamedMetadata("amdgpu.pal.metadata");
3212cab237bSDimitry Andric   if (!NamedMD || !NamedMD->getNumOperands())
3222cab237bSDimitry Andric     return;
3232cab237bSDimitry Andric   auto Tuple = dyn_cast<MDTuple>(NamedMD->getOperand(0));
3242cab237bSDimitry Andric   if (!Tuple)
3252cab237bSDimitry Andric     return;
3262cab237bSDimitry Andric   for (unsigned I = 0, E = Tuple->getNumOperands() & -2; I != E; I += 2) {
3272cab237bSDimitry Andric     auto Key = mdconst::dyn_extract<ConstantInt>(Tuple->getOperand(I));
3282cab237bSDimitry Andric     auto Val = mdconst::dyn_extract<ConstantInt>(Tuple->getOperand(I + 1));
3292cab237bSDimitry Andric     if (!Key || !Val)
3302cab237bSDimitry Andric       continue;
3312cab237bSDimitry Andric     PALMetadataMap[Key->getZExtValue()] = Val->getZExtValue();
3322cab237bSDimitry Andric   }
3332cab237bSDimitry Andric }
3342cab237bSDimitry Andric 
335f37b6182SDimitry Andric // Print comments that apply to both callable functions and entry points.
emitCommonFunctionComments(uint32_t NumVGPR,uint32_t NumSGPR,uint64_t ScratchSize,uint64_t CodeSize,const AMDGPUMachineFunction * MFI)336f37b6182SDimitry Andric void AMDGPUAsmPrinter::emitCommonFunctionComments(
337f37b6182SDimitry Andric   uint32_t NumVGPR,
338f37b6182SDimitry Andric   uint32_t NumSGPR,
3392cab237bSDimitry Andric   uint64_t ScratchSize,
3404ba319b5SDimitry Andric   uint64_t CodeSize,
3414ba319b5SDimitry Andric   const AMDGPUMachineFunction *MFI) {
342f37b6182SDimitry Andric   OutStreamer->emitRawComment(" codeLenInByte = " + Twine(CodeSize), false);
343f37b6182SDimitry Andric   OutStreamer->emitRawComment(" NumSgprs: " + Twine(NumSGPR), false);
344f37b6182SDimitry Andric   OutStreamer->emitRawComment(" NumVgprs: " + Twine(NumVGPR), false);
345f37b6182SDimitry Andric   OutStreamer->emitRawComment(" ScratchSize: " + Twine(ScratchSize), false);
3464ba319b5SDimitry Andric   OutStreamer->emitRawComment(" MemoryBound: " + Twine(MFI->isMemoryBound()),
3474ba319b5SDimitry Andric                               false);
3484ba319b5SDimitry Andric }
3494ba319b5SDimitry Andric 
getAmdhsaKernelCodeProperties(const MachineFunction & MF) const3504ba319b5SDimitry Andric uint16_t AMDGPUAsmPrinter::getAmdhsaKernelCodeProperties(
3514ba319b5SDimitry Andric     const MachineFunction &MF) const {
3524ba319b5SDimitry Andric   const SIMachineFunctionInfo &MFI = *MF.getInfo<SIMachineFunctionInfo>();
3534ba319b5SDimitry Andric   uint16_t KernelCodeProperties = 0;
3544ba319b5SDimitry Andric 
3554ba319b5SDimitry Andric   if (MFI.hasPrivateSegmentBuffer()) {
3564ba319b5SDimitry Andric     KernelCodeProperties |=
3574ba319b5SDimitry Andric         amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER;
3584ba319b5SDimitry Andric   }
3594ba319b5SDimitry Andric   if (MFI.hasDispatchPtr()) {
3604ba319b5SDimitry Andric     KernelCodeProperties |=
3614ba319b5SDimitry Andric         amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
3624ba319b5SDimitry Andric   }
3634ba319b5SDimitry Andric   if (MFI.hasQueuePtr()) {
3644ba319b5SDimitry Andric     KernelCodeProperties |=
3654ba319b5SDimitry Andric         amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR;
3664ba319b5SDimitry Andric   }
3674ba319b5SDimitry Andric   if (MFI.hasKernargSegmentPtr()) {
3684ba319b5SDimitry Andric     KernelCodeProperties |=
3694ba319b5SDimitry Andric         amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR;
3704ba319b5SDimitry Andric   }
3714ba319b5SDimitry Andric   if (MFI.hasDispatchID()) {
3724ba319b5SDimitry Andric     KernelCodeProperties |=
3734ba319b5SDimitry Andric         amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID;
3744ba319b5SDimitry Andric   }
3754ba319b5SDimitry Andric   if (MFI.hasFlatScratchInit()) {
3764ba319b5SDimitry Andric     KernelCodeProperties |=
3774ba319b5SDimitry Andric         amdhsa::KERNEL_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT;
3784ba319b5SDimitry Andric   }
3794ba319b5SDimitry Andric 
3804ba319b5SDimitry Andric   return KernelCodeProperties;
3814ba319b5SDimitry Andric }
3824ba319b5SDimitry Andric 
getAmdhsaKernelDescriptor(const MachineFunction & MF,const SIProgramInfo & PI) const3834ba319b5SDimitry Andric amdhsa::kernel_descriptor_t AMDGPUAsmPrinter::getAmdhsaKernelDescriptor(
3844ba319b5SDimitry Andric     const MachineFunction &MF,
3854ba319b5SDimitry Andric     const SIProgramInfo &PI) const {
3864ba319b5SDimitry Andric   amdhsa::kernel_descriptor_t KernelDescriptor;
3874ba319b5SDimitry Andric   memset(&KernelDescriptor, 0x0, sizeof(KernelDescriptor));
3884ba319b5SDimitry Andric 
3894ba319b5SDimitry Andric   assert(isUInt<32>(PI.ScratchSize));
3904ba319b5SDimitry Andric   assert(isUInt<32>(PI.ComputePGMRSrc1));
3914ba319b5SDimitry Andric   assert(isUInt<32>(PI.ComputePGMRSrc2));
3924ba319b5SDimitry Andric 
3934ba319b5SDimitry Andric   KernelDescriptor.group_segment_fixed_size = PI.LDSSize;
3944ba319b5SDimitry Andric   KernelDescriptor.private_segment_fixed_size = PI.ScratchSize;
3954ba319b5SDimitry Andric   KernelDescriptor.compute_pgm_rsrc1 = PI.ComputePGMRSrc1;
3964ba319b5SDimitry Andric   KernelDescriptor.compute_pgm_rsrc2 = PI.ComputePGMRSrc2;
3974ba319b5SDimitry Andric   KernelDescriptor.kernel_code_properties = getAmdhsaKernelCodeProperties(MF);
3984ba319b5SDimitry Andric 
3994ba319b5SDimitry Andric   return KernelDescriptor;
400f37b6182SDimitry Andric }
401f37b6182SDimitry Andric 
runOnMachineFunction(MachineFunction & MF)4028f0fd8f6SDimitry Andric bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
403f37b6182SDimitry Andric   CurrentProgramInfo = SIProgramInfo();
404f37b6182SDimitry Andric 
4056bc11b14SDimitry Andric   const AMDGPUMachineFunction *MFI = MF.getInfo<AMDGPUMachineFunction>();
4068f0fd8f6SDimitry Andric 
4078f0fd8f6SDimitry Andric   // The starting address of all shader programs must be 256 bytes aligned.
4086bc11b14SDimitry Andric   // Regular functions just need the basic required instruction alignment.
4096bc11b14SDimitry Andric   MF.setAlignment(MFI->isEntryFunction() ? 8 : 2);
4108f0fd8f6SDimitry Andric 
4118f0fd8f6SDimitry Andric   SetupMachineFunction(MF);
4128f0fd8f6SDimitry Andric 
4134ba319b5SDimitry Andric   const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>();
4148f0fd8f6SDimitry Andric   MCContext &Context = getObjFileLowering().getContext();
4154ba319b5SDimitry Andric   // FIXME: This should be an explicit check for Mesa.
4164ba319b5SDimitry Andric   if (!STM.isAmdHsaOS() && !STM.isAmdPalOS()) {
4178f0fd8f6SDimitry Andric     MCSectionELF *ConfigSection =
4188f0fd8f6SDimitry Andric         Context.getELFSection(".AMDGPU.config", ELF::SHT_PROGBITS, 0);
4198f0fd8f6SDimitry Andric     OutStreamer->SwitchSection(ConfigSection);
42095ec533aSDimitry Andric   }
4218f0fd8f6SDimitry Andric 
422f37b6182SDimitry Andric   if (MFI->isEntryFunction()) {
423f37b6182SDimitry Andric     getSIProgramInfo(CurrentProgramInfo, MF);
424f37b6182SDimitry Andric   } else {
425f37b6182SDimitry Andric     auto I = CallGraphResourceInfo.insert(
4262cab237bSDimitry Andric       std::make_pair(&MF.getFunction(), SIFunctionResourceInfo()));
427f37b6182SDimitry Andric     SIFunctionResourceInfo &Info = I.first->second;
428f37b6182SDimitry Andric     assert(I.second && "should only be called once per function");
429f37b6182SDimitry Andric     Info = analyzeResourceUsage(MF);
430f37b6182SDimitry Andric   }
431f37b6182SDimitry Andric 
4322cab237bSDimitry Andric   if (STM.isAmdPalOS())
4332cab237bSDimitry Andric     EmitPALMetadata(MF, CurrentProgramInfo);
4344ba319b5SDimitry Andric   else if (!STM.isAmdHsaOS()) {
435f37b6182SDimitry Andric     EmitProgramInfoSI(MF, CurrentProgramInfo);
4363dac3a9bSDimitry Andric   }
4378f0fd8f6SDimitry Andric 
4388f0fd8f6SDimitry Andric   DisasmLines.clear();
4398f0fd8f6SDimitry Andric   HexLines.clear();
4408f0fd8f6SDimitry Andric   DisasmLineMaxLen = 0;
4418f0fd8f6SDimitry Andric 
4428f0fd8f6SDimitry Andric   EmitFunctionBody();
4438f0fd8f6SDimitry Andric 
4448f0fd8f6SDimitry Andric   if (isVerbose()) {
4458f0fd8f6SDimitry Andric     MCSectionELF *CommentSection =
4468f0fd8f6SDimitry Andric         Context.getELFSection(".AMDGPU.csdata", ELF::SHT_PROGBITS, 0);
4478f0fd8f6SDimitry Andric     OutStreamer->SwitchSection(CommentSection);
4488f0fd8f6SDimitry Andric 
449f37b6182SDimitry Andric     if (!MFI->isEntryFunction()) {
4506bc11b14SDimitry Andric       OutStreamer->emitRawComment(" Function info:", false);
4512cab237bSDimitry Andric       SIFunctionResourceInfo &Info = CallGraphResourceInfo[&MF.getFunction()];
452f37b6182SDimitry Andric       emitCommonFunctionComments(
453f37b6182SDimitry Andric         Info.NumVGPR,
4544ba319b5SDimitry Andric         Info.getTotalNumSGPRs(MF.getSubtarget<GCNSubtarget>()),
455f37b6182SDimitry Andric         Info.PrivateSegmentSize,
4564ba319b5SDimitry Andric         getFunctionCodeSize(MF), MFI);
457f37b6182SDimitry Andric       return false;
4586bc11b14SDimitry Andric     }
4596bc11b14SDimitry Andric 
460f37b6182SDimitry Andric     OutStreamer->emitRawComment(" Kernel info:", false);
461f37b6182SDimitry Andric     emitCommonFunctionComments(CurrentProgramInfo.NumVGPR,
462f37b6182SDimitry Andric                                CurrentProgramInfo.NumSGPR,
463f37b6182SDimitry Andric                                CurrentProgramInfo.ScratchSize,
4644ba319b5SDimitry Andric                                getFunctionCodeSize(MF), MFI);
465f37b6182SDimitry Andric 
466f37b6182SDimitry Andric     OutStreamer->emitRawComment(
467f37b6182SDimitry Andric       " FloatMode: " + Twine(CurrentProgramInfo.FloatMode), false);
468f37b6182SDimitry Andric     OutStreamer->emitRawComment(
469f37b6182SDimitry Andric       " IeeeMode: " + Twine(CurrentProgramInfo.IEEEMode), false);
470f37b6182SDimitry Andric     OutStreamer->emitRawComment(
471f37b6182SDimitry Andric       " LDSByteSize: " + Twine(CurrentProgramInfo.LDSSize) +
4723ca95b02SDimitry Andric       " bytes/workgroup (compile time only)", false);
4733ca95b02SDimitry Andric 
474f37b6182SDimitry Andric     OutStreamer->emitRawComment(
475f37b6182SDimitry Andric       " SGPRBlocks: " + Twine(CurrentProgramInfo.SGPRBlocks), false);
476f37b6182SDimitry Andric     OutStreamer->emitRawComment(
477f37b6182SDimitry Andric       " VGPRBlocks: " + Twine(CurrentProgramInfo.VGPRBlocks), false);
4786bc11b14SDimitry Andric 
479f37b6182SDimitry Andric     OutStreamer->emitRawComment(
480f37b6182SDimitry Andric       " NumSGPRsForWavesPerEU: " +
481f37b6182SDimitry Andric       Twine(CurrentProgramInfo.NumSGPRsForWavesPerEU), false);
482f37b6182SDimitry Andric     OutStreamer->emitRawComment(
483f37b6182SDimitry Andric       " NumVGPRsForWavesPerEU: " +
484f37b6182SDimitry Andric       Twine(CurrentProgramInfo.NumVGPRsForWavesPerEU), false);
485d88c1a5aSDimitry Andric 
486f37b6182SDimitry Andric     OutStreamer->emitRawComment(
4874ba319b5SDimitry Andric       " WaveLimiterHint : " + Twine(MFI->needsWaveLimiter()), false);
4883ca95b02SDimitry Andric 
4894ba319b5SDimitry Andric     if (MF.getSubtarget<GCNSubtarget>().debuggerEmitPrologue()) {
490f37b6182SDimitry Andric       OutStreamer->emitRawComment(
491f37b6182SDimitry Andric         " DebuggerWavefrontPrivateSegmentOffsetSGPR: s" +
492f37b6182SDimitry Andric         Twine(CurrentProgramInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR), false);
493f37b6182SDimitry Andric       OutStreamer->emitRawComment(
494f37b6182SDimitry Andric         " DebuggerPrivateSegmentBufferSGPR: s" +
495f37b6182SDimitry Andric         Twine(CurrentProgramInfo.DebuggerPrivateSegmentBufferSGPR), false);
4963ca95b02SDimitry Andric     }
4977d523365SDimitry Andric 
498f37b6182SDimitry Andric     OutStreamer->emitRawComment(
499f37b6182SDimitry Andric       " COMPUTE_PGM_RSRC2:USER_SGPR: " +
500f37b6182SDimitry Andric       Twine(G_00B84C_USER_SGPR(CurrentProgramInfo.ComputePGMRSrc2)), false);
501f37b6182SDimitry Andric     OutStreamer->emitRawComment(
502f37b6182SDimitry Andric       " COMPUTE_PGM_RSRC2:TRAP_HANDLER: " +
503f37b6182SDimitry Andric       Twine(G_00B84C_TRAP_HANDLER(CurrentProgramInfo.ComputePGMRSrc2)), false);
504f37b6182SDimitry Andric     OutStreamer->emitRawComment(
505f37b6182SDimitry Andric       " COMPUTE_PGM_RSRC2:TGID_X_EN: " +
506f37b6182SDimitry Andric       Twine(G_00B84C_TGID_X_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
507f37b6182SDimitry Andric     OutStreamer->emitRawComment(
508f37b6182SDimitry Andric       " COMPUTE_PGM_RSRC2:TGID_Y_EN: " +
509f37b6182SDimitry Andric       Twine(G_00B84C_TGID_Y_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
510f37b6182SDimitry Andric     OutStreamer->emitRawComment(
511f37b6182SDimitry Andric       " COMPUTE_PGM_RSRC2:TGID_Z_EN: " +
512f37b6182SDimitry Andric       Twine(G_00B84C_TGID_Z_EN(CurrentProgramInfo.ComputePGMRSrc2)), false);
513f37b6182SDimitry Andric     OutStreamer->emitRawComment(
514f37b6182SDimitry Andric       " COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: " +
515f37b6182SDimitry Andric       Twine(G_00B84C_TIDIG_COMP_CNT(CurrentProgramInfo.ComputePGMRSrc2)),
5167d523365SDimitry Andric       false);
5178f0fd8f6SDimitry Andric   }
5188f0fd8f6SDimitry Andric 
5198f0fd8f6SDimitry Andric   if (STM.dumpCode()) {
5208f0fd8f6SDimitry Andric 
5218f0fd8f6SDimitry Andric     OutStreamer->SwitchSection(
5228f0fd8f6SDimitry Andric         Context.getELFSection(".AMDGPU.disasm", ELF::SHT_NOTE, 0));
5238f0fd8f6SDimitry Andric 
5248f0fd8f6SDimitry Andric     for (size_t i = 0; i < DisasmLines.size(); ++i) {
5252cab237bSDimitry Andric       std::string Comment = "\n";
5262cab237bSDimitry Andric       if (!HexLines[i].empty()) {
5272cab237bSDimitry Andric         Comment = std::string(DisasmLineMaxLen - DisasmLines[i].size(), ' ');
5288f0fd8f6SDimitry Andric         Comment += " ; " + HexLines[i] + "\n";
5292cab237bSDimitry Andric       }
5308f0fd8f6SDimitry Andric 
5318f0fd8f6SDimitry Andric       OutStreamer->EmitBytes(StringRef(DisasmLines[i]));
5328f0fd8f6SDimitry Andric       OutStreamer->EmitBytes(StringRef(Comment));
5338f0fd8f6SDimitry Andric     }
5348f0fd8f6SDimitry Andric   }
5358f0fd8f6SDimitry Andric 
5368f0fd8f6SDimitry Andric   return false;
5378f0fd8f6SDimitry Andric }
5388f0fd8f6SDimitry Andric 
getFunctionCodeSize(const MachineFunction & MF) const5396bc11b14SDimitry Andric uint64_t AMDGPUAsmPrinter::getFunctionCodeSize(const MachineFunction &MF) const {
5404ba319b5SDimitry Andric   const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>();
5413ca95b02SDimitry Andric   const SIInstrInfo *TII = STM.getInstrInfo();
5428f0fd8f6SDimitry Andric 
5436bc11b14SDimitry Andric   uint64_t CodeSize = 0;
5446bc11b14SDimitry Andric 
5458f0fd8f6SDimitry Andric   for (const MachineBasicBlock &MBB : MF) {
5468f0fd8f6SDimitry Andric     for (const MachineInstr &MI : MBB) {
5478f0fd8f6SDimitry Andric       // TODO: CodeSize should account for multiple functions.
5489a4b3118SDimitry Andric 
5499a4b3118SDimitry Andric       // TODO: Should we count size of debug info?
5504ba319b5SDimitry Andric       if (MI.isDebugInstr())
5519a4b3118SDimitry Andric         continue;
5529a4b3118SDimitry Andric 
5533ca95b02SDimitry Andric       CodeSize += TII->getInstSizeInBytes(MI);
5546bc11b14SDimitry Andric     }
5556bc11b14SDimitry Andric   }
5568f0fd8f6SDimitry Andric 
5576bc11b14SDimitry Andric   return CodeSize;
5586bc11b14SDimitry Andric }
5598f0fd8f6SDimitry Andric 
hasAnyNonFlatUseOfReg(const MachineRegisterInfo & MRI,const SIInstrInfo & TII,unsigned Reg)5606bc11b14SDimitry Andric static bool hasAnyNonFlatUseOfReg(const MachineRegisterInfo &MRI,
5616bc11b14SDimitry Andric                                   const SIInstrInfo &TII,
5626bc11b14SDimitry Andric                                   unsigned Reg) {
5636bc11b14SDimitry Andric   for (const MachineOperand &UseOp : MRI.reg_operands(Reg)) {
5646bc11b14SDimitry Andric     if (!UseOp.isImplicit() || !TII.isFLAT(*UseOp.getParent()))
5656bc11b14SDimitry Andric       return true;
5666bc11b14SDimitry Andric   }
5678f0fd8f6SDimitry Andric 
5686bc11b14SDimitry Andric   return false;
5696bc11b14SDimitry Andric }
5707d523365SDimitry Andric 
getTotalNumSGPRs(const GCNSubtarget & ST) const571f37b6182SDimitry Andric int32_t AMDGPUAsmPrinter::SIFunctionResourceInfo::getTotalNumSGPRs(
5724ba319b5SDimitry Andric   const GCNSubtarget &ST) const {
573*b5893f02SDimitry Andric   return NumExplicitSGPR + IsaInfo::getNumExtraSGPRs(&ST,
5744ba319b5SDimitry Andric                                                      UsesVCC, UsesFlatScratch);
575f37b6182SDimitry Andric }
576f37b6182SDimitry Andric 
analyzeResourceUsage(const MachineFunction & MF) const577f37b6182SDimitry Andric AMDGPUAsmPrinter::SIFunctionResourceInfo AMDGPUAsmPrinter::analyzeResourceUsage(
5786bc11b14SDimitry Andric   const MachineFunction &MF) const {
579f37b6182SDimitry Andric   SIFunctionResourceInfo Info;
580f37b6182SDimitry Andric 
5816bc11b14SDimitry Andric   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
5824ba319b5SDimitry Andric   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
583f37b6182SDimitry Andric   const MachineFrameInfo &FrameInfo = MF.getFrameInfo();
5846bc11b14SDimitry Andric   const MachineRegisterInfo &MRI = MF.getRegInfo();
585f37b6182SDimitry Andric   const SIInstrInfo *TII = ST.getInstrInfo();
586f37b6182SDimitry Andric   const SIRegisterInfo &TRI = TII->getRegisterInfo();
5877d523365SDimitry Andric 
588f37b6182SDimitry Andric   Info.UsesFlatScratch = MRI.isPhysRegUsed(AMDGPU::FLAT_SCR_LO) ||
589f37b6182SDimitry Andric                          MRI.isPhysRegUsed(AMDGPU::FLAT_SCR_HI);
5907d523365SDimitry Andric 
591f37b6182SDimitry Andric   // Even if FLAT_SCRATCH is implicitly used, it has no effect if flat
592f37b6182SDimitry Andric   // instructions aren't used to access the scratch buffer. Inline assembly may
593f37b6182SDimitry Andric   // need it though.
594f37b6182SDimitry Andric   //
595f37b6182SDimitry Andric   // If we only have implicit uses of flat_scr on flat instructions, it is not
596f37b6182SDimitry Andric   // really needed.
597f37b6182SDimitry Andric   if (Info.UsesFlatScratch && !MFI->hasFlatScratchInit() &&
598f37b6182SDimitry Andric       (!hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR) &&
599f37b6182SDimitry Andric        !hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR_LO) &&
600f37b6182SDimitry Andric        !hasAnyNonFlatUseOfReg(MRI, *TII, AMDGPU::FLAT_SCR_HI))) {
601f37b6182SDimitry Andric     Info.UsesFlatScratch = false;
602f37b6182SDimitry Andric   }
603f37b6182SDimitry Andric 
604f37b6182SDimitry Andric   Info.HasDynamicallySizedStack = FrameInfo.hasVarSizedObjects();
605f37b6182SDimitry Andric   Info.PrivateSegmentSize = FrameInfo.getStackSize();
6064ba319b5SDimitry Andric   if (MFI->isStackRealigned())
6074ba319b5SDimitry Andric     Info.PrivateSegmentSize += FrameInfo.getMaxAlignment();
608f37b6182SDimitry Andric 
609f9448bf3SDimitry Andric 
610f37b6182SDimitry Andric   Info.UsesVCC = MRI.isPhysRegUsed(AMDGPU::VCC_LO) ||
611f37b6182SDimitry Andric                  MRI.isPhysRegUsed(AMDGPU::VCC_HI);
612f37b6182SDimitry Andric 
613f37b6182SDimitry Andric   // If there are no calls, MachineRegisterInfo can tell us the used register
614f37b6182SDimitry Andric   // count easily.
6152cab237bSDimitry Andric   // A tail call isn't considered a call for MachineFrameInfo's purposes.
6162cab237bSDimitry Andric   if (!FrameInfo.hasCalls() && !FrameInfo.hasTailCall()) {
617f37b6182SDimitry Andric     MCPhysReg HighestVGPRReg = AMDGPU::NoRegister;
6186bc11b14SDimitry Andric     for (MCPhysReg Reg : reverse(AMDGPU::VGPR_32RegClass.getRegisters())) {
6196bc11b14SDimitry Andric       if (MRI.isPhysRegUsed(Reg)) {
620f37b6182SDimitry Andric         HighestVGPRReg = Reg;
6217d523365SDimitry Andric         break;
6228f0fd8f6SDimitry Andric       }
6236bc11b14SDimitry Andric     }
6248f0fd8f6SDimitry Andric 
625f37b6182SDimitry Andric     MCPhysReg HighestSGPRReg = AMDGPU::NoRegister;
6266bc11b14SDimitry Andric     for (MCPhysReg Reg : reverse(AMDGPU::SGPR_32RegClass.getRegisters())) {
6276bc11b14SDimitry Andric       if (MRI.isPhysRegUsed(Reg)) {
628f37b6182SDimitry Andric         HighestSGPRReg = Reg;
6296bc11b14SDimitry Andric         break;
6308f0fd8f6SDimitry Andric       }
6318f0fd8f6SDimitry Andric     }
6328f0fd8f6SDimitry Andric 
6336bc11b14SDimitry Andric     // We found the maximum register index. They start at 0, so add one to get the
6346bc11b14SDimitry Andric     // number of registers.
635f37b6182SDimitry Andric     Info.NumVGPR = HighestVGPRReg == AMDGPU::NoRegister ? 0 :
636f37b6182SDimitry Andric       TRI.getHWRegIndex(HighestVGPRReg) + 1;
637f37b6182SDimitry Andric     Info.NumExplicitSGPR = HighestSGPRReg == AMDGPU::NoRegister ? 0 :
638f37b6182SDimitry Andric       TRI.getHWRegIndex(HighestSGPRReg) + 1;
639444ed5c5SDimitry Andric 
640f37b6182SDimitry Andric     return Info;
6416bc11b14SDimitry Andric   }
6426bc11b14SDimitry Andric 
6432cab237bSDimitry Andric   int32_t MaxVGPR = -1;
6442cab237bSDimitry Andric   int32_t MaxSGPR = -1;
6452cab237bSDimitry Andric   uint64_t CalleeFrameSize = 0;
6462cab237bSDimitry Andric 
6472cab237bSDimitry Andric   for (const MachineBasicBlock &MBB : MF) {
6482cab237bSDimitry Andric     for (const MachineInstr &MI : MBB) {
6492cab237bSDimitry Andric       // TODO: Check regmasks? Do they occur anywhere except calls?
6502cab237bSDimitry Andric       for (const MachineOperand &MO : MI.operands()) {
6512cab237bSDimitry Andric         unsigned Width = 0;
6522cab237bSDimitry Andric         bool IsSGPR = false;
6532cab237bSDimitry Andric 
6542cab237bSDimitry Andric         if (!MO.isReg())
6552cab237bSDimitry Andric           continue;
6562cab237bSDimitry Andric 
6572cab237bSDimitry Andric         unsigned Reg = MO.getReg();
6582cab237bSDimitry Andric         switch (Reg) {
6592cab237bSDimitry Andric         case AMDGPU::EXEC:
6602cab237bSDimitry Andric         case AMDGPU::EXEC_LO:
6612cab237bSDimitry Andric         case AMDGPU::EXEC_HI:
6622cab237bSDimitry Andric         case AMDGPU::SCC:
6632cab237bSDimitry Andric         case AMDGPU::M0:
6642cab237bSDimitry Andric         case AMDGPU::SRC_SHARED_BASE:
6652cab237bSDimitry Andric         case AMDGPU::SRC_SHARED_LIMIT:
6662cab237bSDimitry Andric         case AMDGPU::SRC_PRIVATE_BASE:
6672cab237bSDimitry Andric         case AMDGPU::SRC_PRIVATE_LIMIT:
6682cab237bSDimitry Andric           continue;
6692cab237bSDimitry Andric 
6702cab237bSDimitry Andric         case AMDGPU::NoRegister:
6714ba319b5SDimitry Andric           assert(MI.isDebugInstr());
6722cab237bSDimitry Andric           continue;
6732cab237bSDimitry Andric 
6742cab237bSDimitry Andric         case AMDGPU::VCC:
6752cab237bSDimitry Andric         case AMDGPU::VCC_LO:
6762cab237bSDimitry Andric         case AMDGPU::VCC_HI:
6772cab237bSDimitry Andric           Info.UsesVCC = true;
6782cab237bSDimitry Andric           continue;
6792cab237bSDimitry Andric 
6802cab237bSDimitry Andric         case AMDGPU::FLAT_SCR:
6812cab237bSDimitry Andric         case AMDGPU::FLAT_SCR_LO:
6822cab237bSDimitry Andric         case AMDGPU::FLAT_SCR_HI:
6832cab237bSDimitry Andric           continue;
6842cab237bSDimitry Andric 
6854ba319b5SDimitry Andric         case AMDGPU::XNACK_MASK:
6864ba319b5SDimitry Andric         case AMDGPU::XNACK_MASK_LO:
6874ba319b5SDimitry Andric         case AMDGPU::XNACK_MASK_HI:
6884ba319b5SDimitry Andric           llvm_unreachable("xnack_mask registers should not be used");
6894ba319b5SDimitry Andric 
6902cab237bSDimitry Andric         case AMDGPU::TBA:
6912cab237bSDimitry Andric         case AMDGPU::TBA_LO:
6922cab237bSDimitry Andric         case AMDGPU::TBA_HI:
6932cab237bSDimitry Andric         case AMDGPU::TMA:
6942cab237bSDimitry Andric         case AMDGPU::TMA_LO:
6952cab237bSDimitry Andric         case AMDGPU::TMA_HI:
6962cab237bSDimitry Andric           llvm_unreachable("trap handler registers should not be used");
6972cab237bSDimitry Andric 
6982cab237bSDimitry Andric         default:
6992cab237bSDimitry Andric           break;
7002cab237bSDimitry Andric         }
7012cab237bSDimitry Andric 
7022cab237bSDimitry Andric         if (AMDGPU::SReg_32RegClass.contains(Reg)) {
7032cab237bSDimitry Andric           assert(!AMDGPU::TTMP_32RegClass.contains(Reg) &&
7042cab237bSDimitry Andric                  "trap handler registers should not be used");
7052cab237bSDimitry Andric           IsSGPR = true;
7062cab237bSDimitry Andric           Width = 1;
7072cab237bSDimitry Andric         } else if (AMDGPU::VGPR_32RegClass.contains(Reg)) {
7082cab237bSDimitry Andric           IsSGPR = false;
7092cab237bSDimitry Andric           Width = 1;
7102cab237bSDimitry Andric         } else if (AMDGPU::SReg_64RegClass.contains(Reg)) {
7112cab237bSDimitry Andric           assert(!AMDGPU::TTMP_64RegClass.contains(Reg) &&
7122cab237bSDimitry Andric                  "trap handler registers should not be used");
7132cab237bSDimitry Andric           IsSGPR = true;
7142cab237bSDimitry Andric           Width = 2;
7152cab237bSDimitry Andric         } else if (AMDGPU::VReg_64RegClass.contains(Reg)) {
7162cab237bSDimitry Andric           IsSGPR = false;
7172cab237bSDimitry Andric           Width = 2;
7182cab237bSDimitry Andric         } else if (AMDGPU::VReg_96RegClass.contains(Reg)) {
7192cab237bSDimitry Andric           IsSGPR = false;
7202cab237bSDimitry Andric           Width = 3;
7212cab237bSDimitry Andric         } else if (AMDGPU::SReg_128RegClass.contains(Reg)) {
722da09e106SDimitry Andric           assert(!AMDGPU::TTMP_128RegClass.contains(Reg) &&
723da09e106SDimitry Andric             "trap handler registers should not be used");
7242cab237bSDimitry Andric           IsSGPR = true;
7252cab237bSDimitry Andric           Width = 4;
7262cab237bSDimitry Andric         } else if (AMDGPU::VReg_128RegClass.contains(Reg)) {
7272cab237bSDimitry Andric           IsSGPR = false;
7282cab237bSDimitry Andric           Width = 4;
7292cab237bSDimitry Andric         } else if (AMDGPU::SReg_256RegClass.contains(Reg)) {
730da09e106SDimitry Andric           assert(!AMDGPU::TTMP_256RegClass.contains(Reg) &&
731da09e106SDimitry Andric             "trap handler registers should not be used");
7322cab237bSDimitry Andric           IsSGPR = true;
7332cab237bSDimitry Andric           Width = 8;
7342cab237bSDimitry Andric         } else if (AMDGPU::VReg_256RegClass.contains(Reg)) {
7352cab237bSDimitry Andric           IsSGPR = false;
7362cab237bSDimitry Andric           Width = 8;
7372cab237bSDimitry Andric         } else if (AMDGPU::SReg_512RegClass.contains(Reg)) {
738da09e106SDimitry Andric           assert(!AMDGPU::TTMP_512RegClass.contains(Reg) &&
739da09e106SDimitry Andric             "trap handler registers should not be used");
7402cab237bSDimitry Andric           IsSGPR = true;
7412cab237bSDimitry Andric           Width = 16;
7422cab237bSDimitry Andric         } else if (AMDGPU::VReg_512RegClass.contains(Reg)) {
7432cab237bSDimitry Andric           IsSGPR = false;
7442cab237bSDimitry Andric           Width = 16;
7452cab237bSDimitry Andric         } else {
7462cab237bSDimitry Andric           llvm_unreachable("Unknown register class");
7472cab237bSDimitry Andric         }
7482cab237bSDimitry Andric         unsigned HWReg = TRI.getHWRegIndex(Reg);
7492cab237bSDimitry Andric         int MaxUsed = HWReg + Width - 1;
7502cab237bSDimitry Andric         if (IsSGPR) {
7512cab237bSDimitry Andric           MaxSGPR = MaxUsed > MaxSGPR ? MaxUsed : MaxSGPR;
7522cab237bSDimitry Andric         } else {
7532cab237bSDimitry Andric           MaxVGPR = MaxUsed > MaxVGPR ? MaxUsed : MaxVGPR;
7542cab237bSDimitry Andric         }
7552cab237bSDimitry Andric       }
7562cab237bSDimitry Andric 
7572cab237bSDimitry Andric       if (MI.isCall()) {
7582cab237bSDimitry Andric         // Pseudo used just to encode the underlying global. Is there a better
7592cab237bSDimitry Andric         // way to track this?
7602cab237bSDimitry Andric 
7612cab237bSDimitry Andric         const MachineOperand *CalleeOp
7622cab237bSDimitry Andric           = TII->getNamedOperand(MI, AMDGPU::OpName::callee);
7632cab237bSDimitry Andric         const Function *Callee = cast<Function>(CalleeOp->getGlobal());
7642cab237bSDimitry Andric         if (Callee->isDeclaration()) {
7652cab237bSDimitry Andric           // If this is a call to an external function, we can't do much. Make
7662cab237bSDimitry Andric           // conservative guesses.
7672cab237bSDimitry Andric 
7682cab237bSDimitry Andric           // 48 SGPRs - vcc, - flat_scr, -xnack
7694ba319b5SDimitry Andric           int MaxSGPRGuess =
770*b5893f02SDimitry Andric               47 - IsaInfo::getNumExtraSGPRs(getSTI(), true,
7712cab237bSDimitry Andric                                              ST.hasFlatAddressSpace());
7722cab237bSDimitry Andric           MaxSGPR = std::max(MaxSGPR, MaxSGPRGuess);
7732cab237bSDimitry Andric           MaxVGPR = std::max(MaxVGPR, 23);
7742cab237bSDimitry Andric 
7752cab237bSDimitry Andric           CalleeFrameSize = std::max(CalleeFrameSize, UINT64_C(16384));
7762cab237bSDimitry Andric           Info.UsesVCC = true;
7772cab237bSDimitry Andric           Info.UsesFlatScratch = ST.hasFlatAddressSpace();
7782cab237bSDimitry Andric           Info.HasDynamicallySizedStack = true;
7792cab237bSDimitry Andric         } else {
7802cab237bSDimitry Andric           // We force CodeGen to run in SCC order, so the callee's register
7812cab237bSDimitry Andric           // usage etc. should be the cumulative usage of all callees.
7822cab237bSDimitry Andric           auto I = CallGraphResourceInfo.find(Callee);
7832cab237bSDimitry Andric           assert(I != CallGraphResourceInfo.end() &&
7842cab237bSDimitry Andric                  "callee should have been handled before caller");
7852cab237bSDimitry Andric 
7862cab237bSDimitry Andric           MaxSGPR = std::max(I->second.NumExplicitSGPR - 1, MaxSGPR);
7872cab237bSDimitry Andric           MaxVGPR = std::max(I->second.NumVGPR - 1, MaxVGPR);
7882cab237bSDimitry Andric           CalleeFrameSize
7892cab237bSDimitry Andric             = std::max(I->second.PrivateSegmentSize, CalleeFrameSize);
7902cab237bSDimitry Andric           Info.UsesVCC |= I->second.UsesVCC;
7912cab237bSDimitry Andric           Info.UsesFlatScratch |= I->second.UsesFlatScratch;
7922cab237bSDimitry Andric           Info.HasDynamicallySizedStack |= I->second.HasDynamicallySizedStack;
7932cab237bSDimitry Andric           Info.HasRecursion |= I->second.HasRecursion;
7942cab237bSDimitry Andric         }
7952cab237bSDimitry Andric 
7962cab237bSDimitry Andric         if (!Callee->doesNotRecurse())
7972cab237bSDimitry Andric           Info.HasRecursion = true;
7982cab237bSDimitry Andric       }
7992cab237bSDimitry Andric     }
8002cab237bSDimitry Andric   }
8012cab237bSDimitry Andric 
8022cab237bSDimitry Andric   Info.NumExplicitSGPR = MaxSGPR + 1;
8032cab237bSDimitry Andric   Info.NumVGPR = MaxVGPR + 1;
8042cab237bSDimitry Andric   Info.PrivateSegmentSize += CalleeFrameSize;
8052cab237bSDimitry Andric 
8062cab237bSDimitry Andric   return Info;
8072cab237bSDimitry Andric }
8082cab237bSDimitry Andric 
getSIProgramInfo(SIProgramInfo & ProgInfo,const MachineFunction & MF)809f37b6182SDimitry Andric void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo,
810f37b6182SDimitry Andric                                         const MachineFunction &MF) {
811f37b6182SDimitry Andric   SIFunctionResourceInfo Info = analyzeResourceUsage(MF);
812f37b6182SDimitry Andric 
813f37b6182SDimitry Andric   ProgInfo.NumVGPR = Info.NumVGPR;
814f37b6182SDimitry Andric   ProgInfo.NumSGPR = Info.NumExplicitSGPR;
815f37b6182SDimitry Andric   ProgInfo.ScratchSize = Info.PrivateSegmentSize;
816f37b6182SDimitry Andric   ProgInfo.VCCUsed = Info.UsesVCC;
817f37b6182SDimitry Andric   ProgInfo.FlatUsed = Info.UsesFlatScratch;
818f37b6182SDimitry Andric   ProgInfo.DynamicCallStack = Info.HasDynamicallySizedStack || Info.HasRecursion;
819f37b6182SDimitry Andric 
8202cab237bSDimitry Andric   if (!isUInt<32>(ProgInfo.ScratchSize)) {
8212cab237bSDimitry Andric     DiagnosticInfoStackSize DiagStackSize(MF.getFunction(),
8222cab237bSDimitry Andric                                           ProgInfo.ScratchSize, DS_Error);
8232cab237bSDimitry Andric     MF.getFunction().getContext().diagnose(DiagStackSize);
8242cab237bSDimitry Andric   }
8252cab237bSDimitry Andric 
8264ba319b5SDimitry Andric   const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>();
827f37b6182SDimitry Andric   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
828f37b6182SDimitry Andric   const SIInstrInfo *TII = STM.getInstrInfo();
829f37b6182SDimitry Andric   const SIRegisterInfo *RI = &TII->getRegisterInfo();
830f37b6182SDimitry Andric 
8314ba319b5SDimitry Andric   // TODO(scott.linder): The calculations related to SGPR/VGPR blocks are
8324ba319b5SDimitry Andric   // duplicated in part in AMDGPUAsmParser::calculateGPRBlocks, and could be
8334ba319b5SDimitry Andric   // unified.
8344ba319b5SDimitry Andric   unsigned ExtraSGPRs = IsaInfo::getNumExtraSGPRs(
835*b5893f02SDimitry Andric       getSTI(), ProgInfo.VCCUsed, ProgInfo.FlatUsed);
8363ca95b02SDimitry Andric 
837d88c1a5aSDimitry Andric   // Check the addressable register limit before we add ExtraSGPRs.
838d88c1a5aSDimitry Andric   if (STM.getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS &&
839d88c1a5aSDimitry Andric       !STM.hasSGPRInitBug()) {
8407a7e6055SDimitry Andric     unsigned MaxAddressableNumSGPRs = STM.getAddressableNumSGPRs();
8416bc11b14SDimitry Andric     if (ProgInfo.NumSGPR > MaxAddressableNumSGPRs) {
842d88c1a5aSDimitry Andric       // This can happen due to a compiler bug or when using inline asm.
8432cab237bSDimitry Andric       LLVMContext &Ctx = MF.getFunction().getContext();
8442cab237bSDimitry Andric       DiagnosticInfoResourceLimit Diag(MF.getFunction(),
845d88c1a5aSDimitry Andric                                        "addressable scalar registers",
8466bc11b14SDimitry Andric                                        ProgInfo.NumSGPR, DS_Error,
8477a7e6055SDimitry Andric                                        DK_ResourceLimit,
8487a7e6055SDimitry Andric                                        MaxAddressableNumSGPRs);
849d88c1a5aSDimitry Andric       Ctx.diagnose(Diag);
8506bc11b14SDimitry Andric       ProgInfo.NumSGPR = MaxAddressableNumSGPRs - 1;
851d88c1a5aSDimitry Andric     }
852d88c1a5aSDimitry Andric   }
853d88c1a5aSDimitry Andric 
854d88c1a5aSDimitry Andric   // Account for extra SGPRs and VGPRs reserved for debugger use.
8556bc11b14SDimitry Andric   ProgInfo.NumSGPR += ExtraSGPRs;
8564ba319b5SDimitry Andric 
8574ba319b5SDimitry Andric   // Ensure there are enough SGPRs and VGPRs for wave dispatch, where wave
8584ba319b5SDimitry Andric   // dispatch registers are function args.
8594ba319b5SDimitry Andric   unsigned WaveDispatchNumSGPR = 0, WaveDispatchNumVGPR = 0;
8604ba319b5SDimitry Andric   for (auto &Arg : MF.getFunction().args()) {
8614ba319b5SDimitry Andric     unsigned NumRegs = (Arg.getType()->getPrimitiveSizeInBits() + 31) / 32;
8624ba319b5SDimitry Andric     if (Arg.hasAttribute(Attribute::InReg))
8634ba319b5SDimitry Andric       WaveDispatchNumSGPR += NumRegs;
8644ba319b5SDimitry Andric     else
8654ba319b5SDimitry Andric       WaveDispatchNumVGPR += NumRegs;
8664ba319b5SDimitry Andric   }
8674ba319b5SDimitry Andric   ProgInfo.NumSGPR = std::max(ProgInfo.NumSGPR, WaveDispatchNumSGPR);
8684ba319b5SDimitry Andric   ProgInfo.NumVGPR = std::max(ProgInfo.NumVGPR, WaveDispatchNumVGPR);
8698f0fd8f6SDimitry Andric 
870d88c1a5aSDimitry Andric   // Adjust number of registers used to meet default/requested minimum/maximum
871d88c1a5aSDimitry Andric   // number of waves per execution unit request.
872d88c1a5aSDimitry Andric   ProgInfo.NumSGPRsForWavesPerEU = std::max(
8736bc11b14SDimitry Andric     std::max(ProgInfo.NumSGPR, 1u), STM.getMinNumSGPRs(MFI->getMaxWavesPerEU()));
874d88c1a5aSDimitry Andric   ProgInfo.NumVGPRsForWavesPerEU = std::max(
8756bc11b14SDimitry Andric     std::max(ProgInfo.NumVGPR, 1u), STM.getMinNumVGPRs(MFI->getMaxWavesPerEU()));
876d88c1a5aSDimitry Andric 
877d88c1a5aSDimitry Andric   if (STM.getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS ||
878d88c1a5aSDimitry Andric       STM.hasSGPRInitBug()) {
8797a7e6055SDimitry Andric     unsigned MaxAddressableNumSGPRs = STM.getAddressableNumSGPRs();
8807a7e6055SDimitry Andric     if (ProgInfo.NumSGPR > MaxAddressableNumSGPRs) {
8817a7e6055SDimitry Andric       // This can happen due to a compiler bug or when using inline asm to use
8827a7e6055SDimitry Andric       // the registers which are usually reserved for vcc etc.
8832cab237bSDimitry Andric       LLVMContext &Ctx = MF.getFunction().getContext();
8842cab237bSDimitry Andric       DiagnosticInfoResourceLimit Diag(MF.getFunction(),
885d88c1a5aSDimitry Andric                                        "scalar registers",
886d88c1a5aSDimitry Andric                                        ProgInfo.NumSGPR, DS_Error,
8877a7e6055SDimitry Andric                                        DK_ResourceLimit,
8887a7e6055SDimitry Andric                                        MaxAddressableNumSGPRs);
8893ca95b02SDimitry Andric       Ctx.diagnose(Diag);
8907a7e6055SDimitry Andric       ProgInfo.NumSGPR = MaxAddressableNumSGPRs;
8917a7e6055SDimitry Andric       ProgInfo.NumSGPRsForWavesPerEU = MaxAddressableNumSGPRs;
892d88c1a5aSDimitry Andric     }
8938f0fd8f6SDimitry Andric   }
8948f0fd8f6SDimitry Andric 
895d88c1a5aSDimitry Andric   if (STM.hasSGPRInitBug()) {
8967a7e6055SDimitry Andric     ProgInfo.NumSGPR =
8977a7e6055SDimitry Andric         AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG;
8987a7e6055SDimitry Andric     ProgInfo.NumSGPRsForWavesPerEU =
8997a7e6055SDimitry Andric         AMDGPU::IsaInfo::FIXED_NUM_SGPRS_FOR_INIT_BUG;
9008f0fd8f6SDimitry Andric   }
9018f0fd8f6SDimitry Andric 
9026bc11b14SDimitry Andric   if (MFI->getNumUserSGPRs() > STM.getMaxNumUserSGPRs()) {
9032cab237bSDimitry Andric     LLVMContext &Ctx = MF.getFunction().getContext();
9042cab237bSDimitry Andric     DiagnosticInfoResourceLimit Diag(MF.getFunction(), "user SGPRs",
9056bc11b14SDimitry Andric                                      MFI->getNumUserSGPRs(), DS_Error);
9063ca95b02SDimitry Andric     Ctx.diagnose(Diag);
9073ca95b02SDimitry Andric   }
9083ca95b02SDimitry Andric 
909d88c1a5aSDimitry Andric   if (MFI->getLDSSize() > static_cast<unsigned>(STM.getLocalMemorySize())) {
9102cab237bSDimitry Andric     LLVMContext &Ctx = MF.getFunction().getContext();
9112cab237bSDimitry Andric     DiagnosticInfoResourceLimit Diag(MF.getFunction(), "local memory",
912d88c1a5aSDimitry Andric                                      MFI->getLDSSize(), DS_Error);
9133ca95b02SDimitry Andric     Ctx.diagnose(Diag);
9147d523365SDimitry Andric   }
9157d523365SDimitry Andric 
9164ba319b5SDimitry Andric   ProgInfo.SGPRBlocks = IsaInfo::getNumSGPRBlocks(
917*b5893f02SDimitry Andric       &STM, ProgInfo.NumSGPRsForWavesPerEU);
9184ba319b5SDimitry Andric   ProgInfo.VGPRBlocks = IsaInfo::getNumVGPRBlocks(
919*b5893f02SDimitry Andric       &STM, ProgInfo.NumVGPRsForWavesPerEU);
9207a7e6055SDimitry Andric 
9217a7e6055SDimitry Andric   // Update DebuggerWavefrontPrivateSegmentOffsetSGPR and
9227a7e6055SDimitry Andric   // DebuggerPrivateSegmentBufferSGPR fields if "amdgpu-debugger-emit-prologue"
9237a7e6055SDimitry Andric   // attribute was requested.
9247a7e6055SDimitry Andric   if (STM.debuggerEmitPrologue()) {
9257a7e6055SDimitry Andric     ProgInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR =
9267a7e6055SDimitry Andric       RI->getHWRegIndex(MFI->getScratchWaveOffsetReg());
9277a7e6055SDimitry Andric     ProgInfo.DebuggerPrivateSegmentBufferSGPR =
9287a7e6055SDimitry Andric       RI->getHWRegIndex(MFI->getScratchRSrcReg());
9297a7e6055SDimitry Andric   }
930d88c1a5aSDimitry Andric 
9318f0fd8f6SDimitry Andric   // Set the value to initialize FP_ROUND and FP_DENORM parts of the mode
9328f0fd8f6SDimitry Andric   // register.
9338f0fd8f6SDimitry Andric   ProgInfo.FloatMode = getFPMode(MF);
9348f0fd8f6SDimitry Andric 
935d88c1a5aSDimitry Andric   ProgInfo.IEEEMode = STM.enableIEEEBit(MF);
9368f0fd8f6SDimitry Andric 
9373ca95b02SDimitry Andric   // Make clamp modifier on NaN input returns 0.
9387a7e6055SDimitry Andric   ProgInfo.DX10Clamp = STM.enableDX10Clamp();
9398f0fd8f6SDimitry Andric 
9408f0fd8f6SDimitry Andric   unsigned LDSAlignShift;
9414ba319b5SDimitry Andric   if (STM.getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) {
9428f0fd8f6SDimitry Andric     // LDS is allocated in 64 dword blocks.
9438f0fd8f6SDimitry Andric     LDSAlignShift = 8;
9448f0fd8f6SDimitry Andric   } else {
9458f0fd8f6SDimitry Andric     // LDS is allocated in 128 dword blocks.
9468f0fd8f6SDimitry Andric     LDSAlignShift = 9;
9478f0fd8f6SDimitry Andric   }
9488f0fd8f6SDimitry Andric 
949d88c1a5aSDimitry Andric   unsigned LDSSpillSize =
9506bc11b14SDimitry Andric     MFI->getLDSWaveSpillSize() * MFI->getMaxFlatWorkGroupSize();
9518f0fd8f6SDimitry Andric 
952d88c1a5aSDimitry Andric   ProgInfo.LDSSize = MFI->getLDSSize() + LDSSpillSize;
9538f0fd8f6SDimitry Andric   ProgInfo.LDSBlocks =
9543ca95b02SDimitry Andric       alignTo(ProgInfo.LDSSize, 1ULL << LDSAlignShift) >> LDSAlignShift;
9558f0fd8f6SDimitry Andric 
9568f0fd8f6SDimitry Andric   // Scratch is allocated in 256 dword blocks.
9578f0fd8f6SDimitry Andric   unsigned ScratchAlignShift = 10;
9588f0fd8f6SDimitry Andric   // We need to program the hardware with the amount of scratch memory that
9598f0fd8f6SDimitry Andric   // is used by the entire wave.  ProgInfo.ScratchSize is the amount of
9608f0fd8f6SDimitry Andric   // scratch memory used per thread.
9618f0fd8f6SDimitry Andric   ProgInfo.ScratchBlocks =
9623ca95b02SDimitry Andric       alignTo(ProgInfo.ScratchSize * STM.getWavefrontSize(),
9633ca95b02SDimitry Andric               1ULL << ScratchAlignShift) >>
9643ca95b02SDimitry Andric       ScratchAlignShift;
9658f0fd8f6SDimitry Andric 
9668f0fd8f6SDimitry Andric   ProgInfo.ComputePGMRSrc1 =
9678f0fd8f6SDimitry Andric       S_00B848_VGPRS(ProgInfo.VGPRBlocks) |
9688f0fd8f6SDimitry Andric       S_00B848_SGPRS(ProgInfo.SGPRBlocks) |
9698f0fd8f6SDimitry Andric       S_00B848_PRIORITY(ProgInfo.Priority) |
9708f0fd8f6SDimitry Andric       S_00B848_FLOAT_MODE(ProgInfo.FloatMode) |
9718f0fd8f6SDimitry Andric       S_00B848_PRIV(ProgInfo.Priv) |
9728f0fd8f6SDimitry Andric       S_00B848_DX10_CLAMP(ProgInfo.DX10Clamp) |
9737d523365SDimitry Andric       S_00B848_DEBUG_MODE(ProgInfo.DebugMode) |
9748f0fd8f6SDimitry Andric       S_00B848_IEEE_MODE(ProgInfo.IEEEMode);
9758f0fd8f6SDimitry Andric 
9767d523365SDimitry Andric   // 0 = X, 1 = XY, 2 = XYZ
9777d523365SDimitry Andric   unsigned TIDIGCompCnt = 0;
9787d523365SDimitry Andric   if (MFI->hasWorkItemIDZ())
9797d523365SDimitry Andric     TIDIGCompCnt = 2;
9807d523365SDimitry Andric   else if (MFI->hasWorkItemIDY())
9817d523365SDimitry Andric     TIDIGCompCnt = 1;
9827d523365SDimitry Andric 
9838f0fd8f6SDimitry Andric   ProgInfo.ComputePGMRSrc2 =
9848f0fd8f6SDimitry Andric       S_00B84C_SCRATCH_EN(ProgInfo.ScratchBlocks > 0) |
9857d523365SDimitry Andric       S_00B84C_USER_SGPR(MFI->getNumUserSGPRs()) |
9864ba319b5SDimitry Andric       // For AMDHSA, TRAP_HANDLER must be zero, as it is populated by the CP.
9874ba319b5SDimitry Andric       S_00B84C_TRAP_HANDLER(STM.isAmdHsaOS() ? 0 : STM.isTrapHandlerEnabled()) |
9887d523365SDimitry Andric       S_00B84C_TGID_X_EN(MFI->hasWorkGroupIDX()) |
9897d523365SDimitry Andric       S_00B84C_TGID_Y_EN(MFI->hasWorkGroupIDY()) |
9907d523365SDimitry Andric       S_00B84C_TGID_Z_EN(MFI->hasWorkGroupIDZ()) |
9917d523365SDimitry Andric       S_00B84C_TG_SIZE_EN(MFI->hasWorkGroupInfo()) |
9927d523365SDimitry Andric       S_00B84C_TIDIG_COMP_CNT(TIDIGCompCnt) |
9937d523365SDimitry Andric       S_00B84C_EXCP_EN_MSB(0) |
9940f5676f4SDimitry Andric       // For AMDHSA, LDS_SIZE must be zero, as it is populated by the CP.
9950f5676f4SDimitry Andric       S_00B84C_LDS_SIZE(STM.isAmdHsaOS() ? 0 : ProgInfo.LDSBlocks) |
9967d523365SDimitry Andric       S_00B84C_EXCP_EN(0);
9978f0fd8f6SDimitry Andric }
9988f0fd8f6SDimitry Andric 
getRsrcReg(CallingConv::ID CallConv)9993ca95b02SDimitry Andric static unsigned getRsrcReg(CallingConv::ID CallConv) {
10003ca95b02SDimitry Andric   switch (CallConv) {
1001d88c1a5aSDimitry Andric   default: LLVM_FALLTHROUGH;
10023ca95b02SDimitry Andric   case CallingConv::AMDGPU_CS: return R_00B848_COMPUTE_PGM_RSRC1;
10032cab237bSDimitry Andric   case CallingConv::AMDGPU_LS: return R_00B528_SPI_SHADER_PGM_RSRC1_LS;
1004f37b6182SDimitry Andric   case CallingConv::AMDGPU_HS: return R_00B428_SPI_SHADER_PGM_RSRC1_HS;
10052cab237bSDimitry Andric   case CallingConv::AMDGPU_ES: return R_00B328_SPI_SHADER_PGM_RSRC1_ES;
10063ca95b02SDimitry Andric   case CallingConv::AMDGPU_GS: return R_00B228_SPI_SHADER_PGM_RSRC1_GS;
10073ca95b02SDimitry Andric   case CallingConv::AMDGPU_VS: return R_00B128_SPI_SHADER_PGM_RSRC1_VS;
10082cab237bSDimitry Andric   case CallingConv::AMDGPU_PS: return R_00B028_SPI_SHADER_PGM_RSRC1_PS;
10098f0fd8f6SDimitry Andric   }
10108f0fd8f6SDimitry Andric }
10118f0fd8f6SDimitry Andric 
EmitProgramInfoSI(const MachineFunction & MF,const SIProgramInfo & CurrentProgramInfo)10128f0fd8f6SDimitry Andric void AMDGPUAsmPrinter::EmitProgramInfoSI(const MachineFunction &MF,
1013f37b6182SDimitry Andric                                          const SIProgramInfo &CurrentProgramInfo) {
10148f0fd8f6SDimitry Andric   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
10152cab237bSDimitry Andric   unsigned RsrcReg = getRsrcReg(MF.getFunction().getCallingConv());
10168f0fd8f6SDimitry Andric 
10172cab237bSDimitry Andric   if (AMDGPU::isCompute(MF.getFunction().getCallingConv())) {
10188f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_00B848_COMPUTE_PGM_RSRC1, 4);
10198f0fd8f6SDimitry Andric 
1020f37b6182SDimitry Andric     OutStreamer->EmitIntValue(CurrentProgramInfo.ComputePGMRSrc1, 4);
10218f0fd8f6SDimitry Andric 
10228f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4);
1023f37b6182SDimitry Andric     OutStreamer->EmitIntValue(CurrentProgramInfo.ComputePGMRSrc2, 4);
10248f0fd8f6SDimitry Andric 
10258f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_00B860_COMPUTE_TMPRING_SIZE, 4);
1026f37b6182SDimitry Andric     OutStreamer->EmitIntValue(S_00B860_WAVESIZE(CurrentProgramInfo.ScratchBlocks), 4);
10278f0fd8f6SDimitry Andric 
10288f0fd8f6SDimitry Andric     // TODO: Should probably note flat usage somewhere. SC emits a "FlatPtr32 =
10298f0fd8f6SDimitry Andric     // 0" comment but I don't see a corresponding field in the register spec.
10308f0fd8f6SDimitry Andric   } else {
10318f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(RsrcReg, 4);
1032f37b6182SDimitry Andric     OutStreamer->EmitIntValue(S_00B028_VGPRS(CurrentProgramInfo.VGPRBlocks) |
1033f37b6182SDimitry Andric                               S_00B028_SGPRS(CurrentProgramInfo.SGPRBlocks), 4);
10348f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_0286E8_SPI_TMPRING_SIZE, 4);
1035*b5893f02SDimitry Andric     OutStreamer->EmitIntValue(
1036*b5893f02SDimitry Andric         S_0286E8_WAVESIZE(CurrentProgramInfo.ScratchBlocks), 4);
10374ba319b5SDimitry Andric   }
10384ba319b5SDimitry Andric 
10392cab237bSDimitry Andric   if (MF.getFunction().getCallingConv() == CallingConv::AMDGPU_PS) {
10404ba319b5SDimitry Andric     OutStreamer->EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4);
10414ba319b5SDimitry Andric     OutStreamer->EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(CurrentProgramInfo.LDSBlocks), 4);
10428f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4);
10437a7e6055SDimitry Andric     OutStreamer->EmitIntValue(MFI->getPSInputEnable(), 4);
1044444ed5c5SDimitry Andric     OutStreamer->EmitIntValue(R_0286D0_SPI_PS_INPUT_ADDR, 4);
1045444ed5c5SDimitry Andric     OutStreamer->EmitIntValue(MFI->getPSInputAddr(), 4);
10468f0fd8f6SDimitry Andric   }
10473ca95b02SDimitry Andric 
10483ca95b02SDimitry Andric   OutStreamer->EmitIntValue(R_SPILLED_SGPRS, 4);
10493ca95b02SDimitry Andric   OutStreamer->EmitIntValue(MFI->getNumSpilledSGPRs(), 4);
10503ca95b02SDimitry Andric   OutStreamer->EmitIntValue(R_SPILLED_VGPRS, 4);
10513ca95b02SDimitry Andric   OutStreamer->EmitIntValue(MFI->getNumSpilledVGPRs(), 4);
10523ca95b02SDimitry Andric }
10533ca95b02SDimitry Andric 
10542cab237bSDimitry Andric // This is the equivalent of EmitProgramInfoSI above, but for when the OS type
10552cab237bSDimitry Andric // is AMDPAL.  It stores each compute/SPI register setting and other PAL
10562cab237bSDimitry Andric // metadata items into the PALMetadataMap, combining with any provided by the
10572cab237bSDimitry Andric // frontend as LLVM metadata. Once all functions are written, PALMetadataMap is
10582cab237bSDimitry Andric // then written as a single block in the .note section.
EmitPALMetadata(const MachineFunction & MF,const SIProgramInfo & CurrentProgramInfo)10592cab237bSDimitry Andric void AMDGPUAsmPrinter::EmitPALMetadata(const MachineFunction &MF,
10602cab237bSDimitry Andric        const SIProgramInfo &CurrentProgramInfo) {
10612cab237bSDimitry Andric   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
10622cab237bSDimitry Andric   // Given the calling convention, calculate the register number for rsrc1. In
10632cab237bSDimitry Andric   // principle the register number could change in future hardware, but we know
10642cab237bSDimitry Andric   // it is the same for gfx6-9 (except that LS and ES don't exist on gfx9), so
10652cab237bSDimitry Andric   // we can use the same fixed value that .AMDGPU.config has for Mesa. Note
10662cab237bSDimitry Andric   // that we use a register number rather than a byte offset, so we need to
10672cab237bSDimitry Andric   // divide by 4.
10682cab237bSDimitry Andric   unsigned Rsrc1Reg = getRsrcReg(MF.getFunction().getCallingConv()) / 4;
10692cab237bSDimitry Andric   unsigned Rsrc2Reg = Rsrc1Reg + 1;
10702cab237bSDimitry Andric   // Also calculate the PAL metadata key for *S_SCRATCH_SIZE. It can be used
10712cab237bSDimitry Andric   // with a constant offset to access any non-register shader-specific PAL
10722cab237bSDimitry Andric   // metadata key.
10732cab237bSDimitry Andric   unsigned ScratchSizeKey = PALMD::Key::CS_SCRATCH_SIZE;
10742cab237bSDimitry Andric   switch (MF.getFunction().getCallingConv()) {
10752cab237bSDimitry Andric     case CallingConv::AMDGPU_PS:
10762cab237bSDimitry Andric       ScratchSizeKey = PALMD::Key::PS_SCRATCH_SIZE;
10772cab237bSDimitry Andric       break;
10782cab237bSDimitry Andric     case CallingConv::AMDGPU_VS:
10792cab237bSDimitry Andric       ScratchSizeKey = PALMD::Key::VS_SCRATCH_SIZE;
10802cab237bSDimitry Andric       break;
10812cab237bSDimitry Andric     case CallingConv::AMDGPU_GS:
10822cab237bSDimitry Andric       ScratchSizeKey = PALMD::Key::GS_SCRATCH_SIZE;
10832cab237bSDimitry Andric       break;
10842cab237bSDimitry Andric     case CallingConv::AMDGPU_ES:
10852cab237bSDimitry Andric       ScratchSizeKey = PALMD::Key::ES_SCRATCH_SIZE;
10862cab237bSDimitry Andric       break;
10872cab237bSDimitry Andric     case CallingConv::AMDGPU_HS:
10882cab237bSDimitry Andric       ScratchSizeKey = PALMD::Key::HS_SCRATCH_SIZE;
10892cab237bSDimitry Andric       break;
10902cab237bSDimitry Andric     case CallingConv::AMDGPU_LS:
10912cab237bSDimitry Andric       ScratchSizeKey = PALMD::Key::LS_SCRATCH_SIZE;
10922cab237bSDimitry Andric       break;
10932cab237bSDimitry Andric   }
10942cab237bSDimitry Andric   unsigned NumUsedVgprsKey = ScratchSizeKey +
10952cab237bSDimitry Andric       PALMD::Key::VS_NUM_USED_VGPRS - PALMD::Key::VS_SCRATCH_SIZE;
10962cab237bSDimitry Andric   unsigned NumUsedSgprsKey = ScratchSizeKey +
10972cab237bSDimitry Andric       PALMD::Key::VS_NUM_USED_SGPRS - PALMD::Key::VS_SCRATCH_SIZE;
10982cab237bSDimitry Andric   PALMetadataMap[NumUsedVgprsKey] = CurrentProgramInfo.NumVGPRsForWavesPerEU;
10992cab237bSDimitry Andric   PALMetadataMap[NumUsedSgprsKey] = CurrentProgramInfo.NumSGPRsForWavesPerEU;
11002cab237bSDimitry Andric   if (AMDGPU::isCompute(MF.getFunction().getCallingConv())) {
11012cab237bSDimitry Andric     PALMetadataMap[Rsrc1Reg] |= CurrentProgramInfo.ComputePGMRSrc1;
11022cab237bSDimitry Andric     PALMetadataMap[Rsrc2Reg] |= CurrentProgramInfo.ComputePGMRSrc2;
11032cab237bSDimitry Andric     // ScratchSize is in bytes, 16 aligned.
11042cab237bSDimitry Andric     PALMetadataMap[ScratchSizeKey] |=
11052cab237bSDimitry Andric         alignTo(CurrentProgramInfo.ScratchSize, 16);
11062cab237bSDimitry Andric   } else {
11072cab237bSDimitry Andric     PALMetadataMap[Rsrc1Reg] |= S_00B028_VGPRS(CurrentProgramInfo.VGPRBlocks) |
11082cab237bSDimitry Andric         S_00B028_SGPRS(CurrentProgramInfo.SGPRBlocks);
11092cab237bSDimitry Andric     if (CurrentProgramInfo.ScratchBlocks > 0)
11102cab237bSDimitry Andric       PALMetadataMap[Rsrc2Reg] |= S_00B84C_SCRATCH_EN(1);
11112cab237bSDimitry Andric     // ScratchSize is in bytes, 16 aligned.
11122cab237bSDimitry Andric     PALMetadataMap[ScratchSizeKey] |=
11132cab237bSDimitry Andric         alignTo(CurrentProgramInfo.ScratchSize, 16);
11142cab237bSDimitry Andric   }
11152cab237bSDimitry Andric   if (MF.getFunction().getCallingConv() == CallingConv::AMDGPU_PS) {
11162cab237bSDimitry Andric     PALMetadataMap[Rsrc2Reg] |=
11172cab237bSDimitry Andric         S_00B02C_EXTRA_LDS_SIZE(CurrentProgramInfo.LDSBlocks);
11182cab237bSDimitry Andric     PALMetadataMap[R_0286CC_SPI_PS_INPUT_ENA / 4] |= MFI->getPSInputEnable();
11192cab237bSDimitry Andric     PALMetadataMap[R_0286D0_SPI_PS_INPUT_ADDR / 4] |= MFI->getPSInputAddr();
11202cab237bSDimitry Andric   }
11212cab237bSDimitry Andric }
11222cab237bSDimitry Andric 
11233ca95b02SDimitry Andric // This is supposed to be log2(Size)
getElementByteSizeValue(unsigned Size)11243ca95b02SDimitry Andric static amd_element_byte_size_t getElementByteSizeValue(unsigned Size) {
11253ca95b02SDimitry Andric   switch (Size) {
11263ca95b02SDimitry Andric   case 4:
11273ca95b02SDimitry Andric     return AMD_ELEMENT_4_BYTES;
11283ca95b02SDimitry Andric   case 8:
11293ca95b02SDimitry Andric     return AMD_ELEMENT_8_BYTES;
11303ca95b02SDimitry Andric   case 16:
11313ca95b02SDimitry Andric     return AMD_ELEMENT_16_BYTES;
11323ca95b02SDimitry Andric   default:
11333ca95b02SDimitry Andric     llvm_unreachable("invalid private_element_size");
11343ca95b02SDimitry Andric   }
11358f0fd8f6SDimitry Andric }
11368f0fd8f6SDimitry Andric 
getAmdKernelCode(amd_kernel_code_t & Out,const SIProgramInfo & CurrentProgramInfo,const MachineFunction & MF) const11377a7e6055SDimitry Andric void AMDGPUAsmPrinter::getAmdKernelCode(amd_kernel_code_t &Out,
1138f37b6182SDimitry Andric                                         const SIProgramInfo &CurrentProgramInfo,
11397a7e6055SDimitry Andric                                         const MachineFunction &MF) const {
11404ba319b5SDimitry Andric   const Function &F = MF.getFunction();
11414ba319b5SDimitry Andric   assert(F.getCallingConv() == CallingConv::AMDGPU_KERNEL ||
11424ba319b5SDimitry Andric          F.getCallingConv() == CallingConv::SPIR_KERNEL);
11434ba319b5SDimitry Andric 
11448f0fd8f6SDimitry Andric   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
11454ba319b5SDimitry Andric   const GCNSubtarget &STM = MF.getSubtarget<GCNSubtarget>();
11468f0fd8f6SDimitry Andric 
1147*b5893f02SDimitry Andric   AMDGPU::initDefaultAMDKernelCodeT(Out, getSTI());
11488f0fd8f6SDimitry Andric 
11497a7e6055SDimitry Andric   Out.compute_pgm_resource_registers =
1150f37b6182SDimitry Andric       CurrentProgramInfo.ComputePGMRSrc1 |
1151f37b6182SDimitry Andric       (CurrentProgramInfo.ComputePGMRSrc2 << 32);
11527a7e6055SDimitry Andric   Out.code_properties = AMD_CODE_PROPERTY_IS_PTR64;
11537d523365SDimitry Andric 
1154f37b6182SDimitry Andric   if (CurrentProgramInfo.DynamicCallStack)
1155f37b6182SDimitry Andric     Out.code_properties |= AMD_CODE_PROPERTY_IS_DYNAMIC_CALLSTACK;
1156f37b6182SDimitry Andric 
11577a7e6055SDimitry Andric   AMD_HSA_BITS_SET(Out.code_properties,
11583ca95b02SDimitry Andric                    AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE,
11593ca95b02SDimitry Andric                    getElementByteSizeValue(STM.getMaxPrivateElementSize()));
11603ca95b02SDimitry Andric 
11617d523365SDimitry Andric   if (MFI->hasPrivateSegmentBuffer()) {
11627a7e6055SDimitry Andric     Out.code_properties |=
11637d523365SDimitry Andric       AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER;
11647d523365SDimitry Andric   }
11657d523365SDimitry Andric 
11667d523365SDimitry Andric   if (MFI->hasDispatchPtr())
11677a7e6055SDimitry Andric     Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
11687d523365SDimitry Andric 
11697d523365SDimitry Andric   if (MFI->hasQueuePtr())
11707a7e6055SDimitry Andric     Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR;
11717d523365SDimitry Andric 
11727d523365SDimitry Andric   if (MFI->hasKernargSegmentPtr())
11737a7e6055SDimitry Andric     Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR;
11747d523365SDimitry Andric 
11757d523365SDimitry Andric   if (MFI->hasDispatchID())
11767a7e6055SDimitry Andric     Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID;
11777d523365SDimitry Andric 
11787d523365SDimitry Andric   if (MFI->hasFlatScratchInit())
11797a7e6055SDimitry Andric     Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT;
11807d523365SDimitry Andric 
11817d523365SDimitry Andric   if (MFI->hasDispatchPtr())
11827a7e6055SDimitry Andric     Out.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
11838f0fd8f6SDimitry Andric 
11843ca95b02SDimitry Andric   if (STM.debuggerSupported())
11857a7e6055SDimitry Andric     Out.code_properties |= AMD_CODE_PROPERTY_IS_DEBUG_SUPPORTED;
11863ca95b02SDimitry Andric 
11874d0b32cdSDimitry Andric   if (STM.isXNACKEnabled())
11887a7e6055SDimitry Andric     Out.code_properties |= AMD_CODE_PROPERTY_IS_XNACK_SUPPORTED;
11894d0b32cdSDimitry Andric 
11904ba319b5SDimitry Andric   unsigned MaxKernArgAlign;
11914ba319b5SDimitry Andric   Out.kernarg_segment_byte_size = STM.getKernArgSegmentSize(F, MaxKernArgAlign);
1192f37b6182SDimitry Andric   Out.wavefront_sgpr_count = CurrentProgramInfo.NumSGPR;
1193f37b6182SDimitry Andric   Out.workitem_vgpr_count = CurrentProgramInfo.NumVGPR;
1194f37b6182SDimitry Andric   Out.workitem_private_segment_byte_size = CurrentProgramInfo.ScratchSize;
1195f37b6182SDimitry Andric   Out.workgroup_group_segment_byte_size = CurrentProgramInfo.LDSSize;
11963ca95b02SDimitry Andric 
1197d88c1a5aSDimitry Andric   // These alignment values are specified in powers of two, so alignment =
1198d88c1a5aSDimitry Andric   // 2^n.  The minimum alignment is 2^4 = 16.
11997a7e6055SDimitry Andric   Out.kernarg_segment_alignment = std::max((size_t)4,
12004ba319b5SDimitry Andric       countTrailingZeros(MaxKernArgAlign));
1201d88c1a5aSDimitry Andric 
12023ca95b02SDimitry Andric   if (STM.debuggerEmitPrologue()) {
12037a7e6055SDimitry Andric     Out.debug_wavefront_private_segment_offset_sgpr =
1204f37b6182SDimitry Andric       CurrentProgramInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR;
12057a7e6055SDimitry Andric     Out.debug_private_segment_buffer_sgpr =
1206f37b6182SDimitry Andric       CurrentProgramInfo.DebuggerPrivateSegmentBufferSGPR;
12073ca95b02SDimitry Andric   }
12088f0fd8f6SDimitry Andric }
12098f0fd8f6SDimitry Andric 
PrintAsmOperand(const MachineInstr * MI,unsigned OpNo,unsigned AsmVariant,const char * ExtraCode,raw_ostream & O)12108f0fd8f6SDimitry Andric bool AMDGPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
12118f0fd8f6SDimitry Andric                                        unsigned AsmVariant,
12128f0fd8f6SDimitry Andric                                        const char *ExtraCode, raw_ostream &O) {
12132cab237bSDimitry Andric   // First try the generic code, which knows about modifiers like 'c' and 'n'.
12142cab237bSDimitry Andric   if (!AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O))
12152cab237bSDimitry Andric     return false;
12162cab237bSDimitry Andric 
12178f0fd8f6SDimitry Andric   if (ExtraCode && ExtraCode[0]) {
12188f0fd8f6SDimitry Andric     if (ExtraCode[1] != 0)
12198f0fd8f6SDimitry Andric       return true; // Unknown modifier.
12208f0fd8f6SDimitry Andric 
12218f0fd8f6SDimitry Andric     switch (ExtraCode[0]) {
12228f0fd8f6SDimitry Andric     case 'r':
12238f0fd8f6SDimitry Andric       break;
12242cab237bSDimitry Andric     default:
12252cab237bSDimitry Andric       return true;
12268f0fd8f6SDimitry Andric     }
12278f0fd8f6SDimitry Andric   }
12288f0fd8f6SDimitry Andric 
12292cab237bSDimitry Andric   // TODO: Should be able to support other operand types like globals.
12302cab237bSDimitry Andric   const MachineOperand &MO = MI->getOperand(OpNo);
12312cab237bSDimitry Andric   if (MO.isReg()) {
12322cab237bSDimitry Andric     AMDGPUInstPrinter::printRegOperand(MO.getReg(), O,
12332cab237bSDimitry Andric                                        *MF->getSubtarget().getRegisterInfo());
12348f0fd8f6SDimitry Andric     return false;
12358f0fd8f6SDimitry Andric   }
12362cab237bSDimitry Andric 
12372cab237bSDimitry Andric   return true;
12382cab237bSDimitry Andric }
1239