Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sys_core_fold: Remove problematic optimization #6642

Merged
merged 2 commits into from
Jan 10, 2023

Conversation

bjorng
Copy link
Contributor

@bjorng bjorng commented Jan 9, 2023

The optimization of nested lets was unsafe if the number of iterations for sys_core_fold ran out. There have been several attempts to fix that problem:

5076069 (#6624)
30d4c45 (#6631)
ff05bee (#6575)

It's time to cut the Gordian knot by removing the optimization. While at it, remove all optimizations of nested lets, not only the offending part of the optimization.

Running scripts/diffable before and after and diffing, shows changes in 40 modules. Most of them are neutral (the same instructions in a different order). There are few changes where an extra test_heap instruction has been introduced, for example in the compile module:

-    {test_heap,19,1}.
+    {test_heap,15,1}.
     {update_record,{atom,reuse},14,{y,0},{x,0},{list,[7,{x,0}]}}.
-    {put_tuple2,{x,0},{list,[{atom,ok},{y,1},{x,0}]}}.
     {try_end,{y,2}}.
+    {test_heap,4,1}.
+    {put_tuple2,{x,0},{list,[{atom,ok},{y,1},{x,0}]}}.
     {deallocate,3}.
     return.

which was compiled from this source code:

try
    Mod = cerl:concrete(cerl:module_name(Core)),
    {ok,Core,St#compile{module=Mod}}
catch
    _:_ ->
        {ok,Core,St}
end.

In the ct_run module, the following code:

case proplists:get_value(step, Misc) of
    undefined ->
        #opts{};
    StepOpts ->
        #opts{step = StepOpts}
end

is no longer optimized to:

StepOpts = proplists:get_value(step, Misc)
#opts{step = StepOpts}

There are also improvements in a few modules.

Closes #6633
Closes #6635

The optimization of nested lets was unsafe if the number
of iterations for `sys_core_fold` ran out. There have been
several attempts to fix that problem:

5076069 (erlang#6624)
30d4c45 (erlang#6631)
ff05bee (erlang#6575)

It's time to cut the Gordian knot by removing the optimization.
While at it, remove all optimizations of nested lets, not only
the offending part of the optimization.

Running `scripts/diffable` before and after and diffing, shows changes
in 40 modules. Most of them are neutral (the same instructions in a
different order). There are few changes where an extra `test_heap`
instruction has been introduced, for example in the `compile` module:

    -    {test_heap,19,1}.
    +    {test_heap,15,1}.
         {update_record,{atom,reuse},14,{y,0},{x,0},{list,[7,{x,0}]}}.
    -    {put_tuple2,{x,0},{list,[{atom,ok},{y,1},{x,0}]}}.
         {try_end,{y,2}}.
    +    {test_heap,4,1}.
    +    {put_tuple2,{x,0},{list,[{atom,ok},{y,1},{x,0}]}}.
         {deallocate,3}.
         return.

which was compiled from this source code:

    try
        Mod = cerl:concrete(cerl:module_name(Core)),
        {ok,Core,St#compile{module=Mod}}
    catch
        _:_ ->
            {ok,Core,St}
    end.

In the `ct_run` module, the following code:

    case proplists:get_value(step, Misc) of
        undefined ->
            #opts{};
        StepOpts ->
            #opts{step = StepOpts}
    end

is no longer optimized to:

    StepOpts = proplists:get_value(step, Misc)
    #opts{step = StepOpts}

There are also improvements in a few modules.

Closes erlang#6633
Closes erlang#6635
@bjorng bjorng added team:VM Assigned to OTP team VM fix labels Jan 9, 2023
@bjorng bjorng requested a review from jhogberg January 9, 2023 08:40
@bjorng bjorng self-assigned this Jan 9, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jan 9, 2023

CT Test Results

No tests were run for this PR. This is either because the build failed, or the PR is based on a branch without GH actions tests configured.

Results for commit 19aa6b1

To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass.

See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally.

Artifacts

// Erlang/OTP Github Action Bot

@bjorng bjorng added this to the OTP-26.0 milestone Jan 9, 2023
@bjorng bjorng added the testing currently being tested, tag is used by OTP internal CI label Jan 9, 2023
@bjorng bjorng force-pushed the bjorn/compiler/cutting-the-gordian-knot branch from 8cde394 to 19aa6b1 Compare January 9, 2023 10:56
@bjorng
Copy link
Contributor Author

bjorng commented Jan 9, 2023

I have included a commit authored by @jhogberg that enhances an existing optimization so that the extra test_heap instructions are avoided.

@bjorng bjorng force-pushed the bjorn/compiler/cutting-the-gordian-knot branch from 19aa6b1 to 22f0521 Compare January 10, 2023 12:04
@bjorng bjorng merged commit b1f11e4 into erlang:master Jan 10, 2023
@bjorng bjorng deleted the bjorn/compiler/cutting-the-gordian-knot branch January 10, 2023 12:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix team:VM Assigned to OTP team VM testing currently being tested, tag is used by OTP internal CI
Projects
None yet
2 participants