@@ -802,8 +802,19 @@ void IRGenerator::emitGlobalTopLevel() {
802
802
bool isWholeModule = PrimaryIGM->getSILModule ().isWholeModule ();
803
803
for (SILFunction &f : PrimaryIGM->getSILModule ()) {
804
804
// Only eagerly emit functions that are externally visible.
805
- if (!isPossiblyUsedExternally (f.getLinkage (), isWholeModule))
805
+ if (!isPossiblyUsedExternally (f.getLinkage (), isWholeModule)) {
806
+ // This function is not externally visible.
807
+ // It could be still useful to emit it for debug purposes if it is
808
+ // a debug build with disabled optimizations.
809
+ // Do not emit this function if it is coming from a binary SIL file
810
+ // or if debug info is not supposed to be emitted or if it is a JIT mode.
811
+ if (!PrimaryIGM->IRGen .Opts .Optimize && !PrimaryIGM->IRGen .Opts .UseJIT &&
812
+ f.hasLocation () && !f.getLocation ().isSILFile () &&
813
+ PrimaryIGM->DebugInfo ) {
814
+ addLazyFunction (&f);
815
+ }
806
816
continue ;
817
+ }
807
818
808
819
CurrentIGMPtr IGM = getGenModule (&f);
809
820
IGM->emitSILFunction (&f);
@@ -1660,6 +1671,16 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(SILFunction *f,
1660
1671
llvm::AttributeSet::FunctionIndex, llvm::Attribute::ReadOnly);
1661
1672
}
1662
1673
fn = link.createFunction (*this , fnType, cc, attrs, insertBefore);
1674
+ if (forDefinition && !link.isUsed ()) {
1675
+ if (IRGen.isLazyFunction (f) &&
1676
+ f->hasLocation () && !f->getLocation ().isSILFile () &&
1677
+ link.getName () != SWIFT_ENTRY_POINT_FUNCTION) {
1678
+ // This is a user-defined function and it is an -Onone
1679
+ // compilation. Mark it as used so that LLVM backend
1680
+ // does not remove it.
1681
+ addUsedGlobal (fn);
1682
+ }
1683
+ }
1663
1684
1664
1685
// If we have an order number for this function, set it up as appropriate.
1665
1686
if (hasOrderNumber) {
0 commit comments