https://github.com/pzzp updated https://github.com/llvm/llvm-project/pull/126481
>From 85c7d8edebcdd5a50f01db6c51f96189a5e2a729 Mon Sep 17 00:00:00 2001 From: pzzp <pzz...@outlook.com> Date: Fri, 28 Feb 2025 10:46:12 +0800 Subject: [PATCH] [llvm:ir] Add support for constant data exceeding 4GiB --- clang/lib/CodeGen/CGExprConstant.cpp | 8 +++---- llvm/include/llvm/IR/Constants.h | 16 ++++++------- llvm/lib/Analysis/TargetTransformInfo.cpp | 2 +- llvm/lib/Analysis/ValueTracking.cpp | 2 +- llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 8 +++---- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 6 ++--- .../SelectionDAG/SelectionDAGBuilder.cpp | 2 +- llvm/lib/IR/AsmWriter.cpp | 2 +- llvm/lib/IR/Constants.cpp | 23 ++++++++----------- llvm/lib/Target/TargetLoweringObjectFile.cpp | 4 ++-- llvm/lib/Target/X86/X86MCInstLower.cpp | 2 +- 11 files changed, 36 insertions(+), 39 deletions(-) diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp index e90881a9743bf..b016c6e36d1a8 100644 --- a/clang/lib/CodeGen/CGExprConstant.cpp +++ b/clang/lib/CodeGen/CGExprConstant.cpp @@ -364,14 +364,14 @@ bool ConstantAggregateBuilder::split(size_t Index, CharUnits Hint) { // FIXME: If possible, split into two ConstantDataSequentials at Hint. CharUnits ElemSize = getSize(CDS->getElementType()); replace(Elems, Index, Index + 1, - llvm::map_range(llvm::seq(0u, CDS->getNumElements()), - [&](unsigned Elem) { + llvm::map_range(llvm::seq(uint64_t(0u), CDS->getNumElements()), + [&](uint64_t Elem) { return CDS->getElementAsConstant(Elem); })); replace(Offsets, Index, Index + 1, llvm::map_range( - llvm::seq(0u, CDS->getNumElements()), - [&](unsigned Elem) { return Offset + Elem * ElemSize; })); + llvm::seq(uint64_t(0u), CDS->getNumElements()), + [&](uint64_t Elem) { return Offset + Elem * ElemSize; })); return true; } diff --git a/llvm/include/llvm/IR/Constants.h b/llvm/include/llvm/IR/Constants.h index 676d59e3fcb08..a50217078d0ed 100644 --- a/llvm/include/llvm/IR/Constants.h +++ b/llvm/include/llvm/IR/Constants.h @@ -617,34 +617,34 @@ class ConstantDataSequential : public ConstantData { /// If this is a sequential container of integers (of any size), return the /// specified element in the low bits of a uint64_t. - uint64_t getElementAsInteger(unsigned i) const; + uint64_t getElementAsInteger(uint64_t i) const; /// If this is a sequential container of integers (of any size), return the /// specified element as an APInt. - APInt getElementAsAPInt(unsigned i) const; + APInt getElementAsAPInt(uint64_t i) const; /// If this is a sequential container of floating point type, return the /// specified element as an APFloat. - APFloat getElementAsAPFloat(unsigned i) const; + APFloat getElementAsAPFloat(uint64_t i) const; /// If this is an sequential container of floats, return the specified element /// as a float. - float getElementAsFloat(unsigned i) const; + float getElementAsFloat(uint64_t i) const; /// If this is an sequential container of doubles, return the specified /// element as a double. - double getElementAsDouble(unsigned i) const; + double getElementAsDouble(uint64_t i) const; /// Return a Constant for a specified index's element. /// Note that this has to compute a new constant to return, so it isn't as /// efficient as getElementAsInteger/Float/Double. - Constant *getElementAsConstant(unsigned i) const; + Constant *getElementAsConstant(uint64_t i) const; /// Return the element type of the array/vector. Type *getElementType() const; /// Return the number of elements in the array or vector. - unsigned getNumElements() const; + uint64_t getNumElements() const; /// Return the size (in bytes) of each element in the array/vector. /// The size of the elements is known to be a multiple of one byte. @@ -684,7 +684,7 @@ class ConstantDataSequential : public ConstantData { } private: - const char *getElementPointer(unsigned Elt) const; + const char *getElementPointer(uint64_t Elt) const; }; //===----------------------------------------------------------------------===// diff --git a/llvm/lib/Analysis/TargetTransformInfo.cpp b/llvm/lib/Analysis/TargetTransformInfo.cpp index b57624ba811d3..4df551aca30a7 100644 --- a/llvm/lib/Analysis/TargetTransformInfo.cpp +++ b/llvm/lib/Analysis/TargetTransformInfo.cpp @@ -919,7 +919,7 @@ TargetTransformInfo::getOperandInfo(const Value *V) { } else if (const auto *CDS = dyn_cast<ConstantDataSequential>(V)) { OpInfo = OK_NonUniformConstantValue; bool AllPow2 = true, AllNegPow2 = true; - for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) { + for (uint64_t I = 0, E = CDS->getNumElements(); I != E; ++I) { if (auto *CI = dyn_cast<ConstantInt>(CDS->getElementAsConstant(I))) { AllPow2 &= CI->getValue().isPowerOf2(); AllNegPow2 &= CI->getValue().isNegatedPowerOf2(); diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp index 2920738445d5b..880781742fae0 100644 --- a/llvm/lib/Analysis/ValueTracking.cpp +++ b/llvm/lib/Analysis/ValueTracking.cpp @@ -6355,7 +6355,7 @@ Value *llvm::isBytewiseValue(Value *V, const DataLayout &DL) { if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) { Value *Val = UndefInt8; - for (unsigned I = 0, E = CA->getNumElements(); I != E; ++I) + for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I) if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL)))) return nullptr; return Val; diff --git a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp index f6510a7a1549d..8f7482d167977 100644 --- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -2803,7 +2803,7 @@ void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal, cast<ConstantDataSequential>(C)->isString()) { const ConstantDataSequential *Str = cast<ConstantDataSequential>(C); // Emit constant strings specially. - unsigned NumElts = Str->getNumElements(); + uint64_t NumElts = Str->getNumElements(); // If this is a null-terminated string, use the denser CSTRING encoding. if (Str->isCString()) { Code = bitc::CST_CODE_CSTRING; @@ -2814,7 +2814,7 @@ void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal, } bool isCStr7 = Code == bitc::CST_CODE_CSTRING; bool isCStrChar6 = Code == bitc::CST_CODE_CSTRING; - for (unsigned i = 0; i != NumElts; ++i) { + for (uint64_t i = 0; i != NumElts; ++i) { unsigned char V = Str->getElementAsInteger(i); Record.push_back(V); isCStr7 &= (V & 128) == 0; @@ -2831,10 +2831,10 @@ void ModuleBitcodeWriter::writeConstants(unsigned FirstVal, unsigned LastVal, Code = bitc::CST_CODE_DATA; Type *EltTy = CDS->getElementType(); if (isa<IntegerType>(EltTy)) { - for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) + for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i) Record.push_back(CDS->getElementAsInteger(i)); } else { - for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) + for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i) Record.push_back( CDS->getElementAsAPFloat(i).bitcastToAPInt().getLimitedValue()); } diff --git a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp index 2e63cd9b4481c..743551822a8fc 100644 --- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp @@ -3612,9 +3612,9 @@ static void emitGlobalConstantDataSequential( return AP.OutStreamer->emitBytes(CDS->getAsString()); // Otherwise, emit the values in successive locations. - unsigned ElementByteSize = CDS->getElementByteSize(); + uint64_t ElementByteSize = CDS->getElementByteSize(); if (isa<IntegerType>(CDS->getElementType())) { - for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) { + for (uint64_t I = 0, E = CDS->getNumElements(); I != E; ++I) { emitGlobalAliasInline(AP, ElementByteSize * I, AliasList); if (AP.isVerbose()) AP.OutStreamer->getCommentOS() @@ -3624,7 +3624,7 @@ static void emitGlobalConstantDataSequential( } } else { Type *ET = CDS->getElementType(); - for (unsigned I = 0, E = CDS->getNumElements(); I != E; ++I) { + for (uint64_t I = 0, E = CDS->getNumElements(); I != E; ++I) { emitGlobalAliasInline(AP, ElementByteSize * I, AliasList); emitGlobalConstantFP(CDS->getElementAsAPFloat(I), ET, AP); } diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp index 1d0a3f8294546..3dbee7d675b46 100644 --- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp @@ -1844,7 +1844,7 @@ SDValue SelectionDAGBuilder::getValueImpl(const Value *V) { if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) { SmallVector<SDValue, 4> Ops; - for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) { + for (uint64_t i = 0, e = CDS->getNumElements(); i != e; ++i) { SDNode *Val = getValue(CDS->getElementAsConstant(i)).getNode(); // Add each leaf value from the operand to the Constants list // to form a flattened list of all the values. diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp index ae68da0182dc4..eb8bbd08fb762 100644 --- a/llvm/lib/IR/AsmWriter.cpp +++ b/llvm/lib/IR/AsmWriter.cpp @@ -1679,7 +1679,7 @@ static void WriteConstantInternal(raw_ostream &Out, const Constant *CV, WriterCtx.TypePrinter->print(ETy, Out); Out << ' '; WriteAsOperandInternal(Out, CA->getElementAsConstant(0), WriterCtx); - for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) { + for (uint64_t i = 1, e = CA->getNumElements(); i != e; ++i) { Out << ", "; WriterCtx.TypePrinter->print(ETy, Out); Out << ' '; diff --git a/llvm/lib/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp index 36f4136457617..fb659450bfeeb 100644 --- a/llvm/lib/IR/Constants.cpp +++ b/llvm/lib/IR/Constants.cpp @@ -2850,24 +2850,22 @@ bool ConstantDataSequential::isElementTypeCompatible(Type *Ty) { return false; } -unsigned ConstantDataSequential::getNumElements() const { +uint64_t ConstantDataSequential::getNumElements() const { if (ArrayType *AT = dyn_cast<ArrayType>(getType())) return AT->getNumElements(); return cast<FixedVectorType>(getType())->getNumElements(); } - uint64_t ConstantDataSequential::getElementByteSize() const { - return getElementType()->getPrimitiveSizeInBits()/8; + return getElementType()->getPrimitiveSizeInBits() / 8; } /// Return the start of the specified element. -const char *ConstantDataSequential::getElementPointer(unsigned Elt) const { +const char *ConstantDataSequential::getElementPointer(uint64_t Elt) const { assert(Elt < getNumElements() && "Invalid Elt"); - return DataElements+Elt*getElementByteSize(); + return DataElements + Elt * getElementByteSize(); } - /// Return true if the array is empty or all zeros. static bool isAllZeros(StringRef Arr) { for (char I : Arr) @@ -3106,8 +3104,7 @@ Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *V) { return ConstantVector::getSplat(ElementCount::getFixed(NumElts), V); } - -uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const { +uint64_t ConstantDataSequential::getElementAsInteger(uint64_t Elt) const { assert(isa<IntegerType>(getElementType()) && "Accessor can only be used when element is an integer"); const char *EltPtr = getElementPointer(Elt); @@ -3127,7 +3124,7 @@ uint64_t ConstantDataSequential::getElementAsInteger(unsigned Elt) const { } } -APInt ConstantDataSequential::getElementAsAPInt(unsigned Elt) const { +APInt ConstantDataSequential::getElementAsAPInt(uint64_t Elt) const { assert(isa<IntegerType>(getElementType()) && "Accessor can only be used when element is an integer"); const char *EltPtr = getElementPointer(Elt); @@ -3155,7 +3152,7 @@ APInt ConstantDataSequential::getElementAsAPInt(unsigned Elt) const { } } -APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const { +APFloat ConstantDataSequential::getElementAsAPFloat(uint64_t Elt) const { const char *EltPtr = getElementPointer(Elt); switch (getElementType()->getTypeID()) { @@ -3180,19 +3177,19 @@ APFloat ConstantDataSequential::getElementAsAPFloat(unsigned Elt) const { } } -float ConstantDataSequential::getElementAsFloat(unsigned Elt) const { +float ConstantDataSequential::getElementAsFloat(uint64_t Elt) const { assert(getElementType()->isFloatTy() && "Accessor can only be used when element is a 'float'"); return *reinterpret_cast<const float *>(getElementPointer(Elt)); } -double ConstantDataSequential::getElementAsDouble(unsigned Elt) const { +double ConstantDataSequential::getElementAsDouble(uint64_t Elt) const { assert(getElementType()->isDoubleTy() && "Accessor can only be used when element is a 'float'"); return *reinterpret_cast<const double *>(getElementPointer(Elt)); } -Constant *ConstantDataSequential::getElementAsConstant(unsigned Elt) const { +Constant *ConstantDataSequential::getElementAsConstant(uint64_t Elt) const { if (getElementType()->isHalfTy() || getElementType()->isBFloatTy() || getElementType()->isFloatTy() || getElementType()->isDoubleTy()) return ConstantFP::get(getContext(), getElementAsAPFloat(Elt)); diff --git a/llvm/lib/Target/TargetLoweringObjectFile.cpp b/llvm/lib/Target/TargetLoweringObjectFile.cpp index 2f2f194980edd..cab9bc8678a58 100644 --- a/llvm/lib/Target/TargetLoweringObjectFile.cpp +++ b/llvm/lib/Target/TargetLoweringObjectFile.cpp @@ -104,14 +104,14 @@ static bool isSuitableForBSS(const GlobalVariable *GV) { static bool IsNullTerminatedString(const Constant *C) { // First check: is we have constant array terminated with zero if (const ConstantDataSequential *CDS = dyn_cast<ConstantDataSequential>(C)) { - unsigned NumElts = CDS->getNumElements(); + uint64_t NumElts = CDS->getNumElements(); assert(NumElts != 0 && "Can't have an empty CDS"); if (CDS->getElementAsInteger(NumElts-1) != 0) return false; // Not null terminated. // Verify that the null doesn't occur anywhere else in the string. - for (unsigned i = 0; i != NumElts-1; ++i) + for (uint64_t i = 0; i != NumElts - 1; ++i) if (CDS->getElementAsInteger(i) == 0) return false; return true; diff --git a/llvm/lib/Target/X86/X86MCInstLower.cpp b/llvm/lib/Target/X86/X86MCInstLower.cpp index c172c9e60795f..2740c496e68a3 100644 --- a/llvm/lib/Target/X86/X86MCInstLower.cpp +++ b/llvm/lib/Target/X86/X86MCInstLower.cpp @@ -1581,7 +1581,7 @@ static void printConstant(const Constant *COp, unsigned BitWidth, bool IsInteger = EltTy->isIntegerTy(); bool IsFP = EltTy->isHalfTy() || EltTy->isFloatTy() || EltTy->isDoubleTy(); unsigned EltBits = EltTy->getPrimitiveSizeInBits(); - unsigned E = std::min(BitWidth / EltBits, CDS->getNumElements()); + unsigned E = std::min(BitWidth / EltBits, (unsigned)CDS->getNumElements()); if ((BitWidth % EltBits) == 0) { for (unsigned I = 0; I != E; ++I) { if (I != 0) _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits