-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
See the comment I added for why this is weird. We might want to have a different mechanism for this in the future.
@llvm/pr-subscribers-clang Author: Timm Baeder (tbaederr) ChangesSee the comment I added for why this is weird. We might want to have a different mechanism for this in the future. Full diff: https://github.com/llvm/llvm-project/pull/107576.diff 4 Files Affected:
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index a831f196abdcb5..2e9807b2d8426b 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -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);
diff --git a/clang/lib/AST/ByteCode/Context.cpp b/clang/lib/AST/ByteCode/Context.cpp
index e682d87b703ad7..594c74cd3796d6 100644
--- a/clang/lib/AST/ByteCode/Context.cpp
+++ b/clang/lib/AST/ByteCode/Context.cpp
@@ -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;
}
@@ -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
}
@@ -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;
}
@@ -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
}
@@ -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 =
@@ -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;
}
@@ -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
}
diff --git a/clang/lib/AST/ByteCode/InterpStack.cpp b/clang/lib/AST/ByteCode/InterpStack.cpp
index b8cdaeee72166c..ae3721e983741d 100644
--- a/clang/lib/AST/ByteCode/InterpStack.cpp
+++ b/clang/lib/AST/ByteCode/InterpStack.cpp
@@ -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");
@@ -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 {
diff --git a/clang/lib/AST/ByteCode/InterpStack.h b/clang/lib/AST/ByteCode/InterpStack.h
index 153d17f0d70e1b..43988bb680d1c6 100644
--- a/clang/lib/AST/ByteCode/InterpStack.h
+++ b/clang/lib/AST/ByteCode/InterpStack.h
@@ -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; }
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:frontend
Language frontend issues, e.g. anything involving "Sema"
clang
Clang issues not falling into any other category
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
See the comment I added for why this is weird. We might want to have a different mechanism for this in the future.
Fixes #101801