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-through-aliases.polish.md
Original file line number Diff line number Diff line change
@@ -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)`.
3 changes: 2 additions & 1 deletion compiler/backend/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
19 changes: 19 additions & 0 deletions compiler/backend/lam_pass_remove_alias.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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) *)
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler_tests/src/hello_res.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
73 changes: 73 additions & 0 deletions tests/compiler_tests/src/inline_through_alias_test.mjs
Original file line number Diff line number Diff line change
@@ -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 */
53 changes: 53 additions & 0 deletions tests/compiler_tests/src/inline_through_alias_test.res
Original file line number Diff line number Diff line change
@@ -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<int>) => getOr(opt, 0)
let viaDestructured = (opt: option<int>) => opt->map(x => x + 1)

let viaLocal = (opt: option<int>) => {
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"]))
})
})
Loading