1 //===-- AMDGPUAsmPrinter.cpp - AMDGPU Assebly printer --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
16 //===----------------------------------------------------------------------===//
19 #include "AMDGPUAsmPrinter.h"
21 #include "AMDGPUSubtarget.h"
22 #include "R600Defines.h"
23 #include "R600MachineFunctionInfo.h"
24 #include "R600RegisterInfo.h"
25 #include "SIDefines.h"
26 #include "SIMachineFunctionInfo.h"
27 #include "SIRegisterInfo.h"
28 #include "llvm/MC/MCContext.h"
29 #include "llvm/MC/MCSectionELF.h"
30 #include "llvm/MC/MCStreamer.h"
31 #include "llvm/Support/ELF.h"
32 #include "llvm/Support/MathExtras.h"
33 #include "llvm/Support/TargetRegistry.h"
34 #include "llvm/Target/TargetLoweringObjectFile.h"
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
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.
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);
57 static AsmPrinter *createAMDGPUAsmPrinterPass(TargetMachine &tm,
58 MCStreamer &Streamer) {
59 return new AMDGPUAsmPrinter(tm, Streamer);
62 extern "C" void LLVMInitializeR600AsmPrinter() {
63 TargetRegistry::RegisterAsmPrinter(TheAMDGPUTarget, createAMDGPUAsmPrinterPass);
66 AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
67 : AsmPrinter(TM, Streamer) {
68 DisasmEnabled = TM.getSubtarget<AMDGPUSubtarget>().dumpCode();
71 bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
72 SetupMachineFunction(MF);
74 OutStreamer.emitRawComment(Twine('@') + MF.getName() + Twine(':'));
76 MCContext &Context = getObjFileLowering().getContext();
77 const MCSectionELF *ConfigSection = Context.getELFSection(".AMDGPU.config",
79 SectionKind::getReadOnly());
80 OutStreamer.SwitchSection(ConfigSection);
82 const AMDGPUSubtarget &STM = TM.getSubtarget<AMDGPUSubtarget>();
83 SIProgramInfo KernelInfo;
84 if (STM.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
85 getSIProgramInfo(KernelInfo, MF);
86 EmitProgramInfoSI(MF, KernelInfo);
88 EmitProgramInfoR600(MF);
95 OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
99 const MCSectionELF *CommentSection
100 = Context.getELFSection(".AMDGPU.csdata",
101 ELF::SHT_PROGBITS, 0,
102 SectionKind::getReadOnly());
103 OutStreamer.SwitchSection(CommentSection);
105 if (STM.getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) {
106 OutStreamer.emitRawComment(" Kernel info:", false);
107 OutStreamer.emitRawComment(" codeLenInByte = " + Twine(KernelInfo.CodeLen),
109 OutStreamer.emitRawComment(" NumSgprs: " + Twine(KernelInfo.NumSGPR),
111 OutStreamer.emitRawComment(" NumVgprs: " + Twine(KernelInfo.NumVGPR),
113 OutStreamer.emitRawComment(" FloatMode: " + Twine(KernelInfo.FloatMode),
115 OutStreamer.emitRawComment(" IeeeMode: " + Twine(KernelInfo.IEEEMode),
118 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
119 OutStreamer.emitRawComment(
120 Twine("SQ_PGM_RESOURCES:STACK_SIZE = " + Twine(MFI->StackSize)));
124 if (STM.dumpCode()) {
125 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
130 OutStreamer.SwitchSection(Context.getELFSection(".AMDGPU.disasm",
132 SectionKind::getReadOnly()));
134 for (size_t i = 0; i < DisasmLines.size(); ++i) {
135 std::string Comment(DisasmLineMaxLen - DisasmLines[i].size(), ' ');
136 Comment += " ; " + HexLines[i] + "\n";
138 OutStreamer.EmitBytes(StringRef(DisasmLines[i]));
139 OutStreamer.EmitBytes(StringRef(Comment));
147 void AMDGPUAsmPrinter::EmitProgramInfoR600(MachineFunction &MF) {
149 bool killPixel = false;
150 const R600RegisterInfo * RI =
151 static_cast<const R600RegisterInfo*>(TM.getRegisterInfo());
152 R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
153 const AMDGPUSubtarget &STM = TM.getSubtarget<AMDGPUSubtarget>();
155 for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end();
157 MachineBasicBlock &MBB = *BB;
158 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
160 MachineInstr &MI = *I;
161 if (MI.getOpcode() == AMDGPU::KILLGT)
163 unsigned numOperands = MI.getNumOperands();
164 for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
165 MachineOperand & MO = MI.getOperand(op_idx);
168 unsigned HWReg = RI->getEncodingValue(MO.getReg()) & 0xff;
170 // Register with value > 127 aren't GPR
173 MaxGPR = std::max(MaxGPR, HWReg);
179 if (STM.getGeneration() >= AMDGPUSubtarget::EVERGREEN) {
180 // Evergreen / Northern Islands
181 switch (MFI->getShaderType()) {
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;
190 switch (MFI->getShaderType()) {
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;
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);
205 if (MFI->getShaderType() == ShaderType::COMPUTE) {
206 OutStreamer.EmitIntValue(R_0288E8_SQ_LDS_ALLOC, 4);
207 OutStreamer.EmitIntValue(RoundUpToAlignment(MFI->LDSSize, 4) >> 2, 4);
211 void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &ProgInfo,
212 MachineFunction &MF) const {
213 uint64_t CodeSize = 0;
214 unsigned MaxSGPR = 0;
215 unsigned MaxVGPR = 0;
216 bool VCCUsed = false;
217 const SIRegisterInfo * RI =
218 static_cast<const SIRegisterInfo*>(TM.getRegisterInfo());
220 for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end();
222 MachineBasicBlock &MBB = *BB;
223 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
225 MachineInstr &MI = *I;
227 // TODO: CodeSize should account for multiple functions.
228 CodeSize += MI.getDesc().Size;
230 unsigned numOperands = MI.getNumOperands();
231 for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
232 MachineOperand &MO = MI.getOperand(op_idx);
239 unsigned reg = MO.getReg();
240 if (reg == AMDGPU::VCC || reg == AMDGPU::VCC_LO ||
241 reg == AMDGPU::VCC_HI) {
254 if (AMDGPU::SReg_32RegClass.contains(reg)) {
257 } else if (AMDGPU::VReg_32RegClass.contains(reg)) {
260 } else if (AMDGPU::SReg_64RegClass.contains(reg)) {
263 } else if (AMDGPU::VReg_64RegClass.contains(reg)) {
266 } else if (AMDGPU::VReg_96RegClass.contains(reg)) {
269 } else if (AMDGPU::SReg_128RegClass.contains(reg)) {
272 } else if (AMDGPU::VReg_128RegClass.contains(reg)) {
275 } else if (AMDGPU::SReg_256RegClass.contains(reg)) {
278 } else if (AMDGPU::VReg_256RegClass.contains(reg)) {
281 } else if (AMDGPU::SReg_512RegClass.contains(reg)) {
284 } else if (AMDGPU::VReg_512RegClass.contains(reg)) {
288 llvm_unreachable("Unknown register class");
290 unsigned hwReg = RI->getEncodingValue(reg) & 0xff;
291 unsigned maxUsed = hwReg + width - 1;
293 MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR;
295 MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR;
304 ProgInfo.NumVGPR = MaxVGPR;
305 ProgInfo.NumSGPR = MaxSGPR;
307 // Set the value to initialize FP_ROUND and FP_DENORM parts of the mode
309 ProgInfo.FloatMode = getFPMode(MF);
311 // XXX: Not quite sure what this does, but sc seems to unset this.
312 ProgInfo.IEEEMode = 0;
314 // Do not clamp NAN to 0.
315 ProgInfo.DX10Clamp = 0;
317 ProgInfo.CodeLen = CodeSize;
320 void AMDGPUAsmPrinter::EmitProgramInfoSI(MachineFunction &MF,
321 const SIProgramInfo &KernelInfo) {
322 const AMDGPUSubtarget &STM = TM.getSubtarget<AMDGPUSubtarget>();
323 SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
326 switch (MFI->getShaderType()) {
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;
334 unsigned LDSAlignShift;
335 if (STM.getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) {
336 // LDS is allocated in 64 dword blocks.
339 // LDS is allocated in 128 dword blocks.
344 RoundUpToAlignment(MFI->LDSSize, 1 << LDSAlignShift) >> LDSAlignShift;
346 if (MFI->getShaderType() == ShaderType::COMPUTE) {
347 OutStreamer.EmitIntValue(R_00B848_COMPUTE_PGM_RSRC1, 4);
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);
359 OutStreamer.EmitIntValue(ComputePGMRSrc1, 4);
361 OutStreamer.EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4);
362 OutStreamer.EmitIntValue(S_00B84C_LDS_SIZE(LDSBlocks), 4);
364 OutStreamer.EmitIntValue(RsrcReg, 4);
365 OutStreamer.EmitIntValue(S_00B028_VGPRS(KernelInfo.NumVGPR / 4) |
366 S_00B028_SGPRS(KernelInfo.NumSGPR / 8), 4);
369 if (MFI->getShaderType() == ShaderType::PIXEL) {
370 OutStreamer.EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4);
371 OutStreamer.EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(LDSBlocks), 4);
372 OutStreamer.EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4);
373 OutStreamer.EmitIntValue(MFI->PSInputAddr, 4);