Skip to content

Commit

Permalink
Merge pull request #5625 from roc-lang/reuse-into-tag
Browse files Browse the repository at this point in the history
Reuse into tag
  • Loading branch information
folkertdev committed Jun 29, 2023
2 parents c4b0a2e + 0308e02 commit 1bede92
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 157 deletions.
9 changes: 2 additions & 7 deletions crates/compiler/alias_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1315,16 +1315,11 @@ fn expr_spec<'a>(
builder.add_unknown_with(block, &[], pointer_type)
}
Call(call) => call_spec(builder, interner, env, block, layout, call),
Reuse {
tag_layout,
tag_id,
arguments,
..
}
| Tag {
Tag {
tag_layout,
tag_id,
arguments,
reuse: _,
} => {
let data_id = build_tuple_value(builder, env, block, arguments)?;

Expand Down
31 changes: 10 additions & 21 deletions crates/compiler/gen_dev/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ impl<'a> LastSeenMap<'a> {

Expr::Call(call) => self.scan_ast_call(call, stmt),

Expr::Tag { arguments, .. } => {
Expr::Tag {
arguments, reuse, ..
} => {
if let Some(ru) = reuse {
self.set_last_seen(ru.symbol, stmt);
}

for sym in *arguments {
self.set_last_seen(*sym, stmt);
}
Expand Down Expand Up @@ -173,14 +179,6 @@ impl<'a> LastSeenMap<'a> {
}
}
}
Expr::Reuse {
symbol, arguments, ..
} => {
self.set_last_seen(*symbol, stmt);
for sym in *arguments {
self.set_last_seen(*sym, stmt);
}
}
Expr::Reset { symbol, .. } | Expr::ResetRef { symbol, .. } => {
self.set_last_seen(*symbol, stmt);
}
Expand Down Expand Up @@ -838,10 +836,11 @@ trait Backend<'a> {
tag_layout,
tag_id,
arguments,
..
reuse,
} => {
self.load_literal_symbols(arguments);
self.tag(sym, arguments, tag_layout, *tag_id, None);
let reuse = reuse.map(|ru| ru.symbol);
self.tag(sym, arguments, tag_layout, *tag_id, reuse);
}
Expr::ExprBox { symbol: value } => {
let element_layout = match self.interner().get_repr(*layout) {
Expand All @@ -861,16 +860,6 @@ trait Backend<'a> {
Expr::NullPointer => {
self.load_literal_i64(sym, 0);
}
Expr::Reuse {
tag_layout,
tag_id,
symbol: reused,
arguments,
..
} => {
self.load_literal_symbols(arguments);
self.tag(sym, arguments, tag_layout, *tag_id, Some(*reused));
}
Expr::Reset { symbol, .. } => {
let layout = *self.layout_map().get(symbol).unwrap();

Expand Down
26 changes: 5 additions & 21 deletions crates/compiler/gen_llvm/src/llvm/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,42 +1064,26 @@ pub(crate) fn build_exp_expr<'a, 'ctx>(
)
.into(),

Reuse {
Tag {
arguments,
tag_layout: union_layout,
tag_id,
symbol,
..
reuse,
} => {
let reset = scope.load_symbol(symbol).into_pointer_value();
let reuse_ptr = reuse.map(|ru| scope.load_symbol(&ru.symbol).into_pointer_value());

build_tag(
env,
layout_interner,
scope,
union_layout,
*tag_id,
arguments,
Some(reset),
reuse_ptr,
parent,
)
}

Tag {
arguments,
tag_layout: union_layout,
tag_id,
..
} => build_tag(
env,
layout_interner,
scope,
union_layout,
*tag_id,
arguments,
None,
parent,
),

ExprBox { symbol } => {
let (value, layout) = scope.load_symbol_and_layout(symbol);
let basic_type =
Expand Down
15 changes: 5 additions & 10 deletions crates/compiler/gen_wasm/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,8 +1104,11 @@ impl<'a, 'r> WasmBackend<'a, 'r> {
tag_layout: union_layout,
tag_id,
arguments,
..
} => self.expr_tag(union_layout, *tag_id, arguments, sym, storage, None),
reuse,
} => {
let reuse = reuse.map(|ru| ru.symbol);
self.expr_tag(union_layout, *tag_id, arguments, sym, storage, reuse)
}

Expr::GetTagId {
structure,
Expand Down Expand Up @@ -1137,14 +1140,6 @@ impl<'a, 'r> WasmBackend<'a, 'r> {

Expr::ExprUnbox { symbol: arg_sym } => self.expr_unbox(sym, *arg_sym),

Expr::Reuse {
tag_layout,
tag_id,
arguments,
symbol: reused,
..
} => self.expr_tag(tag_layout, *tag_id, arguments, sym, storage, Some(*reused)),

Expr::Reset { symbol: arg, .. } => self.expr_reset(*arg, sym, storage),

Expr::ResetRef { symbol: arg, .. } => self.expr_resetref(*arg, sym, storage),
Expand Down
27 changes: 17 additions & 10 deletions crates/compiler/mono/src/borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,22 @@ impl<'a> BorrowInfState<'a> {
// the function must take it as an owned parameter
self.own_args_if_param(&xs);
}
Tag { arguments: xs, .. } | Struct(xs) => {

Struct(xs) => {
self.own_var(z);

// if the used symbol is an argument to the current function,
// the function must take it as an owned parameter
self.own_args_if_param(xs);
}

Tag {
arguments: xs,
reuse,
..
} => {
debug_assert!(reuse.is_none());

self.own_var(z);

// if the used symbol is an argument to the current function,
Expand Down Expand Up @@ -708,15 +723,7 @@ impl<'a> BorrowInfState<'a> {
self.own_var(z);
self.own_var(*x);
}
Reuse {
symbol: x,
arguments: ys,
..
} => {
self.own_var(z);
self.own_var(*x);
self.own_args_if_param(ys);
}

EmptyArray => {
self.own_var(z);
}
Expand Down
22 changes: 7 additions & 15 deletions crates/compiler/mono/src/debug/checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,11 +397,18 @@ impl<'a, 'r> Ctx<'a, 'r> {
tag_layout,
tag_id,
arguments,
reuse,
} => {
let interned_layout = self
.interner
.insert_direct_no_semantic(LayoutRepr::Union(tag_layout));

if let Some(reuse_token) = reuse {
self.check_sym_layout(reuse_token.symbol, interned_layout, UseKind::TagReuse);
}

self.check_tag_expr(interned_layout, tag_layout, tag_id, arguments);

Some(interned_layout)
}
Expr::Struct(syms) => {
Expand Down Expand Up @@ -474,21 +481,6 @@ impl<'a, 'r> Ctx<'a, 'r> {
}
}
}),
&Expr::Reuse {
symbol,
update_tag_id: _,
update_mode: _,
tag_layout,
tag_id: _,
arguments: _,
} => {
let union = self
.interner
.insert_direct_no_semantic(LayoutRepr::Union(tag_layout));
self.check_sym_layout(symbol, union, UseKind::TagReuse);
// TODO also check update arguments
Some(union)
}
&Expr::Reset {
symbol,
update_mode: _,
Expand Down
3 changes: 0 additions & 3 deletions crates/compiler/mono/src/drop_specialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,6 @@ fn specialize_drops_stmt<'a, 'i>(
alloc_let_with_continuation!(environment)
}

Expr::Reuse { .. } => {
alloc_let_with_continuation!(environment)
}
Expr::Reset { .. } => {
// TODO allow to inline this to replace it with resetref
alloc_let_with_continuation!(environment)
Expand Down
4 changes: 2 additions & 2 deletions crates/compiler/mono/src/inc_dec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1093,8 +1093,8 @@ fn insert_refcount_operations_binding<'a>(
}
}
}
Expr::Reuse { .. } | Expr::Reset { .. } | Expr::ResetRef { .. } => {
unreachable!("Reset(ref) and reuse should not exist at this point")
Expr::Reset { .. } | Expr::ResetRef { .. } => {
unreachable!("Reset(ref) should not exist at this point")
}
}
}
Expand Down
Loading

0 comments on commit 1bede92

Please sign in to comment.