# Drop alignment padding fields in type definitions in LLVM IR

**URL:** <https://internals.rust-lang.org/t/drop-alignment-padding-fields-in-type-definitions-in-llvm-ir/11701>\
**Category:** compiler\
**Created:** [January 26, 2020, 12:55am UTC](https://internals.rust-lang.org/t/drop-alignment-padding-fields-in-type-definitions-in-llvm-ir/11701 "2020-01-26T00:55:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sfzhu93](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sfzhu93/32/7874_2.png) [@sfzhu93](https://internals.rust-lang.org/u/sfzhu93)\
**Post date:** [January 26, 2020, 12:55am UTC](https://internals.rust-lang.org/t/drop-alignment-padding-fields-in-type-definitions-in-llvm-ir/11701/1 "2020-01-26T00:55:02Z")

</div>

Given the following code:

```rust
struct Point<'a> {
    x: i32,
    caption: &'a str,
    y: i32,
}

static mut global_var: Point = Point {
    x: 123,
    y: 344,
    caption: "123",
};

```

The generated LLVM IR code contains empty arrays, which are believed to be alignment paddings, according to discussions on Reddit: [https://www.reddit.com/r/rust/comments/et2i62/why\_the\_global\_variables\_in\_llvm\_ir\_is\_generated/](https://www.reddit.com/r/rust/comments/et2i62/why_the_global_variables_in_llvm_ir_is_generated/)

```rust
%Point = type
{
    [0 x i64],
    { [0 x i8]*, i64 },
    [0 x i32],
    i32,
    [0 x i32],
    i32,
    [0 x i32]
}

```

Can I avoid those paddings to make some analyses on LLVM IR easier? To my understand, if an LLVM pass can do this job, it should be able to first identify those paddings, and then correctly rewrite the GEP instructions in the LLVM code.

---

<div class="post-metadata">

**Author:** ![Diggsey](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/diggsey/32/6311_2.png) [@Diggsey](https://internals.rust-lang.org/u/Diggsey)\
**Post date:** [January 26, 2020, 3:10pm UTC](https://internals.rust-lang.org/t/drop-alignment-padding-fields-in-type-definitions-in-llvm-ir/11701/2 "2020-01-26T15:10:29Z")

</div>

It depends on your analysis, but in general no: it's not possible in every case to identify which instructions would need to be modified to mirror the change in layout.

It would be easier to make the analysis cope with padding, or to make the analysis run on a higher-level intermediate representation (eg. MIR) instead of LLVM IR.

---

<div class="post-metadata">

**Author:** ![sfzhu93](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/sfzhu93/32/7874_2.png) [@sfzhu93](https://internals.rust-lang.org/u/sfzhu93)\
**Post date:** [January 26, 2020, 8:44pm UTC](https://internals.rust-lang.org/t/drop-alignment-padding-fields-in-type-definitions-in-llvm-ir/11701/3 "2020-01-26T20:44:06Z")

</div>

Can I do something like adding a compiler option or parameter to avoid them?

---

<div class="post-metadata">

**Author:** ![Ixrec](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/ixrec/32/6754_2.png) [@Ixrec](https://internals.rust-lang.org/u/Ixrec)\
**Post date:** [January 26, 2020, 8:47pm UTC](https://internals.rust-lang.org/t/drop-alignment-padding-fields-in-type-definitions-in-llvm-ir/11701/4 "2020-01-26T20:47:44Z")

</div>

[`#[repr(packed)]` should remove most (all?) padding](https://doc.rust-lang.org/reference/type-layout.html#the-alignment-modifiers). But of course, not everything can be `#[repr(packed)]`, so at best this will only delay the need to make your analysis handle cope with padding or run on a higher-level IR.

---

<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:** [April 25, 2020, 8:47pm UTC](https://internals.rust-lang.org/t/drop-alignment-padding-fields-in-type-definitions-in-llvm-ir/11701/5 "2020-04-25T20:47:52Z")

</div>

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