mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 13:29:12 +00:00
Make all layouts interned in mono
This commit is contained in:
parent
dc6b7003a8
commit
fa8effd3e8
14 changed files with 932 additions and 866 deletions
|
@ -9,14 +9,14 @@ use crate::ir::{
|
|||
SelfRecursive, Stmt, UpdateModeId,
|
||||
};
|
||||
use crate::layout::{
|
||||
Builtin, LambdaName, Layout, LayoutInterner, Niche, STLayoutInterner, UnionLayout,
|
||||
Builtin, InLayout, LambdaName, Layout, LayoutInterner, Niche, STLayoutInterner, UnionLayout,
|
||||
};
|
||||
|
||||
mod equality;
|
||||
mod refcount;
|
||||
|
||||
const LAYOUT_BOOL: Layout = Layout::Builtin(Builtin::Bool);
|
||||
const LAYOUT_UNIT: Layout = Layout::UNIT;
|
||||
const LAYOUT_BOOL: InLayout = Layout::BOOL;
|
||||
const LAYOUT_UNIT: InLayout = Layout::UNIT;
|
||||
|
||||
const ARG_1: Symbol = Symbol::ARG_1;
|
||||
const ARG_2: Symbol = Symbol::ARG_2;
|
||||
|
@ -43,7 +43,7 @@ impl HelperOp {
|
|||
#[derive(Debug)]
|
||||
struct Specialization<'a> {
|
||||
op: HelperOp,
|
||||
layout: Layout<'a>,
|
||||
layout: InLayout<'a>,
|
||||
symbol: Symbol,
|
||||
proc: Option<Proc<'a>>,
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ pub struct CodeGenHelp<'a> {
|
|||
arena: &'a Bump,
|
||||
home: ModuleId,
|
||||
target_info: TargetInfo,
|
||||
layout_isize: Layout<'a>,
|
||||
layout_isize: InLayout<'a>,
|
||||
union_refcount: UnionLayout<'a>,
|
||||
specializations: Vec<'a, Specialization<'a>>,
|
||||
debug_recursion_depth: usize,
|
||||
|
@ -121,11 +121,11 @@ impl<'a> CodeGenHelp<'a> {
|
|||
&mut self,
|
||||
ident_ids: &mut IdentIds,
|
||||
layout_interner: &mut STLayoutInterner<'a>,
|
||||
layout: Layout<'a>,
|
||||
layout: InLayout<'a>,
|
||||
modify: &ModifyRc,
|
||||
following: &'a Stmt<'a>,
|
||||
) -> (&'a Stmt<'a>, Vec<'a, (Symbol, ProcLayout<'a>)>) {
|
||||
if !refcount::is_rc_implemented_yet(layout_interner, &layout) {
|
||||
if !refcount::is_rc_implemented_yet(layout_interner, layout) {
|
||||
// Just a warning, so we can decouple backend development from refcounting development.
|
||||
// When we are closer to completion, we can change it to a panic.
|
||||
println!(
|
||||
|
@ -166,7 +166,7 @@ impl<'a> CodeGenHelp<'a> {
|
|||
&mut self,
|
||||
ident_ids: &mut IdentIds,
|
||||
layout_interner: &mut STLayoutInterner<'a>,
|
||||
layout: Layout<'a>,
|
||||
layout: InLayout<'a>,
|
||||
argument: Symbol,
|
||||
) -> (Expr<'a>, Vec<'a, (Symbol, ProcLayout<'a>)>) {
|
||||
let mut ctx = Context {
|
||||
|
@ -178,7 +178,7 @@ impl<'a> CodeGenHelp<'a> {
|
|||
let proc_name = self.find_or_create_proc(ident_ids, &mut ctx, layout_interner, layout);
|
||||
|
||||
let arguments = self.arena.alloc([argument]);
|
||||
let ret_layout = self.arena.alloc(layout);
|
||||
let ret_layout = layout;
|
||||
let arg_layouts = self.arena.alloc([layout]);
|
||||
let expr = Expr::Call(Call {
|
||||
call_type: CallType::ByName {
|
||||
|
@ -200,7 +200,7 @@ impl<'a> CodeGenHelp<'a> {
|
|||
&mut self,
|
||||
ident_ids: &mut IdentIds,
|
||||
layout_interner: &mut STLayoutInterner<'a>,
|
||||
layout: Layout<'a>,
|
||||
layout: InLayout<'a>,
|
||||
op: HelperOp,
|
||||
) -> (Symbol, Vec<'a, (Symbol, ProcLayout<'a>)>) {
|
||||
let mut ctx = Context {
|
||||
|
@ -220,7 +220,7 @@ impl<'a> CodeGenHelp<'a> {
|
|||
&mut self,
|
||||
ident_ids: &mut IdentIds,
|
||||
layout_interner: &mut STLayoutInterner<'a>,
|
||||
layout: &Layout<'a>,
|
||||
layout: InLayout<'a>,
|
||||
arguments: &'a [Symbol],
|
||||
) -> (Expr<'a>, Vec<'a, (Symbol, ProcLayout<'a>)>) {
|
||||
let mut ctx = Context {
|
||||
|
@ -230,7 +230,7 @@ impl<'a> CodeGenHelp<'a> {
|
|||
};
|
||||
|
||||
let expr = self
|
||||
.call_specialized_op(ident_ids, &mut ctx, layout_interner, *layout, arguments)
|
||||
.call_specialized_op(ident_ids, &mut ctx, layout_interner, layout, arguments)
|
||||
.unwrap();
|
||||
|
||||
(expr, ctx.new_linker_data)
|
||||
|
@ -247,7 +247,7 @@ impl<'a> CodeGenHelp<'a> {
|
|||
ident_ids: &mut IdentIds,
|
||||
ctx: &mut Context<'a>,
|
||||
layout_interner: &mut STLayoutInterner<'a>,
|
||||
called_layout: Layout<'a>,
|
||||
called_layout: InLayout<'a>,
|
||||
arguments: &'a [Symbol],
|
||||
) -> Option<Expr<'a>> {
|
||||
use HelperOp::*;
|
||||
|
@ -255,23 +255,23 @@ impl<'a> CodeGenHelp<'a> {
|
|||
// debug_assert!(self.debug_recursion_depth < 100);
|
||||
self.debug_recursion_depth += 1;
|
||||
|
||||
let layout = if matches!(called_layout, Layout::RecursivePointer) {
|
||||
let layout = if matches!(layout_interner.get(called_layout), Layout::RecursivePointer) {
|
||||
let union_layout = ctx.recursive_union.unwrap();
|
||||
Layout::Union(union_layout)
|
||||
layout_interner.insert(Layout::Union(union_layout))
|
||||
} else {
|
||||
called_layout
|
||||
};
|
||||
|
||||
if layout_needs_helper_proc(&layout, ctx.op) {
|
||||
if layout_needs_helper_proc(layout_interner, layout, ctx.op) {
|
||||
let proc_name = self.find_or_create_proc(ident_ids, ctx, layout_interner, layout);
|
||||
|
||||
let (ret_layout, arg_layouts): (&'a Layout<'a>, &'a [Layout<'a>]) = {
|
||||
let (ret_layout, arg_layouts): (InLayout<'a>, &'a [InLayout<'a>]) = {
|
||||
let arg = self.replace_rec_ptr(ctx, layout_interner, layout);
|
||||
match ctx.op {
|
||||
Dec | DecRef(_) => (&LAYOUT_UNIT, self.arena.alloc([arg])),
|
||||
Reset => (self.arena.alloc(layout), self.arena.alloc([layout])),
|
||||
Inc => (&LAYOUT_UNIT, self.arena.alloc([arg, self.layout_isize])),
|
||||
Eq => (&LAYOUT_BOOL, self.arena.alloc([arg, arg])),
|
||||
Dec | DecRef(_) => (LAYOUT_UNIT, self.arena.alloc([arg])),
|
||||
Reset => (layout, self.arena.alloc([layout])),
|
||||
Inc => (LAYOUT_UNIT, self.arena.alloc([arg, self.layout_isize])),
|
||||
Eq => (LAYOUT_BOOL, self.arena.alloc([arg, arg])),
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -302,7 +302,7 @@ impl<'a> CodeGenHelp<'a> {
|
|||
ident_ids: &mut IdentIds,
|
||||
ctx: &mut Context<'a>,
|
||||
layout_interner: &mut STLayoutInterner<'a>,
|
||||
orig_layout: Layout<'a>,
|
||||
orig_layout: InLayout<'a>,
|
||||
) -> Symbol {
|
||||
use HelperOp::*;
|
||||
|
||||
|
@ -320,7 +320,7 @@ impl<'a> CodeGenHelp<'a> {
|
|||
// Procs can be recursive, so we need to create the symbol before the body is complete
|
||||
// But with nested recursion, that means Symbols and Procs can end up in different orders.
|
||||
// We want the same order, especially for function indices in Wasm. So create an empty slot and fill it in later.
|
||||
let (proc_symbol, proc_layout) = self.create_proc_symbol(ident_ids, ctx, &layout);
|
||||
let (proc_symbol, proc_layout) = self.create_proc_symbol(ident_ids, ctx, layout);
|
||||
ctx.new_linker_data.push((proc_symbol, proc_layout));
|
||||
let spec_index = self.specializations.len();
|
||||
self.specializations.push(Specialization {
|
||||
|
@ -360,7 +360,7 @@ impl<'a> CodeGenHelp<'a> {
|
|||
),
|
||||
};
|
||||
|
||||
let args: &'a [(Layout<'a>, Symbol)] = {
|
||||
let args: &'a [(InLayout<'a>, Symbol)] = {
|
||||
let roc_value = (layout, ARG_1);
|
||||
match ctx.op {
|
||||
Inc => {
|
||||
|
@ -390,7 +390,7 @@ impl<'a> CodeGenHelp<'a> {
|
|||
&self,
|
||||
ident_ids: &mut IdentIds,
|
||||
ctx: &mut Context<'a>,
|
||||
layout: &Layout<'a>,
|
||||
layout: InLayout<'a>,
|
||||
) -> (Symbol, ProcLayout<'a>) {
|
||||
let debug_name = format!(
|
||||
"#help{}_{:?}_{:?}",
|
||||
|
@ -403,23 +403,23 @@ impl<'a> CodeGenHelp<'a> {
|
|||
|
||||
let proc_layout = match ctx.op {
|
||||
HelperOp::Inc => ProcLayout {
|
||||
arguments: self.arena.alloc([*layout, self.layout_isize]),
|
||||
arguments: self.arena.alloc([layout, self.layout_isize]),
|
||||
result: LAYOUT_UNIT,
|
||||
niche: Niche::NONE,
|
||||
},
|
||||
HelperOp::Dec => ProcLayout {
|
||||
arguments: self.arena.alloc([*layout]),
|
||||
arguments: self.arena.alloc([layout]),
|
||||
result: LAYOUT_UNIT,
|
||||
niche: Niche::NONE,
|
||||
},
|
||||
HelperOp::Reset => ProcLayout {
|
||||
arguments: self.arena.alloc([*layout]),
|
||||
result: *layout,
|
||||
arguments: self.arena.alloc([layout]),
|
||||
result: layout,
|
||||
niche: Niche::NONE,
|
||||
},
|
||||
HelperOp::DecRef(_) => unreachable!("No generated Proc for DecRef"),
|
||||
HelperOp::Eq => ProcLayout {
|
||||
arguments: self.arena.alloc([*layout, *layout]),
|
||||
arguments: self.arena.alloc([layout, layout]),
|
||||
result: LAYOUT_BOOL,
|
||||
niche: Niche::NONE,
|
||||
},
|
||||
|
@ -442,16 +442,15 @@ impl<'a> CodeGenHelp<'a> {
|
|||
&mut self,
|
||||
ctx: &Context<'a>,
|
||||
layout_interner: &mut STLayoutInterner<'a>,
|
||||
layout: Layout<'a>,
|
||||
) -> Layout<'a> {
|
||||
match layout {
|
||||
layout: InLayout<'a>,
|
||||
) -> InLayout<'a> {
|
||||
let layout = match layout_interner.get(layout) {
|
||||
Layout::Builtin(Builtin::List(v)) => {
|
||||
let v = self.replace_rec_ptr(ctx, layout_interner, layout_interner.get(v));
|
||||
let v = layout_interner.insert(v);
|
||||
let v = self.replace_rec_ptr(ctx, layout_interner, v);
|
||||
Layout::Builtin(Builtin::List(v))
|
||||
}
|
||||
|
||||
Layout::Builtin(_) => layout,
|
||||
Layout::Builtin(_) => return layout,
|
||||
|
||||
Layout::Struct {
|
||||
field_layouts,
|
||||
|
@ -482,54 +481,53 @@ impl<'a> CodeGenHelp<'a> {
|
|||
Layout::Union(_) => {
|
||||
// we always fully unroll recursive types. That means tha when we find a
|
||||
// recursive tag union we can replace it with the layout
|
||||
layout
|
||||
return layout;
|
||||
}
|
||||
|
||||
Layout::Boxed(inner) => {
|
||||
let inner = layout_interner.get(inner);
|
||||
let inner = self.replace_rec_ptr(ctx, layout_interner, inner);
|
||||
let inner = layout_interner.insert(inner);
|
||||
Layout::Boxed(inner)
|
||||
}
|
||||
|
||||
Layout::LambdaSet(lambda_set) => self.replace_rec_ptr(
|
||||
ctx,
|
||||
layout_interner,
|
||||
lambda_set.runtime_representation(layout_interner),
|
||||
),
|
||||
Layout::LambdaSet(lambda_set) => {
|
||||
return self.replace_rec_ptr(ctx, layout_interner, lambda_set.representation)
|
||||
}
|
||||
|
||||
// This line is the whole point of the function
|
||||
Layout::RecursivePointer => Layout::Union(ctx.recursive_union.unwrap()),
|
||||
}
|
||||
};
|
||||
layout_interner.insert(layout)
|
||||
}
|
||||
|
||||
fn union_tail_recursion_fields(
|
||||
&self,
|
||||
layout_interner: &STLayoutInterner<'a>,
|
||||
union: UnionLayout<'a>,
|
||||
) -> (bool, Vec<'a, Option<usize>>) {
|
||||
use UnionLayout::*;
|
||||
match union {
|
||||
NonRecursive(_) => (false, bumpalo::vec![in self.arena]),
|
||||
|
||||
Recursive(tags) => self.union_tail_recursion_fields_help(tags),
|
||||
Recursive(tags) => self.union_tail_recursion_fields_help(layout_interner, tags),
|
||||
|
||||
NonNullableUnwrapped(field_layouts) => {
|
||||
self.union_tail_recursion_fields_help(&[field_layouts])
|
||||
self.union_tail_recursion_fields_help(layout_interner, &[field_layouts])
|
||||
}
|
||||
|
||||
NullableWrapped {
|
||||
other_tags: tags, ..
|
||||
} => self.union_tail_recursion_fields_help(tags),
|
||||
} => self.union_tail_recursion_fields_help(layout_interner, tags),
|
||||
|
||||
NullableUnwrapped { other_fields, .. } => {
|
||||
self.union_tail_recursion_fields_help(&[other_fields])
|
||||
self.union_tail_recursion_fields_help(layout_interner, &[other_fields])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn union_tail_recursion_fields_help(
|
||||
&self,
|
||||
tags: &[&'a [Layout<'a>]],
|
||||
layout_interner: &STLayoutInterner<'a>,
|
||||
tags: &[&'a [InLayout<'a>]],
|
||||
) -> (bool, Vec<'a, Option<usize>>) {
|
||||
let mut can_use_tailrec = false;
|
||||
let mut tailrec_indices = Vec::with_capacity_in(tags.len(), self.arena);
|
||||
|
@ -537,7 +535,7 @@ impl<'a> CodeGenHelp<'a> {
|
|||
for fields in tags.iter() {
|
||||
let found_index = fields
|
||||
.iter()
|
||||
.position(|f| matches!(f, Layout::RecursivePointer));
|
||||
.position(|f| matches!(layout_interner.get(*f), Layout::RecursivePointer));
|
||||
tailrec_indices.push(found_index);
|
||||
can_use_tailrec |= found_index.is_some();
|
||||
}
|
||||
|
@ -548,7 +546,7 @@ impl<'a> CodeGenHelp<'a> {
|
|||
|
||||
fn let_lowlevel<'a>(
|
||||
arena: &'a Bump,
|
||||
result_layout: Layout<'a>,
|
||||
result_layout: InLayout<'a>,
|
||||
result: Symbol,
|
||||
op: LowLevel,
|
||||
arguments: &[Symbol],
|
||||
|
@ -568,8 +566,12 @@ fn let_lowlevel<'a>(
|
|||
)
|
||||
}
|
||||
|
||||
fn layout_needs_helper_proc(layout: &Layout, op: HelperOp) -> bool {
|
||||
match layout {
|
||||
fn layout_needs_helper_proc<'a>(
|
||||
layout_interner: &STLayoutInterner<'a>,
|
||||
layout: InLayout<'a>,
|
||||
op: HelperOp,
|
||||
) -> bool {
|
||||
match layout_interner.get(layout) {
|
||||
Layout::Builtin(Builtin::Int(_) | Builtin::Float(_) | Builtin::Bool | Builtin::Decimal) => {
|
||||
false
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue