mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-01-03 13:31:05 +00:00
[NVPTX] Add support for addrspacecast in global variable initializers, including emitting generic() when casting to address space 0.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@205906 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
ff7dcc527f
commit
ac4c131de6
@ -152,6 +152,21 @@ const MCExpr *nvptx::LowerConstant(const Constant *CV, AsmPrinter &AP) {
|
|||||||
!AP.MF ? 0 : AP.MF->getFunction()->getParent());
|
!AP.MF ? 0 : AP.MF->getFunction()->getParent());
|
||||||
report_fatal_error(OS.str());
|
report_fatal_error(OS.str());
|
||||||
}
|
}
|
||||||
|
case Instruction::AddrSpaceCast: {
|
||||||
|
// Strip any addrspace(1)->addrspace(0) addrspace casts. These will be
|
||||||
|
// handled by the generic() logic in the MCExpr printer
|
||||||
|
PointerType *DstTy = cast<PointerType>(CE->getType());
|
||||||
|
PointerType *SrcTy = cast<PointerType>(CE->getOperand(0)->getType());
|
||||||
|
if (SrcTy->getAddressSpace() == 1 && DstTy->getAddressSpace() == 0) {
|
||||||
|
return LowerConstant(cast<const Constant>(CE->getOperand(0)), AP);
|
||||||
|
}
|
||||||
|
std::string S;
|
||||||
|
raw_string_ostream OS(S);
|
||||||
|
OS << "Unsupported expression in static initializer: ";
|
||||||
|
CE->printAsOperand(OS, /*PrintType=*/ false,
|
||||||
|
!AP.MF ? 0 : AP.MF->getFunction()->getParent());
|
||||||
|
report_fatal_error(OS.str());
|
||||||
|
}
|
||||||
case Instruction::GetElementPtr: {
|
case Instruction::GetElementPtr: {
|
||||||
const DataLayout &TD = *AP.TM.getDataLayout();
|
const DataLayout &TD = *AP.TM.getDataLayout();
|
||||||
// Generate a symbolic expression for the byte address
|
// Generate a symbolic expression for the byte address
|
||||||
@ -1754,13 +1769,35 @@ void NVPTXAsmPrinter::printScalarConstant(const Constant *CPV, raw_ostream &O) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) {
|
if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) {
|
||||||
|
PointerType *PTy = dyn_cast<PointerType>(GVar->getType());
|
||||||
|
bool IsNonGenericPointer = false;
|
||||||
|
if (PTy && PTy->getAddressSpace() != 0) {
|
||||||
|
IsNonGenericPointer = true;
|
||||||
|
}
|
||||||
|
if (EmitGeneric && !isa<Function>(CPV) && !IsNonGenericPointer) {
|
||||||
|
O << "generic(";
|
||||||
O << *getSymbol(GVar);
|
O << *getSymbol(GVar);
|
||||||
|
O << ")";
|
||||||
|
} else {
|
||||||
|
O << *getSymbol(GVar);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
|
if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
|
||||||
const Value *v = Cexpr->stripPointerCasts();
|
const Value *v = Cexpr->stripPointerCasts();
|
||||||
|
PointerType *PTy = dyn_cast<PointerType>(Cexpr->getType());
|
||||||
|
bool IsNonGenericPointer = false;
|
||||||
|
if (PTy && PTy->getAddressSpace() != 0) {
|
||||||
|
IsNonGenericPointer = true;
|
||||||
|
}
|
||||||
if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
|
if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
|
||||||
|
if (EmitGeneric && !isa<Function>(v) && !IsNonGenericPointer) {
|
||||||
|
O << "generic(";
|
||||||
O << *getSymbol(GVar);
|
O << *getSymbol(GVar);
|
||||||
|
O << ")";
|
||||||
|
} else {
|
||||||
|
O << *getSymbol(GVar);
|
||||||
|
}
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
O << *LowerConstant(CPV, *this);
|
O << *LowerConstant(CPV, *this);
|
||||||
|
@ -96,6 +96,7 @@ class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter {
|
|||||||
unsigned curpos;
|
unsigned curpos;
|
||||||
raw_ostream &O;
|
raw_ostream &O;
|
||||||
NVPTXAsmPrinter &AP;
|
NVPTXAsmPrinter &AP;
|
||||||
|
bool EmitGeneric;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
AggBuffer(unsigned _size, raw_ostream &_O, NVPTXAsmPrinter &_AP)
|
AggBuffer(unsigned _size, raw_ostream &_O, NVPTXAsmPrinter &_AP)
|
||||||
@ -104,6 +105,7 @@ class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter {
|
|||||||
size = _size;
|
size = _size;
|
||||||
curpos = 0;
|
curpos = 0;
|
||||||
numSymbols = 0;
|
numSymbols = 0;
|
||||||
|
EmitGeneric = AP.EmitGeneric;
|
||||||
}
|
}
|
||||||
~AggBuffer() { delete[] buffer; }
|
~AggBuffer() { delete[] buffer; }
|
||||||
unsigned addBytes(unsigned char *Ptr, int Num, int Bytes) {
|
unsigned addBytes(unsigned char *Ptr, int Num, int Bytes) {
|
||||||
@ -155,7 +157,18 @@ class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter {
|
|||||||
const Value *v = Symbols[nSym];
|
const Value *v = Symbols[nSym];
|
||||||
if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
|
if (const GlobalValue *GVar = dyn_cast<GlobalValue>(v)) {
|
||||||
MCSymbol *Name = AP.getSymbol(GVar);
|
MCSymbol *Name = AP.getSymbol(GVar);
|
||||||
|
PointerType *PTy = dyn_cast<PointerType>(GVar->getType());
|
||||||
|
bool IsNonGenericPointer = false;
|
||||||
|
if (PTy && PTy->getAddressSpace() != 0) {
|
||||||
|
IsNonGenericPointer = true;
|
||||||
|
}
|
||||||
|
if (EmitGeneric && !isa<Function>(v) && !IsNonGenericPointer) {
|
||||||
|
O << "generic(";
|
||||||
O << *Name;
|
O << *Name;
|
||||||
|
O << ")";
|
||||||
|
} else {
|
||||||
|
O << *Name;
|
||||||
|
}
|
||||||
} else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(v)) {
|
} else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(v)) {
|
||||||
O << *nvptx::LowerConstant(Cexpr, AP);
|
O << *nvptx::LowerConstant(Cexpr, AP);
|
||||||
} else
|
} else
|
||||||
@ -276,12 +289,27 @@ private:
|
|||||||
|
|
||||||
LineReader *reader;
|
LineReader *reader;
|
||||||
LineReader *getReader(std::string);
|
LineReader *getReader(std::string);
|
||||||
|
|
||||||
|
// Used to control the need to emit .generic() in the initializer of
|
||||||
|
// module scope variables.
|
||||||
|
// Although ptx supports the hybrid mode like the following,
|
||||||
|
// .global .u32 a;
|
||||||
|
// .global .u32 b;
|
||||||
|
// .global .u32 addr[] = {a, generic(b)}
|
||||||
|
// we have difficulty representing the difference in the NVVM IR.
|
||||||
|
//
|
||||||
|
// Since the address value should always be generic in CUDA C and always
|
||||||
|
// be specific in OpenCL, we use this simple control here.
|
||||||
|
//
|
||||||
|
bool EmitGeneric;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
NVPTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
|
NVPTXAsmPrinter(TargetMachine &TM, MCStreamer &Streamer)
|
||||||
: AsmPrinter(TM, Streamer),
|
: AsmPrinter(TM, Streamer),
|
||||||
nvptxSubtarget(TM.getSubtarget<NVPTXSubtarget>()) {
|
nvptxSubtarget(TM.getSubtarget<NVPTXSubtarget>()) {
|
||||||
CurrentBankselLabelInBasicBlock = "";
|
CurrentBankselLabelInBasicBlock = "";
|
||||||
reader = NULL;
|
reader = NULL;
|
||||||
|
EmitGeneric = (nvptxSubtarget.getDrvInterface() == NVPTX::CUDA);
|
||||||
}
|
}
|
||||||
|
|
||||||
~NVPTXAsmPrinter() {
|
~NVPTXAsmPrinter() {
|
||||||
|
9
test/CodeGen/NVPTX/addrspacecast-gvar.ll
Normal file
9
test/CodeGen/NVPTX/addrspacecast-gvar.ll
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
; RUN: llc < %s -march=nvptx -mcpu=sm_20 | FileCheck %s
|
||||||
|
|
||||||
|
; CHECK: .visible .global .align 4 .u32 g = 42;
|
||||||
|
; CHECK: .visible .global .align 4 .u32 g2 = generic(g);
|
||||||
|
; CHECK: .visible .global .align 4 .u32 g3 = g;
|
||||||
|
|
||||||
|
@g = addrspace(1) global i32 42
|
||||||
|
@g2 = addrspace(1) global i32* addrspacecast (i32 addrspace(1)* @g to i32*)
|
||||||
|
@g3 = addrspace(1) global i32 addrspace(1)* @g
|
Loading…
Reference in New Issue
Block a user