Skip to content

Don't inline @inline functions that update a local ref in other modules - #9535

Open
cknitt wants to merge 1 commit into
inline-parameter-argsfrom
inline-no-assigned-body
Open

cknitt wants to merge 1 commit into
inline-parameter-argsfrom
inline-no-assigned-body

Conversation

@cknitt

@cknitt cknitt commented Dec 5, 2026 •

Copy link
Copy Markdown
Member

An @inline function that updates a local ref or mutable record is no longer inlined in other modules. Warning 113 says so:

@inline
let sumTo = n => {
  let total = ref(0)
  for i in 0 to n {
    total := total.contents + i
  }
  total.contents
}

This @inline function is inlined only in this module, not in modules that use it: it updates a local ref or mutable record.

Why. Within a module, a local ref only becomes a JavaScript variable assigned with = at the end of the optimizations (Lam_pass_sroa). The passes before it assume no variable is assigned. A body stored for other modules is stored after that step, so inlining it brought assigned variables into those passes in the other module. So a Some payload, a tuple field or a module field made from the ref could be read after the ref was updated. The Some payload case dates from #9022, the others from BuckleScript; all of #9533 belongs to this class:

// Other.res
@inline
let f = (n: int, q: 'a, q2: 'a) => {
  let s = ref(q)
  let t = (s.contents, 1)
  for _ in 0 to n {
    s := q2
  }
  let (a, _) = t
  (a, s.contents)
}

// Main.res
let main = (n, q) => Other.f(n, q, "b")

main(3, "a") returned ["b", "b"]. It now calls Other.f and returns ["a", "b"].

This closes the whole class at its source instead of guarding each optimization. The cost:

We'll see from user feedback whether such helpers need cross-module inlining. If they do, a later change could store the body from before Lam_pass_sroa, keeping this check as the fallback.

Checked, not assumed. With -check-lam, which the compiler tests use, Lam_check.check runs after every pass and now also fails if a pass before Lam_pass_sroa leaves an assignment, naming that pass. Without this restriction, inlining a ref-updating @inline function from another module fails it.

Tests:

  • warning_113_inline_not_stored gets the sumTo case.
  • inline_assigned_payload_test covers refs of the caller, assigned by a callback passed next to an argument made from them. Those bodies are still inlined and were never affected.
  • The ref-updating @inline helpers of the older cross-module tests are removed: they compile to calls now, and keeping them would need a warning suppression.

Fixes #9533.

Stacked on #9536.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Kbf3gHZYwA1rq7zbk3dhQq

@cknitt
cknitt changed the base branch from master to assigned-variable-module-field December 5, 2026 07:56
@cknitt
cknitt added this pull request to stack #9532 December 5, 2026 07:56
@github-actions

github-actions Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Changelog

🐛 Bug fix

  • Fix an @inline function from another module that read a local ref after the ref was updated, as in a Some or a tuple made from the ref before a loop. An @inline function that updates a local ref or mutable record is no longer inlined in other modules, and warning 113 says so.

Semantic Diff

1 commit · Full page

Don't store @inline bodies that update a local ref

97f2861 · cknitt + Claude · Dec 05, 2026, 09:58 UTC

Why

Lam_pass_sroa turns a local ref or mutable record into a variable that Lassign updates, and an @inline body is stored after it. Other modules inline such a body early in their pipeline, in passes that assume no variable is assigned before their own sroa. So a Some payload, a tuple field or a module field made from the ref could be read after the ref was updated: the Some payload case since #9022, the others since BuckleScript. Other modules now call these functions, and warning 113 says why. No @inline function of the runtime or Belt updates a ref, and storing @inline bodies by default (#9482 and before it) has not been released: v13.0.0-alpha.7 stored them only with -cross-module-opt.

Diagnostic added (exported @inline function that updates a local ref)

@inline
let sumTo = n => {
  let total = ref(0)
  for i in 0 to n {
    total := total.contents + i
  }
  total.contents
}

diagnostic

-none
+Warning number 113
+
+This @inline function is inlined only in this module, not in modules that use it: it updates a local ref or mutable record.

JS modified (cross-module @inline that updates a ref)

// Other.res
@inline
let f = (n: int, q: 'a, q2: 'a) => {
  let s = ref(q)
  let o = Some(s.contents)
  for _ in 0 to n {
    s := q2
  }
  switch o {
  | Some(v) => Some((v, s.contents))
  | None => None
  }
}

// Main.res
let main = () => Other.f(3, "a", "b")

js · Before: returns ["b", "b"] · After: returns ["a", "b"]

 function main() {
-  let s = "a";
-  for (let _for = 0; _for <= 3; ++_for) {
-    s = "b";
-  }
-  return [
-    s,
-    s
-  ];
+  return Other.f(3, "a", "b");
 }

Fixes #9533

Review

  • Which cases do the tests cover? The warning fixture gets sumTo. The ref-updating @inline helpers in the cross-module tests, and the repros of Reads of a reassigned variable from an inlined @inline body are moved past the assignment #9533 and the Some payload, are removed: they compile to calls now, and keeping them @inline would need a warning suppression. What stays are refs of the caller, assigned by a callback passed next to an argument made from them. Those bodies are still inlined, and were not affected; their tests keep it that way.
  • How does this stay true? With -check-lam, which the compiler tests use, Lam_check.check runs after every pass and now also fails on an assignment before Lam_pass_sroa, naming the pass that brought it in. Without this restriction, inlining a ref-updating @inline function from another module fails it.

@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:29.162268Z dc22125 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: 1a66c3c08d

ℹ️ 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,602,910 → 657,345,589 (+0.11%).

Generated projects: 628,519,080 → 628,524,174 (±0.00%).

⚠️ Regression: 2 benchmarks allocate more than 1% more.

Benchmark Base Head Change
build/no-op 9,041,058 9,282,062 +2.67%
build/leaf-edit 11,680,562 11,921,494 +2.06%
build/shared-edit 85,692,718 85,917,874 +0.26%

Instructions

In total: 8,976,444,511 → 8,976,277,988 (±0.00%).

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

Output size

In total: 27,600,498 → 27,600,450 (±0.00%).

Benchmark Base Head Change
output/tests/*.cmj 101,631 101,159 -0.46%

Pull request 97f2861 compared with base 2583394 in this run.

@codecov

codecov Bot commented Dec 5, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 80.00000% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.65%. Comparing base (6c39508) to head (97f2861).

Files with missing lines Patch % Lines
compiler/backend/lam_check.ml 0.00% 3 Missing ⚠️
Additional details and impacted files
@@                    Coverage Diff                    @@
##           inline-parameter-args    #9535      +/-   ##
=========================================================
- Coverage                  82.68%   82.65%   -0.03%     
=========================================================
  Files                        469      469              
  Lines                      66575    66587      +12     
=========================================================
- Hits                       55047    55038       -9     
- Misses                     11528    11549      +21     
Files with missing lines Coverage Δ
compiler/backend/lam_compile_main.ml 94.75% <100.00%> (+0.07%) ⬆️
compiler/backend/lam_stats_export.ml 93.22% <100.00%> (+0.49%) ⬆️
compiler/backend/lam_check.ml 90.74% <0.00%> (-2.53%) ⬇️

... and 7 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@9535

@rescript/belt

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

@rescript/darwin-arm64

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

@rescript/linux-arm64

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

@rescript/linux-x64

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

@rescript/runtime

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

@rescript/win32-x64

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

commit: 97f2861

@cknitt
cknitt force-pushed the inline-no-assigned-body branch from 1a66c3c to 9ddb95a Compare December 5, 2026 08:08
@cknitt

cknitt commented Dec 5, 2026

Copy link
Copy Markdown
Member Author

@codex review

@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: 9ddb95a930

ℹ️ 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 force-pushed the inline-no-assigned-body branch from 9ddb95a to db8768c Compare December 5, 2026 10:18
@cknitt
cknitt changed the base branch from assigned-variable-module-field to master December 5, 2026 10:20
@cknitt
cknitt changed the base branch from master to inline-parameter-args 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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: db8768ce91

ℹ️ 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-no-assigned-body branch from db8768c to 9fffb48 Compare December 5, 2026 10:23
@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. Already looking forward to the next diff.

Reviewed commit: 9fffb48cab

ℹ️ 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-no-assigned-body branch from 9fffb48 to dc22125 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: dc2212502a

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

Why:
    `Lam_pass_sroa` turns a local ref or mutable record into a variable
    that `Lassign` updates, and an `@inline` body is stored after it.
    Other modules inline such a body early in their pipeline, in passes
    that assume no variable is assigned before their own sroa. So a
    `Some` payload, a tuple field or a module field made from the ref
    could be read after the ref was updated: the `Some` payload case
    since #9022, the others since BuckleScript. Other modules now call
    these functions, and warning 113 says why. No `@inline` function of
    the runtime or Belt updates a ref, and storing `@inline` bodies by
    default (#9482 and before it) has not been released: v13.0.0-alpha.7
    stored them only with `-cross-module-opt`.

Diagnostic added (exported @inline function that updates a local ref):
Example:
    @inline
    let sumTo = n => {
      let total = ref(0)
      for i in 0 to n {
        total := total.contents + i
      }
      total.contents
    }
Before (diagnostic): none
After (diagnostic):
    Warning number 113

    This @inline function is inlined only in this module, not in modules that use it: it updates a local ref or mutable record.

JS modified (cross-module @inline that updates a ref):
Example:
    // Other.res
    @inline
    let f = (n: int, q: 'a, q2: 'a) => {
      let s = ref(q)
      let o = Some(s.contents)
      for _ in 0 to n {
        s := q2
      }
      switch o {
      | Some(v) => Some((v, s.contents))
      | None => None
      }
    }

    // Main.res
    let main = () => Other.f(3, "a", "b")
Before (js): returns ["b", "b"]
    function main() {
      let s = "a";
      for (let _for = 0; _for <= 3; ++_for) {
        s = "b";
      }
      return [
        s,
        s
      ];
    }
After (js): returns ["a", "b"]
    function main() {
      return Other.f(3, "a", "b");
    }

Review: Which cases do the tests cover? The warning fixture gets
`sumTo`. The ref-updating `@inline` helpers in the cross-module tests,
and the repros of #9533 and the `Some` payload, are removed: they
compile to calls now, and keeping them `@inline` would need a warning
suppression. What stays are refs of the caller, assigned by a callback
passed next to an argument made from them. Those bodies are still
inlined, and were not affected; their tests keep it that way.

Review: How does this stay true? With `-check-lam`, which the compiler
tests use, `Lam_check.check` runs after every pass and now also fails on
an assignment before `Lam_pass_sroa`, naming the pass that brought it in.
Without this restriction, inlining a ref-updating `@inline` function from
another module fails it.

Fixes #9533

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-no-assigned-body branch from dc22125 to 97f2861 Compare December 5, 2026 11:50

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.

Reads of a reassigned variable from an inlined @inline body are moved past the assignment

1 participant