Skip to content

Commit 08e20c0

Browse files
authored
Merge pull request #21929 from slavapestov/cache-superclass-decl-request-5.0
AST: Cache SuperclassDeclRequest [5.0]
2 parents f9c124e + 407e818 commit 08e20c0

File tree

6 files changed

+57
-15
lines changed

6 files changed

+57
-15
lines changed

include/swift/AST/Decl.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3569,11 +3569,16 @@ class ClassDecl final : public NominalTypeDecl {
35693569
void createObjCMethodLookup();
35703570

35713571
struct {
3572+
/// The superclass decl and a bit to indicate whether the
3573+
/// superclass was computed yet or not.
3574+
llvm::PointerIntPair<ClassDecl *, 1, bool> SuperclassDecl;
3575+
35723576
/// The superclass type and a bit to indicate whether the
35733577
/// superclass was computed yet or not.
3574-
llvm::PointerIntPair<Type, 1, bool> Superclass;
3578+
llvm::PointerIntPair<Type, 1, bool> SuperclassType;
35753579
} LazySemanticInfo;
35763580

3581+
friend class SuperclassDeclRequest;
35773582
friend class SuperclassTypeRequest;
35783583
friend class TypeChecker;
35793584

@@ -3898,11 +3903,16 @@ class ProtocolDecl final : public NominalTypeDecl {
38983903
bool existentialTypeSupportedSlow(LazyResolver *resolver);
38993904

39003905
struct {
3906+
/// The superclass decl and a bit to indicate whether the
3907+
/// superclass was computed yet or not.
3908+
llvm::PointerIntPair<ClassDecl *, 1, bool> SuperclassDecl;
3909+
39013910
/// The superclass type and a bit to indicate whether the
39023911
/// superclass was computed yet or not.
3903-
llvm::PointerIntPair<Type, 1, bool> Superclass;
3912+
llvm::PointerIntPair<Type, 1, bool> SuperclassType;
39043913
} LazySemanticInfo;
39053914

3915+
friend class SuperclassDeclRequest;
39063916
friend class SuperclassTypeRequest;
39073917
friend class TypeChecker;
39083918

include/swift/AST/NameLookupRequests.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class UnderlyingTypeDeclsReferencedRequest :
133133
/// Request the superclass declaration for the given class.
134134
class SuperclassDeclRequest :
135135
public SimpleRequest<SuperclassDeclRequest,
136-
CacheKind::Uncached, // FIXME: Cache these
136+
CacheKind::SeparatelyCached,
137137
ClassDecl *,
138138
NominalTypeDecl *> {
139139
public:
@@ -149,6 +149,8 @@ class SuperclassDeclRequest :
149149
public:
150150
// Caching
151151
bool isCached() const { return true; }
152+
Optional<ClassDecl *> getCachedResult() const;
153+
void cacheResult(ClassDecl *value) const;
152154

153155
// Cycle handling
154156
void diagnoseCycle(DiagnosticEngine &diags) const;

lib/AST/Decl.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3919,7 +3919,10 @@ ClassDecl *ProtocolDecl::getSuperclassDecl() const {
39193919
void ProtocolDecl::setSuperclass(Type superclass) {
39203920
assert((!superclass || !superclass->hasArchetype())
39213921
&& "superclass must be interface type");
3922-
LazySemanticInfo.Superclass.setPointerAndInt(superclass, true);
3922+
LazySemanticInfo.SuperclassType.setPointerAndInt(superclass, true);
3923+
LazySemanticInfo.SuperclassDecl.setPointerAndInt(
3924+
superclass ? superclass->getClassOrBoundGenericClass() : nullptr,
3925+
true);
39233926
}
39243927

39253928
bool ProtocolDecl::walkInheritedProtocols(
@@ -6437,7 +6440,10 @@ ClassDecl *ClassDecl::getSuperclassDecl() const {
64376440
void ClassDecl::setSuperclass(Type superclass) {
64386441
assert((!superclass || !superclass->hasArchetype())
64396442
&& "superclass must be interface type");
6440-
LazySemanticInfo.Superclass.setPointerAndInt(superclass, true);
6443+
LazySemanticInfo.SuperclassType.setPointerAndInt(superclass, true);
6444+
LazySemanticInfo.SuperclassDecl.setPointerAndInt(
6445+
superclass ? superclass->getClassOrBoundGenericClass() : nullptr,
6446+
true);
64416447
}
64426448

64436449
ClangNode Decl::getClangNodeImpl() const {

lib/AST/NameLookupRequests.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,30 @@ void UnderlyingTypeDeclsReferencedRequest::noteCycleStep(
7474
//----------------------------------------------------------------------------//
7575
// Superclass declaration computation.
7676
//----------------------------------------------------------------------------//
77+
Optional<ClassDecl *> SuperclassDeclRequest::getCachedResult() const {
78+
auto nominalDecl = std::get<0>(getStorage());
79+
80+
if (auto *classDecl = dyn_cast<ClassDecl>(nominalDecl))
81+
if (classDecl->LazySemanticInfo.SuperclassDecl.getInt())
82+
return classDecl->LazySemanticInfo.SuperclassDecl.getPointer();
83+
84+
if (auto *protocolDecl = dyn_cast<ProtocolDecl>(nominalDecl))
85+
if (protocolDecl->LazySemanticInfo.SuperclassDecl.getInt())
86+
return protocolDecl->LazySemanticInfo.SuperclassDecl.getPointer();
87+
88+
return None;
89+
}
90+
91+
void SuperclassDeclRequest::cacheResult(ClassDecl *value) const {
92+
auto nominalDecl = std::get<0>(getStorage());
93+
94+
if (auto *classDecl = dyn_cast<ClassDecl>(nominalDecl))
95+
classDecl->LazySemanticInfo.SuperclassDecl.setPointerAndInt(value, true);
96+
97+
if (auto *protocolDecl = dyn_cast<ProtocolDecl>(nominalDecl))
98+
protocolDecl->LazySemanticInfo.SuperclassDecl.setPointerAndInt(value, true);
99+
}
100+
77101
void SuperclassDeclRequest::diagnoseCycle(DiagnosticEngine &diags) const {
78102
// FIXME: Improve this diagnostic.
79103
auto subjectDecl = std::get<0>(getStorage());
@@ -87,7 +111,7 @@ void SuperclassDeclRequest::noteCycleStep(DiagnosticEngine &diags) const {
87111
}
88112

89113
//----------------------------------------------------------------------------//
90-
// Superclass declaration computation.
114+
// Extended nominal computation.
91115
//----------------------------------------------------------------------------//
92116
Optional<NominalTypeDecl *> ExtendedNominalRequest::getCachedResult() const {
93117
// Note: if we fail to compute any nominal declaration, it's considered

lib/AST/TypeCheckRequests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,12 @@ Optional<Type> SuperclassTypeRequest::getCachedResult() const {
125125
auto nominalDecl = std::get<0>(getStorage());
126126

127127
if (auto *classDecl = dyn_cast<ClassDecl>(nominalDecl))
128-
if (classDecl->LazySemanticInfo.Superclass.getInt())
129-
return classDecl->LazySemanticInfo.Superclass.getPointer();
128+
if (classDecl->LazySemanticInfo.SuperclassType.getInt())
129+
return classDecl->LazySemanticInfo.SuperclassType.getPointer();
130130

131131
if (auto *protocolDecl = dyn_cast<ProtocolDecl>(nominalDecl))
132-
if (protocolDecl->LazySemanticInfo.Superclass.getInt())
133-
return protocolDecl->LazySemanticInfo.Superclass.getPointer();
132+
if (protocolDecl->LazySemanticInfo.SuperclassType.getInt())
133+
return protocolDecl->LazySemanticInfo.SuperclassType.getPointer();
134134

135135
return None;
136136
}
@@ -139,10 +139,10 @@ void SuperclassTypeRequest::cacheResult(Type value) const {
139139
auto nominalDecl = std::get<0>(getStorage());
140140

141141
if (auto *classDecl = dyn_cast<ClassDecl>(nominalDecl))
142-
classDecl->LazySemanticInfo.Superclass.setPointerAndInt(value, true);
142+
classDecl->LazySemanticInfo.SuperclassType.setPointerAndInt(value, true);
143143

144144
if (auto *protocolDecl = dyn_cast<ProtocolDecl>(nominalDecl))
145-
protocolDecl->LazySemanticInfo.Superclass.setPointerAndInt(value, true);
145+
protocolDecl->LazySemanticInfo.SuperclassType.setPointerAndInt(value, true);
146146
}
147147

148148
//----------------------------------------------------------------------------//

test/Misc/stats_dir_tracer.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// RUN: %target-swiftc_driver -o %t/main -module-name main -stats-output-dir %t %s -trace-stats-events
33
// RUN: %FileCheck -input-file %t/*.csv %s
44

5-
// CHECK: {{[0-9]+,[0-9]+,"exit","typecheck-expr","Sema.NumTypesDeserialized",[0-9]+,[0-9]+,"Call","\[.*stats_dir_tracer.swift.*\]"}}
6-
// CHECK: {{[0-9]+,[0-9]+,"exit","typecheck-expr","Sema.NumConstraintScopes",[0-9]+,[0-9]+,"Sequence","\[.*stats_dir_tracer.swift.*\]"}}
7-
// CHECK: {{[0-9]+,[0-9]+,"exit","SuperclassDeclRequest","Sema.SuperclassDeclRequest",[0-9]+,[0-9]+,"Bar","\[.*stats_dir_tracer.swift.*\]"}}
5+
// CHECK-DAG: {{[0-9]+,[0-9]+,"exit","typecheck-expr","Sema.NumTypesDeserialized",[0-9]+,[0-9]+,"Call","\[.*stats_dir_tracer.swift.*\]"}}
6+
// CHECK-DAG: {{[0-9]+,[0-9]+,"exit","typecheck-expr","Sema.NumConstraintScopes",[0-9]+,[0-9]+,"Sequence","\[.*stats_dir_tracer.swift.*\]"}}
7+
// CHECK-DAG: {{[0-9]+,[0-9]+,"exit","SuperclassDeclRequest","Sema.SuperclassDeclRequest",[0-9]+,[0-9]+,"Bar","\[.*stats_dir_tracer.swift.*\]"}}
88

99
public func foo() {
1010
print("hello")

0 commit comments

Comments
 (0)