Make all layouts interned in mono

This commit is contained in:
Ayaz Hafiz 2023-01-03 19:49:07 -06:00
parent dc6b7003a8
commit fa8effd3e8
No known key found for this signature in database
GPG key ID: 0E2A37416A25EF58
14 changed files with 932 additions and 866 deletions

View file

@ -20,9 +20,9 @@ pub fn eq_generic<'a>(
ident_ids: &mut IdentIds,
ctx: &mut Context<'a>,
layout_interner: &mut STLayoutInterner<'a>,
layout: Layout<'a>,
layout: InLayout<'a>,
) -> Stmt<'a> {
let main_body = match layout {
let main_body = match layout_interner.get(layout) {
Layout::Builtin(Builtin::Int(_) | Builtin::Float(_) | Builtin::Bool | Builtin::Decimal) => {
unreachable!(
"No generated proc for `==`. Use direct code gen for {:?}",
@ -142,7 +142,7 @@ fn eq_struct<'a>(
ident_ids: &mut IdentIds,
ctx: &mut Context<'a>,
layout_interner: &mut STLayoutInterner<'a>,
field_layouts: &'a [Layout<'a>],
field_layouts: &'a [InLayout<'a>],
) -> Stmt<'a> {
let mut else_stmt = Stmt::Ret(Symbol::BOOL_TRUE);
for (i, layout) in field_layouts.iter().enumerate().rev() {
@ -277,7 +277,7 @@ fn eq_tag_union_help<'a>(
ctx: &mut Context<'a>,
layout_interner: &mut STLayoutInterner<'a>,
union_layout: UnionLayout<'a>,
tag_layouts: &'a [&'a [Layout<'a>]],
tag_layouts: &'a [&'a [InLayout<'a>]],
nullable_id: Option<TagIdIntType>,
) -> Stmt<'a> {
let tailrec_loop = JoinPointId(root.create_symbol(ident_ids, "tailrec_loop"));
@ -417,10 +417,11 @@ fn eq_tag_union_help<'a>(
if is_non_recursive {
compare_ptr_or_value
} else {
let union_layout = layout_interner.insert(Layout::Union(union_layout));
let loop_params_iter = operands.iter().map(|arg| Param {
symbol: *arg,
ownership: Ownership::Borrowed,
layout: Layout::Union(union_layout),
layout: union_layout,
});
let loop_start = Stmt::Jump(tailrec_loop, root.arena.alloc([ARG_1, ARG_2]));
@ -442,7 +443,7 @@ fn eq_tag_fields<'a>(
layout_interner: &mut STLayoutInterner<'a>,
tailrec_loop: JoinPointId,
union_layout: UnionLayout<'a>,
field_layouts: &'a [Layout<'a>],
field_layouts: &'a [InLayout<'a>],
operands: [Symbol; 2],
tag_id: TagIdIntType,
) -> Stmt<'a> {
@ -450,7 +451,7 @@ fn eq_tag_fields<'a>(
// (If there are more than one, the others will use non-tail recursion)
let rec_ptr_index = field_layouts
.iter()
.position(|field| matches!(field, Layout::RecursivePointer));
.position(|field| matches!(layout_interner.get(*field), Layout::RecursivePointer));
let (tailrec_index, innermost_stmt) = match rec_ptr_index {
None => {
@ -579,8 +580,6 @@ fn eq_boxed<'a>(
layout_interner: &mut STLayoutInterner<'a>,
inner_layout: InLayout<'a>,
) -> Stmt<'a> {
let inner_layout = layout_interner.get(inner_layout);
let a = root.create_symbol(ident_ids, "a");
let b = root.create_symbol(ident_ids, "b");
let result = root.create_symbol(ident_ids, "result");
@ -638,11 +637,9 @@ fn eq_list<'a>(
let layout_isize = root.layout_isize;
let arena = root.arena;
let elem_layout = layout_interner.get(elem_layout);
// A "Box" layout (heap pointer to a single list element)
let box_union_layout = UnionLayout::NonNullableUnwrapped(root.arena.alloc([elem_layout]));
let box_layout = Layout::Union(box_union_layout);
let box_layout = layout_interner.insert(Layout::Union(box_union_layout));
// Compare lengths
@ -687,7 +684,10 @@ fn eq_list<'a>(
// let size = literal int
let size = root.create_symbol(ident_ids, "size");
let size_expr = Expr::Literal(Literal::Int(
(elem_layout.stack_size(layout_interner, root.target_info) as i128).to_ne_bytes(),
(layout_interner
.get(elem_layout)
.stack_size(layout_interner, root.target_info) as i128)
.to_ne_bytes(),
));
let size_stmt = |next| Stmt::Let(size, size_expr, layout_isize, next);

View file

@ -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
}

View file

@ -1,7 +1,6 @@
#![allow(clippy::too_many_arguments)]
use bumpalo::collections::vec::Vec;
use roc_builtins::bitcode::IntWidth;
use roc_module::low_level::{LowLevel, LowLevel::*};
use roc_module::symbol::{IdentIds, Symbol};
use roc_target::PtrWidth;
@ -17,19 +16,20 @@ use crate::layout::{
use super::{CodeGenHelp, Context, HelperOp};
const LAYOUT_BOOL: Layout = Layout::Builtin(Builtin::Bool);
const LAYOUT_UNIT: Layout = Layout::UNIT;
const LAYOUT_U32: Layout = Layout::Builtin(Builtin::Int(IntWidth::U32));
const LAYOUT_BOOL: InLayout = Layout::BOOL;
const LAYOUT_UNIT: InLayout = Layout::UNIT;
const LAYOUT_U32: InLayout = Layout::U32;
// TODO: Replace usages with root.union_refcount
const LAYOUT_PTR: Layout = Layout::RecursivePointer;
// TODO(recursive-layouts): update once we have disjoint recursive pointers
const LAYOUT_PTR: InLayout = Layout::RECURSIVE_PTR;
pub fn refcount_stmt<'a>(
root: &mut CodeGenHelp<'a>,
ident_ids: &mut IdentIds,
ctx: &mut Context<'a>,
layout_interner: &mut STLayoutInterner<'a>,
layout: Layout<'a>,
layout: InLayout<'a>,
modify: &ModifyRc,
following: &'a Stmt<'a>,
) -> &'a Stmt<'a> {
@ -77,7 +77,7 @@ pub fn refcount_stmt<'a>(
}
ModifyRc::DecRef(structure) => {
match layout {
match layout_interner.get(layout) {
// Str has no children, so we might as well do what we normally do and call the helper.
Layout::Builtin(Builtin::Str) => {
ctx.op = HelperOp::Dec;
@ -128,12 +128,12 @@ pub fn refcount_generic<'a>(
ident_ids: &mut IdentIds,
ctx: &mut Context<'a>,
layout_interner: &mut STLayoutInterner<'a>,
layout: Layout<'a>,
layout: InLayout<'a>,
structure: Symbol,
) -> Stmt<'a> {
debug_assert!(is_rc_implemented_yet(layout_interner, &layout));
debug_assert!(is_rc_implemented_yet(layout_interner, layout));
match layout {
match layout_interner.get(layout) {
Layout::Builtin(Builtin::Int(_) | Builtin::Float(_) | Builtin::Bool | Builtin::Decimal) => {
// Generate a dummy function that immediately returns Unit
// Some higher-order Zig builtins *always* call an RC function on List elements.
@ -145,7 +145,7 @@ pub fn refcount_generic<'a>(
ident_ids,
ctx,
layout_interner,
&layout,
layout,
elem_layout,
structure,
),
@ -166,7 +166,7 @@ pub fn refcount_generic<'a>(
structure,
),
Layout::LambdaSet(lambda_set) => {
let runtime_layout = lambda_set.runtime_representation(layout_interner);
let runtime_layout = lambda_set.representation;
refcount_generic(
root,
ident_ids,
@ -179,18 +179,15 @@ pub fn refcount_generic<'a>(
Layout::RecursivePointer => unreachable!(
"We should never call a refcounting helper on a RecursivePointer layout directly"
),
Layout::Boxed(inner_layout) => {
let inner_layout = layout_interner.get(inner_layout);
refcount_boxed(
root,
ident_ids,
ctx,
layout_interner,
&layout,
&inner_layout,
structure,
)
}
Layout::Boxed(inner_layout) => refcount_boxed(
root,
ident_ids,
ctx,
layout_interner,
layout,
inner_layout,
structure,
),
}
}
@ -199,7 +196,7 @@ pub fn refcount_reset_proc_body<'a>(
ident_ids: &mut IdentIds,
ctx: &mut Context<'a>,
layout_interner: &mut STLayoutInterner<'a>,
layout: Layout<'a>,
layout: InLayout<'a>,
structure: Symbol,
) -> Stmt<'a> {
let rc_ptr = root.create_symbol(ident_ids, "rc_ptr");
@ -208,7 +205,7 @@ pub fn refcount_reset_proc_body<'a>(
let is_unique = root.create_symbol(ident_ids, "is_unique");
let addr = root.create_symbol(ident_ids, "addr");
let union_layout = match layout {
let union_layout = match layout_interner.get(layout) {
Layout::Union(u) => u,
_ => unimplemented!("Reset is only implemented for UnionLayout"),
};
@ -267,7 +264,10 @@ pub fn refcount_reset_proc_body<'a>(
let alloc_addr_stmt = {
let alignment = root.create_symbol(ident_ids, "alignment");
let alignment_expr = Expr::Literal(Literal::Int(
(layout.alignment_bytes(layout_interner, root.target_info) as i128).to_ne_bytes(),
(layout_interner
.get(layout)
.alignment_bytes(layout_interner, root.target_info) as i128)
.to_ne_bytes(),
));
let alloc_addr = root.create_symbol(ident_ids, "alloc_addr");
let alloc_addr_expr = Expr::Call(Call {
@ -420,41 +420,36 @@ pub fn refcount_reset_proc_body<'a>(
// Check if refcounting is implemented yet. In the long term, this will be deleted.
// In the short term, it helps us to skip refcounting and let it leak, so we can make
// progress incrementally. Kept in sync with generate_procs using assertions.
pub fn is_rc_implemented_yet<'a, I>(interner: &I, layout: &Layout<'a>) -> bool
pub fn is_rc_implemented_yet<'a, I>(interner: &I, layout: InLayout<'a>) -> bool
where
I: LayoutInterner<'a>,
{
use UnionLayout::*;
match layout {
Layout::Builtin(Builtin::List(elem_layout)) => {
let elem_layout = interner.get(*elem_layout);
is_rc_implemented_yet(interner, &elem_layout)
}
match interner.get(layout) {
Layout::Builtin(Builtin::List(elem_layout)) => is_rc_implemented_yet(interner, elem_layout),
Layout::Builtin(_) => true,
Layout::Struct { field_layouts, .. } => field_layouts
.iter()
.all(|l| is_rc_implemented_yet(interner, l)),
.all(|l| is_rc_implemented_yet(interner, *l)),
Layout::Union(union_layout) => match union_layout {
NonRecursive(tags) => tags
.iter()
.all(|fields| fields.iter().all(|l| is_rc_implemented_yet(interner, l))),
.all(|fields| fields.iter().all(|l| is_rc_implemented_yet(interner, *l))),
Recursive(tags) => tags
.iter()
.all(|fields| fields.iter().all(|l| is_rc_implemented_yet(interner, l))),
.all(|fields| fields.iter().all(|l| is_rc_implemented_yet(interner, *l))),
NonNullableUnwrapped(fields) => {
fields.iter().all(|l| is_rc_implemented_yet(interner, l))
fields.iter().all(|l| is_rc_implemented_yet(interner, *l))
}
NullableWrapped { other_tags, .. } => other_tags
.iter()
.all(|fields| fields.iter().all(|l| is_rc_implemented_yet(interner, l))),
.all(|fields| fields.iter().all(|l| is_rc_implemented_yet(interner, *l))),
NullableUnwrapped { other_fields, .. } => other_fields
.iter()
.all(|l| is_rc_implemented_yet(interner, l)),
.all(|l| is_rc_implemented_yet(interner, *l)),
},
Layout::LambdaSet(lambda_set) => {
is_rc_implemented_yet(interner, &lambda_set.runtime_representation(interner))
}
Layout::LambdaSet(lambda_set) => is_rc_implemented_yet(interner, lambda_set.representation),
Layout::RecursivePointer => true,
Layout::Boxed(_) => true,
}
@ -765,18 +760,16 @@ fn refcount_list<'a>(
ident_ids: &mut IdentIds,
ctx: &mut Context<'a>,
layout_interner: &mut STLayoutInterner<'a>,
layout: &Layout,
layout: InLayout,
elem_layout: InLayout<'a>,
structure: Symbol,
) -> Stmt<'a> {
let layout_isize = root.layout_isize;
let arena = root.arena;
let elem_layout = layout_interner.get(elem_layout);
// A "Box" layout (heap pointer to a single list element)
let box_union_layout = UnionLayout::NonNullableUnwrapped(arena.alloc([elem_layout]));
let box_layout = Layout::Union(box_union_layout);
let box_layout = layout_interner.insert(Layout::Union(box_union_layout));
//
// Check if the list is empty
@ -816,7 +809,7 @@ fn refcount_list<'a>(
//
let rc_ptr = root.create_symbol(ident_ids, "rc_ptr");
let alignment = layout.alignment_bytes(layout_interner, root.target_info);
let alignment = layout_interner.alignment_bytes(layout);
let ret_stmt = rc_return_stmt(root, ident_ids, ctx);
let modify_list = modify_refcount(
@ -837,22 +830,23 @@ fn refcount_list<'a>(
arena.alloc(modify_list),
);
let modify_elems_and_list = if elem_layout.is_refcounted() && !ctx.op.is_decref() {
refcount_list_elems(
root,
ident_ids,
ctx,
layout_interner,
&elem_layout,
LAYOUT_UNIT,
box_union_layout,
len,
elements,
get_rc_and_modify_list,
)
} else {
get_rc_and_modify_list
};
let modify_elems_and_list =
if layout_interner.get(elem_layout).is_refcounted() && !ctx.op.is_decref() {
refcount_list_elems(
root,
ident_ids,
ctx,
layout_interner,
elem_layout,
LAYOUT_UNIT,
box_union_layout,
len,
elements,
get_rc_and_modify_list,
)
} else {
get_rc_and_modify_list
};
//
// Do nothing if the list is empty
@ -893,8 +887,8 @@ fn refcount_list_elems<'a>(
ident_ids: &mut IdentIds,
ctx: &mut Context<'a>,
layout_interner: &mut STLayoutInterner<'a>,
elem_layout: &Layout<'a>,
ret_layout: Layout<'a>,
elem_layout: InLayout<'a>,
ret_layout: InLayout<'a>,
box_union_layout: UnionLayout<'a>,
length: Symbol,
elements: Symbol,
@ -915,7 +909,7 @@ fn refcount_list_elems<'a>(
// let size = literal int
let elem_size = root.create_symbol(ident_ids, "elem_size");
let elem_size_expr = Expr::Literal(Literal::Int(
(elem_layout.stack_size(layout_interner, root.target_info) as i128).to_ne_bytes(),
(layout_interner.stack_size(elem_layout) as i128).to_ne_bytes(),
));
let elem_size_stmt = |next| Stmt::Let(elem_size, elem_size_expr, layout_isize, next);
@ -955,7 +949,7 @@ fn refcount_list_elems<'a>(
// Cast integer to box pointer
let box_ptr = root.create_symbol(ident_ids, "box");
let box_layout = Layout::Union(box_union_layout);
let box_layout = layout_interner.insert(Layout::Union(box_union_layout));
let box_stmt = |next| let_lowlevel(arena, box_layout, box_ptr, PtrCast, &[addr], next);
// Dereference the box pointer to get the current element
@ -966,7 +960,7 @@ fn refcount_list_elems<'a>(
tag_id: 0,
index: 0,
};
let elem_stmt = |next| Stmt::Let(elem, elem_expr, *elem_layout, next);
let elem_stmt = |next| Stmt::Let(elem, elem_expr, elem_layout, next);
//
// Modify element refcount
@ -975,7 +969,7 @@ fn refcount_list_elems<'a>(
let mod_elem_unit = root.create_symbol(ident_ids, "mod_elem_unit");
let mod_elem_args = refcount_args(root, ctx, elem);
let mod_elem_expr = root
.call_specialized_op(ident_ids, ctx, layout_interner, *elem_layout, mod_elem_args)
.call_specialized_op(ident_ids, ctx, layout_interner, elem_layout, mod_elem_args)
.unwrap();
let mod_elem_stmt = |next| Stmt::Let(mod_elem_unit, mod_elem_expr, LAYOUT_UNIT, next);
@ -1059,13 +1053,13 @@ fn refcount_struct<'a>(
ident_ids: &mut IdentIds,
ctx: &mut Context<'a>,
layout_interner: &mut STLayoutInterner<'a>,
field_layouts: &'a [Layout<'a>],
field_layouts: &'a [InLayout<'a>],
structure: Symbol,
) -> Stmt<'a> {
let mut stmt = rc_return_stmt(root, ident_ids, ctx);
for (i, field_layout) in field_layouts.iter().enumerate().rev() {
if field_layout.contains_refcounted(layout_interner) {
if layout_interner.contains_refcounted(*field_layout) {
let field_val = root.create_symbol(ident_ids, &format!("field_val_{}", i));
let field_val_expr = Expr::StructAtIndex {
index: i as u64,
@ -1121,7 +1115,7 @@ fn refcount_union<'a>(
),
Recursive(tags) => {
let (is_tailrec, tail_idx) = root.union_tail_recursion_fields(union);
let (is_tailrec, tail_idx) = root.union_tail_recursion_fields(layout_interner, union);
if is_tailrec && !ctx.op.is_decref() {
refcount_union_tailrec(
root,
@ -1171,7 +1165,7 @@ fn refcount_union<'a>(
nullable_id,
} => {
let null_id = Some(nullable_id);
let (is_tailrec, tail_idx) = root.union_tail_recursion_fields(union);
let (is_tailrec, tail_idx) = root.union_tail_recursion_fields(layout_interner, union);
if is_tailrec && !ctx.op.is_decref() {
refcount_union_tailrec(
root,
@ -1204,7 +1198,7 @@ fn refcount_union<'a>(
} => {
let null_id = Some(nullable_id as TagIdIntType);
let tags = root.arena.alloc([other_fields]);
let (is_tailrec, tail_idx) = root.union_tail_recursion_fields(union);
let (is_tailrec, tail_idx) = root.union_tail_recursion_fields(layout_interner, union);
if is_tailrec && !ctx.op.is_decref() {
refcount_union_tailrec(
root,
@ -1243,7 +1237,7 @@ fn refcount_union_nonrec<'a>(
ctx: &mut Context<'a>,
layout_interner: &mut STLayoutInterner<'a>,
union_layout: UnionLayout<'a>,
tag_layouts: &'a [&'a [Layout<'a>]],
tag_layouts: &'a [&'a [InLayout<'a>]],
structure: Symbol,
) -> Stmt<'a> {
let tag_id_layout = union_layout.tag_id_layout();
@ -1289,11 +1283,11 @@ fn refcount_union_contents<'a>(
ctx: &mut Context<'a>,
layout_interner: &mut STLayoutInterner<'a>,
union_layout: UnionLayout<'a>,
tag_layouts: &'a [&'a [Layout<'a>]],
tag_layouts: &'a [&'a [InLayout<'a>]],
null_id: Option<TagIdIntType>,
structure: Symbol,
tag_id_sym: Symbol,
tag_id_layout: Layout<'a>,
tag_id_layout: InLayout<'a>,
next_stmt: Stmt<'a>,
) -> Stmt<'a> {
let jp_contents_modified = JoinPointId(root.create_symbol(ident_ids, "jp_contents_modified"));
@ -1358,7 +1352,7 @@ fn refcount_union_rec<'a>(
ctx: &mut Context<'a>,
layout_interner: &mut STLayoutInterner<'a>,
union_layout: UnionLayout<'a>,
tag_layouts: &'a [&'a [Layout<'a>]],
tag_layouts: &'a [&'a [InLayout<'a>]],
null_id: Option<TagIdIntType>,
structure: Symbol,
) -> Stmt<'a> {
@ -1437,7 +1431,7 @@ fn refcount_union_tailrec<'a>(
ctx: &mut Context<'a>,
layout_interner: &mut STLayoutInterner<'a>,
union_layout: UnionLayout<'a>,
tag_layouts: &'a [&'a [Layout<'a>]],
tag_layouts: &'a [&'a [InLayout<'a>]],
null_id: Option<TagIdIntType>,
tailrec_indices: Vec<'a, Option<usize>>,
initial_structure: Symbol,
@ -1445,7 +1439,7 @@ fn refcount_union_tailrec<'a>(
let tailrec_loop = JoinPointId(root.create_symbol(ident_ids, "tailrec_loop"));
let current = root.create_symbol(ident_ids, "current");
let next_ptr = root.create_symbol(ident_ids, "next_ptr");
let layout = Layout::Union(union_layout);
let layout = layout_interner.insert(Layout::Union(union_layout));
let tag_id_layout = union_layout.tag_id_layout();
@ -1490,7 +1484,7 @@ fn refcount_union_tailrec<'a>(
)
};
let alignment = layout.alignment_bytes(layout_interner, root.target_info);
let alignment = layout_interner.alignment_bytes(layout);
let modify_structure_stmt = modify_refcount(
root,
ident_ids,
@ -1621,10 +1615,11 @@ fn refcount_union_tailrec<'a>(
));
let loop_init = Stmt::Jump(tailrec_loop, root.arena.alloc([initial_structure]));
let union_layout = layout_interner.insert(Layout::Union(union_layout));
let loop_param = Param {
symbol: current,
ownership: Ownership::Borrowed,
layout: Layout::Union(union_layout),
layout: union_layout,
};
Stmt::Join {
@ -1641,7 +1636,7 @@ fn refcount_tag_fields<'a>(
ctx: &mut Context<'a>,
layout_interner: &mut STLayoutInterner<'a>,
union_layout: UnionLayout<'a>,
field_layouts: &'a [Layout<'a>],
field_layouts: &'a [InLayout<'a>],
structure: Symbol,
tag_id: TagIdIntType,
following: Stmt<'a>,
@ -1649,7 +1644,7 @@ fn refcount_tag_fields<'a>(
let mut stmt = following;
for (i, field_layout) in field_layouts.iter().enumerate().rev() {
if field_layout.contains_refcounted(layout_interner) {
if layout_interner.contains_refcounted(*field_layout) {
let field_val = root.create_symbol(ident_ids, &format!("field_{}_{}", tag_id, i));
let field_val_expr = Expr::UnionAtIndex {
union_layout,
@ -1684,8 +1679,8 @@ fn refcount_boxed<'a>(
ident_ids: &mut IdentIds,
ctx: &mut Context<'a>,
layout_interner: &mut STLayoutInterner<'a>,
layout: &Layout<'a>,
inner_layout: &Layout<'a>,
layout: InLayout<'a>,
inner_layout: InLayout<'a>,
outer: Symbol,
) -> Stmt<'a> {
let arena = root.arena;
@ -1697,7 +1692,7 @@ fn refcount_boxed<'a>(
//
let rc_ptr = root.create_symbol(ident_ids, "rc_ptr");
let alignment = layout.alignment_bytes(layout_interner, root.target_info);
let alignment = layout_interner.alignment_bytes(layout);
let ret_stmt = rc_return_stmt(root, ident_ids, ctx);
let modify_outer = modify_refcount(
root,
@ -1717,7 +1712,7 @@ fn refcount_boxed<'a>(
arena.alloc(modify_outer),
);
if inner_layout.is_refcounted() && !ctx.op.is_decref() {
if layout_interner.is_refcounted(inner_layout) && !ctx.op.is_decref() {
let inner = root.create_symbol(ident_ids, "inner");
let inner_expr = Expr::ExprUnbox { symbol: outer };
@ -1728,7 +1723,7 @@ fn refcount_boxed<'a>(
ident_ids,
ctx,
layout_interner,
*inner_layout,
inner_layout,
mod_inner_args,
)
.unwrap();
@ -1736,7 +1731,7 @@ fn refcount_boxed<'a>(
Stmt::Let(
inner,
inner_expr,
*inner_layout,
inner_layout,
arena.alloc(Stmt::Let(
mod_inner_unit,
mod_inner_expr,