Skip to content

Commit 1cb0099

Browse files
committed
IRGen,Serialization: account for transitive dynamic linkage
When building complex projects, there may cases of static libraries which expose `@inlinable` functions which reference functions from dynamically linked dependencies. In such a case, we need to consider the provenance of the `function_ref` when determining the DLL storage for linkage. We would previously use the deserialised metadata on the `SILFunction` as there are entities where the `DeclContext` may not be deserialised. However, this leaves us in a state where we are unable to determine the actual provenance correctly in some cases. By simply accessing the parent module directly from the `SILFunction` we ensure that we properly identify the origin of the function allowing us to query the DLL storage property. This further allows us to remove the extra storage required for whether the `SILFunction` is statically linked.
1 parent 899a389 commit 1cb0099

File tree

5 files changed

+25
-17
lines changed

5 files changed

+25
-17
lines changed

include/swift/SIL/SILFunction.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -424,9 +424,6 @@ class SILFunction
424424
/// The function's effects attribute.
425425
unsigned EffectsKindAttr : NumEffectsKindBits;
426426

427-
/// The function is in a statically linked module.
428-
unsigned IsStaticallyLinked : 1;
429-
430427
/// If true, the function has lexical lifetimes even if the module does not.
431428
unsigned ForceEnableLexicalLifetimes : 1;
432429

@@ -698,12 +695,6 @@ class SILFunction
698695
WasDeserializedCanonical = val;
699696
}
700697

701-
bool isStaticallyLinked() const { return IsStaticallyLinked; }
702-
703-
void setIsStaticallyLinked(bool value) {
704-
IsStaticallyLinked = value;
705-
}
706-
707698
ForceEnableLexicalLifetimes_t forceEnableLexicalLifetimes() const {
708699
return ForceEnableLexicalLifetimes_t(ForceEnableLexicalLifetimes);
709700
}

lib/IRGen/GenDecl.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2404,7 +2404,8 @@ LinkInfo LinkInfo::get(const UniversalLinkageInfo &linkInfo,
24042404
// an associated DeclContext and are serialized into the current module. As
24052405
// a result, we explicitly handle SIL Functions here. We do not expect other
24062406
// types to be referenced directly.
2407-
isKnownLocal = entity.getSILFunction()->isStaticallyLinked();
2407+
if (const auto *MD = entity.getSILFunction()->getParentModule())
2408+
isKnownLocal = MD == swiftModule || MD->isStaticLibrary();
24082409
}
24092410

24102411
bool weakImported = entity.isWeakImported(swiftModule);

lib/SIL/IR/SILFunction.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ void SILFunction::init(
214214
this->Zombie = false;
215215
this->HasOwnership = true,
216216
this->WasDeserializedCanonical = false;
217-
this->IsStaticallyLinked = false;
218217
this->IsWithoutActuallyEscapingThunk = false;
219218
this->OptMode = unsigned(OptimizationMode::NotSet);
220219
this->perfConstraints = PerformanceConstraints::None;
@@ -290,7 +289,6 @@ void SILFunction::createSnapshot(int id) {
290289
newSnapshot->HasOwnership = HasOwnership;
291290
newSnapshot->IsWithoutActuallyEscapingThunk = IsWithoutActuallyEscapingThunk;
292291
newSnapshot->OptMode = OptMode;
293-
newSnapshot->IsStaticallyLinked = IsStaticallyLinked;
294292
newSnapshot->copyEffects(this);
295293

296294
SILFunctionCloner cloner(newSnapshot);

lib/Serialization/DeserializeSIL.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -639,10 +639,6 @@ SILDeserializer::readSILFunctionChecked(DeclID FID, SILFunction *existingFn,
639639
if (getFile()->getParentModule() == SILMod.getSwiftModule())
640640
fn->setLinkage(linkage);
641641

642-
if (getFile()->getParentModule()->isStaticLibrary() ||
643-
getFile()->getParentModule() == SILMod.getSwiftModule())
644-
fn->setIsStaticallyLinked(true);
645-
646642
// Don't override the transparency or linkage of a function with
647643
// an existing declaration, except if we deserialized a
648644
// PublicNonABI function, which has HiddenExternal when
@@ -678,7 +674,6 @@ SILDeserializer::readSILFunctionChecked(DeclID FID, SILFunction *existingFn,
678674
fn->setIsAlwaysWeakImported(isWeakImported);
679675
fn->setClassSubclassScope(SubclassScope(subclassScope));
680676
fn->setHasCReferences(bool(hasCReferences));
681-
fn->setIsStaticallyLinked(MF->getAssociatedModule()->isStaticLibrary());
682677

683678
llvm::VersionTuple available;
684679
DECODE_VER_TUPLE(available);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file --leading-lines %s %t
3+
4+
// REQUIRES: OS=windows-msvc
5+
6+
// RUN: %target-swiftc_driver -emit-module -emit-module-path %t/swift/dynamic.swiftmodule -I %t/swift %t/dynamic.swift
7+
// RUN: %target-swiftc_driver -static -emit-module -emit-module-path %t/swift/static.swiftmodule -I %t/swift %t/static.swift
8+
// RUN: %target-swiftc_driver -O -emit-ir -I %t/swift %t/library.swift | %FileCheck %s
9+
10+
// CHECK: declare dllimport swiftcc i{{[0-9]+}} @"$s7dynamic1fSiyF"()
11+
12+
//--- dynamic.swift
13+
public func f() -> Int { 32 }
14+
15+
//--- static.swift
16+
import dynamic
17+
18+
@inlinable
19+
public func g() -> Int { f() + 1 }
20+
21+
//--- library.swift
22+
import `static`
23+
public func h() -> Int { g() + 1 }

0 commit comments

Comments
 (0)