diff --git a/Src/ILGPU.Algorithms/Optimization/CPU/SGOOptimizer.cs b/Src/ILGPU.Algorithms/Optimization/CPU/SGOOptimizer.cs index b83a082fd..9857b8e67 100644 --- a/Src/ILGPU.Algorithms/Optimization/CPU/SGOOptimizer.cs +++ b/Src/ILGPU.Algorithms/Optimization/CPU/SGOOptimizer.cs @@ -14,6 +14,7 @@ using System.Diagnostics.CodeAnalysis; using System.Numerics; using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; using System.Threading; #if NET7_0_OR_GREATER @@ -478,7 +479,7 @@ private unsafe Span GetPosition(int playerIndex) { ref var baseRef = ref positions.AsSpan().GetItemRef( playerIndex * NumPaddedDimensions); - return new Span(Unsafe.AsPointer(ref baseRef), NumPaddedDimensions); + return MemoryMarshal.CreateSpan(ref baseRef, NumPaddedDimensions); } /// @@ -493,7 +494,7 @@ private unsafe Span GetNextPosition(int playerIndex) { ref var baseRef = ref nextPositions.AsSpan().GetItemRef( playerIndex * NumPaddedDimensions); - return new Span(Unsafe.AsPointer(ref baseRef), NumPaddedDimensions); + return MemoryMarshal.CreateSpan(ref baseRef, NumPaddedDimensions); } /// diff --git a/Src/ILGPU.Algorithms/Vectors/VectorTypes.tt b/Src/ILGPU.Algorithms/Vectors/VectorTypes.tt index 260f07642..1deb9f2d7 100644 --- a/Src/ILGPU.Algorithms/Vectors/VectorTypes.tt +++ b/Src/ILGPU.Algorithms/Vectors/VectorTypes.tt @@ -465,9 +465,9 @@ namespace ILGPU.Algorithms.Vectors /// The readonly span instance. public unsafe ReadOnlySpan<<#= type.Type #>> AsSpan() => #if NET8_0_OR_GREATER - new(Unsafe.AsPointer(ref Unsafe.AsRef(in this)), <#= vectorLength #>); + MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(in this), <#= vectorLength #>); #else - new(Unsafe.AsPointer(ref Unsafe.AsRef(this)), <#= vectorLength #>); + MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(this), <#= vectorLength #>); #endif /// diff --git a/Src/ILGPU/Frontend/InvocationContext.cs b/Src/ILGPU/Frontend/InvocationContext.cs index a55ecb975..8a684f12e 100644 --- a/Src/ILGPU/Frontend/InvocationContext.cs +++ b/Src/ILGPU/Frontend/InvocationContext.cs @@ -16,7 +16,6 @@ using ILGPU.Util; using System; using System.Reflection; -using System.Runtime.CompilerServices; using System.Text; using ValueList = ILGPU.Util.InlineList; @@ -33,7 +32,7 @@ public unsafe ref struct InvocationContext /// /// The internal arguments pointer. /// - private readonly void* argumentsRef; + private readonly ref ValueList argumentsRef; /// /// Constructs a new invocation context. @@ -58,7 +57,7 @@ internal InvocationContext( CallerMethod = callerMethod; Method = method; - argumentsRef = Unsafe.AsPointer(ref arguments); + argumentsRef = ref arguments; } #endregion @@ -119,7 +118,7 @@ internal InvocationContext( /// Returns the call arguments. /// public readonly ref ValueList Arguments => - ref Unsafe.AsRef(argumentsRef); + ref argumentsRef; /// /// Returns the number of arguments. diff --git a/Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs b/Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs index ef32f407b..c694afe4f 100644 --- a/Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs +++ b/Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs @@ -72,12 +72,14 @@ public static unsafe void CPUCopyToCPU( ref byte sourcePtr, ref byte targetPtr, long sourceLengthInBytes, - long targetLengthInBytes) => - Buffer.MemoryCopy( - Unsafe.AsPointer(ref sourcePtr), - Unsafe.AsPointer(ref targetPtr), - sourceLengthInBytes, - targetLengthInBytes); + long targetLengthInBytes) + { + ArgumentOutOfRangeException.ThrowIfLessThan(targetLengthInBytes, sourceLengthInBytes); + Unsafe.CopyBlock( + ref targetPtr, + ref sourcePtr, + (uint)sourceLengthInBytes); + } /// /// Copies CPU data (target view) from the given source view. diff --git a/Src/ILGPU/Util/Vectors.cs b/Src/ILGPU/Util/Vectors.cs index 4becac721..098eea3ee 100644 --- a/Src/ILGPU/Util/Vectors.cs +++ b/Src/ILGPU/Util/Vectors.cs @@ -31,10 +31,7 @@ public static class Vectors public static unsafe Vector LoadAlignedVectorUnsafe( this ReadOnlySpan source) where T : struct - { - void* sourcePtr = Unsafe.AsPointer(ref MemoryMarshal.GetReference(source)); - return Unsafe.Read>(sourcePtr); - } + => Unsafe.As>(ref MemoryMarshal.GetReference(source)); /// /// Loads a vector (unsafe) from the given span while assuming proper alignment. @@ -58,9 +55,6 @@ public static unsafe void StoreAlignedVectorUnsafe( this Vector value, Span target) where T : struct - { - void* targetPtr = Unsafe.AsPointer(ref MemoryMarshal.GetReference(target)); - Unsafe.Write(targetPtr, value); - } + => Unsafe.As>(ref MemoryMarshal.GetReference(target)) = value; } -} \ No newline at end of file +}