Skip to content

Allow @_silgen_name to be used on globals and add a @_silgen_name(raw: ...) version that skips mangling #66540

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 3 commits into from
Jun 29, 2023

Conversation

kubamracek
Copy link
Contributor

Attribute @_silgen_name is today only allowed to be used on functions, this PR allows usage on globals as well. The motivation for that is to be able to "forward declare" globals just like it's today possible to do with functions (for the cases where it's not practical or convenient to use a bridging header).

Separately, this PR also adds a @_silgen_name(raw: ...) syntax, which simply avoids mangling the name (by using the \01 name prefix that LLVM uses). The motivation for that is to be able to reference the "magic Darwin linker symbols" that can be used to look up section bounds (in the current dylib/module) -- those symbols don't use the underscore prefix in their mangled names. The attached testcases showcase this as a low-level "linker set" implementation.

@jckarter
Copy link
Contributor

It isn't safe to export the symbol of a global generally because anyone directly accessing it would bypass the one-time initializer, which is why this hasn't been allowed yet. If @_silgen_name-ing a global also required that the initializer get optimized into a static constant in the binary, that would be safer.

@kubamracek
Copy link
Contributor Author

@jckarter Added a check that rejects @_silgen_name if the global isn't guaranteed to be statically initialized. (See added test.)

@ahoppen ahoppen removed their request for review June 19, 2023 15:44
@rintaro rintaro removed their request for review June 23, 2023 18:24
kubamracek added a commit to swiftlang/swift-syntax that referenced this pull request Jun 26, 2023
@kubamracek
Copy link
Contributor Author

Please test with following pull request:
swiftlang/swift-syntax#1850

@swift-ci Please test

kubamracek added a commit to swiftlang/swift-syntax that referenced this pull request Jun 28, 2023
@kubamracek kubamracek force-pushed the silgen-name-for-globals branch from 6e4863b to ecfeb1d Compare June 28, 2023 15:30
@kubamracek
Copy link
Contributor Author

Please test with following pull request:
swiftlang/swift-syntax#1850

@swift-ci Please test

@kubamracek
Copy link
Contributor Author

Please test with following pull request:
swiftlang/swift-syntax#1850

@swift-ci Please test

kubamracek added a commit to swiftlang/swift-syntax that referenced this pull request Jun 29, 2023
Support for swiftlang/swift#66540: @_silgen_name on globals, @_silgen_name(raw: ...)
@kubamracek kubamracek merged commit d427696 into swiftlang:main Jun 29, 2023
@kubamracek kubamracek deleted the silgen-name-for-globals branch June 29, 2023 15:37
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.

2 participants