Skip to content
Draft
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
2 changes: 2 additions & 0 deletions changes/inline-option-unwrap.polish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Inlined functions such as `Option.getOr` and `Option.map` no longer call
`Primitive_option.valFromOption` on an argument whose payload cannot be `undefined`.
34 changes: 18 additions & 16 deletions compiler/backend/lam_analysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@ let rec delay_safe ~assignable (lam : Lambda.t) : bool =
( Fld_module _ | Fld_tuple | Fld_poly_var_tag | Fld_poly_var_content
| Fld_extension | Fld_variant | Fld_cons ) )
(* values and fresh blocks *)
| Ptypeof | Psome | Psome_not_nest | Pmakeblock _ | Pjs_object_create _
| Pmakearray | Pmakelist | Pmakedict | Ptemplate _ | Pstringadd
| Pstringlength | Pstringcomp _ | Pstringorder | Pstringmin | Pstringmax
| Psequand | Psequor | Pnot | Pboolcomp _ | Pboolorder | Pboolmin | Pboolmax
| Pnegint | Paddint | Psubint | Pmulint | Ppowint | Pandint | Porint
| Pxorint | Pnotint | Plslint | Plsrint | Pasrint | Pintcomp _ | Pintorder
| Pintmin | Pintmax | Pintoffloat | Pfloatofint | Pnegfloat | Pmodfloat
| Paddfloat | Psubfloat | Pmulfloat | Pdivfloat | Ppowfloat | Pfloatcomp _
| Pfloatorder | Pfloatmin | Pfloatmax | Pnegbigint | Paddbigint | Psubbigint
| Pmulbigint | Pandbigint | Porbigint | Pxorbigint | Pnotbigint | Plslbigint
| Pasrbigint | Pbigintcomp _ | Pbigintorder | Pbigintmin | Pbigintmax
| Phash_mixint | Phash_mixstring | Phash_finalmix | Pis_null_undefined
| Pjscomp _ | Pnull_to_opt | Pnull_undefined_to_opt | Pis_null
| Pis_not_none | Pval_from_option | Pval_from_option_not_nest
| Pis_poly_var_block ->
| Ptypeof | Psome | Psome_not_nest | Pnot_nested_option | Pmakeblock _
| Pjs_object_create _ | Pmakearray | Pmakelist | Pmakedict | Ptemplate _
| Pstringadd | Pstringlength | Pstringcomp _ | Pstringorder | Pstringmin
| Pstringmax | Psequand | Psequor | Pnot | Pboolcomp _ | Pboolorder
| Pboolmin | Pboolmax | Pnegint | Paddint | Psubint | Pmulint | Ppowint
| Pandint | Porint | Pxorint | Pnotint | Plslint | Plsrint | Pasrint
| Pintcomp _ | Pintorder | Pintmin | Pintmax | Pintoffloat | Pfloatofint
| Pnegfloat | Pmodfloat | Paddfloat | Psubfloat | Pmulfloat | Pdivfloat
| Ppowfloat | Pfloatcomp _ | Pfloatorder | Pfloatmin | Pfloatmax
| Pnegbigint | Paddbigint | Psubbigint | Pmulbigint | Pandbigint | Porbigint
| Pxorbigint | Pnotbigint | Plslbigint | Pasrbigint | Pbigintcomp _
| Pbigintorder | Pbigintmin | Pbigintmax | Phash_mixint | Phash_mixstring
| Phash_finalmix | Pis_null_undefined | Pjscomp _ | Pnull_to_opt
| Pnull_undefined_to_opt | Pis_null | Pis_not_none | Pval_from_option
| Pval_from_option_not_nest | Pis_poly_var_block ->
true
(* reads of the heap, and primitives with side effects *)
| Pfield (_, (Fld_record _ | Fld_record_inline _ | Fld_record_extension _))
Expand Down Expand Up @@ -140,7 +140,9 @@ let size_upto ~limit (lam : Lambda.t) =
_;
} ->
add 1
| Lprim {primitive = Praise | Pis_not_none; args = [l]; _} -> size l
| Lprim {primitive = Praise | Pis_not_none | Pnot_nested_option; args = [l]}
->
size l
| Lglobal_module _ -> add 1
| Lprim {primitive = Praw_js_code _} -> add too_big
| Lprim {args = ll; _} ->
Expand Down
39 changes: 39 additions & 0 deletions compiler/backend/lam_beta_reduce.ml
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,45 @@ let effects_after args =
(effects || not (Lam_analysis.no_side_effects arg), effects :: acc))
args (false, []))

(* An argument marked [Pnot_nested_option] holds an option whose payload
cannot be [undefined], so [body] unwraps its parameter with
[Pval_from_option_not_nest], which reads the value directly, and passes
it on to other calls marked too. That holds for this call only: the
argument may be a variable whose own type differs, through [Obj.magic] or
a coercion. So [body] is rewritten and the argument passed without the
mark, so that a variable can still be substituted. *)
let unmark_arguments params (body : Lambda.t) (args : Lambda.t list) =
let marked, args =
List.fold_right2
(fun param (arg : Lambda.t) (marked, args) ->
match arg with
| Lprim {primitive = Pnot_nested_option; args = [arg]} ->
(Set_ident.add param marked, arg :: args)
| _ -> (marked, arg :: args))
params args (Set_ident.empty, [])
in
if Set_ident.is_empty marked then (body, args)
else
let rec unwrap (lam : Lambda.t) =
match lam with
| 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} ->
let mark (arg : Lambda.t) =
match arg with
| Lvar param when Set_ident.mem param marked ->
Lambda.prim ~primitive:Pnot_nested_option ~args:[arg] ap_loc
| _ -> unwrap arg
in
Lambda.apply (unwrap ap_func) (List.map mark 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
let assignable = meta.assignable in
match
Lam_beta_reduce_util.simple_beta_reduce ~assignable params body args
Expand Down Expand Up @@ -156,6 +193,7 @@ let propagate_beta_reduce (meta : Lam_stats.t) (params : Ident.t list)

let propagate_beta_reduce_with_map (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 assignable = meta.assignable in
match
Lam_beta_reduce_util.simple_beta_reduce ~assignable params body args
Expand Down Expand Up @@ -207,6 +245,7 @@ let propagate_beta_reduce_with_map (meta : Lam_stats.t)
new_body rest_bindings

let no_names_beta_reduce ~assignable params body args =
let body, args = unmark_arguments params body args in
match
Lam_beta_reduce_util.simple_beta_reduce ~assignable params body args
with
Expand Down
9 changes: 9 additions & 0 deletions compiler/backend/lam_beta_reduce_util.ml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ let simple_beta_reduce ~assignable params body args =
let u =
match u with
| Lvar x -> find_param_exn ~conditional:(i >= conditional_from) x u
| Lprim {primitive = Pnot_nested_option; args = [(Lvar x as var)]; loc}
-> (
(* The value substituted for [x] is the one [x] was bound to, so
the mark still holds for it. *)
match find_param_exn ~conditional:(i >= conditional_from) x var with
| Lprim {primitive = Pnot_nested_option} as arg -> arg
| arg ->
if arg == var then u
else Lambda.prim ~primitive:Pnot_nested_option ~args:[arg] loc)
| Lconst _ -> u
| _ -> raise_notrace Not_simple_apply
in
Expand Down
3 changes: 2 additions & 1 deletion compiler/backend/lam_compile_primitive.ml
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ let translate output_prefix loc (cxt : Lam_compile_context.t)
tag_info args
| Pval_from_option ->
Js_of_lam_option.val_from_option (Ext_list.singleton_exn args)
| Pval_from_option_not_nest -> Ext_list.singleton_exn args
| Pval_from_option_not_nest | Pnot_nested_option ->
Ext_list.singleton_exn args
| Pfield (i, fld_info) ->
Js_of_lam_block.field fld_info
(Ext_list.singleton_exn args)
Expand Down
4 changes: 3 additions & 1 deletion compiler/backend/lam_pass_remove_alias.ml
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t =
&& List.for_all
(fun (arg : Lambda.t) ->
match arg with
| Lvar p -> (
| Lvar p
| Lprim {primitive = Pnot_nested_option; args = [Lvar p]}
-> (
match Hash_ident.find_opt meta.ident_tbl p with
| Some v -> v <> Parameter
| None -> true)
Expand Down
57 changes: 32 additions & 25 deletions compiler/typing/lambda.ml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ type primitive =
| Psome_not_nest
(** [Some x] where [x] cannot itself be [undefined], so no wrapping is
needed. *)
| Pnot_nested_option
(** The identity on an option whose payload cannot be [undefined], so
it is never the block that represents [Some] of undefined. The
translation of a call marks such arguments, so that the body of an
inlined function unwraps them without
[Primitive_option.valFromOption]. *)
(* Operations on heap blocks *)
| Pmakeblock of tag_info
| Pfield of int * field_dbg_info
Expand Down Expand Up @@ -499,10 +505,10 @@ let eq_primitive_approx (lhs : primitive) (rhs : primitive) =
(* etc *)
| Pval_from_option | Pval_from_option_not_nest | Pnull_to_opt
| Pnull_undefined_to_opt | Pis_null | Pis_not_none | Psome | Psome_not_nest
| Pis_null_undefined | Ptypeof | Pis_poly_var_block | Pdebugger | Pinit_mod
| Pupdate_mod | Pduprecord | Pmakearray | Parraylength | Parrayrefu
| Parraysetu | Parrayrefs | Pjs_fn_method | Phash | Phash_mixstring
| Phash_mixint | Phash_finalmix | Precord_rest _ ->
| Pnot_nested_option | Pis_null_undefined | Ptypeof | Pis_poly_var_block
| Pdebugger | Pinit_mod | Pupdate_mod | Pduprecord | Pmakearray | Parraylength
| Parrayrefu | Parraysetu | Parrayrefs | Pjs_fn_method | Phash
| Phash_mixstring | Phash_mixint | Phash_finalmix | Precord_rest _ ->
rhs = lhs
(* Reachable only via the optimizer's term-equality comparison, which the
test suite doesn't exercise for template primitives. *)
Expand Down Expand Up @@ -721,27 +727,28 @@ let rec is_eta_conversion_exn params inner_args outer_args : t list =
let may_skip_operands (primitive : primitive) =
match primitive with
| Psequand | Psequor -> true
| Pdebugger | Ptypeof | Psome | Psome_not_nest | Pmakeblock _ | Pfield _
| Psetfield _ | Pduprecord | Precord_rest _ | Pjs_call _ | Pjsx _
| Pjs_object_create _ | Pjs_object_get _ | Pjs_object_set _ | Praise
| Pobjcomp _ | Pobjorder | Pobjmin | Pobjmax | Pobjtag | Pobjsize | Pnot
| Pboolcomp _ | Pboolorder | Pboolmin | Pboolmax | Pnegint | Paddint | Psubint
| Pmulint | Pdivint | Pmodint | Ppowint | Pandint | Porint | Pxorint | Pnotint
| Plslint | Plsrint | Pasrint | Pintcomp _ | Pintorder | Pintmin | Pintmax
| Pintoffloat | Pfloatofint | Pnegfloat | Pmodfloat | Paddfloat | Psubfloat
| Pmulfloat | Pdivfloat | Ppowfloat | Pfloatcomp _ | Pfloatorder | Pfloatmin
| Pfloatmax | Pnegbigint | Paddbigint | Psubbigint | Ppowbigint | Pmulbigint
| Pdivbigint | Pmodbigint | Pandbigint | Porbigint | Pxorbigint | Pnotbigint
| Plslbigint | Pasrbigint | Pbigintcomp _ | Pbigintorder | Pbigintmin
| Pbigintmax | Pstringlength | Pstringrefu | Pstringrefs | Pstringcomp _
| Pstringorder | Pstringmin | Pstringmax | Pstringadd | Pmakearray
| Parraylength | Parrayrefu | Parraysetu | Parrayrefs | Pmakelist | Pmakedict
| Pdict_has | Pawait | Pimport _ | Pinit_mod | Pupdate_mod | Phash
| Phash_mixint | Phash_mixstring | Phash_finalmix | Pis_null_undefined
| Pcreate_extension _ | Pjscomp _ | Pnull_to_opt | Pnull_undefined_to_opt
| Pis_null | Pis_not_none | Pval_from_option | Pval_from_option_not_nest
| Pis_poly_var_block | Praw_js_code _ | Pjs_fn_method | Ptagged_template _
| Ptemplate _ | Pdict_spread | Parray_spread | Plist_spread ->
| Pdebugger | Ptypeof | Psome | Psome_not_nest | Pnot_nested_option
| Pmakeblock _ | Pfield _ | Psetfield _ | Pduprecord | Precord_rest _
| Pjs_call _ | Pjsx _ | Pjs_object_create _ | Pjs_object_get _
| Pjs_object_set _ | Praise | Pobjcomp _ | Pobjorder | Pobjmin | Pobjmax
| Pobjtag | Pobjsize | Pnot | Pboolcomp _ | Pboolorder | Pboolmin | Pboolmax
| Pnegint | Paddint | Psubint | Pmulint | Pdivint | Pmodint | Ppowint
| Pandint | Porint | Pxorint | Pnotint | Plslint | Plsrint | Pasrint
| Pintcomp _ | Pintorder | Pintmin | Pintmax | Pintoffloat | Pfloatofint
| Pnegfloat | Pmodfloat | Paddfloat | Psubfloat | Pmulfloat | Pdivfloat
| Ppowfloat | Pfloatcomp _ | Pfloatorder | Pfloatmin | Pfloatmax | Pnegbigint
| Paddbigint | Psubbigint | Ppowbigint | Pmulbigint | Pdivbigint | Pmodbigint
| Pandbigint | Porbigint | Pxorbigint | Pnotbigint | Plslbigint | Pasrbigint
| Pbigintcomp _ | Pbigintorder | Pbigintmin | Pbigintmax | Pstringlength
| Pstringrefu | Pstringrefs | Pstringcomp _ | Pstringorder | Pstringmin
| Pstringmax | Pstringadd | Pmakearray | Parraylength | Parrayrefu
| Parraysetu | Parrayrefs | Pmakelist | Pmakedict | Pdict_has | Pawait
| Pimport _ | Pinit_mod | Pupdate_mod | Phash | Phash_mixint | Phash_mixstring
| Phash_finalmix | Pis_null_undefined | Pcreate_extension _ | Pjscomp _
| Pnull_to_opt | Pnull_undefined_to_opt | Pis_null | Pis_not_none
| Pval_from_option | Pval_from_option_not_nest | Pis_poly_var_block
| Praw_js_code _ | Pjs_fn_method | Ptagged_template _ | Ptemplate _
| Pdict_spread | Parray_spread | Plist_spread ->
false

(* Whether [primitive] on [args], the arguments of a call, evaluates them as
Expand Down
6 changes: 6 additions & 0 deletions compiler/typing/lambda.mli
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ type primitive =
| Psome_not_nest
(** [Some x] where [x] cannot itself be [undefined], so no wrapping is
needed. *)
| Pnot_nested_option
(** The identity on an option whose payload cannot be [undefined], so
it is never the block that represents [Some] of undefined. The
translation of a call marks such arguments, so that the body of an
inlined function unwraps them without
[Primitive_option.valFromOption]. *)
(* Operations on heap blocks *)
| Pmakeblock of tag_info
| Pfield of int * field_dbg_info
Expand Down
2 changes: 1 addition & 1 deletion compiler/typing/lambda_effects.ml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ let rec no_side_effects (lam : Lambda.t) : bool =
| [_; Lconst (Const_bigint (true, _))] -> true
| _ -> false)
| Pcreate_extension _ | Ptypeof | Pis_null | Pis_not_none | Psome
| Psome_not_nest | Pis_null_undefined | Pnull_to_opt
| Psome_not_nest | Pnot_nested_option | Pis_null_undefined | Pnull_to_opt
| Pnull_undefined_to_opt | Pjs_object_create _ | Pimport _
(* TODO: check *)
| Pmakeblock _
Expand Down
1 change: 1 addition & 0 deletions compiler/typing/printlambda.ml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ let primitive ppf = function
| Ptypeof -> fprintf ppf "typeof"
| Psome -> fprintf ppf "some"
| Psome_not_nest -> fprintf ppf "some_not_nest"
| Pnot_nested_option -> fprintf ppf "#not_nested_option"
| Pmakeblock taginfo ->
let what =
if Lambda.mutable_flag_of_tag_info taginfo = Immutable then "makeblock"
Expand Down
31 changes: 30 additions & 1 deletion compiler/typing/translcore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1702,10 +1702,39 @@ and transl_apply ?(uncurried_partial_application = None) lam sargs loc =
| _ ->
(build_apply lam []
(List.map
(fun (l, x) -> (Option.map transl_exp x, Btype.is_optional l))
(fun (l, x) ->
let optional = Btype.is_optional l in
( Option.map
(fun e ->
if optional then transl_exp e else transl_marked_argument e)
x,
optional ))
sargs)
: Lambda.t)

(* An argument of type [option<t>], where [t] cannot be [undefined], is
marked as such, so that the body of a function inlined here unwraps it
without [Primitive_option.valFromOption], as a [switch] written here
would. [Some(x)], constants and conversions from [null] are known
already, and marking them would hide what they are. *)
and transl_marked_argument (e : expression) =
let lam = transl_exp e in
match lam with
| Lconst _
| Lprim
{
primitive =
Psome | Psome_not_nest | Pnull_to_opt | Pnull_undefined_to_opt;
} ->
lam
| _ -> (
match (Ctype.expand_head e.exp_env e.exp_type).desc with
| Tconstr (path, [payload], _)
when Path.same path Predef.path_option
&& Typeopt.type_cannot_contain_undefined payload e.exp_env ->
Lambda.prim ~primitive:Pnot_nested_option ~args:[lam] e.exp_loc
| _ -> lam)

and transl_function loc (params : function_param list) body =
match params with
| [] -> assert false
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler_tests/src/flat_make_export_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Mocha.describe("Flat_make_export_test", () => {
Test_utils.eq("File \"flat_make_export_test.res\", line 23, characters 7-14", Flat_make_export_use.nested, "nested");
Test_utils.eq("File \"flat_make_export_test.res\", line 24, characters 7-14", Flat_make_export_use.recursive, "recursive");
Test_utils.eq("File \"flat_make_export_test.res\", line 25, characters 7-14", Flat_make_export_use.coerced, "coerced");
Test_utils.eq("File \"flat_make_export_test.res\", line 26, characters 7-14", Flat_make_export_use.rebound !== undefined ? Primitive_option.some(Primitive_option.valFromOption(Flat_make_export_use.rebound)()) : undefined, "rebound");
Test_utils.eq("File \"flat_make_export_test.res\", line 26, characters 7-14", Flat_make_export_use.rebound !== undefined ? Primitive_option.some(Flat_make_export_use.rebound()) : undefined, "rebound");
Test_utils.eq("File \"flat_make_export_test.res\", line 27, characters 7-14", Flat_make_export_use.applied, "applied");
Test_utils.eq("File \"flat_make_export_test.res\", line 28, characters 7-14", Flat_make_export_use.included, "one");
Test_utils.eq("File \"flat_make_export_test.res\", line 29, characters 7-14", Flat_make_export_use.aliased, "one");
Expand Down
5 changes: 2 additions & 3 deletions tests/compiler_tests/src/inline_export_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ let mappedNested = Primitive_option.some(Primitive_option.valFromOption(nested))

function mapValue(opt) {
if (opt !== undefined) {
return Primitive_option.some((x => x + 1 | 0)(Primitive_option.valFromOption(opt)));
return Primitive_option.some((x => x + 1 | 0)(opt));
}
}

function flatMapValue(opt) {
if (opt !== undefined) {
let x = Primitive_option.valFromOption(opt);
return x + 2 | 0;
return opt + 2 | 0;
}
}

Expand Down
Loading
Loading