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

Implement the special repr(C)-non-clike-enum layout #46123

Merged
merged 4 commits into from
Nov 28, 2017

Conversation

Gankra
Copy link
Contributor

@Gankra Gankra commented Nov 20, 2017

This is the second half of rust-lang/rfcs#2195

which specifies that

#[repr(C, u8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
enum MyEnum {
    A(u32),                 // Single primitive value
    B { x: u8, y: i16 },    // Composite, and the offset of `y` depends on tag being internal
    C,                      // Empty
    D(Option<u32>),         // Contains an enum
    E(Duration),            // Contains a struct
}

Has the same layout as

#[repr(C)]
struct MyEnumRepr {
    tag: MyEnumTag,
    payload: MyEnumPayload,
}

#[repr(C)]
#[allow(non_snake_case)]
union MyEnumPayload {
    A: MyEnumVariantA,
    B: MyEnumVariantB,
    D: MyEnumVariantD,
    E: MyEnumVariantE,
}

#[repr(u8)] #[derive(Copy, Clone)] enum MyEnumTag { A, B, C, D, E }
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantA(u32);
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantB {x: u8, y: i16 }
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantD(Option<u32>);
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantE(Duration);

@rust-highfive
Copy link
Collaborator

r? @petrochenkov

(rust_highfive has picked a reviewer for you, use r? to override)

@Gankra
Copy link
Contributor Author

Gankra commented Nov 20, 2017

r? @eddyb

@@ -945,6 +945,10 @@ impl<'a, 'tcx> LayoutDetails {
/// A univariant, but part of an enum.
EnumVariant(Integer),
}

// To be updated further down, if needed.
let enum_union_align = ::std::cell::Cell::new(Align::from_bytes(1, 1).unwrap());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're not using this, are you?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind.

@kennytm kennytm added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Nov 20, 2017
@Gankra
Copy link
Contributor Author

Gankra commented Nov 20, 2017

Changed impl to reflect discussion in IRC. Also fixed a test that was asserting the old behaviour.

@@ -943,8 +943,9 @@ impl<'a, 'tcx> LayoutDetails {
/// A univariant, the last field of which may be coerced to unsized.
MaybeUnsized,
/// A univariant, but part of an enum.
EnumVariant(Integer),
EnumVariant(Size, Align),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rename this to Prefixed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh right, forgot!

@@ -962,13 +963,11 @@ impl<'a, 'tcx> LayoutDetails {
let mut inverse_memory_index: Vec<u32> = (0..fields.len() as u32).collect();

// Anything with repr(C) or repr(packed) doesn't optimize.
let can_optimize = (repr.flags & ReprFlags::IS_UNOPTIMISABLE).is_empty();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I prefer naming this optimize, making it mutable, and &=-ing it below instead.

}
// Round the offset up the union's aggregate alignment.
offset = discr_size.abi_align(union_align);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I meant was that you wouldn't need special discriminant vs union semantics, just applying a size&align prefix.

// repr(C) on an enum tells us to make a (tag, union) layout,
// so we need to compute the alignment of the union here.
// (enum_union_align is checked by univariant_uninterned)
let mut union_align = min_ity.align(dl);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rename this to prefix_align, I guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it's not how aligned the prefix should be, it's how aligned the suffix should be.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's the alignment imposed by the prefix, the logic looking at it need not know why it's chosen the way it is.

// (enum_union_align is checked by univariant_uninterned)
let mut union_align = min_ity.align(dl);
if def.repr.c() {
for fields in variants.iter() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&variants

@Gankra
Copy link
Contributor Author

Gankra commented Nov 20, 2017

Comments addressed

@@ -943,7 +943,7 @@ impl<'a, 'tcx> LayoutDetails {
/// A univariant, the last field of which may be coerced to unsized.
MaybeUnsized,
/// A univariant, but part of an enum.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change the comment to say "with a prefix of an arbitrary size & alignment (e.g. enum tag)" instead of "but part of an enum"?

@@ -1001,12 +998,11 @@ impl<'a, 'tcx> LayoutDetails {

let mut offset = Size::from_bytes(0);

if let StructKind::EnumVariant(discr) = kind {
offset = discr.size();
if let StructKind::Prefixed(discr_size, prefix_align) = kind {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefix_size instead of discr_size should do nicely.

@@ -1558,10 +1554,21 @@ impl<'a, 'tcx> LayoutDetails {
let mut start_align = Align::from_bytes(256, 256).unwrap();
assert_eq!(Integer::for_abi_align(dl, start_align), None);

// repr(C) on an enum tells us to make a (tag, union) layout,
// so we need to compute the alignment of the union here.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment could be expanded to describe how increasing the prefix alignment produces the same variant layouts as if they were separately computed as structs and then had some space inserted in the front.

@eddyb
Copy link
Member

eddyb commented Nov 20, 2017

cc @rust-lang/compiler @rust-lang/lang LGTM, should this wait on the RFC / have a FCP?

@Gankra
Copy link
Contributor Author

Gankra commented Nov 20, 2017

Comments addressed.

@kennytm kennytm added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Nov 23, 2017
@kennytm kennytm added the T-lang Relevant to the language team, which will review and decide on the PR/issue. label Nov 23, 2017
@carols10cents
Copy link
Member

carols10cents commented Nov 27, 2017

eddyb (Eduard-Mihai Burtescu) approved these changes 7 days ago

soooo do I hear an r+ @eddyb? or an fcp merge? :)

@eddyb
Copy link
Member

eddyb commented Nov 27, 2017

@carols10cents But nobody answered...

@nikomatsakis
Copy link
Contributor

hould this wait on the RFC / have a FCP?

I do expect the RFC to be accepted, but I don't believe we have to block on it. I believe the current behavior is undefined, so we are within our rights to adjust it.

FCP might be reasonable, though I think not really necessary, I don't feel like major controversy is expected here.

@eddyb
Copy link
Member

eddyb commented Nov 28, 2017

@bors r+

@bors
Copy link
Contributor

bors commented Nov 28, 2017

📌 Commit 904ccbc has been approved by eddyb

@bors
Copy link
Contributor

bors commented Nov 28, 2017

⌛ Testing commit 904ccbc9c2b1f8f24664a7ef89071738954f0028 with merge 2e0fd271c40ec90c0d6fa5b99929b81f12413d63...

@bors
Copy link
Contributor

bors commented Nov 28, 2017

💔 Test failed - status-travis

@Gankra
Copy link
Contributor Author

Gankra commented Nov 28, 2017

uhhh so apparently

#[repr(u64)]
enum E6 {
    A(u8, u16, u8),
    B(u8, u16, u8)
}

is 16 bytes on i586-gnu-i686-musl, but u64 is only aligned to 4 bytes?

wat

[00:50:11] thread 'main' panicked at 'assertion failed: `(left == right)`
[00:50:11]   left: `16`,
[00:50:11]  right: `12`', /checkout/src/test/run-pass/multiple-reprs.rs:78:4
    assert_eq!(size_of::<E6>(), align_size(12, align_of::<u64>()));

@Gankra
Copy link
Contributor Author

Gankra commented Nov 28, 2017

nevermind, I'm dumb. 6 + 8 != 12. On 64-bit that error didn't matter.

@eddyb
Copy link
Member

eddyb commented Nov 28, 2017

@bors r+

@bors
Copy link
Contributor

bors commented Nov 28, 2017

📌 Commit 0e63d27 has been approved by eddyb

@bors
Copy link
Contributor

bors commented Nov 28, 2017

⌛ Testing commit 0e63d27 with merge 436ac89...

bors added a commit that referenced this pull request Nov 28, 2017
Implement the special repr(C)-non-clike-enum layout

This is the second half of rust-lang/rfcs#2195

which specifies that

```rust
#[repr(C, u8)]
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
enum MyEnum {
    A(u32),                 // Single primitive value
    B { x: u8, y: i16 },    // Composite, and the offset of `y` depends on tag being internal
    C,                      // Empty
    D(Option<u32>),         // Contains an enum
    E(Duration),            // Contains a struct
}
```

Has the same layout as

```rust
#[repr(C)]
struct MyEnumRepr {
    tag: MyEnumTag,
    payload: MyEnumPayload,
}

#[repr(C)]
#[allow(non_snake_case)]
union MyEnumPayload {
    A: MyEnumVariantA,
    B: MyEnumVariantB,
    D: MyEnumVariantD,
    E: MyEnumVariantE,
}

#[repr(u8)] #[derive(Copy, Clone)] enum MyEnumTag { A, B, C, D, E }
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantA(u32);
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantB {x: u8, y: i16 }
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantD(Option<u32>);
#[repr(C)] #[derive(Copy, Clone)] struct MyEnumVariantE(Duration);

```
@bors
Copy link
Contributor

bors commented Nov 28, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: eddyb
Pushing 436ac89 to master...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants