Skip to content

Conversation

@MichalPetryka
Copy link
Contributor

Converts MemoryMarshal.GetArrayDataReference to
an always expand JIT intrinsic and removes the VM intrinsics.

Introduces JIT tests validating the correct behaviour.

Fixes invalid codegen samples from:
#58312 (comment)

@ghost ghost added area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI community-contribution Indicates that the PR has been added by a community member labels Dec 16, 2022
@ghost
Copy link

ghost commented Dec 16, 2022

Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch, @kunalspathak
See info in area-owners.md if you want to be subscribed.

Issue Details

Converts MemoryMarshal.GetArrayDataReference to
an always expand JIT intrinsic and removes the VM intrinsics.

Introduces JIT tests validating the correct behaviour.

Fixes invalid codegen samples from:
#58312 (comment)

Author: MichalPetryka
Assignees: -
Labels:

area-CodeGen-coreclr

Milestone: -

@MichalPetryka
Copy link
Contributor Author

@tannergooding
Copy link
Member

Just reiterating what I said in the last PR...

We've got known aliasing bugs around using GetArrayDataReference and so even if this causes some small regressions, it resolves real world bugs caused by us relying on the UB of aliasing reference types.

Ideally, we'd take this as is now and then separately look at improving the codegen to bring us back to the current numbers before NET 8 ships.

@tannergooding
Copy link
Member

Another example of where this is problematic is:

// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
//

using System;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

internal partial class VectorTest
{
    private static void Main()
    {
        int[] inputArray = CreateArray(17);

        for (int i = 0; i < Vector<int>.Count; i++)
        {
            Console.WriteLine(inputArray[i]);
        }

        Create(out Vector<int> v, inputArray, inputArray.Length);
    }

    public static void Create(out Vector<int> result, int[] values, int index)
    {
        if ((index < 0) || ((values.Length - index) < Vector<int>.Count))
        {
            ThrowArgumentOutOfRangeException();
        }

        result = Unsafe.ReadUnaligned<Vector<int>>(ref Unsafe.As<int, byte>(ref Unsafe.Add(ref MemoryMarshal.GetArrayDataReference(values), index)));
    }

    public static void ThrowArgumentOutOfRangeException()
    {
        throw new ArgumentOutOfRangeException();
    }

    [MethodImpl(MethodImplOptions.NoInlining)]
    public static int[] CreateArray(int size) => new int[size];
}

Where the JIT inlines Create(out Vector v, inputArray, inputArray.Length); and believes that inputArray must be null, so it internally asserts and leads to bad codegen in release builds.

This can impact actual usage of some of the Vector64/128/256<T>, Vector<T>, and Vector2/3/4 APIs.

@MichalPetryka MichalPetryka force-pushed the getarraydatareference-intrinsic branch from 47d576e to 4157675 Compare December 25, 2022 21:56
Copy link
Member

@MichalStrehovsky MichalStrehovsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The AOT changes look good, thanks!

@tannergooding
Copy link
Member

@jkotas, before this gets merged, just wanted to ensure there no concerns on your end about the minor regressions this will introduce in codegen.

We'll of course have the rest of .NET 8 to work towards resolving them, but given we keep encountering edge case bugs it seems like a good idea to take it as is for now to resolve the UB.

@jkotas
Copy link
Member

jkotas commented Jan 6, 2023

LGTM

@tannergooding tannergooding merged commit 312dbe4 into dotnet:main Jan 6, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Feb 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-CodeGen-coreclr CLR JIT compiler in src/coreclr/src/jit and related components such as SuperPMI community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants