Hash record field name order in generated layouts

Closes #2535

See the referenced issue for longer discussion - here's the synopsis.
Consider this program

```
app "test" provides [ nums ] to "./platform"

alpha = { a: 1, b: 2 }

nums : List U8
nums =
    [
        alpha.a,
        alpha.b,
    ]
```

Here's its IR:

```
procedure : `#UserApp.alpha` {I64, U8}
procedure = `#UserApp.alpha` ():
    let `#UserApp.5` : Builtin(Int(I64)) = 1i64;
    let `#UserApp.6` : Builtin(Int(U8)) = 2i64;
    let `#UserApp.4` : Struct([Builtin(Int(I64)), Builtin(Int(U8))]) = Struct {`#UserApp.5`, `#UserApp.6`};
    ret `#UserApp.4`;

procedure : `#UserApp.nums` List U8
procedure = `#UserApp.nums` ():
    let `#UserApp.7` : Struct([Builtin(Int(I64)), Builtin(Int(U8))]) = CallByName `#UserApp.alpha`;
    let `#UserApp.1` : Builtin(Int(U8)) = StructAtIndex 1 `#UserApp.7`;
    let `#UserApp.3` : Struct([Builtin(Int(I64)), Builtin(Int(U8))]) = CallByName `#UserApp.alpha`;
    let `#UserApp.2` : Builtin(Int(U8)) = StructAtIndex 1 `#UserApp.3`;
    let `#UserApp.0` : Builtin(List(Builtin(Int(U8)))) = Array [`#UserApp.1`, `#UserApp.2`];
    ret `#UserApp.0`;
```

What's happening is that we need to specialize `alpha` twice - once for the
type of a narrowed to a U8, another time for the type of b narrowed to a U8.

We do the specialization for alpha.b first - record fields are sorted by
layout, so we generate a record of type {i64, u8}. But then we go to
specialize alpha.a, but this has the same layout - {i64, u8} - so we reuse
the existing one! So (at least for records), we need to include record field
order associated with the sorted layout fields, so that we don't reuse
monomorphizations like this incorrectly!
This commit is contained in:
ayazhafiz 2022-02-20 19:51:21 -05:00
parent 74daec84df
commit e52d427ac8
22 changed files with 225 additions and 105 deletions

View file

@ -331,7 +331,7 @@ fn jit_to_ast_help<'a, A: ReplApp<'a>>(
Layout::Builtin(other) => {
todo!("add support for rendering builtin {:?} to the REPL", other)
}
Layout::Struct(field_layouts) => {
Layout::Struct { field_layouts, .. } => {
let struct_addr_to_ast = |mem: &'a A::Memory, addr: usize| match content {
Content::Structure(FlatType::Record(fields, _)) => {
Ok(struct_to_ast(env, mem, addr, field_layouts, *fields))
@ -382,7 +382,7 @@ fn jit_to_ast_help<'a, A: ReplApp<'a>>(
};
let fields = [Layout::u64(), *layout];
let layout = Layout::Struct(&fields);
let layout = Layout::struct_no_name_order(&fields);
let result_stack_size = layout.stack_size(env.target_info);
@ -516,7 +516,7 @@ fn addr_to_ast<'a, M: ReplAppMemory>(
str_to_ast(env.arena, arena_str)
}
(_, Layout::Struct(field_layouts)) => match content {
(_, Layout::Struct{field_layouts, ..}) => match content {
Content::Structure(FlatType::Record(fields, _)) => {
struct_to_ast(env, mem, addr, field_layouts, *fields)
}
@ -796,7 +796,7 @@ fn single_tag_union_to_ast<'a, M: ReplAppMemory>(
sequence_of_expr(env, mem, addr, it, WhenRecursive::Unreachable).into_bump_slice()
} else if field_layouts.is_empty() && !payload_vars.is_empty() {
// happens for e.g. `Foo Bar` where unit structures are nested and the inner one is dropped
let it = payload_vars.iter().copied().zip([&Layout::Struct(&[])]);
let it = payload_vars.iter().copied().zip([&Layout::UNIT]);
sequence_of_expr(env, mem, addr, it, WhenRecursive::Unreachable).into_bump_slice()
} else {
unreachable!()
@ -864,7 +864,7 @@ fn struct_to_ast<'a, M: ReplAppMemory>(
env,
mem,
addr,
&Layout::Struct(field_layouts),
&Layout::struct_no_name_order(field_layouts),
WhenRecursive::Unreachable,
inner_content,
),