diff --git a/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs b/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs
index afedebb6..31f9094a 100644
--- a/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs
+++ b/src/ComputeSharp.Shaders/Extensions/ILGeneratorExtensions.cs
@@ -1,4 +1,5 @@
-using System.Runtime.InteropServices;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
namespace System.Reflection.Emit
{
@@ -85,9 +86,17 @@ namespace System.Reflection.Emit
///
/// Puts the appropriate , and instructions to advance a reference onto the stream of instructions
///
+ /// The type of reference at the top of the stack
/// The input instance to use to emit instructions
/// The offset to use to advance the current reference on top of the execution stack
- public static void EmitAddByteOffset(this ILGenerator il, int offset)
+ public static void EmitAddOffset(this ILGenerator il, int offset) => il.EmitAddOffset(Unsafe.SizeOf() * offset);
+
+ ///
+ /// Puts the appropriate , and instructions to advance a reference onto the stream of instructions
+ ///
+ /// The input instance to use to emit instructions
+ /// The offset in bytes to use to advance the current reference on top of the execution stack
+ public static void EmitAddOffset(this ILGenerator il, int offset)
{
// Push the offset to the stack
if (offset > 8) il.Emit(OpCodes.Ldc_I4, offset);
@@ -115,19 +124,23 @@ namespace System.Reflection.Emit
/// The type of value being written to the current reference on top of the execution stack
public static void EmitStoreToAddress(this ILGenerator il, Type type)
{
- il.Emit(Marshal.SizeOf(type) switch
+ if (type.IsValueType)
{
- // Use the faster op codes for sizes <= 8
- 1 => OpCodes.Stind_I1,
- 2 => OpCodes.Stind_I2,
- 4 when type == typeof(float) => OpCodes.Stind_R4,
- 4 => OpCodes.Stind_I4,
- 8 when type == typeof(double) => OpCodes.Stind_R8,
- 8 when type == typeof(long) || type == typeof(ulong) => OpCodes.Stind_I8,
+ il.Emit(Marshal.SizeOf(type) switch
+ {
+ // Use the faster op codes for sizes <= 8
+ 1 => OpCodes.Stind_I1,
+ 2 => OpCodes.Stind_I2,
+ 4 when type == typeof(float) => OpCodes.Stind_R4,
+ 4 => OpCodes.Stind_I4,
+ 8 when type == typeof(double) => OpCodes.Stind_R8,
+ 8 when type == typeof(long) || type == typeof(ulong) => OpCodes.Stind_I8,
- // Default to stobj for all other value types
- _ => OpCodes.Stobj
- });
+ // Default to stobj for all other value types
+ _ => OpCodes.Stobj
+ });
+ }
+ else il.Emit(OpCodes.Stind_Ref);
}
}
}