Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async inline #5755

Merged
merged 4 commits into from
Oct 26, 2022
Merged
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@

# 10.1.0-rc.3

#### :bug: Bug Fix

- Fix issue where the JSX key type is not an optional string https://github.com/rescript-lang/syntax/pull/693

- Prevent inlining of async functions https://github.com/rescript-lang/rescript-compiler/issues/5754

# 10.1.0-rc.2

#### :bug: Bug Fix
Expand Down
4 changes: 4 additions & 0 deletions jscomp/core/lam_analysis.ml
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ let destruct_pattern (body : Lam.t) params args =
| Some _ | None -> false)
| _ -> false

(* Async functions cannot be beta reduced *)
let lfunction_can_be_beta_reduced (lfunction : Lam.lfunction) =
not lfunction.attr.async

(** Hints to inlining *)
let ok_to_inline_fun_when_app (m : Lam.lfunction) (args : Lam.t list) =
match m.attr.inline with
Expand Down
2 changes: 2 additions & 0 deletions jscomp/core/lam_analysis.mli
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ val no_side_effects : Lam.t -> bool

val size : Lam.t -> int

val lfunction_can_be_beta_reduced : Lam.lfunction -> bool

val ok_to_inline_fun_when_app : Lam.lfunction -> Lam.t list -> bool

val small_inline_size : int
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/lam_beta_reduce.ml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
]}
we can bound [x] to [100] in a single step
*)
let propogate_beta_reduce (meta : Lam_stats.t) (params : Ident.t list)
let propagate_beta_reduce (meta : Lam_stats.t) (params : Ident.t list)
(body : Lam.t) (args : Lam.t list) =
match Lam_beta_reduce_util.simple_beta_reduce params body args with
| Some x -> x
Expand Down Expand Up @@ -73,7 +73,7 @@ let propogate_beta_reduce (meta : Lam_stats.t) (params : Ident.t list)
| _ -> ());
Lam_util.refine_let ~kind:Strict param arg l)

let propogate_beta_reduce_with_map (meta : Lam_stats.t)
let propagate_beta_reduce_with_map (meta : Lam_stats.t)
(map : Lam_var_stats.stats Map_ident.t) params body args =
match Lam_beta_reduce_util.simple_beta_reduce params body args with
| Some x -> x
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/lam_beta_reduce.mli
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ val no_names_beta_reduce : Ident.t list -> Lam.t -> Lam.t list -> Lam.t
the obvious example is parameter
*)

val propogate_beta_reduce :
val propagate_beta_reduce :
Lam_stats.t -> Ident.t list -> Lam.t -> Lam.t list -> Lam.t

val propogate_beta_reduce_with_map :
val propagate_beta_reduce_with_map :
Lam_stats.t ->
Lam_var_stats.stats Map_ident.t ->
Ident.t list ->
Expand Down
6 changes: 3 additions & 3 deletions jscomp/core/lam_compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,14 @@ and compile_external_field_apply (appinfo : Lam.apply) (module_id : Ident.t)
in
let ap_args = appinfo.ap_args in
match ident_info.persistent_closed_lambda with
| Some (Lfunction { params; body; _ })
when Ext_list.same_length params ap_args ->
| Some (Lfunction ({ params; body; _ } as lfunction))
when Ext_list.same_length params ap_args && Lam_analysis.lfunction_can_be_beta_reduced lfunction ->
(* TODO: serialize it when exporting to save compile time *)
let _, param_map =
Lam_closure.is_closed_with_map Set_ident.empty params body
in
compile_lambda lambda_cxt
(Lam_beta_reduce.propogate_beta_reduce_with_map lambda_cxt.meta
(Lam_beta_reduce.propagate_beta_reduce_with_map lambda_cxt.meta
param_map params body ap_args)
| _ ->
let args_code, args =
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/lam_pass_count.ml
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ let collect_occurs lam : occ_tbl =
List.iter (fun (_v, l) -> count bv l) bindings;
count bv body
(* Note there is a difference here when do beta reduction for *)
| Lapply { ap_func = Lfunction { params; body }; ap_args = args; _ }
when Ext_list.same_length params args ->
| Lapply { ap_func = Lfunction ({ params; body } as lfunction); ap_args = args; _ }
when Ext_list.same_length params args && Lam_analysis.lfunction_can_be_beta_reduced lfunction ->
count bv (Lam_beta_reduce.no_names_beta_reduce params body args)
(* | Lapply{fn = Lfunction{function_kind = Tupled; params; body}; *)
(* args = [Lprim {primitive = Pmakeblock _; args; _}]; _} *)
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/lam_pass_lets_dce.ml
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ let lets_helper (count_var : Ident.t -> Lam_pass_count.used_info) lam : Lam.t =
end
| Lsequence(l1, l2) -> Lam.seq (simplif l1) (simplif l2)

| Lapply{ap_func = Lfunction{params; body}; ap_args = args; _}
when Ext_list.same_length params args ->
| Lapply{ap_func = Lfunction ({params; body} as lfunction); ap_args = args; _}
when Ext_list.same_length params args && Lam_analysis.lfunction_can_be_beta_reduced lfunction ->
simplif (Lam_beta_reduce.no_names_beta_reduce params body args)
(* | Lapply{ fn = Lfunction{function_kind = Tupled; params; body}; *)
(* args = [Lprim {primitive = Pmakeblock _; args; _}]; _} *)
Expand Down
4 changes: 2 additions & 2 deletions jscomp/core/lam_pass_lets_dce.pp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ let lets_helper (count_var : Ident.t -> Lam_pass_count.used_info) lam : Lam.t =
end
| Lsequence(l1, l2) -> Lam.seq (simplif l1) (simplif l2)

| Lapply{ap_func = Lfunction{params; body}; ap_args = args; _}
when Ext_list.same_length params args ->
| Lapply{ap_func = Lfunction ({params; body} as lfunction); ap_args = args; _}
when Ext_list.same_length params args && Lam_analysis.lfunction_can_be_beta_reduced lfunction ->
simplif (Lam_beta_reduce.no_names_beta_reduce params body args)
(* | Lapply{ fn = Lfunction{function_kind = Tupled; params; body}; *)
(* args = [Lprim {primitive = Pmakeblock _; args; _}]; _} *)
Expand Down
16 changes: 8 additions & 8 deletions jscomp/core/lam_pass_remove_alias.ml
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
| Some v -> v <> Parameter
| None -> true)
| _ -> true) ->
simpl (Lam_beta_reduce.propogate_beta_reduce meta params body args)
simpl (Lam_beta_reduce.propagate_beta_reduce meta params body args)
| _ -> Lam.apply (simpl l1) (Ext_list.map args simpl) ap_info)
(* Function inlining interact with other optimizations...

Expand All @@ -160,7 +160,7 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
Some
( Lfunction ({ params; body; attr = { is_a_functor } } as m),
rec_flag );
}) ->
}) when Lam_analysis.lfunction_can_be_beta_reduced m ->
if Ext_list.same_length ap_args params (* && false *) then
if
is_a_functor
Expand All @@ -174,7 +174,7 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
(* Check: recursive applying may result in non-termination *)
(* Ext_log.dwarn __LOC__ "beta .. %s/%d" v.name v.stamp ; *)
simpl
(Lam_beta_reduce.propogate_beta_reduce meta params body
(Lam_beta_reduce.propagate_beta_reduce meta params body
ap_args)
else if
(* Lam_analysis.size body < Lam_analysis.small_inline_size *)
Expand All @@ -194,7 +194,7 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
| false, (_, param_map) | true, (true, param_map) -> (
match rec_flag with
| Lam_rec ->
Lam_beta_reduce.propogate_beta_reduce_with_map meta
Lam_beta_reduce.propagate_beta_reduce_with_map meta
param_map params body ap_args
| Lam_self_rec -> normal ()
| Lam_non_rec ->
Expand All @@ -205,15 +205,15 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
then normal ()
else
simpl
(Lam_beta_reduce.propogate_beta_reduce_with_map meta
(Lam_beta_reduce.propagate_beta_reduce_with_map meta
param_map params body ap_args))
| _ -> normal ()
else normal ()
else normal ()
| Some _ | None -> normal ())
| Lapply { ap_func = Lfunction { params; body }; ap_args = args; _ }
when Ext_list.same_length params args ->
simpl (Lam_beta_reduce.propogate_beta_reduce meta params body args)
| Lapply { ap_func = Lfunction ({ params; body } as lfunction); ap_args = args; _ }
when Ext_list.same_length params args && Lam_analysis.lfunction_can_be_beta_reduced lfunction ->
simpl (Lam_beta_reduce.propagate_beta_reduce meta params body args)
(* | Lapply{ fn = Lfunction{function_kind = Tupled; params; body}; *)
(* args = [Lprim {primitive = Pmakeblock _; args; _}]; _} *)
(* (\** TODO: keep track of this parameter in ocaml trunk, *)
Expand Down
20 changes: 20 additions & 0 deletions jscomp/test/async_inline.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';


async function willBeInlined(param) {
return 3;
}

var inlined = willBeInlined(undefined);

function wrapSomethingAsync(param) {
((async function (param) {
var test = await Promise.resolve("Test");
console.log(test);
})(777));
}

exports.willBeInlined = willBeInlined;
exports.inlined = inlined;
exports.wrapSomethingAsync = wrapSomethingAsync;
/* inlined Not a pure module */
12 changes: 12 additions & 0 deletions jscomp/test/async_inline.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
let willBeInlined = async () => 3

let inlined = willBeInlined ()

let wrapSomethingAsync: unit => unit = () => {
let _ = (
async (_) => {
let test = await Js.Promise.resolve("Test")
Js.log(test)
}
)(777)
}
3 changes: 2 additions & 1 deletion jscomp/test/build.ninja

Large diffs are not rendered by default.

44 changes: 25 additions & 19 deletions lib/4.06.1/unstable/js_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -91322,6 +91322,8 @@ val no_side_effects : Lam.t -> bool

val size : Lam.t -> int

val lfunction_can_be_beta_reduced : Lam.lfunction -> bool

val ok_to_inline_fun_when_app : Lam.lfunction -> Lam.t list -> bool

val small_inline_size : int
Expand Down Expand Up @@ -91578,6 +91580,10 @@ let destruct_pattern (body : Lam.t) params args =
| Some _ | None -> false)
| _ -> false

(* Async functions cannot be beta reduced *)
let lfunction_can_be_beta_reduced (lfunction : Lam.lfunction) =
not lfunction.attr.async

(** Hints to inlining *)
let ok_to_inline_fun_when_app (m : Lam.lfunction) (args : Lam.t list) =
match m.attr.inline with
Expand Down Expand Up @@ -94288,10 +94294,10 @@ val no_names_beta_reduce : Ident.t list -> Lam.t -> Lam.t list -> Lam.t
the obvious example is parameter
*)

val propogate_beta_reduce :
val propagate_beta_reduce :
Lam_stats.t -> Ident.t list -> Lam.t -> Lam.t list -> Lam.t

val propogate_beta_reduce_with_map :
val propagate_beta_reduce_with_map :
Lam_stats.t ->
Lam_var_stats.stats Map_ident.t ->
Ident.t list ->
Expand Down Expand Up @@ -94367,7 +94373,7 @@ end = struct
]}
we can bound [x] to [100] in a single step
*)
let propogate_beta_reduce (meta : Lam_stats.t) (params : Ident.t list)
let propagate_beta_reduce (meta : Lam_stats.t) (params : Ident.t list)
(body : Lam.t) (args : Lam.t list) =
match Lam_beta_reduce_util.simple_beta_reduce params body args with
| Some x -> x
Expand Down Expand Up @@ -94396,7 +94402,7 @@ let propogate_beta_reduce (meta : Lam_stats.t) (params : Ident.t list)
| _ -> ());
Lam_util.refine_let ~kind:Strict param arg l)

let propogate_beta_reduce_with_map (meta : Lam_stats.t)
let propagate_beta_reduce_with_map (meta : Lam_stats.t)
(map : Lam_var_stats.stats Map_ident.t) params body args =
match Lam_beta_reduce_util.simple_beta_reduce params body args with
| Some x -> x
Expand Down Expand Up @@ -99765,14 +99771,14 @@ and compile_external_field_apply (appinfo : Lam.apply) (module_id : Ident.t)
in
let ap_args = appinfo.ap_args in
match ident_info.persistent_closed_lambda with
| Some (Lfunction { params; body; _ })
when Ext_list.same_length params ap_args ->
| Some (Lfunction ({ params; body; _ } as lfunction))
when Ext_list.same_length params ap_args && Lam_analysis.lfunction_can_be_beta_reduced lfunction ->
(* TODO: serialize it when exporting to save compile time *)
let _, param_map =
Lam_closure.is_closed_with_map Set_ident.empty params body
in
compile_lambda lambda_cxt
(Lam_beta_reduce.propogate_beta_reduce_with_map lambda_cxt.meta
(Lam_beta_reduce.propagate_beta_reduce_with_map lambda_cxt.meta
param_map params body ap_args)
| _ ->
let args_code, args =
Expand Down Expand Up @@ -257925,8 +257931,8 @@ let collect_occurs lam : occ_tbl =
List.iter (fun (_v, l) -> count bv l) bindings;
count bv body
(* Note there is a difference here when do beta reduction for *)
| Lapply { ap_func = Lfunction { params; body }; ap_args = args; _ }
when Ext_list.same_length params args ->
| Lapply { ap_func = Lfunction ({ params; body } as lfunction); ap_args = args; _ }
when Ext_list.same_length params args && Lam_analysis.lfunction_can_be_beta_reduced lfunction ->
count bv (Lam_beta_reduce.no_names_beta_reduce params body args)
(* | Lapply{fn = Lfunction{function_kind = Tupled; params; body}; *)
(* args = [Lprim {primitive = Pmakeblock _; args; _}]; _} *)
Expand Down Expand Up @@ -258306,8 +258312,8 @@ let lets_helper (count_var : Ident.t -> Lam_pass_count.used_info) lam : Lam.t =
end
| Lsequence(l1, l2) -> Lam.seq (simplif l1) (simplif l2)

| Lapply{ap_func = Lfunction{params; body}; ap_args = args; _}
when Ext_list.same_length params args ->
| Lapply{ap_func = Lfunction ({params; body} as lfunction); ap_args = args; _}
when Ext_list.same_length params args && Lam_analysis.lfunction_can_be_beta_reduced lfunction ->
simplif (Lam_beta_reduce.no_names_beta_reduce params body args)
(* | Lapply{ fn = Lfunction{function_kind = Tupled; params; body}; *)
(* args = [Lprim {primitive = Pmakeblock _; args; _}]; _} *)
Expand Down Expand Up @@ -258606,7 +258612,7 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
| Some v -> v <> Parameter
| None -> true)
| _ -> true) ->
simpl (Lam_beta_reduce.propogate_beta_reduce meta params body args)
simpl (Lam_beta_reduce.propagate_beta_reduce meta params body args)
| _ -> Lam.apply (simpl l1) (Ext_list.map args simpl) ap_info)
(* Function inlining interact with other optimizations...

Expand All @@ -258628,7 +258634,7 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
Some
( Lfunction ({ params; body; attr = { is_a_functor } } as m),
rec_flag );
}) ->
}) when Lam_analysis.lfunction_can_be_beta_reduced m ->
if Ext_list.same_length ap_args params (* && false *) then
if
is_a_functor
Expand All @@ -258642,7 +258648,7 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
(* Check: recursive applying may result in non-termination *)
(* Ext_log.dwarn __LOC__ "beta .. %s/%d" v.name v.stamp ; *)
simpl
(Lam_beta_reduce.propogate_beta_reduce meta params body
(Lam_beta_reduce.propagate_beta_reduce meta params body
ap_args)
else if
(* Lam_analysis.size body < Lam_analysis.small_inline_size *)
Expand All @@ -258662,7 +258668,7 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
| false, (_, param_map) | true, (true, param_map) -> (
match rec_flag with
| Lam_rec ->
Lam_beta_reduce.propogate_beta_reduce_with_map meta
Lam_beta_reduce.propagate_beta_reduce_with_map meta
param_map params body ap_args
| Lam_self_rec -> normal ()
| Lam_non_rec ->
Expand All @@ -258673,15 +258679,15 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lam.t) : Lam.t =
then normal ()
else
simpl
(Lam_beta_reduce.propogate_beta_reduce_with_map meta
(Lam_beta_reduce.propagate_beta_reduce_with_map meta
param_map params body ap_args))
| _ -> normal ()
else normal ()
else normal ()
| Some _ | None -> normal ())
| Lapply { ap_func = Lfunction { params; body }; ap_args = args; _ }
when Ext_list.same_length params args ->
simpl (Lam_beta_reduce.propogate_beta_reduce meta params body args)
| Lapply { ap_func = Lfunction ({ params; body } as lfunction); ap_args = args; _ }
when Ext_list.same_length params args && Lam_analysis.lfunction_can_be_beta_reduced lfunction ->
simpl (Lam_beta_reduce.propagate_beta_reduce meta params body args)
(* | Lapply{ fn = Lfunction{function_kind = Tupled; params; body}; *)
(* args = [Lprim {primitive = Pmakeblock _; args; _}]; _} *)
(* (\** TODO: keep track of this parameter in ocaml trunk, *)
Expand Down
Loading