Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 1 | //===-- AMDGPUAsmPrinter.cpp - AMDGPU Assebly printer --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | /// \file |
| 11 | /// |
| 12 | /// The AMDGPUAsmPrinter is used to print both assembly string and also binary |
| 13 | /// code. When passed an MCAsmStreamer it prints assembly and when passed |
| 14 | /// an MCObjectStreamer it outputs binary code. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | // |
| 18 | |
| 19 | #include "AMDGPUAsmPrinter.h" |
Tom Stellard | 347ac79 | 2015-06-26 21:15:07 +0000 | [diff] [blame] | 20 | #include "MCTargetDesc/AMDGPUTargetStreamer.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 21 | #include "InstPrinter/AMDGPUInstPrinter.h" |
Tom Stellard | 347ac79 | 2015-06-26 21:15:07 +0000 | [diff] [blame] | 22 | #include "Utils/AMDGPUBaseInfo.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 23 | #include "AMDGPU.h" |
| 24 | #include "AMDKernelCodeT.h" |
| 25 | #include "AMDGPUSubtarget.h" |
| 26 | #include "R600Defines.h" |
| 27 | #include "R600MachineFunctionInfo.h" |
| 28 | #include "R600RegisterInfo.h" |
| 29 | #include "SIDefines.h" |
| 30 | #include "SIMachineFunctionInfo.h" |
Matt Arsenault | a9720c6 | 2016-06-20 17:51:32 +0000 | [diff] [blame] | 31 | #include "SIInstrInfo.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 32 | #include "SIRegisterInfo.h" |
| 33 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Matt Arsenault | ff98241 | 2016-06-20 18:13:04 +0000 | [diff] [blame] | 34 | #include "llvm/IR/DiagnosticInfo.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 35 | #include "llvm/MC/MCContext.h" |
| 36 | #include "llvm/MC/MCSectionELF.h" |
| 37 | #include "llvm/MC/MCStreamer.h" |
| 38 | #include "llvm/Support/ELF.h" |
| 39 | #include "llvm/Support/MathExtras.h" |
| 40 | #include "llvm/Support/TargetRegistry.h" |
| 41 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Yaxun Liu | a711cc7 | 2016-07-16 05:09:21 +0000 | [diff] [blame] | 42 | #include "AMDGPURuntimeMetadata.h" |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 43 | |
Yaxun Liu | a711cc7 | 2016-07-16 05:09:21 +0000 | [diff] [blame] | 44 | using namespace ::AMDGPU; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 45 | using namespace llvm; |
| 46 | |
| 47 | // TODO: This should get the default rounding mode from the kernel. We just set |
| 48 | // the default here, but this could change if the OpenCL rounding mode pragmas |
| 49 | // are used. |
| 50 | // |
| 51 | // The denormal mode here should match what is reported by the OpenCL runtime |
| 52 | // for the CL_FP_DENORM bit from CL_DEVICE_{HALF|SINGLE|DOUBLE}_FP_CONFIG, but |
| 53 | // can also be override to flush with the -cl-denorms-are-zero compiler flag. |
| 54 | // |
| 55 | // AMD OpenCL only sets flush none and reports CL_FP_DENORM for double |
| 56 | // precision, and leaves single precision to flush all and does not report |
| 57 | // CL_FP_DENORM for CL_DEVICE_SINGLE_FP_CONFIG. Mesa's OpenCL currently reports |
| 58 | // CL_FP_DENORM for both. |
| 59 | // |
| 60 | // FIXME: It seems some instructions do not support single precision denormals |
| 61 | // regardless of the mode (exp_*_f32, rcp_*_f32, rsq_*_f32, rsq_*f32, sqrt_f32, |
| 62 | // and sin_f32, cos_f32 on most parts). |
| 63 | |
| 64 | // We want to use these instructions, and using fp32 denormals also causes |
| 65 | // instructions to run at the double precision rate for the device so it's |
| 66 | // probably best to just report no single precision denormals. |
| 67 | static uint32_t getFPMode(const MachineFunction &F) { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 68 | const SISubtarget& ST = F.getSubtarget<SISubtarget>(); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 69 | // TODO: Is there any real use for the flush in only / flush out only modes? |
| 70 | |
| 71 | uint32_t FP32Denormals = |
| 72 | ST.hasFP32Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT; |
| 73 | |
| 74 | uint32_t FP64Denormals = |
| 75 | ST.hasFP64Denormals() ? FP_DENORM_FLUSH_NONE : FP_DENORM_FLUSH_IN_FLUSH_OUT; |
| 76 | |
| 77 | return FP_ROUND_MODE_SP(FP_ROUND_ROUND_TO_NEAREST) | |
| 78 | FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_NEAREST) | |
| 79 | FP_DENORM_MODE_SP(FP32Denormals) | |
| 80 | FP_DENORM_MODE_DP(FP64Denormals); |
| 81 | } |
| 82 | |
| 83 | static AsmPrinter * |
| 84 | createAMDGPUAsmPrinterPass(TargetMachine &tm, |
| 85 | std::unique_ptr<MCStreamer> &&Streamer) { |
| 86 | return new AMDGPUAsmPrinter(tm, std::move(Streamer)); |
| 87 | } |
| 88 | |
| 89 | extern "C" void LLVMInitializeAMDGPUAsmPrinter() { |
| 90 | TargetRegistry::RegisterAsmPrinter(TheAMDGPUTarget, createAMDGPUAsmPrinterPass); |
| 91 | TargetRegistry::RegisterAsmPrinter(TheGCNTarget, createAMDGPUAsmPrinterPass); |
| 92 | } |
| 93 | |
| 94 | AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM, |
| 95 | std::unique_ptr<MCStreamer> Streamer) |
| 96 | : AsmPrinter(TM, std::move(Streamer)) {} |
| 97 | |
Tom Stellard | f421837 | 2016-01-12 17:18:17 +0000 | [diff] [blame] | 98 | void AMDGPUAsmPrinter::EmitStartOfAsmFile(Module &M) { |
| 99 | if (TM.getTargetTriple().getOS() != Triple::AMDHSA) |
| 100 | return; |
| 101 | |
| 102 | // Need to construct an MCSubtargetInfo here in case we have no functions |
| 103 | // in the module. |
| 104 | std::unique_ptr<MCSubtargetInfo> STI(TM.getTarget().createMCSubtargetInfo( |
| 105 | TM.getTargetTriple().str(), TM.getTargetCPU(), |
| 106 | TM.getTargetFeatureString())); |
| 107 | |
| 108 | AMDGPUTargetStreamer *TS = |
| 109 | static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer()); |
| 110 | |
Tom Stellard | 418beb7 | 2016-07-13 14:23:33 +0000 | [diff] [blame] | 111 | TS->EmitDirectiveHSACodeObjectVersion(2, 1); |
Tom Stellard | fcfaea4 | 2016-05-05 17:03:33 +0000 | [diff] [blame] | 112 | |
Tom Stellard | f421837 | 2016-01-12 17:18:17 +0000 | [diff] [blame] | 113 | AMDGPU::IsaVersion ISA = AMDGPU::getIsaVersion(STI->getFeatureBits()); |
| 114 | TS->EmitDirectiveHSACodeObjectISA(ISA.Major, ISA.Minor, ISA.Stepping, |
| 115 | "AMD", "AMDGPU"); |
Yaxun Liu | a711cc7 | 2016-07-16 05:09:21 +0000 | [diff] [blame] | 116 | emitStartOfRuntimeMetadata(M); |
Tom Stellard | f421837 | 2016-01-12 17:18:17 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Tom Stellard | f151a45 | 2015-06-26 21:14:58 +0000 | [diff] [blame] | 119 | void AMDGPUAsmPrinter::EmitFunctionBodyStart() { |
| 120 | const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>(); |
| 121 | SIProgramInfo KernelInfo; |
| 122 | if (STM.isAmdHsaOS()) { |
| 123 | getSIProgramInfo(KernelInfo, *MF); |
| 124 | EmitAmdKernelCodeT(*MF, KernelInfo); |
| 125 | } |
| 126 | } |
| 127 | |
Tom Stellard | 1e1b05d | 2015-11-06 11:45:14 +0000 | [diff] [blame] | 128 | void AMDGPUAsmPrinter::EmitFunctionEntryLabel() { |
| 129 | const SIMachineFunctionInfo *MFI = MF->getInfo<SIMachineFunctionInfo>(); |
| 130 | const AMDGPUSubtarget &STM = MF->getSubtarget<AMDGPUSubtarget>(); |
| 131 | if (MFI->isKernel() && STM.isAmdHsaOS()) { |
| 132 | AMDGPUTargetStreamer *TS = |
| 133 | static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer()); |
| 134 | TS->EmitAMDGPUSymbolType(CurrentFnSym->getName(), |
| 135 | ELF::STT_AMDGPU_HSA_KERNEL); |
| 136 | } |
| 137 | |
| 138 | AsmPrinter::EmitFunctionEntryLabel(); |
| 139 | } |
| 140 | |
Tom Stellard | e3b5aea | 2015-12-02 17:00:42 +0000 | [diff] [blame] | 141 | void AMDGPUAsmPrinter::EmitGlobalVariable(const GlobalVariable *GV) { |
| 142 | |
Tom Stellard | 00f2f91 | 2015-12-02 19:47:57 +0000 | [diff] [blame] | 143 | // Group segment variables aren't emitted in HSA. |
| 144 | if (AMDGPU::isGroupSegment(GV)) |
| 145 | return; |
| 146 | |
Tom Stellard | fcfaea4 | 2016-05-05 17:03:33 +0000 | [diff] [blame] | 147 | AsmPrinter::EmitGlobalVariable(GV); |
Tom Stellard | e3b5aea | 2015-12-02 17:00:42 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 150 | bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
| 151 | |
| 152 | // The starting address of all shader programs must be 256 bytes aligned. |
| 153 | MF.setAlignment(8); |
| 154 | |
| 155 | SetupMachineFunction(MF); |
| 156 | |
| 157 | MCContext &Context = getObjFileLowering().getContext(); |
| 158 | MCSectionELF *ConfigSection = |
| 159 | Context.getELFSection(".AMDGPU.config", ELF::SHT_PROGBITS, 0); |
| 160 | OutStreamer->SwitchSection(ConfigSection); |
| 161 | |
| 162 | const AMDGPUSubtarget &STM = MF.getSubtarget<AMDGPUSubtarget>(); |
| 163 | SIProgramInfo KernelInfo; |
Tom Stellard | f151a45 | 2015-06-26 21:14:58 +0000 | [diff] [blame] | 164 | if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) { |
Matt Arsenault | 297ae31 | 2015-08-15 00:12:39 +0000 | [diff] [blame] | 165 | getSIProgramInfo(KernelInfo, MF); |
Tom Stellard | f151a45 | 2015-06-26 21:14:58 +0000 | [diff] [blame] | 166 | if (!STM.isAmdHsaOS()) { |
Tom Stellard | f151a45 | 2015-06-26 21:14:58 +0000 | [diff] [blame] | 167 | EmitProgramInfoSI(MF, KernelInfo); |
| 168 | } |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 169 | } else { |
| 170 | EmitProgramInfoR600(MF); |
| 171 | } |
| 172 | |
| 173 | DisasmLines.clear(); |
| 174 | HexLines.clear(); |
| 175 | DisasmLineMaxLen = 0; |
| 176 | |
| 177 | EmitFunctionBody(); |
| 178 | |
| 179 | if (isVerbose()) { |
| 180 | MCSectionELF *CommentSection = |
| 181 | Context.getELFSection(".AMDGPU.csdata", ELF::SHT_PROGBITS, 0); |
| 182 | OutStreamer->SwitchSection(CommentSection); |
| 183 | |
| 184 | if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) { |
| 185 | OutStreamer->emitRawComment(" Kernel info:", false); |
| 186 | OutStreamer->emitRawComment(" codeLenInByte = " + Twine(KernelInfo.CodeLen), |
| 187 | false); |
| 188 | OutStreamer->emitRawComment(" NumSgprs: " + Twine(KernelInfo.NumSGPR), |
| 189 | false); |
| 190 | OutStreamer->emitRawComment(" NumVgprs: " + Twine(KernelInfo.NumVGPR), |
| 191 | false); |
| 192 | OutStreamer->emitRawComment(" FloatMode: " + Twine(KernelInfo.FloatMode), |
| 193 | false); |
| 194 | OutStreamer->emitRawComment(" IeeeMode: " + Twine(KernelInfo.IEEEMode), |
| 195 | false); |
| 196 | OutStreamer->emitRawComment(" ScratchSize: " + Twine(KernelInfo.ScratchSize), |
| 197 | false); |
Matt Arsenault | fd8ab09 | 2016-04-14 22:11:51 +0000 | [diff] [blame] | 198 | OutStreamer->emitRawComment(" LDSByteSize: " + Twine(KernelInfo.LDSSize) + |
| 199 | " bytes/workgroup (compile time only)", false); |
Matt Arsenault | d41c0db | 2015-11-05 05:27:07 +0000 | [diff] [blame] | 200 | |
Konstantin Zhuravlyov | 1d99c4d | 2016-04-26 15:43:14 +0000 | [diff] [blame] | 201 | OutStreamer->emitRawComment(" ReservedVGPRFirst: " + Twine(KernelInfo.ReservedVGPRFirst), |
| 202 | false); |
| 203 | OutStreamer->emitRawComment(" ReservedVGPRCount: " + Twine(KernelInfo.ReservedVGPRCount), |
| 204 | false); |
| 205 | |
Konstantin Zhuravlyov | f2f3d14 | 2016-06-25 03:11:28 +0000 | [diff] [blame] | 206 | if (MF.getSubtarget<SISubtarget>().debuggerEmitPrologue()) { |
| 207 | OutStreamer->emitRawComment(" DebuggerWavefrontPrivateSegmentOffsetSGPR: s" + |
| 208 | Twine(KernelInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR), false); |
| 209 | OutStreamer->emitRawComment(" DebuggerPrivateSegmentBufferSGPR: s" + |
| 210 | Twine(KernelInfo.DebuggerPrivateSegmentBufferSGPR), false); |
| 211 | } |
| 212 | |
Matt Arsenault | d41c0db | 2015-11-05 05:27:07 +0000 | [diff] [blame] | 213 | OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:USER_SGPR: " + |
Matt Arsenault | 8246d4a | 2015-11-11 00:27:46 +0000 | [diff] [blame] | 214 | Twine(G_00B84C_USER_SGPR(KernelInfo.ComputePGMRSrc2)), |
Matt Arsenault | d41c0db | 2015-11-05 05:27:07 +0000 | [diff] [blame] | 215 | false); |
Matt Arsenault | 8246d4a | 2015-11-11 00:27:46 +0000 | [diff] [blame] | 216 | OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_X_EN: " + |
| 217 | Twine(G_00B84C_TGID_X_EN(KernelInfo.ComputePGMRSrc2)), |
| 218 | false); |
| 219 | OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_Y_EN: " + |
| 220 | Twine(G_00B84C_TGID_Y_EN(KernelInfo.ComputePGMRSrc2)), |
| 221 | false); |
| 222 | OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TGID_Z_EN: " + |
| 223 | Twine(G_00B84C_TGID_Z_EN(KernelInfo.ComputePGMRSrc2)), |
| 224 | false); |
| 225 | OutStreamer->emitRawComment(" COMPUTE_PGM_RSRC2:TIDIG_COMP_CNT: " + |
| 226 | Twine(G_00B84C_TIDIG_COMP_CNT(KernelInfo.ComputePGMRSrc2)), |
| 227 | false); |
| 228 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 229 | } else { |
| 230 | R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>(); |
| 231 | OutStreamer->emitRawComment( |
| 232 | Twine("SQ_PGM_RESOURCES:STACK_SIZE = " + Twine(MFI->StackSize))); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | if (STM.dumpCode()) { |
| 237 | |
| 238 | OutStreamer->SwitchSection( |
| 239 | Context.getELFSection(".AMDGPU.disasm", ELF::SHT_NOTE, 0)); |
| 240 | |
| 241 | for (size_t i = 0; i < DisasmLines.size(); ++i) { |
| 242 | std::string Comment(DisasmLineMaxLen - DisasmLines[i].size(), ' '); |
| 243 | Comment += " ; " + HexLines[i] + "\n"; |
| 244 | |
| 245 | OutStreamer->EmitBytes(StringRef(DisasmLines[i])); |
| 246 | OutStreamer->EmitBytes(StringRef(Comment)); |
| 247 | } |
| 248 | } |
| 249 | |
Yaxun Liu | a711cc7 | 2016-07-16 05:09:21 +0000 | [diff] [blame] | 250 | emitRuntimeMetadata(*MF.getFunction()); |
| 251 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 252 | return false; |
| 253 | } |
| 254 | |
| 255 | void AMDGPUAsmPrinter::EmitProgramInfoR600(const MachineFunction &MF) { |
| 256 | unsigned MaxGPR = 0; |
| 257 | bool killPixel = false; |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 258 | const R600Subtarget &STM = MF.getSubtarget<R600Subtarget>(); |
| 259 | const R600RegisterInfo *RI = STM.getRegisterInfo(); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 260 | const R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>(); |
| 261 | |
| 262 | for (const MachineBasicBlock &MBB : MF) { |
| 263 | for (const MachineInstr &MI : MBB) { |
| 264 | if (MI.getOpcode() == AMDGPU::KILLGT) |
| 265 | killPixel = true; |
| 266 | unsigned numOperands = MI.getNumOperands(); |
| 267 | for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) { |
| 268 | const MachineOperand &MO = MI.getOperand(op_idx); |
| 269 | if (!MO.isReg()) |
| 270 | continue; |
| 271 | unsigned HWReg = RI->getEncodingValue(MO.getReg()) & 0xff; |
| 272 | |
| 273 | // Register with value > 127 aren't GPR |
| 274 | if (HWReg > 127) |
| 275 | continue; |
| 276 | MaxGPR = std::max(MaxGPR, HWReg); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | unsigned RsrcReg; |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 282 | if (STM.getGeneration() >= R600Subtarget::EVERGREEN) { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 283 | // Evergreen / Northern Islands |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 284 | switch (MF.getFunction()->getCallingConv()) { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 285 | default: // Fall through |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 286 | case CallingConv::AMDGPU_CS: RsrcReg = R_0288D4_SQ_PGM_RESOURCES_LS; break; |
| 287 | case CallingConv::AMDGPU_GS: RsrcReg = R_028878_SQ_PGM_RESOURCES_GS; break; |
| 288 | case CallingConv::AMDGPU_PS: RsrcReg = R_028844_SQ_PGM_RESOURCES_PS; break; |
| 289 | case CallingConv::AMDGPU_VS: RsrcReg = R_028860_SQ_PGM_RESOURCES_VS; break; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 290 | } |
| 291 | } else { |
| 292 | // R600 / R700 |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 293 | switch (MF.getFunction()->getCallingConv()) { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 294 | default: // Fall through |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 295 | case CallingConv::AMDGPU_GS: // Fall through |
| 296 | case CallingConv::AMDGPU_CS: // Fall through |
| 297 | case CallingConv::AMDGPU_VS: RsrcReg = R_028868_SQ_PGM_RESOURCES_VS; break; |
| 298 | case CallingConv::AMDGPU_PS: RsrcReg = R_028850_SQ_PGM_RESOURCES_PS; break; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 299 | } |
| 300 | } |
| 301 | |
| 302 | OutStreamer->EmitIntValue(RsrcReg, 4); |
| 303 | OutStreamer->EmitIntValue(S_NUM_GPRS(MaxGPR + 1) | |
| 304 | S_STACK_SIZE(MFI->StackSize), 4); |
| 305 | OutStreamer->EmitIntValue(R_02880C_DB_SHADER_CONTROL, 4); |
| 306 | OutStreamer->EmitIntValue(S_02880C_KILL_ENABLE(killPixel), 4); |
| 307 | |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 308 | if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 309 | OutStreamer->EmitIntValue(R_0288E8_SQ_LDS_ALLOC, 4); |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 310 | OutStreamer->EmitIntValue(alignTo(MFI->LDSSize, 4) >> 2, 4); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
| 314 | void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo, |
| 315 | const MachineFunction &MF) const { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 316 | const SISubtarget &STM = MF.getSubtarget<SISubtarget>(); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 317 | const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
| 318 | uint64_t CodeSize = 0; |
| 319 | unsigned MaxSGPR = 0; |
| 320 | unsigned MaxVGPR = 0; |
| 321 | bool VCCUsed = false; |
| 322 | bool FlatUsed = false; |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 323 | const SIRegisterInfo *RI = STM.getRegisterInfo(); |
| 324 | const SIInstrInfo *TII = STM.getInstrInfo(); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 325 | |
| 326 | for (const MachineBasicBlock &MBB : MF) { |
| 327 | for (const MachineInstr &MI : MBB) { |
| 328 | // TODO: CodeSize should account for multiple functions. |
Matt Arsenault | c574686 | 2015-08-12 09:04:44 +0000 | [diff] [blame] | 329 | |
| 330 | // TODO: Should we count size of debug info? |
| 331 | if (MI.isDebugValue()) |
| 332 | continue; |
| 333 | |
Matt Arsenault | a9720c6 | 2016-06-20 17:51:32 +0000 | [diff] [blame] | 334 | CodeSize += TII->getInstSizeInBytes(MI); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 335 | |
| 336 | unsigned numOperands = MI.getNumOperands(); |
| 337 | for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) { |
| 338 | const MachineOperand &MO = MI.getOperand(op_idx); |
| 339 | unsigned width = 0; |
| 340 | bool isSGPR = false; |
| 341 | |
Matt Arsenault | d2c7589 | 2015-10-01 21:51:59 +0000 | [diff] [blame] | 342 | if (!MO.isReg()) |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 343 | continue; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 344 | |
Matt Arsenault | d2c7589 | 2015-10-01 21:51:59 +0000 | [diff] [blame] | 345 | unsigned reg = MO.getReg(); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 346 | switch (reg) { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 347 | case AMDGPU::EXEC: |
Nicolai Haehnle | 7483937 | 2016-04-19 21:58:17 +0000 | [diff] [blame] | 348 | case AMDGPU::EXEC_LO: |
| 349 | case AMDGPU::EXEC_HI: |
Matt Arsenault | d2c7589 | 2015-10-01 21:51:59 +0000 | [diff] [blame] | 350 | case AMDGPU::SCC: |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 351 | case AMDGPU::M0: |
| 352 | continue; |
Matt Arsenault | d2c7589 | 2015-10-01 21:51:59 +0000 | [diff] [blame] | 353 | |
| 354 | case AMDGPU::VCC: |
| 355 | case AMDGPU::VCC_LO: |
| 356 | case AMDGPU::VCC_HI: |
| 357 | VCCUsed = true; |
| 358 | continue; |
| 359 | |
| 360 | case AMDGPU::FLAT_SCR: |
| 361 | case AMDGPU::FLAT_SCR_LO: |
| 362 | case AMDGPU::FLAT_SCR_HI: |
| 363 | FlatUsed = true; |
| 364 | continue; |
| 365 | |
Artem Tamazov | eb4d5a9 | 2016-04-13 16:18:41 +0000 | [diff] [blame] | 366 | case AMDGPU::TBA: |
| 367 | case AMDGPU::TBA_LO: |
| 368 | case AMDGPU::TBA_HI: |
| 369 | case AMDGPU::TMA: |
| 370 | case AMDGPU::TMA_LO: |
| 371 | case AMDGPU::TMA_HI: |
| 372 | llvm_unreachable("Trap Handler registers should not be used"); |
| 373 | continue; |
| 374 | |
Matt Arsenault | d2c7589 | 2015-10-01 21:51:59 +0000 | [diff] [blame] | 375 | default: |
| 376 | break; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | if (AMDGPU::SReg_32RegClass.contains(reg)) { |
Artem Tamazov | eb4d5a9 | 2016-04-13 16:18:41 +0000 | [diff] [blame] | 380 | if (AMDGPU::TTMP_32RegClass.contains(reg)) { |
| 381 | llvm_unreachable("Trap Handler registers should not be used"); |
| 382 | } |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 383 | isSGPR = true; |
| 384 | width = 1; |
| 385 | } else if (AMDGPU::VGPR_32RegClass.contains(reg)) { |
| 386 | isSGPR = false; |
| 387 | width = 1; |
| 388 | } else if (AMDGPU::SReg_64RegClass.contains(reg)) { |
Artem Tamazov | eb4d5a9 | 2016-04-13 16:18:41 +0000 | [diff] [blame] | 389 | if (AMDGPU::TTMP_64RegClass.contains(reg)) { |
| 390 | llvm_unreachable("Trap Handler registers should not be used"); |
| 391 | } |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 392 | isSGPR = true; |
| 393 | width = 2; |
| 394 | } else if (AMDGPU::VReg_64RegClass.contains(reg)) { |
| 395 | isSGPR = false; |
| 396 | width = 2; |
| 397 | } else if (AMDGPU::VReg_96RegClass.contains(reg)) { |
| 398 | isSGPR = false; |
| 399 | width = 3; |
| 400 | } else if (AMDGPU::SReg_128RegClass.contains(reg)) { |
| 401 | isSGPR = true; |
| 402 | width = 4; |
| 403 | } else if (AMDGPU::VReg_128RegClass.contains(reg)) { |
| 404 | isSGPR = false; |
| 405 | width = 4; |
| 406 | } else if (AMDGPU::SReg_256RegClass.contains(reg)) { |
| 407 | isSGPR = true; |
| 408 | width = 8; |
| 409 | } else if (AMDGPU::VReg_256RegClass.contains(reg)) { |
| 410 | isSGPR = false; |
| 411 | width = 8; |
| 412 | } else if (AMDGPU::SReg_512RegClass.contains(reg)) { |
| 413 | isSGPR = true; |
| 414 | width = 16; |
| 415 | } else if (AMDGPU::VReg_512RegClass.contains(reg)) { |
| 416 | isSGPR = false; |
| 417 | width = 16; |
| 418 | } else { |
| 419 | llvm_unreachable("Unknown register class"); |
| 420 | } |
| 421 | unsigned hwReg = RI->getEncodingValue(reg) & 0xff; |
| 422 | unsigned maxUsed = hwReg + width - 1; |
| 423 | if (isSGPR) { |
| 424 | MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR; |
| 425 | } else { |
| 426 | MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR; |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | } |
| 431 | |
Nicolai Haehnle | 3c05d6d | 2016-01-07 17:10:20 +0000 | [diff] [blame] | 432 | unsigned ExtraSGPRs = 0; |
| 433 | |
| 434 | if (VCCUsed) |
| 435 | ExtraSGPRs = 2; |
| 436 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 437 | if (STM.getGeneration() < SISubtarget::VOLCANIC_ISLANDS) { |
Nicolai Haehnle | 3c05d6d | 2016-01-07 17:10:20 +0000 | [diff] [blame] | 438 | if (FlatUsed) |
| 439 | ExtraSGPRs = 4; |
| 440 | } else { |
| 441 | if (STM.isXNACKEnabled()) |
| 442 | ExtraSGPRs = 4; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 443 | |
Nicolai Haehnle | 5b50497 | 2016-01-04 23:35:53 +0000 | [diff] [blame] | 444 | if (FlatUsed) |
Nicolai Haehnle | 3c05d6d | 2016-01-07 17:10:20 +0000 | [diff] [blame] | 445 | ExtraSGPRs = 6; |
Tom Stellard | caaa3aa | 2015-12-17 17:05:09 +0000 | [diff] [blame] | 446 | } |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 447 | |
Nicolai Haehnle | 3c05d6d | 2016-01-07 17:10:20 +0000 | [diff] [blame] | 448 | MaxSGPR += ExtraSGPRs; |
| 449 | |
Konstantin Zhuravlyov | 29ddd2b | 2016-05-24 18:37:18 +0000 | [diff] [blame] | 450 | // Record first reserved register and reserved register count fields, and |
| 451 | // update max register counts if "amdgpu-debugger-reserve-regs" attribute was |
| 452 | // specified. |
| 453 | if (STM.debuggerReserveRegs()) { |
Konstantin Zhuravlyov | 1d99c4d | 2016-04-26 15:43:14 +0000 | [diff] [blame] | 454 | ProgInfo.ReservedVGPRFirst = MaxVGPR + 1; |
Konstantin Zhuravlyov | 29ddd2b | 2016-05-24 18:37:18 +0000 | [diff] [blame] | 455 | ProgInfo.ReservedVGPRCount = MFI->getDebuggerReservedVGPRCount(); |
| 456 | MaxVGPR += MFI->getDebuggerReservedVGPRCount(); |
Konstantin Zhuravlyov | 1d99c4d | 2016-04-26 15:43:14 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Konstantin Zhuravlyov | f2f3d14 | 2016-06-25 03:11:28 +0000 | [diff] [blame] | 459 | // Update DebuggerWavefrontPrivateSegmentOffsetSGPR and |
| 460 | // DebuggerPrivateSegmentBufferSGPR fields if "amdgpu-debugger-emit-prologue" |
| 461 | // attribute was specified. |
| 462 | if (STM.debuggerEmitPrologue()) { |
| 463 | ProgInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR = |
| 464 | RI->getHWRegIndex(MFI->getScratchWaveOffsetReg()); |
| 465 | ProgInfo.DebuggerPrivateSegmentBufferSGPR = |
| 466 | RI->getHWRegIndex(MFI->getScratchRSrcReg()); |
| 467 | } |
| 468 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 469 | // We found the maximum register index. They start at 0, so add one to get the |
| 470 | // number of registers. |
| 471 | ProgInfo.NumVGPR = MaxVGPR + 1; |
| 472 | ProgInfo.NumSGPR = MaxSGPR + 1; |
| 473 | |
| 474 | if (STM.hasSGPRInitBug()) { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 475 | if (ProgInfo.NumSGPR > SISubtarget::FIXED_SGPR_COUNT_FOR_INIT_BUG) { |
Matt Arsenault | 417c93e | 2015-06-17 20:55:25 +0000 | [diff] [blame] | 476 | LLVMContext &Ctx = MF.getFunction()->getContext(); |
Matt Arsenault | ff98241 | 2016-06-20 18:13:04 +0000 | [diff] [blame] | 477 | DiagnosticInfoResourceLimit Diag(*MF.getFunction(), |
| 478 | "SGPRs with SGPR init bug", |
| 479 | ProgInfo.NumSGPR, DS_Error); |
| 480 | Ctx.diagnose(Diag); |
Matt Arsenault | 417c93e | 2015-06-17 20:55:25 +0000 | [diff] [blame] | 481 | } |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 482 | |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 483 | ProgInfo.NumSGPR = SISubtarget::FIXED_SGPR_COUNT_FOR_INIT_BUG; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 484 | } |
| 485 | |
Matt Arsenault | 41003af | 2015-11-30 21:16:07 +0000 | [diff] [blame] | 486 | if (MFI->NumUserSGPRs > STM.getMaxNumUserSGPRs()) { |
| 487 | LLVMContext &Ctx = MF.getFunction()->getContext(); |
Matt Arsenault | ff98241 | 2016-06-20 18:13:04 +0000 | [diff] [blame] | 488 | DiagnosticInfoResourceLimit Diag(*MF.getFunction(), "user SGPRs", |
| 489 | MFI->NumUserSGPRs, DS_Error); |
| 490 | Ctx.diagnose(Diag); |
Matt Arsenault | 41003af | 2015-11-30 21:16:07 +0000 | [diff] [blame] | 491 | } |
| 492 | |
Matt Arsenault | 1c4d0ef | 2016-04-28 19:37:35 +0000 | [diff] [blame] | 493 | if (MFI->LDSSize > static_cast<unsigned>(STM.getLocalMemorySize())) { |
| 494 | LLVMContext &Ctx = MF.getFunction()->getContext(); |
Matt Arsenault | ff98241 | 2016-06-20 18:13:04 +0000 | [diff] [blame] | 495 | DiagnosticInfoResourceLimit Diag(*MF.getFunction(), "local memory", |
| 496 | MFI->LDSSize, DS_Error); |
| 497 | Ctx.diagnose(Diag); |
Matt Arsenault | 1c4d0ef | 2016-04-28 19:37:35 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 500 | ProgInfo.VGPRBlocks = (ProgInfo.NumVGPR - 1) / 4; |
| 501 | ProgInfo.SGPRBlocks = (ProgInfo.NumSGPR - 1) / 8; |
| 502 | // Set the value to initialize FP_ROUND and FP_DENORM parts of the mode |
| 503 | // register. |
| 504 | ProgInfo.FloatMode = getFPMode(MF); |
| 505 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 506 | ProgInfo.IEEEMode = 0; |
| 507 | |
Matt Arsenault | 7293f98 | 2016-01-28 20:53:35 +0000 | [diff] [blame] | 508 | // Make clamp modifier on NaN input returns 0. |
| 509 | ProgInfo.DX10Clamp = 1; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 510 | |
| 511 | const MachineFrameInfo *FrameInfo = MF.getFrameInfo(); |
Matt Arsenault | f4dfc1a | 2016-03-01 04:58:20 +0000 | [diff] [blame] | 512 | ProgInfo.ScratchSize = FrameInfo->getStackSize(); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 513 | |
| 514 | ProgInfo.FlatUsed = FlatUsed; |
| 515 | ProgInfo.VCCUsed = VCCUsed; |
| 516 | ProgInfo.CodeLen = CodeSize; |
| 517 | |
| 518 | unsigned LDSAlignShift; |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 519 | if (STM.getGeneration() < SISubtarget::SEA_ISLANDS) { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 520 | // LDS is allocated in 64 dword blocks. |
| 521 | LDSAlignShift = 8; |
| 522 | } else { |
| 523 | // LDS is allocated in 128 dword blocks. |
| 524 | LDSAlignShift = 9; |
| 525 | } |
| 526 | |
| 527 | unsigned LDSSpillSize = MFI->LDSWaveSpillSize * |
| 528 | MFI->getMaximumWorkGroupSize(MF); |
| 529 | |
| 530 | ProgInfo.LDSSize = MFI->LDSSize + LDSSpillSize; |
| 531 | ProgInfo.LDSBlocks = |
Aaron Ballman | ef0fe1e | 2016-03-30 21:30:00 +0000 | [diff] [blame] | 532 | alignTo(ProgInfo.LDSSize, 1ULL << LDSAlignShift) >> LDSAlignShift; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 533 | |
| 534 | // Scratch is allocated in 256 dword blocks. |
| 535 | unsigned ScratchAlignShift = 10; |
| 536 | // We need to program the hardware with the amount of scratch memory that |
| 537 | // is used by the entire wave. ProgInfo.ScratchSize is the amount of |
| 538 | // scratch memory used per thread. |
| 539 | ProgInfo.ScratchBlocks = |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 540 | alignTo(ProgInfo.ScratchSize * STM.getWavefrontSize(), |
Aaron Ballman | ef0fe1e | 2016-03-30 21:30:00 +0000 | [diff] [blame] | 541 | 1ULL << ScratchAlignShift) >> |
Rui Ueyama | da00f2f | 2016-01-14 21:06:47 +0000 | [diff] [blame] | 542 | ScratchAlignShift; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 543 | |
| 544 | ProgInfo.ComputePGMRSrc1 = |
| 545 | S_00B848_VGPRS(ProgInfo.VGPRBlocks) | |
| 546 | S_00B848_SGPRS(ProgInfo.SGPRBlocks) | |
| 547 | S_00B848_PRIORITY(ProgInfo.Priority) | |
| 548 | S_00B848_FLOAT_MODE(ProgInfo.FloatMode) | |
| 549 | S_00B848_PRIV(ProgInfo.Priv) | |
| 550 | S_00B848_DX10_CLAMP(ProgInfo.DX10Clamp) | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 551 | S_00B848_DEBUG_MODE(ProgInfo.DebugMode) | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 552 | S_00B848_IEEE_MODE(ProgInfo.IEEEMode); |
| 553 | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 554 | // 0 = X, 1 = XY, 2 = XYZ |
| 555 | unsigned TIDIGCompCnt = 0; |
| 556 | if (MFI->hasWorkItemIDZ()) |
| 557 | TIDIGCompCnt = 2; |
| 558 | else if (MFI->hasWorkItemIDY()) |
| 559 | TIDIGCompCnt = 1; |
| 560 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 561 | ProgInfo.ComputePGMRSrc2 = |
| 562 | S_00B84C_SCRATCH_EN(ProgInfo.ScratchBlocks > 0) | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 563 | S_00B84C_USER_SGPR(MFI->getNumUserSGPRs()) | |
| 564 | S_00B84C_TGID_X_EN(MFI->hasWorkGroupIDX()) | |
| 565 | S_00B84C_TGID_Y_EN(MFI->hasWorkGroupIDY()) | |
| 566 | S_00B84C_TGID_Z_EN(MFI->hasWorkGroupIDZ()) | |
| 567 | S_00B84C_TG_SIZE_EN(MFI->hasWorkGroupInfo()) | |
| 568 | S_00B84C_TIDIG_COMP_CNT(TIDIGCompCnt) | |
| 569 | S_00B84C_EXCP_EN_MSB(0) | |
| 570 | S_00B84C_LDS_SIZE(ProgInfo.LDSBlocks) | |
| 571 | S_00B84C_EXCP_EN(0); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 572 | } |
| 573 | |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 574 | static unsigned getRsrcReg(CallingConv::ID CallConv) { |
| 575 | switch (CallConv) { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 576 | default: // Fall through |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 577 | case CallingConv::AMDGPU_CS: return R_00B848_COMPUTE_PGM_RSRC1; |
| 578 | case CallingConv::AMDGPU_GS: return R_00B228_SPI_SHADER_PGM_RSRC1_GS; |
| 579 | case CallingConv::AMDGPU_PS: return R_00B028_SPI_SHADER_PGM_RSRC1_PS; |
| 580 | case CallingConv::AMDGPU_VS: return R_00B128_SPI_SHADER_PGM_RSRC1_VS; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
| 584 | void AMDGPUAsmPrinter::EmitProgramInfoSI(const MachineFunction &MF, |
| 585 | const SIProgramInfo &KernelInfo) { |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 586 | const SISubtarget &STM = MF.getSubtarget<SISubtarget>(); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 587 | const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 588 | unsigned RsrcReg = getRsrcReg(MF.getFunction()->getCallingConv()); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 589 | |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 590 | if (AMDGPU::isCompute(MF.getFunction()->getCallingConv())) { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 591 | OutStreamer->EmitIntValue(R_00B848_COMPUTE_PGM_RSRC1, 4); |
| 592 | |
| 593 | OutStreamer->EmitIntValue(KernelInfo.ComputePGMRSrc1, 4); |
| 594 | |
| 595 | OutStreamer->EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4); |
| 596 | OutStreamer->EmitIntValue(KernelInfo.ComputePGMRSrc2, 4); |
| 597 | |
| 598 | OutStreamer->EmitIntValue(R_00B860_COMPUTE_TMPRING_SIZE, 4); |
| 599 | OutStreamer->EmitIntValue(S_00B860_WAVESIZE(KernelInfo.ScratchBlocks), 4); |
| 600 | |
| 601 | // TODO: Should probably note flat usage somewhere. SC emits a "FlatPtr32 = |
| 602 | // 0" comment but I don't see a corresponding field in the register spec. |
| 603 | } else { |
| 604 | OutStreamer->EmitIntValue(RsrcReg, 4); |
| 605 | OutStreamer->EmitIntValue(S_00B028_VGPRS(KernelInfo.VGPRBlocks) | |
| 606 | S_00B028_SGPRS(KernelInfo.SGPRBlocks), 4); |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 607 | if (STM.isVGPRSpillingEnabled(*MF.getFunction())) { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 608 | OutStreamer->EmitIntValue(R_0286E8_SPI_TMPRING_SIZE, 4); |
| 609 | OutStreamer->EmitIntValue(S_0286E8_WAVESIZE(KernelInfo.ScratchBlocks), 4); |
| 610 | } |
| 611 | } |
| 612 | |
Nicolai Haehnle | df3a20c | 2016-04-06 19:40:20 +0000 | [diff] [blame] | 613 | if (MF.getFunction()->getCallingConv() == CallingConv::AMDGPU_PS) { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 614 | OutStreamer->EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4); |
| 615 | OutStreamer->EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(KernelInfo.LDSBlocks), 4); |
| 616 | OutStreamer->EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4); |
Marek Olsak | fccabaf | 2016-01-13 11:45:36 +0000 | [diff] [blame] | 617 | OutStreamer->EmitIntValue(MFI->PSInputEna, 4); |
| 618 | OutStreamer->EmitIntValue(R_0286D0_SPI_PS_INPUT_ADDR, 4); |
| 619 | OutStreamer->EmitIntValue(MFI->getPSInputAddr(), 4); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 620 | } |
Marek Olsak | 0532c19 | 2016-07-13 17:35:15 +0000 | [diff] [blame] | 621 | |
| 622 | OutStreamer->EmitIntValue(R_SPILLED_SGPRS, 4); |
| 623 | OutStreamer->EmitIntValue(MFI->getNumSpilledSGPRs(), 4); |
| 624 | OutStreamer->EmitIntValue(R_SPILLED_VGPRS, 4); |
| 625 | OutStreamer->EmitIntValue(MFI->getNumSpilledVGPRs(), 4); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Matt Arsenault | 24ee078 | 2016-02-12 02:40:47 +0000 | [diff] [blame] | 628 | // This is supposed to be log2(Size) |
| 629 | static amd_element_byte_size_t getElementByteSizeValue(unsigned Size) { |
| 630 | switch (Size) { |
| 631 | case 4: |
| 632 | return AMD_ELEMENT_4_BYTES; |
| 633 | case 8: |
| 634 | return AMD_ELEMENT_8_BYTES; |
| 635 | case 16: |
| 636 | return AMD_ELEMENT_16_BYTES; |
| 637 | default: |
| 638 | llvm_unreachable("invalid private_element_size"); |
| 639 | } |
| 640 | } |
| 641 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 642 | void AMDGPUAsmPrinter::EmitAmdKernelCodeT(const MachineFunction &MF, |
Tom Stellard | ff7416b | 2015-06-26 21:58:31 +0000 | [diff] [blame] | 643 | const SIProgramInfo &KernelInfo) const { |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 644 | const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Matt Arsenault | 43e92fe | 2016-06-24 06:30:11 +0000 | [diff] [blame] | 645 | const SISubtarget &STM = MF.getSubtarget<SISubtarget>(); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 646 | amd_kernel_code_t header; |
| 647 | |
Tom Stellard | ff7416b | 2015-06-26 21:58:31 +0000 | [diff] [blame] | 648 | AMDGPU::initDefaultAMDKernelCodeT(header, STM.getFeatureBits()); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 649 | |
| 650 | header.compute_pgm_resource_registers = |
| 651 | KernelInfo.ComputePGMRSrc1 | |
| 652 | (KernelInfo.ComputePGMRSrc2 << 32); |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 653 | header.code_properties = AMD_CODE_PROPERTY_IS_PTR64; |
| 654 | |
Matt Arsenault | 24ee078 | 2016-02-12 02:40:47 +0000 | [diff] [blame] | 655 | |
| 656 | AMD_HSA_BITS_SET(header.code_properties, |
| 657 | AMD_CODE_PROPERTY_PRIVATE_ELEMENT_SIZE, |
| 658 | getElementByteSizeValue(STM.getMaxPrivateElementSize())); |
| 659 | |
Matt Arsenault | 26f8f3d | 2015-11-30 21:16:03 +0000 | [diff] [blame] | 660 | if (MFI->hasPrivateSegmentBuffer()) { |
| 661 | header.code_properties |= |
| 662 | AMD_CODE_PROPERTY_ENABLE_SGPR_PRIVATE_SEGMENT_BUFFER; |
| 663 | } |
| 664 | |
| 665 | if (MFI->hasDispatchPtr()) |
| 666 | header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR; |
| 667 | |
| 668 | if (MFI->hasQueuePtr()) |
| 669 | header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_QUEUE_PTR; |
| 670 | |
| 671 | if (MFI->hasKernargSegmentPtr()) |
| 672 | header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_KERNARG_SEGMENT_PTR; |
| 673 | |
| 674 | if (MFI->hasDispatchID()) |
| 675 | header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_ID; |
| 676 | |
| 677 | if (MFI->hasFlatScratchInit()) |
| 678 | header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_FLAT_SCRATCH_INIT; |
| 679 | |
| 680 | // TODO: Private segment size |
| 681 | |
| 682 | if (MFI->hasGridWorkgroupCountX()) { |
| 683 | header.code_properties |= |
| 684 | AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_X; |
| 685 | } |
| 686 | |
| 687 | if (MFI->hasGridWorkgroupCountY()) { |
| 688 | header.code_properties |= |
| 689 | AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Y; |
| 690 | } |
| 691 | |
| 692 | if (MFI->hasGridWorkgroupCountZ()) { |
| 693 | header.code_properties |= |
| 694 | AMD_CODE_PROPERTY_ENABLE_SGPR_GRID_WORKGROUP_COUNT_Z; |
| 695 | } |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 696 | |
Tom Stellard | 48f29f2 | 2015-11-26 00:43:29 +0000 | [diff] [blame] | 697 | if (MFI->hasDispatchPtr()) |
| 698 | header.code_properties |= AMD_CODE_PROPERTY_ENABLE_SGPR_DISPATCH_PTR; |
| 699 | |
Konstantin Zhuravlyov | f2f3d14 | 2016-06-25 03:11:28 +0000 | [diff] [blame] | 700 | if (STM.debuggerSupported()) |
| 701 | header.code_properties |= AMD_CODE_PROPERTY_IS_DEBUG_SUPPORTED; |
| 702 | |
Nicolai Haehnle | 5b50497 | 2016-01-04 23:35:53 +0000 | [diff] [blame] | 703 | if (STM.isXNACKEnabled()) |
| 704 | header.code_properties |= AMD_CODE_PROPERTY_IS_XNACK_SUPPORTED; |
| 705 | |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 706 | header.kernarg_segment_byte_size = MFI->ABIArgOffset; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 707 | header.wavefront_sgpr_count = KernelInfo.NumSGPR; |
| 708 | header.workitem_vgpr_count = KernelInfo.NumVGPR; |
Tom Stellard | a495307 | 2015-12-15 22:55:30 +0000 | [diff] [blame] | 709 | header.workitem_private_segment_byte_size = KernelInfo.ScratchSize; |
Tom Stellard | 7750f4e | 2015-12-15 23:15:25 +0000 | [diff] [blame] | 710 | header.workgroup_group_segment_byte_size = KernelInfo.LDSSize; |
Konstantin Zhuravlyov | 1d99c4d | 2016-04-26 15:43:14 +0000 | [diff] [blame] | 711 | header.reserved_vgpr_first = KernelInfo.ReservedVGPRFirst; |
| 712 | header.reserved_vgpr_count = KernelInfo.ReservedVGPRCount; |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 713 | |
Konstantin Zhuravlyov | f2f3d14 | 2016-06-25 03:11:28 +0000 | [diff] [blame] | 714 | if (STM.debuggerEmitPrologue()) { |
| 715 | header.debug_wavefront_private_segment_offset_sgpr = |
| 716 | KernelInfo.DebuggerWavefrontPrivateSegmentOffsetSGPR; |
| 717 | header.debug_private_segment_buffer_sgpr = |
| 718 | KernelInfo.DebuggerPrivateSegmentBufferSGPR; |
| 719 | } |
| 720 | |
Tom Stellard | ff7416b | 2015-06-26 21:58:31 +0000 | [diff] [blame] | 721 | AMDGPUTargetStreamer *TS = |
| 722 | static_cast<AMDGPUTargetStreamer *>(OutStreamer->getTargetStreamer()); |
Tom Stellard | fcfaea4 | 2016-05-05 17:03:33 +0000 | [diff] [blame] | 723 | |
| 724 | OutStreamer->SwitchSection(getObjFileLowering().getTextSection()); |
Tom Stellard | ff7416b | 2015-06-26 21:58:31 +0000 | [diff] [blame] | 725 | TS->EmitAMDKernelCodeT(header); |
Tom Stellard | 45bb48e | 2015-06-13 03:28:10 +0000 | [diff] [blame] | 726 | } |
| 727 | |
| 728 | bool AMDGPUAsmPrinter::PrintAsmOperand(const MachineInstr *MI, unsigned OpNo, |
| 729 | unsigned AsmVariant, |
| 730 | const char *ExtraCode, raw_ostream &O) { |
| 731 | if (ExtraCode && ExtraCode[0]) { |
| 732 | if (ExtraCode[1] != 0) |
| 733 | return true; // Unknown modifier. |
| 734 | |
| 735 | switch (ExtraCode[0]) { |
| 736 | default: |
| 737 | // See if this is a generic print operand |
| 738 | return AsmPrinter::PrintAsmOperand(MI, OpNo, AsmVariant, ExtraCode, O); |
| 739 | case 'r': |
| 740 | break; |
| 741 | } |
| 742 | } |
| 743 | |
| 744 | AMDGPUInstPrinter::printRegOperand(MI->getOperand(OpNo).getReg(), O, |
| 745 | *TM.getSubtargetImpl(*MF->getFunction())->getRegisterInfo()); |
| 746 | return false; |
| 747 | } |
Yaxun Liu | a711cc7 | 2016-07-16 05:09:21 +0000 | [diff] [blame] | 748 | |
| 749 | // Emit a key and an integer value for runtime metadata. |
| 750 | static void emitRuntimeMDIntValue(std::unique_ptr<MCStreamer> &Streamer, |
| 751 | RuntimeMD::Key K, uint64_t V, |
| 752 | unsigned Size) { |
| 753 | Streamer->EmitIntValue(K, 1); |
| 754 | Streamer->EmitIntValue(V, Size); |
| 755 | } |
| 756 | |
| 757 | // Emit a key and a string value for runtime metadata. |
| 758 | static void emitRuntimeMDStringValue(std::unique_ptr<MCStreamer> &Streamer, |
| 759 | RuntimeMD::Key K, StringRef S) { |
| 760 | Streamer->EmitIntValue(K, 1); |
| 761 | Streamer->EmitIntValue(S.size(), 4); |
| 762 | Streamer->EmitBytes(S); |
| 763 | } |
| 764 | |
| 765 | // Emit a key and three integer values for runtime metadata. |
| 766 | // The three integer values are obtained from MDNode \p Node; |
| 767 | static void emitRuntimeMDThreeIntValues(std::unique_ptr<MCStreamer> &Streamer, |
| 768 | RuntimeMD::Key K, MDNode *Node, |
| 769 | unsigned Size) { |
| 770 | Streamer->EmitIntValue(K, 1); |
| 771 | Streamer->EmitIntValue(mdconst::extract<ConstantInt>( |
| 772 | Node->getOperand(0))->getZExtValue(), Size); |
| 773 | Streamer->EmitIntValue(mdconst::extract<ConstantInt>( |
| 774 | Node->getOperand(1))->getZExtValue(), Size); |
| 775 | Streamer->EmitIntValue(mdconst::extract<ConstantInt>( |
| 776 | Node->getOperand(2))->getZExtValue(), Size); |
| 777 | } |
| 778 | |
| 779 | void AMDGPUAsmPrinter::emitStartOfRuntimeMetadata(const Module &M) { |
| 780 | OutStreamer->SwitchSection(getObjFileLowering().getContext() |
| 781 | .getELFSection(RuntimeMD::SectionName, ELF::SHT_PROGBITS, 0)); |
| 782 | |
| 783 | emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyMDVersion, |
| 784 | RuntimeMD::MDVersion << 8 | RuntimeMD::MDRevision, 2); |
| 785 | if (auto MD = M.getNamedMetadata("opencl.ocl.version")) { |
Yaxun Liu | 4b1d9f7 | 2016-07-20 14:38:06 +0000 | [diff] [blame^] | 786 | if (MD->getNumOperands()) { |
| 787 | auto Node = MD->getOperand(0); |
| 788 | if (Node->getNumOperands() > 1) { |
| 789 | emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyLanguage, |
| 790 | RuntimeMD::OpenCL_C, 1); |
| 791 | uint16_t Major = mdconst::extract<ConstantInt>(Node->getOperand(0)) |
| 792 | ->getZExtValue(); |
| 793 | uint16_t Minor = mdconst::extract<ConstantInt>(Node->getOperand(1)) |
| 794 | ->getZExtValue(); |
| 795 | emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyLanguageVersion, |
| 796 | Major * 100 + Minor * 10, 2); |
| 797 | } |
| 798 | } |
Yaxun Liu | a711cc7 | 2016-07-16 05:09:21 +0000 | [diff] [blame] | 799 | } |
| 800 | } |
| 801 | |
| 802 | static std::string getOCLTypeName(Type *Ty, bool isSigned) { |
| 803 | if (VectorType* VecTy = dyn_cast<VectorType>(Ty)) { |
| 804 | Type* EleTy = VecTy->getElementType(); |
| 805 | unsigned Size = VecTy->getVectorNumElements(); |
| 806 | return (Twine(getOCLTypeName(EleTy, isSigned)) + Twine(Size)).str(); |
| 807 | } |
| 808 | switch (Ty->getTypeID()) { |
| 809 | case Type::HalfTyID: return "half"; |
| 810 | case Type::FloatTyID: return "float"; |
| 811 | case Type::DoubleTyID: return "double"; |
| 812 | case Type::IntegerTyID: { |
| 813 | if (!isSigned) |
| 814 | return (Twine('u') + Twine(getOCLTypeName(Ty, true))).str(); |
| 815 | auto IntTy = cast<IntegerType>(Ty); |
| 816 | auto BW = IntTy->getIntegerBitWidth(); |
| 817 | switch (BW) { |
| 818 | case 8: |
| 819 | return "char"; |
| 820 | case 16: |
| 821 | return "short"; |
| 822 | case 32: |
| 823 | return "int"; |
| 824 | case 64: |
| 825 | return "long"; |
| 826 | default: |
| 827 | return (Twine('i') + Twine(BW)).str(); |
| 828 | } |
| 829 | } |
| 830 | default: |
| 831 | llvm_unreachable("invalid type"); |
| 832 | } |
| 833 | } |
| 834 | |
| 835 | static RuntimeMD::KernelArg::ValueType getRuntimeMDValueType( |
| 836 | Type *Ty, StringRef TypeName) { |
| 837 | if (auto VT = dyn_cast<VectorType>(Ty)) |
| 838 | return getRuntimeMDValueType(VT->getElementType(), TypeName); |
| 839 | else if (auto PT = dyn_cast<PointerType>(Ty)) |
| 840 | return getRuntimeMDValueType(PT->getElementType(), TypeName); |
| 841 | else if (Ty->isHalfTy()) |
| 842 | return RuntimeMD::KernelArg::F16; |
| 843 | else if (Ty->isFloatTy()) |
| 844 | return RuntimeMD::KernelArg::F32; |
| 845 | else if (Ty->isDoubleTy()) |
| 846 | return RuntimeMD::KernelArg::F64; |
| 847 | else if (IntegerType* intTy = dyn_cast<IntegerType>(Ty)) { |
| 848 | bool Signed = !TypeName.startswith("u"); |
| 849 | switch (intTy->getIntegerBitWidth()) { |
| 850 | case 8: |
| 851 | return Signed ? RuntimeMD::KernelArg::I8 : RuntimeMD::KernelArg::U8; |
| 852 | case 16: |
| 853 | return Signed ? RuntimeMD::KernelArg::I16 : RuntimeMD::KernelArg::U16; |
| 854 | case 32: |
| 855 | return Signed ? RuntimeMD::KernelArg::I32 : RuntimeMD::KernelArg::U32; |
| 856 | case 64: |
| 857 | return Signed ? RuntimeMD::KernelArg::I64 : RuntimeMD::KernelArg::U64; |
| 858 | default: |
| 859 | // Runtime does not recognize other integer types. Report as |
| 860 | // struct type. |
| 861 | return RuntimeMD::KernelArg::Struct; |
| 862 | } |
| 863 | } else |
| 864 | return RuntimeMD::KernelArg::Struct; |
| 865 | } |
| 866 | |
| 867 | void AMDGPUAsmPrinter::emitRuntimeMetadata(const Function &F) { |
| 868 | if (!F.getMetadata("kernel_arg_type")) |
| 869 | return; |
| 870 | |
| 871 | MCContext &Context = getObjFileLowering().getContext(); |
| 872 | OutStreamer->SwitchSection( |
| 873 | Context.getELFSection(RuntimeMD::SectionName, ELF::SHT_PROGBITS, 0)); |
| 874 | OutStreamer->EmitIntValue(RuntimeMD::KeyKernelBegin, 1); |
| 875 | emitRuntimeMDStringValue(OutStreamer, RuntimeMD::KeyKernelName, F.getName()); |
| 876 | |
| 877 | for (auto &Arg:F.args()) { |
| 878 | // Emit KeyArgBegin. |
| 879 | unsigned I = Arg.getArgNo(); |
| 880 | OutStreamer->EmitIntValue(RuntimeMD::KeyArgBegin, 1); |
| 881 | |
| 882 | // Emit KeyArgSize and KeyArgAlign. |
| 883 | auto T = Arg.getType(); |
| 884 | auto DL = F.getParent()->getDataLayout(); |
| 885 | emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyArgSize, |
| 886 | DL.getTypeAllocSize(T), 4); |
| 887 | emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyArgAlign, |
| 888 | DL.getABITypeAlignment(T), 4); |
| 889 | |
| 890 | // Emit KeyArgTypeName. |
| 891 | auto TypeName = dyn_cast<MDString>(F.getMetadata( |
| 892 | "kernel_arg_type")->getOperand(I))->getString(); |
| 893 | emitRuntimeMDStringValue(OutStreamer, RuntimeMD::KeyArgTypeName, TypeName); |
| 894 | |
| 895 | // Emit KeyArgName. |
| 896 | if (auto ArgNameMD = F.getMetadata("kernel_arg_name")) { |
| 897 | auto ArgName = cast<MDString>(ArgNameMD->getOperand( |
| 898 | I))->getString(); |
| 899 | emitRuntimeMDStringValue(OutStreamer, RuntimeMD::KeyArgName, ArgName); |
| 900 | } |
| 901 | |
| 902 | // Emit KeyArgIsVolatile, KeyArgIsRestrict, KeyArgIsConst and KeyArgIsPipe. |
| 903 | auto TypeQual = cast<MDString>(F.getMetadata( |
| 904 | "kernel_arg_type_qual")->getOperand(I))->getString(); |
| 905 | SmallVector<StringRef, 1> SplitQ; |
| 906 | TypeQual.split(SplitQ, " ", -1, false/* drop empty entry*/); |
| 907 | for (auto &I:SplitQ) { |
| 908 | auto Key = StringSwitch<RuntimeMD::Key>(I) |
| 909 | .Case("volatile", RuntimeMD::KeyArgIsVolatile) |
| 910 | .Case("restrict", RuntimeMD::KeyArgIsRestrict) |
| 911 | .Case("const", RuntimeMD::KeyArgIsConst) |
| 912 | .Case("pipe", RuntimeMD::KeyArgIsPipe) |
| 913 | .Default(RuntimeMD::KeyNull); |
| 914 | OutStreamer->EmitIntValue(Key, 1); |
| 915 | } |
| 916 | |
| 917 | // Emit KeyArgTypeKind. |
| 918 | auto BaseTypeName = cast<MDString>( |
| 919 | F.getMetadata("kernel_arg_base_type")->getOperand(I))->getString(); |
| 920 | auto TypeKind = StringSwitch<RuntimeMD::KernelArg::TypeKind>(BaseTypeName) |
| 921 | .Case("sampler_t", RuntimeMD::KernelArg::Sampler) |
| 922 | .Case("queue_t", RuntimeMD::KernelArg::Queue) |
| 923 | .Cases("image1d_t", "image1d_array_t", "image1d_buffer_t", |
| 924 | "image2d_t" , "image2d_array_t", RuntimeMD::KernelArg::Image) |
| 925 | .Cases("image2d_depth_t", "image2d_array_depth_t", |
| 926 | "image2d_msaa_t", "image2d_array_msaa_t", |
| 927 | "image2d_msaa_depth_t", RuntimeMD::KernelArg::Image) |
| 928 | .Cases("image2d_array_msaa_depth_t", "image3d_t", |
| 929 | RuntimeMD::KernelArg::Image) |
| 930 | .Default(isa<PointerType>(T) ? RuntimeMD::KernelArg::Pointer : |
| 931 | RuntimeMD::KernelArg::Value); |
| 932 | emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyArgTypeKind, TypeKind, 1); |
| 933 | |
| 934 | // Emit KeyArgValueType. |
| 935 | emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyArgValueType, |
| 936 | getRuntimeMDValueType(T, BaseTypeName), 2); |
| 937 | |
| 938 | // Emit KeyArgAccQual. |
| 939 | auto AccQual = cast<MDString>(F.getMetadata( |
| 940 | "kernel_arg_access_qual")->getOperand(I))->getString(); |
| 941 | auto AQ = StringSwitch<RuntimeMD::KernelArg::AccessQualifer>(AccQual) |
| 942 | .Case("read_only", RuntimeMD::KernelArg::ReadOnly) |
| 943 | .Case("write_only", RuntimeMD::KernelArg::WriteOnly) |
| 944 | .Case("read_write", RuntimeMD::KernelArg::ReadWrite) |
| 945 | .Default(RuntimeMD::KernelArg::None); |
| 946 | emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyArgAccQual, |
| 947 | AQ, 1); |
| 948 | |
| 949 | // Emit KeyArgAddrQual. |
| 950 | if (isa<PointerType>(T)) |
| 951 | emitRuntimeMDIntValue(OutStreamer, RuntimeMD::KeyArgAddrQual, |
| 952 | T->getPointerAddressSpace(), 1); |
| 953 | |
| 954 | // Emit KeyArgEnd |
| 955 | OutStreamer->EmitIntValue(RuntimeMD::KeyArgEnd, 1); |
| 956 | } |
| 957 | |
| 958 | // Emit KeyReqdWorkGroupSize, KeyWorkGroupSizeHint, and KeyVecTypeHint. |
| 959 | if (auto RWGS = F.getMetadata("reqd_work_group_size")) |
| 960 | emitRuntimeMDThreeIntValues(OutStreamer, RuntimeMD::KeyReqdWorkGroupSize, |
| 961 | RWGS, 4); |
| 962 | if (auto WGSH = F.getMetadata("work_group_size_hint")) |
| 963 | emitRuntimeMDThreeIntValues(OutStreamer, RuntimeMD::KeyWorkGroupSizeHint, |
| 964 | WGSH, 4); |
| 965 | if (auto VTH = F.getMetadata("vec_type_hint")) { |
| 966 | auto TypeName = getOCLTypeName(cast<ValueAsMetadata>( |
| 967 | VTH->getOperand(0))->getType(), mdconst::extract<ConstantInt>( |
| 968 | VTH->getOperand(1))->getZExtValue()); |
| 969 | emitRuntimeMDStringValue(OutStreamer, RuntimeMD::KeyVecTypeHint, |
| 970 | TypeName); |
| 971 | } |
| 972 | |
| 973 | // Emit KeyKernelEnd |
| 974 | OutStreamer->EmitIntValue(RuntimeMD::KeyKernelEnd, 1); |
| 975 | } |