Skip to content

Commit c1e1bf2

Browse files
committed
cross-module-optimization: Don't serialize functions which reference implementationOnly-imported functions
The check for implementationOnly imports was already done for types, but it was missing for functions. Fixes a crash when implementationOnly-importing a C module. https://bugs.swift.org/browse/SR-15048 rdar://81701218
1 parent 990fc4b commit c1e1bf2

File tree

12 files changed

+57
-18
lines changed

12 files changed

+57
-18
lines changed

include/swift/AST/Module.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -676,9 +676,10 @@ class ModuleDecl : public DeclContext, public TypeDecl {
676676
/// This assumes that \p module was imported.
677677
bool isImportedImplementationOnly(const ModuleDecl *module) const;
678678

679-
/// Returns true if a function, which is using \p nominal, can be serialized
680-
/// by cross-module-optimization.
681-
bool canBeUsedForCrossModuleOptimization(NominalTypeDecl *nominal) const;
679+
/// Returns true if decl context or its content can be serialized by
680+
/// cross-module-optimization.
681+
/// The \p ctxt can e.g. be a NominalType or the context of a function.
682+
bool canBeUsedForCrossModuleOptimization(DeclContext *ctxt) const;
682683

683684
/// Finds all top-level decls of this module.
684685
///

lib/AST/Module.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2353,14 +2353,15 @@ bool ModuleDecl::isImportedImplementationOnly(const ModuleDecl *module) const {
23532353
}
23542354

23552355
bool ModuleDecl::
2356-
canBeUsedForCrossModuleOptimization(NominalTypeDecl *nominal) const {
2357-
ModuleDecl *moduleOfNominal = nominal->getParentModule();
2356+
canBeUsedForCrossModuleOptimization(DeclContext *ctxt) const {
2357+
ModuleDecl *moduleOfCtxt = ctxt->getParentModule();
23582358

2359-
// If the nominal is defined in the same module, it's fine.
2360-
if (moduleOfNominal == this)
2359+
// If the context defined in the same module - or is the same module, it's
2360+
// fine.
2361+
if (moduleOfCtxt == this)
23612362
return true;
23622363

2363-
// See if nominal is imported in a "regular" way, i.e. not with
2364+
// See if context is imported in a "regular" way, i.e. not with
23642365
// @_implementationOnly or @_spi.
23652366
ModuleDecl::ImportFilter filter = {
23662367
ModuleDecl::ImportFilterKind::Exported,
@@ -2370,7 +2371,7 @@ canBeUsedForCrossModuleOptimization(NominalTypeDecl *nominal) const {
23702371

23712372
auto &imports = getASTContext().getImportCache();
23722373
for (auto &desc : results) {
2373-
if (imports.isImportedBy(moduleOfNominal, desc.importedModule))
2374+
if (imports.isImportedBy(moduleOfCtxt, desc.importedModule))
23742375
return true;
23752376
}
23762377
return false;

lib/SILOptimizer/IPO/CrossModuleSerializationSetup.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,11 @@ bool CrossModuleSerializationSetup::canUseFromInline(SILFunction *func,
386386
if (!func)
387387
return false;
388388

389+
if (DeclContext *funcCtxt = func->getDeclContext()) {
390+
if (!M.getSwiftModule()->canBeUsedForCrossModuleOptimization(funcCtxt))
391+
return false;
392+
}
393+
389394
switch (func->getLinkage()) {
390395
case SILLinkage::PublicNonABI:
391396
return func->isSerialized() != IsNotSerialized;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
#include "c-module.h"
3+
4+
long privateCFunc() {
5+
return 123;
6+
}
7+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
long privateCFunc();
3+

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Submodule
22
@_implementationOnly import PrivateSubmodule
3+
@_implementationOnly import PrivateCModule
34

45
private enum PE<T> {
56
case A
@@ -268,11 +269,19 @@ public func callUnrelated<T>(_ t: T) -> T {
268269
return t
269270
}
270271

271-
public func callImplementationOnly<T>(_ t: T) -> T {
272+
public func callImplementationOnlyType<T>(_ t: T) -> T {
272273
let p = PrivateStr(i: 27)
273274
print(p.test())
274275
return t
275276
}
276277

278+
public func callImplementationOnlyFunc<T>(_ t: T) -> Int {
279+
return privateFunc()
280+
}
281+
282+
public func callCImplementationOnly<T>(_ t: T) -> Int {
283+
return Int(privateCFunc())
284+
}
285+
277286

278287
public let globalLet = 529387

test/SILOptimizer/Inputs/cross-private-submodule.swift renamed to test/SILOptimizer/Inputs/cross-module/cross-private-submodule.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ public struct PrivateStr {
1212
}
1313
}
1414

15+
public func privateFunc() -> Int {
16+
return 40
17+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module PrivateCModule {
2+
header "c-module.h"
3+
}

0 commit comments

Comments
 (0)