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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// 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) {
|
|
|
|
const AMDGPUSubtarget &STM = TM.getSubtarget<AMDGPUSubtarget>();
|
|
|
|
if (STM.dumpCode()) {
|
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
|
|
|
MF.dump();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
SetupMachineFunction(MF);
|
2013-02-06 01:09:11 +08:00
|
|
|
if (OutStreamer.hasRawTextSupport()) {
|
|
|
|
OutStreamer.EmitRawText("@" + MF.getName() + ":");
|
|
|
|
}
|
2013-04-17 23:17:25 +08:00
|
|
|
|
|
|
|
const MCSectionELF *ConfigSection = getObjFileLowering().getContext()
|
|
|
|
.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-06-08 04:37:48 +08:00
|
|
|
if (STM.getGeneration() > AMDGPUSubtarget::NORTHERN_ISLANDS) {
|
2013-04-17 23:17:25 +08:00
|
|
|
EmitProgramInfoSI(MF);
|
|
|
|
} else {
|
|
|
|
EmitProgramInfoR600(MF);
|
2012-12-12 05:25:42 +08:00
|
|
|
}
|
2013-04-16 01:51:30 +08:00
|
|
|
OutStreamer.SwitchSection(getObjFileLowering().getTextSection());
|
2012-12-12 05:25:42 +08:00
|
|
|
EmitFunctionBody();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
void AMDGPUAsmPrinter::EmitProgramInfoSI(MachineFunction &MF) {
|
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++) {
|
|
|
|
MachineOperand & MO = MI.getOperand(op_idx);
|
|
|
|
unsigned maxUsed;
|
|
|
|
unsigned width = 0;
|
|
|
|
bool isSGPR = false;
|
|
|
|
unsigned reg;
|
|
|
|
unsigned hwReg;
|
|
|
|
if (!MO.isReg()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
reg = MO.getReg();
|
|
|
|
if (reg == AMDGPU::VCC) {
|
|
|
|
VCCUsed = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (reg) {
|
|
|
|
default: break;
|
|
|
|
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;
|
|
|
|
} else if (AMDGPU::VReg_512RegClass.contains(reg)) {
|
|
|
|
isSGPR = false;
|
|
|
|
width = 16;
|
2012-12-12 05:25:42 +08:00
|
|
|
} else {
|
|
|
|
assert(!"Unknown register class");
|
|
|
|
}
|
2013-02-08 03:39:45 +08:00
|
|
|
hwReg = RI->getEncodingValue(reg) & 0xff;
|
2012-12-12 05:25:42 +08:00
|
|
|
maxUsed = hwReg + width - 1;
|
|
|
|
if (isSGPR) {
|
|
|
|
MaxSGPR = maxUsed > MaxSGPR ? maxUsed : MaxSGPR;
|
|
|
|
} else {
|
|
|
|
MaxVGPR = maxUsed > MaxVGPR ? maxUsed : MaxVGPR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (VCCUsed) {
|
|
|
|
MaxSGPR += 2;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
OutStreamer.EmitIntValue(S_00B028_VGPRS(MaxVGPR / 4) | S_00B028_SGPRS(MaxSGPR / 8), 4);
|
2013-07-11 00:37:07 +08:00
|
|
|
|
|
|
|
if (MFI->ShaderType == ShaderType::COMPUTE) {
|
|
|
|
OutStreamer.EmitIntValue(R_00B84C_COMPUTE_PGM_RSRC2, 4);
|
|
|
|
OutStreamer.EmitIntValue(S_00B84C_LDS_SIZE(RoundUpToAlignment(MFI->LDSSize, 256) >> 8), 4);
|
|
|
|
}
|
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);
|
|
|
|
OutStreamer.EmitIntValue(S_00B02C_EXTRA_LDS_SIZE(RoundUpToAlignment(MFI->LDSSize, 256) >> 8), 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
|
|
|
}
|