Skip to content

AST/IRGen: Accept @_weakLinked on import decls to force weak linkage of symbols from a module #60414

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 5 commits into from
Aug 11, 2022

Conversation

tshortli
Copy link
Contributor

@tshortli tshortli commented Aug 5, 2022

The effect of declaring an import @_weakLinked is to treat every declaration from the module as if it were declared with @_weakLinked. This is useful in environments where entire modules may not be present at runtime. Although it is already possible to instruct the linker to weakly link an entire dylib, a Swift attribute provides a way to declare intent in source code and also opens the door to diagnostics and other compiler behaviors that depend on knowing that all the module's symbols will be weakly linked.

The @_weakLinked attribute on imports is not transitive; if module A imports module B which @_weakLinked imports module C, it does not follow that A should weakly link symbols from C. Just because B needs to operate on runtimes where C is absent does not mean A needs to also operate on runtimes where C is absent. Therefore the @_weakLinked attribute is not serialized in binary modules nor is it printed in swiftinterface files.

Resolves rdar://96098097.

@tshortli tshortli force-pushed the weak-linked-import branch 2 times, most recently from fdd2d96 to 64181cb Compare August 5, 2022 21:40
@tshortli tshortli changed the title AST: Accept @_weakLinked on import decls to force weak linkage of s ymbols from a module AST: Accept @_weakLinked on import decls to force weak linkage of symbols from a module Aug 5, 2022
@tshortli tshortli force-pushed the weak-linked-import branch from 64181cb to e755814 Compare August 9, 2022 21:20
@tshortli tshortli changed the title AST: Accept @_weakLinked on import decls to force weak linkage of symbols from a module AST/IRGen: Accept @_weakLinked on import decls to force weak linkage of symbols from a module Aug 9, 2022
@tshortli tshortli force-pushed the weak-linked-import branch 2 times, most recently from b963e14 to 4389458 Compare August 10, 2022 00:22
@tshortli
Copy link
Contributor Author

@swift-ci please test

@tshortli
Copy link
Contributor Author

@swift-ci please smoke test

@tshortli tshortli marked this pull request as ready for review August 10, 2022 17:01
@tshortli tshortli requested review from xymus, nkcsgexi and artemcm August 10, 2022 17:01
Copy link
Contributor

@nkcsgexi nkcsgexi left a comment

Choose a reason for hiding this comment

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

This is awesome! Just some nitpicks.

bool ModuleDecl::isImportedAsWeakLinked(const Decl *targetDecl) const {
const auto *declaringModule = targetDecl->getModuleContext();
for (auto file : getFiles()) {
if (file->importsModuleAsWeakLinked(declaringModule))
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we diagnose some source files use @ _weakLinked import Foo but some others use import Foo?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I'm planning to do that in an upcoming PR.

@@ -0,0 +1,159 @@
// RUN: %empty-directory(%t)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you also add a test for adding @_weakLinked import Foo where Foo is a transitive dependency? APIs from Foo are not directly used by the source file, but a direct dependency Bar of the source file has some APIs that trigger inlining Foo's APIs. Those APIs should be weakly linked.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, I'll add that test.

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 was a good catch. It turns out that the logic in this PR does not properly change the linkage of symbol references inlined from other modules. I think it's going to require some deeper investigation so I'd like to tackle it in another PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds good!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a short test case that documents that this currently doesn't work.

…ymbols from a module.

The effect of declaring an import `@_weakLinked` is to treat every declaration from the module as if it were declared with `@_weakLinked`. This is useful in environments where entire modules may not be present at runtime. Although it is already possible to instruct the linker to weakly link an entire dylib, a Swift attribute provides a way to declare intent in source code and also opens the door to diagnostics and other compiler behaviors that depend on knowing that all the module's symbols will be weakly linked.

rdar://96098097
@tshortli tshortli force-pushed the weak-linked-import branch from 5a64127 to 1778a76 Compare August 11, 2022 18:23
@tshortli
Copy link
Contributor Author

@swift-ci please test

@tshortli tshortli requested a review from nkcsgexi August 11, 2022 18:23
@tshortli tshortli merged commit 0d94fb1 into swiftlang:main Aug 11, 2022
@tshortli tshortli deleted the weak-linked-import branch August 11, 2022 22:10
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