Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +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 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 19 | #include "AMDGPUAsmPrinter.h" |
| 20 | #include "AMDGPU.h" |
Tom Stellard | 2e59a45 | 2014-06-13 01:32:00 +0000 | [diff] [blame] | 21 | #include "AMDGPUSubtarget.h" |
Tom Stellard | 043de4c | 2013-05-06 17:50:51 +0000 | [diff] [blame] | 22 | #include "R600Defines.h" |
Vincent Lejeune | 117f075 | 2013-04-23 17:34:12 +0000 | [diff] [blame] | 23 | #include "R600MachineFunctionInfo.h" |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 24 | #include "R600RegisterInfo.h" |
Benjamin Kramer | d78bb46 | 2013-05-23 17:10:37 +0000 | [diff] [blame] | 25 | #include "SIDefines.h" |
| 26 | #include "SIMachineFunctionInfo.h" |
| 27 | #include "SIRegisterInfo.h" |
Tom Stellard | 3a7beafb3 | 2013-04-15 17:51:30 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCContext.h" |
| 29 | #include "llvm/MC/MCSectionELF.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 30 | #include "llvm/MC/MCStreamer.h" |
Tom Stellard | 3a7beafb3 | 2013-04-15 17:51:30 +0000 | [diff] [blame] | 31 | #include "llvm/Support/ELF.h" |
Tom Stellard | c026e8b | 2013-06-28 15:47:08 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MathExtras.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 33 | #include "llvm/Support/TargetRegistry.h" |
Chandler Carruth | be81023 | 2013-01-02 10:22:59 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetLoweringObjectFile.h" |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 35 | |
| 36 | using namespace llvm; |
| 37 | |
Matt Arsenault | 0989d51 | 2014-06-26 17:22:30 +0000 | [diff] [blame] | 38 | // TODO: This should get the default rounding mode from the kernel. We just set |
| 39 | // the default here, but this could change if the OpenCL rounding mode pragmas |
| 40 | // are used. |
| 41 | // |
| 42 | // The denormal mode here should match what is reported by the OpenCL runtime |
| 43 | // for the CL_FP_DENORM bit from CL_DEVICE_{HALF|SINGLE|DOUBLE}_FP_CONFIG, but |
| 44 | // can also be override to flush with the -cl-denorms-are-zero compiler flag. |
| 45 | // |
| 46 | // AMD OpenCL only sets flush none and reports CL_FP_DENORM for double |
| 47 | // precision, and leaves single precision to flush all and does not report |
| 48 | // CL_FP_DENORM for CL_DEVICE_SINGLE_FP_CONFIG. Mesa's OpenCL currently reports |
| 49 | // CL_FP_DENORM for both. |
| 50 | static uint32_t getFPMode(MachineFunction &) { |
| 51 | return FP_ROUND_MODE_SP(FP_ROUND_ROUND_TO_NEAREST) | |
| 52 | FP_ROUND_MODE_DP(FP_ROUND_ROUND_TO_NEAREST) | |
| 53 | FP_DENORM_MODE_SP(FP_DENORM_FLUSH_NONE) | |
| 54 | FP_DENORM_MODE_DP(FP_DENORM_FLUSH_NONE); |
| 55 | } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 56 | |
| 57 | static AsmPrinter *createAMDGPUAsmPrinterPass(TargetMachine &tm, |
| 58 | MCStreamer &Streamer) { |
| 59 | return new AMDGPUAsmPrinter(tm, Streamer); |
| 60 | } |
| 61 | |
| 62 | extern "C" void LLVMInitializeR600AsmPrinter() { |
| 63 | TargetRegistry::RegisterAsmPrinter(TheAMDGPUTarget, createAMDGPUAsmPrinterPass); |
| 64 | } |
| 65 | |
Tom Stellard | ed69925 | 2013-10-12 05:02:51 +0000 | [diff] [blame] | 66 | AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) |
Matt Arsenault | 89cc49f | 2013-12-05 05:15:35 +0000 | [diff] [blame] | 67 | : AsmPrinter(TM, Streamer) { |
Rafael Espindola | 277f906 | 2014-01-31 22:14:06 +0000 | [diff] [blame] | 68 | DisasmEnabled = TM.getSubtarget<AMDGPUSubtarget>().dumpCode(); |
Tom Stellard | ed69925 | 2013-10-12 05:02:51 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 71 | bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 72 | SetupMachineFunction(MF); |
Matt Arsenault | 89cc49f | 2013-12-05 05:15:35 +0000 | [diff] [blame] | 73 | |
Rafael Espindola | 19656ba | 2014-01-31 21:54:49 +0000 | [diff] [blame] | 74 | OutStreamer.emitRawComment(Twine('@') + MF.getName() + Twine(':')); |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 75 | |
Tom Stellard | ed69925 | 2013-10-12 05:02:51 +0000 | [diff] [blame] | 76 | MCContext &Context = getObjFileLowering().getContext(); |
| 77 | const MCSectionELF *ConfigSection = Context.getELFSection(".AMDGPU.config", |
Tom Stellard | 34e4068 | 2013-04-24 23:56:14 +0000 | [diff] [blame] | 78 | ELF::SHT_PROGBITS, 0, |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 79 | SectionKind::getReadOnly()); |
| 80 | OutStreamer.SwitchSection(ConfigSection); |
Matt Arsenault | 89cc49f | 2013-12-05 05:15:35 +0000 | [diff] [blame] | 81 | |
Tom Stellard | ed69925 | 2013-10-12 05:02:51 +0000 | [diff] [blame] | 82 | const AMDGPUSubtarget &STM = TM.getSubtarget<AMDGPUSubtarget>(); |
Matt Arsenault | 89cc49f | 2013-12-05 05:15:35 +0000 | [diff] [blame] | 83 | SIProgramInfo KernelInfo; |
Tom Stellard | a6c6e1b | 2013-06-07 20:37:48 +0000 | [diff] [blame] | 84 | if (STM.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) { |
Matt Arsenault | e500e32 | 2014-04-15 22:40:47 +0000 | [diff] [blame] | 85 | getSIProgramInfo(KernelInfo, MF); |
Matt Arsenault | 89cc49f | 2013-12-05 05:15:35 +0000 | [diff] [blame] | 86 | EmitProgramInfoSI(MF, KernelInfo); |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 87 | } else { |
| 88 | EmitProgramInfoR600(MF); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 89 | } |
Tom Stellard | ed69925 | 2013-10-12 05:02:51 +0000 | [diff] [blame] | 90 | |
| 91 | DisasmLines.clear(); |
| 92 | HexLines.clear(); |
| 93 | DisasmLineMaxLen = 0; |
| 94 | |
Tom Stellard | 3a7beafb3 | 2013-04-15 17:51:30 +0000 | [diff] [blame] | 95 | OutStreamer.SwitchSection(getObjFileLowering().getTextSection()); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 96 | EmitFunctionBody(); |
Tom Stellard | ed69925 | 2013-10-12 05:02:51 +0000 | [diff] [blame] | 97 | |
Rafael Espindola | 887541f | 2014-01-31 22:08:19 +0000 | [diff] [blame] | 98 | if (isVerbose()) { |
Matt Arsenault | 89cc49f | 2013-12-05 05:15:35 +0000 | [diff] [blame] | 99 | const MCSectionELF *CommentSection |
| 100 | = Context.getELFSection(".AMDGPU.csdata", |
| 101 | ELF::SHT_PROGBITS, 0, |
| 102 | SectionKind::getReadOnly()); |
| 103 | OutStreamer.SwitchSection(CommentSection); |
| 104 | |
Matt Arsenault | e500e32 | 2014-04-15 22:40:47 +0000 | [diff] [blame] | 105 | if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) { |
Rafael Espindola | 98f5b54 | 2014-01-27 00:19:41 +0000 | [diff] [blame] | 106 | OutStreamer.emitRawComment(" Kernel info:", false); |
Matt Arsenault | e500e32 | 2014-04-15 22:40:47 +0000 | [diff] [blame] | 107 | OutStreamer.emitRawComment(" codeLenInByte = " + Twine(KernelInfo.CodeLen), |
| 108 | false); |
Rafael Espindola | 98f5b54 | 2014-01-27 00:19:41 +0000 | [diff] [blame] | 109 | OutStreamer.emitRawComment(" NumSgprs: " + Twine(KernelInfo.NumSGPR), |
Rafael Espindola | bcf890b | 2014-01-27 00:16:00 +0000 | [diff] [blame] | 110 | false); |
Rafael Espindola | 98f5b54 | 2014-01-27 00:19:41 +0000 | [diff] [blame] | 111 | OutStreamer.emitRawComment(" NumVgprs: " + Twine(KernelInfo.NumVGPR), |
Rafael Espindola | bcf890b | 2014-01-27 00:16:00 +0000 | [diff] [blame] | 112 | false); |
Matt Arsenault | 0989d51 | 2014-06-26 17:22:30 +0000 | [diff] [blame] | 113 | OutStreamer.emitRawComment(" FloatMode: " + Twine(KernelInfo.FloatMode), |
| 114 | false); |
| 115 | OutStreamer.emitRawComment(" IeeeMode: " + Twine(KernelInfo.IEEEMode), |
| 116 | false); |
Tom Stellard | 08b6af9 | 2014-01-22 21:55:35 +0000 | [diff] [blame] | 117 | } else { |
| 118 | R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>(); |
Rafael Espindola | 887541f | 2014-01-31 22:08:19 +0000 | [diff] [blame] | 119 | OutStreamer.emitRawComment( |
Tom Stellard | 08b6af9 | 2014-01-22 21:55:35 +0000 | [diff] [blame] | 120 | Twine("SQ_PGM_RESOURCES:STACK_SIZE = " + Twine(MFI->StackSize))); |
| 121 | } |
Matt Arsenault | 89cc49f | 2013-12-05 05:15:35 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Tom Stellard | ed69925 | 2013-10-12 05:02:51 +0000 | [diff] [blame] | 124 | if (STM.dumpCode()) { |
| 125 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 126 | MF.dump(); |
| 127 | #endif |
| 128 | |
| 129 | if (DisasmEnabled) { |
| 130 | OutStreamer.SwitchSection(Context.getELFSection(".AMDGPU.disasm", |
| 131 | ELF::SHT_NOTE, 0, |
| 132 | SectionKind::getReadOnly())); |
| 133 | |
| 134 | for (size_t i = 0; i < DisasmLines.size(); ++i) { |
| 135 | std::string Comment(DisasmLineMaxLen - DisasmLines[i].size(), ' '); |
| 136 | Comment += " ; " + HexLines[i] + "\n"; |
| 137 | |
| 138 | OutStreamer.EmitBytes(StringRef(DisasmLines[i])); |
| 139 | OutStreamer.EmitBytes(StringRef(Comment)); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 144 | return false; |
| 145 | } |
| 146 | |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 147 | void AMDGPUAsmPrinter::EmitProgramInfoR600(MachineFunction &MF) { |
| 148 | unsigned MaxGPR = 0; |
Vincent Lejeune | 4a0beb5 | 2013-04-30 00:13:13 +0000 | [diff] [blame] | 149 | bool killPixel = false; |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 150 | const R600RegisterInfo * RI = |
| 151 | static_cast<const R600RegisterInfo*>(TM.getRegisterInfo()); |
Vincent Lejeune | 117f075 | 2013-04-23 17:34:12 +0000 | [diff] [blame] | 152 | R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>(); |
Tom Stellard | 043de4c | 2013-05-06 17:50:51 +0000 | [diff] [blame] | 153 | const AMDGPUSubtarget &STM = TM.getSubtarget<AMDGPUSubtarget>(); |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 154 | |
| 155 | for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end(); |
| 156 | BB != BB_E; ++BB) { |
| 157 | MachineBasicBlock &MBB = *BB; |
| 158 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 159 | I != E; ++I) { |
| 160 | MachineInstr &MI = *I; |
Vincent Lejeune | 4a0beb5 | 2013-04-30 00:13:13 +0000 | [diff] [blame] | 161 | if (MI.getOpcode() == AMDGPU::KILLGT) |
| 162 | killPixel = true; |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 163 | unsigned numOperands = MI.getNumOperands(); |
| 164 | for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) { |
| 165 | MachineOperand & MO = MI.getOperand(op_idx); |
| 166 | if (!MO.isReg()) |
| 167 | continue; |
| 168 | unsigned HWReg = RI->getEncodingValue(MO.getReg()) & 0xff; |
| 169 | |
| 170 | // Register with value > 127 aren't GPR |
| 171 | if (HWReg > 127) |
| 172 | continue; |
| 173 | MaxGPR = std::max(MaxGPR, HWReg); |
| 174 | } |
| 175 | } |
| 176 | } |
Tom Stellard | 043de4c | 2013-05-06 17:50:51 +0000 | [diff] [blame] | 177 | |
| 178 | unsigned RsrcReg; |
Tom Stellard | a6c6e1b | 2013-06-07 20:37:48 +0000 | [diff] [blame] | 179 | if (STM.getGeneration() >= AMDGPUSubtarget::EVERGREEN) { |
Tom Stellard | 043de4c | 2013-05-06 17:50:51 +0000 | [diff] [blame] | 180 | // Evergreen / Northern Islands |
Matt Arsenault | 762af96 | 2014-07-13 03:06:39 +0000 | [diff] [blame^] | 181 | switch (MFI->getShaderType()) { |
Tom Stellard | 043de4c | 2013-05-06 17:50:51 +0000 | [diff] [blame] | 182 | default: // Fall through |
| 183 | case ShaderType::COMPUTE: RsrcReg = R_0288D4_SQ_PGM_RESOURCES_LS; break; |
| 184 | case ShaderType::GEOMETRY: RsrcReg = R_028878_SQ_PGM_RESOURCES_GS; break; |
| 185 | case ShaderType::PIXEL: RsrcReg = R_028844_SQ_PGM_RESOURCES_PS; break; |
| 186 | case ShaderType::VERTEX: RsrcReg = R_028860_SQ_PGM_RESOURCES_VS; break; |
| 187 | } |
| 188 | } else { |
| 189 | // R600 / R700 |
Matt Arsenault | 762af96 | 2014-07-13 03:06:39 +0000 | [diff] [blame^] | 190 | switch (MFI->getShaderType()) { |
Tom Stellard | 043de4c | 2013-05-06 17:50:51 +0000 | [diff] [blame] | 191 | default: // Fall through |
| 192 | case ShaderType::GEOMETRY: // Fall through |
| 193 | case ShaderType::COMPUTE: // Fall through |
| 194 | case ShaderType::VERTEX: RsrcReg = R_028868_SQ_PGM_RESOURCES_VS; break; |
| 195 | case ShaderType::PIXEL: RsrcReg = R_028850_SQ_PGM_RESOURCES_PS; break; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | OutStreamer.EmitIntValue(RsrcReg, 4); |
| 200 | OutStreamer.EmitIntValue(S_NUM_GPRS(MaxGPR + 1) | |
| 201 | S_STACK_SIZE(MFI->StackSize), 4); |
| 202 | OutStreamer.EmitIntValue(R_02880C_DB_SHADER_CONTROL, 4); |
| 203 | OutStreamer.EmitIntValue(S_02880C_KILL_ENABLE(killPixel), 4); |
Tom Stellard | c026e8b | 2013-06-28 15:47:08 +0000 | [diff] [blame] | 204 | |
Matt Arsenault | 762af96 | 2014-07-13 03:06:39 +0000 | [diff] [blame^] | 205 | if (MFI->getShaderType() == ShaderType::COMPUTE) { |
Tom Stellard | c026e8b | 2013-06-28 15:47:08 +0000 | [diff] [blame] | 206 | OutStreamer.EmitIntValue(R_0288E8_SQ_LDS_ALLOC, 4); |
| 207 | OutStreamer.EmitIntValue(RoundUpToAlignment(MFI->LDSSize, 4) >> 2, 4); |
| 208 | } |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Matt Arsenault | e500e32 | 2014-04-15 22:40:47 +0000 | [diff] [blame] | 211 | void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo, |
| 212 | MachineFunction &MF) const { |
| 213 | uint64_t CodeSize = 0; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 214 | unsigned MaxSGPR = 0; |
| 215 | unsigned MaxVGPR = 0; |
| 216 | bool VCCUsed = false; |
| 217 | const SIRegisterInfo * RI = |
| 218 | static_cast<const SIRegisterInfo*>(TM.getRegisterInfo()); |
| 219 | |
| 220 | for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end(); |
| 221 | BB != BB_E; ++BB) { |
| 222 | MachineBasicBlock &MBB = *BB; |
| 223 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 224 | I != E; ++I) { |
| 225 | MachineInstr &MI = *I; |
| 226 | |
Matt Arsenault | e500e32 | 2014-04-15 22:40:47 +0000 | [diff] [blame] | 227 | // TODO: CodeSize should account for multiple functions. |
| 228 | CodeSize += MI.getDesc().Size; |
| 229 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 230 | unsigned numOperands = MI.getNumOperands(); |
| 231 | for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) { |
Matt Arsenault | 671a005 | 2013-11-14 10:08:50 +0000 | [diff] [blame] | 232 | MachineOperand &MO = MI.getOperand(op_idx); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 233 | unsigned width = 0; |
| 234 | bool isSGPR = false; |
Matt Arsenault | a64ee17 | 2014-01-08 21:47:14 +0000 | [diff] [blame] | 235 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 236 | if (!MO.isReg()) { |
| 237 | continue; |
| 238 | } |
Matt Arsenault | a64ee17 | 2014-01-08 21:47:14 +0000 | [diff] [blame] | 239 | unsigned reg = MO.getReg(); |
Tom Stellard | fbe435d | 2014-03-17 17:03:51 +0000 | [diff] [blame] | 240 | if (reg == AMDGPU::VCC || reg == AMDGPU::VCC_LO || |
| 241 | reg == AMDGPU::VCC_HI) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 242 | VCCUsed = true; |
| 243 | continue; |
| 244 | } |
Matt Arsenault | 65864e3 | 2013-10-22 21:11:31 +0000 | [diff] [blame] | 245 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 246 | switch (reg) { |
| 247 | default: break; |
Matt Arsenault | 65864e3 | 2013-10-22 21:11:31 +0000 | [diff] [blame] | 248 | case AMDGPU::SCC: |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 249 | case AMDGPU::EXEC: |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 250 | case AMDGPU::M0: |
| 251 | continue; |
| 252 | } |
| 253 | |
| 254 | if (AMDGPU::SReg_32RegClass.contains(reg)) { |
| 255 | isSGPR = true; |
| 256 | width = 1; |
| 257 | } else if (AMDGPU::VReg_32RegClass.contains(reg)) { |
| 258 | isSGPR = false; |
| 259 | width = 1; |
| 260 | } else if (AMDGPU::SReg_64RegClass.contains(reg)) { |
| 261 | isSGPR = true; |
| 262 | width = 2; |
| 263 | } else if (AMDGPU::VReg_64RegClass.contains(reg)) { |
| 264 | isSGPR = false; |
| 265 | width = 2; |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 266 | } else if (AMDGPU::VReg_96RegClass.contains(reg)) { |
| 267 | isSGPR = false; |
| 268 | width = 3; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 269 | } else if (AMDGPU::SReg_128RegClass.contains(reg)) { |
| 270 | isSGPR = true; |
| 271 | width = 4; |
| 272 | } else if (AMDGPU::VReg_128RegClass.contains(reg)) { |
| 273 | isSGPR = false; |
| 274 | width = 4; |
| 275 | } else if (AMDGPU::SReg_256RegClass.contains(reg)) { |
| 276 | isSGPR = true; |
| 277 | width = 8; |
Tom Stellard | 538ceeb | 2013-02-07 17:02:09 +0000 | [diff] [blame] | 278 | } else if (AMDGPU::VReg_256RegClass.contains(reg)) { |
| 279 | isSGPR = false; |
| 280 | width = 8; |
Tom Stellard | a66cafa | 2013-10-23 00:44:12 +0000 | [diff] [blame] | 281 | } else if (AMDGPU::SReg_512RegClass.contains(reg)) { |
| 282 | isSGPR = true; |
| 283 | width = 16; |
Tom Stellard | 538ceeb | 2013-02-07 17:02:09 +0000 | [diff] [blame] | 284 | } else if (AMDGPU::VReg_512RegClass.contains(reg)) { |
| 285 | isSGPR = false; |
| 286 | width = 16; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 287 | } else { |
Matt Arsenault | eaa3a7e | 2013-12-10 21:37:42 +0000 | [diff] [blame] | 288 | llvm_unreachable("Unknown register class"); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 289 | } |
Matt Arsenault | a64ee17 | 2014-01-08 21:47:14 +0000 | [diff] [blame] | 290 | unsigned hwReg = RI->getEncodingValue(reg) & 0xff; |
| 291 | unsigned maxUsed = hwReg + width - 1; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 292 | if (isSGPR) { |
| 293 | MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR; |
| 294 | } else { |
| 295 | MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR; |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | } |
Matt Arsenault | 89cc49f | 2013-12-05 05:15:35 +0000 | [diff] [blame] | 300 | |
| 301 | if (VCCUsed) |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 302 | MaxSGPR += 2; |
Matt Arsenault | 89cc49f | 2013-12-05 05:15:35 +0000 | [diff] [blame] | 303 | |
Matt Arsenault | e500e32 | 2014-04-15 22:40:47 +0000 | [diff] [blame] | 304 | ProgInfo.NumVGPR = MaxVGPR; |
Matt Arsenault | 0989d51 | 2014-06-26 17:22:30 +0000 | [diff] [blame] | 305 | ProgInfo.NumSGPR = MaxSGPR; |
| 306 | |
| 307 | // Set the value to initialize FP_ROUND and FP_DENORM parts of the mode |
| 308 | // register. |
| 309 | ProgInfo.FloatMode = getFPMode(MF); |
| 310 | |
| 311 | // XXX: Not quite sure what this does, but sc seems to unset this. |
| 312 | ProgInfo.IEEEMode = 0; |
| 313 | |
| 314 | // Do not clamp NAN to 0. |
| 315 | ProgInfo.DX10Clamp = 0; |
| 316 | |
| 317 | ProgInfo.CodeLen = CodeSize; |
Matt Arsenault | 89cc49f | 2013-12-05 05:15:35 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | void AMDGPUAsmPrinter::EmitProgramInfoSI(MachineFunction &MF, |
| 321 | const SIProgramInfo &KernelInfo) { |
| 322 | const AMDGPUSubtarget &STM = TM.getSubtarget<AMDGPUSubtarget>(); |
Matt Arsenault | 89cc49f | 2013-12-05 05:15:35 +0000 | [diff] [blame] | 323 | SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Matt Arsenault | 0989d51 | 2014-06-26 17:22:30 +0000 | [diff] [blame] | 324 | |
Tom Stellard | cb97e3a | 2013-04-15 17:51:35 +0000 | [diff] [blame] | 325 | unsigned RsrcReg; |
Matt Arsenault | 762af96 | 2014-07-13 03:06:39 +0000 | [diff] [blame^] | 326 | switch (MFI->getShaderType()) { |
Tom Stellard | cb97e3a | 2013-04-15 17:51:35 +0000 | [diff] [blame] | 327 | default: // Fall through |
| 328 | case ShaderType::COMPUTE: RsrcReg = R_00B848_COMPUTE_PGM_RSRC1; break; |
| 329 | case ShaderType::GEOMETRY: RsrcReg = R_00B228_SPI_SHADER_PGM_RSRC1_GS; break; |
| 330 | case ShaderType::PIXEL: RsrcReg = R_00B028_SPI_SHADER_PGM_RSRC1_PS; break; |
| 331 | case ShaderType::VERTEX: RsrcReg = R_00B128_SPI_SHADER_PGM_RSRC1_VS; break; |
| 332 | } |
| 333 | |
Tom Stellard | 6e1ee47 | 2013-10-29 16:37:28 +0000 | [diff] [blame] | 334 | unsigned LDSAlignShift; |
| 335 | if (STM.getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) { |
Matt Arsenault | 0989d51 | 2014-06-26 17:22:30 +0000 | [diff] [blame] | 336 | // LDS is allocated in 64 dword blocks. |
Tom Stellard | 6e1ee47 | 2013-10-29 16:37:28 +0000 | [diff] [blame] | 337 | LDSAlignShift = 8; |
| 338 | } else { |
Matt Arsenault | 0989d51 | 2014-06-26 17:22:30 +0000 | [diff] [blame] | 339 | // LDS is allocated in 128 dword blocks. |
Tom Stellard | 6e1ee47 | 2013-10-29 16:37:28 +0000 | [diff] [blame] | 340 | LDSAlignShift = 9; |
| 341 | } |
Matt Arsenault | 0989d51 | 2014-06-26 17:22:30 +0000 | [diff] [blame] | 342 | |
Tom Stellard | 6e1ee47 | 2013-10-29 16:37:28 +0000 | [diff] [blame] | 343 | unsigned LDSBlocks = |
Matt Arsenault | 0989d51 | 2014-06-26 17:22:30 +0000 | [diff] [blame] | 344 | RoundUpToAlignment(MFI->LDSSize, 1 << LDSAlignShift) >> LDSAlignShift; |
Tom Stellard | 6e1ee47 | 2013-10-29 16:37:28 +0000 | [diff] [blame] | 345 | |
Matt Arsenault | 762af96 | 2014-07-13 03:06:39 +0000 | [diff] [blame^] | 346 | if (MFI->getShaderType() == ShaderType::COMPUTE) { |
Matt Arsenault | 0989d51 | 2014-06-26 17:22:30 +0000 | [diff] [blame] | 347 | OutStreamer.EmitIntValue(R_00B848_COMPUTE_PGM_RSRC1, 4); |
| 348 | |
| 349 | const uint32_t ComputePGMRSrc1 = |
| 350 | S_00B848_VGPRS(KernelInfo.NumVGPR / 4) | |
| 351 | S_00B848_SGPRS(KernelInfo.NumSGPR / 8) | |
| 352 | S_00B848_PRIORITY(KernelInfo.Priority) | |
| 353 | S_00B848_FLOAT_MODE(KernelInfo.FloatMode) | |
| 354 | S_00B848_PRIV(KernelInfo.Priv) | |
| 355 | S_00B848_DX10_CLAMP(KernelInfo.DX10Clamp) | |
| 356 | S_00B848_IEEE_MODE(KernelInfo.DebugMode) | |
| 357 | S_00B848_IEEE_MODE(KernelInfo.IEEEMode); |
| 358 | |
| 359 | OutStreamer.EmitIntValue(ComputePGMRSrc1, 4); |
| 360 | |
Michel Danzer | 49812b5 | 2013-07-10 16:37:07 +0000 | [diff] [blame] | 361 | OutStreamer.EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4); |
Tom Stellard | 6e1ee47 | 2013-10-29 16:37:28 +0000 | [diff] [blame] | 362 | OutStreamer.EmitIntValue(S_00B84C_LDS_SIZE(LDSBlocks), 4); |
Matt Arsenault | 0989d51 | 2014-06-26 17:22:30 +0000 | [diff] [blame] | 363 | } else { |
| 364 | OutStreamer.EmitIntValue(RsrcReg, 4); |
| 365 | OutStreamer.EmitIntValue(S_00B028_VGPRS(KernelInfo.NumVGPR / 4) | |
| 366 | S_00B028_SGPRS(KernelInfo.NumSGPR / 8), 4); |
Michel Danzer | 49812b5 | 2013-07-10 16:37:07 +0000 | [diff] [blame] | 367 | } |
Matt Arsenault | 0989d51 | 2014-06-26 17:22:30 +0000 | [diff] [blame] | 368 | |
Matt Arsenault | 762af96 | 2014-07-13 03:06:39 +0000 | [diff] [blame^] | 369 | if (MFI->getShaderType() == ShaderType::PIXEL) { |
Michel Danzer | 49812b5 | 2013-07-10 16:37:07 +0000 | [diff] [blame] | 370 | OutStreamer.EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4); |
Tom Stellard | 6e1ee47 | 2013-10-29 16:37:28 +0000 | [diff] [blame] | 371 | OutStreamer.EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(LDSBlocks), 4); |
Tom Stellard | cb97e3a | 2013-04-15 17:51:35 +0000 | [diff] [blame] | 372 | OutStreamer.EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4); |
| 373 | OutStreamer.EmitIntValue(MFI->PSInputAddr, 4); |
| 374 | } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 375 | } |