Skip to content

[embedded] In -assert-config Debug, print errors in assertions, preconditions, fatalErrors #72817

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 7 commits into from
Apr 10, 2024

Conversation

kubamracek
Copy link
Contributor

This PR aims to improve how preconditions and fatalErrors are compiled in Embedded Swift:

  • For "production" binaries, i.e. -O or -Osize builds:
    • Failed preconditions, preconditionFailures and fatalErrors still just emit a trap (already the case today).
    • fatalErrors will use Builtin.condfail_message instead of Builtin.int_trap to avoid collapsing multiple trap sites into one. This will get the behavior on par with failed preconditions and preconditionFailures, which already do that.
  • In non-production binaries, i.e. -Onone builds or -O/-Osize builds with -assert-config Debug:
    • Failed preconditions and fatalErrors will print the file name, line number and message, just like desktop Swift does. This will have a (probably significant) codesize cost.
    • Indirectly, this will also prevent traps from collapsing into one, because the line numbers will be different.

@kubamracek kubamracek requested a review from a team as a code owner April 3, 2024 22:05
@kubamracek
Copy link
Contributor Author

PR is missing tests, will add them once we get agreement that this is the right approach :)

@@ -102,7 +102,7 @@ void SILLinkerVisitor::maybeAddFunctionToWorklist(SILFunction *F,
bool setToSerializable) {
SILLinkage linkage = F->getLinkage();
assert((!setToSerializable || F->hasValidLinkageForFragileRef() ||
hasSharedVisibility(linkage)) &&
hasSharedVisibility(linkage) || Mod.getOptions().EmbeddedSwift) &&
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eeckstein do you have guidance on whether relaxing this assert is reasonable, or if there's a better way to solve the following problem?

Basically the situation is: The stdlib has an @_extern(c) func putchar (without a body), which gets serialized into the stdlib. A user module also has a @_extern(c) func putchar (without a body). This then triggers this assert when we indirectly end up deserializing this forward declaration from the stdlib.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, can this also happen in regular swift?
Is it possible to narrow down the case, e.g. exclude C function decls?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the minimal reproducer:

// RUN: %empty-directory(%t)
// RUN: %{python} %utils/split_file.py -o %t %s

// RUN: %target-swift-frontend -emit-module -o %t/MyModule.swiftmodule %t/MyModule.swift -enable-experimental-feature Embedded -parse-as-library
// RUN: %target-swift-frontend -c -I %t %t/Main.swift -enable-experimental-feature Embedded -o %t/a.o

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

// BEGIN MyModule.swift

@_extern(c)
func some_c_api()

@_transparent
public func publicFuncInAModule() {
  internalFuncInAModule()
}

@usableFromInline
internal func internalFuncInAModule() {
  some_c_api()
}

// BEGIN Main.swift

import MyModule

@_extern(c)
func some_c_api()

some_c_api()
publicFuncInAModule()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed offline, it's only happening in embedded swift. It's fine to keep it as is. Just add your explanation as a short comment

Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See my comment about the assert. Otherwise LGTM

@@ -102,7 +102,7 @@ void SILLinkerVisitor::maybeAddFunctionToWorklist(SILFunction *F,
bool setToSerializable) {
SILLinkage linkage = F->getLinkage();
assert((!setToSerializable || F->hasValidLinkageForFragileRef() ||
hasSharedVisibility(linkage)) &&
hasSharedVisibility(linkage) || Mod.getOptions().EmbeddedSwift) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, can this also happen in regular swift?
Is it possible to narrow down the case, e.g. exclude C function decls?

@kubamracek kubamracek added the embedded Embedded Swift label Apr 8, 2024
@kubamracek kubamracek force-pushed the embedded-print-on-assert branch from 1100ab8 to 34dc983 Compare April 8, 2024 20:42
@kubamracek
Copy link
Contributor Author

@swift-ci please test

2 similar comments
@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek kubamracek force-pushed the embedded-print-on-assert branch from df7ca9e to 9fea230 Compare April 9, 2024 16:32
Copy link
Contributor

@eeckstein eeckstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek kubamracek force-pushed the embedded-print-on-assert branch from 9fea230 to 0c73c57 Compare April 9, 2024 18:33
@kubamracek kubamracek force-pushed the embedded-print-on-assert branch from 0c73c57 to 67f3d34 Compare April 9, 2024 18:36
@kubamracek
Copy link
Contributor Author

@swift-ci please test

1 similar comment
@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek kubamracek merged commit 6e4ccca into swiftlang:main Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
embedded Embedded Swift
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants