Skip to content

Commit 1d78da3

Browse files
committed
Merge pull request #2185 from swiftix/master
Minor re-factoring of SILModule::hasFunction
2 parents 9174a46 + 768e892 commit 1d78da3

File tree

1 file changed

+48
-30
lines changed

1 file changed

+48
-30
lines changed

lib/SIL/SILModule.cpp

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -484,47 +484,65 @@ bool SILModule::linkFunction(StringRef Name, SILModule::LinkingMode Mode) {
484484
}
485485

486486
SILFunction *SILModule::hasFunction(StringRef Name, SILLinkage Linkage) {
487-
SILFunction *F = lookUpFunction(Name);
488-
489487
assert((Linkage == SILLinkage::Public ||
490488
Linkage == SILLinkage::PublicExternal) &&
491489
"Only a lookup of public functions is supported currently");
492490

493-
// Nothing to do if the current module has a required function
494-
// with a proper linkage.
495-
if (F && F->getLinkage() == Linkage)
496-
return F;
491+
SILFunction *F = nullptr;
497492

498-
assert((!F || F->getLinkage() != Linkage) &&
499-
"hasFunction should be only called for functions that are not "
500-
"contained in the SILModule yet or do not have a required linkage");
501-
(void)F;
493+
// First, check if there is a function with a required name in the
494+
// current module.
495+
SILFunction *CurF = lookUpFunction(Name);
502496

503-
SILLinkerVisitor Visitor(*this, getSILLoader(),
504-
SILModule::LinkingMode::LinkNormal);
497+
// Nothing to do if the current module has a required function
498+
// with a proper linkage already.
499+
if (CurF && CurF->getLinkage() == Linkage) {
500+
F = CurF;
501+
} else {
502+
assert((!CurF || CurF->getLinkage() != Linkage) &&
503+
"hasFunction should be only called for functions that are not "
504+
"contained in the SILModule yet or do not have a required linkage");
505+
}
505506

506-
if (Visitor.hasFunction(Name, Linkage)) {
507+
if (!F) {
508+
SILLinkerVisitor Visitor(*this, getSILLoader(),
509+
SILModule::LinkingMode::LinkNormal);
510+
if (CurF) {
511+
// Perform this lookup only if a function with a given
512+
// name is present in the current module.
513+
// This is done to reduce the amount of IO from the
514+
// swift module file.
515+
if (!Visitor.hasFunction(Name, Linkage))
516+
return nullptr;
517+
// The function in the current module will be changed.
518+
F = CurF;
519+
}
520+
521+
// If function with a given name wasn't seen anywhere yet
522+
// or if it is known to exist, perform a lookup.
507523
if (!F) {
508-
// Load the function.
524+
// Try to load the function from other modules.
509525
F = Visitor.lookupFunction(Name, Linkage);
510-
assert(F && "Function should be present");
511-
assert(F->getLinkage() == Linkage &&
512-
"SILFunction has a wrong linkage");
513-
}
514-
// If a function exists already and it is a non-optimizing
515-
// compilation, simply convert it into an external declaration,
516-
// so that a compiled version from the shared library is used.
517-
if (F->isDefinition() &&
518-
F->getModule().getOptions().Optimization <
519-
SILOptions::SILOptMode::Optimize) {
520-
F->convertToDeclaration();
526+
// Bail if nothing was found and we are not sure if
527+
// this function exists elsewhere.
528+
if (!F)
529+
return nullptr;
530+
assert(F && "SILFunction should be present in one of the modules");
531+
assert(F->getLinkage() == Linkage && "SILFunction has a wrong linkage");
521532
}
522-
if (F->isExternalDeclaration())
523-
F->setFragile(IsFragile_t::IsNotFragile);
524-
F->setLinkage(Linkage);
525-
} else {
526-
F = nullptr;
527533
}
534+
535+
// If a function exists already and it is a non-optimizing
536+
// compilaiton, simply convert it into an external declaration,
537+
// so that a compiled version from the shared library is used.
538+
if (F->isDefinition() &&
539+
F->getModule().getOptions().Optimization <
540+
SILOptions::SILOptMode::Optimize) {
541+
F->convertToDeclaration();
542+
}
543+
if (F->isExternalDeclaration())
544+
F->setFragile(IsFragile_t::IsNotFragile);
545+
F->setLinkage(Linkage);
528546
return F;
529547
}
530548

0 commit comments

Comments
 (0)