Skip to content
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

Remove some Gc holes #1296

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 @@
/// <summary>
/// The internal arguments pointer.
/// </summary>
private readonly void* argumentsRef;
private readonly ref ValueList argumentsRef;

Check failure on line 35 in Src/ILGPU/Frontend/InvocationContext.cs

View workflow job for this annotation

GitHub Actions / test-library (ubuntu-latest, ILGPU.Analyzers, net6.0, CPU)

Target runtime doesn't support ref fields.

Check failure on line 35 in Src/ILGPU/Frontend/InvocationContext.cs

View workflow job for this annotation

GitHub Actions / test-library (ubuntu-latest, ILGPU, net6.0, CPU)

Target runtime doesn't support ref fields.

Check failure on line 35 in Src/ILGPU/Frontend/InvocationContext.cs

View workflow job for this annotation

GitHub Actions / test-library (ubuntu-latest, ILGPU, net6.0, Velocity)

Target runtime doesn't support ref fields.

Check failure on line 35 in Src/ILGPU/Frontend/InvocationContext.cs

View workflow job for this annotation

GitHub Actions / test-library (ubuntu-latest, ILGPU, net6.0, Velocity128)

Target runtime doesn't support ref fields.

Check failure on line 35 in Src/ILGPU/Frontend/InvocationContext.cs

View workflow job for this annotation

GitHub Actions / test-library (ubuntu-latest, ILGPU.Algorithms, net6.0, CPU)

Target runtime doesn't support ref fields.

Check failure on line 35 in Src/ILGPU/Frontend/InvocationContext.cs

View workflow job for this annotation

GitHub Actions / test-library (windows-latest, ILGPU, net6.0, Velocity128)

Target runtime doesn't support ref fields.

Check failure on line 35 in Src/ILGPU/Frontend/InvocationContext.cs

View workflow job for this annotation

GitHub Actions / test-library (windows-latest, ILGPU.Algorithms, net6.0, CPU)

Target runtime doesn't support ref fields.

Check failure on line 35 in Src/ILGPU/Frontend/InvocationContext.cs

View workflow job for this annotation

GitHub Actions / test-library (windows-latest, ILGPU, net6.0, Velocity)

Target runtime doesn't support ref fields.

Check failure on line 35 in Src/ILGPU/Frontend/InvocationContext.cs

View workflow job for this annotation

GitHub Actions / test-library (windows-latest, ILGPU.Analyzers, net6.0, CPU)

Target runtime doesn't support ref fields.

Check failure on line 35 in Src/ILGPU/Frontend/InvocationContext.cs

View workflow job for this annotation

GitHub Actions / build (Src)

Target runtime doesn't support ref fields.

Check failure on line 35 in Src/ILGPU/Frontend/InvocationContext.cs

View workflow job for this annotation

GitHub Actions / test-library (windows-latest, ILGPU, net6.0, CPU)

Target runtime doesn't support ref fields.

Check failure on line 35 in Src/ILGPU/Frontend/InvocationContext.cs

View workflow job for this annotation

GitHub Actions / build (Samples)

Target runtime doesn't support ref fields.

Check failure on line 35 in Src/ILGPU/Frontend/InvocationContext.cs

View workflow job for this annotation

GitHub Actions / Analyze (Samples/ILGPU.Samples.sln, net6.0)

Target runtime doesn't support ref fields.

Check failure on line 35 in Src/ILGPU/Frontend/InvocationContext.cs

View workflow job for this annotation

GitHub Actions / Analyze (Samples/ILGPU.Samples.sln, net6.0)

Target runtime doesn't support ref fields.

Check failure on line 35 in Src/ILGPU/Frontend/InvocationContext.cs

View workflow job for this annotation

GitHub Actions / package-library

Target runtime doesn't support ref fields.

Check failure on line 35 in Src/ILGPU/Frontend/InvocationContext.cs

View workflow job for this annotation

GitHub Actions / Analyze (Src/ILGPU.sln, net6.0)

Target runtime doesn't support ref fields.

Check failure on line 35 in Src/ILGPU/Frontend/InvocationContext.cs

View workflow job for this annotation

GitHub Actions / Analyze (Src/ILGPU.sln, net6.0)

Target runtime doesn't support ref fields.

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

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

#endregion
Expand Down Expand Up @@ -119,7 +118,7 @@
/// 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 @@
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);

Check failure on line 77 in Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs

View workflow job for this annotation

GitHub Actions / test-library (ubuntu-latest, ILGPU, net7.0, Velocity)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThan'

Check failure on line 77 in Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs

View workflow job for this annotation

GitHub Actions / test-library (ubuntu-latest, ILGPU, net7.0, Velocity128)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThan'

Check failure on line 77 in Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs

View workflow job for this annotation

GitHub Actions / test-library (ubuntu-latest, ILGPU.Algorithms, net7.0, CPU)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThan'

Check failure on line 77 in Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs

View workflow job for this annotation

GitHub Actions / test-library (ubuntu-latest, ILGPU.Analyzers, net7.0, CPU)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThan'

Check failure on line 77 in Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs

View workflow job for this annotation

GitHub Actions / test-library (ubuntu-latest, ILGPU, net7.0, CPU)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThan'

Check failure on line 77 in Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs

View workflow job for this annotation

GitHub Actions / test-library (windows-latest, ILGPU, net7.0, Velocity128)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThan'

Check failure on line 77 in Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs

View workflow job for this annotation

GitHub Actions / test-library (windows-latest, ILGPU.Algorithms, net7.0, CPU)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThan'

Check failure on line 77 in Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs

View workflow job for this annotation

GitHub Actions / build (Src)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThan'

Check failure on line 77 in Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs

View workflow job for this annotation

GitHub Actions / test-library (windows-latest, ILGPU, net7.0, Velocity)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThan'

Check failure on line 77 in Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs

View workflow job for this annotation

GitHub Actions / test-library (windows-latest, ILGPU, net7.0, CPU)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThan'

Check failure on line 77 in Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs

View workflow job for this annotation

GitHub Actions / build (Samples)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThan'

Check failure on line 77 in Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs

View workflow job for this annotation

GitHub Actions / test-library (windows-latest, ILGPU.Analyzers, net7.0, CPU)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThan'

Check failure on line 77 in Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs

View workflow job for this annotation

GitHub Actions / package-library

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThan'

Check failure on line 77 in Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs

View workflow job for this annotation

GitHub Actions / Analyze (Src/ILGPU.sln, net7.0)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThan'

Check failure on line 77 in Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs

View workflow job for this annotation

GitHub Actions / Analyze (Src/ILGPU.sln, net7.0)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThan'

Check failure on line 77 in Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs

View workflow job for this annotation

GitHub Actions / Analyze (Samples/ILGPU.Samples.sln, net7.0)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThan'

Check failure on line 77 in Src/ILGPU/Runtime/CPU/CPUMemoryBuffer.cs

View workflow job for this annotation

GitHub Actions / Analyze (Samples/ILGPU.Samples.sln, net7.0)

'ArgumentOutOfRangeException' does not contain a definition for 'ThrowIfLessThan'
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;
}
}
}
Loading