Skip to content

Remove unused and slow 'protocol implementations' analysis #15667

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
merged 2 commits into from
Apr 2, 2018
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
7 changes: 0 additions & 7 deletions include/swift/SIL/SILModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -493,13 +493,6 @@ class SILModule {
bool linkFunction(SILFunction *Fun,
LinkingMode LinkAll = LinkingMode::LinkNormal);

/// Attempt to link a function by mangled name. Returns true if linking
/// succeeded, false otherwise.
///
/// \return false if the linking failed.
bool linkFunction(StringRef Name,
LinkingMode LinkAll = LinkingMode::LinkNormal);

/// Check if a given function exists in any of the modules with a
/// required linkage, i.e. it can be linked by linkFunction.
///
Expand Down
12 changes: 0 additions & 12 deletions include/swift/SILOptimizer/Analysis/ClassHierarchyAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ class ClassHierarchyAnalysis : public SILAnalysis {
return IndirectSubclassesCache[C];
}

const NominalTypeList &getProtocolImplementations(ProtocolDecl *P) {
return ProtocolImplementationsCache[P];
}

/// Returns true if the class is inherited by another class in this module.
bool hasKnownDirectSubclasses(ClassDecl *C) {
return DirectSubclassesCache.count(C);
Expand All @@ -96,11 +92,6 @@ class ClassHierarchyAnalysis : public SILAnalysis {
!IndirectSubclassesCache[C].empty();
}

/// Returns true if the protocol is implemented by any class in this module.
bool hasKnownImplementations(ProtocolDecl *C) {
return ProtocolImplementationsCache.count(C);
}

private:
/// Compute inheritance properties.
void init();
Expand All @@ -114,9 +105,6 @@ class ClassHierarchyAnalysis : public SILAnalysis {

/// A cache that maps a class to all of its known indirect subclasses.
llvm::DenseMap<ClassDecl*, ClassList> IndirectSubclassesCache;

/// A cache that maps a protocol to all of its known implementations.
ProtocolImplementations ProtocolImplementationsCache;
};

}
Expand Down
43 changes: 0 additions & 43 deletions lib/SIL/Linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,49 +56,6 @@ bool SILLinkerVisitor::processFunction(SILFunction *F) {
return true;
}

/// Process Decl, recursively deserializing any thing Decl may reference.
bool SILLinkerVisitor::processFunction(StringRef Name) {
if (Mode == LinkingMode::LinkNone)
return false;

// If F is a declaration, first deserialize it.
auto *NewFn = Loader->lookupSILFunction(Name);

if (!NewFn || NewFn->isExternalDeclaration())
return false;

++NumFuncLinked;

// Try to transitively deserialize everything referenced by NewFn.
Worklist.push_back(NewFn);
process();

// Since we successfully processed at least one function, return true.
return true;
}

/// Process Decl, recursively deserializing any thing Decl may reference.
SILFunction *SILLinkerVisitor::lookupFunction(StringRef Name,
SILLinkage Linkage) {

auto *NewFn = Loader->lookupSILFunction(Name, /* declarationOnly */ true,
Linkage);

if (!NewFn)
return nullptr;

assert(NewFn->isExternalDeclaration() &&
"SIL function lookup should never read function bodies");

return NewFn;
}

/// Process Decl, recursively deserializing any thing Decl may reference.
bool SILLinkerVisitor::hasFunction(StringRef Name,
Optional<SILLinkage> Linkage) {
return Loader->hasSILFunction(Name, Linkage);
}

/// Deserialize the VTable mapped to C if it exists and all SIL the VTable
/// transitively references.
///
Expand Down
12 changes: 0 additions & 12 deletions lib/SIL/Linker.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,6 @@ class SILLinkerVisitor : public SILInstructionVisitor<SILLinkerVisitor, bool> {
/// Process F, recursively deserializing any thing F may reference.
bool processFunction(SILFunction *F);

/// Process Name, recursively deserializing any thing function with name Name
/// may reference.
bool processFunction(StringRef Name);

/// Process Name, try to deserialize a declaration of a function with
/// this Name.
SILFunction *lookupFunction(StringRef Name, SILLinkage Linkage);

/// Process Name, try to check if there is a declaration of a function
/// with this Name.
bool hasFunction(StringRef Name, Optional<SILLinkage> Linkage = None);

/// Deserialize the VTable mapped to C if it exists and all SIL the VTable
/// transitively references.
///
Expand Down
15 changes: 4 additions & 11 deletions lib/SIL/SILModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -476,10 +476,6 @@ bool SILModule::linkFunction(SILFunction *Fun, SILModule::LinkingMode Mode) {
return SILLinkerVisitor(*this, getSILLoader(), Mode).processFunction(Fun);
}

bool SILModule::linkFunction(StringRef Name, SILModule::LinkingMode Mode) {
return SILLinkerVisitor(*this, getSILLoader(), Mode).processFunction(Name);
}

SILFunction *SILModule::findFunction(StringRef Name, SILLinkage Linkage) {
assert((Linkage == SILLinkage::Public ||
Linkage == SILLinkage::PublicExternal) &&
Expand All @@ -502,14 +498,12 @@ SILFunction *SILModule::findFunction(StringRef Name, SILLinkage Linkage) {
}

if (!F) {
SILLinkerVisitor Visitor(*this, getSILLoader(),
SILModule::LinkingMode::LinkNormal);
if (CurF) {
// Perform this lookup only if a function with a given
// name is present in the current module.
// This is done to reduce the amount of IO from the
// swift module file.
if (!Visitor.hasFunction(Name, Linkage))
if (!getSILLoader()->hasSILFunction(Name, Linkage))
return nullptr;
// The function in the current module will be changed.
F = CurF;
Expand All @@ -519,7 +513,8 @@ SILFunction *SILModule::findFunction(StringRef Name, SILLinkage Linkage) {
// or if it is known to exist, perform a lookup.
if (!F) {
// Try to load the function from other modules.
F = Visitor.lookupFunction(Name, Linkage);
F = getSILLoader()->lookupSILFunction(Name, /*declarationOnly*/ true,
Linkage);
// Bail if nothing was found and we are not sure if
// this function exists elsewhere.
if (!F)
Expand All @@ -545,9 +540,7 @@ SILFunction *SILModule::findFunction(StringRef Name, SILLinkage Linkage) {
bool SILModule::hasFunction(StringRef Name) {
if (lookUpFunction(Name))
return true;
SILLinkerVisitor Visitor(*this, getSILLoader(),
SILModule::LinkingMode::LinkNormal);
return Visitor.hasFunction(Name);
return getSILLoader()->hasSILFunction(Name);
}

void SILModule::linkAllFromCurrentModule() {
Expand Down
39 changes: 0 additions & 39 deletions lib/SILOptimizer/Analysis/ClassHierarchyAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,14 @@

#include "swift/SILOptimizer/Analysis/ClassHierarchyAnalysis.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/Module.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILValue.h"
#include "swift/SIL/SILModule.h"

using namespace swift;

namespace {
/// A helper class to collect all nominal type declarations.
class NominalTypeWalker: public ASTWalker {
ClassHierarchyAnalysis::ProtocolImplementations &ProtocolImplementationsCache;
public:
NominalTypeWalker(ClassHierarchyAnalysis::ProtocolImplementations
&ProtocolImplementationsCache)
:ProtocolImplementationsCache(ProtocolImplementationsCache) {
}

bool walkToDeclPre(Decl *D) override {
auto *NTD = dyn_cast<NominalTypeDecl>(D);
if (!NTD || !NTD->hasInterfaceType())
return true;
auto Protocols = NTD->getAllProtocols();
// We are only interested in types implementing protocols.
if (!Protocols.empty()) {
for (auto &Protocol : Protocols) {
auto &K = ProtocolImplementationsCache[Protocol];
K.push_back(NTD);
}
}
return true;
}
};
} // end anonymous namespace

void ClassHierarchyAnalysis::init() {
// Process all types implementing protocols.
SmallVector<Decl *, 32> Decls;
// TODO: It would be better if we could get all declarations
// from a given module, not only the top-level ones.
M->getSwiftModule()->getTopLevelDecls(Decls);

NominalTypeWalker Walker(ProtocolImplementationsCache);
for (auto *D: Decls) {
D->walk(Walker);
}

// For each class declaration in our V-table list:
for (auto &VT : M->getVTableList()) {
ClassDecl *C = VT.getClass();
Expand Down