Skip to content

Commit 17a0284

Browse files
committed
code split
1 parent 3013a4d commit 17a0284

9 files changed

+421
-198
lines changed

jscomp/core/js_pass_get_used.ml

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
(* Copyright (C) 2020- Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
26+
(** Update ident info use cases, it is a non pure function,
27+
it will annotate [program] with some meta data
28+
TODO: Ident Hash could be improved,
29+
since in this case it can not be global?
30+
31+
*)
32+
let count_collects () =
33+
object (self)
34+
inherit Js_fold.fold as super
35+
(* collect used status*)
36+
val stats : int Hash_ident.t = Hash_ident.create 83
37+
(* collect all def sites *)
38+
val defined_idents : J.variable_declaration Hash_ident.t = Hash_ident.create 83
39+
40+
val mutable my_export_set : Set_ident.t = Set_ident.empty
41+
42+
method add_use id =
43+
Hash_ident.add_or_update stats id 1 ~update:succ
44+
method! program x =
45+
my_export_set <- x.export_set ;
46+
super#program x
47+
method! variable_declaration
48+
({ident; value ; property = _ ; ident_info = _} as v)
49+
=
50+
Hash_ident.add defined_idents ident v;
51+
match value with
52+
| None ->
53+
self
54+
| Some x
55+
-> self#expression x
56+
method! ident id = self#add_use id; self
57+
method get_stats =
58+
Hash_ident.iter defined_idents (fun ident v ->
59+
if Set_ident.mem my_export_set ident then
60+
Js_op_util.update_used_stats v.ident_info Exported
61+
else
62+
let pure =
63+
match v.value with
64+
| None -> false (* can not happen *)
65+
| Some x -> Js_analyzer.no_side_effect_expression x in
66+
match Hash_ident.find_opt stats ident with
67+
| None ->
68+
Js_op_util.update_used_stats v.ident_info
69+
(if pure then Dead_pure else Dead_non_pure)
70+
| Some num ->
71+
if num = 1 then
72+
Js_op_util.update_used_stats v.ident_info
73+
(if pure then Once_pure else Used)
74+
) ; defined_idents
75+
end
76+
77+
78+
let get_stats (program : J.program) : J.variable_declaration Hash_ident.t
79+
= ((count_collects ()) #program program) #get_stats
80+

jscomp/core/js_pass_get_used.mli

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
(* Copyright (C) 2020- Authors of BuckleScript
2+
*
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU Lesser General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* In addition to the permissions granted to you by the LGPL, you may combine
9+
* or link a "work that uses the Library" with a publicly distributed version
10+
* of this file to produce a combined library or application, then distribute
11+
* that combined work under the terms of your choosing, with no requirement
12+
* to comply with the obligations normally placed on you by section 4 of the
13+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
14+
* should you choose to use a later version).
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Lesser General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Lesser General Public License
22+
* along with this program; if not, write to the Free Software
23+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
24+
25+
26+
27+
val get_stats:
28+
J.program -> J.variable_declaration Hash_ident.t

jscomp/core/js_pass_tailcall_inline.ml

+1-57
Original file line numberDiff line numberDiff line change
@@ -41,62 +41,6 @@ module S = Js_stmt_make
4141
(* module E = Js_exp_make *)
4242

4343

44-
(** Update ident info use cases, it is a non pure function,
45-
it will annotate [program] with some meta data
46-
TODO: Ident Hash could be improved,
47-
since in this case it can not be global?
48-
49-
*)
50-
let count_collects () =
51-
object (self)
52-
inherit Js_fold.fold as super
53-
(* collect used status*)
54-
val stats : int Hash_ident.t = Hash_ident.create 83
55-
(* collect all def sites *)
56-
val defined_idents : J.variable_declaration Hash_ident.t = Hash_ident.create 83
57-
58-
val mutable my_export_set : Set_ident.t = Set_ident.empty
59-
60-
61-
method add_use id =
62-
Hash_ident.add_or_update stats id 1 ~update:succ
63-
method! program x =
64-
my_export_set <- x.export_set ;
65-
66-
super#program x
67-
method! variable_declaration
68-
({ident; value ; property = _ ; ident_info = _} as v)
69-
=
70-
Hash_ident.add defined_idents ident v;
71-
match value with
72-
| None ->
73-
self
74-
| Some x
75-
-> self#expression x
76-
method! ident id = self#add_use id; self
77-
method get_stats =
78-
Hash_ident.iter defined_idents (fun ident v ->
79-
if Set_ident.mem my_export_set ident then
80-
Js_op_util.update_used_stats v.ident_info Exported
81-
else
82-
let pure =
83-
match v.value with
84-
| None -> false (* can not happen *)
85-
| Some x -> Js_analyzer.no_side_effect_expression x in
86-
match Hash_ident.find_opt stats ident with
87-
| None ->
88-
Js_op_util.update_used_stats v.ident_info
89-
(if pure then Dead_pure else Dead_non_pure)
90-
| Some num ->
91-
if num = 1 then
92-
Js_op_util.update_used_stats v.ident_info
93-
(if pure then Once_pure else Used)
94-
) ; defined_idents
95-
end
96-
97-
98-
let get_stats (program : J.program) : J.variable_declaration Hash_ident.t
99-
= ((count_collects ()) #program program) #get_stats
10044

10145

10246
(* 1. recursive value ? let rec x = 1 :: x
@@ -227,7 +171,7 @@ let subst (export_set : Set_ident.t) stats =
227171

228172

229173
let tailcall_inline (program : J.program) =
230-
let stats = get_stats program in
174+
let stats = Js_pass_get_used.get_stats program in
231175
let export_set = program.export_set in
232176
(subst export_set stats )#program program
233177

lib/4.06.1/unstable/js_compiler.ml

+103-46
Original file line numberDiff line numberDiff line change
@@ -392894,9 +392894,9 @@ let program js =
392894392894
(scope_pass # program js ) # get_loop_mutable_values
392895392895

392896392896
end
392897-
module Js_pass_tailcall_inline : sig
392898-
#1 "js_pass_tailcall_inline.mli"
392899-
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
392897+
module Js_pass_get_used : sig
392898+
#1 "js_pass_get_used.mli"
392899+
(* Copyright (C) 2020- Authors of BuckleScript
392900392900
*
392901392901
* This program is free software: you can redistribute it and/or modify
392902392902
* it under the terms of the GNU Lesser General Public License as published by
@@ -392922,29 +392922,11 @@ module Js_pass_tailcall_inline : sig
392922392922

392923392923

392924392924

392925-
392926-
392927-
392928-
392929-
392930-
(** This pass detect functions used once and if it is used in used
392931-
in the tail position, it will get inlined, this will help
392932-
remove some common use cases like This
392933-
{[
392934-
let length x =
392935-
let rec aux n x =
392936-
match x with
392937-
| [] -> n
392938-
| _ :: rest -> aux (n + 1) rest in
392939-
aux 0 x
392940-
]}
392941-
*)
392942-
392943-
val tailcall_inline : J.program -> J.program
392944-
392925+
val get_stats:
392926+
J.program -> J.variable_declaration Hash_ident.t
392945392927
end = struct
392946-
#1 "js_pass_tailcall_inline.ml"
392947-
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
392928+
#1 "js_pass_get_used.ml"
392929+
(* Copyright (C) 2020- Authors of BuckleScript
392948392930
*
392949392931
* This program is free software: you can redistribute it and/or modify
392950392932
* it under the terms of the GNU Lesser General Public License as published by
@@ -392969,24 +392951,6 @@ end = struct
392969392951
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
392970392952

392971392953

392972-
392973-
392974-
392975-
392976-
(* When we inline a function call, if we don't do a beta-reduction immediately, there is
392977-
a chance that it is ignored, (we can not assume that each pass is robust enough)
392978-
392979-
After we do inlining, it makes sense to do another constant folding and propogation
392980-
*)
392981-
392982-
(* Check: shall we inline functions with while loop? if it is used only once,
392983-
it makes sense to inline it
392984-
*)
392985-
392986-
module S = Js_stmt_make
392987-
(* module E = Js_exp_make *)
392988-
392989-
392990392954
(** Update ident info use cases, it is a non pure function,
392991392955
it will annotate [program] with some meta data
392992392956
TODO: Ident Hash could be improved,
@@ -393003,12 +392967,10 @@ let count_collects () =
393003392967

393004392968
val mutable my_export_set : Set_ident.t = Set_ident.empty
393005392969

393006-
393007392970
method add_use id =
393008392971
Hash_ident.add_or_update stats id 1 ~update:succ
393009392972
method! program x =
393010392973
my_export_set <- x.export_set ;
393011-
393012392974
super#program x
393013392975
method! variable_declaration
393014392976
({ident; value ; property = _ ; ident_info = _} as v)
@@ -393043,6 +393005,101 @@ let count_collects () =
393043393005

393044393006
let get_stats (program : J.program) : J.variable_declaration Hash_ident.t
393045393007
= ((count_collects ()) #program program) #get_stats
393008+
393009+
end
393010+
module Js_pass_tailcall_inline : sig
393011+
#1 "js_pass_tailcall_inline.mli"
393012+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
393013+
*
393014+
* This program is free software: you can redistribute it and/or modify
393015+
* it under the terms of the GNU Lesser General Public License as published by
393016+
* the Free Software Foundation, either version 3 of the License, or
393017+
* (at your option) any later version.
393018+
*
393019+
* In addition to the permissions granted to you by the LGPL, you may combine
393020+
* or link a "work that uses the Library" with a publicly distributed version
393021+
* of this file to produce a combined library or application, then distribute
393022+
* that combined work under the terms of your choosing, with no requirement
393023+
* to comply with the obligations normally placed on you by section 4 of the
393024+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
393025+
* should you choose to use a later version).
393026+
*
393027+
* This program is distributed in the hope that it will be useful,
393028+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
393029+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
393030+
* GNU Lesser General Public License for more details.
393031+
*
393032+
* You should have received a copy of the GNU Lesser General Public License
393033+
* along with this program; if not, write to the Free Software
393034+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
393035+
393036+
393037+
393038+
393039+
393040+
393041+
393042+
393043+
(** This pass detect functions used once and if it is used in used
393044+
in the tail position, it will get inlined, this will help
393045+
remove some common use cases like This
393046+
{[
393047+
let length x =
393048+
let rec aux n x =
393049+
match x with
393050+
| [] -> n
393051+
| _ :: rest -> aux (n + 1) rest in
393052+
aux 0 x
393053+
]}
393054+
*)
393055+
393056+
val tailcall_inline : J.program -> J.program
393057+
393058+
end = struct
393059+
#1 "js_pass_tailcall_inline.ml"
393060+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
393061+
*
393062+
* This program is free software: you can redistribute it and/or modify
393063+
* it under the terms of the GNU Lesser General Public License as published by
393064+
* the Free Software Foundation, either version 3 of the License, or
393065+
* (at your option) any later version.
393066+
*
393067+
* In addition to the permissions granted to you by the LGPL, you may combine
393068+
* or link a "work that uses the Library" with a publicly distributed version
393069+
* of this file to produce a combined library or application, then distribute
393070+
* that combined work under the terms of your choosing, with no requirement
393071+
* to comply with the obligations normally placed on you by section 4 of the
393072+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
393073+
* should you choose to use a later version).
393074+
*
393075+
* This program is distributed in the hope that it will be useful,
393076+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
393077+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
393078+
* GNU Lesser General Public License for more details.
393079+
*
393080+
* You should have received a copy of the GNU Lesser General Public License
393081+
* along with this program; if not, write to the Free Software
393082+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
393083+
393084+
393085+
393086+
393087+
393088+
393089+
(* When we inline a function call, if we don't do a beta-reduction immediately, there is
393090+
a chance that it is ignored, (we can not assume that each pass is robust enough)
393091+
393092+
After we do inlining, it makes sense to do another constant folding and propogation
393093+
*)
393094+
393095+
(* Check: shall we inline functions with while loop? if it is used only once,
393096+
it makes sense to inline it
393097+
*)
393098+
393099+
module S = Js_stmt_make
393100+
(* module E = Js_exp_make *)
393101+
393102+
393046393103

393047393104

393048393105
(* 1. recursive value ? let rec x = 1 :: x
@@ -393173,7 +393230,7 @@ let subst (export_set : Set_ident.t) stats =
393173393230

393174393231

393175393232
let tailcall_inline (program : J.program) =
393176-
let stats = get_stats program in
393233+
let stats = Js_pass_get_used.get_stats program in
393177393234
let export_set = program.export_set in
393178393235
(subst export_set stats )#program program
393179393236

0 commit comments

Comments
 (0)