Skip to content
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

Introduce @_backDeploy attribute #41271

Merged
merged 5 commits into from
Feb 9, 2022

Conversation

tshortli
Copy link
Contributor

@tshortli tshortli commented Feb 8, 2022

Introduces the @_backDeploy attribute for use on function declarations. The purpose and desired semantics of this attribute are documented in UnderscoredAttributes.md.

This PR implements parsing and serialization and upcoming PRs will implement the semantics as well as additional diagnostics. Aspects of the parsing have been designed to be reused by @_originallyDefinedIn in the future.

Resolves rdar://88453905

@tshortli
Copy link
Contributor Author

tshortli commented Feb 8, 2022

@swift-ci please smoke test

// Ensure @_backDeploy attributes and function bodies are present after
// deserializing .swiftmodule files.
// RUN: %target-swift-frontend -emit-module -o /dev/null -merge-modules %t/Test.swiftmodule -disable-objc-attr-requires-foundation-module -emit-module-interface-path %t/TestFromModule.swiftinterface -module-name Test
// RUN: %FileCheck %s --check-prefix FROMMODULE --check-prefix CHECK < %t/TestFromModule.swiftinterface
Copy link
Contributor

Choose a reason for hiding this comment

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

Just so you know, merge-module isn't used in practice anymore (except on Windows) and we are planning on removing the merge-modules logic completely when the new driver is the default everywhere. I'm not sure what is the best alternative here, I guess when the attribute is fully implemented just building a client will be enough to test if it can be properly deserialized. Leaving this as is is fine for now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's helpful clarification, thanks! I was following the example set by test/ModuleInterface/inlinable-function.swift. I can remove this step when there is a test that exercises using a back deployed decl from a library, as you say.

// Ensure @_backDeploy attributes and function bodies are printed in
// swiftinterface files.
// RUN: %target-swift-frontend -emit-module -o %t/Test.swiftmodule -emit-module-interface-path %t/Test.swiftinterface -module-name Test %s
// RUN: %FileCheck %s --check-prefix FROMSOURCE --check-prefix CHECK < %t/Test.swiftinterface
Copy link
Contributor

Choose a reason for hiding this comment

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

I like to verify the swiftinterfaces that the compiler emits whenever possible. Just for this it might be worth invoking the driver instead of the frontend and passing -verify-emitted-module-interface.

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, fixed

@@ -713,6 +713,12 @@ DECL_ATTR(exclusivity, Exclusivity,
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
128)

DECL_ATTR(_backDeploy, BackDeploy,
OnAbstractFunction |
AllowMultipleAttributes | LongAttribute | UserInaccessible |
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we want AllowMultipleAttributes or is one attribute listing many platforms enough?

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 chose to allow multiple attributes because currently this attribute explodes into one attribute per platform and version tuple when printed into the interface (following @_originallyDefinedIn as an archetype).

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, sounds good.

"* as platform name has no effect in '%0' attribute", (StringRef))

ERROR(attr_availability_need_platform_version,none,
"expected at least one platform version in in '%0' attribute", (StringRef))
Copy link
Contributor

@xymus xymus Feb 8, 2022

Choose a reason for hiding this comment

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

Typo: in in

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed


// FIXME(backDeploy): Computed properties should be supported
@available(macOS 12.0, *)
@_backDeploy(macOS 11.0) // expected-error {{'@_backDeploy' attribute cannot be applied to this declaration}}
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice to explain why a bit. Sometimes we pass down the kind of decl cannot be applied to a computer property and it gives just enough context for a google search to return useful information.

Copy link
Contributor

Choose a reason for hiding this comment

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

There are facilities to print a textual version of the decl kind in the diagnostics already, I can't find it at the moment.

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 dig around to see if I can find that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looked into this but it seems like improving the diagnostic would improve it for every attribute which would affect a ton of tests. I'll file a follow up radar to investigate doing this systemically.

Copy link
Contributor

@xymus xymus left a comment

Choose a reason for hiding this comment

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

It looks good!


@available(macOS 12.0, *)
@_backDeploy(macOS 11.0)
public func backDeployedTopLevelFunc() {}
Copy link
Contributor

Choose a reason for hiding this comment

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

Great work!

Semantically, does this mean we emit the code into client when deploying to macOS 11.0 (inclusively) and before? I wonder what happens if the deployment target is somewhere between 11.0 and 12.0, say 11.4?

Copy link
Contributor

Choose a reason for hiding this comment

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

Never mind, it seems 11.0 is the lower bound of backward deployment, which makes a lot of sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's right, it's intended to be a lower bound for inlinability, which is going to help us diagnose when library authors use potentially unavailable APIs from the bodies of back deployed functions.

@tshortli
Copy link
Contributor Author

tshortli commented Feb 8, 2022

@swift-ci please smoke test

…iftinterface containing a back-deployed decl. Also leave a FIXME to stop using -merge-modules in the future and fix a diagnostic typo.
@tshortli
Copy link
Contributor Author

tshortli commented Feb 8, 2022

@swift-ci please smoke test

@tshortli tshortli merged commit 967a8b4 into swiftlang:main Feb 9, 2022
@nkcsgexi
Copy link
Contributor

nkcsgexi commented Feb 9, 2022

🎉

@tshortli tshortli deleted the introduce-backdeploy-attr branch February 18, 2022 17:07
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.

3 participants