Skip to content

Commit

Permalink
feat: apply Lifused optimization (#746)
Browse files Browse the repository at this point in the history
  • Loading branch information
anmonteiro authored Oct 1, 2023
1 parent d9d0700 commit 7aa9b0b
Show file tree
Hide file tree
Showing 37 changed files with 237 additions and 273 deletions.
8 changes: 8 additions & 0 deletions jscomp/common/lam.ml
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ module Types = struct
| Lfor of ident * t * t * Asttypes.direction_flag * t
| Lassign of ident * t
| Lsend of Lam_compat.meth_kind * t * t * t list * Location.t
| Lifused of ident * t
end

module X = struct
Expand Down Expand Up @@ -157,6 +158,7 @@ module X = struct
| Lfor of ident * t * t * Asttypes.direction_flag * t
| Lassign of ident * t
| Lsend of Lam_compat.meth_kind * t * t * t list * Location.t
| Lifused of ident * t
end

include Types
Expand Down Expand Up @@ -256,6 +258,7 @@ let inner_map (l : t) (f : t -> X.t) : X.t =
let obj = f obj in
let args = List.map f args in
Lsend (k, met, obj, args, loc)
| Lifused (v, e) -> Lifused (v, f e)

exception Not_simple_form

Expand Down Expand Up @@ -411,6 +414,10 @@ let rec eq_approx (l1 : t) (l2 : t) =
| Lfor (_, _, _, _, _)
| Lsend _ ->
false
| Lifused (v1, e1) -> (
match l2 with
| Lifused (v2, e2) -> Ident.equal v1 v2 && eq_approx e1 e2
| _ -> false)

and eq_option l1 l2 =
match l1 with
Expand Down Expand Up @@ -465,6 +472,7 @@ let letrec bindings body : t = Lletrec (bindings, body)
let while_ a b : t = Lwhile (a, b)
let try_ body id handler : t = Ltrywith (body, id, handler)
let for_ v e1 e2 dir e3 : t = Lfor (v, e1, e2, dir, e3)
let ifused v l : t = Lifused (v, l)
let assign v l : t = Lassign (v, l)
let send u m o ll v : t = Lsend (u, m, o, ll, v)
let staticcatch a b c : t = Lstaticcatch (a, b, c)
Expand Down
2 changes: 2 additions & 0 deletions jscomp/common/lam.mli
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ and t = private
| Lfor of ident * t * t * Asttypes.direction_flag * t
| Lassign of ident * t
| Lsend of Lambda.meth_kind * t * t * t list * Location.t
| Lifused of ident * t
(* | Levent of t * Lambda.lambda_event
[Levent] in the branch hurt pattern match,
we should use record for trivial debugger info
Expand Down Expand Up @@ -144,6 +145,7 @@ val while_ : t -> t -> t

(* val event : t -> Lambda.lambda_event -> t *)
val try_ : t -> ident -> t -> t
val ifused : ident -> t -> t
val assign : ident -> t -> t
val send : Lambda.meth_kind -> t -> t -> t list -> Location.t -> t

Expand Down
2 changes: 2 additions & 0 deletions jscomp/core/lam_analysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ let rec no_side_effects (lam : Lam.t) : bool =
| Lfor _ -> false
| Lassign _ -> false (* actually it depends ... *)
| Lsend _ -> false
| Lifused _ -> false
| Lapply
{
ap_func =
Expand Down Expand Up @@ -202,6 +203,7 @@ let rec size (lam : Lam.t) =
| Lfor _ -> really_big ()
| Lassign (_, v) -> 1 + size v (* This is side effectful, be careful *)
| Lsend _ -> really_big ()
| Lifused (_v, l) -> size l
with Too_big_to_inline -> 1000

and size_constant x =
Expand Down
3 changes: 2 additions & 1 deletion jscomp/core/lam_arity_analysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ let rec get_arity (meta : Lam_stats.t) (lam : Lam.t) : Lam_arity.t =
| Ltrywith (l1, _, l2) -> all_lambdas meta [ l1; l2 ]
| Lifthenelse (_, l2, l3) -> all_lambdas meta [ l2; l3 ]
| Lsequence (_, l2) -> get_arity meta l2
| Lstaticraise _ (* since it will not be in tail position *) | Lsend _ ->
| Lstaticraise _ (* since it will not be in tail position *) | Lsend _
| Lifused _ ->
Lam_arity.na
| Lwhile _ | Lfor _ | Lassign _ -> Lam_arity.non_function_arity_info

Expand Down
3 changes: 3 additions & 0 deletions jscomp/core/lam_bounded_vars.ml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ let rewrite (map : _ Hash_ident.t) (lam : Lam.t) : Lam.t =
let o = aux o in
let ll = List.map aux ll in
Lam.send u m o ll v
| Lifused (v, l) ->
let l = aux l in
Lam.ifused v l
in
aux lam

Expand Down
2 changes: 2 additions & 0 deletions jscomp/core/lam_check.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ let check file lam =
| Lsequence (e1, e2) -> check_list [ e1; e2 ] cxt
| Lassign (_id, e) -> check_staticfails e cxt
| Lsend (_k, met, obj, args, _) -> check_list (met :: obj :: args) cxt
| Lifused (_v, e) -> check_staticfails e cxt
in
let rec iter_list xs = List.iter iter xs
and iter_list_snd : 'a. ('a * Lam.t) list -> unit =
Expand Down Expand Up @@ -158,6 +159,7 @@ let check file lam =
iter met;
iter obj;
iter_list args
| Lifused (_v, e) -> iter e
in

check_staticfails lam Set_int.empty;
Expand Down
1 change: 1 addition & 0 deletions jscomp/core/lam_closure.ml
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ let free_variables (export_idents : Set_ident.t) (params : stats Map_ident.t)
iter sink_pos met;
iter sink_pos obj;
List.iter (iter sink_pos) args
| Lifused (_v, e) -> iter sink_pos e
in
iter Lam_var_stats.fresh_env lam;
!fv
Expand Down
1 change: 1 addition & 0 deletions jscomp/core/lam_compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1748,3 +1748,4 @@ and compile_lambda (lambda_cxt : Lam_compile_context.t) (cur_lam : Lam.t) :
compile_trywith lam id catch lambda_cxt
| Lsend (meth_kind, met, obj, args, _loc) ->
compile_send meth_kind met obj args lambda_cxt
| Lifused (_, lam) -> compile_lambda lambda_cxt lam
3 changes: 2 additions & 1 deletion jscomp/core/lam_convert.ml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ let exception_id_destructed (l : Lam.t) (fv : Ident.t) : bool =
| Lsequence (e1, e2) -> hit e1 || hit e2
| Lwhile (e1, e2) -> hit e1 || hit e2
| Lsend (_k, met, obj, args, _) -> hit met || hit obj || hit_list args
| Lifused (_v, e) -> hit e
in
hit l

Expand Down Expand Up @@ -762,7 +763,7 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) :
Lam.send kind a b ls
(Debuginfo.Scoped_location.to_location outer_loc))
| Levent (e, _ev) -> convert_aux e
| Lifused (_, e) -> convert_aux e (* TODO: remove it ASAP *)
| Lifused (v, e) -> Lam.ifused v (convert_aux e)
and convert_let (kind : Lam_compat.let_kind) id (e : Lambda.lambda) body :
Lam.t =
let e = convert_aux e in
Expand Down
1 change: 1 addition & 0 deletions jscomp/core/lam_exit_count.ml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ let count_helper ~try_depth (lam : Lam.t) : collection =
count m;
count o;
List.iter count ll
| Lifused (_v, e) -> count e
and count_default sw =
match sw.sw_failaction with
| None -> ()
Expand Down
1 change: 1 addition & 0 deletions jscomp/core/lam_free_variables.ml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ let pass_free_variables (l : Lam.t) : Set_ident.t =
free met;
free obj;
free_list args
| Lifused (_v, e) -> free e
in
free l;
!fv
Expand Down
2 changes: 2 additions & 0 deletions jscomp/core/lam_hit.ml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ let hit_variables (fv : Set_ident.t) (l : t) : bool =
| Lsequence (e1, e2) -> hit e1 || hit e2
| Lwhile (e1, e2) -> hit e1 || hit e2
| Lsend (_k, met, obj, args, _) -> hit met || hit obj || hit_list args
| Lifused (_v, e) -> hit e
in
hit l

Expand Down Expand Up @@ -89,5 +90,6 @@ let hit_variable (fv : Ident.t) (l : t) : bool =
| Lsequence (e1, e2) -> hit e1 || hit e2
| Lwhile (e1, e2) -> hit e1 || hit e2
| Lsend (_k, met, obj, args, _) -> hit met || hit obj || hit_list args
| Lifused (_v, e) -> hit e
in
hit l
2 changes: 2 additions & 0 deletions jscomp/core/lam_iter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ let inner_iter (l : t) (f : t -> unit) : unit =
f met;
f obj;
List.iter f args
| Lifused (_v, e) -> f e

let option_exists v f = match v with None -> false | Some x -> f x

Expand Down Expand Up @@ -124,3 +125,4 @@ let inner_exists (l : t) (f : t -> bool) : bool =
| Lfor (_v, e1, e2, _dir, e3) -> f e1 || f e2 || f e3
| Lassign (_id, e) -> f e
| Lsend (_k, met, obj, args, _loc) -> f met || f obj || List.exists f args
| Lifused (_v, e) -> f e
1 change: 1 addition & 0 deletions jscomp/core/lam_pass_alpha_conversion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ let alpha_conversion (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
Lam.assign v (simpl l)
| Lsend (u, m, o, ll, v) ->
Lam.send u (simpl m) (simpl o) (List.map simpl ll) v
| Lifused (v, e) -> Lam.ifused v (simpl e)
in

simpl lam
1 change: 1 addition & 0 deletions jscomp/core/lam_pass_collect.ml
Original file line number Diff line number Diff line change
Expand Up @@ -167,5 +167,6 @@ let collect_info (meta : Lam_stats.t) (lam : Lam.t) =
collect m;
collect o;
List.iter collect ll
| Lifused (_v, e) -> collect e
in
collect lam
1 change: 1 addition & 0 deletions jscomp/core/lam_pass_count.ml
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ let collect_occurs lam : occ_tbl =
count bv m;
count bv o;
List.iter (count bv) ll
| Lifused (v, l) -> if used v then count bv l
and count_default bv sw =
match sw.sw_failaction with
| None -> ()
Expand Down
1 change: 1 addition & 0 deletions jscomp/core/lam_pass_deep_flatten.ml
Original file line number Diff line number Diff line change
Expand Up @@ -322,5 +322,6 @@ let deep_flatten (lam : Lam.t) : Lam.t =
v's refaux *)
Lam.assign v (aux l)
| Lsend (u, m, o, ll, v) -> Lam.send u (aux m) (aux o) (List.map aux ll) v
| Lifused (v, l) -> Lam.ifused v (aux l)
in
aux lam
1 change: 1 addition & 0 deletions jscomp/core/lam_pass_eliminate_ref.ml
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,4 @@ let rec eliminate_ref id (lam : Lam.t) =
Lam.send k (eliminate_ref id m) (eliminate_ref id o)
(List.map (eliminate_ref id) el)
loc
| Lifused (v, e) -> Lam.ifused v (eliminate_ref id e)
2 changes: 2 additions & 0 deletions jscomp/core/lam_pass_exits.ml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ and no_bounded_variables (l : Lam.t) =
| Lwhile (e1, e2) -> no_bounded_variables e1 && no_bounded_variables e2
| Lsend (_k, met, obj, args, _) ->
no_bounded_variables met && no_bounded_variables obj && no_list args
| Lifused (_v, e) -> no_bounded_variables e
| Lstaticcatch (e1, (_, vars), e2) ->
vars = [] && no_bounded_variables e1 && no_bounded_variables e2
| Lfunction { body; params; _ } -> params = [] && no_bounded_variables body
Expand Down Expand Up @@ -241,6 +242,7 @@ let subst_helper ~try_depth (subst : subst_tbl)
| Lassign (v, l) -> Lam.assign v (simplif l)
| Lsend (k, m, o, ll, loc) ->
Lam.send k (simplif m) (simplif o) (List.map simplif ll) loc
| Lifused (v, l) -> Lam.ifused v (simplif l)
in
simplif lam

Expand Down
11 changes: 11 additions & 0 deletions jscomp/core/lam_pass_lets_dce.ml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,17 @@ let lets_helper (count_var : Ident.t -> Lam_pass_count.used_info) lam : Lam.t =
else
let l1 = (simplif l1) in
Lam_util.refine_let ~kind:Variable v l1 (simplif l2)


| Lifused(v, l) ->
if used v then
simplif l
else Lam.unit
| Lsequence(Lifused(v, l1), l2) ->
if used v
then Lam.seq (simplif l1) (simplif l2)
else simplif l2

| Lsequence(l1, l2) -> Lam.seq (simplif l1) (simplif l2)

| Lapply{ap_func = Lfunction{params; body;_}; ap_args = args; _}
Expand Down
1 change: 1 addition & 0 deletions jscomp/core/lam_pass_remove_alias.ml
Original file line number Diff line number Diff line change
Expand Up @@ -276,5 +276,6 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
Lam.assign v (simpl l)
| Lsend (u, m, o, ll, v) ->
Lam.send u (simpl m) (simpl o) (List.map simpl ll) v
| Lifused (v, l) -> Lam.ifused v (simpl l)
in
simpl lam
2 changes: 2 additions & 0 deletions jscomp/core/lam_print.ml
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ let lambda ppf v =
| Public None -> ""
in
fprintf ppf "@[<2>(send%s@ %a@ %a%a)@]" kind lam obj lam met args largs
| Lifused (id, expr) ->
fprintf ppf "@[<2>(ifused@ %a@ %a)@]" Ident.print id lam expr
and sequence ppf = function
| Lsequence (l1, l2) -> fprintf ppf "%a@ %a" sequence l1 sequence l2
| l -> lam ppf l
Expand Down
1 change: 1 addition & 0 deletions jscomp/core/lam_scc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ let hit_mask (mask : Hash_set_ident_mask.t) (l : Lam.t) : bool =
| Lsequence (e1, e2) -> hit e1 || hit e2
| Lwhile (e1, e2) -> hit e1 || hit e2
| Lsend (_k, met, obj, args, _) -> hit met || hit obj || hit_list args
| Lifused (_v, e) -> hit e
in
hit l

Expand Down
1 change: 1 addition & 0 deletions jscomp/core/lam_subst.ml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ let subst (s : Lam.t Map_ident.t) lam =
| Lassign (id, e) -> Lam.assign id (subst_aux e)
| Lsend (k, met, obj, args, loc) ->
Lam.send k (subst_aux met) (subst_aux obj) (List.map subst_aux args) loc
| Lifused (v, e) -> Lam.ifused v (subst_aux e)
and subst_decl (id, exp) = (id, subst_aux exp)
and subst_case (key, case) = (key, subst_aux case)
and subst_strcase (key, case) = (key, subst_aux case)
Expand Down
Loading

0 comments on commit 7aa9b0b

Please sign in to comment.