From b6e67793a6b6843e4e9f59383a4d5f7696e686a7 Mon Sep 17 00:00:00 2001 From: MoFtZ Date: Sat, 2 Dec 2023 02:01:54 +1100 Subject: [PATCH] Fixed marshalling ArrayView.Empty as kernel parameter. (#1131) --- Src/ILGPU.Tests/MemoryBufferOperations.tt | 21 ++++++++++++++++++++ Src/ILGPU/Backends/PointerViews/Utilities.cs | 2 +- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/Src/ILGPU.Tests/MemoryBufferOperations.tt b/Src/ILGPU.Tests/MemoryBufferOperations.tt index da355499e..a01634a19 100644 --- a/Src/ILGPU.Tests/MemoryBufferOperations.tt +++ b/Src/ILGPU.Tests/MemoryBufferOperations.tt @@ -109,6 +109,27 @@ namespace ILGPU.Tests { output[index] = input[index]; } + + internal static void EmptyView_Kernel( + Index1D index, + ArrayView1D output) + where T : unmanaged + { + if (index < output.Length) + output[index] = default(T); + } + +<# foreach (var type in copyTypes) { #> + [Fact] + [KernelMethod(nameof(EmptyView_Kernel))] + public void EmptyView_<#= type.Name #>() + { + Execute>( + 42, + ArrayView1D<<#= type.Type #>, Stride1D.Dense>.Empty); + } + +<# } #> internal static void ZeroLength_Kernel( Index1D index, diff --git a/Src/ILGPU/Backends/PointerViews/Utilities.cs b/Src/ILGPU/Backends/PointerViews/Utilities.cs index 370fae61a..79eb0957e 100644 --- a/Src/ILGPU/Backends/PointerViews/Utilities.cs +++ b/Src/ILGPU/Backends/PointerViews/Utilities.cs @@ -92,7 +92,7 @@ public static FieldInfo GetLengthField(Type implType) => /// The underlying native pointer. private static IntPtr GetNativePtr(in ArrayView view) where T : unmanaged => - view.Buffer.NativePtr; + view.Buffer?.NativePtr ?? IntPtr.Zero; /// /// Gets the native-pointer method for the given element type.