-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add alignment to the NPO guarantee #114845
Conversation
r? @m-ou-se (rustbot has picked a reviewer for you, use r? to override) |
@@ -119,7 +119,7 @@ | |||
//! # Representation | |||
//! | |||
//! Rust guarantees to optimize the following types `T` such that | |||
//! [`Option<T>`] has the same size as `T`: | |||
//! [`Option<T>`] has the same size and alignment as `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.
These two words are the core of the change; the other places refer back to here.
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.
We actually also need them to be function-call-ABI compatible, right? Even types with the same size and alignment can have different ABIs.
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.
That's already guaranteed for a few types, like https://doc.rust-lang.org/stable/std/num/struct.NonZeroU32.html#layout-1.
That said, I'd like to keep this PR+FCP to just the hopefully-obvious and non-controversial addition of alignment, and something else can add more.
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.
Hm I see. We should probably standardize terminology at some point, "FFI" isn't really clear that it talks about ABI -- after all, for by-ref FFI, you don't need ABI compatibility.
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.
And avoiding deciding on the terminology for this is part of why I'm happy keeping this to just something we have a word for already 🙃
This comment has been minimized.
This comment has been minimized.
As far as I know, this is always true already, but it's not in the text of the Option module docs, so I figured I'd bring this up to FCP it.
@rfcbot merge This is intentionally both lang (for the null-pointer optimization) and libs-api (for the specific types involved). |
Team member @joshtriplett has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! cc @rust-lang/lang-advisors: FCP proposed for lang, please feel free to register concerns. |
This comment was marked as resolved.
This comment was marked as resolved.
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
@bors r+ As per the completed FCP above. |
@bors rollup |
Add alignment to the NPO guarantee This PR [changes](rust-lang#114845 (comment)) "same size" to "same size and alignment" in the option module's null pointer optimization docs in <https://doc.rust-lang.org/std/option/#representation>. As far as I know, this has been true for a long time in the actual rustc implementation, but it's not in the text of those docs, so I figured I'd bring this up to FCP it. I also see no particular reason that we'd ever *want* to have higher alignment on these. In many of the cases it's impossible, as the minimum alignment is already the size of the type, but even if we *could* do things like on 32-bit we could say that `NonZeroU64` is 4-align but `Option<NonZeroU64>` is 8-align, I just don't see any value in doing that, so feel completely fine closing this door for the few things on which the NPO is already guaranteed. These are basically all primitives, and should end up with the same size & alignment as those primitives. (There's no layout guarantee for something like `Option<[u8; 3]>`, where it'd be at least plausible to consider raising the alignment from 1 to 4 on, say, some hypothetical target that doesn't have efficient unaligned 4-byte load/stores. And even if we ever did start to offer some kind of guarantee around such a type, I doubt we'd put it under the "null pointer" optimization header.) Screenshots for the new examples: ![image](https://github.com/rust-lang/rust/assets/18526288/a7dbff42-50b4-462e-9e27-00d511b58763) ![image](https://github.com/rust-lang/rust/assets/18526288/dfd55288-80fb-419a-bc11-26198c27f9f9)
Add alignment to the NPO guarantee This PR [changes](rust-lang#114845 (comment)) "same size" to "same size and alignment" in the option module's null pointer optimization docs in <https://doc.rust-lang.org/std/option/#representation>. As far as I know, this has been true for a long time in the actual rustc implementation, but it's not in the text of those docs, so I figured I'd bring this up to FCP it. I also see no particular reason that we'd ever *want* to have higher alignment on these. In many of the cases it's impossible, as the minimum alignment is already the size of the type, but even if we *could* do things like on 32-bit we could say that `NonZeroU64` is 4-align but `Option<NonZeroU64>` is 8-align, I just don't see any value in doing that, so feel completely fine closing this door for the few things on which the NPO is already guaranteed. These are basically all primitives, and should end up with the same size & alignment as those primitives. (There's no layout guarantee for something like `Option<[u8; 3]>`, where it'd be at least plausible to consider raising the alignment from 1 to 4 on, say, some hypothetical target that doesn't have efficient unaligned 4-byte load/stores. And even if we ever did start to offer some kind of guarantee around such a type, I doubt we'd put it under the "null pointer" optimization header.) Screenshots for the new examples: ![image](https://github.com/rust-lang/rust/assets/18526288/a7dbff42-50b4-462e-9e27-00d511b58763) ![image](https://github.com/rust-lang/rust/assets/18526288/dfd55288-80fb-419a-bc11-26198c27f9f9)
…iaskrgr Rollup of 7 pull requests Successful merges: - rust-lang#114349 (rustc_llvm: Link to `zlib` on dragonfly and solaris) - rust-lang#114845 (Add alignment to the NPO guarantee) - rust-lang#115427 (kmc-solid: Fix `is_interrupted`) - rust-lang#115443 (feat(std): Stabilize 'os_str_bytes' feature) - rust-lang#115444 (Create a SMIR visitor) - rust-lang#115449 (Const-stabilize `is_ascii`) - rust-lang#115456 (Add spastorino on vacation) r? `@ghost` `@rustbot` modify labels: rollup
Add alignment to the NPO guarantee This PR [changes](rust-lang/rust#114845 (comment)) "same size" to "same size and alignment" in the option module's null pointer optimization docs in <https://doc.rust-lang.org/std/option/#representation>. As far as I know, this has been true for a long time in the actual rustc implementation, but it's not in the text of those docs, so I figured I'd bring this up to FCP it. I also see no particular reason that we'd ever *want* to have higher alignment on these. In many of the cases it's impossible, as the minimum alignment is already the size of the type, but even if we *could* do things like on 32-bit we could say that `NonZeroU64` is 4-align but `Option<NonZeroU64>` is 8-align, I just don't see any value in doing that, so feel completely fine closing this door for the few things on which the NPO is already guaranteed. These are basically all primitives, and should end up with the same size & alignment as those primitives. (There's no layout guarantee for something like `Option<[u8; 3]>`, where it'd be at least plausible to consider raising the alignment from 1 to 4 on, say, some hypothetical target that doesn't have efficient unaligned 4-byte load/stores. And even if we ever did start to offer some kind of guarantee around such a type, I doubt we'd put it under the "null pointer" optimization header.) Screenshots for the new examples: ![image](https://github.com/rust-lang/rust/assets/18526288/a7dbff42-50b4-462e-9e27-00d511b58763) ![image](https://github.com/rust-lang/rust/assets/18526288/dfd55288-80fb-419a-bc11-26198c27f9f9)
This PR changes "same size" to "same size and alignment" in the option module's null pointer optimization docs in https://doc.rust-lang.org/std/option/#representation.
As far as I know, this has been true for a long time in the actual rustc implementation, but it's not in the text of those docs, so I figured I'd bring this up to FCP it.
I also see no particular reason that we'd ever want to have higher alignment on these. In many of the cases it's impossible, as the minimum alignment is already the size of the type, but even if we could do things like on 32-bit we could say that
NonZeroU64
is 4-align butOption<NonZeroU64>
is 8-align, I just don't see any value in doing that, so feel completely fine closing this door for the few things on which the NPO is already guaranteed. These are basically all primitives, and should end up with the same size & alignment as those primitives.(There's no layout guarantee for something like
Option<[u8; 3]>
, where it'd be at least plausible to consider raising the alignment from 1 to 4 on, say, some hypothetical target that doesn't have efficient unaligned 4-byte load/stores. And even if we ever did start to offer some kind of guarantee around such a type, I doubt we'd put it under the "null pointer" optimization header.)Screenshots for the new examples: