Skip to content

Commit 5824fe7

Browse files
authored
Merge pull request #1089 from bloomberg/remove_Module_opt
recursive apply field opt, bug fix for alias_tbl
2 parents 708a516 + 816c4bd commit 5824fe7

10 files changed

+96
-34
lines changed

jscomp/bin/whole_compiler.ml

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69698,13 +69698,13 @@ val string_of_primitive : Lam.primitive -> string
6969869698
val kind_of_lambda_block : Lam_stats.boxed_nullable -> Lam.t list -> Lam_stats.kind
6969969699

6970069700
val field_flatten_get :
69701-
Lam.t -> Ident.t -> int -> Lam_stats.ident_tbl -> Lam.t
69701+
(unit -> Lam.t) -> Ident.t -> int -> Lam_stats.ident_tbl -> Lam.t
6970269702

6970369703

6970469704

6970569705

6970669706

69707-
val alias : Lam_stats.meta ->
69707+
val alias_ident_or_global : Lam_stats.meta ->
6970869708
Ident.t -> Ident.t -> Lam_stats.kind -> Lambda.let_kind -> unit
6970969709

6971069710

@@ -69922,7 +69922,7 @@ let refine_let
6992269922
| None , _, _ ->
6992369923
Lam.let_ Strict param arg l
6992469924

69925-
let alias (meta : Lam_stats.meta) (k:Ident.t) (v:Ident.t)
69925+
let alias_ident_or_global (meta : Lam_stats.meta) (k:Ident.t) (v:Ident.t)
6992669926
(v_kind : Lam_stats.kind) (let_kind : Lambda.let_kind) =
6992769927
(** treat rec as Strict, k is assigned to v
6992869928
{[ let k = v ]}
@@ -70002,13 +70002,13 @@ let field_flatten_get
7000270002
~args:[Lam.prim ~primitive:(Pgetglobal g) ~args:[] Location.none] Location.none
7000370003
| Some (ImmutableBlock (arr, _)) ->
7000470004
begin match arr.(i) with
70005-
| NA -> lam
70005+
| NA -> lam ()
7000670006
| SimpleForm l -> l
7000770007
end
7000870008
| Some (Constant (Const_block (_,_,ls))) ->
7000970009
Lam.const (List.nth ls i)
7001070010
| Some _
70011-
| None -> lam
70011+
| None -> lam ()
7001270012

7001370013

7001470014
(* TODO: check that if label belongs to a different
@@ -93705,12 +93705,12 @@ let collect_helper (meta : Lam_stats.meta) (lam : Lam.t) =
9370593705

9370693706
| Lprim {primitive = Pgetglobal v; args = []; _}
9370793707
->
93708-
Lam_util.alias meta ident v (Module v) kind;
93708+
Lam_util.alias_ident_or_global meta ident v (Module v) kind;
9370993709
| Lvar v
9371093710
->
9371193711
(
9371293712
(* if Ident.global v then *)
93713-
Lam_util.alias meta ident v NA kind
93713+
Lam_util.alias_ident_or_global meta ident v NA kind
9371493714
(* enven for not subsitution, it still propogate some properties *)
9371593715
(* else () *)
9371693716
)
@@ -95438,6 +95438,8 @@ val simplify_alias :
9543895438
Lam.t ->
9543995439
Lam.t
9544095440

95441+
95442+
9544195443
end = struct
9544295444
#1 "lam_pass_remove_alias.ml"
9544395445
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -95481,7 +95483,11 @@ let simplify_alias
9548195483
| Lvar v ->
9548295484
begin match (Ident_hashtbl.find_opt meta.alias_tbl v) with
9548395485
| None -> lam
95484-
| Some v -> Lam.var v
95486+
| Some v ->
95487+
if Ident.persistent v then
95488+
Lam.prim ~primitive:(Pgetglobal v) ~args:[] Location.none
95489+
else
95490+
Lam.var v
9548595491
(* This is wrong
9548695492
currently alias table has info
9548795493
include -> Array
@@ -95501,21 +95507,26 @@ let simplify_alias
9550195507
since global module access is not the same as local module
9550295508
TODO:
9550395509
since we aliased k, so it's safe to remove it?
95510+
no, we should not shake away code just by [Ident_set.mem k meta.export_idents ]
95511+
in that case, we should provide strong guarantee that all [k] will be substitued
9550495512
*)
9550595513
let v = simpl l in
95506-
if Ident_set.mem k meta.export_idents
95507-
then
95508-
Lam.let_ kind k g v
95514+
Lam.let_ kind k g v
9550995515
(* in this case it is preserved, but will still be simplified
9551095516
for the inner expression
9551195517
*)
95512-
else v
95518+
9551395519
| Lprim {primitive = (Pfield (i,_) as primitive); args = [arg]; loc} ->
9551495520
(* ATTENTION:
9551595521
Main use case, we should detect inline all immutable block .. *)
95516-
begin match (* simpl*) arg with
95517-
| Lvar v ->
95518-
Lam_util.field_flatten_get lam
95522+
begin match simpl arg with
95523+
| Lprim {primitive = Pgetglobal g; args= []} ->
95524+
Lam.prim
95525+
~primitive:(Pfield(i,Lambda.Fld_na))
95526+
~args:[Lam.prim ~primitive:(Pgetglobal g) ~args:[] Location.none]
95527+
loc
95528+
| Lvar v as l->
95529+
Lam_util.field_flatten_get (fun _ -> Lam.prim ~primitive ~args:[l] loc )
9551995530
v i meta.ident_tbl
9552095531
| _ ->
9552195532
Lam.prim ~primitive ~args:[simpl arg] loc
@@ -95739,6 +95750,8 @@ let simplify_alias
9573995750
in
9574095751
simpl lam
9574195752

95753+
95754+
9574295755
end
9574395756
module Ext_option : sig
9574495757
#1 "ext_option.mli"

jscomp/core/lam_compile_group.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ let lambda_as_module
370370
begin
371371
Js_config.set_current_file filename ;
372372
#if BS_DEBUG then
373-
Js_config.set_debug_file "exception_alias.ml";
373+
Js_config.set_debug_file "test_include.ml";
374374
#end
375375
let lambda_output = compile ~filename output_prefix env sigs lam in
376376
let (//) = Filename.concat in

jscomp/core/lam_pass_collect.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ let collect_helper (meta : Lam_stats.meta) (lam : Lam.t) =
9090

9191
| Lprim {primitive = Pgetglobal v; args = []; _}
9292
->
93-
Lam_util.alias meta ident v (Module v) kind;
93+
Lam_util.alias_ident_or_global meta ident v (Module v) kind;
9494
| Lvar v
9595
->
9696
(
9797
(* if Ident.global v then *)
98-
Lam_util.alias meta ident v NA kind
98+
Lam_util.alias_ident_or_global meta ident v NA kind
9999
(* enven for not subsitution, it still propogate some properties *)
100100
(* else () *)
101101
)

jscomp/core/lam_pass_remove_alias.ml

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ let simplify_alias
3939
| Lvar v ->
4040
begin match (Ident_hashtbl.find_opt meta.alias_tbl v) with
4141
| None -> lam
42-
| Some v -> Lam.var v
42+
| Some v ->
43+
if Ident.persistent v then
44+
Lam.prim ~primitive:(Pgetglobal v) ~args:[] Location.none
45+
else
46+
Lam.var v
4347
(* This is wrong
4448
currently alias table has info
4549
include -> Array
@@ -59,21 +63,26 @@ let simplify_alias
5963
since global module access is not the same as local module
6064
TODO:
6165
since we aliased k, so it's safe to remove it?
66+
no, we should not shake away code just by [Ident_set.mem k meta.export_idents ]
67+
in that case, we should provide strong guarantee that all [k] will be substitued
6268
*)
6369
let v = simpl l in
64-
if Ident_set.mem k meta.export_idents
65-
then
66-
Lam.let_ kind k g v
70+
Lam.let_ kind k g v
6771
(* in this case it is preserved, but will still be simplified
6872
for the inner expression
6973
*)
70-
else v
74+
7175
| Lprim {primitive = (Pfield (i,_) as primitive); args = [arg]; loc} ->
7276
(* ATTENTION:
7377
Main use case, we should detect inline all immutable block .. *)
74-
begin match (* simpl*) arg with
75-
| Lvar v ->
76-
Lam_util.field_flatten_get lam
78+
begin match simpl arg with
79+
| Lprim {primitive = Pgetglobal g; args= []} ->
80+
Lam.prim
81+
~primitive:(Pfield(i,Lambda.Fld_na))
82+
~args:[Lam.prim ~primitive:(Pgetglobal g) ~args:[] Location.none]
83+
loc
84+
| Lvar v as l->
85+
Lam_util.field_flatten_get (fun _ -> Lam.prim ~primitive ~args:[l] loc )
7786
v i meta.ident_tbl
7887
| _ ->
7988
Lam.prim ~primitive ~args:[simpl arg] loc
@@ -296,3 +305,5 @@ let simplify_alias
296305
| Lifused (v, l) -> Lam.ifused v (simpl l)
297306
in
298307
simpl lam
308+
309+

jscomp/core/lam_pass_remove_alias.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,5 @@ val simplify_alias :
4646
Lam_stats.meta ->
4747
Lam.t ->
4848
Lam.t
49+
50+

jscomp/core/lam_util.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ let refine_let
173173
| None , _, _ ->
174174
Lam.let_ Strict param arg l
175175

176-
let alias (meta : Lam_stats.meta) (k:Ident.t) (v:Ident.t)
176+
let alias_ident_or_global (meta : Lam_stats.meta) (k:Ident.t) (v:Ident.t)
177177
(v_kind : Lam_stats.kind) (let_kind : Lambda.let_kind) =
178178
(** treat rec as Strict, k is assigned to v
179179
{[ let k = v ]}
@@ -253,13 +253,13 @@ let field_flatten_get
253253
~args:[Lam.prim ~primitive:(Pgetglobal g) ~args:[] Location.none] Location.none
254254
| Some (ImmutableBlock (arr, _)) ->
255255
begin match arr.(i) with
256-
| NA -> lam
256+
| NA -> lam ()
257257
| SimpleForm l -> l
258258
end
259259
| Some (Constant (Const_block (_,_,ls))) ->
260260
Lam.const (List.nth ls i)
261261
| Some _
262-
| None -> lam
262+
| None -> lam ()
263263

264264

265265
(* TODO: check that if label belongs to a different

jscomp/core/lam_util.mli

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ val string_of_primitive : Lam.primitive -> string
3636
val kind_of_lambda_block : Lam_stats.boxed_nullable -> Lam.t list -> Lam_stats.kind
3737

3838
val field_flatten_get :
39-
Lam.t -> Ident.t -> int -> Lam_stats.ident_tbl -> Lam.t
39+
(unit -> Lam.t) -> Ident.t -> int -> Lam_stats.ident_tbl -> Lam.t
4040

4141

4242

4343

4444

45-
val alias : Lam_stats.meta ->
45+
val alias_ident_or_global : Lam_stats.meta ->
4646
Ident.t -> Ident.t -> Lam_stats.kind -> Lambda.let_kind -> unit
4747

4848

jscomp/test/submodule.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict';
22

3-
var Curry = require("../../lib/js/curry");
43

54
console.log(2);
65

@@ -58,9 +57,9 @@ var v1 = a1(1, 2);
5857

5958
var v2 = a2(1, 2);
6059

61-
var v3 = Curry._2(/* a3 */a3, 1, 2);
60+
var v3 = a3(1, 2);
6261

63-
var v4 = Curry._2(/* A4 */A4[/* a4 */0], 1, 2);
62+
var v4 = a4(1, 2);
6463

6564
var v0 = 4;
6665

jscomp/test/test_include.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,20 @@ var N = 0;
2626

2727
var v = List.length;
2828

29+
var N0 = 0;
30+
31+
var N1 = 0;
32+
33+
var N2 = 0;
34+
35+
var N3 = 0;
36+
37+
var N4 = 0;
38+
39+
var N5 = 0;
40+
41+
var N6 = 0;
42+
2943
var length = List.length;
3044

3145
var hd = List.hd;
@@ -119,6 +133,13 @@ exports.v = v;
119133
exports.Make = Make;
120134
exports.X = X;
121135
exports.U = U;
136+
exports.N0 = N0;
137+
exports.N1 = N1;
138+
exports.N2 = N2;
139+
exports.N3 = N3;
140+
exports.N4 = N4;
141+
exports.N5 = N5;
142+
exports.N6 = N6;
122143
exports.length = length;
123144
exports.hd = hd;
124145
exports.tl = tl;

jscomp/test/test_include.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,19 @@ module U = Make(Test_order)
1818
include N
1919

2020

21+
(* Missing optimization
22+
alias to a number could also be removed, especially 0
23+
*)
24+
25+
module N0 = N
26+
27+
module N1 = N0
28+
29+
module N2 = N1
30+
module N3 = N2
31+
module N4 = N3
32+
module N5 = N4
33+
module N6 = N5
34+
35+
include N6
36+

0 commit comments

Comments
 (0)