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