Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 23 additions & 12 deletions jscomp/core/lam.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ type apply_status =
| App_infer_full
| App_uncurry

type function_attribute =
| Always_inline
| Never_inline
| Default_inline

module Types = struct

Expand All @@ -39,6 +43,12 @@ module Types = struct
sw_blocks: (int * t) list;
sw_failaction : t option;
sw_names : Lambda.switch_names option }
and lfunction = {
arity : int ;
params : ident list ;
body : t;
attr : function_attribute
}
(*
Invariant:
length (sw_consts) <= sw_consts_full
Expand Down Expand Up @@ -86,10 +96,7 @@ module Types = struct
| Lglobal_module of ident
| Lconst of Lam_constant.t
| Lapply of apply_info
| Lfunction of { arity : int ;
params : ident list ;
body : t
}
| Lfunction of lfunction
| Llet of Lam_compat.let_kind * ident * t * t
| Lletrec of (ident * t) list * t
| Lprim of prim_info
Expand Down Expand Up @@ -132,17 +139,21 @@ module X = struct
ap_loc : Location.t;
ap_status : apply_status
}
and lfunction = Types.lfunction =
{
arity : int ;
params : ident list ;
body : t;
attr : function_attribute
}
and t
= Types.t
=
| Lvar of ident
| Lglobal_module of ident
| Lconst of Lam_constant.t
| Lapply of apply_info
| Lfunction of { arity : int ;
params : ident list ;
body : t
}
| Lfunction of lfunction
| Llet of Lam_compat.let_kind * ident * t * t
| Lletrec of (ident * t) list * t
| Lprim of prim_info
Expand Down Expand Up @@ -175,9 +186,9 @@ let inner_map
let ap_func = f ap_func in
let ap_args = Ext_list.map ap_args f in
Lapply { ap_func ; ap_args; ap_loc; ap_status }
| Lfunction({body; arity; params } ) ->
| Lfunction({body; arity; params ; attr } ) ->
let body = f body in
Lfunction {body; arity; params}
Lfunction {body; arity; params; attr}
| Llet(str, id, arg, body) ->
let arg = f arg in let body = f body in
Llet(str,id,arg,body)
Expand Down Expand Up @@ -428,8 +439,8 @@ let rec seq (a : t) b : t =
let var id : t = Lvar id
let global_module id = Lglobal_module id
let const ct : t = Lconst ct
let function_ ~arity ~params ~body : t =
Lfunction { arity; params ; body}
let function_ ~attr ~arity ~params ~body : t =
Lfunction { arity; params ; body; attr}

let let_ kind id e body : t
= Llet (kind,id,e,body)
Expand Down
18 changes: 12 additions & 6 deletions jscomp/core/lam.mli
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@




type function_attribute =
| Always_inline
| Never_inline
| Default_inline

type ident = Ident.t

Expand All @@ -45,7 +48,12 @@ and apply_info = private
ap_loc : Location.t;
ap_status : apply_status
}

and lfunction = {
arity : int ;
params : ident list ;
body : t ;
attr : function_attribute;
}
and prim_info = private
{ primitive : Lam_primitive.t ;
args : t list ;
Expand All @@ -56,10 +64,7 @@ and t = private
| Lglobal_module of ident
| Lconst of Lam_constant.t
| Lapply of apply_info
| Lfunction of { arity : int ;
params : ident list ;
body : t
}
| Lfunction of lfunction
| Llet of Lam_compat.let_kind * ident * t * t
| Lletrec of (ident * t) list * t
| Lprim of prim_info
Expand Down Expand Up @@ -102,6 +107,7 @@ val const : Lam_constant.t -> t

val apply : t -> t list -> Location.t -> apply_status -> t
val function_ :
attr:function_attribute ->
arity:int ->
params:ident list ->
body:t -> t
Expand Down
18 changes: 11 additions & 7 deletions jscomp/core/lam_analysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -334,14 +334,18 @@ let destruct_pattern (body : Lam.t) params args =

(** Hints to inlining *)
let ok_to_inline_fun_when_app
~(body : Lam.t)
(params : Ident.t list)
(m : Lam.lfunction)
(args : Lam.t list) =
let s = size body in
s < small_inline_size ||
(destruct_pattern body params args) ||
(args_all_const args &&
(s < 10 && no_side_effects body ))
match m.attr with
| Always_inline -> true
| Never_inline -> false
| Default_inline ->
let Lam.{body; params} = m in
let s = size body in
s < small_inline_size ||
(destruct_pattern body params args) ||
(args_all_const args &&
(s < 10 && no_side_effects body ))



Expand Down
5 changes: 4 additions & 1 deletion jscomp/core/lam_analysis.mli
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ val no_side_effects : Lam.t -> bool

val size : Lam.t -> int

val ok_to_inline_fun_when_app : body:Lam.t -> Lam.ident list -> Lam.t list -> bool
val ok_to_inline_fun_when_app :
Lam.lfunction ->
Lam.t list ->
bool



Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/lam_bounded_vars.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ let rewrite (map : _ Hash_ident.t)
let bindings = Ext_list.map2 vars bindings (fun var (_,l) -> var, aux l) in
let body = aux body in
Lam.letrec bindings body
| Lfunction{arity; params; body} ->
| Lfunction{arity; params; body; attr} ->
let params = Ext_list.map params rebind in
let body = aux body in
Lam.function_ ~arity ~params ~body
Lam.function_ ~arity ~params ~body ~attr
| Lstaticcatch(l1, (i,xs), l2) ->
let l1 = aux l1 in
let xs = Ext_list.map xs rebind in
Expand Down
16 changes: 12 additions & 4 deletions jscomp/core/lam_convert.ml
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,8 @@ let lam_prim ~primitive:( p : Lambda.primitive) ~args loc : Lam.t =
let args =
[ Lam.const Const_js_false ;
(* FIXME: arity 0 does not get proper supported*)
prim ~primitive:(Pjs_fn_make 0) ~args:[Lam.function_ ~arity:1 ~params:[Ident.create "param"] ~body:computation]
prim ~primitive:(Pjs_fn_make 0) ~args:[Lam.function_ ~arity:1 ~params:[Ident.create "param"] ~body:computation
~attr:Default_inline]
loc
] in
prim ~primitive:(Pmakeblock (tag,lazy_block_info,Mutable)) ~args loc
Expand Down Expand Up @@ -439,6 +440,12 @@ let lam_prim ~primitive:( p : Lambda.primitive) ~args loc : Lam.t =
(* Does not exist since we compile array in js backend unlike native backend *)


let convert_fn_attribute (attr : Lambda.function_attribute) : Lam.function_attribute =
match attr.inline with
| Always_inline -> Always_inline
| Never_inline -> Never_inline
| Unroll _
| Default_inline -> Default_inline



Expand Down Expand Up @@ -624,17 +631,18 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) : Lam.t * Lam_module_i
(** we need do this eargly in case [aux fn] add some wrapper *)
Lam.apply (convert_aux fn) (Ext_list.map args convert_aux ) loc App_na
| Lfunction
{kind; params; body }
{kind; params; body ; attr }
->
assert (kind = Curried);
let new_map,body = rename_optional_parameters Map_ident.empty params body in
let attr = convert_fn_attribute attr in
if Map_ident.is_empty new_map then
Lam.function_
Lam.function_ ~attr
~arity:(List.length params) ~params
~body:(convert_aux body)
else
let params = Ext_list.map params (fun x -> Map_ident.find_default new_map x x) in
Lam.function_
Lam.function_ ~attr
~arity:(List.length params) ~params
~body:(convert_aux body)

Expand Down
18 changes: 11 additions & 7 deletions jscomp/core/lam_eta_conversion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ let transform_under_supply n loc status fn args =
of an existing function which may cause inconsistency
*)
Lam.function_ ~arity:n ~params:extra_args
~attr:Default_inline
~body:(Lam.apply fn (Ext_list.append args extra_lambdas)
loc
status
Expand All @@ -74,6 +75,7 @@ let transform_under_supply n loc status fn args =

let rest : Lam.t =
Lam.function_ ~arity:n ~params:extra_args
~attr:Default_inline
~body:(Lam.apply fn (Ext_list.append args extra_lambdas)
loc
status
Expand Down Expand Up @@ -131,6 +133,7 @@ let unsafe_adjust_to_arity loc ~(to_:int) ?(from : int option) (fn : Lam.t) : La
match fn with
| Lfunction{params = [param]; body} ->
Lam.function_ ~arity:0
~attr:Default_inline
~params:[]
~body:(
Lam.let_ Alias param Lam.unit body
Expand All @@ -150,6 +153,7 @@ let unsafe_adjust_to_arity loc ~(to_:int) ?(from : int option) (fn : Lam.t) : La
Some partial_arg, Lam.var partial_arg in

let cont = Lam.function_
~attr:Default_inline
~arity:0
~params:[]
~body:(
Expand All @@ -168,7 +172,7 @@ let unsafe_adjust_to_arity loc ~(to_:int) ?(from : int option) (fn : Lam.t) : La
{[ fun x y -> f y ]}
*)
let extra_args = Ext_list.init (to_ - from) (fun _ -> Ident.create Literals.param) in
Lam.function_
Lam.function_ ~attr:Default_inline
~arity:to_
~params:(Ext_list.append params extra_args )
~body:(Lam.apply body (Ext_list.map extra_args Lam.var) loc App_na)
Expand All @@ -186,7 +190,7 @@ let unsafe_adjust_to_arity loc ~(to_:int) ?(from : int option) (fn : Lam.t) : La
in
let cont =
Lam.function_
~arity
~arity ~attr:Default_inline

~params:extra_args
~body:(
Expand Down Expand Up @@ -214,10 +218,10 @@ let unsafe_adjust_to_arity loc ~(to_:int) ?(from : int option) (fn : Lam.t) : La
let extra_outer_args, extra_inner_args = Ext_list.split_at params arity in
Lam.function_
~arity

~attr:Default_inline
~params:extra_outer_args
~body:(
Lam.function_ ~arity:(from - to_)
Lam.function_ ~arity:(from - to_) ~attr:Default_inline
~params:extra_inner_args ~body:body)
| _
->
Expand All @@ -234,12 +238,12 @@ let unsafe_adjust_to_arity loc ~(to_:int) ?(from : int option) (fn : Lam.t) : La
Some partial_arg, Lam.var partial_arg
in
let cont =
Lam.function_ ~arity:to_ ~params:extra_outer_args
Lam.function_ ~arity:to_ ~params:extra_outer_args ~attr:Default_inline
~body:(
let arity = from - to_ in
let extra_inner_args =
Ext_list.init arity (fun _ -> Ident.create Literals.param ) in
Lam.function_ ~arity ~params:extra_inner_args
Lam.function_ ~arity ~params:extra_inner_args ~attr:Default_inline
~body:(Lam.apply new_fn
(Ext_list.map_append extra_outer_args
(Ext_list.map extra_inner_args Lam.var)
Expand All @@ -265,7 +269,7 @@ let unsafe_adjust_to_arity loc ~(to_:int) ?(from : int option) (fn : Lam.t) : La
let partial_arg = Ext_ident.create Literals.partial_arg in
Some partial_arg, Lam.var partial_arg in

let cont = Lam.function_
let cont = Lam.function_ ~attr:Default_inline
~arity:0
~params:[]
~body:(
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/lam_pass_alpha_conversion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ let alpha_conversion (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
end
| Lprim {primitive; args ; loc} ->
Lam.prim ~primitive ~args:(Ext_list.map args simpl) loc
| Lfunction {arity; params; body = l} ->
| Lfunction {arity; params; body; attr} ->
(* Lam_mk.lfunction kind params (simpl l) *)
Lam.function_ ~arity ~params ~body:(simpl l)
Lam.function_ ~arity ~params ~body:(simpl body) ~attr
| Lswitch (l, {sw_failaction;
sw_consts;
sw_blocks;
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/lam_pass_deep_flatten.ml
Original file line number Diff line number Diff line change
Expand Up @@ -262,8 +262,8 @@ let deep_flatten
let args = Ext_list.map args aux in
Lam.prim ~primitive ~args loc

| Lfunction{arity; params; body = l} ->
Lam.function_ ~arity ~params ~body:(aux l)
| Lfunction{arity; params; body; attr} ->
Lam.function_ ~arity ~params ~body:(aux body) ~attr
| Lswitch(l, {sw_failaction;
sw_consts;
sw_blocks;
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/lam_pass_exits.ml
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,8 @@ let subst_helper (subst : subst_tbl) (query : int -> int) (lam : Lam.t) : Lam.t
| Lvar _|Lconst _ -> lam
| Lapply {ap_func; ap_args; ap_loc; ap_status } ->
Lam.apply (simplif ap_func) (Ext_list.map ap_args simplif) ap_loc ap_status
| Lfunction {arity; params; body} ->
Lam.function_ ~arity ~params ~body:(simplif body)
| Lfunction {arity; params; body; attr} ->
Lam.function_ ~arity ~params ~body:(simplif body) ~attr
| Llet (kind, v, l1, l2) ->
Lam.let_ kind v (simplif l1) (simplif l2)
| Lletrec (bindings, body) ->
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/lam_pass_lets_dce.ml
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ let lets_helper (count_var : Ident.t -> Lam_pass_count.used_info) lam : Lam.t =

| Lapply{ap_func = l1; ap_args = ll; ap_loc = loc; ap_status = status} ->
Lam.apply (simplif l1) (Ext_list.map ll simplif) loc status
| Lfunction{arity; params; body = l} ->
Lam.function_ ~arity ~params ~body:(simplif l)
| Lfunction{arity; params; body; attr} ->
Lam.function_ ~arity ~params ~body:(simplif body) ~attr
| Lconst _ -> lam
| Lletrec(bindings, body) ->
Lam.letrec
Expand Down
8 changes: 4 additions & 4 deletions jscomp/core/lam_pass_remove_alias.ml
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ let simplify_alias
let normal () = Lam.apply ( simpl fn) (Ext_list.map args simpl) loc status in
begin
match Hash_ident.find_opt meta.ident_tbl v with
| Some (FunctionId {lambda = Some(Lfunction {params; body} as _m,
| Some (FunctionId {lambda = Some(Lfunction ({params; body} as m),
rec_flag)
})
->
Expand All @@ -177,7 +177,7 @@ let simplify_alias
end
else
if (* Lam_analysis.size body < Lam_analysis.small_inline_size *)
Lam_analysis.ok_to_inline_fun_when_app ~body params args
Lam_analysis.ok_to_inline_fun_when_app m args
then

(* let param_map = *)
Expand Down Expand Up @@ -223,8 +223,8 @@ let simplify_alias

| Lapply { ap_func = l1; ap_args = ll; ap_loc = loc; ap_status = status} ->
Lam.apply (simpl l1) (Ext_list.map ll simpl) loc status
| Lfunction {arity; params; body = l}
-> Lam.function_ ~arity ~params ~body:(simpl l)
| Lfunction {arity; params; body; attr}
-> Lam.function_ ~arity ~params ~body:(simpl body) ~attr
| Lswitch (l, {sw_failaction;
sw_consts;
sw_blocks;
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/lam_subst.ml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
| Lconst _ -> x
| Lapply{ap_func; ap_args; ap_loc; ap_status} ->
Lam.apply (subst_aux ap_func) (Ext_list.map ap_args subst_aux ) ap_loc ap_status
| Lfunction {arity; params; body} ->
Lam.function_ ~arity ~params ~body:(subst_aux body)
| Lfunction {arity; params; body; attr} ->
Lam.function_ ~arity ~params ~body:(subst_aux body) ~attr
| Llet(str, id, arg, body) ->
Lam.let_ str id (subst_aux arg) (subst_aux body)
| Lletrec(decl, body) ->
Expand Down
Loading