Skip to content

Commit e129ba4

Browse files
committed
IRGen should emit code for unused private functions at -Onone -g
This allows for setting breakpoints on such functions and calling them when evaluating expressions in the debugger.
1 parent 24988f8 commit e129ba4

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -802,8 +802,19 @@ void IRGenerator::emitGlobalTopLevel() {
802802
bool isWholeModule = PrimaryIGM->getSILModule().isWholeModule();
803803
for (SILFunction &f : PrimaryIGM->getSILModule()) {
804804
// 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+
}
806816
continue;
817+
}
807818

808819
CurrentIGMPtr IGM = getGenModule(&f);
809820
IGM->emitSILFunction(&f);
@@ -1660,6 +1671,16 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(SILFunction *f,
16601671
llvm::AttributeSet::FunctionIndex, llvm::Attribute::ReadOnly);
16611672
}
16621673
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+
}
16631684

16641685
// If we have an order number for this function, set it up as appropriate.
16651686
if (hasOrderNumber) {

lib/IRGen/IRGenModule.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,11 @@ class IRGenerator {
281281
}
282282
}
283283

284+
285+
bool isLazyFunction(SILFunction *f) const {
286+
return LazilyEmittedFunctions.count(f) != 0;
287+
}
288+
284289
void addLazyTypeMetadata(CanType type) {
285290
// Add it to the queue if it hasn't already been put there.
286291
if (LazilyEmittedTypeMetadata.insert(type).second)

test/IRGen/unused.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// RUN: %target-swift-frontend -primary-file %s -emit-ir > %t
2+
// RUN: FileCheck %s --check-prefix=CHECK-ONONE-NODEBUG --check-prefix=CHECK < %t
3+
// RUN: %target-swift-frontend -primary-file %s -emit-ir -g > %t.Onone-debug
4+
// RUN: FileCheck %s --check-prefix=CHECK-ONONE-DEBUG --check-prefix=CHECK < %t.Onone-debug
5+
6+
// Check that unused user-defined functions are preserved at -Onone when -g is used.
7+
8+
// Check that both user functions are marked as used.when using -Onone -g
9+
// CHECK-ONONE-DEBUG: llvm.used{{.*}}_TF6unused7publicFFT_T_{{.*}}_TF6unusedP33_E4A5FE4B57779A0FC903FD67CDBCAAFB8privateFFT_T_{{.*}}
10+
11+
// Check that private function is not marked as used when -g is not used.
12+
// CHECK-ONONE-NODEBUG-NOT: llvm.used{{.*}}_TF6unusedP33_E4A5FE4B57779A0FC903FD67CDBCAAFB8privateFFT_T_{{.*}}
13+
14+
// CHECK-ONONE-DEBUG-LABEL: define{{.*}}void @_TF6unusedP33_E4A5FE4B57779A0FC903FD67CDBCAAFB8privateFFT_T_
15+
// CHECK-ONONE-NODEBUG-NOT: @_TF6unusedP33_E4A5FE4B57779A0FC903FD67CDBCAAFB8privateFFT_T_
16+
private func privateF() {
17+
print(1)
18+
}
19+
20+
// CHECK-LABEL: define{{.*}}void @_TF6unused7publicFFT_T_()
21+
public func publicF() {
22+
print(2)
23+
}
24+

0 commit comments

Comments
 (0)