Skip to content

Loop Unrolling is not Enabled in Release Build #41063

@dogac00

Description

@dogac00

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

No one assigned

    Labels

    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMItenet-performancePerformance related issue

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions