Skip to content

Put arguments read once into inlined bodies directly - #9517

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

cknitt wants to merge 1 commit into
masterfrom
inline-args-read-once

Conversation

@cknitt

@cknitt cknitt commented Dec 3, 2026

Copy link
Copy Markdown
Member

When a function from another module is inlined, an argument that has no side effects and that the body uses once now goes straight into the body, instead of into a variable first.

The compiler inlines such calls in two places. If an argument is a parameter of the calling function, inlining waits until JavaScript generation, and that step already substitutes these arguments. Otherwise lam_pass_remove_alias inlines the call early, and it bound every argument that isn't a constant to a variable. It did the same for a function applied directly. Both now use the same substitution as the late step (propagate_beta_reduce_with_map).

let called = []
Ok(3)->Result.forEach(i => called->Array.push(i))
let merge = arr => Array.concatAll([[1], arr])

Before:

let called = [];
let f = i => {
  called.push(i);
};
f(3);

function merge(arr) {
  let arrays = [
    [1],
    arr
  ];
  return [].concat(...arrays);
}

After:

let called = [];
called.push(3);

function merge(arr) {
  return [].concat([1], arr);
}

The other changed test outputs are of the same kind. Evaluation order is unchanged: an argument still moves only when the existing checks allow it, so inline_arg_order_cross_module_test still reads the variable before the call that increments it.

Part of the inlining follow-ups (#8733), and a prerequisite for replacing @as(json) arguments with @inline functions (next PR).

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq

@cknitt
cknitt added this pull request to stack #9519 December 3, 2026 19:26
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Changelog

💅 Polish

  • Put a literal or a function passed to an inlined function from another module straight into its body when the body uses it once, instead of binding it to a variable first.

Semantic Diff

1 commit · Full page

Put arguments read once into inlined bodies directly

516009a · cknitt + Claude · Dec 03, 2026, 19:19 UTC

Why

Calls to other modules' functions are inlined in two places. When an argument is a parameter of the caller, inlining waits until the JavaScript is generated, and a pure argument the body reads once takes the parameter's place. Otherwise lam_pass_remove_alias inlines the call early and bound every argument to a variable first; it did the same when applying a function directly. Both now substitute like the late path.

JS modified (literal and function arguments)

let called = []
Ok(3)->Result.forEach(i => called->Array.push(i))
let merged = Array.concatAll([[1], arr])

js

 let called = [];
-let f = i => {
-  called.push(i);
-};
-f(3);
+called.push(3);
 
 function merge(arr) {
-  let arrays = [
-    [1],
-    arr
-  ];
-  return [].concat(...arrays);
+  return [].concat([1], arr);
 }

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

✅ Compiler benchmarks

Allocated words

In total: 653,178,413 → 653,169,005 (±0.00%).

Generated projects: 627,296,569 → 627,293,948 (±0.00%).

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

Instructions

In total: 8,966,820,894 → 8,966,803,087 (±0.00%).

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

Output size

In total: 27,449,936 → 27,449,924 (±0.00%).

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

Pull request 516009a compared with base 7968a96 in this run.

Why:
    Calls to other modules' functions are inlined in two places. When an
    argument is a parameter of the caller, inlining waits until the
    JavaScript is generated, and a pure argument the body reads once takes
    the parameter's place. Otherwise `lam_pass_remove_alias` inlines the call
    early and bound every argument to a variable first; it did the same when
    applying a function directly. Both now substitute like the late path.

JS modified (literal and function arguments):
Example:
    let called = []
    Ok(3)->Result.forEach(i => called->Array.push(i))
    let merged = Array.concatAll([[1], arr])
Before (js):
    let called = [];
    let f = i => {
      called.push(i);
    };
    f(3);

    function merge(arr) {
      let arrays = [
        [1],
        arr
      ];
      return [].concat(...arrays);
    }
After (js):
    let called = [];
    called.push(3);

    function merge(arr) {
      return [].concat([1], arr);
    }

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-args-read-once branch from 820b806 to 516009a Compare December 3, 2026 19:32
@codecov

codecov Bot commented Dec 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.56%. Comparing base (7968a96) to head (516009a).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #9517      +/-   ##
==========================================
+ Coverage   82.54%   82.56%   +0.01%     
==========================================
  Files         468      468              
  Lines       66226    66228       +2     
==========================================
+ Hits        54665    54678      +13     
+ Misses      11561    11550      -11     
Files with missing lines Coverage Δ
compiler/backend/lam_pass_remove_alias.ml 93.18% <100.00%> (+0.10%) ⬆️

... and 3 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 3, 2026

Copy link
Copy Markdown

Open in StackBlitz

rescript

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

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 516009a

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

@cknitt
cknitt requested a review from cristianoc December 3, 2026 19:59
@cknitt
cknitt marked this pull request as ready for review December 3, 2026 19:59

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