Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upimplement `#[repr(align)]` (tracking issue for RFC 1358) #33626
Comments
nikomatsakis
added
B-RFC-approved
T-lang
B-unstable
labels
May 13, 2016
This comment has been minimized.
This comment has been minimized.
|
#[repr(align="8")]
struct F;
println!("{:p}", Box::new(F));
// prints 0x1? 0x8?cc #27700. |
This comment has been minimized.
This comment has been minimized.
|
@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 |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Anyone else implementing this? I need it so I may give it a go. |
This comment has been minimized.
This comment has been minimized.
mfarrugi
commented
Nov 17, 2016
|
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? |
This comment has been minimized.
This comment has been minimized.
|
@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). |
This comment has been minimized.
This comment has been minimized.
|
@lu-zero Ok let me give it another try. Rustbuild and (hopefully?) incremental compilation have made rustc hacking much less painful... |
This comment has been minimized.
This comment has been minimized.
|
@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. |
This comment has been minimized.
This comment has been minimized.
|
@bitshifter not really, please go ahead! |
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Jan 15, 2017
|
@bitshifter It would be useful to know how this would compose with |
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
|
My changes so far are here https://github.com/bitshifter/rust/tree/struct_align_wip if anyone is interested. Feedback welcome! |
This comment has been minimized.
This comment has been minimized.
|
@bitshifter I believe what was figured out earlier is that you have to specify the alignment on every load and store and alloca manually. |
This comment has been minimized.
This comment has been minimized.
|
Woah! That sounds awful |
This comment has been minimized.
This comment has been minimized.
|
@retep998 ah, at the moment it seems that only |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
|
You should not touch cc @camlorn who almost also got started on this. |
This comment has been minimized.
This comment has been minimized.
|
Oh and the parsing of the data layout string should set both alignments for all primitives. |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
|
What |
This comment has been minimized.
This comment has been minimized.
|
Oh, since you mentioned it: there is one invariant you need to take into account, and that's |
This comment has been minimized.
This comment has been minimized.
|
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. |
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Aug 31, 2017
|
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? |
This comment has been minimized.
This comment has been minimized.
|
I'm not aware of anything right now. cc @alexcrichton @retep998 |
This comment has been minimized.
This comment has been minimized.
|
Personally I'd like to see |
This comment has been minimized.
This comment has been minimized.
briansmith
commented
Aug 31, 2017
Do you mean that you want to make sure that |
matthewjasper
referenced this issue
Oct 31, 2017
Closed
ICE: repr align with extern "C" functions (). #45662
This comment has been minimized.
This comment has been minimized.
mfarrugi
commented
Nov 27, 2017
I am also interested in seeing this stabilized asap, is this stalled or blocked by anything? |
This comment has been minimized.
This comment has been minimized.
|
One thing preventing stabilisation of |
This comment has been minimized.
This comment has been minimized.
mfarrugi
commented
Nov 28, 2017
|
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>
.
|
This comment has been minimized.
This comment has been minimized.
|
Is anyone opposed to having |
This comment has been minimized.
This comment has been minimized.
Perhaps that was my misunderstanding, I removed the old code when I added support for |
This comment has been minimized.
This comment has been minimized.
|
What I remember was a consensus(?) that attributes using the new syntax can be stabilized independently. OTOH, if |
This comment has been minimized.
This comment has been minimized.
|
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, |
This comment has been minimized.
This comment has been minimized.
|
To be clear, my position is that we should just stabilize the |
bitshifter
referenced this issue
Dec 25, 2017
Merged
Stabilized `#[repr(align(x))]` attribute (RFC 1358) #47006
This comment has been minimized.
This comment has been minimized.
|
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 |
bitshifter
referenced this issue
Dec 26, 2017
Merged
Documented the `#[repr(align(x))]` attribute. #182
This comment has been minimized.
This comment has been minimized.
|
I have not updated the reference Attribute syntax grammar documentation as the new syntax is only used by |
bors
added a commit
that referenced
this issue
Jan 24, 2018
bors
added a commit
that referenced
this issue
Jan 25, 2018
Fraser999
added a commit
to Fraser999/rust_sodium
that referenced
this issue
Feb 8, 2018
kennytm
referenced this issue
Feb 12, 2018
Open
#[repr(align(4))] struct has 8 byte alignment #48159
This comment has been minimized.
This comment has been minimized.
|
I think this can be closed now since the attribute is stable. |
This comment has been minimized.
This comment has been minimized.
|
@kennytm can this issue be closed? |
nikomatsakis commentedMay 13, 2016
Tracking issue for rust-lang/rfcs#1358