Skip to content

JIT: (bug) Vector128/256.WithElement throws ArgumentOutOfRangeException before evaluating its value argument #134196

Closed

Description

@EgorBo

The JIT expansion of WithElement emits the index range check ahead of the value argument, so when the index is out of range the value expression's side effects are dropped entirely — violating C# left-to-right argument evaluation.

Minimal Repro

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

public static class Program
{
    [MethodImpl(MethodImplOptions.NoInlining)]
    static float Value() { Console.WriteLine("Value() ran"); return 42.0f; }

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static Vector128<float> Test(Vector128<float> v, int idx) => v.WithElement(idx, Value());

    static void Main()
    {
        try { Test(Vector128<float>.Zero, 99); }
        catch (Exception e) { Console.WriteLine(e.GetType().Name); }
    }
}

Expected

Value() ran
ArgumentOutOfRangeException

Actual

ArgumentOutOfRangeException

Notes

Correct output with DOTNET_EnableHWIntrinsic=0 (managed fallback). Also repros on released .NET 10.0.12.
Not float- or width-specific: Vector256<float> and Vector128<int> behave the same, and a null receiver in the value argument (v.WithElement(idx, h.F)) yields ArgumentOutOfRangeException instead of NullReferenceException.
Compiler::gtNewSimdWithElementNode (gentree.cpp) calls addRangeCheckForHWIntrinsic on op2, rewriting the index into COMMA(BOUNDS_CHECK, index), without first spilling op3 to a temp; the check is above the per-target #if, so all targets should be affected.

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. 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
  6. locked and limited conversation to collaborators on Oct 25, 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