make tag union opaque space use more optimal instructions

This commit is contained in:
Folkert 2023-06-14 12:49:07 +02:00 committed by Ayaz Hafiz
parent 5ce4435911
commit 9b0cc2a5fd
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58

View file

@ -260,6 +260,13 @@ pub(crate) struct RocUnion<'ctx> {
tag_type: Option<TagType>,
}
fn is_multiple_of(big: u32, small: u32) -> bool {
match small {
0 => true, // 0 is a multiple of all n, because n * 0 = 0
n => big % n == 0,
}
}
impl<'ctx> RocUnion<'ctx> {
pub const TAG_ID_INDEX: u32 = 2;
pub const TAG_DATA_INDEX: u32 = 1;
@ -272,7 +279,15 @@ impl<'ctx> RocUnion<'ctx> {
tag_type: Option<TagType>,
) -> Self {
let bytes = round_up_to_alignment(data_width, data_align);
let byte_array_type = context.i8_type().array_type(bytes).as_basic_type_enum();
let byte_array_type = if is_multiple_of(bytes, 8) && is_multiple_of(data_align, 8) {
context
.i64_type()
.array_type(bytes / 8)
.as_basic_type_enum()
} else {
context.i8_type().array_type(bytes).as_basic_type_enum()
};
let alignment_array_type = alignment_type(context, data_align)
.array_type(0)