[clang] [llvm] [llvm:ir] Add support for constant data exceeding 4GiB (PR #126481)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 13 17:32:35 PST 2025
https://github.com/pzzp updated https://github.com/llvm/llvm-project/pull/126481
>From a1d2d3538d44a0740dc24ed81e9d4bf6e7ed0524 Mon Sep 17 00:00:00 2001
From: zhoupeng12 <zhoupeng12 at baidu.com>
Date: Mon, 30 Oct 2023 18:13:07 +0800
Subject: [PATCH] [llvm:ir] Add support for constant data exceeding 4GiB
---
clang/lib/CodeGen/CGExprConstant.cpp | 8 ++++----
llvm/include/llvm/IR/Constants.h | 8 ++++----
llvm/lib/Bitcode/Writer/BitcodeWriter.cpp | 8 ++++----
llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp | 6 +++---
llvm/lib/IR/Constants.cpp | 13 ++++++-------
llvm/lib/Target/TargetLoweringObjectFile.cpp | 4 ++--
llvm/lib/Target/X86/X86MCInstLower.cpp | 2 +-
7 files changed, 24 insertions(+), 25 deletions(-)
diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp
index ef11798869d3b..b0ff6fae65f16 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 15b90589b7e2b..769ac821d1bd3 100644
--- a/llvm/include/llvm/IR/Constants.h
+++ b/llvm/include/llvm/IR/Constants.h
@@ -617,7 +617,7 @@ 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.
@@ -625,7 +625,7 @@ class ConstantDataSequential : public ConstantData {
/// 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.
@@ -644,7 +644,7 @@ class ConstantDataSequential : public ConstantData {
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/Bitcode/Writer/BitcodeWriter.cpp b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
index 7ca63c2c7251d..c95bfc5cf2415 100644
--- a/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
+++ b/llvm/lib/Bitcode/Writer/BitcodeWriter.cpp
@@ -2773,7 +2773,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;
@@ -2784,7 +2784,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;
@@ -2801,10 +2801,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 44b10c3ef9972..34ea45ad8d145 100644
--- a/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -3628,9 +3628,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()
@@ -3640,7 +3640,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/IR/Constants.cpp b/llvm/lib/IR/Constants.cpp
index 33f4dc78c6d3f..d7ab4981d788a 100644
--- a/llvm/lib/IR/Constants.cpp
+++ b/llvm/lib/IR/Constants.cpp
@@ -2855,21 +2855,20 @@ 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();
}
@@ -3112,7 +3111,7 @@ Constant *ConstantDataVector::getSplat(unsigned NumElts, Constant *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);
@@ -3160,7 +3159,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()) {
diff --git a/llvm/lib/Target/TargetLoweringObjectFile.cpp b/llvm/lib/Target/TargetLoweringObjectFile.cpp
index 02c101055d9f3..94891507bc577 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 0f8fbf5be1c95..09166ed9d0f40 100644
--- a/llvm/lib/Target/X86/X86MCInstLower.cpp
+++ b/llvm/lib/Target/X86/X86MCInstLower.cpp
@@ -1583,7 +1583,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());
assert((BitWidth % EltBits) == 0 && "Element size mismatch");
for (unsigned I = 0; I != E; ++I) {
if (I != 0)
More information about the cfe-commits
mailing list