Repository navigation
Closed
Closed
Copy link
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Milestone
Description
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=1runs fine.-c Releaseis fine (Roslyn folds the two labels into a single
self-jump, which the existing guard handles). Leaving the assembly markedDisableOptimizationsis
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--).
fgReplaceJumpTargetonly rewrites the incoming switch edge, neverA -> BorB -> A, so the
retry makes no progress. The siblingCompiler::fgOptimizeBranchToEmptyUnconditionaldoes a
slow/fast pointer walk to detect exactly this; the switch path has no equivalent.
Metadata
Metadata
Assignees
Labels
area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
Activity
dotnet-policy-service commentedon Sep 11, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
[-]JIT: hang in fgOptimizeSwitchBranches on a multi-block empty branch cycle[/-][+]JIT: (bug) hang in fgOptimizeSwitchBranches on a multi-block empty branch cycle[/+]JIT: Fix hang when a switch targets an empty-block cycle (#133686)
JIT: Fix hang when a switch targets an empty-block cycle (dotnet#133686)