From 6c39508333bed3fb57159df64ac47c669c4410b7 Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Sat, 5 Dec 2026 09:56:21 +0000 Subject: [PATCH] Inline other modules' functions early when an argument is a parameter Why: The alias pass inlined a call to another module's `@inline` function only when no argument was a parameter of the caller. The check dates from the initial export in 2016, when only functors stored their bodies: it kept a functor that applies another functor to its unknown argument from being inlined there, to limit code size. Since late inlining was added, such calls were inlined anyway, only later, while compiling to JavaScript. There the body missed the alias pass, so a function argument was called instead of inlined and a `Some(x)` argument was bound and unwrapped again. The pass now inlines these calls like all others. JS modified (a function argument next to a parameter): Example: let mapped = (opt: option) => opt->Option.map(x => x + 1) Before (js): function mapped(opt) { if (opt !== undefined) { return Primitive_option.some((x => x + 1 | 0)(opt)); } } After (js): function mapped(opt) { if (opt !== undefined) { return Primitive_option.some(opt + 1 | 0); } } JS modified (a literal Some next to a parameter): Example: let knownSome = (x: int, f) => Some(x)->Option.map(f)->Option.getOr(0) Before (js): function knownSome(x, f) { let opt = x; let opt$1 = opt !== undefined ? Primitive_option.some(f(Primitive_option.valFromOption(opt))) : undefined; if (opt$1 !== undefined) { return opt$1; } else { return 0; } } After (js): function knownSome(x, f) { return Primitive_option.some(f(x)); } Review: What else changes? The generated JavaScript of the runtime and Belt stays the same. In the tests, `pick(x, y, true)` now uses its known flag (`inline_static_exit_cross_module_test`). Review: Why does `addBumped` in `inline_assign_cross_module_test` get a `let tmp = r`? Its `bumpTwice` updates a local ref, so its stored body assigns `r`. Inlined late, the JavaScript generation saw that the second copy assigns only `r$1`. Inlined early, the first copy's result is bound to keep the order of evaluation, and the binding is substituted back only if `r` can't change: the Lambda passes only know that `r` is assigned somewhere, so `tmp` stays. The output is correct. The next PR in the stack stops storing bodies that update a local ref, so `bumpTwice` is called and this case goes away. Review: Does `Primitive_option.some(f(x))` return the right value? Yes: the payload is an `int`, so it is returned as it is. Not wrapping it at all needs the type of the call's result. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq Signed-off-by: Christoph Knittel --- changes/inline-parameter-args.polish.md | 1 + compiler/backend/lam_pass_remove_alias.ml | 14 +---- .../inline_arg_order_cross_module_test.res | 8 +-- .../src/inline_assign_cross_module_test.mjs | 3 +- .../compiler_tests/src/inline_export_test.mjs | 10 +-- .../src/inline_option_unwrap_test.mjs | 61 ++++++++++++------- .../src/inline_option_unwrap_test.res | 10 +++ .../inline_static_exit_cross_module_test.mjs | 16 ++--- .../inline_static_exit_cross_module_test.res | 2 +- 9 files changed, 67 insertions(+), 58 deletions(-) create mode 100644 changes/inline-parameter-args.polish.md diff --git a/changes/inline-parameter-args.polish.md b/changes/inline-parameter-args.polish.md new file mode 100644 index 0000000000..df4a11e5a6 --- /dev/null +++ b/changes/inline-parameter-args.polish.md @@ -0,0 +1 @@ +- Inline `@inline` functions of other modules in the same pass when a call passes a parameter of the caller, so that a known argument such as `Some(x)` is read directly and a function argument is inlined. diff --git a/compiler/backend/lam_pass_remove_alias.ml b/compiler/backend/lam_pass_remove_alias.ml index e4b41b0ae8..e4f6c97799 100644 --- a/compiler/backend/lam_pass_remove_alias.ml +++ b/compiler/backend/lam_pass_remove_alias.ml @@ -162,19 +162,9 @@ let simplify_alias (meta : Lam_stats.t) (lam : Lambda.t) : Lambda.t = | { persistent_closed_lambda = Some (Lfunction ({params; body} as lfunction)); } - (* be more cautious when do cross module inlining *) + (* A body stored in the other module's .cmj: only [@inline] functions + and functors store one, and every full call of it is inlined *) when List.compare_lengths params args = 0 - && List.for_all - (fun (arg : Lambda.t) -> - match arg with - | Lvar p - | Lprim {primitive = Pnot_nested_option; args = [Lvar p]} - -> ( - match Hash_ident.find_opt meta.ident_tbl p with - | Some v -> v <> Parameter - | None -> true) - | _ -> true) - args && Lambda.lfunction_can_be_inlined lfunction -> (* Like the inlining in [Lam_compile]: an argument without side effects that the body reads once takes the parameter's place, diff --git a/tests/compiler_tests/src/inline_arg_order_cross_module_test.res b/tests/compiler_tests/src/inline_arg_order_cross_module_test.res index b3fbe034b9..383d615e50 100644 --- a/tests/compiler_tests/src/inline_arg_order_cross_module_test.res +++ b/tests/compiler_tests/src/inline_arg_order_cross_module_test.res @@ -1,7 +1,7 @@ -// A call to another module's function that takes a parameter of the caller is -// inlined while compiling to JavaScript, after a local ref has become a -// variable. An argument that reads the variable used to be substituted into -// the body, past the argument that assigns it. +// A call to another module's function that takes a parameter of the caller, +// with arguments that read and assign a local ref. An argument that reads the +// ref used to be substituted into the body, past the argument that assigns +// it. open Mocha open Test_utils 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 3032853204..cee70373d7 100644 --- a/tests/compiler_tests/src/inline_assign_cross_module_test.mjs +++ b/tests/compiler_tests/src/inline_assign_cross_module_test.mjs @@ -14,12 +14,13 @@ function addBumped(x, y) { 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 r + r$1 | 0; + return tmp + r$1 | 0; } function bumpConstants() { diff --git a/tests/compiler_tests/src/inline_export_test.mjs b/tests/compiler_tests/src/inline_export_test.mjs index 5b76b2b6a7..497d253df5 100644 --- a/tests/compiler_tests/src/inline_export_test.mjs +++ b/tests/compiler_tests/src/inline_export_test.mjs @@ -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); } } @@ -92,10 +92,10 @@ Mocha.describe("@inline across modules", () => { Test_utils.eq("File \"inline_export_test.res\", line 30, characters 7-14", mapped, 3); Test_utils.eq("File \"inline_export_test.res\", line 31, characters 7-14", flatMapped, 4); Test_utils.eq("File \"inline_export_test.res\", line 32, characters 7-14", mappedNested, Primitive_option.some(undefined)); - Test_utils.eq("File \"inline_export_test.res\", line 33, characters 7-14", mapValue(2), 3); - Test_utils.eq("File \"inline_export_test.res\", line 34, characters 7-14", mapValue(undefined), undefined); - Test_utils.eq("File \"inline_export_test.res\", line 35, characters 7-14", flatMapValue(2), 4); - Test_utils.eq("File \"inline_export_test.res\", line 36, characters 7-14", flatMapValue(undefined), undefined); + Test_utils.eq("File \"inline_export_test.res\", line 33, characters 7-14", Primitive_option.some(2 + 1 | 0), 3); + Test_utils.eq("File \"inline_export_test.res\", line 34, characters 7-14", undefined, undefined); + Test_utils.eq("File \"inline_export_test.res\", line 35, characters 7-14", 2 + 2 | 0, 4); + Test_utils.eq("File \"inline_export_test.res\", line 36, characters 7-14", undefined, undefined); visited.push(5); Test_utils.eq("File \"inline_export_test.res\", line 38, characters 7-14", visited, [ 4, diff --git a/tests/compiler_tests/src/inline_option_unwrap_test.mjs b/tests/compiler_tests/src/inline_option_unwrap_test.mjs index 6e8d6848cd..7a8fd3d5b9 100644 --- a/tests/compiler_tests/src/inline_option_unwrap_test.mjs +++ b/tests/compiler_tests/src/inline_option_unwrap_test.mjs @@ -30,7 +30,7 @@ function stdlibIntOr(opt) { function mapped(opt) { if (opt !== undefined) { - return Primitive_option.some((x => x + 1 | 0)(opt)); + return Primitive_option.some(opt + 1 | 0); } } @@ -138,43 +138,56 @@ function castOnce(opt) { ]; } +function knownSome(x, f) { + return Primitive_option.some(f(x)); +} + +function knownSomeNested(x, f) { + return Primitive_option.some(f(x)); +} + Mocha.describe("Inline_option_unwrap_test", () => { + Mocha.test("literal Some with a parameter argument", () => { + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 64, characters 7-14", knownSome(2, x => x + 1 | 0), 3); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 65, characters 7-14", Primitive_option.some(true), true); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 66, characters 7-14", Primitive_option.some(false), false); + }); Mocha.test("payload that cannot be undefined", () => { - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 59, characters 7-14", 2, 2); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 60, characters 7-14", 0, 0); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 61, characters 7-14", 2, 2); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 62, characters 7-14", 0, 0); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 63, characters 7-14", mapped(2), 3); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 64, characters 7-14", mapped(undefined), undefined); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 65, characters 7-14", 2, 2); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 66, characters 7-14", 2, 2); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 67, characters 7-14", 0, 0); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 68, characters 7-14", 2, 2); Test_utils.eq("File \"inline_option_unwrap_test.res\", line 69, characters 7-14", 2, 2); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 70, characters 7-14", 1, 1); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 71, characters 7-14", "ab".length, 2); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 70, characters 7-14", 0, 0); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 71, characters 7-14", 2, 2); Test_utils.eq("File \"inline_option_unwrap_test.res\", line 72, characters 7-14", 0, 0); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 73, characters 7-14", nullableInput("x"), [ + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 73, characters 7-14", Primitive_option.some(2 + 1 | 0), 3); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 74, characters 7-14", undefined, undefined); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 75, characters 7-14", 2, 2); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 76, characters 7-14", 2, 2); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 77, characters 7-14", 0, 0); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 78, characters 7-14", 2, 2); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 79, characters 7-14", 2, 2); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 80, characters 7-14", 1, 1); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 81, characters 7-14", "ab".length, 2); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 82, characters 7-14", 0, 0); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 83, characters 7-14", nullableInput("x"), [ "xa", "xb" ]); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 74, characters 7-14", nullableInput(null), [ + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 84, characters 7-14", nullableInput(null), [ undefined, undefined ]); }); Mocha.test("payload that can be undefined", () => { - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 77, characters 7-14", undefined, undefined); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 78, characters 7-14", undefined, undefined); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 79, characters 7-14", undefined, undefined); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 80, characters 7-14", 1, 1); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 81, characters 7-14", 0, 0); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 82, characters 7-14", undefined, undefined); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 83, characters 7-14", undefined, undefined); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 87, characters 7-14", undefined, undefined); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 88, characters 7-14", undefined, undefined); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 89, characters 7-14", undefined, undefined); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 90, characters 7-14", 1, 1); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 91, characters 7-14", 0, 0); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 92, characters 7-14", undefined, undefined); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 93, characters 7-14", undefined, undefined); }); Mocha.test("cast at one call", () => { let match = castOnce(Primitive_option.some(undefined)); - Test_utils.eq("File \"inline_option_unwrap_test.res\", line 87, characters 7-14", match[1], undefined); + Test_utils.eq("File \"inline_option_unwrap_test.res\", line 97, characters 7-14", match[1], undefined); }); }); @@ -196,5 +209,7 @@ export { nullableOr, genericOr, castOnce, + knownSome, + knownSomeNested, } /* Not a pure module */ diff --git a/tests/compiler_tests/src/inline_option_unwrap_test.res b/tests/compiler_tests/src/inline_option_unwrap_test.res index 3251ba6821..06431078ca 100644 --- a/tests/compiler_tests/src/inline_option_unwrap_test.res +++ b/tests/compiler_tests/src/inline_option_unwrap_test.res @@ -54,7 +54,17 @@ let castOnce = (opt: option>) => { (unwrapped, inner) } +// The other arguments are parameters of the caller: a literal Some argument +// is still read directly. +let knownSome = (x: int, f) => Some(x)->Option.map(f)->Option.getOr(0) +let knownSomeNested = (x: option, f) => Some(x)->Option.map(f) + describe(__MODULE__, () => { + test("literal Some with a parameter argument", () => { + eq(__LOC__, knownSome(2, x => x + 1), 3) + eq(__LOC__, knownSomeNested(None, o => o == None), Some(true)) + eq(__LOC__, knownSomeNested(Some(1), o => o == None), Some(false)) + }) test("payload that cannot be undefined", () => { eq(__LOC__, intOr(Some(2)), 2) eq(__LOC__, intOr(None), 0) diff --git a/tests/compiler_tests/src/inline_static_exit_cross_module_test.mjs b/tests/compiler_tests/src/inline_static_exit_cross_module_test.mjs index dd5f85a84d..accadbc941 100644 --- a/tests/compiler_tests/src/inline_static_exit_cross_module_test.mjs +++ b/tests/compiler_tests/src/inline_static_exit_cross_module_test.mjs @@ -58,19 +58,11 @@ function both(x, y, flag) { exit = 1; } if (exit === 1) { - tmp = "t"; + tmp = flag ? "t" : "a" + x.toString() + (x + y | 0).toString(); } - let tmp$1; - let exit$1 = 0; - if (x !== 0 && y !== 0) { - tmp$1 = "other"; - } else { - exit$1 = 1; - } - if (exit$1 === 1) { - tmp$1 = flag ? "t" : "a" + x.toString() + (x + y | 0).toString(); - } - return tmp + " " + tmp$1; + return ( + x !== 0 && y !== 0 ? "other" : "t" + ) + " " + tmp; } let r = "t"; diff --git a/tests/compiler_tests/src/inline_static_exit_cross_module_test.res b/tests/compiler_tests/src/inline_static_exit_cross_module_test.res index f91355d5fd..411d334161 100644 --- a/tests/compiler_tests/src/inline_static_exit_cross_module_test.res +++ b/tests/compiler_tests/src/inline_static_exit_cross_module_test.res @@ -9,7 +9,7 @@ let constant = Inline_static_exit_cross_module_lib.pick(0, 5, true) let computed = Inline_static_exit_cross_module_lib.pick(id(0), id(5), id(false)) let computedOther = Inline_static_exit_cross_module_lib.pick(id(1), id(5), id(true)) -// Parameters as arguments: the call is inlined while compiling to JavaScript. +// Parameters as arguments: both copies are in one function. let both = (x, y, flag) => Inline_static_exit_cross_module_lib.pick(x, y, true) ++ " " ++