Skip to content

Commit c599c2c

Browse files
committed
Compiler: refactor profile handling
1 parent c75b7b7 commit c599c2c

File tree

12 files changed

+108
-58
lines changed

12 files changed

+108
-58
lines changed

compiler/bin-js_of_ocaml/cmd_arg.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ let normalize_effects (effects : [ `Cps | `Double_translation ] option) common :
5353
type t =
5454
{ common : Jsoo_cmdline.Arg.t
5555
; (* compile option *)
56-
profile : Driver.profile option
56+
profile : Profile.t option
5757
; source_map : Source_map.Encoding_spec.t option
5858
; runtime_files : string list
5959
; no_runtime : bool
@@ -127,7 +127,9 @@ let options =
127127
in
128128
let profile =
129129
let doc = "Set optimization profile : [$(docv)]." in
130-
let profile = List.map Driver.profiles ~f:(fun (i, p) -> string_of_int i, p) in
130+
let profile =
131+
List.map Profile.all ~f:(fun p -> string_of_int (Profile.to_int p), p)
132+
in
131133
Arg.(value & opt (some (enum profile)) None & info [ "opt" ] ~docv:"NUM" ~doc)
132134
in
133135
let noruntime =

compiler/bin-js_of_ocaml/cmd_arg.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ open Js_of_ocaml_compiler
2222
type t =
2323
{ common : Jsoo_cmdline.Arg.t
2424
; (* compile option *)
25-
profile : Driver.profile option
25+
profile : Profile.t option
2626
; source_map : Source_map.Encoding_spec.t option
2727
; runtime_files : string list
2828
; no_runtime : bool

compiler/bin-wasm_of_ocaml/cmd_arg.ml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ let normalize_effects (effects : [ `Cps | `Jspi ] option) common : Config.effect
5252
type t =
5353
{ common : Jsoo_cmdline.Arg.t
5454
; (* compile option *)
55-
profile : Driver.profile option
55+
profile : Profile.t option
5656
; runtime_files : string list
5757
; runtime_only : bool
5858
; output_file : string * bool
@@ -80,7 +80,9 @@ let options () =
8080
in
8181
let profile =
8282
let doc = "Set optimization profile : [$(docv)]." in
83-
let profile = List.map Driver.profiles ~f:(fun (i, p) -> string_of_int i, p) in
83+
let profile =
84+
List.map Profile.all ~f:(fun p -> string_of_int (Profile.to_int p), p)
85+
in
8486
Arg.(value & opt (some (enum profile)) None & info [ "opt" ] ~docv:"NUM" ~doc)
8587
in
8688
let linkall =

compiler/bin-wasm_of_ocaml/cmd_arg.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ open Js_of_ocaml_compiler
2121
type t =
2222
{ common : Jsoo_cmdline.Arg.t
2323
; (* compile option *)
24-
profile : Driver.profile option
24+
profile : Profile.t option
2525
; runtime_files : string list
2626
; runtime_only : bool
2727
; output_file : string * bool

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,7 @@ let generate_prelude ~out_file =
232232
Filename.gen_file out_file
233233
@@ fun ch ->
234234
let code, uinfo = Parse_bytecode.predefined_exceptions () in
235-
let profile =
236-
match Driver.profile 1 with
237-
| Some p -> p
238-
| None -> assert false
239-
in
235+
let profile = Profile.O1 in
240236
let Driver.{ program; variable_uses; in_cps; deadcode_sentinal; _ } =
241237
Driver.optimize ~profile code
242238
in
@@ -260,7 +256,7 @@ let build_prelude z =
260256
@@ fun tmp_prelude_file ->
261257
let predefined_exceptions = generate_prelude ~out_file:prelude_file in
262258
Binaryen.optimize
263-
~profile:(Driver.profile 1)
259+
~profile:Profile.O1
264260
~input_file:prelude_file
265261
~output_file:tmp_prelude_file
266262
~opt_input_sourcemap:None
@@ -389,16 +385,15 @@ let run
389385
Warning: Consider passing '-g' option to ocamlc.\n\
390386
%!"
391387
in
388+
let profile =
389+
match profile with
390+
| Some p -> p
391+
| None -> Profile.O1
392+
in
392393
let output (one : Parse_bytecode.one) ~unit_name ch =
393394
check_debug one;
394395
let code = one.code in
395396
let standalone = Option.is_none unit_name in
396-
let profile =
397-
match profile, Driver.profile 1 with
398-
| Some p, _ -> p
399-
| None, Some p -> p
400-
| None, None -> assert false
401-
in
402397
let Driver.{ program; variable_uses; in_cps; deadcode_sentinal; _ } =
403398
Driver.optimize ~profile code
404399
in

compiler/lib-wasm/binaryen.ml

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,10 @@ let dead_code_elimination
110110
@ [ ">"; Filename.quote usage_file ]));
111111
filter_unused_primitives primitives usage_file
112112

113-
let optimization_options =
114-
[| [ "-O2"; "--skip-pass=inlining-optimizing"; "--traps-never-happen" ]
115-
; [ "-O2"; "--skip-pass=inlining-optimizing"; "--traps-never-happen" ]
116-
; [ "-O3"; "--skip-pass=inlining-optimizing"; "--traps-never-happen" ]
117-
|]
113+
let optimization_options : Profile.t -> _ = function
114+
| O1 -> [ "-O2"; "--skip-pass=inlining-optimizing"; "--traps-never-happen" ]
115+
| O2 -> [ "-O2"; "--skip-pass=inlining-optimizing"; "--traps-never-happen" ]
116+
| O3 -> [ "-O3"; "--skip-pass=inlining-optimizing"; "--traps-never-happen" ]
118117

119118
let optimize
120119
~profile
@@ -124,16 +123,12 @@ let optimize
124123
~opt_output_sourcemap
125124
~output_file
126125
() =
127-
let level =
128-
match profile with
129-
| None -> 1
130-
| Some p ->
131-
fst (List.find ~f:(fun (_, p') -> Driver.profile_equal p p') Driver.profiles)
132-
in
133126
command
134127
("wasm-opt"
135128
:: (common_options ()
136-
@ Option.value ~default:optimization_options.(level - 1) options
129+
@ (match options with
130+
| Some o -> o
131+
| None -> optimization_options profile)
137132
@ [ Filename.quote input_file; "-o"; Filename.quote output_file ])
138133
@ opt_flag "--input-source-map" opt_input_sourcemap
139134
@ opt_flag "--output-source-map" opt_output_sourcemap)

compiler/lib-wasm/binaryen.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ val dead_code_elimination :
3838
-> Stdlib.StringSet.t
3939

4040
val optimize :
41-
profile:Driver.profile option
41+
profile:Profile.t
4242
-> ?options:string list
4343
-> opt_input_sourcemap:string option
4444
-> input_file:string

compiler/lib-wasm/runtime.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let build ~allowed_imports ~link_options ~opt_options ~variables ~inputs ~output
1212
~output_file:merge_file
1313
());
1414
Binaryen.optimize
15-
~profile:None
15+
~profile:Profile.O1
1616
~options:opt_options
1717
~opt_input_sourcemap:None
1818
~input_file:merge_file

compiler/lib/driver.ml

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,6 @@ type optimized_result =
3131
; deadcode_sentinal : Code.Var.t
3232
}
3333

34-
type profile =
35-
| O1
36-
| O2
37-
| O3
38-
39-
let profile_equal (a : profile) b = Poly.equal a b
40-
4134
let should_export = function
4235
| `Iife -> false
4336
| `Named _ | `Anonymous -> true
@@ -123,7 +116,7 @@ let effects ~deadcode_sentinal p =
123116
, (Code.Var.Set.empty : Effects.trampolined_calls)
124117
, (Code.Var.Set.empty : Effects.in_cps) )
125118

126-
let exact_calls profile ~deadcode_sentinal p =
119+
let exact_calls (profile : Profile.t) ~deadcode_sentinal p =
127120
match Config.effects () with
128121
| `Disabled | `Jspi ->
129122
let fast =
@@ -635,7 +628,7 @@ let optimize ~profile p =
635628
let opt =
636629
Specialize.switches
637630
+> specialize_js_once_before
638-
+> (match profile with
631+
+> (match (profile : Profile.t) with
639632
| O1 -> o1
640633
| O2 -> o2
641634
| O3 -> o3)
@@ -676,14 +669,20 @@ let full_no_source_map ~formatter ~standalone ~wrap_with_fun ~profile ~link p =
676669
let f
677670
?(standalone = true)
678671
?(wrap_with_fun = `Iife)
679-
?(profile = O1)
672+
?(profile = Profile.O1)
680673
~link
681674
~source_map
682675
~formatter
683676
p =
684677
full ~standalone ~wrap_with_fun ~profile ~link ~source_map ~formatter p
685678

686-
let f' ?(standalone = true) ?(wrap_with_fun = `Iife) ?(profile = O1) ~link formatter p =
679+
let f'
680+
?(standalone = true)
681+
?(wrap_with_fun = `Iife)
682+
?(profile = Profile.O1)
683+
~link
684+
formatter
685+
p =
687686
full_no_source_map ~formatter ~standalone ~wrap_with_fun ~profile ~link p
688687

689688
let from_string ~prims ~debug s formatter =
@@ -695,7 +694,3 @@ let from_string ~prims ~debug s formatter =
695694
~profile:O1
696695
~link:`No
697696
p
698-
699-
let profiles = [ 1, O1; 2, O2; 3, O3 ]
700-
701-
let profile i = List.find_map ~f:(fun (i', p) -> if i = i' then Some p else None) profiles

compiler/lib/driver.mli

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,6 @@
1818
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1919
*)
2020

21-
type profile
22-
23-
val profile_equal : profile -> profile -> bool
24-
2521
type optimized_result =
2622
{ program : Code.program
2723
; variable_uses : Deadcode.variable_uses
@@ -30,12 +26,12 @@ type optimized_result =
3026
; deadcode_sentinal : Code.Var.t
3127
}
3228

33-
val optimize : profile:profile -> Code.program -> optimized_result
29+
val optimize : profile:Profile.t -> Code.program -> optimized_result
3430

3531
val f :
3632
?standalone:bool
3733
-> ?wrap_with_fun:[ `Iife | `Anonymous | `Named of string ]
38-
-> ?profile:profile
34+
-> ?profile:Profile.t
3935
-> link:[ `All | `All_from of string list | `Needed | `No ]
4036
-> source_map:bool
4137
-> formatter:Pretty_print.t
@@ -45,7 +41,7 @@ val f :
4541
val f' :
4642
?standalone:bool
4743
-> ?wrap_with_fun:[ `Iife | `Anonymous | `Named of string ]
48-
-> ?profile:profile
44+
-> ?profile:Profile.t
4945
-> link:[ `All | `All_from of string list | `Needed | `No ]
5046
-> Pretty_print.t
5147
-> Code.program
@@ -70,7 +66,3 @@ val simplify_js : Javascript.statement_list -> Javascript.statement_list
7066
val name_variables : Javascript.statement_list -> Javascript.statement_list
7167

7268
val configure : Pretty_print.t -> unit
73-
74-
val profiles : (int * profile) list
75-
76-
val profile : int -> profile option

0 commit comments

Comments
 (0)