Skip to content

Timings #2005

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Timings #2005

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
12 changes: 11 additions & 1 deletion compiler/bin-wasm_of_ocaml/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ let link_and_optimize
@@ fun opt_temp_sourcemap ->
(with_runtime_files ~runtime_wasm_files
@@ fun runtime_inputs ->
let t = Timer.make ~get_time:Unix.time () in
Binaryen.link
~inputs:
({ Binaryen.module_name = "env"; file = runtime_file; source_map_file = None }
Expand All @@ -169,13 +170,16 @@ let link_and_optimize
wat_files)
~opt_output_sourcemap:opt_temp_sourcemap
~output_file:temp_file
());
();
if times () then Format.eprintf " binaryen link: %a@." Timer.print t);

Fs.with_intermediate_file (Filename.temp_file "wasm-dce" ".wasm")
@@ fun temp_file' ->
opt_with
Fs.with_intermediate_file
(if enable_source_maps then Some (Filename.temp_file "wasm-dce" ".wasm.map") else None)
@@ fun opt_temp_sourcemap' ->
let t = Timer.make ~get_time:Unix.time () in
let primitives =
Binaryen.dead_code_elimination
~dependencies:Runtime_files.dependencies
Expand All @@ -184,13 +188,16 @@ let link_and_optimize
~input_file:temp_file
~output_file:temp_file'
in
if times () then Format.eprintf " binaryen dce: %a@." Timer.print t;
let t = Timer.make ~get_time:Unix.time () in
Binaryen.optimize
~profile
~opt_input_sourcemap:opt_temp_sourcemap'
~opt_output_sourcemap:opt_sourcemap
~input_file:temp_file'
~output_file
();
if times () then Format.eprintf " binaryen opt: %a@." Timer.print t;
Option.iter
~f:(update_sourcemap ~sourcemap_root ~sourcemap_don't_inline_content)
opt_sourcemap_file;
Expand Down Expand Up @@ -533,6 +540,7 @@ let run
~opt_source_map_file
in
let tmp_wasm_file = Filename.concat tmp_dir "code.wasm" in
let t2 = Timer.make ~get_time:Unix.time () in
let primitives =
link_and_optimize
~profile
Expand All @@ -543,6 +551,7 @@ let run
[ input_wasm_file, opt_source_map_file ]
tmp_wasm_file
in
if times () then Format.eprintf " link_and_optimize: %a@." Timer.print t2;
let wasm_name =
Printf.sprintf
"code-%s"
Expand All @@ -556,6 +565,7 @@ let run
Link.Wasm_binary.append_source_map_section
~file:tmp_wasm_file'
~url:(wasm_name ^ ".wasm.map"));
if times () then Format.eprintf "Start building js runtime@.";
let js_runtime =
let missing_primitives =
let l = Link.Wasm_binary.read_imports ~file:tmp_wasm_file' in
Expand Down
1 change: 1 addition & 0 deletions compiler/bin-wasm_of_ocaml/dune
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
compiler-libs.common
js_of_ocaml-compiler.runtime-files
yojson
unix
(select
findlib_support.ml
from
Expand Down
8 changes: 6 additions & 2 deletions compiler/lib-wasm/binaryen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ open Stdlib

let debug = Debug.find "binaryen"

let times = Debug.find "binaryen-times"

let command cmdline =
let cmdline = String.concat ~sep:" " cmdline in
if debug () then Format.eprintf "+ %s@." cmdline;
let res = Sys.command cmdline in
let res = Sys.command ((if times () then "BINARYEN_PASS_DEBUG=1 " else "") ^ cmdline) in
if res <> 0 then failwith ("the following command terminated unsuccessfully: " ^ cmdline)

let common_options () =
Expand All @@ -38,7 +40,9 @@ let common_options () =
; "--enable-strings"
]
in
if Config.Flag.pretty () then "-g" :: l else l
let l = if Config.Flag.pretty () then "-g" :: l else l in
let l = if times () then "--no-validation" :: l else l in
l

let opt_flag flag v =
match v with
Expand Down
2 changes: 2 additions & 0 deletions compiler/lib-wasm/generate.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1318,11 +1318,13 @@ let add_init_function = G.add_init_function
let output ch ~context =
let t = Timer.make () in
let fields = G.output ~context in
if times () then Format.eprintf " fields: %a@." Timer.print t;
Wat_output.f ch fields;
if times () then Format.eprintf " output: %a@." Timer.print t

let wasm_output ch ~opt_source_map_file ~context =
let t = Timer.make () in
let fields = G.output ~context in
if times () then Format.eprintf " fields: %a@." Timer.print t;
Wasm_output.f ch ~opt_source_map_file fields;
if times () then Format.eprintf " output: %a@." Timer.print t
20 changes: 10 additions & 10 deletions compiler/lib-wasm/wat_output.ml
Original file line number Diff line number Diff line change
Expand Up @@ -666,10 +666,13 @@ let field ctx st f =
| Type [ t ] -> [ type_field st t ]
| Type l -> [ List (Atom "rec" :: List.map ~f:(type_field st) l) ]

let times = Debug.find "times"

let f ch fields =
let t = Timer.make () in
let st = build_name_tables fields in
let ctx = { function_refs = Code.Var.Set.empty } in
let other_fields = List.concat (List.map ~f:(fun f -> field ctx st f) fields) in
let other_fields = List.concat_map ~f:(fun f -> field ctx st f) fields in
let funct_decl =
let functions = Code.Var.Set.elements ctx.function_refs in
if List.is_empty functions
Expand All @@ -682,12 +685,9 @@ let f ch fields =
:: List.map ~f:(index st.func_names) functions)
]
in
Format.fprintf
(Format.formatter_of_out_channel ch)
"%a@."
format_sexp
(List
(Atom "module"
:: (List.concat (List.map ~f:(fun i -> import st i) fields)
@ funct_decl
@ other_fields)))
let imports = List.concat_map ~f:(fun i -> import st i) fields in
let sexp = List (Atom "module" :: List.concat [ imports; funct_decl; other_fields ]) in
if times () then Format.eprintf " prepare: %a@." Timer.print t;
let t = Timer.make () in
Format.fprintf (Format.formatter_of_out_channel ch) "%a@." format_sexp sexp;
if times () then Format.eprintf " format: %a@." Timer.print t
11 changes: 6 additions & 5 deletions compiler/lib/timer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@

open! Stdlib

type t = float
type t =
{ get_time : unit -> float
; start : float
}

let timer = Sys.time
let make ?(get_time = Sys.time) () = { get_time; start = get_time () }

let make () = timer ()

let get t = timer () -. t
let get t = t.get_time () -. t.start

let print f t = Format.fprintf f "%.2f" (get t)
2 changes: 1 addition & 1 deletion compiler/lib/timer.mli
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

type t

val make : unit -> t
val make : ?get_time:(unit -> float) -> unit -> t

val get : t -> float

Expand Down
Loading