Skip to content

Simplify bodies inlined while compiling to JavaScript - #9527

Closed
cknitt wants to merge 1 commit into
alias-assigned-payloadfrom
late-inline-alias-pass
Closed

cknitt wants to merge 1 commit into
alias-assigned-payloadfrom
late-inline-alias-pass

Conversation

@cknitt

@cknitt cknitt commented Dec 4, 2026 •

Copy link
Copy Markdown
Member

A call to another module's function is inlined in one of two places. If an argument is a parameter of the calling function, inlining waits until the code is compiled to JavaScript. By then the alias pass has already run, so the inlined body kept what that pass removes elsewhere:

  • a Some(x) argument was bound to a variable and unwrapped again with Primitive_option.valFromOption;
  • a function argument was called instead of inlined.

The alias pass now also runs on such a body before it is compiled, in lam_compile.ml. It runs with a fresh table of facts: the one the earlier passes filled describes the caller as it was before the last passes rewrote it, so it names variables that are gone. The fresh table only knows the parameters the inlined body binds. The pass also keeps the caller's assigned variables in its set of assigned variables, which it used to replace with the body's own.

let knownSome = (x: int, f) => Some(x)->Option.map(f)->Option.getOr(0)
let mapped = (opt: option<int>) => opt->Option.map(x => x + 1)

Before:

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

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

After:

function knownSome(x, f) {
  let opt = Primitive_option.some(f(x));
  if (opt !== undefined) {
    return opt;
  } else {
    return 0;
  }
}

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

A literal Some(x) passed without a parameter argument was already read directly, since that call is inlined before the alias pass. The remaining Primitive_option.some is the result of Option.map; dropping it needs a mark on the result of the call, which is a separate change.

New test cases in inline_option_unwrap_test: a literal Some next to a parameter argument, with a payload that can't be undefined and with one that can. The new inline_late_alias_test runs cases that a version without the fresh table or the assigned variables miscompiled: local functions and Somes of the caller naming removed variables, and reads of assigned variables moved past a call that assigns them.

Stacked on #9531.

Follow-up of #9504 (#8733).

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq

@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Changelog

💅 Polish

  • Simplify the body of a function from another module that is inlined while compiling to JavaScript, so a Some(x) argument is read directly and a function argument applied in the body is inlined.

Semantic Diff

1 commit · Full page

✏️ Simplify bodies inlined while compiling to JavaScript

9af8bb9 · cknitt + Claude · Dec 04, 2026, 17:58 UTC · edited

Why

A call to another module's function that also passes a parameter of the caller is inlined late, while compiling to JavaScript, after the alias pass has run. So the inlined body kept what that pass removes elsewhere: a Some(x) argument was bound and unwrapped again, and a function argument was called instead of inlined. The alias pass now also runs on such a body before it is compiled.

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;
+  let opt = Primitive_option.some(f(x));
+  if (opt !== undefined) {
+    return opt;
   } else {
     return 0;
   }
 }

JS modified (a function argument)

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);
   }
 }

Review

  • Why does the late pass get a fresh ident_tbl? The table was filled by collect_info before simplify_alias 3, simplify_lets, sroa and simplify_exits rewrote the program, so at codegen it can give a function body or a Some payload naming a variable those passes removed or turned into an assigned one: inlining a let-bound local function from it printed p + k with no k in scope. The body is now simplified knowing only the parameters it binds; the caller is still compiled with the real meta.
  • Which variables count as assigned there? The pass used to take only the ones lam assigns, but at codegen lam is just the inlined body, so a variable of the caller assigned by a call in it looked immutable and its read moved past that call. The pass now starts from meta.assignable as well. In the other direction, the pass may inline a further function that assigns a variable of its own, so the variables the body assigns are collected after the pass, before it is compiled.
  • What stays? Option.map still wraps its result with Primitive_option.some; that needs a mark on the result of the call.

@github-actions

github-actions Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 657,488,730 → 657,493,477 (±0.00%).

Generated projects: 629,122,823 → 629,123,605 (±0.00%).

Benchmark Base Head Change
packages/@rescript/runtime/Primitive_object.res 1,112,644 1,114,944 +0.21%

Instructions

In total: 8,977,171,177 → 8,977,304,619 (±0.00%).

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

Output size

In total: 27,616,044 → 27,616,210 (±0.00%).

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

Pull request 9af8bb9 compared with base 38f28fc in this run.

@codecov

codecov Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.69%. Comparing base (a507341) to head (9af8bb9).

Additional details and impacted files
@@                   Coverage Diff                   @@
##           alias-assigned-payload    #9527   +/-   ##
=======================================================
  Coverage                   82.68%   82.69%           
=======================================================
  Files                         469      469           
  Lines                       66595    66598    +3     
=======================================================
+ Hits                        55064    55071    +7     
+ Misses                      11531    11527    -4     
Files with missing lines Coverage Δ
compiler/backend/lam_compile.ml 92.31% <100.00%> (+0.01%) ⬆️
compiler/backend/lam_pass_remove_alias.ml 93.61% <100.00%> (+0.04%) ⬆️

... and 1 file 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 4, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 9af8bb9

@github-actions

github-actions Bot commented Dec 4, 2026

Copy link
Copy Markdown

@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-05T07:58:48.345159Z 9af8bb9 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 chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 33c9f93aff

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

Comment thread compiler/backend/lam_compile.ml Resolved
Why:
    A call to another module's function that also passes a parameter of
    the caller is inlined late, while compiling to JavaScript, after the
    alias pass has run. So the inlined body kept what that pass removes
    elsewhere: a `Some(x)` argument was bound and unwrapped again, and a
    function argument was called instead of inlined. The alias pass now
    also runs on such a body before it is compiled.

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) {
      let opt = Primitive_option.some(f(x));
      if (opt !== undefined) {
        return opt;
      } else {
        return 0;
      }
    }

JS modified (a function argument):
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);
      }
    }

Review: Why does the late pass get a fresh `ident_tbl`? The table was
filled by `collect_info` before `simplify_alias 3`, `simplify_lets`,
`sroa` and `simplify_exits` rewrote the program, so at codegen it can
give a function body or a `Some` payload naming a variable those passes
removed or turned into an assigned one: inlining a let-bound local
function from it printed `p + k` with no `k` in scope. The body is now
simplified knowing only the parameters it binds; the caller is still
compiled with the real `meta`.

Review: Which variables count as assigned there? The pass used to take
only the ones `lam` assigns, but at codegen `lam` is just the inlined
body, so a variable of the caller assigned by a call in it looked
immutable and its read moved past that call. The pass now starts from
`meta.assignable` as well. In the other direction, the pass may inline a
further function that assigns a variable of its own, so the variables
the body assigns are collected after the pass, before it is compiled.

Review: What stays? `Option.map` still wraps its result with
`Primitive_option.some`; that needs a mark on the result of the call.

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 late-inline-alias-pass branch from 33c9f93 to 9af8bb9 Compare December 5, 2026 07:28
@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: 9af8bb988f

ℹ️ 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 removed this pull request from stack #9532 December 5, 2026 10:18
@cknitt

cknitt commented Dec 5, 2026

Copy link
Copy Markdown
Member Author

Superseded by stack #9541. #9536 makes the alias pass inline calls with parameter arguments, and #9537 removes late inlining while compiling to JavaScript, so there is no late body left to simplify. This PR's tests moved to #9536 and #9537 and pass with better output. (Comment by Claude.)

@cknitt cknitt closed this Dec 5, 2026
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