1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 18:54:02 +01:00

[NVPTX] do not rely on cached subtarget info.

If a module has function references, but no functions
themselves, we may end up never calling runOnMachineFunction
and therefore would never initialize nvptxSubtarget field
which would eventually cause a crash.

Instead of relying on nvptxSubtarget being initialized by
one of the methods, retrieve subtarget info directly.

Differential Revision: https://reviews.llvm.org/D55580

llvm-svn: 348952
This commit is contained in:
Artem Belevich 2018-12-12 18:31:04 +00:00
parent b72e26000d
commit f4a6455e0c
3 changed files with 29 additions and 13 deletions

View File

@ -219,11 +219,12 @@ void NVPTXAsmPrinter::lowerToMCInst(const MachineInstr *MI, MCInst &OutMI) {
return; return;
} }
const NVPTXSubtarget &STI = MI->getMF()->getSubtarget<NVPTXSubtarget>();
for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
const MachineOperand &MO = MI->getOperand(i); const MachineOperand &MO = MI->getOperand(i);
MCOperand MCOp; MCOperand MCOp;
if (!nvptxSubtarget->hasImageHandles()) { if (!STI.hasImageHandles()) {
if (lowerImageHandleOperand(MI, i, MCOp)) { if (lowerImageHandleOperand(MI, i, MCOp)) {
OutMI.addOperand(MCOp); OutMI.addOperand(MCOp);
continue; continue;
@ -329,11 +330,12 @@ MCOperand NVPTXAsmPrinter::GetSymbolRef(const MCSymbol *Symbol) {
void NVPTXAsmPrinter::printReturnValStr(const Function *F, raw_ostream &O) { void NVPTXAsmPrinter::printReturnValStr(const Function *F, raw_ostream &O) {
const DataLayout &DL = getDataLayout(); const DataLayout &DL = getDataLayout();
const TargetLowering *TLI = nvptxSubtarget->getTargetLowering(); const NVPTXSubtarget &STI = TM.getSubtarget<NVPTXSubtarget>(*F);
const TargetLowering *TLI = STI.getTargetLowering();
Type *Ty = F->getReturnType(); Type *Ty = F->getReturnType();
bool isABI = (nvptxSubtarget->getSmVersion() >= 20); bool isABI = (STI.getSmVersion() >= 20);
if (Ty->getTypeID() == Type::VoidTyID) if (Ty->getTypeID() == Type::VoidTyID)
return; return;
@ -474,7 +476,6 @@ void NVPTXAsmPrinter::EmitFunctionEntryLabel() {
} }
bool NVPTXAsmPrinter::runOnMachineFunction(MachineFunction &F) { bool NVPTXAsmPrinter::runOnMachineFunction(MachineFunction &F) {
nvptxSubtarget = &F.getSubtarget<NVPTXSubtarget>();
bool Result = AsmPrinter::runOnMachineFunction(F); bool Result = AsmPrinter::runOnMachineFunction(F);
// Emit closing brace for the body of function F. // Emit closing brace for the body of function F.
// The closing brace must be emitted here because we need to emit additional // The closing brace must be emitted here because we need to emit additional
@ -508,8 +509,9 @@ void NVPTXAsmPrinter::emitImplicitDef(const MachineInstr *MI) const {
OutStreamer->AddComment(Twine("implicit-def: ") + OutStreamer->AddComment(Twine("implicit-def: ") +
getVirtualRegisterName(RegNo)); getVirtualRegisterName(RegNo));
} else { } else {
const NVPTXSubtarget &STI = MI->getMF()->getSubtarget<NVPTXSubtarget>();
OutStreamer->AddComment(Twine("implicit-def: ") + OutStreamer->AddComment(Twine("implicit-def: ") +
nvptxSubtarget->getRegisterInfo()->getName(RegNo)); STI.getRegisterInfo()->getName(RegNo));
} }
OutStreamer->AddBlankLine(); OutStreamer->AddBlankLine();
} }
@ -1431,12 +1433,14 @@ void NVPTXAsmPrinter::printParamName(Function::const_arg_iterator I,
void NVPTXAsmPrinter::emitFunctionParamList(const Function *F, raw_ostream &O) { void NVPTXAsmPrinter::emitFunctionParamList(const Function *F, raw_ostream &O) {
const DataLayout &DL = getDataLayout(); const DataLayout &DL = getDataLayout();
const AttributeList &PAL = F->getAttributes(); const AttributeList &PAL = F->getAttributes();
const TargetLowering *TLI = nvptxSubtarget->getTargetLowering(); const NVPTXSubtarget &STI = TM.getSubtarget<NVPTXSubtarget>(*F);
const TargetLowering *TLI = STI.getTargetLowering();
Function::const_arg_iterator I, E; Function::const_arg_iterator I, E;
unsigned paramIndex = 0; unsigned paramIndex = 0;
bool first = true; bool first = true;
bool isKernelFunc = isKernelFunction(*F); bool isKernelFunc = isKernelFunction(*F);
bool isABI = (nvptxSubtarget->getSmVersion() >= 20); bool isABI = (STI.getSmVersion() >= 20);
bool hasImageHandles = STI.hasImageHandles();
MVT thePointerTy = TLI->getPointerTy(DL); MVT thePointerTy = TLI->getPointerTy(DL);
if (F->arg_empty()) { if (F->arg_empty()) {
@ -1460,7 +1464,7 @@ void NVPTXAsmPrinter::emitFunctionParamList(const Function *F, raw_ostream &O) {
if (isImage(*I)) { if (isImage(*I)) {
std::string sname = I->getName(); std::string sname = I->getName();
if (isImageWriteOnly(*I) || isImageReadWrite(*I)) { if (isImageWriteOnly(*I) || isImageReadWrite(*I)) {
if (nvptxSubtarget->hasImageHandles()) if (hasImageHandles)
O << "\t.param .u64 .ptr .surfref "; O << "\t.param .u64 .ptr .surfref ";
else else
O << "\t.param .surfref "; O << "\t.param .surfref ";
@ -1468,7 +1472,7 @@ void NVPTXAsmPrinter::emitFunctionParamList(const Function *F, raw_ostream &O) {
O << "_param_" << paramIndex; O << "_param_" << paramIndex;
} }
else { // Default image is read_only else { // Default image is read_only
if (nvptxSubtarget->hasImageHandles()) if (hasImageHandles)
O << "\t.param .u64 .ptr .texref "; O << "\t.param .u64 .ptr .texref ";
else else
O << "\t.param .texref "; O << "\t.param .texref ";
@ -1476,7 +1480,7 @@ void NVPTXAsmPrinter::emitFunctionParamList(const Function *F, raw_ostream &O) {
O << "_param_" << paramIndex; O << "_param_" << paramIndex;
} }
} else { } else {
if (nvptxSubtarget->hasImageHandles()) if (hasImageHandles)
O << "\t.param .u64 .ptr .samplerref "; O << "\t.param .u64 .ptr .samplerref ";
else else
O << "\t.param .samplerref "; O << "\t.param .samplerref ";

View File

@ -258,9 +258,6 @@ private:
typedef DenseMap<const TargetRegisterClass *, VRegMap> VRegRCMap; typedef DenseMap<const TargetRegisterClass *, VRegMap> VRegRCMap;
VRegRCMap VRegMapping; VRegRCMap VRegMapping;
// Cache the subtarget here.
const NVPTXSubtarget *nvptxSubtarget;
// List of variables demoted to a function scope. // List of variables demoted to a function scope.
std::map<const Function *, std::vector<const GlobalVariable *>> localDecls; std::map<const Function *, std::vector<const GlobalVariable *>> localDecls;

View File

@ -0,0 +1,15 @@
; RUN: llc < %s -march=nvptx -mcpu=sm_20 | FileCheck %s
; RUN: llc < %s -march=nvptx64 -mcpu=sm_20 | FileCheck %s
; Test that we don't crash if we're compiling a module with function references,
; but without any functions in it.
target datalayout = "e-i64:64-i128:128-v16:16-v32:32-n16:32:64"
target triple = "nvptx64-nvidia-cuda"
@Funcs = local_unnamed_addr addrspace(1) externally_initialized
global [1 x void (i8*)*] [void (i8*)* @func], align 8
declare void @func(i8*)
; CHECK: Funcs[1] = {func}