Skip to content

Conversation

@EgorBo
Copy link
Member

@EgorBo EgorBo commented Oct 28, 2020

JIT is able to unroll small constant-size loops via optUnrollLoops, but currently it only works if Vector_.Count is used (magic) as the upper limit. This PR extends that behavior to also handle single iteration loops (thus it won't regress anything)

static int GetCount() => 1; // e.g. static readonly field inited via an envvar

static int Test()
{
    int x = 0;
    for (int i = 0; i < GetCount(); i++)
        x++;
    return x;
}

Current codegen for Test:

G_M58954_IG01:
G_M58954_IG02:
       xor      eax, eax
       xor      edx, edx
G_M58954_IG03:
       inc      eax
       inc      edx
       test     edx, edx
       jle      SHORT G_M58954_IG03
G_M58954_IG04:
       ret

New codegen for Test:

G_M58954_IG02:
       mov      eax, 1
G_M58954_IG03:
       ret  

jit-diff (-f --cctors --pmi):

(surprisingly, it's not empty)

Total bytes of delta: -352 (-0.00% of base)
    diff is an improvement.

Top file improvements (bytes):
        -211 : System.Private.CoreLib.dasm (-0.00% of base)
        -103 : Microsoft.CodeAnalysis.dasm (-0.01% of base)
         -38 : Microsoft.Diagnostics.Tracing.TraceEvent.dasm (-0.00% of base)

3 total files with Code Size differences (3 improved, 0 regressed), 265 unchanged.

Top method improvements (bytes):
        -103 (-12.50% of base) : Microsoft.CodeAnalysis.dasm - SmallDictionary`2:LeftComplex(AvlNode):AvlNode (8 base, 7 diff methods)
         -66 (-12.69% of base) : System.Private.CoreLib.dasm - Vector64`1:Equals(Vector64`1):bool:this (6 methods)
         -66 (-11.21% of base) : System.Private.CoreLib.dasm - Vector64`1:GetHashCode():int:this (6 methods)
         -62 (-4.52% of base) : System.Private.CoreLib.dasm - Utf8Formatter:TryFormat(TimeSpan,Span`1,byref,StandardFormat):bool
         -17 (-5.28% of base) : System.Private.CoreLib.dasm - Utf8Parser:TryParseByteX(ReadOnlySpan`1,byref,byref):bool
         -14 (-24.56% of base) : Microsoft.Diagnostics.Tracing.TraceEvent.dasm - FileVersionTraceData:get_FileDescription():String:this
         -13 (-1.53% of base) : Microsoft.Diagnostics.Tracing.TraceEvent.dasm - FileVersionTraceData:ToXml(StringBuilder):StringBuilder:this
         -11 (-1.75% of base) : Microsoft.Diagnostics.Tracing.TraceEvent.dasm - FileVersionTraceData:PayloadValue(int):Object:this

Top method improvements (percentages):
         -14 (-24.56% of base) : Microsoft.Diagnostics.Tracing.TraceEvent.dasm - FileVersionTraceData:get_FileDescription():String:this
         -66 (-12.69% of base) : System.Private.CoreLib.dasm - Vector64`1:Equals(Vector64`1):bool:this (6 methods)
        -103 (-12.50% of base) : Microsoft.CodeAnalysis.dasm - SmallDictionary`2:LeftComplex(AvlNode):AvlNode (8 base, 7 diff methods)
         -66 (-11.21% of base) : System.Private.CoreLib.dasm - Vector64`1:GetHashCode():int:this (6 methods)
         -17 (-5.28% of base) : System.Private.CoreLib.dasm - Utf8Parser:TryParseByteX(ReadOnlySpan`1,byref,byref):bool
         -62 (-4.52% of base) : System.Private.CoreLib.dasm - Utf8Formatter:TryFormat(TimeSpan,Span`1,byref,StandardFormat):bool
         -11 (-1.75% of base) : Microsoft.Diagnostics.Tracing.TraceEvent.dasm - FileVersionTraceData:PayloadValue(int):Object:this
         -13 (-1.53% of base) : Microsoft.Diagnostics.Tracing.TraceEvent.dasm - FileVersionTraceData:ToXml(StringBuilder):StringBuilder:this

8 total methods with Code Size differences (8 improved, 0 regressed), 259114 unchanged.
Completed analysis in 23.84

Example: Utf8Parser:TryParseByteX (ByteOverflowLengthHex = 2) - looks suspicious btw:

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Oct 28, 2020
@EgorBo
Copy link
Member Author

EgorBo commented Nov 5, 2020

@dotnet/jit-contrib PTAL

@AndyAyersMS
Copy link
Member

cc @BruceForstall

Copy link
Contributor

@BruceForstall BruceForstall left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question

Copy link
Contributor

@sandreenko sandreenko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you.

@sandreenko sandreenko merged commit 51393be into dotnet:master Nov 12, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 12, 2020
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.

5 participants