Skip to content

Commit 0405ab5

Browse files
committed
AST: GenericContexts store a GenericSignature instead of a GenericEnvironment
This eliminates the entire 'lazy generic environment' concept; essentially, all generic environments are now lazy, and since each signature has exactly one environment, their construction no longer needs to be co-ordinated with deserialization.
1 parent e027c82 commit 0405ab5

File tree

11 files changed

+48
-196
lines changed

11 files changed

+48
-196
lines changed

include/swift/AST/ASTContext.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@ namespace swift {
6868
class InFlightDiagnostic;
6969
class IterableDeclContext;
7070
class LazyContextData;
71-
class LazyGenericContextData;
7271
class LazyIterableDeclContextData;
7372
class LazyMemberLoader;
7473
class LazyResolver;
@@ -817,15 +816,6 @@ class ASTContext final {
817816
LazyContextData *getOrCreateLazyContextData(const DeclContext *decl,
818817
LazyMemberLoader *lazyLoader);
819818

820-
/// Get the lazy function data for the given generic context.
821-
///
822-
/// \param lazyLoader If non-null, the lazy loader to use when creating the
823-
/// function data. The pointer must either be null or be consistent
824-
/// across all calls for the same \p func.
825-
LazyGenericContextData *getOrCreateLazyGenericContextData(
826-
const GenericContext *dc,
827-
LazyMemberLoader *lazyLoader);
828-
829819
/// Get the lazy iterable context for the given iterable declaration context.
830820
///
831821
/// \param lazyLoader If non-null, the lazy loader to use when creating the

include/swift/AST/Decl.h

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1482,18 +1482,11 @@ class alignas(8) _GenericContext {
14821482
/// moves the trailing where clause into the generic parameter list.
14831483
TrailingWhereClause *TrailingWhere = nullptr;
14841484

1485-
/// The generic signature or environment of this declaration.
1486-
///
1487-
/// When this declaration stores only a signature, the generic
1488-
/// environment will be lazily loaded.
1489-
mutable llvm::PointerUnion<GenericSignature *, GenericEnvironment *>
1490-
GenericSigOrEnv;
1485+
/// The generic signature of this declaration.
1486+
GenericSignature *GenericSig = nullptr;
14911487
};
14921488

14931489
class GenericContext : private _GenericContext, public DeclContext {
1494-
/// Lazily populate the generic environment.
1495-
GenericEnvironment *getLazyGenericEnvironmentSlow() const;
1496-
14971490
protected:
14981491
GenericContext(DeclContextKind Kind, DeclContext *Parent)
14991492
: _GenericContext(), DeclContext(Kind, Parent) { }
@@ -1542,18 +1535,12 @@ class GenericContext : private _GenericContext, public DeclContext {
15421535
/// Retrieve the generic requirements.
15431536
ArrayRef<Requirement> getGenericRequirements() const;
15441537

1545-
/// Set a lazy generic environment.
1546-
void setLazyGenericEnvironment(LazyMemberLoader *lazyLoader,
1547-
GenericSignature *genericSig,
1548-
uint64_t genericEnvData);
1549-
1550-
/// Whether this generic context has a lazily-created generic environment
1551-
/// that has not yet been constructed.
1552-
bool hasLazyGenericEnvironment() const;
1553-
15541538
/// Set the generic context of this context.
15551539
void setGenericEnvironment(GenericEnvironment *genericEnv);
15561540

1541+
/// Set the generic signature of this context.
1542+
void setGenericSignature(GenericSignature *genericSig);
1543+
15571544
/// Retrieve the position of any where clause for this context's
15581545
/// generic parameters.
15591546
SourceRange getGenericTrailingWhereClauseSourceRange() const;

include/swift/AST/DeclContext.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -360,10 +360,6 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
360360
/// of its parents.
361361
GenericEnvironment *getGenericEnvironmentOfContext() const;
362362

363-
/// Whether the context has a generic environment that will be constructed
364-
/// on first access (but has not yet been constructed).
365-
bool contextHasLazyGenericEnvironment() const;
366-
367363
/// Map an interface type to a contextual type within this context.
368364
Type mapTypeIntoContext(Type type) const;
369365

include/swift/AST/LazyResolver.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,8 @@ class LazyContextData {
8585
LazyMemberLoader *loader;
8686
};
8787

88-
/// Context data for generic contexts.
89-
class LazyGenericContextData : public LazyContextData {
90-
public:
91-
/// The context data used for loading the generic environment.
92-
uint64_t genericEnvData = 0;
93-
};
94-
9588
/// Context data for iterable decl contexts.
96-
class LazyIterableDeclContextData : public LazyGenericContextData {
89+
class LazyIterableDeclContextData : public LazyContextData {
9790
public:
9891
/// The context data used for loading all of the members of the iterable
9992
/// context.
@@ -145,10 +138,6 @@ class alignas(void*) LazyMemberLoader {
145138
virtual Type loadAssociatedTypeDefault(const AssociatedTypeDecl *ATD,
146139
uint64_t contextData) = 0;
147140

148-
/// Returns the generic environment.
149-
virtual GenericEnvironment *loadGenericEnvironment(const DeclContext *decl,
150-
uint64_t contextData) = 0;
151-
152141
/// Loads the requirement signature for a protocol.
153142
virtual void
154143
loadRequirementSignature(const ProtocolDecl *proto, uint64_t contextData,

include/swift/Basic/Statistics.def

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,6 @@ FRONTEND_STATISTIC(Sema, NumFunctionsTypechecked)
249249
/// amount of work the GSB does analyzing type signatures.
250250
FRONTEND_STATISTIC(Sema, NumGenericSignatureBuilders)
251251

252-
/// Number of lazy generic environments registered.
253-
FRONTEND_STATISTIC(Sema, NumLazyGenericEnvironments)
254-
255-
/// Number of lazy generic environments deserialized.
256-
FRONTEND_STATISTIC(Sema, NumLazyGenericEnvironmentsLoaded)
257-
258252
/// Number of lazy requirement signatures registered.
259253
FRONTEND_STATISTIC(Sema, NumLazyRequirementSignatures)
260254

lib/AST/ASTContext.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1970,10 +1970,10 @@ LazyContextData *ASTContext::getOrCreateLazyContextData(
19701970
assert(lazyLoader && "Queried lazy data for non-lazy iterable context");
19711971
if (isa<ProtocolDecl>(dc))
19721972
entry = Allocate<LazyProtocolData>();
1973-
else if (isa<NominalTypeDecl>(dc) || isa<ExtensionDecl>(dc))
1973+
else {
1974+
assert(isa<NominalTypeDecl>(dc) || isa<ExtensionDecl>(dc));
19741975
entry = Allocate<LazyIterableDeclContextData>();
1975-
else
1976-
entry = Allocate<LazyGenericContextData>();
1976+
}
19771977

19781978
entry->loader = lazyLoader;
19791979
return entry;
@@ -1992,13 +1992,6 @@ LazyIterableDeclContextData *ASTContext::getOrCreateLazyIterableContextData(
19921992
lazyLoader);
19931993
}
19941994

1995-
LazyGenericContextData *ASTContext::getOrCreateLazyGenericContextData(
1996-
const GenericContext *dc,
1997-
LazyMemberLoader *lazyLoader) {
1998-
return (LazyGenericContextData *)getOrCreateLazyContextData(dc,
1999-
lazyLoader);
2000-
}
2001-
20021995
bool ASTContext::hasDelayedConformanceErrors() const {
20031996
for (const auto &entry : getImpl().DelayedConformanceDiags) {
20041997
auto &diagnostics = entry.getSecond();

lib/AST/Decl.cpp

Lines changed: 11 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ using namespace swift;
6666

6767
#define DEBUG_TYPE "Serialization"
6868

69-
STATISTIC(NumLazyGenericEnvironments,
70-
"# of lazily-deserialized generic environments known");
71-
STATISTIC(NumLazyGenericEnvironmentsLoaded,
72-
"# of lazily-deserialized generic environments loaded");
7369
STATISTIC(NumLazyRequirementSignatures,
7470
"# of lazily-deserialized requirement signatures known");
7571

@@ -829,11 +825,8 @@ void GenericContext::setGenericParams(GenericParamList *params) {
829825
}
830826

831827
GenericSignature *GenericContext::getGenericSignature() const {
832-
if (auto genericEnv = GenericSigOrEnv.dyn_cast<GenericEnvironment *>())
833-
return genericEnv->getGenericSignature();
834-
835-
if (auto genericSig = GenericSigOrEnv.dyn_cast<GenericSignature *>())
836-
return genericSig;
828+
if (GenericSig)
829+
return GenericSig;
837830

838831
// The signature of a Protocol is trivial (Self: TheProtocol) so let's compute
839832
// it.
@@ -842,81 +835,28 @@ GenericSignature *GenericContext::getGenericSignature() const {
842835
auto self = PD->getSelfInterfaceType()->castTo<GenericTypeParamType>();
843836
auto req =
844837
Requirement(RequirementKind::Conformance, self, PD->getDeclaredType());
845-
auto *genericSig = GenericSignature::get({self}, {req});
846-
847-
// Save it for next time.
848-
const_cast<GenericContext *>(this)->GenericSigOrEnv = genericSig;
849-
return genericSig;
838+
const_cast<GenericContext *>(this)->GenericSig
839+
= GenericSignature::get({self}, {req});
840+
return GenericSig;
850841
}
851842

852843
return nullptr;
853844
}
854845

855846
GenericEnvironment *GenericContext::getGenericEnvironment() const {
856-
// Fast case: we already have a generic environment.
857-
if (auto genericEnv = GenericSigOrEnv.dyn_cast<GenericEnvironment *>())
858-
return genericEnv;
859-
860-
// If we only have a generic signature, build the generic environment.
861-
if (GenericSigOrEnv.dyn_cast<GenericSignature *>() || isa<ProtocolDecl>(this))
862-
return getLazyGenericEnvironmentSlow();
847+
if (auto *genericSig = getGenericSignature())
848+
return genericSig->getGenericEnvironment();
863849

864850
return nullptr;
865851
}
866852

867-
bool GenericContext::hasLazyGenericEnvironment() const {
868-
return GenericSigOrEnv.dyn_cast<GenericSignature *>() != nullptr;
869-
}
870-
871853
void GenericContext::setGenericEnvironment(GenericEnvironment *genericEnv) {
872-
assert((GenericSigOrEnv.isNull() ||
873-
getGenericSignature()->getCanonicalSignature() ==
874-
genericEnv->getGenericSignature()->getCanonicalSignature()) &&
875-
"set a generic environment with a different generic signature");
876-
this->GenericSigOrEnv = genericEnv;
854+
setGenericSignature(genericEnv ? genericEnv->getGenericSignature() : nullptr);
877855
}
878856

879-
GenericEnvironment *
880-
GenericContext::getLazyGenericEnvironmentSlow() const {
881-
assert(GenericSigOrEnv.is<GenericSignature *>() &&
882-
"not a lazily computed generic environment");
883-
884-
if (auto PD = dyn_cast<ProtocolDecl>(this)) {
885-
// The signature of a Protocol is trivial (Self: TheProtocol) so let's
886-
// compute it directly.
887-
auto *genericEnv = getGenericSignature()->getGenericEnvironment();
888-
const_cast<GenericContext *>(this)->setGenericEnvironment(genericEnv);
889-
return genericEnv;
890-
}
891-
892-
auto contextData = getASTContext().getOrCreateLazyGenericContextData(
893-
this, nullptr);
894-
auto *genericEnv = contextData->loader->loadGenericEnvironment(
895-
this, contextData->genericEnvData);
896-
897-
const_cast<GenericContext *>(this)->setGenericEnvironment(genericEnv);
898-
++NumLazyGenericEnvironmentsLoaded;
899-
// FIXME: (transitional) increment the redundant "always-on" counter.
900-
if (getASTContext().Stats)
901-
getASTContext().Stats->getFrontendCounters().NumLazyGenericEnvironmentsLoaded++;
902-
return genericEnv;
903-
}
904-
905-
void GenericContext::setLazyGenericEnvironment(LazyMemberLoader *lazyLoader,
906-
GenericSignature *genericSig,
907-
uint64_t genericEnvData) {
908-
assert(GenericSigOrEnv.isNull() && "already have a generic signature");
909-
GenericSigOrEnv = genericSig;
910-
911-
auto contextData =
912-
getASTContext().getOrCreateLazyGenericContextData(this, lazyLoader);
913-
contextData->genericEnvData = genericEnvData;
914-
915-
++NumLazyGenericEnvironments;
916-
// FIXME: (transitional) increment the redundant "always-on" counter.
917-
if (getASTContext().Stats)
918-
getASTContext().Stats->getFrontendCounters().NumLazyGenericEnvironments++;
919-
857+
void GenericContext::setGenericSignature(GenericSignature *genericSig) {
858+
assert(GenericSig == nullptr && "Generic signature cannot be changed");
859+
this->GenericSig = genericSig;
920860
}
921861

922862
SourceRange GenericContext::getGenericTrailingWhereClauseSourceRange() const {

lib/AST/DeclContext.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,6 @@ GenericEnvironment *DeclContext::getGenericEnvironmentOfContext() const {
175175
return nullptr;
176176
}
177177

178-
bool DeclContext::contextHasLazyGenericEnvironment() const {
179-
auto dc = this;
180-
do {
181-
if (auto decl = dc->getAsDecl())
182-
if (auto GC = decl->getAsGenericContext())
183-
return GC->hasLazyGenericEnvironment();
184-
} while ((dc = dc->getParent()));
185-
186-
return false;
187-
}
188-
189178
Type DeclContext::mapTypeIntoContext(Type type) const {
190179
return GenericEnvironment::mapTypeIntoContext(
191180
getGenericEnvironmentOfContext(), type);

lib/ClangImporter/ImporterImpl.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,12 +1242,6 @@ class LLVM_LIBRARY_VISIBILITY ClangImporter::Implementation
12421242
uint64_t contextData) override {
12431243
llvm_unreachable("unimplemented for ClangImporter");
12441244
}
1245-
1246-
/// Returns the generic environment.
1247-
virtual GenericEnvironment *loadGenericEnvironment(const DeclContext *decl,
1248-
uint64_t contextData) override {
1249-
llvm_unreachable("unimplemented for ClangImporter");
1250-
}
12511245

12521246
void loadRequirementSignature(const ProtocolDecl *decl, uint64_t contextData,
12531247
SmallVectorImpl<Requirement> &reqs) override {

0 commit comments

Comments
 (0)