Skip to content

JIT: optimize away range check in common slice patterns #9425

Closed

Description

@AndyAyersMS

Here's an example similar to #9421 that came up in some span conversion efforts in CoreFX:

    static ReadOnlySpan<char> Slice(ReadOnlySpan<char> s)
    {
        ReadOnlySpan<char> result = s.Slice(s.Length & ~3);
        return result;
    }

Current asm:

G_M7513_IG01:
       4883EC28             sub      rsp, 40

G_M7513_IG02:
       488B02               mov      rax, bword ptr [rdx]
       8B5208               mov      edx, dword ptr [rdx+8]
       448BC2               mov      r8d, edx
       4183E0FC             and      r8d, -4
       443BC2               cmp      r8d, edx
       7718                 ja       SHORT G_M7513_IG05

G_M7513_IG03:
       412BD0               sub      edx, r8d
       4D63C0               movsxd   r8, r8d
       4A8D0440             lea      rax, bword ptr [rax+2*r8]
       488901               mov      bword ptr [rcx], rax
       895108               mov      dword ptr [rcx+8], edx
       488BC1               mov      rax, rcx

G_M7513_IG04:
       4883C428             add      rsp, 40
       C3                   ret

G_M7513_IG05:
       E89DF34A5E           call     ThrowHelper:ThrowArgumentOutOfRangeException()
       CC                   int3

Slicing a span with lengths specified by suitable functions of the span length should allow the range check to be optimized away (suitable meaning mods, &, guarded subtracts, etc). This should be a very common case as presumably users are almost always looking at the original length when figuring out slices and out of bounds slicing will be rare.

Or, for an even more blatant example:

    static ReadOnlySpan<char> Slice(ReadOnlySpan<char> s)
    {
        ReadOnlySpan<char> result = s.Slice(s.Length);
        return result;
    }

current asm is kinda sad:

G_M7513_IG01:
       4883EC28             sub      rsp, 40

G_M7513_IG02:
       488B02               mov      rax, bword ptr [rdx]
       8B5208               mov      edx, dword ptr [rdx+8]
       448BC2               mov      r8d, edx
       413BD0               cmp      edx, r8d
       771B                 ja       SHORT G_M7513_IG05

G_M7513_IG03:
       418BD0               mov      edx, r8d
       412BD0               sub      edx, r8d
       4D63C0               movsxd   r8, r8d
       4A8D0440             lea      rax, bword ptr [rax+2*r8]
       488901               mov      bword ptr [rcx], rax
       895108               mov      dword ptr [rcx+8], edx
       488BC1               mov      rax, rcx

G_M7513_IG04:
       4883C428             add      rsp, 40
       C3                   ret

G_M7513_IG05:
       E89EF33C5F           call     ThrowHelper:ThrowArgumentOutOfRangeException()
       CC                   int3

cc @dotnet/jit-contrib

Activity

  1. AndyAyersMS commented on Dec 12, 2017

    @AndyAyersMS
    MemberAuthor

    Guessing that part of the problem is that struct promotion doesn't clear the GTF_GLOBAL_REF flags in ancestor trees, leaving us with things like:

    Replacing the field in promoted struct with a local var:
                   [000021] ------------              /--*  LCL_VAR   int    V10 tmp8         
                   [000071] -A--G-------              *  ASG       int   
                   [000070] D------N----              \--*  LCL_VAR   int    V02 loc0         
    
    Replacing the field in promoted struct with a local var:
                   [000029] ----G-------              *  JTRUE     void  
                   [000027] ------------              |  /--*  LCL_VAR   int    V10 tmp8         
                   [000028] N---G----U--              \--*  LE        int   
                   [000024] ------------                 \--*  LCL_VAR   int    V02 loc0         
    
  2. AndyAyersMS commented on Dec 13, 2017

    @AndyAyersMS
    MemberAuthor

    Looks like we can optimize even when those flags are set. Here are some possibly useful changes in value numbering: master...AndyAyersMS:ValueNumCompareSubIdentities

    Leads to a small handful of good diffs in jit-diffs. Won't have time to push these further now as I'm off on vacation for a few weeks.

    cc @briansull

  3. briansull commented on Jan 3, 2018

    @briansull
    Contributor

    @AndyAyersMS The additional ValueNumbering optimizations mentioned, look good.

  4. transferred this issue fromdotnet/coreclron Jan 31, 2020
  5. added this to the Future milestone on Jan 31, 2020
  6. ghost locked as resolved and limited conversation to collaborators on Dec 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-CodeGen-coreclrenhancementoptimizationtenet-performance

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions