Skip to content

[clang][bytecode] Add AccessFlags to Block #152590

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7139,10 +7139,6 @@ bool Compiler<Emitter>::emitDestruction(const Descriptor *Desc,
assert(!Desc->isPrimitive());
assert(!Desc->isPrimitiveArray());

// Can happen if the decl is invalid.
if (Desc->isDummy())
return true;

// Arrays.
if (Desc->isArray()) {
const Descriptor *ElemDesc = Desc->ElemDesc;
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/AST/ByteCode/Descriptor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ Descriptor::Descriptor(const DeclTy &D, const Record *R, MetadataSize MD,
Descriptor::Descriptor(const DeclTy &D, MetadataSize MD)
: Source(D), ElemSize(1), Size(1), MDSize(MD.value_or(0)),
AllocSize(MDSize), ElemRecord(nullptr), IsConst(true), IsMutable(false),
IsTemporary(false), IsDummy(true) {
IsTemporary(false) {
assert(Source && "Missing source");
}

Expand Down Expand Up @@ -453,7 +453,7 @@ SourceInfo Descriptor::getLoc() const {
}

bool Descriptor::hasTrivialDtor() const {
if (isPrimitive() || isPrimitiveArray() || isDummy())
if (isPrimitive() || isPrimitiveArray())
return true;

if (isRecord()) {
Expand All @@ -462,8 +462,9 @@ bool Descriptor::hasTrivialDtor() const {
return !Dtor || Dtor->isTrivial();
}

if (!ElemDesc)
return true;
// Composite arrays.
assert(ElemDesc);
return ElemDesc->hasTrivialDtor();
}

Expand Down
7 changes: 0 additions & 7 deletions clang/lib/AST/ByteCode/Descriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@ struct Descriptor final {
const bool IsVolatile = false;
/// Flag indicating if the block is an array.
const bool IsArray = false;
/// Flag indicating if this is a dummy descriptor.
bool IsDummy = false;
bool IsConstexprUnknown = false;

/// Storage management methods.
Expand Down Expand Up @@ -203,9 +201,6 @@ struct Descriptor final {
/// Allocates a dummy descriptor.
Descriptor(const DeclTy &D, MetadataSize MD = std::nullopt);

/// Make this descriptor a dummy descriptor.
void makeDummy() { IsDummy = true; }

QualType getType() const;
QualType getElemQualType() const;
QualType getDataType(const ASTContext &Ctx) const;
Expand Down Expand Up @@ -273,8 +268,6 @@ struct Descriptor final {
bool isRecord() const { return !IsArray && ElemRecord; }
/// Checks if the descriptor is of a union.
bool isUnion() const;
/// Checks if this is a dummy descriptor.
bool isDummy() const { return IsDummy; }

/// Whether variables of this descriptor need their destructor called or not.
bool hasTrivialDtor() const;
Expand Down
11 changes: 5 additions & 6 deletions clang/lib/AST/ByteCode/Disasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ LLVM_DUMP_METHOD void Program::dump(llvm::raw_ostream &OS) const {
}

OS << "\n";
if (GP.isInitialized() && Desc->isPrimitive() && !Desc->isDummy()) {
if (GP.isInitialized() && Desc->isPrimitive() && !G->block()->isDummy()) {
OS << " ";
{
ColorScope SC(OS, true, {llvm::raw_ostream::BRIGHT_CYAN, false});
Expand Down Expand Up @@ -394,8 +394,6 @@ LLVM_DUMP_METHOD void Descriptor::dump(llvm::raw_ostream &OS) const {
else if (isUnknownSizeArray())
OS << " unknown-size-array";

if (isDummy())
OS << " dummy";
if (IsConstexprUnknown)
OS << " constexpr-unknown";
}
Expand Down Expand Up @@ -541,11 +539,12 @@ LLVM_DUMP_METHOD void Block::dump(llvm::raw_ostream &OS) const {
else
OS << "-\n";
OS << " Pointers: " << NPointers << "\n";
OS << " Dead: " << IsDead << "\n";
OS << " Dead: " << isDead() << "\n";
OS << " Static: " << IsStatic << "\n";
OS << " Extern: " << IsExtern << "\n";
OS << " Extern: " << isExtern() << "\n";
OS << " Initialized: " << IsInitialized << "\n";
OS << " Weak: " << IsWeak << "\n";
OS << " Weak: " << isWeak() << "\n";
OS << " Dummy: " << isDummy() << '\n';
OS << " Dynamic: " << IsDynamic << "\n";
}

Expand Down
8 changes: 4 additions & 4 deletions clang/lib/AST/ByteCode/DynamicAllocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void DynamicAllocator::cleanup() {
auto &AllocSite = Iter.second;
for (auto &Alloc : AllocSite.Allocations) {
Block *B = Alloc.block();
assert(!B->IsDead);
assert(!B->isDead());
assert(B->isInitialized());
B->invokeDtor();

Expand Down Expand Up @@ -121,15 +121,15 @@ bool DynamicAllocator::deallocate(const Expr *Source,
assert(!Site.empty());

// Find the Block to delete.
auto AllocIt = llvm::find_if(Site.Allocations, [&](const Allocation &A) {
auto *AllocIt = llvm::find_if(Site.Allocations, [&](const Allocation &A) {
return BlockToDelete == A.block();
});

assert(AllocIt != Site.Allocations.end());

Block *B = AllocIt->block();
assert(B->isInitialized());
assert(!B->IsDead);
assert(!B->isDead());
B->invokeDtor();

// Almost all our dynamic allocations have a pointer pointing to them
Expand All @@ -139,7 +139,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->IsDead = true;
B->AccessFlags |= Block::DeadFlag;
DeadAllocations.push_back(std::move(*AllocIt));
Site.Allocations.erase(AllocIt);

Expand Down
111 changes: 77 additions & 34 deletions clang/lib/AST/ByteCode/Interp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -738,19 +738,21 @@ static bool CheckWeak(InterpState &S, CodePtr OpPC, const Block *B) {
bool CheckGlobalLoad(InterpState &S, CodePtr OpPC, const Block *B) {
const auto &Desc =
*reinterpret_cast<const GlobalInlineDescriptor *>(B->rawData());
if (!CheckExtern(S, OpPC, Pointer(const_cast<Block *>(B))))
return false;
if (!B->isAccessible()) {
if (!CheckExtern(S, OpPC, Pointer(const_cast<Block *>(B))))
return false;
if (!CheckDummy(S, OpPC, B, AK_Read))
return false;
return CheckWeak(S, OpPC, B);
}

if (!CheckConstant(S, OpPC, B->getDescriptor()))
return false;
if (!CheckDummy(S, OpPC, B, AK_Read))
return false;
if (Desc.InitState != GlobalInitState::Initialized)
return DiagnoseUninitialized(S, OpPC, B->isExtern(), B->getDescriptor(),
AK_Read);
if (!CheckTemporary(S, OpPC, B, AK_Read))
return false;
if (!CheckWeak(S, OpPC, B))
return false;
if (B->getDescriptor()->IsVolatile) {
if (!S.getLangOpts().CPlusPlus)
return Invalid(S, OpPC);
Expand Down Expand Up @@ -790,14 +792,32 @@ bool CheckLocalLoad(InterpState &S, CodePtr OpPC, const Block *B) {

bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
AccessKinds AK) {
if (!CheckLive(S, OpPC, Ptr, AK))
return false;
if (!CheckExtern(S, OpPC, Ptr))
if (!Ptr.isBlockPointer()) {
if (Ptr.isZero()) {
const auto &Src = S.Current->getSource(OpPC);

if (Ptr.isField())
S.FFDiag(Src, diag::note_constexpr_null_subobject) << CSK_Field;
else
S.FFDiag(Src, diag::note_constexpr_access_null) << AK;
}
return false;
}

// Block pointers are the only ones we can actually read from.
if (!Ptr.block()->isAccessible()) {
if (!CheckLive(S, OpPC, Ptr, AK))
return false;
if (!CheckExtern(S, OpPC, Ptr))
return false;
if (!CheckDummy(S, OpPC, Ptr.block(), AK))
return false;
if (!CheckWeak(S, OpPC, Ptr.block()))
return false;
}

if (!CheckConstant(S, OpPC, Ptr))
return false;
if (Ptr.isBlockPointer() && !CheckDummy(S, OpPC, Ptr.block(), AK))
return false;
if (!CheckRange(S, OpPC, Ptr, AK))
return false;
if (!CheckActive(S, OpPC, Ptr, AK))
Expand All @@ -806,10 +826,9 @@ bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
return false;
if (!Ptr.isInitialized())
return DiagnoseUninitialized(S, OpPC, Ptr, AK);
if (Ptr.isBlockPointer() && !CheckTemporary(S, OpPC, Ptr.block(), AK))
return false;
if (Ptr.isBlockPointer() && !CheckWeak(S, OpPC, Ptr.block()))
if (!CheckTemporary(S, OpPC, Ptr.block(), AK))
return false;

if (!CheckMutable(S, OpPC, Ptr))
return false;
if (!CheckVolatile(S, OpPC, Ptr, AK))
Expand All @@ -820,35 +839,54 @@ bool CheckLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr,
/// This is not used by any of the opcodes directly. It's used by
/// EvalEmitter to do the final lvalue-to-rvalue conversion.
bool CheckFinalLoad(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
if (!CheckLive(S, OpPC, Ptr, AK_Read))
if (!Ptr.isBlockPointer()) {
if (Ptr.isZero()) {
const auto &Src = S.Current->getSource(OpPC);

if (Ptr.isField())
S.FFDiag(Src, diag::note_constexpr_null_subobject) << CSK_Field;
else
S.FFDiag(Src, diag::note_constexpr_access_null) << AK_Read;
}
return false;
}

assert(Ptr.isBlockPointer());
if (!Ptr.block()->isAccessible()) {
if (!CheckLive(S, OpPC, Ptr, AK_Read))
return false;
if (!CheckExtern(S, OpPC, Ptr))
return false;
if (!CheckDummy(S, OpPC, Ptr.block(), AK_Read))
return false;
return CheckWeak(S, OpPC, Ptr.block());
}

if (!CheckConstant(S, OpPC, Ptr))
return false;

if (Ptr.isBlockPointer() && !CheckDummy(S, OpPC, Ptr.block(), AK_Read))
return false;
if (!CheckExtern(S, OpPC, Ptr))
return false;
if (!CheckActive(S, OpPC, Ptr, AK_Read))
return false;
if (!CheckLifetime(S, OpPC, Ptr.getLifetime(), AK_Read))
return false;
if (!Ptr.isInitialized())
return DiagnoseUninitialized(S, OpPC, Ptr, AK_Read);
if (Ptr.isBlockPointer() && !CheckTemporary(S, OpPC, Ptr.block(), AK_Read))
return false;
if (Ptr.isBlockPointer() && !CheckWeak(S, OpPC, Ptr.block()))
if (!CheckTemporary(S, OpPC, Ptr.block(), AK_Read))
return false;
if (!CheckMutable(S, OpPC, Ptr))
return false;
return true;
}

bool CheckStore(InterpState &S, CodePtr OpPC, const Pointer &Ptr) {
if (!CheckLive(S, OpPC, Ptr, AK_Assign))
return false;
if (Ptr.isBlockPointer() && !CheckDummy(S, OpPC, Ptr.block(), AK_Assign))
if (!Ptr.isBlockPointer())
return false;

if (!Ptr.block()->isAccessible()) {
if (!CheckLive(S, OpPC, Ptr, AK_Assign))
return false;
return CheckDummy(S, OpPC, Ptr.block(), AK_Assign);
}
if (!CheckLifetime(S, OpPC, Ptr.getLifetime(), AK_Assign))
return false;
if (!CheckExtern(S, OpPC, Ptr))
Expand Down Expand Up @@ -1124,11 +1162,10 @@ bool CheckDeclRef(InterpState &S, CodePtr OpPC, const DeclRefExpr *DR) {
}

bool CheckDummy(InterpState &S, CodePtr OpPC, const Block *B, AccessKinds AK) {
const Descriptor *Desc = B->getDescriptor();
if (!Desc->isDummy())
if (!B->isDummy())
return true;

const ValueDecl *D = Desc->asValueDecl();
const ValueDecl *D = B->getDescriptor()->asValueDecl();
if (!D)
return false;

Expand Down Expand Up @@ -1835,14 +1872,21 @@ bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E,
if (Ptr.inUnion() && Ptr.getBase().getRecord()->isUnion())
Ptr.activate();

if (!Ptr.isBlockPointer())
return false;

// Similar to CheckStore(), but with the additional CheckTemporary() call and
// the AccessKinds are different.

if (!Ptr.block()->isAccessible()) {
if (!CheckExtern(S, OpPC, Ptr))
return false;
if (!CheckLive(S, OpPC, Ptr, AK_Construct))
return false;
return CheckDummy(S, OpPC, Ptr.block(), AK_Construct);
}
if (!CheckTemporary(S, OpPC, Ptr.block(), AK_Construct))
return false;
if (!CheckLive(S, OpPC, Ptr, AK_Construct))
return false;
if (!CheckDummy(S, OpPC, Ptr.block(), AK_Construct))
return false;

// CheckLifetime for this and all base pointers.
for (Pointer P = Ptr;;) {
Expand All @@ -1853,8 +1897,7 @@ bool CheckNewTypeMismatch(InterpState &S, CodePtr OpPC, const Expr *E,
break;
P = P.getBase();
}
if (!CheckExtern(S, OpPC, Ptr))
return false;

if (!CheckRange(S, OpPC, Ptr, AK_Construct))
return false;
if (!CheckGlobal(S, OpPC, Ptr))
Expand Down
6 changes: 3 additions & 3 deletions clang/lib/AST/ByteCode/InterpBlock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void Block::removePointer(Pointer *P) {
}

void Block::cleanup() {
if (Pointers == nullptr && !IsDynamic && IsDead)
if (Pointers == nullptr && !IsDynamic && isDead())
(reinterpret_cast<DeadBlock *>(this + 1) - 1)->free();
}

Expand Down Expand Up @@ -113,8 +113,8 @@ bool Block::hasPointer(const Pointer *P) const {
#endif

DeadBlock::DeadBlock(DeadBlock *&Root, Block *Blk)
: Root(Root), B(~0u, Blk->Desc, Blk->IsStatic, Blk->IsExtern, Blk->IsWeak,
/*isDead=*/true) {
: Root(Root), B(~0u, Blk->Desc, Blk->isExtern(), Blk->IsStatic,
Blk->isWeak(), Blk->isDummy(), /*IsDead=*/true) {
// Add the block to the chain of dead blocks.
if (Root)
Root->Prev = this;
Expand Down
Loading