diff --git a/changes/inline-no-assigned-body.fix.md b/changes/inline-no-assigned-body.fix.md new file mode 100644 index 0000000000..41064da22f --- /dev/null +++ b/changes/inline-no-assigned-body.fix.md @@ -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. diff --git a/compiler/backend/lam_check.ml b/compiler/backend/lam_check.ml index ba7ce824fe..91e35c430c 100644 --- a/compiler/backend/lam_check.ml +++ b/compiler/backend/lam_check.ml @@ -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) = @@ -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 diff --git a/compiler/backend/lam_check.mli b/compiler/backend/lam_check.mli index 93bb81b74c..3b2d7c6cc4 100644 --- a/compiler/backend/lam_check.mli +++ b/compiler/backend/lam_check.mli @@ -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 diff --git a/compiler/backend/lam_compile_main.ml b/compiler/backend/lam_compile_main.ml index 9bb2998f13..3e27c933e3 100644 --- a/compiler/backend/lam_compile_main.ml +++ b/compiler/backend/lam_compile_main.ml @@ -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 @@ -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; diff --git a/compiler/backend/lam_stats_export.ml b/compiler/backend/lam_stats_export.ml index 54ab69c686..303f97a1c3 100644 --- a/compiler/backend/lam_stats_export.ml +++ b/compiler/backend/lam_stats_export.ml @@ -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 diff --git a/tests/compiler_tests/src/inline_assign_cross_module_lib.mjs b/tests/compiler_tests/src/inline_assign_cross_module_lib.mjs index bb9487176c..56d74ce71c 100644 --- a/tests/compiler_tests/src/inline_assign_cross_module_lib.mjs +++ b/tests/compiler_tests/src/inline_assign_cross_module_lib.mjs @@ -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(); @@ -54,11 +10,6 @@ function orDefault(f, $$default) { } export { - bumpTwice, - readThenAssign, - copyThenAssign, - negated, - matchAfterAssign, orDefault, } /* No side effect */ diff --git a/tests/compiler_tests/src/inline_assign_cross_module_lib.res b/tests/compiler_tests/src/inline_assign_cross_module_lib.res index 89c86bb55e..b80c4a1b88 100644 --- a/tests/compiler_tests/src/inline_assign_cross_module_lib.res +++ b/tests/compiler_tests/src/inline_assign_cross_module_lib.res @@ -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 diff --git a/tests/compiler_tests/src/inline_assign_cross_module_test.mjs b/tests/compiler_tests/src/inline_assign_cross_module_test.mjs index cee70373d7..8c644ef6ae 100644 --- a/tests/compiler_tests/src/inline_assign_cross_module_test.mjs +++ b/tests/compiler_tests/src/inline_assign_cross_module_test.mjs @@ -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 { @@ -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 */ diff --git a/tests/compiler_tests/src/inline_assign_cross_module_test.res b/tests/compiler_tests/src/inline_assign_cross_module_test.res index bad171c8db..f443f902bf 100644 --- a/tests/compiler_tests/src/inline_assign_cross_module_test.res +++ b/tests/compiler_tests/src/inline_assign_cross_module_test.res @@ -1,66 +1,14 @@ -// Inlining another module's function renames its local variables. An -// assignment to one used to keep the old name, a variable this module doesn't -// declare. +// Inlining another module's function renames its local variables, including +// the exception a handler binds. open Mocha open Test_utils -@inline(never) let id = x => x - -let addBumped = (x, y) => - Inline_assign_cross_module_lib.bumpTwice(x) + Inline_assign_cross_module_lib.bumpTwice(y) - -let bumpConstants = () => ( - Inline_assign_cross_module_lib.bumpTwice(5), - Inline_assign_cross_module_lib.bumpTwice(3), -) - -let readBeforeAssign = () => Inline_assign_cross_module_lib.readThenAssign(5, p => () => p) - -let copyBeforeAssign = () => Inline_assign_cross_module_lib.copyThenAssign(1) - -let readBeforeAssignBound = () => { - let capture = p => () => p - Inline_assign_cross_module_lib.readThenAssign(5, capture) -} - -let negatedTrue = () => Inline_assign_cross_module_lib.negated(true) - -let matchAfterAssignA = () => Inline_assign_cross_module_lib.matchAfterAssign("a") - let caught = () => ( Inline_assign_cross_module_lib.orDefault(() => 1, 0), Inline_assign_cross_module_lib.orDefault(() => JsError.throwWithMessage("boom"), 2), ) describe(__MODULE__, () => { - test("a variable the inlined function assigns, twice in a function", () => { - eq(__LOC__, 14, addBumped(id(3), id(4))) - }) - - test("a variable the inlined function assigns, with a constant argument", () => { - eq(__LOC__, (12, 4), bumpConstants()) - }) - - test("a read of an assigned variable, passed to a closure before the assignment", () => { - eq(__LOC__, 15, readBeforeAssign()) - }) - - test("a copy of an assigned variable, made before the assignment", () => { - eq(__LOC__, 3, copyBeforeAssign()) - }) - - test("a read of an assigned variable, passed to a bound closure before the assignment", () => { - eq(__LOC__, 15, readBeforeAssignBound()) - }) - - test("a condition on an assigned variable bound to a constant", () => { - eq(__LOC__, 2, negatedTrue()) - }) - - test("a switch on an assigned variable bound to a constant", () => { - eq(__LOC__, 2, matchAfterAssignA()) - }) - test("a function with a try handler, inlined twice in a function", () => { eq(__LOC__, (1, 2), caught()) }) diff --git a/tests/compiler_tests/src/inline_assigned_payload_lib.mjs b/tests/compiler_tests/src/inline_assigned_payload_lib.mjs new file mode 100644 index 0000000000..e4ee6d4dc1 --- /dev/null +++ b/tests/compiler_tests/src/inline_assigned_payload_lib.mjs @@ -0,0 +1,50 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + + +let A = { + x: "a" +}; + +let B = { + x: "b" +}; + +function nullableAfter(p, o, f) { + f(); + if (o !== undefined) { + return [ + o, + p + ]; + } else { + return [ + "none", + p + ]; + } +} + +function fieldAfter(p, t, f) { + f(); + return [ + t[0], + p + ]; +} + +function valueAfter(p, a, f) { + f(); + return [ + a, + p + ]; +} + +export { + A, + B, + nullableAfter, + fieldAfter, + valueAfter, +} +/* No side effect */ diff --git a/tests/compiler_tests/src/inline_assigned_payload_lib.res b/tests/compiler_tests/src/inline_assigned_payload_lib.res new file mode 100644 index 0000000000..1a4e4bd6a1 --- /dev/null +++ b/tests/compiler_tests/src/inline_assigned_payload_lib.res @@ -0,0 +1,36 @@ +// Inlined by inline_assigned_payload_test.res. [f] assigns the caller's ref +// that an argument was made from: the argument must keep the value the ref +// had before the call. +module type S = { + let x: string +} + +module A = { + let x = "a" +} + +module B = { + let x = "b" +} + +@inline +let nullableAfter = (p: int, o: option, f) => { + f() + switch o { + | Some(v) => (v, p) + | None => ("none", p) + } +} + +@inline +let fieldAfter = (p: int, t: (string, int), f) => { + f() + let (a, _) = t + (a, p) +} + +@inline +let valueAfter = (p: int, a: string, f) => { + f() + (a, p) +} diff --git a/tests/compiler_tests/src/inline_assigned_payload_test.mjs b/tests/compiler_tests/src/inline_assigned_payload_test.mjs new file mode 100644 index 0000000000..a9f7ea1f07 --- /dev/null +++ b/tests/compiler_tests/src/inline_assigned_payload_test.mjs @@ -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"; +import * as Inline_assigned_payload_lib from "./inline_assigned_payload_lib.mjs"; + +function nullableBeforeCall(p, q, q2) { + let s = q; + let o = s; + s = q2; + let res = o == null ? [ + "none", + p + ] : [ + o == null ? undefined : Primitive_option.some(o), + p + ]; + return [ + res, + s + ]; +} + +function moduleFieldBeforeCall(p, q, q2) { + let s = q; + let t_0 = s.x; + s = q2; + let res_0 = t_0; + let res = [ + res_0, + p + ]; + let M2 = s; + return [ + res, + M2.x + ]; +} + +function moduleValueBeforeCall(p, q, q2) { + let s = q; + let a = s.x; + s = q2; + let res = [ + a, + p + ]; + let M2 = s; + return [ + res, + M2.x + ]; +} + +Mocha.describe("Inline_assigned_payload_test", () => { + Mocha.test("a nullable converted to an option before a call assigns its variable", () => Test_utils.eq("File \"inline_assigned_payload_test.res\", line 52, characters 7-14", [ + [ + "a", + 1 + ], + null + ], nullableBeforeCall(1, "a", null))); + Mocha.test("a module field in a tuple, read before a call assigns its variable", () => Test_utils.eq("File \"inline_assigned_payload_test.res\", line 56, characters 7-14", [ + [ + "a", + 1 + ], + "b" + ], moduleFieldBeforeCall(1, Inline_assigned_payload_lib.A, Inline_assigned_payload_lib.B))); + Mocha.test("a module field, read before a call assigns its variable", () => Test_utils.eq("File \"inline_assigned_payload_test.res\", line 60, characters 7-14", [ + [ + "a", + 1 + ], + "b" + ], moduleValueBeforeCall(1, Inline_assigned_payload_lib.A, Inline_assigned_payload_lib.B))); +}); + +let Lib; + +export { + Lib, + nullableBeforeCall, + moduleFieldBeforeCall, + moduleValueBeforeCall, +} +/* Not a pure module */ diff --git a/tests/compiler_tests/src/inline_assigned_payload_test.res b/tests/compiler_tests/src/inline_assigned_payload_test.res new file mode 100644 index 0000000000..af9547bf09 --- /dev/null +++ b/tests/compiler_tests/src/inline_assigned_payload_test.res @@ -0,0 +1,62 @@ +// An option, a tuple or a module field made from the caller's ref must keep +// the value the ref had before an inlined call's callback assigns it. +open Mocha +open Test_utils + +module Lib = Inline_assigned_payload_lib + +// The caller's own ref, assigned by a callback. +@inline(never) +let nullableBeforeCall = (p, q: Nullable.t, q2) => { + let s = ref(q) + let res = Lib.nullableAfter(p, Nullable.toOption(s.contents), () => s := q2) + (res, s.contents) +} + +// The tuple holds a field of the module [s] holds before the call. +@inline(never) +let moduleFieldBeforeCall = (p, q: module(Lib.S), q2) => { + let s = ref(q) + let res = Lib.fieldAfter( + p, + ( + { + let module(M) = s.contents + M.x + }, + 1, + ), + () => s := q2, + ) + let module(M2) = s.contents + (res, M2.x) +} + +@inline(never) +let moduleValueBeforeCall = (p, q: module(Lib.S), q2) => { + let s = ref(q) + let res = Lib.valueAfter( + p, + { + let module(M) = s.contents + M.x + }, + () => s := q2, + ) + let module(M2) = s.contents + (res, M2.x) +} + +describe(__MODULE__, () => { + test("a nullable converted to an option before a call assigns its variable", () => { + eq(__LOC__, (("a", 1), Nullable.null), nullableBeforeCall(1, Nullable.make("a"), Nullable.null)) + }) + + test("a module field in a tuple, read before a call assigns its variable", () => { + eq(__LOC__, (("a", 1), "b"), moduleFieldBeforeCall(1, module(Lib.A), module(Lib.B))) + }) + + test("a module field, read before a call assigns its variable", () => { + eq(__LOC__, (("a", 1), "b"), moduleValueBeforeCall(1, module(Lib.A), module(Lib.B))) + }) +}) diff --git a/tests/error_tests/single/expected/warning_113_inline_not_stored.res.expected b/tests/error_tests/single/expected/warning_113_inline_not_stored.res.expected index 5d6b259c3c..5a09693b40 100644 --- a/tests/error_tests/single/expected/warning_113_inline_not_stored.res.expected +++ b/tests/error_tests/single/expected/warning_113_inline_not_stored.res.expected @@ -42,18 +42,34 @@ 6 │ @module("./h.js") external raw: int => int = "raw" 7 │ @inline let relative = x => raw(x) 8 │ - 9 │ // Silenced where it is bound: no warning. + 9 │ @inline This @inline function is inlined only in this module, not in modules that use it: it calls an external with a path relative to this file. Warning number 113 - /.../fixtures/warning_113_inline_not_stored.res:26:24-41 + /.../fixtures/warning_113_inline_not_stored.res:10:13-16:1 - 24 │ } - 25 │ - 26 │ @inline let shadowed = x => helper(x) - 1 - 27 │ - 28 │ // A functor silenced where it is bound: no warning. + 8 │ + 9 │ @inline + 10 │ let sumTo = n => { + 11 │  let total = ref(0) + . │ ... + 15 │  total.contents + 16 │ } + 17 │ + 18 │ // Silenced where it is bound: no warning. + + This @inline function is inlined only in this module, not in modules that use it: it updates a local ref or mutable record. + + + Warning number 113 + /.../fixtures/warning_113_inline_not_stored.res:35:24-41 + + 33 │ } + 34 │ + 35 │ @inline let shadowed = x => helper(x) - 1 + 36 │ + 37 │ // A functor silenced where it is bound: no warning. This @inline function is inlined only in this module, not in modules that use it: it uses helper, a value of this module. \ No newline at end of file diff --git a/tests/error_tests/single/fixtures/warning_113_inline_not_stored.res b/tests/error_tests/single/fixtures/warning_113_inline_not_stored.res index 53e86371e5..9f7f8f3a1f 100644 --- a/tests/error_tests/single/fixtures/warning_113_inline_not_stored.res +++ b/tests/error_tests/single/fixtures/warning_113_inline_not_stored.res @@ -6,6 +6,15 @@ let rec helper = x => x > 10 ? x : helper(x + 1) @module("./h.js") external raw: int => int = "raw" @inline let relative = x => raw(x) +@inline +let sumTo = n => { + let total = ref(0) + for i in 0 to n { + total := total.contents + i + } + total.contents +} + // Silenced where it is bound: no warning. @warning("-113") @inline let silenced = x => helper(x)