Skip to content

Commit 07064e8

Browse files
authored
Merge pull request #4605 from BuckleScript/inline_support
take @@inline attribute into consideration
2 parents 178ef22 + 1e8a084 commit 07064e8

22 files changed

+880
-719
lines changed

jscomp/core/lam.ml

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ type apply_status =
2929
| App_infer_full
3030
| App_uncurry
3131

32+
type function_attribute =
33+
| Always_inline
34+
| Never_inline
35+
| Default_inline
3236

3337
module Types = struct
3438

@@ -39,6 +43,12 @@ module Types = struct
3943
sw_blocks: (int * t) list;
4044
sw_failaction : t option;
4145
sw_names : Lambda.switch_names option }
46+
and lfunction = {
47+
arity : int ;
48+
params : ident list ;
49+
body : t;
50+
attr : function_attribute
51+
}
4252
(*
4353
Invariant:
4454
length (sw_consts) <= sw_consts_full
@@ -86,10 +96,7 @@ module Types = struct
8696
| Lglobal_module of ident
8797
| Lconst of Lam_constant.t
8898
| Lapply of apply_info
89-
| Lfunction of { arity : int ;
90-
params : ident list ;
91-
body : t
92-
}
99+
| Lfunction of lfunction
93100
| Llet of Lam_compat.let_kind * ident * t * t
94101
| Lletrec of (ident * t) list * t
95102
| Lprim of prim_info
@@ -132,17 +139,21 @@ module X = struct
132139
ap_loc : Location.t;
133140
ap_status : apply_status
134141
}
142+
and lfunction = Types.lfunction =
143+
{
144+
arity : int ;
145+
params : ident list ;
146+
body : t;
147+
attr : function_attribute
148+
}
135149
and t
136150
= Types.t
137151
=
138152
| Lvar of ident
139153
| Lglobal_module of ident
140154
| Lconst of Lam_constant.t
141155
| Lapply of apply_info
142-
| Lfunction of { arity : int ;
143-
params : ident list ;
144-
body : t
145-
}
156+
| Lfunction of lfunction
146157
| Llet of Lam_compat.let_kind * ident * t * t
147158
| Lletrec of (ident * t) list * t
148159
| Lprim of prim_info
@@ -175,9 +186,9 @@ let inner_map
175186
let ap_func = f ap_func in
176187
let ap_args = Ext_list.map ap_args f in
177188
Lapply { ap_func ; ap_args; ap_loc; ap_status }
178-
| Lfunction({body; arity; params } ) ->
189+
| Lfunction({body; arity; params ; attr } ) ->
179190
let body = f body in
180-
Lfunction {body; arity; params}
191+
Lfunction {body; arity; params; attr}
181192
| Llet(str, id, arg, body) ->
182193
let arg = f arg in let body = f body in
183194
Llet(str,id,arg,body)
@@ -428,8 +439,8 @@ let rec seq (a : t) b : t =
428439
let var id : t = Lvar id
429440
let global_module id = Lglobal_module id
430441
let const ct : t = Lconst ct
431-
let function_ ~arity ~params ~body : t =
432-
Lfunction { arity; params ; body}
442+
let function_ ~attr ~arity ~params ~body : t =
443+
Lfunction { arity; params ; body; attr}
433444

434445
let let_ kind id e body : t
435446
= Llet (kind,id,e,body)

jscomp/core/lam.mli

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@
2424

2525

2626

27-
27+
type function_attribute =
28+
| Always_inline
29+
| Never_inline
30+
| Default_inline
2831

2932
type ident = Ident.t
3033

@@ -45,7 +48,12 @@ and apply_info = private
4548
ap_loc : Location.t;
4649
ap_status : apply_status
4750
}
48-
51+
and lfunction = {
52+
arity : int ;
53+
params : ident list ;
54+
body : t ;
55+
attr : function_attribute;
56+
}
4957
and prim_info = private
5058
{ primitive : Lam_primitive.t ;
5159
args : t list ;
@@ -56,10 +64,7 @@ and t = private
5664
| Lglobal_module of ident
5765
| Lconst of Lam_constant.t
5866
| Lapply of apply_info
59-
| Lfunction of { arity : int ;
60-
params : ident list ;
61-
body : t
62-
}
67+
| Lfunction of lfunction
6368
| Llet of Lam_compat.let_kind * ident * t * t
6469
| Lletrec of (ident * t) list * t
6570
| Lprim of prim_info
@@ -102,6 +107,7 @@ val const : Lam_constant.t -> t
102107

103108
val apply : t -> t list -> Location.t -> apply_status -> t
104109
val function_ :
110+
attr:function_attribute ->
105111
arity:int ->
106112
params:ident list ->
107113
body:t -> t

jscomp/core/lam_analysis.ml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -334,14 +334,18 @@ let destruct_pattern (body : Lam.t) params args =
334334

335335
(** Hints to inlining *)
336336
let ok_to_inline_fun_when_app
337-
~(body : Lam.t)
338-
(params : Ident.t list)
337+
(m : Lam.lfunction)
339338
(args : Lam.t list) =
340-
let s = size body in
341-
s < small_inline_size ||
342-
(destruct_pattern body params args) ||
343-
(args_all_const args &&
344-
(s < 10 && no_side_effects body ))
339+
match m.attr with
340+
| Always_inline -> true
341+
| Never_inline -> false
342+
| Default_inline ->
343+
let Lam.{body; params} = m in
344+
let s = size body in
345+
s < small_inline_size ||
346+
(destruct_pattern body params args) ||
347+
(args_all_const args &&
348+
(s < 10 && no_side_effects body ))
345349

346350

347351

jscomp/core/lam_analysis.mli

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ val no_side_effects : Lam.t -> bool
3434

3535
val size : Lam.t -> int
3636

37-
val ok_to_inline_fun_when_app : body:Lam.t -> Lam.ident list -> Lam.t list -> bool
37+
val ok_to_inline_fun_when_app :
38+
Lam.lfunction ->
39+
Lam.t list ->
40+
bool
3841

3942

4043

jscomp/core/lam_bounded_vars.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ let rewrite (map : _ Hash_ident.t)
9191
let bindings = Ext_list.map2 vars bindings (fun var (_,l) -> var, aux l) in
9292
let body = aux body in
9393
Lam.letrec bindings body
94-
| Lfunction{arity; params; body} ->
94+
| Lfunction{arity; params; body; attr} ->
9595
let params = Ext_list.map params rebind in
9696
let body = aux body in
97-
Lam.function_ ~arity ~params ~body
97+
Lam.function_ ~arity ~params ~body ~attr
9898
| Lstaticcatch(l1, (i,xs), l2) ->
9999
let l1 = aux l1 in
100100
let xs = Ext_list.map xs rebind in

jscomp/core/lam_convert.ml

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@ let lam_prim ~primitive:( p : Lambda.primitive) ~args loc : Lam.t =
294294
let args =
295295
[ Lam.const Const_js_false ;
296296
(* FIXME: arity 0 does not get proper supported*)
297-
prim ~primitive:(Pjs_fn_make 0) ~args:[Lam.function_ ~arity:1 ~params:[Ident.create "param"] ~body:computation]
297+
prim ~primitive:(Pjs_fn_make 0) ~args:[Lam.function_ ~arity:1 ~params:[Ident.create "param"] ~body:computation
298+
~attr:Default_inline]
298299
loc
299300
] in
300301
prim ~primitive:(Pmakeblock (tag,lazy_block_info,Mutable)) ~args loc
@@ -439,6 +440,12 @@ let lam_prim ~primitive:( p : Lambda.primitive) ~args loc : Lam.t =
439440
(* Does not exist since we compile array in js backend unlike native backend *)
440441

441442

443+
let convert_fn_attribute (attr : Lambda.function_attribute) : Lam.function_attribute =
444+
match attr.inline with
445+
| Always_inline -> Always_inline
446+
| Never_inline -> Never_inline
447+
| Unroll _
448+
| Default_inline -> Default_inline
442449

443450

444451

@@ -624,17 +631,18 @@ let convert (exports : Set_ident.t) (lam : Lambda.lambda) : Lam.t * Lam_module_i
624631
(** we need do this eargly in case [aux fn] add some wrapper *)
625632
Lam.apply (convert_aux fn) (Ext_list.map args convert_aux ) loc App_na
626633
| Lfunction
627-
{kind; params; body }
634+
{kind; params; body ; attr }
628635
->
629636
assert (kind = Curried);
630637
let new_map,body = rename_optional_parameters Map_ident.empty params body in
638+
let attr = convert_fn_attribute attr in
631639
if Map_ident.is_empty new_map then
632-
Lam.function_
640+
Lam.function_ ~attr
633641
~arity:(List.length params) ~params
634642
~body:(convert_aux body)
635643
else
636644
let params = Ext_list.map params (fun x -> Map_ident.find_default new_map x x) in
637-
Lam.function_
645+
Lam.function_ ~attr
638646
~arity:(List.length params) ~params
639647
~body:(convert_aux body)
640648

jscomp/core/lam_eta_conversion.ml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ let transform_under_supply n loc status fn args =
6666
of an existing function which may cause inconsistency
6767
*)
6868
Lam.function_ ~arity:n ~params:extra_args
69+
~attr:Default_inline
6970
~body:(Lam.apply fn (Ext_list.append args extra_lambdas)
7071
loc
7172
status
@@ -74,6 +75,7 @@ let transform_under_supply n loc status fn args =
7475

7576
let rest : Lam.t =
7677
Lam.function_ ~arity:n ~params:extra_args
78+
~attr:Default_inline
7779
~body:(Lam.apply fn (Ext_list.append args extra_lambdas)
7880
loc
7981
status
@@ -131,6 +133,7 @@ let unsafe_adjust_to_arity loc ~(to_:int) ?(from : int option) (fn : Lam.t) : La
131133
match fn with
132134
| Lfunction{params = [param]; body} ->
133135
Lam.function_ ~arity:0
136+
~attr:Default_inline
134137
~params:[]
135138
~body:(
136139
Lam.let_ Alias param Lam.unit body
@@ -150,6 +153,7 @@ let unsafe_adjust_to_arity loc ~(to_:int) ?(from : int option) (fn : Lam.t) : La
150153
Some partial_arg, Lam.var partial_arg in
151154

152155
let cont = Lam.function_
156+
~attr:Default_inline
153157
~arity:0
154158
~params:[]
155159
~body:(
@@ -168,7 +172,7 @@ let unsafe_adjust_to_arity loc ~(to_:int) ?(from : int option) (fn : Lam.t) : La
168172
{[ fun x y -> f y ]}
169173
*)
170174
let extra_args = Ext_list.init (to_ - from) (fun _ -> Ident.create Literals.param) in
171-
Lam.function_
175+
Lam.function_ ~attr:Default_inline
172176
~arity:to_
173177
~params:(Ext_list.append params extra_args )
174178
~body:(Lam.apply body (Ext_list.map extra_args Lam.var) loc App_na)
@@ -186,7 +190,7 @@ let unsafe_adjust_to_arity loc ~(to_:int) ?(from : int option) (fn : Lam.t) : La
186190
in
187191
let cont =
188192
Lam.function_
189-
~arity
193+
~arity ~attr:Default_inline
190194
191195
~params:extra_args
192196
~body:(
@@ -214,10 +218,10 @@ let unsafe_adjust_to_arity loc ~(to_:int) ?(from : int option) (fn : Lam.t) : La
214218
let extra_outer_args, extra_inner_args = Ext_list.split_at params arity in
215219
Lam.function_
216220
~arity
217-
221+
~attr:Default_inline
218222
~params:extra_outer_args
219223
~body:(
220-
Lam.function_ ~arity:(from - to_)
224+
Lam.function_ ~arity:(from - to_) ~attr:Default_inline
221225
~params:extra_inner_args ~body:body)
222226
| _
223227
->
@@ -234,12 +238,12 @@ let unsafe_adjust_to_arity loc ~(to_:int) ?(from : int option) (fn : Lam.t) : La
234238
Some partial_arg, Lam.var partial_arg
235239
in
236240
let cont =
237-
Lam.function_ ~arity:to_ ~params:extra_outer_args
241+
Lam.function_ ~arity:to_ ~params:extra_outer_args ~attr:Default_inline
238242
~body:(
239243
let arity = from - to_ in
240244
let extra_inner_args =
241245
Ext_list.init arity (fun _ -> Ident.create Literals.param ) in
242-
Lam.function_ ~arity ~params:extra_inner_args
246+
Lam.function_ ~arity ~params:extra_inner_args ~attr:Default_inline
243247
~body:(Lam.apply new_fn
244248
(Ext_list.map_append extra_outer_args
245249
(Ext_list.map extra_inner_args Lam.var)
@@ -265,7 +269,7 @@ let unsafe_adjust_to_arity loc ~(to_:int) ?(from : int option) (fn : Lam.t) : La
265269
let partial_arg = Ext_ident.create Literals.partial_arg in
266270
Some partial_arg, Lam.var partial_arg in
267271
268-
let cont = Lam.function_
272+
let cont = Lam.function_ ~attr:Default_inline
269273
~arity:0
270274
~params:[]
271275
~body:(

jscomp/core/lam_pass_alpha_conversion.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,9 @@ let alpha_conversion (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
8686
end
8787
| Lprim {primitive; args ; loc} ->
8888
Lam.prim ~primitive ~args:(Ext_list.map args simpl) loc
89-
| Lfunction {arity; params; body = l} ->
89+
| Lfunction {arity; params; body; attr} ->
9090
(* Lam_mk.lfunction kind params (simpl l) *)
91-
Lam.function_ ~arity ~params ~body:(simpl l)
91+
Lam.function_ ~arity ~params ~body:(simpl body) ~attr
9292
| Lswitch (l, {sw_failaction;
9393
sw_consts;
9494
sw_blocks;

jscomp/core/lam_pass_deep_flatten.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,8 @@ let deep_flatten
262262
let args = Ext_list.map args aux in
263263
Lam.prim ~primitive ~args loc
264264

265-
| Lfunction{arity; params; body = l} ->
266-
Lam.function_ ~arity ~params ~body:(aux l)
265+
| Lfunction{arity; params; body; attr} ->
266+
Lam.function_ ~arity ~params ~body:(aux body) ~attr
267267
| Lswitch(l, {sw_failaction;
268268
sw_consts;
269269
sw_blocks;

jscomp/core/lam_pass_exits.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,8 @@ let subst_helper (subst : subst_tbl) (query : int -> int) (lam : Lam.t) : Lam.t
217217
| Lvar _|Lconst _ -> lam
218218
| Lapply {ap_func; ap_args; ap_loc; ap_status } ->
219219
Lam.apply (simplif ap_func) (Ext_list.map ap_args simplif) ap_loc ap_status
220-
| Lfunction {arity; params; body} ->
221-
Lam.function_ ~arity ~params ~body:(simplif body)
220+
| Lfunction {arity; params; body; attr} ->
221+
Lam.function_ ~arity ~params ~body:(simplif body) ~attr
222222
| Llet (kind, v, l1, l2) ->
223223
Lam.let_ kind v (simplif l1) (simplif l2)
224224
| Lletrec (bindings, body) ->

0 commit comments

Comments
 (0)