-
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
implement #[repr(align)]
(tracking issue for RFC 1358)
#33626
Comments
#[repr(align="8")]
struct F;
println!("{:p}", Box::new(F));
// prints 0x1? 0x8? cc #27700. |
@kennytm That already is an issue without this RFC. use std::mem::align_of;
struct F([u32; 0]);
println!("{:p}", Box::new(F)); //prints 0x1
println!("{}", align_of::<F>()); //prints 4 |
Anyone else implementing this? I need it so I may give it a go. |
What's the relationship of this issue to simd support #27731 (ie. the current way to accomplish this on nightly)? Can/should this be implemented separately? |
@mfarrugi the two are related but not that much. SIMD usually works better on aligned data, but you need aligned data also when you use other kind of hardware features (dma engines for crypto, video decoding/encoding, etc). @whitequark got time to implement it? From what I can see the allocator now is taking an alignment so it should be not terrible to implement (I do not know the rust internals well enough to be confident in poking around this). |
@lu-zero Ok let me give it another try. Rustbuild and (hopefully?) incremental compilation have made rustc hacking much less painful... |
@whitequark have started on this? I was interested and got a proof of concept working, so if you haven't started on it I could probably finish off what I'm doing and make a PR. I'd probably need some mentoring from someone, I haven't added a feature before. |
@bitshifter not really, please go ahead! |
@bitshifter It would be useful to know how this would compose with |
This has turned out to be a bit more involved than what I originally thought, what a surprise! I keep missing comment notifications for some reason. @briansmith looking at the RFC it seems you aren't allowed to combine I have been making progress on this but one sticking point right now is Struct alignment on Rust is not passed to LLVM, in fact as far as I can tell LLVM StuctType doesn't appear to support alignment specified by an attribute, but only returns alignment based on the struct's fields. It seems that alignment attributes in LLVM are handled some other way. The array type in LLVM is also not picking up alignment from the equivalent Rust array type either. As a result this compiler bug macro gets hit https://github.com/rust-lang/rust/blob/master/src/librustc_trans/type_of.rs#L125. If I comment out that |
My changes so far are here https://github.com/bitshifter/rust/tree/struct_align_wip if anyone is interested. Feedback welcome! |
@bitshifter I believe what was figured out earlier is that you have to specify the alignment on every load and store and alloca manually. |
Woah! That sounds awful |
@retep998 ah, at the moment it seems that only |
I have covered all of the cases mentioned in the RFC but have found a couple of other things that I think I need to address. One is enums containing aligned structs and the other is structs containing aligned structs. In the case of a struct containing an aligned struct, I think the members probably need to be manually aligned. Unless I'm totally missing something, as far as I can tell LLVM is completely unaware of alignment attributes (please let me know if you know different!). It seems that it will align/pad things correctly for primitives types that it knows about, but I haven't seen anything on the interface for creating types/structs that supports specifying a different alignment. So I think I probably need manually add padding between fields when necessary to ensure they are aligned correctly. From what I can tell, Rust enums are basically structs with a hidden discriminant field which specifies the type (as i32?), plus the data. If this is the case, I think what probably needs to happen is the enum itself must use the maximum alignment of the data inside it, and the data must be padded so it's also aligned correctly, so hopefully fixing structs will also fix enums. These cases in code form:
Those are the two outstanding things that I'm aware of. Possibly there's other use cases I haven't though of. |
Paging @eddyb :) I was looking through git history and see you added https://github.com/rust-lang/rust/blob/master/src/librustc/ty/layout.rs and the I need to keep track of both abi alignment and alignment requested by repr align. I had added a separate Does my explanation make sense? Can you see any problem with doing this? What is the intention of the |
You should not touch cc @camlorn who almost also got started on this. |
Oh and the parsing of the data layout string should set both alignments for all primitives. |
OK, thanks for the info. The only reason I need to keep track of the original align is for some checks to see if the Rust field matches the LLVM field alignment in https://github.com/rust-lang/rust/blob/master/src/librustc_trans/type_of.rs#L125., LLVM doesn't track alignment attributes of fields/structs so these values end up being different. Anyway, I'll leave pref alone. |
What |
Oh, since you mentioned it: there is one invariant you need to take into account, and that's |
Yes, I'm in the process of padding fields now. Adding the padding fields should mean the sizes match what LLVM expects but unless I'm mistaken it won't help when comparing what alignment it expects for a field, since I think that's based on the type of the field, which is something we can't tell LLVM about as far as I'm aware. |
What needs to be done w.r.t. this feature before it can be put in Rust stable? I'd like to help with the stabilization effort so that it can be stabilized ASAP. Is the stabilization something that can happen in the impl period? |
I'm not aware of anything right now. cc @alexcrichton @retep998 |
Personally I'd like to see |
Do you mean that you want to make sure that |
I am also interested in seeing this stabilized asap, is this stalled or blocked by anything? |
One thing preventing stabilisation of |
Is that the only thing? We could support string arguments to speed this
along.
…On Mon, Nov 27, 2017, 21:15 Cameron Hart ***@***.***> wrote:
One thing preventing stabilisation of repr_align is it's currently
dependant on attr_literals #34981
<#34981> which is also still
unstable.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#33626 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ACLjePtV_vAWb2fIiSRjbOrVtemrk1cyks5s62yvgaJpZM4IedCO>
.
|
Is anyone opposed to having |
Perhaps that was my misunderstanding, I removed the old code when I added support for |
What I remember was a consensus(?) that attributes using the new syntax can be stabilized independently. OTOH, if |
Yes, looking at my original PR I think I misunderstood adding I'll try add it back. As for if there's anything else preventing this from being stabilised, |
To be clear, my position is that we should just stabilize the |
I've created a pull request for stabilization, I still need to update the reference. I wasn't planning on updating the book or rust by example as they don't appear to talk about |
I have not updated the reference Attribute syntax grammar documentation as the new syntax is only used by |
Stabilized `#[repr(align(x))]` attribute (RFC 1358) Stabilzed `#[repr(align(x))]` with attr_literal syntax as proposed by @eddyb #33626 (comment)
Also disable crypto_generichash due to inability to specify alignment (see rust-lang/rust#33626)
I think this can be closed now since the attribute is stable. |
@kennytm can this issue be closed? |
Tracking issue for rust-lang/rfcs#1358
The text was updated successfully, but these errors were encountered: