Skip to content

JIT: (bug) Tail duplication in fgUpdateFlowGraph does not terminate on a conditional cycle #134008

Closed

Description

@EgorBo

fgUpdateFlowGraph can endlessly rotate one unconditional edge around a cycle of simple conditional blocks. The JIT hangs in "Optimize control flow" and allocates memory without bound.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

public class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    public static int Test(int b, int c)
    {
        int L = b + c;
        if (b == 1) goto T1;
        if (b == 2) goto T2;
        if (b == 3) goto T3;
        if (c == 1) goto T1;
        if (c == 2) goto T2;
        if (c == 3) goto T3;
        L = 7;
        goto T1;

    T1:
        if (L == 1) return 11;
        goto T2;
    T2:
        if (L == 2) return 22;
        goto T3;
    T3:
        if (L == 3) return 33;
        goto T1;
    }

    public static void Main()
    {
        Console.WriteLine("start");
        Console.WriteLine(Test(1, 0));
        Console.WriteLine("done");
    }
}

Expected

start
11
done

Actual

start
<hangs forever, 100% of one core, memory grows without bound>

Measured on a Checked x64 build:

STILL RUNNING after 90s. PID=123416 CPU=89.73  WS=44684.6MB
After 5 more s:             CPU=94.72  WS=45296.5MB

With JitDump, the last phase repeats the same tail-duplication cycle indefinitely:

fgOptimizeUncondBranchToSimpleCond(from BB07 to cond BB12), modified BB07
fgOptimizeUncondBranchToSimpleCond(from BB07 to cond BB08), modified BB07
fgOptimizeUncondBranchToSimpleCond(from BB07 to cond BB10), modified BB07

Notes

Compiler::fgUpdateFlowGraph() (fgopt.cpp) has no duplication budget or visited (source,target) set; fgBlockIsGoodTailDuplicationCandidate() rejects direct self-edges but not multi-block conditional cycles.
fgFoldSimpleCondByForwardSub() folds the cloned test while leaving the constant store, so the source block remains a tail-dup candidate for the next edge.
Also reproduces on released .NET 10.0.12 x64. A bounded budget or per-invocation duplicate-pair tracking would break the cycle.

Activity

  1. added this to the 12.0.0 milestone on Sep 15, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 15, 2026
  3. dotnet-policy-service commented on Sep 15, 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 16, 2026
  5. changed the title [-]JIT (bug): Tail duplication in fgUpdateFlowGraph does not terminate on a conditional cycle[/-] [+]JIT: (bug) Tail duplication in fgUpdateFlowGraph does not terminate on a conditional cycle[/+] on Sep 16, 2026
  6. added a commit that references this issue on Sep 17, 2026
    6056b18
  7. locked and limited conversation to collaborators on Oct 18, 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