Skip to content

Commit 4952a2c

Browse files
committed
[Embedded] Prefer linkonce_odr to weak_odr for nonunique definitions
This allows the implementation to drop definitions that it does need.
1 parent 69717bd commit 4952a2c

File tree

4 files changed

+22
-10
lines changed

4 files changed

+22
-10
lines changed

include/swift/SIL/SILFunction.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,10 @@ class SILFunction
930930
/// function, such as swift_retain.
931931
bool isSwiftRuntimeFunction() const;
932932

933+
/// Helper method that determines whether a function with the given name and
934+
/// parent module is a Swift runtime function such as swift_retain.
935+
static bool isSwiftRuntimeFunction(StringRef name, const ModuleDecl *module);
936+
933937
/// Helper method which returns true if the linkage of the SILFunction
934938
/// indicates that the object's definition might be required outside the
935939
/// current SILModule.

lib/IRGen/GenDecl.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2329,8 +2329,13 @@ getIRLinkage(StringRef name, const UniversalLinkageInfo &info,
23292329
case SILLinkage::Package: {
23302330
auto linkage = llvm::GlobalValue::ExternalLinkage;
23312331

2332-
if (hasNonUniqueDefinition)
2333-
linkage = llvm::GlobalValue::WeakODRLinkage;
2332+
if (hasNonUniqueDefinition) {
2333+
// Keep Swift runtime functions around so IRGen can reference them.
2334+
if (SILFunction::isSwiftRuntimeFunction(name, nullptr))
2335+
linkage = llvm::GlobalValue::WeakODRLinkage;
2336+
else
2337+
linkage = llvm::GlobalValue::LinkOnceODRLinkage;
2338+
}
23342339

23352340
return {linkage, PublicDefinitionVisibility,
23362341
info.Internalize ? llvm::GlobalValue::DefaultStorageClass
@@ -2348,7 +2353,7 @@ getIRLinkage(StringRef name, const UniversalLinkageInfo &info,
23482353

23492354
case SILLinkage::Hidden:
23502355
if (hasNonUniqueDefinition)
2351-
return RESULT(WeakODR, Hidden, Default);
2356+
return RESULT(LinkOnceODR, Hidden, Default);
23522357

23532358
return RESULT(External, Hidden, Default);
23542359

lib/SIL/IR/SILFunction.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,17 +1038,20 @@ bool SILFunction::hasValidLinkageForFragileRef(SerializedKind_t callerSerialized
10381038
return hasPublicOrPackageVisibility(getLinkage(), /*includePackage*/ true);
10391039
}
10401040

1041-
bool SILFunction::isSwiftRuntimeFunction() const {
1042-
if (!getName().starts_with("swift_") &&
1043-
!getName().starts_with("_swift_"))
1041+
bool SILFunction::isSwiftRuntimeFunction(
1042+
StringRef name, const ModuleDecl *module) {
1043+
if (!name.starts_with("swift_") && !name.starts_with("_swift_"))
10441044
return false;
10451045

1046-
auto module = getParentModule();
10471046
return !module ||
10481047
module->getName().str() == "Swift" ||
10491048
module->getName().str() == "_Concurrency";
10501049
}
10511050

1051+
bool SILFunction::isSwiftRuntimeFunction() const {
1052+
return isSwiftRuntimeFunction(getName(), getParentModule());
1053+
}
1054+
10521055
bool
10531056
SILFunction::isPossiblyUsedExternally() const {
10541057
auto linkage = getLinkage();

test/embedded/linkage/leaf_application.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@
2121

2222
//--- Library.swift
2323

24-
// LIBRARY-IR: @"$e7Library10PointClassCN" = weak_odr {{.*}}global
24+
// LIBRARY-IR: @"$e7Library10PointClassCN" = linkonce_odr {{.*}}global
2525

2626
// Never referenced.
27-
// LIBRARY-IR-NOT: @"$es23_swiftEmptyArrayStorageSi_S3itvp" = weak_odr {{(protected |dllexport )?}}global
27+
// LIBRARY-IR-NOT: @"$es23_swiftEmptyArrayStorageSi_S3itvp" = linkonce_odr {{(protected |dllexport )?}}global
2828

2929
// Note: referenced by swift_allocEmptyBox.
30-
// LIBRARY-IR: @"$es16_emptyBoxStorageSi_Sitvp" = weak_odr {{(protected |dllexport )?}}global
30+
// LIBRARY-IR: @"$es16_emptyBoxStorageSi_Sitvp" = linkonce_odr {{(protected |dllexport )?}}global
3131

3232
// LIBRARY-IR-NOT: define {{.*}}@"$e7Library5helloSaySiGyF"()
3333
public func hello() -> [Int] {

0 commit comments

Comments
 (0)