Skip to content

re-instate: embedded: allow dead function elimination for de-serialized functions #72227

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
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
5 changes: 3 additions & 2 deletions lib/SIL/IR/Linker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,9 @@ void SILLinkerVisitor::process() {
Fn->setSerialized(IsSerialized_t::IsNotSerialized);
}

// TODO: This should probably be done as a separate SIL pass ("internalize")
if (Fn->getModule().getASTContext().LangOpts.hasFeature(Feature::Embedded)) {
if (Fn->getModule().getASTContext().LangOpts.hasFeature(Feature::Embedded) &&
Fn->getModule().getASTContext().LangOpts.DebuggerSupport) {
// LLDB requires that functions with bodies are not external.
Fn->setLinkage(stripExternalFromLinkage(Fn->getLinkage()));
}

Expand Down
3 changes: 3 additions & 0 deletions lib/SIL/IR/SILFunction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,9 @@ bool SILFunction::shouldBePreservedForDebugger() const {
if (getEffectiveOptimizationMode() != OptimizationMode::NoOptimization)
return false;

if (isAvailableExternally())
return false;

if (hasSemanticsAttr("no.preserve.debugger"))
return false;

Expand Down
9 changes: 9 additions & 0 deletions lib/SILOptimizer/Mandatory/IRGenPrepare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ class IRGenPrepare : public SILFunctionTransform {
void run() override {
SILFunction *F = getFunction();

if (getOptions().EmbeddedSwift) {
// In embedded swift all the code is generated in the top-level module.
// Even de-serialized functions must be code-gen'd.
SILLinkage linkage = F->getLinkage();
if (isAvailableExternally(linkage)) {
F->setLinkage(stripExternalFromLinkage(linkage));
}
}

bool shouldInvalidate = cleanFunction(*F);

if (shouldInvalidate)
Expand Down
7 changes: 7 additions & 0 deletions lib/SILOptimizer/UtilityPasses/Link.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ class SILLinker : public SILModuleTransform {

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

// Make sure that dead-function-elimination doesn't remove runtime 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);
}
};
} // end anonymous namespace
Expand Down
22 changes: 22 additions & 0 deletions test/embedded/floatingpoint.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %target-run-simple-swift(-enable-experimental-feature Embedded -parse-as-library -runtime-compatibility-version none -wmo -Xfrontend -disable-objc-interop) | %FileCheck %s
// RUN: %target-run-simple-swift(-O -enable-experimental-feature Embedded -parse-as-library -runtime-compatibility-version none -wmo -Xfrontend -disable-objc-interop) | %FileCheck %s
// RUN: %target-run-simple-swift(-O -lto=llvm-full %lto_flags -enable-experimental-feature Embedded -parse-as-library -runtime-compatibility-version none -wmo -Xfrontend -disable-objc-interop) | %FileCheck %s

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

// Check that initializing a Double with an integer literal doesn't result in unresolved symbols
@inline(never)
func testLiteral() -> Double {
return Double(1)
}

@main
struct Main {
static func main() {
print(testLiteral() == 1.0)
// CHECK: true
}
}
4 changes: 0 additions & 4 deletions test/embedded/stdlib-basic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,8 @@ public func checks(n: Int) {

// CHECK: define {{.*}}i32 @main(i32 %0, ptr %1)
// CHECK: define {{.*}}i1 @"$s4main4boolSbyF"()
// CHECK: define {{.*}}i1 @"$sSb22_builtinBooleanLiteralSbBi1__tcfC"(i1 %0)
// CHECK: define {{.*}}{{i32|i64}} @"$s4main3intSiyF"()
// CHECK: define {{.*}}{{i32|i64}} @"$sSi22_builtinIntegerLiteralSiBI_tcfC"(ptr %0, {{i32|i64}} %1)
// CHECK: define {{.*}}ptr @"$s4main3ptr1p1nS2V_SitF"(ptr %0, {{i32|i64}} %1)
// CHECK: define {{.*}}ptr @"$sSV8advanced2bySVSi_tF"({{i32|i64}} %0, ptr %1)
// CHECK: define {{.*}}{ {{i32|i64}}, i8 } @"$s4main8optionalSiSgyF"()
// CHECK: define {{.*}}{ {{i32|i64}}, {{i32|i64}}, i8 } @"$s4main12staticstrings12StaticStringVyF"()
// CHECK: define {{.*}}{ {{i32|i64}}, {{i32|i64}}, i8 } @"$ss12StaticStringV08_builtinB7Literal17utf8CodeUnitCount7isASCIIABBp_BwBi1_tcfC"(ptr %0, {{i32|i64}} %1, i1 %2)
// CHECK: define {{.*}}void @"$s4main6checks1nySi_tF"({{i32|i64}} %0)