Skip to content

Commit d99a83b

Browse files
committed
[NVPTX] Simplify and generalize constant printer.
This allows handling i128 values and fixes https://bugs.llvm.org/show_bug.cgi?id=51789. Differential Revision: https://reviews.llvm.org/D109458
1 parent 367a9e7 commit d99a83b

File tree

3 files changed

+50
-114
lines changed

3 files changed

+50
-114
lines changed

llvm/lib/Target/NVPTX/NVPTXAsmPrinter.cpp

Lines changed: 41 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,148 +1748,75 @@ void NVPTXAsmPrinter::printScalarConstant(const Constant *CPV, raw_ostream &O) {
17481748
llvm_unreachable("Not scalar type found in printScalarConstant()");
17491749
}
17501750

1751-
// These utility functions assure we get the right sequence of bytes for a given
1752-
// type even for big-endian machines
1753-
template <typename T> static void ConvertIntToBytes(unsigned char *p, T val) {
1754-
int64_t vp = (int64_t)val;
1755-
for (unsigned i = 0; i < sizeof(T); ++i) {
1756-
p[i] = (unsigned char)vp;
1757-
vp >>= 8;
1758-
}
1759-
}
1760-
static void ConvertFloatToBytes(unsigned char *p, float val) {
1761-
int32_t *vp = (int32_t *)&val;
1762-
for (unsigned i = 0; i < sizeof(int32_t); ++i) {
1763-
p[i] = (unsigned char)*vp;
1764-
*vp >>= 8;
1765-
}
1766-
}
1767-
static void ConvertDoubleToBytes(unsigned char *p, double val) {
1768-
int64_t *vp = (int64_t *)&val;
1769-
for (unsigned i = 0; i < sizeof(int64_t); ++i) {
1770-
p[i] = (unsigned char)*vp;
1771-
*vp >>= 8;
1772-
}
1773-
}
1774-
17751751
void NVPTXAsmPrinter::bufferLEByte(const Constant *CPV, int Bytes,
1776-
AggBuffer *aggBuffer) {
1752+
AggBuffer *AggBuffer) {
17771753
const DataLayout &DL = getDataLayout();
1778-
1754+
int AllocSize = DL.getTypeAllocSize(CPV->getType());
17791755
if (isa<UndefValue>(CPV) || CPV->isNullValue()) {
1780-
int s = DL.getTypeAllocSize(CPV->getType());
1781-
if (s < Bytes)
1782-
s = Bytes;
1783-
aggBuffer->addZeros(s);
1756+
// Non-zero Bytes indicates that we need to zero-fill everything. Otherwise,
1757+
// only the space allocated by CPV.
1758+
AggBuffer->addZeros(Bytes ? Bytes : AllocSize);
17841759
return;
17851760
}
17861761

1787-
unsigned char ptr[8];
1788-
switch (CPV->getType()->getTypeID()) {
1762+
// Helper for filling AggBuffer with APInts.
1763+
auto AddIntToBuffer = [AggBuffer, Bytes](const APInt &Val) {
1764+
size_t NumBytes = (Val.getBitWidth() + 7) / 8;
1765+
SmallVector<unsigned char, 16> Buf(NumBytes);
1766+
for (unsigned I = 0; I < NumBytes; ++I) {
1767+
Buf[I] = Val.extractBitsAsZExtValue(8, I * 8);
1768+
}
1769+
AggBuffer->addBytes(Buf.data(), NumBytes, Bytes);
1770+
};
17891771

1790-
case Type::IntegerTyID: {
1791-
Type *ETy = CPV->getType();
1792-
if (ETy == Type::getInt8Ty(CPV->getContext())) {
1793-
unsigned char c = (unsigned char)cast<ConstantInt>(CPV)->getZExtValue();
1794-
ConvertIntToBytes<>(ptr, c);
1795-
aggBuffer->addBytes(ptr, 1, Bytes);
1796-
} else if (ETy == Type::getInt16Ty(CPV->getContext())) {
1797-
short int16 = (short)cast<ConstantInt>(CPV)->getZExtValue();
1798-
ConvertIntToBytes<>(ptr, int16);
1799-
aggBuffer->addBytes(ptr, 2, Bytes);
1800-
} else if (ETy == Type::getInt32Ty(CPV->getContext())) {
1801-
if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) {
1802-
int int32 = (int)(constInt->getZExtValue());
1803-
ConvertIntToBytes<>(ptr, int32);
1804-
aggBuffer->addBytes(ptr, 4, Bytes);
1772+
switch (CPV->getType()->getTypeID()) {
1773+
case Type::IntegerTyID:
1774+
if (const auto CI = dyn_cast<ConstantInt>(CPV)) {
1775+
AddIntToBuffer(CI->getValue());
1776+
break;
1777+
}
1778+
if (const auto *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1779+
if (const auto *CI =
1780+
dyn_cast<ConstantInt>(ConstantFoldConstant(Cexpr, DL))) {
1781+
AddIntToBuffer(CI->getValue());
18051782
break;
1806-
} else if (const auto *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1807-
if (const auto *constInt = dyn_cast<ConstantInt>(
1808-
ConstantFoldConstant(Cexpr, DL))) {
1809-
int int32 = (int)(constInt->getZExtValue());
1810-
ConvertIntToBytes<>(ptr, int32);
1811-
aggBuffer->addBytes(ptr, 4, Bytes);
1812-
break;
1813-
}
1814-
if (Cexpr->getOpcode() == Instruction::PtrToInt) {
1815-
Value *v = Cexpr->getOperand(0)->stripPointerCasts();
1816-
aggBuffer->addSymbol(v, Cexpr->getOperand(0));
1817-
aggBuffer->addZeros(4);
1818-
break;
1819-
}
18201783
}
1821-
llvm_unreachable("unsupported integer const type");
1822-
} else if (ETy == Type::getInt64Ty(CPV->getContext())) {
1823-
if (const ConstantInt *constInt = dyn_cast<ConstantInt>(CPV)) {
1824-
long long int64 = (long long)(constInt->getZExtValue());
1825-
ConvertIntToBytes<>(ptr, int64);
1826-
aggBuffer->addBytes(ptr, 8, Bytes);
1784+
if (Cexpr->getOpcode() == Instruction::PtrToInt) {
1785+
Value *V = Cexpr->getOperand(0)->stripPointerCasts();
1786+
AggBuffer->addSymbol(V, Cexpr->getOperand(0));
1787+
AggBuffer->addZeros(AllocSize);
18271788
break;
1828-
} else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
1829-
if (const auto *constInt = dyn_cast<ConstantInt>(
1830-
ConstantFoldConstant(Cexpr, DL))) {
1831-
long long int64 = (long long)(constInt->getZExtValue());
1832-
ConvertIntToBytes<>(ptr, int64);
1833-
aggBuffer->addBytes(ptr, 8, Bytes);
1834-
break;
1835-
}
1836-
if (Cexpr->getOpcode() == Instruction::PtrToInt) {
1837-
Value *v = Cexpr->getOperand(0)->stripPointerCasts();
1838-
aggBuffer->addSymbol(v, Cexpr->getOperand(0));
1839-
aggBuffer->addZeros(8);
1840-
break;
1841-
}
18421789
}
1843-
llvm_unreachable("unsupported integer const type");
1844-
} else
1845-
llvm_unreachable("unsupported integer const type");
1790+
}
1791+
llvm_unreachable("unsupported integer const type");
18461792
break;
1847-
}
1793+
18481794
case Type::HalfTyID:
18491795
case Type::FloatTyID:
1850-
case Type::DoubleTyID: {
1851-
const auto *CFP = cast<ConstantFP>(CPV);
1852-
Type *Ty = CFP->getType();
1853-
if (Ty == Type::getHalfTy(CPV->getContext())) {
1854-
APInt API = CFP->getValueAPF().bitcastToAPInt();
1855-
uint16_t float16 = API.getLoBits(16).getZExtValue();
1856-
ConvertIntToBytes<>(ptr, float16);
1857-
aggBuffer->addBytes(ptr, 2, Bytes);
1858-
} else if (Ty == Type::getFloatTy(CPV->getContext())) {
1859-
float float32 = (float) CFP->getValueAPF().convertToFloat();
1860-
ConvertFloatToBytes(ptr, float32);
1861-
aggBuffer->addBytes(ptr, 4, Bytes);
1862-
} else if (Ty == Type::getDoubleTy(CPV->getContext())) {
1863-
double float64 = CFP->getValueAPF().convertToDouble();
1864-
ConvertDoubleToBytes(ptr, float64);
1865-
aggBuffer->addBytes(ptr, 8, Bytes);
1866-
} else {
1867-
llvm_unreachable("unsupported fp const type");
1868-
}
1796+
case Type::DoubleTyID:
1797+
AddIntToBuffer(cast<ConstantFP>(CPV)->getValueAPF().bitcastToAPInt());
18691798
break;
1870-
}
1799+
18711800
case Type::PointerTyID: {
18721801
if (const GlobalValue *GVar = dyn_cast<GlobalValue>(CPV)) {
1873-
aggBuffer->addSymbol(GVar, GVar);
1802+
AggBuffer->addSymbol(GVar, GVar);
18741803
} else if (const ConstantExpr *Cexpr = dyn_cast<ConstantExpr>(CPV)) {
18751804
const Value *v = Cexpr->stripPointerCasts();
1876-
aggBuffer->addSymbol(v, Cexpr);
1805+
AggBuffer->addSymbol(v, Cexpr);
18771806
}
1878-
unsigned int s = DL.getTypeAllocSize(CPV->getType());
1879-
aggBuffer->addZeros(s);
1807+
AggBuffer->addZeros(AllocSize);
18801808
break;
18811809
}
18821810

18831811
case Type::ArrayTyID:
18841812
case Type::FixedVectorTyID:
18851813
case Type::StructTyID: {
18861814
if (isa<ConstantAggregate>(CPV) || isa<ConstantDataSequential>(CPV)) {
1887-
int ElementSize = DL.getTypeAllocSize(CPV->getType());
1888-
bufferAggregateConstant(CPV, aggBuffer);
1889-
if (Bytes > ElementSize)
1890-
aggBuffer->addZeros(Bytes - ElementSize);
1815+
bufferAggregateConstant(CPV, AggBuffer);
1816+
if (Bytes > AllocSize)
1817+
AggBuffer->addZeros(Bytes - AllocSize);
18911818
} else if (isa<ConstantAggregateZero>(CPV))
1892-
aggBuffer->addZeros(Bytes);
1819+
AggBuffer->addZeros(Bytes);
18931820
else
18941821
llvm_unreachable("Unexpected Constant type");
18951822
break;

llvm/lib/Target/NVPTX/NVPTXAsmPrinter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ class LLVM_LIBRARY_VISIBILITY NVPTXAsmPrinter : public AsmPrinter {
106106
EmitGeneric = AP.EmitGeneric;
107107
}
108108

109+
// Copy Num bytes from Ptr.
110+
// if Bytes > Num, zero fill up to Bytes.
109111
unsigned addBytes(unsigned char *Ptr, int Num, int Bytes) {
110112
assert((curpos + Num) <= size);
111113
assert((curpos + Bytes) <= size);

llvm/test/CodeGen/NVPTX/globals_init.ll

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@
1515
; CHECK-DAG: .b8 Gbli64[16] = {137, 103, 69, 35, 1, 239, 205, 171, 239, 205, 171, 137, 103, 69, 35, 1};
1616
@Gbli64 = global [2 x i64] [i64 12379813738877118345, i64 81985529216486895]
1717

18+
; CHECK-DAG: .b8 Gbli128[32] = {16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
19+
@Gbli128 = global [2 x i128] [i128 1339673755198158349044581307228491536, i128 21345817372864405881847059188222722561]
20+
1821
; CHECK-DAG: .b8 Gblf32[8] = {192, 225, 100, 75, 0, 96, 106, 69};
1922
@Gblf32 = global [2 x float] [float 1.5e+7, float 3.75e+3]
2023

2124
; CHECK-DAG: .b8 Gblf64[16] = {116, 10, 181, 48, 134, 62, 230, 58, 106, 222, 138, 98, 204, 250, 200, 75};
2225
@Gblf64 = global [2 x double] [double 5.75e-25, double 12.25e+56]
2326

27+
; Make sure we fill in alignment gaps correctly.
28+
; CHECK-DAG: .b8 GblU[12] = {7, 6, 0, 0, 5, 4, 3, 2, 1, 0, 0, 0};
29+
@GblU = global {i16, i32, i8} {i16 1543, i32 33752069, i8 1}
30+

0 commit comments

Comments
 (0)