Skip to content

JIT: (bug) hang in fgOptimizeSwitchBranches on a multi-block empty branch cycle #133677

Closed

Description

@EgorBo

A switch case targeting A -> B -> A, where A and B are both empty BBJ_ALWAYS blocks, makes
fgOptimizeSwitchBranches redirect the edge back and forth indefinitely. The JIT never returns; the
process spins at 100% CPU and the method is never compiled, even though the loop is never executed.

Repro

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

// Declared explicitly so csc does NOT synthesize DebuggableAttribute(DisableOptimizations)
// for the Debug build - the runtime then JITs this unoptimized IL with full optimizations.
[assembly: Debuggable(DebuggableAttribute.DebuggingModes.IgnoreSymbolStoreSequencePoints)]

public static class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    public static int Candidate(int selector)
    {
        switch (selector)   // 3 arms => Roslyn emits an IL 'switch' jump table
        {
            case 0: goto A;
            case 1: goto B;
            case 2: goto A;
            default: return 7;
        }
    A:
        goto B;             // unoptimized IL: "nop; br B" => empty BBJ_ALWAYS after import
    B:
        goto A;             // unoptimized IL: "nop; br A" => empty BBJ_ALWAYS after import
    }

    public static void Main()
    {
        Console.WriteLine("before");
        Console.WriteLine(Candidate(99));   // default arm; the loop is never executed
        Console.WriteLine("after");
    }
}

Build with dotnet build -c Debug (unoptimized IL is required) and run with
DOTNET_TieredCompilation=0.

Expected

before
7
after

Actual

Prints before, then hangs forever at 100% CPU inside the JIT (44.8 s CPU in 45 s wall clock, killed).

DOTNET_JitDump=Candidate grows without bound — 156,899 lines in 20 s of:

Optimizing a switch jump to an empty block with an unconditional jump (BB01 -> BB07 -> BB08)
Optimizing a switch jump to an empty block with an unconditional jump (BB01 -> BB08 -> BB07)
Optimizing a switch jump to an empty block with an unconditional jump (BB01 -> BB07 -> BB08)
...

Notes

  • Windows x64, net11.0. Requires unoptimized IL + DOTNET_TieredCompilation=0.
    DOTNET_JITMinOpts=1 runs fine. -c Release is fine (Roslyn folds the two labels into a single
    self-jump, which the existing guard handles). Leaving the assembly marked DisableOptimizations is
    also fine, since the runtime then forces MinOpts.
  • Verified on a Checked build of 97ea9ba17a825a62c1b52a259999566eed748b79. The Checked JIT does not
    assert — it simply never returns.
  • Other producers of unoptimized-but-not-debuggable IL reach the same shape (IL rewriters, ILLink
    output, AOT/R2R compilation of <Optimize>false</Optimize> assemblies), and an AOT/R2R compiler
    running this optimizer would hang the same way.
  • Compiler::fgOptimizeSwitchBranches (src/coreclr/jit/fgopt.cpp) guards only against a one-block
    self loop (!bDest->TargetIs(bDest)) and then retries the same successor index (i--).
    fgReplaceJumpTarget only rewrites the incoming switch edge, never A -> B or B -> A, so the
    retry makes no progress. The sibling Compiler::fgOptimizeBranchToEmptyUnconditional does a
    slow/fast pointer walk to detect exactly this; the switch path has no equivalent.

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. changed the title [-]JIT: hang in fgOptimizeSwitchBranches on a multi-block empty branch cycle[/-] [+]JIT: (bug) hang in fgOptimizeSwitchBranches on a multi-block empty branch cycle[/+] on Sep 11, 2026
  5. self-assigned this
    on Sep 11, 2026
  6. added a commit that references this issue on Sep 14, 2026
    7ee2d1b
  7. added a commit that references this issue on Sep 18, 2026
    3354a52
  8. locked and limited conversation to collaborators 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