https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/238248
We can use this instead of three or four boolean parameters for the Block constructors. >From 859360b4e867998b367cc4fc4ab13092a94a4d0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]> Date: Tue, 3 Nov 2026 09:02:06 +0100 Subject: [PATCH] [clang][bytecode][NFC] Add a BlockFlags struct We can use this instead of three or four boolean parameters for the Block constructors. --- clang/lib/AST/ByteCode/Block.cpp | 3 +- clang/lib/AST/ByteCode/Block.h | 70 ++++++++++----------- clang/lib/AST/ByteCode/DynamicAllocator.cpp | 2 +- clang/lib/AST/ByteCode/Program.cpp | 4 +- 4 files changed, 37 insertions(+), 42 deletions(-) diff --git a/clang/lib/AST/ByteCode/Block.cpp b/clang/lib/AST/ByteCode/Block.cpp index 30597f2a081cec..b615466d225bac 100644 --- a/clang/lib/AST/ByteCode/Block.cpp +++ b/clang/lib/AST/ByteCode/Block.cpp @@ -125,8 +125,7 @@ void Block::removePointers() { } DeadBlock::DeadBlock(DeadBlock *&Root, Block *Blk) - : Root(Root), B(~0u, Blk->Desc, Blk->Kind, Blk->isExtern(), Blk->isWeak(), - /*IsDead=*/true) { + : Root(Root), B(~0u, Blk->Desc, Blk->Kind, Blk->Flags.withDead()) { // Add the block to the chain of dead blocks. if (Root) Root->Prev = this; diff --git a/clang/lib/AST/ByteCode/Block.h b/clang/lib/AST/ByteCode/Block.h index 29c291a9d9a636..afdf726e8be268 100644 --- a/clang/lib/AST/ByteCode/Block.h +++ b/clang/lib/AST/ByteCode/Block.h @@ -24,6 +24,26 @@ class InterpState; class Pointer; enum PrimType : uint8_t; +struct BlockFlags { + static constexpr uint8_t Extern = 1u << 0u; + static constexpr uint8_t Dead = 1u << 1u; + static constexpr uint8_t Weak = 1u << 2u; + + uint8_t V = 0; + constexpr BlockFlags(uint8_t V = 0) : V(V) {} + static constexpr BlockFlags empty() { return BlockFlags(); } + + constexpr BlockFlags withExtern(bool B = true) const { + return BlockFlags(V | (B * Extern)); + } + constexpr BlockFlags withDead(bool B = true) const { + return BlockFlags(V | (B * Dead)); + } + constexpr BlockFlags withWeak(bool B = true) const { + return BlockFlags(V | (B * Weak)); + } +}; + /// A memory block, either on the stack or in the heap. /// /// The storage described by the block is immediately followed by @@ -42,33 +62,18 @@ enum PrimType : uint8_t; /// getAllocSize() returns getMetadataSize() + Desc->getAllocSize(). /// class Block final { -private: - static constexpr uint8_t ExternFlag = 1 << 0; - static constexpr uint8_t DeadFlag = 1 << 1; - static constexpr uint8_t WeakFlag = 1 << 2; - public: enum BlockKind : uint8_t { None, Local, Global }; - /// Creates a new block. Block(unsigned EvalID, UnsignedOrNone DeclID, const Descriptor *Desc, - BlockKind K, bool IsExtern, bool IsWeak) - + BlockKind K, BlockFlags Flags = BlockFlags::empty()) : Desc(Desc), DescSize(Desc->getAllocSize()), DeclID(DeclID), - EvalID(EvalID), Kind(K), MDSize(getMetadataSize(K)) { - assert(Desc); - AccessFlags |= (ExternFlag * IsExtern); - AccessFlags |= (WeakFlag * IsWeak); - } + EvalID(EvalID), Flags(Flags), Kind(K), MDSize(getMetadataSize(K)) {} Block(unsigned EvalID, const Descriptor *Desc, BlockKind K, - bool IsExtern = false, bool IsWeak = false) - : Desc(Desc), DescSize(Desc->getAllocSize()), EvalID(EvalID), Kind(K), - MDSize(getMetadataSize(K)) { - assert(Desc); - AccessFlags |= (ExternFlag * IsExtern); - AccessFlags |= (WeakFlag * IsWeak); - } + BlockFlags Flags = BlockFlags::empty()) + : Desc(Desc), DescSize(Desc->getAllocSize()), EvalID(EvalID), + Flags(Flags), Kind(K), MDSize(getMetadataSize(K)) {} /// Number of bytes needed to allocate the block. static unsigned allocSize(const Descriptor *D, BlockKind K) { @@ -95,17 +100,18 @@ class Block final { /// Checks if the block has any live pointers. bool hasPointers() const { return Pointers; } /// Checks if the block is extern. - bool isExtern() const { return AccessFlags & ExternFlag; } + bool isExtern() const { return Flags.V & BlockFlags::Extern; } /// Checks if the block is temporary. bool isTemporary() const { return Desc->IsTemporary; } - bool isWeak() const { return AccessFlags & WeakFlag; } + bool isWeak() const { return Flags.V & BlockFlags::Weak; } bool isDynamic() const { bool Result = (DynAllocId != std::nullopt); assert((Result == Desc->isDynAlloc()) && "Inconsistent block/descriptor dynamic alloc state"); return Result; } - bool isDead() const { return AccessFlags & DeadFlag; } + bool isDead() const { return Flags.V & BlockFlags::Dead; } + 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, including metadata. @@ -184,8 +190,6 @@ class Block final { void dump() const { dump(llvm::errs()); } void dump(llvm::raw_ostream &OS) const; - bool isAccessible() const { return AccessFlags == 0; } - private: friend class Pointer; friend class DeadBlock; @@ -193,16 +197,6 @@ class Block final { friend class DynamicAllocator; friend class Program; - Block(unsigned EvalID, const Descriptor *Desc, BlockKind K, bool IsExtern, - bool IsWeak, bool IsDead) - : Desc(Desc), DescSize(Desc->getAllocSize()), EvalID(EvalID), Kind(K), - MDSize(getMetadataSize(K)) { - assert(Desc); - AccessFlags |= (ExternFlag * IsExtern); - AccessFlags |= (DeadFlag * IsDead); - AccessFlags |= (WeakFlag * IsWeak); - } - /// To be called by DynamicAllocator. void setDynAllocId(unsigned ID) { DynAllocId = ID; } @@ -217,6 +211,9 @@ class Block final { bool hasPointer(const Pointer *P) const; #endif + /// For DynamicAllocator. + void markDead() { Flags = Flags.withDead(); } + /// Pointer to the stack slot descriptor. const Descriptor *const Desc; /// Start of the chain of pointers. @@ -228,8 +225,7 @@ class Block final { const unsigned EvalID = ~0u; /// Allocation ID for this dynamic allocation, if it is one. UnsignedOrNone DynAllocId = std::nullopt; - /// AccessFlags containing IsExtern, IsDead and IsWeak bits. - uint8_t AccessFlags = 0; + BlockFlags Flags; /// Type of this block. const BlockKind Kind; /// Size of block-level metadata. diff --git a/clang/lib/AST/ByteCode/DynamicAllocator.cpp b/clang/lib/AST/ByteCode/DynamicAllocator.cpp index b2f5260483d89d..57b4fb516e388b 100644 --- a/clang/lib/AST/ByteCode/DynamicAllocator.cpp +++ b/clang/lib/AST/ByteCode/DynamicAllocator.cpp @@ -133,7 +133,7 @@ bool DynamicAllocator::deallocate(const Expr *Source, // over to a DeadBlock and simply keep the block in a separate DeadAllocations // list. if (B->hasPointers()) { - B->AccessFlags |= Block::DeadFlag; + B->markDead(); DeadAllocations.push_back(std::move(*AllocIt)); Site.Allocations.erase(AllocIt); diff --git a/clang/lib/AST/ByteCode/Program.cpp b/clang/lib/AST/ByteCode/Program.cpp index fec97c8b700f05..4f7daf450b5ff5 100644 --- a/clang/lib/AST/ByteCode/Program.cpp +++ b/clang/lib/AST/ByteCode/Program.cpp @@ -168,10 +168,10 @@ UnsignedOrNone Program::createGlobal(DeclOrExpr D, QualType Ty, bool IsExtern, // Allocate a block for storage. unsigned I = Globals.size(); + BlockFlags Flags = BlockFlags().withExtern(IsExtern).withWeak(IsWeak); auto *G = new (Allocator, Desc->getAllocSize() + Block::getMetadataSize(Block::Global)) - Global(Ctx.getEvalID(), getCurrentDecl(), Desc, Block::Global, IsExtern, - IsWeak); + Global(Ctx.getEvalID(), getCurrentDecl(), Desc, Block::Global, Flags); G->block()->invokeCtor(); // Initialize GlobalInlineDescriptor fields. _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
