From db557bee1e2c128e77805deb86c1f364b5c29e70 Mon Sep 17 00:00:00 2001 From: Timm Baeder Date: Mon, 15 Dec 2025 09:21:41 +0100 Subject: [PATCH] [clang][bytecode][NFC] Add Block::getBlockDesc() (#172218) 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 ed5493c315dd..4daab0702f14 100644 --- a/clang/lib/AST/ByteCode/Compiler.cpp +++ b/clang/lib/AST/ByteCode/Compiler.cpp @@ -4798,8 +4798,7 @@ VarCreationState Compiler::visitDecl(const VarDecl *VD, if (!R && Context::shouldBeGloballyIndexed(VD)) { if (auto GlobalIndex = P.getGlobal(VD)) { Block *GlobalBlock = P.getGlobal(*GlobalIndex); - GlobalInlineDescriptor &GD = - *reinterpret_cast(GlobalBlock->rawData()); + auto &GD = GlobalBlock->getBlockDesc(); GD.InitState = GlobalInitState::InitializerFailed; GlobalBlock->invokeDtor(); @@ -4860,8 +4859,7 @@ bool Compiler::visitDeclAndReturn(const VarDecl *VD, const Expr *Init, auto GlobalIndex = P.getGlobal(VD); assert(GlobalIndex); Block *GlobalBlock = P.getGlobal(*GlobalIndex); - GlobalInlineDescriptor &GD = - *reinterpret_cast(GlobalBlock->rawData()); + auto &GD = GlobalBlock->getBlockDesc(); GD.InitState = GlobalInitState::InitializerFailed; GlobalBlock->invokeDtor(); diff --git a/clang/lib/AST/ByteCode/EvalEmitter.cpp b/clang/lib/AST/ByteCode/EvalEmitter.cpp index a2e01efc8ffd..2ed5147a1549 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(B->rawData()); + auto &Desc = B->getBlockDesc(); 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(B->data()) = S.Stk.pop(); - InlineDescriptor &Desc = *reinterpret_cast(B->rawData()); + auto &Desc = B->getBlockDesc(); 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(B->rawData()); + const auto &Desc = B->getBlockDesc(); S.Stk.push(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(B->rawData()); + auto &Desc = B->getBlockDesc(); Desc.IsActive = true; return true; } diff --git a/clang/lib/AST/ByteCode/Interp.cpp b/clang/lib/AST/ByteCode/Interp.cpp index 80ef656dc628..889ac1e1a9a7 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(B->rawData()); + const auto &Desc = B->getBlockDesc(); if (!B->isAccessible()) { if (!CheckExtern(S, OpPC, Pointer(const_cast(B)))) return false; diff --git a/clang/lib/AST/ByteCode/Interp.h b/clang/lib/AST/ByteCode/Interp.h index d8b8b209fa92..427f694319b6 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 ::T> bool GetGlobalUnchecked(InterpState &S, CodePtr OpPC, uint32_t I) { const Block *B = S.P.getGlobal(I); - const auto &Desc = - *reinterpret_cast(B->rawData()); + const auto &Desc = B->getBlockDesc(); 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 73fdc8d85da1..57f9e7ec3714 100644 --- a/clang/lib/AST/ByteCode/InterpBlock.h +++ b/clang/lib/AST/ByteCode/InterpBlock.h @@ -122,6 +122,14 @@ public: } template T &deref() { return *reinterpret_cast(data()); } + template T &getBlockDesc() { + assert(sizeof(T) == getDescriptor()->getMetadataSize()); + return *reinterpret_cast(rawData()); + } + template const T &getBlockDesc() const { + return const_cast(this)->getBlockDesc(); + } + /// Invokes the constructor. void invokeCtor() { assert(!IsInitialized); diff --git a/clang/lib/AST/ByteCode/Pointer.cpp b/clang/lib/AST/ByteCode/Pointer.cpp index 00e74db5655d..90f41bed3944 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(block()->rawData()); + const auto &GD = block()->getBlockDesc(); 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(block()->rawData()); + const auto &GD = block()->getBlockDesc(); 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( - asBlockPointer().Pointee->rawData()); + auto &GD = BS.Pointee->getBlockDesc(); 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(block()->rawData()); + const auto &GD = block()->getBlockDesc(); return GD.InitState == GlobalInitState::Initialized; }