-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Closed
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMICLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMItenet-performancePerformance related issuePerformance related issue
Milestone
Description
I'm trying the following code in SharpLab. In GCC this loop is unrolled and there are no jump statements in generated assembly.
public class Sample {
public int SumOfFirstThreeElements(int [] arr) {
int sum = 0;
for (int i = 0; i < 3; i++)
sum += arr[i];
return sum;
}
}
// The generated IL is this:
.class private auto ansi '<Module>'
{
} // end of class <Module>
.class public auto ansi beforefieldinit Sample
extends [System.Private.CoreLib]System.Object
{
// Methods
.method public hidebysig
instance int32 SumOfFirstThreeElements (
int32[] arr
) cil managed
{
// Method begins at RVA 0x2050
// Code size 22 (0x16)
.maxstack 3
.locals init (
[0] int32 sum,
[1] int32 i
)
IL_0000: ldc.i4.0
IL_0001: stloc.0
IL_0002: ldc.i4.0
IL_0003: stloc.1
// sequence point: hidden
IL_0004: br.s IL_0010
// loop start (head: IL_0010)
IL_0006: ldloc.0
IL_0007: ldarg.1
IL_0008: ldloc.1
IL_0009: ldelem.i4
IL_000a: add
IL_000b: stloc.0
IL_000c: ldloc.1
IL_000d: ldc.i4.1
IL_000e: add
IL_000f: stloc.1
IL_0010: ldloc.1
IL_0011: ldc.i4.3
IL_0012: blt.s IL_0006
// end loop
IL_0014: ldloc.0
IL_0015: ret
} // end of method Sample::SumOfFirstThreeElements
.method public hidebysig specialname rtspecialname
instance void .ctor () cil managed
{
// Method begins at RVA 0x2072
// Code size 7 (0x7)
.maxstack 8
IL_0000: ldarg.0
IL_0001: call instance void [System.Private.CoreLib]System.Object::.ctor()
IL_0006: ret
} // end of method Sample::.ctor
} // end of class Sample
As we can see a loop code is generated with jump statements. I think the performance would be better if the loop was unrolled.
SharpLab link.
Thanks.
Metadata
Metadata
Assignees
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMICLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMItenet-performancePerformance related issuePerformance related issue