-
Notifications
You must be signed in to change notification settings - Fork 13.9k
Show packed field alignment in mir_transform_unaligned_packed_ref #147743
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
base: master
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
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.
Thanks, and welcome to working on the compiler! Have a few comments, see below. Remember to squash once you're done.
Also, I see that you originally implemented the full message as discussed by the referenced issue, but then decided to do something simpler - could you edit your top-level comment to discuss why this wasn't done in the end? (see the review policy for why to put stuff in the PR message).
mir_transform_unaligned_packed_ref = reference to packed field is unaligned | ||
.note = packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses | ||
.note = this field is only {$actual}-byte aligned, but its type requires more alignment |
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.
.note = this field is only {$actual}-byte aligned, but its type requires more alignment | |
.note = this field is only {$actual}-byte aligned, but its type requires higher alignment |
} | ||
mir_transform_unaligned_packed_ref = reference to packed field is unaligned | ||
.note = packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses |
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.
What's the rationale for dropping the "many modern architectures penalize unaligned field accesses" part?
Maybe keep it as another note
?
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'm actually happy to see that disappear -- no idea when or why it was added, but I don't see why it is relevant at all here. The most likely effect it has is to reinforce the common misconception that the alignment requirements in Rust arise because of hardware alignment requirements.
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.
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.
However, the new message arguably regresses talking about the struct type. So maybe this should say
reference to field of packed struct is unaligned
(since arguably it's not the field that's packed, it's the struct)
the struct is at most {$actual}-byte aligned, but the type of this field may require higher alignment
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 initially removed "this struct" because it's ambiguous - in the expression &a.b.c
, it could be either a
or b
that causes the unalignment. (It's also technically incorrect, because unions can also be packed.)
I'm happy with this final message in spite of that though, so I'll go ahead and implement it.
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.
Deep inside is_within_packed
we know the actual ADT this is talking about; this could be exposed to call adt.descr()
instead of just saying "struct".
_ => { | ||
// We cannot figure out the layout. Conservatively assume that this is disaligned. | ||
debug!("is_disaligned({:?}) - true", place); | ||
true |
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.
Nit: Fix the debug!
message above
/// struct). | ||
/// Returns the packed alignment if this place is allowed to be less aligned | ||
/// than its type normally requires (because it is within a packed struct). | ||
pub fn is_disaligned<'tcx, L>( |
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.
Nit: The is_
part is kinda wrong now, maybe the function should be renamed?
Maybe something like misalignment
? (would also fix my problem with the use of "disaligned" here which sounds weird, the proper term is "misaligned").
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.
"misaligned" is the term for "alignment violated". But this function can't know if alignment is truly violated, it only checks if alignment may be violated.
So maybe "unaligned" is a better term.
| ^^^^ | ||
| | ||
= note: packed structs are only aligned by one byte, and many modern architectures penalize unaligned field accesses | ||
= note: this field is only 1-byte aligned, but its type requires more alignment |
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.
So if I have a packed(8)
struct with a field whose type cannot be determined, the error will say
this field is only 8-byte aligned, but its type requires more alignment
That's not correct though -- the type may require higher alignment, but we don't actually know that for sure.
Fixes #147528