Skip to content

Commit 5d59445

Browse files
authored
Merge pull request #68662 from kubamracek/embedded-globals-modules
[embedded] Deserialize and import global variables in embedded Swift mode
2 parents a7d006e + f5e9e07 commit 5d59445

File tree

5 files changed

+155
-0
lines changed

5 files changed

+155
-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: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
// REQUIRES: VENDOR=apple
11+
// REQUIRES: OS=macosx
12+
13+
// Dependencies look like this:
14+
//
15+
// ┌─── ModuleB ◀─┐
16+
// ModuleA ◀──┤ ├─── Main
17+
// └─── ModuleC ◀─┘
18+
19+
// BEGIN MyModuleA.swift
20+
21+
public var global = 0
22+
23+
public func foo() { global += 1 }
24+
25+
// BEGIN MyModuleB.swift
26+
27+
import MyModuleA
28+
29+
public func foo() { global += 1 }
30+
31+
// BEGIN MyModuleC.swift
32+
33+
import MyModuleA
34+
35+
public func foo() { global += 1 }
36+
37+
// BEGIN Main.swift
38+
39+
import MyModuleB
40+
import MyModuleC
41+
42+
public func main() {
43+
MyModuleB.foo()
44+
MyModuleC.foo()
45+
}
46+
47+
// CHECK: @"$s9MyModuleA6globalSivp" = global %TSi zeroinitializer

test/embedded/modules-globals.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
// REQUIRES: VENDOR=apple
9+
// REQUIRES: OS=macosx
10+
11+
// BEGIN MyModule.swift
12+
13+
public var global_in_module_used_in_module = 0
14+
public var global_in_module_unused_in_module = 0
15+
16+
public func foo() {
17+
global_in_module_used_in_module += 1
18+
}
19+
20+
// BEGIN Main.swift
21+
22+
import MyModule
23+
24+
public var global_in_client_used_in_client = 0
25+
public var global_in_client_unused_in_client = 0
26+
27+
public func main() {
28+
global_in_module_used_in_module = 42
29+
global_in_module_unused_in_module = 42
30+
global_in_client_used_in_client = 42
31+
foo()
32+
}
33+
34+
// CHECK: @"$s4Main022global_in_client_used_c1_D0Sivp" = global %TSi zeroinitializer
35+
// CHECK: @"$s4Main024global_in_client_unused_c1_D0Sivp" = global %TSi zeroinitializer
36+
// CHECK: @"$s8MyModule022global_in_module_used_d1_E0Sivp" = global %TSi zeroinitializer
37+
// CHECK: @"$s8MyModule024global_in_module_unused_d1_E0Sivp" = global %TSi zeroinitializer

0 commit comments

Comments
 (0)