-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Convert MemoryMarshal.GetArrayDataReference to a JIT intrinsic #79760
Convert MemoryMarshal.GetArrayDataReference to a JIT intrinsic #79760
Conversation
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch, @kunalspathak Issue DetailsConverts MemoryMarshal.GetArrayDataReference to Introduces JIT tests validating the correct behaviour. Fixes invalid codegen samples from:
|
Just reiterating what I said in the last PR... We've got known aliasing bugs around using 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. |
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 |
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: dotnet#58312 (comment)
This reverts commit 7af6e18.
47d576e
to
4157675
Compare
There was a problem hiding this 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!
...tiveaot/System.Private.CoreLib/src/System/Runtime/InteropServices/MemoryMarshal.NativeAot.cs
Show resolved
Hide resolved
@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. |
LGTM |
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)