Skip to content

CrossModuleOptimization: serialized witness tables in embedded mode #75987

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

Merged
merged 1 commit into from
Aug 21, 2024
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
72 changes: 41 additions & 31 deletions lib/SILOptimizer/IPO/CrossModuleOptimization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,43 +301,53 @@ void CrossModuleOptimization::serializeFunctionsInModule(SILPassManager *manager
}

void CrossModuleOptimization::serializeWitnessTablesInModule() {
if (!isPackageCMOEnabled(M.getSwiftModule()))
if (!isPackageCMOEnabled(M.getSwiftModule()) && !everything)
return;

for (auto &wt : M.getWitnessTables()) {
if (wt.getSerializedKind() != getRightSerializedKind(M) &&
hasPublicOrPackageVisibility(wt.getLinkage(), /*includePackage*/ true)) {
auto unserializedWTMethodRange = llvm::make_filter_range(
wt.getEntries(), [&](const SILWitnessTable::Entry &entry) {
return entry.getKind() == SILWitnessTable::Method &&
entry.getMethodWitness().Witness->getSerializedKind() !=
getRightSerializedKind(M);
});
// In Package CMO, we try serializing witness thunks that
// are private if they don't contain hidden or private
// references. If they are serialized, they are set to
// a shared linkage. If they can't be serialized, we set
// the linkage to package so that the witness table itself
// can still be serialized, thus giving a chance for entires
// that _are_ serialized to be accessed directly.
for (const SILWitnessTable::Entry &entry: unserializedWTMethodRange) {
if (entry.getMethodWitness().Witness->getLinkage() == SILLinkage::Private)
entry.getMethodWitness().Witness->setLinkage(SILLinkage::Package);
if (wt.getSerializedKind() == getRightSerializedKind(M))
continue;

if (!hasPublicOrPackageVisibility(wt.getLinkage(), /*includePackage*/ true) && !everything)
continue;

bool containsInternal = false;

for (const SILWitnessTable::Entry &entry : wt.getEntries()) {
if (entry.getKind() != SILWitnessTable::Method)
continue;

SILFunction *witness = entry.getMethodWitness().Witness;
if (!witness)
continue;

if (everything) {
makeFunctionUsableFromInline(witness);
} else {
assert(isPackageCMOEnabled(M.getSwiftModule()));

// In Package CMO, we try serializing witness thunks that
// are private if they don't contain hidden or private
// references. If they are serialized, they are set to
// a shared linkage. If they can't be serialized, we set
// the linkage to package so that the witness table itself
// can still be serialized, thus giving a chance for entires
// that _are_ serialized to be accessed directly.
if (witness->getSerializedKind() != getRightSerializedKind(M) &&
witness->getLinkage() == SILLinkage::Private) {
witness->setLinkage(SILLinkage::Package);
}
}

bool containsInternal = llvm::any_of(
wt.getEntries(), [&](const SILWitnessTable::Entry &entry) {
return entry.getKind() == SILWitnessTable::Method &&
!entry.getMethodWitness()
.Witness->hasValidLinkageForFragileRef(
getRightSerializedKind(M));
});
// FIXME: This check shouldn't be necessary but added as a caution
// to ensure we don't serialize witness table if it contains an
// internal entry.
if (!containsInternal)
wt.setSerializedKind(getRightSerializedKind(M));
if (!witness->hasValidLinkageForFragileRef(getRightSerializedKind(M)))
containsInternal = true;
}

// FIXME: This check shouldn't be necessary but added as a caution
// to ensure we don't serialize witness table if it contains an
// internal entry.
if (!containsInternal)
wt.setSerializedKind(getRightSerializedKind(M));
}
}

Expand Down
49 changes: 49 additions & 0 deletions test/embedded/generic-modules.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// RUN: %empty-directory(%t)
// RUN: %{python} %utils/split_file.py -o %t %s

// Check that this compiles successfully.

// RUN: %target-swift-frontend -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -enable-experimental-feature Embedded -parse-as-library
// RUN: %target-swift-frontend -c -o %t/Main.o -I %t %t/Main.swift -enable-experimental-feature Embedded -parse-as-library

// REQUIRES: OS=macosx || OS=linux-gnu

// BEGIN MyModule.swift

public func foo<Info: Collection>(info: Info) {
let b = MyContainer()
_ = b.dropFirst()
}

struct MyContainer {
var x = 42
init() { }
}

extension MyContainer: Collection {
typealias Index = Int
var startIndex: Index { fatalError() }
var endIndex: Index { fatalError() }

subscript(_ index: Index) -> UInt8 {
get {
fatalError()
}
set {
fatalError()
}
}

func index(after i: Int) -> Int {
fatalError()
}
}

// BEGIN Main.swift

import MyModule

public func main() {
foo(info: [1, 2, 3])
}