-
Notifications
You must be signed in to change notification settings - Fork 10.5k
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
Conversation
fdd2d96
to
64181cb
Compare
@_weakLinked
on import decls to force weak linkage of s ymbols from a module@_weakLinked
on import decls to force weak linkage of symbols from a module
64181cb
to
e755814
Compare
@_weakLinked
on import decls to force weak linkage of symbols from a module@_weakLinked
on import decls to force weak linkage of symbols from a module
b963e14
to
4389458
Compare
@swift-ci please test |
@swift-ci please smoke test |
There was a problem hiding this 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)) |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good!
There was a problem hiding this comment.
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
…ported modules. rdar://96098097
…nsupported on Windows.
…sses @_weakLinked import.
5a64127
to
1778a76
Compare
@swift-ci please test |
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.