Skip to content

JIT: (bug) Operand reordering ignores GTF_ORDER_SIDEEFF on the second operand, moving a "proven safe" divide above its proof #134191

Closed

Description

@EgorBo

gtCanSwapOrder only rejects GTF_ORDER_SIDEEFF on the first node, so a divide that assertion prop proved cannot divide by zero gets commuted above the checked add that establishes the proof, and the wrong exception is thrown.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

class Program
{
    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static int Test(int p, int q, int y, int z, int w, int u, int v)
    {
        return (checked(p + q) / y) + ((((z * w) + (u * v)) & 0x7FFFFF) / y);
    }

    static void Main()
    {
        try
        {
            Console.WriteLine(Test(int.MaxValue, 1, 0, 3, 5, 7, 9));
        }
        catch (Exception e)
        {
            Console.WriteLine(e.GetType().Name);
        }
    }
}

Expected

OverflowException

Actual

DivideByZeroException

Notes

Regression on main: released .NET 10.0.12 throws OverflowException, and so does an unoptimized (tier-0) compile of the same source on main. No stress config or env vars required.
Also repros on much simpler shapes with DOTNET_JitStressModeNames=STRESS_REVERSE_FLAG.
Assertion prop proves y != 0 from the first divide, sets GTF_DIV_MOD_NO_BY_ZERO + GTF_ORDER_SIDEEFF and clears GTF_EXCEPT on the second divide; gtCanSwapOrder (gentree.cpp) then lets gtSetEvalOrder commute the ADD.

Activity

  1. added this to the 12.0.0 milestone on Sep 18, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 18, 2026
  3. dotnet-policy-service commented on Sep 18, 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. MihuBot commented on Sep 18, 2026

    @MihuBot

    I'm a bot. Here is a possible related and/or duplicate issue (I may be wrong):

  5. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 11.0.100-rc.2.26467.112
  6. self-assigned this
    on Oct 13, 2026
  7. added a commit that references this issue on Oct 14, 2026
    9a19078
  8. locked and limited conversation to collaborators on Nov 14, 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