From 4f43210218029ea72f3eecf3495657eb92d1272a Mon Sep 17 00:00:00 2001 From: Richard Feldman Date: Fri, 20 May 2022 22:35:51 -0400 Subject: [PATCH] Use target_info for discriminant alignment --- bindgen/src/types.rs | 19 +++++++++++-------- compiler/builtins/src/bitcode.rs | 1 + 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/bindgen/src/types.rs b/bindgen/src/types.rs index dc2be50694..29c9fa7c13 100644 --- a/bindgen/src/types.rs +++ b/bindgen/src/types.rs @@ -322,7 +322,7 @@ impl RocType { RocType::TagUnion(RocTagUnion::NonRecursive { tags, .. }) => { // The smallest alignment this could possibly have is based on the number of tags - e.g. // 0 tags is an empty union (so, alignment 0), 1-255 tags has a u8 tag (so, alignment 1), etc. - let mut align = align_for_tag_count(tags.len()); + let mut align = align_for_tag_count(tags.len(), target_info); for (_, payloads) in tags { for id in payloads { @@ -338,7 +338,7 @@ impl RocType { // // Unlike a regular tag union, a recursive one also includes a pointer. let ptr_align = target_info.ptr_alignment_bytes(); - let mut align = ptr_align.max(align_for_tag_count(tags.len())); + let mut align = ptr_align.max(align_for_tag_count(tags.len(), target_info)); for (_, payloads) in tags { for id in payloads { @@ -354,7 +354,8 @@ impl RocType { // // Unlike a regular tag union, a recursive one also includes a pointer. let ptr_align = target_info.ptr_alignment_bytes(); - let mut align = ptr_align.max(align_for_tag_count(non_null_tags.len())); + let mut align = + ptr_align.max(align_for_tag_count(non_null_tags.len(), target_info)); for (_, _, payloads) in non_null_tags { for id in payloads { @@ -398,18 +399,20 @@ impl RocType { } } -fn align_for_tag_count(num_tags: usize) -> usize { +/// Returns the alignment of the discriminant based on the target +/// (e.g. on wasm, these are always 4) +fn align_for_tag_count(num_tags: usize, target_info: TargetInfo) -> usize { if num_tags == 0 { // empty tag union 0 } else if num_tags < u8::MAX as usize { - align_of::() + IntWidth::U8.alignment_bytes(target_info) as usize } else if num_tags < u16::MAX as usize { - align_of::() + IntWidth::U16.alignment_bytes(target_info) as usize } else if num_tags < u32::MAX as usize { - align_of::() + IntWidth::U32.alignment_bytes(target_info) as usize } else if num_tags < u64::MAX as usize { - align_of::() + IntWidth::U64.alignment_bytes(target_info) as usize } else { panic!( "Too many tags. You can't have more than {} tags in a tag union!", diff --git a/compiler/builtins/src/bitcode.rs b/compiler/builtins/src/bitcode.rs index a78e562ec2..8862661791 100644 --- a/compiler/builtins/src/bitcode.rs +++ b/compiler/builtins/src/bitcode.rs @@ -99,6 +99,7 @@ impl IntWidth { matches!(self, I8 | I16 | I32 | I64 | I128) } + pub const fn stack_size(&self) -> u32 { use IntWidth::*;