Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve n-ary continuation coercion #4615

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/codegen/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -10590,8 +10590,7 @@ and compile_prim_invocation (env : E.t) ae p es at =
let call_as_prim = match fun_sr, sort with
| SR.Const (_, Const.Fun (mk_fi, Const.PrimWrapper prim)), _ ->
begin match n_args, e2.it with
| 0, _ -> true
| 1, _ -> true
| (0 | 1), _ -> true
| n, PrimE (TupPrim, es) when List.length es = n -> true
| _, _ -> false
end
Expand Down
11 changes: 2 additions & 9 deletions src/ir_passes/async.ml
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,9 @@ let new_nary_async_reply ts =
(* construct the n-ary async value, coercing the continuation, if necessary *)
let nary_async =
let coerce u =
let v = fresh_var "v" u in
let k = fresh_var "k" (contT u T.unit) in
let r = fresh_var "r" (err_contT T.unit) in
[k; r] -->* (
varE unary_async -*-
(tupE [
[v] -->* (varE k -*- varE v);
varE r
])
)
varE (var "@coerce_and_cont" (unary_async --> ([k; fail] -->* (varE unary_async -*- tupE [varE unary_fulfill; varE fail])) |> typ))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually this is for 0-ary continuations (impedance matching to unary, with unit)

Can we improve below?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, we really get pairs etc., not only unit. Fixing...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there something wrong with the original code? The replacement actually seems way more convoluted. I would revert, tbh.

-*- varE unary_async
in
match ts with
| [t1] ->
Expand Down
4 changes: 2 additions & 2 deletions src/ir_passes/await.ml
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ and t_comp_unit context = function
let e = fresh_var "e" T.catch in
ProgU [
funcD throw e (assertE (falseE ()));
expD (c_block context' ds (tupE []) (meta (T.unit) (fun v1 -> tupE [])))
expD (c_block context' ds (tupE []) (meta T.unit (fun v1 -> tupE [])))
]
end
| ActorU (as_opt, ds, ids, { meta = m; preupgrade; postupgrade; heartbeat; timer; inspect}, t) ->
Expand All @@ -619,7 +619,7 @@ and t_ignore_throw context exp =
{ (blockE [
funcD throw e (tupE[]);
]
(c_exp context' exp (meta (T.unit) (fun v1 -> tupE []))))
(c_exp context' exp (meta T.unit (fun v1 -> tupE []))))
(* timer logic requires us to preserve any source location,
or timer won't be initialized in compile.ml *)
with at = exp.at
Expand Down
7 changes: 7 additions & 0 deletions src/prelude/internals.mo
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,13 @@ func @getSystemRefund() : @Refund {
return (prim "cyclesRefunded" : () -> Nat) ();
};

func @coerce_and_cont(a : @Async<()>) :
(k : () -> (), r : @Cont<Error>) -> {
#suspend;
#schedule : () -> ()
} =
func(k, r) = a(func() = k(), r);

func @new_async<T <: Any>() : (@Async<T>, @Cont<T>, @Cont<Error>) {
let w_null = func(r : @Refund, t : T) { };
let r_null = func(_ : Error) {};
Expand Down
Loading