Skip to content

Commit 4709bdc

Browse files
committed
[embedded] Deserialize and import global variables in embedded Swift mode
1 parent f88c919 commit 4709bdc

File tree

5 files changed

+151
-0
lines changed

5 files changed

+151
-0
lines changed

lib/SIL/IR/Linker.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,15 @@ void SILLinkerVisitor::visitMetatypeInst(MetatypeInst *MI) {
418418
linkInVTable(C);
419419
}
420420

421+
void SILLinkerVisitor::visitGlobalAddrInst(GlobalAddrInst *GAI) {
422+
if (!Mod.getASTContext().LangOpts.hasFeature(Feature::Embedded))
423+
return;
424+
425+
SILGlobalVariable *G = GAI->getReferencedGlobal();
426+
G->setDeclaration(false);
427+
G->setLinkage(stripExternalFromLinkage(G->getLinkage()));
428+
}
429+
421430
//===----------------------------------------------------------------------===//
422431
// Top Level Routine
423432
//===----------------------------------------------------------------------===//

lib/SIL/IR/Linker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ class SILLinkerVisitor : public SILInstructionVisitor<SILLinkerVisitor, void> {
127127
void visitAllocRefInst(AllocRefInst *ARI);
128128
void visitAllocRefDynamicInst(AllocRefDynamicInst *ARI);
129129
void visitMetatypeInst(MetatypeInst *MI);
130+
void visitGlobalAddrInst(GlobalAddrInst *i);
130131

131132
private:
132133
/// Cause a function to be deserialized, and visit all other functions
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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+
// RUN: %target-clang -x c -c %S/Inputs/tiny-runtime-dummy-refcounting.c -o %t/runtime.o
7+
// RUN: %target-clang -x c -c %S/Inputs/print.c -o %t/print.o
8+
// RUN: %target-clang %t/a.o %t/print.o %t/runtime.o -o %t/a.out
9+
// RUN: %target-run %t/a.out | %FileCheck %s
10+
11+
// REQUIRES: executable_test
12+
// REQUIRES: VENDOR=apple
13+
// REQUIRES: OS=macosx
14+
15+
// BEGIN MyModule.swift
16+
17+
public var global_in_module_used_in_module = 0
18+
public var global_in_module_unused_in_module = 0
19+
20+
public func foo() {
21+
global_in_module_used_in_module += 1
22+
}
23+
24+
// BEGIN Main.swift
25+
26+
import MyModule
27+
28+
@_silgen_name("putchar")
29+
func putchar(_: UInt8)
30+
31+
public func print(_ s: StaticString, terminator: StaticString = "\n") {
32+
var p = s.utf8Start
33+
while p.pointee != 0 {
34+
putchar(p.pointee)
35+
p += 1
36+
}
37+
p = terminator.utf8Start
38+
while p.pointee != 0 {
39+
putchar(p.pointee)
40+
p += 1
41+
}
42+
}
43+
44+
@_silgen_name("print_long")
45+
func print_long(_: Int)
46+
47+
public func print(_ n: Int, terminator: StaticString = "\n") {
48+
print_long(n)
49+
print("", terminator: terminator)
50+
}
51+
52+
func test() {
53+
print("Testing globals...") // CHECK: Testing globals...
54+
print(global_in_module_used_in_module) // CHECK-NEXT: 0
55+
print(global_in_module_unused_in_module) // CHECK-NEXT: 0
56+
foo()
57+
print(global_in_module_used_in_module) // CHECK-NEXT: 1
58+
print(global_in_module_unused_in_module) // CHECK-NEXT: 0
59+
}
60+
61+
test()
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %{python} %utils/split_file.py -o %t %s
3+
4+
// RUN: %target-swift-frontend -emit-module -I %t -o %t/MyModuleA.swiftmodule %t/MyModuleA.swift -enable-experimental-feature Embedded -parse-as-library
5+
// RUN: %target-swift-frontend -emit-module -I %t -o %t/MyModuleB.swiftmodule %t/MyModuleB.swift -enable-experimental-feature Embedded -parse-as-library
6+
// RUN: %target-swift-frontend -emit-module -I %t -o %t/MyModuleC.swiftmodule %t/MyModuleC.swift -enable-experimental-feature Embedded -parse-as-library
7+
// RUN: %target-swift-frontend -emit-ir -I %t %t/Main.swift -enable-experimental-feature Embedded -parse-as-library | %FileCheck %s
8+
9+
// REQUIRES: swift_in_compiler
10+
11+
// Dependencies look like this:
12+
//
13+
// ┌─── ModuleB ◀─┐
14+
// ModuleA ◀──┤ ├─── Main
15+
// └─── ModuleC ◀─┘
16+
17+
// BEGIN MyModuleA.swift
18+
19+
public var global = 0
20+
21+
public func foo() { global += 1 }
22+
23+
// BEGIN MyModuleB.swift
24+
25+
import MyModuleA
26+
27+
public func foo() { global += 1 }
28+
29+
// BEGIN MyModuleC.swift
30+
31+
import MyModuleA
32+
33+
public func foo() { global += 1 }
34+
35+
// BEGIN Main.swift
36+
37+
import MyModuleB
38+
import MyModuleC
39+
40+
public func main() {
41+
MyModuleB.foo()
42+
MyModuleC.foo()
43+
}
44+
45+
// CHECK: @"$s9MyModuleA6globalSivp" = global %TSi zeroinitializer

test/embedded/modules-globals.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 -emit-ir -I %t %t/Main.swift -enable-experimental-feature Embedded -parse-as-library | %FileCheck %s
6+
7+
// REQUIRES: swift_in_compiler
8+
9+
// BEGIN MyModule.swift
10+
11+
public var global_in_module_used_in_module = 0
12+
public var global_in_module_unused_in_module = 0
13+
14+
public func foo() {
15+
global_in_module_used_in_module += 1
16+
}
17+
18+
// BEGIN Main.swift
19+
20+
import MyModule
21+
22+
public var global_in_client_used_in_client = 0
23+
public var global_in_client_unused_in_client = 0
24+
25+
public func main() {
26+
global_in_module_used_in_module = 42
27+
global_in_module_unused_in_module = 42
28+
global_in_client_used_in_client = 42
29+
foo()
30+
}
31+
32+
// CHECK: @"$s4Main022global_in_client_used_c1_D0Sivp" = global %TSi zeroinitializer
33+
// CHECK: @"$s4Main024global_in_client_unused_c1_D0Sivp" = global %TSi zeroinitializer
34+
// CHECK: @"$s8MyModule022global_in_module_used_d1_E0Sivp" = global %TSi zeroinitializer
35+
// CHECK: @"$s8MyModule024global_in_module_unused_d1_E0Sivp" = global %TSi zeroinitializer

0 commit comments

Comments
 (0)