Skip to content

Commit 34dc983

Browse files
committed
[embedded] Relax assert in SILLinkerVisitor::maybeAddFunctionToWorklist to allow external forward-declarations even in regular Swift, add tests
1 parent 9fcdf7e commit 34dc983

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

include/swift/SIL/SILFunction.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,6 +1354,20 @@ class SILFunction
13541354
WasmImportModuleAndField = std::make_pair(module, field);
13551355
}
13561356

1357+
bool isExternForwardDeclaration() const {
1358+
if (isExternalDeclaration()) {
1359+
if (auto declContext = getDeclContext()) {
1360+
if (auto decl = declContext->getAsDecl()) {
1361+
if (decl->getAttrs().hasAttribute<ExternAttr>())
1362+
return true;
1363+
if (decl->getAttrs().hasAttribute<SILGenNameAttr>())
1364+
return true;
1365+
}
1366+
}
1367+
}
1368+
return false;
1369+
}
1370+
13571371
/// Returns true if this function belongs to a declaration that returns
13581372
/// an opaque result type with one or more availability conditions that are
13591373
/// allowed to produce a different underlying type at runtime.

lib/SIL/IR/Linker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ void SILLinkerVisitor::maybeAddFunctionToWorklist(SILFunction *F,
102102
bool setToSerializable) {
103103
SILLinkage linkage = F->getLinkage();
104104
assert((!setToSerializable || F->hasValidLinkageForFragileRef() ||
105-
hasSharedVisibility(linkage) || Mod.getOptions().EmbeddedSwift) &&
105+
hasSharedVisibility(linkage) || F->isExternForwardDeclaration()) &&
106106
"called function has wrong linkage for serialized function");
107107

108108
if (!F->isExternalDeclaration()) {

test/SIL/modules-extern.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %{python} %utils/split_file.py -o %t %s
3+
4+
// RUN: %target-swift-frontend -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -parse-as-library
5+
// RUN: %target-swift-frontend -c -I %t %t/Main.swift -o %t/a.o
6+
7+
// BEGIN MyModule.swift
8+
9+
@_extern(c)
10+
@_alwaysEmitIntoClient
11+
func some_c_api()
12+
13+
@_transparent
14+
public func publicFuncInAModule() {
15+
some_c_api()
16+
}
17+
18+
// BEGIN Main.swift
19+
20+
import MyModule
21+
22+
@_extern(c)
23+
func some_c_api()
24+
25+
some_c_api()
26+
publicFuncInAModule()

test/SIL/modules-extern2.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %{python} %utils/split_file.py -o %t %s
3+
4+
// RUN: %target-swift-frontend -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -parse-as-library
5+
// RUN: %target-swift-frontend -c -I %t %t/Main.swift -o %t/a.o
6+
7+
// BEGIN MyModule.swift
8+
9+
@_silgen_name("some_forward_declared_api")
10+
@_alwaysEmitIntoClient
11+
func some_forward_declared_api()
12+
13+
@_transparent
14+
public func publicFuncInAModule() {
15+
some_forward_declared_api()
16+
}
17+
18+
// BEGIN Main.swift
19+
20+
import MyModule
21+
22+
@_silgen_name("some_forward_declared_api")
23+
func some_forward_declared_api()
24+
25+
some_forward_declared_api()
26+
publicFuncInAModule()

test/embedded/modules-extern.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %{python} %utils/split_file.py -o %t %s
3+
4+
// RUN: %target-swift-frontend -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -enable-experimental-feature Embedded -parse-as-library
5+
// RUN: %target-swift-frontend -c -I %t %t/Main.swift -enable-experimental-feature Embedded -o %t/a.o
6+
7+
// REQUIRES: swift_in_compiler
8+
// REQUIRES: OS=macosx || OS=linux-gnu
9+
10+
// BEGIN MyModule.swift
11+
12+
@_extern(c)
13+
func some_c_api()
14+
15+
@_transparent
16+
public func publicFuncInAModule() {
17+
internalFuncInAModule()
18+
}
19+
20+
@usableFromInline
21+
internal func internalFuncInAModule() {
22+
some_c_api()
23+
}
24+
25+
// BEGIN Main.swift
26+
27+
import MyModule
28+
29+
@_extern(c)
30+
func some_c_api()
31+
32+
some_c_api()
33+
publicFuncInAModule()

0 commit comments

Comments
 (0)