Skip to content

JIT: (bug) SIMD GetElement(int) range check is reordered before an earlier-evaluated "abc"[i] bounds check in call arguments #137637

Closed

Description

@EgorBo

In Callee("abc"[i], v.GetElement(j) * ...) the string index must throw IndexOutOfRangeException first when both i and j are out of range. The JIT reorders the arguments so Vector128<int>.GetElement(j) throws ArgumentOutOfRangeException first, violating C# left-to-right evaluation order.

Minimal Repro

using System;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;

class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static void Callee(char c, int e) { }

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static void Test(int i, int j, int a)
    {
        Vector128<int> v = Vector128.Create(a, a + 1, a + 2, a + 3);
        Callee("abc"[i], v.GetElement(j) * a + v.GetElement(1) * 5 + v.GetElement(2) * j);
    }

    static void Main()
    {
        try
        {
            Test(100, 100, 7);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.GetType().Name);
        }
    }
}

Expected

IndexOutOfRangeException

Actual

ArgumentOutOfRangeException

The GetElement(j) check comes before the "abc"[i] check:

       cmp      edx, 4                      ; v.GetElement(j)
       jae      SHORT G_M51568_IG05         ; -> CORINFO_HELP_THROW_ARGUMENTOUTOFRANGEEXCEPTION
       ...
       cmp      ecx, 3                      ; "abc"[i]
       jae      SHORT G_M51568_IG04         ; -> CORINFO_HELP_RNGCHKFAIL

Regression?

No, reproduces on every runtime checked: 8.0.31, 9.0.20, 10.0.12, 10.0.15, 11.0.0-rc.2, 11.0.0 and main (7bbe3a812f7). .NET 7 and older were not checked.

Notes

  • GenTree::OperExceptions (case GT_BOUNDS_CHECK, gentree.cpp) returns IndexOutOfRangeException for every bounds check, including SIMD GetElement ones with gtThrowKind == SCK_ARG_RNG_EXCPN, which actually throw ArgumentOutOfRangeException.
  • CallArgs::ArgsComplete (morph.cpp) then sees both args as throwing the same single exception and lets cost-based SortArgs reorder them (inferred from source, not confirmed with a JitDump).
  • Possible fix: return a distinct exception flag for SCK_ARG_RNG_EXCPN bounds checks.

Activity

  1. added this to the 12.0.0 milestone on Nov 16, 2026
  2. added
    area-CodeGen-coreclr
    on Nov 16, 2026
  3. dotnet-policy-service commented on Nov 16, 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 Nov 17, 2026
  5. added a commit that references this issue on Nov 17, 2026
    a134a71
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