Skip to content

JIT: (bug) signed Log2 intrinsic drops ArgumentOutOfRangeException when the result is unused #133965

Closed

Description

@EgorBo

For signed int.Log2 and long.Log2, the imported throwing fallback can be deleted when the intrinsic result is dead, so negative inputs return normally instead of throwing.

Minimal Repro

using System;
using System.Runtime.CompilerServices;

public class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    public static int Neg() => -1;

    [MethodImpl(MethodImplOptions.NoInlining)]
    public static long NegL() => -1;

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    public static void Test(int x)
    {
        int unused = int.Log2(x);
    }

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    public static void TestLong(long x)
    {
        long unused = long.Log2(x);
    }

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    public static int TestUsed(int x)
    {
        return int.Log2(x);
    }

    public static void Main()
    {
        try { Test(Neg()); Console.WriteLine("Test: No exception"); }
        catch (ArgumentOutOfRangeException) { Console.WriteLine("Test: ArgumentOutOfRangeException"); }

        try { TestLong(NegL()); Console.WriteLine("TestLong: No exception"); }
        catch (ArgumentOutOfRangeException) { Console.WriteLine("TestLong: ArgumentOutOfRangeException"); }

        try { TestUsed(Neg()); Console.WriteLine("TestUsed: No exception"); }
        catch (ArgumentOutOfRangeException) { Console.WriteLine("TestUsed: ArgumentOutOfRangeException"); }
    }
}

Expected

Test: ArgumentOutOfRangeException
TestLong: ArgumentOutOfRangeException
TestUsed: ArgumentOutOfRangeException

Actual

Test: No exception
TestLong: No exception
TestUsed: ArgumentOutOfRangeException

Notes

The signed Log2 fallback is a GT_INTRINSIC with GTF_CALL, but GT_INTRINSIC is not treated as throwing/side-effecting by the relevant side-effect checks.
Early liveness therefore deletes the dead store containing the fallback; Test and TestLong become a bare ret.
This is a regression in main; .NET 10 throws as expected.

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. EgorBo commented on Sep 18, 2026

    @EgorBo
    MemberAuthor

    Affected SDK versions:

    • 11.0.100-rc.2.26467.112
  5. self-assigned this
    on Oct 9, 2026
  6. locked and limited conversation to collaborators on Nov 10, 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