Skip to content

Commit a1d2d35

Browse files
zhoupeng12pzzp
authored andcommitted
[llvm:ir] Add support for constant data exceeding 4GiB
1 parent 161cfc6 commit a1d2d35

File tree

7 files changed

+24
-25
lines changed

7 files changed

+24
-25
lines changed

clang/lib/CodeGen/CGExprConstant.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,14 @@ bool ConstantAggregateBuilder::split(size_t Index, CharUnits Hint) {
364364
// FIXME: If possible, split into two ConstantDataSequentials at Hint.
365365
CharUnits ElemSize = getSize(CDS->getElementType());
366366
replace(Elems, Index, Index + 1,
367-
llvm::map_range(llvm::seq(0u, CDS->getNumElements()),
368-
[&](unsigned Elem) {
367+
llvm::map_range(llvm::seq(uint64_t(0u), CDS->getNumElements()),
368+
[&](uint64_t Elem) {
369369
return CDS->getElementAsConstant(Elem);
370370
}));
371371
replace(Offsets, Index, Index + 1,
372372
llvm::map_range(
373-
llvm::seq(0u, CDS->getNumElements()),
374-
[&](unsigned Elem) { return Offset + Elem * ElemSize; }));
373+
llvm::seq(uint64_t(0u), CDS->getNumElements()),
374+
[&](uint64_t Elem) { return Offset + Elem * ElemSize; }));
375375
return true;
376376
}
377377

llvm/include/llvm/IR/Constants.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -617,15 +617,15 @@ class ConstantDataSequential : public ConstantData {
617617

618618
/// If this is a sequential container of integers (of any size), return the
619619
/// specified element in the low bits of a uint64_t.
620-
uint64_t getElementAsInteger(unsigned i) const;
620+
uint64_t getElementAsInteger(uint64_t i) const;
621621

622622
/// If this is a sequential container of integers (of any size), return the
623623
/// specified element as an APInt.
624624
APInt getElementAsAPInt(unsigned i) const;
625625

626626
/// If this is a sequential container of floating point type, return the
627627
/// specified element as an APFloat.
628-
APFloat getElementAsAPFloat(unsigned i) const;
628+
APFloat getElementAsAPFloat(uint64_t i) const;
629629

630630
/// If this is an sequential container of floats, return the specified element
631631
/// as a float.
@@ -644,7 +644,7 @@ class ConstantDataSequential : public ConstantData {
644644
Type *getElementType() const;
645645

646646
/// Return the number of elements in the array or vector.
647-
unsigned getNumElements() const;
647+
uint64_t getNumElements() const;
648648

649649
/// Return the size (in bytes) of each element in the array/vector.
650650
/// The size of the elements is known to be a multiple of one byte.
@@ -684,7 +684,7 @@ class ConstantDataSequential : public ConstantData {
684684
}
685685

686686
private:
687-
const char *getElementPointer(unsigned Elt) const;
687+
const char *getElementPointer(uint64_t Elt) const;
688688
};
689689

690690
//===----------------------------------------------------------------------===//

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2773,7 +2773,7 @@ void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
27732773
cast<ConstantDataSequential>(C)->isString()) {
27742774
const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
27752775
// Emit constant strings specially.
2776-
unsigned NumElts = Str->getNumElements();
2776+
uint64_t NumElts = Str->getNumElements();
27772777
// If this is a null-terminated string, use the denser CSTRING encoding.
27782778
if (Str->isCString()) {
27792779
Code = bitc::CST_CODE_CSTRING;
@@ -2784,7 +2784,7 @@ void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
27842784
}
27852785
bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
27862786
bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
2787-
for (unsigned i = 0; i != NumElts; ++i) {
2787+
for (uint64_t i = 0; i != NumElts; ++i) {
27882788
unsigned char V = Str->getElementAsInteger(i);
27892789
Record.push_back(V);
27902790
isCStr7 &= (V & 128) == 0;
@@ -2801,10 +2801,10 @@ void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
28012801
Code = bitc::CST_CODE_DATA;
28022802
Type *EltTy = CDS->getElementType();
28032803
if (isa<IntegerType>(EltTy)) {
2804-
for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2804+
for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i)
28052805
Record.push_back(CDS->getElementAsInteger(i));
28062806
} else {
2807-
for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2807+
for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i)
28082808
Record.push_back(
28092809
CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
28102810
}

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3628,9 +3628,9 @@ static void emitGlobalConstantDataSequential(
36283628
return AP.OutStreamer->emitBytes(CDS->getAsString());
36293629

36303630
// Otherwise, emit the values in successive locations.
3631-
unsigned ElementByteSize = CDS->getElementByteSize();
3631+
uint64_t ElementByteSize = CDS->getElementByteSize();
36323632
if (isa<IntegerType>(CDS->getElementType())) {
3633-
for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) {
3633+
for (uint64_t I = 0, E = CDS->getNumElements(); I != E; ++I) {
36343634
emitGlobalAliasInline(AP, ElementByteSize * I, AliasList);
36353635
if (AP.isVerbose())
36363636
AP.OutStreamer->getCommentOS()
@@ -3640,7 +3640,7 @@ static void emitGlobalConstantDataSequential(
36403640
}
36413641
} else {
36423642
Type *ET = CDS->getElementType();
3643-
for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) {
3643+
for (uint64_t I = 0, E = CDS->getNumElements(); I != E; ++I) {
36443644
emitGlobalAliasInline(AP, ElementByteSize * I, AliasList);
36453645
emitGlobalConstantFP(CDS->getElementAsAPFloat(I), ET, AP);
36463646
}

llvm/lib/IR/Constants.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2855,21 +2855,20 @@ bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) {
28552855
return false;
28562856
}
28572857

2858-
unsigned ConstantDataSequential::getNumElements() const {
2858+
uint64_t ConstantDataSequential::getNumElements() const {
28592859
if (ArrayType *AT = dyn_cast<ArrayType>(getType()))
28602860
return AT->getNumElements();
28612861
return cast<FixedVectorType>(getType())->getNumElements();
28622862
}
28632863

2864-
28652864
uint64_t ConstantDataSequential::getElementByteSize() const {
2866-
return getElementType()->getPrimitiveSizeInBits()/8;
2865+
return getElementType()->getPrimitiveSizeInBits() / 8;
28672866
}
28682867

28692868
/// Return the start of the specified element.
2870-
const char *ConstantDataSequential::getElementPointer(unsigned Elt) const {
2869+
const char *ConstantDataSequential::getElementPointer(uint64_t Elt) const {
28712870
assert(Elt < getNumElements() && "Invalid Elt");
2872-
return DataElements+Elt*getElementByteSize();
2871+
return DataElements + Elt * getElementByteSize();
28732872
}
28742873

28752874

@@ -3112,7 +3111,7 @@ Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) {
31123111
}
31133112

31143113

3115-
uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const {
3114+
uint64_t ConstantDataSequential::getElementAsInteger(uint64_t Elt) const {
31163115
assert(isa<IntegerType>(getElementType()) &&
31173116
"Accessor can only be used when element is an integer");
31183117
const char *EltPtr = getElementPointer(Elt);
@@ -3160,7 +3159,7 @@ APInt ConstantDataSequential::getElementAsAPInt(unsigned Elt) const {
31603159
}
31613160
}
31623161

3163-
APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const {
3162+
APFloat ConstantDataSequential::getElementAsAPFloat(uint64_t Elt) const {
31643163
const char *EltPtr = getElementPointer(Elt);
31653164

31663165
switch (getElementType()->getTypeID()) {

llvm/lib/Target/TargetLoweringObjectFile.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@ static bool isSuitableForBSS(const GlobalVariable *GV) {
104104
static bool IsNullTerminatedString(const Constant *C) {
105105
// First check: is we have constant array terminated with zero
106106
if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) {
107-
unsigned NumElts = CDS->getNumElements();
107+
uint64_t NumElts = CDS->getNumElements();
108108
assert(NumElts != 0 && "Can't have an empty CDS");
109109

110110
if (CDS->getElementAsInteger(NumElts-1) != 0)
111111
return false; // Not null terminated.
112112

113113
// Verify that the null doesn't occur anywhere else in the string.
114-
for (unsigned i = 0; i != NumElts-1; ++i)
114+
for (uint64_t i = 0; i != NumElts - 1; ++i)
115115
if (CDS->getElementAsInteger(i) == 0)
116116
return false;
117117
return true;

llvm/lib/Target/X86/X86MCInstLower.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1583,7 +1583,7 @@ static void printConstant(const Constant *COp, unsigned BitWidth,
15831583
bool IsInteger = EltTy->isIntegerTy();
15841584
bool IsFP = EltTy->isHalfTy() || EltTy->isFloatTy() || EltTy->isDoubleTy();
15851585
unsigned EltBits = EltTy->getPrimitiveSizeInBits();
1586-
unsigned E = std::min(BitWidth / EltBits, CDS->getNumElements());
1586+
unsigned E = std::min(BitWidth / EltBits, (unsigned)CDS->getNumElements());
15871587
assert((BitWidth % EltBits) == 0 && "Element size mismatch");
15881588
for (unsigned I = 0; I != E; ++I) {
15891589
if (I != 0)

0 commit comments

Comments
 (0)