-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[SILGen] Consistently use SIL asmname for foreign function/variable references #85203
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
+904
−756
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Member
Author
|
@swift-ci please test |
Member
Author
|
@swift-ci please test source compatibility |
…eferences Whenever we have a reference to a foreign function/variable in SIL, use a mangled name at the SIL level with the C name in the asmname attribute. The expands the use of asmname to three kinds of cases that it hadn't been used in yet: * Declarations imported from C headers/modules * @_cdecl @implementation of C headers/modules * @_cdecl functions in general Some code within the SIL pipeline makes assumptions that the C names of various runtime functions are reflected at the SIL level. For example, the linking of Embedded Swift runtime functions is done by-name, and some of those names refer to C functions (like `swift_retain`) and others refer to Swift functions that use `@_silgen_name` (like `swift_getDefaultExecutor`). Extend the serialized module format to include a table that maps from the asmname of functions/variables over to their mangled names, so we can look up functions by asmname if we want. These tables could also be used for checking for declarations that conflict on their asmname in the future. Right now, we leave it up to LLVM or the linker to do the checking. `@_silgen_name` is not affected by these changes, nor should it be: that hidden feature is specifically meant to affect the name at the SIL level. The vast majority of test changes are SIL tests where we had expected to see the C/C++/Objective-C names in the tests for references to foreign entities, and now we see Swift mangled names (ending in To). The SIL declarations themselves will have a corresponding asmname. Notably, the IRGen tests have *not* changed, because we generally the same IR as before. It's only the modeling at the SIL lever that has changed. Another part of rdar://137014448.
When creating a C++ method that thunks from a particular C++ derived class to call a method on one of its base classes, we do some manual name mangling. This name mangling did not properly capture all of the aspects of the thunk, so it was prone to collisions. In moving to SIL asmname, we got a different set of collisions from the ones that existed previously. Expanding the mangling to account for small differences (const or not) as well as the base class type eliminates these collisions. Most of the test changes account for the new mangling, but the member-inheritance test indicates that this change fixes an existing miscompile as well.
hnrklssn
reviewed
Oct 30, 2025
hnrklssn
reviewed
Oct 30, 2025
hnrklssn
reviewed
Oct 30, 2025
6a2845a to
523b3b7
Compare
Member
Author
|
@swift-ci please test |
Member
Author
|
@swift-ci please test |
Member
Author
|
@swift-ci please test source compatibility |
hnrklssn
reviewed
Oct 31, 2025
Member
Author
|
@swift-ci please test Linux |
Member
Author
|
@swift-ci please test Windows |
Member
Author
|
Source-compatibility failures were unrelated |
Member
Author
|
@swift-ci please test |
c88d754 to
576cb02
Compare
Member
Author
|
@swift-ci please test |
…cationMain from C
Member
Author
|
@swift-ci please test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Whenever we have a reference to a foreign function/variable in SIL, use a mangled name at the SIL level with the C name in the asmname attribute. The expands the use of asmname to three kinds of cases that it hadn't been used in yet:
@_cdecl @implementationand@c @implementationof C headers/modules@_cdeclfunctions in generalSome code within the SIL pipeline makes assumptions that the C names of various runtime functions are reflected at the SIL level. For example, the linking of Embedded Swift runtime functions is done by-name, and some of those names refer to C functions (like
swift_retain) and others refer to Swift functions that use@_silgen_name(likeswift_getDefaultExecutor). Extend the serialized module format to include a table that maps from the asmname of functions/variables over to their mangled names, so we can look up functions by asmname if we want. These tables could also be used for checking for declarationsthat conflict on their asmname in the future. Right now, we leave it up to LLVM or the linker to do the checking.
@_silgen_nameis not affected by these changes, nor should it be: that hidden feature is specifically meant to affect the name at the SIL level.The vast majority of test changes are SIL tests where we had expected to see the C/C++/Objective-C names in the tests for references to foreign entities, and now we see Swift mangled names (ending in To). The SIL declarations themselves will have a corresponding asmname. Notably, the IRGen tests have not changed, because we generate the same IR as before. It's only the modeling at the SIL level that has changed.
This ended up fixing a C++ interoperability bug where some bespoke mangling in the Clang importer had mangling conflicts that resulted in miscomputes of derived-to-base thunks.
This is the last SIL part of rdar://137014448; there is some deserializer work to do as well.