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/inline-assigned-payload.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix the payload of a `Some` or a tuple field, made from a variable that is assigned later, giving the variable's new value.
28 changes: 19 additions & 9 deletions compiler/backend/lam_pass_remove_alias.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t =
Set_ident.union !assignable (Lambda_traverse.assigned_variables lam);
lam
in
(* What [meta.ident_tbl] records about a binding, like [Some(x)] or
[(x, 1)], gives [x] as it was when the binding was made. Moving a read of
[x] to a later use is wrong once something assigns [x] in between. *)
let stable (lam : Lambda.t) =
match lam with
| Lvar x -> not (Set_ident.mem x !assignable)
| _ -> true
in
let inline_depth = ref 0 in
let rec simpl (lam : Lambda.t) : Lambda.t =
match lam with
Expand All @@ -89,22 +97,24 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t =
(* ATTENTION:
Main use case, we should detect inline all immutable block .. *)
match simpl arg with
| Lvar v as l ->
Lam_util.field_flatten_get
(fun _ ->
if l == arg then lam else Lambda.prim ~primitive ~args:[l] loc)
v i info meta.ident_tbl
| Lvar v as l -> (
let keep _ =
if l == arg then lam else Lambda.prim ~primitive ~args:[l] loc
in
match Lam_util.field_flatten_get keep v i info meta.ident_tbl with
| field when stable field -> field
| _ -> keep ())
| l -> if l == arg then lam else Lambda.prim ~primitive ~args:[l] loc)
| Lprim
{
primitive = (Pval_from_option | Pval_from_option_not_nest) as p;
args = [(Lvar v as lvar)];
} as x -> (
match Hash_ident.find_opt meta.ident_tbl v with
| Some (OptionalBlock (l, _)) -> l
| Some (OptionalBlock (l, _)) when stable l -> l
(* [v] bound to [Some(l)]: unwrapping gives back [l], whether or not
it was nested. Only substitute forms that are safe to repeat. *)
| Some (Normal_optional (Lvar _ as l)) -> l
| Some (Normal_optional (Lvar _ as l)) when stable l -> l
| Some (Normal_optional (Lconst c as l))
when not (Lambda.const_is_allocating c) ->
l
Expand All @@ -114,12 +124,12 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t =
match Hash_ident.find_opt meta.ident_tbl id with
| Some (Constant c) when is_const_some c -> simpl l2
| Some (ImmutableBlock _ | Normal_optional _) -> simpl l2
| Some (OptionalBlock (l, Null)) ->
| Some (OptionalBlock (l, Null)) when stable l ->
Lambda.if_
(Lambda.not_ Location.none
(Lambda.prim ~primitive:Pis_null ~args:[l] Location.none))
(simpl l2) (simpl l3)
| Some (OptionalBlock (l, Null_undefined)) ->
| Some (OptionalBlock (l, Null_undefined)) when stable l ->
Lambda.if_
(Lambda.not_ Location.none
(Lambda.prim ~primitive:Pis_null_undefined ~args:[l] Location.none))
Expand Down
47 changes: 47 additions & 0 deletions tests/compiler_tests/src/inline_assigned_payload_lib.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

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

function someBeforeAssign(n, q, q2) {
let s = q;
let o = Primitive_option.some(s);
for (let _for = 0; _for <= n; ++_for) {
s = q2;
}
return [
Primitive_option.valFromOption(o),
s
];
}

function tupleBeforeAssign(n, q, q2) {
let s = q;
let t_0 = s;
for (let _for = 0; _for <= n; ++_for) {
s = q2;
}
return [
t_0,
s
];
}

function nullableBeforeAssign(n, q, q2) {
let s = q;
let o = s;
for (let _for = 0; _for <= n; ++_for) {
s = q2;
}
if (o == null) {
return "none";
} else {
return o;
}
}

export {
someBeforeAssign,
tupleBeforeAssign,
nullableBeforeAssign,
}
/* No side effect */
39 changes: 39 additions & 0 deletions tests/compiler_tests/src/inline_assigned_payload_lib.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Inlined by inline_assigned_payload_test.res. Each function is stored
// optimized, so [s] is a variable the loop assigns: the payload [o] or the
// field of [t] must keep the value [s] had before the loop.
@inline
let someBeforeAssign = (n: int, q: 'a, q2: 'a) => {
let s = ref(q)
let o = Some(s.contents)
for _ in 0 to n {
s := q2
}
switch o {
| Some(v) => Some((v, s.contents))
| None => None
}
}

@inline
let tupleBeforeAssign = (n: int, q: 'a, q2: 'a) => {
let s = ref(q)
let t = (s.contents, 1)
for _ in 0 to n {
s := q2
}
let (a, _) = t
(a, s.contents)
}

@inline
let nullableBeforeAssign = (n: int, q: Nullable.t<string>, q2: Nullable.t<string>) => {
let s = ref(q)
let o = Nullable.toOption(s.contents)
for _ in 0 to n {
s := q2
}
switch o {
| Some(v) => v
| None => "none"
}
}
88 changes: 88 additions & 0 deletions tests/compiler_tests/src/inline_assigned_payload_test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Generated by ReScript, PLEASE EDIT WITH CARE

import * as Mocha from "mocha";
import * as Test_utils from "./test_utils.mjs";
import * as Primitive_option from "@rescript/runtime/lib/es6/Primitive_option.mjs";

function someBeforeAssign() {
let s = "a";
let o = Primitive_option.some(s);
for (let _for = 0; _for <= 3; ++_for) {
s = "b";
}
return [
Primitive_option.valFromOption(o),
s
];
}

function tupleBeforeAssign() {
let s = "a";
let t_0 = s;
for (let _for = 0; _for <= 3; ++_for) {
s = "b";
}
return [
t_0,
s
];
}

function nullableBeforeAssign() {
let s = "a";
let o = s;
for (let _for = 0; _for <= 3; ++_for) {
s = null;
}
if (o == null) {
return "none";
} else {
return o;
}
}

Mocha.describe("Inline_assigned_payload_test", () => {
Mocha.test("the payload of a Some, made before its variable is assigned", () => {
let s = "a";
let o = Primitive_option.some(s);
for (let _for = 0; _for <= 3; ++_for) {
s = "b";
}
Test_utils.eq("File \"inline_assigned_payload_test.res\", line 15, characters 7-14", [
"a",
"b"
], [
Primitive_option.valFromOption(o),
s
]);
});
Mocha.test("the field of a tuple, made before its variable is assigned", () => {
let s = "a";
let t_0 = s;
for (let _for = 0; _for <= 3; ++_for) {
s = "b";
}
Test_utils.eq("File \"inline_assigned_payload_test.res\", line 19, characters 7-14", [
"a",
"b"
], [
t_0,
s
]);
});
Mocha.test("a nullable converted to an option before its variable is assigned", () => {
let s = "a";
let o = s;
for (let _for = 0; _for <= 3; ++_for) {
s = null;
}
Test_utils.eq("File \"inline_assigned_payload_test.res\", line 23, characters 7-14", "a", o == null ? "none" : o);
});
});

export {
someBeforeAssign,
tupleBeforeAssign,
nullableBeforeAssign,
}
/* Not a pure module */
25 changes: 25 additions & 0 deletions tests/compiler_tests/src/inline_assigned_payload_test.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// A [Some] or a tuple made from a variable that is assigned later must keep
// the value the variable had when it was made.
open Mocha
open Test_utils

let someBeforeAssign = () => Inline_assigned_payload_lib.someBeforeAssign(3, "a", "b")

let tupleBeforeAssign = () => Inline_assigned_payload_lib.tupleBeforeAssign(3, "a", "b")

let nullableBeforeAssign = () =>
Inline_assigned_payload_lib.nullableBeforeAssign(3, Nullable.make("a"), Nullable.null)

describe(__MODULE__, () => {
test("the payload of a Some, made before its variable is assigned", () => {
eq(__LOC__, Some(("a", "b")), someBeforeAssign())
})

test("the field of a tuple, made before its variable is assigned", () => {
eq(__LOC__, ("a", "b"), tupleBeforeAssign())
})

test("a nullable converted to an option before its variable is assigned", () => {
eq(__LOC__, "a", nullableBeforeAssign())
})
})
Loading