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"
313ca95b02SDimitry Andric #include "SIInstrInfo.h"
328f0fd8f6SDimitry Andric #include "SIRegisterInfo.h"
338f0fd8f6SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
343ca95b02SDimitry Andric #include "llvm/IR/DiagnosticInfo.h"
358f0fd8f6SDimitry Andric #include "llvm/MC/MCContext.h"
368f0fd8f6SDimitry Andric #include "llvm/MC/MCSectionELF.h"
378f0fd8f6SDimitry Andric #include "llvm/MC/MCStreamer.h"
388f0fd8f6SDimitry Andric #include "llvm/Support/ELF.h"
398f0fd8f6SDimitry Andric #include "llvm/Support/MathExtras.h"
408f0fd8f6SDimitry Andric #include "llvm/Support/TargetRegistry.h"
418f0fd8f6SDimitry Andric #include "llvm/Target/TargetLoweringObjectFile.h"
423ca95b02SDimitry Andric #include "AMDGPURuntimeMetadata.h"
438f0fd8f6SDimitry Andric 
443ca95b02SDimitry Andric using namespace ::AMDGPU;
458f0fd8f6SDimitry Andric using namespace llvm;
468f0fd8f6SDimitry Andric 
478f0fd8f6SDimitry Andric // TODO: This should get the default rounding mode from the kernel. We just set
488f0fd8f6SDimitry Andric // the default here, but this could change if the OpenCL rounding mode pragmas
498f0fd8f6SDimitry Andric // are used.
508f0fd8f6SDimitry Andric //
518f0fd8f6SDimitry Andric // The denormal mode here should match what is reported by the OpenCL runtime
528f0fd8f6SDimitry Andric // for the CL_FP_DENORM bit from CL_DEVICE_{HALF|SINGLE|DOUBLE}_FP_CONFIG, but
538f0fd8f6SDimitry Andric // can also be override to flush with the -cl-denorms-are-zero compiler flag.
548f0fd8f6SDimitry Andric //
558f0fd8f6SDimitry Andric // AMD OpenCL only sets flush none and reports CL_FP_DENORM for double
568f0fd8f6SDimitry Andric // precision, and leaves single precision to flush all and does not report
578f0fd8f6SDimitry Andric // CL_FP_DENORM for CL_DEVICE_SINGLE_FP_CONFIG. Mesa's OpenCL currently reports
588f0fd8f6SDimitry Andric // CL_FP_DENORM for both.
598f0fd8f6SDimitry Andric //
608f0fd8f6SDimitry Andric // FIXME: It seems some instructions do not support single precision denormals
618f0fd8f6SDimitry Andric // regardless of the mode (exp_*_f32, rcp_*_f32, rsq_*_f32, rsq_*f32, sqrt_f32,
628f0fd8f6SDimitry Andric // and sin_f32, cos_f32 on most parts).
638f0fd8f6SDimitry Andric 
648f0fd8f6SDimitry Andric // We want to use these instructions, and using fp32 denormals also causes
658f0fd8f6SDimitry Andric // instructions to run at the double precision rate for the device so it's
668f0fd8f6SDimitry Andric // probably best to just report no single precision denormals.
678f0fd8f6SDimitry Andric static uint32_t getFPMode(const MachineFunction &F) {
683ca95b02SDimitry Andric   const SISubtarget& ST = F.getSubtarget<SISubtarget>();
698f0fd8f6SDimitry Andric   // TODO: Is there any real use for the flush in only / flush out only modes?
708f0fd8f6SDimitry Andric 
718f0fd8f6SDimitry Andric   uint32_t FP32Denormals =
728f0fd8f6SDimitry Andric     ST.hasFP32Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
738f0fd8f6SDimitry Andric 
748f0fd8f6SDimitry Andric   uint32_t FP64Denormals =
758f0fd8f6SDimitry Andric     ST.hasFP64Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT;
768f0fd8f6SDimitry Andric 
778f0fd8f6SDimitry Andric   return FP_ROUND_MODE_SP(FP_ROUND_ROUND_TO_NEAREST) |
788f0fd8f6SDimitry Andric          FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_NEAREST) |
798f0fd8f6SDimitry Andric          FP_DENORM_MODE_SP(FP32Denormals) |
808f0fd8f6SDimitry Andric          FP_DENORM_MODE_DP(FP64Denormals);
818f0fd8f6SDimitry Andric }
828f0fd8f6SDimitry Andric 
838f0fd8f6SDimitry Andric static AsmPrinter *
848f0fd8f6SDimitry Andric createAMDGPUAsmPrinterPass(TargetMachine &tm,
858f0fd8f6SDimitry Andric                            std::unique_ptr<MCStreamer> &&Streamer) {
868f0fd8f6SDimitry Andric   return new AMDGPUAsmPrinter(tm, std::move(Streamer));
878f0fd8f6SDimitry Andric }
888f0fd8f6SDimitry Andric 
898f0fd8f6SDimitry Andric extern "C" void LLVMInitializeAMDGPUAsmPrinter() {
908f0fd8f6SDimitry Andric   TargetRegistry::RegisterAsmPrinter(TheAMDGPUTarget, createAMDGPUAsmPrinterPass);
918f0fd8f6SDimitry Andric   TargetRegistry::RegisterAsmPrinter(TheGCNTarget, createAMDGPUAsmPrinterPass);
928f0fd8f6SDimitry Andric }
938f0fd8f6SDimitry Andric 
948f0fd8f6SDimitry Andric AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM,
958f0fd8f6SDimitry Andric                                    std::unique_ptr<MCStreamer> Streamer)
968f0fd8f6SDimitry Andric     : AsmPrinter(TM, std::move(Streamer)) {}
978f0fd8f6SDimitry Andric 
98444ed5c5SDimitry Andric void AMDGPUAsmPrinter::EmitStartOfAsmFile(Module &M) {
99444ed5c5SDimitry Andric   if (TM.getTargetTriple().getOS() != Triple::AMDHSA)
100444ed5c5SDimitry Andric     return;
101444ed5c5SDimitry Andric 
102444ed5c5SDimitry Andric   // Need to construct an MCSubtargetInfo here in case we have no functions
103444ed5c5SDimitry Andric   // in the module.
104444ed5c5SDimitry Andric   std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo(
105444ed5c5SDimitry Andric         TM.getTargetTriple().str(), TM.getTargetCPU(),
106444ed5c5SDimitry Andric         TM.getTargetFeatureString()));
107444ed5c5SDimitry Andric 
108444ed5c5SDimitry Andric   AMDGPUTargetStreamer *TS =
109444ed5c5SDimitry Andric       static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
110444ed5c5SDimitry Andric 
1113ca95b02SDimitry Andric   TS->EmitDirectiveHSACodeObjectVersion(2, 1);
1123ca95b02SDimitry Andric 
113444ed5c5SDimitry Andric   AMDGPU::IsaVersion ISA = AMDGPU::getIsaVersion(STI->getFeatureBits());
114444ed5c5SDimitry Andric   TS->EmitDirectiveHSACodeObjectISA(ISA.Major, ISA.Minor, ISA.Stepping,
115444ed5c5SDimitry Andric                                     "AMD", "AMDGPU");
1163ca95b02SDimitry Andric   emitStartOfRuntimeMetadata(M);
117444ed5c5SDimitry Andric }
118444ed5c5SDimitry Andric 
1193dac3a9bSDimitry Andric void AMDGPUAsmPrinter::EmitFunctionBodyStart() {
1203dac3a9bSDimitry Andric   const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
1213dac3a9bSDimitry Andric   SIProgramInfo KernelInfo;
1223dac3a9bSDimitry Andric   if (STM.isAmdHsaOS()) {
1233dac3a9bSDimitry Andric     getSIProgramInfo(KernelInfo, *MF);
1243dac3a9bSDimitry Andric     EmitAmdKernelCodeT(*MF, KernelInfo);
1253dac3a9bSDimitry Andric   }
1263dac3a9bSDimitry Andric }
1273dac3a9bSDimitry Andric 
1287d523365SDimitry Andric void AMDGPUAsmPrinter::EmitFunctionEntryLabel() {
1297d523365SDimitry Andric   const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>();
1307d523365SDimitry Andric   const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>();
1317d523365SDimitry Andric   if (MFI->isKernel() && STM.isAmdHsaOS()) {
1327d523365SDimitry Andric     AMDGPUTargetStreamer *TS =
1337d523365SDimitry Andric         static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
1347d523365SDimitry Andric     TS->EmitAMDGPUSymbolType(CurrentFnSym->getName(),
1357d523365SDimitry Andric                              ELF::STT_AMDGPU_HSA_KERNEL);
1367d523365SDimitry Andric   }
1378f0fd8f6SDimitry Andric 
1387d523365SDimitry Andric   AsmPrinter::EmitFunctionEntryLabel();
1397d523365SDimitry Andric }
1407d523365SDimitry Andric 
1417d523365SDimitry Andric void AMDGPUAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) {
1427d523365SDimitry Andric 
1437d523365SDimitry Andric   // Group segment variables aren't emitted in HSA.
1447d523365SDimitry Andric   if (AMDGPU::isGroupSegment(GV))
1457d523365SDimitry Andric     return;
1467d523365SDimitry Andric 
1473ca95b02SDimitry Andric   AsmPrinter::EmitGlobalVariable(GV);
1488f0fd8f6SDimitry Andric }
1498f0fd8f6SDimitry Andric 
1508f0fd8f6SDimitry Andric bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
1518f0fd8f6SDimitry Andric 
1528f0fd8f6SDimitry Andric   // The starting address of all shader programs must be 256 bytes aligned.
1538f0fd8f6SDimitry Andric   MF.setAlignment(8);
1548f0fd8f6SDimitry Andric 
1558f0fd8f6SDimitry Andric   SetupMachineFunction(MF);
1568f0fd8f6SDimitry Andric 
1578f0fd8f6SDimitry Andric   MCContext &Context = getObjFileLowering().getContext();
1588f0fd8f6SDimitry Andric   MCSectionELF *ConfigSection =
1598f0fd8f6SDimitry Andric       Context.getELFSection(".AMDGPU.config", ELF::SHT_PROGBITS, 0);
1608f0fd8f6SDimitry Andric   OutStreamer->SwitchSection(ConfigSection);
1618f0fd8f6SDimitry Andric 
1628f0fd8f6SDimitry Andric   const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>();
1638f0fd8f6SDimitry Andric   SIProgramInfo KernelInfo;
1643dac3a9bSDimitry Andric   if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
1658f0fd8f6SDimitry Andric     getSIProgramInfo(KernelInfo, MF);
1667d523365SDimitry Andric     if (!STM.isAmdHsaOS()) {
1678f0fd8f6SDimitry Andric       EmitProgramInfoSI(MF, KernelInfo);
1683dac3a9bSDimitry Andric     }
1698f0fd8f6SDimitry Andric   } else {
1708f0fd8f6SDimitry Andric     EmitProgramInfoR600(MF);
1718f0fd8f6SDimitry Andric   }
1728f0fd8f6SDimitry Andric 
1738f0fd8f6SDimitry Andric   DisasmLines.clear();
1748f0fd8f6SDimitry Andric   HexLines.clear();
1758f0fd8f6SDimitry Andric   DisasmLineMaxLen = 0;
1768f0fd8f6SDimitry Andric 
1778f0fd8f6SDimitry Andric   EmitFunctionBody();
1788f0fd8f6SDimitry Andric 
1798f0fd8f6SDimitry Andric   if (isVerbose()) {
1808f0fd8f6SDimitry Andric     MCSectionELF *CommentSection =
1818f0fd8f6SDimitry Andric         Context.getELFSection(".AMDGPU.csdata", ELF::SHT_PROGBITS, 0);
1828f0fd8f6SDimitry Andric     OutStreamer->SwitchSection(CommentSection);
1838f0fd8f6SDimitry Andric 
1848f0fd8f6SDimitry Andric     if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
1858f0fd8f6SDimitry Andric       OutStreamer->emitRawComment(" Kernel info:", false);
1868f0fd8f6SDimitry Andric       OutStreamer->emitRawComment(" codeLenInByte = " + Twine(KernelInfo.CodeLen),
1878f0fd8f6SDimitry Andric                                   false);
1888f0fd8f6SDimitry Andric       OutStreamer->emitRawComment(" NumSgprs: " + Twine(KernelInfo.NumSGPR),
1898f0fd8f6SDimitry Andric                                   false);
1908f0fd8f6SDimitry Andric       OutStreamer->emitRawComment(" NumVgprs: " + Twine(KernelInfo.NumVGPR),
1918f0fd8f6SDimitry Andric                                   false);
1928f0fd8f6SDimitry Andric       OutStreamer->emitRawComment(" FloatMode: " + Twine(KernelInfo.FloatMode),
1938f0fd8f6SDimitry Andric                                   false);
1948f0fd8f6SDimitry Andric       OutStreamer->emitRawComment(" IeeeMode: " + Twine(KernelInfo.IEEEMode),
1958f0fd8f6SDimitry Andric                                   false);
1968f0fd8f6SDimitry Andric       OutStreamer->emitRawComment(" ScratchSize: " + Twine(KernelInfo.ScratchSize),
1978f0fd8f6SDimitry Andric                                   false);
1983ca95b02SDimitry Andric       OutStreamer->emitRawComment(" LDSByteSize: " + Twine(KernelInfo.LDSSize) +
1993ca95b02SDimitry Andric                                   " bytes/workgroup (compile time only)", false);
2003ca95b02SDimitry Andric 
2013ca95b02SDimitry Andric       OutStreamer->emitRawComment(" ReservedVGPRFirst: " + Twine(KernelInfo.ReservedVGPRFirst),
2023ca95b02SDimitry Andric                                   false);
2033ca95b02SDimitry Andric       OutStreamer->emitRawComment(" ReservedVGPRCount: " + Twine(KernelInfo.ReservedVGPRCount),
2043ca95b02SDimitry Andric                                   false);
2053ca95b02SDimitry Andric 
2063ca95b02SDimitry Andric       if (MF.getSubtarget<SISubtarget>().debuggerEmitPrologue()) {
2073ca95b02SDimitry Andric         OutStreamer->emitRawComment(" DebuggerWavefrontPrivateSegmentOffsetSGPR: s" +
2083ca95b02SDimitry Andric                                     Twine(KernelInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR), false);
2093ca95b02SDimitry Andric         OutStreamer->emitRawComment(" DebuggerPrivateSegmentBufferSGPR: s" +
2103ca95b02SDimitry Andric                                     Twine(KernelInfo.DebuggerPrivateSegmentBufferSGPR), false);
2113ca95b02SDimitry Andric       }
2127d523365SDimitry Andric 
2137d523365SDimitry Andric       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:USER_SGPR: " +
2147d523365SDimitry Andric                                   Twine(G_00B84C_USER_SGPR(KernelInfo.ComputePGMRSrc2)),
2157d523365SDimitry Andric                                   false);
2167d523365SDimitry Andric       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_X_EN: " +
2177d523365SDimitry Andric                                   Twine(G_00B84C_TGID_X_EN(KernelInfo.ComputePGMRSrc2)),
2187d523365SDimitry Andric                                   false);
2197d523365SDimitry Andric       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_Y_EN: " +
2207d523365SDimitry Andric                                   Twine(G_00B84C_TGID_Y_EN(KernelInfo.ComputePGMRSrc2)),
2217d523365SDimitry Andric                                   false);
2227d523365SDimitry Andric       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_Z_EN: " +
2237d523365SDimitry Andric                                   Twine(G_00B84C_TGID_Z_EN(KernelInfo.ComputePGMRSrc2)),
2247d523365SDimitry Andric                                   false);
2257d523365SDimitry Andric       OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: " +
2267d523365SDimitry Andric                                   Twine(G_00B84C_TIDIG_COMP_CNT(KernelInfo.ComputePGMRSrc2)),
2277d523365SDimitry Andric                                   false);
2287d523365SDimitry Andric 
2298f0fd8f6SDimitry Andric     } else {
2308f0fd8f6SDimitry Andric       R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
2318f0fd8f6SDimitry Andric       OutStreamer->emitRawComment(
2328f0fd8f6SDimitry Andric         Twine("SQ_PGM_RESOURCES:STACK_SIZE = " + Twine(MFI->StackSize)));
2338f0fd8f6SDimitry Andric     }
2348f0fd8f6SDimitry Andric   }
2358f0fd8f6SDimitry Andric 
2368f0fd8f6SDimitry Andric   if (STM.dumpCode()) {
2378f0fd8f6SDimitry Andric 
2388f0fd8f6SDimitry Andric     OutStreamer->SwitchSection(
2398f0fd8f6SDimitry Andric         Context.getELFSection(".AMDGPU.disasm", ELF::SHT_NOTE, 0));
2408f0fd8f6SDimitry Andric 
2418f0fd8f6SDimitry Andric     for (size_t i = 0; i < DisasmLines.size(); ++i) {
2428f0fd8f6SDimitry Andric       std::string Comment(DisasmLineMaxLen - DisasmLines[i].size(), ' ');
2438f0fd8f6SDimitry Andric       Comment += " ; " + HexLines[i] + "\n";
2448f0fd8f6SDimitry Andric 
2458f0fd8f6SDimitry Andric       OutStreamer->EmitBytes(StringRef(DisasmLines[i]));
2468f0fd8f6SDimitry Andric       OutStreamer->EmitBytes(StringRef(Comment));
2478f0fd8f6SDimitry Andric     }
2488f0fd8f6SDimitry Andric   }
2498f0fd8f6SDimitry Andric 
2503ca95b02SDimitry Andric   emitRuntimeMetadata(*MF.getFunction());
2513ca95b02SDimitry Andric 
2528f0fd8f6SDimitry Andric   return false;
2538f0fd8f6SDimitry Andric }
2548f0fd8f6SDimitry Andric 
2558f0fd8f6SDimitry Andric void AMDGPUAsmPrinter::EmitProgramInfoR600(const MachineFunction &MF) {
2568f0fd8f6SDimitry Andric   unsigned MaxGPR = 0;
2578f0fd8f6SDimitry Andric   bool killPixel = false;
2583ca95b02SDimitry Andric   const R600Subtarget &STM = MF.getSubtarget<R600Subtarget>();
2593ca95b02SDimitry Andric   const R600RegisterInfo *RI = STM.getRegisterInfo();
2608f0fd8f6SDimitry Andric   const R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
2618f0fd8f6SDimitry Andric 
2628f0fd8f6SDimitry Andric   for (const MachineBasicBlock &MBB : MF) {
2638f0fd8f6SDimitry Andric     for (const MachineInstr &MI : MBB) {
2648f0fd8f6SDimitry Andric       if (MI.getOpcode() == AMDGPU::KILLGT)
2658f0fd8f6SDimitry Andric         killPixel = true;
2668f0fd8f6SDimitry Andric       unsigned numOperands = MI.getNumOperands();
2678f0fd8f6SDimitry Andric       for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
2688f0fd8f6SDimitry Andric         const MachineOperand &MO = MI.getOperand(op_idx);
2698f0fd8f6SDimitry Andric         if (!MO.isReg())
2708f0fd8f6SDimitry Andric           continue;
2718f0fd8f6SDimitry Andric         unsigned HWReg = RI->getEncodingValue(MO.getReg()) & 0xff;
2728f0fd8f6SDimitry Andric 
2738f0fd8f6SDimitry Andric         // Register with value > 127 aren't GPR
2748f0fd8f6SDimitry Andric         if (HWReg > 127)
2758f0fd8f6SDimitry Andric           continue;
2768f0fd8f6SDimitry Andric         MaxGPR = std::max(MaxGPR, HWReg);
2778f0fd8f6SDimitry Andric       }
2788f0fd8f6SDimitry Andric     }
2798f0fd8f6SDimitry Andric   }
2808f0fd8f6SDimitry Andric 
2818f0fd8f6SDimitry Andric   unsigned RsrcReg;
2823ca95b02SDimitry Andric   if (STM.getGeneration() >= R600Subtarget::EVERGREEN) {
2838f0fd8f6SDimitry Andric     // Evergreen / Northern Islands
2843ca95b02SDimitry Andric     switch (MF.getFunction()->getCallingConv()) {
2858f0fd8f6SDimitry Andric     default: // Fall through
2863ca95b02SDimitry Andric     case CallingConv::AMDGPU_CS: RsrcReg = R_0288D4_SQ_PGM_RESOURCES_LS; break;
2873ca95b02SDimitry Andric     case CallingConv::AMDGPU_GS: RsrcReg = R_028878_SQ_PGM_RESOURCES_GS; break;
2883ca95b02SDimitry Andric     case CallingConv::AMDGPU_PS: RsrcReg = R_028844_SQ_PGM_RESOURCES_PS; break;
2893ca95b02SDimitry Andric     case CallingConv::AMDGPU_VS: RsrcReg = R_028860_SQ_PGM_RESOURCES_VS; break;
2908f0fd8f6SDimitry Andric     }
2918f0fd8f6SDimitry Andric   } else {
2928f0fd8f6SDimitry Andric     // R600 / R700
2933ca95b02SDimitry Andric     switch (MF.getFunction()->getCallingConv()) {
2948f0fd8f6SDimitry Andric     default: // Fall through
2953ca95b02SDimitry Andric     case CallingConv::AMDGPU_GS: // Fall through
2963ca95b02SDimitry Andric     case CallingConv::AMDGPU_CS: // Fall through
2973ca95b02SDimitry Andric     case CallingConv::AMDGPU_VS: RsrcReg = R_028868_SQ_PGM_RESOURCES_VS; break;
2983ca95b02SDimitry Andric     case CallingConv::AMDGPU_PS: RsrcReg = R_028850_SQ_PGM_RESOURCES_PS; break;
2998f0fd8f6SDimitry Andric     }
3008f0fd8f6SDimitry Andric   }
3018f0fd8f6SDimitry Andric 
3028f0fd8f6SDimitry Andric   OutStreamer->EmitIntValue(RsrcReg, 4);
3038f0fd8f6SDimitry Andric   OutStreamer->EmitIntValue(S_NUM_GPRS(MaxGPR + 1) |
3048f0fd8f6SDimitry Andric                            S_STACK_SIZE(MFI->StackSize), 4);
3058f0fd8f6SDimitry Andric   OutStreamer->EmitIntValue(R_02880C_DB_SHADER_CONTROL, 4);
3068f0fd8f6SDimitry Andric   OutStreamer->EmitIntValue(S_02880C_KILL_ENABLE(killPixel), 4);
3078f0fd8f6SDimitry Andric 
3083ca95b02SDimitry Andric   if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) {
3098f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_0288E8_SQ_LDS_ALLOC, 4);
3103ca95b02SDimitry Andric     OutStreamer->EmitIntValue(alignTo(MFI->LDSSize, 4) >> 2, 4);
3118f0fd8f6SDimitry Andric   }
3128f0fd8f6SDimitry Andric }
3138f0fd8f6SDimitry Andric 
3148f0fd8f6SDimitry Andric void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo,
3158f0fd8f6SDimitry Andric                                         const MachineFunction &MF) const {
3163ca95b02SDimitry Andric   const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
3178f0fd8f6SDimitry Andric   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
3188f0fd8f6SDimitry Andric   uint64_t CodeSize = 0;
3198f0fd8f6SDimitry Andric   unsigned MaxSGPR = 0;
3208f0fd8f6SDimitry Andric   unsigned MaxVGPR = 0;
3218f0fd8f6SDimitry Andric   bool VCCUsed = false;
3228f0fd8f6SDimitry Andric   bool FlatUsed = false;
3233ca95b02SDimitry Andric   const SIRegisterInfo *RI = STM.getRegisterInfo();
3243ca95b02SDimitry Andric   const SIInstrInfo *TII = STM.getInstrInfo();
3258f0fd8f6SDimitry Andric 
3268f0fd8f6SDimitry Andric   for (const MachineBasicBlock &MBB : MF) {
3278f0fd8f6SDimitry Andric     for (const MachineInstr &MI : MBB) {
3288f0fd8f6SDimitry Andric       // TODO: CodeSize should account for multiple functions.
3299a4b3118SDimitry Andric 
3309a4b3118SDimitry Andric       // TODO: Should we count size of debug info?
3319a4b3118SDimitry Andric       if (MI.isDebugValue())
3329a4b3118SDimitry Andric         continue;
3339a4b3118SDimitry Andric 
3343ca95b02SDimitry Andric       CodeSize += TII->getInstSizeInBytes(MI);
3358f0fd8f6SDimitry Andric 
3368f0fd8f6SDimitry Andric       unsigned numOperands = MI.getNumOperands();
3378f0fd8f6SDimitry Andric       for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
3388f0fd8f6SDimitry Andric         const MachineOperand &MO = MI.getOperand(op_idx);
3398f0fd8f6SDimitry Andric         unsigned width = 0;
3408f0fd8f6SDimitry Andric         bool isSGPR = false;
3418f0fd8f6SDimitry Andric 
3427d523365SDimitry Andric         if (!MO.isReg())
3438f0fd8f6SDimitry Andric           continue;
3448f0fd8f6SDimitry Andric 
3457d523365SDimitry Andric         unsigned reg = MO.getReg();
3468f0fd8f6SDimitry Andric         switch (reg) {
3478f0fd8f6SDimitry Andric         case AMDGPU::EXEC:
3483ca95b02SDimitry Andric         case AMDGPU::EXEC_LO:
3493ca95b02SDimitry Andric         case AMDGPU::EXEC_HI:
3507d523365SDimitry Andric         case AMDGPU::SCC:
3518f0fd8f6SDimitry Andric         case AMDGPU::M0:
3528f0fd8f6SDimitry Andric           continue;
3537d523365SDimitry Andric 
3547d523365SDimitry Andric         case AMDGPU::VCC:
3557d523365SDimitry Andric         case AMDGPU::VCC_LO:
3567d523365SDimitry Andric         case AMDGPU::VCC_HI:
3577d523365SDimitry Andric           VCCUsed = true;
3587d523365SDimitry Andric           continue;
3597d523365SDimitry Andric 
3607d523365SDimitry Andric         case AMDGPU::FLAT_SCR:
3617d523365SDimitry Andric         case AMDGPU::FLAT_SCR_LO:
3627d523365SDimitry Andric         case AMDGPU::FLAT_SCR_HI:
3637d523365SDimitry Andric           FlatUsed = true;
3647d523365SDimitry Andric           continue;
3657d523365SDimitry Andric 
3663ca95b02SDimitry Andric         case AMDGPU::TBA:
3673ca95b02SDimitry Andric         case AMDGPU::TBA_LO:
3683ca95b02SDimitry Andric         case AMDGPU::TBA_HI:
3693ca95b02SDimitry Andric         case AMDGPU::TMA:
3703ca95b02SDimitry Andric         case AMDGPU::TMA_LO:
3713ca95b02SDimitry Andric         case AMDGPU::TMA_HI:
3723ca95b02SDimitry Andric           llvm_unreachable("Trap Handler registers should not be used");
3733ca95b02SDimitry Andric           continue;
3743ca95b02SDimitry Andric 
3757d523365SDimitry Andric         default:
3767d523365SDimitry Andric           break;
3778f0fd8f6SDimitry Andric         }
3788f0fd8f6SDimitry Andric 
3798f0fd8f6SDimitry Andric         if (AMDGPU::SReg_32RegClass.contains(reg)) {
3803ca95b02SDimitry Andric           if (AMDGPU::TTMP_32RegClass.contains(reg)) {
3813ca95b02SDimitry Andric             llvm_unreachable("Trap Handler registers should not be used");
3823ca95b02SDimitry Andric           }
3838f0fd8f6SDimitry Andric           isSGPR = true;
3848f0fd8f6SDimitry Andric           width = 1;
3858f0fd8f6SDimitry Andric         } else if (AMDGPU::VGPR_32RegClass.contains(reg)) {
3868f0fd8f6SDimitry Andric           isSGPR = false;
3878f0fd8f6SDimitry Andric           width = 1;
3888f0fd8f6SDimitry Andric         } else if (AMDGPU::SReg_64RegClass.contains(reg)) {
3893ca95b02SDimitry Andric           if (AMDGPU::TTMP_64RegClass.contains(reg)) {
3903ca95b02SDimitry Andric             llvm_unreachable("Trap Handler registers should not be used");
3913ca95b02SDimitry Andric           }
3928f0fd8f6SDimitry Andric           isSGPR = true;
3938f0fd8f6SDimitry Andric           width = 2;
3948f0fd8f6SDimitry Andric         } else if (AMDGPU::VReg_64RegClass.contains(reg)) {
3958f0fd8f6SDimitry Andric           isSGPR = false;
3968f0fd8f6SDimitry Andric           width = 2;
3978f0fd8f6SDimitry Andric         } else if (AMDGPU::VReg_96RegClass.contains(reg)) {
3988f0fd8f6SDimitry Andric           isSGPR = false;
3998f0fd8f6SDimitry Andric           width = 3;
4008f0fd8f6SDimitry Andric         } else if (AMDGPU::SReg_128RegClass.contains(reg)) {
4018f0fd8f6SDimitry Andric           isSGPR = true;
4028f0fd8f6SDimitry Andric           width = 4;
4038f0fd8f6SDimitry Andric         } else if (AMDGPU::VReg_128RegClass.contains(reg)) {
4048f0fd8f6SDimitry Andric           isSGPR = false;
4058f0fd8f6SDimitry Andric           width = 4;
4068f0fd8f6SDimitry Andric         } else if (AMDGPU::SReg_256RegClass.contains(reg)) {
4078f0fd8f6SDimitry Andric           isSGPR = true;
4088f0fd8f6SDimitry Andric           width = 8;
4098f0fd8f6SDimitry Andric         } else if (AMDGPU::VReg_256RegClass.contains(reg)) {
4108f0fd8f6SDimitry Andric           isSGPR = false;
4118f0fd8f6SDimitry Andric           width = 8;
4128f0fd8f6SDimitry Andric         } else if (AMDGPU::SReg_512RegClass.contains(reg)) {
4138f0fd8f6SDimitry Andric           isSGPR = true;
4148f0fd8f6SDimitry Andric           width = 16;
4158f0fd8f6SDimitry Andric         } else if (AMDGPU::VReg_512RegClass.contains(reg)) {
4168f0fd8f6SDimitry Andric           isSGPR = false;
4178f0fd8f6SDimitry Andric           width = 16;
4188f0fd8f6SDimitry Andric         } else {
4198f0fd8f6SDimitry Andric           llvm_unreachable("Unknown register class");
4208f0fd8f6SDimitry Andric         }
4218f0fd8f6SDimitry Andric         unsigned hwReg = RI->getEncodingValue(reg) & 0xff;
4228f0fd8f6SDimitry Andric         unsigned maxUsed = hwReg + width - 1;
4238f0fd8f6SDimitry Andric         if (isSGPR) {
4248f0fd8f6SDimitry Andric           MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR;
4258f0fd8f6SDimitry Andric         } else {
4268f0fd8f6SDimitry Andric           MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR;
4278f0fd8f6SDimitry Andric         }
4288f0fd8f6SDimitry Andric       }
4298f0fd8f6SDimitry Andric     }
4308f0fd8f6SDimitry Andric   }
4318f0fd8f6SDimitry Andric 
432444ed5c5SDimitry Andric   unsigned ExtraSGPRs = 0;
433444ed5c5SDimitry Andric 
434444ed5c5SDimitry Andric   if (VCCUsed)
435444ed5c5SDimitry Andric     ExtraSGPRs = 2;
436444ed5c5SDimitry Andric 
4373ca95b02SDimitry Andric   if (STM.getGeneration() < SISubtarget::VOLCANIC_ISLANDS) {
438444ed5c5SDimitry Andric     if (FlatUsed)
439444ed5c5SDimitry Andric       ExtraSGPRs = 4;
440444ed5c5SDimitry Andric   } else {
441444ed5c5SDimitry Andric     if (STM.isXNACKEnabled())
442444ed5c5SDimitry Andric       ExtraSGPRs = 4;
4438f0fd8f6SDimitry Andric 
4444d0b32cdSDimitry Andric     if (FlatUsed)
445444ed5c5SDimitry Andric       ExtraSGPRs = 6;
4467d523365SDimitry Andric   }
4478f0fd8f6SDimitry Andric 
448444ed5c5SDimitry Andric   MaxSGPR += ExtraSGPRs;
449444ed5c5SDimitry Andric 
4503ca95b02SDimitry Andric   // Record first reserved register and reserved register count fields, and
4513ca95b02SDimitry Andric   // update max register counts if "amdgpu-debugger-reserve-regs" attribute was
4523ca95b02SDimitry Andric   // specified.
4533ca95b02SDimitry Andric   if (STM.debuggerReserveRegs()) {
4543ca95b02SDimitry Andric     ProgInfo.ReservedVGPRFirst = MaxVGPR + 1;
4553ca95b02SDimitry Andric     ProgInfo.ReservedVGPRCount = MFI->getDebuggerReservedVGPRCount();
4563ca95b02SDimitry Andric     MaxVGPR += MFI->getDebuggerReservedVGPRCount();
4573ca95b02SDimitry Andric   }
4583ca95b02SDimitry Andric 
4593ca95b02SDimitry Andric   // Update DebuggerWavefrontPrivateSegmentOffsetSGPR and
4603ca95b02SDimitry Andric   // DebuggerPrivateSegmentBufferSGPR fields if "amdgpu-debugger-emit-prologue"
4613ca95b02SDimitry Andric   // attribute was specified.
4623ca95b02SDimitry Andric   if (STM.debuggerEmitPrologue()) {
4633ca95b02SDimitry Andric     ProgInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR =
4643ca95b02SDimitry Andric       RI->getHWRegIndex(MFI->getScratchWaveOffsetReg());
4653ca95b02SDimitry Andric     ProgInfo.DebuggerPrivateSegmentBufferSGPR =
4663ca95b02SDimitry Andric       RI->getHWRegIndex(MFI->getScratchRSrcReg());
4673ca95b02SDimitry Andric   }
4683ca95b02SDimitry Andric 
4698f0fd8f6SDimitry Andric   // We found the maximum register index. They start at 0, so add one to get the
4708f0fd8f6SDimitry Andric   // number of registers.
4718f0fd8f6SDimitry Andric   ProgInfo.NumVGPR = MaxVGPR + 1;
4728f0fd8f6SDimitry Andric   ProgInfo.NumSGPR = MaxSGPR + 1;
4738f0fd8f6SDimitry Andric 
4748f0fd8f6SDimitry Andric   if (STM.hasSGPRInitBug()) {
4753ca95b02SDimitry Andric     if (ProgInfo.NumSGPR > SISubtarget::FIXED_SGPR_COUNT_FOR_INIT_BUG) {
4768f0fd8f6SDimitry Andric       LLVMContext &Ctx = MF.getFunction()->getContext();
4773ca95b02SDimitry Andric       DiagnosticInfoResourceLimit Diag(*MF.getFunction(),
4783ca95b02SDimitry Andric                                        "SGPRs with SGPR init bug",
4793ca95b02SDimitry Andric                                        ProgInfo.NumSGPR, DS_Error);
4803ca95b02SDimitry Andric       Ctx.diagnose(Diag);
4818f0fd8f6SDimitry Andric     }
4828f0fd8f6SDimitry Andric 
4833ca95b02SDimitry Andric     ProgInfo.NumSGPR = SISubtarget::FIXED_SGPR_COUNT_FOR_INIT_BUG;
4848f0fd8f6SDimitry Andric   }
4858f0fd8f6SDimitry Andric 
4867d523365SDimitry Andric   if (MFI->NumUserSGPRs > STM.getMaxNumUserSGPRs()) {
4877d523365SDimitry Andric     LLVMContext &Ctx = MF.getFunction()->getContext();
4883ca95b02SDimitry Andric     DiagnosticInfoResourceLimit Diag(*MF.getFunction(), "user SGPRs",
4893ca95b02SDimitry Andric                                      MFI->NumUserSGPRs, DS_Error);
4903ca95b02SDimitry Andric     Ctx.diagnose(Diag);
4913ca95b02SDimitry Andric   }
4923ca95b02SDimitry Andric 
4933ca95b02SDimitry Andric   if (MFI->LDSSize > static_cast<unsigned>(STM.getLocalMemorySize())) {
4943ca95b02SDimitry Andric     LLVMContext &Ctx = MF.getFunction()->getContext();
4953ca95b02SDimitry Andric     DiagnosticInfoResourceLimit Diag(*MF.getFunction(), "local memory",
4963ca95b02SDimitry Andric                                      MFI->LDSSize, DS_Error);
4973ca95b02SDimitry Andric     Ctx.diagnose(Diag);
4987d523365SDimitry Andric   }
4997d523365SDimitry Andric 
5008f0fd8f6SDimitry Andric   ProgInfo.VGPRBlocks = (ProgInfo.NumVGPR - 1) / 4;
5018f0fd8f6SDimitry Andric   ProgInfo.SGPRBlocks = (ProgInfo.NumSGPR - 1) / 8;
5028f0fd8f6SDimitry Andric   // Set the value to initialize FP_ROUND and FP_DENORM parts of the mode
5038f0fd8f6SDimitry Andric   // register.
5048f0fd8f6SDimitry Andric   ProgInfo.FloatMode = getFPMode(MF);
5058f0fd8f6SDimitry Andric 
5068f0fd8f6SDimitry Andric   ProgInfo.IEEEMode = 0;
5078f0fd8f6SDimitry Andric 
5083ca95b02SDimitry Andric   // Make clamp modifier on NaN input returns 0.
5093ca95b02SDimitry Andric   ProgInfo.DX10Clamp = 1;
5108f0fd8f6SDimitry Andric 
5118f0fd8f6SDimitry Andric   const MachineFrameInfo *FrameInfo = MF.getFrameInfo();
5123ca95b02SDimitry Andric   ProgInfo.ScratchSize = FrameInfo->getStackSize();
5138f0fd8f6SDimitry Andric 
5148f0fd8f6SDimitry Andric   ProgInfo.FlatUsed = FlatUsed;
5158f0fd8f6SDimitry Andric   ProgInfo.VCCUsed = VCCUsed;
5168f0fd8f6SDimitry Andric   ProgInfo.CodeLen = CodeSize;
5178f0fd8f6SDimitry Andric 
5188f0fd8f6SDimitry Andric   unsigned LDSAlignShift;
5193ca95b02SDimitry Andric   if (STM.getGeneration() < SISubtarget::SEA_ISLANDS) {
5208f0fd8f6SDimitry Andric     // LDS is allocated in 64 dword blocks.
5218f0fd8f6SDimitry Andric     LDSAlignShift = 8;
5228f0fd8f6SDimitry Andric   } else {
5238f0fd8f6SDimitry Andric     // LDS is allocated in 128 dword blocks.
5248f0fd8f6SDimitry Andric     LDSAlignShift = 9;
5258f0fd8f6SDimitry Andric   }
5268f0fd8f6SDimitry Andric 
5278f0fd8f6SDimitry Andric   unsigned LDSSpillSize = MFI->LDSWaveSpillSize *
5288f0fd8f6SDimitry Andric                           MFI->getMaximumWorkGroupSize(MF);
5298f0fd8f6SDimitry Andric 
5308f0fd8f6SDimitry Andric   ProgInfo.LDSSize = MFI->LDSSize + LDSSpillSize;
5318f0fd8f6SDimitry Andric   ProgInfo.LDSBlocks =
5323ca95b02SDimitry Andric       alignTo(ProgInfo.LDSSize, 1ULL << LDSAlignShift) >> LDSAlignShift;
5338f0fd8f6SDimitry Andric 
5348f0fd8f6SDimitry Andric   // Scratch is allocated in 256 dword blocks.
5358f0fd8f6SDimitry Andric   unsigned ScratchAlignShift = 10;
5368f0fd8f6SDimitry Andric   // We need to program the hardware with the amount of scratch memory that
5378f0fd8f6SDimitry Andric   // is used by the entire wave.  ProgInfo.ScratchSize is the amount of
5388f0fd8f6SDimitry Andric   // scratch memory used per thread.
5398f0fd8f6SDimitry Andric   ProgInfo.ScratchBlocks =
5403ca95b02SDimitry Andric       alignTo(ProgInfo.ScratchSize * STM.getWavefrontSize(),
5413ca95b02SDimitry Andric               1ULL << ScratchAlignShift) >>
5423ca95b02SDimitry Andric       ScratchAlignShift;
5438f0fd8f6SDimitry Andric 
5448f0fd8f6SDimitry Andric   ProgInfo.ComputePGMRSrc1 =
5458f0fd8f6SDimitry Andric       S_00B848_VGPRS(ProgInfo.VGPRBlocks) |
5468f0fd8f6SDimitry Andric       S_00B848_SGPRS(ProgInfo.SGPRBlocks) |
5478f0fd8f6SDimitry Andric       S_00B848_PRIORITY(ProgInfo.Priority) |
5488f0fd8f6SDimitry Andric       S_00B848_FLOAT_MODE(ProgInfo.FloatMode) |
5498f0fd8f6SDimitry Andric       S_00B848_PRIV(ProgInfo.Priv) |
5508f0fd8f6SDimitry Andric       S_00B848_DX10_CLAMP(ProgInfo.DX10Clamp) |
5517d523365SDimitry Andric       S_00B848_DEBUG_MODE(ProgInfo.DebugMode) |
5528f0fd8f6SDimitry Andric       S_00B848_IEEE_MODE(ProgInfo.IEEEMode);
5538f0fd8f6SDimitry Andric 
5547d523365SDimitry Andric   // 0 = X, 1 = XY, 2 = XYZ
5557d523365SDimitry Andric   unsigned TIDIGCompCnt = 0;
5567d523365SDimitry Andric   if (MFI->hasWorkItemIDZ())
5577d523365SDimitry Andric     TIDIGCompCnt = 2;
5587d523365SDimitry Andric   else if (MFI->hasWorkItemIDY())
5597d523365SDimitry Andric     TIDIGCompCnt = 1;
5607d523365SDimitry Andric 
5618f0fd8f6SDimitry Andric   ProgInfo.ComputePGMRSrc2 =
5628f0fd8f6SDimitry Andric       S_00B84C_SCRATCH_EN(ProgInfo.ScratchBlocks > 0) |
5637d523365SDimitry Andric       S_00B84C_USER_SGPR(MFI->getNumUserSGPRs()) |
5647d523365SDimitry Andric       S_00B84C_TGID_X_EN(MFI->hasWorkGroupIDX()) |
5657d523365SDimitry Andric       S_00B84C_TGID_Y_EN(MFI->hasWorkGroupIDY()) |
5667d523365SDimitry Andric       S_00B84C_TGID_Z_EN(MFI->hasWorkGroupIDZ()) |
5677d523365SDimitry Andric       S_00B84C_TG_SIZE_EN(MFI->hasWorkGroupInfo()) |
5687d523365SDimitry Andric       S_00B84C_TIDIG_COMP_CNT(TIDIGCompCnt) |
5697d523365SDimitry Andric       S_00B84C_EXCP_EN_MSB(0) |
5707d523365SDimitry Andric       S_00B84C_LDS_SIZE(ProgInfo.LDSBlocks) |
5717d523365SDimitry Andric       S_00B84C_EXCP_EN(0);
5728f0fd8f6SDimitry Andric }
5738f0fd8f6SDimitry Andric 
5743ca95b02SDimitry Andric static unsigned getRsrcReg(CallingConv::ID CallConv) {
5753ca95b02SDimitry Andric   switch (CallConv) {
5768f0fd8f6SDimitry Andric   default: // Fall through
5773ca95b02SDimitry Andric   case CallingConv::AMDGPU_CS: return R_00B848_COMPUTE_PGM_RSRC1;
5783ca95b02SDimitry Andric   case CallingConv::AMDGPU_GS: return R_00B228_SPI_SHADER_PGM_RSRC1_GS;
5793ca95b02SDimitry Andric   case CallingConv::AMDGPU_PS: return R_00B028_SPI_SHADER_PGM_RSRC1_PS;
5803ca95b02SDimitry Andric   case CallingConv::AMDGPU_VS: return R_00B128_SPI_SHADER_PGM_RSRC1_VS;
5818f0fd8f6SDimitry Andric   }
5828f0fd8f6SDimitry Andric }
5838f0fd8f6SDimitry Andric 
5848f0fd8f6SDimitry Andric void AMDGPUAsmPrinter::EmitProgramInfoSI(const MachineFunction &MF,
5858f0fd8f6SDimitry Andric                                          const SIProgramInfo &KernelInfo) {
5863ca95b02SDimitry Andric   const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
5878f0fd8f6SDimitry Andric   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
5883ca95b02SDimitry Andric   unsigned RsrcReg = getRsrcReg(MF.getFunction()->getCallingConv());
5898f0fd8f6SDimitry Andric 
5903ca95b02SDimitry Andric   if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) {
5918f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_00B848_COMPUTE_PGM_RSRC1, 4);
5928f0fd8f6SDimitry Andric 
5938f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(KernelInfo.ComputePGMRSrc1, 4);
5948f0fd8f6SDimitry Andric 
5958f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4);
5968f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(KernelInfo.ComputePGMRSrc2, 4);
5978f0fd8f6SDimitry Andric 
5988f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_00B860_COMPUTE_TMPRING_SIZE, 4);
5998f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(S_00B860_WAVESIZE(KernelInfo.ScratchBlocks), 4);
6008f0fd8f6SDimitry Andric 
6018f0fd8f6SDimitry Andric     // TODO: Should probably note flat usage somewhere. SC emits a "FlatPtr32 =
6028f0fd8f6SDimitry Andric     // 0" comment but I don't see a corresponding field in the register spec.
6038f0fd8f6SDimitry Andric   } else {
6048f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(RsrcReg, 4);
6058f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(S_00B028_VGPRS(KernelInfo.VGPRBlocks) |
6068f0fd8f6SDimitry Andric                               S_00B028_SGPRS(KernelInfo.SGPRBlocks), 4);
6073ca95b02SDimitry Andric     if (STM.isVGPRSpillingEnabled(*MF.getFunction())) {
6088f0fd8f6SDimitry Andric       OutStreamer->EmitIntValue(R_0286E8_SPI_TMPRING_SIZE, 4);
6098f0fd8f6SDimitry Andric       OutStreamer->EmitIntValue(S_0286E8_WAVESIZE(KernelInfo.ScratchBlocks), 4);
6108f0fd8f6SDimitry Andric     }
6118f0fd8f6SDimitry Andric   }
6128f0fd8f6SDimitry Andric 
6133ca95b02SDimitry Andric   if (MF.getFunction()->getCallingConv() == CallingConv::AMDGPU_PS) {
6148f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4);
6158f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(KernelInfo.LDSBlocks), 4);
6168f0fd8f6SDimitry Andric     OutStreamer->EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4);
617444ed5c5SDimitry Andric     OutStreamer->EmitIntValue(MFI->PSInputEna, 4);
618444ed5c5SDimitry Andric     OutStreamer->EmitIntValue(R_0286D0_SPI_PS_INPUT_ADDR, 4);
619444ed5c5SDimitry Andric     OutStreamer->EmitIntValue(MFI->getPSInputAddr(), 4);
6208f0fd8f6SDimitry Andric   }
6213ca95b02SDimitry Andric 
6223ca95b02SDimitry Andric   OutStreamer->EmitIntValue(R_SPILLED_SGPRS, 4);
6233ca95b02SDimitry Andric   OutStreamer->EmitIntValue(MFI->getNumSpilledSGPRs(), 4);
6243ca95b02SDimitry Andric   OutStreamer->EmitIntValue(R_SPILLED_VGPRS, 4);
6253ca95b02SDimitry Andric   OutStreamer->EmitIntValue(MFI->getNumSpilledVGPRs(), 4);
6263ca95b02SDimitry Andric }
6273ca95b02SDimitry Andric 
6283ca95b02SDimitry Andric // This is supposed to be log2(Size)
6293ca95b02SDimitry Andric static amd_element_byte_size_t getElementByteSizeValue(unsigned Size) {
6303ca95b02SDimitry Andric   switch (Size) {
6313ca95b02SDimitry Andric   case 4:
6323ca95b02SDimitry Andric     return AMD_ELEMENT_4_BYTES;
6333ca95b02SDimitry Andric   case 8:
6343ca95b02SDimitry Andric     return AMD_ELEMENT_8_BYTES;
6353ca95b02SDimitry Andric   case 16:
6363ca95b02SDimitry Andric     return AMD_ELEMENT_16_BYTES;
6373ca95b02SDimitry Andric   default:
6383ca95b02SDimitry Andric     llvm_unreachable("invalid private_element_size");
6393ca95b02SDimitry Andric   }
6408f0fd8f6SDimitry Andric }
6418f0fd8f6SDimitry Andric 
6428f0fd8f6SDimitry Andric void AMDGPUAsmPrinter::EmitAmdKernelCodeT(const MachineFunction &MF,
6438f0fd8f6SDimitry Andric                                          const SIProgramInfo &KernelInfo) const {
6448f0fd8f6SDimitry Andric   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
6453ca95b02SDimitry Andric   const SISubtarget &STM = MF.getSubtarget<SISubtarget>();
6468f0fd8f6SDimitry Andric   amd_kernel_code_t header;
6478f0fd8f6SDimitry Andric 
6483dac3a9bSDimitry Andric   AMDGPU::initDefaultAMDKernelCodeT(header, STM.getFeatureBits());
6498f0fd8f6SDimitry Andric 
6508f0fd8f6SDimitry Andric   header.compute_pgm_resource_registers =
6518f0fd8f6SDimitry Andric       KernelInfo.ComputePGMRSrc1 |
6528f0fd8f6SDimitry Andric       (KernelInfo.ComputePGMRSrc2 << 32);
6537d523365SDimitry Andric   header.code_properties = AMD_CODE_PROPERTY_IS_PTR64;
6547d523365SDimitry Andric 
6553ca95b02SDimitry Andric 
6563ca95b02SDimitry Andric   AMD_HSA_BITS_SET(header.code_properties,
6573ca95b02SDimitry Andric                    AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE,
6583ca95b02SDimitry Andric                    getElementByteSizeValue(STM.getMaxPrivateElementSize()));
6593ca95b02SDimitry Andric 
6607d523365SDimitry Andric   if (MFI->hasPrivateSegmentBuffer()) {
6617d523365SDimitry Andric     header.code_properties |=
6627d523365SDimitry Andric       AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER;
6637d523365SDimitry Andric   }
6647d523365SDimitry Andric 
6657d523365SDimitry Andric   if (MFI->hasDispatchPtr())
6667d523365SDimitry Andric     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
6677d523365SDimitry Andric 
6687d523365SDimitry Andric   if (MFI->hasQueuePtr())
6697d523365SDimitry Andric     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR;
6707d523365SDimitry Andric 
6717d523365SDimitry Andric   if (MFI->hasKernargSegmentPtr())
6727d523365SDimitry Andric     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR;
6737d523365SDimitry Andric 
6747d523365SDimitry Andric   if (MFI->hasDispatchID())
6757d523365SDimitry Andric     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID;
6767d523365SDimitry Andric 
6777d523365SDimitry Andric   if (MFI->hasFlatScratchInit())
6787d523365SDimitry Andric     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT;
6797d523365SDimitry Andric 
6807d523365SDimitry Andric   // TODO: Private segment size
6817d523365SDimitry Andric 
6827d523365SDimitry Andric   if (MFI->hasGridWorkgroupCountX()) {
6837d523365SDimitry Andric     header.code_properties |=
6847d523365SDimitry Andric       AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X;
6857d523365SDimitry Andric   }
6867d523365SDimitry Andric 
6877d523365SDimitry Andric   if (MFI->hasGridWorkgroupCountY()) {
6887d523365SDimitry Andric     header.code_properties |=
6897d523365SDimitry Andric       AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y;
6907d523365SDimitry Andric   }
6917d523365SDimitry Andric 
6927d523365SDimitry Andric   if (MFI->hasGridWorkgroupCountZ()) {
6937d523365SDimitry Andric     header.code_properties |=
6947d523365SDimitry Andric       AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z;
6957d523365SDimitry Andric   }
6967d523365SDimitry Andric 
6977d523365SDimitry Andric   if (MFI->hasDispatchPtr())
6987d523365SDimitry Andric     header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR;
6998f0fd8f6SDimitry Andric 
7003ca95b02SDimitry Andric   if (STM.debuggerSupported())
7013ca95b02SDimitry Andric     header.code_properties |= AMD_CODE_PROPERTY_IS_DEBUG_SUPPORTED;
7023ca95b02SDimitry Andric 
7034d0b32cdSDimitry Andric   if (STM.isXNACKEnabled())
7044d0b32cdSDimitry Andric     header.code_properties |= AMD_CODE_PROPERTY_IS_XNACK_SUPPORTED;
7054d0b32cdSDimitry Andric 
7068f0fd8f6SDimitry Andric   header.kernarg_segment_byte_size = MFI->ABIArgOffset;
7078f0fd8f6SDimitry Andric   header.wavefront_sgpr_count = KernelInfo.NumSGPR;
7088f0fd8f6SDimitry Andric   header.workitem_vgpr_count = KernelInfo.NumVGPR;
7097d523365SDimitry Andric   header.workitem_private_segment_byte_size = KernelInfo.ScratchSize;
7107d523365SDimitry Andric   header.workgroup_group_segment_byte_size = KernelInfo.LDSSize;
7113ca95b02SDimitry Andric   header.reserved_vgpr_first = KernelInfo.ReservedVGPRFirst;
7123ca95b02SDimitry Andric   header.reserved_vgpr_count = KernelInfo.ReservedVGPRCount;
7133ca95b02SDimitry Andric 
7143ca95b02SDimitry Andric   if (STM.debuggerEmitPrologue()) {
7153ca95b02SDimitry Andric     header.debug_wavefront_private_segment_offset_sgpr =
7163ca95b02SDimitry Andric       KernelInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR;
7173ca95b02SDimitry Andric     header.debug_private_segment_buffer_sgpr =
7183ca95b02SDimitry Andric       KernelInfo.DebuggerPrivateSegmentBufferSGPR;
7193ca95b02SDimitry Andric   }
7208f0fd8f6SDimitry Andric 
7213dac3a9bSDimitry Andric   AMDGPUTargetStreamer *TS =
7223dac3a9bSDimitry Andric       static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer());
7233ca95b02SDimitry Andric 
7243ca95b02SDimitry Andric   OutStreamer->SwitchSection(getObjFileLowering().getTextSection());
7253dac3a9bSDimitry Andric   TS->EmitAMDKernelCodeT(header);
7268f0fd8f6SDimitry Andric }
7278f0fd8f6SDimitry Andric 
7288f0fd8f6SDimitry Andric bool AMDGPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
7298f0fd8f6SDimitry Andric                                        unsigned AsmVariant,
7308f0fd8f6SDimitry Andric                                        const char *ExtraCode, raw_ostream &O) {
7318f0fd8f6SDimitry Andric   if (ExtraCode && ExtraCode[0]) {
7328f0fd8f6SDimitry Andric     if (ExtraCode[1] != 0)
7338f0fd8f6SDimitry Andric       return true; // Unknown modifier.
7348f0fd8f6SDimitry Andric 
7358f0fd8f6SDimitry Andric     switch (ExtraCode[0]) {
7368f0fd8f6SDimitry Andric     default:
7378f0fd8f6SDimitry Andric       // See if this is a generic print operand
7388f0fd8f6SDimitry Andric       return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O);
7398f0fd8f6SDimitry Andric     case 'r':
7408f0fd8f6SDimitry Andric       break;
7418f0fd8f6SDimitry Andric     }
7428f0fd8f6SDimitry Andric   }
7438f0fd8f6SDimitry Andric 
7448f0fd8f6SDimitry Andric   AMDGPUInstPrinter::printRegOperand(MI->getOperand(OpNo).getReg(), O,
7458f0fd8f6SDimitry Andric                    *TM.getSubtargetImpl(*MF->getFunction())->getRegisterInfo());
7468f0fd8f6SDimitry Andric   return false;
7478f0fd8f6SDimitry Andric }
7483ca95b02SDimitry Andric 
7493ca95b02SDimitry Andric // Emit a key and an integer value for runtime metadata.
7503ca95b02SDimitry Andric static void emitRuntimeMDIntValue(std::unique_ptr<MCStreamer> &Streamer,
7513ca95b02SDimitry Andric                                   RuntimeMD::Key K, uint64_t V,
7523ca95b02SDimitry Andric                                   unsigned Size) {
7533ca95b02SDimitry Andric   Streamer->EmitIntValue(K, 1);
7543ca95b02SDimitry Andric   Streamer->EmitIntValue(V, Size);
7553ca95b02SDimitry Andric }
7563ca95b02SDimitry Andric 
7573ca95b02SDimitry Andric // Emit a key and a string value for runtime metadata.
7583ca95b02SDimitry Andric static void emitRuntimeMDStringValue(std::unique_ptr<MCStreamer> &Streamer,
7593ca95b02SDimitry Andric                                      RuntimeMD::Key K, StringRef S) {
7603ca95b02SDimitry Andric   Streamer->EmitIntValue(K, 1);
7613ca95b02SDimitry Andric   Streamer->EmitIntValue(S.size(), 4);
7623ca95b02SDimitry Andric   Streamer->EmitBytes(S);
7633ca95b02SDimitry Andric }
7643ca95b02SDimitry Andric 
7653ca95b02SDimitry Andric // Emit a key and three integer values for runtime metadata.
7663ca95b02SDimitry Andric // The three integer values are obtained from MDNode \p Node;
7673ca95b02SDimitry Andric static void emitRuntimeMDThreeIntValues(std::unique_ptr<MCStreamer> &Streamer,
7683ca95b02SDimitry Andric                                         RuntimeMD::Key K, MDNode *Node,
7693ca95b02SDimitry Andric                                         unsigned Size) {
7703ca95b02SDimitry Andric   Streamer->EmitIntValue(K, 1);
7713ca95b02SDimitry Andric   Streamer->EmitIntValue(mdconst::extract<ConstantInt>(
7723ca95b02SDimitry Andric     Node->getOperand(0))->getZExtValue(), Size);
7733ca95b02SDimitry Andric   Streamer->EmitIntValue(mdconst::extract<ConstantInt>(
7743ca95b02SDimitry Andric     Node->getOperand(1))->getZExtValue(), Size);
7753ca95b02SDimitry Andric   Streamer->EmitIntValue(mdconst::extract<ConstantInt>(
7763ca95b02SDimitry Andric     Node->getOperand(2))->getZExtValue(), Size);
7773ca95b02SDimitry Andric }
7783ca95b02SDimitry Andric 
7793ca95b02SDimitry Andric void AMDGPUAsmPrinter::emitStartOfRuntimeMetadata(const Module &M) {
7803ca95b02SDimitry Andric   OutStreamer->SwitchSection(getObjFileLowering().getContext()
7813ca95b02SDimitry Andric     .getELFSection(RuntimeMD::SectionName, ELF::SHT_PROGBITS, 0));
7823ca95b02SDimitry Andric 
7833ca95b02SDimitry Andric   emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyMDVersion,
7843ca95b02SDimitry Andric                         RuntimeMD::MDVersion << 8 | RuntimeMD::MDRevision, 2);
7853ca95b02SDimitry Andric   if (auto MD = M.getNamedMetadata("opencl.ocl.version")) {
7863ca95b02SDimitry Andric     emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyLanguage,
7873ca95b02SDimitry Andric                           RuntimeMD::OpenCL_C, 1);
7883ca95b02SDimitry Andric     auto Node = MD->getOperand(0);
7893ca95b02SDimitry Andric     unsigned short Major = mdconst::extract<ConstantInt>(Node->getOperand(0))
7903ca95b02SDimitry Andric                              ->getZExtValue();
7913ca95b02SDimitry Andric     unsigned short Minor = mdconst::extract<ConstantInt>(Node->getOperand(1))
7923ca95b02SDimitry Andric                              ->getZExtValue();
7933ca95b02SDimitry Andric     emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyLanguageVersion,
7943ca95b02SDimitry Andric                           Major * 100 + Minor * 10, 2);
7953ca95b02SDimitry Andric   }
7963ca95b02SDimitry Andric }
7973ca95b02SDimitry Andric 
7983ca95b02SDimitry Andric static std::string getOCLTypeName(Type *Ty, bool isSigned) {
7993ca95b02SDimitry Andric   if (VectorType* VecTy = dyn_cast<VectorType>(Ty)) {
8003ca95b02SDimitry Andric     Type* EleTy = VecTy->getElementType();
8013ca95b02SDimitry Andric     unsigned Size = VecTy->getVectorNumElements();
8023ca95b02SDimitry Andric     return (Twine(getOCLTypeName(EleTy, isSigned)) + Twine(Size)).str();
8033ca95b02SDimitry Andric   }
8043ca95b02SDimitry Andric   switch (Ty->getTypeID()) {
8053ca95b02SDimitry Andric   case Type::HalfTyID:   return "half";
8063ca95b02SDimitry Andric   case Type::FloatTyID:  return "float";
8073ca95b02SDimitry Andric   case Type::DoubleTyID: return "double";
8083ca95b02SDimitry Andric   case Type::IntegerTyID: {
8093ca95b02SDimitry Andric     if (!isSigned)
8103ca95b02SDimitry Andric       return (Twine('u') + Twine(getOCLTypeName(Ty, true))).str();
8113ca95b02SDimitry Andric     auto IntTy = cast<IntegerType>(Ty);
8123ca95b02SDimitry Andric     auto BW = IntTy->getIntegerBitWidth();
8133ca95b02SDimitry Andric     switch (BW) {
8143ca95b02SDimitry Andric     case 8:
8153ca95b02SDimitry Andric       return "char";
8163ca95b02SDimitry Andric     case 16:
8173ca95b02SDimitry Andric       return "short";
8183ca95b02SDimitry Andric     case 32:
8193ca95b02SDimitry Andric       return "int";
8203ca95b02SDimitry Andric     case 64:
8213ca95b02SDimitry Andric       return "long";
8223ca95b02SDimitry Andric     default:
8233ca95b02SDimitry Andric       return (Twine('i') + Twine(BW)).str();
8243ca95b02SDimitry Andric     }
8253ca95b02SDimitry Andric   }
8263ca95b02SDimitry Andric   default:
8273ca95b02SDimitry Andric     llvm_unreachable("invalid type");
8283ca95b02SDimitry Andric   }
8293ca95b02SDimitry Andric }
8303ca95b02SDimitry Andric 
8313ca95b02SDimitry Andric static RuntimeMD::KernelArg::ValueType getRuntimeMDValueType(
8323ca95b02SDimitry Andric          Type *Ty, StringRef TypeName) {
8333ca95b02SDimitry Andric   if (auto VT = dyn_cast<VectorType>(Ty))
8343ca95b02SDimitry Andric     return getRuntimeMDValueType(VT->getElementType(), TypeName);
8353ca95b02SDimitry Andric   else if (auto PT = dyn_cast<PointerType>(Ty))
8363ca95b02SDimitry Andric     return getRuntimeMDValueType(PT->getElementType(), TypeName);
8373ca95b02SDimitry Andric   else if (Ty->isHalfTy())
8383ca95b02SDimitry Andric     return RuntimeMD::KernelArg::F16;
8393ca95b02SDimitry Andric   else if (Ty->isFloatTy())
8403ca95b02SDimitry Andric     return RuntimeMD::KernelArg::F32;
8413ca95b02SDimitry Andric   else if (Ty->isDoubleTy())
8423ca95b02SDimitry Andric     return RuntimeMD::KernelArg::F64;
8433ca95b02SDimitry Andric   else if (IntegerType* intTy = dyn_cast<IntegerType>(Ty)) {
8443ca95b02SDimitry Andric     bool Signed = !TypeName.startswith("u");
8453ca95b02SDimitry Andric     switch (intTy->getIntegerBitWidth()) {
8463ca95b02SDimitry Andric     case 8:
8473ca95b02SDimitry Andric       return Signed ? RuntimeMD::KernelArg::I8 : RuntimeMD::KernelArg::U8;
8483ca95b02SDimitry Andric     case 16:
8493ca95b02SDimitry Andric       return Signed ? RuntimeMD::KernelArg::I16 : RuntimeMD::KernelArg::U16;
8503ca95b02SDimitry Andric     case 32:
8513ca95b02SDimitry Andric       return Signed ? RuntimeMD::KernelArg::I32 : RuntimeMD::KernelArg::U32;
8523ca95b02SDimitry Andric     case 64:
8533ca95b02SDimitry Andric       return Signed ? RuntimeMD::KernelArg::I64 : RuntimeMD::KernelArg::U64;
8543ca95b02SDimitry Andric     default:
8553ca95b02SDimitry Andric       // Runtime does not recognize other integer types. Report as
8563ca95b02SDimitry Andric       // struct type.
8573ca95b02SDimitry Andric       return RuntimeMD::KernelArg::Struct;
8583ca95b02SDimitry Andric     }
8593ca95b02SDimitry Andric   } else
8603ca95b02SDimitry Andric     return RuntimeMD::KernelArg::Struct;
8613ca95b02SDimitry Andric }
8623ca95b02SDimitry Andric 
8633ca95b02SDimitry Andric void AMDGPUAsmPrinter::emitRuntimeMetadata(const Function &F) {
8643ca95b02SDimitry Andric   if (!F.getMetadata("kernel_arg_type"))
8653ca95b02SDimitry Andric     return;
8663ca95b02SDimitry Andric 
8673ca95b02SDimitry Andric   MCContext &Context = getObjFileLowering().getContext();
8683ca95b02SDimitry Andric   OutStreamer->SwitchSection(
8693ca95b02SDimitry Andric       Context.getELFSection(RuntimeMD::SectionName, ELF::SHT_PROGBITS, 0));
8703ca95b02SDimitry Andric   OutStreamer->EmitIntValue(RuntimeMD::KeyKernelBegin, 1);
8713ca95b02SDimitry Andric   emitRuntimeMDStringValue(OutStreamer, RuntimeMD::KeyKernelName, F.getName());
8723ca95b02SDimitry Andric 
8733ca95b02SDimitry Andric   for (auto &Arg:F.args()) {
8743ca95b02SDimitry Andric     // Emit KeyArgBegin.
8753ca95b02SDimitry Andric     unsigned I = Arg.getArgNo();
8763ca95b02SDimitry Andric     OutStreamer->EmitIntValue(RuntimeMD::KeyArgBegin, 1);
8773ca95b02SDimitry Andric 
8783ca95b02SDimitry Andric     // Emit KeyArgSize and KeyArgAlign.
8793ca95b02SDimitry Andric     auto T = Arg.getType();
8803ca95b02SDimitry Andric     auto DL = F.getParent()->getDataLayout();
8813ca95b02SDimitry Andric     emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyArgSize,
8823ca95b02SDimitry Andric                           DL.getTypeAllocSize(T), 4);
8833ca95b02SDimitry Andric     emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyArgAlign,
8843ca95b02SDimitry Andric                           DL.getABITypeAlignment(T), 4);
8853ca95b02SDimitry Andric 
8863ca95b02SDimitry Andric     // Emit KeyArgTypeName.
8873ca95b02SDimitry Andric     auto TypeName = dyn_cast<MDString>(F.getMetadata(
8883ca95b02SDimitry Andric       "kernel_arg_type")->getOperand(I))->getString();
8893ca95b02SDimitry Andric     emitRuntimeMDStringValue(OutStreamer, RuntimeMD::KeyArgTypeName, TypeName);
8903ca95b02SDimitry Andric 
8913ca95b02SDimitry Andric     // Emit KeyArgName.
8923ca95b02SDimitry Andric     if (auto ArgNameMD = F.getMetadata("kernel_arg_name")) {
8933ca95b02SDimitry Andric       auto ArgName = cast<MDString>(ArgNameMD->getOperand(
8943ca95b02SDimitry Andric         I))->getString();
8953ca95b02SDimitry Andric       emitRuntimeMDStringValue(OutStreamer, RuntimeMD::KeyArgName, ArgName);
8963ca95b02SDimitry Andric     }
8973ca95b02SDimitry Andric 
8983ca95b02SDimitry Andric     // Emit KeyArgIsVolatile, KeyArgIsRestrict, KeyArgIsConst and KeyArgIsPipe.
8993ca95b02SDimitry Andric     auto TypeQual = cast<MDString>(F.getMetadata(
9003ca95b02SDimitry Andric       "kernel_arg_type_qual")->getOperand(I))->getString();
9013ca95b02SDimitry Andric     SmallVector<StringRef, 1> SplitQ;
9023ca95b02SDimitry Andric     TypeQual.split(SplitQ, " ", -1, false/* drop empty entry*/);
9033ca95b02SDimitry Andric     for (auto &I:SplitQ) {
9043ca95b02SDimitry Andric       auto Key = StringSwitch<RuntimeMD::Key>(I)
9053ca95b02SDimitry Andric         .Case("volatile", RuntimeMD::KeyArgIsVolatile)
9063ca95b02SDimitry Andric         .Case("restrict", RuntimeMD::KeyArgIsRestrict)
9073ca95b02SDimitry Andric         .Case("const",    RuntimeMD::KeyArgIsConst)
9083ca95b02SDimitry Andric         .Case("pipe",     RuntimeMD::KeyArgIsPipe)
9093ca95b02SDimitry Andric         .Default(RuntimeMD::KeyNull);
9103ca95b02SDimitry Andric       OutStreamer->EmitIntValue(Key, 1);
9113ca95b02SDimitry Andric     }
9123ca95b02SDimitry Andric 
9133ca95b02SDimitry Andric     // Emit KeyArgTypeKind.
9143ca95b02SDimitry Andric     auto BaseTypeName = cast<MDString>(
9153ca95b02SDimitry Andric       F.getMetadata("kernel_arg_base_type")->getOperand(I))->getString();
9163ca95b02SDimitry Andric     auto TypeKind = StringSwitch<RuntimeMD::KernelArg::TypeKind>(BaseTypeName)
9173ca95b02SDimitry Andric       .Case("sampler_t", RuntimeMD::KernelArg::Sampler)
9183ca95b02SDimitry Andric       .Case("queue_t",   RuntimeMD::KernelArg::Queue)
9193ca95b02SDimitry Andric       .Cases("image1d_t", "image1d_array_t", "image1d_buffer_t",
9203ca95b02SDimitry Andric              "image2d_t" , "image2d_array_t",  RuntimeMD::KernelArg::Image)
9213ca95b02SDimitry Andric       .Cases("image2d_depth_t", "image2d_array_depth_t",
9223ca95b02SDimitry Andric              "image2d_msaa_t", "image2d_array_msaa_t",
9233ca95b02SDimitry Andric              "image2d_msaa_depth_t",  RuntimeMD::KernelArg::Image)
9243ca95b02SDimitry Andric       .Cases("image2d_array_msaa_depth_t", "image3d_t",
9253ca95b02SDimitry Andric              RuntimeMD::KernelArg::Image)
9263ca95b02SDimitry Andric       .Default(isa<PointerType>(T) ? RuntimeMD::KernelArg::Pointer :
9273ca95b02SDimitry Andric                RuntimeMD::KernelArg::Value);
9283ca95b02SDimitry Andric     emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyArgTypeKind, TypeKind, 1);
9293ca95b02SDimitry Andric 
9303ca95b02SDimitry Andric     // Emit KeyArgValueType.
9313ca95b02SDimitry Andric     emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyArgValueType,
9323ca95b02SDimitry Andric                           getRuntimeMDValueType(T, BaseTypeName), 2);
9333ca95b02SDimitry Andric 
9343ca95b02SDimitry Andric     // Emit KeyArgAccQual.
9353ca95b02SDimitry Andric     auto AccQual = cast<MDString>(F.getMetadata(
9363ca95b02SDimitry Andric       "kernel_arg_access_qual")->getOperand(I))->getString();
9373ca95b02SDimitry Andric     auto AQ = StringSwitch<RuntimeMD::KernelArg::AccessQualifer>(AccQual)
9383ca95b02SDimitry Andric       .Case("read_only",  RuntimeMD::KernelArg::ReadOnly)
9393ca95b02SDimitry Andric       .Case("write_only", RuntimeMD::KernelArg::WriteOnly)
9403ca95b02SDimitry Andric       .Case("read_write", RuntimeMD::KernelArg::ReadWrite)
9413ca95b02SDimitry Andric       .Default(RuntimeMD::KernelArg::None);
9423ca95b02SDimitry Andric     emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyArgAccQual,
9433ca95b02SDimitry Andric                           AQ, 1);
9443ca95b02SDimitry Andric 
9453ca95b02SDimitry Andric     // Emit KeyArgAddrQual.
9463ca95b02SDimitry Andric     if (isa<PointerType>(T))
9473ca95b02SDimitry Andric       emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyArgAddrQual,
9483ca95b02SDimitry Andric                             T->getPointerAddressSpace(), 1);
9493ca95b02SDimitry Andric 
9503ca95b02SDimitry Andric     // Emit KeyArgEnd
9513ca95b02SDimitry Andric     OutStreamer->EmitIntValue(RuntimeMD::KeyArgEnd, 1);
9523ca95b02SDimitry Andric   }
9533ca95b02SDimitry Andric 
9543ca95b02SDimitry Andric   // Emit KeyReqdWorkGroupSize, KeyWorkGroupSizeHint, and KeyVecTypeHint.
9553ca95b02SDimitry Andric   if (auto RWGS = F.getMetadata("reqd_work_group_size"))
9563ca95b02SDimitry Andric     emitRuntimeMDThreeIntValues(OutStreamer, RuntimeMD::KeyReqdWorkGroupSize,
9573ca95b02SDimitry Andric                                 RWGS, 4);
9583ca95b02SDimitry Andric   if (auto WGSH = F.getMetadata("work_group_size_hint"))
9593ca95b02SDimitry Andric     emitRuntimeMDThreeIntValues(OutStreamer, RuntimeMD::KeyWorkGroupSizeHint,
9603ca95b02SDimitry Andric                                 WGSH, 4);
9613ca95b02SDimitry Andric   if (auto VTH = F.getMetadata("vec_type_hint")) {
9623ca95b02SDimitry Andric     auto TypeName = getOCLTypeName(cast<ValueAsMetadata>(
9633ca95b02SDimitry Andric       VTH->getOperand(0))->getType(), mdconst::extract<ConstantInt>(
9643ca95b02SDimitry Andric       VTH->getOperand(1))->getZExtValue());
9653ca95b02SDimitry Andric     emitRuntimeMDStringValue(OutStreamer, RuntimeMD::KeyVecTypeHint,
9663ca95b02SDimitry Andric                              TypeName);
9673ca95b02SDimitry Andric   }
9683ca95b02SDimitry Andric 
9693ca95b02SDimitry Andric   // Emit KeyKernelEnd
9703ca95b02SDimitry Andric   OutStreamer->EmitIntValue(RuntimeMD::KeyKernelEnd, 1);
9713ca95b02SDimitry Andric }
972