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 | |
| 19 | |
| 20 | #include "AMDGPUAsmPrinter.h" |
| 21 | #include "AMDGPU.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 | |
| 38 | |
| 39 | static AsmPrinter *createAMDGPUAsmPrinterPass(TargetMachine &tm, |
| 40 | MCStreamer &Streamer) { |
| 41 | return new AMDGPUAsmPrinter(tm, Streamer); |
| 42 | } |
| 43 | |
| 44 | extern "C" void LLVMInitializeR600AsmPrinter() { |
| 45 | TargetRegistry::RegisterAsmPrinter(TheAMDGPUTarget, createAMDGPUAsmPrinterPass); |
| 46 | } |
| 47 | |
Tom Stellard | ed69925 | 2013-10-12 05:02:51 +0000 | [diff] [blame] | 48 | AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM, MCStreamer &Streamer) |
| 49 | : AsmPrinter(TM, Streamer) |
| 50 | { |
| 51 | DisasmEnabled = TM.getSubtarget<AMDGPUSubtarget>().dumpCode() && |
| 52 | ! Streamer.hasRawTextSupport(); |
| 53 | } |
| 54 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 55 | /// We need to override this function so we can avoid |
| 56 | /// the call to EmitFunctionHeader(), which the MCPureStreamer can't handle. |
| 57 | bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) { |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 58 | SetupMachineFunction(MF); |
Tom Stellard | 2e5e7a5 | 2013-02-05 17:09:11 +0000 | [diff] [blame] | 59 | if (OutStreamer.hasRawTextSupport()) { |
| 60 | OutStreamer.EmitRawText("@" + MF.getName() + ":"); |
| 61 | } |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 62 | |
Tom Stellard | ed69925 | 2013-10-12 05:02:51 +0000 | [diff] [blame] | 63 | MCContext &Context = getObjFileLowering().getContext(); |
| 64 | const MCSectionELF *ConfigSection = Context.getELFSection(".AMDGPU.config", |
Tom Stellard | 34e4068 | 2013-04-24 23:56:14 +0000 | [diff] [blame] | 65 | ELF::SHT_PROGBITS, 0, |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 66 | SectionKind::getReadOnly()); |
| 67 | OutStreamer.SwitchSection(ConfigSection); |
Tom Stellard | ed69925 | 2013-10-12 05:02:51 +0000 | [diff] [blame] | 68 | const AMDGPUSubtarget &STM = TM.getSubtarget<AMDGPUSubtarget>(); |
Tom Stellard | a6c6e1b | 2013-06-07 20:37:48 +0000 | [diff] [blame] | 69 | if (STM.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) { |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 70 | EmitProgramInfoSI(MF); |
| 71 | } else { |
| 72 | EmitProgramInfoR600(MF); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 73 | } |
Tom Stellard | ed69925 | 2013-10-12 05:02:51 +0000 | [diff] [blame] | 74 | |
| 75 | DisasmLines.clear(); |
| 76 | HexLines.clear(); |
| 77 | DisasmLineMaxLen = 0; |
| 78 | |
Tom Stellard | 3a7beafb3 | 2013-04-15 17:51:30 +0000 | [diff] [blame] | 79 | OutStreamer.SwitchSection(getObjFileLowering().getTextSection()); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 80 | EmitFunctionBody(); |
Tom Stellard | ed69925 | 2013-10-12 05:02:51 +0000 | [diff] [blame] | 81 | |
| 82 | if (STM.dumpCode()) { |
| 83 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
| 84 | MF.dump(); |
| 85 | #endif |
| 86 | |
| 87 | if (DisasmEnabled) { |
| 88 | OutStreamer.SwitchSection(Context.getELFSection(".AMDGPU.disasm", |
| 89 | ELF::SHT_NOTE, 0, |
| 90 | SectionKind::getReadOnly())); |
| 91 | |
| 92 | for (size_t i = 0; i < DisasmLines.size(); ++i) { |
| 93 | std::string Comment(DisasmLineMaxLen - DisasmLines[i].size(), ' '); |
| 94 | Comment += " ; " + HexLines[i] + "\n"; |
| 95 | |
| 96 | OutStreamer.EmitBytes(StringRef(DisasmLines[i])); |
| 97 | OutStreamer.EmitBytes(StringRef(Comment)); |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 102 | return false; |
| 103 | } |
| 104 | |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 105 | void AMDGPUAsmPrinter::EmitProgramInfoR600(MachineFunction &MF) { |
| 106 | unsigned MaxGPR = 0; |
Vincent Lejeune | 4a0beb5 | 2013-04-30 00:13:13 +0000 | [diff] [blame] | 107 | bool killPixel = false; |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 108 | const R600RegisterInfo * RI = |
| 109 | static_cast<const R600RegisterInfo*>(TM.getRegisterInfo()); |
Vincent Lejeune | 117f075 | 2013-04-23 17:34:12 +0000 | [diff] [blame] | 110 | R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>(); |
Tom Stellard | 043de4c | 2013-05-06 17:50:51 +0000 | [diff] [blame] | 111 | const AMDGPUSubtarget &STM = TM.getSubtarget<AMDGPUSubtarget>(); |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 112 | |
| 113 | for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end(); |
| 114 | BB != BB_E; ++BB) { |
| 115 | MachineBasicBlock &MBB = *BB; |
| 116 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 117 | I != E; ++I) { |
| 118 | MachineInstr &MI = *I; |
Vincent Lejeune | 4a0beb5 | 2013-04-30 00:13:13 +0000 | [diff] [blame] | 119 | if (MI.getOpcode() == AMDGPU::KILLGT) |
| 120 | killPixel = true; |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 121 | unsigned numOperands = MI.getNumOperands(); |
| 122 | for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) { |
| 123 | MachineOperand & MO = MI.getOperand(op_idx); |
| 124 | if (!MO.isReg()) |
| 125 | continue; |
| 126 | unsigned HWReg = RI->getEncodingValue(MO.getReg()) & 0xff; |
| 127 | |
| 128 | // Register with value > 127 aren't GPR |
| 129 | if (HWReg > 127) |
| 130 | continue; |
| 131 | MaxGPR = std::max(MaxGPR, HWReg); |
| 132 | } |
| 133 | } |
| 134 | } |
Tom Stellard | 043de4c | 2013-05-06 17:50:51 +0000 | [diff] [blame] | 135 | |
| 136 | unsigned RsrcReg; |
Tom Stellard | a6c6e1b | 2013-06-07 20:37:48 +0000 | [diff] [blame] | 137 | if (STM.getGeneration() >= AMDGPUSubtarget::EVERGREEN) { |
Tom Stellard | 043de4c | 2013-05-06 17:50:51 +0000 | [diff] [blame] | 138 | // Evergreen / Northern Islands |
| 139 | switch (MFI->ShaderType) { |
| 140 | default: // Fall through |
| 141 | case ShaderType::COMPUTE: RsrcReg = R_0288D4_SQ_PGM_RESOURCES_LS; break; |
| 142 | case ShaderType::GEOMETRY: RsrcReg = R_028878_SQ_PGM_RESOURCES_GS; break; |
| 143 | case ShaderType::PIXEL: RsrcReg = R_028844_SQ_PGM_RESOURCES_PS; break; |
| 144 | case ShaderType::VERTEX: RsrcReg = R_028860_SQ_PGM_RESOURCES_VS; break; |
| 145 | } |
| 146 | } else { |
| 147 | // R600 / R700 |
| 148 | switch (MFI->ShaderType) { |
| 149 | default: // Fall through |
| 150 | case ShaderType::GEOMETRY: // Fall through |
| 151 | case ShaderType::COMPUTE: // Fall through |
| 152 | case ShaderType::VERTEX: RsrcReg = R_028868_SQ_PGM_RESOURCES_VS; break; |
| 153 | case ShaderType::PIXEL: RsrcReg = R_028850_SQ_PGM_RESOURCES_PS; break; |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | OutStreamer.EmitIntValue(RsrcReg, 4); |
| 158 | OutStreamer.EmitIntValue(S_NUM_GPRS(MaxGPR + 1) | |
| 159 | S_STACK_SIZE(MFI->StackSize), 4); |
| 160 | OutStreamer.EmitIntValue(R_02880C_DB_SHADER_CONTROL, 4); |
| 161 | OutStreamer.EmitIntValue(S_02880C_KILL_ENABLE(killPixel), 4); |
Tom Stellard | c026e8b | 2013-06-28 15:47:08 +0000 | [diff] [blame] | 162 | |
| 163 | if (MFI->ShaderType == ShaderType::COMPUTE) { |
| 164 | OutStreamer.EmitIntValue(R_0288E8_SQ_LDS_ALLOC, 4); |
| 165 | OutStreamer.EmitIntValue(RoundUpToAlignment(MFI->LDSSize, 4) >> 2, 4); |
| 166 | } |
Vincent Lejeune | 98a7380 | 2013-04-17 15:17:25 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void AMDGPUAsmPrinter::EmitProgramInfoSI(MachineFunction &MF) { |
Tom Stellard | 6e1ee47 | 2013-10-29 16:37:28 +0000 | [diff] [blame] | 170 | const AMDGPUSubtarget &STM = TM.getSubtarget<AMDGPUSubtarget>(); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 171 | unsigned MaxSGPR = 0; |
| 172 | unsigned MaxVGPR = 0; |
| 173 | bool VCCUsed = false; |
| 174 | const SIRegisterInfo * RI = |
| 175 | static_cast<const SIRegisterInfo*>(TM.getRegisterInfo()); |
| 176 | |
| 177 | for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end(); |
| 178 | BB != BB_E; ++BB) { |
| 179 | MachineBasicBlock &MBB = *BB; |
| 180 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 181 | I != E; ++I) { |
| 182 | MachineInstr &MI = *I; |
| 183 | |
| 184 | unsigned numOperands = MI.getNumOperands(); |
| 185 | for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) { |
Matt Arsenault | 671a005 | 2013-11-14 10:08:50 +0000 | [diff] [blame^] | 186 | MachineOperand &MO = MI.getOperand(op_idx); |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 187 | unsigned maxUsed; |
| 188 | unsigned width = 0; |
| 189 | bool isSGPR = false; |
| 190 | unsigned reg; |
| 191 | unsigned hwReg; |
| 192 | if (!MO.isReg()) { |
| 193 | continue; |
| 194 | } |
| 195 | reg = MO.getReg(); |
| 196 | if (reg == AMDGPU::VCC) { |
| 197 | VCCUsed = true; |
| 198 | continue; |
| 199 | } |
Matt Arsenault | 65864e3 | 2013-10-22 21:11:31 +0000 | [diff] [blame] | 200 | |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 201 | switch (reg) { |
| 202 | default: break; |
Matt Arsenault | 65864e3 | 2013-10-22 21:11:31 +0000 | [diff] [blame] | 203 | case AMDGPU::SCC: |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 204 | case AMDGPU::EXEC: |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 205 | case AMDGPU::M0: |
| 206 | continue; |
| 207 | } |
| 208 | |
| 209 | if (AMDGPU::SReg_32RegClass.contains(reg)) { |
| 210 | isSGPR = true; |
| 211 | width = 1; |
| 212 | } else if (AMDGPU::VReg_32RegClass.contains(reg)) { |
| 213 | isSGPR = false; |
| 214 | width = 1; |
| 215 | } else if (AMDGPU::SReg_64RegClass.contains(reg)) { |
| 216 | isSGPR = true; |
| 217 | width = 2; |
| 218 | } else if (AMDGPU::VReg_64RegClass.contains(reg)) { |
| 219 | isSGPR = false; |
| 220 | width = 2; |
Christian Konig | 8b1ed28 | 2013-04-10 08:39:16 +0000 | [diff] [blame] | 221 | } else if (AMDGPU::VReg_96RegClass.contains(reg)) { |
| 222 | isSGPR = false; |
| 223 | width = 3; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 224 | } else if (AMDGPU::SReg_128RegClass.contains(reg)) { |
| 225 | isSGPR = true; |
| 226 | width = 4; |
| 227 | } else if (AMDGPU::VReg_128RegClass.contains(reg)) { |
| 228 | isSGPR = false; |
| 229 | width = 4; |
| 230 | } else if (AMDGPU::SReg_256RegClass.contains(reg)) { |
| 231 | isSGPR = true; |
| 232 | width = 8; |
Tom Stellard | 538ceeb | 2013-02-07 17:02:09 +0000 | [diff] [blame] | 233 | } else if (AMDGPU::VReg_256RegClass.contains(reg)) { |
| 234 | isSGPR = false; |
| 235 | width = 8; |
Tom Stellard | a66cafa | 2013-10-23 00:44:12 +0000 | [diff] [blame] | 236 | } else if (AMDGPU::SReg_512RegClass.contains(reg)) { |
| 237 | isSGPR = true; |
| 238 | width = 16; |
Tom Stellard | 538ceeb | 2013-02-07 17:02:09 +0000 | [diff] [blame] | 239 | } else if (AMDGPU::VReg_512RegClass.contains(reg)) { |
| 240 | isSGPR = false; |
| 241 | width = 16; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 242 | } else { |
| 243 | assert(!"Unknown register class"); |
| 244 | } |
Tom Stellard | 1c822a8 | 2013-02-07 19:39:45 +0000 | [diff] [blame] | 245 | hwReg = RI->getEncodingValue(reg) & 0xff; |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 246 | maxUsed = hwReg + width - 1; |
| 247 | if (isSGPR) { |
| 248 | MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR; |
| 249 | } else { |
| 250 | MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR; |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | } |
| 255 | if (VCCUsed) { |
| 256 | MaxSGPR += 2; |
| 257 | } |
| 258 | SIMachineFunctionInfo * MFI = MF.getInfo<SIMachineFunctionInfo>(); |
Tom Stellard | cb97e3a | 2013-04-15 17:51:35 +0000 | [diff] [blame] | 259 | unsigned RsrcReg; |
| 260 | switch (MFI->ShaderType) { |
| 261 | default: // Fall through |
| 262 | case ShaderType::COMPUTE: RsrcReg = R_00B848_COMPUTE_PGM_RSRC1; break; |
| 263 | case ShaderType::GEOMETRY: RsrcReg = R_00B228_SPI_SHADER_PGM_RSRC1_GS; break; |
| 264 | case ShaderType::PIXEL: RsrcReg = R_00B028_SPI_SHADER_PGM_RSRC1_PS; break; |
| 265 | case ShaderType::VERTEX: RsrcReg = R_00B128_SPI_SHADER_PGM_RSRC1_VS; break; |
| 266 | } |
| 267 | |
| 268 | OutStreamer.EmitIntValue(RsrcReg, 4); |
| 269 | OutStreamer.EmitIntValue(S_00B028_VGPRS(MaxVGPR / 4) | S_00B028_SGPRS(MaxSGPR / 8), 4); |
Michel Danzer | 49812b5 | 2013-07-10 16:37:07 +0000 | [diff] [blame] | 270 | |
Tom Stellard | 6e1ee47 | 2013-10-29 16:37:28 +0000 | [diff] [blame] | 271 | unsigned LDSAlignShift; |
| 272 | if (STM.getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) { |
| 273 | // LDS is allocated in 64 dword blocks |
| 274 | LDSAlignShift = 8; |
| 275 | } else { |
| 276 | // LDS is allocated in 128 dword blocks |
| 277 | LDSAlignShift = 9; |
| 278 | } |
| 279 | unsigned LDSBlocks = |
| 280 | RoundUpToAlignment(MFI->LDSSize, 1 << LDSAlignShift) >> LDSAlignShift; |
| 281 | |
Michel Danzer | 49812b5 | 2013-07-10 16:37:07 +0000 | [diff] [blame] | 282 | if (MFI->ShaderType == ShaderType::COMPUTE) { |
| 283 | OutStreamer.EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4); |
Tom Stellard | 6e1ee47 | 2013-10-29 16:37:28 +0000 | [diff] [blame] | 284 | OutStreamer.EmitIntValue(S_00B84C_LDS_SIZE(LDSBlocks), 4); |
Michel Danzer | 49812b5 | 2013-07-10 16:37:07 +0000 | [diff] [blame] | 285 | } |
Tom Stellard | cb97e3a | 2013-04-15 17:51:35 +0000 | [diff] [blame] | 286 | if (MFI->ShaderType == ShaderType::PIXEL) { |
Michel Danzer | 49812b5 | 2013-07-10 16:37:07 +0000 | [diff] [blame] | 287 | OutStreamer.EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4); |
Tom Stellard | 6e1ee47 | 2013-10-29 16:37:28 +0000 | [diff] [blame] | 288 | OutStreamer.EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(LDSBlocks), 4); |
Tom Stellard | cb97e3a | 2013-04-15 17:51:35 +0000 | [diff] [blame] | 289 | OutStreamer.EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4); |
| 290 | OutStreamer.EmitIntValue(MFI->PSInputAddr, 4); |
| 291 | } |
Tom Stellard | 75aadc2 | 2012-12-11 21:25:42 +0000 | [diff] [blame] | 292 | } |