Skip to content
Closed
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/late-inline-alias-pass.polish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Simplify the body of a function from another module that is inlined while compiling to JavaScript, so a `Some(x)` argument is read directly and a function argument applied in the body is inlined.
24 changes: 20 additions & 4 deletions compiler/backend/lam_compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -589,12 +589,28 @@ let compile output_prefix =
let _, param_map =
Lam_closure.is_closed_with_map Set_ident.empty params body
in
(* What [meta.ident_tbl] records about the caller's bindings was
collected before the last passes rewrote them, so it may give
bodies or payloads naming variables that are gone. The inlined body
is simplified with a fresh table, which only gets what is known
about the parameters it binds. *)
let ident_tbl = Hash_ident.create 17 in
let inlined =
Lam_beta_reduce.propagate_beta_reduce_with_map lambda_cxt.meta param_map
params body ap_args
Lam_beta_reduce.propagate_beta_reduce_with_map
{lambda_cxt.meta with ident_tbl}
param_map params body ap_args
in
(* The inlined body comes from another module, so the variables it
assigns are not in [meta.assignable] yet. *)
(* The alias pass ran before this call was inlined: run it on the
inlined body, so that a known argument, such as a [Some], is
resolved there too *)
let inlined =
Lam_pass_remove_alias.simplify_alias
{lambda_cxt.meta with ident_tbl}
inlined
in
(* The inlined body comes from another module, and the alias pass may
have inlined more into it, so the variables it assigns are not in
[meta.assignable] yet. *)
let meta =
{
lambda_cxt.meta with
Expand Down
11 changes: 8 additions & 3 deletions compiler/backend/lam_pass_remove_alias.ml
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,14 @@ let max_inline_depth = 4
let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t =
(* Inlining keeps the reads of assigned variables in order (see
[Lam_beta_reduce]), so it needs the variables some [Lassign] assigns.
Only [Lam_pass_sroa], which runs later, makes [Lassign]s, so [lam] has
none yet; computing them here keeps the inlining sound if that changes. *)
let assignable = ref (Lambda_traverse.assigned_variables lam) in
Before [Lam_pass_sroa], only bodies inlined from other modules have
[Lassign]s. When this pass runs on a body inlined while compiling to
JavaScript, [lam] is only that body: the variables of the function
around it that are assigned come from [meta.assignable]. *)
let assignable =
ref
(Set_ident.union meta.assignable (Lambda_traverse.assigned_variables lam))
in
let current_meta () = {meta with assignable = !assignable} in
(* A body inlined from another module was optimized by [Lam_pass_sroa]
there, so it may assign variables, and inlining renamed them. They are
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler_tests/src/inline_export_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ let mappedNested = Primitive_option.some(Primitive_option.valFromOption(nested))

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

Expand Down
75 changes: 75 additions & 0 deletions tests/compiler_tests/src/inline_late_alias_lib.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.mjs";

function id(x) {
return x;
}

let c = id(3);

function apply(x, f) {
return f(x);
}

function apply2(p, v, g) {
return g(v, p);
}

function unwrapG(x, o) {
if (o !== undefined) {
return [
Primitive_option.valFromOption(o),
x
];
}
}

function weird(p, o, f) {
f();
if (o !== undefined) {
return o + p | 0;
} else {
return p;
}
}

function weirdT(p, t, f) {
f();
return t[0] + p | 0;
}

function countUp(p, n) {
let r = p;
let tmp = r;
for (let _for = 0; _for <= n; ++_for) {
r = r + 1 | 0;
}
return [
tmp,
r
];
}

function weirdG(p, o, f) {
f();
if (o !== undefined) {
return [
Primitive_option.valFromOption(o),
p
];
}
}

export {
id,
c,
apply,
apply2,
unwrapG,
weird,
weirdT,
countUp,
weirdG,
}
/* c Not a pure module */
61 changes: 61 additions & 0 deletions tests/compiler_tests/src/inline_late_alias_lib.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Inlined by inline_late_alias_test.res. Each call there passes a parameter of
// the caller, so it is inlined while compiling to JavaScript.
@inline(never) let id = x => x

let c = id(3)

@inline
let apply = (x, f) => f(x)

@inline
let apply2 = (p: int, v, g) => g(v, p)

@inline
let unwrapG = (x: int, o: option<'a>) =>
switch o {
| Some(v) => Some((v, x))
| None => None
}

// [f] may assign the variable the option or the tuple was made from.
@inline
let weird = (p: int, o: option<int>, f) => {
f()
switch o {
| Some(v) => v + p
| None => p
}
}

@inline
let weirdT = (p: int, t: (int, int), f) => {
f()
let (a, _) = t
a + p
}

// Reads [r] before the loop that assigns it. Inlined into
// inline_late_alias_wrap.res's [countUpWrapped] only by the alias pass that
// runs while compiling to JavaScript.
@inline
let countUp = (p: int, n: int) => {
let r = ref(p)
(
r.contents,
{
for _ in 0 to n {
r := r.contents + 1
}
r.contents
},
)
}

@inline
let weirdG = (p: int, o: option<'a>, f) => {
f()
switch o {
| Some(v) => Some((v, p))
| None => None
}
}
Loading
Loading