Skip to content

[clang][bytecode] Only visit local variables if they have constant init #107576

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
Sep 7, 2024
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
13 changes: 10 additions & 3 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5507,11 +5507,18 @@ bool Compiler<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) {
if (isa<DecompositionDecl>(VD))
return revisit(VD);

// Visit local const variables like normal.
if ((VD->hasGlobalStorage() || VD->isLocalVarDecl() ||
VD->isStaticDataMember()) &&
if ((VD->hasGlobalStorage() || VD->isStaticDataMember()) &&
typeShouldBeVisited(VD->getType()))
return revisit(VD);

// FIXME: The evaluateValue() check here is a little ridiculous, since
// it will ultimately call into Context::evaluateAsInitializer(). In
// other words, we're evaluating the initializer, just to know if we can
// evaluate the initializer.
if (VD->isLocalVarDecl() && typeShouldBeVisited(VD->getType()) &&
VD->getInit() && !VD->getInit()->isValueDependent() &&
VD->evaluateValue())
return revisit(VD);
}
} else {
if (const auto *VD = dyn_cast<VarDecl>(D);
Expand Down
16 changes: 10 additions & 6 deletions clang/lib/AST/ByteCode/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ bool Context::isPotentialConstantExpr(State &Parent, const FunctionDecl *FD) {
bool Context::evaluateAsRValue(State &Parent, const Expr *E, APValue &Result) {
++EvalID;
bool Recursing = !Stk.empty();
size_t StackSizeBefore = Stk.size();
Compiler<EvalEmitter> C(*this, *P, Parent, Stk);

auto Res = C.interpretExpr(E, /*ConvertResultToRValue=*/E->isGLValue());

if (Res.isInvalid()) {
C.cleanup();
Stk.clear();
Stk.clearTo(StackSizeBefore);
return false;
}

Expand All @@ -60,7 +61,7 @@ bool Context::evaluateAsRValue(State &Parent, const Expr *E, APValue &Result) {
#ifndef NDEBUG
// Make sure we don't rely on some value being still alive in
// InterpStack memory.
Stk.clear();
Stk.clearTo(StackSizeBefore);
#endif
}

Expand All @@ -72,12 +73,13 @@ bool Context::evaluateAsRValue(State &Parent, const Expr *E, APValue &Result) {
bool Context::evaluate(State &Parent, const Expr *E, APValue &Result) {
++EvalID;
bool Recursing = !Stk.empty();
size_t StackSizeBefore = Stk.size();
Compiler<EvalEmitter> C(*this, *P, Parent, Stk);

auto Res = C.interpretExpr(E);
if (Res.isInvalid()) {
C.cleanup();
Stk.clear();
Stk.clearTo(StackSizeBefore);
return false;
}

Expand All @@ -87,7 +89,7 @@ bool Context::evaluate(State &Parent, const Expr *E, APValue &Result) {
#ifndef NDEBUG
// Make sure we don't rely on some value being still alive in
// InterpStack memory.
Stk.clear();
Stk.clearTo(StackSizeBefore);
#endif
}

Expand All @@ -99,6 +101,7 @@ bool Context::evaluateAsInitializer(State &Parent, const VarDecl *VD,
APValue &Result) {
++EvalID;
bool Recursing = !Stk.empty();
size_t StackSizeBefore = Stk.size();
Compiler<EvalEmitter> C(*this, *P, Parent, Stk);

bool CheckGlobalInitialized =
Expand All @@ -107,7 +110,8 @@ bool Context::evaluateAsInitializer(State &Parent, const VarDecl *VD,
auto Res = C.interpretDecl(VD, CheckGlobalInitialized);
if (Res.isInvalid()) {
C.cleanup();
Stk.clear();
Stk.clearTo(StackSizeBefore);

return false;
}

Expand All @@ -117,7 +121,7 @@ bool Context::evaluateAsInitializer(State &Parent, const VarDecl *VD,
#ifndef NDEBUG
// Make sure we don't rely on some value being still alive in
// InterpStack memory.
Stk.clear();
Stk.clearTo(StackSizeBefore);
#endif
}

Expand Down
25 changes: 25 additions & 0 deletions clang/lib/AST/ByteCode/InterpStack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ void InterpStack::clear() {
#endif
}

void InterpStack::clearTo(size_t NewSize) {
assert(NewSize <= size());
size_t ToShrink = size() - NewSize;
if (ToShrink == 0)
return;

shrink(ToShrink);
assert(size() == NewSize);
}

void *InterpStack::grow(size_t Size) {
assert(Size < ChunkSize - sizeof(StackChunk) && "Object too large");

Expand Down Expand Up @@ -81,6 +91,21 @@ void InterpStack::shrink(size_t Size) {

Chunk->End -= Size;
StackSize -= Size;

#ifndef NDEBUG
size_t TypesSize = 0;
for (PrimType T : ItemTypes)
TYPE_SWITCH(T, { TypesSize += aligned_size<T>(); });

size_t StackSize = size();
while (TypesSize > StackSize) {
TYPE_SWITCH(ItemTypes.back(), {
TypesSize -= aligned_size<T>();
ItemTypes.pop_back();
});
}
assert(TypesSize == StackSize);
#endif
}

void InterpStack::dump() const {
Expand Down
1 change: 1 addition & 0 deletions clang/lib/AST/ByteCode/InterpStack.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ class InterpStack final {

/// Clears the stack without calling any destructors.
void clear();
void clearTo(size_t NewSize);

/// Returns whether the stack is empty.
bool empty() const { return StackSize == 0; }
Expand Down
Loading