Skip to content
Merged
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
41 changes: 40 additions & 1 deletion lib/SILOptimizer/IPO/DeadFunctionElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,47 @@ class ExternalFunctionDefinitionsElimination : FunctionLivenessComputation {

/// ExternalFunctionDefinitionsElimination pass does not take functions
/// reachable via vtables and witness_tables into account when computing
/// a function liveness information.
/// a function liveness information. The only exceptions are external
/// transparent functions, because bodies of external transparent functions
/// should never be removed.
void findAnchorsInTables() override {
// Check vtable methods.
for (SILVTable &vTable : Module->getVTableList()) {
for (auto &entry : vTable.getEntries()) {
SILFunction *F = entry.second;
if (F->isTransparent() && isAvailableExternally(F->getLinkage()))
ensureAlive(F);
}
}

// Check witness methods.
for (SILWitnessTable &WT : Module->getWitnessTableList()) {
bool tableIsAlive = isVisibleExternally(WT.getConformance()->getProtocol());
for (const SILWitnessTable::Entry &entry : WT.getEntries()) {
if (entry.getKind() != SILWitnessTable::Method)
continue;

auto methodWitness = entry.getMethodWitness();
SILFunction *F = methodWitness.Witness;
if (!F)
continue;
if (F->isTransparent() && isAvailableExternally(F->getLinkage()))
ensureAlive(F);
}
}

// Check default witness methods.
for (SILDefaultWitnessTable &WT : Module->getDefaultWitnessTableList()) {
for (const SILDefaultWitnessTable::Entry &entry : WT.getEntries()) {
if (!entry.isValid())
continue;

SILFunction *F = entry.getWitness();
if (F->isTransparent() && isAvailableExternally(F->getLinkage()))
ensureAlive(F);
}
}

}

bool findAliveFunctions() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// RUN: %target-sil-opt -assume-parsing-unqualified-ownership-sil -external-func-definition-elim %s | %FileCheck %s

// Check that public_external transparent methods referenced from vtables
// and witness tables are not removed by ExternalFunctionDefinitionsElimination.

sil_stage canonical

import Builtin
import Swift
import SwiftShims

private class Base {
init()
func foo()
}

private class Derived : Base {
}

sil private @BaseInit : $@convention(method) (@owned Base) -> @owned Base {
bb0(%4 : $Base):
return %4 : $Base
}

sil private @DerivedInit : $@convention(method) (@owned Derived) -> @owned Derived {
bb0(%4 : $Derived):
return %4 : $Derived
}

sil public_external [transparent] @foo : $@convention(method) (@guaranteed Base) -> () {
bb0(%0 : $Base):
%2 = tuple ()
return %2 : $()
} // end sil function 'foo'


sil_vtable Base {
#Base.init!initializer.1: BaseInit
#Base.foo!1: foo
}

sil_vtable Derived {
#Base.init!initializer.1: DerivedInit
#Base.foo!1: foo
}

// CHECK-LABEL: sil public_external [transparent] @foo
// CHECK: end sil function 'foo'

// CHECK-LABEL: sil_vtable Base
// CHECK-: BaseInit

// CHECK-LABEL: sil_vtable Derived
// CHECK: DerivedInit