mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 22:34:45 +00:00
refactor, insert reset more intelligently
This commit is contained in:
parent
048326f9a3
commit
7be0349eee
2 changed files with 56 additions and 14 deletions
|
@ -997,29 +997,29 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
|
||||||
} => build_tag(env, scope, union_layout, *tag_id, arguments, None, parent),
|
} => build_tag(env, scope, union_layout, *tag_id, arguments, None, parent),
|
||||||
|
|
||||||
Reset(symbol) => {
|
Reset(symbol) => {
|
||||||
// 1. fetch refcount
|
|
||||||
// 2. if rc == 1, decrement fields, return pointer as opaque pointer
|
|
||||||
// 3. otherwise, return NULL
|
|
||||||
let (tag_ptr, layout) = load_symbol_and_layout(scope, symbol);
|
let (tag_ptr, layout) = load_symbol_and_layout(scope, symbol);
|
||||||
let tag_ptr = tag_ptr.into_pointer_value();
|
let tag_ptr = tag_ptr.into_pointer_value();
|
||||||
let null_ptr = tag_ptr.get_type().const_null();
|
|
||||||
let refcount_ptr = PointerToRefcount::from_ptr_to_data(env, tag_ptr);
|
// reset is only generated for union values
|
||||||
let is_unique = refcount_ptr.is_1(env);
|
let union_layout = match layout {
|
||||||
|
Layout::Union(ul) => ul,
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
|
|
||||||
let ctx = env.context;
|
let ctx = env.context;
|
||||||
let then_block = ctx.append_basic_block(parent, "then");
|
let then_block = ctx.append_basic_block(parent, "then");
|
||||||
let else_block = ctx.append_basic_block(parent, "else");
|
let else_block = ctx.append_basic_block(parent, "else");
|
||||||
let cont_block = ctx.append_basic_block(parent, "cont");
|
let cont_block = ctx.append_basic_block(parent, "cont");
|
||||||
|
|
||||||
|
let refcount_ptr = PointerToRefcount::from_ptr_to_data(env, tag_ptr);
|
||||||
|
let is_unique = refcount_ptr.is_1(env);
|
||||||
|
|
||||||
env.builder
|
env.builder
|
||||||
.build_conditional_branch(is_unique, then_block, else_block);
|
.build_conditional_branch(is_unique, then_block, else_block);
|
||||||
|
|
||||||
let union_layout = match layout {
|
|
||||||
Layout::Union(ul) => ul,
|
|
||||||
_ => unreachable!(),
|
|
||||||
};
|
|
||||||
|
|
||||||
{
|
{
|
||||||
|
// reset, when used on a unique reference, eagerly decrements the components of the
|
||||||
|
// referenced value, and returns the location of the now-invalid cell
|
||||||
env.builder.position_at_end(then_block);
|
env.builder.position_at_end(then_block);
|
||||||
match build_reset(env, layout_ids, union_layout) {
|
match build_reset(env, layout_ids, union_layout) {
|
||||||
Some(reset_function) => {
|
Some(reset_function) => {
|
||||||
|
@ -1040,6 +1040,8 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
|
||||||
env.builder.build_unconditional_branch(cont_block);
|
env.builder.build_unconditional_branch(cont_block);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
|
// If reset is used on a shared, non-reusable reference, it behaves
|
||||||
|
// like dec and returns , which instructs reuse to behave like ctor
|
||||||
env.builder.position_at_end(else_block);
|
env.builder.position_at_end(else_block);
|
||||||
refcount_ptr.decrement(env, layout);
|
refcount_ptr.decrement(env, layout);
|
||||||
env.builder.build_unconditional_branch(cont_block);
|
env.builder.build_unconditional_branch(cont_block);
|
||||||
|
@ -1048,6 +1050,7 @@ pub fn build_exp_expr<'a, 'ctx, 'env>(
|
||||||
env.builder.position_at_end(cont_block);
|
env.builder.position_at_end(cont_block);
|
||||||
let phi = env.builder.build_phi(tag_ptr.get_type(), "branch");
|
let phi = env.builder.build_phi(tag_ptr.get_type(), "branch");
|
||||||
|
|
||||||
|
let null_ptr = tag_ptr.get_type().const_null();
|
||||||
phi.add_incoming(&[(&tag_ptr, then_block), (&null_ptr, else_block)]);
|
phi.add_incoming(&[(&tag_ptr, then_block), (&null_ptr, else_block)]);
|
||||||
|
|
||||||
phi.as_basic_value()
|
phi.as_basic_value()
|
||||||
|
|
|
@ -212,12 +212,51 @@ fn try_function_s<'a, 'i>(
|
||||||
if std::ptr::eq(stmt, new_stmt) || stmt == new_stmt {
|
if std::ptr::eq(stmt, new_stmt) || stmt == new_stmt {
|
||||||
stmt
|
stmt
|
||||||
} else {
|
} else {
|
||||||
let f00 = Stmt::Let(w, Expr::Reset(x), Layout::Union(c.layout), new_stmt);
|
insert_reset(env, w, x, Layout::Union(c.layout), new_stmt)
|
||||||
|
|
||||||
env.arena.alloc(f00)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn insert_reset<'a>(
|
||||||
|
env: &mut Env<'a, '_>,
|
||||||
|
w: Symbol,
|
||||||
|
x: Symbol,
|
||||||
|
layout: Layout<'a>,
|
||||||
|
mut stmt: &'a Stmt<'a>,
|
||||||
|
) -> &'a Stmt<'a> {
|
||||||
|
use crate::ir::Expr::*;
|
||||||
|
|
||||||
|
let mut stack = vec![];
|
||||||
|
|
||||||
|
while let Stmt::Let(symbol, expr, expr_layout, rest) = stmt {
|
||||||
|
match &expr {
|
||||||
|
StructAtIndex { .. } | GetTagId { .. } | UnionAtIndex { .. } => {
|
||||||
|
stack.push((symbol, expr, expr_layout));
|
||||||
|
stmt = rest;
|
||||||
|
}
|
||||||
|
Literal(_)
|
||||||
|
| Call(_)
|
||||||
|
| Tag { .. }
|
||||||
|
| Struct(_)
|
||||||
|
| Array { .. }
|
||||||
|
| EmptyArray
|
||||||
|
| Reuse { .. }
|
||||||
|
| Reset(_)
|
||||||
|
| RuntimeErrorFunction(_) => break,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let reset_expr = Expr::Reset(x);
|
||||||
|
stmt = env.arena.alloc(Stmt::Let(w, reset_expr, layout, stmt));
|
||||||
|
|
||||||
|
for (symbol, expr, expr_layout) in stack.into_iter().rev() {
|
||||||
|
stmt = env
|
||||||
|
.arena
|
||||||
|
.alloc(Stmt::Let(*symbol, expr.clone(), *expr_layout, stmt));
|
||||||
|
}
|
||||||
|
|
||||||
|
stmt
|
||||||
|
}
|
||||||
|
|
||||||
fn function_d_finalize<'a, 'i>(
|
fn function_d_finalize<'a, 'i>(
|
||||||
env: &mut Env<'a, 'i>,
|
env: &mut Env<'a, 'i>,
|
||||||
x: Symbol,
|
x: Symbol,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue