Skip to content

Commit 8c52853

Browse files
committed
cross-module-optimization: fix a problem with global variables
don't make public external globals non-external
1 parent a7c698c commit 8c52853

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

lib/SILOptimizer/IPO/CrossModuleOptimization.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,12 @@ bool CrossModuleOptimization::canSerializeInstruction(SILInstruction *inst,
268268

269269
return true;
270270
}
271+
if (auto *GAI = dyn_cast<GlobalAddrInst>(inst)) {
272+
SILGlobalVariable *global = GAI->getReferencedGlobal();
273+
if (conservative && !hasPublicVisibility(global->getLinkage()))
274+
return false;
275+
return true;
276+
}
271277
if (auto *KPI = dyn_cast<KeyPathInst>(inst)) {
272278
bool canUse = true;
273279
KPI->getPattern()->visitReferencedFunctionsAndMethods(
@@ -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)