Skip to content

[Mangling] [NFC] Prepare for a new mangling prefix for Embedded Swift: $e #77115

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 9 commits into from
Dec 3, 2024

Conversation

kubamracek
Copy link
Contributor

@kubamracek kubamracek commented Oct 18, 2024

Given that Embedded Swift's ABI and layout of class metadata is (intentionally) different and incompatible with regular Swift, this PR is introducing a different mangling for all Embedded Swift. Concretely, it changes the mangling prefix from $s to $e.

For reviewing, I recommend looking at commits individually: The first commit is a mechanical change to pass an ASTContext to all instantiations of ASTMangler and its derivatives, which is a large diff but essentially an NFC.

This PR is a large diff, but it's an NFC, just preparing for an actual mangling change in a subsequent commit.

Copy link
Contributor

@slavapestov slavapestov left a comment

Choose a reason for hiding this comment

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

In ASTContext.cpp I see a handful of places where we call getASTContext() on a Type or Decl we have sitting nearby. Do you mind also changing those calls to directly use the new Context instance member that you added?

Copy link
Contributor

@benrimmington benrimmington left a comment

Choose a reason for hiding this comment

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

Should the new prefix be documented? docs/ABI/Mangling.rst

@kubamracek
Copy link
Contributor Author

I'm missing a change to swift/docs/ABI/Mangling.rst?

Added.

@kubamracek
Copy link
Contributor Author

Could you check if changing the mangling prefix changes the USRs used for indexing? Could you add a test to test/Index that builds with embedded mode and just has a FileCheck line for a simple declaration? (index_curry_thunk.swift seems to be a good starting point that you can simply further.

If it does change the USR prefix, I think that’s fine but we might need to do some downstream adjustments for that.

Added the test: test/Index/index_embedded.swift.

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek
Copy link
Contributor Author

Another place to update is in swift-demangle:

if (ch == 'S' || ch == 's') {

Fixed, added test.

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@@ -400,6 +401,10 @@ class TypeRefBuilder {
TypeRefBuilder(const TypeRefBuilder &other) = delete;
TypeRefBuilder &operator=(const TypeRefBuilder &other) = delete;

Mangle::ManglingFlavor getManglingFlavor() {
return Mangle::ManglingFlavor::Default;
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you make this a parameter in the constructor? It can be defaulted to true. Reason being that LLDB uses TypeRefBuilder to process types found in DWARF.

@@ -185,6 +185,7 @@ int swift::Demangle::getManglingPrefixLength(llvm::StringRef mangledName) {
/*Swift 4*/ "_T0",
/*Swift 4.x*/ "$S", "_$S",
/*Swift 5+*/ "$s", "_$s",
/*Swift 5+ Embedded Swift*/ "$e", "_$e",
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add a function here that returns the Mangling Flavor of a string, and declare it on Demangle.h as well?

Something like:

std::optional<Mangle::ManglingFlavor> getManglingFlavor(llvm::StringRef mangledName) {
  if (!isSwiftSymbol(mangledName))
    return {};

  if (mangledName.starts_with("$e") || mangledName.starts_with("_$e"))
    return Mangle::ManglingFlavor::Embedded;
  return Mangle::ManglingFlavor::Default;
}

@kubamracek kubamracek force-pushed the embedded-mangling-prefix branch from 0a19011 to 6dbff5f Compare December 2, 2024 18:26
@kubamracek
Copy link
Contributor Author

Given the size of this PR and the revlock with LLDB, I'm going to break this PR up, and stage the changes over a series of PRs. I have changed this PR to be a NFC, i.e. not actually changing the mangling just yet. Also added a legacy constructor on ASTMangler that does not require a ASTContext& passed in, to avoid this being a breaking API change.

Subsequent separate PRs will adapt LLDB, actually enable the $e prefix, and drop the legacy ASTMangler constructor.

@kubamracek kubamracek force-pushed the embedded-mangling-prefix branch from 1d630ae to 6acc3cd Compare December 2, 2024 19:09
@kubamracek
Copy link
Contributor Author

@swift-ci please test

1 similar comment
@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek kubamracek changed the title [Mangling] Establish a new mangling prefix for Embedded Swift: $e [Mangling] [NFC] Prepare for a new mangling prefix for Embedded Swift: $e Dec 2, 2024
@kubamracek kubamracek force-pushed the embedded-mangling-prefix branch from 94f6433 to b760541 Compare December 2, 2024 23:04
@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek
Copy link
Contributor Author

@swift-ci please test Linux platform

@kubamracek
Copy link
Contributor Author

swiftlang/swift-driver#1744

@swift-ci please test

@kubamracek kubamracek merged commit 8792efe into swiftlang:main Dec 3, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants