From b55e9545f2092b27799a45ed4d4690df5bc1296f Mon Sep 17 00:00:00 2001 From: Jingyue Wu Date: Fri, 24 Apr 2015 02:57:30 +0000 Subject: [PATCH] [NVPTX] Emits "generic()" depending on the original address space Summary: Fixes a bug in the NVPTX codegen. The code used to miss necessary "generic()" on aggregates of addrspacecasts. Test Plan: addrspacecast-gvar.ll Reviewers: eliben, jholewinski Reviewed By: jholewinski Subscribers: jholewinski, llvm-commits Differential Revision: http://reviews.llvm.org/D9130 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@235689 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/NVPTX/NVPTXAsmPrinter.cpp | 10 +++++----- lib/Target/NVPTX/NVPTXAsmPrinter.h | 16 +++++++++++++--- test/CodeGen/NVPTX/addrspacecast-gvar.ll | 2 ++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/Target/NVPTX/NVPTXAsmPrinter.cpp b/lib/Target/NVPTX/NVPTXAsmPrinter.cpp index 9a719644c56..12170a680d4 100644 --- a/lib/Target/NVPTX/NVPTXAsmPrinter.cpp +++ b/lib/Target/NVPTX/NVPTXAsmPrinter.cpp @@ -1179,7 +1179,7 @@ void NVPTXAsmPrinter::printModuleLevelGV(const GlobalVariable *GVar, if ((PTy->getAddressSpace() == llvm::ADDRESS_SPACE_GLOBAL) || (PTy->getAddressSpace() == llvm::ADDRESS_SPACE_CONST)) { const Constant *Initializer = GVar->getInitializer(); - // 'undef' is treated as there is no value spefied. + // 'undef' is treated as there is no value specified. if (!Initializer->isNullValue() && !isa(Initializer)) { O << " = "; printScalarConstant(Initializer, O); @@ -1788,7 +1788,7 @@ void NVPTXAsmPrinter::bufferLEByte(const Constant *CPV, int Bytes, } if (Cexpr->getOpcode() == Instruction::PtrToInt) { Value *v = Cexpr->getOperand(0)->stripPointerCasts(); - aggBuffer->addSymbol(v); + aggBuffer->addSymbol(v, Cexpr->getOperand(0)); aggBuffer->addZeros(4); break; } @@ -1810,7 +1810,7 @@ void NVPTXAsmPrinter::bufferLEByte(const Constant *CPV, int Bytes, } if (Cexpr->getOpcode() == Instruction::PtrToInt) { Value *v = Cexpr->getOperand(0)->stripPointerCasts(); - aggBuffer->addSymbol(v); + aggBuffer->addSymbol(v, Cexpr->getOperand(0)); aggBuffer->addZeros(8); break; } @@ -1839,10 +1839,10 @@ void NVPTXAsmPrinter::bufferLEByte(const Constant *CPV, int Bytes, } case Type::PointerTyID: { if (const GlobalValue *GVar = dyn_cast(CPV)) { - aggBuffer->addSymbol(GVar); + aggBuffer->addSymbol(GVar, GVar); } else if (const ConstantExpr *Cexpr = dyn_cast(CPV)) { const Value *v = Cexpr->stripPointerCasts(); - aggBuffer->addSymbol(v); + aggBuffer->addSymbol(v, Cexpr); } unsigned int s = TD->getTypeAllocSize(CPV->getType()); aggBuffer->addZeros(s); diff --git a/lib/Target/NVPTX/NVPTXAsmPrinter.h b/lib/Target/NVPTX/NVPTXAsmPrinter.h index 9b11e70b2ae..82c5a33834d 100644 --- a/lib/Target/NVPTX/NVPTXAsmPrinter.h +++ b/lib/Target/NVPTX/NVPTXAsmPrinter.h @@ -86,6 +86,14 @@ class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter { std::vector buffer; // the buffer SmallVector symbolPosInBuffer; SmallVector Symbols; + // SymbolsBeforeStripping[i] is the original form of Symbols[i] before + // stripping pointer casts, i.e., + // Symbols[i] == SymbolsBeforeStripping[i]->stripPointerCasts(). + // + // We need to keep these values because AggBuffer::print decides whether to + // emit a "generic()" cast for Symbols[i] depending on the address space of + // SymbolsBeforeStripping[i]. + SmallVector SymbolsBeforeStripping; unsigned curpos; raw_ostream &O; NVPTXAsmPrinter &AP; @@ -119,9 +127,10 @@ class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter { } return curpos; } - void addSymbol(const Value *GVar) { + void addSymbol(const Value *GVar, const Value *GVarBeforeStripping) { symbolPosInBuffer.push_back(curpos); Symbols.push_back(GVar); + SymbolsBeforeStripping.push_back(GVarBeforeStripping); numSymbols++; } void print() { @@ -145,10 +154,11 @@ class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter { O << ", "; if (pos == nextSymbolPos) { const Value *v = Symbols[nSym]; + const Value *v0 = SymbolsBeforeStripping[nSym]; if (const GlobalValue *GVar = dyn_cast(v)) { MCSymbol *Name = AP.getSymbol(GVar); - PointerType *PTy = dyn_cast(GVar->getType()); - bool IsNonGenericPointer = false; + PointerType *PTy = dyn_cast(v0->getType()); + bool IsNonGenericPointer = false; // Is v0 a non-generic pointer? if (PTy && PTy->getAddressSpace() != 0) { IsNonGenericPointer = true; } diff --git a/test/CodeGen/NVPTX/addrspacecast-gvar.ll b/test/CodeGen/NVPTX/addrspacecast-gvar.ll index 6afbdb8a429..b87c05b87ef 100644 --- a/test/CodeGen/NVPTX/addrspacecast-gvar.ll +++ b/test/CodeGen/NVPTX/addrspacecast-gvar.ll @@ -3,7 +3,9 @@ ; 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; +; CHECK: .visible .global .align 8 .u32 g4[2] = {0, generic(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 +@g4 = constant {i32*, i32*} {i32* null, i32* addrspacecast (i32 addrspace(1)* @g to i32*)} -- 2.40.0