Loop is not cloned for partial array iteration

Open
#118,800 2 comments 0 reactions 1 assignee View on GitHub

@EgorBo is already working on this.

Since Aug 15, 2025.

Assessment

This issue has not been assessed yet.

Description

area-CodeGen-coreclr
static int SumARange1(int[] array, int offset, int count)
{
    int sum = 0;
    for (int i = offset; i < offset + count; i++)
        sum += array[i];
    return sum;
}

Current codegen:

; Method Test_DevDiv_255294:SumARange1(int[],int,int):int (FullOpts)
       sub      rsp, 40
       xor      eax, eax
       mov      r10d, edx
       add      r8d, r10d
       cmp      r8d, edx
       jle      SHORT G_M48878_IG05
       mov      edx, dword ptr [rcx+0x08]
       align    [12 bytes for IG04]
G_M48878_IG04:
       cmp      r10d, edx
       jae      SHORT G_M48878_IG06
       add      eax, dword ptr [rcx+4*r10+0x10]
       inc      r10d
       cmp      r8d, r10d
       jg       SHORT G_M48878_IG04
G_M48878_IG05:
       add      rsp, 40
       ret      
G_M48878_IG06:
       call     CORINFO_HELP_RNGCHKFAIL
       int3     
; Total bytes of code: 61

While it's not legal to drop the bounds check here, JIT should be able to handle it via loop cloning.

Dominant language
C#
Stars
18.3k
Forks
5.6k
PR merge metrics
PR metrics pending

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.