From ec4cc890c68c1fdfb745721d6b91e430904bf42d Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Sun, 22 Nov 2026 20:52:46 +0000 Subject: [PATCH 1/2] Rename a for loop's start expression once when inlining Lam_bounded_vars renames the variables bound inside a function body before it is inlined. Its for-loop case rewrote the start expression twice (`let l1 = aux l1 in ... Lambda.for_ ident (aux l1) ...`), a slip that has been there since at least 2019. The second pass only renamed the fresh binders again, consistently, so the result was the same program; JS output and diagnostics are byte-identical on 77 benchmark files, and allocation drops by 56 words in total. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QchYQ7itFLVLUmZ368okjH Signed-off-by: Christoph Knittel --- changes/lam-bounded-vars-for.internal.md | 1 + compiler/backend/lam_bounded_vars.ml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 changes/lam-bounded-vars-for.internal.md diff --git a/changes/lam-bounded-vars-for.internal.md b/changes/lam-bounded-vars-for.internal.md new file mode 100644 index 0000000000..c964165010 --- /dev/null +++ b/changes/lam-bounded-vars-for.internal.md @@ -0,0 +1 @@ +- Rename the variables in a for loop's start expression once instead of twice when inlining a function. diff --git a/compiler/backend/lam_bounded_vars.ml b/compiler/backend/lam_bounded_vars.ml index 370ece7c45..a0e5e08c9a 100644 --- a/compiler/backend/lam_bounded_vars.ml +++ b/compiler/backend/lam_bounded_vars.ml @@ -103,7 +103,7 @@ let rewrite (map : _ Hash_ident.t) (lam : Lambda.t) : Lambda.t = let l1 = aux l1 in let l2 = aux l2 in let l3 = aux l3 in - Lambda.for_ ident (aux l1) l2 dir l3 + Lambda.for_ ident l1 l2 dir l3 | Lfor_of (ident, l1, l2) -> let ident = rebind ident in let l1 = aux l1 in From 4c7a4c34e9bad3eaa86694d6f6a70e54e08f387c Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Mon, 23 Nov 2026 08:37:43 +0000 Subject: [PATCH 2/2] Test the renaming of a for loop's start expression when inlining The previous commit had no lasting test, as Codex pointed out. A unit test now rewrites a for loop whose start expression binds a variable and checks that the binder, its use and the renaming map agree, with one map entry per binder; with the old double rename the map records a name the result doesn't use. An end-to-end test applies two function literals right away, which inlines them through Lam_bounded_vars, with a let and a callback in the loop's start expression, and checks their results. Functions with loops are otherwise too big to inline, so this is the path that reaches the for-loop case. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01QchYQ7itFLVLUmZ368okjH Signed-off-by: Christoph Knittel --- .../ounit_lam_bounded_vars_tests.ml | 37 +++++++++++++++++ tests/ounit_tests/ounit_tests_main.ml | 1 + tests/tests/src/inline_for_start_test.mjs | 36 ++++++++++++++++ tests/tests/src/inline_for_start_test.res | 41 +++++++++++++++++++ 4 files changed, 115 insertions(+) create mode 100644 tests/ounit_tests/ounit_lam_bounded_vars_tests.ml create mode 100644 tests/tests/src/inline_for_start_test.mjs create mode 100644 tests/tests/src/inline_for_start_test.res diff --git a/tests/ounit_tests/ounit_lam_bounded_vars_tests.ml b/tests/ounit_tests/ounit_lam_bounded_vars_tests.ml new file mode 100644 index 0000000000..2a78895964 --- /dev/null +++ b/tests/ounit_tests/ounit_lam_bounded_vars_tests.ml @@ -0,0 +1,37 @@ +let ( >:: ), ( >::: ) = OUnit.(( >:: ), ( >::: )) + +let int i = Lambda.const (Lambda.const_int i) + +(* [for i in {let s = 1; s} to 10 { i }]: a binder in the start expression *) +let i = Ident.create "i" + +let s = Ident.create "s" + +let loop = + Lambda.for_ i + (Lambda.let_ Strict s (int 1) (Lambda.var s)) + (int 10) Upto (Lambda.var i) + +(* [rewrite] gives each bound variable one fresh name and records it in the + map: the binder in the result, its uses and the map agree, and the map has + one entry per binder. Renaming the start expression twice gave its binder + a second name the map didn't record. *) +let test_for_start_is_renamed_once _ = + let map = Hash_ident.create 4 in + match Lam_bounded_vars.rewrite map loop with + | Lfor (i', Llet (_, s', _, Lvar s_use), _, _, Lvar i_use) -> + OUnit.assert_bool "the loop variable is renamed" (not (Ident.same i' i)); + OUnit.assert_bool "its use follows" (Ident.same i_use i'); + OUnit.assert_bool "the start binder is renamed" (not (Ident.same s' s)); + OUnit.assert_bool "its use follows" (Ident.same s_use s'); + (match Hash_ident.find_opt map s with + | Some (Lvar recorded) -> + OUnit.assert_bool "the map records the binder's name" + (Ident.same recorded s') + | _ -> OUnit.assert_failure "the start binder is in the map"); + OUnit.assert_equal ~printer:string_of_int ~msg:"one entry per binder" 2 + (Hash_ident.length map) + | _ -> OUnit.assert_failure "a for loop with a let in its start" + +let suites = + __FILE__ >::: ["for_start_is_renamed_once" >:: test_for_start_is_renamed_once] diff --git a/tests/ounit_tests/ounit_tests_main.ml b/tests/ounit_tests/ounit_tests_main.ml index a8a280b0ae..5b9c3dba27 100644 --- a/tests/ounit_tests/ounit_tests_main.ml +++ b/tests/ounit_tests/ounit_tests_main.ml @@ -19,6 +19,7 @@ let suites = Ounit_deep_flatten_tests.suites; Ounit_exits_tests.suites; Ounit_lam_size_tests.suites; + Ounit_lam_bounded_vars_tests.suites; Ounit_sroa_tests.suites; Ounit_ast_mapper0_tests.suites; Ounit_ast_helper_tests.suites; diff --git a/tests/tests/src/inline_for_start_test.mjs b/tests/tests/src/inline_for_start_test.mjs new file mode 100644 index 0000000000..035cf94599 --- /dev/null +++ b/tests/tests/src/inline_for_start_test.mjs @@ -0,0 +1,36 @@ +// Generated by ReScript, PLEASE EDIT WITH CARE + +import * as Mocha from "mocha"; +import * as Test_utils from "./test_utils.mjs"; +import * as Stdlib_Array from "@rescript/runtime/lib/es6/Stdlib_Array.mjs"; + +function fromBlock(x) { + let acc = 0; + let s = (x << 1); + for (let i = s - x | 0, i_finish = x + 3 | 0; i <= i_finish; ++i) { + acc = acc + i | 0; + } + return acc; +} + +function fromCallback(xs, x) { + let acc = 0; + for (let i = Stdlib_Array.reduce(xs, 0, (a, b) => a + b | 0); i <= x; ++i) { + acc = acc + i | 0; + } + return acc; +} + +Mocha.describe("Inline_for_start_test", () => { + Mocha.test("a let in the start expression", () => Test_utils.eq("File \"inline_for_start_test.res\", line 35, characters 7-14", fromBlock(2), 14)); + Mocha.test("a callback in the start expression", () => Test_utils.eq("File \"inline_for_start_test.res\", line 39, characters 7-14", fromCallback([ + 1, + 2 + ], 5), 12)); +}); + +export { + fromBlock, + fromCallback, +} +/* Not a pure module */ diff --git a/tests/tests/src/inline_for_start_test.res b/tests/tests/src/inline_for_start_test.res new file mode 100644 index 0000000000..9de2fe1b9d --- /dev/null +++ b/tests/tests/src/inline_for_start_test.res @@ -0,0 +1,41 @@ +open Mocha +open Test_utils + +// Applying a function literal right away inlines it, which renames the +// variables bound in its body, including those in a for loop's start +// expression: a let in a block, and a callback's parameters. +let fromBlock = x => + ( + n => { + let acc = ref(0) + for i in { + let s = n * 2 + s - n + } to n + 3 { + acc := acc.contents + i + } + acc.contents + } + )(x) + +let fromCallback = (xs, x) => + ( + n => { + let acc = ref(0) + for i in xs->Array.reduce(0, (a, b) => a + b) to n { + acc := acc.contents + i + } + acc.contents + } + )(x) + +describe(__MODULE__, () => { + test("a let in the start expression", () => { + // 2 + 3 + 4 + 5 + eq(__LOC__, fromBlock(2), 14) + }) + test("a callback in the start expression", () => { + // 3 + 4 + 5 + eq(__LOC__, fromCallback([1, 2], 5), 12) + }) +})