Skip to content

JIT: (bug) Loop cloning drops bounds checks when the limit local is redefined between the zero-trip guard and the loop preheader #133762

Closed

Description

@EgorBo

Minimal repro

using System;
using System.Runtime.CompilerServices;

public class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Test(int[] a, int i, int n)
    {
        int sum = 0;
        if (i < n)
        {
            n = a.Length;
            do
            {
                sum += a[i];
                i++;
            } while (i < n);
        }
        return sum;
    }

    public static int Main()
    {
        int[] a = new int[10];
        try
        {
            int r = Test(a, 20, 30);
            Console.WriteLine($"Expected: IndexOutOfRangeException / Actual: returned {r}");
            return 1;
        }
        catch (IndexOutOfRangeException)
        {
            Console.WriteLine("Expected: IndexOutOfRangeException / Actual: IndexOutOfRangeException");
            return 100;
        }
    }
}

Run: corerun repro.dll (Checked or Release JIT, .NET 11 main @ b44cd90)

Expected

a[20] on a 10-element array throws IndexOutOfRangeException; exit code 100.

Actual

No exception

Notes

No JIT knobs needed. DOTNET_JitCloneLoops=0 restores the exception. FlowGraphNaturalLoop::HasZeroTripTest (flowgraph.cpp:6629) accepts the i < n guard without checking that n is not redefined between the guard and the preheader, so NeedsZeroTripGuard stays false and no entry guard is cloned (loopcloning.cpp:1346).

Activity

  1. added this to the 12.0.0 milestone on Sep 11, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 11, 2026
  3. dotnet-policy-service commented on Sep 11, 2026

    @dotnet-policy-service
    Contributor

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

  4. self-assigned this
    on Sep 11, 2026
  5. AndyAyersMS commented on Sep 11, 2026

    @AndyAyersMS
    Member

    Thought this was a cloning bug, but it looks like the regression was introduced by #119537, which allowed zero-trip detection to walk unique predecessors without invalidating an earlier guard when the iterator or limit is redefined along that path

  6. jakobbotsch commented on Sep 12, 2026

    @jakobbotsch
    Member

    Yeah, MatchLimit takes care to validate that the limit is invariant inside the loop, but similar care needs to be taken when we try to validate the zero trip test since there we are walking outside the loop.

  7. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 11.0.100-rc.2.26467.112
  8. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    @jakobbotsch would you consider backporting the fix from #133808 to release/11.0?

    It currently only exists in main (12.0). The repro still fails on the shipped 11.0 RC2 SDK (11.0.100-rc.2.26467.112), while 8.0.425, 9.0.318 and 10.0.401 all behave correctly, so this is a regression introduced during the 11.0 cycle rather than a long-standing bug — 11.0 would ship with it.

    Note: this comment was produced by an AI agent; the repro was re-run against all four released SDKs to confirm the results above.

  9. locked and limited conversation to collaborators on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions