# Struct alignment implementation questions

**URL:** <https://internals.rust-lang.org/t/struct-alignment-implementation-questions/4629>\
**Category:** Uncategorized\
**Created:** [January 19, 2017, 11:38am UTC](https://internals.rust-lang.org/t/struct-alignment-implementation-questions/4629 "2017-01-19T11:38:05Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bitshifter](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bitshifter/32/3120_2.png) [@bitshifter](https://internals.rust-lang.org/u/bitshifter)\
**Post date:** [January 19, 2017, 11:38am UTC](https://internals.rust-lang.org/t/struct-alignment-implementation-questions/4629/1 "2017-01-19T11:38:05Z")

</div>

I’m attempting to implement the [#[repr(align)] RFC](https://github.com/rust-lang/rfcs/blob/master/text/1358-repr-align.md) which is jumping in the deep end slightly with my level of knowledge about Rust compiler internals. I managed to get parsing an align repr and setting alignment of the struct such that `mem::align_of` returns the right thing for my aligned type. However `mem::size_of` is not returning the aligned size and I’m not totally sure what the correct way to do this is.

If I create this aligned type in C++:

```
struct alignas(16) Align16 {
        int i;
};

```

The IR type generated from Clang is:

```
%struct.Align16 = type { i32, [12 x i8] }

```

My similarly aligned type in Rust is missing the `[12 xi8]`. I wasn’t sure if I’d need to add padding or if LLVM would pad the layout to the alignment, but it seems that it’s something that needs to be handled on the Rust side of things. Does that sound correct, the way to get the type sized correctly would be to add padding bytes to the struct layout? If so, where would be the place to do this in the code? I figure someone here might be able to point me in the right direction 🙂

---

<div class="post-metadata">

**Author:** ![oli-obk](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/oli-obk/32/3168_2.png) [@oli-obk](https://internals.rust-lang.org/u/oli-obk)\
**Post date:** [January 19, 2017, 11:59am UTC](https://internals.rust-lang.org/t/struct-alignment-implementation-questions/4629/2 "2017-01-19T11:59:41Z")

</div>

I think the compiler will take care of it. Probably all you need to do is to check the `repr(align)` attribute at all places where a `ty::Layout` is created, error if the attribute alignment is smaller than the alignment required by the type, and overwrite the alignment, if the attribute alignment is bigger than the required one.

Afaik, anything breaking due to this can probably be considered a bug.

---

<div class="post-metadata">

**Author:** ![bitshifter](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bitshifter/32/3120_2.png) [@bitshifter](https://internals.rust-lang.org/u/bitshifter)\
**Post date:** [January 19, 2017, 12:20pm UTC](https://internals.rust-lang.org/t/struct-alignment-implementation-questions/4629/3 "2017-01-19T12:20:52Z")

</div>

Hmm, I was hoping that would be the case, but it didn’t seem to work. Possibly I’m not adding alignment everywhere I need to. At the moment I check for repr align in `librustc::ty::layout::Struct::new` and set alignment on the returned `Struct` there, the alignment did appear to be picked up but it didn’t seem to result in a correctly sized type. I also tried setting `Struct::min_size` to the alignment value, although I wasn’t sure where that was used, and indeed it didn’t seem to change the size. The change I’ve made so far is [https://github.com/bitshifter/rust/commit/19007861eff40a326f951e6b6159f9a2d7e8b0e3](https://github.com/bitshifter/rust/commit/19007861eff40a326f951e6b6159f9a2d7e8b0e3) plus a unit test in the next commit.

---

<div class="post-metadata">

**Author:** ![bitshifter](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/bitshifter/32/3120_2.png) [@bitshifter](https://internals.rust-lang.org/u/bitshifter)\
**Post date:** [January 19, 2017, 11:40pm UTC](https://internals.rust-lang.org/t/struct-alignment-implementation-questions/4629/4 "2017-01-19T23:40:53Z")

</div>

Added setting min\_size back since that sets the Layout stride correctly, but still doesn’t result in a correct LLVM type. One thing I’m trying to work out is where in the code the corresponding LLVM type is created from the Rust layout type. That might help work out what’s going on.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [March 25, 2019, 8:27am UTC](https://internals.rust-lang.org/t/struct-alignment-implementation-questions/4629/5 "2019-03-25T08:27:43Z")

</div>

Closed on March 25, 2019, 8:27am UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
