Skip to content

Inline other modules' functions called through a variable - #9539

Open
cknitt wants to merge 1 commit into
inline-assigned-cleanupfrom
inline-through-aliases
Open

cknitt wants to merge 1 commit into
inline-assigned-cleanupfrom
inline-through-aliases

Conversation

@cknitt

@cknitt cknitt commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

Since late inlining went (PR below), a call through a variable bound to another module's function was compiled as a call, even when that function is @inline. The alias pass now remembers variables bound to a field of another module and calls that field directly, so the existing case for other modules' functions inlines it.

let {f} = module(M) in a function:

let orEmpty = (x: option<string>) => {
  let {getOr} = module(Option)
  x->getOr("")
}

Before:

function orEmpty(x) {
  return Stdlib_Option.getOr(x, "");
}

After:

function orEmpty(x) {
  if (x !== undefined) {
    return x;
  } else {
    return "";
  }
}

A top-level let f = M.f:

let getOr = Option.getOr
let orZero = (x: option<int>) => x->getOr(0)

Before:

function orZero(x) {
  return Stdlib_Option.getOr(x, 0);
}

After:

function orZero(x) {
  if (x !== undefined) {
    return x;
  } else {
    return 0;
  }
}

New test inline_through_alias_test covers these shapes, a local let f = M.f, and the order of arguments. hello_res.mjs is back to its output on master.

🤖 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 when they are called through a variable, as in let f = M.f or let {f} = module(M).

Semantic Diff

1 commit · Full page

✏️ Inline other modules' functions called through a variable

9d227df · cknitt + Claude · Dec 05, 2026, 10:14 UTC · edited

Why

Since the late inlining went, a call through a variable bound to another module's function, as in let f = M.f or let {f} = module(M), was compiled as a call even when M.f is @inline. The alias pass now remembers which variables are bound to a field of another module and calls that field directly, so the existing case for other modules' functions inlines it. This works for top-level and local bindings alike.

JS modified (let {f} = module(M) in a function)

let orEmpty = (x: option<string>) => {
  let {getOr} = module(Option)
  x->getOr("")
}

js

 function orEmpty(x) {
-  return Stdlib_Option.getOr(x, "");
+  if (x !== undefined) {
+    return x;
+  } else {
+    return "";
+  }
 }

JS modified (a top-level let f = M.f)

let getOr = Option.getOr
let orZero = (x: option<int>) => x->getOr(0)

js

 function orZero(x) {
-  return Stdlib_Option.getOr(x, 0);
+  if (x !== undefined) {
+    return x;
+  } else {
+    return 0;
+  }
 }

Review

  • What does inline_through_alias_test cover? The three shapes, let f = M.f, let {f} = module(M) and a local let f = M.f, each with Option functions that inline, and a call whose arguments must still run in order. hello_res.mjs is back to its output on master, where u is neither exported nor used, so only the length call remains.

@cknitt
cknitt changed the base branch from master to inline-assigned-cleanup December 5, 2026 10:20
@cknitt
cknitt added this pull request to stack #9541 December 5, 2026 10:20
@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:51:27.023120Z 9141ac7 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. Already looking forward to the next diff.

Reviewed commit: a0e9b0bc00

ℹ️ 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".

@cknitt
cknitt force-pushed the inline-through-aliases branch from a0e9b0b to f4299ac Compare December 5, 2026 10:23
@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.63%. Comparing base (fb36509) to head (9d227df).

Additional details and impacted files
@@                   Coverage Diff                    @@
##           inline-assigned-cleanup    #9539   +/-   ##
========================================================
  Coverage                    82.63%   82.63%           
========================================================
  Files                          469      469           
  Lines                        66585    66593    +8     
========================================================
+ Hits                         55021    55029    +8     
  Misses                       11564    11564           
Files with missing lines Coverage Δ
compiler/backend/lam_pass_remove_alias.ml 92.80% <100.00%> (+0.49%) ⬆️
🚀 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.

@github-actions

github-actions Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 660,462,564 → 660,608,442 (+0.02%).

Generated projects: 628,088,036 → 628,248,539 (+0.03%).

Benchmark Base Head Change
build/Namespace/clean 84,526,449 84,643,810 +0.14%
packages/@rescript/runtime/Primitive_float_extern.res 93,097 93,214 +0.13%
packages/@rescript/runtime/Stdlib_Blob.res 93,322 93,439 +0.13%
packages/@rescript/runtime/Stdlib_File.res 95,125 95,242 +0.12%
packages/@rescript/runtime/Primitive_promise.res 96,403 96,520 +0.12%
packages/@rescript/runtime/Primitive_int_extern.res 97,298 97,415 +0.12%
packages/@rescript/runtime/Primitive_string_extern.res 97,609 97,726 +0.12%
packages/@rescript/runtime/Stdlib_Iterable.res 98,323 98,440 +0.12%
packages/@rescript/runtime/Stdlib_AsyncIterable.res 98,395 98,512 +0.12%
packages/@rescript/runtime/Obj.res 98,584 98,701 +0.12%
8 more changes
Benchmark Base Head Change
packages/@rescript/runtime/Primitive_dict.res 102,005 102,122 +0.11%
packages/@rescript/runtime/RescriptTools.res 104,018 104,135 +0.11%
packages/@rescript/runtime/Stdlib_ArrayBuffer.res 106,891 107,008 +0.11%
packages/@rescript/runtime/RescriptTools_ExtractCodeBlocks.res 111,627 111,744 +0.10%
packages/@rescript/runtime/Primitive_array_extern.res 113,386 113,503 +0.10%
packages/@rescript/runtime/Primitive_object_extern.res 114,054 114,171 +0.10%
packages/@rescript/runtime/Stdlib_TaggedTemplate.res 114,588 114,705 +0.10%
packages/@rescript/runtime/Stdlib_Intl_Segments.res 115,901 116,018 +0.10%

Instructions

In total: 8,945,560,836 → 8,946,887,495 (+0.01%).

Benchmark Base Head Change
stress/InlineChain.res 134,605,458 134,849,568 +0.18%

Output size

In total: 27,764,252 → 27,764,662 (±0.00%).

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

Pull request 9d227df compared with base 24e1c7b in this run.

@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@9539

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 9d227df

@cknitt
cknitt force-pushed the inline-through-aliases branch from f4299ac to 9141ac7 Compare December 5, 2026 10:49
@cknitt

cknitt commented Dec 5, 2026

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 9141ac7abd

ℹ️ 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".

@cknitt
cknitt force-pushed the inline-through-aliases branch 3 times, most recently from 9983141 to b37eb36 Compare December 5, 2026 12:52
Why:
    Since the late inlining went, a call through a variable bound to
    another module's function, as in `let f = M.f` or
    `let {f} = module(M)`, was compiled as a call even when `M.f` is
    `@inline`. The alias pass now remembers which variables are bound to
    a field of another module and calls that field directly, so the
    existing case for other modules' functions inlines it. This works
    for top-level and local bindings alike.

JS modified (`let {f} = module(M)` in a function):
Example:
    let orEmpty = (x: option<string>) => {
      let {getOr} = module(Option)
      x->getOr("")
    }
Before (js):
    function orEmpty(x) {
      return Stdlib_Option.getOr(x, "");
    }
After (js):
    function orEmpty(x) {
      if (x !== undefined) {
        return x;
      } else {
        return "";
      }
    }

JS modified (a top-level `let f = M.f`):
Example:
    let getOr = Option.getOr
    let orZero = (x: option<int>) => x->getOr(0)
Before (js):
    function orZero(x) {
      return Stdlib_Option.getOr(x, 0);
    }
After (js):
    function orZero(x) {
      if (x !== undefined) {
        return x;
      } else {
        return 0;
      }
    }

Review: What does `inline_through_alias_test` cover? The three shapes,
`let f = M.f`, `let {f} = module(M)` and a local `let f = M.f`, each
with `Option` functions that inline, and a call whose arguments must
still run in order. `hello_res.mjs` is back to its output on master,
where `u` is neither exported nor used, so only the `length` call
remains.

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-through-aliases branch from b37eb36 to 9d227df Compare December 5, 2026 12:54
@cknitt
cknitt marked this pull request as ready for review December 5, 2026 12:55
@cknitt
cknitt requested a review from cristianoc December 5, 2026 13:12

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