Skip to content

JIT: (bug) dominator-based jump threading in redundantbranchopts drops a mandatory DivideByZeroException #133982

Closed

Description

@EgorBo

optRedundantBranch -> optJumpThreadDom can thread flow around a BBJ_COND block whose JTRUE tree has a GTF_EXCEPT side effect that the dominating compare does not cover, orphaning the exception-raising IR. A mandatory DivideByZeroException is silently dropped.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Test(int a, int b, int c)
    {
        int r;
        if (a == b) { r = 1; } else { r = 2; }
        if (a == b + (a / c - a / c)) { r += 10; } else { r += 20; }
        return r;
    }

    static int Main()
    {
        try
        {
            Console.WriteLine("No exception, result=" + Test(5, 5, 0));
            return 1;
        }
        catch (DivideByZeroException)
        {
            Console.WriteLine("DivideByZeroException");
            return 100;
        }
    }
}

Expected

DivideByZeroException
exit=100

This is what MinOpts produces.

Actual

No exception, result=11
exit=1

The division is gone from the codegen entirely and V02 arg2 (c) is zero-ref.

Notes

Deterministic; reproduces with default tiering as well as DOTNET_TieredCompilation=0. Also reproduces on released .NET 10.0.12, so it is not a main-only regression.

Root cause: the two relops are matched on their normal VNs only (both sides go through VNUnpackExc), so the dominated relop's exception set can be a strict superset of the dominating one's. optJumpThreadCheck nonetheless grants a blanket exemption to a GTF_EXCEPT-only JTRUE tree, commenting that the exceptions "are covered by the exception effects in the dominating compare... because the VNs match". The non-threading branch-folding path in the same function gets this right by testing vnStore->VNExcIsSubset(domCmpExcVN, treeExcVN) first; the jump-threading path should do the same (or bail out).

Activity

  1. added this to the 12.0.0 milestone on Sep 15, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 15, 2026
  3. dotnet-policy-service commented on Sep 15, 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 (bug): dominator-based jump threading in redundantbranchopts drops a mandatory DivideByZeroException[/-] [+]JIT: (bug) dominator-based jump threading in redundantbranchopts drops a mandatory DivideByZeroException[/+] on Sep 16, 2026
  5. self-assigned this
    on Sep 17, 2026
  6. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 8.0.425
    • 9.0.318
    • 10.0.401
    • 11.0.100-rc.2.26467.112
  7. added a commit that references this issue on Sep 21, 2026
    9351053
  8. locked and limited conversation to collaborators on Oct 22, 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