Skip to content

Commit

Permalink
Remove some Gc holes
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Oct 30, 2024
1 parent c3af8f3 commit caf8cf3
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 23 deletions.
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;

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 @@ 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);

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);
}

/// <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;
}
}
}

0 comments on commit caf8cf3

Please sign in to comment.