-
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
emit "align 1" metadata on loads/stores of packed structs #39586
Conversation
src/librustc_trans/intrinsic.rs
Outdated
@@ -243,7 +245,7 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, | |||
bcx.volatile_store(llargs[2], get_meta(bcx, llargs[0])); | |||
} else { | |||
let val = if fn_ty.args[1].is_indirect() { | |||
bcx.load(llargs[1]) | |||
bcx.load(llargs[1], None) // FIXME: this is incorrect |
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.
Are you going to do something about this?
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 hard to fix without a refactor.
LGTM, except for that one FIXME. f? @Aatch, r=me if no concerns remain. |
And the FIXME turned out to be unneeded - ABI indirect arguments are always aligned. @bors r=eddyb |
📌 Commit 56591f6 has been approved by |
@bors r=eddyb |
💡 This pull request was already approved, no need to approve it again.
|
📌 Commit 56591f6 has been approved by |
@bors r=eddyb |
📌 Commit 67789c4 has been approved by |
emit "align 1" metadata on loads/stores of packed structs According to the LLVM reference: > A value of 0 or an omitted align argument means that the operation has the ABI alignment for the target. So loads/stores of fields of packed structs need to have their align set to 1. Implement that by tracking the alignment of `LvalueRef`s. Fixes rust-lang#39376. r? @eddyb
According to the LLVM reference: > A value of 0 or an omitted align argument means that the operation has the ABI alignment for the target. So loads/stores of fields of packed structs need to have their align set to 1. Implement that by tracking the alignment of `LvalueRef`s. Fixes rust-lang#39376.
The function was a footgun because it created `undef` references to ZSTs, which could cause trouble were they to leak to user code.
67789c4
to
d71988a
Compare
rebased @bors r=eddyb |
📌 Commit d71988a has been approved by |
Unfortunately, this patch doesn't seem to resolve the issue fully. See #39376 for an example test case that appears to still fail. Note that brute force hack patch also mentioned in the bug there that does resolve the issue for the test case shown. |
This is seen in the RUST_LOG=4 output when building the referenced example:
|
I compile your example using your patch - that's it, use std::slice;
use std::u32;
#[repr(packed)]
#[derive(Copy, Clone)]
struct Unaligned<T>(T);
impl<T> Unaligned<T> {
fn get(self) -> T { self.0 }
}
fn bytes_to_words(b: &[u8]) -> &[Unaligned<u32>] {
unsafe { slice::from_raw_parts(b.as_ptr() as *const Unaligned<u32>, b.len() / 4) }
}
fn main() {
let values = String::from("fedcba98765432100123456789abcdef");
let bytes = values.as_bytes();
let mut words = &bytes_to_words(&bytes[1..]);
let index = 1;
let position = u32::from_le(words[index].get());
} and it works (emits align metadata on the necessary loads). Is there some other example you are talking about?
We should not be having this (immediate |
emit "align 1" metadata on loads/stores of packed structs According to the LLVM reference: > A value of 0 or an omitted align argument means that the operation has the ABI alignment for the target. So loads/stores of fields of packed structs need to have their align set to 1. Implement that by tracking the alignment of `LvalueRef`s. Fixes #39376. r? @eddyb
☀️ Test successful - status-appveyor, status-travis |
Would someone be willing to beta-nominate this? This patch has been used successfully against 1.16 beta for almost two weeks and resolved the last code generation issue we encountered on sparc (and I suspect would help other architectures too). I believe it applies cleanly without any changes to current beta tip. |
[beta] next - #39913 - #39730 - #39674 - #39602 - #39586 - #39471 - #39980 - #40020 - #40135 @nikomatsakis [this commit](3787d33) did not pick cleanly. You might peek at it. I took the liberty of accepting all the nominations myself, but the [packed struct alignment](#39586) PR is quite large. It did pick fine though and there's a comment there suggesting it works on beta cc @rust-lang/compiler. cc @alexcrichton
According to the LLVM reference:
So loads/stores of fields of packed structs need to have their align set
to 1. Implement that by tracking the alignment of
LvalueRef
s.Fixes #39376.
r? @eddyb