Skip to content

Commit 6e4b457

Browse files
authored
Use a specific runtime for CPS-based effects (#1920)
* Support for several Wasm runtimes (depending on flags) The runtimes will either be precompiled for most common flag combinations, or compiled on the flight for unusual combinations. * Use a specific runtime for CPS-based effects
1 parent 967b503 commit 6e4b457

File tree

8 files changed

+247
-138
lines changed

8 files changed

+247
-138
lines changed

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,42 @@ let with_runtime_files ~runtime_wasm_files f =
8383
in
8484
Wat_preprocess.with_preprocessed_files ~variables:[] ~inputs f
8585

86+
let build_runtime ~runtime_file =
87+
(* Keep this variables in sync with gen/gen.ml *)
88+
let variables =
89+
[ ( "effects"
90+
, Wat_preprocess.String
91+
(match Config.effects () with
92+
| `Jspi -> "jspi"
93+
| `Cps -> "cps"
94+
| `Disabled | `Double_translation -> assert false) )
95+
]
96+
in
97+
match
98+
List.find_opt Runtime_files.precompiled_runtimes ~f:(fun (flags, _) ->
99+
assert (
100+
List.length flags = List.length variables
101+
&& List.for_all2 ~f:(fun (k, _) (k', _) -> String.equal k k') flags variables);
102+
Poly.equal flags variables)
103+
with
104+
| Some (_, contents) -> Fs.write_file ~name:runtime_file ~contents
105+
| None ->
106+
let inputs =
107+
List.map
108+
~f:(fun (module_name, contents) ->
109+
{ Wat_preprocess.module_name
110+
; file = module_name ^ ".wat"
111+
; source = Contents contents
112+
})
113+
Runtime_files.wat_files
114+
in
115+
Runtime.build
116+
~link_options:[ "-g" ]
117+
~opt_options:[ "-g"; "-O2" ]
118+
~variables
119+
~inputs
120+
~output_file:runtime_file
121+
86122
let link_and_optimize
87123
~profile
88124
~sourcemap_root
@@ -101,7 +137,7 @@ let link_and_optimize
101137
let enable_source_maps = Option.is_some opt_sourcemap_file in
102138
Fs.with_intermediate_file (Filename.temp_file "runtime" ".wasm")
103139
@@ fun runtime_file ->
104-
Fs.write_file ~name:runtime_file ~contents:Runtime_files.wasm_runtime;
140+
build_runtime ~runtime_file;
105141
Fs.with_intermediate_file (Filename.temp_file "wasm-merged" ".wasm")
106142
@@ fun temp_file ->
107143
opt_with
@@ -147,7 +183,7 @@ let link_and_optimize
147183

148184
let link_runtime ~profile runtime_wasm_files output_file =
149185
if List.is_empty runtime_wasm_files
150-
then Fs.write_file ~name:output_file ~contents:Runtime_files.wasm_runtime
186+
then build_runtime ~runtime_file:output_file
151187
else
152188
Fs.with_intermediate_file (Filename.temp_file "extra_runtime" ".wasm")
153189
@@ fun extra_runtime ->
@@ -169,7 +205,7 @@ let link_runtime ~profile runtime_wasm_files output_file =
169205
();
170206
Fs.with_intermediate_file (Filename.temp_file "runtime" ".wasm")
171207
@@ fun runtime_file ->
172-
Fs.write_file ~name:runtime_file ~contents:Runtime_files.wasm_runtime;
208+
build_runtime ~runtime_file;
173209
Binaryen.link
174210
~opt_output_sourcemap:None
175211
~inputs:

compiler/bin-wasm_of_ocaml/dune

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@
2525
(target runtime_files.ml)
2626
(deps
2727
gen/gen.exe
28-
../../runtime/wasm/runtime.wasm
2928
../../runtime/wasm/runtime.js
30-
../../runtime/wasm/deps.json)
29+
../../runtime/wasm/deps.json
30+
(glob_files ../../runtime/wasm/*.wat)
31+
(glob_files ../../runtime/wasm/runtime-*.wasm))
3132
(action
3233
(with-stdout-to
3334
%{target}
Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,77 @@
11
let read_file ic = really_input_string ic (in_channel_length ic)
22

3+
(* Keep the two variables below in sync with function build_runtime in
4+
../compile.ml *)
5+
6+
let default_flags = []
7+
8+
let interesting_runtimes = [ [ "effects", `S "jspi" ]; [ "effects", `S "cps" ] ]
9+
10+
let name_runtime standard l =
11+
let flags =
12+
List.filter_map
13+
(fun (k, v) ->
14+
match v with
15+
| `S s -> Some s
16+
| `B b -> if b then Some k else None)
17+
l
18+
in
19+
String.concat "-" ("runtime" :: (if standard then [ "standard" ] else flags)) ^ ".wasm"
20+
21+
let print_flags f flags =
22+
Format.fprintf
23+
f
24+
"@[<2>[ %a ]@]"
25+
(Format.pp_print_list
26+
~pp_sep:(fun f () -> Format.fprintf f ";@ ")
27+
(fun f (k, v) ->
28+
Format.fprintf
29+
f
30+
"@[\"%s\",@ %a@]"
31+
k
32+
(fun f v ->
33+
match v with
34+
| `S s -> Format.fprintf f "Wat_preprocess.String \"%s\"" s
35+
| `B b ->
36+
Format.fprintf f "Wat_preprocess.Bool %s" (if b then "true" else "false"))
37+
v))
38+
flags
39+
340
let () =
441
let () = set_binary_mode_out stdout true in
42+
Format.printf "open Wasm_of_ocaml_compiler@.";
543
Format.printf
6-
"let wasm_runtime = \"%s\"@."
44+
"let js_runtime = \"%s\"@."
745
(String.escaped (read_file (open_in_bin Sys.argv.(1))));
846
Format.printf
9-
"let js_runtime = \"%s\"@."
47+
"let dependencies = \"%s\"@."
1048
(String.escaped (read_file (open_in_bin Sys.argv.(2))));
49+
let wat_files, runtimes =
50+
List.partition
51+
(fun f -> Filename.check_suffix f ".wat")
52+
(Array.to_list (Array.sub Sys.argv 3 (Array.length Sys.argv - 3)))
53+
in
1154
Format.printf
12-
"let dependencies = \"%s\"@."
13-
(String.escaped (read_file (open_in_bin Sys.argv.(3))))
55+
"let wat_files = [%a]@."
56+
(Format.pp_print_list (fun f file ->
57+
Format.fprintf
58+
f
59+
"\"%s\", \"%s\"; "
60+
Filename.(chop_suffix (basename file) ".wat")
61+
(String.escaped (read_file (open_in_bin file)))))
62+
wat_files;
63+
Format.printf
64+
"let precompiled_runtimes = [%a]@."
65+
(Format.pp_print_list (fun f (standard, flags) ->
66+
let flags = flags @ default_flags in
67+
let name = name_runtime standard flags in
68+
match List.find_opt (fun file -> Filename.basename file = name) runtimes with
69+
| None -> failwith ("Missing runtime " ^ name)
70+
| Some file ->
71+
Format.fprintf
72+
f
73+
"%a, \"%s\"; "
74+
print_flags
75+
flags
76+
(String.escaped (read_file (open_in_bin file)))))
77+
(List.mapi (fun i flags -> i = 0, flags) interesting_runtimes)

compiler/lib-wasm/gc_target.ml

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1704,18 +1704,6 @@ let post_process_function_body = Initialize_locals.f
17041704

17051705
let entry_point ~toplevel_fun =
17061706
let code =
1707-
let* () =
1708-
match Config.effects () with
1709-
| `Cps | `Double_translation ->
1710-
let* f =
1711-
register_import
1712-
~name:"caml_cps_initialize_effects"
1713-
(Fun { W.params = []; result = [] })
1714-
in
1715-
instr (W.CallInstr (f, []))
1716-
| `Jspi -> return ()
1717-
| `Disabled -> assert false
1718-
in
17191707
let* main =
17201708
register_import
17211709
~name:"caml_main"

compiler/lib-wasm/generate.ml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,9 +1204,6 @@ let init () =
12041204
]
12051205
in
12061206
Primitive.register "caml_array_of_uniform_array" `Mutable None None;
1207-
let l =
1208-
if effects_cps () then ("caml_alloc_stack", "caml_cps_alloc_stack") :: l else l
1209-
in
12101207
List.iter ~f:(fun (nm, nm') -> Primitive.alias nm nm') l
12111208

12121209
(* Make sure we can use [br_table] for switches *)

runtime/wasm/dune

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
(install
22
(section lib)
33
(package wasm_of_ocaml-compiler)
4-
(files runtime.wasm runtime.js))
4+
(files
5+
(glob_files *.wat)
6+
(glob_files runtime-*.wasm)
7+
runtime.js))
58

69
(rule
7-
(target runtime.wasm)
10+
(target runtime-standard.wasm)
811
(deps
912
args
1013
(glob_files *.wat))
@@ -13,6 +16,21 @@
1316
../../compiler/bin-wasm_of_ocaml/wasmoo_link_wasm.exe
1417
--binaryen=-g
1518
--binaryen-opt=-O3
19+
--set=effects=jspi
20+
%{target}
21+
%{read-lines:args})))
22+
23+
(rule
24+
(target runtime-cps.wasm)
25+
(deps
26+
args
27+
(glob_files *.wat))
28+
(action
29+
(run
30+
../../compiler/bin-wasm_of_ocaml/wasmoo_link_wasm.exe
31+
--binaryen=-g
32+
--binaryen-opt=-O3
33+
--set=effects=cps
1634
%{target}
1735
%{read-lines:args})))
1836

0 commit comments

Comments
 (0)