[clang] [clang] Implement constexpr heap allocation alignments (PR #174549)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 25 05:25:37 PDT 2026


================
@@ -63,31 +64,49 @@ class TypeInfoLValue {
 
 /// Symbolic representation of a dynamic allocation.
 class DynamicAllocLValue {
-  unsigned Index;
+public:
+  static constexpr int NumLowBitsAvailable = 2;
+  static constexpr int NumAlignmentBits = 5;
+
+private:
+  // lower NumAlignmentBits: alignment exponent
+  // remaining bits: allocation index incremented by one
+  // value of zero indicates distinct empty state
+  uintptr_t Align : NumAlignmentBits;
+  uintptr_t Index : sizeof(uintptr_t) * CHAR_BIT - NumAlignmentBits;
 
 public:
-  DynamicAllocLValue() : Index(0) {}
-  explicit DynamicAllocLValue(unsigned Index) : Index(Index + 1) {}
-  unsigned getIndex() { return Index - 1; }
+  DynamicAllocLValue() : Align(0), Index(0) {}
+  explicit DynamicAllocLValue(unsigned Idx, uint64_t Align)
+      : Align(llvm::countr_zero(Align)), Index(Idx + 1) {
+    assert(Align > 0 && "Invalid alignment for DynamicAllocLValue constructor");
----------------
tbaederr wrote:

As for assertions, I guess it would make sense to assert that `Align` is a power of 2 and that `llvm::countr_zero(Align)` actually fits into the 2 bits we have for it.

https://github.com/llvm/llvm-project/pull/174549


More information about the cfe-commits mailing list