Skip to content

Effects: introduce primitive caml_cps_trampoline #1922

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion compiler/lib-wasm/generate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,6 @@ let init () =
let l =
[ "caml_ensure_stack_capacity", "%identity"
; "caml_process_pending_actions_with_root", "%identity"
; "caml_callback", "caml_trampoline"
; "caml_make_array", "caml_array_of_uniform_array"
]
in
Expand Down
14 changes: 7 additions & 7 deletions compiler/lib/effects.ml
Original file line number Diff line number Diff line change
Expand Up @@ -847,7 +847,7 @@ let cps_transform ~live_vars ~flow_info ~cps_needed p =
(* Toplevel code: if we double-translate, no need to handle it
specially: CPS calls in it are like all other CPS calls from
direct code. Otherwise, it needs to wrapped within a
[caml_callback], but only if it performs CPS calls. *)
[caml_cps_trampoline], but only if it performs CPS calls. *)
not (double_translate () || Addr.Set.is_empty blocks_to_transform)
in
if debug ()
Expand Down Expand Up @@ -957,7 +957,7 @@ let cps_transform ~live_vars ~flow_info ~cps_needed p =
match Hashtbl.find_opt closure_info p.start with
| None -> p
| Some (cps_params, cps_cont) ->
(* Call [caml_callback] to set up the execution context. *)
(* Call [caml_cps_trampoline] to set up the execution context. *)
let new_start = p.free_pc in
let blocks =
let main = Var.fresh () in
Expand All @@ -969,7 +969,7 @@ let cps_transform ~live_vars ~flow_info ~cps_needed p =
; body =
[ Let (main, Closure (cps_params, cps_cont))
; Let (args, Prim (Extern "%js_array", []))
; Let (res, Prim (Extern "caml_callback", [ Pv main; Pv args ]))
; Let (res, Prim (Extern "caml_cps_trampoline", [ Pv main; Pv args ]))
]
; branch = Return res
}
Expand All @@ -994,7 +994,7 @@ let wrap_call ~cps_needed p x f args accu =
( p
, Var.Set.remove x cps_needed
, [ Let (arg_array, Prim (Extern "%js_array", List.map ~f:(fun y -> Pv y) args))
; Let (x, Prim (Extern "caml_callback", [ Pv f; Pv arg_array ]))
; Let (x, Prim (Extern "caml_cps_trampoline", [ Pv f; Pv arg_array ]))
]
:: accu )

Expand All @@ -1014,7 +1014,7 @@ let wrap_primitive ~cps_needed (p : program) x e accu =
, let args = Var.fresh () in
[ Let (f, Closure ([], (closure_pc, [])))
; Let (args, Prim (Extern "%js_array", []))
; Let (x, Prim (Extern "caml_callback", [ Pv f; Pv args ]))
; Let (x, Prim (Extern "caml_cps_trampoline", [ Pv f; Pv args ]))
]
:: accu )

Expand All @@ -1026,9 +1026,9 @@ let rewrite_toplevel_instr (p, cps_needed, accu) instr =
wrap_primitive ~cps_needed p x e accu
| _ -> p, cps_needed, [ instr ] :: accu

(* Wrap function calls inside [caml_callback] at toplevel to avoid
(* Wrap function calls inside [caml_cps_trampoline] at toplevel to avoid
unncessary function nestings. This is not done inside loops since
using repeatedly [caml_callback] can be costly. *)
using repeatedly [caml_cps_trampoline] can be costly. *)
let rewrite_toplevel ~cps_needed p =
let { start; blocks; _ } = p in
let cfg = build_graph blocks start in
Expand Down
1 change: 1 addition & 0 deletions runtime/js/jslib.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ var caml_callback = caml_call_gen;
//If: !doubletranslate
//Requires: caml_stack_depth, caml_call_gen, caml_wrap_exception
//Requires: caml_current_stack
//Alias: caml_cps_trampoline
Copy link
Member

Choose a reason for hiding this comment

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

I think this make the CI fail because wasmoo will "resolve" caml_cps_trampoline to caml_callback

function caml_callback(f, args) {
var saved_stack_depth = caml_stack_depth;
var saved_current_stack = caml_current_stack;
Expand Down
2 changes: 1 addition & 1 deletion runtime/wasm/effect.wat
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@
(param (ref eq)) (param (ref eq)) (param (ref eq)) (result (ref eq))
(unreachable))

(func $caml_trampoline (export "caml_trampoline")
(func $caml_trampoline (export "caml_cps_trampoline")
(param $f (ref eq)) (param $vargs (ref eq)) (result (ref eq))
(local $args (ref $block))
(local $i i32) (local $res (ref eq))
Expand Down
Loading