Semantic Diff

#9424 Rename the assigned variable when inlining an assignment

5 commits

aacaf7206841611e209512ff949c9b6cf6596228..pr-9424

5f21374b5 · illusionalsagacity · Nov 25, 2026, 19:33 UTC

Rename the assigned variable when inlining an assignment

Why

With -bs-cross-module-opt, inlining another module's function renames the variables bound in its body (Lam_bounded_vars.rewrite). The binders and the reads were renamed, but Lassign (v, l) kept the old v, which the caller doesn't declare. The generated code then assigned an undeclared variable, a ReferenceError when run, or the compiler crashed in Lam_dce when a later Lambda pass saw the assignment. The assigned variable is now renamed too.

modifiedLambda renaming of an assignment

text
Before Lam_bounded_vars.rewrite
After Lam_bounded_vars.rewrite
let r = x in r = r + 1; r ⇝ let r' = x in r = r' + 1; r' let r = x in r = r + 1; r ⇝ let r' = x in r' = r' + 1; r'a variable bound outside the body keeps its name

modifiedJS inlined function that assigns a local variable

// Lib.res@@config({flags: ["-bs-cross-module-opt"]})@inlinelet mut = (x: int) => {  let r = ref(x)  r := r.contents + 1  if x > 3 {    r := r.contents * 2  }  r.contents} // Use.res@@config({flags: ["-bs-cross-module-opt"]})let a = (x, y) => Lib.mut(x) + Lib.mut(y)
js
Before
After
function a(x, y) {function a(x, y) { let r$1 = x; r = r$1 + 1 | 0; let r = x; r = r + 1 | 0; if (x > 3) { if (x > 3) { r = (r$1 << 1); r = (r << 1); } } let r$2 = y; r = r$2 + 1 | 0; let r$1 = y; r$1 = r$1 + 1 | 0; if (y > 3) { if (y > 3) { r = (r$2 << 1); r$1 = (r$1 << 1); } } return r$1 + r$2 | 0; return r + r$1 | 0;}}

modifiedJS constant argument, inlined in the Lambda passes

// Use.res, with the Lib.res above@@config({flags: ["-bs-cross-module-opt"]})let b = () => Lib.mut(5)
text
Before
Fatal error: exception Failure("File \"compiler/backend/lam_dce.ml\", line 33, characters 31-38r/1003 not found")
js
After
function b() {  let r = 5;  r = r + 1 | 0;  r = (r << 1);  return r;}

Review

  • Can the map hold something other than a variable for an assigned one? No: only let-bound variables are assigned, and the map replaces those with variables; only parameters map to other terms.
  • Why does the new test fail with a ReferenceError rather than the crash? Lam_dce only misses the old variable when no top-level variable of the module has its stamp, and here addBumped does. The unit test checks the renaming itself.
  • Which checked-in outputs change? None but the new test's.
c3dbaae4a · illusionalsagacity · Nov 27, 2026, 16:06 UTC

Keep the binding of a copy of an assigned variable

Why

A binding let p = r was an alias: Lam_util.refine_let made it one and Lam_pass_lets_dce replaced p by r, both for any variable. When r is assigned after the binding, a read of p then gives the new value. Within a module this didn't happen, since only Lam_pass_sroa, after both, makes Lassigns, but a body inlined from another module with -bs-cross-module-opt comes with its own. A variable some Lassign assigns is no longer an alias.

modifiedLambda binding a variable to an assigned one

text
Before Lam_util.refine_let, Lam_pass_lets_dce
After Lam_util.refine_let, Lam_pass_lets_dce
let p = r in (r = 10; p + r) ⇝ r = 10; r + r let p = r in (r = 10; p + r) ⇝ let p = r in (r = 10; p + r)r: assigned by an Lassign

modifiedJS inlined function that reads a variable before assigning it

// Lib.res@@config({flags: ["-bs-cross-module-opt"]})@inlinelet readThenAssign = (x: int, f: int => unit => int) => {  let r = ref(x)  let g = f(r.contents)  r := 10  g() + r.contents} @inlinelet copyThenAssign = (x: int) => {  let r = ref(x)  let s = r.contents  r := s + 1  s + r.contents} // Use.res@@config({flags: ["-bs-cross-module-opt"]})let c = () => Lib.readThenAssign(5, p => () => p)let d = () => Lib.copyThenAssign(1)
js
Before
After
function c() {function c() { let r = 5; let r = 5; let p = r; r = 10; r = 10; return r + r | 0; return p + r | 0;}}function d() {function d() { let r = 1; let r = 1; r = r + 1 | 0; return r + r | 0; let s = r; r = s + 1 | 0; return s + r | 0;}}

Review

  • Which checked-in outputs change? None but the new tests'.
9f7f1d646 · illusionalsagacity · Nov 27, 2026, 16:06 UTC

Count the variables an inlined body assigns as assigned

Why

Lam_pass_remove_alias.simplify_alias computes the variables some Lassign assigns once, before it inlines. A body inlined from another module with -bs-cross-module-opt may assign variables, renamed by the inlining, so they were missing. Inlining a call in that body then moved a read of one past an assignment. The variables an inlined body assigns are now added before it is simplified further, as Lam_compile.compile_external_field_apply does for the bodies it inlines.

modifiedJS closure bound before it is passed to an inlined function

// Lib.res@@config({flags: ["-bs-cross-module-opt"]})@inlinelet readThenAssign = (x: int, f: int => unit => int) => {  let r = ref(x)  let g = f(r.contents)  r := 10  g() + r.contents} // Use.res@@config({flags: ["-bs-cross-module-opt"]})let c = () => {  let capture = p => () => p  Lib.readThenAssign(5, capture)}
js
Before
After
function c() {function c() { let r = 5; let r = 5; let p = r; r = 10; r = 10; return r + r | 0; return p + r | 0;}}

Review

  • Why does a closure written in the call not need this? There the variables are found by the next simplify_alias round, which inlines the call to the closure; a bound closure is inlined in the same round as the body.
  • Which checked-in outputs change? None but the new test's.
6510a1478 · illusionalsagacity · Nov 27, 2026, 16:06 UTC

Record nothing about the value of a variable bound by a mutable let

Why

Lam_pass_collect recorded what any let binds, a mutable one too, so Lam_pass_remove_alias took a variable bound to a constant for that constant even after an Lassign. It decided a condition on the variable, or a string switch, by the value it started with. Within a module no Lassign exists yet when these passes run, but a body inlined from another module with -bs-cross-module-opt comes with its own. A mutable let now records nothing about its variable.

modifiedJS inlined function that tests a variable after assigning it

// Lib.res@@config({flags: ["-bs-cross-module-opt"]})@inlinelet negated = (x: bool) => {  let r = ref(x)  r := !r.contents  r.contents ? 1 : 2} @inlinelet matchAfterAssign = (x: string) => {  let r = ref(x)  r := "b"  switch r.contents {  | "a" => 1  | _ => 2  }} // Use.res@@config({flags: ["-bs-cross-module-opt"]})let a = () => Lib.negated(true)
js
Before
After
function a() {function a() { let r = true; let r = true; r = !r; r = !r; return 1; if (r) { return 1; } else { return 2; }}}

modifiedJS inlined function that switches on a string after assigning it

// Use.res, with the Lib.res above@@config({flags: ["-bs-cross-module-opt"]})let b = () => Lib.matchAfterAssign("a")
text
Before
Fatal error: exception Failure("File \"compiler/backend/lam_dce.ml\", line 33, characters 31-38r/1007 not found")
js
After
function b() {  let r = "a";  r = "b";  let match = r;  if (match === "a") {    return 1;  } else {    return 2;  }}

Review

  • Which checked-in outputs change? None but the new tests'.
a51ced59d · illusionalsagacity · Nov 27, 2026, 16:07 UTC

Report an assigned variable inlining can't rename instead of asserting

Lam_bounded_vars.rewrite stopped with assert false when the variable of an Lassign was mapped to a term other than a variable. It can't be today, but if it ever is, the failure now names the variable.