[PATCH] D133570: [Clang] changing behavior of constant array emission[AsmPrinter] changing Size from unsigned to uint64_t

Ofek Shochat via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 9 05:38:54 PDT 2022


OfekShochat created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
OfekShochat requested review of this revision.
Herald added projects: clang, LLVM.
Herald added subscribers: llvm-commits, cfe-commits.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D133570

Files:
  clang/lib/CodeGen/CGExprConstant.cpp
  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) {
Index: clang/lib/CodeGen/CGExprConstant.cpp
===================================================================
--- clang/lib/CodeGen/CGExprConstant.cpp
+++ clang/lib/CodeGen/CGExprConstant.cpp
@@ -1222,29 +1222,18 @@
   llvm::Constant *EmitArrayInitialization(InitListExpr *ILE, QualType T) {
     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 NumInitElements = ILE->getNumInits();
+    uint64_t NumElements = CAT->getSize().getZExtValue();
+    printf("hi\n");
 
     // 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(NumInitElements, NumElements);
 
     QualType EltType = CAT->getElementType();
 
-    // Initialize remaining array elements.
-    llvm::Constant *fillC = nullptr;
-    if (Expr *filler = ILE->getArrayFiller()) {
-      fillC = Emitter.tryEmitAbstractForMemory(filler, EltType);
-      if (!fillC)
-        return nullptr;
-    }
-
-    // Copy initializer elements.
-    SmallVector<llvm::Constant*, 16> Elts;
-    if (fillC && fillC->isNullValue())
-      Elts.reserve(NumInitableElts + 1);
-    else
-      Elts.reserve(NumElements);
+    SmallVector<llvm::Constant *, 16> Inits;
+    Inits.reserve(NumInitableElts + 1);
 
     llvm::Type *CommonElementType = nullptr;
     for (unsigned i = 0; i < NumInitableElts; ++i) {
@@ -1256,13 +1245,33 @@
         CommonElementType = C->getType();
       else if (C->getType() != CommonElementType)
         CommonElementType = nullptr;
-      Elts.push_back(C);
+      Inits.push_back(C);
     }
 
-    llvm::ArrayType *Desired =
+    uint64_t TrailingZeroes = NumElements - NumInitElements;
+
+    // If all elements have the same type, just emit an array constant.
+    if (CommonElementType && TrailingZeroes == 0)
+      return llvm::ConstantArray::get(
+          llvm::ArrayType::get(CommonElementType, NumElements), Inits);
+
+    llvm::ArrayType *DesiredType =
         cast<llvm::ArrayType>(CGM.getTypes().ConvertType(ILE->getType()));
-    return EmitArrayConstant(CGM, Desired, CommonElementType, NumElements, Elts,
-                             fillC);
+
+    auto *FillerType =
+        CommonElementType ? CommonElementType : DesiredType->getElementType();
+    FillerType = llvm::ArrayType::get(FillerType, TrailingZeroes);
+    
+    Inits.push_back(llvm::ConstantAggregateZero::get(FillerType));
+
+    llvm::SmallVector<llvm::Type *, 16> Types;
+    for (llvm::Constant *Elt : Inits)
+      Types.push_back(Elt->getType());
+
+    llvm::StructType *SType =
+        llvm::StructType::get(CGM.getLLVMContext(), Types, true);
+
+    return llvm::ConstantStruct::get(SType, Inits);
   }
 
   llvm::Constant *EmitRecordInitialization(InitListExpr *ILE, QualType T) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D133570.459022.patch
Type: text/x-patch
Size: 3790 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220909/754253ea/attachment.bin>


More information about the cfe-commits mailing list