diff --git a/changes/inline-through-aliases.polish.md b/changes/inline-through-aliases.polish.md new file mode 100644 index 0000000000..b9cfba4fe6 --- /dev/null +++ b/changes/inline-through-aliases.polish.md @@ -0,0 +1 @@ +- Inline `@inline` functions of other modules when they are called through a variable, as in `let f = M.f` or `let {f} = module(M)`. diff --git a/compiler/backend/README.md b/compiler/backend/README.md index 8183d7f83c..f94aaa097b 100644 --- a/compiler/backend/README.md +++ b/compiler/backend/README.md @@ -62,7 +62,8 @@ compute what they need themselves. There is one cross-module inlining phase: `simplify_alias` inlines a full call of another module's `@inline` function or functor, whose body that -module stored in its `.cmj`. A call it leaves alone is compiled as a call. +module stored in its `.cmj`, also through a variable bound to it, as in +`let f = M.f`. A call it leaves alone is compiled as a call. Inlining at the JavaScript level (`js_pass_tailcall_inline.ml`) is a separate step for local functions. diff --git a/compiler/backend/lam_pass_remove_alias.ml b/compiler/backend/lam_pass_remove_alias.ml index 84afc0bc11..3d61975d9e 100644 --- a/compiler/backend/lam_pass_remove_alias.ml +++ b/compiler/backend/lam_pass_remove_alias.ml @@ -55,8 +55,27 @@ let max_inline_depth = 4 let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t = let inline_depth = ref 0 in + (* The variables bound to a value of another module, as in [let f = M.f] + or [let {f} = module(M)]: a call of one is a call of [M.f], which the + case for other modules' functions below inlines *) + let module_values : Lambda.t Hash_ident.t = Hash_ident.create 17 in let rec simpl (lam : Lambda.t) : Lambda.t = match lam with + | Llet + ( _, + id, + (Lprim + {primitive = Pfield (_, Fld_module _); args = [Lglobal_module _]} + as value), + _ ) -> + Hash_ident.replace module_values id value; + Lambda_traverse.shallow_map_sharing simpl lam + | Lapply {ap_func = Lvar v; ap_args; ap_loc; ap_result_not_nested} + when Hash_ident.mem module_values v -> + simpl + (Lambda.apply ~result_not_nested:ap_result_not_nested + (Hash_ident.find module_values v) + ap_args ap_loc) (* In JSX preserve mode, keep a JSX tag like [M.make] as it is: flattening it may give a variable with a lowercase name, which JSX reads as an intrinsic element (#7432) *) diff --git a/tests/compiler_tests/src/hello_res.mjs b/tests/compiler_tests/src/hello_res.mjs index 456027a0c6..803440b662 100644 --- a/tests/compiler_tests/src/hello_res.mjs +++ b/tests/compiler_tests/src/hello_res.mjs @@ -8,9 +8,9 @@ let a = b - 1 | 0; console.log("hello, res"); -Stdlib_List.add(/* [] */0, Stdlib_List.length({hd: 1, tl: {hd: 2, tl: {hd: 3, tl: /* [] */0}}})); +Stdlib_List.length({hd: 1, tl: {hd: 2, tl: {hd: 3, tl: /* [] */0}}}); -Stdlib_List.add(/* [] */0, Stdlib_List.length({hd: 1, tl: {hd: 2, tl: {hd: 3, tl: /* [] */0}}})); +Stdlib_List.length({hd: 1, tl: {hd: 2, tl: {hd: 3, tl: /* [] */0}}}); console.log(3); diff --git a/tests/compiler_tests/src/inline_through_alias_test.mjs b/tests/compiler_tests/src/inline_through_alias_test.mjs new file mode 100644 index 0000000000..39f82e5cc2 --- /dev/null +++ b/tests/compiler_tests/src/inline_through_alias_test.mjs @@ -0,0 +1,73 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Mocha from "mocha"; +import * as Test_utils from "./test_utils.mjs"; +import * as Stdlib_Option from "@rescript/runtime/lib/es6/Stdlib_Option.mjs"; + +function viaLet(opt) { + if (opt !== undefined) { + return opt; + } else { + return 0; + } +} + +function viaDestructured(opt) { + if (opt !== undefined) { + return opt + 1 | 0; + } +} + +function viaLocal(opt) { + if (opt !== undefined) { + return opt; + } else { + return 1; + } +} + +function inOrder() { + let log = []; + log.push("opt"); + log.push("default"); + return [ + 2, + log + ]; +} + +Mocha.describe("Inline_through_alias_test", () => { + Mocha.test("let f = M.f", () => { + Test_utils.eq("File \"inline_through_alias_test.res\", line 36, characters 7-14", 2, 2); + Test_utils.eq("File \"inline_through_alias_test.res\", line 37, characters 7-14", 0, 0); + }); + Mocha.test("let {f} = module(M)", () => { + Test_utils.eq("File \"inline_through_alias_test.res\", line 41, characters 7-14", 2 + 1 | 0, 3); + Test_utils.eq("File \"inline_through_alias_test.res\", line 42, characters 7-14", undefined, undefined); + }); + Mocha.test("a local let f = M.f", () => { + Test_utils.eq("File \"inline_through_alias_test.res\", line 46, characters 7-14", 2, 2); + Test_utils.eq("File \"inline_through_alias_test.res\", line 47, characters 7-14", 1, 1); + }); + Mocha.test("arguments in order", () => Test_utils.eq("File \"inline_through_alias_test.res\", line 51, characters 7-14", inOrder(), [ + 2, + [ + "opt", + "default" + ] + ])); +}); + +let getOr = Stdlib_Option.getOr; + +let map = Stdlib_Option.map; + +export { + getOr, + map, + viaLet, + viaDestructured, + viaLocal, + inOrder, +} +/* Not a pure module */ diff --git a/tests/compiler_tests/src/inline_through_alias_test.res b/tests/compiler_tests/src/inline_through_alias_test.res new file mode 100644 index 0000000000..4fd2d59f67 --- /dev/null +++ b/tests/compiler_tests/src/inline_through_alias_test.res @@ -0,0 +1,53 @@ +// A call through a variable bound to another module's @inline function is +// inlined like a call of the function itself. +open Mocha +open Test_utils + +let getOr = Option.getOr +let {map} = module(Option) + +let viaLet = (opt: option) => getOr(opt, 0) +let viaDestructured = (opt: option) => opt->map(x => x + 1) + +let viaLocal = (opt: option) => { + let getOr = Option.getOr + getOr(opt, 1) +} + +// The arguments still run in order. +let inOrder = () => { + let log = [] + let getOr = Option.getOr + let v = getOr( + { + log->Array.push("opt") + Some(2) + }, + { + log->Array.push("default") + 0 + }, + ) + (v, log) +} + +describe(__MODULE__, () => { + test("let f = M.f", () => { + eq(__LOC__, viaLet(Some(2)), 2) + eq(__LOC__, viaLet(None), 0) + }) + + test("let {f} = module(M)", () => { + eq(__LOC__, viaDestructured(Some(2)), Some(3)) + eq(__LOC__, viaDestructured(None), None) + }) + + test("a local let f = M.f", () => { + eq(__LOC__, viaLocal(Some(2)), 2) + eq(__LOC__, viaLocal(None), 1) + }) + + test("arguments in order", () => { + eq(__LOC__, inOrder(), (2, ["opt", "default"])) + }) +})