Skip to content

AST: Cache SuperclassDeclRequest [5.0] #21929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3569,11 +3569,16 @@ class ClassDecl final : public NominalTypeDecl {
void createObjCMethodLookup();

struct {
/// The superclass decl and a bit to indicate whether the
/// superclass was computed yet or not.
llvm::PointerIntPair<ClassDecl *, 1, bool> SuperclassDecl;

/// The superclass type and a bit to indicate whether the
/// superclass was computed yet or not.
llvm::PointerIntPair<Type, 1, bool> Superclass;
llvm::PointerIntPair<Type, 1, bool> SuperclassType;
} LazySemanticInfo;

friend class SuperclassDeclRequest;
friend class SuperclassTypeRequest;
friend class TypeChecker;

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

struct {
/// The superclass decl and a bit to indicate whether the
/// superclass was computed yet or not.
llvm::PointerIntPair<ClassDecl *, 1, bool> SuperclassDecl;

/// The superclass type and a bit to indicate whether the
/// superclass was computed yet or not.
llvm::PointerIntPair<Type, 1, bool> Superclass;
llvm::PointerIntPair<Type, 1, bool> SuperclassType;
} LazySemanticInfo;

friend class SuperclassDeclRequest;
friend class SuperclassTypeRequest;
friend class TypeChecker;

Expand Down
4 changes: 3 additions & 1 deletion include/swift/AST/NameLookupRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class UnderlyingTypeDeclsReferencedRequest :
/// Request the superclass declaration for the given class.
class SuperclassDeclRequest :
public SimpleRequest<SuperclassDeclRequest,
CacheKind::Uncached, // FIXME: Cache these
CacheKind::SeparatelyCached,
ClassDecl *,
NominalTypeDecl *> {
public:
Expand All @@ -149,6 +149,8 @@ class SuperclassDeclRequest :
public:
// Caching
bool isCached() const { return true; }
Optional<ClassDecl *> getCachedResult() const;
void cacheResult(ClassDecl *value) const;

// Cycle handling
void diagnoseCycle(DiagnosticEngine &diags) const;
Expand Down
10 changes: 8 additions & 2 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3919,7 +3919,10 @@ ClassDecl *ProtocolDecl::getSuperclassDecl() const {
void ProtocolDecl::setSuperclass(Type superclass) {
assert((!superclass || !superclass->hasArchetype())
&& "superclass must be interface type");
LazySemanticInfo.Superclass.setPointerAndInt(superclass, true);
LazySemanticInfo.SuperclassType.setPointerAndInt(superclass, true);
LazySemanticInfo.SuperclassDecl.setPointerAndInt(
superclass ? superclass->getClassOrBoundGenericClass() : nullptr,
true);
}

bool ProtocolDecl::walkInheritedProtocols(
Expand Down Expand Up @@ -6437,7 +6440,10 @@ ClassDecl *ClassDecl::getSuperclassDecl() const {
void ClassDecl::setSuperclass(Type superclass) {
assert((!superclass || !superclass->hasArchetype())
&& "superclass must be interface type");
LazySemanticInfo.Superclass.setPointerAndInt(superclass, true);
LazySemanticInfo.SuperclassType.setPointerAndInt(superclass, true);
LazySemanticInfo.SuperclassDecl.setPointerAndInt(
superclass ? superclass->getClassOrBoundGenericClass() : nullptr,
true);
}

ClangNode Decl::getClangNodeImpl() const {
Expand Down
26 changes: 25 additions & 1 deletion lib/AST/NameLookupRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,30 @@ void UnderlyingTypeDeclsReferencedRequest::noteCycleStep(
//----------------------------------------------------------------------------//
// Superclass declaration computation.
//----------------------------------------------------------------------------//
Optional<ClassDecl *> SuperclassDeclRequest::getCachedResult() const {
auto nominalDecl = std::get<0>(getStorage());

if (auto *classDecl = dyn_cast<ClassDecl>(nominalDecl))
if (classDecl->LazySemanticInfo.SuperclassDecl.getInt())
return classDecl->LazySemanticInfo.SuperclassDecl.getPointer();

if (auto *protocolDecl = dyn_cast<ProtocolDecl>(nominalDecl))
if (protocolDecl->LazySemanticInfo.SuperclassDecl.getInt())
return protocolDecl->LazySemanticInfo.SuperclassDecl.getPointer();

return None;
}

void SuperclassDeclRequest::cacheResult(ClassDecl *value) const {
auto nominalDecl = std::get<0>(getStorage());

if (auto *classDecl = dyn_cast<ClassDecl>(nominalDecl))
classDecl->LazySemanticInfo.SuperclassDecl.setPointerAndInt(value, true);

if (auto *protocolDecl = dyn_cast<ProtocolDecl>(nominalDecl))
protocolDecl->LazySemanticInfo.SuperclassDecl.setPointerAndInt(value, true);
}

void SuperclassDeclRequest::diagnoseCycle(DiagnosticEngine &diags) const {
// FIXME: Improve this diagnostic.
auto subjectDecl = std::get<0>(getStorage());
Expand All @@ -87,7 +111,7 @@ void SuperclassDeclRequest::noteCycleStep(DiagnosticEngine &diags) const {
}

//----------------------------------------------------------------------------//
// Superclass declaration computation.
// Extended nominal computation.
//----------------------------------------------------------------------------//
Optional<NominalTypeDecl *> ExtendedNominalRequest::getCachedResult() const {
// Note: if we fail to compute any nominal declaration, it's considered
Expand Down
12 changes: 6 additions & 6 deletions lib/AST/TypeCheckRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,12 @@ Optional<Type> SuperclassTypeRequest::getCachedResult() const {
auto nominalDecl = std::get<0>(getStorage());

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

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

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

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

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

//----------------------------------------------------------------------------//
Expand Down
6 changes: 3 additions & 3 deletions test/Misc/stats_dir_tracer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
// RUN: %target-swiftc_driver -o %t/main -module-name main -stats-output-dir %t %s -trace-stats-events
// RUN: %FileCheck -input-file %t/*.csv %s

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

public func foo() {
print("hello")
Expand Down