Skip to content

JIT: (bug) (-v1) - (-v2) vector rewrite reorders side effects (wrong exception) #134895

Closed

Description

@EgorBo

fgOptimizeHWIntrinsic rewrites (-v1) - (-v2) to v2 - v1 without checking whether the operands can be swapped, so side-effecting operands are evaluated in the wrong order.

Minimal Repro

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

public class Program
{
    public static void Main()
    {
        try
        {
            Test(new Vector128<int>[1], null, 5);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.GetType().Name);
        }
    }

    [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)]
    static Vector128<int> Test(Vector128<int>[] a, Vector128<int>[] b, int i) => -a[i] - -b[i];
}

Expected

IndexOutOfRangeException

Actual

NullReferenceException

Regression?

Yes. .NET 8 and 9 are correct; .NET 10, 11 RC2 and main are wrong.

Notes

The GT_SUB case ("(-v1) - (-v2) to v2 - v1") swaps Op(1)/Op(2) without a gtCanSwapOrder check, unlike the GT_ADD case above it. Introduced by #116892.

Activity

  1. added this to the 12.0.0 milestone on Sep 29, 2026
  2. added
    area-CodeGen-coreclr
    on Sep 29, 2026
  3. dotnet-policy-service commented on Sep 29, 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. added a commit that references this issue on Nov 14, 2026
    9419b06
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