Skip to content

Commit 69f8f24

Browse files
committed
WIP
1 parent 13e2b0c commit 69f8f24

File tree

9 files changed

+47
-27
lines changed

9 files changed

+47
-27
lines changed

compiler/bin-wasm_of_ocaml/compile.ml

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,8 +244,8 @@ let generate_prelude ~out_file =
244244
@@ fun ch ->
245245
let code, uinfo = Parse_bytecode.predefined_exceptions () in
246246
let profile = Profile.O1 in
247-
let Driver.{ program; variable_uses; in_cps; deadcode_sentinal; _ } =
248-
Driver.optimize ~profile code
247+
let Driver.{ program; variable_uses; in_cps; deadcode_sentinal; _ }, global_flow_data =
248+
Driver.optimize_for_wasm ~profile code
249249
in
250250
let context = Generate.start () in
251251
let _ =
@@ -255,6 +255,7 @@ let generate_prelude ~out_file =
255255
~live_vars:variable_uses
256256
~in_cps
257257
~deadcode_sentinal
258+
~global_flow_data
258259
program
259260
in
260261
Generate.wasm_output ch ~opt_source_map_file:None ~context;
@@ -396,8 +397,9 @@ let run
396397
check_debug one;
397398
let code = one.code in
398399
let standalone = Option.is_none unit_name in
399-
let Driver.{ program; variable_uses; in_cps; deadcode_sentinal; _ } =
400-
Driver.optimize ~profile code
400+
let Driver.{ program; variable_uses; in_cps; deadcode_sentinal; _ }, global_flow_data
401+
=
402+
Driver.optimize_for_wasm ~profile code
401403
in
402404
let context = Generate.start () in
403405
let toplevel_name, generated_js =
@@ -407,6 +409,7 @@ let run
407409
~live_vars:variable_uses
408410
~in_cps
409411
~deadcode_sentinal
412+
~global_flow_data
410413
program
411414
in
412415
if standalone then Generate.add_start_function ~context toplevel_name;

compiler/lib-wasm/generate.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,9 +1490,9 @@ let init = G.init
14901490

14911491
let start () = make_context ~value_type:Gc_target.Type.value
14921492

1493-
let f ~context ~unit_name p ~live_vars ~in_cps ~deadcode_sentinal =
1494-
let state, info = Global_flow.f' ~fast:false p in
1495-
let types = Typing.f ~state ~info p in
1493+
let f ~context ~unit_name p ~live_vars ~in_cps ~deadcode_sentinal ~global_flow_data =
1494+
let state, info = global_flow_data in
1495+
let types = Typing.f ~state ~info ~deadcode_sentinal p in
14961496
let t = Timer.make () in
14971497
let p = fix_switch_branches p in
14981498
let res = G.f ~context ~unit_name ~live_vars ~in_cps ~deadcode_sentinal ~types p in

compiler/lib-wasm/generate.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ val f :
2727
-> live_vars:int array
2828
-> in_cps:Effects.in_cps
2929
-> deadcode_sentinal:Code.Var.t
30+
-> global_flow_data:Global_flow.state * Global_flow.info
3031
-> Wasm_ast.var * (string * Javascript.expression) list
3132

3233
val add_start_function : context:Code_generation.context -> Wasm_ast.var -> unit

compiler/lib-wasm/typing.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,10 +414,11 @@ let solver st =
414414
in
415415
Solver.f () g (propagate st)
416416

417-
let f ~state ~info p =
417+
let f ~state ~info ~deadcode_sentinal p =
418418
update_deps state p;
419419
let function_parameters = mark_function_parameters p in
420420
let typ = solver { state; info; function_parameters } in
421+
Var.Tbl.set typ deadcode_sentinal (Int Normalized);
421422
if debug ()
422423
then (
423424
Var.ISet.iter

compiler/lib-wasm/typing.mli

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ type typ =
2121
val constant_type : Code.constant -> typ
2222

2323
val f :
24-
state:Global_flow.state -> info:Global_flow.info -> Code.program -> typ Code.Var.Tbl.t
24+
state:Global_flow.state
25+
-> info:Global_flow.info
26+
-> deadcode_sentinal:Code.Var.t
27+
-> Code.program
28+
-> typ Code.Var.Tbl.t

compiler/lib/driver.ml

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,18 @@ let phi p =
9393

9494
let ( +> ) f g x = g (f x)
9595

96-
let map_fst f (x, y, z) = f x, y, z
96+
let map_fst f (x, y) = f x, y
9797

98-
let effects_and_exact_calls ~deadcode_sentinal (profile : Profile.t) p =
98+
let effects_and_exact_calls ~keep_flow_data ~deadcode_sentinal (profile : Profile.t) p =
9999
let fast =
100100
match Config.effects (), profile with
101101
| (`Cps | `Double_translation), _ -> false
102102
| _, (O2 | O3) -> false
103103
| _, O1 -> true
104104
in
105-
let info = Global_flow.f ~fast p in
105+
let global_flow_data = Global_flow.f ~fast p in
106+
let _, info = global_flow_data in
107+
let global_flow_data = if keep_flow_data then Some global_flow_data else None in
106108
let pure_fun = Pure_fun.f p in
107109
let p, live_vars =
108110
if Config.Flag.globaldeadcode () && Config.Flag.deadcode ()
@@ -114,7 +116,8 @@ let effects_and_exact_calls ~deadcode_sentinal (profile : Profile.t) p =
114116
match Config.effects () with
115117
| `Cps | `Double_translation ->
116118
if debug () then Format.eprintf "Effects...@.";
117-
Effects.f ~flow_info:info ~live_vars p
119+
let p, trampolined_calls, in_cps = Effects.f ~flow_info:info ~live_vars p in
120+
(p, (trampolined_calls, in_cps, None))
118121
|> map_fst
119122
(match Config.target () with
120123
| `Wasm -> Fun.id
@@ -124,8 +127,9 @@ let effects_and_exact_calls ~deadcode_sentinal (profile : Profile.t) p =
124127
Specialize.f ~function_arity:(fun f -> Global_flow.function_arity info f) p
125128
in
126129
( p
127-
, (Code.Var.Set.empty : Effects.trampolined_calls)
128-
, (Code.Var.Set.empty : Effects.in_cps) )
130+
, ( (Code.Var.Set.empty : Effects.trampolined_calls)
131+
, (Code.Var.Set.empty : Effects.in_cps)
132+
, global_flow_data ) )
129133

130134
let print p =
131135
if debug () then Code.Print.program Format.err_formatter (fun _ _ -> "") p;
@@ -613,7 +617,7 @@ let link_and_pack ?(standalone = true) ?(wrap_with_fun = `Iife) ?(link = `No) p
613617
|> pack ~wrap_with_fun ~standalone
614618
|> check_js
615619

616-
let optimize ~profile p =
620+
let optimize ~profile ~keep_flow_data p =
617621
let deadcode_sentinal =
618622
(* If deadcode is disabled, this field is just fresh variable *)
619623
Code.Var.fresh_n "dummy"
@@ -626,7 +630,7 @@ let optimize ~profile p =
626630
| O2 -> o2
627631
| O3 -> o3)
628632
+> specialize_js_once_after
629-
+> effects_and_exact_calls ~deadcode_sentinal profile
633+
+> effects_and_exact_calls ~keep_flow_data ~deadcode_sentinal profile
630634
+> map_fst
631635
(match Config.target (), Config.effects () with
632636
| `JavaScript, `Disabled -> Generate_closure.f
@@ -637,12 +641,20 @@ let optimize ~profile p =
637641
in
638642
if times () then Format.eprintf "Start Optimizing...@.";
639643
let t = Timer.make () in
640-
let (program, variable_uses), trampolined_calls, in_cps = opt p in
644+
let (program, variable_uses), (trampolined_calls, in_cps, global_flow_info) = opt p in
641645
let () = if times () then Format.eprintf " optimizations : %a@." Timer.print t in
642-
{ program; variable_uses; trampolined_calls; in_cps; deadcode_sentinal }
646+
( { program; variable_uses; trampolined_calls; in_cps; deadcode_sentinal }
647+
, global_flow_info )
648+
649+
let optimize_for_wasm ~profile p =
650+
let optimized_code, global_flow_data = optimize ~profile ~keep_flow_data:true p in
651+
( optimized_code
652+
, match global_flow_data with
653+
| Some data -> data
654+
| None -> Global_flow.f ~fast:false optimized_code.program )
643655

644656
let full ~standalone ~wrap_with_fun ~profile ~link ~source_map ~formatter p =
645-
let optimized_code = optimize ~profile p in
657+
let optimized_code, _ = optimize ~profile ~keep_flow_data:false p in
646658
let exported_runtime = not standalone in
647659
let emit formatter =
648660
generate ~exported_runtime ~wrap_with_fun ~warn_on_unhandled_effect:standalone

compiler/lib/driver.mli

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ type optimized_result =
2626
; deadcode_sentinal : Code.Var.t
2727
}
2828

29-
val optimize : profile:Profile.t -> Code.program -> optimized_result
29+
val optimize_for_wasm :
30+
profile:Profile.t
31+
-> Code.program
32+
-> optimized_result * (Global_flow.state * Global_flow.info)
3033

3134
val f :
3235
?standalone:bool

compiler/lib/global_flow.ml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -691,7 +691,7 @@ type info =
691691
; info_return_vals : Var.Set.t Var.Map.t
692692
}
693693

694-
let f' ~fast p =
694+
let f ~fast p =
695695
let t = Timer.make () in
696696
let t1 = Timer.make () in
697697
let rets = return_values p in
@@ -793,8 +793,6 @@ let f' ~fast p =
793793
; info_return_vals = rets
794794
} )
795795

796-
let f ~fast p = snd (f' ~fast p)
797-
798796
let exact_call info f n =
799797
match Var.Tbl.get info.info_approximation f with
800798
| Top | Values { others = true; _ } -> false

compiler/lib/global_flow.mli

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ type state =
7878
; fast : bool
7979
}
8080

81-
val f : fast:bool -> Code.program -> info
82-
83-
val f' : fast:bool -> Code.program -> state * info
81+
val f : fast:bool -> Code.program -> state * info
8482

8583
val exact_call : info -> Var.t -> int -> bool
8684

0 commit comments

Comments
 (0)