Skip to content

Commit 2910c24

Browse files
authored
[Modules] Record side effect info in EvaluatedStmt (#146468)
All deserialized VarDecl initializers are EvaluatedStmt, but not all EvaluatedStmt initializers are from a PCH. Calling `VarDecl::hasInitWithSideEffects` can trigger constant evaluation, but it's hard to know ahead of time whether that will trigger deserialization - even if the initializer is fully deserialized, it may contain a call to a constructor whose body is not deserialized. By caching the result of `VarDecl::hasInitWithSideEffects` and populating that cache during deserialization we can guarantee that calling it won't trigger deserialization regardless of the state of the initializer. This also reduces memory usage by removing the `InitSideEffectVars` set in `ASTReader`. rdar://154717930
1 parent 9bfb347 commit 2910c24

File tree

11 files changed

+77
-63
lines changed

11 files changed

+77
-63
lines changed

clang/include/clang/AST/Decl.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -888,13 +888,17 @@ struct EvaluatedStmt {
888888
bool HasICEInit : 1;
889889
bool CheckedForICEInit : 1;
890890

891+
bool HasSideEffects : 1;
892+
bool CheckedForSideEffects : 1;
893+
891894
LazyDeclStmtPtr Value;
892895
APValue Evaluated;
893896

894897
EvaluatedStmt()
895898
: WasEvaluated(false), IsEvaluating(false),
896899
HasConstantInitialization(false), HasConstantDestruction(false),
897-
HasICEInit(false), CheckedForICEInit(false) {}
900+
HasICEInit(false), CheckedForICEInit(false), HasSideEffects(false),
901+
CheckedForSideEffects(false) {}
898902
};
899903

900904
/// Represents a variable declaration or definition.
@@ -1353,9 +1357,11 @@ class VarDecl : public DeclaratorDecl, public Redeclarable<VarDecl> {
13531357
return const_cast<VarDecl *>(this)->getInitializingDeclaration();
13541358
}
13551359

1356-
/// Checks whether this declaration has an initializer with side effects,
1357-
/// without triggering deserialization if the initializer is not yet
1358-
/// deserialized.
1360+
/// Checks whether this declaration has an initializer with side effects.
1361+
/// The result is cached. If the result hasn't been computed this can trigger
1362+
/// deserialization and constant evaluation. By running this during
1363+
/// serialization and serializing the result all clients can safely call this
1364+
/// without triggering further deserialization.
13591365
bool hasInitWithSideEffects() const;
13601366

13611367
/// Determine whether this variable's value might be usable in a

clang/include/clang/AST/ExternalASTSource.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -196,10 +196,6 @@ class ExternalASTSource : public RefCountedBase<ExternalASTSource> {
196196
/// module.
197197
virtual bool wasThisDeclarationADefinition(const FunctionDecl *FD);
198198

199-
virtual bool hasInitializerWithSideEffects(const VarDecl *VD) const {
200-
return false;
201-
}
202-
203199
/// Finds all declarations lexically contained within the given
204200
/// DeclContext, after applying an optional filter predicate.
205201
///
@@ -434,17 +430,6 @@ struct LazyOffsetPtr {
434430
return GetPtr();
435431
}
436432

437-
/// Retrieve the pointer to the AST node that this lazy pointer points to,
438-
/// if it can be done without triggering deserialization.
439-
///
440-
/// \returns a pointer to the AST node, or null if not yet deserialized.
441-
T *getWithoutDeserializing() const {
442-
if (isOffset()) {
443-
return nullptr;
444-
}
445-
return GetPtr();
446-
}
447-
448433
/// Retrieve the address of the AST node pointer. Deserializes the pointee if
449434
/// necessary.
450435
T **getAddressOfPointer(ExternalASTSource *Source) const {

clang/include/clang/Sema/MultiplexExternalSemaSource.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,6 @@ class MultiplexExternalSemaSource : public ExternalSemaSource {
9494

9595
bool wasThisDeclarationADefinition(const FunctionDecl *FD) override;
9696

97-
bool hasInitializerWithSideEffects(const VarDecl *VD) const override;
98-
9997
/// Find all declarations with the given name in the
10098
/// given context.
10199
bool FindExternalVisibleDeclsByName(const DeclContext *DC,

clang/include/clang/Serialization/ASTReader.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,12 +1453,6 @@ class ASTReader
14531453
const StringRef &operator*() && = delete;
14541454
};
14551455

1456-
/// VarDecls with initializers containing side effects must be emitted,
1457-
/// but DeclMustBeEmitted is not allowed to deserialize the intializer.
1458-
/// FIXME: Lower memory usage by removing VarDecls once the initializer
1459-
/// is deserialized.
1460-
llvm::SmallPtrSet<Decl *, 16> InitSideEffectVars;
1461-
14621456
public:
14631457
/// Get the buffer for resolving paths.
14641458
SmallString<0> &getPathBuf() { return PathBuf; }
@@ -2410,8 +2404,6 @@ class ASTReader
24102404

24112405
bool wasThisDeclarationADefinition(const FunctionDecl *FD) override;
24122406

2413-
bool hasInitializerWithSideEffects(const VarDecl *VD) const override;
2414-
24152407
/// Retrieve a selector from the given module with its local ID
24162408
/// number.
24172409
Selector getLocalSelector(ModuleFile &M, unsigned LocalID);

clang/lib/AST/Decl.cpp

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2444,24 +2444,16 @@ bool VarDecl::hasInitWithSideEffects() const {
24442444
if (!hasInit())
24452445
return false;
24462446

2447-
// Check if we can get the initializer without deserializing
2448-
const Expr *E = nullptr;
2449-
if (auto *S = dyn_cast<Stmt *>(Init)) {
2450-
E = cast<Expr>(S);
2451-
} else {
2452-
E = cast_or_null<Expr>(getEvaluatedStmt()->Value.getWithoutDeserializing());
2453-
}
2454-
2455-
if (E)
2456-
return E->HasSideEffects(getASTContext()) &&
2457-
// We can get a value-dependent initializer during error recovery.
2458-
(E->isValueDependent() || !evaluateValue());
2459-
2460-
assert(getEvaluatedStmt()->Value.isOffset());
2461-
// ASTReader tracks this without having to deserialize the initializer
2462-
if (auto Source = getASTContext().getExternalSource())
2463-
return Source->hasInitializerWithSideEffects(this);
2464-
return false;
2447+
EvaluatedStmt *ES = ensureEvaluatedStmt();
2448+
if (!ES->CheckedForSideEffects) {
2449+
const Expr *E = getInit();
2450+
ES->HasSideEffects =
2451+
E->HasSideEffects(getASTContext()) &&
2452+
// We can get a value-dependent initializer during error recovery.
2453+
(E->isValueDependent() || !evaluateValue());
2454+
ES->CheckedForSideEffects = true;
2455+
}
2456+
return ES->HasSideEffects;
24652457
}
24662458

24672459
bool VarDecl::isOutOfLine() const {

clang/lib/Sema/MultiplexExternalSemaSource.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -115,14 +115,6 @@ bool MultiplexExternalSemaSource::wasThisDeclarationADefinition(
115115
return false;
116116
}
117117

118-
bool MultiplexExternalSemaSource::hasInitializerWithSideEffects(
119-
const VarDecl *VD) const {
120-
for (const auto &S : Sources)
121-
if (S->hasInitializerWithSideEffects(VD))
122-
return true;
123-
return false;
124-
}
125-
126118
bool MultiplexExternalSemaSource::FindExternalVisibleDeclsByName(
127119
const DeclContext *DC, DeclarationName Name,
128120
const DeclContext *OriginalDC) {

clang/lib/Serialization/ASTReader.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9733,10 +9733,6 @@ bool ASTReader::wasThisDeclarationADefinition(const FunctionDecl *FD) {
97339733
return ThisDeclarationWasADefinitionSet.contains(FD);
97349734
}
97359735

9736-
bool ASTReader::hasInitializerWithSideEffects(const VarDecl *VD) const {
9737-
return InitSideEffectVars.count(VD);
9738-
}
9739-
97409736
Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
97419737
return DecodeSelector(getGlobalSelectorID(M, LocalID));
97429738
}

clang/lib/Serialization/ASTReaderDecl.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,9 +1628,6 @@ RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) {
16281628
VD->NonParmVarDeclBits.PreviousDeclInSameBlockScope =
16291629
VarDeclBits.getNextBit();
16301630

1631-
if (VarDeclBits.getNextBit())
1632-
Reader.InitSideEffectVars.insert(VD);
1633-
16341631
VD->NonParmVarDeclBits.EscapingByref = VarDeclBits.getNextBit();
16351632
HasDeducedType = VarDeclBits.getNextBit();
16361633
VD->NonParmVarDeclBits.ImplicitParamKind =
@@ -1701,6 +1698,8 @@ void ASTDeclReader::ReadVarDeclInit(VarDecl *VD) {
17011698
Eval->HasConstantInitialization = (Val & 2) != 0;
17021699
Eval->HasConstantDestruction = (Val & 4) != 0;
17031700
Eval->WasEvaluated = (Val & 8) != 0;
1701+
Eval->HasSideEffects = (Val & 16) != 0;
1702+
Eval->CheckedForSideEffects = true;
17041703
if (Eval->WasEvaluated) {
17051704
Eval->Evaluated = Record.readAPValue();
17061705
if (Eval->Evaluated.needsCleanup())

clang/lib/Serialization/ASTWriter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7320,6 +7320,10 @@ void ASTRecordWriter::AddVarDeclInit(const VarDecl *VD) {
73207320

73217321
uint64_t Val = 1;
73227322
if (EvaluatedStmt *ES = VD->getEvaluatedStmt()) {
7323+
// This may trigger evaluation, so run it first
7324+
if (VD->hasInitWithSideEffects())
7325+
Val |= 16;
7326+
assert(ES->CheckedForSideEffects);
73237327
Val |= (ES->HasConstantInitialization ? 2 : 0);
73247328
Val |= (ES->HasConstantDestruction ? 4 : 0);
73257329
APValue *Evaluated = VD->getEvaluatedValue();

clang/lib/Serialization/ASTWriterDecl.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1305,7 +1305,6 @@ void ASTDeclWriter::VisitVarDecl(VarDecl *D) {
13051305
VarDeclBits.addBit(D->isConstexpr());
13061306
VarDeclBits.addBit(D->isInitCapture());
13071307
VarDeclBits.addBit(D->isPreviousDeclInSameBlockScope());
1308-
VarDeclBits.addBit(D->hasInitWithSideEffects());
13091308

13101309
VarDeclBits.addBit(D->isEscapingByref());
13111310
HasDeducedType = D->getType()->getContainedDeducedType();

0 commit comments

Comments
 (0)