Skip to content

JIT: (bug) OSR patchpoint is not placed for a loop whose backedge-source block has a non-empty IL stack (e.g. while (i < (flag ? a : b))), so the loop stays in Tier0 (~100x slower) #135818

Open

Description

@EgorBo

When the backedge-source block has a non-empty IL stack, the importer should fall back to placing the patchpoint at the backedge target, but the fallback is guarded by an inverted condition and never fires. Such loops get no patchpoint and run in (instrumented) Tier0 for their whole lifetime.

Minimal Repro

using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;

class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static long Test(bool flag, int a, int b)
    {
        long sum = 0;
        int i = 0;
        while (i < (flag ? a : b))
        {
            sum += i;
            i++;
        }
        return sum;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static long Control(int a)
    {
        long sum = 0;
        int i = 0;
        while (i < a)
        {
            sum += i;
            i++;
        }
        return sum;
    }

    public static int Main()
    {
        var sw = Stopwatch.StartNew();
        Control(1_000_000_000);
        Console.WriteLine("Control: " + sw.ElapsedMilliseconds + " ms");
        sw.Restart();
        Test(true, 1_000_000_000, 0);
        Console.WriteLine("Test:    " + sw.ElapsedMilliseconds + " ms");
        return 100;
    }
}

Default tiering/OSR settings.

Expected

Test gets a Tier1-OSR version like Control; both loops take about the same time.

Actual

Control: 188 ms
Test:    22788 ms     (20512 ms in another run)

DOTNET_JitDisasmSummary=1 lists Control with Tier1-OSR @0x10, but Test only as Instrumented Tier0; the JitDump of Test shows -- 0 patchpoints transformed.

Regression?

Also reproduces on .NET 10 (no Tier1-OSR for Test); not re-verified.

Notes

  • impImportBlockCode (importer.cpp, default TC_PatchpointStrategy, mustUseTargetPatchpoint path) has if (succBlock->HasFlag(BBF_OSR_PATCHPOINT)) guarding code that sets BBF_OSR_PATCHPOINT; the comment above it says "If not, add one", so the condition should be !HasFlag.
  • As written, the branch can only set an already-set flag. The loop head has bbRefs == 1, so no other path places a patchpoint there.

Activity

  1. added this to the 12.0.0 milestone on Oct 14, 2026
  2. added
    area-CodeGen-coreclr
    on Oct 14, 2026
  3. dotnet-policy-service commented on Oct 14, 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 Oct 15, 2026
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