Skip to content

Commit eb1c925

Browse files
authored
Merge pull request swiftlang#80416 from kubamracek/embedded-mergeable-global-refs
[embedded] Fix compilation crash with -num-threads and -mergeable-symbols
2 parents 50f5221 + c9ea28c commit eb1c925

File tree

5 files changed

+104
-2
lines changed

5 files changed

+104
-2
lines changed

lib/IRGen/IRGen.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,7 +1666,8 @@ static void performParallelIRGeneration(IRGenDescriptor desc) {
16661666
llvm::Module *M = IGM->getModule();
16671667
auto collectReference = [&](llvm::GlobalValue &G) {
16681668
if (G.isDeclaration()
1669-
&& (G.getLinkage() == GlobalValue::LinkOnceODRLinkage ||
1669+
&& (G.getLinkage() == GlobalValue::WeakODRLinkage ||
1670+
G.getLinkage() == GlobalValue::LinkOnceODRLinkage ||
16701671
G.getLinkage() == GlobalValue::ExternalLinkage)) {
16711672
referencedGlobals.insert(G.getName());
16721673
G.setLinkage(GlobalValue::ExternalLinkage);
@@ -1693,7 +1694,8 @@ static void performParallelIRGeneration(IRGenDescriptor desc) {
16931694
// definition (if it's not referenced in the same file).
16941695
auto updateLinkage = [&](llvm::GlobalValue &G) {
16951696
if (!G.isDeclaration()
1696-
&& G.getLinkage() == GlobalValue::LinkOnceODRLinkage
1697+
&& (G.getLinkage() == GlobalValue::WeakODRLinkage ||
1698+
G.getLinkage() == GlobalValue::LinkOnceODRLinkage)
16971699
&& referencedGlobals.count(G.getName()) != 0) {
16981700
G.setLinkage(GlobalValue::WeakODRLinkage);
16991701
}

test/embedded/Inputs/linux-rng-support.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@
55

66
#ifdef __linux__
77

8+
#ifdef __cplusplus
9+
extern "C"
10+
#endif
811
ssize_t getrandom(void *buf, size_t len, unsigned int flags);
912

13+
#ifdef __cplusplus
14+
extern "C"
15+
#endif
16+
void arc4random_buf(void *buf, size_t nbytes);
17+
1018
void arc4random_buf(void *buf, size_t nbytes) {
1119
while (nbytes > 0) {
1220
ssize_t actual_nbytes = 0;
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 -mergeable-symbols -num-threads 2 -O -c -emit-module -o %t/MyModule.o %t/MyModule.swift -enable-experimental-feature Embedded -parse-as-library
5+
// RUN: %target-swift-frontend -mergeable-symbols -num-threads 2 -O -c -o %t/MainA.o -o %t/MainB.o %t/MainA.swift %t/MainB.swift -I %t -enable-experimental-feature Embedded -parse-as-library
6+
// RUN: %target-clang %t/MainA.o %t/MainB.o %t/MyModule.o -o %t/a.out
7+
// RUN: %target-run %t/a.out | %FileCheck %s
8+
9+
// REQUIRES: swift_in_compiler
10+
// REQUIRES: executable_test
11+
// REQUIRES: swift_feature_Embedded
12+
13+
// BEGIN MyModule.swift
14+
15+
public func module_func(n: Int) {
16+
var a: [Int] = [1, 2, 3]
17+
print("module_func: \(a[0])")
18+
}
19+
20+
// BEGIN MainA.swift
21+
22+
import MyModule
23+
24+
// BEGIN MainB.swift
25+
26+
import MyModule
27+
28+
@main
29+
struct Main {
30+
static func main() {
31+
module_func(n: 5)
32+
}
33+
}
34+
35+
// CHECK: module_func: 1
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %{python} %utils/split_file.py -o %t %s
3+
4+
// RUN: %target-swift-frontend -mergeable-symbols -num-threads 2 -O -c -emit-module -o %t/MyModule.o %t/MyModule.swift -enable-experimental-feature Embedded -parse-as-library
5+
// RUN: %target-swift-frontend -mergeable-symbols -num-threads 2 -O -c -o %t/MainA.o -o %t/MainB.o %t/MainA.swift %t/MainB.swift -I %t -enable-experimental-feature Embedded -parse-as-library
6+
// RUN: %target-clang %linux_rng_support_c_if_needed %t/MainA.o %t/MainB.o %t/MyModule.o -o %t/a.out
7+
// RUN: %target-run %t/a.out | %FileCheck %s
8+
9+
// REQUIRES: swift_in_compiler
10+
// REQUIRES: executable_test
11+
// REQUIRES: swift_feature_Embedded
12+
13+
// BEGIN MyModule.swift
14+
15+
var dict: [Int:Int] = [1: 2, 3: 4]
16+
17+
public func module_func() {
18+
print("module_func: \(dict[1]!)")
19+
}
20+
21+
// BEGIN MainA.swift
22+
23+
import MyModule
24+
25+
fileprivate var dict: [Int:Int] = [5: 6, 7: 8]
26+
27+
func mainA_func() {
28+
print("main_func: \(dict[5]!)")
29+
}
30+
31+
// BEGIN MainB.swift
32+
33+
import MyModule
34+
35+
fileprivate var dict: [Int:Int] = [5: 6, 7: 8]
36+
37+
func mainB_func() {
38+
print("main_func: \(dict[5]!)")
39+
}
40+
41+
@main
42+
struct Main {
43+
static func main() {
44+
module_func()
45+
mainA_func()
46+
mainB_func()
47+
}
48+
}
49+
50+
// CHECK: module_func: 2
51+
// CHECK: main_func: 6

test/embedded/lit.local.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,9 @@ if config.available_features.intersection(set(supported_test_os_list)) == set():
2828
config.environment = dict(config.environment)
2929
if 'SWIFT_USE_OLD_DRIVER' in config.environment: del config.environment['SWIFT_USE_OLD_DRIVER']
3030
if 'SWIFT_AVOID_WARNING_USING_OLD_DRIVER' in config.environment: del config.environment['SWIFT_AVOID_WARNING_USING_OLD_DRIVER']
31+
32+
# (5) Provide some useful substitutions to simplify writing tests that work across platforms (macOS, Linux, etc.)
33+
if "OS=linux-gnu" in config.available_features:
34+
config.substitutions.append(("%linux_rng_support_c_if_needed", "%S/Inputs/linux-rng-support.c"))
35+
else:
36+
config.substitutions.append(("%linux_rng_support_c_if_needed", ""))

0 commit comments

Comments
 (0)