Skip to content

[wasm] Option to implement OCaml strings with JavaScript strings #1772

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

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion .github/workflows/build-wasm_of_ocaml.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
- name: Set-up Node.js
uses: actions/setup-node@v4
with:
node-version: latest
node-version: 'v24.0.0-v8-canary202412116884e26428'

- name: Set-up OCaml ${{ matrix.ocaml-compiler }}
uses: ocaml/setup-ocaml@v3
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ bench:
$(MAKE) -C benchmark-fiat-crypto bench
$(MAKE) -C benchmark-ocamlc bench
$(MAKE) -C benchmark-partial-render-table bench
$(MAKE) -C benchmark-camlboy bench
$(MAKE) -C benchmark-others bench
#$(MAKE) -C benchmark-camlboy bench
$(MAKE) -C benchmark-others bench # To try later!

microbenchmarks:
@date -u +"%FT%TZ - Microbenchmarks: starting"
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/benchmark-fiat-crypto/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

export NAME=Fiat-Crypto

SHELL=/bin/bash -o pipefail
SHELL=/usr/bin/env bash -o pipefail

bench:
@date -u +"%FT%TZ - $(NAME): starting"
Expand All @@ -11,7 +11,7 @@ bench:
@date -u +"%FT%TZ - $(NAME): done"

perform: bedrock2_fiat_crypto.byte
/usr/bin/time -f "%E %R" $(COMPILER) --debug times --source-map $(EXTRA_ARGS) $< -o out.js 2>&1 | \
env time -f "%E %R" $(COMPILER) --debug times --source-map $(EXTRA_ARGS) $< -o out.js 2>&1 | \
ocaml -I +str str.cma ../utils/compilation_metrics.ml $(COMPILER) $(NAME) out.js | \
sh ../utils/aggregate.sh $(KIND)

Expand Down
6 changes: 3 additions & 3 deletions benchmarks/benchmark-ocamlc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

export NAME=Ocamlc

SHELL=/bin/bash -o pipefail
SHELL=/usr/bin/env bash -o pipefail

bench:
@date -u +"%FT%TZ - $(NAME): starting"
Expand All @@ -14,9 +14,9 @@ bench:
ARGS=ml/*.ml ml/*.ml ml/*.ml ml/*.ml ml/*.ml ml/*.ml ml/*.ml ml/*.ml

perform:
/usr/bin/time -f "%E %R" $(COMPILER) --debug times --opt 2 --pretty `which ocamlc.byte` -o $(SCRIPT) 2>&1 | \
env time -f "%E %R" $(COMPILER) --debug times --opt 2 --pretty `which ocamlc.byte` -o $(SCRIPT) 2>&1 | \
ocaml -I +str str.cma ../utils/compilation_metrics.ml $(COMPILER) $(NAME) $(SCRIPT) | \
sh ../utils/aggregate.sh $(KIND)
/usr/bin/time -f '{"compiler": "$(COMPILER)", "time":"%E"}' node $(SCRIPT) -c $(ARGS) 2>&1 | \
env time -f '{"compiler": "$(COMPILER)", "time":"%E"}' node $(SCRIPT) -c $(ARGS) 2>&1 | \
sh ../utils/format_metrics.sh exec | \
sh ../utils/aggregate.sh $(KIND)
10 changes: 5 additions & 5 deletions benchmarks/benchmark-others/bigarrays/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
export NAME=Others
export SUBNAME=bigarrays

SHELL=/bin/bash -o pipefail
SHELL=/usr/bin/env bash -o pipefail

bench:
@date -u +"%FT%TZ - $(NAME)/$(SUBNAME): starting"
ocamlc bench.ml -o bench
$(MAKE) perform COMPILER=js_of_ocaml SCRIPT=bench.js KIND=js
$(MAKE) perform COMPILER=wasm_of_ocaml SCRIPT=bench.wasm.js KIND=wasm
#$(MAKE) perform COMPILER=js_of_ocaml FLAGS= SCRIPT=bench.js KIND=js
$(MAKE) perform COMPILER=wasm_of_ocaml FLAGS="--enable use-js-string" SCRIPT=bench.wasm.js KIND=wasm
@date -u +"%FT%TZ - $(NAME)/$(SUBNAME): done"

perform:
$(COMPILER) --opt 2 --pretty bench -o $(SCRIPT)
/usr/bin/time -f '{"compiler": "$(COMPILER)", "time":"%E"}' node $(SCRIPT) 2>&1 | \
$(COMPILER) $(FLAGS) --opt 2 --pretty bench -o $(SCRIPT)
env time -f '{"compiler": "$(COMPILER)", "time":"%E"}' node $(SCRIPT) 2>&1 | \
sh ../../utils/format_metrics.sh exec | \
sh ../../utils/aggregate.sh $(KIND)
4 changes: 2 additions & 2 deletions benchmarks/benchmark-others/bin_prot/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
export NAME=Others
export SUBNAME=bin_prot

SHELL=/bin/bash -o pipefail
SHELL=/usr/bin/env bash -o pipefail

bench:
@date -u +"%FT%TZ - $(NAME)/$(SUBNAME): starting"
Expand All @@ -14,7 +14,7 @@ bench:
@date -u +"%FT%TZ - $(NAME)/$(SUBNAME): done"

perform:
/usr/bin/time -f '{"compiler": "$(COMPILER)", "time":"%E"}' node $(SCRIPT) 2>&1 1> /dev/null | \
env time -f '{"compiler": "$(COMPILER)", "time":"%E"}' node $(SCRIPT) 2>&1 1> /dev/null | \
tee /dev/stderr | \
sh ../../utils/format_metrics.sh exec | \
sh ../../utils/aggregate.sh $(KIND)
2 changes: 1 addition & 1 deletion benchmarks/benchmark-others/bin_prot/dune
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
(js_of_ocaml
(flags --opt 2))
(wasm_of_ocaml
(flags --opt 2))
(flags --opt 2 --enable use-js-string))
(preprocess
(pps ppx_bin_prot))
(libraries unix))
4 changes: 2 additions & 2 deletions benchmarks/benchmark-others/lexifi-g2pp/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
export NAME=Others
export SUBNAME=lexifi-g2pp

SHELL=/bin/bash -o pipefail
SHELL=/usr/bin/env bash -o pipefail

bench:
@date -u +"%FT%TZ - $(NAME)/$(SUBNAME): starting"
Expand All @@ -13,6 +13,6 @@ bench:
@date -u +"%FT%TZ - $(NAME)/$(SUBNAME): done"

perform:
/usr/bin/time -f '{"compiler": "$(COMPILER)", "time":"%E"}' node $(SCRIPT) 2>&1 1> /dev/null | \
env time -f '{"compiler": "$(COMPILER)", "time":"%E"}' node $(SCRIPT) 2>&1 1> /dev/null | \
sh ../../utils/format_metrics.sh exec | \
sh ../../utils/aggregate.sh $(KIND)
4 changes: 2 additions & 2 deletions benchmarks/benchmark-partial-render-table/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

export NAME=Partial Render Table

SHELL=/bin/bash -o pipefail
SHELL=/usr/bin/env bash -o pipefail

bench:
@date -u +"%FT%TZ - $(NAME): starting"
Expand All @@ -14,7 +14,7 @@ bench:
@date -u +"%FT%TZ - $(NAME): done"

perform:
/usr/bin/time -f "%E %R" $(COMPILER) --debug times --opt 2 --pretty main.bc-for-jsoo -o out.js 2>&1 | \
env time -f "%E %R" $(COMPILER) --debug times --opt 2 --pretty --enable use-js-string main.bc-for-jsoo -o out.js 2>&1 | \
tee /dev/stderr | \
ocaml -I +str str.cma ../utils/compilation_metrics.ml $(COMPILER) "$(NAME)" out.js | \
sh ../utils/aggregate.sh $(KIND)
Expand Down
21 changes: 14 additions & 7 deletions compiler/bin-wasm_of_ocaml/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ let preprocessor_variables () =
| `Disabled | `Jspi -> "jspi"
| `Cps -> "cps"
| `Double_translation -> assert false) )
; "use-js-string", Wat_preprocess.Bool (Config.Flag.use_js_string ())
]

let with_runtime_files ~runtime_wasm_files f =
Expand Down Expand Up @@ -126,6 +127,7 @@ let build_runtime ~runtime_file =
[ "bindings"
; "Math"
; "js"
; "str"
; "wasm:js-string"
; "wasm:text-encoder"
; "wasm:text-decoder"
Expand Down Expand Up @@ -249,25 +251,25 @@ let generate_prelude ~out_file =
Driver.optimize_for_wasm ~profile code
in
let context = Generate.start () in
let _ =
let _, generated_js =
Generate.f
~context
~unit_name:(Some "prelude")
~unit_name:(Some "wasmoo_prelude")
~live_vars:variable_uses
~in_cps
~deadcode_sentinal
~global_flow_data
program
in
Generate.wasm_output ch ~opt_source_map_file:None ~context;
uinfo.provides
uinfo.provides, generated_js

let build_prelude z =
Fs.with_intermediate_file (Filename.temp_file "prelude" ".wasm")
@@ fun prelude_file ->
let predefined_exceptions = generate_prelude ~out_file:prelude_file in
let info = generate_prelude ~out_file:prelude_file in
Zip.add_file z ~name:"prelude.wasm" ~file:prelude_file;
predefined_exceptions
info

let build_js_runtime ~primitives ?runtime_arguments () =
let always_required_js, primitives =
Expand Down Expand Up @@ -443,12 +445,17 @@ let run
let z = Zip.open_out tmp_output_file in
Zip.add_file z ~name:"runtime.wasm" ~file:tmp_wasm_file;
Zip.add_entry z ~name:"runtime.js" ~contents:js_runtime;
let predefined_exceptions = build_prelude z in
let predefined_exceptions, fragments = build_prelude z in
Link.add_info
z
~predefined_exceptions
~build_info:(Build_info.create `Runtime)
~unit_data:[]
~unit_data:
[ { Link.unit_name = "wasmoo_prelude"
; unit_info = Unit_info.empty
; fragments
}
]
();
Zip.close_out z)
else
Expand Down
7 changes: 6 additions & 1 deletion compiler/bin-wasm_of_ocaml/gen/gen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ let check_js_file fname =

let default_flags = []

let interesting_runtimes = [ [ "effects", `S "jspi" ]; [ "effects", `S "cps" ] ]
let interesting_runtimes =
[ [ "effects", `S "jspi"; "use-js-string", `B false ]
; [ "effects", `S "cps"; "use-js-string", `B false ]
; [ "effects", `S "jspi"; "use-js-string", `B true ]
; [ "effects", `S "cps"; "use-js-string", `B true ]
]

let name_runtime standard l =
let flags =
Expand Down
9 changes: 8 additions & 1 deletion compiler/bin-wasm_of_ocaml/link_wasm.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ let options =
in
let build_t input_modules output_file variables allowed_imports common opt merge =
let allowed_imports =
if List.is_empty allowed_imports then None else Some (List.concat allowed_imports)
match allowed_imports with
| [] -> Some [ "str" ]
| _ :: _ as l ->
let l = List.concat l in
if List.mem ~eq:String.equal "str" l then
Some l
else
Some ("str" :: l)
in
`Ok
{ input_modules
Expand Down
Loading