Skip to content

Commit 9f052eb

Browse files
authored
Merge pull request #60239 from eeckstein/fix-cmo
cross-module-optimization: be more conservative when emitting a TBD file, part 2
2 parents 8c52775 + a1da35c commit 9f052eb

File tree

4 files changed

+77
-8
lines changed

4 files changed

+77
-8
lines changed

lib/SILOptimizer/IPO/CrossModuleOptimization.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ class CrossModuleOptimization {
7979

8080
bool canSerializeType(SILType type);
8181

82+
bool canUseFromInline(DeclContext *declCtxt);
83+
8284
bool canUseFromInline(SILFunction *func);
8385

8486
bool shouldSerialize(SILFunction *F);
@@ -184,8 +186,7 @@ bool CrossModuleOptimization::canSerializeFunction(
184186
return iter->second;
185187

186188
if (DeclContext *funcCtxt = function->getDeclContext()) {
187-
ModuleDecl *module = function->getModule().getSwiftModule();
188-
if (!module->canBeUsedForCrossModuleOptimization(funcCtxt))
189+
if (!canUseFromInline(funcCtxt))
189190
return false;
190191
}
191192

@@ -335,7 +336,7 @@ bool CrossModuleOptimization::canSerializeType(SILType type) {
335336

336337
// Exclude types which are defined in an @_implementationOnly imported
337338
// module. Such modules are not transitively available.
338-
if (!M.getSwiftModule()->canBeUsedForCrossModuleOptimization(subNT)) {
339+
if (!canUseFromInline(subNT)) {
339340
return true;
340341
}
341342
}
@@ -364,10 +365,9 @@ static bool couldBeLinkedStatically(DeclContext *funcCtxt, SILModule &module) {
364365
return true;
365366
}
366367

367-
/// Returns true if the function \p func can be used from a serialized function.
368-
bool CrossModuleOptimization::canUseFromInline(SILFunction *function) {
369-
DeclContext *funcCtxt = function->getDeclContext();
370-
if (funcCtxt && !M.getSwiftModule()->canBeUsedForCrossModuleOptimization(funcCtxt))
368+
/// Returns true if the \p declCtxt can be used from a serialized function.
369+
bool CrossModuleOptimization::canUseFromInline(DeclContext *declCtxt) {
370+
if (!M.getSwiftModule()->canBeUsedForCrossModuleOptimization(declCtxt))
371371
return false;
372372

373373
/// If we are emitting a TBD file, the TBD file only contains public symbols
@@ -377,8 +377,18 @@ bool CrossModuleOptimization::canUseFromInline(SILFunction *function) {
377377
/// (potentially) linked statically. Unfortunately there is no way to find out
378378
/// if another module is linked statically or dynamically, so we have to be
379379
/// conservative here.
380-
if (conservative && M.getOptions().emitTBD && couldBeLinkedStatically(funcCtxt, M))
380+
if (conservative && M.getOptions().emitTBD && couldBeLinkedStatically(declCtxt, M))
381381
return false;
382+
383+
return true;
384+
}
385+
386+
/// Returns true if the function \p func can be used from a serialized function.
387+
bool CrossModuleOptimization::canUseFromInline(SILFunction *function) {
388+
if (DeclContext *funcCtxt = function->getDeclContext()) {
389+
if (!canUseFromInline(funcCtxt))
390+
return false;
391+
}
382392

383393
switch (function->getLinkage()) {
384394
case SILLinkage::PublicNonABI:

test/SILOptimizer/Inputs/cross-module/default-module.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,22 @@ public func incrementByThree(_ x: Int) -> Int {
88
public func incrementByThreeWithCall(_ x: Int) -> Int {
99
return incrementByOneNoCMO(x) + 2
1010
}
11+
12+
public func submoduleKlassMember() -> Int {
13+
let k = SubmoduleKlass()
14+
return k.i
15+
}
16+
17+
public final class ModuleKlass {
18+
public var i: Int
19+
20+
public init() {
21+
i = 27
22+
}
23+
}
24+
25+
public func moduleKlassMember() -> Int {
26+
let k = ModuleKlass()
27+
return k.i
28+
}
29+

test/SILOptimizer/Inputs/cross-module/default-submodule.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,11 @@ public func incrementByOneNoCMO(_ x: Int) -> Int {
88
return x + 1
99
}
1010

11+
public final class SubmoduleKlass {
12+
public var i: Int
13+
14+
public init() {
15+
i = 27
16+
}
17+
}
18+

test/SILOptimizer/default-cmo.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,35 @@ public func doIncrementTBDWithCall(_ x: Int) -> Int {
4141
return ModuleTBD.incrementByThreeWithCall(x)
4242
}
4343

44+
// CHECK-LABEL: sil @$s4Main23getSubmoduleKlassMemberSiyF
45+
// CHECK-NOT: function_ref
46+
// CHECK-NOT: apply
47+
// CHECK: } // end sil function '$s4Main23getSubmoduleKlassMemberSiyF'
48+
public func getSubmoduleKlassMember() -> Int {
49+
return Module.submoduleKlassMember()
50+
}
51+
52+
// CHECK-LABEL: sil @$s4Main26getSubmoduleKlassMemberTBDSiyF
53+
// CHECK: [[F:%[0-9]+]] = function_ref @$s9ModuleTBD20submoduleKlassMemberSiyF
54+
// CHECK: [[I:%[0-9]+]] = apply [[F]]
55+
// CHECK: return [[I]]
56+
// CHECK: } // end sil function '$s4Main26getSubmoduleKlassMemberTBDSiyF'
57+
public func getSubmoduleKlassMemberTBD() -> Int {
58+
return ModuleTBD.submoduleKlassMember()
59+
}
60+
61+
// CHECK-LABEL: sil @$s4Main20getModuleKlassMemberSiyF
62+
// CHECK-NOT: function_ref
63+
// CHECK-NOT: apply
64+
// CHECK: } // end sil function '$s4Main20getModuleKlassMemberSiyF'
65+
public func getModuleKlassMember() -> Int {
66+
return Module.moduleKlassMember()
67+
}
68+
69+
// CHECK-LABEL: sil @$s4Main23getModuleKlassMemberTBDSiyF
70+
// CHECK-NOT: function_ref
71+
// CHECK-NOT: apply
72+
// CHECK: } // end sil function '$s4Main23getModuleKlassMemberTBDSiyF'
73+
public func getModuleKlassMemberTBD() -> Int {
74+
return ModuleTBD.moduleKlassMember()
75+
}

0 commit comments

Comments
 (0)