Skip to content

[12.x] Rename the assigned variable when inlining an assignment - #9451

Open
illusionalsagacity wants to merge 1 commit into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-inline-assign-rename-12.x
Open

illusionalsagacity wants to merge 1 commit into
rescript-lang:maintenance/12.xfrom
illusionalsagacity:fix-inline-assign-rename-12.x

Conversation

@illusionalsagacity

Copy link
Copy Markdown
Contributor

Backport of #9424 to 12.x.

Problem

With -bs-cross-module-opt, inlining another module's function renames the variables bound in its body. Lam_bounded_vars.rewrite renamed the binders and the reads, but not the variable of an assignment. The output assigned an undeclared variable, or the compiler stopped in Lam_check.

// Lib.res
@@config({flags: ["-bs-cross-module-opt"]})
@inline
let 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 addBumped = (x, y) => Lib.mut(x) + Lib.mut(y)
let bumpConstants = () => (Lib.mut(5), Lib.mut(3))

Before:

function addBumped(x, y) {
  let r$1 = x;
  r = r$1 + 1 | 0;
  if (x > 3) {
    r = (r$1 << 1);
  }
  let r$2 = y;
  r = r$2 + 1 | 0;
  if (y > 3) {
    r = (r$2 << 1);
  }
  return r$1 + r$2 | 0;
}

and for bumpConstants:

[SANITY]:r/1003 used before defined in Use.res
Fatal error: exception File "compiler/core/lam_check.ml", line 155, characters 2-8: Assertion failed

After:

function addBumped(x, y) {
  let r = x;
  r = r + 1 | 0;
  if (x > 3) {
    r = (r << 1);
  }
  let r$1 = y;
  r$1 = r$1 + 1 | 0;
  if (y > 3) {
    r$1 = (r$1 << 1);
  }
  return r + r$1 | 0;
}

function bumpConstants() {
  let r = 5;
  r = r + 1 | 0;
  r = (r << 1);
  let r$1 = 3;
  r$1 = r$1 + 1 | 0;
  return [
    r,
    r$1
  ];
}

With the renaming fixed, 12.x has the same three further bugs as master: a body inlined from another module comes with its own Lassigns, while within a module the Lambda passes see none until Lam_pass_lets_dce, the last one, makes them. The outputs below are from 12.x with only the renaming fixed; without it, each of these stops in Lam_check too.

// Lib.res
@inline
let readThenAssign = (x: int, f: int => unit => int) => {
  let r = ref(x)
  let g = f(r.contents)
  r := 10
  g() + r.contents
}

@inline
let copyThenAssign = (x: int) => {
  let r = ref(x)
  let s = r.contents
  r := s + 1
  s + r.contents
}

@inline
let negated = (x: bool) => {
  let r = ref(x)
  r := !r.contents
  r.contents ? 1 : 2
}

// Use.res
let readBeforeAssign = () => Lib.readThenAssign(5, p => () => p)
let copyBeforeAssign = () => Lib.copyThenAssign(1)
let negatedTrue = () => Lib.negated(true)

Before (readBeforeAssign() gives 20 instead of 15, copyBeforeAssign() 4 instead of 3, negatedTrue() 1 instead of 2):

function readBeforeAssign() {
  let r = 5;
  let g = () => r;
  r = 10;
  return g() + r | 0;
}

function copyBeforeAssign() {
  let r = 1;
  r = r + 1 | 0;
  return r + r | 0;
}

function negatedTrue() {
  let r = true;
  r = !r;
  return 1;
}

After:

function readBeforeAssign() {
  let r = 5;
  let p = r;
  let g = () => p;
  r = 10;
  return g() + r | 0;
}

function copyBeforeAssign() {
  let r = 1;
  let s = r;
  r = s + 1 | 0;
  return s + r | 0;
}

function negatedTrue() {
  let r = true;
  r = !r;
  if (r) {
    return 1;
  } else {
    return 2;
  }
}

A string switch on the variable after an assignment (matchAfterAssign in the test) still stopped in Lam_check with the renaming fixed, and now compiles to a switch on the new value.

Fix

As on master:

  • Lam_bounded_vars.rewrite renames the variable of an Lassign like its reads.
  • A variable some Lassign assigns is not an alias: Lam_util.refine_let keeps a binding let p = r and Lam_pass_lets_dce no longer replaces p by r.
  • Lam_pass_remove_alias.simplify_alias adds the variables an inlined body assigns before it simplifies the body further.
  • Lam_pass_collect records nothing about the value of a variable bound by a mutable let.

Differences from master, which has the infrastructure for assigned variables from earlier PRs that are not on 12.x (#9177, #9284 and others):

  • Lam_util.assigned_variables gives the variables some Lassign assigns, as Lambda_traverse.assigned_variables does on master.
  • refine_let, Lam_beta_reduce.no_names_beta_reduce and Lam_pass_count.collect_occurs take ~assignable. Lam_pass_lets_dce and Lam_pass_deep_flatten compute it from the term they rewrite.
  • Lam_stats.t has an assignable field, as on master. simplify_alias sets it; elsewhere it is empty, so inlining in Lam_compile behaves as before.
  • Lam_beta_reduce.propagate_beta_reduce and propagate_beta_reduce_with_map bind an argument read from an assigned variable instead of substituting it into the body. Master decides this more finely, substituting it when the body reads it before any side effect. So on 12.x the closure in readBeforeAssign above isn't inlined (master gives return p + r | 0;), but the result is the same.

Within a module nothing changes, since no Lassign exists before Lam_pass_lets_dce: no checked-in output in tests/tests other than the new tests' changes.

Tests

  • tests/tests/src/inline_assign_cross_module_test.res and inline_assign_cross_module_lib.res are the same as on master. Their .mjs differs from master's as described above. On 12.x without the fix, the test module doesn't compile.
  • tests/ounit_tests/ounit_lam_bounded_vars_tests.ml checks that the variable of an assignment is renamed and one bound outside the body is not. 12.x had no such file, so it is added with only this test.

Investigated by Claude.

…ng#9424)

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, or the compiler stopped in Lam_check ("used before defined").
The assigned variable is now renamed too.

With the renaming fixed, three more bugs show up on 12.x as on master,
all because an inlined body comes with Lassigns of its own, while within
a module Lam_pass_lets_dce, the last Lambda pass, makes the first ones:

- A binding `let p = r` was an alias (Lam_util.refine_let,
  Lam_pass_lets_dce), so after an assignment to r, a read of p gave the
  new value. A variable some Lassign assigns is no longer an alias.
- Inlining a call substituted an argument read from a variable into the
  body, so a closure in the body read the variable after a later
  assignment. Such an argument is now bound where the call is. On master
  Lam_beta_reduce decides this with Lam_stats.assignable, from earlier
  PRs not on 12.x. This adds that field, set by
  Lam_pass_remove_alias.simplify_alias, which also adds the variables an
  inlined body assigns, as on master. Elsewhere it is empty, so the
  JS-layer inlining in Lam_compile behaves as before.
- Lam_pass_collect recorded what a mutable let binds, so a condition on
  the variable, or a string switch, was decided by the value it started
  with. A mutable let now records nothing about its variable.

Lam_util.assigned_variables gives the assigned variables, as
Lambda_traverse.assigned_variables does on master.

12.x has no tests/ounit_tests/ounit_lam_bounded_vars_tests.ml, so it is
added with only the renaming test.

(cherry picked from commit 6a7e3b8)
Signed-off-by: Rob <illusionalsagacity@users.noreply.github.com>

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