Timm =?utf-8?q?Bäder?= <[email protected]>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/[email protected]>


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: Timm Baeder (tbaederr)

<details>
<summary>Changes</summary>

This implements array fillers for global declarations. When a global
declarations is initialized by a an `InitListExpr` of appropriate form,
we only allocate as many array elements as the `InitListExpr` has
initializers. We additionaly append another element as the filler slot.

When reading from an array, we check if the pointer points after the
allocated elements and if so, we read from the filler instead. The
filler also isn't writable of course.






Still broken are:
 - test/AST/ast-dump-APValue-array.cpp: We do not support array
   fillers for the second dimension of a multidimensional array, so the
   value for `arr_f` is:
```
  |-value: Array size=3
  | |-element: Array size=5
  | | |-elements: Float 1.000000e+00, Float 2.000000e+00, Float 3.000000e+00, 
Float 4.000000e+00
  | | `-element: Float 5.000000e+00
  | `-filler: 2 x Array size=5
  |   |-elements: Float 0.000000e+00, Float 0.000000e+00, Float 0.000000e+00, 
Float 0.000000e+00
  |   `-element: Float 0.000000e+00
```

   where the current interpreter uses:
   ```
  |-value: Array size=3
  | |-element: Array size=5
  | | |-elements: Float 1.000000e+00, Float 2.000000e+00, Float 3.000000e+00, 
Float 4.000000e+00
  | | `-element: Float 5.000000e+00
  | `-filler: 2 x Array size=5
  |   `-filler: 5 x Float 0.000000e+00
   ```

 - tests/AST/ast-dump-APValue-struct: Since we don't do array fillers
   for struct members, `S5::arr0` does not get a filler.


The slowdown on c-t-t is _almost_ negligible but it's much worse in my
own benchmarks, an almost 10% slowdown. That's because we're introducing
an additional check (`&gt;= Pointee-&gt;getSize()`) in every `load()` or
`deref()` operation (via the new `data(Offset)`). Similarly,
`getOffsetInParent()` is worse than the old `getInlineDesc()-&gt;Offset`.


---

Patch is 52.53 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/242995.diff


21 Files Affected:

- (modified) clang/CMakeLists.txt (+1-1) 
- (modified) clang/lib/AST/ByteCode/Block.h (+8-2) 
- (modified) clang/lib/AST/ByteCode/Compiler.cpp (+38-28) 
- (modified) clang/lib/AST/ByteCode/Compiler.h (+3) 
- (modified) clang/lib/AST/ByteCode/Context.cpp (+9-5) 
- (modified) clang/lib/AST/ByteCode/Descriptor.cpp (+29-17) 
- (modified) clang/lib/AST/ByteCode/Descriptor.h (+47-2) 
- (modified) clang/lib/AST/ByteCode/DynamicAllocator.cpp (+5-5) 
- (modified) clang/lib/AST/ByteCode/EvaluationResult.cpp (+5-5) 
- (modified) clang/lib/AST/ByteCode/Interp.cpp (+17-4) 
- (modified) clang/lib/AST/ByteCode/InterpBuiltinBitCast.cpp (+7-6) 
- (modified) clang/lib/AST/ByteCode/MemberPointer.cpp (+3-3) 
- (modified) clang/lib/AST/ByteCode/Pointer.cpp (+31-20) 
- (modified) clang/lib/AST/ByteCode/Pointer.h (+59-16) 
- (modified) clang/lib/AST/ByteCode/Program.cpp (+91-9) 
- (added) clang/test/AST/ByteCode/array-fillers-ast-dump.cpp (+67) 
- (added) clang/test/AST/ByteCode/array-fillers-codegen.cpp (+17) 
- (added) clang/test/AST/ByteCode/array-fillers.cpp (+163) 
- (modified) clang/test/CodeGenCXX/cxx11-initializer-aggregate.cpp (+4-2) 
- (modified) clang/test/SemaCXX/aggregate-initialization.cpp (+5) 
- (modified) clang/test/SemaCXX/large-array-init.cpp (+2-2) 


``````````diff
diff --git a/clang/CMakeLists.txt b/clang/CMakeLists.txt
index fbb4cfaf090059..d3854da59e00ce 100644
--- a/clang/CMakeLists.txt
+++ b/clang/CMakeLists.txt
@@ -529,7 +529,7 @@ option(CLANG_ENABLE_STATIC_ANALYZER
 option(CLANG_ENABLE_PROTO_FUZZER "Build Clang protobuf fuzzer." OFF)
 
 option(CLANG_USE_EXPERIMENTAL_CONST_INTERP
-  "Use the new experimental constant interpreter for compile-time evaluation." 
OFF)
+  "Use the new experimental constant interpreter for compile-time evaluation." 
ON)
 
 if (DEFINED CLANG_ENABLE_ARCMT)
   set(CLANG_ENABLE_OBJC_REWRITER ${CLANG_ENABLE_ARCMT})
diff --git a/clang/lib/AST/ByteCode/Block.h b/clang/lib/AST/ByteCode/Block.h
index 731790ab4aebfa..52986622c686b6 100644
--- a/clang/lib/AST/ByteCode/Block.h
+++ b/clang/lib/AST/ByteCode/Block.h
@@ -71,12 +71,14 @@ class Block final {
 
   Block(unsigned EvalID, UnsignedOrNone DeclID, const Descriptor *Desc,
         BlockKind K, BlockFlags Flags = BlockFlags::empty())
-      : Desc(Desc), DescSize(Desc->getAllocSize()), DeclID(DeclID),
+      : Desc(Desc), DescSize(Desc->getAllocSize()),
+        AddressableSize(Desc->getAddressableSize()), DeclID(DeclID),
         EvalID(EvalID), Flags(Flags), Kind(K), MDSize(getMetadataSize(K)) {}
 
   Block(unsigned EvalID, const Descriptor *Desc, BlockKind K,
         BlockFlags Flags = BlockFlags::empty())
-      : Desc(Desc), DescSize(Desc->getAllocSize()), EvalID(EvalID),
+      : Desc(Desc), DescSize(Desc->getAllocSize()),
+        AddressableSize(Desc->getAddressableSize()), EvalID(EvalID),
         Flags(Flags), Kind(K), MDSize(getMetadataSize(K)) {}
 
   /// Number of bytes needed to allocate the block.
@@ -121,6 +123,9 @@ class Block final {
   bool isAccessible() const { return Flags.V == 0; }
   /// Returns the size of the block, excluding metadata.
   unsigned getSize() const { return DescSize; }
+  /// Returns the size of the block as seen by pointers, excluding metadata.
+  /// Exceeds getSize() for arrays with a filler, see Descriptor::HasFiller.
+  unsigned getAddressableSize() const { return AddressableSize; }
   /// Returns the size of the block, including metadata.
   unsigned getAllocSize() const { return DescSize + MDSize; }
   /// Returns the declaration ID.
@@ -227,6 +232,7 @@ class Block final {
   Pointer *Pointers = nullptr;
   /// Size of the data(), excluding block-level metadata.
   const unsigned DescSize;
+  const unsigned AddressableSize;
   /// Unique identifier of the declaration.
   const UnsignedOrNone DeclID = std::nullopt;
   const unsigned EvalID = ~0u;
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp 
b/clang/lib/AST/ByteCode/Compiler.cpp
index 5044656e3afff1..bacb8b77a5b492 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -2562,12 +2562,19 @@ bool Compiler<Emitter>::visitInitList(ArrayRef<const 
Expr *> Inits,
       }
     }
 
-    // Expand the filler expression.
-    // FIXME: This should go away.
+    // Expand the filler expression. Global arrays with a filler only store
+    // one filler element.
     if (ArrayFiller && !isa<NoInitExpr>(ArrayFiller)) {
-      for (; ElementIndex != NumElems; ++ElementIndex) {
+      if (FillerSlotInit == E) {
+        // Only initialize the filler element.
         if (!this->visitArrayElemInit(ElementIndex, ArrayFiller, InitT))
           return false;
+      } else {
+        // Expand the InitListExpr's filler to all the allocated array 
elements.
+        for (; ElementIndex != NumElems; ++ElementIndex) {
+          if (!this->visitArrayElemInit(ElementIndex, ArrayFiller, InitT))
+            return false;
+        }
       }
     }
 
@@ -3212,38 +3219,38 @@ bool Compiler<Emitter>::VisitStringLiteral(const 
StringLiteral *E) {
   unsigned ArraySize = CAT->getZExtSize();
   unsigned N = std::min(ArraySize, E->getLength());
   unsigned CharWidth = E->getCharByteWidth();
+  assert(CharWidth == 1 || CharWidth == 2 || CharWidth == 4);
+
+  auto initElem = [&](unsigned Index, uint32_t CodeUnit) -> bool {
+    if (CharWidth == 1)
+      return this->emitConstSint8(CodeUnit, E) &&
+             this->emitInitElemSint8(Index, E);
+    else if (CharWidth == 2)
+      return this->emitConstUint16(CodeUnit, E) &&
+             this->emitInitElemUint16(Index, E);
+    else
+      return this->emitConstUint32(CodeUnit, E) &&
+             this->emitInitElemUint32(Index, E);
+  };
 
   for (unsigned I = 0; I != N; ++I) {
     uint32_t CodeUnit = E->getCodeUnit(I);
-
-    if (CharWidth == 1) {
-      this->emitConstSint8(CodeUnit, E);
-      this->emitInitElemSint8(I, E);
-    } else if (CharWidth == 2) {
-      this->emitConstUint16(CodeUnit, E);
-      this->emitInitElemUint16(I, E);
-    } else if (CharWidth == 4) {
-      this->emitConstUint32(CodeUnit, E);
-      this->emitInitElemUint32(I, E);
-    } else {
-      llvm_unreachable("unsupported character width");
-    }
+    if (!initElem(I, CodeUnit))
+      return false;
   }
 
+  // Special case: If this is initializing a global variable with an array
+  // filler, skip the rest of the string since it's just the filler element
+  // anyway.
+  // FIXME: For the `foo[100] = {"abc"}` case, the FillerSlotInit is the
+  // InitListExpr, not the StringLiteral, so this has no effect.
+  if (FillerSlotInit == E)
+    return initElem(N, 0);
+
   // Fill up the rest of the char array with NUL bytes.
   for (unsigned I = N; I != ArraySize; ++I) {
-    if (CharWidth == 1) {
-      this->emitConstSint8(0, E);
-      this->emitInitElemSint8(I, E);
-    } else if (CharWidth == 2) {
-      this->emitConstUint16(0, E);
-      this->emitInitElemUint16(I, E);
-    } else if (CharWidth == 4) {
-      this->emitConstUint32(0, E);
-      this->emitInitElemUint32(I, E);
-    } else {
-      llvm_unreachable("unsupported character width");
-    }
+    if (!initElem(I, 0))
+      return false;
   }
 
   return true;
@@ -5765,6 +5772,9 @@ VarCreationState Compiler<Emitter>::visitVarDecl(const 
VarDecl *VD,
     if (!this->emitStartInit(Init))
       return false;
 
+    const Descriptor *Desc = P.getGlobal(*GlobalIndex)->getDescriptor();
+    llvm::SaveAndRestore FillerSAR(FillerSlotInit,
+                                   Desc->hasFiller() ? Init : nullptr);
     if (!visitInitializer(Init))
       return false;
 
diff --git a/clang/lib/AST/ByteCode/Compiler.h 
b/clang/lib/AST/ByteCode/Compiler.h
index 72fbef7c8b2920..e196945f4c68d4 100644
--- a/clang/lib/AST/ByteCode/Compiler.h
+++ b/clang/lib/AST/ByteCode/Compiler.h
@@ -499,6 +499,9 @@ class Compiler final : public 
ConstStmtVisitor<Compiler<Emitter>, bool>,
   /// variable. This is set in visitInitializer().
   bool Initializing = false;
   const VarDecl *InitializingDecl = nullptr;
+  /// Root initializer of the global array being initialized, if that array
+  /// only stores one filler element. See Descriptor::HasFiller.
+  const Expr *FillerSlotInit = nullptr;
 
   llvm::SmallVector<InitLink> InitStack;
   bool InitStackActive = false;
diff --git a/clang/lib/AST/ByteCode/Context.cpp 
b/clang/lib/AST/ByteCode/Context.cpp
index 0a0ed5fc394c86..a6bed003cfa30d 100644
--- a/clang/lib/AST/ByteCode/Context.cpp
+++ b/clang/lib/AST/ByteCode/Context.cpp
@@ -262,10 +262,14 @@ bool Context::evaluateStringRepr(const EvalSettings 
&Settings,
 
       const char *Addr = reinterpret_cast<const char *>(Ptr.getRawAddress());
 
-      if (Ptr.isStringPointer())
+      if (Ptr.isStringPointer()) {
         Result.assign(Addr, Size - static_cast<unsigned>(Limited));
-      else
-        Result.assign(Addr, Size);
+      } else {
+        // Filler elements are zero.
+        unsigned NumContiguous = Ptr.getNumContiguousElems(Size);
+        Result.assign(Addr, NumContiguous);
+        Result.append(Size - NumContiguous, '\0');
+      }
     }
 
     return true;
@@ -322,7 +326,7 @@ bool Context::evaluateString(const EvalSettings &Settings, 
const Expr *E,
         Result.assign(Chars, N - 1);
         return true;
       }
-      unsigned Length = strnlen(Chars, N);
+      unsigned Length = strnlen(Chars, Ptr.getNumContiguousElems(N));
       // Wasn't null terminated.
       if (N == Length)
         return false;
@@ -403,7 +407,7 @@ std::optional<uint64_t> Context::evaluateStrlen(const 
EvalSettings &Settings,
 
     unsigned N = Ptr.getNumElems();
     if (Ptr.elemSize() == 1) {
-      unsigned Size = N - Ptr.getIndex();
+      unsigned Size = Ptr.getNumContiguousElems(N) - Ptr.getIndex();
       Result =
           strnlen(reinterpret_cast<const char *>(Ptr.getRawAddress()), Size);
       return Result != Size;
diff --git a/clang/lib/AST/ByteCode/Descriptor.cpp 
b/clang/lib/AST/ByteCode/Descriptor.cpp
index 67e09cb004a88a..768f72b6c39809 100644
--- a/clang/lib/AST/ByteCode/Descriptor.cpp
+++ b/clang/lib/AST/ByteCode/Descriptor.cpp
@@ -39,7 +39,7 @@ static void dtorArrayTy(std::byte *Ptr, const Descriptor *D) {
 
   if constexpr (needsDtor<T>()) {
     Ptr += sizeof(InitMapPtr);
-    for (unsigned I = 0, NE = D->getNumElems(); I != NE; ++I) {
+    for (unsigned I = 0, NE = D->getNumStoredElems(); I != NE; ++I) {
       reinterpret_cast<T *>(Ptr)[I].~T();
     }
   }
@@ -48,7 +48,7 @@ static void dtorArrayTy(std::byte *Ptr, const Descriptor *D) {
 static void ctorArrayDesc(std::byte *Ptr, bool IsConst, bool IsMutable,
                           bool IsVolatile, bool IsActive, bool InUnion,
                           const Descriptor *D) {
-  const unsigned NumElems = D->getNumElems();
+  const unsigned NumElems = D->getNumStoredElems();
   const unsigned ElemSize =
       D->ElemDesc->getAllocSize() + sizeof(InlineDescriptor);
 
@@ -78,7 +78,7 @@ static void ctorArrayDesc(std::byte *Ptr, bool IsConst, bool 
IsMutable,
 }
 
 static void dtorArrayDesc(std::byte *Ptr, const Descriptor *D) {
-  const unsigned NumElems = D->getNumElems();
+  const unsigned NumElems = D->getNumStoredElems();
   const unsigned ElemSize =
       D->ElemDesc->getAllocSize() + sizeof(InlineDescriptor);
 
@@ -272,7 +272,7 @@ static void ctorArrayTy(std::byte *Ptr, bool, bool, bool, 
bool, bool,
 
   if constexpr (needsCtor<T>()) {
     Ptr += sizeof(InitMapPtr);
-    for (unsigned I = 0, NE = D->getNumElems(); I < NE; ++I) {
+    for (unsigned I = 0, NE = D->getNumStoredElems(); I != NE; ++I) {
       new (&reinterpret_cast<T *>(Ptr)[I]) T();
     }
   }
@@ -301,17 +301,24 @@ Descriptor::Descriptor(DeclOrExpr D, const Type 
*SourceTy, PrimType Type,
   assert(Source && "Missing source");
 }
 
-/// Primitive arrays.
+/// Primitive arrays, possibly with a filler. A filler array is not padded, so
+/// every byte past its storage belongs to a filler element.
 Descriptor::Descriptor(DeclOrExpr D, const Type *SourceTy, PrimType Type,
-                       size_t NumElems, bool IsConst, bool IsTemporary,
-                       bool IsMutable, bool IsVolatile)
+                       size_t NumElems, size_t NumStoredElems, bool IsConst,
+                       bool IsTemporary, bool IsMutable, bool IsVolatile)
     : Source(D), SourceType(SourceTy), ElemSize(primSize(Type)),
-      Size(ElemSize * NumElems), AllocSize(align(Size) + sizeof(InitMapPtr)),
-      PrimT(Type), IsConst(IsConst), IsMutable(IsMutable),
-      IsTemporary(IsTemporary), IsVolatile(IsVolatile), IsArray(true),
-      CtorFn(nullptr), DtorFn(getDtorArrayPrim(Type)) {
+      Size(ElemSize * NumElems),
+      AllocSize((NumStoredElems == NumElems ? align(Size)
+                                            : ElemSize * NumStoredElems) +
+                sizeof(InitMapPtr)),
+      NumStoredElems(NumStoredElems), PrimT(Type), IsConst(IsConst),
+      IsMutable(IsMutable), IsTemporary(IsTemporary), IsVolatile(IsVolatile),
+      IsArray(true), HasFiller(NumStoredElems != NumElems), CtorFn(nullptr),
+      DtorFn(getDtorArrayPrim(Type)) {
   assert(Source && "Missing source");
   assert(NumElems <= (MaxArrayElemBytes / ElemSize));
+  assert(NumStoredElems <= NumElems);
+  assert(!HasFiller || NumStoredElems != 0);
 }
 
 /// Primitive unknown-size arrays.
@@ -324,18 +331,23 @@ Descriptor::Descriptor(DeclOrExpr D, PrimType Type, bool 
IsConst,
   assert(Source && "Missing source");
 }
 
-/// Arrays of composite elements.
+/// Arrays of composite elements, possibly with a filler. Elements are larger
+/// than alignof(void *), so a filler array is never padded.
 Descriptor::Descriptor(DeclOrExpr D, const Type *SourceTy,
-                       const Descriptor *Elem, unsigned NumElems, bool IsConst,
-                       bool IsTemporary, bool IsMutable)
+                       const Descriptor *Elem, unsigned NumElems,
+                       unsigned NumStoredElems, bool IsConst, bool IsTemporary,
+                       bool IsMutable)
     : Source(D), SourceType(SourceTy),
       ElemSize(Elem->getAllocSize() + sizeof(InlineDescriptor)),
       Size(ElemSize * NumElems),
-      AllocSize(std::max<size_t>(alignof(void *), Size)), ElemDesc(Elem),
-      IsConst(IsConst), IsMutable(IsMutable), IsTemporary(IsTemporary),
-      IsArray(true), CtorFn(ctorArrayDesc),
+      AllocSize(std::max<size_t>(alignof(void *), ElemSize * NumStoredElems)),
+      NumStoredElems(NumStoredElems), ElemDesc(Elem), IsConst(IsConst),
+      IsMutable(IsMutable), IsTemporary(IsTemporary), IsArray(true),
+      HasFiller(NumStoredElems != NumElems), CtorFn(ctorArrayDesc),
       DtorFn(Elem->DtorFn ? dtorArrayDesc : nullptr) {
   assert(Source && "Missing source");
+  assert(NumStoredElems <= NumElems);
+  assert(!HasFiller || NumStoredElems != 0);
 }
 
 /// Unknown-size arrays of composite elements.
diff --git a/clang/lib/AST/ByteCode/Descriptor.h 
b/clang/lib/AST/ByteCode/Descriptor.h
index 564ce64e6f40c5..4426de4bf8ed72 100644
--- a/clang/lib/AST/ByteCode/Descriptor.h
+++ b/clang/lib/AST/ByteCode/Descriptor.h
@@ -134,6 +134,10 @@ struct Descriptor final {
   const unsigned Size;
   /// Size of the allocation (storage), in host bytes.
   const unsigned AllocSize;
+  /// Number of array elements backed by storage.
+  /// This is either the same as `getNumElems()`, or it is
+  /// less and HasFiller is true.
+  const unsigned NumStoredElems = 0;
 
   /// Value to denote arrays of unknown size.
   static constexpr unsigned UnknownSizeMark = (unsigned)-1;
@@ -163,6 +167,8 @@ struct Descriptor final {
   const bool IsVolatile = false;
   /// Flag indicating if the block is an array.
   const bool IsArray = false;
+  /// Whether NumStoredElems is less than the number of elements.
+  const bool HasFiller = false;
 
   /// Storage management methods.
   const BlockCtorFn CtorFn = nullptr;
@@ -174,7 +180,8 @@ struct Descriptor final {
 
   /// Allocates a descriptor for an array of primitives.
   Descriptor(DeclOrExpr D, const Type *SourceTy, PrimType Type, size_t 
NumElems,
-             bool IsConst, bool IsTemporary, bool IsMutable, bool IsVolatile);
+             size_t NumStoredElems, bool IsConst, bool IsTemporary,
+             bool IsMutable, bool IsVolatile);
 
   /// Allocates a descriptor for an array of primitives of unknown size.
   Descriptor(DeclOrExpr D, PrimType Type, bool IsConst, bool IsTemporary,
@@ -182,7 +189,8 @@ struct Descriptor final {
 
   /// Allocates a descriptor for an array of composites.
   Descriptor(DeclOrExpr D, const Type *SourceTy, const Descriptor *Elem,
-             unsigned NumElems, bool IsConst, bool IsTemporary, bool 
IsMutable);
+             unsigned NumElems, unsigned NumStoredElems, bool IsConst,
+             bool IsTemporary, bool IsMutable);
 
   /// Allocates a descriptor for an array of composites of unknown size.
   Descriptor(DeclOrExpr D, const Descriptor *Elem, bool IsTemporary,
@@ -249,6 +257,43 @@ struct Descriptor final {
   unsigned getNumElems() const {
     return Size == UnknownSizeMark ? 0 : (getSize() / getElemSize());
   }
+  /// Returns the number of array elements backed by storage.
+  unsigned getNumStoredElems() const { return NumStoredElems; }
+  /// Checks if filler elements share storage, see NumStoredElems.
+  bool hasFiller() const { return HasFiller; }
+  /// Maps an element index to the index of its storage.
+  unsigned toStoredIndex(unsigned I) const {
+    if (LLVM_LIKELY(!HasFiller))
+      return I;
+    return std::min(I, NumStoredElems - 1);
+  }
+  /// Returns the byte offset of the filler slot in the data.
+  unsigned getFillerOffset() const {
+    assert(HasFiller);
+    unsigned Offset = (NumStoredElems - 1) * ElemSize;
+    if (isPrimitiveArray())
+      Offset += sizeof(InitMapPtr);
+    return Offset;
+  }
+  /// Maps a byte offset into the data to the offset of its storage.
+  uint64_t toStorageOffset(uint64_t Offset) const {
+    if (LLVM_LIKELY(!HasFiller))
+      return Offset;
+
+    // Offsets past the filler slot wrap around inside of it, e.g. with an
+    // element size of 4 and the slot at 8, offset 21 maps to 9.
+    uint64_t FillerOffset = getFillerOffset();
+    if (Offset < FillerOffset)
+      return Offset;
+    return FillerOffset + ((Offset - FillerOffset) % ElemSize);
+  }
+  /// Returns the data size as seen by pointers. Same as getAllocSize(),
+  /// plus the filler elements without storage.
+  unsigned getAddressableSize() const {
+    if (LLVM_LIKELY(!HasFiller))
+      return AllocSize;
+    return AllocSize + ((getNumElems() - NumStoredElems) * ElemSize);
+  }
 
   /// Checks if the descriptor is of an array of primitives.
   bool isPrimitiveArray() const { return IsArray && !ElemDesc; }
diff --git a/clang/lib/AST/ByteCode/DynamicAllocator.cpp 
b/clang/lib/AST/ByteCode/DynamicAllocator.cpp
index be02fbf0158df6..e946497b291ab1 100644
--- a/clang/lib/AST/ByteCode/DynamicAllocator.cpp
+++ b/clang/lib/AST/ByteCode/DynamicAllocator.cpp
@@ -37,10 +37,10 @@ Block *DynamicAllocator::allocate(const Expr *Source, 
PrimType T,
                                   Form AllocForm) {
   // Create a new descriptor for an array of the specified size and
   // element type.
-  const Descriptor *D =
-      allocateDescriptor(Source, nullptr, T, NumElements, /*IsConst=*/false,
-                         /*IsTemporary=*/false, /*IsMutable=*/false,
-                         /*IsVolatile=*/false);
+  const Descriptor *D = allocateDescriptor(
+      Source, nullptr, T, NumElements, NumElements, /*IsConst=*/false,
+      /*IsTemporary=*/false, /*IsMutable=*/false,
+      /*IsVolatile=*/false);
 
   return allocate(D, EvalID, AllocForm);
 }
@@ -52,7 +52,7 @@ Block *DynamicAllocator::allocate(const Descriptor 
*ElementDesc,
   // element type.
   // FIXME: Pass proper element type.
   const Descriptor *D = allocateDescriptor(
-      ElementDesc->asExpr(), nullptr, ElementDesc, NumElements,
+      ElementDesc->asExpr(), nullptr, ElementDesc, NumElements, NumElements,
       /*IsConst=*/false, /*IsTemporary=*/false, /*IsMutable=*/false);
   return allocate(D, EvalID, AllocForm);
 }
diff --git a/clang/lib/AST/ByteCode/EvaluationResult.cpp 
b/clang/lib/AST/ByteCode/EvaluationResult.cpp
index d44239545e5b49..fb2ce44f36887b 100644
--- a/clang/lib/AST/ByteCode/EvaluationResult.cpp
+++ b/clang/lib/AST/ByteCode/EvaluationResult.cpp
@@ -53,7 +53,7 @@ static bool CheckArrayInitialized(InterpState &S, 
SourceLocation Loc,
   const Descriptor *BaseDesc = BasePtr.getFieldDesc();
   assert(BaseDesc->isArray());
 
-  size_t NumElems = BaseDesc->getNumElems();
+  size_t NumElems = BaseDesc->getNumStoredElems();
   if (NumElems == 0)
     return true;
 
@@ -246,7 +246,7 @@ static void collectBlocks(PtrView Ptr,
   }
 
   if (Desc->isPrimitiveArray() && Desc->getPrimType() == PT_Ptr) {
-    for (unsigned I = 0; I != Desc->getNumElems(); ++I) {
+    for (unsigned I = 0, N = Desc->getNumStoredElems(); I != N; ++I) {
       Pointer ElemPointee = Ptr.elem<Pointer>(I);
       if (isUsefulPtr(ElemPointee) && !Blocks.contains(ElemPointee.block()))
         collectBlocks(ElemPointee.view(), Blocks);
@@ -255,7 +255,7 @@ static void collectBlocks(PtrView Ptr,
   }
 
   if (Desc->isCompositeArray() && isOrHasPtr(Desc->ElemDesc)) {
-    for (unsigned I = 0; I != Desc->getNumElems(); ++I) {
+    for (unsigned I = 0, N = Desc->getNumStoredElems(); I != N; ++I) {
       PtrView ElemPtr = Ptr.atIndex(I).narrow();
       collectBlocks(ElemPtr, Blocks);
     }
@@ -504,7 +504,7 @@ static bool lvalFields(InterpState &S, const ASTContext 
&Ctx, PtrView Ptr,
   }
 
   if (FieldDesc->isPrimitiveArray() && FieldDesc->getPrimType() == PT_Ptr) {
-    for (unsigned I = 0; I != FieldDesc->getNumElems(); ++I) {
+    for (unsigned I = 0, N = FieldDesc->getNumStoredElem...
[truncated]

``````````

</details>


https://github.com/llvm/llvm-project/pull/242995
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to