Skip to content

Commit

Permalink
add stuff to runtime, remove imported memory, separate test and bench
Browse files Browse the repository at this point in the history
  • Loading branch information
zapashcanon committed Oct 22, 2024
1 parent 8cb9f8a commit 3c72d43
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 107 deletions.
26 changes: 22 additions & 4 deletions wasm/imports.wat
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
(type $Gen_block (sub (array (mut (ref eq)))))


;; (import "js_runtime" "print_string_mem" (func $print_string_mem (param i32) (param i32)))

(import "js_runtime" "print_string" (func $print_string (param (ref $String))))
(import "js_runtime" "print_string_mem"
(func $print_string_mem (param i32) (param i32)))
(import "js_runtime" "print_endline" (func $print_endline))

(import "js_runtime" "print_i32" (func $print_i32 (param i32)))
Expand All @@ -18,16 +18,20 @@
(import "js_runtime" "flush" (func $flush))
(import "js_runtime" "atan2" (func $atan2 (param f64 f64) (result f64)))
(import "js_runtime" "sin" (func $sin (param f64) (result f64)))
(import "js_runtime" "asin" (func $asin (param f64) (result f64)))
(import "js_runtime" "cos" (func $cos (param f64) (result f64)))

(import "js_runtime" "memory" (memory $mem 1))
(import "js_runtime" "fmod" (func $fmod (param f64 f64) (result f64)))

(import "runtime" "compare_ints"
(func $compare_int (param (ref eq)) (param (ref eq)) (result (ref eq))))

(import "runtime" "string_eq"
(func $string_eq (param $a (ref eq)) (param $b (ref eq)) (result (ref eq))))

;; (import "js_runtime" "memory"
(memory $mem 1)
;; )

(func (export "caml_int64_float_of_bits") (param $x (ref eq)) (result (ref $Float))
(struct.new $Float
(f64.reinterpret_i64
Expand Down Expand Up @@ -83,6 +87,17 @@
call $cos
)

(func $C_asin (export "asin") (param f64) (result f64)
local.get 0
call $asin
)

(func $C_fmod (export "fmod") (param f64 f64) (result f64)
local.get 1
local.get 0
call $fmod
)

;; =====
;; Bytes
;; =====
Expand Down Expand Up @@ -668,11 +683,14 @@

)

(;
(func (export "print_string") (param $a (ref eq)) (result (ref eq))
(call $print_string_mem (i32.const 0)
(call $copy_string (ref.cast (ref $String) (local.get $a))))
(ref.i31 (i32.const 0)))
;)

(func (export "print_endline") (param $a (ref eq)) (result (ref eq))
(call $print_endline)
(ref.i31 (i32.const 0)))
Expand Down
62 changes: 62 additions & 0 deletions wasm/test/bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/bin/sh

set -eu

NODE="node-canary --stack-size=10000"

ulimit -s 20000

#UNSAFE="-unsafe"
UNSAFE=""

bench() {
echo "*** Running ${1}:"

echo ""

# clean-up
rm -rf *.assets *.wasm *.wat *.bc *.js || true 2> /dev/null
# compile native and wasocaml
../../ocamlopt $UNSAFE -O3 ./${2}.ml > /dev/null
# optimize wasocaml code
wasm-opt --enable-gc --enable-reference-types --enable-multivalue --enable-tail-call --enable-nontrapping-float-to-int --traps-never-happen --skip-pass=inlining-optimizing a.out.wasm -o a.out.wasm -O3
# compile bytecode
ocamlc $UNSAFE ./${2}.ml -o a.out.bc > /dev/null
# compile wsoo
wasm_of_ocaml compile --opt=3 ./a.out.bc -o a.wsoo.bc.js > /dev/null
# compile jsoo
js_of_ocaml compile --target-env=nodejs --opt=3 ./a.out.bc -o a.jsoo.bc.js

# bench
hyperfine --runs 3 \
-n "OCaml native" "./a.out" \
-n "Wasocaml" "${NODE} ./main_node.mjs" \
-n "Wsoo" "${NODE} ./a.wsoo.bc.js" \
-n "Jsoo" "${NODE} ./a.jsoo.bc.js" \
-n "Bytecode" "ocamlrun ./a.out.bc" # TODO: use ../../ocamlrun when it'll be produced

echo ""
}


#bench "Almabench" "almabench" # global init must have correct type
#bench "Binary Trees" "binary_trees" # unreachable
#bench "Boyer" "boyer" # unreachable
#bench "Boyer no exceptions" "boyer_no_exc" # unreachable
#bench "Pfannkuchen" "fannkuch" # unreachable
#bench "Pfannkuchen 2" "fannkuch2" # missing "caml_string_notequal" and "caml_lessthan"
#bench "Fast Fourier Transform" "fft" # unreachable
#bench "Hamming" "hamming" # missing value let-rec
#bench "Nucleic" "nucleic" # unreachable
#bench "Ray-Trace" "raytrace" # global init must have correct type
#bench "Splay Tree" "splay" # unreachable

bench "Knuth-Bendix" "kb"
bench "Knuth-Bendix (no exception)" "kb_no_exc"
bench "Soli" "soli"
bench "Fibonacci" "fib"
bench "Binary Decision Diagram" "bdd"
bench "Loop" "loop"
bench "Takc" "takc"
bench "Taku" "taku"
bench "Quicksort" "quicksort"
2 changes: 2 additions & 0 deletions wasm/test/main_node.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const bindings = {
"memory": memory,
"atan2": Math.atan2,
"sin": Math.sin,
"asin": Math.asin,
"fmod": (x, y) => x % y,
"cos": Math.cos,
}

Expand Down
105 changes: 100 additions & 5 deletions wasm/test/test.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,102 @@
#!/bin/sh

../../ocamlopt ./kb.ml
../../ocamlopt ./soli.ml
../../ocamlopt ./fib.ml
# wasm-opt --enable-gc --enable-reference-types --enable-exception-handling --enable-multivalue --enable-tail-call a.out.wasm -o a.out.wasm -O3
python3 -m http.server 8000 --directory .
set -eu

ULIMIT_STACK_SIZE=20000
STACK_SIZE=10000
NODE="node-canary --stack-size=${STACK_SIZE}"

ulimit -s $ULIMIT_STACK_SIZE

#UNSAFE="-unsafe"
UNSAFE=""

bench_native() {
echo -n " OCaml native: "
ocamlopt $UNSAFE -O3 ./${2}.ml > /dev/null
./a.out > output_${2}_ocaml_native.txt
echo "OK"
}

bench_wasocaml_opt() {
echo -n " Wasocaml + wasm-opt (node): "
../../ocamlopt $UNSAFE -O3 ./${2}.ml > /dev/null
wasm-opt --enable-gc --enable-reference-types --enable-multivalue --enable-tail-call --enable-nontrapping-float-to-int --traps-never-happen --skip-pass=inlining-optimizing a.out.wasm -o a.out.wasm -O3
$NODE ./main_node.mjs > output_${2}_wasocaml_opt.txt
diff output_${2}_ocaml_native.txt output_${2}_wasocaml_opt.txt
echo "OK"
}

bench_wasocaml() {
echo -n " Wasocaml (node): "
../../ocamlopt $UNSAFE -O3 ./${2}.ml > /dev/null
$NODE ./main_node.mjs > output_${2}_wasocaml.txt
diff output_${2}_ocaml_native.txt output_${2}_wasocaml.txt
echo "OK"
}

bench_wsoo() {
echo -n " wasm_of_ocaml (node): "
ocamlc $UNSAFE ./${2}.ml > /dev/null
rm -rf a.assets* a.js a.wat || true 2> /dev/null
wasm_of_ocaml compile --opt=3 ./a.out > /dev/null
$NODE ./a.js > output_${2}_wsoo.txt
diff output_${2}_ocaml_native.txt output_${2}_wsoo.txt
echo "OK"
}

bench_jsoo() {
echo -n " js_of_ocaml (node): "
ocamlc $UNSAFE ./${2}.ml > /dev/null
rm -f a.js a.wat || true 2> /dev/null
js_of_ocaml compile --target-env=nodejs --opt=3 ./a.out
$NODE ./a.js > output_${2}_jsoo.txt
diff output_${2}_ocaml_native.txt output_${2}_jsoo.txt
echo "OK"
}

bench_bytecode() {
echo -n " OCaml bytecode: "
ocamlc $UNSAFE ./${2}.ml > /dev/null
ocamlrun ./a.out > output_${2}_bytecode.txt
diff output_${2}_ocaml_native.txt output_${2}_bytecode.txt
echo "OK"
}

bench() {
echo "*** Running ${1}:"

echo ""

bench_native "${1}" "${2}"
bench_wasocaml_opt "${1}" "${2}"
bench_wasocaml "${1}" "${2}"
bench_wsoo "${1}" "${2}"
bench_jsoo "${1}" "${2}"
bench_bytecode "${1}" "${2}"

echo ""
}


#bench "Almabench" "almabench" # global init must have correct type
#bench "Binary Trees" "binary_trees" # unreachable
#bench "Boyer" "boyer" # unreachable
#bench "Boyer no exceptions" "boyer_no_exc" # unreachable
#bench "Pfannkuchen" "fannkuch" # unreachable
#bench "Pfannkuchen 2" "fannkuch2" # missing "caml_string_notequal" and "caml_lessthan"
#bench "Fast Fourier Transform" "fft" # unreachable
#bench "Hamming" "hamming" # missing value let-rec
#bench "Nucleic" "nucleic" # unreachable
#bench "Ray-Trace" "raytrace" # global init must have correct type
#bench "Splay Tree" "splay" # unreachable

bench "Knuth-Bendix" "kb"
bench "Knuth-Bendix (no exception)" "kb_no_exc"
bench "Soli" "soli"
bench "Fibonacci" "fib"
bench "Binary Decision Diagram" "bdd"
bench "Loop" "loop"
bench "Takc" "takc"
bench "Taku" "taku"
bench "Quicksort" "quicksort"
98 changes: 0 additions & 98 deletions wasm/test/test_node.sh

This file was deleted.

0 comments on commit 3c72d43

Please sign in to comment.