Skip to content
Closed
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
1 change: 1 addition & 0 deletions changes/assigned-variable-module-field.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix a field of a first-class module held in a ref, in a function inlined from another module, being read from the module the ref is set to later.
1 change: 1 addition & 0 deletions changes/inline-assigned-payload.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix the payload of a `Some` or a tuple field, made from a variable that is assigned later, giving the variable's new value.
1 change: 1 addition & 0 deletions changes/inline-option-result.polish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Return the result of an inlined function such as `Option.map` without `Primitive_option.some` when the caller's type says the payload cannot be `undefined`; a cast or external inside the inlined function, or a cast of the function itself at the call site, that hides an `undefined` payload, as in `Some(a->Array.getUnsafe(5))`, now behaves as the same `Some` written at the call site.
1 change: 1 addition & 0 deletions changes/late-inline-alias-pass.polish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Simplify the body of a function from another module that is inlined while compiling to JavaScript, so a `Some(x)` argument is read directly and a function argument applied in the body is inlined.
102 changes: 93 additions & 9 deletions compiler/backend/lam_beta_reduce.ml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ let unmark_arguments params (body : Lambda.t) (args : Lambda.t list) =
match arg with
| Lprim {primitive = Pnot_nested_option; args = [arg]} ->
(Set_ident.add param marked, arg :: args)
| Lapply {ap_result_not_nested = true} ->
(* The call's own type says the same of its result, which is what
the parameter is bound to *)
(Set_ident.add param marked, arg :: args)
| _ -> (marked, arg :: args))
params args (Set_ident.empty, [])
in
Expand All @@ -135,7 +139,7 @@ let unmark_arguments params (body : Lambda.t) (args : Lambda.t list) =
| Lprim {primitive = Pval_from_option; args = [(Lvar param as var)]; loc}
when Set_ident.mem param marked ->
Lambda.prim ~primitive:Pval_from_option_not_nest ~args:[var] loc
| Lapply {ap_func; ap_args; ap_loc} ->
| Lapply {ap_func; ap_args; ap_loc; ap_result_not_nested} ->
let mark (arg : Lambda.t) =
match arg with
| Lvar param when Set_ident.mem param marked ->
Expand All @@ -145,14 +149,94 @@ let unmark_arguments params (body : Lambda.t) (args : Lambda.t list) =
let ap_func' = unwrap ap_func in
let ap_args' = Ext_list.map_sharing ap_args mark in
if ap_func' == ap_func && ap_args' == ap_args then lam
else Lambda.apply ap_func' ap_args' ap_loc
else
Lambda.apply ~result_not_nested:ap_result_not_nested ap_func' ap_args'
ap_loc
| _ -> Lambda_traverse.shallow_map_sharing unwrap lam
in
(unwrap body, args)

let propagate_beta_reduce (meta : Lam_stats.t) (params : Ident.t list)
(body : Lambda.t) (args : Lambda.t list) =
let body, args = unmark_arguments params body args in
(* [body] is the body of a function inlined at a call whose own type is an
option whose payload cannot be [undefined] ([ap_result_not_nested]). So a
[Some(v)] the body returns needs no wrapping, and a call it returns gets
the same mark, so that it can be inlined the same way. Only the code in
return position is rewritten: a [Some] bound to a variable first is left
as it is. The entry points below run this before the arguments are
substituted, so that only the body's own [Some]s and calls are
rewritten, not an argument that ends up in return position. *)
let rec mark_result (body : Lambda.t) : Lambda.t =
match body with
| Lprim {primitive = Psome; args = [v]; loc} ->
Lambda.prim ~primitive:Psome_not_nest ~args:[v] loc
| Lapply {ap_func; ap_args; ap_loc; ap_result_not_nested = false} ->
(* Every call is built by [Lambda.apply], whose shortcuts depend on the
function and arguments only, so they leave this one a call *)
Lambda.apply ~result_not_nested:true ap_func ap_args ap_loc
| Llet (kind, id, arg, rest) ->
let rest' = mark_result rest in
if rest' == rest then body else Lambda.let_ kind id arg rest'
| Lletrec (bindings, rest) ->
let rest' = mark_result rest in
if rest' == rest then body else Lambda.letrec bindings rest'
| Lsequence (first, rest) ->
let rest' = mark_result rest in
if rest' == rest then body else Lambda.seq first rest'
| Lifthenelse (cond, then_, else_) ->
let then' = mark_result then_ and else' = mark_result else_ in
if then' == then_ && else' == else_ then body
else Lambda.if_ cond then' else'
| Lswitch (arg, sw) ->
let consts = Ext_list.map_snd_sharing sw.sw_consts mark_result in
let blocks = Ext_list.map_snd_sharing sw.sw_blocks mark_result in
let fail = mark_option sw.sw_failaction in
if
consts == sw.sw_consts && blocks == sw.sw_blocks
&& fail == sw.sw_failaction
then body
else
Lambda.switch arg
{sw with sw_consts = consts; sw_blocks = blocks; sw_failaction = fail}
| Lstringswitch (arg, cases, default) ->
let cases' = Ext_list.map_snd_sharing cases mark_result in
let default' = mark_option default in
if cases' == cases && default' == default then body
else Lambda.stringswitch arg cases' default'
| Lstaticcatch (rest, handler_id, handler) ->
let rest' = mark_result rest and handler' = mark_result handler in
if rest' == rest && handler' == handler then body
else Lambda.staticcatch rest' handler_id handler'
| Ltrywith (rest, id, handler) ->
let rest' = mark_result rest and handler' = mark_result handler in
if rest' == rest && handler' == handler then body
else Lambda.try_ rest' id handler'
| Ltryfinally (rest, finally) ->
let rest' = mark_result rest in
if rest' == rest then body else Lambda.try_finally rest' finally
(* Already marked, or not a result this body builds: a variable is a
parameter or bound before, a primitive such as an argument mark only
passes on a value, and a function's body returns from that function *)
| Lapply {ap_result_not_nested = true}
| Lprim _ | Lvar _ | Lglobal_module _ | Lconst _ | Lfunction _
| Lstaticraise _ | Lwhile _ | Lfor _ | Lfor_of _ | Lfor_await_of _ | Lassign _
| Lbreak | Lcontinue ->
body

and mark_option (lam : Lambda.t option) =
match lam with
| None -> lam
| Some l ->
let l' = mark_result l in
if l' == l then lam else Some l'

(* The body of a function inlined at a call: [result_not_nested] is the
call's own mark ([ap_result_not_nested]) *)
let inlined_body ~result_not_nested params body args =
let body = if result_not_nested then mark_result body else body in
unmark_arguments params body args

let propagate_beta_reduce ~result_not_nested (meta : Lam_stats.t)
(params : Ident.t list) (body : Lambda.t) (args : Lambda.t list) =
let body, args = inlined_body ~result_not_nested params body args in
let assignable = meta.assignable in
match
Lam_beta_reduce_util.simple_beta_reduce ~assignable params body args
Expand Down Expand Up @@ -195,9 +279,9 @@ let propagate_beta_reduce (meta : Lam_stats.t) (params : Ident.t list)
Lam_util.refine_let ~assignable ~kind:Strict param arg l)
new_body rest_bindings

let propagate_beta_reduce_with_map (meta : Lam_stats.t)
let propagate_beta_reduce_with_map ~result_not_nested (meta : Lam_stats.t)
(map : Lam_var_stats.stats Map_ident.t) params body args =
let body, args = unmark_arguments params body args in
let body, args = inlined_body ~result_not_nested params body args in
let assignable = meta.assignable in
match
Lam_beta_reduce_util.simple_beta_reduce ~assignable params body args
Expand Down Expand Up @@ -249,8 +333,8 @@ let propagate_beta_reduce_with_map (meta : Lam_stats.t)
Lam_util.refine_let ~assignable ~kind:Strict param arg l)
new_body rest_bindings

let no_names_beta_reduce ~assignable params body args =
let body, args = unmark_arguments params body args in
let no_names_beta_reduce ~assignable ~result_not_nested params body args =
let body, args = inlined_body ~result_not_nested params body args in
match
Lam_beta_reduce_util.simple_beta_reduce ~assignable params body args
with
Expand Down
13 changes: 12 additions & 1 deletion compiler/backend/lam_beta_reduce.mli
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

val no_names_beta_reduce :
assignable:Set_ident.t ->
result_not_nested:bool ->
Ident.t list ->
Lambda.t ->
Lambda.t list ->
Expand All @@ -49,9 +50,19 @@ val no_names_beta_reduce :
*)

val propagate_beta_reduce :
Lam_stats.t -> Ident.t list -> Lambda.t -> Lambda.t list -> Lambda.t
result_not_nested:bool ->
Lam_stats.t ->
Ident.t list ->
Lambda.t ->
Lambda.t list ->
Lambda.t
(** [result_not_nested] is the inlined call's [ap_result_not_nested]. When it
is set, a [Some] the body returns no longer wraps its payload, and a call
it returns gets the same mark; the body is rewritten for that before the
arguments are substituted. The same holds for the other two functions. *)

val propagate_beta_reduce_with_map :
result_not_nested:bool ->
Lam_stats.t ->
Lam_var_stats.stats Map_ident.t ->
Ident.t list ->
Expand Down
4 changes: 3 additions & 1 deletion compiler/backend/lam_beta_reduce_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ let simple_beta_reduce ~assignable params body args =
f;
ap_args;
ap_loc;
ap_result_not_nested = result_not_nested;
} -> (
add_params ();
(*since we adde each param only once,
Expand All @@ -188,7 +189,8 @@ let simple_beta_reduce ~assignable params body args =
let new_args = aux_exn ~conditional_from:max_int 0 ap_args in
check_order_exn ();
let result =
seq_unused param_hash params (Lambda.apply f new_args ap_loc)
seq_unused param_hash params
(Lambda.apply ~result_not_nested f new_args ap_loc)
in
Some result
with Not_simple_apply -> None)
Expand Down
4 changes: 2 additions & 2 deletions compiler/backend/lam_bounded_vars.ml
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,10 @@ let rewrite (map : _ Hash_ident.t) (lam : Lambda.t) : Lambda.t =
(* here it makes sure that global vars are not rebound *)
Lambda.prim ~primitive ~args:(List.map aux args) loc
| Lglobal_module _ -> lam
| Lapply {ap_func; ap_args; ap_loc} ->
| Lapply {ap_func; ap_args; ap_loc; ap_result_not_nested} ->
let fn = aux ap_func in
let args = List.map aux ap_args in
Lambda.apply fn args ap_loc
Lambda.apply ~result_not_nested:ap_result_not_nested fn args ap_loc
| Lswitch
( l,
{
Expand Down
25 changes: 21 additions & 4 deletions compiler/backend/lam_compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -589,12 +589,29 @@ let compile output_prefix =
let _, param_map =
Lam_closure.is_closed_with_map Set_ident.empty params body
in
(* What [meta.ident_tbl] records about the caller's bindings was
collected before the last passes rewrote them, so it may give
bodies or payloads naming variables that are gone. The inlined body
is simplified with a fresh table, which only gets what is known
about the parameters it binds. *)
let ident_tbl = Hash_ident.create 17 in
let inlined =
Lam_beta_reduce.propagate_beta_reduce_with_map lambda_cxt.meta param_map
params body ap_args
Lam_beta_reduce.propagate_beta_reduce_with_map
~result_not_nested:appinfo.ap_result_not_nested
{lambda_cxt.meta with ident_tbl}
param_map params body ap_args
in
(* The inlined body comes from another module, so the variables it
assigns are not in [meta.assignable] yet. *)
(* The alias pass ran before this call was inlined: run it on the
inlined body, so that a known argument, such as a [Some], is
resolved there too *)
let inlined =
Lam_pass_remove_alias.simplify_alias
{lambda_cxt.meta with ident_tbl}
inlined
in
(* The inlined body comes from another module, and the alias pass may
have inlined more into it, so the variables it assigns are not in
[meta.assignable] yet. *)
let meta =
{
lambda_cxt.meta with
Expand Down
9 changes: 7 additions & 2 deletions compiler/backend/lam_pass_count.ml
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,16 @@ let collect_occurs ~assignable lam : occ_tbl =
count bv body
(* Note there is a difference here when do beta reduction for *)
| Lapply
{ap_func = Lfunction ({params; body} as lfunction); ap_args = args; _}
{
ap_func = Lfunction ({params; body} as lfunction);
ap_args = args;
ap_result_not_nested;
}
when List.compare_lengths params args = 0
&& Lambda.lfunction_can_be_inlined lfunction ->
count bv
(Lam_beta_reduce.no_names_beta_reduce ~assignable params body args)
(Lam_beta_reduce.no_names_beta_reduce ~assignable
~result_not_nested:ap_result_not_nested params body args)
| Lapply {ap_func = l1; ap_args = ll; _} ->
count bv l1;
List.iter (count bv) ll
Expand Down
9 changes: 7 additions & 2 deletions compiler/backend/lam_pass_lets_dce.ml
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,16 @@ let lets_helper ~assignable (count_var : Ident.t -> Lam_pass_count.used_info)
| _ ->
Lam_util.refine_let ~original:lam ~assignable ~kind v l1 (simplif l2))
| Lapply
{ap_func = Lfunction ({params; body} as lfunction); ap_args = args; _}
{
ap_func = Lfunction ({params; body} as lfunction);
ap_args = args;
ap_result_not_nested;
}
when List.compare_lengths params args = 0
&& Lambda.lfunction_can_be_inlined lfunction ->
simplif
(Lam_beta_reduce.no_names_beta_reduce ~assignable params body args)
(Lam_beta_reduce.no_names_beta_reduce ~assignable
~result_not_nested:ap_result_not_nested params body args)
| Lprim {primitive = Pstringadd; args = [l; r]; loc} -> (
let l' = simplif l in
let r' = simplif r in
Expand Down
Loading
Loading