[clang] [NFC] Refactor ConstantArrayType size storage (PR #85716)

2024-03-26 Thread Chris B via cfe-commits

https://github.com/llvm-beanz closed 
https://github.com/llvm/llvm-project/pull/85716
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [NFC] Refactor ConstantArrayType size storage (PR #85716)

2024-03-26 Thread Cooper Partin via cfe-commits

https://github.com/coopp approved this pull request.

Looks good to me for what I can see in the code.

I took special care to make sure places where noticed the old size accessor 
(CAT->getSize().getZExtValue() ) being called, was now using 
(CAT->getZExtSize()).


https://github.com/llvm/llvm-project/pull/85716
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [NFC] Refactor ConstantArrayType size storage (PR #85716)

2024-03-26 Thread Chris B via cfe-commits

https://github.com/llvm-beanz updated 
https://github.com/llvm/llvm-project/pull/85716

>From 4a11a73b4dd41637b1d730489954c2994489d6be Mon Sep 17 00:00:00 2001
From: Chris Bieneman 
Date: Mon, 18 Mar 2024 17:30:41 -0500
Subject: [PATCH 1/5] [NFC] Refactor ConstantArrayType size storage

In PR #79382, I need to add a new type that derives from
ConstantArrayType. This means that ConstantArrayType can no longer use
`llvm::TrailingObjects` to store the trailing optional Expr*.

This change refactors ConstantArrayType to store a 60-bit integer and
4-bits for the integer size in bytes. This replaces the APInt field
previously in the type but preserves enough information to recreate it
where needed.

To reduce the number of places where the APInt is re-constructed I've
also added some helper methods to the ConstantArrayType to allow some
common use cases that operate on either the stored small integer or the
APInt as appropriate.

Resolves #85124.
---
 clang/include/clang/AST/Type.h| 104 ++
 clang/lib/AST/ASTContext.cpp  |  21 ++--
 clang/lib/AST/Decl.cpp|   2 +-
 clang/lib/AST/ExprConstant.cpp|  30 ++---
 clang/lib/AST/Interp/ByteCodeExprGen.cpp  |   8 +-
 clang/lib/AST/Interp/EvaluationResult.cpp |   2 +-
 clang/lib/AST/Interp/Program.cpp  |   2 +-
 clang/lib/AST/JSONNodeDumper.cpp  |   2 +-
 clang/lib/AST/MicrosoftMangle.cpp |   3 +-
 clang/lib/AST/ScanfFormatString.cpp   |   2 +-
 clang/lib/AST/Type.cpp|  20 +++-
 clang/lib/AST/TypePrinter.cpp |   2 +-
 clang/lib/Analysis/CFG.cpp|   4 +-
 clang/lib/Analysis/UnsafeBufferUsage.cpp  |   8 +-
 clang/lib/CodeGen/ABIInfo.cpp |   4 +-
 clang/lib/CodeGen/ABIInfoImpl.cpp |   4 +-
 clang/lib/CodeGen/CGCall.cpp  |   4 +-
 clang/lib/CodeGen/CGDebugInfo.cpp |   2 +-
 clang/lib/CodeGen/CGExprCXX.cpp   |   6 +-
 clang/lib/CodeGen/CGExprConstant.cpp  |   8 +-
 clang/lib/CodeGen/CGObjCMac.cpp   |   6 +-
 clang/lib/CodeGen/CGOpenMPRuntime.cpp |   4 +-
 clang/lib/CodeGen/CodeGenFunction.cpp |   4 +-
 clang/lib/CodeGen/CodeGenModule.cpp   |   2 +-
 clang/lib/CodeGen/CodeGenTypes.cpp|   2 +-
 clang/lib/CodeGen/SwiftCallingConv.cpp|   2 +-
 clang/lib/CodeGen/Targets/ARM.cpp |   2 +-
 clang/lib/CodeGen/Targets/LoongArch.cpp   |   2 +-
 clang/lib/CodeGen/Targets/RISCV.cpp   |   2 +-
 clang/lib/CodeGen/Targets/X86.cpp |   4 +-
 clang/lib/Sema/SemaChecking.cpp   |   8 +-
 clang/lib/Sema/SemaDecl.cpp   |   2 +-
 clang/lib/Sema/SemaDeclCXX.cpp|   2 +-
 clang/lib/Sema/SemaExpr.cpp   |   4 +-
 clang/lib/Sema/SemaExprCXX.cpp|   2 +-
 clang/lib/Sema/SemaInit.cpp   |  20 ++--
 clang/lib/Sema/SemaOpenMP.cpp |   6 +-
 clang/lib/Sema/SemaSYCL.cpp   |   2 +-
 .../Checkers/CastSizeChecker.cpp  |   2 +-
 .../Checkers/PaddingChecker.cpp   |   2 +-
 clang/lib/StaticAnalyzer/Core/MemRegion.cpp   |   2 +-
 clang/lib/StaticAnalyzer/Core/RegionStore.cpp |   8 +-
 42 files changed, 201 insertions(+), 127 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 10916053cdfbf5..f8739fd54f33d1 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -1689,7 +1689,7 @@ class alignas(TypeAlignment) Type : public 
ExtQualsTypeCommonBase {
 
 /// Whether we have a stored size expression.
 LLVM_PREFERRED_TYPE(bool)
-unsigned HasStoredSizeExpr : 1;
+unsigned HasExternalSize : 1;
   };
 
   class BuiltinTypeBitfields {
@@ -3182,34 +3182,98 @@ class ArrayType : public Type, public 
llvm::FoldingSetNode {
 /// For example, the canonical type for 'int A[4 + 4*100]' is a
 /// ConstantArrayType where the element type is 'int' and the size is 404.
 class ConstantArrayType final
-: public ArrayType,
-  private llvm::TrailingObjects {
+: public ArrayType {
   friend class ASTContext; // ASTContext creates these.
-  friend TrailingObjects;
 
-  llvm::APInt Size; // Allows us to unique the type.
+  struct ExternalSize {
+ExternalSize(const llvm::APInt &Sz, const Expr *SE)
+: Size(Sz), SizeExpr(SE) {}
+llvm::APInt Size; // Allows us to unique the type.
+const Expr *SizeExpr;
+  };
+  struct InlineSize {
+InlineSize(uint64_t TSz, uint64_t Sz) : ByteWidth(TSz), Size(Sz) {}
+uint64_t ByteWidth : 4;
+uint64_t Size : 60;
+  };
+  union {
+struct InlineSize I;
+ExternalSize *SizePtr;
+  };
 
-  ConstantArrayType(QualType et, QualType can, const llvm::APInt &size,
-const Expr *sz, ArraySizeModifier sm, unsigned tq)
-  : ArrayType(ConstantArray, et, can, sm, tq, sz)

[clang] [NFC] Refactor ConstantArrayType size storage (PR #85716)

2024-03-26 Thread Chris B via cfe-commits

https://github.com/llvm-beanz updated 
https://github.com/llvm/llvm-project/pull/85716

>From 4a11a73b4dd41637b1d730489954c2994489d6be Mon Sep 17 00:00:00 2001
From: Chris Bieneman 
Date: Mon, 18 Mar 2024 17:30:41 -0500
Subject: [PATCH 1/4] [NFC] Refactor ConstantArrayType size storage

In PR #79382, I need to add a new type that derives from
ConstantArrayType. This means that ConstantArrayType can no longer use
`llvm::TrailingObjects` to store the trailing optional Expr*.

This change refactors ConstantArrayType to store a 60-bit integer and
4-bits for the integer size in bytes. This replaces the APInt field
previously in the type but preserves enough information to recreate it
where needed.

To reduce the number of places where the APInt is re-constructed I've
also added some helper methods to the ConstantArrayType to allow some
common use cases that operate on either the stored small integer or the
APInt as appropriate.

Resolves #85124.
---
 clang/include/clang/AST/Type.h| 104 ++
 clang/lib/AST/ASTContext.cpp  |  21 ++--
 clang/lib/AST/Decl.cpp|   2 +-
 clang/lib/AST/ExprConstant.cpp|  30 ++---
 clang/lib/AST/Interp/ByteCodeExprGen.cpp  |   8 +-
 clang/lib/AST/Interp/EvaluationResult.cpp |   2 +-
 clang/lib/AST/Interp/Program.cpp  |   2 +-
 clang/lib/AST/JSONNodeDumper.cpp  |   2 +-
 clang/lib/AST/MicrosoftMangle.cpp |   3 +-
 clang/lib/AST/ScanfFormatString.cpp   |   2 +-
 clang/lib/AST/Type.cpp|  20 +++-
 clang/lib/AST/TypePrinter.cpp |   2 +-
 clang/lib/Analysis/CFG.cpp|   4 +-
 clang/lib/Analysis/UnsafeBufferUsage.cpp  |   8 +-
 clang/lib/CodeGen/ABIInfo.cpp |   4 +-
 clang/lib/CodeGen/ABIInfoImpl.cpp |   4 +-
 clang/lib/CodeGen/CGCall.cpp  |   4 +-
 clang/lib/CodeGen/CGDebugInfo.cpp |   2 +-
 clang/lib/CodeGen/CGExprCXX.cpp   |   6 +-
 clang/lib/CodeGen/CGExprConstant.cpp  |   8 +-
 clang/lib/CodeGen/CGObjCMac.cpp   |   6 +-
 clang/lib/CodeGen/CGOpenMPRuntime.cpp |   4 +-
 clang/lib/CodeGen/CodeGenFunction.cpp |   4 +-
 clang/lib/CodeGen/CodeGenModule.cpp   |   2 +-
 clang/lib/CodeGen/CodeGenTypes.cpp|   2 +-
 clang/lib/CodeGen/SwiftCallingConv.cpp|   2 +-
 clang/lib/CodeGen/Targets/ARM.cpp |   2 +-
 clang/lib/CodeGen/Targets/LoongArch.cpp   |   2 +-
 clang/lib/CodeGen/Targets/RISCV.cpp   |   2 +-
 clang/lib/CodeGen/Targets/X86.cpp |   4 +-
 clang/lib/Sema/SemaChecking.cpp   |   8 +-
 clang/lib/Sema/SemaDecl.cpp   |   2 +-
 clang/lib/Sema/SemaDeclCXX.cpp|   2 +-
 clang/lib/Sema/SemaExpr.cpp   |   4 +-
 clang/lib/Sema/SemaExprCXX.cpp|   2 +-
 clang/lib/Sema/SemaInit.cpp   |  20 ++--
 clang/lib/Sema/SemaOpenMP.cpp |   6 +-
 clang/lib/Sema/SemaSYCL.cpp   |   2 +-
 .../Checkers/CastSizeChecker.cpp  |   2 +-
 .../Checkers/PaddingChecker.cpp   |   2 +-
 clang/lib/StaticAnalyzer/Core/MemRegion.cpp   |   2 +-
 clang/lib/StaticAnalyzer/Core/RegionStore.cpp |   8 +-
 42 files changed, 201 insertions(+), 127 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 10916053cdfbf5..f8739fd54f33d1 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -1689,7 +1689,7 @@ class alignas(TypeAlignment) Type : public 
ExtQualsTypeCommonBase {
 
 /// Whether we have a stored size expression.
 LLVM_PREFERRED_TYPE(bool)
-unsigned HasStoredSizeExpr : 1;
+unsigned HasExternalSize : 1;
   };
 
   class BuiltinTypeBitfields {
@@ -3182,34 +3182,98 @@ class ArrayType : public Type, public 
llvm::FoldingSetNode {
 /// For example, the canonical type for 'int A[4 + 4*100]' is a
 /// ConstantArrayType where the element type is 'int' and the size is 404.
 class ConstantArrayType final
-: public ArrayType,
-  private llvm::TrailingObjects {
+: public ArrayType {
   friend class ASTContext; // ASTContext creates these.
-  friend TrailingObjects;
 
-  llvm::APInt Size; // Allows us to unique the type.
+  struct ExternalSize {
+ExternalSize(const llvm::APInt &Sz, const Expr *SE)
+: Size(Sz), SizeExpr(SE) {}
+llvm::APInt Size; // Allows us to unique the type.
+const Expr *SizeExpr;
+  };
+  struct InlineSize {
+InlineSize(uint64_t TSz, uint64_t Sz) : ByteWidth(TSz), Size(Sz) {}
+uint64_t ByteWidth : 4;
+uint64_t Size : 60;
+  };
+  union {
+struct InlineSize I;
+ExternalSize *SizePtr;
+  };
 
-  ConstantArrayType(QualType et, QualType can, const llvm::APInt &size,
-const Expr *sz, ArraySizeModifier sm, unsigned tq)
-  : ArrayType(ConstantArray, et, can, sm, tq, sz)

[clang] [NFC] Refactor ConstantArrayType size storage (PR #85716)

2024-03-25 Thread Eli Friedman via cfe-commits


@@ -3182,34 +3182,100 @@ class ArrayType : public Type, public 
llvm::FoldingSetNode {
 /// For example, the canonical type for 'int A[4 + 4*100]' is a
 /// ConstantArrayType where the element type is 'int' and the size is 404.
 class ConstantArrayType final
-: public ArrayType,
-  private llvm::TrailingObjects {
+: public ArrayType {
   friend class ASTContext; // ASTContext creates these.
-  friend TrailingObjects;
 
-  llvm::APInt Size; // Allows us to unique the type.
+  struct ExternalSize {
+ExternalSize(const llvm::APInt &Sz, const Expr *SE)
+: Size(Sz), SizeExpr(SE) {}
+llvm::APInt Size; // Allows us to unique the type.
+const Expr *SizeExpr;
+  };
+  struct InlineSize {
+InlineSize(uint64_t TSz, uint64_t Sz) : ByteWidth(TSz), Size(Sz) {}
+uint64_t ByteWidth : 4;
+uint64_t Size : 60;
+  };
+  union {
+struct InlineSize I;
+ExternalSize *SizePtr;
+  };
 
-  ConstantArrayType(QualType et, QualType can, const llvm::APInt &size,
-const Expr *sz, ArraySizeModifier sm, unsigned tq)
-  : ArrayType(ConstantArray, et, can, sm, tq, sz), Size(size) {
-ConstantArrayTypeBits.HasStoredSizeExpr = sz != nullptr;
-if (ConstantArrayTypeBits.HasStoredSizeExpr) {
-  assert(!can.isNull() && "canonical constant array should not have size");
-  *getTrailingObjects() = sz;
-}
+  ConstantArrayType(QualType Et, QualType Can, uint64_t Width, uint64_t Sz,
+ArraySizeModifier SM, unsigned TQ)
+  : ArrayType(ConstantArray, Et, Can, SM, TQ, nullptr), I(Width / 8, Sz) {
+ConstantArrayTypeBits.HasExternalSize = false;
+assert(Sz < 0x0FFF && "Size must fit in 60 bits");
+// The in-structure size stores the size in bytes rather than bits so we
+// drop the four least significant bits since they're always zero anyways.
+assert(Width < 0xFF && "Type width must fit in 8 bits");
   }
 
-  unsigned numTrailingObjects(OverloadToken) const {
-return ConstantArrayTypeBits.HasStoredSizeExpr;
+  ConstantArrayType(QualType Et, QualType Can, ExternalSize *SzPtr,
+ArraySizeModifier SM, unsigned TQ)
+  : ArrayType(ConstantArray, Et, Can, SM, TQ, SzPtr->SizeExpr),
+SizePtr(SzPtr) {
+ConstantArrayTypeBits.HasExternalSize = true;
+
+assert((SzPtr->SizeExpr == nullptr || !Can.isNull()) &&
+   "canonical constant array should not have size expression");
   }
 
+  static ConstantArrayType *Create(const ASTContext &Ctx, QualType ET,
+   QualType Can, const llvm::APInt &Sz,
+   const Expr *SzExpr, ArraySizeModifier SzMod,
+   unsigned Qual);
+
 public:
-  const llvm::APInt &getSize() const { return Size; }
+  /// Return the constant array size as an APInt.
+  llvm::APInt getSize() const {
+return ConstantArrayTypeBits.HasExternalSize
+   ? SizePtr->Size
+   : llvm::APInt(I.ByteWidth * 8, I.Size);
+  }
+
+  /// Return the bit width of the size type.
+  unsigned getSizeBitWidth() const {
+return ConstantArrayTypeBits.HasExternalSize
+   ? SizePtr->Size.getBitWidth()
+   : static_cast(I.ByteWidth * 8);
+  }
+
+  /// Return true if the size is zero.
+  bool isZeroSize() const {
+return ConstantArrayTypeBits.HasExternalSize ? SizePtr->Size.isZero()
+   : 0 == I.Size;
+  }
+
+  /// Return the size zero-extended as a uint64_t.
+  uint64_t getZExtSize() const {
+return ConstantArrayTypeBits.HasExternalSize
+   ? SizePtr->Size.getZExtValue()
+   : I.Size;
+  }
+
+  /// Return the size zero-extended as a uint64_t.

efriedma-quic wrote:

Sign-extended

https://github.com/llvm/llvm-project/pull/85716
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [NFC] Refactor ConstantArrayType size storage (PR #85716)

2024-03-25 Thread Eli Friedman via cfe-commits


@@ -3182,34 +3182,100 @@ class ArrayType : public Type, public 
llvm::FoldingSetNode {
 /// For example, the canonical type for 'int A[4 + 4*100]' is a
 /// ConstantArrayType where the element type is 'int' and the size is 404.
 class ConstantArrayType final
-: public ArrayType,
-  private llvm::TrailingObjects {
+: public ArrayType {
   friend class ASTContext; // ASTContext creates these.
-  friend TrailingObjects;
 
-  llvm::APInt Size; // Allows us to unique the type.
+  struct ExternalSize {
+ExternalSize(const llvm::APInt &Sz, const Expr *SE)
+: Size(Sz), SizeExpr(SE) {}
+llvm::APInt Size; // Allows us to unique the type.
+const Expr *SizeExpr;
+  };
+  struct InlineSize {
+InlineSize(uint64_t TSz, uint64_t Sz) : ByteWidth(TSz), Size(Sz) {}
+uint64_t ByteWidth : 4;

efriedma-quic wrote:

Do we actually need to store the ByteWidth here?  It's always just 
Target->getMaxPointerWidth().  Granted, it might be inconvenient to change the 
API... in which case, I guess we can store it here.

Maybe consider sticking the ByteWidth in ConstantArrayTypeBitfields; there 
should be a few spare bits available there.

https://github.com/llvm/llvm-project/pull/85716
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [NFC] Refactor ConstantArrayType size storage (PR #85716)

2024-03-25 Thread Eli Friedman via cfe-commits


@@ -3182,34 +3182,100 @@ class ArrayType : public Type, public 
llvm::FoldingSetNode {
 /// For example, the canonical type for 'int A[4 + 4*100]' is a
 /// ConstantArrayType where the element type is 'int' and the size is 404.
 class ConstantArrayType final
-: public ArrayType,
-  private llvm::TrailingObjects {
+: public ArrayType {
   friend class ASTContext; // ASTContext creates these.
-  friend TrailingObjects;
 
-  llvm::APInt Size; // Allows us to unique the type.
+  struct ExternalSize {
+ExternalSize(const llvm::APInt &Sz, const Expr *SE)
+: Size(Sz), SizeExpr(SE) {}
+llvm::APInt Size; // Allows us to unique the type.
+const Expr *SizeExpr;
+  };
+  struct InlineSize {
+InlineSize(uint64_t TSz, uint64_t Sz) : ByteWidth(TSz), Size(Sz) {}
+uint64_t ByteWidth : 4;
+uint64_t Size : 60;
+  };
+  union {
+struct InlineSize I;
+ExternalSize *SizePtr;
+  };

efriedma-quic wrote:

If I'm reading this correctly, this actually saves 8 bytes over the current 
representation?  That's nice.

https://github.com/llvm/llvm-project/pull/85716
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [NFC] Refactor ConstantArrayType size storage (PR #85716)

2024-03-25 Thread Eli Friedman via cfe-commits


@@ -3182,34 +3182,98 @@ class ArrayType : public Type, public 
llvm::FoldingSetNode {
 /// For example, the canonical type for 'int A[4 + 4*100]' is a
 /// ConstantArrayType where the element type is 'int' and the size is 404.
 class ConstantArrayType final
-: public ArrayType,
-  private llvm::TrailingObjects {
+: public ArrayType {
   friend class ASTContext; // ASTContext creates these.
-  friend TrailingObjects;
 
-  llvm::APInt Size; // Allows us to unique the type.
+  struct ExternalSize {
+ExternalSize(const llvm::APInt &Sz, const Expr *SE)
+: Size(Sz), SizeExpr(SE) {}
+llvm::APInt Size; // Allows us to unique the type.
+const Expr *SizeExpr;
+  };
+  struct InlineSize {
+InlineSize(uint64_t TSz, uint64_t Sz) : ByteWidth(TSz), Size(Sz) {}
+uint64_t ByteWidth : 4;
+uint64_t Size : 60;
+  };
+  union {
+struct InlineSize I;
+ExternalSize *SizePtr;
+  };
 
-  ConstantArrayType(QualType et, QualType can, const llvm::APInt &size,
-const Expr *sz, ArraySizeModifier sm, unsigned tq)
-  : ArrayType(ConstantArray, et, can, sm, tq, sz), Size(size) {
-ConstantArrayTypeBits.HasStoredSizeExpr = sz != nullptr;
-if (ConstantArrayTypeBits.HasStoredSizeExpr) {
-  assert(!can.isNull() && "canonical constant array should not have size");
-  *getTrailingObjects() = sz;
-}
+  ConstantArrayType(QualType Et, QualType Can, uint64_t Width, uint64_t Sz,
+ArraySizeModifier SM, unsigned TQ)
+  : ArrayType(ConstantArray, Et, Can, SM, TQ, nullptr), I(Width / 8, Sz) {
+ConstantArrayTypeBits.HasExternalSize = false;
+assert(Sz < 0x0FFF && "Size must fit in 60 bits");
+assert(Width < 0xFF && "Type width must fit in 8 bits");

efriedma-quic wrote:

You divide by 8, then truncate the bits above the lowest 4... so the max value 
you can actually store is 15*8==120.

https://github.com/llvm/llvm-project/pull/85716
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [NFC] Refactor ConstantArrayType size storage (PR #85716)

2024-03-25 Thread via cfe-commits

github-actions[bot] wrote:



:white_check_mark: With the latest revision this PR passed the Python code 
formatter.

https://github.com/llvm/llvm-project/pull/85716
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [NFC] Refactor ConstantArrayType size storage (PR #85716)

2024-03-25 Thread Chris B via cfe-commits

https://github.com/llvm-beanz updated 
https://github.com/llvm/llvm-project/pull/85716

>From 4a11a73b4dd41637b1d730489954c2994489d6be Mon Sep 17 00:00:00 2001
From: Chris Bieneman 
Date: Mon, 18 Mar 2024 17:30:41 -0500
Subject: [PATCH 1/2] [NFC] Refactor ConstantArrayType size storage

In PR #79382, I need to add a new type that derives from
ConstantArrayType. This means that ConstantArrayType can no longer use
`llvm::TrailingObjects` to store the trailing optional Expr*.

This change refactors ConstantArrayType to store a 60-bit integer and
4-bits for the integer size in bytes. This replaces the APInt field
previously in the type but preserves enough information to recreate it
where needed.

To reduce the number of places where the APInt is re-constructed I've
also added some helper methods to the ConstantArrayType to allow some
common use cases that operate on either the stored small integer or the
APInt as appropriate.

Resolves #85124.
---
 clang/include/clang/AST/Type.h| 104 ++
 clang/lib/AST/ASTContext.cpp  |  21 ++--
 clang/lib/AST/Decl.cpp|   2 +-
 clang/lib/AST/ExprConstant.cpp|  30 ++---
 clang/lib/AST/Interp/ByteCodeExprGen.cpp  |   8 +-
 clang/lib/AST/Interp/EvaluationResult.cpp |   2 +-
 clang/lib/AST/Interp/Program.cpp  |   2 +-
 clang/lib/AST/JSONNodeDumper.cpp  |   2 +-
 clang/lib/AST/MicrosoftMangle.cpp |   3 +-
 clang/lib/AST/ScanfFormatString.cpp   |   2 +-
 clang/lib/AST/Type.cpp|  20 +++-
 clang/lib/AST/TypePrinter.cpp |   2 +-
 clang/lib/Analysis/CFG.cpp|   4 +-
 clang/lib/Analysis/UnsafeBufferUsage.cpp  |   8 +-
 clang/lib/CodeGen/ABIInfo.cpp |   4 +-
 clang/lib/CodeGen/ABIInfoImpl.cpp |   4 +-
 clang/lib/CodeGen/CGCall.cpp  |   4 +-
 clang/lib/CodeGen/CGDebugInfo.cpp |   2 +-
 clang/lib/CodeGen/CGExprCXX.cpp   |   6 +-
 clang/lib/CodeGen/CGExprConstant.cpp  |   8 +-
 clang/lib/CodeGen/CGObjCMac.cpp   |   6 +-
 clang/lib/CodeGen/CGOpenMPRuntime.cpp |   4 +-
 clang/lib/CodeGen/CodeGenFunction.cpp |   4 +-
 clang/lib/CodeGen/CodeGenModule.cpp   |   2 +-
 clang/lib/CodeGen/CodeGenTypes.cpp|   2 +-
 clang/lib/CodeGen/SwiftCallingConv.cpp|   2 +-
 clang/lib/CodeGen/Targets/ARM.cpp |   2 +-
 clang/lib/CodeGen/Targets/LoongArch.cpp   |   2 +-
 clang/lib/CodeGen/Targets/RISCV.cpp   |   2 +-
 clang/lib/CodeGen/Targets/X86.cpp |   4 +-
 clang/lib/Sema/SemaChecking.cpp   |   8 +-
 clang/lib/Sema/SemaDecl.cpp   |   2 +-
 clang/lib/Sema/SemaDeclCXX.cpp|   2 +-
 clang/lib/Sema/SemaExpr.cpp   |   4 +-
 clang/lib/Sema/SemaExprCXX.cpp|   2 +-
 clang/lib/Sema/SemaInit.cpp   |  20 ++--
 clang/lib/Sema/SemaOpenMP.cpp |   6 +-
 clang/lib/Sema/SemaSYCL.cpp   |   2 +-
 .../Checkers/CastSizeChecker.cpp  |   2 +-
 .../Checkers/PaddingChecker.cpp   |   2 +-
 clang/lib/StaticAnalyzer/Core/MemRegion.cpp   |   2 +-
 clang/lib/StaticAnalyzer/Core/RegionStore.cpp |   8 +-
 42 files changed, 201 insertions(+), 127 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 10916053cdfbf5..f8739fd54f33d1 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -1689,7 +1689,7 @@ class alignas(TypeAlignment) Type : public 
ExtQualsTypeCommonBase {
 
 /// Whether we have a stored size expression.
 LLVM_PREFERRED_TYPE(bool)
-unsigned HasStoredSizeExpr : 1;
+unsigned HasExternalSize : 1;
   };
 
   class BuiltinTypeBitfields {
@@ -3182,34 +3182,98 @@ class ArrayType : public Type, public 
llvm::FoldingSetNode {
 /// For example, the canonical type for 'int A[4 + 4*100]' is a
 /// ConstantArrayType where the element type is 'int' and the size is 404.
 class ConstantArrayType final
-: public ArrayType,
-  private llvm::TrailingObjects {
+: public ArrayType {
   friend class ASTContext; // ASTContext creates these.
-  friend TrailingObjects;
 
-  llvm::APInt Size; // Allows us to unique the type.
+  struct ExternalSize {
+ExternalSize(const llvm::APInt &Sz, const Expr *SE)
+: Size(Sz), SizeExpr(SE) {}
+llvm::APInt Size; // Allows us to unique the type.
+const Expr *SizeExpr;
+  };
+  struct InlineSize {
+InlineSize(uint64_t TSz, uint64_t Sz) : ByteWidth(TSz), Size(Sz) {}
+uint64_t ByteWidth : 4;
+uint64_t Size : 60;
+  };
+  union {
+struct InlineSize I;
+ExternalSize *SizePtr;
+  };
 
-  ConstantArrayType(QualType et, QualType can, const llvm::APInt &size,
-const Expr *sz, ArraySizeModifier sm, unsigned tq)
-  : ArrayType(ConstantArray, et, can, sm, tq, sz)

[clang] [NFC] Refactor ConstantArrayType size storage (PR #85716)

2024-03-19 Thread Chris B via cfe-commits


@@ -3182,34 +3182,98 @@ class ArrayType : public Type, public 
llvm::FoldingSetNode {
 /// For example, the canonical type for 'int A[4 + 4*100]' is a
 /// ConstantArrayType where the element type is 'int' and the size is 404.
 class ConstantArrayType final
-: public ArrayType,
-  private llvm::TrailingObjects {
+: public ArrayType {
   friend class ASTContext; // ASTContext creates these.
-  friend TrailingObjects;
 
-  llvm::APInt Size; // Allows us to unique the type.
+  struct ExternalSize {
+ExternalSize(const llvm::APInt &Sz, const Expr *SE)
+: Size(Sz), SizeExpr(SE) {}
+llvm::APInt Size; // Allows us to unique the type.
+const Expr *SizeExpr;
+  };
+  struct InlineSize {
+InlineSize(uint64_t TSz, uint64_t Sz) : ByteWidth(TSz), Size(Sz) {}
+uint64_t ByteWidth : 4;
+uint64_t Size : 60;
+  };
+  union {
+struct InlineSize I;
+ExternalSize *SizePtr;
+  };
 
-  ConstantArrayType(QualType et, QualType can, const llvm::APInt &size,
-const Expr *sz, ArraySizeModifier sm, unsigned tq)
-  : ArrayType(ConstantArray, et, can, sm, tq, sz), Size(size) {
-ConstantArrayTypeBits.HasStoredSizeExpr = sz != nullptr;
-if (ConstantArrayTypeBits.HasStoredSizeExpr) {
-  assert(!can.isNull() && "canonical constant array should not have size");
-  *getTrailingObjects() = sz;
-}
+  ConstantArrayType(QualType Et, QualType Can, uint64_t Width, uint64_t Sz,
+ArraySizeModifier SM, unsigned TQ)
+  : ArrayType(ConstantArray, Et, Can, SM, TQ, nullptr), I(Width / 8, Sz) {
+ConstantArrayTypeBits.HasExternalSize = false;
+assert(Sz < 0x0FFF && "Size must fit in 60 bits");
+assert(Width < 0xFF && "Type width must fit in 8 bits");

llvm-beanz wrote:

I guess that's poorly worded. I verify that Width is within 8, but I only store 
4 since the least significant bits are always zero.

https://github.com/llvm/llvm-project/pull/85716
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [NFC] Refactor ConstantArrayType size storage (PR #85716)

2024-03-18 Thread Timm Baeder via cfe-commits


@@ -3182,34 +3182,98 @@ class ArrayType : public Type, public 
llvm::FoldingSetNode {
 /// For example, the canonical type for 'int A[4 + 4*100]' is a
 /// ConstantArrayType where the element type is 'int' and the size is 404.
 class ConstantArrayType final
-: public ArrayType,
-  private llvm::TrailingObjects {
+: public ArrayType {
   friend class ASTContext; // ASTContext creates these.
-  friend TrailingObjects;
 
-  llvm::APInt Size; // Allows us to unique the type.
+  struct ExternalSize {
+ExternalSize(const llvm::APInt &Sz, const Expr *SE)
+: Size(Sz), SizeExpr(SE) {}
+llvm::APInt Size; // Allows us to unique the type.
+const Expr *SizeExpr;
+  };
+  struct InlineSize {
+InlineSize(uint64_t TSz, uint64_t Sz) : ByteWidth(TSz), Size(Sz) {}
+uint64_t ByteWidth : 4;
+uint64_t Size : 60;
+  };
+  union {
+struct InlineSize I;
+ExternalSize *SizePtr;
+  };
 
-  ConstantArrayType(QualType et, QualType can, const llvm::APInt &size,
-const Expr *sz, ArraySizeModifier sm, unsigned tq)
-  : ArrayType(ConstantArray, et, can, sm, tq, sz), Size(size) {
-ConstantArrayTypeBits.HasStoredSizeExpr = sz != nullptr;
-if (ConstantArrayTypeBits.HasStoredSizeExpr) {
-  assert(!can.isNull() && "canonical constant array should not have size");
-  *getTrailingObjects() = sz;
-}
+  ConstantArrayType(QualType Et, QualType Can, uint64_t Width, uint64_t Sz,
+ArraySizeModifier SM, unsigned TQ)
+  : ArrayType(ConstantArray, Et, Can, SM, TQ, nullptr), I(Width / 8, Sz) {
+ConstantArrayTypeBits.HasExternalSize = false;
+assert(Sz < 0x0FFF && "Size must fit in 60 bits");
+assert(Width < 0xFF && "Type width must fit in 8 bits");

tbaederr wrote:

8 or 4?

https://github.com/llvm/llvm-project/pull/85716
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [NFC] Refactor ConstantArrayType size storage (PR #85716)

2024-03-18 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff 4841858862df4b8ac4ac68922086f03c8bbd3dc2 
4a11a73b4dd41637b1d730489954c2994489d6be -- clang/include/clang/AST/Type.h 
clang/lib/AST/ASTContext.cpp clang/lib/AST/Decl.cpp 
clang/lib/AST/ExprConstant.cpp clang/lib/AST/Interp/ByteCodeExprGen.cpp 
clang/lib/AST/Interp/EvaluationResult.cpp clang/lib/AST/Interp/Program.cpp 
clang/lib/AST/JSONNodeDumper.cpp clang/lib/AST/MicrosoftMangle.cpp 
clang/lib/AST/ScanfFormatString.cpp clang/lib/AST/Type.cpp 
clang/lib/AST/TypePrinter.cpp clang/lib/Analysis/CFG.cpp 
clang/lib/Analysis/UnsafeBufferUsage.cpp clang/lib/CodeGen/ABIInfo.cpp 
clang/lib/CodeGen/ABIInfoImpl.cpp clang/lib/CodeGen/CGCall.cpp 
clang/lib/CodeGen/CGDebugInfo.cpp clang/lib/CodeGen/CGExprCXX.cpp 
clang/lib/CodeGen/CGExprConstant.cpp clang/lib/CodeGen/CGObjCMac.cpp 
clang/lib/CodeGen/CGOpenMPRuntime.cpp clang/lib/CodeGen/CodeGenFunction.cpp 
clang/lib/CodeGen/CodeGenModule.cpp clang/lib/CodeGen/CodeGenTypes.cpp 
clang/lib/CodeGen/SwiftCallingConv.cpp clang/lib/CodeGen/Targets/ARM.cpp 
clang/lib/CodeGen/Targets/LoongArch.cpp clang/lib/CodeGen/Targets/RISCV.cpp 
clang/lib/CodeGen/Targets/X86.cpp clang/lib/Sema/SemaChecking.cpp 
clang/lib/Sema/SemaDecl.cpp clang/lib/Sema/SemaDeclCXX.cpp 
clang/lib/Sema/SemaExpr.cpp clang/lib/Sema/SemaExprCXX.cpp 
clang/lib/Sema/SemaInit.cpp clang/lib/Sema/SemaOpenMP.cpp 
clang/lib/Sema/SemaSYCL.cpp 
clang/lib/StaticAnalyzer/Checkers/CastSizeChecker.cpp 
clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp 
clang/lib/StaticAnalyzer/Core/MemRegion.cpp 
clang/lib/StaticAnalyzer/Core/RegionStore.cpp
``





View the diff from clang-format here.


``diff
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index f8739fd54f..f7d7b7f577 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -3181,8 +3181,7 @@ public:
 /// Represents the canonical version of C arrays with a specified constant 
size.
 /// For example, the canonical type for 'int A[4 + 4*100]' is a
 /// ConstantArrayType where the element type is 'int' and the size is 404.
-class ConstantArrayType final
-: public ArrayType {
+class ConstantArrayType final : public ArrayType {
   friend class ASTContext; // ASTContext creates these.
 
   struct ExternalSize {
@@ -3242,21 +3241,19 @@ public:
   /// Return true if the size is zero.
   bool isZeroSize() const {
 return ConstantArrayTypeBits.HasExternalSize ? SizePtr->Size.isZero()
-   : 0 == I.Size;
+ : 0 == I.Size;
   }
 
   /// Return the size zero-extended as a uint64_t.
   uint64_t getZExtSize() const {
-return ConstantArrayTypeBits.HasExternalSize
-   ? SizePtr->Size.getZExtValue()
-   : I.Size;
+return ConstantArrayTypeBits.HasExternalSize ? SizePtr->Size.getZExtValue()
+ : I.Size;
   }
 
   /// Return the size zero-extended as a uint64_t.
   int64_t getSExtSize() const {
-return ConstantArrayTypeBits.HasExternalSize
-   ? SizePtr->Size.getSExtValue()
-   : static_cast(I.Size);
+return ConstantArrayTypeBits.HasExternalSize ? SizePtr->Size.getSExtValue()
+ : 
static_cast(I.Size);
   }
 
   /// Return the size zero-extended to uint64_t or UINT64_MAX if the value is
@@ -3269,9 +3266,7 @@ public:
 
   /// Return a pointer to the size expression.
   const Expr *getSizeExpr() const {
-return ConstantArrayTypeBits.HasExternalSize
-   ? SizePtr->SizeExpr
-   : nullptr;
+return ConstantArrayTypeBits.HasExternalSize ? SizePtr->SizeExpr : nullptr;
   }
 
   bool isSugared() const { return false; }
@@ -3295,9 +3290,8 @@ public:
   }
 
   static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx,
-  QualType ET, uint64_t ArraySize,
-  const Expr *SizeExpr, ArraySizeModifier SizeMod,
-  unsigned TypeQuals);
+  QualType ET, uint64_t ArraySize, const Expr *SizeExpr,
+  ArraySizeModifier SizeMod, unsigned TypeQuals);
 
   static bool classof(const Type *T) {
 return T->getTypeClass() == ConstantArray;
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 7889dc00e5..c5bdff0df0 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -10996,8 +10996,7 @@ namespace {
 return Error(E);
   }
 
-  Result = APValue(APValue::UninitArray(), 0,
-   CAT->getZExtSize());
+  Result = APValue(APValue::UninitArray(), 0, CAT->getZExtSize());
   if (!Result.hasArrayFiller())
 return true;
 
diff --git a/clang/l

[clang] [NFC] Refactor ConstantArrayType size storage (PR #85716)

2024-03-18 Thread via cfe-commits

llvmbot wrote:



@llvm/pr-subscribers-clang-analysis
@llvm/pr-subscribers-backend-risc-v

@llvm/pr-subscribers-backend-arm

Author: Chris B (llvm-beanz)


Changes

In PR #79382, I need to add a new type that derives from 
ConstantArrayType. This means that ConstantArrayType can no longer use 
`llvm::TrailingObjects` to store the trailing optional Expr*.

This change refactors ConstantArrayType to store a 60-bit integer and 4-bits 
for the integer size in bytes. This replaces the APInt field previously in the 
type but preserves enough information to recreate it where needed.

To reduce the number of places where the APInt is re-constructed I've also 
added some helper methods to the ConstantArrayType to allow some common use 
cases that operate on either the stored small integer or the APInt as 
appropriate.

Resolves #85124.

---

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


42 Files Affected:

- (modified) clang/include/clang/AST/Type.h (+84-20) 
- (modified) clang/lib/AST/ASTContext.cpp (+9-12) 
- (modified) clang/lib/AST/Decl.cpp (+1-1) 
- (modified) clang/lib/AST/ExprConstant.cpp (+15-15) 
- (modified) clang/lib/AST/Interp/ByteCodeExprGen.cpp (+4-4) 
- (modified) clang/lib/AST/Interp/EvaluationResult.cpp (+1-1) 
- (modified) clang/lib/AST/Interp/Program.cpp (+1-1) 
- (modified) clang/lib/AST/JSONNodeDumper.cpp (+1-1) 
- (modified) clang/lib/AST/MicrosoftMangle.cpp (+1-2) 
- (modified) clang/lib/AST/ScanfFormatString.cpp (+1-1) 
- (modified) clang/lib/AST/Type.cpp (+18-2) 
- (modified) clang/lib/AST/TypePrinter.cpp (+1-1) 
- (modified) clang/lib/Analysis/CFG.cpp (+2-2) 
- (modified) clang/lib/Analysis/UnsafeBufferUsage.cpp (+4-4) 
- (modified) clang/lib/CodeGen/ABIInfo.cpp (+2-2) 
- (modified) clang/lib/CodeGen/ABIInfoImpl.cpp (+2-2) 
- (modified) clang/lib/CodeGen/CGCall.cpp (+2-2) 
- (modified) clang/lib/CodeGen/CGDebugInfo.cpp (+1-1) 
- (modified) clang/lib/CodeGen/CGExprCXX.cpp (+2-4) 
- (modified) clang/lib/CodeGen/CGExprConstant.cpp (+4-4) 
- (modified) clang/lib/CodeGen/CGObjCMac.cpp (+3-3) 
- (modified) clang/lib/CodeGen/CGOpenMPRuntime.cpp (+2-2) 
- (modified) clang/lib/CodeGen/CodeGenFunction.cpp (+2-2) 
- (modified) clang/lib/CodeGen/CodeGenModule.cpp (+1-1) 
- (modified) clang/lib/CodeGen/CodeGenTypes.cpp (+1-1) 
- (modified) clang/lib/CodeGen/SwiftCallingConv.cpp (+1-1) 
- (modified) clang/lib/CodeGen/Targets/ARM.cpp (+1-1) 
- (modified) clang/lib/CodeGen/Targets/LoongArch.cpp (+1-1) 
- (modified) clang/lib/CodeGen/Targets/RISCV.cpp (+1-1) 
- (modified) clang/lib/CodeGen/Targets/X86.cpp (+2-2) 
- (modified) clang/lib/Sema/SemaChecking.cpp (+4-4) 
- (modified) clang/lib/Sema/SemaDecl.cpp (+1-1) 
- (modified) clang/lib/Sema/SemaDeclCXX.cpp (+1-1) 
- (modified) clang/lib/Sema/SemaExpr.cpp (+2-2) 
- (modified) clang/lib/Sema/SemaExprCXX.cpp (+1-1) 
- (modified) clang/lib/Sema/SemaInit.cpp (+10-10) 
- (modified) clang/lib/Sema/SemaOpenMP.cpp (+3-3) 
- (modified) clang/lib/Sema/SemaSYCL.cpp (+1-1) 
- (modified) clang/lib/StaticAnalyzer/Checkers/CastSizeChecker.cpp (+1-1) 
- (modified) clang/lib/StaticAnalyzer/Checkers/PaddingChecker.cpp (+1-1) 
- (modified) clang/lib/StaticAnalyzer/Core/MemRegion.cpp (+1-1) 
- (modified) clang/lib/StaticAnalyzer/Core/RegionStore.cpp (+4-4) 


``diff
diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 10916053cdfbf5..f8739fd54f33d1 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -1689,7 +1689,7 @@ class alignas(TypeAlignment) Type : public 
ExtQualsTypeCommonBase {
 
 /// Whether we have a stored size expression.
 LLVM_PREFERRED_TYPE(bool)
-unsigned HasStoredSizeExpr : 1;
+unsigned HasExternalSize : 1;
   };
 
   class BuiltinTypeBitfields {
@@ -3182,34 +3182,98 @@ class ArrayType : public Type, public 
llvm::FoldingSetNode {
 /// For example, the canonical type for 'int A[4 + 4*100]' is a
 /// ConstantArrayType where the element type is 'int' and the size is 404.
 class ConstantArrayType final
-: public ArrayType,
-  private llvm::TrailingObjects {
+: public ArrayType {
   friend class ASTContext; // ASTContext creates these.
-  friend TrailingObjects;
 
-  llvm::APInt Size; // Allows us to unique the type.
+  struct ExternalSize {
+ExternalSize(const llvm::APInt &Sz, const Expr *SE)
+: Size(Sz), SizeExpr(SE) {}
+llvm::APInt Size; // Allows us to unique the type.
+const Expr *SizeExpr;
+  };
+  struct InlineSize {
+InlineSize(uint64_t TSz, uint64_t Sz) : ByteWidth(TSz), Size(Sz) {}
+uint64_t ByteWidth : 4;
+uint64_t Size : 60;
+  };
+  union {
+struct InlineSize I;
+ExternalSize *SizePtr;
+  };
 
-  ConstantArrayType(QualType et, QualType can, const llvm::APInt &size,
-const Expr *sz, ArraySizeModifier sm, unsigned tq)
-  : ArrayType(ConstantArray, et, can, sm, tq, sz), Size(size) {
-ConstantArrayTypeBits.HasStoredSize

[clang] [NFC] Refactor ConstantArrayType size storage (PR #85716)

2024-03-18 Thread Chris B via cfe-commits

https://github.com/llvm-beanz created 
https://github.com/llvm/llvm-project/pull/85716

In PR #79382, I need to add a new type that derives from ConstantArrayType. 
This means that ConstantArrayType can no longer use `llvm::TrailingObjects` to 
store the trailing optional Expr*.

This change refactors ConstantArrayType to store a 60-bit integer and 4-bits 
for the integer size in bytes. This replaces the APInt field previously in the 
type but preserves enough information to recreate it where needed.

To reduce the number of places where the APInt is re-constructed I've also 
added some helper methods to the ConstantArrayType to allow some common use 
cases that operate on either the stored small integer or the APInt as 
appropriate.

Resolves #85124.

>From 4a11a73b4dd41637b1d730489954c2994489d6be Mon Sep 17 00:00:00 2001
From: Chris Bieneman 
Date: Mon, 18 Mar 2024 17:30:41 -0500
Subject: [PATCH] [NFC] Refactor ConstantArrayType size storage

In PR #79382, I need to add a new type that derives from
ConstantArrayType. This means that ConstantArrayType can no longer use
`llvm::TrailingObjects` to store the trailing optional Expr*.

This change refactors ConstantArrayType to store a 60-bit integer and
4-bits for the integer size in bytes. This replaces the APInt field
previously in the type but preserves enough information to recreate it
where needed.

To reduce the number of places where the APInt is re-constructed I've
also added some helper methods to the ConstantArrayType to allow some
common use cases that operate on either the stored small integer or the
APInt as appropriate.

Resolves #85124.
---
 clang/include/clang/AST/Type.h| 104 ++
 clang/lib/AST/ASTContext.cpp  |  21 ++--
 clang/lib/AST/Decl.cpp|   2 +-
 clang/lib/AST/ExprConstant.cpp|  30 ++---
 clang/lib/AST/Interp/ByteCodeExprGen.cpp  |   8 +-
 clang/lib/AST/Interp/EvaluationResult.cpp |   2 +-
 clang/lib/AST/Interp/Program.cpp  |   2 +-
 clang/lib/AST/JSONNodeDumper.cpp  |   2 +-
 clang/lib/AST/MicrosoftMangle.cpp |   3 +-
 clang/lib/AST/ScanfFormatString.cpp   |   2 +-
 clang/lib/AST/Type.cpp|  20 +++-
 clang/lib/AST/TypePrinter.cpp |   2 +-
 clang/lib/Analysis/CFG.cpp|   4 +-
 clang/lib/Analysis/UnsafeBufferUsage.cpp  |   8 +-
 clang/lib/CodeGen/ABIInfo.cpp |   4 +-
 clang/lib/CodeGen/ABIInfoImpl.cpp |   4 +-
 clang/lib/CodeGen/CGCall.cpp  |   4 +-
 clang/lib/CodeGen/CGDebugInfo.cpp |   2 +-
 clang/lib/CodeGen/CGExprCXX.cpp   |   6 +-
 clang/lib/CodeGen/CGExprConstant.cpp  |   8 +-
 clang/lib/CodeGen/CGObjCMac.cpp   |   6 +-
 clang/lib/CodeGen/CGOpenMPRuntime.cpp |   4 +-
 clang/lib/CodeGen/CodeGenFunction.cpp |   4 +-
 clang/lib/CodeGen/CodeGenModule.cpp   |   2 +-
 clang/lib/CodeGen/CodeGenTypes.cpp|   2 +-
 clang/lib/CodeGen/SwiftCallingConv.cpp|   2 +-
 clang/lib/CodeGen/Targets/ARM.cpp |   2 +-
 clang/lib/CodeGen/Targets/LoongArch.cpp   |   2 +-
 clang/lib/CodeGen/Targets/RISCV.cpp   |   2 +-
 clang/lib/CodeGen/Targets/X86.cpp |   4 +-
 clang/lib/Sema/SemaChecking.cpp   |   8 +-
 clang/lib/Sema/SemaDecl.cpp   |   2 +-
 clang/lib/Sema/SemaDeclCXX.cpp|   2 +-
 clang/lib/Sema/SemaExpr.cpp   |   4 +-
 clang/lib/Sema/SemaExprCXX.cpp|   2 +-
 clang/lib/Sema/SemaInit.cpp   |  20 ++--
 clang/lib/Sema/SemaOpenMP.cpp |   6 +-
 clang/lib/Sema/SemaSYCL.cpp   |   2 +-
 .../Checkers/CastSizeChecker.cpp  |   2 +-
 .../Checkers/PaddingChecker.cpp   |   2 +-
 clang/lib/StaticAnalyzer/Core/MemRegion.cpp   |   2 +-
 clang/lib/StaticAnalyzer/Core/RegionStore.cpp |   8 +-
 42 files changed, 201 insertions(+), 127 deletions(-)

diff --git a/clang/include/clang/AST/Type.h b/clang/include/clang/AST/Type.h
index 10916053cdfbf5..f8739fd54f33d1 100644
--- a/clang/include/clang/AST/Type.h
+++ b/clang/include/clang/AST/Type.h
@@ -1689,7 +1689,7 @@ class alignas(TypeAlignment) Type : public 
ExtQualsTypeCommonBase {
 
 /// Whether we have a stored size expression.
 LLVM_PREFERRED_TYPE(bool)
-unsigned HasStoredSizeExpr : 1;
+unsigned HasExternalSize : 1;
   };
 
   class BuiltinTypeBitfields {
@@ -3182,34 +3182,98 @@ class ArrayType : public Type, public 
llvm::FoldingSetNode {
 /// For example, the canonical type for 'int A[4 + 4*100]' is a
 /// ConstantArrayType where the element type is 'int' and the size is 404.
 class ConstantArrayType final
-: public ArrayType,
-  private llvm::TrailingObjects {
+: public ArrayType {
   friend class ASTContext; // ASTContext creates these.
-  friend TrailingObjects;
 
-  llvm::APInt Size