Skip to content

[12.x] Give each inlined copy of a function or functor body its own static exits - #9459

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

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

Conversation

@illusionalsagacity

Copy link
Copy Markdown
Contributor

Backport of #9414 to 12.x.

Problem

Lam_bounded_vars.rewrite refreshes the bound variables of a function body before it is copied (an @inline function at each call site, a functor body at each application), but kept the ids of the static catches inside it. The copies and the definition then shared an exit id, and Lam_pass_exits.subst_helper, which records inlinable handlers by exit id, substituted one copy's handler into the raises of another, so the wrong arm of a switch with or-patterns or an | exception case ran.

@inline(never) let id = x => x
@inline(never) let big = (s: string) => s ++ "!" ++ s

@inline
let pick = (x: int, y: int, flag: bool) =>
  switch (x, y) {
  | (0, _) | (_, 0) => flag ? "t" : big("a") ++ big("b")
  | _ => "other"
  }

let constant = pick(0, 5, true)
let computed = pick(id(0), id(5), id(false))

On master the second copy took the first one's handler. On 12.x the copies come out right, and the definition is the one that takes the handler the constant copy was simplified to, so pick(0, 5, false) called without inlining returns "t".

Before:

function pick(x, y, flag) {
  if (x !== 0 && y !== 0) {
    return "other";
  } else {
    return "t";
  }
}

After:

function pick(x, y, flag) {
  if (x !== 0 && y !== 0) {
    return "other";
  }
  if (flag) {
    return "t";
  } else {
    return big("a") + big("b");
  }
}

A functor applied twice had the same problem: the Make function of master's test computed "t" for flag = false.

Fix

As on master, each catch in the rewritten body gets a fresh exit id (negative ids, which mark exception cases, stay negative) and the raises it scopes are redirected; raises to exits caught outside the body keep their ids.

Differences from master:

  • 12.x works on Lam.t where master has Lambda.t, takes the exit counters from Lambda rather than Lambda_exits, and has Map_int's map-first argument order.
  • 12.x has no tests/ounit_tests/ounit_lam_bounded_vars_tests.ml, so it is added with only this PR's unit tests. The unmerged Rename the assigned variable when inlining an assignment #9424 backport adds the same file, so whichever lands second needs a small merge of that file and of ounit_tests_main.ml.
  • Since the bug shows in the definitions on 12.x, master's runtime tests pass on 12.x without the fix. An extra test calls pick, nested and bound through id, and Make through a functor unpacked from a first-class module, so they aren't inlined.

Tests

  • tests/ounit_tests/ounit_lam_bounded_vars_tests.ml: master's four unit tests (a fresh exit per copy, outer exits kept, a catch scoping over its body only, negative ids staying negative). All four fail without the fix.
  • tests/tests/src/inline_static_exit_test.res: master's tests plus "definitions of inlined functions and functors", which fails without the fix. The generated JS of the definitions now matches master's.
  • tests/tests/src/inline_static_exit_cross_module_{lib,test}.res: master's cross-module tests, unchanged; they pass on 12.x with and without the fix.

Investigated by Claude.

…xits (rescript-lang#9414)

Lam_bounded_vars.rewrite refreshes the bound variables of a function
body before it is copied (an `@inline` function at each call site, a
functor body at each application) but kept the ids of the static catches
inside it. The copies and the definition then shared an exit id, and
Lam_pass_exits.subst_helper, which records inlinable handlers by exit id,
substituted one copy's handler into the raises of another. Each catch in
the rewritten body now gets a fresh exit id (negative ids, which mark
exception cases, stay negative) and the raises it scopes are redirected;
raises to exits caught outside the body are left alone.

On 12.x the copies come out right and the definition is the one that
takes a copy's handler: with master's test, `pick` itself returned "t"
for every match of its or-pattern, and the functor `Make` did the same.
Master's runtime tests therefore pass on 12.x without the fix, so a test
calls the definitions without inlining them, through `id` and through a
functor unpacked from a first-class module; it fails without the fix.

12.x has Lam.t where master has Lambda.t, takes the exit counters from
Lambda instead of Lambda_exits, and has Map_int's map-first argument
order. It has no tests/ounit_tests/ounit_lam_bounded_vars_tests.ml, so
it is added with only this PR's tests.

(cherry picked from commit 64cdd3c)
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