Skip to content

Commit 319a51a

Browse files
authored
[Modules] Record whether VarDecl initializers contain side effects (#143739)
Calling `DeclMustBeEmitted` should not lead to more deserialization, as it may occur before previous deserialization has finished. When passed a `VarDecl` with an initializer however, `DeclMustBeEmitted` needs to know whether that initializer contains side effects. When the `VarDecl` is deserialized but the initializer is not, this triggers deserialization of the initializer. To avoid this we add a bit to the serialization format for `VarDecl`s, indicating whether its initializer contains side effects or not, so that the `ASTReader` can query this information directly without deserializing the initializer. rdar://153085264
1 parent 836ff36 commit 319a51a

File tree

11 files changed

+116
-9
lines changed

11 files changed

+116
-9
lines changed

clang/include/clang/AST/Decl.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,6 +1352,11 @@ class VarDecl : public DeclaratorDecl, public Redeclarable<VarDecl> {
13521352
return const_cast<VarDecl *>(this)->getInitializingDeclaration();
13531353
}
13541354

1355+
/// Checks whether this declaration has an initializer with side effects,
1356+
/// without triggering deserialization if the initializer is not yet
1357+
/// deserialized.
1358+
bool hasInitWithSideEffects() const;
1359+
13551360
/// Determine whether this variable's value might be usable in a
13561361
/// constant expression, according to the relevant language standard.
13571362
/// This only checks properties of the declaration, and does not check

clang/include/clang/AST/ExternalASTSource.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class RecordDecl;
5151
class Selector;
5252
class Stmt;
5353
class TagDecl;
54+
class VarDecl;
5455

5556
/// Abstract interface for external sources of AST nodes.
5657
///
@@ -195,6 +196,10 @@ class ExternalASTSource : public RefCountedBase<ExternalASTSource> {
195196
/// module.
196197
virtual bool wasThisDeclarationADefinition(const FunctionDecl *FD);
197198

199+
virtual bool hasInitializerWithSideEffects(const VarDecl *VD) const {
200+
return false;
201+
}
202+
198203
/// Finds all declarations lexically contained within the given
199204
/// DeclContext, after applying an optional filter predicate.
200205
///
@@ -429,6 +434,17 @@ struct LazyOffsetPtr {
429434
return GetPtr();
430435
}
431436

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+
432448
/// Retrieve the address of the AST node pointer. Deserializes the pointee if
433449
/// necessary.
434450
T **getAddressOfPointer(ExternalASTSource *Source) const {

clang/include/clang/Sema/MultiplexExternalSemaSource.h

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

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

97+
bool hasInitializerWithSideEffects(const VarDecl *VD) const override;
98+
9799
/// Find all declarations with the given name in the
98100
/// given context.
99101
bool FindExternalVisibleDeclsByName(const DeclContext *DC,

clang/include/clang/Serialization/ASTReader.h

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

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

24072413
bool wasThisDeclarationADefinition(const FunctionDecl *FD) override;
24082414

2415+
bool hasInitializerWithSideEffects(const VarDecl *VD) const override;
2416+
24092417
/// Retrieve a selector from the given module with its local ID
24102418
/// number.
24112419
Selector getLocalSelector(ModuleFile &M, unsigned LocalID);

clang/lib/AST/ASTContext.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13111,9 +13111,7 @@ bool ASTContext::DeclMustBeEmitted(const Decl *D) {
1311113111
return true;
1311213112

1311313113
// Variables that have initialization with side-effects are required.
13114-
if (VD->getInit() && VD->getInit()->HasSideEffects(*this) &&
13115-
// We can get a value-dependent initializer during error recovery.
13116-
(VD->getInit()->isValueDependent() || !VD->evaluateValue()))
13114+
if (VD->hasInitWithSideEffects())
1311713115
return true;
1311813116

1311913117
// Likewise, variables with tuple-like bindings are required if their

clang/lib/AST/Decl.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2441,6 +2441,30 @@ VarDecl *VarDecl::getInitializingDeclaration() {
24412441
return Def;
24422442
}
24432443

2444+
bool VarDecl::hasInitWithSideEffects() const {
2445+
if (!hasInit())
2446+
return false;
2447+
2448+
// Check if we can get the initializer without deserializing
2449+
const Expr *E = nullptr;
2450+
if (auto *S = dyn_cast<Stmt *>(Init)) {
2451+
E = cast<Expr>(S);
2452+
} else {
2453+
E = cast_or_null<Expr>(getEvaluatedStmt()->Value.getWithoutDeserializing());
2454+
}
2455+
2456+
if (E)
2457+
return E->HasSideEffects(getASTContext()) &&
2458+
// We can get a value-dependent initializer during error recovery.
2459+
(E->isValueDependent() || !evaluateValue());
2460+
2461+
assert(getEvaluatedStmt()->Value.isOffset());
2462+
// ASTReader tracks this without having to deserialize the initializer
2463+
if (auto Source = getASTContext().getExternalSource())
2464+
return Source->hasInitializerWithSideEffects(this);
2465+
return false;
2466+
}
2467+
24442468
bool VarDecl::isOutOfLine() const {
24452469
if (Decl::isOutOfLine())
24462470
return true;

clang/lib/Sema/MultiplexExternalSemaSource.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ 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+
118126
bool MultiplexExternalSemaSource::FindExternalVisibleDeclsByName(
119127
const DeclContext *DC, DeclarationName Name,
120128
const DeclContext *OriginalDC) {

clang/lib/Serialization/ASTReader.cpp

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

9725+
bool ASTReader::hasInitializerWithSideEffects(const VarDecl *VD) const {
9726+
return InitSideEffectVars.count(VD);
9727+
}
9728+
97259729
Selector ASTReader::getLocalSelector(ModuleFile &M, unsigned LocalID) {
97269730
return DecodeSelector(getGlobalSelectorID(M, LocalID));
97279731
}

clang/lib/Serialization/ASTReaderDecl.cpp

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

1631+
if (VarDeclBits.getNextBit())
1632+
Reader.InitSideEffectVars.insert(VD);
1633+
16311634
VD->NonParmVarDeclBits.EscapingByref = VarDeclBits.getNextBit();
16321635
HasDeducedType = VarDeclBits.getNextBit();
16331636
VD->NonParmVarDeclBits.ImplicitParamKind =

clang/lib/Serialization/ASTWriterDecl.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,7 @@ void ASTDeclWriter::VisitVarDecl(VarDecl *D) {
13061306
VarDeclBits.addBit(D->isConstexpr());
13071307
VarDeclBits.addBit(D->isInitCapture());
13081308
VarDeclBits.addBit(D->isPreviousDeclInSameBlockScope());
1309+
VarDeclBits.addBit(D->hasInitWithSideEffects());
13091310

13101311
VarDeclBits.addBit(D->isEscapingByref());
13111312
HasDeducedType = D->getType()->getContainedDeducedType();
@@ -1355,10 +1356,11 @@ void ASTDeclWriter::VisitVarDecl(VarDecl *D) {
13551356
!D->hasExtInfo() && D->getFirstDecl() == D->getMostRecentDecl() &&
13561357
D->getKind() == Decl::Var && !D->isInline() && !D->isConstexpr() &&
13571358
!D->isInitCapture() && !D->isPreviousDeclInSameBlockScope() &&
1358-
!D->isEscapingByref() && !HasDeducedType &&
1359-
D->getStorageDuration() != SD_Static && !D->getDescribedVarTemplate() &&
1360-
!D->getMemberSpecializationInfo() && !D->isObjCForDecl() &&
1361-
!isa<ImplicitParamDecl>(D) && !D->isEscapingByref())
1359+
!D->hasInitWithSideEffects() && !D->isEscapingByref() &&
1360+
!HasDeducedType && D->getStorageDuration() != SD_Static &&
1361+
!D->getDescribedVarTemplate() && !D->getMemberSpecializationInfo() &&
1362+
!D->isObjCForDecl() && !isa<ImplicitParamDecl>(D) &&
1363+
!D->isEscapingByref())
13621364
AbbrevToUse = Writer.getDeclVarAbbrev();
13631365

13641366
Code = serialization::DECL_VAR;
@@ -2731,12 +2733,12 @@ void ASTWriter::WriteDeclAbbrevs() {
27312733
// VarDecl
27322734
Abv->Add(BitCodeAbbrevOp(
27332735
BitCodeAbbrevOp::Fixed,
2734-
21)); // Packed Var Decl bits: Linkage, ModulesCodegen,
2736+
22)); // Packed Var Decl bits: Linkage, ModulesCodegen,
27352737
// SClass, TSCSpec, InitStyle,
27362738
// isARCPseudoStrong, IsThisDeclarationADemotedDefinition,
27372739
// isExceptionVariable, isNRVOVariable, isCXXForRangeDecl,
27382740
// isInline, isInlineSpecified, isConstexpr,
2739-
// isInitCapture, isPrevDeclInSameScope,
2741+
// isInitCapture, isPrevDeclInSameScope, hasInitWithSideEffects,
27402742
// EscapingByref, HasDeducedType, ImplicitParamKind, isObjCForDecl
27412743
Abv->Add(BitCodeAbbrevOp(0)); // VarKind (local enum)
27422744
// Type Source Info
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Tests referencing variable with initializer containing side effect across module boundary
2+
//
3+
// RUN: rm -rf %t
4+
// RUN: mkdir -p %t
5+
// RUN: split-file %s %t
6+
7+
// RUN: %clang_cc1 -std=c++20 -emit-module-interface %t/Foo.cppm \
8+
// RUN: -o %t/Foo.pcm
9+
10+
// RUN: %clang_cc1 -std=c++20 -emit-module-interface \
11+
// RUN: -fmodule-file=Foo=%t/Foo.pcm \
12+
// RUN: %t/Bar.cppm \
13+
// RUN: -o %t/Bar.pcm
14+
15+
// RUN: %clang_cc1 -std=c++20 -emit-obj \
16+
// RUN: -main-file-name Bar.cppm \
17+
// RUN: -fmodule-file=Foo=%t/Foo.pcm \
18+
// RUN: -x pcm %t/Bar.pcm \
19+
// RUN: -o %t/Bar.o
20+
21+
//--- Foo.cppm
22+
export module Foo;
23+
24+
export {
25+
class S {};
26+
27+
inline S s = S{};
28+
}
29+
30+
//--- Bar.cppm
31+
export module Bar;
32+
import Foo;
33+
34+
S bar() {
35+
return s;
36+
}
37+

0 commit comments

Comments
 (0)