Skip to content

Commit b806c21

Browse files
authored
Merge pull request #41337 from eeckstein/cmo-fixes
two small fixes for cross-module-optimization
2 parents a7c698c + 80a22e3 commit b806c21

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

lib/SILOptimizer/IPO/CrossModuleOptimization.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,12 @@ bool CrossModuleOptimization::canSerializeInstruction(SILInstruction *inst,
248248
if (!callee)
249249
return false;
250250

251+
// In conservative mode we don't want to turn non-public functions into
252+
// public functions, because that can increase code size. E.g. if the
253+
// function is completely inlined afterwards.
254+
if (conservative && !hasPublicVisibility(callee->getLinkage()))
255+
return false;
256+
251257
// Recursivly walk down the call graph.
252258
if (canSerializeFunction(callee, canSerializeFlags, maxDepth - 1))
253259
return true;
@@ -260,12 +266,12 @@ bool CrossModuleOptimization::canSerializeInstruction(SILInstruction *inst,
260266
if (!canUseFromInline(callee))
261267
return false;
262268

263-
// In conservative mode we don't want to turn non-public functions into
264-
// public functions, because that can increase code size. E.g. if the
265-
// function is completely inlined afterwards.
266-
if (conservative && callee->getLinkage() != SILLinkage::Public)
269+
return true;
270+
}
271+
if (auto *GAI = dyn_cast<GlobalAddrInst>(inst)) {
272+
SILGlobalVariable *global = GAI->getReferencedGlobal();
273+
if (conservative && !hasPublicVisibility(global->getLinkage()))
267274
return false;
268-
269275
return true;
270276
}
271277
if (auto *KPI = dyn_cast<KeyPathInst>(inst)) {
@@ -457,7 +463,7 @@ void CrossModuleOptimization::serializeInstruction(SILInstruction *inst,
457463
if (canSerializeGlobal(global)) {
458464
serializeGlobal(global);
459465
}
460-
if (global->getLinkage() != SILLinkage::Public) {
466+
if (!hasPublicVisibility(global->getLinkage())) {
461467
global->setLinkage(SILLinkage::Public);
462468
M.addPublicCMOSymbol(global->getName());
463469
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,3 +290,7 @@ public struct StructWithClosure {
290290
public static let c = { (x: Int) -> Int in return x }
291291
}
292292

293+
public func getEmptySet() -> Set<Int> {
294+
return Set()
295+
}
296+

test/SILOptimizer/cross-module-optimization.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import Test
2929

30+
// CHECK-SIL: sil_global public_external [serialized] @_swiftEmptySetSingleton : $_SwiftEmptySetSingleton
3031

3132
func testNestedTypes() {
3233
let c = Container()
@@ -132,6 +133,9 @@ func testMisc() {
132133

133134
// CHECK-OUTPUT: 27
134135
print(classWithPublicProperty(33))
136+
137+
// CHECK-OUTPUT: []
138+
print(getEmptySet())
135139
}
136140

137141
// CHECK-SIL2-LABEL: sil hidden [noinline] @$s4Main10testGlobalyyF

0 commit comments

Comments
 (0)