Stream: cranelift

Topic: Representing unions


view this post on Zulip PROMETHIA (Jan 22 2023 at 19:33):

I've been trying to figure out how to most efficiently represent unions, and debating some different approaches. I'm writing off heap-allocation because stackslots can do that but better as far as I can tell. Mainly I'm wondering how unions might be represented with SSA values. Take the simple union f32 | u32; I don't believe either of SSA registers or cranelift_frontend variables can actually represent a union of primitive types, only one specific primitive, and there's no way to change that at runtime afaik. It seems like the only reasonable method (which it appears rustc_codegen_cranelift uses) is to allocate unions on the stack and manipulate them by reference.
Is there anything else I missed, or is that about it?

view this post on Zulip Carlo Kok (Jan 22 2023 at 19:36):

In LLVM i would stack allocate a [i8 x sizeofmafield] and cast it yes.

view this post on Zulip bjorn3 (Jan 22 2023 at 23:51):

You can also choose to specify variables with one type and use bitcast when you want to read or write it as the other type.


Last updated: Nov 22 2024 at 16:03 UTC