Skip to content

Commit 0358924

Browse files
committed
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.
1 parent d02ff40 commit 0358924

File tree

4 files changed

+92
-11
lines changed

4 files changed

+92
-11
lines changed

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,35 @@ let with_runtime_files ~runtime_wasm_files f =
8181
in
8282
Wat_preprocess.with_preprocessed_files ~variables:[] ~inputs f
8383

84+
let build_runtime ~runtime_file =
85+
(* Keep this variables in sync with gen/gen.ml *)
86+
let variables = [] in
87+
match
88+
List.find_opt Runtime_files.precompiled_runtimes ~f:(fun (flags, _) ->
89+
assert (
90+
List.length flags = List.length variables
91+
&& List.for_all2 ~f:(fun (k, _) (k', _) -> String.equal k k') flags variables);
92+
Poly.equal flags variables)
93+
with
94+
| Some (_, contents) -> Fs.write_file ~name:runtime_file ~contents
95+
| None ->
96+
let inputs =
97+
List.map
98+
~f:(fun (module_name, contents) ->
99+
{ Wat_preprocess.module_name
100+
; file = module_name ^ ".wat"
101+
; source = Contents contents
102+
})
103+
Runtime_files.wat_files
104+
in
105+
Runtime.build
106+
~link_options:[ "-g" ]
107+
~opt_options:[ "-g"; "-O2" ]
108+
~variables:
109+
(List.map ~f:(fun (k, v) : (_ * Wat_preprocess.value) -> k, Bool v) variables)
110+
~inputs
111+
~output_file:runtime_file
112+
84113
let link_and_optimize
85114
~profile
86115
~sourcemap_root
@@ -99,7 +128,7 @@ let link_and_optimize
99128
let enable_source_maps = Option.is_some opt_sourcemap_file in
100129
Fs.with_intermediate_file (Filename.temp_file "runtime" ".wasm")
101130
@@ fun runtime_file ->
102-
Fs.write_file ~name:runtime_file ~contents:Runtime_files.wasm_runtime;
131+
build_runtime ~runtime_file;
103132
Fs.with_intermediate_file (Filename.temp_file "wasm-merged" ".wasm")
104133
@@ fun temp_file ->
105134
opt_with
@@ -145,7 +174,7 @@ let link_and_optimize
145174

146175
let link_runtime ~profile runtime_wasm_files output_file =
147176
if List.is_empty runtime_wasm_files
148-
then Fs.write_file ~name:output_file ~contents:Runtime_files.wasm_runtime
177+
then build_runtime ~runtime_file:output_file
149178
else
150179
Fs.with_intermediate_file (Filename.temp_file "extra_runtime" ".wasm")
151180
@@ fun extra_runtime ->
@@ -167,7 +196,7 @@ let link_runtime ~profile runtime_wasm_files output_file =
167196
();
168197
Fs.with_intermediate_file (Filename.temp_file "runtime" ".wasm")
169198
@@ fun runtime_file ->
170-
Fs.write_file ~name:runtime_file ~contents:Runtime_files.wasm_runtime;
199+
build_runtime ~runtime_file;
171200
Binaryen.link
172201
~opt_output_sourcemap:None
173202
~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: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,61 @@
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 = [ [] ]
9+
10+
let name_runtime l =
11+
let flags = List.filter_map (fun (k, v) -> if v then Some k else None) l in
12+
String.concat "-" ("runtime" :: (if flags = [] then [ "standard" ] else flags))
13+
^ ".wasm"
14+
15+
let print_flags f flags =
16+
Format.fprintf
17+
f
18+
"@[<2>[ %a ]@]"
19+
(Format.pp_print_list
20+
~pp_sep:(fun f () -> Format.fprintf f ";@ ")
21+
(fun f (k, v) ->
22+
Format.fprintf f "@[\"%s\",@ %s@]" k (if v then "true" else "false")))
23+
flags
24+
325
let () =
426
let () = set_binary_mode_out stdout true in
527
Format.printf
6-
"let wasm_runtime = \"%s\"@."
28+
"let js_runtime = \"%s\"@."
729
(String.escaped (read_file (open_in_bin Sys.argv.(1))));
830
Format.printf
9-
"let js_runtime = \"%s\"@."
31+
"let dependencies = \"%s\"@."
1032
(String.escaped (read_file (open_in_bin Sys.argv.(2))));
33+
let wat_files, runtimes =
34+
List.partition
35+
(fun f -> Filename.check_suffix f ".wat")
36+
(Array.to_list (Array.sub Sys.argv 3 (Array.length Sys.argv - 3)))
37+
in
1138
Format.printf
12-
"let dependencies = \"%s\"@."
13-
(String.escaped (read_file (open_in_bin Sys.argv.(3))))
39+
"let wat_files = [%a]@."
40+
(Format.pp_print_list (fun f file ->
41+
Format.fprintf
42+
f
43+
"\"%s\", \"%s\"; "
44+
Filename.(chop_suffix (basename file) ".wat")
45+
(String.escaped (read_file (open_in_bin file)))))
46+
wat_files;
47+
Format.printf
48+
"let precompiled_runtimes = [%a]@."
49+
(Format.pp_print_list (fun f flags ->
50+
let flags = flags @ default_flags in
51+
let name = name_runtime flags in
52+
match List.find_opt (fun file -> Filename.basename file = name) runtimes with
53+
| None -> failwith ("Missing runtime " ^ name)
54+
| Some file ->
55+
Format.fprintf
56+
f
57+
"%a, \"%s\"; "
58+
print_flags
59+
flags
60+
(String.escaped (read_file (open_in_bin file)))))
61+
interesting_runtimes

runtime/wasm/dune

Lines changed: 5 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))

0 commit comments

Comments
 (0)