Skip to content

Commit 7cca7a3

Browse files
committed
cleanup
1 parent e438414 commit 7cca7a3

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -816,22 +816,18 @@ bool ByteCodeExprGen<Emitter>::VisitArrayInitLoopExpr(
816816
const ArrayInitLoopExpr *E) {
817817
assert(Initializing);
818818
assert(!DiscardResult);
819+
820+
// We visit the common opaque expression here once so we have its value
821+
// cached.
822+
if (!this->discard(E->getCommonExpr()))
823+
return false;
824+
819825
// TODO: This compiles to quite a lot of bytecode if the array is larger.
820826
// Investigate compiling this to a loop.
821-
822827
const Expr *SubExpr = E->getSubExpr();
823-
const Expr *CommonExpr = E->getCommonExpr();
824828
size_t Size = E->getArraySize().getZExtValue();
825829
std::optional<PrimType> ElemT = classify(SubExpr->getType());
826830

827-
// If the common expression is an opaque expression, we visit it
828-
// here once so we have its value cached.
829-
// FIXME: This might be necessary (or useful) for all expressions.
830-
if (isa<OpaqueValueExpr>(CommonExpr)) {
831-
if (!this->discard(CommonExpr))
832-
return false;
833-
}
834-
835831
// So, every iteration, we execute an assignment here
836832
// where the LHS is on the stack (the target array)
837833
// and the RHS is our SubExpr.
@@ -882,13 +878,13 @@ bool ByteCodeExprGen<Emitter>::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
882878
return false;
883879

884880
// Here the local variable is created but the value is removed from the stack,
885-
// so we put it back, because the caller might need it.
881+
// so we put it back if the caller needs it.
886882
if (!DiscardResult) {
887883
if (!this->emitGetLocal(SubExprT, *LocalIndex, E))
888884
return false;
889885
}
890886

891-
// FIXME: Ideally the cached value should be cleaned up later.
887+
// This is cleaned up when the local variable is destroyed.
892888
OpaqueExprs.insert({E, *LocalIndex});
893889

894890
return true;

clang/lib/AST/Interp/ByteCodeExprGen.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ template <class Emitter> class LocalScope : public VariableScope<Emitter> {
360360
if (!Idx)
361361
return;
362362
this->Ctx->emitDestroy(*Idx, SourceInfo{});
363+
removeStoredOpaqueValues();
363364
}
364365

365366
/// Overriden to support explicit destruction.
@@ -368,6 +369,7 @@ template <class Emitter> class LocalScope : public VariableScope<Emitter> {
368369
return;
369370
this->emitDestructors();
370371
this->Ctx->emitDestroy(*Idx, SourceInfo{});
372+
removeStoredOpaqueValues();
371373
this->Idx = std::nullopt;
372374
}
373375

@@ -389,10 +391,28 @@ template <class Emitter> class LocalScope : public VariableScope<Emitter> {
389391
if (!Local.Desc->isPrimitive() && !Local.Desc->isPrimitiveArray()) {
390392
this->Ctx->emitGetPtrLocal(Local.Offset, SourceInfo{});
391393
this->Ctx->emitRecordDestruction(Local.Desc);
394+
removeIfStoredOpaqueValue(Local);
392395
}
393396
}
394397
}
395398

399+
void removeStoredOpaqueValues() {
400+
if (!Idx)
401+
return;
402+
403+
for (Scope::Local &Local : this->Ctx->Descriptors[*Idx]) {
404+
removeIfStoredOpaqueValue(Local);
405+
}
406+
}
407+
408+
void removeIfStoredOpaqueValue(const Scope::Local &Local) {
409+
if (auto *OVE =
410+
llvm::dyn_cast_or_null<OpaqueValueExpr>(Local.Desc->asExpr());
411+
OVE && this->Ctx->OpaqueExprs.contains(OVE)) {
412+
this->Ctx->OpaqueExprs.erase(OVE);
413+
};
414+
}
415+
396416
/// Index of the scope in the chain.
397417
std::optional<unsigned> Idx;
398418
};

0 commit comments

Comments
 (0)