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

JIT: Avoid cloning cold descendant loops quadratically #96580

Closed
wants to merge 1 commit into from

Conversation

jakobbotsch
Copy link
Member

@jakobbotsch jakobbotsch commented Jan 6, 2024

When we have a loop nest where we clone multiple ancestor loops we would clone descendant loops every time, ending up with a total quadratic number of cloned loops in terms of the depth of the loop nest.

Instead of producing these multiple versions of cold loops (each of which is optimized for a particular hot ancestor loop) we can reuse the first cold loop we cloned. This has the effect of exiting all the "hot" versions of ancestor loops if the cloning conditions of a descendant loop fails, but it gets rid of the quadratic loop cloning problem.

Size improvements expected, with a few regressions where RBO was able to fully optimize away some cold loop nests before, and now cannot because they are entered in new, strange ways.

Example:

[MethodImpl(MethodImplOptions.NoInlining)]
private static long Foo(int[] a, int[] b, int[] c, int[] d, int[] e, int n)
{
    long sum = 0;
    for (int i = 0; i < n; i++)
    {
        sum += a[i];
        for (int j = 0; j < n; j++)
        {
            sum += b[j];
            for (int k = 0; k < n; k++)
            {
                sum += c[k];
                for (int l = 0; l < n; l++)
                {
                    sum += d[l];
                    for (int m = 0; m < n; m++)
                    {
                        sum += e[m];
                    }
                }
            }
        }
    }

    return sum;
}

Flow graph before; a total of 20 loops after cloning has run.
Flow graph after; a total of 5 natural loops after cloning has run, and 5 more unnatural ones.

Assembly diff:

-; Total bytes of code 756, prolog size 27, PerfScore 11557.63, instruction count 234, allocated bytes for code 756 (MethodHash=9f186a76) for method Program:Foo(int[],int[],int[],int[],int[],int):long (FullOpts)
+; Total bytes of code 423, prolog size 27, PerfScore 11001.65, instruction count 134, allocated bytes for code 423 (MethodHash=9f186a76) for method Program:Foo(int[],int[],int[],int[],int[],int):long (FullOpts)

(Easiest to review with whitespace disabled.)

Addresses part of #8558

When we have a loop nest where we clone multiple ancestor loops we would
clone descendant loops, ending up with a total quadratic number of
cloned loops in terms of the depth of the loop nest.

Instead of producing these multiple versions of cold loops (each of
which is optimized for a particular hot ancestor loop) we can reuse the
first cold loop we cloned. This has the effect of exiting all the "hot"
versions of ancestor loops if the cloning conditions fail, but it gets
rid of the quadratic loop cloning problem.
@ghost ghost assigned jakobbotsch Jan 6, 2024
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Jan 6, 2024
@ghost
Copy link

ghost commented Jan 6, 2024

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.

Issue Details

When we have a loop nest where we clone multiple ancestor loops we would clone descendant loops, ending up with a total quadratic number of cloned loops in terms of the depth of the loop nest.

Instead of producing these multiple versions of cold loops (each of which is optimized for a particular hot ancestor loop) we can reuse the first cold loop we cloned. This has the effect of exiting all the "hot" versions of ancestor loops if the cloning conditions fail, but it gets rid of the quadratic loop cloning problem.

Size improvements expected, with a few regressions where RBO was able to fully optimize away some cold loop nests before, and now cannot because they are entered in new, strange ways.

Example:

[MethodImpl(MethodImplOptions.NoInlining)]
private static long Foo(int[] a, int[] b, int[] c, int[] d, int[] e, int n)
{
    long sum = 0;
    for (int i = 0; i < n; i++)
    {
        sum += a[i];
        for (int j = 0; j < n; j++)
        {
            sum += b[j];
            for (int k = 0; k < n; k++)
            {
                sum += c[k];
                for (int l = 0; l < n; l++)
                {
                    sum += d[l];
                    for (int m = 0; m < n; m++)
                    {
                        sum += e[m];
                    }
                }
            }
        }
    }

    return sum;
}

Flow graph before; a total of 20 loops after cloning has run.
Flow graph after; a total of 5 natural loops after cloning has run, and 5 more unnatural ones.

Assembly diff:

-; Total bytes of code 756, prolog size 27, PerfScore 11557.63, instruction count 234, allocated bytes for code 756 (MethodHash=9f186a76) for method Program:Foo(int[],int[],int[],int[],int[],int):long (FullOpts)
+; Total bytes of code 423, prolog size 27, PerfScore 11001.65, instruction count 134, allocated bytes for code 423 (MethodHash=9f186a76) for method Program:Foo(int[],int[],int[],int[],int[],int):long (FullOpts)
Author: jakobbotsch
Assignees: jakobbotsch
Labels:

area-CodeGen-coreclr

Milestone: -

@ghost ghost closed this Feb 5, 2024
@ghost
Copy link

ghost commented Feb 5, 2024

Draft Pull Request was automatically closed for 30 days of inactivity. Please let us know if you'd like to reopen it.

@github-actions github-actions bot locked and limited conversation to collaborators Mar 7, 2024
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant