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

Add alignment to the NPO guarantee #114845

Merged
merged 1 commit into from
Sep 2, 2023
Merged

Conversation

scottmcm
Copy link
Member

@scottmcm scottmcm commented Aug 15, 2023

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 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
image

@scottmcm scottmcm added T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Aug 15, 2023
@rustbot
Copy link
Collaborator

rustbot commented Aug 15, 2023

r? @m-ou-se

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 15, 2023
@@ -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`:
Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member Author

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 🙃

@scottmcm scottmcm added the I-lang-nominated Nominated for discussion during a lang team meeting. label Aug 15, 2023
@rust-log-analyzer

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.
@joshtriplett
Copy link
Member

@rfcbot merge

This is intentionally both lang (for the null-pointer optimization) and libs-api (for the specific types involved).

@scottmcm scottmcm removed the I-lang-nominated Nominated for discussion during a lang team meeting. label Aug 15, 2023
@rfcbot
Copy link

rfcbot commented Aug 15, 2023

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.
See this document for info about what commands tagged team members can give me.

@rfcbot rfcbot added proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. labels Aug 15, 2023
@scottmcm

This comment was marked as resolved.

@rfcbot rfcbot added final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. and removed proposed-final-comment-period Proposed to merge/close by relevant subteam, see T-<team> label. Will enter FCP once signed off. labels Aug 22, 2023
@rfcbot
Copy link

rfcbot commented Aug 22, 2023

🔔 This is now entering its final comment period, as per the review above. 🔔

@rfcbot rfcbot added finished-final-comment-period The final comment period is finished for this PR / Issue. to-announce Announce this issue on triage meeting and removed final-comment-period In the final comment period and will be merged soon unless new substantive objections are raised. labels Sep 1, 2023
@rfcbot
Copy link

rfcbot commented Sep 1, 2023

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.

@WaffleLapkin
Copy link
Member

@bors r+

As per the completed FCP above.

@bors
Copy link
Contributor

bors commented Sep 1, 2023

📌 Commit 107cd8e has been approved by WaffleLapkin

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Sep 1, 2023
@WaffleLapkin
Copy link
Member

@bors rollup

fmease added a commit to fmease/rust that referenced this pull request Sep 2, 2023
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)
fmease added a commit to fmease/rust that referenced this pull request Sep 2, 2023
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)
bors added a commit to rust-lang-ci/rust that referenced this pull request Sep 2, 2023
…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
@bors bors merged commit 1df0c21 into rust-lang:master Sep 2, 2023
11 checks passed
@rustbot rustbot added this to the 1.74.0 milestone Sep 2, 2023
@scottmcm scottmcm deleted the npo-align branch September 2, 2023 08:30
RalfJung pushed a commit to RalfJung/miri that referenced this pull request Sep 3, 2023
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)
@apiraino apiraino removed the to-announce Announce this issue on triage meeting label Sep 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
disposition-merge This issue / PR is in PFCP or FCP with a disposition to merge it. finished-final-comment-period The final comment period is finished for this PR / Issue. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-lang Relevant to the language team, which will review and decide on the PR/issue. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants