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"
203dac3a9bSDimitry Andric #include "MCTargetDesc/AMDGPUTargetStreamer.h"
218f0fd8f6SDimitry Andric #include "InstPrinter/AMDGPUInstPrinter.h"
223dac3a9bSDimitry Andric #include "Utils/AMDGPUBaseInfo.h"
238f0fd8f6SDimitry Andric #include "AMDGPU.h"
248f0fd8f6SDimitry Andric #include "AMDKernelCodeT.h"
258f0fd8f6SDimitry Andric #include "AMDGPUSubtarget.h"
268f0fd8f6SDimitry Andric #include "R600Defines.h"
278f0fd8f6SDimitry Andric #include "R600MachineFunctionInfo.h"
288f0fd8f6SDimitry Andric #include "R600RegisterInfo.h"
298f0fd8f6SDimitry Andric #include "SIDefines.h"
308f0fd8f6SDimitry Andric #include "SIMachineFunctionInfo.h"
318f0fd8f6SDimitry Andric #include "SIRegisterInfo.h"
328f0fd8f6SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
338f0fd8f6SDimitry Andric #include "llvm/MC/MCContext.h"
348f0fd8f6SDimitry Andric #include "llvm/MC/MCSectionELF.h"
358f0fd8f6SDimitry Andric #include "llvm/MC/MCStreamer.h"
368f0fd8f6SDimitry Andric #include "llvm/Support/ELF.h"
378f0fd8f6SDimitry Andric #include "llvm/Support/MathExtras.h"
388f0fd8f6SDimitry Andric #include "llvm/Support/TargetRegistry.h"
398f0fd8f6SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h"
408f0fd8f6SDimitry Andric 
418f0fd8f6SDimitry Andric using namespace llvm;
428f0fd8f6SDimitry Andric 
438f0fd8f6SDimitry Andric // TODO: This should get the default rounding mode from the kernel. We just set
448f0fd8f6SDimitry Andric // the default here, but this could change if the OpenCL rounding mode pragmas
458f0fd8f6SDimitry Andric // are used.
468f0fd8f6SDimitry Andric //
478f0fd8f6SDimitry Andric // The denormal mode here should match what is reported by the OpenCL runtime
488f0fd8f6SDimitry Andric // for the CL_FP_DENORM bit from CL_DEVICE_{HALF|SINGLE|DOUBLE}_FP_CONFIG, but
498f0fd8f6SDimitry Andric // can also be override to flush with the -cl-denorms-are-zero compiler flag.
508f0fd8f6SDimitry Andric //
518f0fd8f6SDimitry Andric // AMD OpenCL only sets flush none and reports CL_FP_DENORM for double
528f0fd8f6SDimitry Andric // precision, and leaves single precision to flush all and does not report
538f0fd8f6SDimitry Andric // CL_FP_DENORM for CL_DEVICE_SINGLE_FP_CONFIG. Mesa's OpenCL currently reports
548f0fd8f6SDimitry Andric // CL_FP_DENORM for both.
558f0fd8f6SDimitry Andric //
568f0fd8f6SDimitry Andric // FIXME: It seems some instructions do not support single precision denormals
578f0fd8f6SDimitry Andric // regardless of the mode (exp_*_f32, rcp_*_f32, rsq_*_f32, rsq_*f32, sqrt_f32,
588f0fd8f6SDimitry Andric // and sin_f32, cos_f32 on most parts).
598f0fd8f6SDimitry Andric 
608f0fd8f6SDimitry Andric // We want to use these instructions, and using fp32 denormals also causes
618f0fd8f6SDimitry Andric // instructions to run at the double precision rate for the device so it's
628f0fd8f6SDimitry Andric // probably best to just report no single precision denormals.
638f0fd8f6SDimitry Andric static uint32_t getFPMode(const MachineFunction &F) {
648f0fd8f6SDimitry Andric   const AMDGPUSubtarget& ST = F.getSubtarget<AMDGPUSubtarget>();
658f0fd8f6SDimitry Andric   // TODO: Is there any real use for the flush in only / flush out only modes?
668f0fd8f6SDimitry Andric 
678f0fd8f6SDimitry Andric   uint32_t FP32Denormals =
688f0fd8f6SDimitry Andric     ST.hasFP32Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
698f0fd8f6SDimitry Andric 
708f0fd8f6SDimitry Andric   uint32_t FP64Denormals =
718f0fd8f6SDimitry Andric     ST.hasFP64Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
728f0fd8f6SDimitry Andric 
738f0fd8f6SDimitry Andric   return FP_ROUND_MODE_SP(FP_ROUND_ROUND_TO_NEAREST) |
748f0fd8f6SDimitry Andric          FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_NEAREST) |
758f0fd8f6SDimitry Andric          FP_DENORM_MODE_SP(FP32Denormals) |
768f0fd8f6SDimitry Andric          FP_DENORM_MODE_DP(FP64Denormals);
778f0fd8f6SDimitry Andric }
788f0fd8f6SDimitry Andric 
798f0fd8f6SDimitry Andric static AsmPrinter *
808f0fd8f6SDimitry Andric createAMDGPUAsmPrinterPass(TargetMachine &tm,
818f0fd8f6SDimitry Andric                            std::unique_ptr<MCStreamer> &&Streamer) {
828f0fd8f6SDimitry Andric   return new AMDGPUAsmPrinter(tm, std::move(Streamer));
838f0fd8f6SDimitry Andric }
848f0fd8f6SDimitry Andric 
858f0fd8f6SDimitry Andric extern "C" void LLVMInitializeAMDGPUAsmPrinter() {
868f0fd8f6SDimitry Andric   TargetRegistry::RegisterAsmPrinter(TheAMDGPUTarget, createAMDGPUAsmPrinterPass);
878f0fd8f6SDimitry Andric   TargetRegistry::RegisterAsmPrinter(TheGCNTarget, createAMDGPUAsmPrinterPass);
888f0fd8f6SDimitry Andric }
898f0fd8f6SDimitry Andric 
908f0fd8f6SDimitry Andric AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM,
918f0fd8f6SDimitry Andric                                    std::unique_ptr<MCStreamer> Streamer)
928f0fd8f6SDimitry Andric     : AsmPrinter(TM, std::move(Streamer)) {}
938f0fd8f6SDimitry Andric 
94444ed5c5SDimitry Andric void AMDGPUAsmPrinter::EmitStartOfAsmFile(Module &M) {
95444ed5c5SDimitry Andric   if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
96444ed5c5SDimitry Andric     return;
97444ed5c5SDimitry Andric 
98444ed5c5SDimitry Andric   // Need to construct an MCSubtargetInfo here in case we have no functions
99444ed5c5SDimitry Andric   // in the module.
100444ed5c5SDimitry Andric   std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
101444ed5c5SDimitry Andric         TM.getTargetTriple().str(), TM.getTargetCPU(),
102444ed5c5SDimitry Andric         TM.getTargetFeatureString()));
103444ed5c5SDimitry Andric 
104444ed5c5SDimitry Andric   AMDGPUTargetStreamer *TS =
105444ed5c5SDimitry Andric       static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
106444ed5c5SDimitry Andric 
107444ed5c5SDimitry Andric   TS->EmitDirectiveHSACodeObjectVersion(1, 0);
108444ed5c5SDimitry Andric   AMDGPU::IsaVersion ISA = AMDGPU::getIsaVersion(STI->getFeatureBits());
109444ed5c5SDimitry Andric   TS->EmitDirectiveHSACodeObjectISA(ISA.Major, ISA.Minor, ISA.Stepping,
110444ed5c5SDimitry Andric                                     "AMD", "AMDGPU");
111444ed5c5SDimitry Andric }
112444ed5c5SDimitry Andric 
1133dac3a9bSDimitry Andric void AMDGPUAsmPrinter::EmitFunctionBodyStart() {
1143dac3a9bSDimitry Andric   const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
1153dac3a9bSDimitry Andric   SIProgramInfo KernelInfo;
1163dac3a9bSDimitry Andric   if (STM.isAmdHsaOS()) {
1173dac3a9bSDimitry Andric     getSIProgramInfo(KernelInfo, *MF);
1183dac3a9bSDimitry Andric     EmitAmdKernelCodeT(*MF, KernelInfo);
1193dac3a9bSDimitry Andric   }
1203dac3a9bSDimitry Andric }
1213dac3a9bSDimitry Andric 
1227d523365SDimitry Andric void AMDGPUAsmPrinter::EmitFunctionEntryLabel() {
1237d523365SDimitry Andric   const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
1247d523365SDimitry Andric   const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
1257d523365SDimitry Andric   if (MFI->isKernel() && STM.isAmdHsaOS()) {
1267d523365SDimitry Andric     AMDGPUTargetStreamer *TS =
1277d523365SDimitry Andric         static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
1287d523365SDimitry Andric     TS->EmitAMDGPUSymbolType(CurrentFnSym->getName(),
1297d523365SDimitry Andric                              ELF::STT_AMDGPU_HSA_KERNEL);
1307d523365SDimitry Andric   }
1318f0fd8f6SDimitry Andric 
1327d523365SDimitry Andric   AsmPrinter::EmitFunctionEntryLabel();
1337d523365SDimitry Andric }
1347d523365SDimitry Andric 
1357d523365SDimitry Andric static bool isModuleLinkage(const GlobalValue *GV) {
1367d523365SDimitry Andric   switch (GV->getLinkage()) {
1377d523365SDimitry Andric   case GlobalValue::InternalLinkage:
1387d523365SDimitry Andric   case GlobalValue::CommonLinkage:
1397d523365SDimitry Andric    return true;
1407d523365SDimitry Andric   case GlobalValue::ExternalLinkage:
1417d523365SDimitry Andric    return false;
1427d523365SDimitry Andric   default: llvm_unreachable("unknown linkage type");
1437d523365SDimitry Andric   }
1447d523365SDimitry Andric }
1457d523365SDimitry Andric 
1467d523365SDimitry Andric void AMDGPUAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
1477d523365SDimitry Andric 
1487d523365SDimitry Andric   if (TM.getTargetTriple().getOS() != Triple::AMDHSA) {
1497d523365SDimitry Andric     AsmPrinter::EmitGlobalVariable(GV);
1507d523365SDimitry Andric     return;
1517d523365SDimitry Andric   }
1527d523365SDimitry Andric 
1537d523365SDimitry Andric   if (GV->isDeclaration() || GV->getLinkage() == GlobalValue::PrivateLinkage) {
1547d523365SDimitry Andric     AsmPrinter::EmitGlobalVariable(GV);
1557d523365SDimitry Andric     return;
1567d523365SDimitry Andric   }
1577d523365SDimitry Andric 
1587d523365SDimitry Andric   // Group segment variables aren't emitted in HSA.
1597d523365SDimitry Andric   if (AMDGPU::isGroupSegment(GV))
1607d523365SDimitry Andric     return;
1617d523365SDimitry Andric 
1627d523365SDimitry Andric   AMDGPUTargetStreamer *TS =
1637d523365SDimitry Andric       static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
1647d523365SDimitry Andric   if (isModuleLinkage(GV)) {
1657d523365SDimitry Andric     TS->EmitAMDGPUHsaModuleScopeGlobal(GV->getName());
1667d523365SDimitry Andric   } else {
1677d523365SDimitry Andric     TS->EmitAMDGPUHsaProgramScopeGlobal(GV->getName());
1687d523365SDimitry Andric   }
1697d523365SDimitry Andric 
170444ed5c5SDimitry Andric   MCSymbolELF *GVSym = cast<MCSymbolELF>(getSymbol(GV));
1717d523365SDimitry Andric   const DataLayout &DL = getDataLayout();
172444ed5c5SDimitry Andric 
173444ed5c5SDimitry Andric   // Emit the size
174444ed5c5SDimitry Andric   uint64_t Size = DL.getTypeAllocSize(GV->getType()->getElementType());
175444ed5c5SDimitry Andric   OutStreamer->emitELFSize(GVSym, MCConstantExpr::create(Size, OutContext));
1767d523365SDimitry Andric   OutStreamer->PushSection();
1777d523365SDimitry Andric   OutStreamer->SwitchSection(
1787d523365SDimitry Andric       getObjFileLowering().SectionForGlobal(GV, *Mang, TM));
1797d523365SDimitry Andric   const Constant *C = GV->getInitializer();
1807d523365SDimitry Andric   OutStreamer->EmitLabel(GVSym);
1817d523365SDimitry Andric   EmitGlobalConstant(DL, C);
1827d523365SDimitry Andric   OutStreamer->PopSection();
1838f0fd8f6SDimitry Andric }
1848f0fd8f6SDimitry Andric 
1858f0fd8f6SDimitry Andric bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
1868f0fd8f6SDimitry Andric 
1878f0fd8f6SDimitry Andric   // The starting address of all shader programs must be 256 bytes aligned.
1888f0fd8f6SDimitry Andric   MF.setAlignment(8);
1898f0fd8f6SDimitry Andric 
1908f0fd8f6SDimitry Andric   SetupMachineFunction(MF);
1918f0fd8f6SDimitry Andric 
1928f0fd8f6SDimitry Andric   MCContext &Context = getObjFileLowering().getContext();
1938f0fd8f6SDimitry Andric   MCSectionELF *ConfigSection =
1948f0fd8f6SDimitry Andric       Context.getELFSection(".AMDGPU.config", ELF::SHT_PROGBITS, 0);
1958f0fd8f6SDimitry Andric   OutStreamer->SwitchSection(ConfigSection);
1968f0fd8f6SDimitry Andric 
1978f0fd8f6SDimitry Andric   const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
1988f0fd8f6SDimitry Andric   SIProgramInfo KernelInfo;
1993dac3a9bSDimitry Andric   if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
2008f0fd8f6SDimitry Andric     getSIProgramInfo(KernelInfo, MF);
2017d523365SDimitry Andric     if (!STM.isAmdHsaOS()) {
2028f0fd8f6SDimitry Andric       EmitProgramInfoSI(MF, KernelInfo);
2033dac3a9bSDimitry Andric     }
2048f0fd8f6SDimitry Andric   } else {
2058f0fd8f6SDimitry Andric     EmitProgramInfoR600(MF);
2068f0fd8f6SDimitry Andric   }
2078f0fd8f6SDimitry Andric 
2088f0fd8f6SDimitry Andric   DisasmLines.clear();
2098f0fd8f6SDimitry Andric   HexLines.clear();
2108f0fd8f6SDimitry Andric   DisasmLineMaxLen = 0;
2118f0fd8f6SDimitry Andric 
2128f0fd8f6SDimitry Andric   EmitFunctionBody();
2138f0fd8f6SDimitry Andric 
2148f0fd8f6SDimitry Andric   if (isVerbose()) {
2158f0fd8f6SDimitry Andric     MCSectionELF *CommentSection =
2168f0fd8f6SDimitry Andric         Context.getELFSection(".AMDGPU.csdata", ELF::SHT_PROGBITS, 0);
2178f0fd8f6SDimitry Andric     OutStreamer->SwitchSection(CommentSection);
2188f0fd8f6SDimitry Andric 
2198f0fd8f6SDimitry Andric     if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
2208f0fd8f6SDimitry Andric       OutStreamer->emitRawComment(" Kernel info:", false);
2218f0fd8f6SDimitry Andric       OutStreamer->emitRawComment(" codeLenInByte = " + Twine(KernelInfo.CodeLen),
2228f0fd8f6SDimitry Andric                                   false);
2238f0fd8f6SDimitry Andric       OutStreamer->emitRawComment(" NumSgprs: " + Twine(KernelInfo.NumSGPR),
2248f0fd8f6SDimitry Andric                                   false);
2258f0fd8f6SDimitry Andric       OutStreamer->emitRawComment(" NumVgprs: " + Twine(KernelInfo.NumVGPR),
2268f0fd8f6SDimitry Andric                                   false);
2278f0fd8f6SDimitry Andric       OutStreamer->emitRawComment(" FloatMode: " + Twine(KernelInfo.FloatMode),
2288f0fd8f6SDimitry Andric                                   false);
2298f0fd8f6SDimitry Andric       OutStreamer->emitRawComment(" IeeeMode: " + Twine(KernelInfo.IEEEMode),
2308f0fd8f6SDimitry Andric                                   false);
2318f0fd8f6SDimitry Andric       OutStreamer->emitRawComment(" ScratchSize: " + Twine(KernelInfo.ScratchSize),
2328f0fd8f6SDimitry Andric                                   false);
2337d523365SDimitry Andric 
2347d523365SDimitry Andric       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:USER_SGPR: " +
2357d523365SDimitry Andric                                   Twine(G_00B84C_USER_SGPR(KernelInfo.ComputePGMRSrc2)),
2367d523365SDimitry Andric                                   false);
2377d523365SDimitry Andric       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_X_EN: " +
2387d523365SDimitry Andric                                   Twine(G_00B84C_TGID_X_EN(KernelInfo.ComputePGMRSrc2)),
2397d523365SDimitry Andric                                   false);
2407d523365SDimitry Andric       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_Y_EN: " +
2417d523365SDimitry Andric                                   Twine(G_00B84C_TGID_Y_EN(KernelInfo.ComputePGMRSrc2)),
2427d523365SDimitry Andric                                   false);
2437d523365SDimitry Andric       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_Z_EN: " +
2447d523365SDimitry Andric                                   Twine(G_00B84C_TGID_Z_EN(KernelInfo.ComputePGMRSrc2)),
2457d523365SDimitry Andric                                   false);
2467d523365SDimitry Andric       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: " +
2477d523365SDimitry Andric                                   Twine(G_00B84C_TIDIG_COMP_CNT(KernelInfo.ComputePGMRSrc2)),
2487d523365SDimitry Andric                                   false);
2497d523365SDimitry Andric 
2508f0fd8f6SDimitry Andric     } else {
2518f0fd8f6SDimitry Andric       R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
2528f0fd8f6SDimitry Andric       OutStreamer->emitRawComment(
2538f0fd8f6SDimitry Andric         Twine("SQ_PGM_RESOURCES:STACK_SIZE = " + Twine(MFI->StackSize)));
2548f0fd8f6SDimitry Andric     }
2558f0fd8f6SDimitry Andric   }
2568f0fd8f6SDimitry Andric 
2578f0fd8f6SDimitry Andric   if (STM.dumpCode()) {
2588f0fd8f6SDimitry Andric 
2598f0fd8f6SDimitry Andric     OutStreamer->SwitchSection(
2608f0fd8f6SDimitry Andric         Context.getELFSection(".AMDGPU.disasm", ELF::SHT_NOTE, 0));
2618f0fd8f6SDimitry Andric 
2628f0fd8f6SDimitry Andric     for (size_t i = 0; i < DisasmLines.size(); ++i) {
2638f0fd8f6SDimitry Andric       std::string Comment(DisasmLineMaxLen - DisasmLines[i].size(), ' ');
2648f0fd8f6SDimitry Andric       Comment += " ; " + HexLines[i] + "\n";
2658f0fd8f6SDimitry Andric 
2668f0fd8f6SDimitry Andric       OutStreamer->EmitBytes(StringRef(DisasmLines[i]));
2678f0fd8f6SDimitry Andric       OutStreamer->EmitBytes(StringRef(Comment));
2688f0fd8f6SDimitry Andric     }
2698f0fd8f6SDimitry Andric   }
2708f0fd8f6SDimitry Andric 
2718f0fd8f6SDimitry Andric   return false;
2728f0fd8f6SDimitry Andric }
2738f0fd8f6SDimitry Andric 
2748f0fd8f6SDimitry Andric void AMDGPUAsmPrinter::EmitProgramInfoR600(const MachineFunction &MF) {
2758f0fd8f6SDimitry Andric   unsigned MaxGPR = 0;
2768f0fd8f6SDimitry Andric   bool killPixel = false;
2778f0fd8f6SDimitry Andric   const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
2788f0fd8f6SDimitry Andric   const R600RegisterInfo *RI =
2798f0fd8f6SDimitry Andric       static_cast<const R600RegisterInfo *>(STM.getRegisterInfo());
2808f0fd8f6SDimitry Andric   const R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
2818f0fd8f6SDimitry Andric 
2828f0fd8f6SDimitry Andric   for (const MachineBasicBlock &MBB : MF) {
2838f0fd8f6SDimitry Andric     for (const MachineInstr &MI : MBB) {
2848f0fd8f6SDimitry Andric       if (MI.getOpcode() == AMDGPU::KILLGT)
2858f0fd8f6SDimitry Andric         killPixel = true;
2868f0fd8f6SDimitry Andric       unsigned numOperands = MI.getNumOperands();
2878f0fd8f6SDimitry Andric       for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
2888f0fd8f6SDimitry Andric         const MachineOperand &MO = MI.getOperand(op_idx);
2898f0fd8f6SDimitry Andric         if (!MO.isReg())
2908f0fd8f6SDimitry Andric           continue;
2918f0fd8f6SDimitry Andric         unsigned HWReg = RI->getEncodingValue(MO.getReg()) & 0xff;
2928f0fd8f6SDimitry Andric 
2938f0fd8f6SDimitry Andric         // Register with value > 127 aren't GPR
2948f0fd8f6SDimitry Andric         if (HWReg > 127)
2958f0fd8f6SDimitry Andric           continue;
2968f0fd8f6SDimitry Andric         MaxGPR = std::max(MaxGPR, HWReg);
2978f0fd8f6SDimitry Andric       }
2988f0fd8f6SDimitry Andric     }
2998f0fd8f6SDimitry Andric   }
3008f0fd8f6SDimitry Andric 
3018f0fd8f6SDimitry Andric   unsigned RsrcReg;
3028f0fd8f6SDimitry Andric   if (STM.getGeneration() >= AMDGPUSubtarget::EVERGREEN) {
3038f0fd8f6SDimitry Andric     // Evergreen / Northern Islands
3048f0fd8f6SDimitry Andric     switch (MFI->getShaderType()) {
3058f0fd8f6SDimitry Andric     default: // Fall through
3068f0fd8f6SDimitry Andric     case ShaderType::COMPUTE:  RsrcReg = R_0288D4_SQ_PGM_RESOURCES_LS; break;
3078f0fd8f6SDimitry Andric     case ShaderType::GEOMETRY: RsrcReg = R_028878_SQ_PGM_RESOURCES_GS; break;
3088f0fd8f6SDimitry Andric     case ShaderType::PIXEL:    RsrcReg = R_028844_SQ_PGM_RESOURCES_PS; break;
3098f0fd8f6SDimitry Andric     case ShaderType::VERTEX:   RsrcReg = R_028860_SQ_PGM_RESOURCES_VS; break;
3108f0fd8f6SDimitry Andric     }
3118f0fd8f6SDimitry Andric   } else {
3128f0fd8f6SDimitry Andric     // R600 / R700
3138f0fd8f6SDimitry Andric     switch (MFI->getShaderType()) {
3148f0fd8f6SDimitry Andric     default: // Fall through
3158f0fd8f6SDimitry Andric     case ShaderType::GEOMETRY: // Fall through
3168f0fd8f6SDimitry Andric     case ShaderType::COMPUTE:  // Fall through
3178f0fd8f6SDimitry Andric     case ShaderType::VERTEX:   RsrcReg = R_028868_SQ_PGM_RESOURCES_VS; break;
3188f0fd8f6SDimitry Andric     case ShaderType::PIXEL:    RsrcReg = R_028850_SQ_PGM_RESOURCES_PS; break;
3198f0fd8f6SDimitry Andric     }
3208f0fd8f6SDimitry Andric   }
3218f0fd8f6SDimitry Andric 
3228f0fd8f6SDimitry Andric   OutStreamer->EmitIntValue(RsrcReg, 4);
3238f0fd8f6SDimitry Andric   OutStreamer->EmitIntValue(S_NUM_GPRS(MaxGPR + 1) |
3248f0fd8f6SDimitry Andric                            S_STACK_SIZE(MFI->StackSize), 4);
3258f0fd8f6SDimitry Andric   OutStreamer->EmitIntValue(R_02880C_DB_SHADER_CONTROL, 4);
3268f0fd8f6SDimitry Andric   OutStreamer->EmitIntValue(S_02880C_KILL_ENABLE(killPixel), 4);
3278f0fd8f6SDimitry Andric 
3288f0fd8f6SDimitry Andric   if (MFI->getShaderType() == ShaderType::COMPUTE) {
3298f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_0288E8_SQ_LDS_ALLOC, 4);
3308f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(RoundUpToAlignment(MFI->LDSSize, 4) >> 2, 4);
3318f0fd8f6SDimitry Andric   }
3328f0fd8f6SDimitry Andric }
3338f0fd8f6SDimitry Andric 
3348f0fd8f6SDimitry Andric void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo,
3358f0fd8f6SDimitry Andric                                         const MachineFunction &MF) const {
3368f0fd8f6SDimitry Andric   const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
3378f0fd8f6SDimitry Andric   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
3388f0fd8f6SDimitry Andric   uint64_t CodeSize = 0;
3398f0fd8f6SDimitry Andric   unsigned MaxSGPR = 0;
3408f0fd8f6SDimitry Andric   unsigned MaxVGPR = 0;
3418f0fd8f6SDimitry Andric   bool VCCUsed = false;
3428f0fd8f6SDimitry Andric   bool FlatUsed = false;
3438f0fd8f6SDimitry Andric   const SIRegisterInfo *RI =
3448f0fd8f6SDimitry Andric       static_cast<const SIRegisterInfo *>(STM.getRegisterInfo());
3458f0fd8f6SDimitry Andric 
3468f0fd8f6SDimitry Andric   for (const MachineBasicBlock &MBB : MF) {
3478f0fd8f6SDimitry Andric     for (const MachineInstr &MI : MBB) {
3488f0fd8f6SDimitry Andric       // TODO: CodeSize should account for multiple functions.
3499a4b3118SDimitry Andric 
3509a4b3118SDimitry Andric       // TODO: Should we count size of debug info?
3519a4b3118SDimitry Andric       if (MI.isDebugValue())
3529a4b3118SDimitry Andric         continue;
3539a4b3118SDimitry Andric 
3549a4b3118SDimitry Andric       // FIXME: This is reporting 0 for many instructions.
3558f0fd8f6SDimitry Andric       CodeSize += MI.getDesc().Size;
3568f0fd8f6SDimitry Andric 
3578f0fd8f6SDimitry Andric       unsigned numOperands = MI.getNumOperands();
3588f0fd8f6SDimitry Andric       for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
3598f0fd8f6SDimitry Andric         const MachineOperand &MO = MI.getOperand(op_idx);
3608f0fd8f6SDimitry Andric         unsigned width = 0;
3618f0fd8f6SDimitry Andric         bool isSGPR = false;
3628f0fd8f6SDimitry Andric 
3637d523365SDimitry Andric         if (!MO.isReg())
3648f0fd8f6SDimitry Andric           continue;
3658f0fd8f6SDimitry Andric 
3667d523365SDimitry Andric         unsigned reg = MO.getReg();
3678f0fd8f6SDimitry Andric         switch (reg) {
3688f0fd8f6SDimitry Andric         case AMDGPU::EXEC:
3697d523365SDimitry Andric         case AMDGPU::SCC:
3708f0fd8f6SDimitry Andric         case AMDGPU::M0:
3718f0fd8f6SDimitry Andric           continue;
3727d523365SDimitry Andric 
3737d523365SDimitry Andric         case AMDGPU::VCC:
3747d523365SDimitry Andric         case AMDGPU::VCC_LO:
3757d523365SDimitry Andric         case AMDGPU::VCC_HI:
3767d523365SDimitry Andric           VCCUsed = true;
3777d523365SDimitry Andric           continue;
3787d523365SDimitry Andric 
3797d523365SDimitry Andric         case AMDGPU::FLAT_SCR:
3807d523365SDimitry Andric         case AMDGPU::FLAT_SCR_LO:
3817d523365SDimitry Andric         case AMDGPU::FLAT_SCR_HI:
3827d523365SDimitry Andric           FlatUsed = true;
3837d523365SDimitry Andric           continue;
3847d523365SDimitry Andric 
3857d523365SDimitry Andric         default:
3867d523365SDimitry Andric           break;
3878f0fd8f6SDimitry Andric         }
3888f0fd8f6SDimitry Andric 
3898f0fd8f6SDimitry Andric         if (AMDGPU::SReg_32RegClass.contains(reg)) {
3908f0fd8f6SDimitry Andric           isSGPR = true;
3918f0fd8f6SDimitry Andric           width = 1;
3928f0fd8f6SDimitry Andric         } else if (AMDGPU::VGPR_32RegClass.contains(reg)) {
3938f0fd8f6SDimitry Andric           isSGPR = false;
3948f0fd8f6SDimitry Andric           width = 1;
3958f0fd8f6SDimitry Andric         } else if (AMDGPU::SReg_64RegClass.contains(reg)) {
3968f0fd8f6SDimitry Andric           isSGPR = true;
3978f0fd8f6SDimitry Andric           width = 2;
3988f0fd8f6SDimitry Andric         } else if (AMDGPU::VReg_64RegClass.contains(reg)) {
3998f0fd8f6SDimitry Andric           isSGPR = false;
4008f0fd8f6SDimitry Andric           width = 2;
4018f0fd8f6SDimitry Andric         } else if (AMDGPU::VReg_96RegClass.contains(reg)) {
4028f0fd8f6SDimitry Andric           isSGPR = false;
4038f0fd8f6SDimitry Andric           width = 3;
4048f0fd8f6SDimitry Andric         } else if (AMDGPU::SReg_128RegClass.contains(reg)) {
4058f0fd8f6SDimitry Andric           isSGPR = true;
4068f0fd8f6SDimitry Andric           width = 4;
4078f0fd8f6SDimitry Andric         } else if (AMDGPU::VReg_128RegClass.contains(reg)) {
4088f0fd8f6SDimitry Andric           isSGPR = false;
4098f0fd8f6SDimitry Andric           width = 4;
4108f0fd8f6SDimitry Andric         } else if (AMDGPU::SReg_256RegClass.contains(reg)) {
4118f0fd8f6SDimitry Andric           isSGPR = true;
4128f0fd8f6SDimitry Andric           width = 8;
4138f0fd8f6SDimitry Andric         } else if (AMDGPU::VReg_256RegClass.contains(reg)) {
4148f0fd8f6SDimitry Andric           isSGPR = false;
4158f0fd8f6SDimitry Andric           width = 8;
4168f0fd8f6SDimitry Andric         } else if (AMDGPU::SReg_512RegClass.contains(reg)) {
4178f0fd8f6SDimitry Andric           isSGPR = true;
4188f0fd8f6SDimitry Andric           width = 16;
4198f0fd8f6SDimitry Andric         } else if (AMDGPU::VReg_512RegClass.contains(reg)) {
4208f0fd8f6SDimitry Andric           isSGPR = false;
4218f0fd8f6SDimitry Andric           width = 16;
4228f0fd8f6SDimitry Andric         } else {
4238f0fd8f6SDimitry Andric           llvm_unreachable("Unknown register class");
4248f0fd8f6SDimitry Andric         }
4258f0fd8f6SDimitry Andric         unsigned hwReg = RI->getEncodingValue(reg) & 0xff;
4268f0fd8f6SDimitry Andric         unsigned maxUsed = hwReg + width - 1;
4278f0fd8f6SDimitry Andric         if (isSGPR) {
4288f0fd8f6SDimitry Andric           MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR;
4298f0fd8f6SDimitry Andric         } else {
4308f0fd8f6SDimitry Andric           MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR;
4318f0fd8f6SDimitry Andric         }
4328f0fd8f6SDimitry Andric       }
4338f0fd8f6SDimitry Andric     }
4348f0fd8f6SDimitry Andric   }
4358f0fd8f6SDimitry Andric 
436444ed5c5SDimitry Andric   unsigned ExtraSGPRs = 0;
437444ed5c5SDimitry Andric 
438444ed5c5SDimitry Andric   if (VCCUsed)
439444ed5c5SDimitry Andric     ExtraSGPRs = 2;
440444ed5c5SDimitry Andric 
441444ed5c5SDimitry Andric   if (STM.getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS) {
442444ed5c5SDimitry Andric     if (FlatUsed)
443444ed5c5SDimitry Andric       ExtraSGPRs = 4;
444444ed5c5SDimitry Andric   } else {
445444ed5c5SDimitry Andric     if (STM.isXNACKEnabled())
446444ed5c5SDimitry Andric       ExtraSGPRs = 4;
4478f0fd8f6SDimitry Andric 
4484d0b32cdSDimitry Andric     if (FlatUsed)
449444ed5c5SDimitry Andric       ExtraSGPRs = 6;
4507d523365SDimitry Andric   }
4518f0fd8f6SDimitry Andric 
452444ed5c5SDimitry Andric   MaxSGPR += ExtraSGPRs;
453444ed5c5SDimitry Andric 
4548f0fd8f6SDimitry Andric   // We found the maximum register index. They start at 0, so add one to get the
4558f0fd8f6SDimitry Andric   // number of registers.
4568f0fd8f6SDimitry Andric   ProgInfo.NumVGPR = MaxVGPR + 1;
4578f0fd8f6SDimitry Andric   ProgInfo.NumSGPR = MaxSGPR + 1;
4588f0fd8f6SDimitry Andric 
4598f0fd8f6SDimitry Andric   if (STM.hasSGPRInitBug()) {
4608f0fd8f6SDimitry Andric     if (ProgInfo.NumSGPR > AMDGPUSubtarget::FIXED_SGPR_COUNT_FOR_INIT_BUG) {
4618f0fd8f6SDimitry Andric       LLVMContext &Ctx = MF.getFunction()->getContext();
4628f0fd8f6SDimitry Andric       Ctx.emitError("too many SGPRs used with the SGPR init bug");
4638f0fd8f6SDimitry Andric     }
4648f0fd8f6SDimitry Andric 
4658f0fd8f6SDimitry Andric     ProgInfo.NumSGPR = AMDGPUSubtarget::FIXED_SGPR_COUNT_FOR_INIT_BUG;
4668f0fd8f6SDimitry Andric   }
4678f0fd8f6SDimitry Andric 
4687d523365SDimitry Andric   if (MFI->NumUserSGPRs > STM.getMaxNumUserSGPRs()) {
4697d523365SDimitry Andric     LLVMContext &Ctx = MF.getFunction()->getContext();
4707d523365SDimitry Andric     Ctx.emitError("too many user SGPRs used");
4717d523365SDimitry Andric   }
4727d523365SDimitry Andric 
4738f0fd8f6SDimitry Andric   ProgInfo.VGPRBlocks = (ProgInfo.NumVGPR - 1) / 4;
4748f0fd8f6SDimitry Andric   ProgInfo.SGPRBlocks = (ProgInfo.NumSGPR - 1) / 8;
4758f0fd8f6SDimitry Andric   // Set the value to initialize FP_ROUND and FP_DENORM parts of the mode
4768f0fd8f6SDimitry Andric   // register.
4778f0fd8f6SDimitry Andric   ProgInfo.FloatMode = getFPMode(MF);
4788f0fd8f6SDimitry Andric 
4798f0fd8f6SDimitry Andric   // XXX: Not quite sure what this does, but sc seems to unset this.
4808f0fd8f6SDimitry Andric   ProgInfo.IEEEMode = 0;
4818f0fd8f6SDimitry Andric 
4828f0fd8f6SDimitry Andric   // Do not clamp NAN to 0.
4838f0fd8f6SDimitry Andric   ProgInfo.DX10Clamp = 0;
4848f0fd8f6SDimitry Andric 
4858f0fd8f6SDimitry Andric   const MachineFrameInfo *FrameInfo = MF.getFrameInfo();
4868f0fd8f6SDimitry Andric   ProgInfo.ScratchSize = FrameInfo->estimateStackSize(MF);
4878f0fd8f6SDimitry Andric 
4888f0fd8f6SDimitry Andric   ProgInfo.FlatUsed = FlatUsed;
4898f0fd8f6SDimitry Andric   ProgInfo.VCCUsed = VCCUsed;
4908f0fd8f6SDimitry Andric   ProgInfo.CodeLen = CodeSize;
4918f0fd8f6SDimitry Andric 
4928f0fd8f6SDimitry Andric   unsigned LDSAlignShift;
4938f0fd8f6SDimitry Andric   if (STM.getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) {
4948f0fd8f6SDimitry Andric     // LDS is allocated in 64 dword blocks.
4958f0fd8f6SDimitry Andric     LDSAlignShift = 8;
4968f0fd8f6SDimitry Andric   } else {
4978f0fd8f6SDimitry Andric     // LDS is allocated in 128 dword blocks.
4988f0fd8f6SDimitry Andric     LDSAlignShift = 9;
4998f0fd8f6SDimitry Andric   }
5008f0fd8f6SDimitry Andric 
5018f0fd8f6SDimitry Andric   unsigned LDSSpillSize = MFI->LDSWaveSpillSize *
5028f0fd8f6SDimitry Andric                           MFI->getMaximumWorkGroupSize(MF);
5038f0fd8f6SDimitry Andric 
5048f0fd8f6SDimitry Andric   ProgInfo.LDSSize = MFI->LDSSize + LDSSpillSize;
5058f0fd8f6SDimitry Andric   ProgInfo.LDSBlocks =
5068f0fd8f6SDimitry Andric      RoundUpToAlignment(ProgInfo.LDSSize, 1 << LDSAlignShift) >> LDSAlignShift;
5078f0fd8f6SDimitry Andric 
5088f0fd8f6SDimitry Andric   // Scratch is allocated in 256 dword blocks.
5098f0fd8f6SDimitry Andric   unsigned ScratchAlignShift = 10;
5108f0fd8f6SDimitry Andric   // We need to program the hardware with the amount of scratch memory that
5118f0fd8f6SDimitry Andric   // is used by the entire wave.  ProgInfo.ScratchSize is the amount of
5128f0fd8f6SDimitry Andric   // scratch memory used per thread.
5138f0fd8f6SDimitry Andric   ProgInfo.ScratchBlocks =
5148f0fd8f6SDimitry Andric     RoundUpToAlignment(ProgInfo.ScratchSize * STM.getWavefrontSize(),
5158f0fd8f6SDimitry Andric                        1 << ScratchAlignShift) >> ScratchAlignShift;
5168f0fd8f6SDimitry Andric 
5178f0fd8f6SDimitry Andric   ProgInfo.ComputePGMRSrc1 =
5188f0fd8f6SDimitry Andric       S_00B848_VGPRS(ProgInfo.VGPRBlocks) |
5198f0fd8f6SDimitry Andric       S_00B848_SGPRS(ProgInfo.SGPRBlocks) |
5208f0fd8f6SDimitry Andric       S_00B848_PRIORITY(ProgInfo.Priority) |
5218f0fd8f6SDimitry Andric       S_00B848_FLOAT_MODE(ProgInfo.FloatMode) |
5228f0fd8f6SDimitry Andric       S_00B848_PRIV(ProgInfo.Priv) |
5238f0fd8f6SDimitry Andric       S_00B848_DX10_CLAMP(ProgInfo.DX10Clamp) |
5247d523365SDimitry Andric       S_00B848_DEBUG_MODE(ProgInfo.DebugMode) |
5258f0fd8f6SDimitry Andric       S_00B848_IEEE_MODE(ProgInfo.IEEEMode);
5268f0fd8f6SDimitry Andric 
5277d523365SDimitry Andric   // 0 = X, 1 = XY, 2 = XYZ
5287d523365SDimitry Andric   unsigned TIDIGCompCnt = 0;
5297d523365SDimitry Andric   if (MFI->hasWorkItemIDZ())
5307d523365SDimitry Andric     TIDIGCompCnt = 2;
5317d523365SDimitry Andric   else if (MFI->hasWorkItemIDY())
5327d523365SDimitry Andric     TIDIGCompCnt = 1;
5337d523365SDimitry Andric 
5348f0fd8f6SDimitry Andric   ProgInfo.ComputePGMRSrc2 =
5358f0fd8f6SDimitry Andric       S_00B84C_SCRATCH_EN(ProgInfo.ScratchBlocks > 0) |
5367d523365SDimitry Andric       S_00B84C_USER_SGPR(MFI->getNumUserSGPRs()) |
5377d523365SDimitry Andric       S_00B84C_TGID_X_EN(MFI->hasWorkGroupIDX()) |
5387d523365SDimitry Andric       S_00B84C_TGID_Y_EN(MFI->hasWorkGroupIDY()) |
5397d523365SDimitry Andric       S_00B84C_TGID_Z_EN(MFI->hasWorkGroupIDZ()) |
5407d523365SDimitry Andric       S_00B84C_TG_SIZE_EN(MFI->hasWorkGroupInfo()) |
5417d523365SDimitry Andric       S_00B84C_TIDIG_COMP_CNT(TIDIGCompCnt) |
5427d523365SDimitry Andric       S_00B84C_EXCP_EN_MSB(0) |
5437d523365SDimitry Andric       S_00B84C_LDS_SIZE(ProgInfo.LDSBlocks) |
5447d523365SDimitry Andric       S_00B84C_EXCP_EN(0);
5458f0fd8f6SDimitry Andric }
5468f0fd8f6SDimitry Andric 
5478f0fd8f6SDimitry Andric static unsigned getRsrcReg(unsigned ShaderType) {
5488f0fd8f6SDimitry Andric   switch (ShaderType) {
5498f0fd8f6SDimitry Andric   default: // Fall through
5508f0fd8f6SDimitry Andric   case ShaderType::COMPUTE:  return R_00B848_COMPUTE_PGM_RSRC1;
5518f0fd8f6SDimitry Andric   case ShaderType::GEOMETRY: return R_00B228_SPI_SHADER_PGM_RSRC1_GS;
5528f0fd8f6SDimitry Andric   case ShaderType::PIXEL:    return R_00B028_SPI_SHADER_PGM_RSRC1_PS;
5538f0fd8f6SDimitry Andric   case ShaderType::VERTEX:   return R_00B128_SPI_SHADER_PGM_RSRC1_VS;
5548f0fd8f6SDimitry Andric   }
5558f0fd8f6SDimitry Andric }
5568f0fd8f6SDimitry Andric 
5578f0fd8f6SDimitry Andric void AMDGPUAsmPrinter::EmitProgramInfoSI(const MachineFunction &MF,
5588f0fd8f6SDimitry Andric                                          const SIProgramInfo &KernelInfo) {
5598f0fd8f6SDimitry Andric   const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
5608f0fd8f6SDimitry Andric   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
5618f0fd8f6SDimitry Andric   unsigned RsrcReg = getRsrcReg(MFI->getShaderType());
5628f0fd8f6SDimitry Andric 
5638f0fd8f6SDimitry Andric   if (MFI->getShaderType() == ShaderType::COMPUTE) {
5648f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_00B848_COMPUTE_PGM_RSRC1, 4);
5658f0fd8f6SDimitry Andric 
5668f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(KernelInfo.ComputePGMRSrc1, 4);
5678f0fd8f6SDimitry Andric 
5688f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4);
5698f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(KernelInfo.ComputePGMRSrc2, 4);
5708f0fd8f6SDimitry Andric 
5718f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_00B860_COMPUTE_TMPRING_SIZE, 4);
5728f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(S_00B860_WAVESIZE(KernelInfo.ScratchBlocks), 4);
5738f0fd8f6SDimitry Andric 
5748f0fd8f6SDimitry Andric     // TODO: Should probably note flat usage somewhere. SC emits a "FlatPtr32 =
5758f0fd8f6SDimitry Andric     // 0" comment but I don't see a corresponding field in the register spec.
5768f0fd8f6SDimitry Andric   } else {
5778f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(RsrcReg, 4);
5788f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(S_00B028_VGPRS(KernelInfo.VGPRBlocks) |
5798f0fd8f6SDimitry Andric                               S_00B028_SGPRS(KernelInfo.SGPRBlocks), 4);
5808f0fd8f6SDimitry Andric     if (STM.isVGPRSpillingEnabled(MFI)) {
5818f0fd8f6SDimitry Andric       OutStreamer->EmitIntValue(R_0286E8_SPI_TMPRING_SIZE, 4);
5828f0fd8f6SDimitry Andric       OutStreamer->EmitIntValue(S_0286E8_WAVESIZE(KernelInfo.ScratchBlocks), 4);
5838f0fd8f6SDimitry Andric     }
5848f0fd8f6SDimitry Andric   }
5858f0fd8f6SDimitry Andric 
5868f0fd8f6SDimitry Andric   if (MFI->getShaderType() == ShaderType::PIXEL) {
5878f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4);
5888f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(KernelInfo.LDSBlocks), 4);
5898f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4);
590444ed5c5SDimitry Andric     OutStreamer->EmitIntValue(MFI->PSInputEna, 4);
591444ed5c5SDimitry Andric     OutStreamer->EmitIntValue(R_0286D0_SPI_PS_INPUT_ADDR, 4);
592444ed5c5SDimitry Andric     OutStreamer->EmitIntValue(MFI->getPSInputAddr(), 4);
5938f0fd8f6SDimitry Andric   }
5948f0fd8f6SDimitry Andric }
5958f0fd8f6SDimitry Andric 
5968f0fd8f6SDimitry Andric void AMDGPUAsmPrinter::EmitAmdKernelCodeT(const MachineFunction &MF,
5978f0fd8f6SDimitry Andric                                          const SIProgramInfo &KernelInfo) const {
5988f0fd8f6SDimitry Andric   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
5998f0fd8f6SDimitry Andric   const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
6008f0fd8f6SDimitry Andric   amd_kernel_code_t header;
6018f0fd8f6SDimitry Andric 
6023dac3a9bSDimitry Andric   AMDGPU::initDefaultAMDKernelCodeT(header, STM.getFeatureBits());
6038f0fd8f6SDimitry Andric 
6048f0fd8f6SDimitry Andric   header.compute_pgm_resource_registers =
6058f0fd8f6SDimitry Andric       KernelInfo.ComputePGMRSrc1 |
6068f0fd8f6SDimitry Andric       (KernelInfo.ComputePGMRSrc2 << 32);
6077d523365SDimitry Andric   header.code_properties = AMD_CODE_PROPERTY_IS_PTR64;
6087d523365SDimitry Andric 
6097d523365SDimitry Andric   if (MFI->hasPrivateSegmentBuffer()) {
6107d523365SDimitry Andric     header.code_properties |=
6117d523365SDimitry Andric       AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER;
6127d523365SDimitry Andric   }
6137d523365SDimitry Andric 
6147d523365SDimitry Andric   if (MFI->hasDispatchPtr())
6157d523365SDimitry Andric     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
6167d523365SDimitry Andric 
6177d523365SDimitry Andric   if (MFI->hasQueuePtr())
6187d523365SDimitry Andric     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR;
6197d523365SDimitry Andric 
6207d523365SDimitry Andric   if (MFI->hasKernargSegmentPtr())
6217d523365SDimitry Andric     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR;
6227d523365SDimitry Andric 
6237d523365SDimitry Andric   if (MFI->hasDispatchID())
6247d523365SDimitry Andric     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID;
6257d523365SDimitry Andric 
6267d523365SDimitry Andric   if (MFI->hasFlatScratchInit())
6277d523365SDimitry Andric     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT;
6287d523365SDimitry Andric 
6297d523365SDimitry Andric   // TODO: Private segment size
6307d523365SDimitry Andric 
6317d523365SDimitry Andric   if (MFI->hasGridWorkgroupCountX()) {
6327d523365SDimitry Andric     header.code_properties |=
6337d523365SDimitry Andric       AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X;
6347d523365SDimitry Andric   }
6357d523365SDimitry Andric 
6367d523365SDimitry Andric   if (MFI->hasGridWorkgroupCountY()) {
6377d523365SDimitry Andric     header.code_properties |=
6387d523365SDimitry Andric       AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y;
6397d523365SDimitry Andric   }
6407d523365SDimitry Andric 
6417d523365SDimitry Andric   if (MFI->hasGridWorkgroupCountZ()) {
6427d523365SDimitry Andric     header.code_properties |=
6437d523365SDimitry Andric       AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z;
6447d523365SDimitry Andric   }
6457d523365SDimitry Andric 
6467d523365SDimitry Andric   if (MFI->hasDispatchPtr())
6477d523365SDimitry Andric     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
6488f0fd8f6SDimitry Andric 
6494d0b32cdSDimitry Andric   if (STM.isXNACKEnabled())
6504d0b32cdSDimitry Andric     header.code_properties |= AMD_CODE_PROPERTY_IS_XNACK_SUPPORTED;
6514d0b32cdSDimitry Andric 
6528f0fd8f6SDimitry Andric   header.kernarg_segment_byte_size = MFI->ABIArgOffset;
6538f0fd8f6SDimitry Andric   header.wavefront_sgpr_count = KernelInfo.NumSGPR;
6548f0fd8f6SDimitry Andric   header.workitem_vgpr_count = KernelInfo.NumVGPR;
6557d523365SDimitry Andric   header.workitem_private_segment_byte_size = KernelInfo.ScratchSize;
6567d523365SDimitry Andric   header.workgroup_group_segment_byte_size = KernelInfo.LDSSize;
6578f0fd8f6SDimitry Andric 
6583dac3a9bSDimitry Andric   AMDGPUTargetStreamer *TS =
6593dac3a9bSDimitry Andric       static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
6603dac3a9bSDimitry Andric   TS->EmitAMDKernelCodeT(header);
6618f0fd8f6SDimitry Andric }
6628f0fd8f6SDimitry Andric 
6638f0fd8f6SDimitry Andric bool AMDGPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
6648f0fd8f6SDimitry Andric                                        unsigned AsmVariant,
6658f0fd8f6SDimitry Andric                                        const char *ExtraCode, raw_ostream &O) {
6668f0fd8f6SDimitry Andric   if (ExtraCode && ExtraCode[0]) {
6678f0fd8f6SDimitry Andric     if (ExtraCode[1] != 0)
6688f0fd8f6SDimitry Andric       return true; // Unknown modifier.
6698f0fd8f6SDimitry Andric 
6708f0fd8f6SDimitry Andric     switch (ExtraCode[0]) {
6718f0fd8f6SDimitry Andric     default:
6728f0fd8f6SDimitry Andric       // See if this is a generic print operand
6738f0fd8f6SDimitry Andric       return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
6748f0fd8f6SDimitry Andric     case 'r':
6758f0fd8f6SDimitry Andric       break;
6768f0fd8f6SDimitry Andric     }
6778f0fd8f6SDimitry Andric   }
6788f0fd8f6SDimitry Andric 
6798f0fd8f6SDimitry Andric   AMDGPUInstPrinter::printRegOperand(MI->getOperand(OpNo).getReg(), O,
6808f0fd8f6SDimitry Andric                    *TM.getSubtargetImpl(*MF->getFunction())->getRegisterInfo());
6818f0fd8f6SDimitry Andric   return false;
6828f0fd8f6SDimitry Andric }
683