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-no-assigned-body.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fix an `@inline` function from another module that read a local ref after the ref was updated, as in a `Some` or a tuple made from the ref before a loop. An `@inline` function that updates a local ref or mutable record is no longer inlined in other modules, and warning 113 says so.
11 changes: 10 additions & 1 deletion compiler/backend/lam_check.ml
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
checks
1. variables are not bound twice
2. all variables are of right scope
3. no variable is assigned before [Lam_pass_sroa], unless [after_sroa]
*)
let check ~file ~pass lam =
let check ~file ~pass ~after_sroa lam =
let defined_variables = Hash_ident.create 1000 in
let success = ref true in
let use (id : Ident.t) =
Expand Down Expand Up @@ -170,6 +171,14 @@ let check ~file ~pass lam =
def v;
iter e2
| Lassign (id, e) ->
(* Only [Lam_pass_sroa] makes assignments. The passes before it rely
on there being none, so a variable keeps the value it is bound to. *)
if not after_sroa then (
Format.fprintf
(Compiler_request_output.stderr_formatter ())
"\n[SANITY after %s]:%s/%d assigned before sroa in %s@." pass id.name
id.stamp file;
success := false);
use id;
iter e
in
Expand Down
3 changes: 2 additions & 1 deletion compiler/backend/lam_check.mli
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)

val check : file:string -> pass:string -> Lambda.t -> Lambda.t
val check :
file:string -> pass:string -> after_sroa:bool -> Lambda.t -> Lambda.t
15 changes: 10 additions & 5 deletions compiler/backend/lam_compile_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -287,14 +287,17 @@ let compile ?on_optimized_lambda (output_prefix : string)
let diagnostics =
if debug_ir then Some (Ir_diagnostics.create ~output_prefix) else None
in
let d pass lam =
let d ?(after_sroa = false) pass lam =
(match diagnostics with
| Some diagnostics ->
Ir_diagnostics.dump_lam diagnostics ~pass lam;
Ir_log.dwarn ~__POS__ "START CHECKING PASS %s@." pass
| None -> ());
if !((Js_config.current ()).check_lam) || debug_ir then (
ignore @@ Lam_check.check ~file:(Loc_report.get_input_name ()) ~pass lam;
ignore
@@ Lam_check.check
~file:(Loc_report.get_input_name ())
~pass ~after_sroa lam;
if debug_ir then Ir_log.dwarn ~__POS__ "FINISH CHECKING PASS %s@." pass);
lam
in
Expand Down Expand Up @@ -349,9 +352,11 @@ let compile ?on_optimized_lambda (output_prefix : string)
|> d "simplify_alias 3"
(* we should investigate a better way to put different passes : )*)
|> Lam_pass_lets_dce.simplify_lets
|> d "simplify_lets" |> Lam_pass_sroa.simplify |> d "sroa"
|> Lam_pass_exits.simplify_exits |> d "simplify_exits 3"
|> Lam_pass_guard_raises.guard_raises |> d "guard_raises"
|> d "simplify_lets" |> Lam_pass_sroa.simplify |> d ~after_sroa:true "sroa"
|> Lam_pass_exits.simplify_exits
|> d ~after_sroa:true "simplify_exits 3"
|> Lam_pass_guard_raises.guard_raises
|> d ~after_sroa:true "guard_raises"
|> fun lam ->
if debug_ir then
Ir_log.dwarn ~__POS__ "Before coercion: %a@." Lam_stats.print meta;
Expand Down
9 changes: 9 additions & 0 deletions compiler/backend/lam_stats_export.ml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ let values_of_export (meta : Lam_stats.t) (export_map : Lambda.t Map_ident.t) :
else if Option.is_some directive then Some "it has a directive"
else if calls_relative_external lambda then
Some "it calls an external with a path relative to this file"
else if
(* [Lam_pass_sroa] has turned a local ref or mutable record
into a variable that [Lassign] updates. Other modules
inline the body before their own sroa, in passes that
assume no variable is assigned yet. *)
not
(Set_ident.is_empty
(Lambda_traverse.assigned_variables lambda))
then Some "it updates a local ref or mutable record"
else if Lam_closure.is_closed lambda then None
else
(* The first variable that [Lam_closure.is_closed] objects
Expand Down
49 changes: 0 additions & 49 deletions tests/compiler_tests/src/inline_assign_cross_module_lib.mjs
Original file line number Diff line number Diff line change
@@ -1,50 +1,6 @@
// Generated by ReScript, PLEASE EDIT WITH CARE


function bumpTwice(x) {
let r = x;
r = r + 1 | 0;
if (x > 3) {
r = (r << 1);
}
return r;
}

function readThenAssign(x, f) {
let r = x;
let g = f(r);
r = 10;
return g() + r | 0;
}

function copyThenAssign(x) {
let r = x;
let s = r;
r = s + 1 | 0;
return s + r | 0;
}

function negated(x) {
let r = x;
r = !r;
if (r) {
return 1;
} else {
return 2;
}
}

function matchAfterAssign(x) {
let r = x;
r = "b";
let match = r;
if (match === "a") {
return 1;
} else {
return 2;
}
}

function orDefault(f, $$default) {
try {
return f();
Expand All @@ -54,11 +10,6 @@ function orDefault(f, $$default) {
}

export {
bumpTwice,
readThenAssign,
copyThenAssign,
negated,
matchAfterAssign,
orDefault,
}
/* No side effect */
50 changes: 0 additions & 50 deletions tests/compiler_tests/src/inline_assign_cross_module_lib.res
Original file line number Diff line number Diff line change
@@ -1,53 +1,3 @@
// Inlined by inline_assign_cross_module_test.res
@inline
let bumpTwice = (x: int) => {
let r = ref(x)
r := r.contents + 1
if x > 3 {
r := r.contents * 2
}
r.contents
}

// Inlined by inline_assign_cross_module_test.res: [f] reads [r] before the
// assignment, and the closure it returns must see that value, not the new one.
@inline
let readThenAssign = (x: int, f: int => unit => int) => {
let r = ref(x)
let g = f(r.contents)
r := 10
g() + r.contents
}

// Inlined by inline_assign_cross_module_test.res: [s] keeps the value [r] had
// before the assignment.
@inline
let copyThenAssign = (x: int) => {
let r = ref(x)
let s = r.contents
r := s + 1
s + r.contents
}

// Inlined by inline_assign_cross_module_test.res: the condition and the switch
// read [r] after the assignment, not the argument it was bound to.
@inline
let negated = (x: bool) => {
let r = ref(x)
r := !r.contents
r.contents ? 1 : 2
}

@inline
let matchAfterAssign = (x: string) => {
let r = ref(x)
r := "b"
switch r.contents {
| "a" => 1
| _ => 2
}
}

// Inlined by inline_assign_cross_module_test.res: the handler binds the
// exception, which each inlined copy renames.
@inline
Expand Down
114 changes: 1 addition & 113 deletions tests/compiler_tests/src/inline_assign_cross_module_test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,80 +4,6 @@ import * as Mocha from "mocha";
import * as Test_utils from "./test_utils.mjs";
import * as Stdlib_JsError from "@rescript/runtime/lib/es6/Stdlib_JsError.mjs";

function id(x) {
return x;
}

function addBumped(x, y) {
let r = x;
r = r + 1 | 0;
if (x > 3) {
r = (r << 1);
}
let tmp = r;
let r$1 = y;
r$1 = r$1 + 1 | 0;
if (y > 3) {
r$1 = (r$1 << 1);
}
return tmp + r$1 | 0;
}

function bumpConstants() {
let r = 5;
r = r + 1 | 0;
r = (r << 1);
let tmp = r;
let r$1 = 3;
r$1 = r$1 + 1 | 0;
return [
tmp,
r$1
];
}

function readBeforeAssign() {
let r = 5;
let p = r;
r = 10;
return p + r | 0;
}

function copyBeforeAssign() {
let r = 1;
let s = r;
r = s + 1 | 0;
return s + r | 0;
}

function readBeforeAssignBound() {
let r = 5;
let p = r;
r = 10;
return p + r | 0;
}

function negatedTrue() {
let r = true;
r = !r;
if (r) {
return 1;
} else {
return 2;
}
}

function matchAfterAssignA() {
let r = "a";
r = "b";
let match = r;
if (match === "a") {
return 1;
} else {
return 2;
}
}

function caught() {
let tmp;
try {
Expand All @@ -98,51 +24,13 @@ function caught() {
}

Mocha.describe("Inline_assign_cross_module_test", () => {
Mocha.test("a variable the inlined function assigns, twice in a function", () => Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 37, characters 7-14", 14, addBumped(id(3), id(4))));
Mocha.test("a variable the inlined function assigns, with a constant argument", () => Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 41, characters 7-14", [
12,
4
], bumpConstants()));
Mocha.test("a read of an assigned variable, passed to a closure before the assignment", () => {
let r = 5;
let p = r;
r = 10;
Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 45, characters 7-14", 15, p + r | 0);
});
Mocha.test("a copy of an assigned variable, made before the assignment", () => {
let r = 1;
let s = r;
r = s + 1 | 0;
Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 49, characters 7-14", 3, s + r | 0);
});
Mocha.test("a read of an assigned variable, passed to a bound closure before the assignment", () => Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 53, characters 7-14", 15, readBeforeAssignBound()));
Mocha.test("a condition on an assigned variable bound to a constant", () => {
let r = true;
r = !r;
Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 57, characters 7-14", 2, r ? 1 : 2);
});
Mocha.test("a switch on an assigned variable bound to a constant", () => {
let r = "a";
r = "b";
let match = r;
let tmp = match === "a" ? 1 : 2;
Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 61, characters 7-14", 2, tmp);
});
Mocha.test("a function with a try handler, inlined twice in a function", () => Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 65, characters 7-14", [
Mocha.test("a function with a try handler, inlined twice in a function", () => Test_utils.eq("File \"inline_assign_cross_module_test.res\", line 13, characters 7-14", [
1,
2
], caught()));
});

export {
id,
addBumped,
bumpConstants,
readBeforeAssign,
copyBeforeAssign,
readBeforeAssignBound,
negatedTrue,
matchAfterAssignA,
caught,
}
/* Not a pure module */
Loading
Loading