Skip to content

Commit 82cb466

Browse files
committed
[llvm:ir] Add support for constant data exceeding 4GiB
1 parent 2639dea commit 82cb466

File tree

11 files changed

+36
-37
lines changed

11 files changed

+36
-37
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: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -617,34 +617,34 @@ 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.
624-
APInt getElementAsAPInt(unsigned i) const;
624+
APInt getElementAsAPInt(uint64_t 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.
632-
float getElementAsFloat(unsigned i) const;
632+
float getElementAsFloat(uint64_t i) const;
633633

634634
/// If this is an sequential container of doubles, return the specified
635635
/// element as a double.
636-
double getElementAsDouble(unsigned i) const;
636+
double getElementAsDouble(uint64_t i) const;
637637

638638
/// Return a Constant for a specified index's element.
639639
/// Note that this has to compute a new constant to return, so it isn't as
640640
/// efficient as getElementAsInteger/Float/Double.
641-
Constant *getElementAsConstant(unsigned i) const;
641+
Constant *getElementAsConstant(uint64_t i) const;
642642

643643
/// Return the element type of the array/vector.
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/Analysis/TargetTransformInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ TargetTransformInfo::getOperandInfo(const Value *V) {
918918
} else if (const auto *CDS = dyn_cast<ConstantDataSequential>(V)) {
919919
OpInfo = OK_NonUniformConstantValue;
920920
bool AllPow2 = true, AllNegPow2 = true;
921-
for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) {
921+
for (uint64_t I = 0, E = CDS->getNumElements(); I != E; ++I) {
922922
if (auto *CI = dyn_cast<ConstantInt>(CDS->getElementAsConstant(I))) {
923923
AllPow2 &= CI->getValue().isPowerOf2();
924924
AllNegPow2 &= CI->getValue().isNegatedPowerOf2();

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6329,7 +6329,7 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) {
63296329

63306330
if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) {
63316331
Value *Val = UndefInt8;
6332-
for (unsigned I = 0, E = CA->getNumElements(); I != E; ++I)
6332+
for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
63336333
if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
63346334
return nullptr;
63356335
return Val;

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2803,7 +2803,7 @@ void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
28032803
cast<ConstantDataSequential>(C)->isString()) {
28042804
const ConstantDataSequential *Str = cast<ConstantDataSequential>(C);
28052805
// Emit constant strings specially.
2806-
unsigned NumElts = Str->getNumElements();
2806+
uint64_t NumElts = Str->getNumElements();
28072807
// If this is a null-terminated string, use the denser CSTRING encoding.
28082808
if (Str->isCString()) {
28092809
Code = bitc::CST_CODE_CSTRING;
@@ -2814,7 +2814,7 @@ void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
28142814
}
28152815
bool isCStr7 = Code == bitc::CST_CODE_CSTRING;
28162816
bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING;
2817-
for (unsigned i = 0; i != NumElts; ++i) {
2817+
for (uint64_t i = 0; i != NumElts; ++i) {
28182818
unsigned char V = Str->getElementAsInteger(i);
28192819
Record.push_back(V);
28202820
isCStr7 &= (V & 128) == 0;
@@ -2831,10 +2831,10 @@ void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal,
28312831
Code = bitc::CST_CODE_DATA;
28322832
Type *EltTy = CDS->getElementType();
28332833
if (isa<IntegerType>(EltTy)) {
2834-
for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2834+
for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i)
28352835
Record.push_back(CDS->getElementAsInteger(i));
28362836
} else {
2837-
for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i)
2837+
for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i)
28382838
Record.push_back(
28392839
CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue());
28402840
}

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp

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

36283628
// Otherwise, emit the values in successive locations.
3629-
unsigned ElementByteSize = CDS->getElementByteSize();
3629+
uint64_t ElementByteSize = CDS->getElementByteSize();
36303630
if (isa<IntegerType>(CDS->getElementType())) {
3631-
for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) {
3631+
for (uint64_t I = 0, E = CDS->getNumElements(); I != E; ++I) {
36323632
emitGlobalAliasInline(AP, ElementByteSize * I, AliasList);
36333633
if (AP.isVerbose())
36343634
AP.OutStreamer->getCommentOS()
@@ -3638,7 +3638,7 @@ static void emitGlobalConstantDataSequential(
36383638
}
36393639
} else {
36403640
Type *ET = CDS->getElementType();
3641-
for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) {
3641+
for (uint64_t I = 0, E = CDS->getNumElements(); I != E; ++I) {
36423642
emitGlobalAliasInline(AP, ElementByteSize * I, AliasList);
36433643
emitGlobalConstantFP(CDS->getElementAsAPFloat(I), ET, AP);
36443644
}

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1845,7 +1845,7 @@ SDValue SelectionDAGBuilder::getValueImpl(const Value *V) {
18451845
if (const ConstantDataSequential *CDS =
18461846
dyn_cast<ConstantDataSequential>(C)) {
18471847
SmallVector<SDValue, 4> Ops;
1848-
for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
1848+
for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i) {
18491849
SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode();
18501850
// Add each leaf value from the operand to the Constants list
18511851
// to form a flattened list of all the values.

llvm/lib/IR/AsmWriter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1658,7 +1658,7 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
16581658
WriterCtx.TypePrinter->print(ETy, Out);
16591659
Out << ' ';
16601660
WriteAsOperandInternal(Out, CA->getElementAsConstant(0), WriterCtx);
1661-
for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) {
1661+
for (uint64_t i = 1, e = CA->getNumElements(); i != e; ++i) {
16621662
Out << ", ";
16631663
WriterCtx.TypePrinter->print(ETy, Out);
16641664
Out << ' ';

llvm/lib/IR/Constants.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2848,21 +2848,20 @@ bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) {
28482848
return false;
28492849
}
28502850

2851-
unsigned ConstantDataSequential::getNumElements() const {
2851+
uint64_t ConstantDataSequential::getNumElements() const {
28522852
if (ArrayType *AT = dyn_cast<ArrayType>(getType()))
28532853
return AT->getNumElements();
28542854
return cast<FixedVectorType>(getType())->getNumElements();
28552855
}
28562856

2857-
28582857
uint64_t ConstantDataSequential::getElementByteSize() const {
2859-
return getElementType()->getPrimitiveSizeInBits()/8;
2858+
return getElementType()->getPrimitiveSizeInBits() / 8;
28602859
}
28612860

28622861
/// Return the start of the specified element.
2863-
const char *ConstantDataSequential::getElementPointer(unsigned Elt) const {
2862+
const char *ConstantDataSequential::getElementPointer(uint64_t Elt) const {
28642863
assert(Elt < getNumElements() && "Invalid Elt");
2865-
return DataElements+Elt*getElementByteSize();
2864+
return DataElements + Elt * getElementByteSize();
28662865
}
28672866

28682867

@@ -3105,7 +3104,7 @@ Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) {
31053104
}
31063105

31073106

3108-
uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const {
3107+
uint64_t ConstantDataSequential::getElementAsInteger(uint64_t Elt) const {
31093108
assert(isa<IntegerType>(getElementType()) &&
31103109
"Accessor can only be used when element is an integer");
31113110
const char *EltPtr = getElementPointer(Elt);
@@ -3125,7 +3124,7 @@ uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const {
31253124
}
31263125
}
31273126

3128-
APInt ConstantDataSequential::getElementAsAPInt(unsigned Elt) const {
3127+
APInt ConstantDataSequential::getElementAsAPInt(uint64_t Elt) const {
31293128
assert(isa<IntegerType>(getElementType()) &&
31303129
"Accessor can only be used when element is an integer");
31313130
const char *EltPtr = getElementPointer(Elt);
@@ -3153,7 +3152,7 @@ APInt ConstantDataSequential::getElementAsAPInt(unsigned Elt) const {
31533152
}
31543153
}
31553154

3156-
APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const {
3155+
APFloat ConstantDataSequential::getElementAsAPFloat(uint64_t Elt) const {
31573156
const char *EltPtr = getElementPointer(Elt);
31583157

31593158
switch (getElementType()->getTypeID()) {
@@ -3178,19 +3177,19 @@ APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const {
31783177
}
31793178
}
31803179

3181-
float ConstantDataSequential::getElementAsFloat(unsigned Elt) const {
3180+
float ConstantDataSequential::getElementAsFloat(uint64_t Elt) const {
31823181
assert(getElementType()->isFloatTy() &&
31833182
"Accessor can only be used when element is a 'float'");
31843183
return *reinterpret_cast<const float *>(getElementPointer(Elt));
31853184
}
31863185

3187-
double ConstantDataSequential::getElementAsDouble(unsigned Elt) const {
3186+
double ConstantDataSequential::getElementAsDouble(uint64_t Elt) const {
31883187
assert(getElementType()->isDoubleTy() &&
31893188
"Accessor can only be used when element is a 'float'");
31903189
return *reinterpret_cast<const double *>(getElementPointer(Elt));
31913190
}
31923191

3193-
Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const {
3192+
Constant *ConstantDataSequential::getElementAsConstant(uint64_t Elt) const {
31943193
if (getElementType()->isHalfTy() || getElementType()->isBFloatTy() ||
31953194
getElementType()->isFloatTy() || getElementType()->isDoubleTy())
31963195
return ConstantFP::get(getContext(), getElementAsAPFloat(Elt));

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)