Skip to content

JIT: (bug) Loop unrolling takes the constant IV init from an exception filter, ignoring a finally that runs between the filter and the catch #137683

Closed

Description

@EgorBo

FlowGraphNaturalLoop::FindConstInit (and HasZeroTripTest) walk GetUniquePred backwards from the loop preheader. For a loop in a catch handler the unique predecessor of the handler entry is the filter's BBJ_EHFILTERRET block, so a constant stored in the filter is used as the loop's initial IV value. The second-pass finally that runs between the filter and the handler is not visible on that edge, so the loop is fully unrolled with the wrong init value: wrong code in plain safe C#.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

class Program
{
    static int s_sum;

    static void Main()
    {
        Test(1);
        Console.WriteLine(s_sum);
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static void MayThrow(int k)
    {
        if (k != 0) throw new Exception();
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static bool Yes(int x) => x == 0;

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static void Test(int k)
    {
        int i = 100;
        try
        {
            try
            {
                MayThrow(k);
            }
            finally
            {
                i = 10;
            }
        }
        catch when (Yes(i = 0))
        {
            do
            {
                s_sum += i;
                i++;
            } while (i < 3);
        }
    }
}

Expected

10: the filter runs first (i = 0), then the inner finally runs during the second pass (i = 10), then the catch loop runs once with i = 10. This is also what a Checked JIT prints with DOTNET_JitNoUnroll=1.

Actual

3 on Checked and Release JIT (win-x64, main a5d7014cefc). The catch loop is fully unrolled as 0+1+2, using the init 0 from the filter. JitDump of the Checked JIT (Unroll loops): Const init with value 0, Computed loop repetition count (number of test block executions) to be 3.

Regression?

Yes. Same repro, default settings:

Runtime Result
.NET 8 (8.0.34) OK (10)
.NET 9 (9.0.23) OK
.NET 10 (10.0.15) OK
.NET 11 (11.0.0) / main (a5d7014cefc) 3

Notes

  • FindConstInit / HasZeroTripTest (flowgraph.cpp) walk GetUniquePred backwards from the loop preheader; from the catch entry the unique predecessor is the filter's BBJ_EHFILTERRET, so the filter's i = 0 is taken as the init.
  • The real control flow goes through the second-pass finally, which has no flow edge, so facts from the filter are not valid at handler entry.
  • The same walk is also wrong when it crosses the entry of a finally that can be entered both normally and exceptionally. If such a finally is not cloned (for example because it contains a dense switch, which fgCloneFinally refuses) it has a single explicit flow predecessor, the normal-path BBJ_CALLFINALLY, and a constant assigned in the try after the last throwing call is used as the init of a loop in the finally even when the finally runs after an exception while the variable still has its old value (observed: 3 instead of 10). A fix has to cover both entries.
  • Possible fix: stop the GetUniquePred walk at EH handler / filter entry blocks.
  • Different from JIT: (bug) Loop cloning trusts a stale zero-trip guard after the IV is redefined #134007 / JIT: (bug) Loop cloning drops bounds checks when the limit local is redefined between the zero-trip guard and the loop preheader #133762 (stale zero-trip guard after the IV or limit is redefined on a normal path).

Activity

  1. added this to the 12.0.0 milestone on Nov 16, 2026
  2. added
    area-CodeGen-coreclr
    on Nov 16, 2026
  3. dotnet-policy-service commented on Nov 16, 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 Nov 17, 2026
  5. added a commit that references this issue on Nov 19, 2026
    094fa6a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclr

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions