Skip to content

Commit 356a388

Browse files
committed
Cross-module-optimization: no need to add AST-attributes to make functions always-emit-into-client.
This is less hacky and possible now, as we de-serialize the linkage from SIL (and not just derive it from the AST attributes).
1 parent d224031 commit 356a388

File tree

3 files changed

+18
-40
lines changed

3 files changed

+18
-40
lines changed

lib/SILOptimizer/IPO/CrossModuleSerializationSetup.cpp

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ class CrossModuleSerializationSetup {
6161

6262
void makeSubstUsableFromInline(const SubstitutionMap &substs);
6363

64-
bool markAsEmitIntoClient(SILFunction *F);
65-
6664
public:
6765
CrossModuleSerializationSetup(SILModule &M) : M(M) { }
6866

@@ -313,12 +311,7 @@ void CrossModuleSerializationSetup::scanModule() {
313311

314312
// As a code size optimization, make serialized functions
315313
// @alwaysEmitIntoClient.
316-
if (!markAsEmitIntoClient(F)) {
317-
// We don't have a declaration to put the attribute on (e.g. in case
318-
// the function is a closure). Just make the function public instead
319-
// of @alwaysEmitIntoClient.
320-
F->setLinkage(SILLinkage::Public);
321-
}
314+
F->setLinkage(SILLinkage::PublicNonABI);
322315
} else {
323316
// If for some reason the function cannot be serialized, we mark it as
324317
// usable-from-inline.
@@ -328,36 +321,6 @@ void CrossModuleSerializationSetup::scanModule() {
328321
}
329322
}
330323

331-
/// Marks a function as @alwaysEmitIntoClient and returns true if this is
332-
/// successful.
333-
bool CrossModuleSerializationSetup::markAsEmitIntoClient(SILFunction *F) {
334-
auto *DC = F->getDeclContext();
335-
if (!DC)
336-
return false;
337-
338-
Decl *decl = DC->getAsDecl();
339-
if (!decl)
340-
return false;
341-
342-
if (!isa<AbstractFunctionDecl>(decl))
343-
return false;
344-
345-
F->setLinkage(SILLinkage::PublicNonABI);
346-
347-
// Adding the attribute is only needed to be able to compile the
348-
// client module with -Onone. For optimized builds, setting the
349-
// SILLinkage is enough, because with optimization, the client module
350-
// eagerly de-serializes all functions and therefore gets the
351-
// linkage right. But with -Onone, the linkage is derived purly from
352-
// the AST.
353-
// TODO: also here, we should find a way to not modify the AST.
354-
auto &ctx = M.getASTContext();
355-
auto *attr = new (ctx) AlwaysEmitIntoClientAttr(/*implicit=*/true);
356-
decl->getAttrs().add(attr);
357-
358-
return true;
359-
}
360-
361324
class CrossModuleSerializationSetupPass: public SILModuleTransform {
362325
void run() override {
363326

test/SILOptimizer/Inputs/cross-module.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ private protocol PrivateProtocol {
9292

9393
open class OpenClass<T> {
9494
public init() { }
95+
96+
@inline(never)
97+
fileprivate func bar(_ t: T) {
98+
print(t)
99+
}
95100
}
96101

97102
extension OpenClass {
@@ -114,6 +119,12 @@ public func checkIfClassConforms_gen<T>(_ t: T) {
114119
print(x.testit())
115120
}
116121

122+
@inline(never)
123+
public func callClassMethod<T>(_ t: T) {
124+
let k = OpenClass<T>()
125+
k.bar(t)
126+
}
127+
117128
extension Int : PrivateProtocol {
118129
func foo() -> Int { return self }
119130
}

test/SILOptimizer/cross-module-optimization.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ func testError() {
6464
print(returnInternalError(27))
6565
}
6666

67-
func testProtocol() {
67+
class DerivedFromOpen<T> : OpenClass<T> { }
68+
69+
func testProtocolsAndClasses() {
6870
// CHECK-OUTPUT: false
6971
// CHECK-SIL-DAG: sil shared [noinline] @$s4Test20checkIfClassConformsyyxlFSi_Tg5
7072
checkIfClassConforms(27)
@@ -80,6 +82,8 @@ func testProtocol() {
8082
// CHECK-OUTPUT: 1234
8183
// CHECK-SIL-DAG: sil shared_external {{.*}} @$s4Test11callFoo_genyyxlF
8284
callFoo_gen(27)
85+
// CHECK-OUTPUT: 55
86+
callClassMethod(55)
8387
}
8488

8589
func testSubModule() {
@@ -111,7 +115,7 @@ func testKeypath() {
111115
testNestedTypes()
112116
testClass()
113117
testError()
114-
testProtocol()
118+
testProtocolsAndClasses()
115119
testSubModule()
116120
testClosures()
117121
testKeypath()

0 commit comments

Comments
 (0)