Skip to content

[don't merge] IRGen should emit code for unused private functions at -Onone -g #2914

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

Closed
Closed
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
23 changes: 22 additions & 1 deletion lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,19 @@ void IRGenerator::emitGlobalTopLevel() {
bool isWholeModule = PrimaryIGM->getSILModule().isWholeModule();
for (SILFunction &f : PrimaryIGM->getSILModule()) {
// Only eagerly emit functions that are externally visible.
if (!isPossiblyUsedExternally(f.getLinkage(), isWholeModule))
if (!isPossiblyUsedExternally(f.getLinkage(), isWholeModule)) {
// This function is not externally visible.
// It could be still useful to emit it for debug purposes if it is
// a debug build with disabled optimizations.
// Do not emit this function if it is coming from a binary SIL file
// or if debug info is not supposed to be emitted or if it is a JIT mode.
if (!PrimaryIGM->IRGen.Opts.Optimize && !PrimaryIGM->IRGen.Opts.UseJIT &&
f.hasLocation() && !f.getLocation().isSILFile() &&
PrimaryIGM->DebugInfo) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In all of LLVM it is considered a bug if the debug options affect codegen. IMHO, we should also try to live by this rule in Swift. Practically, there are few reasons to build at -Onone without debug information anyway, so I think we don't loose much by just deciding based on IRGen.Opts.Optimize.

addLazyFunction(&f);
}
continue;
}

CurrentIGMPtr IGM = getGenModule(&f);
IGM->emitSILFunction(&f);
Expand Down Expand Up @@ -1660,6 +1671,16 @@ llvm::Function *IRGenModule::getAddrOfSILFunction(SILFunction *f,
llvm::AttributeSet::FunctionIndex, llvm::Attribute::ReadOnly);
}
fn = link.createFunction(*this, fnType, cc, attrs, insertBefore);
if (forDefinition && !link.isUsed()) {
if (IRGen.isLazyFunction(f) &&
f->hasLocation() && !f->getLocation().isSILFile() &&
link.getName() != SWIFT_ENTRY_POINT_FUNCTION) {
// This is a user-defined function and it is an -Onone
// compilation. Mark it as used so that LLVM backend
// does not remove it.
addUsedGlobal(fn);
}
}

// If we have an order number for this function, set it up as appropriate.
if (hasOrderNumber) {
Expand Down
5 changes: 5 additions & 0 deletions lib/IRGen/IRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ class IRGenerator {
}
}


bool isLazyFunction(SILFunction *f) const {
return LazilyEmittedFunctions.count(f) != 0;
}

void addLazyTypeMetadata(CanType type) {
// Add it to the queue if it hasn't already been put there.
if (LazilyEmittedTypeMetadata.insert(type).second)
Expand Down
24 changes: 24 additions & 0 deletions test/IRGen/unused.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// RUN: %target-swift-frontend -primary-file %s -emit-ir > %t
// RUN: FileCheck %s --check-prefix=CHECK-ONONE-NODEBUG --check-prefix=CHECK < %t
// RUN: %target-swift-frontend -primary-file %s -emit-ir -g > %t.Onone-debug
// RUN: FileCheck %s --check-prefix=CHECK-ONONE-DEBUG --check-prefix=CHECK < %t.Onone-debug

// Check that unused user-defined functions are preserved at -Onone when -g is used.

// Check that both user functions are marked as used.when using -Onone -g
// CHECK-ONONE-DEBUG: llvm.used{{.*}}_TF6unused7publicFFT_T_{{.*}}_TF6unusedP33_E4A5FE4B57779A0FC903FD67CDBCAAFB8privateFFT_T_{{.*}}

// Check that private function is not marked as used when -g is not used.
// CHECK-ONONE-NODEBUG-NOT: llvm.used{{.*}}_TF6unusedP33_E4A5FE4B57779A0FC903FD67CDBCAAFB8privateFFT_T_{{.*}}

// CHECK-ONONE-DEBUG-LABEL: define{{.*}}void @_TF6unusedP33_E4A5FE4B57779A0FC903FD67CDBCAAFB8privateFFT_T_
// CHECK-ONONE-NODEBUG-NOT: @_TF6unusedP33_E4A5FE4B57779A0FC903FD67CDBCAAFB8privateFFT_T_
private func privateF() {
print(1)
}

// CHECK-LABEL: define{{.*}}void @_TF6unused7publicFFT_T_()
public func publicF() {
print(2)
}