Skip to content

JIT: (bug) fgFoldConditional leaves inconsistent block weights when a folded switch case targets the switch block itself #137642

Closed

Description

@EgorBo

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_SWITCH case of fgFoldConditional, the first loop calls targetBlock->decreaseBBProfileWeight(succEdge->getLikelyWeight()), and getLikelyWeight() is likelihood * block->bbWeight, evaluated on every iteration.
  • For the self-edge, decreaseBBProfileWeight lowers block->bbWeight itself (1.25 -> 1.0), so later edges subtract 0.2 instead of 0.25 (leaving 0.05 on each removed target), and the final increaseBBProfileWeight(block->bbWeight) uses the already reduced weight.
  • Likely fix: snapshot the source weight before the loop.

Activity

  1. added this to the 12.0.0 milestone on Nov 16, 2026
  2. added
    area-CodeGen-coreclr
    on Nov 16, 2026
  3. dotnet-policy-service commented on Nov 16, 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. added a commit that references this issue on Nov 16, 2026
    643cff9
  5. self-assigned this
    on Nov 16, 2026
  6. added a commit that references this issue on Nov 17, 2026
    ad132e1
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