Skip to content

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

Open
cknitt wants to merge 1 commit into
masterfrom
inline-parameter-args
Open

cknitt wants to merge 1 commit into
masterfrom
inline-parameter-args

Conversation

@cknitt

@cknitt cknitt commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

The alias pass did not inline a call to another module's @inline function when an argument was a parameter of the caller. This is the most common shape: let g = opt => opt->Option.map(f).

The check dates from the initial export in 2016. Back then only functors stored their bodies, and 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. The runtime and Belt JavaScript doesn't change. Three test outputs get better.

One gets a temporary it doesn't need: addBumped in inline_assign_cross_module_test inlines bumpTwice, which updates a local ref, so its stored body assigns r. Inlined early, the first copy's result is bound to keep the order of evaluation, and the Lambda passes only know that r is assigned somewhere, so let tmp = r stays. The output is correct, and the next PR (#9535) removes the case: bodies that update a local ref are no longer stored, so bumpTwice is called.

This is the bottom of a stack that moves all cross-module inlining into the alias pass and removes what that makes unnecessary.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq

@github-actions

github-actions Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Changelog

💅 Polish

  • 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.

Semantic Diff

1 commit · Full page

✏️ Inline other modules' functions early when an argument is a parameter

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

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)

let mapped = (opt: option<int>) => opt->Option.map(x => x + 1)

js

 function mapped(opt) {
   if (opt !== undefined) {
-    return Primitive_option.some((x => x + 1 | 0)(opt));
+    return Primitive_option.some(opt + 1 | 0);
   }
 }

JS modified (a literal Some next to a parameter)

let knownSome = (x: int, f) => Some(x)->Option.map(f)->Option.getOr(0)

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;
-  }
+  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.

@cknitt

cknitt commented Dec 5, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Dec 5, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-12-05T10:22:23.967331Z b196ac5 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Nice work!

Reviewed commit: b196ac5a97

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@github-actions

github-actions Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 656,455,989 → 656,460,407 (±0.00%).

Generated projects: 628,519,897 → 628,520,270 (±0.00%).

Benchmark Base Head Change
packages/@rescript/runtime/Primitive_object.res 1,112,113 1,106,220 -0.53%

Instructions

In total: 8,976,406,377 → 8,976,444,923 (±0.00%).

None of the 33 benchmarks changed by 0.1% or more.

Output size

In total: 27,594,926 → 27,594,546 (±0.00%).

None of the 13 benchmarks changed by 0.1% or more.

Pull request 6c39508 compared with base daa4f2d in this run.

@codecov

codecov Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.68%. Comparing base (2796a44) to head (6c39508).
⚠️ Report is 3 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #9536   +/-   ##
=======================================
  Coverage   82.67%   82.68%           
=======================================
  Files         469      469           
  Lines       66553    66575   +22     
=======================================
+ Hits        55023    55047   +24     
+ Misses      11530    11528    -2     
Files with missing lines Coverage Δ
compiler/backend/lam_pass_remove_alias.ml 92.85% <ø> (-0.38%) ⬇️

... and 9 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@pkg-pr-new

pkg-pr-new Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

npm i https://pkg.pr.new/rescript@9536

@rescript/belt

npm i https://pkg.pr.new/@rescript/belt@9536

@rescript/darwin-arm64

npm i https://pkg.pr.new/@rescript/darwin-arm64@9536

@rescript/linux-arm64

npm i https://pkg.pr.new/@rescript/linux-arm64@9536

@rescript/linux-x64

npm i https://pkg.pr.new/@rescript/linux-x64@9536

@rescript/runtime

npm i https://pkg.pr.new/@rescript/runtime@9536

@rescript/win32-x64

npm i https://pkg.pr.new/@rescript/win32-x64@9536

commit: 6c39508

@github-actions

github-actions Bot commented Dec 5, 2026

Copy link
Copy Markdown

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<int>) => 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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq
Signed-off-by: Christoph Knittel <ck@cca.io>
@cknitt
cknitt force-pushed the inline-parameter-args branch from b196ac5 to 6c39508 Compare December 5, 2026 11:50
@cknitt
cknitt marked this pull request as ready for review December 5, 2026 13:11
@cknitt
cknitt requested a review from cristianoc December 5, 2026 13:11

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant