Skip to content
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

better LLVM struct layout for tagged unions (beating the C equivalent!) #2166

Open
andrewrk opened this issue Apr 2, 2019 · 0 comments
Open
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Apr 2, 2019

To see the LLVM IR, use clang -nostdlib -S -g -emit-llvm test.c:

#include <inttypes.h>

// %struct.Foo = type { %union.anon, i8 }
// %union.anon = type { i64, [8 x i8] }
// ABI Size: 24

struct Foo {
    union {
        uint64_t a;
        uint8_t b[9];
    } bar;
    uint8_t c;
};

int main(void) {
    struct Foo foo;
    return 0;
}

For this tagged union, the C version will have ABI size = 24. However, when written in zig:

// %struct.Foo = type { i64, [1 x i8], i8 }
// ABI Size: 16
const Foo = union(enum) {
    a: u64,
    b: [9]u8,
};

A savings of 8 bytes for this case.

This will require implementing the above LLVM type. Currently, Zig matches the equivalent C code.

@andrewrk andrewrk added this to the 1.1.0 milestone Apr 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant