2010-09-07 20:14:24 +02:00
|
|
|
//===-- PTXAsmPrinter.cpp - PTX LLVM assembly writer ----------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file contains a printer that converts from our internal representation
|
|
|
|
// of machine-dependent LLVM code to PTX assembly language.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-11-08 04:06:08 +01:00
|
|
|
#define DEBUG_TYPE "ptx-asm-printer"
|
|
|
|
|
2010-09-07 20:14:24 +02:00
|
|
|
#include "PTX.h"
|
2010-11-08 04:06:08 +01:00
|
|
|
#include "PTXMachineFunctionInfo.h"
|
2010-09-07 20:14:24 +02:00
|
|
|
#include "PTXTargetMachine.h"
|
2010-12-22 11:38:51 +01:00
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Module.h"
|
2010-09-18 20:52:28 +02:00
|
|
|
#include "llvm/ADT/SmallString.h"
|
2010-11-08 04:06:08 +01:00
|
|
|
#include "llvm/ADT/StringExtras.h"
|
|
|
|
#include "llvm/ADT/Twine.h"
|
2010-09-07 20:14:24 +02:00
|
|
|
#include "llvm/CodeGen/AsmPrinter.h"
|
2010-09-18 20:52:28 +02:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2010-11-08 04:06:08 +01:00
|
|
|
#include "llvm/MC/MCSymbol.h"
|
2010-12-22 11:38:51 +01:00
|
|
|
#include "llvm/Target/Mangler.h"
|
2010-11-08 04:06:08 +01:00
|
|
|
#include "llvm/Target/TargetLoweringObjectFile.h"
|
2010-09-07 20:14:24 +02:00
|
|
|
#include "llvm/Target/TargetRegistry.h"
|
2010-11-30 11:14:14 +01:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2010-11-08 04:06:08 +01:00
|
|
|
#include "llvm/Support/Debug.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2010-12-22 11:38:51 +01:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2010-11-08 04:06:08 +01:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2010-09-07 20:14:24 +02:00
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
2010-11-30 11:14:14 +01:00
|
|
|
static cl::opt<std::string>
|
|
|
|
OptPTXVersion("ptx-version", cl::desc("Set PTX version"),
|
|
|
|
cl::init("1.4"));
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
OptPTXTarget("ptx-target", cl::desc("Set GPU target (comma-separated list)"),
|
|
|
|
cl::init("sm_10"));
|
|
|
|
|
2010-09-07 20:14:24 +02:00
|
|
|
namespace {
|
2010-11-08 04:06:08 +01:00
|
|
|
class PTXAsmPrinter : public AsmPrinter {
|
|
|
|
public:
|
|
|
|
explicit PTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
|
|
|
|
: AsmPrinter(TM, Streamer) {}
|
|
|
|
|
|
|
|
const char *getPassName() const { return "PTX Assembly Printer"; }
|
|
|
|
|
2010-12-22 11:38:51 +01:00
|
|
|
bool doFinalization(Module &M);
|
|
|
|
|
2010-11-30 11:14:14 +01:00
|
|
|
virtual void EmitStartOfAsmFile(Module &M);
|
|
|
|
|
2010-11-08 04:06:08 +01:00
|
|
|
virtual bool runOnMachineFunction(MachineFunction &MF);
|
2010-09-18 20:52:28 +02:00
|
|
|
|
2010-11-08 04:06:08 +01:00
|
|
|
virtual void EmitFunctionBodyStart();
|
|
|
|
virtual void EmitFunctionBodyEnd() { OutStreamer.EmitRawText(Twine("}")); }
|
2010-09-18 20:52:28 +02:00
|
|
|
|
2010-11-08 04:06:08 +01:00
|
|
|
virtual void EmitInstruction(const MachineInstr *MI);
|
2010-10-19 15:14:40 +02:00
|
|
|
|
2010-11-08 04:06:08 +01:00
|
|
|
void printOperand(const MachineInstr *MI, int opNum, raw_ostream &OS);
|
2010-11-30 08:34:44 +01:00
|
|
|
void printMemOperand(const MachineInstr *MI, int opNum, raw_ostream &OS,
|
|
|
|
const char *Modifier = 0);
|
2010-11-08 04:06:08 +01:00
|
|
|
|
|
|
|
// autogen'd.
|
|
|
|
void printInstruction(const MachineInstr *MI, raw_ostream &OS);
|
|
|
|
static const char *getRegisterName(unsigned RegNo);
|
|
|
|
|
|
|
|
private:
|
2010-12-22 11:38:51 +01:00
|
|
|
void EmitVariableDeclaration(const GlobalVariable *gv);
|
2010-11-08 04:06:08 +01:00
|
|
|
void EmitFunctionDeclaration();
|
|
|
|
}; // class PTXAsmPrinter
|
2010-09-07 20:14:24 +02:00
|
|
|
} // namespace
|
|
|
|
|
2010-11-08 04:06:08 +01:00
|
|
|
static const char PARAM_PREFIX[] = "__param_";
|
|
|
|
|
2010-11-30 08:34:44 +01:00
|
|
|
static const char *getRegisterTypeName(unsigned RegNo) {
|
2010-11-08 04:06:08 +01:00
|
|
|
#define TEST_REGCLS(cls, clsstr) \
|
|
|
|
if (PTX::cls ## RegisterClass->contains(RegNo)) return # clsstr;
|
2010-11-17 09:08:49 +01:00
|
|
|
TEST_REGCLS(RRegs32, s32);
|
|
|
|
TEST_REGCLS(Preds, pred);
|
2010-11-08 04:06:08 +01:00
|
|
|
#undef TEST_REGCLS
|
|
|
|
|
|
|
|
llvm_unreachable("Not in any register class!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-11-30 08:34:44 +01:00
|
|
|
static const char *getInstructionTypeName(const MachineInstr *MI) {
|
2010-11-17 09:08:49 +01:00
|
|
|
for (int i = 0, e = MI->getNumOperands(); i != e; ++i) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(i);
|
|
|
|
if (MO.getType() == MachineOperand::MO_Register)
|
|
|
|
return getRegisterTypeName(MO.getReg());
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("No reg operand found in instruction!");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-12-22 11:38:51 +01:00
|
|
|
static const char *getStateSpaceName(unsigned addressSpace) {
|
2010-12-30 11:41:27 +01:00
|
|
|
switch (addressSpace) {
|
|
|
|
default: llvm_unreachable("Unknown state space");
|
|
|
|
case PTX::GLOBAL: return "global";
|
|
|
|
case PTX::CONSTANT: return "const";
|
|
|
|
case PTX::LOCAL: return "local";
|
|
|
|
case PTX::PARAMETER: return "param";
|
|
|
|
case PTX::SHARED: return "shared";
|
|
|
|
}
|
2010-12-22 11:38:51 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PTXAsmPrinter::doFinalization(Module &M) {
|
|
|
|
// XXX Temproarily remove global variables so that doFinalization() will not
|
|
|
|
// emit them again (global variables are emitted at beginning).
|
|
|
|
|
|
|
|
Module::GlobalListType &global_list = M.getGlobalList();
|
|
|
|
int i, n = global_list.size();
|
|
|
|
GlobalVariable **gv_array = new GlobalVariable* [n];
|
|
|
|
|
|
|
|
// first, back-up GlobalVariable in gv_array
|
|
|
|
i = 0;
|
|
|
|
for (Module::global_iterator I = global_list.begin(), E = global_list.end();
|
|
|
|
I != E; ++I)
|
|
|
|
gv_array[i++] = &*I;
|
|
|
|
|
|
|
|
// second, empty global_list
|
|
|
|
while (!global_list.empty())
|
|
|
|
global_list.remove(global_list.begin());
|
|
|
|
|
|
|
|
// call doFinalization
|
|
|
|
bool ret = AsmPrinter::doFinalization(M);
|
|
|
|
|
|
|
|
// now we restore global variables
|
|
|
|
for (i = 0; i < n; i ++)
|
|
|
|
global_list.insert(global_list.end(), gv_array[i]);
|
|
|
|
|
|
|
|
delete[] gv_array;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-11-30 11:14:14 +01:00
|
|
|
void PTXAsmPrinter::EmitStartOfAsmFile(Module &M)
|
|
|
|
{
|
|
|
|
OutStreamer.EmitRawText(Twine("\t.version " + OptPTXVersion));
|
|
|
|
OutStreamer.EmitRawText(Twine("\t.target " + OptPTXTarget));
|
|
|
|
OutStreamer.AddBlankLine();
|
2010-12-22 11:38:51 +01:00
|
|
|
|
|
|
|
// declare global variables
|
|
|
|
for (Module::const_global_iterator i = M.global_begin(), e = M.global_end();
|
|
|
|
i != e; ++i)
|
|
|
|
EmitVariableDeclaration(i);
|
2010-11-30 11:14:14 +01:00
|
|
|
}
|
|
|
|
|
2010-11-08 04:06:08 +01:00
|
|
|
bool PTXAsmPrinter::runOnMachineFunction(MachineFunction &MF) {
|
|
|
|
SetupMachineFunction(MF);
|
|
|
|
EmitFunctionDeclaration();
|
|
|
|
EmitFunctionBody();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PTXAsmPrinter::EmitFunctionBodyStart() {
|
|
|
|
OutStreamer.EmitRawText(Twine("{"));
|
|
|
|
|
|
|
|
const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
|
|
|
|
|
|
|
|
// Print local variable definition
|
|
|
|
for (PTXMachineFunctionInfo::reg_iterator
|
|
|
|
i = MFI->localVarRegBegin(), e = MFI->localVarRegEnd(); i != e; ++ i) {
|
|
|
|
unsigned reg = *i;
|
|
|
|
|
2010-11-17 09:08:49 +01:00
|
|
|
std::string def = "\t.reg .";
|
2010-11-08 04:06:08 +01:00
|
|
|
def += getRegisterTypeName(reg);
|
|
|
|
def += ' ';
|
|
|
|
def += getRegisterName(reg);
|
|
|
|
def += ';';
|
|
|
|
OutStreamer.EmitRawText(Twine(def));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-18 20:52:28 +02:00
|
|
|
void PTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
|
2010-12-01 12:45:53 +01:00
|
|
|
std::string str;
|
|
|
|
str.reserve(64);
|
|
|
|
|
|
|
|
// Write instruction to str
|
|
|
|
raw_string_ostream OS(str);
|
2010-10-19 15:14:40 +02:00
|
|
|
printInstruction(MI, OS);
|
|
|
|
OS << ';';
|
2010-12-01 12:45:53 +01:00
|
|
|
OS.flush();
|
2010-11-17 09:08:49 +01:00
|
|
|
|
|
|
|
// Replace "%type" if found
|
|
|
|
size_t pos;
|
2010-12-01 12:45:53 +01:00
|
|
|
if ((pos = str.find("%type")) != std::string::npos)
|
2010-11-30 08:34:44 +01:00
|
|
|
str.replace(pos, /*strlen("%type")==*/5, getInstructionTypeName(MI));
|
|
|
|
|
2010-12-01 12:45:53 +01:00
|
|
|
StringRef strref = StringRef(str);
|
2010-11-30 08:34:44 +01:00
|
|
|
OutStreamer.EmitRawText(strref);
|
2010-10-19 15:14:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void PTXAsmPrinter::printOperand(const MachineInstr *MI, int opNum,
|
|
|
|
raw_ostream &OS) {
|
|
|
|
const MachineOperand &MO = MI->getOperand(opNum);
|
|
|
|
|
|
|
|
switch (MO.getType()) {
|
|
|
|
default:
|
|
|
|
llvm_unreachable("<unknown operand type>");
|
|
|
|
break;
|
2010-12-22 11:38:51 +01:00
|
|
|
case MachineOperand::MO_GlobalAddress:
|
|
|
|
OS << *Mang->getSymbol(MO.getGlobal());
|
2010-10-19 15:14:40 +02:00
|
|
|
break;
|
|
|
|
case MachineOperand::MO_Immediate:
|
|
|
|
OS << (int) MO.getImm();
|
|
|
|
break;
|
2010-12-22 11:38:51 +01:00
|
|
|
case MachineOperand::MO_Register:
|
|
|
|
OS << getRegisterName(MO.getReg());
|
|
|
|
break;
|
2010-10-19 15:14:40 +02:00
|
|
|
}
|
2010-09-18 20:52:28 +02:00
|
|
|
}
|
|
|
|
|
2010-11-30 08:34:44 +01:00
|
|
|
void PTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
|
|
|
|
raw_ostream &OS, const char *Modifier) {
|
|
|
|
printOperand(MI, opNum, OS);
|
|
|
|
|
|
|
|
if (MI->getOperand(opNum+1).isImm() && MI->getOperand(opNum+1).getImm() == 0)
|
|
|
|
return; // don't print "+0"
|
|
|
|
|
|
|
|
OS << "+";
|
|
|
|
printOperand(MI, opNum+1, OS);
|
|
|
|
}
|
|
|
|
|
2010-12-22 11:38:51 +01:00
|
|
|
void PTXAsmPrinter::EmitVariableDeclaration(const GlobalVariable *gv) {
|
|
|
|
// Check to see if this is a special global used by LLVM, if so, emit it.
|
|
|
|
if (EmitSpecialLLVMGlobal(gv))
|
|
|
|
return;
|
|
|
|
|
|
|
|
MCSymbol *gvsym = Mang->getSymbol(gv);
|
|
|
|
|
|
|
|
assert(gvsym->isUndefined() && "Cannot define a symbol twice!");
|
|
|
|
|
|
|
|
std::string decl;
|
|
|
|
|
|
|
|
// check if it is defined in some other translation unit
|
|
|
|
if (gv->isDeclaration())
|
|
|
|
decl += ".extern ";
|
|
|
|
|
|
|
|
// state space: e.g., .global
|
|
|
|
decl += ".";
|
|
|
|
decl += getStateSpaceName(gv->getType()->getAddressSpace());
|
|
|
|
decl += " ";
|
|
|
|
|
|
|
|
// alignment (optional)
|
|
|
|
unsigned alignment = gv->getAlignment();
|
|
|
|
if (alignment != 0) {
|
|
|
|
decl += ".align ";
|
|
|
|
decl += utostr(Log2_32(gv->getAlignment()));
|
|
|
|
decl += " ";
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add types
|
|
|
|
decl += ".s32 ";
|
|
|
|
|
|
|
|
decl += gvsym->getName();
|
|
|
|
|
|
|
|
if (ArrayType::classof(gv->getType()) || PointerType::classof(gv->getType()))
|
|
|
|
decl += "[]";
|
|
|
|
|
|
|
|
decl += ";";
|
|
|
|
|
|
|
|
OutStreamer.EmitRawText(Twine(decl));
|
|
|
|
|
|
|
|
OutStreamer.AddBlankLine();
|
|
|
|
}
|
|
|
|
|
2010-11-08 04:06:08 +01:00
|
|
|
void PTXAsmPrinter::EmitFunctionDeclaration() {
|
|
|
|
// The function label could have already been emitted if two symbols end up
|
|
|
|
// conflicting due to asm renaming. Detect this and emit an error.
|
|
|
|
if (!CurrentFnSym->isUndefined()) {
|
|
|
|
report_fatal_error("'" + Twine(CurrentFnSym->getName()) +
|
|
|
|
"' label emitted multiple times to assembly file");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const PTXMachineFunctionInfo *MFI = MF->getInfo<PTXMachineFunctionInfo>();
|
|
|
|
const bool isKernel = MFI->isKernel();
|
|
|
|
unsigned reg;
|
|
|
|
|
|
|
|
std::string decl = isKernel ? ".entry" : ".func";
|
|
|
|
|
|
|
|
// Print return register
|
|
|
|
reg = MFI->retReg();
|
|
|
|
if (!isKernel && reg != PTX::NoRegister) {
|
2010-11-17 09:08:49 +01:00
|
|
|
decl += " (.reg ."; // FIXME: could it return in .param space?
|
2010-11-08 04:06:08 +01:00
|
|
|
decl += getRegisterTypeName(reg);
|
|
|
|
decl += " ";
|
|
|
|
decl += getRegisterName(reg);
|
|
|
|
decl += ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print function name
|
|
|
|
decl += " ";
|
|
|
|
decl += CurrentFnSym->getName().str();
|
|
|
|
|
|
|
|
// Print parameter list
|
|
|
|
if (!MFI->argRegEmpty()) {
|
|
|
|
decl += " (";
|
|
|
|
if (isKernel) {
|
|
|
|
for (int i = 0, e = MFI->getNumArg(); i != e; ++i) {
|
|
|
|
if (i != 0)
|
|
|
|
decl += ", ";
|
2010-12-22 11:38:51 +01:00
|
|
|
decl += ".param .s32 "; // TODO: add types
|
2010-11-08 04:06:08 +01:00
|
|
|
decl += PARAM_PREFIX;
|
|
|
|
decl += utostr(i + 1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (PTXMachineFunctionInfo::reg_iterator
|
|
|
|
i = MFI->argRegBegin(), e = MFI->argRegEnd(), b = i; i != e; ++i) {
|
|
|
|
reg = *i;
|
|
|
|
assert(reg != PTX::NoRegister && "Not a valid register!");
|
|
|
|
if (i != b)
|
|
|
|
decl += ", ";
|
2010-11-17 09:08:49 +01:00
|
|
|
decl += ".reg .";
|
2010-11-08 04:06:08 +01:00
|
|
|
decl += getRegisterTypeName(reg);
|
|
|
|
decl += " ";
|
|
|
|
decl += getRegisterName(reg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
decl += ")";
|
|
|
|
}
|
|
|
|
|
|
|
|
OutStreamer.EmitRawText(Twine(decl));
|
|
|
|
}
|
|
|
|
|
2010-09-18 20:52:28 +02:00
|
|
|
#include "PTXGenAsmWriter.inc"
|
|
|
|
|
2010-09-07 20:14:24 +02:00
|
|
|
// Force static initialization.
|
2010-09-18 20:52:28 +02:00
|
|
|
extern "C" void LLVMInitializePTXAsmPrinter() {
|
2010-09-07 20:14:24 +02:00
|
|
|
RegisterAsmPrinter<PTXAsmPrinter> X(ThePTXTarget);
|
|
|
|
}
|