Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions Src/ILGPU.Algorithms/Optimization/CPU/SGOOptimizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -478,7 +479,7 @@ private unsafe Span<T> GetPosition(int playerIndex)
{
ref var baseRef = ref positions.AsSpan().GetItemRef(
playerIndex * NumPaddedDimensions);
return new Span<T>(Unsafe.AsPointer(ref baseRef), NumPaddedDimensions);
return MemoryMarshal.CreateSpan(ref baseRef, NumPaddedDimensions);
}

/// <summary>
Expand All @@ -493,7 +494,7 @@ private unsafe Span<T> GetNextPosition(int playerIndex)
{
ref var baseRef = ref nextPositions.AsSpan().GetItemRef(
playerIndex * NumPaddedDimensions);
return new Span<T>(Unsafe.AsPointer(ref baseRef), NumPaddedDimensions);
return MemoryMarshal.CreateSpan(ref baseRef, NumPaddedDimensions);
}

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Src/ILGPU.Algorithms/Vectors/VectorTypes.tt
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,9 @@ namespace ILGPU.Algorithms.Vectors
/// <returns>The readonly span instance.</returns>
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

/// <summary>
Expand Down
7 changes: 3 additions & 4 deletions Src/ILGPU/Frontend/InvocationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using ILGPU.Util;
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Text;
using ValueList = ILGPU.Util.InlineList<ILGPU.IR.Values.ValueReference>;

Expand All @@ -33,7 +32,7 @@ public unsafe ref struct InvocationContext
/// <summary>
/// The internal arguments pointer.
/// </summary>
private readonly void* argumentsRef;
private readonly ref ValueList argumentsRef;

/// <summary>
/// Constructs a new invocation context.
Expand All @@ -58,7 +57,7 @@ internal InvocationContext(
CallerMethod = callerMethod;
Method = method;

argumentsRef = Unsafe.AsPointer(ref arguments);
argumentsRef = ref arguments;
}

#endregion
Expand Down Expand Up @@ -119,7 +118,7 @@ internal InvocationContext(
/// Returns the call arguments.
/// </summary>
public readonly ref ValueList Arguments =>
ref Unsafe.AsRef<ValueList>(argumentsRef);
ref argumentsRef;

/// <summary>
/// Returns the number of arguments.
Expand Down
14 changes: 8 additions & 6 deletions Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link

@neon-sunset neon-sunset Oct 30, 2024

Choose a reason for hiding this comment

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

Cast to uint here will truncate the chunk of memory being copied to 4GiB. Instead, this could be done with pinning both byrefs and calling Buffer.MemoryCopy which does the right thing. Happy to see more people discovering an amazing project that ILGPU is.

}

/// <summary>
/// Copies CPU data (target view) from the given source view.
Expand Down
12 changes: 3 additions & 9 deletions Src/ILGPU/Util/Vectors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ public static class Vectors
public static unsafe Vector<T> LoadAlignedVectorUnsafe<T>(
this ReadOnlySpan<T> source)
where T : struct
{
void* sourcePtr = Unsafe.AsPointer(ref MemoryMarshal.GetReference(source));
return Unsafe.Read<Vector<T>>(sourcePtr);
}
=> Unsafe.As<T, Vector<T>>(ref MemoryMarshal.GetReference(source));

/// <summary>
/// Loads a vector (unsafe) from the given span while assuming proper alignment.
Expand All @@ -58,9 +55,6 @@ public static unsafe void StoreAlignedVectorUnsafe<T>(
this Vector<T> value,
Span<T> target)
where T : struct
{
void* targetPtr = Unsafe.AsPointer(ref MemoryMarshal.GetReference(target));
Unsafe.Write(targetPtr, value);
}
=> Unsafe.As<T, Vector<T>>(ref MemoryMarshal.GetReference(target)) = value;
}
}
}