Minor re-factoring of SILModule::hasFunction

Speed-up the lookup of a function with a given name in different modules by reducing the amount of IO operations reading from swift module files.
This commit is contained in:
Roman Levenstein
2016-04-14 12:12:19 -07:00
parent 7c5c45dbea
commit 768e892f85

View File

@@ -484,47 +484,65 @@ bool SILModule::linkFunction(StringRef Name, SILModule::LinkingMode Mode) {
}
SILFunction *SILModule::hasFunction(StringRef Name, SILLinkage Linkage) {
SILFunction *F = lookUpFunction(Name);
assert((Linkage == SILLinkage::Public ||
Linkage == SILLinkage::PublicExternal) &&
"Only a lookup of public functions is supported currently");
SILFunction *F = nullptr;
// First, check if there is a function with a required name in the
// current module.
SILFunction *CurF = lookUpFunction(Name);
// Nothing to do if the current module has a required function
// with a proper linkage.
if (F && F->getLinkage() == Linkage)
return F;
assert((!F || F->getLinkage() != Linkage) &&
"hasFunction should be only called for functions that are not "
"contained in the SILModule yet or do not have a required linkage");
(void)F;
SILLinkerVisitor Visitor(*this, getSILLoader(),
SILModule::LinkingMode::LinkNormal);
if (Visitor.hasFunction(Name, Linkage)) {
if (!F) {
// Load the function.
F = Visitor.lookupFunction(Name, Linkage);
assert(F && "Function should be present");
assert(F->getLinkage() == Linkage &&
"SILFunction has a wrong linkage");
}
// If a function exists already and it is a non-optimizing
// compilation, simply convert it into an external declaration,
// so that a compiled version from the shared library is used.
if (F->isDefinition() &&
F->getModule().getOptions().Optimization <
SILOptions::SILOptMode::Optimize) {
F->convertToDeclaration();
}
if (F->isExternalDeclaration())
F->setFragile(IsFragile_t::IsNotFragile);
F->setLinkage(Linkage);
// with a proper linkage already.
if (CurF && CurF->getLinkage() == Linkage) {
F = CurF;
} else {
F = nullptr;
assert((!CurF || CurF->getLinkage() != Linkage) &&
"hasFunction should be only called for functions that are not "
"contained in the SILModule yet or do not have a required 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))
return nullptr;
// The function in the current module will be changed.
F = CurF;
}
// If function with a given name wasn't seen anywhere yet
// 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);
// Bail if nothing was found and we are not sure if
// this function exists elsewhere.
if (!F)
return nullptr;
assert(F && "SILFunction should be present in one of the modules");
assert(F->getLinkage() == Linkage && "SILFunction has a wrong linkage");
}
}
// If a function exists already and it is a non-optimizing
// compilaiton, simply convert it into an external declaration,
// so that a compiled version from the shared library is used.
if (F->isDefinition() &&
F->getModule().getOptions().Optimization <
SILOptions::SILOptMode::Optimize) {
F->convertToDeclaration();
}
if (F->isExternalDeclaration())
F->setFragile(IsFragile_t::IsNotFragile);
F->setLinkage(Linkage);
return F;
}