Skip to content

Commit f7516c7

Browse files
authored
[CodeGen] Support arrays with initializers of 64-bit size
Based on @OfekShochat's https://reviews.llvm.org/D133648 init.c is the primary test for array initialization, but it uses a 32-bit triple, which would lead to an "array is too large" error. Add the new test to array-init.c instead. Fix llvm#57353 Pull Request: llvm#92473
1 parent e18c483 commit f7516c7

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

clang/lib/CodeGen/CGExprConstant.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ bool ConstantAggregateBuilder::split(size_t Index, CharUnits Hint) {
393393

394394
static llvm::Constant *
395395
EmitArrayConstant(CodeGenModule &CGM, llvm::ArrayType *DesiredType,
396-
llvm::Type *CommonElementType, unsigned ArrayBound,
396+
llvm::Type *CommonElementType, uint64_t ArrayBound,
397397
SmallVectorImpl<llvm::Constant *> &Elements,
398398
llvm::Constant *Filler);
399399

@@ -949,11 +949,11 @@ tryEmitGlobalCompoundLiteral(ConstantEmitter &emitter,
949949

950950
static llvm::Constant *
951951
EmitArrayConstant(CodeGenModule &CGM, llvm::ArrayType *DesiredType,
952-
llvm::Type *CommonElementType, unsigned ArrayBound,
952+
llvm::Type *CommonElementType, uint64_t ArrayBound,
953953
SmallVectorImpl<llvm::Constant *> &Elements,
954954
llvm::Constant *Filler) {
955955
// Figure out how long the initial prefix of non-zero elements is.
956-
unsigned NonzeroLength = ArrayBound;
956+
uint64_t NonzeroLength = ArrayBound;
957957
if (Elements.size() < NonzeroLength && Filler->isNullValue())
958958
NonzeroLength = Elements.size();
959959
if (NonzeroLength == Elements.size()) {
@@ -965,7 +965,7 @@ EmitArrayConstant(CodeGenModule &CGM, llvm::ArrayType *DesiredType,
965965
return llvm::ConstantAggregateZero::get(DesiredType);
966966

967967
// Add a zeroinitializer array filler if we have lots of trailing zeroes.
968-
unsigned TrailingZeroes = ArrayBound - NonzeroLength;
968+
uint64_t TrailingZeroes = ArrayBound - NonzeroLength;
969969
if (TrailingZeroes >= 8) {
970970
assert(Elements.size() >= NonzeroLength &&
971971
"missing initializer for non-zero element");
@@ -1252,12 +1252,12 @@ class ConstExprEmitter
12521252
llvm::Constant *EmitArrayInitialization(const InitListExpr *ILE, QualType T) {
12531253
auto *CAT = CGM.getContext().getAsConstantArrayType(ILE->getType());
12541254
assert(CAT && "can't emit array init for non-constant-bound array");
1255-
unsigned NumInitElements = ILE->getNumInits();
1256-
unsigned NumElements = CAT->getZExtSize();
1255+
const uint64_t NumElements = CAT->getZExtSize();
12571256

12581257
// Initialising an array requires us to automatically
12591258
// initialise any elements that have not been initialised explicitly
1260-
unsigned NumInitableElts = std::min(NumInitElements, NumElements);
1259+
uint64_t NumInitableElts =
1260+
std::min<uint64_t>(ILE->getNumInits(), NumElements);
12611261

12621262
QualType EltType = CAT->getElementType();
12631263

clang/lib/Sema/SemaInit.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ InitListChecker::FillInEmptyInitializations(const InitializedEntity &Entity,
876876

877877
InitializedEntity ElementEntity = Entity;
878878
unsigned NumInits = ILE->getNumInits();
879-
unsigned NumElements = NumInits;
879+
uint64_t NumElements = NumInits;
880880
if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) {
881881
ElementType = AType->getElementType();
882882
if (const auto *CAType = dyn_cast<ConstantArrayType>(AType))
@@ -896,7 +896,7 @@ InitListChecker::FillInEmptyInitializations(const InitializedEntity &Entity,
896896
ElementType = ILE->getType();
897897

898898
bool SkipEmptyInitChecks = false;
899-
for (unsigned Init = 0; Init != NumElements; ++Init) {
899+
for (uint64_t Init = 0; Init != NumElements; ++Init) {
900900
if (hadError)
901901
return;
902902

clang/test/CodeGen/array-init.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@ void testConstArrayInits(void)
1313
const int a2[5] = {0,0,0};
1414
const int a3[5] = {0};
1515
}
16+
17+
/// https://github.com/llvm/llvm-project/issues/57353
18+
// CHECK: @big_char ={{.*}} global <{ i8, [4294967295 x i8] }> <{ i8 1, [4294967295 x i8] zeroinitializer }>
19+
char big_char[4294967296] = {1};
20+
21+
// CHECK: @big_char2 ={{.*}} global <{ i8, i8, [4294967296 x i8] }> <{ i8 1, i8 2, [4294967296 x i8] zeroinitializer }>
22+
char big_char2[4294967298] = {1, 2};
23+
24+
// CHECK: @big_int ={{.*}} global <{ i32, i32, i32, [2147483647 x i32] }> <{ i32 1, i32 2, i32 3, [2147483645 x i32] zeroinitializer }>
25+
int big_int[0x200000000 >> 2] = {1, 2, 3};

0 commit comments

Comments
 (0)