Repository navigation
Open
Labels
api-approvedAPI was approved in API review, it can be implementedarea-System.Runtime.Intrinsicsreduce-unsafe
Milestone
Description
Background and motivation
#125145 Annotate All Vector-related APIs as caller-unsafe because all of these either:
- Perform memory accesses without bound checks
- May return uninitialized memory in their components.
I'm using a diff format where + indicates that it's now caller-unsafe and - indicates the unsafe modifier has been removed (a non-breaking change).
API Proposal
namespace System.Runtime.Intrinsics
namespace System.Runtime.Intrinsics
public static partial class Vector64
{
+ /// <safety>Initializes only the lowest element and leaves the remaining elements uninitialized; reading them observes undefined data.</safety>
+ unsafe
public static Vector64<byte> CreateScalarUnsafe(byte value)
+ unsafe
public static Vector64<double> CreateScalarUnsafe(double value)
+ unsafe
public static Vector64<short> CreateScalarUnsafe(short value)
+ unsafe
public static Vector64<int> CreateScalarUnsafe(int value)
+ unsafe
public static Vector64<long> CreateScalarUnsafe(long value)
+ unsafe
public static Vector64<nint> CreateScalarUnsafe(nint value)
+ unsafe
public static Vector64<nuint> CreateScalarUnsafe(nuint value)
+ unsafe
public static Vector64<sbyte> CreateScalarUnsafe(sbyte value)
+ unsafe
public static Vector64<float> CreateScalarUnsafe(float value)
+ unsafe
public static Vector64<ushort> CreateScalarUnsafe(ushort value)
+ unsafe
public static Vector64<uint> CreateScalarUnsafe(uint value)
+ unsafe
public static Vector64<ulong> CreateScalarUnsafe(ulong value)
+ unsafe
public static Vector64<T> CreateScalarUnsafe<T>(T value)
+ /// <safety>Loads a full vector starting at the given reference with no bounds check; may read past the end of the source.</safety>
+ unsafe
public static Vector64<T> LoadUnsafe<T>(ref readonly T source)
+ unsafe
public static Vector64<T> LoadUnsafe<T>(ref readonly T source, nuint elementOffset)
+ /// <safety>Stores a full vector starting at the given reference with no bounds check; may write past the end of the destination.</safety>
+ unsafe
public static void StoreUnsafe<T>(this Vector64<T> source, ref T destination)
+ unsafe
public static void StoreUnsafe<T>(this Vector64<T> source, ref T destination, nuint elementOffset)
+ /// <safety>Widens to a larger vector, leaving the upper elements uninitialized.</safety>
+ unsafe
public static Vector128<T> ToVector128Unsafe<T>(this Vector64<T> vector)
}
public static partial class Vector128
{
+ /// <safety>Reinterprets the value as a 128-bit vector, leaving the upper elements uninitialized.</safety>
+ unsafe
public static Vector128<float> AsVector128Unsafe(this System.Numerics.Vector2 value)
+ unsafe
public static Vector128<float> AsVector128Unsafe(this System.Numerics.Vector3 value)
+ /// <safety>Initializes only the lowest element and leaves the remaining elements uninitialized; reading them observes undefined data.</safety>
+ unsafe
public static Vector128<byte> CreateScalarUnsafe(byte value)
+ unsafe
public static Vector128<double> CreateScalarUnsafe(double value)
+ unsafe
public static Vector128<short> CreateScalarUnsafe(short value)
+ unsafe
public static Vector128<int> CreateScalarUnsafe(int value)
+ unsafe
public static Vector128<long> CreateScalarUnsafe(long value)
+ unsafe
public static Vector128<nint> CreateScalarUnsafe(nint value)
+ unsafe
public static Vector128<nuint> CreateScalarUnsafe(nuint value)
+ unsafe
public static Vector128<sbyte> CreateScalarUnsafe(sbyte value)
+ unsafe
public static Vector128<float> CreateScalarUnsafe(float value)
+ unsafe
public static Vector128<ushort> CreateScalarUnsafe(ushort value)
+ unsafe
public static Vector128<uint> CreateScalarUnsafe(uint value)
+ unsafe
public static Vector128<ulong> CreateScalarUnsafe(ulong value)
+ unsafe
public static Vector128<T> CreateScalarUnsafe<T>(T value)
+ /// <safety>Loads a full vector starting at the given reference with no bounds check; may read past the end of the source.</safety>
+ unsafe
public static Vector128<T> LoadUnsafe<T>(ref readonly T source)
+ unsafe
public static Vector128<T> LoadUnsafe<T>(ref readonly T source, nuint elementOffset)
+ /// <safety>Stores a full vector starting at the given reference with no bounds check; may write past the end of the destination.</safety>
+ unsafe
public static void StoreUnsafe<T>(this Vector128<T> source, ref T destination)
+ unsafe
public static void StoreUnsafe<T>(this Vector128<T> source, ref T destination, nuint elementOffset)
+ /// <safety>Widens to a larger vector, leaving the upper elements uninitialized.</safety>
+ unsafe
public static Vector256<T> ToVector256Unsafe<T>(this Vector128<T> vector)
}
public static partial class Vector256
{
+ /// <safety>Initializes only the lowest element and leaves the remaining elements uninitialized; reading them observes undefined data.</safety>
+ unsafe
public static Vector256<byte> CreateScalarUnsafe(byte value)
+ unsafe
public static Vector256<double> CreateScalarUnsafe(double value)
+ unsafe
public static Vector256<short> CreateScalarUnsafe(short value)
+ unsafe
public static Vector256<int> CreateScalarUnsafe(int value)
+ unsafe
public static Vector256<long> CreateScalarUnsafe(long value)
+ unsafe
public static Vector256<nint> CreateScalarUnsafe(nint value)
+ unsafe
public static Vector256<nuint> CreateScalarUnsafe(nuint value)
+ unsafe
public static Vector256<sbyte> CreateScalarUnsafe(sbyte value)
+ unsafe
public static Vector256<float> CreateScalarUnsafe(float value)
+ unsafe
public static Vector256<ushort> CreateScalarUnsafe(ushort value)
+ unsafe
public static Vector256<uint> CreateScalarUnsafe(uint value)
+ unsafe
public static Vector256<ulong> CreateScalarUnsafe(ulong value)
+ unsafe
public static Vector256<T> CreateScalarUnsafe<T>(T value)
+ /// <safety>Loads a full vector starting at the given reference with no bounds check; may read past the end of the source.</safety>
+ unsafe
public static Vector256<T> LoadUnsafe<T>(ref readonly T source)
+ unsafe
public static Vector256<T> LoadUnsafe<T>(ref readonly T source, nuint elementOffset)
+ /// <safety>Stores a full vector starting at the given reference with no bounds check; may write past the end of the destination.</safety>
+ unsafe
public static void StoreUnsafe<T>(this Vector256<T> source, ref T destination)
+ unsafe
public static void StoreUnsafe<T>(this Vector256<T> source, ref T destination, nuint elementOffset)
+ /// <safety>Widens to a larger vector, leaving the upper elements uninitialized.</safety>
+ unsafe
public static Vector512<T> ToVector512Unsafe<T>(this Vector256<T> vector)
}
public static partial class Vector512
{
+ /// <safety>Initializes only the lowest element and leaves the remaining elements uninitialized; reading them observes undefined data.</safety>
+ unsafe
public static Vector512<byte> CreateScalarUnsafe(byte value)
+ unsafe
public static Vector512<double> CreateScalarUnsafe(double value)
+ unsafe
public static Vector512<short> CreateScalarUnsafe(short value)
+ unsafe
public static Vector512<int> CreateScalarUnsafe(int value)
+ unsafe
public static Vector512<long> CreateScalarUnsafe(long value)
+ unsafe
public static Vector512<nint> CreateScalarUnsafe(nint value)
+ unsafe
public static Vector512<nuint> CreateScalarUnsafe(nuint value)
+ unsafe
public static Vector512<sbyte> CreateScalarUnsafe(sbyte value)
+ unsafe
public static Vector512<float> CreateScalarUnsafe(float value)
+ unsafe
public static Vector512<ushort> CreateScalarUnsafe(ushort value)
+ unsafe
public static Vector512<uint> CreateScalarUnsafe(uint value)
+ unsafe
public static Vector512<ulong> CreateScalarUnsafe(ulong value)
+ unsafe
public static Vector512<T> CreateScalarUnsafe<T>(T value)
+ /// <safety>Loads a full vector starting at the given reference with no bounds check; may read past the end of the source.</safety>
+ unsafe
public static Vector512<T> LoadUnsafe<T>(ref readonly T source)
+ unsafe
public static Vector512<T> LoadUnsafe<T>(ref readonly T source, nuint elementOffset)
+ /// <safety>Stores a full vector starting at the given reference with no bounds check; may write past the end of the destination.</safety>
+ unsafe
public static void StoreUnsafe<T>(this Vector512<T> source, ref T destination)
+ unsafe
public static void StoreUnsafe<T>(this Vector512<T> source, ref T destination, nuint elementOffset)
}namespace System.Numerics
namespace System.Numerics
public static partial class Vector
{
+ /// <safety>Widens to a larger vector, leaving the upper elements uninitialized.</safety>
+ unsafe
public static Vector3 AsVector3Unsafe(this Vector2 value)
+ unsafe
public static Vector4 AsVector4Unsafe(this Vector2 value)
+ unsafe
public static Vector4 AsVector4Unsafe(this Vector3 value)
+ /// <safety>Initializes only the lowest element and leaves the remaining elements uninitialized; reading them observes undefined data.</safety>
+ unsafe
public static Vector<T> CreateScalarUnsafe<T>(T value)
+ /// <safety>Loads a full vector starting at the given reference with no bounds check; may read past the end of the source.</safety>
+ unsafe
public static Vector<T> LoadUnsafe<T>(ref readonly T source)
+ unsafe
public static Vector<T> LoadUnsafe<T>(ref readonly T source, nuint elementOffset)
+ /// <safety>Stores a full vector starting at the given reference with no bounds check; may write past the end of the destination.</safety>
+ unsafe
public static void StoreUnsafe<T>(this Vector<T> source, ref T destination)
+ unsafe
public static void StoreUnsafe(this Vector2 source, ref float destination)
+ unsafe
public static void StoreUnsafe(this Vector3 source, ref float destination)
+ unsafe
public static void StoreUnsafe(this Vector4 source, ref float destination)
+ unsafe
public static void StoreUnsafe<T>(this Vector<T> source, ref T destination, nuint elementOffset)
+ unsafe
public static void StoreUnsafe(this Vector2 source, ref float destination, nuint elementOffset)
+ unsafe
public static void StoreUnsafe(this Vector3 source, ref float destination, nuint elementOffset)
+ unsafe
public static void StoreUnsafe(this Vector4 source, ref float destination, nuint elementOffset)
}
public partial struct Vector2
{
+ /// <safety>Initializes only the lowest element and leaves the remaining elements uninitialized; reading them observes undefined data.</safety>
+ unsafe
public static Vector2 CreateScalarUnsafe(float x)
+ /// <safety>Loads a full vector starting at the given reference with no bounds check; may read past the end of the source.</safety>
+ unsafe
public static Vector2 LoadUnsafe(ref readonly float source)
+ unsafe
public static Vector2 LoadUnsafe(ref readonly float source, nuint elementOffset)
}
public partial struct Vector3
{
+ /// <safety>Initializes only the lowest element and leaves the remaining elements uninitialized; reading them observes undefined data.</safety>
+ unsafe
public static Vector3 CreateScalarUnsafe(float x)
+ /// <safety>Loads a full vector starting at the given reference with no bounds check; may read past the end of the source.</safety>
+ unsafe
public static Vector3 LoadUnsafe(ref readonly float source)
+ unsafe
public static Vector3 LoadUnsafe(ref readonly float source, nuint elementOffset)
}
public partial struct Vector4
{
+ /// <safety>Initializes only the lowest element and leaves the remaining elements uninitialized; reading them observes undefined data.</safety>
+ unsafe
public static Vector4 CreateScalarUnsafe(float x)
+ /// <safety>Loads a full vector starting at the given reference with no bounds check; may read past the end of the source.</safety>
+ unsafe
public static Vector4 LoadUnsafe(ref readonly float source)
+ unsafe
public static Vector4 LoadUnsafe(ref readonly float source, nuint elementOffset)
}API Usage
All these apis now require an unsafe context to call them.
Alternative Designs
No response
Risks
No response
Metadata
Metadata
Assignees
Labels
api-approvedAPI was approved in API review, it can be implementedarea-System.Runtime.Intrinsicsreduce-unsafe
Activity
dotnet-policy-service commentedon May 12, 2026
Tagging subscribers to this area: @dotnet/area-system-runtime-intrinsics
See info in area-owners.md if you want to be subscribed.
EgorBo commentedon May 12, 2026
NOTE: it doesn't include APIs in direct intrinsics, those will be annotated separately. E.g.:
namespace System.Runtime.Intrinsics.Arm public abstract partial class Sve : AdvSimd { + public static unsafe void GatherPrefetch16Bit(Vector<short> mask, Vector<ulong> addresses, [ConstantExpected] SvePrefetchType prefetchType)bartonjs commentedon Jul 30, 2026
Video
Looks good as proposed
namespace System.Runtime.Intrinsics public static partial class Vector64 { + /// <safety>Initializes only the lowest element and leaves the remaining elements uninitialized; reading them observes undefined data.</safety> + unsafe public static Vector64<byte> CreateScalarUnsafe(byte value) + unsafe public static Vector64<double> CreateScalarUnsafe(double value) + unsafe public static Vector64<short> CreateScalarUnsafe(short value) + unsafe public static Vector64<int> CreateScalarUnsafe(int value) + unsafe public static Vector64<long> CreateScalarUnsafe(long value) + unsafe public static Vector64<nint> CreateScalarUnsafe(nint value) + unsafe public static Vector64<nuint> CreateScalarUnsafe(nuint value) + unsafe public static Vector64<sbyte> CreateScalarUnsafe(sbyte value) + unsafe public static Vector64<float> CreateScalarUnsafe(float value) + unsafe public static Vector64<ushort> CreateScalarUnsafe(ushort value) + unsafe public static Vector64<uint> CreateScalarUnsafe(uint value) + unsafe public static Vector64<ulong> CreateScalarUnsafe(ulong value) + unsafe public static Vector64<T> CreateScalarUnsafe<T>(T value) + /// <safety>Loads a full vector starting at the given reference with no bounds check; may read past the end of the source.</safety> + unsafe public static Vector64<T> LoadUnsafe<T>(ref readonly T source) + unsafe public static Vector64<T> LoadUnsafe<T>(ref readonly T source, nuint elementOffset) + /// <safety>Stores a full vector starting at the given reference with no bounds check; may write past the end of the destination.</safety> + unsafe public static void StoreUnsafe<T>(this Vector64<T> source, ref T destination) + unsafe public static void StoreUnsafe<T>(this Vector64<T> source, ref T destination, nuint elementOffset) + /// <safety>Widens to a larger vector, leaving the upper elements uninitialized.</safety> + unsafe public static Vector128<T> ToVector128Unsafe<T>(this Vector64<T> vector) } public static partial class Vector128 { + /// <safety>Reinterprets the value as a 128-bit vector, leaving the upper elements uninitialized.</safety> + unsafe public static Vector128<float> AsVector128Unsafe(this System.Numerics.Vector2 value) + unsafe public static Vector128<float> AsVector128Unsafe(this System.Numerics.Vector3 value) + /// <safety>Initializes only the lowest element and leaves the remaining elements uninitialized; reading them observes undefined data.</safety> + unsafe public static Vector128<byte> CreateScalarUnsafe(byte value) + unsafe public static Vector128<double> CreateScalarUnsafe(double value) + unsafe public static Vector128<short> CreateScalarUnsafe(short value) + unsafe public static Vector128<int> CreateScalarUnsafe(int value) + unsafe public static Vector128<long> CreateScalarUnsafe(long value) + unsafe public static Vector128<nint> CreateScalarUnsafe(nint value) + unsafe public static Vector128<nuint> CreateScalarUnsafe(nuint value) + unsafe public static Vector128<sbyte> CreateScalarUnsafe(sbyte value) + unsafe public static Vector128<float> CreateScalarUnsafe(float value) + unsafe public static Vector128<ushort> CreateScalarUnsafe(ushort value) + unsafe public static Vector128<uint> CreateScalarUnsafe(uint value) + unsafe public static Vector128<ulong> CreateScalarUnsafe(ulong value) + unsafe public static Vector128<T> CreateScalarUnsafe<T>(T value) + /// <safety>Loads a full vector starting at the given reference with no bounds check; may read past the end of the source.</safety> + unsafe public static Vector128<T> LoadUnsafe<T>(ref readonly T source) + unsafe public static Vector128<T> LoadUnsafe<T>(ref readonly T source, nuint elementOffset) + /// <safety>Stores a full vector starting at the given reference with no bounds check; may write past the end of the destination.</safety> + unsafe public static void StoreUnsafe<T>(this Vector128<T> source, ref T destination) + unsafe public static void StoreUnsafe<T>(this Vector128<T> source, ref T destination, nuint elementOffset) + /// <safety>Widens to a larger vector, leaving the upper elements uninitialized.</safety> + unsafe public static Vector256<T> ToVector256Unsafe<T>(this Vector128<T> vector) } public static partial class Vector256 { + /// <safety>Initializes only the lowest element and leaves the remaining elements uninitialized; reading them observes undefined data.</safety> + unsafe public static Vector256<byte> CreateScalarUnsafe(byte value) + unsafe public static Vector256<double> CreateScalarUnsafe(double value) + unsafe public static Vector256<short> CreateScalarUnsafe(short value) + unsafe public static Vector256<int> CreateScalarUnsafe(int value) + unsafe public static Vector256<long> CreateScalarUnsafe(long value) + unsafe public static Vector256<nint> CreateScalarUnsafe(nint value) + unsafe public static Vector256<nuint> CreateScalarUnsafe(nuint value) + unsafe public static Vector256<sbyte> CreateScalarUnsafe(sbyte value) + unsafe public static Vector256<float> CreateScalarUnsafe(float value) + unsafe public static Vector256<ushort> CreateScalarUnsafe(ushort value) + unsafe public static Vector256<uint> CreateScalarUnsafe(uint value) + unsafe public static Vector256<ulong> CreateScalarUnsafe(ulong value) + unsafe public static Vector256<T> CreateScalarUnsafe<T>(T value) + /// <safety>Loads a full vector starting at the given reference with no bounds check; may read past the end of the source.</safety> + unsafe public static Vector256<T> LoadUnsafe<T>(ref readonly T source) + unsafe public static Vector256<T> LoadUnsafe<T>(ref readonly T source, nuint elementOffset) + /// <safety>Stores a full vector starting at the given reference with no bounds check; may write past the end of the destination.</safety> + unsafe public static void StoreUnsafe<T>(this Vector256<T> source, ref T destination) + unsafe public static void StoreUnsafe<T>(this Vector256<T> source, ref T destination, nuint elementOffset) + /// <safety>Widens to a larger vector, leaving the upper elements uninitialized.</safety> + unsafe public static Vector512<T> ToVector512Unsafe<T>(this Vector256<T> vector) } public static partial class Vector512 { + /// <safety>Initializes only the lowest element and leaves the remaining elements uninitialized; reading them observes undefined data.</safety> + unsafe public static Vector512<byte> CreateScalarUnsafe(byte value) + unsafe public static Vector512<double> CreateScalarUnsafe(double value) + unsafe public static Vector512<short> CreateScalarUnsafe(short value) + unsafe public static Vector512<int> CreateScalarUnsafe(int value) + unsafe public static Vector512<long> CreateScalarUnsafe(long value) + unsafe public static Vector512<nint> CreateScalarUnsafe(nint value) + unsafe public static Vector512<nuint> CreateScalarUnsafe(nuint value) + unsafe public static Vector512<sbyte> CreateScalarUnsafe(sbyte value) + unsafe public static Vector512<float> CreateScalarUnsafe(float value) + unsafe public static Vector512<ushort> CreateScalarUnsafe(ushort value) + unsafe public static Vector512<uint> CreateScalarUnsafe(uint value) + unsafe public static Vector512<ulong> CreateScalarUnsafe(ulong value) + unsafe public static Vector512<T> CreateScalarUnsafe<T>(T value) + /// <safety>Loads a full vector starting at the given reference with no bounds check; may read past the end of the source.</safety> + unsafe public static Vector512<T> LoadUnsafe<T>(ref readonly T source) + unsafe public static Vector512<T> LoadUnsafe<T>(ref readonly T source, nuint elementOffset) + /// <safety>Stores a full vector starting at the given reference with no bounds check; may write past the end of the destination.</safety> + unsafe public static void StoreUnsafe<T>(this Vector512<T> source, ref T destination) + unsafe public static void StoreUnsafe<T>(this Vector512<T> source, ref T destination, nuint elementOffset) }namespace System.Numerics public static partial class Vector { + /// <safety>Widens to a larger vector, leaving the upper elements uninitialized.</safety> + unsafe public static Vector3 AsVector3Unsafe(this Vector2 value) + unsafe public static Vector4 AsVector4Unsafe(this Vector2 value) + unsafe public static Vector4 AsVector4Unsafe(this Vector3 value) + /// <safety>Initializes only the lowest element and leaves the remaining elements uninitialized; reading them observes undefined data.</safety> + unsafe public static Vector<T> CreateScalarUnsafe<T>(T value) + /// <safety>Loads a full vector starting at the given reference with no bounds check; may read past the end of the source.</safety> + unsafe public static Vector<T> LoadUnsafe<T>(ref readonly T source) + unsafe public static Vector<T> LoadUnsafe<T>(ref readonly T source, nuint elementOffset) + /// <safety>Stores a full vector starting at the given reference with no bounds check; may write past the end of the destination.</safety> + unsafe public static void StoreUnsafe<T>(this Vector<T> source, ref T destination) + unsafe public static void StoreUnsafe(this Vector2 source, ref float destination) + unsafe public static void StoreUnsafe(this Vector3 source, ref float destination) + unsafe public static void StoreUnsafe(this Vector4 source, ref float destination) + unsafe public static void StoreUnsafe<T>(this Vector<T> source, ref T destination, nuint elementOffset) + unsafe public static void StoreUnsafe(this Vector2 source, ref float destination, nuint elementOffset) + unsafe public static void StoreUnsafe(this Vector3 source, ref float destination, nuint elementOffset) + unsafe public static void StoreUnsafe(this Vector4 source, ref float destination, nuint elementOffset) } public partial struct Vector2 { + /// <safety>Initializes only the lowest element and leaves the remaining elements uninitialized; reading them observes undefined data.</safety> + unsafe public static Vector2 CreateScalarUnsafe(float x) + /// <safety>Loads a full vector starting at the given reference with no bounds check; may read past the end of the source.</safety> + unsafe public static Vector2 LoadUnsafe(ref readonly float source) + unsafe public static Vector2 LoadUnsafe(ref readonly float source, nuint elementOffset) } public partial struct Vector3 { + /// <safety>Initializes only the lowest element and leaves the remaining elements uninitialized; reading them observes undefined data.</safety> + unsafe public static Vector3 CreateScalarUnsafe(float x) + /// <safety>Loads a full vector starting at the given reference with no bounds check; may read past the end of the source.</safety> + unsafe public static Vector3 LoadUnsafe(ref readonly float source) + unsafe public static Vector3 LoadUnsafe(ref readonly float source, nuint elementOffset) } public partial struct Vector4 { + /// <safety>Initializes only the lowest element and leaves the remaining elements uninitialized; reading them observes undefined data.</safety> + unsafe public static Vector4 CreateScalarUnsafe(float x) + /// <safety>Loads a full vector starting at the given reference with no bounds check; may read past the end of the source.</safety> + unsafe public static Vector4 LoadUnsafe(ref readonly float source) + unsafe public static Vector4 LoadUnsafe(ref readonly float source, nuint elementOffset) }Compile ref-assemblies with updated-memory-safety-rules (#131733)
jeffhandley commentedon Sep 9, 2026
@EgorBo I moved this to .NET 12. Please pull back in to .NET 11 if that's still the target.