summaryrefslogtreecommitdiff
path: root/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Target/NVPTX/NVPTXAsmPrinter.cpp')
-rw-r--r--lib/Target/NVPTX/NVPTXAsmPrinter.cpp275
1 files changed, 47 insertions, 228 deletions
diff --git a/lib/Target/NVPTX/NVPTXAsmPrinter.cpp b/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
index 753cfff4cdae..a966b9928400 100644
--- a/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
+++ b/lib/Target/NVPTX/NVPTXAsmPrinter.cpp
@@ -44,9 +44,7 @@
#include "llvm/CodeGen/MachineModuleInfo.h"
#include "llvm/CodeGen/MachineOperand.h"
#include "llvm/CodeGen/MachineRegisterInfo.h"
-#include "llvm/CodeGen/MachineValueType.h"
#include "llvm/CodeGen/TargetLowering.h"
-#include "llvm/CodeGen/TargetLoweringObjectFile.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"
#include "llvm/CodeGen/ValueTypes.h"
#include "llvm/IR/Attributes.h"
@@ -75,16 +73,17 @@
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
+#include "llvm/Support/MachineValueType.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Support/raw_ostream.h"
+#include "llvm/Target/TargetLoweringObjectFile.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Transforms/Utils/UnrollLoop.h"
#include <cassert>
#include <cstdint>
#include <cstring>
#include <new>
-#include <sstream>
#include <string>
#include <utility>
#include <vector>
@@ -93,16 +92,6 @@ using namespace llvm;
#define DEPOTNAME "__local_depot"
-static cl::opt<bool>
-EmitLineNumbers("nvptx-emit-line-numbers", cl::Hidden,
- cl::desc("NVPTX Specific: Emit Line numbers even without -G"),
- cl::init(true));
-
-static cl::opt<bool>
-InterleaveSrc("nvptx-emit-src", cl::ZeroOrMore, cl::Hidden,
- cl::desc("NVPTX Specific: Emit source line in ptx file"),
- cl::init(false));
-
/// DiscoverDependentGlobals - Return a set of GlobalVariables on which \p V
/// depends.
static void
@@ -151,56 +140,7 @@ VisitGlobalVariableForEmission(const GlobalVariable *GV,
Visiting.erase(GV);
}
-void NVPTXAsmPrinter::emitLineNumberAsDotLoc(const MachineInstr &MI) {
- if (!EmitLineNumbers)
- return;
- if (ignoreLoc(MI))
- return;
-
- const DebugLoc &curLoc = MI.getDebugLoc();
-
- if (!prevDebugLoc && !curLoc)
- return;
-
- if (prevDebugLoc == curLoc)
- return;
-
- prevDebugLoc = curLoc;
-
- if (!curLoc)
- return;
-
- auto *Scope = cast_or_null<DIScope>(curLoc.getScope());
- if (!Scope)
- return;
-
- StringRef fileName(Scope->getFilename());
- StringRef dirName(Scope->getDirectory());
- SmallString<128> FullPathName = dirName;
- if (!dirName.empty() && !sys::path::is_absolute(fileName)) {
- sys::path::append(FullPathName, fileName);
- fileName = FullPathName;
- }
-
- if (filenameMap.find(fileName) == filenameMap.end())
- return;
-
- // Emit the line from the source file.
- if (InterleaveSrc)
- this->emitSrcInText(fileName, curLoc.getLine());
-
- std::stringstream temp;
- temp << "\t.loc " << filenameMap[fileName] << " " << curLoc.getLine()
- << " " << curLoc.getCol();
- OutStreamer->EmitRawText(temp.str());
-}
-
void NVPTXAsmPrinter::EmitInstruction(const MachineInstr *MI) {
- SmallString<128> Str;
- raw_svector_ostream OS(Str);
- if (static_cast<NVPTXTargetMachine &>(TM).getDrvInterface() == NVPTX::CUDA)
- emitLineNumberAsDotLoc(*MI);
-
MCInst Inst;
lowerToMCInst(MI, Inst);
EmitToStreamer(*OutStreamer, Inst);
@@ -505,7 +445,7 @@ void NVPTXAsmPrinter::EmitFunctionEntryLabel() {
emitGlobals(*MF->getFunction().getParent());
GlobalsEmitted = true;
}
-
+
// Set up
MRI = &MF->getRegInfo();
F = &MF->getFunction();
@@ -526,14 +466,25 @@ void NVPTXAsmPrinter::EmitFunctionEntryLabel() {
OutStreamer->EmitRawText(O.str());
- prevDebugLoc = DebugLoc();
-}
-
-void NVPTXAsmPrinter::EmitFunctionBodyStart() {
VRegMapping.clear();
+ // Emit open brace for function body.
OutStreamer->EmitRawText(StringRef("{\n"));
setAndEmitFunctionVirtualRegisters(*MF);
+}
+bool NVPTXAsmPrinter::runOnMachineFunction(MachineFunction &F) {
+ nvptxSubtarget = &F.getSubtarget<NVPTXSubtarget>();
+ bool Result = AsmPrinter::runOnMachineFunction(F);
+ // Emit closing brace for the body of function F.
+ // The closing brace must be emitted here because we need to emit additional
+ // debug labels/data after the last basic block.
+ // We need to emit the closing brace here because we don't have function that
+ // finished emission of the function body.
+ OutStreamer->EmitRawText(StringRef("}\n"));
+ return Result;
+}
+
+void NVPTXAsmPrinter::EmitFunctionBodyStart() {
SmallString<128> Str;
raw_svector_ostream O(Str);
emitDemotedVars(&MF->getFunction(), O);
@@ -541,10 +492,15 @@ void NVPTXAsmPrinter::EmitFunctionBodyStart() {
}
void NVPTXAsmPrinter::EmitFunctionBodyEnd() {
- OutStreamer->EmitRawText(StringRef("}\n"));
VRegMapping.clear();
}
+const MCSymbol *NVPTXAsmPrinter::getFunctionFrameSymbol() const {
+ SmallString<128> Str;
+ raw_svector_ostream(Str) << DEPOTNAME << getFunctionNumber();
+ return OutContext.getOrCreateSymbol(Str);
+}
+
void NVPTXAsmPrinter::emitImplicitDef(const MachineInstr *MI) const {
unsigned RegNo = MI->getOperand(0).getReg();
if (TargetRegisterInfo::isVirtualRegister(RegNo)) {
@@ -818,42 +774,6 @@ void NVPTXAsmPrinter::emitDeclarations(const Module &M, raw_ostream &O) {
}
}
-void NVPTXAsmPrinter::recordAndEmitFilenames(Module &M) {
- DebugInfoFinder DbgFinder;
- DbgFinder.processModule(M);
-
- unsigned i = 1;
- for (const DICompileUnit *DIUnit : DbgFinder.compile_units()) {
- StringRef Filename = DIUnit->getFilename();
- StringRef Dirname = DIUnit->getDirectory();
- SmallString<128> FullPathName = Dirname;
- if (!Dirname.empty() && !sys::path::is_absolute(Filename)) {
- sys::path::append(FullPathName, Filename);
- Filename = FullPathName;
- }
- if (filenameMap.find(Filename) != filenameMap.end())
- continue;
- filenameMap[Filename] = i;
- OutStreamer->EmitDwarfFileDirective(i, "", Filename);
- ++i;
- }
-
- for (DISubprogram *SP : DbgFinder.subprograms()) {
- StringRef Filename = SP->getFilename();
- StringRef Dirname = SP->getDirectory();
- SmallString<128> FullPathName = Dirname;
- if (!Dirname.empty() && !sys::path::is_absolute(Filename)) {
- sys::path::append(FullPathName, Filename);
- Filename = FullPathName;
- }
- if (filenameMap.find(Filename) != filenameMap.end())
- continue;
- filenameMap[Filename] = i;
- OutStreamer->EmitDwarfFileDirective(i, "", Filename);
- ++i;
- }
-}
-
static bool isEmptyXXStructor(GlobalVariable *GV) {
if (!GV) return true;
const ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
@@ -889,24 +809,13 @@ bool NVPTXAsmPrinter::doInitialization(Module &M) {
SmallString<128> Str1;
raw_svector_ostream OS1(Str1);
- MMI = getAnalysisIfAvailable<MachineModuleInfo>();
-
// We need to call the parent's one explicitly.
- //bool Result = AsmPrinter::doInitialization(M);
-
- // Initialize TargetLoweringObjectFile since we didn't do in
- // AsmPrinter::doInitialization either right above or where it's commented out
- // below.
- const_cast<TargetLoweringObjectFile &>(getObjFileLowering())
- .Initialize(OutContext, TM);
+ bool Result = AsmPrinter::doInitialization(M);
// Emit header before any dwarf directives are emitted below.
emitHeader(M, OS1, STI);
OutStreamer->EmitRawText(OS1.str());
- // Already commented out
- //bool Result = AsmPrinter::doInitialization(M);
-
// Emit module-level inline asm if it exists.
if (!M.getModuleInlineAsm().empty()) {
OutStreamer->AddComment("Start of file scope inline assembly");
@@ -917,13 +826,9 @@ bool NVPTXAsmPrinter::doInitialization(Module &M) {
OutStreamer->AddBlankLine();
}
- // If we're not NVCL we're CUDA, go ahead and emit filenames.
- if (TM.getTargetTriple().getOS() != Triple::NVCL)
- recordAndEmitFilenames(M);
-
GlobalsEmitted = false;
-
- return false; // success
+
+ return Result;
}
void NVPTXAsmPrinter::emitGlobals(const Module &M) {
@@ -974,13 +879,10 @@ void NVPTXAsmPrinter::emitHeader(Module &M, raw_ostream &O,
const NVPTXTargetMachine &NTM = static_cast<const NVPTXTargetMachine &>(TM);
if (NTM.getDrvInterface() == NVPTX::NVCL)
O << ", texmode_independent";
- else {
- if (!STI.hasDouble())
- O << ", map_f64_to_f32";
- }
- if (MAI->doesSupportDebugInformation())
- O << ", debug";
+ // FIXME: remove comment once debug info is properly supported.
+ if (MMI && MMI->hasDebugInfo())
+ O << "//, debug";
O << "\n";
@@ -995,6 +897,8 @@ void NVPTXAsmPrinter::emitHeader(Module &M, raw_ostream &O,
}
bool NVPTXAsmPrinter::doFinalization(Module &M) {
+ bool HasDebugInfo = MMI && MMI->hasDebugInfo();
+
// If we did not emit any functions, then the global declarations have not
// yet been emitted.
if (!GlobalsEmitted) {
@@ -1029,6 +933,11 @@ bool NVPTXAsmPrinter::doFinalization(Module &M) {
clearAnnotationCache(&M);
delete[] gv_array;
+ // FIXME: remove comment once debug info is properly supported.
+ // Close the last emitted section
+ if (HasDebugInfo)
+ OutStreamer->EmitRawText("//\t}");
+
return ret;
//bool Result = AsmPrinter::doFinalization(M);
@@ -1365,7 +1274,8 @@ void NVPTXAsmPrinter::emitPTXAddressSpace(unsigned int AddressSpace,
O << "shared";
break;
default:
- report_fatal_error("Bad address space found while emitting PTX");
+ report_fatal_error("Bad address space found while emitting PTX: " +
+ llvm::Twine(AddressSpace));
break;
}
}
@@ -1433,7 +1343,7 @@ void NVPTXAsmPrinter::emitPTXGlobalVariable(const GlobalVariable *GVar,
return;
}
- if (ETy->isFloatingPointTy() || ETy->isIntegerTy() || ETy->isPointerTy()) {
+ if (ETy->isFloatingPointTy() || ETy->isIntOrPtrTy()) {
O << " .";
O << getPTXFundamentalTypeStr(ETy);
O << " ";
@@ -1948,11 +1858,17 @@ void NVPTXAsmPrinter::bufferLEByte(const Constant *CPV, int Bytes,
llvm_unreachable("unsupported integer const type");
break;
}
+ case Type::HalfTyID:
case Type::FloatTyID:
case Type::DoubleTyID: {
const ConstantFP *CFP = dyn_cast<ConstantFP>(CPV);
Type *Ty = CFP->getType();
- if (Ty == Type::getFloatTy(CPV->getContext())) {
+ if (Ty == Type::getHalfTy(CPV->getContext())) {
+ APInt API = CFP->getValueAPF().bitcastToAPInt();
+ uint16_t float16 = API.getLoBits(16).getZExtValue();
+ ConvertIntToBytes<>(ptr, float16);
+ aggBuffer->addBytes(ptr, 2, Bytes);
+ } else if (Ty == Type::getFloatTy(CPV->getContext())) {
float float32 = (float) CFP->getValueAPF().convertToFloat();
ConvertFloatToBytes(ptr, float32);
aggBuffer->addBytes(ptr, 4, Bytes);
@@ -2049,65 +1965,6 @@ void NVPTXAsmPrinter::bufferAggregateConstant(const Constant *CPV,
llvm_unreachable("unsupported constant type in printAggregateConstant()");
}
-// buildTypeNameMap - Run through symbol table looking for type names.
-//
-
-bool NVPTXAsmPrinter::ignoreLoc(const MachineInstr &MI) {
- switch (MI.getOpcode()) {
- default:
- return false;
- case NVPTX::CallArgBeginInst:
- case NVPTX::CallArgEndInst0:
- case NVPTX::CallArgEndInst1:
- case NVPTX::CallArgF32:
- case NVPTX::CallArgF64:
- case NVPTX::CallArgI16:
- case NVPTX::CallArgI32:
- case NVPTX::CallArgI32imm:
- case NVPTX::CallArgI64:
- case NVPTX::CallArgParam:
- case NVPTX::CallVoidInst:
- case NVPTX::CallVoidInstReg:
- case NVPTX::Callseq_End:
- case NVPTX::CallVoidInstReg64:
- case NVPTX::DeclareParamInst:
- case NVPTX::DeclareRetMemInst:
- case NVPTX::DeclareRetRegInst:
- case NVPTX::DeclareRetScalarInst:
- case NVPTX::DeclareScalarParamInst:
- case NVPTX::DeclareScalarRegInst:
- case NVPTX::StoreParamF32:
- case NVPTX::StoreParamF64:
- case NVPTX::StoreParamI16:
- case NVPTX::StoreParamI32:
- case NVPTX::StoreParamI64:
- case NVPTX::StoreParamI8:
- case NVPTX::StoreRetvalF32:
- case NVPTX::StoreRetvalF64:
- case NVPTX::StoreRetvalI16:
- case NVPTX::StoreRetvalI32:
- case NVPTX::StoreRetvalI64:
- case NVPTX::StoreRetvalI8:
- case NVPTX::LastCallArgF32:
- case NVPTX::LastCallArgF64:
- case NVPTX::LastCallArgI16:
- case NVPTX::LastCallArgI32:
- case NVPTX::LastCallArgI32imm:
- case NVPTX::LastCallArgI64:
- case NVPTX::LastCallArgParam:
- case NVPTX::LoadParamMemF32:
- case NVPTX::LoadParamMemF64:
- case NVPTX::LoadParamMemI16:
- case NVPTX::LoadParamMemI32:
- case NVPTX::LoadParamMemI64:
- case NVPTX::LoadParamMemI8:
- case NVPTX::PrototypeInst:
- case NVPTX::DBG_VALUE:
- return true;
- }
- return false;
-}
-
/// lowerConstantForGV - Return an MCExpr for the given Constant. This is mostly
/// a copy from AsmPrinter::lowerConstant, except customized to only handle
/// expressions that are representable in PTX and create
@@ -2408,44 +2265,6 @@ void NVPTXAsmPrinter::printMemOperand(const MachineInstr *MI, int opNum,
}
}
-void NVPTXAsmPrinter::emitSrcInText(StringRef filename, unsigned line) {
- std::stringstream temp;
- LineReader *reader = this->getReader(filename);
- temp << "\n//";
- temp << filename.str();
- temp << ":";
- temp << line;
- temp << " ";
- temp << reader->readLine(line);
- temp << "\n";
- this->OutStreamer->EmitRawText(temp.str());
-}
-
-LineReader *NVPTXAsmPrinter::getReader(const std::string &filename) {
- if (!reader) {
- reader = new LineReader(filename);
- }
-
- if (reader->fileName() != filename) {
- delete reader;
- reader = new LineReader(filename);
- }
-
- return reader;
-}
-
-std::string LineReader::readLine(unsigned lineNum) {
- if (lineNum < theCurLine) {
- theCurLine = 0;
- fstr.seekg(0, std::ios::beg);
- }
- while (theCurLine < lineNum) {
- fstr.getline(buff, 500);
- theCurLine++;
- }
- return buff;
-}
-
// Force static initialization.
extern "C" void LLVMInitializeNVPTXAsmPrinter() {
RegisterAsmPrinter<NVPTXAsmPrinter> X(getTheNVPTXTarget32());