Skip to content

[embedded] Emit weak_odr instead of linkonce_odr symbols under -Xfrontend -mergeable-symbols #79892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2273,7 +2273,7 @@ getIRLinkage(StringRef name, const UniversalLinkageInfo &info,
auto linkage = llvm::GlobalValue::ExternalLinkage;

if (info.MergeableSymbols)
linkage = llvm::GlobalValue::LinkOnceODRLinkage;
linkage = llvm::GlobalValue::WeakODRLinkage;

return {linkage, PublicDefinitionVisibility,
info.Internalize ? llvm::GlobalValue::DefaultStorageClass
Expand All @@ -2291,7 +2291,7 @@ getIRLinkage(StringRef name, const UniversalLinkageInfo &info,

case SILLinkage::Hidden:
if (info.MergeableSymbols)
return RESULT(LinkOnceODR, Hidden, Default);
return RESULT(WeakODR, Hidden, Default);

return RESULT(External, Hidden, Default);

Expand Down
14 changes: 14 additions & 0 deletions test/embedded/linkage-custom-entry-point.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %target-swift-frontend %s -module-name Application -parse-as-library -entry-point-function-name Application_main -mergeable-symbols -enable-experimental-feature Embedded -emit-ir | %FileCheck %s
// RUN: %target-swift-frontend %s -module-name Application -parse-as-library -entry-point-function-name Application_main -mergeable-symbols -O -enable-experimental-feature Embedded -emit-ir | %FileCheck %s

// REQUIRES: swift_in_compiler
// REQUIRES: swift_feature_Embedded

@main
struct Main {
static func main() {
print("hello")
}
}

// CHECK: @Application_main
26 changes: 26 additions & 0 deletions test/embedded/linkage-mergeable-dead-strip.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: %empty-directory(%t)

// RUN: %target-swift-frontend -enable-experimental-feature Embedded -enable-experimental-feature SymbolLinkageMarkers -module-name main -mergeable-symbols -O %s -emit-ir | %FileCheck %s --check-prefix=CHECK-IR
// RUN: %target-swift-frontend -enable-experimental-feature Embedded -enable-experimental-feature SymbolLinkageMarkers -module-name main -mergeable-symbols -O %s -c -o %t/a.o
// RUN: %target-clang %t/a.o -o %t/a.out -dead_strip
// RUN: %llvm-nm --defined-only --format=just-symbols --demangle %t/a.out | sort | %FileCheck %s --check-prefix=CHECK-NM
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: swift_in_compiler
// REQUIRES: executable_test
// REQUIRES: swift_feature_Embedded
// REQUIRES: swift_feature_SymbolLinkageMarkers

public func a_this_is_unused() { }

@_used
public func b_this_is_unused_but_explicitly_retained() { }

// CHECK-IR: define weak_odr {{.*}}@"$e4main16a_this_is_unusedyyF"()
// CHECK-IR: define weak_odr {{.*}}@"$e4main40b_this_is_unused_but_explicitly_retainedyyF"()

// CHECK-NM-NOT: $e4main14this_is_unusedyyF
// CHECK-NM: $e4main40b_this_is_unused_but_explicitly_retainedyyF

print("Hello")
// CHECK: Hello
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

// REQUIRES: swift_in_compiler
// REQUIRES: executable_test
// REQUIRES: OS=macosx || OS=linux-gnu
// REQUIRES: swift_feature_Embedded

// BEGIN MyModule.swift
Expand Down
35 changes: 35 additions & 0 deletions test/embedded/linkage-mergeable2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// RUN: %empty-directory(%t)
// RUN: %{python} %utils/split_file.py -o %t %s

// RUN: %target-swift-frontend -mergeable-symbols -O -c -emit-module -o %t/MyModule.o %t/MyModule.swift -enable-experimental-feature Embedded -parse-as-library
// RUN: %target-swift-frontend -mergeable-symbols -O -c -o %t/a.o %t/Main.swift -I %t -enable-experimental-feature Embedded
// RUN: %target-clang %t/a.o %t/MyModule.o -o %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: swift_in_compiler
// REQUIRES: executable_test
// REQUIRES: swift_feature_Embedded

// BEGIN MyModule.swift

public func module_func() {
print("module_func")
}

public func module_generic_func<T: CustomStringConvertible>(t: T) {
print("module_generic_func: \(t)")
}

// BEGIN Main.swift

import MyModule

func test() {
module_func()
module_generic_func(t: 42)
}

test()

// CHECK: module_func
// CHECK: module_generic_func: 42
12 changes: 12 additions & 0 deletions test/embedded/linkage-public-exports.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %target-swift-frontend %s -mergeable-symbols -enable-experimental-feature Embedded -emit-ir | %FileCheck %s
// RUN: %target-swift-frontend %s -mergeable-symbols -O -enable-experimental-feature Embedded -emit-ir | %FileCheck %s

// REQUIRES: swift_in_compiler
// REQUIRES: swift_feature_Embedded

public func foo1() { }

func foo2() { }

// CHECK: foo1
// CHECK-NOT: foo2