Repository navigation
[12.x] Rename the assigned variable when inlining an assignment - #9451
Open
illusionalsagacity wants to merge 1 commit into
Open
illusionalsagacity wants to merge 1 commit into
illusionalsagacity wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.rewriterenamed the binders and the reads, but not the variable of an assignment. The output assigned an undeclared variable, or the compiler stopped inLam_check.Before:
and for
bumpConstants:After:
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 untilLam_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 inLam_checktoo.Before (
readBeforeAssign()gives 20 instead of 15,copyBeforeAssign()4 instead of 3,negatedTrue()1 instead of 2):After:
A string switch on the variable after an assignment (
matchAfterAssignin the test) still stopped inLam_checkwith the renaming fixed, and now compiles to a switch on the new value.Fix
As on master:
Lam_bounded_vars.rewriterenames the variable of anLassignlike its reads.Lassignassigns is not an alias:Lam_util.refine_letkeeps a bindinglet p = randLam_pass_lets_dceno longer replacespbyr.Lam_pass_remove_alias.simplify_aliasadds the variables an inlined body assigns before it simplifies the body further.Lam_pass_collectrecords nothing about the value of a variable bound by a mutablelet.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_variablesgives the variables someLassignassigns, asLambda_traverse.assigned_variablesdoes on master.refine_let,Lam_beta_reduce.no_names_beta_reduceandLam_pass_count.collect_occurstake~assignable.Lam_pass_lets_dceandLam_pass_deep_flattencompute it from the term they rewrite.Lam_stats.thas anassignablefield, as on master.simplify_aliassets it; elsewhere it is empty, so inlining inLam_compilebehaves as before.Lam_beta_reduce.propagate_beta_reduceandpropagate_beta_reduce_with_mapbind 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 inreadBeforeAssignabove isn't inlined (master givesreturn p + r | 0;), but the result is the same.Within a module nothing changes, since no
Lassignexists beforeLam_pass_lets_dce: no checked-in output intests/testsother than the new tests' changes.Tests
tests/tests/src/inline_assign_cross_module_test.resandinline_assign_cross_module_lib.resare the same as on master. Their.mjsdiffers 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.mlchecks 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.