diff --git a/compiler/bin-wasm_of_ocaml/compile.ml b/compiler/bin-wasm_of_ocaml/compile.ml index 1e31b66af..386bebcf0 100644 --- a/compiler/bin-wasm_of_ocaml/compile.ml +++ b/compiler/bin-wasm_of_ocaml/compile.ml @@ -69,26 +69,29 @@ let output_gen output_file f = Code.Var.set_stable (Config.Flag.stable_var ()); Filename.gen_file output_file f -let common_binaryen_options = - [ "--enable-gc" - ; "--enable-multivalue" - ; "--enable-exception-handling" - ; "--enable-reference-types" - ; "--enable-tail-call" - ; "--enable-bulk-memory" - ; "--enable-nontrapping-float-to-int" - ; "--enable-strings" - ; "-g" - ] +let common_binaryen_options () = + let l = + [ "--enable-gc" + ; "--enable-multivalue" + ; "--enable-exception-handling" + ; "--enable-reference-types" + ; "--enable-tail-call" + ; "--enable-bulk-memory" + ; "--enable-nontrapping-float-to-int" + ; "--enable-strings" + ] + in + if Config.Flag.pretty () then "-g" :: l else l let link runtime_files input_file output_file = command - (("wasm-merge" :: common_binaryen_options) - @ List.flatten - (List.map - ~f:(fun runtime_file -> [ Filename.quote runtime_file; "env" ]) - runtime_files) - @ [ Filename.quote input_file; "exec"; "-o"; Filename.quote output_file ]) + ("wasm-merge" + :: (common_binaryen_options () + @ List.flatten + (List.map + ~f:(fun runtime_file -> [ Filename.quote runtime_file; "env" ]) + runtime_files) + @ [ Filename.quote input_file; "exec"; "-o"; Filename.quote output_file ])) let generate_dependencies primitives = Yojson.Basic.to_string @@ -124,28 +127,37 @@ let dead_code_elimination in_file out_file = let primitives = Linker.get_provided () in write_file deps_file (generate_dependencies primitives); command - (("wasm-metadce" :: common_binaryen_options) - @ [ "--graph-file" - ; Filename.quote deps_file - ; Filename.quote in_file - ; "-o" - ; Filename.quote out_file - ; ">" - ; Filename.quote usage_file - ]); + ("wasm-metadce" + :: (common_binaryen_options () + @ [ "--graph-file" + ; Filename.quote deps_file + ; Filename.quote in_file + ; "-o" + ; Filename.quote out_file + ; ">" + ; Filename.quote usage_file + ])); filter_unused_primitives primitives usage_file -let optimize in_file out_file = +let optimization_options = + [| [ "-O2"; "--skip-pass=inlining-optimizing" ] + ; [ "-O2"; "--skip-pass=inlining-optimizing"; "--traps-never-happen" ] + ; [ "-O3"; "--traps-never-happen" ] + |] + +let optimize ~profile in_file out_file = + let level = + match profile with + | None -> 1 + | Some p -> fst (List.find ~f:(fun (_, p') -> Poly.equal p p') Driver.profiles) + in command - (("wasm-opt" :: common_binaryen_options) - @ [ "-O2" - ; "--skip-pass=inlining-optimizing" - ; Filename.quote in_file - ; "-o" - ; Filename.quote out_file - ]) + ("wasm-opt" + :: (common_binaryen_options () + @ optimization_options.(level - 1) + @ [ Filename.quote in_file; "-o"; Filename.quote out_file ])) -let link_and_optimize runtime_wasm_files wat_file output_file = +let link_and_optimize ~profile runtime_wasm_files wat_file output_file = with_intermediate_file (Filename.temp_file "runtime" ".wasm") @@ fun runtime_file -> write_file runtime_file Wa_runtime.wasm_runtime; @@ -155,7 +167,7 @@ let link_and_optimize runtime_wasm_files wat_file output_file = with_intermediate_file (Filename.temp_file "wasm-dce" ".wasm") @@ fun temp_file' -> let primitives = dead_code_elimination temp_file temp_file' in - optimize temp_file' output_file; + optimize ~profile temp_file' output_file; primitives let escape_string s = @@ -351,7 +363,9 @@ let run { Cmd_arg.common; profile; runtime_files; input_file; output_file; param gen_file wasm_file @@ fun tmp_wasm_file -> let strings = output_gen wat_file (output code ~standalone:true) in - let primitives = link_and_optimize runtime_wasm_files wat_file tmp_wasm_file in + let primitives = + link_and_optimize ~profile runtime_wasm_files wat_file tmp_wasm_file + in build_js_runtime primitives strings wasm_file output_file | `Cmo _ | `Cma _ -> assert false); close_ic ());