Skip to content

Commit 27fb7e6

Browse files
authored
Merge pull request #3799 from BuckleScript/fix_filed_info_missing_during_optimization
fix field info loss during optimizations, avoid depdency on cmi which is no longer necessary
2 parents 34301c2 + 236131b commit 27fb7e6

File tree

11 files changed

+188
-228
lines changed

11 files changed

+188
-228
lines changed

jscomp/core/lam_arity_analysis.ml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@ let rec get_arity (meta : Lam_stats.t) (lam : Lam.t) : Lam_arity.t =
4444
| Lvar v -> arity_of_var meta v
4545
| Lconst _ -> Lam_arity.non_function_arity_info
4646
| Llet(_,_,_, l ) -> get_arity meta l
47-
| Lprim {primitive = Pfield (n,_);
48-
args = [ Lglobal_module id ]; _} ->
49-
begin match (Lam_compile_env.cached_find_ml_id_pos id n meta.env).arity with
47+
| Lprim {primitive = Pfield (_, Fld_module fld_name);
48+
args = [ Lglobal_module id ]; _} ->
49+
begin match (Lam_compile_env.cached_find_ml_id_pos id fld_name).arity with
5050
| Single x -> x
5151
| Submodule _ -> Lam_arity.na
5252
end
5353
| Lprim {primitive = Pfield (m,_);
54-
args = [ Lprim{primitive = Pfield(n,_);
54+
args = [ Lprim{primitive = Pfield(n,Fld_module fld_name);
5555
args = [ Lglobal_module id]} ]
5656
; _} ->
57-
begin match (Lam_compile_env.cached_find_ml_id_pos id n meta.env).arity with
58-
| Submodule subs -> subs.(m)
57+
begin match (Lam_compile_env.cached_find_ml_id_pos id fld_name ).arity with
58+
| Submodule subs -> subs.(m) (* TODO: shall we store it as array?*)
5959
| Single _ -> Lam_arity.na
6060
end
6161
(* TODO: all information except Pccall is complete, we could

jscomp/core/lam_compile.ml

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,17 @@ let rec
125125
compile_external_field (* Like [List.empty]*)
126126
(lamba_cxt : Lam_compile_context.t)
127127
(id : Ident.t)
128-
(pos : int)
129-
(env : Env.t)
128+
pos
130129
: Js_output.t =
131-
match Lam_compile_env.cached_find_ml_id_pos id pos env with
130+
match Lam_compile_env.cached_find_ml_id_pos id pos with
132131
| { closed_lambda = Some lam}
133132
when Lam_util.not_function lam
134133
->
135134
compile_lambda lamba_cxt lam
136135
| { name} ->
137136
Js_output.output_of_expression lamba_cxt.continuation
138137
~no_effects:no_effects_const
139-
(if id.name = "Sys" && name = "os_type" then E.str Sys.os_type
140-
else E.ml_var_dot id name )
138+
(E.ml_var_dot id name )
141139

142140
(* TODO: how nested module call would behave,
143141
In the future, we should keep in track of if
@@ -169,11 +167,11 @@ let rec
169167
and compile_external_field_apply
170168
(args_lambda : Lam.t list)
171169
(id : Ident.t)
172-
(pos : int)
173-
(env : Env.t) (lambda_cxt : Lam_compile_context.t): Js_output.t =
170+
pos
171+
(lambda_cxt : Lam_compile_context.t): Js_output.t =
174172

175173
let ident_info =
176-
Lam_compile_env.cached_find_ml_id_pos id pos env in
174+
Lam_compile_env.cached_find_ml_id_pos id pos in
177175
let args_code, args =
178176
let dummy = [], [] in
179177
if args_lambda = [] then dummy
@@ -1270,16 +1268,18 @@ and compile_apply
12701268
compile_lambda lambda_cxt (Lam.apply fn (Ext_list.append fn_args args) loc App_na )
12711269
(* External function calll *)
12721270
| { fn =
1273-
Lprim{primitive = Pfield (n,_);
1271+
Lprim{primitive = Pfield (_, fld_info);
12741272
args = [ Lglobal_module id];_};
12751273
args ;
12761274
status = App_na | App_ml_full} ->
12771275
(* Note we skip [App_js_full] since [get_exp_with_args] dont carry
12781276
this information, we should fix [get_exp_with_args]
12791277
*)
1280-
compile_external_field_apply args id n lambda_cxt.meta.env lambda_cxt
1281-
1282-
1278+
begin match fld_info with
1279+
| Fld_module fld_name ->
1280+
compile_external_field_apply args id fld_name lambda_cxt
1281+
| _ -> assert false
1282+
end
12831283
| { fn; args = args_lambda; status} ->
12841284
(* TODO: ---
12851285
1. check arity, can be simplified for pure expression
@@ -1342,9 +1342,15 @@ and compile_apply
13421342
) fn_code args)
13431343
and compile_prim (prim_info : Lam.prim_info) (lambda_cxt : Lam_compile_context.t) =
13441344
match prim_info with
1345-
| {primitive = Pfield (n,_); args = [ Lglobal_module id ]; _}
1345+
| {primitive = Pfield (_, fld_info); args = [ Lglobal_module id ]; _}
13461346
-> (* should be before Lglobal_global *)
1347-
compile_external_field lambda_cxt id n lambda_cxt.meta.env
1347+
begin match fld_info with
1348+
| Fld_module field ->
1349+
if id.name = "Sys" && field = "os_type" then
1350+
Js_output.output_of_expression lambda_cxt.continuation ~no_effects:no_effects_const (E.str Sys.os_type)
1351+
else compile_external_field lambda_cxt id field
1352+
| _ -> assert false
1353+
end
13481354
| {primitive = Praise ; args = [ e ]; _} ->
13491355
(match compile_lambda {lambda_cxt with continuation = NeedValue Not_tail} e with
13501356
| {block ; value = Some v} ->

jscomp/core/lam_compile_env.ml

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ module S = Js_stmt_make
3535
type path = string
3636

3737
type ml_module_info = {
38-
signature : Ocaml_types.t;
3938
cmj_table : Js_cmj_format.t ;
4039
cmj_path : path;
4140
}
@@ -59,7 +58,6 @@ type env_value =
5958
type ident_info = {
6059
(* id : Ident.t; *)
6160
name : string;
62-
signature : Ocaml_types.t;
6361
arity : Js_cmj_format.arity;
6462
closed_lambda : Lam.t option
6563
}
@@ -119,40 +117,29 @@ let add_js_module
119117

120118
let (+>) = Lam_module_ident.Hash.add cached_tbl
121119

122-
let cached_find_ml_id_pos (module_id : Ident.t) (pos : int) env : ident_info =
120+
let cached_find_ml_id_pos (module_id : Ident.t) name : ident_info =
123121
let oid = Lam_module_ident.of_ml module_id in
124122
match Lam_module_ident.Hash.find_opt cached_tbl oid with
125123
| None ->
126124
let cmj_path, cmj_table =
127125
Js_cmj_load.find_cmj (module_id.name ^ Literals.suffix_cmj) in
128-
begin match
129-
Ocaml_types.find_serializable_signatures_by_path module_id env with
130-
| None ->
131-
assert false (*TODO: more informative error message *)
132-
| Some signature ->
133-
oid +> Visit {signature; cmj_table ; cmj_path } ;
134-
let name = Ocaml_types.get_name signature pos in
135-
let arity, closed_lambda =
136-
Js_cmj_format.query_by_name cmj_table name
137-
in
138-
{
139-
(* id; *)
140-
name ;
141-
signature ;
142-
arity ;
143-
closed_lambda
144-
}
145-
end
146-
| Some (Visit {signature ; cmj_table } )
126+
oid +> Visit { cmj_table ; cmj_path } ;
127+
let arity, closed_lambda =
128+
Js_cmj_format.query_by_name cmj_table name
129+
in
130+
{
131+
name ;
132+
arity ;
133+
closed_lambda
134+
}
135+
136+
| Some (Visit { cmj_table } )
147137
->
148-
let name = Ocaml_types.get_name signature pos in
149138
let arity , closed_lambda =
150139
Js_cmj_format.query_by_name cmj_table name
151140
in
152141
{
153-
(* id; *)
154142
name;
155-
signature;
156143
arity;
157144
closed_lambda
158145
(* TODO shall we cache the arity ?*)
@@ -163,7 +150,6 @@ let cached_find_ml_id_pos (module_id : Ident.t) (pos : int) env : ident_info =
163150

164151

165152
type module_info = {
166-
signature : Ocaml_types.t ;
167153
pure : bool
168154
}
169155
(* TODO: it does not make sense to cache
@@ -189,7 +175,7 @@ let query_and_add_if_not_exist
189175
oid +> Runtime (true,cmj_path,cmj_table) ;
190176
(match env with
191177
| Has_env _ ->
192-
found {signature = Ocaml_types.empty; pure = true}
178+
found { pure = true}
193179
| No_env ->
194180
found cmj_info)
195181
| Ml
@@ -202,8 +188,8 @@ let query_and_add_if_not_exist
202188
Ocaml_types.find_serializable_signatures_by_path oid.id env with
203189
| None -> not_found () (* actually when [not_found] in the call site, we throw... *)
204190
| Some signature ->
205-
oid +> Visit {signature; cmj_table;cmj_path } ;
206-
found { signature ; pure = Js_cmj_format.is_pure cmj_table}
191+
oid +> Visit {cmj_table;cmj_path } ;
192+
found { pure = Js_cmj_format.is_pure cmj_table}
207193
end
208194
| No_env ->
209195
found cmj_info)
@@ -217,31 +203,31 @@ let query_and_add_if_not_exist
217203
begin match env with
218204
| Has_env _
219205
->
220-
found {signature = Ocaml_types.empty; pure = false}
206+
found { pure = false}
221207
| No_env ->
222208
found (Ext_string.empty, Js_cmj_format.no_pure_dummy)
223209
(* FIXME: #154, it come from External, should be okay *)
224210
end
225211

226212
end
227-
| Some (Visit {signature ; cmj_table; cmj_path}) ->
213+
| Some (Visit { cmj_table; cmj_path}) ->
228214
begin match env with
229215
| Has_env _ ->
230-
found { signature; pure = Js_cmj_format.is_pure cmj_table}
216+
found { pure = Js_cmj_format.is_pure cmj_table}
231217
| No_env -> found (cmj_path,cmj_table)
232218
end
233219

234220
| Some (Runtime (pure, cmj_path,cmj_table)) ->
235221
begin match env with
236222
| Has_env _ ->
237-
found {signature = Ocaml_types.empty; pure }
223+
found {pure }
238224
| No_env ->
239225
found (cmj_path, cmj_table)
240226
end
241227
| Some External ->
242228
begin match env with
243229
| Has_env _ ->
244-
found {signature = Ocaml_types.empty; pure = false}
230+
found {pure = false}
245231
| No_env ->
246232
found (Ext_string.empty, Js_cmj_format.no_pure_dummy) (* External is okay *)
247233
end

jscomp/core/lam_compile_env.mli

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ type path = string
3838

3939

4040
type module_info = {
41-
signature : Ocaml_types.t ;
4241
pure : bool
4342
}
4443

@@ -49,7 +48,6 @@ type _ t =
4948

5049
type ident_info = {
5150
name : string;
52-
signature : Ocaml_types.t;
5351
arity : Js_cmj_format.arity;
5452
closed_lambda : Lam.t option
5553
}
@@ -98,8 +96,7 @@ val add_js_module :
9896
*)
9997
val cached_find_ml_id_pos :
10098
Ident.t ->
101-
int ->
102-
Env.t ->
99+
string ->
103100
ident_info
104101

105102
val query_and_add_if_not_exist :

jscomp/core/lam_pass_remove_alias.ml

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,15 @@ let simplify_alias
8181
for the inner expression
8282
*)
8383

84-
| Lprim {primitive = (Pfield (i,_) as primitive); args = [arg]; loc} ->
84+
| Lprim {primitive = (Pfield (i,info) as primitive); args = [arg]; loc} ->
8585
(* ATTENTION:
8686
Main use case, we should detect inline all immutable block .. *)
87-
begin match simpl arg with
88-
| Lglobal_module g
89-
->
90-
Lam.prim
91-
~primitive:(Pfield(i,Lam_compat.Fld_na))
92-
~args:[Lam.global_module g ]
93-
loc
87+
begin match simpl arg with
9488
| Lvar v as l->
9589
Lam_util.field_flatten_get (fun _ -> Lam.prim ~primitive ~args:[l] loc )
96-
v i meta.ident_tbl
97-
| _ ->
98-
Lam.prim ~primitive ~args:[simpl arg] loc
90+
v i info meta.ident_tbl
91+
| l ->
92+
Lam.prim ~primitive ~args:[l] loc
9993
end
10094
| Lprim {primitive = Pval_from_option | Pval_from_option_not_nest; args = [Lvar v]} as x ->
10195
begin match Ident_hashtbl.find_opt meta.ident_tbl v with
@@ -156,12 +150,12 @@ let simplify_alias
156150
return [0, $$let[5],... $$let[16]]}
157151
*)
158152
| Lapply{fn =
159-
Lprim {primitive = Pfield (index, _) ;
153+
Lprim {primitive = Pfield (index, Fld_module fld_name) ;
160154
args = [ Lglobal_module ident ];
161155
_} as l1;
162156
args; loc ; status} ->
163157
begin
164-
match Lam_compile_env.cached_find_ml_id_pos ident index meta.env with
158+
match Lam_compile_env.cached_find_ml_id_pos ident fld_name with
165159
| {closed_lambda=Some Lfunction{params; body; _} }
166160
(** be more cautious when do cross module inlining *)
167161
when

jscomp/core/lam_print.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ let primitive ppf (prim : Lam_primitive.t) = match prim with
149149
(* | Psetglobal id -> fprintf ppf "setglobal %a" Ident.print id *)
150150
| Pmakeblock(tag, _, Immutable) -> fprintf ppf "makeblock %i" tag
151151
| Pmakeblock(tag, _, Mutable) -> fprintf ppf "makemutable %i" tag
152-
| Pfield (n,_) -> fprintf ppf "field %i" n
152+
| Pfield (n, (Fld_module s | Fld_record s))
153+
-> fprintf ppf "field %s/%i" s n
154+
| Pfield (n, Fld_na)
155+
-> fprintf ppf "field %i" n
153156
| Pfield_computed ->
154157
fprintf ppf "field_computed"
155158
| Psetfield_computed ->

jscomp/core/lam_util.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,10 @@ let kind_of_lambda_block (xs : Lam.t list) : Lam_id_kind.t =
192192
element_of_lambda x ))
193193

194194
let field_flatten_get
195-
lam v i (tbl : Lam_id_kind.t Ident_hashtbl.t) : Lam.t =
195+
lam v i info (tbl : Lam_id_kind.t Ident_hashtbl.t) : Lam.t =
196196
match Ident_hashtbl.find_opt tbl v with
197197
| Some (Module g) ->
198-
Lam.prim ~primitive:(Pfield (i, Lam_compat.Fld_na))
198+
Lam.prim ~primitive:(Pfield (i, info))
199199
~args:[ Lam.global_module g ] Location.none
200200
| Some (ImmutableBlock (arr)) ->
201201
begin match arr.(i) with

jscomp/core/lam_util.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ val kind_of_lambda_block : Lam.t list -> Lam_id_kind.t
5050
}}
5151
*)
5252
val field_flatten_get :
53-
(unit -> Lam.t) -> Ident.t -> int -> Lam_stats.ident_tbl -> Lam.t
53+
(unit -> Lam.t) -> Ident.t -> int -> Lambda.field_dbg_info -> Lam_stats.ident_tbl -> Lam.t
5454

5555

5656

0 commit comments

Comments
 (0)