Merge pull request #1885 from rtfeldman/tags-invalid-alignment

fix invalid alignment in tags
This commit is contained in:
Richard Feldman 2021-11-04 19:32:26 -04:00 committed by GitHub
commit 18228ec39f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 15 deletions

View File

@ -724,6 +724,14 @@ impl<'a, 'b> Env<'a, 'b> {
}
}
const fn round_up_to_alignment(width: u32, alignment: u32) -> u32 {
if alignment != 0 && width % alignment > 0 {
width + alignment - (width % alignment)
} else {
width
}
}
impl<'a> Layout<'a> {
fn new_help<'b>(
env: &mut Env<'a, 'b>,
@ -859,11 +867,7 @@ impl<'a> Layout<'a> {
let width = self.stack_size_without_alignment(pointer_size);
let alignment = self.alignment_bytes(pointer_size);
if alignment != 0 && width % alignment > 0 {
width + alignment - (width % alignment)
} else {
width
}
round_up_to_alignment(width, alignment)
}
fn stack_size_without_alignment(&self, pointer_size: u32) -> u32 {
@ -885,6 +889,8 @@ impl<'a> Layout<'a> {
match variant {
NonRecursive(fields) => {
let tag_id_builtin = variant.tag_id_builtin();
fields
.iter()
.map(|tag_layout| {
@ -894,9 +900,10 @@ impl<'a> Layout<'a> {
.sum::<u32>()
})
.max()
.map(|w| round_up_to_alignment(w, tag_id_builtin.alignment_bytes(pointer_size)))
.unwrap_or_default()
// the size of the tag_id
+ variant.tag_id_builtin().stack_size(pointer_size)
+ tag_id_builtin.stack_size(pointer_size)
}
Recursive(_)
@ -924,13 +931,28 @@ impl<'a> Layout<'a> {
use UnionLayout::*;
match variant {
NonRecursive(tags) => tags
NonRecursive(tags) => {
let max_alignment = tags
.iter()
.map(|x| x.iter())
.flatten()
.map(|x| x.alignment_bytes(pointer_size))
.max()
.unwrap_or(0),
.flat_map(|layouts| {
layouts
.iter()
.map(|layout| layout.alignment_bytes(pointer_size))
})
.max();
match max_alignment {
Some(align) => {
let tag_id_builtin = variant.tag_id_builtin();
round_up_to_alignment(
align,
tag_id_builtin.alignment_bytes(pointer_size),
)
}
None => 0,
}
}
Recursive(_)
| NullableWrapped { .. }
| NullableUnwrapped { .. }

View File

@ -5,6 +5,23 @@ use crate::assert_evals_to;
use indoc::indoc;
use roc_std::{RocList, RocStr};
#[test]
fn width_and_alignment_u8_u8() {
use roc_mono::layout::Builtin;
use roc_mono::layout::Layout;
use roc_mono::layout::UnionLayout;
let t = &[Layout::Builtin(Builtin::Int8)] as &[_];
let tt = [t, t];
let layout = Layout::Union(UnionLayout::NonRecursive(&tt));
// at the moment, the tag id uses an I64, so
let ptr_width = 8;
assert_eq!(layout.alignment_bytes(ptr_width), 8);
assert_eq!(layout.stack_size(ptr_width), 16);
}
#[test]
fn applied_tag_nothing_ir() {
assert_evals_to!(

View File

@ -52,6 +52,7 @@ extension RocStr {
}
@_cdecl("main")
func main() {
func main() -> UInt8 {
print(roc__mainForHost_1_exposed().string)
return 0
}