Repository navigation
Description
Folding a switch on a constant in fgFoldConditional mis-updates profile weights when one of the cases jumps back to the switch block (case 0: continue; in while (true)): removed targets keep a non-zero weight and the retained target gets more weight than its incoming edge. Block-weight bookkeeping only; codegen and output (6) are correct.
Minimal Repro
using System;
using System.Runtime.CompilerServices;
class Program
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
static int Sel() => 2;
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
static int Test(int n)
{
while (true)
{
switch (Sel())
{
case 0: continue; // switch block jumps to itself
case 1: return n;
case 2: return n + 1;
case 3: return n + 2;
default: return 3;
}
}
}
static void Main() => Console.WriteLine(Test(5));
}Run with a Checked JIT and DOTNET_JitDump=Test; look at the block table after Finishing PHASE Morph - Global.
Expected
Retained block has the weight of its only incoming edge, removed targets have weight 0:
BB01 [0000] 1 BB07 1 [000..01C)-> BB04(1) (always)
BB03 [0002] 0 0 (throw )
BB04 [0003] 1 BB01 1 (return)
BB05 [0004] 0 0 (throw )
BB06 [0005] 0 0 (throw )
Actual
BB03 [0002] 0 0.05 4 [01E..020) (throw )
BB04 [0003] 1 BB01 1.05 84 [020..024) (return)
BB05 [0004] 0 0.05 4 [024..028) (throw )
BB06 [0005] 0 0.05 4 [028..02A) (throw )
Checking Profile Weights (flags:0x1c)
BB04 - block weight 84 inconsistent with incoming likely weight 80
Profile is NOT self-consistent, found 2 problems
Without the self-edge (case 0: return 0;) the weights are exact.
Regression?
Only observable with a Checked JIT, so release runtimes (8.0.31, 9.0.20, 10.0.12, 11.0.0-rc.2) could not be checked for the weights (output and codegen are correct on all). Reproduces on main (7bbe3a812f7), which includes the switch-fold profile rework from #134974.
Notes
- In the
BBJ_SWITCHcase offgFoldConditional, the first loop callstargetBlock->decreaseBBProfileWeight(succEdge->getLikelyWeight()), andgetLikelyWeight()islikelihood * block->bbWeight, evaluated on every iteration. - For the self-edge,
decreaseBBProfileWeightlowersblock->bbWeightitself (1.25 -> 1.0), so later edges subtract 0.2 instead of 0.25 (leaving 0.05 on each removed target), and the finalincreaseBBProfileWeight(block->bbWeight)uses the already reduced weight. - Likely fix: snapshot the source weight before the loop.
Activity
dotnet-policy-service commentedon Nov 16, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch
See info in area-owners.md if you want to be subscribed.
JIT: Fix profile weights when removing a block's self-edge
JIT: fix profile weights when folding switches with self-edges (dotne…