[clang] [clang][bytecode][NFC] Remove Program::Global (PR #238321)
Timm Baeder via cfe-commits
cfe-commits at lists.llvm.org
Tue Nov 3 05:11:38 PST 2026
https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/238321
It was only a wrapper arond a Block.
>From ed7a99337eafc541c61269d7ac4d894587c37b72 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Tue, 3 Nov 2026 12:46:26 +0100
Subject: [PATCH] [clang][bytecode][NFC] Remove Program::Global
It was only a wrapper arond a Block.
---
clang/lib/AST/ByteCode/Disasm.cpp | 6 ++--
clang/lib/AST/ByteCode/Program.cpp | 26 +++++++++-------
clang/lib/AST/ByteCode/Program.h | 50 +++++++-----------------------
3 files changed, 28 insertions(+), 54 deletions(-)
diff --git a/clang/lib/AST/ByteCode/Disasm.cpp b/clang/lib/AST/ByteCode/Disasm.cpp
index db5c116d8b3c8..c1d9cde9b6455 100644
--- a/clang/lib/AST/ByteCode/Disasm.cpp
+++ b/clang/lib/AST/ByteCode/Disasm.cpp
@@ -360,11 +360,11 @@ LLVM_DUMP_METHOD void Program::dump(llvm::raw_ostream &OS) const {
OS << "Global Variables: " << Globals.size() << '\n';
}
unsigned GI = 0;
- for (const Global *G : Globals) {
- const Descriptor *Desc = G->block()->getDescriptor();
+ for (const Block *G : Globals) {
+ const Descriptor *Desc = G->getDescriptor();
Pointer GP = getPtrGlobal(GI);
- OS << GI << ": " << (const void *)G->block() << " ";
+ OS << GI << ": " << (const void *)G << " ";
{
ColorScope SC(OS, true,
GP.isInitialized()
diff --git a/clang/lib/AST/ByteCode/Program.cpp b/clang/lib/AST/ByteCode/Program.cpp
index de4314b405283..665280814fcc6 100644
--- a/clang/lib/AST/ByteCode/Program.cpp
+++ b/clang/lib/AST/ByteCode/Program.cpp
@@ -20,7 +20,7 @@ using namespace clang::interp;
Pointer Program::getPtrGlobal(unsigned Idx) const {
assert(Idx < Globals.size());
- Block *B = Globals[Idx]->block();
+ Block *B = Globals[Idx];
// Force de-serialization of a redeclaration that might initialize this
// global.
@@ -32,7 +32,7 @@ Pointer Program::getPtrGlobal(unsigned Idx) const {
MD->evaluateValue();
// Note that we need to get Globals[Idx] here again since the code block
// above might've actually changed what global Idx points to.
- return Pointer(Globals[Idx]->block());
+ return Pointer(Globals[Idx]);
}
}
}
@@ -98,7 +98,7 @@ UnsignedOrNone Program::createGlobal(const ValueDecl *VD, const Expr *Init,
if (!Idx)
return std::nullopt;
- Global *NewGlobal = Globals[*Idx];
+ Block *NewGlobal = Globals[*Idx];
GlobalIndices[VD] = *Idx;
for (const Decl *Redecl = VD->getPreviousDecl(); Redecl;
@@ -113,13 +113,15 @@ UnsignedOrNone Program::createGlobal(const ValueDecl *VD, const Expr *Init,
continue;
}
- Block *RedeclBlock = Globals[Iter->second]->block();
+ Block *RedeclBlock = Globals[Iter->second];
// All pointers pointing to the previous extern decl now point to the
// new decl.
// A previous iteration might've already fixed up the pointers for this
// global.
- if (RedeclBlock != NewGlobal->block())
- RedeclBlock->movePointersTo(NewGlobal->block());
+ if (RedeclBlock != NewGlobal) {
+ RedeclBlock->movePointersTo(NewGlobal);
+ RedeclBlock->invokeDtor();
+ }
Globals[Iter->second] = NewGlobal;
Iter->second = *Idx;
@@ -170,14 +172,14 @@ UnsignedOrNone Program::createGlobal(DeclOrExpr D, QualType Ty, bool IsExtern,
BlockFlags Flags =
BlockFlags().withExtern(IsExtern).withWeak(IsWeak).withConstexprUnknown(
IsConstexprUnknown);
- auto *G = new (Allocator,
- Desc->getAllocSize() + Block::getMetadataSize(Block::Global))
- Global(Ctx.getEvalID(), getCurrentDecl(), Desc, Block::Global, Flags);
- G->block()->invokeCtor();
+ auto *G =
+ new (Allocate(Block::allocSize(Desc, Block::Global), alignof(Block)))
+ Block(Ctx.getEvalID(), getCurrentDecl(), Desc, Block::Global, Flags);
+ G->invokeCtor();
// Initialize GlobalInlineDescriptor fields.
- auto *GD = new (&G->block()->getBlockDesc<GlobalInlineDescriptor>())
- GlobalInlineDescriptor();
+ auto *GD =
+ new (&G->getBlockDesc<GlobalInlineDescriptor>()) GlobalInlineDescriptor();
if (!Init)
GD->InitState = GlobalInitState::NoInitializer;
Globals.push_back(G);
diff --git a/clang/lib/AST/ByteCode/Program.h b/clang/lib/AST/ByteCode/Program.h
index 82f22579d45fe..3e39e5cc0e110 100644
--- a/clang/lib/AST/ByteCode/Program.h
+++ b/clang/lib/AST/ByteCode/Program.h
@@ -43,9 +43,9 @@ class Program final {
// Manually destroy all the blocks. They are almost all harmless,
// but primitive arrays might have an InitMap* heap allocated and
// that needs to be freed.
- for (Global *G : Globals)
- if (Block *B = G->block(); B->isInitialized())
- B->invokeDtor();
+ for (Block *G : Globals)
+ if (G->isInitialized())
+ G->invokeDtor();
// Records might actually allocate memory themselves, but they
// are allocated using a BumpPtrAllocator. Call their desctructors
@@ -67,7 +67,7 @@ class Program final {
/// Returns the value of a global.
Block *getGlobal(unsigned Idx) {
assert(Idx < Globals.size());
- return Globals[Idx]->block();
+ return Globals[Idx];
}
bool isGlobalInitialized(unsigned Index) const {
@@ -128,10 +128,10 @@ class Program final {
bool IsMutable = false, bool IsVolatile = false,
const Expr *Init = nullptr);
- void *Allocate(size_t Size, unsigned Align = 8) const {
+ void *Allocate(size_t Size, unsigned Align = 8) {
return Allocator.Allocate(Size, Align);
}
- template <typename T> T *Allocate(size_t Num = 1) const {
+ template <typename T> T *Allocate(size_t Num = 1) {
return static_cast<T *>(Allocate(Num * sizeof(T), alignof(T)));
}
void Deallocate(void *Ptr) const {}
@@ -171,38 +171,11 @@ class Program final {
/// List of anonymous functions.
std::vector<std::unique_ptr<Function>> AnonFuncs;
- /// Custom allocator for global storage.
- using PoolAllocTy = llvm::BumpPtrAllocator;
-
- /// Descriptor + storage for a global object.
- ///
- /// Global objects never go out of scope, thus they do not track pointers.
- class Global {
- public:
- /// Create a global descriptor for string literals.
- template <typename... Tys>
- Global(Tys... Args) : B(std::forward<Tys>(Args)...) {}
-
- /// Allocates the global in the pool, reserving storate for data.
- void *operator new(size_t Meta, PoolAllocTy &Alloc, size_t Data) {
- return Alloc.Allocate(Meta + Data, alignof(void *));
- }
-
- /// Return a pointer to the data.
- std::byte *data() { return B.data(); }
- /// Return a pointer to the block.
- Block *block() { return &B; }
- const Block *block() const { return &B; }
-
- private:
- Block B;
- };
-
/// Allocator for globals.
- mutable PoolAllocTy Allocator;
+ llvm::BumpPtrAllocator Allocator;
/// Global objects.
- std::vector<Global *> Globals;
+ std::vector<Block *> Globals;
/// Cached global indices.
llvm::DenseMap<const void *, unsigned> GlobalIndices;
@@ -230,16 +203,15 @@ class Program final {
} // namespace interp
} // namespace clang
-inline void *operator new(size_t Bytes, const clang::interp::Program &C,
+inline void *operator new(size_t Bytes, clang::interp::Program &C,
size_t Alignment = 8) {
return C.Allocate(Bytes, Alignment);
}
-inline void operator delete(void *Ptr, const clang::interp::Program &C,
- size_t) {
+inline void operator delete(void *Ptr, clang::interp::Program &C, size_t) {
C.Deallocate(Ptr);
}
-inline void *operator new[](size_t Bytes, const clang::interp::Program &C,
+inline void *operator new[](size_t Bytes, clang::interp::Program &C,
size_t Alignment = 8) {
return C.Allocate(Bytes, Alignment);
}
More information about the cfe-commits
mailing list