Repository navigation
Description
FlowGraphNaturalLoop::FindConstInit (and HasZeroTripTest) walk GetUniquePred backwards from the loop preheader. For a loop in a catch handler the unique predecessor of the handler entry is the filter's BBJ_EHFILTERRET block, so a constant stored in the filter is used as the loop's initial IV value. The second-pass finally that runs between the filter and the handler is not visible on that edge, so the loop is fully unrolled with the wrong init value: wrong code in plain safe C#.
Minimal Repro
using System;
using System.Runtime.CompilerServices;
class Program
{
static int s_sum;
static void Main()
{
Test(1);
Console.WriteLine(s_sum);
}
[MethodImpl(MethodImplOptions.NoInlining)]
static void MayThrow(int k)
{
if (k != 0) throw new Exception();
}
[MethodImpl(MethodImplOptions.NoInlining)]
static bool Yes(int x) => x == 0;
[MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
static void Test(int k)
{
int i = 100;
try
{
try
{
MayThrow(k);
}
finally
{
i = 10;
}
}
catch when (Yes(i = 0))
{
do
{
s_sum += i;
i++;
} while (i < 3);
}
}
}Expected
10: the filter runs first (i = 0), then the inner finally runs during the second pass (i = 10), then the catch loop runs once with i = 10. This is also what a Checked JIT prints with DOTNET_JitNoUnroll=1.
Actual
3 on Checked and Release JIT (win-x64, main a5d7014cefc). The catch loop is fully unrolled as 0+1+2, using the init 0 from the filter. JitDump of the Checked JIT (Unroll loops): Const init with value 0, Computed loop repetition count (number of test block executions) to be 3.
Regression?
Yes. Same repro, default settings:
| Runtime | Result |
|---|---|
| .NET 8 (8.0.34) | OK (10) |
| .NET 9 (9.0.23) | OK |
| .NET 10 (10.0.15) | OK |
.NET 11 (11.0.0) / main (a5d7014cefc) |
3 |
Notes
FindConstInit/HasZeroTripTest(flowgraph.cpp) walkGetUniquePredbackwards from the loop preheader; from the catch entry the unique predecessor is the filter'sBBJ_EHFILTERRET, so the filter'si = 0is taken as the init.- The real control flow goes through the second-pass
finally, which has no flow edge, so facts from the filter are not valid at handler entry. - The same walk is also wrong when it crosses the entry of a
finallythat can be entered both normally and exceptionally. If such afinallyis not cloned (for example because it contains a denseswitch, whichfgCloneFinallyrefuses) it has a single explicit flow predecessor, the normal-pathBBJ_CALLFINALLY, and a constant assigned in thetryafter the last throwing call is used as the init of a loop in thefinallyeven when thefinallyruns after an exception while the variable still has its old value (observed:3instead of10). A fix has to cover both entries. - Possible fix: stop the
GetUniquePredwalk at EH handler / filter entry blocks. - Different from JIT: (bug) Loop cloning trusts a stale zero-trip guard after the IV is redefined #134007 / JIT: (bug) Loop cloning drops bounds checks when the limit local is redefined between the zero-trip guard and the loop preheader #133762 (stale zero-trip guard after the IV or limit is redefined on a normal path).
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: exclude EH handler/filter entry blocks from GetUniquePred (dotne…