Skip to content

Handle more scenarios for loop cloning - #67930

Merged
kunalspathak merged 7 commits into
dotnet:mainfrom
kunalspathak:loop_clone
Apr 26, 2022
Merged

Handle more scenarios for loop cloning#67930
kunalspathak merged 7 commits into
dotnet:mainfrom
kunalspathak:loop_clone

Conversation

@kunalspathak

@kunalspathakkunalspathak commented Apr 12, 2022

Copy link
Copy Markdown
Contributor
  • Increasing loops that are incremented by more than 1 like "i += 2"

TypeHashingAlgorithm.cs

for(inti=startIndex;i<src.Length;i+=2)
{
_hash1=(_hash1+_rotl(_hash1,5))^src[i];
if((i+1)<src.Length)
_hash2=(_hash2+_rotl(_hash2,5))^src[i+1];
}

  • Decreasing loops like "i--", "i -= 2", etc.

internalvirtualintLastIndexOf(T[]array,Tvalue,intstartIndex,intcount)
{
intendIndex=startIndex-count+1;
for(inti=startIndex;i>=endIndex;i--)
{
if(Equals(array[i],value))
{
returni;
}
}
return-1;
}
#endif
}

PriorityQueue's Heapify method.

for(intindex=lastParentWithChildren;index>=0;--index)
{
MoveDownDefaultComparer(nodes[index],index);
}

Contributes to #65342

@ghostghost added the area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI label Apr 12, 2022
@ghost

Copy link
Copy Markdown

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

Issue Details
  • Increasing loops that are incremented by more than 1 like "i += 2"
  • Decreasing loops like "i--", "i -= 2", etc.
Author:kunalspathak
Assignees:kunalspathak
Labels:

area-CodeGen-coreclr

Milestone:-

@kunalspathak

Copy link
Copy Markdown
ContributorAuthor

Failures are related to #67878

@kunalspathak
kunalspathak marked this pull request as ready for review April 13, 2022 22:27
@kunalspathak

kunalspathak commented Apr 13, 2022

Copy link
Copy Markdown
ContributorAuthor

@dotnet/jit-contrib , @BruceForstall

@BruceForstall

Copy link
Copy Markdown
Contributor

You need to worry about increment/decrement overflowing a 32-bit integer and wrapping. E.g.:

public static int test_up_big(int[] a, int s)
{
int r = 0;
int i;
for (i = 1; i < s; i += 2147483647)
{
r += a[i];
}
return r;
}

with your code will clone, but generate an access violation, not System.IndexOutOfRangeException.

In the case of for ( ; i < x; i += c), c needs to be strictly less than int.MaxValue - (Array.MaxLength - 1) + 1, which is 0x7fffffff - 0x7fffffc7 + 2 = 0x3a = 58. The idea being that if i was the maximum allowed legal array index of 0x7fffffc6 and you added 0x3a, the result is negative (-2,147,483,648).

Similar applies to down counted loops.

@kunalspathak

Copy link
Copy Markdown
ContributorAuthor

public static int test_up_big(int[] a, int s)
{
int r = 0;
int i;
for (i = 1; i < s; i += 2147483647)
{
r += a[i];
}
return r;
}

Thanks for pointing that out. Let me try to fix that.

@kunalspathak

Copy link
Copy Markdown
ContributorAuthor

@BruceForstall - I tried measuring some of the benchmarks that showed up in asmdiffs and they all show improvements.

Fasterbase/diffBase Median (ns)Diff Median (ns)Modality
BilinearTest.Interpol_Scalar1.2312591.2110245.69
BilinearTest.Interpol_Vector1.0827857.1625908.72
BenchmarksGame.FannkuchRedux_5.RunBench(n: 10, expectedSum: 38)1.0616428450.0015492476.47
BenchmarksGame.ReverseComplement_1.RunBench1.131021397.94903558.01
ByteMark.BenchEmFloatClass1.05771413700.00734018150.00
ByteMark.BenchEmFloat1.033737632200.003641513150.00
System.Collections.Tests.Perf_PriorityQueue<String, String>.HeapSort(Size: 100)10.80898710.0783248.11
System.Collections.Tests.Perf_PriorityQueue<String, String>.HeapSort(Size: 10)10.4439025.893739.26
System.Collections.Tests.Perf_PriorityQueue<String, String>.HeapSort(Size: 1000)10.3414949560.001445691.48
System.Collections.Tests.Perf_PriorityQueue<Int32, Int32>.HeapSort(Size: 10)1.04203.82195.96
Benchstone.BenchI.HeapSort.Test1.03294538.92287004.51
Slowerdiff/baseBase Median (ns)Diff Median (ns)Modality
System.Collections.Tests.Perf_PriorityQueue<Guid, Guid>.HeapSort(Size: 100)1.029744.889900.55
System.Collections.Tests.Perf_PriorityQueue<Guid, Guid>.HeapSort(Size: 10)1.01396.27401.45

Here is an example asmdiff from Heapify:

image

@kunalspathak

Copy link
Copy Markdown
ContributorAuthor

ping.

@BruceForstall
BruceForstall self-requested a review April 22, 2022 21:55
Comment threadsrc/coreclr/jit/loopcloning.cpp Outdated
Comment threadsrc/coreclr/jit/loopcloning.cpp Outdated
Comment threadsrc/coreclr/jit/loopcloning.cpp Outdated
Comment threadsrc/coreclr/jit/compiler.hpp Outdated
Comment threadsrc/coreclr/jit/loopcloning.cpp Outdated
Comment threadsrc/coreclr/jit/loopcloning.cpp Outdated
Comment threadsrc/coreclr/jit/loopcloning.cpp Outdated
@ghostghost added needs-author-action An issue or pull request that requires more info or actions from the author. and removed needs-author-action An issue or pull request that requires more info or actions from the author. labels Apr 22, 2022

@BruceForstallBruceForstall left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM.

I think you should run a jitstress job before merging.

@kunalspathak

Copy link
Copy Markdown
ContributorAuthor

/azp run runtime-coreclr jitstress

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@kunalspathak
kunalspathak merged commit 1bc09e7 into dotnet:mainApr 26, 2022
@kunalspathak
kunalspathak deleted the loop_clone branch April 26, 2022 15:58
@kunalspathak

kunalspathak commented May 5, 2022

Copy link
Copy Markdown
ContributorAuthor

Improvements on windows/x64: dotnet/perf-autofiling-issues#5072
Improvements on windows/arm64: dotnet/perf-autofiling-issues#5145
Improvements on windows/x64: dotnet/perf-autofiling-issues#5114 (Reverse complement)
Improvements on Ubuntu/x64: dotnet/perf-autofiling-issues#5092
Improvements on Windows/x86: dotnet/perf-autofiling-issues#5028

@ghostghost locked as resolved and limited conversation to collaborators Jun 4, 2022
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclrCLR 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.

2 participants

@kunalspathak@BruceForstall