Skip to content

Commit fe131f6

Browse files
committed
Compiler: propagate information during optcall optimiastion.
1 parent 9644830 commit fe131f6

File tree

5 files changed

+26
-10
lines changed

5 files changed

+26
-10
lines changed

compiler/lib/driver.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ let specialize_1 (p, info) =
6868
let return_values = Code.Var.Map.empty in
6969
Specialize.f
7070
~function_arity:(fun f -> Specialize.function_arity ~return_values info f)
71+
~update_def:(fun x expr -> Flow.Info.update_def info x expr)
7172
p
7273

7374
let specialize_js (p, info) =
@@ -177,7 +178,10 @@ let effects_and_exact_calls
177178
p, trampolined_calls, in_cps, None, shapes
178179
| `Disabled | `Jspi ->
179180
let p =
180-
Specialize.f ~function_arity:(fun f -> Global_flow.function_arity info f) p
181+
Specialize.f
182+
~function_arity:(fun f -> Global_flow.function_arity info f)
183+
~update_def:(fun x expr -> Global_flow.update_def info x expr)
184+
p
181185
in
182186
let shapes = collects_shapes ~shapes p in
183187
( p

compiler/lib/global_flow.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,10 @@ let get_unique_closure info f =
804804
| None -> None
805805
| Some kind -> kind)
806806

807+
let update_def info x expr =
808+
let idx = Code.Var.idx x in
809+
info.info_defs.(idx) <- Expr expr
810+
807811
let function_arity info f =
808812
match Var.Tbl.get info.info_approximation f with
809813
| Top | Values { others = true; _ } -> None

compiler/lib/global_flow.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ type state =
8080

8181
val f : fast:bool -> Code.program -> state * info
8282

83+
val update_def : info -> Code.Var.t -> Code.expr -> unit
84+
8385
val exact_call : info -> Var.t -> int -> bool
8486

8587
val get_unique_closure : info -> Var.t -> Var.t option

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)