Skip to content
Open
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/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.
94 changes: 87 additions & 7 deletions compiler/backend/lam_beta_reduce.ml
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,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 +145,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 +275,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
13 changes: 12 additions & 1 deletion compiler/backend/lam_beta_reduce.mli
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,20 @@ 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
[propagate_beta_reduce_with_map]. *)

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
34 changes: 25 additions & 9 deletions compiler/backend/lam_pass_remove_alias.ml
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t =
} as l1;
ap_args = args;
ap_loc;
ap_result_not_nested;
} -> (
match Lam_compile_env.query_external_id_info ident fld_name with
| {
Expand All @@ -173,25 +174,32 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t =
in
simpl
(record_assigned
(Lam_beta_reduce.propagate_beta_reduce_with_map (current_meta ())
(Lam_beta_reduce.propagate_beta_reduce_with_map
~result_not_nested:ap_result_not_nested (current_meta ())
param_map params body args))
| _ ->
let fn = simpl l1 in
let args' = Ext_list.map_sharing args simpl in
if fn == l1 && args' == args then lam else Lambda.apply fn args' ap_loc)
if fn == l1 && args' == args then lam
else
Lambda.apply ~result_not_nested:ap_result_not_nested fn args' ap_loc)
(* Function inlining interact with other optimizations...

- parameter attributes
- scope issues
- code bloat
*)
| Lapply {ap_func = Lvar v as fn; ap_args = args; ap_loc} -> (
| Lapply
{ap_func = Lvar v as fn; ap_args = args; ap_loc; ap_result_not_nested}
-> (
(* Check info for always inlining *)
let ap_args = Ext_list.map_sharing args simpl in
let[@local] normal () =
let fn' = simpl fn in
if fn' == fn && ap_args == args then lam
else Lambda.apply fn' ap_args ap_loc
else
Lambda.apply ~result_not_nested:ap_result_not_nested fn' ap_args
ap_loc
in
match Hash_ident.find_opt meta.ident_tbl v with
| Some
Expand All @@ -214,8 +222,9 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t =
(* Ir_log.dwarn __LOC__ "beta .. %s/%d" v.name v.stamp ; *)
simpl
(record_assigned
(Lam_beta_reduce.propagate_beta_reduce (current_meta ()) params
body ap_args))
(Lam_beta_reduce.propagate_beta_reduce
~result_not_nested:ap_result_not_nested (current_meta ())
params body ap_args))
else if Lam_analysis.ok_to_inline_fun_when_app m ap_args then
let param_map =
Lam_closure.is_closed_with_map meta.export_idents params body
Expand All @@ -227,7 +236,8 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t =
| Lam_rec ->
record_assigned
(Lam_beta_reduce.propagate_beta_reduce_with_map
(current_meta ()) param_map params body ap_args)
~result_not_nested:ap_result_not_nested (current_meta ())
param_map params body ap_args)
| Lam_self_rec -> normal ()
| Lam_non_rec ->
if
Expand All @@ -244,6 +254,7 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t =
simpl
(record_assigned
(Lam_beta_reduce.propagate_beta_reduce_with_map
~result_not_nested:ap_result_not_nested
(current_meta ()) param_map params body ap_args))
in
decr inline_depth;
Expand All @@ -253,15 +264,20 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t =
else normal ()
| Some _ | None -> normal ())
| 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 ->
let _, param_map =
Lam_closure.is_closed_with_map Set_ident.empty params body
in
simpl
(record_assigned
(Lam_beta_reduce.propagate_beta_reduce_with_map (current_meta ())
(Lam_beta_reduce.propagate_beta_reduce_with_map
~result_not_nested:ap_result_not_nested (current_meta ())
param_map params body args))
| Lstringswitch (l, sw, d) -> (
let known_constant =
Expand Down
6 changes: 4 additions & 2 deletions compiler/backend/lam_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,17 @@ let refine_let ?original ~assignable ~kind param (arg : Lambda.t) (l : Lambda.t)
before its argument, which is to read the function, so evaluation
order and side effects stay the same. *)
Lambda.prim ~primitive ~args:[arg] loc
| _, _, Lapply {ap_func = fn; ap_args = [Lvar w]; ap_loc}
| ( _,
_,
Lapply {ap_func = fn; ap_args = [Lvar w]; ap_loc; ap_result_not_nested} )
when Ident.same w param
&& (not (Lam_hit.hit_variable param fn))
&& may_follow (Lam_analysis.motion ~assignable fn) ->
(* For a function call such as `{ let x = value; someFn(x) }`, we can
rewrite to `someFn(value)` as long as the callee does not capture `x`
and `value` may run after it: not `{ let x = c.n; next()(x) }`, where
`next()` may assign `c.n`. *)
Lambda.apply fn [arg] ap_loc
Lambda.apply ~result_not_nested:ap_result_not_nested fn [arg] ap_loc
| (Strict | StrictOpt), arg, _ when is_safe_to_alias arg ->
(* `Strict` and `StrictOpt` bindings both evaluate the RHS immediately
(with `StrictOpt` allowing later elimination if unused). When that RHS
Expand Down
32 changes: 22 additions & 10 deletions compiler/typing/lambda.ml
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,16 @@ and lfunction = {

and prim_info = {primitive: primitive; args: t list; loc: Location.t}

and lambda_apply = {ap_func: t; ap_args: t list; ap_loc: Location.t}
and lambda_apply = {
ap_func: t;
ap_args: t list;
ap_loc: Location.t;
ap_result_not_nested: bool;
(* The call's own type is [option<t>], where [t] cannot be [undefined]:
a body inlined here returns a [Some(v)] as [v], without
[Primitive_option.some] (see
[Lam_beta_reduce.propagate_beta_reduce]) *)
}

and switch_key =
| Switch_int of int
Expand Down Expand Up @@ -799,7 +808,8 @@ let rec eq_approx (l1 : t) (l2 : t) =
| Lapply app1 -> (
match l2 with
| Lapply app2 ->
eq_approx app1.ap_func app2.ap_func
app1.ap_result_not_nested = app2.ap_result_not_nested
&& eq_approx app1.ap_func app2.ap_func
&& eq_approx_list app1.ap_args app2.ap_args
| _ -> false)
| Lifthenelse (a, b, c) -> (
Expand Down Expand Up @@ -1142,10 +1152,11 @@ let prim ~primitive:(prim : primitive) ~args loc : t =
let lfunction_can_be_inlined (lfunction : lfunction) =
(not lfunction.attr.async) && lfunction.attr.directive = None

let rec apply fn args (ap_loc : Location.t) : t =
let rec apply ?(result_not_nested = false) fn args (ap_loc : Location.t) : t =
let ap_result_not_nested = result_not_nested in
match fn with
| Lfunction lfunction when not (lfunction_can_be_inlined lfunction) ->
Lapply {ap_func = fn; ap_args = args; ap_loc}
Lapply {ap_func = fn; ap_args = args; ap_loc; ap_result_not_nested}
| Lfunction
{
params;
Expand All @@ -1164,14 +1175,15 @@ let rec apply fn args (ap_loc : Location.t) : t =
prim ~primitive:wrap
~args:[prim ~primitive:primitive_call.primitive ~args ap_loc]
ap_loc
| exception Not_simple_form -> Lapply {ap_func = fn; ap_args = args; ap_loc}
)
| exception Not_simple_form ->
Lapply {ap_func = fn; ap_args = args; ap_loc; ap_result_not_nested})
| Lfunction
{params; body = Lprim ({primitive; args = inner_args} as primitive_call)}
when evaluates_args_as_call primitive args -> (
match is_eta_conversion_exn params inner_args args with
| args -> prim ~primitive:primitive_call.primitive ~args ap_loc
| exception _ -> Lapply {ap_func = fn; ap_args = args; ap_loc})
| exception _ ->
Lapply {ap_func = fn; ap_args = args; ap_loc; ap_result_not_nested})
| Lfunction
{
params;
Expand All @@ -1184,17 +1196,17 @@ let rec apply fn args (ap_loc : Location.t) : t =
match is_eta_conversion_exn params inner_args args with
| args -> seq (prim ~primitive:primitive_call.primitive ~args ap_loc) const
| exception _ ->
Lapply {ap_func = fn; ap_args = args; ap_loc}
Lapply {ap_func = fn; ap_args = args; ap_loc; ap_result_not_nested}
(* | Lfunction {params;body} when Ext_list.same_length params args ->
Ext_list.fold_right2 (fun p arg acc ->
Llet(Strict,p,arg,acc)
) params args body *)
(* TODO: more rigirous analysis on [let_kind] *))
| Llet (kind, id, e, (Lfunction _ as fn)) ->
let_ kind id e (apply fn args ap_loc)
let_ kind id e (apply ~result_not_nested fn args ap_loc)
(* | Llet (kind0, id0, e0, Llet (kind,id, e, (Lfunction _ as fn))) ->
Llet(kind0,id0,e0,Llet (kind, id, e, apply fn args loc status)) *)
| _ -> Lapply {ap_func = fn; ap_args = args; ap_loc}
| _ -> Lapply {ap_func = fn; ap_args = args; ap_loc; ap_result_not_nested}

let not_ loc x : t =
match x with
Expand Down
Loading
Loading