Skip to content

JIT: (bug) signed int.Log2(x) expansion throws before earlier operands are evaluated (wrong exception order, skipped side effect) #135816

Closed

Description

@EgorBo

impPrimitiveNamedIntrinsic expands signed Log2 into a throwing QMARK stored as a separate statement ahead of still-unspilled earlier operands, so Side() + int.Log2(-1) never calls Side() and a[i] + int.Log2(-1) throws ArgumentOutOfRangeException instead of IndexOutOfRangeException.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

public static class Program
{
    public static int Main()
    {
        try { Console.WriteLine(Test1(-1)); }
        catch (Exception e) { Console.WriteLine(e.GetType().Name); }

        try { Console.WriteLine(Test2(new int[2], 5, -1)); }
        catch (Exception e) { Console.WriteLine(e.GetType().Name); }
        return 100;
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    static int Side()
    {
        Console.WriteLine("Side");
        return 1;
    }

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    public static int Test1(int x) => Side() + int.Log2(x);

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    public static int Test2(int[] a, int i, int x) => a[i] + int.Log2(x);
}

Expected

Side
ArgumentOutOfRangeException
IndexOutOfRangeException

Actual

ArgumentOutOfRangeException
ArgumentOutOfRangeException

Side() is never called; the Log2 range check (test ecx, ecx / jl) is emitted before the Side() call / the bounds check. Same with DOTNET_JITMinOpts=1.

Regression?

Default env, Release JIT, same source per TFM:

  • .NET 8.0.32 / 9.0.21 / 10.0.13: expected output
  • .NET 11.0.0-rc.1.26425.128 and main (Checked): reproduces

Notes

  • Case NI_PRIMITIVE_Log2 in impPrimitiveNamedIntrinsic: impCloneExpr(op1, ..., CHECK_SPILL_ALL) doesn't spill anything for a plain local, then impStoreToTemp(tmp, qmark, CHECK_SPILL_NONE) appends the store before the unspilled stack entries (Side() call / a[i] load).
  • The qmark has GTF_CALL | GTF_EXCEPT, so it needs CHECK_SPILL_ALL (or an explicit spill of side-effecting stack entries). Only the negative-argument path is affected.

Activity

  1. added this to the 12.0.0 milestone on Oct 14, 2026
  2. added
    area-CodeGen-coreclr
    on Oct 14, 2026
  3. dotnet-policy-service commented on Oct 14, 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. self-assigned this
    on Oct 15, 2026
  5. added a commit that references this issue on Oct 15, 2026
    05d39ab
  6. locked and limited conversation to collaborators on Nov 15, 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