Skip to content

JIT: (bug) constant-NaN Math.Min/Max folding drops the other argument's side effects #133718

Description

@EgorBo
using System;
using System.Runtime.CompilerServices;

class P
{
    static double[] a = new double[1];

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static double Test(int i)
    {
        return Math.Min(a[i], double.NaN);
    }

    static void Main()
    {
        try
        {
            Console.WriteLine("NO EXCEPTION, result=" + Test(5));
        }
        catch (IndexOutOfRangeException)
        {
            Console.WriteLine("IndexOutOfRangeException (correct)");
        }
    }
}

Expected: IndexOutOfRangeException (correct) — a[5] on a length-1 array must throw before Math.Min returns.
Actual: NO EXCEPTION, result=NaN — the array load, including its bounds check, is discarded.

Notes:

  • run.ps1 -Cs repro.cs (csc -optimize+, Checked corerun x64). Compiling with -optimize- prints the correct IndexOutOfRangeException, so it is purely the optimized-JIT path.
  • src\coreclr\jit\gentree.cpp in gtNewSimdMinMaxNode: the isNaN shortcut does return cnsNode; (non-Number) / return otherNode; (Number) with no gtExtractSideEffList/COMMA for the discarded operand.
  • Only on the non-AVX10v2 xarch path (the AVX10v2 branch is taken earlier and is unaffected). Same shape works for MathF.Min, Math.Max, and the MinNumber/MaxNumber variants (drop the other operand there).

Activity

  1. added this to the 12.0.0 milestone on Sep 11, 2026
  2. added
    area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI
    on Sep 11, 2026
  3. dotnet-policy-service commented on Sep 11, 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. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 10.0.401
    • 11.0.100-rc.2.26467.112
  5. added a commit that references this issue on Sep 24, 2026
    2f56ca5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-CodeGen-coreclrCLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions