2012-12-12 05:25:42 +08:00
|
|
|
//===-- AMDGPUAsmPrinter.cpp - AMDGPU Assebly printer --------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
/// \file
|
|
|
|
///
|
|
|
|
/// The AMDGPUAsmPrinter is used to print both assembly string and also binary
|
|
|
|
/// code. When passed an MCAsmStreamer it prints assembly and when passed
|
|
|
|
/// an MCObjectStreamer it outputs binary code.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
#include "AMDGPUAsmPrinter.h"
|
|
|
|
#include "AMDGPU.h"
|
2013-05-07 01:50:51 +08:00
|
|
|
#include "R600Defines.h"
|
2013-04-24 01:34:12 +08:00
|
|
|
#include "R600MachineFunctionInfo.h"
|
2013-04-17 23:17:25 +08:00
|
|
|
#include "R600RegisterInfo.h"
|
2013-05-24 01:10:37 +08:00
|
|
|
#include "SIDefines.h"
|
|
|
|
#include "SIMachineFunctionInfo.h"
|
|
|
|
#include "SIRegisterInfo.h"
|
2013-04-16 01:51:30 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/MC/MCSectionELF.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2013-04-16 01:51:30 +08:00
|
|
|
#include "llvm/Support/ELF.h"
|
2013-06-28 23:47:08 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2013-01-02 18:22:59 +08:00
|
|
|
#include "llvm/Target/TargetLoweringObjectFile.h"
|
2012-12-12 05:25:42 +08:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
|
|
|
|
static AsmPrinter *createAMDGPUAsmPrinterPass(TargetMachine &tm,
|
|
|
|
MCStreamer &Streamer) {
|
|
|
|
return new AMDGPUAsmPrinter(tm, Streamer);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" void LLVMInitializeR600AsmPrinter() {
|
|
|
|
TargetRegistry::RegisterAsmPrinter(TheAMDGPUTarget, createAMDGPUAsmPrinterPass);
|
|
|
|
}
|
|
|
|
|
2013-10-12 13:02:51 +08:00
|
|
|
AMDGPUAsmPrinter::AMDGPUAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
|
2013-12-05 13:15:35 +08:00
|
|
|
: AsmPrinter(TM, Streamer) {
|
2013-10-12 13:02:51 +08:00
|
|
|
DisasmEnabled = TM.getSubtarget<AMDGPUSubtarget>().dumpCode() &&
|
|
|
|
! Streamer.hasRawTextSupport();
|
|
|
|
}
|
|
|
|
|
2012-12-12 05:25:42 +08:00
|
|
|
/// We need to override this function so we can avoid
|
|
|
|
/// the call to EmitFunctionHeader(), which the MCPureStreamer can't handle.
|
|
|
|
bool AMDGPUAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
SetupMachineFunction(MF);
|
2013-12-05 13:15:35 +08:00
|
|
|
|
2014-02-01 05:54:49 +08:00
|
|
|
OutStreamer.emitRawComment(Twine('@') + MF.getName() + Twine(':'));
|
2013-04-17 23:17:25 +08:00
|
|
|
|
2013-10-12 13:02:51 +08:00
|
|
|
MCContext &Context = getObjFileLowering().getContext();
|
|
|
|
const MCSectionELF *ConfigSection = Context.getELFSection(".AMDGPU.config",
|
2013-04-25 07:56:14 +08:00
|
|
|
ELF::SHT_PROGBITS, 0,
|
2013-04-17 23:17:25 +08:00
|
|
|
SectionKind::getReadOnly());
|
|
|
|
OutStreamer.SwitchSection(ConfigSection);
|
2013-12-05 13:15:35 +08:00
|
|
|
|
2013-10-12 13:02:51 +08:00
|
|
|
const AMDGPUSubtarget &STM = TM.getSubtarget<AMDGPUSubtarget>();
|
2013-12-05 13:15:35 +08:00
|
|
|
SIProgramInfo KernelInfo;
|
2013-06-08 04:37:48 +08:00
|
|
|
if (STM.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
|
2013-12-05 13:15:35 +08:00
|
|
|
findNumUsedRegistersSI(MF, KernelInfo.NumSGPR, KernelInfo.NumVGPR);
|
|
|
|
EmitProgramInfoSI(MF, KernelInfo);
|
2013-04-17 23:17:25 +08:00
|
|
|
} else {
|
|
|
|
EmitProgramInfoR600(MF);
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-10-12 13:02:51 +08:00
|
|
|
|
|
|
|
DisasmLines.clear();
|
|
|
|
HexLines.clear();
|
|
|
|
DisasmLineMaxLen = 0;
|
|
|
|
|
2013-04-16 01:51:30 +08:00
|
|
|
OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
|
2012-12-12 05:25:42 +08:00
|
|
|
EmitFunctionBody();
|
2013-10-12 13:02:51 +08:00
|
|
|
|
2014-02-01 06:08:19 +08:00
|
|
|
if (isVerbose()) {
|
2013-12-05 13:15:35 +08:00
|
|
|
const MCSectionELF *CommentSection
|
|
|
|
= Context.getELFSection(".AMDGPU.csdata",
|
|
|
|
ELF::SHT_PROGBITS, 0,
|
|
|
|
SectionKind::getReadOnly());
|
|
|
|
OutStreamer.SwitchSection(CommentSection);
|
|
|
|
|
2014-01-23 05:55:35 +08:00
|
|
|
if (STM.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
|
2014-01-27 08:19:41 +08:00
|
|
|
OutStreamer.emitRawComment(" Kernel info:", false);
|
|
|
|
OutStreamer.emitRawComment(" NumSgprs: " + Twine(KernelInfo.NumSGPR),
|
2014-01-27 08:16:00 +08:00
|
|
|
false);
|
2014-01-27 08:19:41 +08:00
|
|
|
OutStreamer.emitRawComment(" NumVgprs: " + Twine(KernelInfo.NumVGPR),
|
2014-01-27 08:16:00 +08:00
|
|
|
false);
|
2014-01-23 05:55:35 +08:00
|
|
|
} else {
|
|
|
|
R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
|
2014-02-01 06:08:19 +08:00
|
|
|
OutStreamer.emitRawComment(
|
2014-01-23 05:55:35 +08:00
|
|
|
Twine("SQ_PGM_RESOURCES:STACK_SIZE = " + Twine(MFI->StackSize)));
|
|
|
|
}
|
2013-12-05 13:15:35 +08:00
|
|
|
}
|
|
|
|
|
2013-10-12 13:02:51 +08:00
|
|
|
if (STM.dumpCode()) {
|
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
|
|
|
MF.dump();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (DisasmEnabled) {
|
|
|
|
OutStreamer.SwitchSection(Context.getELFSection(".AMDGPU.disasm",
|
|
|
|
ELF::SHT_NOTE, 0,
|
|
|
|
SectionKind::getReadOnly()));
|
|
|
|
|
|
|
|
for (size_t i = 0; i < DisasmLines.size(); ++i) {
|
|
|
|
std::string Comment(DisasmLineMaxLen - DisasmLines[i].size(), ' ');
|
|
|
|
Comment += " ; " + HexLines[i] + "\n";
|
|
|
|
|
|
|
|
OutStreamer.EmitBytes(StringRef(DisasmLines[i]));
|
|
|
|
OutStreamer.EmitBytes(StringRef(Comment));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-12 05:25:42 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-04-17 23:17:25 +08:00
|
|
|
void AMDGPUAsmPrinter::EmitProgramInfoR600(MachineFunction &MF) {
|
|
|
|
unsigned MaxGPR = 0;
|
2013-04-30 08:13:13 +08:00
|
|
|
bool killPixel = false;
|
2013-04-17 23:17:25 +08:00
|
|
|
const R600RegisterInfo * RI =
|
|
|
|
static_cast<const R600RegisterInfo*>(TM.getRegisterInfo());
|
2013-04-24 01:34:12 +08:00
|
|
|
R600MachineFunctionInfo *MFI = MF.getInfo<R600MachineFunctionInfo>();
|
2013-05-07 01:50:51 +08:00
|
|
|
const AMDGPUSubtarget &STM = TM.getSubtarget<AMDGPUSubtarget>();
|
2013-04-17 23:17:25 +08:00
|
|
|
|
|
|
|
for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end();
|
|
|
|
BB != BB_E; ++BB) {
|
|
|
|
MachineBasicBlock &MBB = *BB;
|
|
|
|
for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
MachineInstr &MI = *I;
|
2013-04-30 08:13:13 +08:00
|
|
|
if (MI.getOpcode() == AMDGPU::KILLGT)
|
|
|
|
killPixel = true;
|
2013-04-17 23:17:25 +08:00
|
|
|
unsigned numOperands = MI.getNumOperands();
|
|
|
|
for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
|
|
|
|
MachineOperand & MO = MI.getOperand(op_idx);
|
|
|
|
if (!MO.isReg())
|
|
|
|
continue;
|
|
|
|
unsigned HWReg = RI->getEncodingValue(MO.getReg()) & 0xff;
|
|
|
|
|
|
|
|
// Register with value > 127 aren't GPR
|
|
|
|
if (HWReg > 127)
|
|
|
|
continue;
|
|
|
|
MaxGPR = std::max(MaxGPR, HWReg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-05-07 01:50:51 +08:00
|
|
|
|
|
|
|
unsigned RsrcReg;
|
2013-06-08 04:37:48 +08:00
|
|
|
if (STM.getGeneration() >= AMDGPUSubtarget::EVERGREEN) {
|
2013-05-07 01:50:51 +08:00
|
|
|
// Evergreen / Northern Islands
|
|
|
|
switch (MFI->ShaderType) {
|
|
|
|
default: // Fall through
|
|
|
|
case ShaderType::COMPUTE: RsrcReg = R_0288D4_SQ_PGM_RESOURCES_LS; break;
|
|
|
|
case ShaderType::GEOMETRY: RsrcReg = R_028878_SQ_PGM_RESOURCES_GS; break;
|
|
|
|
case ShaderType::PIXEL: RsrcReg = R_028844_SQ_PGM_RESOURCES_PS; break;
|
|
|
|
case ShaderType::VERTEX: RsrcReg = R_028860_SQ_PGM_RESOURCES_VS; break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// R600 / R700
|
|
|
|
switch (MFI->ShaderType) {
|
|
|
|
default: // Fall through
|
|
|
|
case ShaderType::GEOMETRY: // Fall through
|
|
|
|
case ShaderType::COMPUTE: // Fall through
|
|
|
|
case ShaderType::VERTEX: RsrcReg = R_028868_SQ_PGM_RESOURCES_VS; break;
|
|
|
|
case ShaderType::PIXEL: RsrcReg = R_028850_SQ_PGM_RESOURCES_PS; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
OutStreamer.EmitIntValue(RsrcReg, 4);
|
|
|
|
OutStreamer.EmitIntValue(S_NUM_GPRS(MaxGPR + 1) |
|
|
|
|
S_STACK_SIZE(MFI->StackSize), 4);
|
|
|
|
OutStreamer.EmitIntValue(R_02880C_DB_SHADER_CONTROL, 4);
|
|
|
|
OutStreamer.EmitIntValue(S_02880C_KILL_ENABLE(killPixel), 4);
|
2013-06-28 23:47:08 +08:00
|
|
|
|
|
|
|
if (MFI->ShaderType == ShaderType::COMPUTE) {
|
|
|
|
OutStreamer.EmitIntValue(R_0288E8_SQ_LDS_ALLOC, 4);
|
|
|
|
OutStreamer.EmitIntValue(RoundUpToAlignment(MFI->LDSSize, 4) >> 2, 4);
|
|
|
|
}
|
2013-04-17 23:17:25 +08:00
|
|
|
}
|
|
|
|
|
2013-12-05 13:15:35 +08:00
|
|
|
void AMDGPUAsmPrinter::findNumUsedRegistersSI(MachineFunction &MF,
|
|
|
|
unsigned &NumSGPR,
|
|
|
|
unsigned &NumVGPR) const {
|
2012-12-12 05:25:42 +08:00
|
|
|
unsigned MaxSGPR = 0;
|
|
|
|
unsigned MaxVGPR = 0;
|
|
|
|
bool VCCUsed = false;
|
|
|
|
const SIRegisterInfo * RI =
|
|
|
|
static_cast<const SIRegisterInfo*>(TM.getRegisterInfo());
|
|
|
|
|
|
|
|
for (MachineFunction::iterator BB = MF.begin(), BB_E = MF.end();
|
|
|
|
BB != BB_E; ++BB) {
|
|
|
|
MachineBasicBlock &MBB = *BB;
|
|
|
|
for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
|
|
|
|
I != E; ++I) {
|
|
|
|
MachineInstr &MI = *I;
|
|
|
|
|
|
|
|
unsigned numOperands = MI.getNumOperands();
|
|
|
|
for (unsigned op_idx = 0; op_idx < numOperands; op_idx++) {
|
2013-11-14 18:08:50 +08:00
|
|
|
MachineOperand &MO = MI.getOperand(op_idx);
|
2012-12-12 05:25:42 +08:00
|
|
|
unsigned width = 0;
|
|
|
|
bool isSGPR = false;
|
2014-01-09 05:47:14 +08:00
|
|
|
|
2012-12-12 05:25:42 +08:00
|
|
|
if (!MO.isReg()) {
|
|
|
|
continue;
|
|
|
|
}
|
2014-01-09 05:47:14 +08:00
|
|
|
unsigned reg = MO.getReg();
|
2012-12-12 05:25:42 +08:00
|
|
|
if (reg == AMDGPU::VCC) {
|
|
|
|
VCCUsed = true;
|
|
|
|
continue;
|
|
|
|
}
|
2013-10-23 05:11:31 +08:00
|
|
|
|
2012-12-12 05:25:42 +08:00
|
|
|
switch (reg) {
|
|
|
|
default: break;
|
2013-10-23 05:11:31 +08:00
|
|
|
case AMDGPU::SCC:
|
2012-12-12 05:25:42 +08:00
|
|
|
case AMDGPU::EXEC:
|
|
|
|
case AMDGPU::M0:
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (AMDGPU::SReg_32RegClass.contains(reg)) {
|
|
|
|
isSGPR = true;
|
|
|
|
width = 1;
|
|
|
|
} else if (AMDGPU::VReg_32RegClass.contains(reg)) {
|
|
|
|
isSGPR = false;
|
|
|
|
width = 1;
|
|
|
|
} else if (AMDGPU::SReg_64RegClass.contains(reg)) {
|
|
|
|
isSGPR = true;
|
|
|
|
width = 2;
|
|
|
|
} else if (AMDGPU::VReg_64RegClass.contains(reg)) {
|
|
|
|
isSGPR = false;
|
|
|
|
width = 2;
|
2013-04-10 16:39:16 +08:00
|
|
|
} else if (AMDGPU::VReg_96RegClass.contains(reg)) {
|
|
|
|
isSGPR = false;
|
|
|
|
width = 3;
|
2012-12-12 05:25:42 +08:00
|
|
|
} else if (AMDGPU::SReg_128RegClass.contains(reg)) {
|
|
|
|
isSGPR = true;
|
|
|
|
width = 4;
|
|
|
|
} else if (AMDGPU::VReg_128RegClass.contains(reg)) {
|
|
|
|
isSGPR = false;
|
|
|
|
width = 4;
|
|
|
|
} else if (AMDGPU::SReg_256RegClass.contains(reg)) {
|
|
|
|
isSGPR = true;
|
|
|
|
width = 8;
|
2013-02-08 01:02:09 +08:00
|
|
|
} else if (AMDGPU::VReg_256RegClass.contains(reg)) {
|
|
|
|
isSGPR = false;
|
|
|
|
width = 8;
|
2013-10-23 08:44:12 +08:00
|
|
|
} else if (AMDGPU::SReg_512RegClass.contains(reg)) {
|
|
|
|
isSGPR = true;
|
|
|
|
width = 16;
|
2013-02-08 01:02:09 +08:00
|
|
|
} else if (AMDGPU::VReg_512RegClass.contains(reg)) {
|
|
|
|
isSGPR = false;
|
|
|
|
width = 16;
|
2012-12-12 05:25:42 +08:00
|
|
|
} else {
|
2013-12-11 05:37:42 +08:00
|
|
|
llvm_unreachable("Unknown register class");
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2014-01-09 05:47:14 +08:00
|
|
|
unsigned hwReg = RI->getEncodingValue(reg) & 0xff;
|
|
|
|
unsigned maxUsed = hwReg + width - 1;
|
2012-12-12 05:25:42 +08:00
|
|
|
if (isSGPR) {
|
|
|
|
MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR;
|
|
|
|
} else {
|
|
|
|
MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-05 13:15:35 +08:00
|
|
|
|
|
|
|
if (VCCUsed)
|
2012-12-12 05:25:42 +08:00
|
|
|
MaxSGPR += 2;
|
2013-12-05 13:15:35 +08:00
|
|
|
|
|
|
|
NumSGPR = MaxSGPR;
|
|
|
|
NumVGPR = MaxVGPR;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AMDGPUAsmPrinter::getSIProgramInfo(SIProgramInfo &Out,
|
|
|
|
MachineFunction &MF) const {
|
|
|
|
findNumUsedRegistersSI(MF, Out.NumSGPR, Out.NumVGPR);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AMDGPUAsmPrinter::EmitProgramInfoSI(MachineFunction &MF,
|
|
|
|
const SIProgramInfo &KernelInfo) {
|
|
|
|
const AMDGPUSubtarget &STM = TM.getSubtarget<AMDGPUSubtarget>();
|
|
|
|
|
|
|
|
SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
|
2013-04-16 01:51:35 +08:00
|
|
|
unsigned RsrcReg;
|
|
|
|
switch (MFI->ShaderType) {
|
|
|
|
default: // Fall through
|
|
|
|
case ShaderType::COMPUTE: RsrcReg = R_00B848_COMPUTE_PGM_RSRC1; break;
|
|
|
|
case ShaderType::GEOMETRY: RsrcReg = R_00B228_SPI_SHADER_PGM_RSRC1_GS; break;
|
|
|
|
case ShaderType::PIXEL: RsrcReg = R_00B028_SPI_SHADER_PGM_RSRC1_PS; break;
|
|
|
|
case ShaderType::VERTEX: RsrcReg = R_00B128_SPI_SHADER_PGM_RSRC1_VS; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
OutStreamer.EmitIntValue(RsrcReg, 4);
|
2013-12-05 13:15:35 +08:00
|
|
|
OutStreamer.EmitIntValue(S_00B028_VGPRS(KernelInfo.NumVGPR / 4) |
|
|
|
|
S_00B028_SGPRS(KernelInfo.NumSGPR / 8), 4);
|
2013-07-11 00:37:07 +08:00
|
|
|
|
2013-10-30 00:37:28 +08:00
|
|
|
unsigned LDSAlignShift;
|
|
|
|
if (STM.getGeneration() < AMDGPUSubtarget::SEA_ISLANDS) {
|
|
|
|
// LDS is allocated in 64 dword blocks
|
|
|
|
LDSAlignShift = 8;
|
|
|
|
} else {
|
|
|
|
// LDS is allocated in 128 dword blocks
|
|
|
|
LDSAlignShift = 9;
|
|
|
|
}
|
|
|
|
unsigned LDSBlocks =
|
|
|
|
RoundUpToAlignment(MFI->LDSSize, 1 << LDSAlignShift) >> LDSAlignShift;
|
|
|
|
|
2013-07-11 00:37:07 +08:00
|
|
|
if (MFI->ShaderType == ShaderType::COMPUTE) {
|
|
|
|
OutStreamer.EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4);
|
2013-10-30 00:37:28 +08:00
|
|
|
OutStreamer.EmitIntValue(S_00B84C_LDS_SIZE(LDSBlocks), 4);
|
2013-07-11 00:37:07 +08:00
|
|
|
}
|
2013-04-16 01:51:35 +08:00
|
|
|
if (MFI->ShaderType == ShaderType::PIXEL) {
|
2013-07-11 00:37:07 +08:00
|
|
|
OutStreamer.EmitIntValue(R_00B02C_SPI_SHADER_PGM_RSRC2_PS, 4);
|
2013-10-30 00:37:28 +08:00
|
|
|
OutStreamer.EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(LDSBlocks), 4);
|
2013-04-16 01:51:35 +08:00
|
|
|
OutStreamer.EmitIntValue(R_0286CC_SPI_PS_INPUT_ENA, 4);
|
|
|
|
OutStreamer.EmitIntValue(MFI->PSInputAddr, 4);
|
|
|
|
}
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|