[PATCH] D133648: Clang, increase upper bound of partially initialized array sizes

Ofek Shochat via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 12 21:11:12 PDT 2022


OfekShochat updated this revision to Diff 459642.
OfekShochat added a comment.

Clang, increase upper bound of partially initialized array sizes

fixes issue with emitting partially initialized constant arrays larger than 2^32.
issue #57353 on github.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D133648/new/

https://reviews.llvm.org/D133648

Files:
  clang/lib/CodeGen/CGExprConstant.cpp
  clang/lib/Sema/SemaInit.cpp
  clang/test/CodeGen/big-array-init.c
  llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp


Index: llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
===================================================================
--- llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
+++ llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
@@ -3164,7 +3164,7 @@
                                      const Constant *BaseCV, uint64_t Offset,
                                      AsmPrinter::AliasMapTy *AliasList) {
   // Print the fields in successive locations. Pad to align if needed!
-  unsigned Size = DL.getTypeAllocSize(CS->getType());
+  uint64_t Size = DL.getTypeAllocSize(CS->getType());
   const StructLayout *Layout = DL.getStructLayout(CS->getType());
   uint64_t SizeSoFar = 0;
   for (unsigned I = 0, E = CS->getNumOperands(); I != E; ++I) {
@@ -3391,7 +3391,7 @@
                                    uint64_t Offset,
                                    AsmPrinter::AliasMapTy *AliasList) {
   emitGlobalAliasInline(AP, Offset, AliasList);
-  uint64_t Size = DL.getTypeAllocSize(CV->getType());
+  unsigned Size = DL.getTypeAllocSize(CV->getType());
 
   // Globals with sub-elements such as combinations of arrays and structs
   // are handled recursively by emitGlobalConstantImpl. Keep track of the
Index: clang/test/CodeGen/big-array-init.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/big-array-init.c
@@ -0,0 +1,7 @@
+// RUN: %clang_cc1 %s -O0 -triple x86_64-unknown-linux-gnu -S -emit-llvm -o - | FileCheck %s
+/* checks for #57353, arrays that are larger than 2^32 were emitting wrongly. */
+
+// CHECK: @bad_char = global <{ i8, [4294967295 x i8] }> <{ i8 1, [4294967295 x i8] zeroinitializer }>, align 16
+char bad_char[4294967296] = {1};
+// CHECK: @bad_int = global <{ i32, [1073741823 x i32] }> <{ i32 1, [1073741823 x i32] zeroinitializer }>, align 16
+int bad_int[1073741824] = {1};
Index: clang/lib/Sema/SemaInit.cpp
===================================================================
--- clang/lib/Sema/SemaInit.cpp
+++ clang/lib/Sema/SemaInit.cpp
@@ -836,8 +836,8 @@
   QualType ElementType;
 
   InitializedEntity ElementEntity = Entity;
-  unsigned NumInits = ILE->getNumInits();
-  unsigned NumElements = NumInits;
+  uint64_t NumInits = ILE->getNumInits();
+  uint64_t NumElements = NumInits;
   if (const ArrayType *AType = SemaRef.Context.getAsArrayType(ILE->getType())) {
     ElementType = AType->getElementType();
     if (const auto *CAType = dyn_cast<ConstantArrayType>(AType))
@@ -857,7 +857,7 @@
     ElementType = ILE->getType();
 
   bool SkipEmptyInitChecks = false;
-  for (unsigned Init = 0; Init != NumElements; ++Init) {
+  for (uint64_t Init = 0; Init != NumElements; ++Init) {
     if (hadError)
       return;
 
Index: clang/lib/CodeGen/CGExprConstant.cpp
===================================================================
--- clang/lib/CodeGen/CGExprConstant.cpp
+++ clang/lib/CodeGen/CGExprConstant.cpp
@@ -391,7 +391,7 @@
 
 static llvm::Constant *
 EmitArrayConstant(CodeGenModule &CGM, llvm::ArrayType *DesiredType,
-                  llvm::Type *CommonElementType, unsigned ArrayBound,
+                  llvm::Type *CommonElementType, uint64_t ArrayBound,
                   SmallVectorImpl<llvm::Constant *> &Elements,
                   llvm::Constant *Filler);
 
@@ -945,11 +945,11 @@
 
 static llvm::Constant *
 EmitArrayConstant(CodeGenModule &CGM, llvm::ArrayType *DesiredType,
-                  llvm::Type *CommonElementType, unsigned ArrayBound,
+                  llvm::Type *CommonElementType, uint64_t ArrayBound,
                   SmallVectorImpl<llvm::Constant *> &Elements,
                   llvm::Constant *Filler) {
   // Figure out how long the initial prefix of non-zero elements is.
-  unsigned NonzeroLength = ArrayBound;
+  uint64_t NonzeroLength = ArrayBound;
   if (Elements.size() < NonzeroLength && Filler->isNullValue())
     NonzeroLength = Elements.size();
   if (NonzeroLength == Elements.size()) {
@@ -1223,11 +1223,11 @@
     auto *CAT = CGM.getContext().getAsConstantArrayType(ILE->getType());
     assert(CAT && "can't emit array init for non-constant-bound array");
     unsigned NumInitElements = ILE->getNumInits();
-    unsigned NumElements = CAT->getSize().getZExtValue();
+    uint64_t NumElements = CAT->getSize().getZExtValue();
 
     // Initialising an array requires us to automatically
     // initialise any elements that have not been initialised explicitly
-    unsigned NumInitableElts = std::min(NumInitElements, NumElements);
+    uint64_t NumInitableElts = std::min((uint64_t)NumInitElements, NumElements);
 
     QualType EltType = CAT->getElementType();
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133648.459642.patch
Type: text/x-patch
Size: 4608 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220913/5341c101/attachment.bin>


More information about the llvm-commits mailing list