Skip to content

Commit 74ffef4

Browse files
authored
Merge pull request #61527 from eeckstein/fix-cmo
CrossModuleOptimization: don't serialize functions referencing imported C functions/variables
2 parents 76b893d + 75bc7f6 commit 74ffef4

File tree

5 files changed

+36
-1
lines changed

5 files changed

+36
-1
lines changed

lib/SILOptimizer/IPO/CrossModuleOptimization.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ bool CrossModuleOptimization::canSerializeInstruction(SILInstruction *inst,
264264
return false;
265265
}
266266

267+
// In some project configurations imported C functions are not necessarily
268+
// public in their modules.
269+
if (conservative && callee->hasClangNode())
270+
return false;
271+
267272
// Recursively walk down the call graph.
268273
if (canSerializeFunction(callee, canSerializeFlags, maxDepth - 1))
269274
return true;
@@ -284,6 +289,12 @@ bool CrossModuleOptimization::canSerializeInstruction(SILInstruction *inst,
284289
!hasPublicVisibility(global->getLinkage())) {
285290
return false;
286291
}
292+
293+
// In some project configurations imported C variables are not necessarily
294+
// public in their modules.
295+
if (conservative && global->hasClangNode())
296+
return false;
297+
287298
return true;
288299
}
289300
if (auto *KPI = dyn_cast<KeyPathInst>(inst)) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ long privateCFunc() {
55
return 123;
66
}
77

8+
long privateCVar = 27;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11

22
long privateCFunc();
33

4+
long privateCVar;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
import Submodule
3+
import PrivateCModule
34

45
public func incrementByThree(_ x: Int) -> Int {
56
return incrementByOne(x) + 2
@@ -32,3 +33,10 @@ public struct ModuleStruct {
3233
public static var privateFunctionPointer: (Int) -> (Int) = { $0 }
3334
}
3435

36+
public func callPrivateCFunc() -> Int {
37+
return Int(privateCFunc())
38+
}
39+
40+
public func usePrivateCVar() -> Int {
41+
return Int(privateCVar);
42+
}

test/SILOptimizer/default-cmo.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// RUN: %target-build-swift -O -wmo -Xfrontend -enable-default-cmo -parse-as-library -emit-module -emit-module-path=%t/Module.swiftmodule -module-name=Module -I%t -I%S/Inputs/cross-module %S/Inputs/cross-module/default-module.swift -c -o %t/module.o
66
// RUN: %target-build-swift -O -wmo -Xfrontend -enable-default-cmo -parse-as-library -emit-tbd -emit-tbd-path %t/ModuleTBD.tbd -emit-module -emit-module-path=%t/ModuleTBD.swiftmodule -module-name=ModuleTBD -I%t -I%S/Inputs/cross-module %S/Inputs/cross-module/default-module.swift -c -o %t/moduletbd.o
77

8-
// RUN: %target-build-swift -O -wmo -module-name=Main -I%t %s -emit-sil | %FileCheck %s
8+
// RUN: %target-build-swift -O -wmo -module-name=Main -I%t -I%S/Inputs/cross-module %s -emit-sil | %FileCheck %s
99

1010

1111
import Module
@@ -24,6 +24,20 @@ public func callPrivateFunctionPointer(_ x: Int) -> Int {
2424
return Module.ModuleStruct.privateFunctionPointer(x)
2525
}
2626

27+
// CHECK-LABEL: sil @$s4Main24callPrivateCFuncInModuleSiyF : $@convention(thin) () -> Int {
28+
// CHECK: function_ref @$s6Module16callPrivateCFuncSiyF
29+
// CHECK: } // end sil function '$s4Main24callPrivateCFuncInModuleSiyF'
30+
public func callPrivateCFuncInModule() -> Int {
31+
return Module.callPrivateCFunc()
32+
}
33+
34+
// CHECK-LABEL: sil @$s4Main22usePrivateCVarInModuleSiyF : $@convention(thin) () -> Int {
35+
// CHECK: function_ref @$s6Module14usePrivateCVarSiyF
36+
// CHECK: } // end sil function '$s4Main22usePrivateCVarInModuleSiyF'
37+
public func usePrivateCVarInModule() -> Int {
38+
return Module.usePrivateCVar()
39+
}
40+
2741
// CHECK-LABEL: sil @$s4Main11doIncrementyS2iF
2842
// CHECK-NOT: function_ref
2943
// CHECK-NOT: apply

0 commit comments

Comments
 (0)