https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/172218
Which returns the block-level descriptor. This way we don't have to do the reinterpret_cast dance everywhere. >From 2b0a7eaff9fae27c225acdf41db331a337dbddd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]> Date: Sun, 14 Dec 2025 17:22:40 +0100 Subject: [PATCH] [clang][bytecode][NFC] Add Block::getBlockDesc<T>() Which returns the block-level descriptor. This way we don't have to do the reinterpret_cast dance everywhere. --- clang/lib/AST/ByteCode/Compiler.cpp | 6 ++---- clang/lib/AST/ByteCode/EvalEmitter.cpp | 9 ++++----- clang/lib/AST/ByteCode/Interp.cpp | 3 +-- clang/lib/AST/ByteCode/Interp.h | 3 +-- clang/lib/AST/ByteCode/InterpBlock.h | 8 ++++++++ clang/lib/AST/ByteCode/Pointer.cpp | 12 ++++-------- 6 files changed, 20 insertions(+), 21 deletions(-) diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp index ed5493c315dd7..4daab0702f147 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -4798,8 +4798,7 @@ VarCreationState Compiler<Emitter>::visitDecl(const VarDecl *VD, if (!R && Context::shouldBeGloballyIndexed(VD)) { if (auto GlobalIndex = P.getGlobal(VD)) { Block *GlobalBlock = P.getGlobal(*GlobalIndex); - GlobalInlineDescriptor &GD = - *reinterpret_cast<GlobalInlineDescriptor *>(GlobalBlock->rawData()); + auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>(); GD.InitState = GlobalInitState::InitializerFailed; GlobalBlock->invokeDtor(); @@ -4860,8 +4859,7 @@ bool Compiler<Emitter>::visitDeclAndReturn(const VarDecl *VD, const Expr *Init, auto GlobalIndex = P.getGlobal(VD); assert(GlobalIndex); Block *GlobalBlock = P.getGlobal(*GlobalIndex); - GlobalInlineDescriptor &GD = - *reinterpret_cast<GlobalInlineDescriptor *>(GlobalBlock->rawData()); + auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>(); GD.InitState = GlobalInitState::InitializerFailed; GlobalBlock->invokeDtor(); diff --git a/clang/lib/AST/ByteCode/EvalEmitter.cpp b/clang/lib/AST/ByteCode/EvalEmitter.cpp index a2e01efc8ffd9..2ed5147a15491 100644 --- a/clang/lib/AST/ByteCode/EvalEmitter.cpp +++ b/clang/lib/AST/ByteCode/EvalEmitter.cpp @@ -110,7 +110,7 @@ Scope::Local EvalEmitter::createLocal(Descriptor *D) { B->invokeCtor(); // Initialize local variable inline descriptor. - InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData()); + auto &Desc = B->getBlockDesc<InlineDescriptor>(); Desc.Desc = D; Desc.Offset = sizeof(InlineDescriptor); Desc.IsActive = false; @@ -304,7 +304,7 @@ bool EvalEmitter::emitSetLocal(uint32_t I, SourceInfo Info) { Block *B = getLocal(I); *reinterpret_cast<T *>(B->data()) = S.Stk.pop<T>(); - InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData()); + auto &Desc = B->getBlockDesc<InlineDescriptor>(); Desc.IsInitialized = true; return true; @@ -327,8 +327,7 @@ bool EvalEmitter::emitGetLocalEnabled(uint32_t I, SourceInfo Info) { return true; Block *B = getLocal(I); - const InlineDescriptor &Desc = - *reinterpret_cast<InlineDescriptor *>(B->rawData()); + const auto &Desc = B->getBlockDesc<InlineDescriptor>(); S.Stk.push<bool>(Desc.IsActive); return true; @@ -344,7 +343,7 @@ bool EvalEmitter::emitEnableLocal(uint32_t I, SourceInfo Info) { // probably use a different struct than InlineDescriptor for the block-level // inline descriptor of local varaibles. Block *B = getLocal(I); - InlineDescriptor &Desc = *reinterpret_cast<InlineDescriptor *>(B->rawData()); + auto &Desc = B->getBlockDesc<InlineDescriptor>(); Desc.IsActive = true; return true; } diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp index 80ef656dc6285..889ac1e1a9a7e 100644 --- a/clang/lib/AST/ByteCode/Interp.cpp +++ b/clang/lib/AST/ByteCode/Interp.cpp @@ -736,8 +736,7 @@ static bool CheckWeak(InterpState &S, CodePtr OpPC, const Block *B) { // For example, since those can't be members of structs, they also can't // be mutable. bool CheckGlobalLoad(InterpState &S, CodePtr OpPC, const Block *B) { - const auto &Desc = - *reinterpret_cast<const GlobalInlineDescriptor *>(B->rawData()); + const auto &Desc = B->getBlockDesc<GlobalInlineDescriptor>(); if (!B->isAccessible()) { if (!CheckExtern(S, OpPC, Pointer(const_cast<Block *>(B)))) return false; diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h index d8b8b209fa927..427f694319b6c 100644 --- a/clang/lib/AST/ByteCode/Interp.h +++ b/clang/lib/AST/ByteCode/Interp.h @@ -1455,8 +1455,7 @@ bool GetGlobal(InterpState &S, CodePtr OpPC, uint32_t I) { template <PrimType Name, class T = typename PrimConv<Name>::T> bool GetGlobalUnchecked(InterpState &S, CodePtr OpPC, uint32_t I) { const Block *B = S.P.getGlobal(I); - const auto &Desc = - *reinterpret_cast<const GlobalInlineDescriptor *>(B->rawData()); + const auto &Desc = B->getBlockDesc<GlobalInlineDescriptor>(); if (Desc.InitState != GlobalInitState::Initialized) return DiagnoseUninitialized(S, OpPC, B->isExtern(), B->getDescriptor(), AK_Read); diff --git a/clang/lib/AST/ByteCode/InterpBlock.h b/clang/lib/AST/ByteCode/InterpBlock.h index 73fdc8d85da11..57f9e7ec3714d 100644 --- a/clang/lib/AST/ByteCode/InterpBlock.h +++ b/clang/lib/AST/ByteCode/InterpBlock.h @@ -122,6 +122,14 @@ class Block final { } template <typename T> T &deref() { return *reinterpret_cast<T *>(data()); } + template <typename T> T &getBlockDesc() { + assert(sizeof(T) == getDescriptor()->getMetadataSize()); + return *reinterpret_cast<T *>(rawData()); + } + template <typename T> const T &getBlockDesc() const { + return const_cast<Block *>(this)->getBlockDesc<T>(); + } + /// Invokes the constructor. void invokeCtor() { assert(!IsInitialized); diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp index 00e74db5655d6..90f41bed39440 100644 --- a/clang/lib/AST/ByteCode/Pointer.cpp +++ b/clang/lib/AST/ByteCode/Pointer.cpp @@ -444,8 +444,7 @@ bool Pointer::isInitialized() const { if (isRoot() && BS.Base == sizeof(GlobalInlineDescriptor) && Offset == BS.Base) { - const GlobalInlineDescriptor &GD = - *reinterpret_cast<const GlobalInlineDescriptor *>(block()->rawData()); + const auto &GD = block()->getBlockDesc<GlobalInlineDescriptor>(); return GD.InitState == GlobalInitState::Initialized; } @@ -473,8 +472,7 @@ bool Pointer::isElementInitialized(unsigned Index) const { if (isRoot() && BS.Base == sizeof(GlobalInlineDescriptor) && Offset == BS.Base) { - const GlobalInlineDescriptor &GD = - *reinterpret_cast<const GlobalInlineDescriptor *>(block()->rawData()); + const auto &GD = block()->getBlockDesc<GlobalInlineDescriptor>(); return GD.InitState == GlobalInitState::Initialized; } @@ -499,8 +497,7 @@ void Pointer::initialize() const { if (isRoot() && BS.Base == sizeof(GlobalInlineDescriptor) && Offset == BS.Base) { - GlobalInlineDescriptor &GD = *reinterpret_cast<GlobalInlineDescriptor *>( - asBlockPointer().Pointee->rawData()); + auto &GD = BS.Pointee->getBlockDesc<GlobalInlineDescriptor>(); GD.InitState = GlobalInitState::Initialized; return; } @@ -565,8 +562,7 @@ bool Pointer::allElementsInitialized() const { if (isRoot() && BS.Base == sizeof(GlobalInlineDescriptor) && Offset == BS.Base) { - const GlobalInlineDescriptor &GD = - *reinterpret_cast<const GlobalInlineDescriptor *>(block()->rawData()); + const auto &GD = block()->getBlockDesc<GlobalInlineDescriptor>(); return GD.InitState == GlobalInitState::Initialized; } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
