llvm.org GIT mirror llvm / 8e8c4b5
The constant initialization for globals in NVPTX is generated as an array of bytes. The generation of this byte arrays was expecting the host to be little endian, which prevents big endian hosts to be used in the generation of the PTX code. This patch fixes the problem by changing the way the bytes are extracted so that it works for either little and big endian. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@239412 91177308-0d34-0410-b5e6-96231b3b80d8 Samuel Antao 5 years ago
2 changed file(s) with 56 addition(s) and 9 deletion(s). Raw diff Collapse all Expand all
17591759 llvm_unreachable("Not scalar type found in printScalarConstant()");
17601760 }
17611761
1762 // These utility functions assure we get the right sequence of bytes for a given
1763 // type even for big-endian machines
1764 template static void ConvertIntToBytes(unsigned char *p, T val) {
1765 int64_t vp = (int64_t)val;
1766 for (unsigned i = 0; i < sizeof(T); ++i) {
1767 p[i] = (unsigned char)vp;
1768 vp >>= 8;
1769 }
1770 }
1771 static void ConvertFloatToBytes(unsigned char *p, float val) {
1772 int32_t *vp = (int32_t *)&val;
1773 for (unsigned i = 0; i < sizeof(int32_t); ++i) {
1774 p[i] = (unsigned char)*vp;
1775 *vp >>= 8;
1776 }
1777 }
1778 static void ConvertDoubleToBytes(unsigned char *p, double val) {
1779 int64_t *vp = (int64_t *)&val;
1780 for (unsigned i = 0; i < sizeof(int64_t); ++i) {
1781 p[i] = (unsigned char)*vp;
1782 *vp >>= 8;
1783 }
1784 }
1785
17621786 void NVPTXAsmPrinter::bufferLEByte(const Constant *CPV, int Bytes,
17631787 AggBuffer *aggBuffer) {
17641788
17721796 return;
17731797 }
17741798
1775 unsigned char *ptr;
1799 unsigned char ptr[8];
17761800 switch (CPV->getType()->getTypeID()) {
17771801
17781802 case Type::IntegerTyID: {
17791803 const Type *ETy = CPV->getType();
17801804 if (ETy == Type::getInt8Ty(CPV->getContext())) {
17811805 unsigned char c = (unsigned char)cast(CPV)->getZExtValue();
1782 ptr = &c;
1806 ConvertIntToBytes<>(ptr, c);
17831807 aggBuffer->addBytes(ptr, 1, Bytes);
17841808 } else if (ETy == Type::getInt16Ty(CPV->getContext())) {
17851809 short int16 = (short)cast(CPV)->getZExtValue();
1786 ptr = (unsigned char *)&int16;
1810 ConvertIntToBytes<>(ptr, int16);
17871811 aggBuffer->addBytes(ptr, 2, Bytes);
17881812 } else if (ETy == Type::getInt32Ty(CPV->getContext())) {
17891813 if (const ConstantInt *constInt = dyn_cast(CPV)) {
17901814 int int32 = (int)(constInt->getZExtValue());
1791 ptr = (unsigned char *)&int32;
1815 ConvertIntToBytes<>(ptr, int32);
17921816 aggBuffer->addBytes(ptr, 4, Bytes);
17931817 break;
17941818 } else if (const ConstantExpr *Cexpr = dyn_cast(CPV)) {
17951819 if (const ConstantInt *constInt = dyn_cast(
17961820 ConstantFoldConstantExpression(Cexpr, *TD))) {
17971821 int int32 = (int)(constInt->getZExtValue());
1798 ptr = (unsigned char *)&int32;
1822 ConvertIntToBytes<>(ptr, int32);
17991823 aggBuffer->addBytes(ptr, 4, Bytes);
18001824 break;
18011825 }
18101834 } else if (ETy == Type::getInt64Ty(CPV->getContext())) {
18111835 if (const ConstantInt *constInt = dyn_cast(CPV)) {
18121836 long long int64 = (long long)(constInt->getZExtValue());
1813 ptr = (unsigned char *)&int64;
1837 ConvertIntToBytes<>(ptr, int64);
18141838 aggBuffer->addBytes(ptr, 8, Bytes);
18151839 break;
18161840 } else if (const ConstantExpr *Cexpr = dyn_cast(CPV)) {
18171841 if (const ConstantInt *constInt = dyn_cast(
18181842 ConstantFoldConstantExpression(Cexpr, *TD))) {
18191843 long long int64 = (long long)(constInt->getZExtValue());
1820 ptr = (unsigned char *)&int64;
1844 ConvertIntToBytes<>(ptr, int64);
18211845 aggBuffer->addBytes(ptr, 8, Bytes);
18221846 break;
18231847 }
18391863 const Type *Ty = CFP->getType();
18401864 if (Ty == Type::getFloatTy(CPV->getContext())) {
18411865 float float32 = (float) CFP->getValueAPF().convertToFloat();
1842 ptr = (unsigned char *)&float32;
1866 ConvertFloatToBytes(ptr, float32);
18431867 aggBuffer->addBytes(ptr, 4, Bytes);
18441868 } else if (Ty == Type::getDoubleTy(CPV->getContext())) {
18451869 double float64 = CFP->getValueAPF().convertToDouble();
1846 ptr = (unsigned char *)&float64;
1870 ConvertDoubleToBytes(ptr, float64);
18471871 aggBuffer->addBytes(ptr, 8, Bytes);
18481872 } else {
18491873 llvm_unreachable("unsupported fp const type");
0 ; RUN: llc < %s -march=nvptx -mcpu=sm_20 | FileCheck %s
1
2 ; Make sure the globals constant initializers are not prone to host endianess
3 ; issues.
4
5 ; CHECK-DAG: .b8 Gbli08[2] = {171, 205};
6 @Gbli08 = global [2 x i8] [i8 171, i8 205]
7
8 ; CHECK-DAG: .b8 Gbli16[4] = {205, 171, 1, 239};
9 @Gbli16 = global [2 x i16] [i16 43981, i16 61185]
10
11 ; CHECK-DAG: .b8 Gbli32[8] = {1, 239, 205, 171, 137, 103, 69, 35};
12 @Gbli32 = global [2 x i32] [i32 2882400001, i32 591751049]
13
14 ; CHECK-DAG: .b8 Gbli64[16] = {137, 103, 69, 35, 1, 239, 205, 171, 239, 205, 171, 137, 103, 69, 35, 1};
15 @Gbli64 = global [2 x i64] [i64 12379813738877118345, i64 81985529216486895]
16
17 ; CHECK-DAG: .b8 Gblf32[8] = {192, 225, 100, 75, 0, 96, 106, 69};
18 @Gblf32 = global [2 x float] [float 1.5e+7, float 3.75e+3]
19
20 ; CHECK-DAG: .b8 Gblf64[16] = {116, 10, 181, 48, 134, 62, 230, 58, 106, 222, 138, 98, 204, 250, 200, 75};
21 @Gblf64 = global [2 x double] [double 5.75e-25, double 12.25e+56]
22