Stream: compiler development

Topic: glue field sorting


view this post on Zulip Dan G Knutson (Oct 18 2024 at 00:27):

Hi! I'm working with Luke on his roc-ray platform, and ran into either a bug or (more likely) a thing I don't get about roc glue.
If I run this command:
roc glue ~/path/to/roc/crates/glue/src/RustGlue.roc glue/out glue/glue.roc
where glue/glue.rochas this in it:

ThingToCheck : {
    timestampMillis : U64,
    frameCount : U64,
    keys : List U64,
    mouseButtons : List U64,
    mousePosX : F32,
    mousePosY : F32,
}

in the output, there's different versions of ThingToCheck as a rust struct for x86, x86_64, wasm32, etc. These different structs have different orders to their fields, which I understand matters because they're repr(C). For example:

#[derive(Clone, Default, Debug, PartialEq, PartialOrd, )]
#[repr(C)]
pub struct ThingToCheck {
    pub frameCount: u64,
    pub keys: roc_std::RocList<u64>,
    pub mouseButtons: roc_std::RocList<u64>,
    pub mousePosX: f32,
    pub mousePosY: f32,
    pub timestampMillis: u64,
}

// x86_64
#[derive(Clone, Default, Debug, PartialEq, PartialOrd, )]
#[repr(C)]
pub struct ThingToCheck {
    pub frameCount: u64,
    pub keys: roc_std::RocList<u64>,
    pub mouseButtons: roc_std::RocList<u64>,
    pub timestampMillis: u64,
    pub mousePosX: f32,
    pub mousePosY: f32,
}

Is that expected, and should our (currently handwritten not generated) host be using cfg(target_arch = "whatever") to handle this kind of thing?

view this post on Zulip Brendan Hansknecht (Oct 18 2024 at 00:40):

The first one is wasm?

view this post on Zulip Brendan Hansknecht (Oct 18 2024 at 00:41):

Or I guess x86_32.

view this post on Zulip Brendan Hansknecht (Oct 18 2024 at 00:41):

Anyway, I think it is correct

view this post on Zulip Brendan Hansknecht (Oct 18 2024 at 00:41):

Roc sorts by alignment first then by name

view this post on Zulip Brendan Hansknecht (Oct 18 2024 at 00:42):

On x86_32 and wasm32, u64 has an alignment of 32 bits

view this post on Zulip Brendan Hansknecht (Oct 18 2024 at 00:42):

So on those targets all fields have the same alignment. Thus just alphabetical order

view this post on Zulip Brendan Hansknecht (Oct 18 2024 at 00:43):

On x86_64, they have u64 has 64bit alignment. So does List. So they go before the f32s

view this post on Zulip Dan G Knutson (Oct 18 2024 at 00:45):

I'm not familiar with having to think about alignment at all. Am I right to think that this means using a single handwritten version (without cfg(target_arch)) is probably going to end up broken across different compilation targets? Like the data roc gets out of the bytes passed from rust would just be arbitrarily incorrect?

view this post on Zulip Dan G Knutson (Oct 18 2024 at 00:48):

For example, given enough variety in fields, if it was sorted correctly for aarch64 it would necessarily be sorted incorrectly for an x86_64 machine.

view this post on Zulip Luke Boswell (Oct 18 2024 at 00:50):

using a single handwritten version (without cfg(target_arch)) is probably going to end up broken across different compilation targets?

Yes

view this post on Zulip Luke Boswell (Oct 18 2024 at 00:50):

aarch64 it would necessarily be sorted incorrectly for an x86_64 machine.

I think these are the same though, at least that's why i just hand rolled it

view this post on Zulip Luke Boswell (Oct 18 2024 at 00:51):

I think at some point we talked about changing roc to just sort by alphabetical. Is there a reason we wouldn't do this?

view this post on Zulip Dan G Knutson (Oct 18 2024 at 00:51):

ah, gotcha; it only really matters for 32 vs 64, which in practice is likely to mean wasm?

view this post on Zulip Luke Boswell (Oct 18 2024 at 00:52):

That's my poor mans understanding

view this post on Zulip Brendan Hansknecht (Oct 18 2024 at 01:05):

Yeah, that is a good poor mans understanding. Sometimes there can be weird exceptions, but that almost always is the difference.

view this post on Zulip Dan G Knutson (Oct 18 2024 at 01:21):

thanks! that's reassuring.


Last updated: Jul 06 2025 at 12:14 UTC