[clang] [clang][bytecode] Add `LocalInlineDesc` for local variables (PR #239505)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 6 00:19:56 PST 2026


https://github.com/tbaederr updated https://github.com/llvm/llvm-project/pull/239505

>From 343ffc23e778a37fd296947261a1a89342e18dde Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Fri, 6 Nov 2026 08:38:04 +0100
Subject: [PATCH] LocalInlineDesc

---
 clang/lib/AST/ByteCode/Block.h              |  4 ++--
 clang/lib/AST/ByteCode/Descriptor.h         | 13 ++++++++++++-
 clang/lib/AST/ByteCode/DynamicAllocator.cpp | 16 ++++------------
 clang/lib/AST/ByteCode/EvalEmitter.cpp      | 21 ++++++++-------------
 clang/lib/AST/ByteCode/Interp.cpp           | 11 ++++++-----
 clang/lib/AST/ByteCode/InterpFrame.cpp      |  8 +-------
 clang/lib/AST/ByteCode/InterpFrame.h        | 14 ++++++++------
 clang/lib/AST/ByteCode/Pointer.cpp          | 12 ++++++------
 clang/lib/AST/ByteCode/Pointer.h            |  2 +-
 9 files changed, 48 insertions(+), 53 deletions(-)

diff --git a/clang/lib/AST/ByteCode/Block.h b/clang/lib/AST/ByteCode/Block.h
index 5a08b5dee64fec..f166d5a608f4de 100644
--- a/clang/lib/AST/ByteCode/Block.h
+++ b/clang/lib/AST/ByteCode/Block.h
@@ -91,7 +91,7 @@ class Block final {
     case Global:
       return sizeof(GlobalInlineDescriptor);
     case Local:
-      return sizeof(InlineDescriptor);
+      return sizeof(LocalInlineDescriptor);
     }
     llvm_unreachable("Unhandled Block kind");
   }
@@ -150,7 +150,7 @@ class Block final {
 
     if constexpr (std::is_same_v<T, GlobalInlineDescriptor>)
       assert(Kind == Global);
-    else if constexpr (std::is_same_v<T, InlineDescriptor>)
+    else if constexpr (std::is_same_v<T, LocalInlineDescriptor>)
       assert(Kind == Local);
     else
       assert(Kind == None);
diff --git a/clang/lib/AST/ByteCode/Descriptor.h b/clang/lib/AST/ByteCode/Descriptor.h
index d344f68ef318e0..da0e4c65bb4f80 100644
--- a/clang/lib/AST/ByteCode/Descriptor.h
+++ b/clang/lib/AST/ByteCode/Descriptor.h
@@ -57,6 +57,18 @@ enum class Lifetime : uint8_t {
   Ended,
 };
 
+struct alignas(void *) LocalInlineDescriptor {
+  bool IsEnabled;
+  bool IsInitialized;
+  Lifetime LifeState;
+
+  LocalInlineDescriptor()
+      : IsEnabled(false), IsInitialized(false), LifeState(Lifetime::Started) {}
+  Lifetime getLifetime() const { return LifeState; }
+  void setLifetime(Lifetime L) { LifeState = L; }
+};
+static_assert(sizeof(LocalInlineDescriptor) == sizeof(void *));
+
 /// Inline descriptor embedded in structures and arrays.
 ///
 /// Such descriptors precede all composite array elements and structure fields.
@@ -115,7 +127,6 @@ struct InlineDescriptor {
   void dump() const { dump(llvm::errs()); }
   void dump(llvm::raw_ostream &OS) const;
 };
-static_assert(sizeof(GlobalInlineDescriptor) != sizeof(InlineDescriptor), "");
 
 /// Describes a memory block created by an allocation site.
 struct Descriptor final {
diff --git a/clang/lib/AST/ByteCode/DynamicAllocator.cpp b/clang/lib/AST/ByteCode/DynamicAllocator.cpp
index b7f765a6e6d501..be02fbf0158df6 100644
--- a/clang/lib/AST/ByteCode/DynamicAllocator.cpp
+++ b/clang/lib/AST/ByteCode/DynamicAllocator.cpp
@@ -72,21 +72,13 @@ Block *DynamicAllocator::allocate(const Descriptor *D, unsigned EvalID,
       std::make_unique<std::byte[]>(Block::allocSize(D, Block::Local));
   auto *B = new (Memory.get()) Block(EvalID, D, Block::Local);
   B->invokeCtorNoMemset();
-  InlineDescriptor &ID = B->getBlockDesc<InlineDescriptor>();
-  ID.Desc = D;
-  ID.IsActive = true;
-  ID.Offset = sizeof(InlineDescriptor);
-  ID.IsBase = false;
-  ID.IsFieldMutable = false;
-  ID.IsConst = false;
-  ID.IsInitialized = false;
-  ID.IsVolatile = false;
+  auto &ID = B->getBlockDesc<LocalInlineDescriptor>();
 
   if (D->isCompositeArray())
-    ID.LifeState = Lifetime::Started;
+    ID.setLifetime(Lifetime::Started);
   else
-    ID.LifeState =
-        AllocForm == Form::Operator ? Lifetime::Ended : Lifetime::Started;
+    ID.setLifetime(AllocForm == Form::Operator ? Lifetime::Ended
+                                               : Lifetime::Started);
 
   if (auto It = AllocationSites.find(D->asExpr());
       It != AllocationSites.end()) {
diff --git a/clang/lib/AST/ByteCode/EvalEmitter.cpp b/clang/lib/AST/ByteCode/EvalEmitter.cpp
index d3d0b9677bc96c..c7289893e47c66 100644
--- a/clang/lib/AST/ByteCode/EvalEmitter.cpp
+++ b/clang/lib/AST/ByteCode/EvalEmitter.cpp
@@ -158,8 +158,8 @@ Scope::Local EvalEmitter::createLocal(const Descriptor *D,
   B->invokeCtor();
 
   // Initialize local variable inline descriptor.
-  auto &Desc = B->getBlockDesc<InlineDescriptor>();
-  new (&Desc) InlineDescriptor(D);
+  auto &Desc = B->getBlockDesc<LocalInlineDescriptor>();
+  new (&Desc) LocalInlineDescriptor();
 
   // Register the local.
   unsigned Off = Locals.size();
@@ -384,9 +384,9 @@ bool EvalEmitter::emitSetLocal(uint32_t I, SourceInfo Info) {
 
   Block *B = getLocal(I);
   B->deref<T>() = S.Stk.pop<T>();
-  auto &Desc = B->getBlockDesc<InlineDescriptor>();
+  auto &Desc = B->getBlockDesc<LocalInlineDescriptor>();
   Desc.IsInitialized = true;
-  Desc.LifeState = Lifetime::Started;
+  Desc.setLifetime(Lifetime::Started);
 
   return true;
 }
@@ -408,9 +408,9 @@ bool EvalEmitter::emitGetLocalEnabled(uint32_t I, SourceInfo Info) {
     return true;
 
   Block *B = getLocal(I);
-  const auto &Desc = B->getBlockDesc<InlineDescriptor>();
+  const auto &Desc = B->getBlockDesc<LocalInlineDescriptor>();
 
-  S.Stk.push<bool>(Desc.IsActive);
+  S.Stk.push<bool>(Desc.IsEnabled);
   return true;
 }
 
@@ -418,14 +418,9 @@ bool EvalEmitter::emitEnableLocal(uint32_t I, SourceInfo Info) {
   if (!isActive())
     return true;
 
-  // FIXME: This is a little dirty, but to avoid adding a flag to
-  // InlineDescriptor that's only ever useful on the toplevel of local
-  // variables, we reuse the IsActive flag for the enabled state. We should
-  // probably use a different struct than InlineDescriptor for the block-level
-  // inline descriptor of local varaibles.
   Block *B = getLocal(I);
-  auto &Desc = B->getBlockDesc<InlineDescriptor>();
-  Desc.IsActive = true;
+  auto &Desc = B->getBlockDesc<LocalInlineDescriptor>();
+  Desc.IsEnabled = true;
   return true;
 }
 
diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp
index 5caede7d1a149d..429dea3eaf9a27 100644
--- a/clang/lib/AST/ByteCode/Interp.cpp
+++ b/clang/lib/AST/ByteCode/Interp.cpp
@@ -892,11 +892,12 @@ bool CheckGlobalLoad(InterpState &S, CodePtr OpPC, const Block *B) {
 // Similarly, for local loads.
 bool CheckLocalLoad(InterpState &S, CodePtr OpPC, const Block *B) {
   assert(!B->isExtern());
-  const auto &Desc = B->getBlockDesc<InlineDescriptor>();
+  const auto &Desc = B->getBlockDesc<LocalInlineDescriptor>();
   const Descriptor *BlockDesc = B->getDescriptor();
   if (!Desc.IsInitialized)
-    return diagnoseUninitialized(S, OpPC, /*Extern=*/false, B, Desc.LifeState);
-  if (!CheckLifetime(S, OpPC, Desc.LifeState, B, AK_Read))
+    return diagnoseUninitialized(S, OpPC, /*Extern=*/false, B,
+                                 Desc.getLifetime());
+  if (!CheckLifetime(S, OpPC, Desc.getLifetime(), B, AK_Read))
     return false;
   if (BlockDesc->IsVolatile) {
     if (!S.getLangOpts().CPlusPlus)
@@ -3061,7 +3062,7 @@ bool handleReference(InterpState &S, CodePtr OpPC, Block *B) {
     return true;
   }
 
-  const auto &ID = B->getBlockDesc<InlineDescriptor>();
+  const auto &ID = B->getBlockDesc<LocalInlineDescriptor>();
   if (!ID.IsInitialized) {
     if (!S.checkingPotentialConstantExpression())
       S.FFDiag(S.Current->getSource(OpPC),
@@ -3360,7 +3361,7 @@ bool Destroy(InterpState &S, CodePtr OpPC, uint32_t I) {
     if (!LocalBlock->isInitialized())
       continue;
 
-    if (LocalBlock->getBlockDesc<InlineDescriptor>().LifeState ==
+    if (LocalBlock->getBlockDesc<LocalInlineDescriptor>().getLifetime() ==
         Lifetime::Ended) {
       const Pointer Ptr = S.Current->getLocalPointer(Local.Offset);
       return diagnoseOutOfLifetimeDestroy(S, OpPC, Ptr);
diff --git a/clang/lib/AST/ByteCode/InterpFrame.cpp b/clang/lib/AST/ByteCode/InterpFrame.cpp
index 52486444820498..df9bfe3d426e9b 100644
--- a/clang/lib/AST/ByteCode/InterpFrame.cpp
+++ b/clang/lib/AST/ByteCode/InterpFrame.cpp
@@ -89,13 +89,7 @@ InterpFrame::~InterpFrame() {
 
 void InterpFrame::enableLocal(unsigned Offset) {
   assert(Func);
-
-  // FIXME: This is a little dirty, but to avoid adding a flag to
-  // InlineDescriptor that's only ever useful on the toplevel of local
-  // variables, we reuse the IsActive flag for the enabled state. We should
-  // probably use a different struct than InlineDescriptor for the block-level
-  // inline descriptor of local varaibles.
-  getLocalBlock(Offset)->getBlockDesc<InlineDescriptor>().IsActive = true;
+  getLocalBlock(Offset)->getBlockDesc<LocalInlineDescriptor>().IsEnabled = true;
 }
 
 void InterpFrame::destroy(unsigned Idx) {
diff --git a/clang/lib/AST/ByteCode/InterpFrame.h b/clang/lib/AST/ByteCode/InterpFrame.h
index 6a093ca1480056..32df11c9ddb973 100644
--- a/clang/lib/AST/ByteCode/InterpFrame.h
+++ b/clang/lib/AST/ByteCode/InterpFrame.h
@@ -60,7 +60,9 @@ class InterpFrame final : public Frame {
   void destroy(unsigned Idx);
   void enableLocal(unsigned Offset);
   bool isLocalEnabled(unsigned Offset) const {
-    return getLocalBlock(Offset)->getBlockDesc<InlineDescriptor>().IsActive;
+    return getLocalBlock(Offset)
+        ->getBlockDesc<LocalInlineDescriptor>()
+        .IsEnabled;
   }
 
   /// Describes the frame with arguments for diagnostic purposes.
@@ -93,9 +95,9 @@ class InterpFrame final : public Frame {
     Block *B = getLocalBlock(Offset);
 
     B->deref<T>() = Value;
-    auto &LID = B->getBlockDesc<InlineDescriptor>();
+    auto &LID = B->getBlockDesc<LocalInlineDescriptor>();
     LID.IsInitialized = true;
-    LID.LifeState = Lifetime::Started;
+    LID.setLifetime(Lifetime::Started);
   }
 
   /// Returns a pointer to a local variables.
@@ -107,7 +109,7 @@ class InterpFrame final : public Frame {
     Block *B = localBlock(Offset);
     if (LLVM_UNLIKELY(!B->isInitialized())) {
       B->invokeCtor();
-      new (localInlineDesc(Offset)) InlineDescriptor(B->getDescriptor());
+      new (localInlineDesc(Offset)) LocalInlineDescriptor();
     }
     return B;
   };
@@ -211,8 +213,8 @@ class InterpFrame final : public Frame {
   }
 
   /// Returns the inline descriptor of the local.
-  InlineDescriptor *localInlineDesc(unsigned Offset) const {
-    return reinterpret_cast<InlineDescriptor *>(locals() + Offset);
+  LocalInlineDescriptor *localInlineDesc(unsigned Offset) const {
+    return reinterpret_cast<LocalInlineDescriptor *>(locals() + Offset);
   }
 
 public:
diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp
index a8aeac09ac51c5..a1b9fa4ca11735 100644
--- a/clang/lib/AST/ByteCode/Pointer.cpp
+++ b/clang/lib/AST/ByteCode/Pointer.cpp
@@ -763,8 +763,8 @@ bool Pointer::isElementAlive(unsigned Index) const {
 Lifetime PtrView::getLifetime() const {
   if (Offset == 0) {
     if (Pointee->isLocal()) {
-      auto &LD = Pointee->getBlockDesc<InlineDescriptor>();
-      return LD.LifeState;
+      auto &LD = Pointee->getBlockDesc<LocalInlineDescriptor>();
+      return LD.getLifetime();
     }
     return Lifetime::Started;
   }
@@ -789,8 +789,8 @@ Lifetime PtrView::getLifetime() const {
 void PtrView::setLifeState(Lifetime L) const {
   if (Offset == 0) {
     if (Pointee->isLocal()) {
-      auto &LD = Pointee->getBlockDesc<InlineDescriptor>();
-      LD.LifeState = L;
+      auto &LD = Pointee->getBlockDesc<LocalInlineDescriptor>();
+      LD.setLifetime(L);
     }
     return;
   }
@@ -820,9 +820,9 @@ void PtrView::initialize() const {
       auto &GD = Pointee->getBlockDesc<GlobalInlineDescriptor>();
       GD.InitState = GlobalInitState::Initialized;
     } else if (Pointee->isLocal()) {
-      auto &LD = Pointee->getBlockDesc<InlineDescriptor>();
+      auto &LD = Pointee->getBlockDesc<LocalInlineDescriptor>();
       LD.IsInitialized = true;
-      LD.LifeState = Lifetime::Started;
+      LD.setLifetime(Lifetime::Started);
     }
     return;
   }
diff --git a/clang/lib/AST/ByteCode/Pointer.h b/clang/lib/AST/ByteCode/Pointer.h
index 3e923a9c1a4c90..43da9fe47c6694 100644
--- a/clang/lib/AST/ByteCode/Pointer.h
+++ b/clang/lib/AST/ByteCode/Pointer.h
@@ -310,7 +310,7 @@ struct PtrView {
         return GD.InitState == GlobalInitState::Initialized;
       }
       if (Pointee->isLocal()) {
-        const auto &LD = Pointee->getBlockDesc<InlineDescriptor>();
+        const auto &LD = Pointee->getBlockDesc<LocalInlineDescriptor>();
         return LD.IsInitialized;
       }
       return true;



More information about the cfe-commits mailing list