Skip to content

[embedded] Link in @_used declarations from other modules in SILLinker #80185

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 2 commits into from
Mar 22, 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
582 changes: 291 additions & 291 deletions include/swift/Runtime/RuntimeFunctions.def

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions include/swift/Serialization/SerializedSILLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class SerializedSILLoader {
SILFunction *lookupSILFunction(SILFunction *Callee, bool onlyUpdateLinkage);
SILFunction *lookupSILFunction(StringRef Name,
std::optional<SILLinkage> linkage);
SILGlobalVariable *lookupSILGlobalVariable(StringRef Name);
bool hasSILFunction(StringRef Name,
std::optional<SILLinkage> linkage = std::nullopt);
SILVTable *lookupVTable(const ClassDecl *C);
Expand Down
3 changes: 2 additions & 1 deletion lib/SIL/IR/Linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,9 @@ void SILLinkerVisitor::visitGlobalAddrInst(GlobalAddrInst *GAI) {
if (!Mod.getOptions().EmbeddedSwift)
return;

// In Embedded Swift, we want to actually link globals from other modules too,
// so strip "external" from the linkage.
SILGlobalVariable *G = GAI->getReferencedGlobal();
G->setDeclaration(false);
G->setLinkage(stripExternalFromLinkage(G->getLinkage()));
}

Expand Down
15 changes: 10 additions & 5 deletions lib/SIL/IR/SILModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ class SILModule::SerializationCallback final

void didDeserialize(ModuleDecl *M, SILGlobalVariable *var) override {
updateLinkage(var);

// For globals we currently do not support available_externally.
// In the interpreter it would result in two instances for a single global:
// one in the imported module and one in the main module.
var->setDeclaration(true);

if (!M->getASTContext().LangOpts.hasFeature(Feature::Embedded)) {
// For globals we currently do not support available_externally.
// In the interpreter it would result in two instances for a single
// global: one in the imported module and one in the main module.
//
// We avoid that in Embedded Swift where we do actually link globals from
// other modules into the client module.
var->setDeclaration(true);
}
}

void didDeserialize(ModuleDecl *M, SILVTable *vtable) override {
Expand Down
102 changes: 96 additions & 6 deletions lib/SILOptimizer/UtilityPasses/Link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@
#include "swift/SILOptimizer/PassManager/Transforms.h"
#include "swift/SIL/SILModule.h"
#include "swift/Serialization/SerializedSILLoader.h"
#include "swift/Serialization/SerializedModuleLoader.h"

using namespace swift;

static llvm::cl::opt<bool> LinkEmbeddedRuntime("link-embedded-runtime",
llvm::cl::init(true));

static llvm::cl::opt<bool> LinkUsedFunctions("link-used-functions",
llvm::cl::init(true));

//===----------------------------------------------------------------------===//
// Top Level Driver
//===----------------------------------------------------------------------===//
Expand All @@ -45,6 +49,12 @@ class SILLinker : public SILModuleTransform {
if (M.getOptions().EmbeddedSwift && LinkEmbeddedRuntime) {
linkEmbeddedRuntimeFromStdlib();
}

// In embedded Swift, we need to explicitly link any @_used globals and
// functions from imported modules.
if (M.getOptions().EmbeddedSwift && LinkUsedFunctions) {
linkUsedGlobalsAndFunctions();
}
}

void linkEmbeddedRuntimeFromStdlib() {
Expand Down Expand Up @@ -82,23 +92,103 @@ class SILLinker : public SILModuleTransform {
// Don't link allocating runtime functions in -no-allocations mode.
if (M.getOptions().NoAllocations && allocating) return;

// Bail if runtime function is already loaded.
if (M.lookUpFunction(name)) return;
// Swift Runtime functions are all expected to be SILLinkage::PublicExternal
linkUsedFunctionByName(name, SILLinkage::PublicExternal);
}

SILFunction *linkUsedFunctionByName(StringRef name,
std::optional<SILLinkage> Linkage) {
SILModule &M = *getModule();

SILFunction *Fn =
M.getSILLoader()->lookupSILFunction(name, SILLinkage::PublicExternal);
if (!Fn) return;
// Bail if function is already loaded.
if (auto *Fn = M.lookUpFunction(name)) return Fn;

SILFunction *Fn = M.getSILLoader()->lookupSILFunction(name, Linkage);
if (!Fn) return nullptr;

if (M.linkFunction(Fn, LinkMode))
invalidateAnalysis(Fn, SILAnalysis::InvalidationKind::Everything);

// Make sure that dead-function-elimination doesn't remove runtime functions.
// Make sure that dead-function-elimination doesn't remove the explicitly
// linked functions.
//
// TODO: lazily emit runtime functions in IRGen so that we don't have to
// rely on dead-stripping in the linker to remove unused runtime
// functions.
if (Fn->isDefinition())
Fn->setLinkage(SILLinkage::Public);

return Fn;
}

SILGlobalVariable *linkUsedGlobalVariableByName(StringRef name) {
SILModule &M = *getModule();

// Bail if runtime function is already loaded.
if (auto *GV = M.lookUpGlobalVariable(name)) return GV;

SILGlobalVariable *GV = M.getSILLoader()->lookupSILGlobalVariable(name);
if (!GV) return nullptr;

// Make sure that dead-function-elimination doesn't remove the explicitly
// linked global variable.
if (GV->isDefinition())
GV->setLinkage(SILLinkage::Public);

return GV;
}

void linkUsedGlobalsAndFunctions() {
SmallVector<VarDecl *, 32> Globals;
SmallVector<AbstractFunctionDecl *, 32> Functions;
collectUsedDeclsFromLoadedModules(Globals, Functions);

for (auto *G : Globals) {
auto declRef = SILDeclRef(G, SILDeclRef::Kind::Func);
linkUsedGlobalVariableByName(declRef.mangle());
}

for (auto *F : Functions) {
auto declRef = SILDeclRef(F, SILDeclRef::Kind::Func);
auto *Fn = linkUsedFunctionByName(declRef.mangle(), /*Linkage*/{});

// If we have @_cdecl or @_silgen_name, also link the foreign thunk
if (Fn->hasCReferences()) {
auto declRef = SILDeclRef(F, SILDeclRef::Kind::Func, /*isForeign*/true);
linkUsedFunctionByName(declRef.mangle(), /*Linkage*/{});
}
}
}

void collectUsedDeclsFromLoadedModules(
SmallVectorImpl<VarDecl *> &Globals,
SmallVectorImpl<AbstractFunctionDecl *> &Functions) {
SILModule &M = *getModule();

for (const auto &Entry : M.getASTContext().getLoadedModules()) {
for (auto File : Entry.second->getFiles()) {
if (auto LoadedAST = dyn_cast<SerializedASTFile>(File)) {
auto matcher = [](const DeclAttributes &attrs) -> bool {
return attrs.hasAttribute<UsedAttr>();
};

SmallVector<Decl *, 32> Decls;
LoadedAST->getTopLevelDeclsWhereAttributesMatch(Decls, matcher);

for (Decl *D : Decls) {
if (AbstractFunctionDecl *F = dyn_cast<AbstractFunctionDecl>(D)) {
Functions.push_back(F);
} else if (VarDecl *G = dyn_cast<VarDecl>(D)) {
Globals.push_back(G);
} else {
assert(false && "only funcs and globals can be @_used");
}
}
}
}
}
}

};
} // end anonymous namespace

Expand Down
4 changes: 4 additions & 0 deletions lib/Serialization/DeserializeSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3977,6 +3977,10 @@ SILFunction *SILDeserializer::lookupSILFunction(StringRef name,
return maybeFunc.get();
}

SILGlobalVariable *SILDeserializer::lookupSILGlobalVariable(StringRef name) {
return getGlobalForReference(name);
}

SILGlobalVariable *SILDeserializer::readGlobalVar(StringRef Name) {
if (!GlobalVarList)
return nullptr;
Expand Down
1 change: 1 addition & 0 deletions lib/Serialization/DeserializeSIL.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ namespace swift {
SILFunction *lookupSILFunction(SILFunction *InFunc, bool onlyUpdateLinkage);
SILFunction *lookupSILFunction(StringRef Name,
bool declarationOnly = false);
SILGlobalVariable *lookupSILGlobalVariable(StringRef Name);
bool hasSILFunction(StringRef Name,
std::optional<SILLinkage> Linkage = std::nullopt);
SILVTable *lookupVTable(StringRef MangledClassName);
Expand Down
9 changes: 9 additions & 0 deletions lib/Serialization/SerializedSILLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ SerializedSILLoader::lookupSILFunction(StringRef Name,
return nullptr;
}

SILGlobalVariable *SerializedSILLoader::lookupSILGlobalVariable(StringRef Name) {
for (auto &Des : LoadedSILSections) {
if (auto *G = Des->lookupSILGlobalVariable(Name)) {
return G;
}
}
return nullptr;
}

bool SerializedSILLoader::hasSILFunction(StringRef Name,
std::optional<SILLinkage> Linkage) {
// It is possible that one module has a declaration of a SILFunction, while
Expand Down
51 changes: 51 additions & 0 deletions test/embedded/modules-used.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// RUN: %empty-directory(%t)
// RUN: %{python} %utils/split_file.py -o %t %s

// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -enable-experimental-feature Embedded -parse-as-library -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -enable-experimental-feature Embedded -parse-as-library -I %t %t/Main.swift -emit-sil | %FileCheck %s --check-prefix CHECK-SIL
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -enable-experimental-feature Embedded -parse-as-library -I %t %t/Main.swift -c -o %t/a.o
// RUN: %target-clang %t/a.o -o %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s

// REQUIRES: swift_in_compiler
// REQUIRES: executable_test
// REQUIRES: OS=macosx
// REQUIRES: swift_feature_Embedded
// REQUIRES: swift_feature_SymbolLinkageMarkers

// BEGIN MyModule.swift

@_used
@_section("__DATA,__mysection")
let i_am_not_referenced = 42

// BEGIN Main.swift

import MyModule

@_silgen_name(raw: "section$start$__DATA$__mysection")
var mysection_start: Int

@_silgen_name(raw: "section$end$__DATA$__mysection")
var mysection_end: Int

@main
struct Main {
static func main() {
let start = UnsafeRawPointer(&mysection_start)
let end = UnsafeRawPointer(&mysection_end)
let size = end - start
let count = size / (Int.bitWidth / 8)
print("count: \(count)")
let linker_set = UnsafeBufferPointer(start: start.bindMemory(to: Int.self, capacity: count), count: count)
for i in 0 ..< linker_set.count {
print("mysection[\(i)]: \(linker_set[i])")
}
}
}

// CHECK-SIL: // i_am_not_referenced
// CHECK-SIL-NEXT: sil_global [serialized] [let] @$e8MyModule19i_am_not_referencedSivp : $Int = {

// CHECK: count: 1
// CHECK: mysection[0]: 42
39 changes: 39 additions & 0 deletions test/embedded/modules-used2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// RUN: %empty-directory(%t)
// RUN: %{python} %utils/split_file.py -o %t %s

// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -enable-experimental-feature Embedded -parse-as-library -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -enable-experimental-feature Embedded -parse-as-library -I %t %t/Main.swift -emit-sil | %FileCheck %s --check-prefix CHECK-SIL
// RUN: %target-swift-frontend -enable-experimental-feature SymbolLinkageMarkers -enable-experimental-feature Embedded -parse-as-library -I %t %t/Main.swift -c -o %t/a.o
// RUN: %target-clang %t/a.o -o %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s

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

// BEGIN MyModule.swift

@_used
@_cdecl("main")
func main() -> CInt {
print("main in a submodule")
return 0
}

@_used
func foo() {
}

// BEGIN Main.swift

import MyModule

// CHECK-SIL: // main()
// CHECK-SIL-NEXT: sil @$e8MyModule4mains5Int32VyF : $@convention(thin) () -> Int32 {
// CHECK-SIL: // main
// CHECK-SIL-NEXT: sil [thunk] @main : $@convention(c) () -> Int32
// CHECK-SIL: // foo()
// CHECK-SIL-NEXT: sil @$e8MyModule3fooyyF : $@convention(thin) () -> () {

// CHECK: main in a submodule