Skip to content

Commit 681ff02

Browse files
committed
Compiler: propagate information during optcall optimiastion.
1 parent 8e8456f commit 681ff02

File tree

5 files changed

+38
-22
lines changed

5 files changed

+38
-22
lines changed

compiler/lib/driver.ml

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ let specialize_1 (p, info) =
6565
let return_values = Code.Var.Map.empty in
6666
Specialize.f
6767
~function_arity:(fun f -> Specialize.function_arity ~return_values info f)
68+
~update_def:(fun x expr -> Flow.Info.update_def info x expr)
6869
p
6970

7071
let specialize_js (p, info) =
@@ -98,7 +99,6 @@ let ( +> ) f g x = g (f x)
9899

99100
let map_fst4 f (x, y, z, t) = f x, y, z, t
100101

101-
102102
let collects_shapes ~shapes (p : Code.program) =
103103
if debug_shapes () || shapes
104104
then (
@@ -139,7 +139,6 @@ let collects_shapes ~shapes (p : Code.program) =
139139
map)
140140
else StringMap.empty
141141

142-
143142
let effects_and_exact_calls ~deadcode_sentinal ~shapes (profile : Profile.t) p =
144143
let fast =
145144
match Config.effects (), profile with
@@ -157,21 +156,27 @@ let effects_and_exact_calls ~deadcode_sentinal ~shapes (profile : Profile.t) p =
157156
in
158157
match Config.effects () with
159158
| `Cps | `Double_translation ->
160-
if debug () then Format.eprintf "Effects...@.";
161-
let shapes = collects_shapes ~shapes p in
162-
let p, tramp, cps = Effects.f ~flow_info:info ~live_vars p in
163-
let p = match Config.target () with
164-
| `Wasm -> p
165-
| `JavaScript -> Lambda_lifting.f p in
166-
p, tramp, cps, shapes
159+
if debug () then Format.eprintf "Effects...@.";
160+
let shapes = collects_shapes ~shapes p in
161+
let p, tramp, cps = Effects.f ~flow_info:info ~live_vars p in
162+
let p =
163+
match Config.target () with
164+
| `Wasm -> p
165+
| `JavaScript -> Lambda_lifting.f p
166+
in
167+
p, tramp, cps, shapes
167168
| `Disabled | `Jspi ->
168169
let p =
169-
Specialize.f ~function_arity:(fun f -> Global_flow.function_arity info f) p
170+
Specialize.f
171+
~function_arity:(fun f -> Global_flow.function_arity info f)
172+
~update_def:(fun x expr -> Global_flow.update_def info x expr)
173+
p
170174
in
171175
let shapes = collects_shapes ~shapes p in
172176
( p
173177
, (Code.Var.Set.empty : Effects.trampolined_calls)
174-
, (Code.Var.Set.empty : Effects.in_cps), shapes )
178+
, (Code.Var.Set.empty : Effects.in_cps)
179+
, shapes )
175180

176181
let print p =
177182
if debug () then Code.Print.program Format.err_formatter (fun _ _ -> "") p;
@@ -644,7 +649,6 @@ let simplify_js js =
644649
if times () then Format.eprintf " optimizing: %a@." Timer.print t;
645650
js
646651

647-
648652
let configure formatter =
649653
let pretty = Config.Flag.pretty () in
650654
Pretty_print.set_compact formatter (not pretty)
@@ -673,7 +677,7 @@ let optimize ~shapes ~profile p =
673677
| O2 -> o2
674678
| O3 -> o3)
675679
+> specialize_js_once_after
676-
+> effects_and_exact_calls ~deadcode_sentinal ~shapes profile
680+
+> effects_and_exact_calls ~deadcode_sentinal ~shapes profile
677681
+> map_fst4
678682
(match Config.target (), Config.effects () with
679683
| `JavaScript, `Disabled -> Generate_closure.f

compiler/lib/global_flow.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,10 @@ let exact_call info f n =
716716
| Expr _ | Phi _ -> assert false)
717717
known
718718

719+
let update_def info x expr =
720+
let idx = Code.Var.idx x in
721+
info.info_defs.(idx) <- Expr expr
722+
719723
let function_arity info f =
720724
match Var.Tbl.get info.info_approximation f with
721725
| Top | Values { others = true; _ } -> None

compiler/lib/global_flow.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ type info =
4444
; info_return_vals : Var.Set.t Var.Map.t
4545
}
4646

47+
val update_def : info -> Code.Var.t -> Code.expr -> unit
48+
4749
val f : fast:bool -> Code.program -> info
4850

4951
val exact_call : info -> Var.t -> int -> bool

compiler/lib/specialize.ml

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ let unknown_apply = function
4040
| Let (_, Apply { f = _; args = _; exact = false }) -> true
4141
| _ -> false
4242

43-
let specialize_apply opt_count function_arity ((acc, free_pc, extra), loc) i =
43+
let specialize_apply opt_count function_arity update_def ((acc, free_pc, extra), loc) i =
4444
match i with
4545
| Let (x, Apply { f; args; exact = false }) -> (
4646
let n' = List.length args in
@@ -74,13 +74,13 @@ let specialize_apply opt_count function_arity ((acc, free_pc, extra), loc) i =
7474
; branch = Return return'
7575
}
7676
in
77-
( Let (x, Closure (missing, (free_pc, missing), None)) :: acc
78-
, free_pc + 1
79-
, (free_pc, block) :: extra )
77+
let expr = Closure (missing, (free_pc, missing), None) in
78+
update_def x expr;
79+
Let (x, expr) :: acc, free_pc + 1, (free_pc, block) :: extra
8080
| Some _ -> assert false)
8181
| _ -> i :: acc, free_pc, extra
8282

83-
let specialize_instrs ~function_arity opt_count p =
83+
let specialize_instrs ~function_arity ~update_def opt_count p =
8484
let blocks, free_pc =
8585
Addr.Map.fold
8686
(fun pc block (blocks, free_pc) ->
@@ -95,7 +95,7 @@ let specialize_instrs ~function_arity opt_count p =
9595
| Event loc ->
9696
let (body, free_pc, extra), _ = acc in
9797
(i :: body, free_pc, extra), Some loc
98-
| _ -> specialize_apply opt_count function_arity acc i, None)
98+
| _ -> specialize_apply opt_count function_arity update_def acc i, None)
9999
in
100100
let blocks =
101101
List.fold_left extra ~init:blocks ~f:(fun blocks (pc, b) ->
@@ -108,13 +108,15 @@ let specialize_instrs ~function_arity opt_count p =
108108
in
109109
{ p with blocks; free_pc }
110110

111-
let f ~function_arity p =
111+
let f ~function_arity ~update_def p =
112112
Code.invariant p;
113113
let previous_p = p in
114114
let t = Timer.make () in
115115
let opt_count = ref 0 in
116116
let p =
117-
if Config.Flag.optcall () then specialize_instrs ~function_arity opt_count p else p
117+
if Config.Flag.optcall ()
118+
then specialize_instrs ~function_arity ~update_def opt_count p
119+
else p
118120
in
119121
if times () then Format.eprintf " optcall: %a@." Timer.print t;
120122
if stats () then Format.eprintf "Stats - optcall: %d@." !opt_count;

compiler/lib/specialize.mli

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
val function_arity :
2222
return_values:Code.Var.Set.t Code.Var.Map.t -> Flow.Info.t -> Code.Var.t -> int option
2323

24-
val f : function_arity:(Code.Var.t -> int option) -> Code.program -> Code.program
24+
val f :
25+
function_arity:(Code.Var.t -> int option)
26+
-> update_def:(Code.Var.t -> Code.expr -> unit)
27+
-> Code.program
28+
-> Code.program
2529

2630
val switches : Code.program -> Code.program

0 commit comments

Comments
 (0)