Semantic Diff

#9536 Inline other modules' functions early when an argument is a parameter

1 commit

2796a446117a261c37df1766640dabc473626473..pr-9536

6c3950833 · cknitt + Claude · Dec 05, 2026, 09:56 UTC

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.

modifiedJS a function argument next to a parameter

let mapped = (opt: option<int>) => opt->Option.map(x => x + 1)
js
Before
After
function mapped(opt) {function mapped(opt) { if (opt !== undefined) { if (opt !== undefined) { return Primitive_option.some((x => x + 1 | 0)(opt)); return Primitive_option.some(opt + 1 | 0); } }}}

modifiedJS a literal Some next to a parameter

let knownSome = (x: int, f) => Some(x)->Option.map(f)->Option.getOr(0)
js
Before
After
function knownSome(x, f) {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; } 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).
  • 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.
  • 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.