Skip to content

IRGen: support static linking on Windows #37211

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 2 commits into from
May 5, 2021

Conversation

compnerd
Copy link
Member

@compnerd compnerd commented May 2, 2021

Replace this paragraph with a description of your changes and rationale. Provide links to external references/discussions if appropriate.

Resolves SR-NNNN.

eeckstein and others added 2 commits May 2, 2021 09:34
If the `-static` option is specified, store that in the generated
swiftmodule file.  When de-serializing, recover this information in the
representative SILModule.

This will be used for code generation on Windows.  It is the missing
piece to allow static linking to function properly.  It additionally
opens the path to additional optimization on ELF-ish targets - GOT, PLT
references can be avoided when the linked module is known to be static.

Co-authored by: Saleem Abdulrasool <[email protected]>
This adjusts the IRGen layer to accommodate the Windows linking model.
We assume dynamic linking by default.  The static linking is enabled by
passing `-static` to the driver, which forwards it to the frontend when
building the module statically.  This has already been required when
generating libraries, however, the non-Windows targets are more
forgiving and let it work.  On those platforms, using this hint would
allow for more efficient code generation, reducing load times and some
runtime penalties from the PLT and GOT references formed to symbols
which are module local.

This corrects static linking on Windows, which is one of the last few
items that are missing on Windows.  It also takes advantage of the hint
for the one peculiar difference between Windows and non-Windows:
protocol conformances that span module boundaries are not available as a
constant.  However, when statically linking, we can enable those
conformances to be statically resolved.  This should enable the last
known pattern to work when using static linking.

This support requires further work in the Swift Package Manager to
actually enable building libraries properly.  However, when building
with CMake, this should be sufficient to enable static linking.
@compnerd
Copy link
Member Author

compnerd commented May 2, 2021

@swift-ci please test

@swift-ci
Copy link
Contributor

swift-ci commented May 2, 2021

Build failed
Swift Test Linux Platform
Git Sha - 8da2c37

@compnerd
Copy link
Member Author

compnerd commented May 2, 2021

Hmm, this might be mishandling C/C++ linkage.

@CodaFi
Copy link
Contributor

CodaFi commented May 2, 2021

Having this as a bit in the AST seems like a huge layering violation. A swiftmodule doesn't have to have anything to do with the final binary image on disk, and one could conceivably share a swiftmodule between a static archive and a dylib if they so choose. Is there a way for us to divine this from the link arguments?

@compnerd
Copy link
Member Author

compnerd commented May 3, 2021

@CodaFi nope, link time is too late. This needs to be available before compiling. We need to know at compile time if the module is statically or dynamically linked. I had brought up this idea at https://forums.swift.org/t/enabling-static-linking-on-windows/40509/10. I'm open to another solution if you have one.

@compnerd
Copy link
Member Author

compnerd commented May 3, 2021

@swift-ci please test Linux platform

@CodaFi
Copy link
Contributor

CodaFi commented May 4, 2021

Discussed this with @compnerd further and there isn't really a good alternative either of us could identify. Approving so we can make progress here. The ABI isn't set in stone on Linux or Windows so we have the opportunity to amend this in the future if need be.

@compnerd compnerd merged commit 0771f1e into swiftlang:main May 5, 2021
@compnerd compnerd deleted the windows-static-linking branch May 5, 2021 14:26
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.

4 participants