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: Add reasoning about loop trip counts and optimize counted loops into downwards counted loops #102261

Merged
merged 21 commits into from
May 30, 2024

Commits on May 15, 2024

  1. JIT: Add reasoning about loop trip counts and optimize counted loops

    This builds out some initial reasoning about trip counts of loops and
    utilizes it to convert upwards counted loops into downwards counted
    loops when beneficial.
    
    The trip count of a loop is defined to be the number of times the header
    block is entered from a back edge. When this value can be computed the
    loop is called counted. The computation here is symbolic and can reason
    in terms of variables, such as array or span lengths.
    
    To be able to compute the trip count requires the JIT to reason about
    overflow and to prove various conditions related to the start and end
    values of the loop. For example, a loop `for (int i = 0; i <= n; i++)`
    only has a determinable trip count if we can prove that `n <
    int.MaxValue`. The implementation here utilizes the logic provided by
    RBO to prove these conditions. In many cases we aren't able to prove
    them and thus must give up, but this should be improvable in an
    incremental fashion to handle common cases.
    
    Converting a counted loop to a downwards counting loop is beneficial if
    the index is not being used for anything else but the loop test. In
    those cases our target platforms are able to combine the decrement with
    the exit test into a single instruction.
    
    This transformation does not have that many hits (as you may imagine,
    the indices of loops are usually used for something else). However, once
    strength reduction is implemented we expect that this transformation
    will be significantly more important since strength reduction in many
    cases is going to remove all uses of an index except the mutation and
    the loop test.
    
    The reasoning about trip counts is itself also needed by strength
    reduction which also needs it to prove no overflow in various cases.
    
    Example:
    
    ```csharp
    private static int Foo(int[] arr, int start, int count)
    {
        int sum = 0;
        for (int i = 0; i < count; i++)
        {
            sum += arr[start++];
        }
    
        return sum;
    }
    ```
    
    ```diff
    @@ -1,20 +1,18 @@
     G_M42127_IG02:  ;; offset=0x0004
            xor      eax, eax
    -       xor      r10d, r10d
            test     r8d, r8d
            jle      SHORT G_M42127_IG05
    -						;; size=10 bbWeight=1 PerfScore 1.75
    -G_M42127_IG03:  ;; offset=0x000E
    -       mov      r9d, dword ptr [rcx+0x08]
    +						;; size=7 bbWeight=1 PerfScore 1.50
    +G_M42127_IG03:  ;; offset=0x000B
    +       mov      r10d, dword ptr [rcx+0x08]
     						;; size=4 bbWeight=0.25 PerfScore 0.50
    -G_M42127_IG04:  ;; offset=0x0012
    -       lea      r9d, [rdx+0x01]
    +G_M42127_IG04:  ;; offset=0x000F
    +       lea      r10d, [rdx+0x01]
            cmp      edx, dword ptr [rcx+0x08]
            jae      SHORT G_M42127_IG06
            mov      edx, edx
            add      eax, dword ptr [rcx+4*rdx+0x10]
    -       inc      r10d
    -       cmp      r10d, r8d
    -       mov      edx, r9d
    -       jl       SHORT G_M42127_IG04
    -						;; size=26 bbWeight=4 PerfScore 38.00
    +       dec      r8d
    +       mov      edx, r10d
    +       jne      SHORT G_M42127_IG04
    +						;; size=23 bbWeight=4 PerfScore 37.00
    ```
    
    Fix dotnet#100915
    jakobbotsch committed May 15, 2024
    Configuration menu
    Copy the full SHA
    13579f3 View commit details
    Browse the repository at this point in the history
  2. Add function header

    jakobbotsch committed May 15, 2024
    Configuration menu
    Copy the full SHA
    2bd7cbb View commit details
    Browse the repository at this point in the history
  3. Switch terminology

    jakobbotsch committed May 15, 2024
    Configuration menu
    Copy the full SHA
    c59de70 View commit details
    Browse the repository at this point in the history
  4. Fix build

    jakobbotsch committed May 15, 2024
    Configuration menu
    Copy the full SHA
    19bf7e2 View commit details
    Browse the repository at this point in the history
  5. Fix linux build

    jakobbotsch committed May 15, 2024
    Configuration menu
    Copy the full SHA
    85241f7 View commit details
    Browse the repository at this point in the history
  6. More clang fixes

    jakobbotsch committed May 15, 2024
    Configuration menu
    Copy the full SHA
    a6fa41f View commit details
    Browse the repository at this point in the history
  7. Clean ups

    jakobbotsch committed May 15, 2024
    Configuration menu
    Copy the full SHA
    4626962 View commit details
    Browse the repository at this point in the history
  8. Configuration menu
    Copy the full SHA
    4062a77 View commit details
    Browse the repository at this point in the history

Commits on May 16, 2024

  1. Fix long constants on x86

    jakobbotsch committed May 16, 2024
    Configuration menu
    Copy the full SHA
    b782045 View commit details
    Browse the repository at this point in the history
  2. Fix a bug

    jakobbotsch committed May 16, 2024
    Configuration menu
    Copy the full SHA
    3c669c0 View commit details
    Browse the repository at this point in the history
  3. Add stress validation

    jakobbotsch committed May 16, 2024
    Configuration menu
    Copy the full SHA
    9e66cf7 View commit details
    Browse the repository at this point in the history
  4. Fix linux build

    jakobbotsch committed May 16, 2024
    Configuration menu
    Copy the full SHA
    a6738ea View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    e588b44 View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    5678a74 View commit details
    Browse the repository at this point in the history
  7. Configuration menu
    Copy the full SHA
    c71c39c View commit details
    Browse the repository at this point in the history

Commits on May 17, 2024

  1. Fix a bug; add a test

    jakobbotsch committed May 17, 2024
    Configuration menu
    Copy the full SHA
    370d098 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    d0f8fd2 View commit details
    Browse the repository at this point in the history

Commits on May 29, 2024

  1. Lots of metrics

    jakobbotsch committed May 29, 2024
    Configuration menu
    Copy the full SHA
    af98790 View commit details
    Browse the repository at this point in the history
  2. Revert "Lots of metrics"

    This reverts commit af98790.
    jakobbotsch committed May 29, 2024
    Configuration menu
    Copy the full SHA
    5275277 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    402571e View commit details
    Browse the repository at this point in the history
  4. Minor nits

    jakobbotsch committed May 29, 2024
    Configuration menu
    Copy the full SHA
    37f6085 View commit details
    Browse the repository at this point in the history